thedotmack/claude-mem · warning

Failed to clean AGENTS.md context

Error message

Failed to clean AGENTS.md context

What it means

While disabling the codex integration, claude-mem strips its managed <claude-mem-context>...</claude-mem-context> block from CODEX_AGENTS_MD_PATH. readAndStripContextTags reads the file and rewrites it without the block; any throw — read EACCES, write failure, or a locked file — surfaces as this warning and the function returns false, leaving the stale context block in the file.

Source

Thrown at src/services/integrations/CodexCliInstaller.ts:341

  }

  if (version.localeCompare(MIN_CODEX_MARKETPLACE_VERSION, undefined, { numeric: true }) < 0) {
    throw new Error(`Codex CLI ${version} is too old for plugin marketplace support. Update Codex CLI to ${MIN_CODEX_MARKETPLACE_VERSION} or newer, then run: npx claude-mem@latest install`);
  }
}

function removeCodexAgentsMdContext(): boolean {
  if (!existsSync(CODEX_AGENTS_MD_PATH)) return true;

  const startTag = '<claude-mem-context>';
  const endTag = '</claude-mem-context>';

  try {
    readAndStripContextTags(startTag, endTag);
    return true;
  } catch (error) {
    const message = error instanceof Error ? error.message : String(error);
    logger.warn('WORKER', 'Failed to clean AGENTS.md context', { error: message });
    return false;
  }
}

function readAndStripContextTags(startTag: string, endTag: string): void {
  const content = readFileSync(CODEX_AGENTS_MD_PATH, 'utf-8');

  const startIdx = content.indexOf(startTag);
  const endIdx = content.indexOf(endTag);

  if (startIdx === -1 || endIdx === -1) return;

  const before = content.substring(0, startIdx).replace(/\n+$/, '');
  const after = content.substring(endIdx + endTag.length).replace(/^\n+/, '');
  const finalContent = (before + (after ? '\n\n' + after : '')).trim();

  if (finalContent) {
    writeFileSync(CODEX_AGENTS_MD_PATH, finalContent + '\n');

View on GitHub (pinned to e2d1df569a)

Solutions

  1. Check permissions and ownership on the AGENTS.md file and its directory; fix ownership if a sudo run changed it.
  2. Close editors holding the file (or exclude it from antivirus locking) and retry the disable step.
  3. Verify no <claude-mem-context> block remains — an earlier partial run may have already stripped it.
  4. As a last resort, delete the managed block by hand and re-run to confirm a clean report.
Defensive patterns

Strategy: try-catch

Validate before calling

// confirm writable before the strip runs
import { accessSync, constants } from 'node:fs';
accessSync(CODEX_AGENTS_MD_PATH, constants.W_OK); // throws with a clear errno

Try / catch

try {
  removeCodexAgentsMdContext();
} catch (e) {
  // false return leaves the managed block: surface it instead of ignoring
  throw new Error(`AGENTS.md context strip failed: ${(e as Error).message}`);
}

Prevention

When it happens

Trigger: readFileSync or the rewrite inside readAndStripContextTags throws: AGENTS.md is read-only or owned by another user, an editor or antivirus holds an exclusive lock (Windows), the path is a symlink to nowhere, or the file is deleted between existsSync and read (race).

Common situations: AGENTS.md owned by root after a sudo run; file open in an editor with exclusive locking; repository permissions changed; the codex config directory reorganized between versions.

Related errors


AI-assisted analysis of thedotmack/claude-mem@e2d1df569a (2026-08-20). Data as JSON: /api/errors/388afc0e3ef3ff60. Report an issue: GitHub.