{"record":{"id":"bcaaea94f108f5bf","repo":"JuliusBrussee/caveman","slug":"agent-native-bundle-removal-failed-error-as-er-bcaaea","errorCode":null,"errorMessage":"agent-native bundle removal failed: ${(error as Error).message}; installed bundle restored","messagePattern":"agent-native bundle removal failed: (.+?); installed bundle restored","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/index.ts","lineNumber":2956,"sourceCode":"    if (nativeJournal) {\n      for (const operation of nativeJournal.operations) restoreNativeOperation(operation);\n    }\n  }\n  atomicWriteFile(agentNativeBundleRemovalJournalPath(agent), Buffer.from(JSON.stringify(journal, null, 2) + \"\\n\"));\n  try {\n    restoreAgentNativeBundleSkills(journal.skills);\n    restoreAgentNativeCloudMcp(agent, journal.previous_cloud_mcp);\n    if (journal.native_owned) disableNativeAgent(agent);\n    unlinkSync(agentNativeBundleJournalPath(agent));\n    unlinkSync(agentNativeBundleRemovalJournalPath(agent));\n  } catch (error) {\n    try {\n      restoreInstalledAgentNativeBundle(agent, journal);\n      unlinkSync(agentNativeBundleRemovalJournalPath(agent));\n    } catch (rollback) {\n      throw new Error(`agent-native bundle removal failed: ${(error as Error).message}; rollback incomplete: ${(rollback as Error).message}`);\n    }\n    throw new Error(`agent-native bundle removal failed: ${(error as Error).message}; installed bundle restored`);\n  }\n  process.stderr.write(`${mark(\"ok\")} ${agent}: agent-native bundle removed; prior skills and cloud MCP restored\\n`);\n}\n\nasync function setup(argv: string[] = []) {\n  const json = argv.includes(\"--json\");\n  const install = argv.includes(\"--install\");\n  const removeBundle = argv.includes(\"--remove\");\n  const agentNative = flagFrom(argv, \"--agent-native\", \"\");\n  const hasAgentNativeFlag = argv.includes(\"--agent-native\") || argv.some((arg) => arg.startsWith(\"--agent-native=\"));\n  const unknown = argv.filter((arg, index) =>\n    arg !== \"--json\"\n    && arg !== \"--install\"\n    && arg !== \"--remove\"\n    && arg !== \"--agent-native\"\n    && !arg.startsWith(\"--agent-native=\")\n    && argv[index - 1] !== \"--agent-native\");\n  if (unknown.length > 0 || (hasAgentNativeFlag && !agentNative) || (agentNative && (json || install)) || (removeBundle && !agentNative)) {","sourceCodeStart":2938,"sourceCodeEnd":2974,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/cli/src/index.ts#L2938-L2974","documentation":"Thrown when agent-native bundle removal failed but the rollback succeeded: restoreInstalledAgentNativeBundle re-created the previously installed bundle and the removal journal was deleted. The original failure is preserved in the message; the key reassurance is that the installed bundle is intact.","triggerScenarios":"Any failure during the destructive phase of `--remove` (unlink/restore of prior skills, MCP restore, journal ops) that is NOT itself a rollback failure — the catch runs restoreInstalledAgentNativeBundle successfully and throws this message with the original error.","commonSituations":"Permission denied deleting a skill file; the removal journal's restore step hit a missing file; a process held an open handle on a journaled file; read-only filesystem.","solutions":["Fix the root cause in the message (usually file permissions or a lock) and re-run `caveman setup --agent-native <agent> --remove`.","Close agents/sync tools holding handles on the skill or MCP config files.","Check that the user running the command owns the agent config directory (chown if setup ran as another user).","If the cause was a transient lock, retry immediately — the bundle was restored, so state is consistent."],"exampleFix":"// before: root-owned skill file blocks removal\nError: .../SKILL.md: EACCES; installed bundle restored\n// after\n$ sudo chown -R $(whoami) ~/.claude/skills/caveman\n$ caveman setup --agent-native claude --remove","handlingStrategy":"try-catch","validationCode":"try { accessSync(agentConfigDir, W_OK); } catch {\n  console.error(\"Agent config dir not writable by current user; fix ownership before --remove\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  removeAgentNativeBundle(agent);\n} catch (err) {\n  if (/installed bundle restored/.test((err as Error).message)) {\n    console.error(`Removal failed but state is consistent: ${(err as Error).message}. Fix cause and retry.`);\n  }\n}","preventionTips":["Keep ownership of the agent config directory consistent with the user running caveman.","Retry after transient locks clear — rollback leaves state consistent.","Check root-cause errno (EACCES/EBUSY) before retrying."],"tags":["removal","rollback","atomicity","cli"],"backgroundTag":"file-write-permission-denied","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"}