{"record":{"id":"fd1f5f12140ed824","repo":"JuliusBrussee/caveman","slug":"path-changed-during-mcp-update-refusing-remova","errorCode":null,"errorMessage":"${path} changed during MCP update; refusing removal","messagePattern":"(.+?) changed during MCP update; refusing removal","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/index.ts","lineNumber":6937,"sourceCode":"    durableAtomicWriteFile(temp, bytes, mode);\n    linkSync(temp, path);\n    fsyncParentDirectory(path);\n  } finally {\n    try { unlinkSync(temp); } catch { /* published or failed before temp creation */ }\n  }\n}\n\nfunction optionalBytesEqual(left: Buffer | null, right: Buffer | null): boolean {\n  return left === null ? right === null : right !== null && left.equals(right);\n}\n\nfunction durableReplaceFileIfUnchanged(path: string, expected: Buffer | null, next: Buffer | null, mode = 0o600): void {\n  const current = fileBytes(path);\n  if (!optionalBytesEqual(current, expected)) throw new Error(`${path} changed during MCP update; refusing overwrite`);\n  if (next === null) {\n    if (current !== null) {\n      const atDelete = fileBytes(path);\n      if (!optionalBytesEqual(atDelete, expected)) throw new Error(`${path} changed during MCP update; refusing removal`);\n      unlinkSync(path);\n      fsyncParentDirectory(path);\n    }\n    return;\n  }\n\n  // Prepare durable replacement first, then run final compare immediately\n  // before rename. Per-agent lock serializes Caveman writers; this CAS catches\n  // external edits observed before commit without erasing them.\n  mkdirSync(dirname(path), { recursive: true, mode: 0o700 });\n  const temp = join(dirname(path), `.${basename(path)}.caveman-cas-${process.pid}-${randomUUID()}.tmp`);\n  let fd: number | undefined;\n  try {\n    fd = openSync(temp, constants.O_CREAT | constants.O_EXCL | constants.O_WRONLY, mode);\n    writeFileSync(fd, next);\n    fchmodSync(fd, mode);\n    fsyncSync(fd);\n    closeSync(fd);","sourceCodeStart":6919,"sourceCodeEnd":6955,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/cli/src/index.ts#L6919-L6955","documentation":"Thrown when the CLI is removing an MCP config entry and re-reads the file immediately before unlinking, finding its bytes no longer match the expected snapshot. This second check guarantees the file was not modified between the initial comparison and its deletion.","triggerScenarios":"durableReplaceFileIfUnchanged called with next===null; after the first equality check passes, fileBytes(path) (atDelete) differs from `expected` right before unlinkSync.","commonSituations":"Another process rewrote or recreated the config file in the microseconds between checks — e.g. a watcher that regenerates config files on change, or concurrent CLI runs.","solutions":["Re-run the removal command when nothing else is watching/writing the config","Stop file watchers or generators (e.g. config sync daemons) that rewrite the file on change","Check whether another caveman instance is running and wait for it to finish","Retry the removal; verify the entry state afterwards"],"exampleFix":"// before\nconfig-watcher --watch ~/.codex & caveman mcp remove ...\n// after\npkill config-watcher && caveman mcp remove ...","handlingStrategy":"retry","validationCode":"// confirm the entry to remove still matches expectations before removal\nconst bytes = fs.existsSync(configPath) ? fs.readFileSync(configPath) : null;\n// compare bytes to your snapshot before calling the removal","typeGuard":"null","tryCatchPattern":"try {\n  runMcpRemove();\n} catch (e) {\n  if (String(e.message).includes(\"refusing removal\")) {\n    console.error(\"File changed mid-removal; nothing was deleted. Retry when the file is stable.\");\n  }\n}","preventionTips":["Stop config-watching daemons before removing entries","Run removals sequentially, not concurrently with other CLI instances","Verify expected file content matches your snapshot before deleting"],"tags":["concurrency","atomic-write","mcp","file-deletion"],"backgroundTag":"conflicting-config-options","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}