{"record":{"id":"6f6e385999eb061c","repo":"JuliusBrussee/caveman","slug":"existing-gemini-caveman-routing-block-is-corrupted","errorCode":null,"errorMessage":"existing Gemini Caveman routing block is corrupted; run `caveman doctor gemini`","messagePattern":"existing Gemini Caveman routing block is corrupted; run `caveman doctor gemini`","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/cli/src/index.ts","lineNumber":7091,"sourceCode":"      kind: \"claude-settings\",\n      owned: { route, previous_route: previousRoute ?? null, assume_first_party: assumeFirstParty ? \"1\" : null },\n    },\n    {\n      file: mcpPath,\n      before: mcpBefore,\n      after: Buffer.from(JSON.stringify(mcpRoot, null, 2) + \"\\n\"),\n      kind: \"claude-mcp\",\n      owned: { installed_mcp: installedMcp, previous_mcp: previousMcp ?? null },\n    },\n  ];\n}\n\nfunction geminiNativeEnv(source: string, route: string): { text: string; block: string } {\n  let stripped = source;\n  const start = stripped.indexOf(GEMINI_NATIVE_ENV_BEGIN);\n  const finish = stripped.indexOf(GEMINI_NATIVE_ENV_END);\n  if ((start === -1) !== (finish === -1) || (start !== -1 && finish < start)) {\n    throw new Error(\"existing Gemini Caveman routing block is corrupted; run `caveman doctor gemini`\");\n  }\n  if (start !== -1) stripped = `${stripped.slice(0, start)}${stripped.slice(finish + GEMINI_NATIVE_ENV_END.length)}`.trim();\n  const block = [\n    GEMINI_NATIVE_ENV_BEGIN,\n    `GEMINI_BASE_URL=${route}`,\n    `GOOGLE_GEMINI_BASE_URL=${route}`,\n    `GOOGLE_VERTEX_BASE_URL=${appendUrlPath(route, \"/vertex\")}`,\n    GEMINI_NATIVE_ENV_END,\n  ].join(\"\\n\");\n  return { text: `${stripped}${stripped ? \"\\n\\n\" : \"\"}${block}\\n`, block };\n}\n\nfunction geminiNativeMutations(gw: string, mcpBinary: string): NativeMutation[] {\n  if (wrapMode(gw) === \"managed\") {\n    throw new Error(\"managed Gemini CLI routing is unsupported because Gemini CLI cannot send separate Caveman and upstream credentials\");\n  }\n  const settingsPath = geminiSettingsPath();\n  const settingsBefore = fileBytes(settingsPath);","sourceCodeStart":7073,"sourceCodeEnd":7109,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/cli/src/index.ts#L7073-L7109","documentation":"`geminiNativeEnv()` rewrites the Caveman-managed env block (delimited by GEMINI_NATIVE_ENV_BEGIN/END markers) in Gemini CLI settings. If exactly one of the two markers is present, or the END marker appears before the BEGIN marker, the block is considered corrupted and the CLI refuses to touch it, telling the user to run `caveman doctor gemini` to repair.","triggerScenarios":"Running native Gemini setup when the settings file contains the GEMINI_NATIVE_ENV_BEGIN marker without GEMINI_NATIVE_ENV_END, the END marker without BEGIN, or `finish < start` — any of which makes `(start === -1) !== (finish === -1) || (start !== -1 && finish < start)` true.","commonSituations":"User manually deleted one of the marker comment lines; a merge conflict resolved to keep only half the block; an editor or formatter stripped the end-marker line; truncated file write left a partial block.","solutions":["Run `caveman doctor gemini` as the message instructs to repair the block","Manually delete both marker lines and everything between them (or the lone marker), then re-run setup","Restore the settings file from backup or VCS if the corruption came from a bad merge","Inspect the file for the BEGIN/END markers and confirm they are both present and in order before retrying"],"exampleFix":"// before: settings.env (Gemini) with orphaned marker\n# CAVEMAN:BEGIN\nGEMINI_BASE_URL=https://gw.example.com\n// after\n$ caveman doctor gemini   # removes/rewrites the corrupted block\n// or manually remove the stray '# CAVEMAN:BEGIN' line, then `caveman setup`","handlingStrategy":"validation","validationCode":"// Check marker integrity before setup:\nconst text = fs.readFileSync(geminiSettingsPath, 'utf8');\nconst begin = text.includes('CAVEMAN:BEGIN');\nconst end = text.includes('CAVEMAN:END');\nif (begin !== end || text.indexOf('CAVEMAN:END') < text.indexOf('CAVEMAN:BEGIN')) {\n  console.error('Corrupted Caveman block — run: caveman doctor gemini');\n}","typeGuard":null,"tryCatchPattern":"try {\n  await cavemanSetup(['gemini']);\n} catch (e) {\n  if (String(e.message).includes('Gemini Caveman routing block is corrupted')) {\n    await exec('caveman doctor gemini');\n    return cavemanSetup(['gemini']);\n  }\n  throw e;\n}","preventionTips":["Never delete or edit the CAVEMAN:BEGIN/END marker comment lines","Resolve merge conflicts by removing the whole block and re-running setup","Run `caveman doctor gemini` after any manual edit of Gemini settings","Disable formatters/plugins that strip comment lines from settings files"],"tags":["config","gemini","corrupted-block","markers"],"backgroundTag":"invalid-config-value","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"}