{"record":{"id":"812fc8f4ca9b2390","repo":"JuliusBrussee/caveman","slug":"existing-codex-caveman-block-is-corrupted-run-ca","errorCode":null,"errorMessage":"existing Codex Caveman block is corrupted; run `caveman doctor codex`","messagePattern":"existing Codex Caveman block is corrupted; run `caveman doctor codex`","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/cli/src/index.ts","lineNumber":7488,"sourceCode":"      kind: \"aider-core\",\n      owned: { marker: AIDER_NATIVE_CORE_MARKER },\n    },\n  ];\n}\n\nfunction codexNativeConfig(source: string, gw: string, subscription: boolean, mcpBinary: string): { text: string; rootBlock: string; tablesBlock: string } {\n  // Remove caveman's own marker blocks FIRST. The legacy table strippers below\n  // skip every line after a caveman table until the next TOML header, and the\n  // tables block ends with [mcp_servers.caveman] followed by the end marker, so\n  // running them first ate \"# <<< caveman:native-tables\" and the block this\n  // function had itself written failed its own re-parse as \"corrupted\" on the\n  // next wrap (every `caveman codex` run fell back to session-only wrap and\n  // doctor reported drift).\n  let stripped = source;\n  for (const [begin, end] of [[CODEX_NATIVE_ROOT_BEGIN, CODEX_NATIVE_ROOT_END], [CODEX_NATIVE_TABLES_BEGIN, CODEX_NATIVE_TABLES_END]] as const) {\n    const start = stripped.indexOf(begin);\n    const finish = stripped.indexOf(end);\n    if ((start === -1) !== (finish === -1) || finish < start) throw new Error(\"existing Codex Caveman block is corrupted; run `caveman doctor codex`\");\n    if (start !== -1) stripped = `${stripped.slice(0, start)}${stripped.slice(finish + end.length)}`.trim();\n  }\n  stripped = stripCodexCavemanMcpToml(stripCodexCavemanProviderToml(stripped));\n  const rootBlock = `${CODEX_NATIVE_ROOT_BEGIN}\\nmodel_provider = \"caveman\"\\n${CODEX_NATIVE_ROOT_END}`;\n  const providerLines = codexCavemanProviderToml(gw, subscription).split(\"\\n\").slice(1).join(\"\\n\");\n  const tablesBlock = [\n    CODEX_NATIVE_TABLES_BEGIN,\n    providerLines,\n    \"\",\n    \"[mcp_servers.caveman]\",\n    `command = ${JSON.stringify(mcpBinary)}`,\n    CODEX_RECOVERY_ENV,\n    CODEX_NATIVE_TABLES_END,\n  ].join(\"\\n\");\n  const middle = stripped ? `\\n\\n${stripped}` : \"\";\n  return { text: `${rootBlock}${middle}\\n\\n${tablesBlock}\\n`, rootBlock, tablesBlock };\n}\n","sourceCodeStart":7470,"sourceCodeEnd":7506,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/cli/src/index.ts#L7470-L7506","documentation":"The Codex installer strips previously-installed Caveman TOML blocks (root, tables, provider, MCP) by matching begin/end markers before re-emitting them. If exactly one of a begin/end marker pair is present, or the end marker appears before the begin marker, the block is considered corrupted and the installer refuses to proceed, directing the user to `caveman doctor codex`.","triggerScenarios":"Running the Codex enable/install flow when ~/.codex/config.toml contains a half-intact Caveman block: a begin marker without its end marker, an end marker without its begin, or end preceding begin.","commonSituations":"Manual edits to config.toml that deleted only part of a Caveman block; interrupted installs or rollbacks; comment-stripping/minifying tools removing marker comment lines; copy/paste edits that moved lines out of order.","solutions":["Run `caveman doctor codex` to detect and repair the corrupted block, then re-run enable.","Manually remove all remaining Caveman marker lines and enclosed content from the TOML so the config is clean, then re-run install.","Restore config.toml from a backup taken before the Caveman install and re-run the enable flow."],"exampleFix":"// before (config.toml)\n# caveman:native-root-begin\nmodel_provider = \"caveman\"\n// (end marker missing)\n// after `caveman doctor codex`\nmodel_provider = \"openai\"  // clean config; re-run enable to re-add complete block","handlingStrategy":"validation","validationCode":"const toml = readFileSync(codexCfg, 'utf8');\nfor (const [b, e] of [[ROOT_BEGIN, ROOT_END], [TABLES_BEGIN, TABLES_END]]) {\n  const s = toml.indexOf(b), f = toml.indexOf(e);\n  if ((s === -1) !== (f === -1) || f < s) {\n    // run `caveman doctor codex` before enabling\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  await cavemanEnableCodex();\n} catch (e) {\n  if (String(e.message).includes('corrupted')) {\n    await cavemanDoctorCodex();\n    await cavemanEnableCodex();\n  }\n}","preventionTips":["Never hand-delete only part of a marker-delimited Caveman TOML block.","Keep BEGIN/END marker comment lines intact when editing config.toml.","Run `caveman doctor codex` after any manual edit to ~/.codex/config.toml.","Back up config.toml before enabling or upgrading."],"tags":["codex","toml","drift","install"],"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"}