{"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":null,"httpStatus":null,"severity":"error","filePath":"packages/cli/src/index.ts","lineNumber":2636,"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":2618,"sourceCodeEnd":2654,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/27d5a3981a347890211bb1bf2439e5c821a63bc9/packages/cli/src/index.ts#L2618-L2654","documentation":"removeAgentNativeBundle failed partway (error in the try block), but the compensating restoreInstalledAgentNativeBundle succeeded: the installed bundle was put back and the removal journal cleaned up. State is back to installed; only the removal attempt itself failed.","triggerScenarios":"Running --remove when one step (skill restore, MCP restore, native disable, or journal unlink) fails while the rest of the environment is healthy enough to restore the bundle.","commonSituations":"Transient failure such as a config file momentarily locked by the agent; a single skill path unwritable; then the agent exits and restore succeeds.","solutions":["Read the embedded original error message — it names the failing step; address it (close agent, fix permissions)","Re-run `caveman setup --agent-native <agent> --remove` once the blocking condition is gone","If it persists, run setup again to normalize the bundle, then remove"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"import { accessSync, constants } from \"node:fs\";\naccessSync(join(homedir(), agentConfigName(agent)), constants.W_OK);  // throws early if unwritable","typeGuard":null,"tryCatchPattern":"try {\n  await setup([\"--agent-native\", agent, \"--remove\"]);\n} catch (error) {\n  const msg = (error as Error).message;\n  if (/installed bundle restored/.test(msg)) {\n    // state is safely back to installed; the embedded original error is the actionable part\n    await resolveOriginalFailure(extractOriginal(msg));   // e.g. close agent\n    await setup([\"--agent-native\", agent, \"--remove\"]);   // safe to retry once\n  } else throw error;\n}","preventionTips":["Close the agent before removal — transient config locks are the top cause","Treat 'installed bundle restored' as a clean, retryable failure","Parse the embedded original error rather than retrying unchanged"],"tags":["removal","rollback","recoverable"],"backgroundTag":null,"analyzedSha":"27d5a3981a347890211bb1bf2439e5c821a63bc9","analyzedAt":"2026-08-15T09:26:11.751Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}