{"record":{"id":"ca5abca2c0996dc1","repo":"JuliusBrussee/caveman","slug":"path-changed-during-mcp-update-refusing-overwr","errorCode":null,"errorMessage":"${path} changed during MCP update; refusing overwrite","messagePattern":"(.+?) changed during MCP update; refusing overwrite","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/index.ts","lineNumber":6933,"sourceCode":"function durableCreateFile(path: string, bytes: Buffer, mode = 0o600): void {\n  mkdirSync(dirname(path), { recursive: true, mode: 0o700 });\n  const temp = join(dirname(path), `.${basename(path)}.caveman-create-${process.pid}-${randomUUID()}.tmp`);\n  try {\n    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);","sourceCodeStart":6915,"sourceCodeEnd":6951,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/cli/src/index.ts#L6915-L6951","documentation":"Thrown by durableReplaceFileIfUnchanged when the target file's current bytes differ from the snapshot (expected) taken at the start of an MCP config update. This optimistic-concurrency check ensures the CLI never clobbers concurrent edits to config files during an MCP server installation/update.","triggerScenarios":"Between reading the original bytes and the replace step, fileBytes(path) returns content different from `expected` — another process/user modified the file during the update.","commonSituations":"An editor, sync client (Dropbox/iCloud), or another agent run writing the same settings/mcp config while the CLI updates it; two setup commands run in parallel.","solutions":["Re-run the MCP update command alone, with no other process editing the config","Close editors or pause file-sync tools that touch the config directory","Avoid running concurrent `caveman setup`/MCP update commands in parallel","Inspect the file for unexpected changes, then retry"],"exampleFix":"// before (concurrent)\ncaveman setup --install & vim ~/.claude.json\n// after (sequential)\nvim ~/.claude.json && caveman setup --install","handlingStrategy":"retry","validationCode":"// before updating, ensure no concurrent writers\nconst before = fs.readFileSync(configPath);\nif (!fs.existsSync(lockPath)) fs.writeFileSync(lockPath, String(process.pid));","typeGuard":"null","tryCatchPattern":"try {\n  runMcpUpdate();\n} catch (e) {\n  if (String(e.message).includes(\"changed during MCP update\")) {\n    // safe: file was NOT overwritten; just retry\n    retryWithBackoff(runMcpUpdate);\n  }\n}","preventionTips":["Never run multiple setup/MCP update commands in parallel","Pause cloud-sync (Dropbox/iCloud) on config directories during updates","Close editors with autosave on agent config files before updating","Use a lock file or serialized script when automating updates"],"tags":["concurrency","atomic-write","mcp","file"],"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"}