JuliusBrussee/caveman · error

existing Gemini Caveman routing block is corrupted; run…

Error message

existing Gemini Caveman routing block is corrupted; run `caveman doctor gemini`

What it means

`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.

Solutions

  1. Run `caveman doctor gemini` as the message instructs to repair the block
  2. Manually delete both marker lines and everything between them (or the lone marker), then re-run setup
  3. Restore the settings file from backup or VCS if the corruption came from a bad merge
  4. Inspect the file for the BEGIN/END markers and confirm they are both present and in order before retrying

Example fix

// before: settings.env (Gemini) with orphaned marker
# CAVEMAN:BEGIN
GEMINI_BASE_URL=https://gw.example.com
// after
$ caveman doctor gemini   # removes/rewrites the corrupted block
// or manually remove the stray '# CAVEMAN:BEGIN' line, then `caveman setup`
Defensive patterns

Strategy: validation

Validate before calling

// Check marker integrity before setup:
const text = fs.readFileSync(geminiSettingsPath, 'utf8');
const begin = text.includes('CAVEMAN:BEGIN');
const end = text.includes('CAVEMAN:END');
if (begin !== end || text.indexOf('CAVEMAN:END') < text.indexOf('CAVEMAN:BEGIN')) {
  console.error('Corrupted Caveman block — run: caveman doctor gemini');
}

Try / catch

try {
  await cavemanSetup(['gemini']);
} catch (e) {
  if (String(e.message).includes('Gemini Caveman routing block is corrupted')) {
    await exec('caveman doctor gemini');
    return cavemanSetup(['gemini']);
  }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/6f6e385999eb061c. Report an issue: GitHub.

Appendix: source

Thrown at packages/cli/src/index.ts:7091

      kind: "claude-settings",
      owned: { route, previous_route: previousRoute ?? null, assume_first_party: assumeFirstParty ? "1" : null },
    },
    {
      file: mcpPath,
      before: mcpBefore,
      after: Buffer.from(JSON.stringify(mcpRoot, null, 2) + "\n"),
      kind: "claude-mcp",
      owned: { installed_mcp: installedMcp, previous_mcp: previousMcp ?? null },
    },
  ];
}

function geminiNativeEnv(source: string, route: string): { text: string; block: string } {
  let stripped = source;
  const start = stripped.indexOf(GEMINI_NATIVE_ENV_BEGIN);
  const finish = stripped.indexOf(GEMINI_NATIVE_ENV_END);
  if ((start === -1) !== (finish === -1) || (start !== -1 && finish < start)) {
    throw new Error("existing Gemini Caveman routing block is corrupted; run `caveman doctor gemini`");
  }
  if (start !== -1) stripped = `${stripped.slice(0, start)}${stripped.slice(finish + GEMINI_NATIVE_ENV_END.length)}`.trim();
  const block = [
    GEMINI_NATIVE_ENV_BEGIN,
    `GEMINI_BASE_URL=${route}`,
    `GOOGLE_GEMINI_BASE_URL=${route}`,
    `GOOGLE_VERTEX_BASE_URL=${appendUrlPath(route, "/vertex")}`,
    GEMINI_NATIVE_ENV_END,
  ].join("\n");
  return { text: `${stripped}${stripped ? "\n\n" : ""}${block}\n`, block };
}

function geminiNativeMutations(gw: string, mcpBinary: string): NativeMutation[] {
  if (wrapMode(gw) === "managed") {
    throw new Error("managed Gemini CLI routing is unsupported because Gemini CLI cannot send separate Caveman and upstream credentials");
  }
  const settingsPath = geminiSettingsPath();
  const settingsBefore = fileBytes(settingsPath);

View on GitHub (pinned to 3ee70a1026)