a0dd516cd5
Systematic cleanup of every error handling anti-pattern detected by the automated scanner. 289 issues fixed via code changes, 12 approved with specific technical justifications. Changes across 90 files: - GENERIC_CATCH (141): Added instanceof Error type discrimination - LARGE_TRY_BLOCK (82): Extracted helper methods to narrow try scope to ≤10 lines - NO_LOGGING_IN_CATCH (65): Added logger/console calls for error visibility - CATCH_AND_CONTINUE_CRITICAL_PATH (10): Added throw/return or approved overrides - ERROR_STRING_MATCHING (2): Approved with rationale (no typed error classes) - ERROR_MESSAGE_GUESSING (1): Replaced chained .includes() with documented pattern array - PROMISE_CATCH_NO_LOGGING (1): Added logging to .catch() handler Also fixes a detector bug where nested try/catch inside a catch block corrupted brace-depth tracking, causing false positives. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
38 lines
1.4 KiB
TypeScript
38 lines
1.4 KiB
TypeScript
import { existsSync, readFileSync, writeFileSync, renameSync, mkdirSync } from 'fs';
|
|
import { dirname, resolve } from 'path';
|
|
import { replaceTaggedContent } from './claude-md-utils.js';
|
|
import { logger } from './logger.js';
|
|
|
|
/**
|
|
* Write AGENTS.md with claude-mem context, preserving user content outside tags.
|
|
* Uses atomic write to prevent partial writes.
|
|
*/
|
|
export function writeAgentsMd(agentsPath: string, context: string): void {
|
|
if (!agentsPath) return;
|
|
|
|
// Never write inside .git directories — corrupts refs (#1165)
|
|
const resolvedPath = resolve(agentsPath);
|
|
if (resolvedPath.includes('/.git/') || resolvedPath.includes('\\.git\\') || resolvedPath.endsWith('/.git') || resolvedPath.endsWith('\\.git')) return;
|
|
|
|
const dir = dirname(agentsPath);
|
|
if (!existsSync(dir)) {
|
|
mkdirSync(dir, { recursive: true });
|
|
}
|
|
|
|
let existingContent = '';
|
|
if (existsSync(agentsPath)) {
|
|
existingContent = readFileSync(agentsPath, 'utf-8');
|
|
}
|
|
|
|
const contentBlock = `# Memory Context\n\n${context}`;
|
|
const finalContent = replaceTaggedContent(existingContent, contentBlock);
|
|
const tempFile = `${agentsPath}.tmp`;
|
|
|
|
try {
|
|
writeFileSync(tempFile, finalContent);
|
|
renameSync(tempFile, agentsPath);
|
|
} catch (error: unknown) {
|
|
logger.error('AGENTS_MD', 'Failed to write AGENTS.md', { agentsPath }, error instanceof Error ? error : new Error(String(error)));
|
|
}
|
|
}
|