Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

test(adapters): add parser repair goldens for the hook-system rewrite

Jack Rudenko8277f505c915
diff --git a/packages/cli/src/test-fixtures/parser-goldens/anthropic-sse.byte-identical.armed.golden.json b/packages/cli/src/test-fixtures/parser-goldens/anthropic-sse.byte-identical.armed.golden.json
new file mode 100644
index 00000000..c29d7bba
--- /dev/null
+++ b/packages/cli/src/test-fixtures/parser-goldens/anthropic-sse.byte-identical.armed.golden.json
@@ -0,0 +1,22 @@
+{
+ "kind": "parser",
+ "id": "anthropic-sse.byte-identical.armed",
+ "source": "anthropic-sse behavior repair > is byte-identical for an untargeted tool after generated pings are stripped (rule armed)",
+ "parser": "anthropic-sse",
+ "modelName": "test-model",
+ "repair": {
+ "kind": "replace",
+ "tool": "target_tool",
+ "args": "{\"value\":\"behavior-repaired\"}"
+ },
+ "input": {
+ "encoding": "utf8",
+ "chunks": [
+ "event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"toolu_anthropic_0\",\"name\":\"untargeted_tool\",\"input\":{}}}\n\n",
+ "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"value\\\":\\\"byte-\"}}\n\n",
+ "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"identical\\\"}\"}}\n\n",
+ "event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n"
+ ]
+ },
+ "output": "event: ping\ndata: {\"type\":\"ping\"}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"toolu_anthropic_0\",\"name\":\"untargeted_tool\",\"input\":{}}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"value\\\":\\\"byte-\"}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"identical\\\"}\"}}\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n"
+}
diff --git a/packages/cli/src/test-fixtures/parser-goldens/anthropic-sse.byte-identical.baseline.golden.json b/packages/cli/src/test-fixtures/parser-goldens/anthropic-sse.byte-identical.baseline.golden.json
new file mode 100644
index 00000000..bcf1e728
--- /dev/null
+++ b/packages/cli/src/test-fixtures/parser-goldens/anthropic-sse.byte-identical.baseline.golden.json
@@ -0,0 +1,20 @@
+{
+ "kind": "parser",
+ "id": "anthropic-sse.byte-identical.baseline",
+ "source": "anthropic-sse behavior repair > is byte-identical for an untargeted tool after generated pings are stripped (baseline, no rule)",
+ "parser": "anthropic-sse",
+ "modelName": "test-model",
+ "repair": {
+ "kind": "none"
+ },
+ "input": {
+ "encoding": "utf8",
+ "chunks": [
+ "event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"toolu_anthropic_0\",\"name\":\"untargeted_tool\",\"input\":{}}}\n\n",
+ "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"value\\\":\\\"byte-\"}}\n\n",
+ "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"identical\\\"}\"}}\n\n",
+ "event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n"
+ ]
+ },
+ "output": "event: ping\ndata: {\"type\":\"ping\"}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"toolu_anthropic_0\",\"name\":\"untargeted_tool\",\"input\":{}}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"value\\\":\\\"byte-\"}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"identical\\\"}\"}}\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n"
+}
diff --git a/packages/cli/src/test-fixtures/parser-goldens/anthropic-sse.targeted-fragmented-repaired.golden.json b/packages/cli/src/test-fixtures/parser-goldens/anthropic-sse.targeted-fragmented-repaired.golden.json
new file mode 100644
index 00000000..a0ab839c
--- /dev/null
+++ b/packages/cli/src/test-fixtures/parser-goldens/anthropic-sse.targeted-fragmented-repaired.golden.json
@@ -0,0 +1,23 @@
+{
+ "kind": "parser",
+ "id": "anthropic-sse.targeted-fragmented-repaired",
+ "source": "anthropic-sse behavior repair > reassembles fragmented targeted arguments, repairs them, and emits one delta",
+ "parser": "anthropic-sse",
+ "modelName": "test-model",
+ "repair": {
+ "kind": "replace",
+ "tool": "target_tool",
+ "args": "{\"value\":\"behavior-repaired\"}"
+ },
+ "input": {
+ "encoding": "utf8",
+ "chunks": [
+ "event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"toolu_anthropic_0\",\"name\":\"target_tool\",\"input\":{}}}\n\n",
+ "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"value\\\":\\\"model-\"}}\n\n",
+ "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"original\\\"}\"}}\n\n",
+ "event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n"
+ ]
+ },
+ "defect": "createToolRepairInterceptor's flush() returns `<content_block_delta frame>` + the stop frame's `data:` line, but enqueueData() has already written the held `event: content_block_stop` line before it. The wire reads `event: content_block_stop\\nevent: content_block_delta\\ndata: <delta>\\n\\ndata: <stop>\\n\\n`: per the SSE spec the delta is dispatched (last `event:` wins) and the stop payload arrives as an UNNAMED event, which a client dispatching on event names drops, leaving the tool_use block unclosed. Compare dataPayloads(), not bytes.",
+ "output": "event: ping\ndata: {\"type\":\"ping\"}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"toolu_anthropic_0\",\"name\":\"target_tool\",\"input\":{}}}\n\n\n\nevent: content_block_stop\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"value\\\":\\\"behavior-repaired\\\"}\"}}\n\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n"
+}
diff --git a/packages/cli/src/test-fixtures/parser-goldens/anthropic-sse.untargeted-unchanged.golden.json b/packages/cli/src/test-fixtures/parser-goldens/anthropic-sse.untargeted-unchanged.golden.json
new file mode 100644
index 00000000..f33ad8be
--- /dev/null
+++ b/packages/cli/src/test-fixtures/parser-goldens/anthropic-sse.untargeted-unchanged.golden.json
@@ -0,0 +1,21 @@
+{
+ "kind": "parser",
+ "id": "anthropic-sse.untargeted-unchanged",
+ "source": "anthropic-sse behavior repair > passes an untargeted tool through unchanged",
+ "parser": "anthropic-sse",
+ "modelName": "test-model",
+ "repair": {
+ "kind": "replace",
+ "tool": "target_tool",
+ "args": "{\"value\":\"behavior-repaired\"}"
+ },
+ "input": {
+ "encoding": "utf8",
+ "chunks": [
+ "event: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"toolu_anthropic_0\",\"name\":\"untargeted_tool\",\"input\":{}}}\n\n",
+ "event: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"value\\\":\\\"model-original\\\"}\"}}\n\n",
+ "event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n"
+ ]
+ },
+ "output": "event: ping\ndata: {\"type\":\"ping\"}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"toolu_anthropic_0\",\"name\":\"untargeted_tool\",\"input\":{}}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"value\\\":\\\"model-original\\\"}\"}}\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n"
+}
diff --git a/packages/cli/src/test-fixtures/parser-goldens/capture.ts b/packages/cli/src/test-fixtures/parser-goldens/capture.ts
new file mode 100644
index 00000000..9d9b3494
--- /dev/null
+++ b/packages/cli/src/test-fixtures/parser-goldens/capture.ts
@@ -0,0 +1,77 @@
+/**
+ * Capture the parser goldens from the CURRENT code.
+ *
+ * bun run src/test-fixtures/parser-goldens/capture.ts (from packages/cli)
+ *
+ * Writes one `<id>.golden.json` per case in `cases.ts`. It refuses to overwrite
+ * an existing golden: the goldens are the pre-rewrite contract, and capturing
+ * them again from rewritten code would make every parity check vacuous. To
+ * re-capture deliberately (only ever from the pre-hook parsers), delete the file
+ * first and say why in the commit.
+ *
+ * Every case is captured twice, 25 ms apart, so a clock- or random-derived value
+ * that `normalizeWire` misses shows up as a mismatch and nothing is written.
+ */
+
+import { existsSync, writeFileSync } from "node:fs";
+
+import {
+ GEMINI_APPLICABILITY_MODELS,
+ GEMINI_GOLDEN_ID,
+ GEMINI_MODEL,
+ GEMINI_SCENARIOS,
+ GEMINI_TOOL_SCHEMAS,
+ PARSER_CASES,
+} from "./cases.js";
+import { runGeminiCapture, runParserCase } from "./drive-current.js";
+import { type Golden, goldenPath } from "./golden-format.js";
+
+const pause = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
+
+async function twice<T>(label: string, run: () => Promise<T>): Promise<T> {
+ const first = await run();
+ await pause(25);
+ const second = await run();
+ const a = JSON.stringify(first);
+ const b = JSON.stringify(second);
+ if (a !== b) {
+ throw new Error(`${label}: two captures differ — a nondeterministic value is not normalized`);
+ }
+ return first;
+}
+
+async function main(): Promise<void> {
+ const goldens: Golden[] = [];
+
+ for (const c of PARSER_CASES) {
+ const output = await twice(c.id, () => runParserCase(c));
+ goldens.push({ kind: "parser", ...c, output });
+ }
+
+ goldens.push(
+ await twice(GEMINI_GOLDEN_ID, () =>
+ runGeminiCapture(
+ GEMINI_GOLDEN_ID,
+ GEMINI_MODEL,
+ GEMINI_TOOL_SCHEMAS,
+ GEMINI_APPLICABILITY_MODELS,
+ GEMINI_SCENARIOS
+ )
+ )
+ );
+
+ const existing = goldens.filter((g) => existsSync(goldenPath(g.id)));
+ if (existing.length > 0) {
+ console.error(
+ `refusing to overwrite ${existing.length} existing golden(s): ${existing.map((g) => g.id).join(", ")}`
+ );
+ process.exit(1);
+ }
+
+ for (const golden of goldens) {
+ writeFileSync(goldenPath(golden.id), `${JSON.stringify(golden, null, 2)}\n`);
+ console.log(`wrote ${golden.id}`);
+ }
+}
+
+await main();
diff --git a/packages/cli/src/test-fixtures/parser-goldens/cases.ts b/packages/cli/src/test-fixtures/parser-goldens/cases.ts
new file mode 100644
index 00000000..63c4350b
--- /dev/null
+++ b/packages/cli/src/test-fixtures/parser-goldens/cases.ts
@@ -0,0 +1,438 @@
+/**
+ * The cases captured into the parser goldens.
+ *
+ * Every case of `handlers/shared/stream-parsers/behavior-repair.test.ts`, with
+ * its exact input frames and repair rule, plus the Devin parser (which carried
+ * the same plumbing but had no repair test) and the Gemini thought-signature
+ * middleware. Only `capture.ts` and the drift check in `parser-goldens.test.ts`
+ * read this file; replays read the self-contained golden files.
+ */
+
+import { readFileSync } from "node:fs";
+import { join } from "node:path";
+
+import { bytes, envelope, msg } from "../../providers/devin/proto-codec.js";
+import type { GoldenInput, GoldenRepair, GoldenToolSchema, ParserGolden } from "./golden-format.js";
+
+// ── Constants copied from behavior-repair.test.ts ────────────────────────────
+export const TARGET_TOOL = "target_tool";
+export const UNTARGETED_TOOL = "untargeted_tool";
+export const ORIGINAL_VALUE = "model-original";
+export const REPAIRED_VALUE = "behavior-repaired";
+export const ORIGINAL_ARGS = JSON.stringify({ value: ORIGINAL_VALUE });
+export const REPAIRED_ARGS = JSON.stringify({ value: REPAIRED_VALUE });
+
+const MODEL = "test-model";
+
+const REPLACE: GoldenRepair = { kind: "replace", tool: TARGET_TOOL, args: REPAIRED_ARGS };
+const THROWS: GoldenRepair = { kind: "throws", tool: TARGET_TOOL };
+const NONE: GoldenRepair = { kind: "none" };
+
+const toolSchemas: GoldenToolSchema[] = [TARGET_TOOL, UNTARGETED_TOOL].map((name) => ({
+ name,
+ input_schema: {
+ type: "object",
+ properties: { value: { type: "string" } },
+ required: ["value"],
+ },
+}));
+
+const dataFrame = (data: unknown) => `data: ${JSON.stringify(data)}\n\n`;
+const anthropicFrame = (event: string, data: unknown) =>
+ `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`;
+const utf8 = (chunks: string[]): GoldenInput => ({ encoding: "utf8", chunks });
+
+function openAiToolFrames(name: string, args = ORIGINAL_ARGS): GoldenInput {
+ return utf8([
+ dataFrame({
+ choices: [
+ {
+ delta: {
+ tool_calls: [
+ {
+ index: 0,
+ id: "call_openai_0",
+ type: "function",
+ function: { name, arguments: args },
+ },
+ ],
+ },
+ finish_reason: null,
+ },
+ ],
+ }),
+ dataFrame({ choices: [{ delta: {}, finish_reason: "tool_calls" }] }),
+ "data: [DONE]\n\n",
+ ]);
+}
+
+function responsesToolFrames(name: string, fragments = [ORIGINAL_ARGS]): GoldenInput {
+ const item = {
+ type: "function_call",
+ id: "fc_responses_item_0",
+ call_id: "responses_call_0",
+ name,
+ };
+ return utf8([
+ dataFrame({ type: "response.output_item.added", item }),
+ ...fragments.map((delta) =>
+ dataFrame({
+ type: "response.function_call_arguments.delta",
+ call_id: item.call_id,
+ item_id: item.id,
+ delta,
+ })
+ ),
+ dataFrame({ type: "response.output_item.done", item }),
+ dataFrame({
+ type: "response.completed",
+ response: { usage: { input_tokens: 5, output_tokens: 3 } },
+ }),
+ ]);
+}
+
+function anthropicToolFrames(name: string, fragments = [ORIGINAL_ARGS]): GoldenInput {
+ return utf8([
+ anthropicFrame("content_block_start", {
+ type: "content_block_start",
+ index: 0,
+ content_block: { type: "tool_use", id: "toolu_anthropic_0", name, input: {} },
+ }),
+ ...fragments.map((partial_json) =>
+ anthropicFrame("content_block_delta", {
+ type: "content_block_delta",
+ index: 0,
+ delta: { type: "input_json_delta", partial_json },
+ })
+ ),
+ anthropicFrame("content_block_stop", { type: "content_block_stop", index: 0 }),
+ ]);
+}
+
+function geminiToolFrames(name: string): GoldenInput {
+ return utf8([
+ dataFrame({
+ candidates: [
+ {
+ content: {
+ role: "model",
+ parts: [{ functionCall: { name, args: { value: ORIGINAL_VALUE } } }],
+ },
+ finishReason: "STOP",
+ },
+ ],
+ }),
+ ]);
+}
+
+/**
+ * Devin Connect frames: a tool-call header (`6 → 1` id, `6 → 2` name), then one
+ * frame per argument fragment (`6 → 3`), then the `{}` end-of-stream frame.
+ */
+function devinToolFrames(name: string, fragments: string[]): GoldenInput {
+ const frames = [
+ envelope(bytes(6, msg(bytes(1, "call_devin_0"), bytes(2, name)))),
+ ...fragments.map((fragment) => envelope(bytes(6, msg(bytes(3, fragment))))),
+ envelope(new TextEncoder().encode("{}"), 2),
+ ];
+ return { encoding: "base64", chunks: frames.map((f) => Buffer.from(f).toString("base64")) };
+}
+
+type ParserCase = Omit<ParserGolden, "kind" | "output">;
+
+const ANTHROPIC_BYTE_FRAGMENTS = ['{"value":"byte-', 'identical"}'];
+const FRAGMENTED_ORIGINAL = ['{"value":"model-', 'original"}'];
+const INCREMENTAL_FRAGMENTS = ['{"value":"incremental-', 'delivery"}'];
+
+export const PARSER_CASES: ParserCase[] = [
+ // ── openai-sse ──
+ {
+ id: "openai-sse.targeted-repaired",
+ source: "openai-sse behavior repair > rewrites arguments for a targeted tool",
+ parser: "openai-sse",
+ modelName: MODEL,
+ toolSchemas,
+ repair: REPLACE,
+ input: openAiToolFrames(TARGET_TOOL),
+ },
+ {
+ id: "openai-sse.untargeted-unchanged",
+ source: "openai-sse behavior repair > passes an untargeted tool through unchanged",
+ parser: "openai-sse",
+ modelName: MODEL,
+ toolSchemas,
+ repair: REPLACE,
+ input: openAiToolFrames(UNTARGETED_TOOL),
+ },
+ {
+ id: "openai-sse.repair-throws",
+ source: "openai-sse behavior repair > emits the model arguments when a repair rule throws",
+ parser: "openai-sse",
+ modelName: MODEL,
+ toolSchemas,
+ repair: THROWS,
+ input: openAiToolFrames(TARGET_TOOL),
+ },
+ // ── openai-responses-sse ──
+ {
+ id: "openai-responses-sse.targeted-repaired",
+ source: "openai-responses-sse behavior repair > rewrites arguments for a targeted tool",
+ parser: "openai-responses-sse",
+ modelName: MODEL,
+ repair: REPLACE,
+ input: responsesToolFrames(TARGET_TOOL),
+ },
+ {
+ id: "openai-responses-sse.untargeted-unchanged",
+ source: "openai-responses-sse behavior repair > passes an untargeted tool through unchanged",
+ parser: "openai-responses-sse",
+ modelName: MODEL,
+ repair: REPLACE,
+ input: responsesToolFrames(UNTARGETED_TOOL),
+ },
+ {
+ id: "openai-responses-sse.untargeted-incremental",
+ source:
+ "openai-responses-sse behavior repair > keeps untargeted argument fragments incremental",
+ parser: "openai-responses-sse",
+ modelName: MODEL,
+ repair: REPLACE,
+ input: responsesToolFrames(UNTARGETED_TOOL, INCREMENTAL_FRAGMENTS),
+ },
+ // ── anthropic-sse ──
+ {
+ id: "anthropic-sse.targeted-fragmented-repaired",
+ source:
+ "anthropic-sse behavior repair > reassembles fragmented targeted arguments, repairs them, and emits one delta",
+ parser: "anthropic-sse",
+ modelName: MODEL,
+ repair: REPLACE,
+ input: anthropicToolFrames(TARGET_TOOL, FRAGMENTED_ORIGINAL),
+ defect:
+ "createToolRepairInterceptor's flush() returns `<content_block_delta frame>` + the stop frame's `data:` line, " +
+ "but enqueueData() has already written the held `event: content_block_stop` line before it. The wire reads " +
+ "`event: content_block_stop\\nevent: content_block_delta\\ndata: <delta>\\n\\ndata: <stop>\\n\\n`: per the SSE " +
+ "spec the delta is dispatched (last `event:` wins) and the stop payload arrives as an UNNAMED event, which a " +
+ "client dispatching on event names drops, leaving the tool_use block unclosed. Compare dataPayloads(), not bytes.",
+ },
+ {
+ id: "anthropic-sse.untargeted-unchanged",
+ source: "anthropic-sse behavior repair > passes an untargeted tool through unchanged",
+ parser: "anthropic-sse",
+ modelName: MODEL,
+ repair: REPLACE,
+ input: anthropicToolFrames(UNTARGETED_TOOL),
+ },
+ {
+ id: "anthropic-sse.byte-identical.baseline",
+ source:
+ "anthropic-sse behavior repair > is byte-identical for an untargeted tool after generated pings are stripped (baseline, no rule)",
+ parser: "anthropic-sse",
+ modelName: MODEL,
+ repair: NONE,
+ input: anthropicToolFrames(UNTARGETED_TOOL, ANTHROPIC_BYTE_FRAGMENTS),
+ },
+ {
+ id: "anthropic-sse.byte-identical.armed",
+ source:
+ "anthropic-sse behavior repair > is byte-identical for an untargeted tool after generated pings are stripped (rule armed)",
+ parser: "anthropic-sse",
+ modelName: MODEL,
+ repair: REPLACE,
+ input: anthropicToolFrames(UNTARGETED_TOOL, ANTHROPIC_BYTE_FRAGMENTS),
+ },
+ // ── gemini-sse ──
+ {
+ id: "gemini-sse.targeted-repaired",
+ source: "gemini-sse behavior repair > rewrites arguments for a targeted tool",
+ parser: "gemini-sse",
+ modelName: MODEL,
+ repair: REPLACE,
+ input: geminiToolFrames(TARGET_TOOL),
+ },
+ {
+ id: "gemini-sse.untargeted-unchanged",
+ source: "gemini-sse behavior repair > passes an untargeted tool through unchanged",
+ parser: "gemini-sse",
+ modelName: MODEL,
+ repair: REPLACE,
+ input: geminiToolFrames(UNTARGETED_TOOL),
+ },
+ // ── devin-connect (no repair test existed; same rule, same three outcomes) ──
+ {
+ id: "devin-connect.targeted-fragmented-repaired",
+ source: "devin addition: fragmented targeted arguments are withheld, repaired, one delta",
+ parser: "devin-connect",
+ modelName: MODEL,
+ repair: REPLACE,
+ input: devinToolFrames(TARGET_TOOL, FRAGMENTED_ORIGINAL),
+ },
+ {
+ id: "devin-connect.untargeted-incremental",
+ source: "devin addition: an untargeted tool streams its fragments unchanged",
+ parser: "devin-connect",
+ modelName: MODEL,
+ repair: REPLACE,
+ input: devinToolFrames(UNTARGETED_TOOL, INCREMENTAL_FRAGMENTS),
+ },
+ {
+ id: "devin-connect.repair-throws",
+ source: "devin addition: the model arguments are emitted when the repair rule throws",
+ parser: "devin-connect",
+ modelName: MODEL,
+ repair: THROWS,
+ input: devinToolFrames(TARGET_TOOL, FRAGMENTED_ORIGINAL),
+ },
+];
+
+// ── Gemini thought-signature middleware ──────────────────────────────────────
+
+export const GEMINI_GOLDEN_ID = "gemini-thought-signature.openai-sse";
+export const GEMINI_MODEL = "google/gemini-3.1-pro-preview";
+
+/** Model ids whose applicability is recorded; the gate is a substring rule. */
+export const GEMINI_APPLICABILITY_MODELS = [
+ "google/gemini-3.1-pro-preview",
+ "gemini-2.5-flash",
+ "google/gemma-3-27b-it",
+ "gpt-5.6-sol",
+ "claude-opus-5",
+];
+
+export const GEMINI_TOOL_SCHEMAS: GoldenToolSchema[] = [
+ {
+ name: "Read",
+ input_schema: {
+ type: "object",
+ properties: { file_path: { type: "string" } },
+ required: ["file_path"],
+ },
+ },
+];
+
+/** Shape of an OpenRouter Gemini chunk, as in the `gemini-3.1-pro-or-*` captures. */
+function geminiChunk(delta: Record<string, unknown>, finishReason: string | null = null) {
+ return dataFrame({
+ id: "gen-golden",
+ object: "chat.completion.chunk",
+ model: GEMINI_MODEL,
+ provider: "Google",
+ choices: [{ index: 0, delta, finish_reason: finishReason, native_finish_reason: null }],
+ });
+}
+
+const reasoningText = (signature: string, index: number) => ({
+ type: "reasoning.text",
+ text: "",
+ signature,
+ format: "google-gemini-v1",
+ index,
+});
+
+/**
+ * One turn: text, reasoning_details split across two chunks, one Read call
+ * whose arguments arrive in two fragments, then `tool_calls` and usage.
+ */
+function geminiToolTurn(callId: string, signatures: [string, string], path: string): GoldenInput {
+ return utf8([
+ geminiChunk({ role: "assistant", content: "Reading it." }),
+ geminiChunk({
+ role: "assistant",
+ content: "",
+ reasoning_details: [reasoningText(signatures[0], 0)],
+ }),
+ geminiChunk({
+ role: "assistant",
+ content: "",
+ reasoning_details: [reasoningText(signatures[1], 1)],
+ tool_calls: [
+ { index: 0, id: callId, type: "function", function: { name: "Read", arguments: "" } },
+ ],
+ }),
+ geminiChunk({
+ role: "assistant",
+ tool_calls: [{ index: 0, function: { arguments: JSON.stringify({ file_path: path }) } }],
+ }),
+ dataFrame({
+ id: "gen-golden",
+ object: "chat.completion.chunk",
+ model: GEMINI_MODEL,
+ choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }],
+ usage: { prompt_tokens: 40, completion_tokens: 12, total_tokens: 52 },
+ }),
+ "data: [DONE]\n\n",
+ ]);
+}
+
+/** A recorded OpenRouter Gemini stream, split into its SSE events. */
+function recordedFixture(file: string): GoldenInput {
+ const text = readFileSync(join(import.meta.dir, "..", "sse-responses", file), "utf8");
+ const events = text.split(/(?<=\n\n)/).filter((e) => e.length > 0);
+ return utf8(events);
+}
+
+/** The follow-up request, in the OpenAI message format `beforeRequest` receives. */
+function followUpMessages(callIds: string[]): unknown[] {
+ const messages: unknown[] = [
+ { role: "system", content: "You are a coding agent." },
+ { role: "user", content: "Read the file." },
+ ];
+ for (const id of callIds) {
+ messages.push(
+ {
+ role: "assistant",
+ content: "Reading it.",
+ tool_calls: [
+ {
+ id,
+ type: "function",
+ function: { name: "Read", arguments: JSON.stringify({ file_path: `/tmp/${id}` }) },
+ },
+ ],
+ },
+ { role: "tool", tool_call_id: id, content: `contents of ${id}` }
+ );
+ }
+ messages.push({ role: "user", content: "Thanks." });
+ return messages;
+}
+
+export interface GeminiScenarioCase {
+ id: string;
+ description: string;
+ streams: GoldenInput[];
+ nextRequestMessages: unknown[];
+}
+
+export const GEMINI_SCENARIOS: GeminiScenarioCase[] = [
+ {
+ id: "tool-call-with-reasoning",
+ description:
+ "reasoning_details accumulated across two chunks are cached against the turn's tool_call id and injected into the matching assistant message of the next request; an assistant tool call with no cached id is left alone",
+ streams: [geminiToolTurn("call_gem_a", ["SIG-A-0", "SIG-A-1"], "/tmp/a")],
+ nextRequestMessages: followUpMessages(["call_gem_a", "call_unknown"]),
+ },
+ {
+ id: "two-turns",
+ description:
+ "two streams cache two entries; each assistant message receives the reasoning_details of its own turn only",
+ streams: [
+ geminiToolTurn("call_gem_a", ["SIG-A-0", "SIG-A-1"], "/tmp/a"),
+ geminiToolTurn("call_gem_b", ["SIG-B-0", "SIG-B-1"], "/tmp/b"),
+ ],
+ nextRequestMessages: followUpMessages(["call_gem_a", "call_gem_b"]),
+ },
+ {
+ id: "reasoning-without-tool-call",
+ description:
+ "a recorded OpenRouter Gemini stream with reasoning_details and no tool call caches nothing; the next request is unchanged",
+ streams: [recordedFixture("gemini-3.1-pro-or-maxtokens-truncated.sse")],
+ nextRequestMessages: followUpMessages(["call_gem_a"]),
+ },
+ {
+ id: "no-prior-stream",
+ description: "with an empty cache, beforeRequest leaves the request unchanged",
+ streams: [],
+ nextRequestMessages: followUpMessages(["call_gem_a"]),
+ },
+];
diff --git a/packages/cli/src/test-fixtures/parser-goldens/devin-connect.repair-throws.golden.json b/packages/cli/src/test-fixtures/parser-goldens/devin-connect.repair-throws.golden.json
new file mode 100644
index 00000000..76571fef
--- /dev/null
+++ b/packages/cli/src/test-fixtures/parser-goldens/devin-connect.repair-throws.golden.json
@@ -0,0 +1,21 @@
+{
+ "kind": "parser",
+ "id": "devin-connect.repair-throws",
+ "source": "devin addition: the model arguments are emitted when the repair rule throws",
+ "parser": "devin-connect",
+ "modelName": "test-model",
+ "repair": {
+ "kind": "throws",
+ "tool": "target_tool"
+ },
+ "input": {
+ "encoding": "base64",
+ "chunks": [
+ "AAAAAB0yGwoMY2FsbF9kZXZpbl8wEgt0YXJnZXRfdG9vbA==",
+ "AAAAABQyEhoQeyJ2YWx1ZSI6Im1vZGVsLQ==",
+ "AAAAAA4yDBoKb3JpZ2luYWwifQ==",
+ "AgAAAAJ7fQ=="
+ ]
+ },
+ "output": "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_<ID>\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"model\":\"test-model\",\"stop_reason\":null,\"stop_sequence\":null,\"usage\":{\"input_tokens\":100,\"output_tokens\":1,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0}}}\n\nevent: ping\ndata: {\"type\":\"ping\"}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"call_devin_0\",\"name\":\"target_tool\",\"input\":{}}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"value\\\":\\\"model-original\\\"}\"}}\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\nevent: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\",\"stop_sequence\":null},\"usage\":{\"output_tokens\":0}}\n\nevent: message_stop\ndata: {\"type\":\"message_stop\"}\n\n"
+}
diff --git a/packages/cli/src/test-fixtures/parser-goldens/devin-connect.targeted-fragmented-repaired.golden.json b/packages/cli/src/test-fixtures/parser-goldens/devin-connect.targeted-fragmented-repaired.golden.json
new file mode 100644
index 00000000..bf8e11f7
--- /dev/null
+++ b/packages/cli/src/test-fixtures/parser-goldens/devin-connect.targeted-fragmented-repaired.golden.json
@@ -0,0 +1,22 @@
+{
+ "kind": "parser",
+ "id": "devin-connect.targeted-fragmented-repaired",
+ "source": "devin addition: fragmented targeted arguments are withheld, repaired, one delta",
+ "parser": "devin-connect",
+ "modelName": "test-model",
+ "repair": {
+ "kind": "replace",
+ "tool": "target_tool",
+ "args": "{\"value\":\"behavior-repaired\"}"
+ },
+ "input": {
+ "encoding": "base64",
+ "chunks": [
+ "AAAAAB0yGwoMY2FsbF9kZXZpbl8wEgt0YXJnZXRfdG9vbA==",
+ "AAAAABQyEhoQeyJ2YWx1ZSI6Im1vZGVsLQ==",
+ "AAAAAA4yDBoKb3JpZ2luYWwifQ==",
+ "AgAAAAJ7fQ=="
+ ]
+ },
+ "output": "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_<ID>\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"model\":\"test-model\",\"stop_reason\":null,\"stop_sequence\":null,\"usage\":{\"input_tokens\":100,\"output_tokens\":1,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0}}}\n\nevent: ping\ndata: {\"type\":\"ping\"}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"call_devin_0\",\"name\":\"target_tool\",\"input\":{}}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"value\\\":\\\"behavior-repaired\\\"}\"}}\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\nevent: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\",\"stop_sequence\":null},\"usage\":{\"output_tokens\":0}}\n\nevent: message_stop\ndata: {\"type\":\"message_stop\"}\n\n"
+}
diff --git a/packages/cli/src/test-fixtures/parser-goldens/devin-connect.untargeted-incremental.golden.json b/packages/cli/src/test-fixtures/parser-goldens/devin-connect.untargeted-incremental.golden.json
new file mode 100644
index 00000000..c05cc285
--- /dev/null
+++ b/packages/cli/src/test-fixtures/parser-goldens/devin-connect.untargeted-incremental.golden.json
@@ -0,0 +1,22 @@
+{
+ "kind": "parser",
+ "id": "devin-connect.untargeted-incremental",
+ "source": "devin addition: an untargeted tool streams its fragments unchanged",
+ "parser": "devin-connect",
+ "modelName": "test-model",
+ "repair": {
+ "kind": "replace",
+ "tool": "target_tool",
+ "args": "{\"value\":\"behavior-repaired\"}"
+ },
+ "input": {
+ "encoding": "base64",
+ "chunks": [
+ "AAAAACEyHwoMY2FsbF9kZXZpbl8wEg91bnRhcmdldGVkX3Rvb2w=",
+ "AAAAABoyGBoWeyJ2YWx1ZSI6ImluY3JlbWVudGFsLQ==",
+ "AAAAAA4yDBoKZGVsaXZlcnkifQ==",
+ "AgAAAAJ7fQ=="
+ ]
+ },
+ "output": "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_<ID>\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"model\":\"test-model\",\"stop_reason\":null,\"stop_sequence\":null,\"usage\":{\"input_tokens\":100,\"output_tokens\":1,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0}}}\n\nevent: ping\ndata: {\"type\":\"ping\"}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"call_devin_0\",\"name\":\"untargeted_tool\",\"input\":{}}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"value\\\":\\\"incremental-\"}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"delivery\\\"}\"}}\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\nevent: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\",\"stop_sequence\":null},\"usage\":{\"output_tokens\":0}}\n\nevent: message_stop\ndata: {\"type\":\"message_stop\"}\n\n"
+}
diff --git a/packages/cli/src/test-fixtures/parser-goldens/drive-current.ts b/packages/cli/src/test-fixtures/parser-goldens/drive-current.ts
new file mode 100644
index 00000000..d61a4412
--- /dev/null
+++ b/packages/cli/src/test-fixtures/parser-goldens/drive-current.ts
@@ -0,0 +1,217 @@
+/**
+ * Drives the CURRENT parsers — the pre-hook API, with repair plumbing on each
+ * parser and the Gemini middleware on `MiddlewareManager` — over golden inputs.
+ *
+ * This file is bound to the API the hook system deletes. When P4 removes the
+ * parser repair options and P5 removes `middleware/`, this driver goes with
+ * them, and the parity test drives the new parser + seam over the same golden
+ * files instead. The goldens themselves never change.
+ */
+
+import { DefaultAPIFormat } from "../../adapters/base-api-format.js";
+import { createAnthropicPassthroughStream } from "../../handlers/shared/stream-parsers/anthropic-sse.js";
+import { createDevinConnectStream } from "../../handlers/shared/stream-parsers/devin-connect.js";
+import { createGeminiSseStream } from "../../handlers/shared/stream-parsers/gemini-sse.js";
+import { createResponsesStreamHandler } from "../../handlers/shared/stream-parsers/openai-responses-sse.js";
+import { createStreamingResponseHandler } from "../../handlers/shared/stream-parsers/openai-sse.js";
+import { GeminiThoughtSignatureMiddleware, MiddlewareManager } from "../../middleware/index.js";
+import {
+ type GeminiMiddlewareGolden,
+ type GoldenInput,
+ type GoldenRepair,
+ type GoldenToolSchema,
+ type ParserGolden,
+ inputResponse,
+ normalizeWire,
+} from "./golden-format.js";
+
+/**
+ * The minimal Hono context the parsers touch (same stub as behavior-repair.test.ts).
+ *
+ * openai-sse also takes an adapter. behavior-repair.test.ts passed `{}`, which
+ * works only because its inputs carry no text: `{}` has no `processTextContent`,
+ * so the parser's per-chunk catch silently drops every text chunk. The goldens
+ * use the real `DefaultAPIFormat`, as format-translation.test.ts does; for the
+ * tool-only repair inputs the output is byte-identical either way.
+ */
+const ctx: any = {
+ body: (stream: any, init: any) => new Response(stream, init),
+ json: () => {
+ throw new Error("Unexpected no-body error path");
+ },
+};
+
+interface RepairFns {
+ shouldBufferTool: (name: string) => boolean;
+ repair: (name: string, argsJson: string) => string | undefined;
+}
+
+/** The repair rule as the callbacks the old plumbing took (mirrors `fakeSession()`). */
+function repairFns(repair: GoldenRepair): RepairFns | undefined {
+ switch (repair.kind) {
+ case "none":
+ return undefined;
+ case "replace":
+ return {
+ shouldBufferTool: (name) => name === repair.tool,
+ repair: (name) => (name === repair.tool ? repair.args : undefined),
+ };
+ case "throws":
+ return {
+ shouldBufferTool: (name) => name === repair.tool,
+ repair: () => {
+ throw new Error("synthetic repair failure");
+ },
+ };
+ }
+}
+
+export type ParserCaseInput = Omit<ParserGolden, "kind" | "output">;
+
+/** Run one parser case and return the client SSE, normalized. */
+export async function runParserCase(c: ParserCaseInput): Promise<string> {
+ const fns = repairFns(c.repair);
+ const upstream = inputResponse(c.input);
+ let out: Response;
+
+ switch (c.parser) {
+ case "openai-sse":
+ out = createStreamingResponseHandler(
+ ctx,
+ upstream,
+ new DefaultAPIFormat(c.modelName),
+ c.modelName,
+ null,
+ undefined,
+ c.toolSchemas,
+ undefined,
+ undefined,
+ fns && {
+ shouldBufferTool: fns.shouldBufferTool,
+ onToolCall: fns.repair,
+ }
+ );
+ break;
+ case "openai-responses-sse":
+ out = createResponsesStreamHandler(ctx, upstream, {
+ modelName: c.modelName,
+ ...(fns ? { shouldBufferTool: fns.shouldBufferTool, onToolCall: fns.repair } : {}),
+ });
+ break;
+ case "anthropic-sse":
+ out = createAnthropicPassthroughStream(ctx, upstream, {
+ modelName: c.modelName,
+ ...(fns ? { shouldBufferTool: fns.shouldBufferTool, repairToolArgs: fns.repair } : {}),
+ });
+ break;
+ case "gemini-sse":
+ // gemini-sse never buffers (a functionCall arrives whole); the old test
+ // passed `repairToolArgs` alone.
+ out = createGeminiSseStream(ctx, upstream, {
+ modelName: c.modelName,
+ ...(fns ? { repairToolArgs: fns.repair } : {}),
+ });
+ break;
+ case "devin-connect":
+ out = createDevinConnectStream(ctx, upstream, {
+ modelName: c.modelName,
+ ...(fns ? { shouldBufferTool: fns.shouldBufferTool, repairToolArgs: fns.repair } : {}),
+ });
+ break;
+ }
+
+ return normalizeWire(await out.text());
+}
+
+function runOpenAiSse(
+ input: GoldenInput,
+ modelId: string,
+ toolSchemas: GoldenToolSchema[],
+ manager: MiddlewareManager | null
+): Promise<string> {
+ return createStreamingResponseHandler(
+ ctx,
+ inputResponse(input),
+ new DefaultAPIFormat(modelId),
+ modelId,
+ manager,
+ undefined,
+ toolSchemas
+ )
+ .text()
+ .then(normalizeWire);
+}
+
+export interface GeminiScenarioInput {
+ id: string;
+ description: string;
+ streams: GoldenInput[];
+ nextRequestMessages: unknown[];
+}
+
+/**
+ * Run the Gemini middleware the way ComposedHandler wires it: one middleware
+ * instance per handler (its cache lives as long as the handler), streams through
+ * openai-sse with `target = modelId`, then `beforeRequest` on the next request.
+ */
+export async function runGeminiCapture(
+ id: string,
+ modelId: string,
+ toolSchemas: GoldenToolSchema[],
+ applicabilityModels: string[],
+ scenarios: GeminiScenarioInput[]
+): Promise<GeminiMiddlewareGolden> {
+ const probe = new GeminiThoughtSignatureMiddleware();
+ const applicability = applicabilityModels.map((m) => {
+ const manager = new MiddlewareManager();
+ manager.register(new GeminiThoughtSignatureMiddleware());
+ return {
+ modelId: m,
+ shouldHandle: probe.shouldHandle(m),
+ activeNames: manager.getActiveNames(m),
+ };
+ });
+
+ const outScenarios: GeminiMiddlewareGolden["scenarios"] = [];
+ for (const scenario of scenarios) {
+ const manager = new MiddlewareManager();
+ manager.register(new GeminiThoughtSignatureMiddleware());
+ await manager.initialize();
+
+ const streams: GeminiMiddlewareGolden["scenarios"][number]["streams"] = [];
+ for (const input of scenario.streams) {
+ const output = await runOpenAiSse(input, modelId, toolSchemas, manager);
+ const without = await runOpenAiSse(input, modelId, toolSchemas, null);
+ streams.push({ input, output, outputUnchangedByMiddleware: output === without });
+ }
+
+ const before = structuredClone(scenario.nextRequestMessages) as any[];
+ const messages = structuredClone(scenario.nextRequestMessages) as any[];
+ await manager.beforeRequest({
+ modelId,
+ messages,
+ tools: [],
+ stream: true,
+ claudeRequest: {},
+ claudeTools: [],
+ });
+ // Round-trip through JSON: the golden records what reaches the wire.
+ const after = JSON.parse(JSON.stringify(messages));
+
+ outScenarios.push({
+ id: scenario.id,
+ description: scenario.description,
+ streams,
+ nextRequest: { before, after },
+ });
+ }
+
+ return {
+ kind: "gemini-middleware",
+ id,
+ modelId,
+ toolSchemas,
+ applicability,
+ scenarios: outScenarios,
+ };
+}
diff --git a/packages/cli/src/test-fixtures/parser-goldens/gemini-sse.targeted-repaired.golden.json b/packages/cli/src/test-fixtures/parser-goldens/gemini-sse.targeted-repaired.golden.json
new file mode 100644
index 00000000..0b642c1d
--- /dev/null
+++ b/packages/cli/src/test-fixtures/parser-goldens/gemini-sse.targeted-repaired.golden.json
@@ -0,0 +1,19 @@
+{
+ "kind": "parser",
+ "id": "gemini-sse.targeted-repaired",
+ "source": "gemini-sse behavior repair > rewrites arguments for a targeted tool",
+ "parser": "gemini-sse",
+ "modelName": "test-model",
+ "repair": {
+ "kind": "replace",
+ "tool": "target_tool",
+ "args": "{\"value\":\"behavior-repaired\"}"
+ },
+ "input": {
+ "encoding": "utf8",
+ "chunks": [
+ "data: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"functionCall\":{\"name\":\"target_tool\",\"args\":{\"value\":\"model-original\"}}}]},\"finishReason\":\"STOP\"}]}\n\n"
+ ]
+ },
+ "output": "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_<ID>\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"model\":\"test-model\",\"stop_reason\":null,\"stop_sequence\":null,\"usage\":{\"input_tokens\":100,\"output_tokens\":1,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0}}}\n\nevent: ping\ndata: {\"type\":\"ping\"}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"toolu_<TS>_0\",\"name\":\"target_tool\"}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"value\\\":\\\"behavior-repaired\\\"}\"}}\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\nevent: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\",\"stop_sequence\":null},\"usage\":{\"output_tokens\":0}}\n\nevent: message_stop\ndata: {\"type\":\"message_stop\"}\n\n"
+}
diff --git a/packages/cli/src/test-fixtures/parser-goldens/gemini-sse.untargeted-unchanged.golden.json b/packages/cli/src/test-fixtures/parser-goldens/gemini-sse.untargeted-unchanged.golden.json
new file mode 100644
index 00000000..d3541df9
--- /dev/null
+++ b/packages/cli/src/test-fixtures/parser-goldens/gemini-sse.untargeted-unchanged.golden.json
@@ -0,0 +1,19 @@
+{
+ "kind": "parser",
+ "id": "gemini-sse.untargeted-unchanged",
+ "source": "gemini-sse behavior repair > passes an untargeted tool through unchanged",
+ "parser": "gemini-sse",
+ "modelName": "test-model",
+ "repair": {
+ "kind": "replace",
+ "tool": "target_tool",
+ "args": "{\"value\":\"behavior-repaired\"}"
+ },
+ "input": {
+ "encoding": "utf8",
+ "chunks": [
+ "data: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"functionCall\":{\"name\":\"untargeted_tool\",\"args\":{\"value\":\"model-original\"}}}]},\"finishReason\":\"STOP\"}]}\n\n"
+ ]
+ },
+ "output": "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_<ID>\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"model\":\"test-model\",\"stop_reason\":null,\"stop_sequence\":null,\"usage\":{\"input_tokens\":100,\"output_tokens\":1,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0}}}\n\nevent: ping\ndata: {\"type\":\"ping\"}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"toolu_<TS>_0\",\"name\":\"untargeted_tool\"}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"value\\\":\\\"model-original\\\"}\"}}\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\nevent: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\",\"stop_sequence\":null},\"usage\":{\"output_tokens\":0}}\n\nevent: message_stop\ndata: {\"type\":\"message_stop\"}\n\n"
+}
diff --git a/packages/cli/src/test-fixtures/parser-goldens/gemini-thought-signature.openai-sse.golden.json b/packages/cli/src/test-fixtures/parser-goldens/gemini-thought-signature.openai-sse.golden.json
new file mode 100644
index 00000000..cc787d4b
--- /dev/null
+++ b/packages/cli/src/test-fixtures/parser-goldens/gemini-thought-signature.openai-sse.golden.json
@@ -0,0 +1,532 @@
+{
+ "kind": "gemini-middleware",
+ "id": "gemini-thought-signature.openai-sse",
+ "modelId": "google/gemini-3.1-pro-preview",
+ "toolSchemas": [
+ {
+ "name": "Read",
+ "input_schema": {
+ "type": "object",
+ "properties": {
+ "file_path": {
+ "type": "string"
+ }
+ },
+ "required": ["file_path"]
+ }
+ }
+ ],
+ "applicability": [
+ {
+ "modelId": "google/gemini-3.1-pro-preview",
+ "shouldHandle": true,
+ "activeNames": ["GeminiThoughtSignature"]
+ },
+ {
+ "modelId": "gemini-2.5-flash",
+ "shouldHandle": true,
+ "activeNames": ["GeminiThoughtSignature"]
+ },
+ {
+ "modelId": "google/gemma-3-27b-it",
+ "shouldHandle": true,
+ "activeNames": ["GeminiThoughtSignature"]
+ },
+ {
+ "modelId": "gpt-5.6-sol",
+ "shouldHandle": false,
+ "activeNames": []
+ },
+ {
+ "modelId": "claude-opus-5",
+ "shouldHandle": false,
+ "activeNames": []
+ }
+ ],
+ "scenarios": [
+ {
+ "id": "tool-call-with-reasoning",
+ "description": "reasoning_details accumulated across two chunks are cached against the turn's tool_call id and injected into the matching assistant message of the next request; an assistant tool call with no cached id is left alone",
+ "streams": [
+ {
+ "input": {
+ "encoding": "utf8",
+ "chunks": [
+ "data: {\"id\":\"gen-golden\",\"object\":\"chat.completion.chunk\",\"model\":\"google/gemini-3.1-pro-preview\",\"provider\":\"Google\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"Reading it.\"},\"finish_reason\":null,\"native_finish_reason\":null}]}\n\n",
+ "data: {\"id\":\"gen-golden\",\"object\":\"chat.completion.chunk\",\"model\":\"google/gemini-3.1-pro-preview\",\"provider\":\"Google\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"\",\"reasoning_details\":[{\"type\":\"reasoning.text\",\"text\":\"\",\"signature\":\"SIG-A-0\",\"format\":\"google-gemini-v1\",\"index\":0}]},\"finish_reason\":null,\"native_finish_reason\":null}]}\n\n",
+ "data: {\"id\":\"gen-golden\",\"object\":\"chat.completion.chunk\",\"model\":\"google/gemini-3.1-pro-preview\",\"provider\":\"Google\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"\",\"reasoning_details\":[{\"type\":\"reasoning.text\",\"text\":\"\",\"signature\":\"SIG-A-1\",\"format\":\"google-gemini-v1\",\"index\":1}],\"tool_calls\":[{\"index\":0,\"id\":\"call_gem_a\",\"type\":\"function\",\"function\":{\"name\":\"Read\",\"arguments\":\"\"}}]},\"finish_reason\":null,\"native_finish_reason\":null}]}\n\n",
+ "data: {\"id\":\"gen-golden\",\"object\":\"chat.completion.chunk\",\"model\":\"google/gemini-3.1-pro-preview\",\"provider\":\"Google\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"{\\\"file_path\\\":\\\"/tmp/a\\\"}\"}}]},\"finish_reason\":null,\"native_finish_reason\":null}]}\n\n",
+ "data: {\"id\":\"gen-golden\",\"object\":\"chat.completion.chunk\",\"model\":\"google/gemini-3.1-pro-preview\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"tool_calls\"}],\"usage\":{\"prompt_tokens\":40,\"completion_tokens\":12,\"total_tokens\":52}}\n\n",
+ "data: [DONE]\n\n"
+ ]
+ },
+ "output": "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_<ID>\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"model\":\"google/gemini-3.1-pro-preview\",\"stop_reason\":null,\"stop_sequence\":null,\"usage\":{\"input_tokens\":100,\"output_tokens\":1,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0}}}\n\nevent: ping\ndata: {\"type\":\"ping\"}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"Reading it.\"}}\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":1,\"content_block\":{\"type\":\"tool_use\",\"id\":\"call_gem_a\",\"name\":\"Read\",\"input\":{}}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":1,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"file_path\\\":\\\"/tmp/a\\\"}\"}}\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":1}\n\nevent: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\",\"stop_sequence\":null},\"usage\":{\"input_tokens\":40,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0,\"output_tokens\":12}}\n\nevent: message_stop\ndata: {\"type\":\"message_stop\"}\n\ndata: [DONE]\n\n\n",
+ "outputUnchangedByMiddleware": true
+ }
+ ],
+ "nextRequest": {
+ "before": [
+ {
+ "role": "system",
+ "content": "You are a coding agent."
+ },
+ {
+ "role": "user",
+ "content": "Read the file."
+ },
+ {
+ "role": "assistant",
+ "content": "Reading it.",
+ "tool_calls": [
+ {
+ "id": "call_gem_a",
+ "type": "function",
+ "function": {
+ "name": "Read",
+ "arguments": "{\"file_path\":\"/tmp/call_gem_a\"}"
+ }
+ }
+ ]
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_gem_a",
+ "content": "contents of call_gem_a"
+ },
+ {
+ "role": "assistant",
+ "content": "Reading it.",
+ "tool_calls": [
+ {
+ "id": "call_unknown",
+ "type": "function",
+ "function": {
+ "name": "Read",
+ "arguments": "{\"file_path\":\"/tmp/call_unknown\"}"
+ }
+ }
+ ]
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_unknown",
+ "content": "contents of call_unknown"
+ },
+ {
+ "role": "user",
+ "content": "Thanks."
+ }
+ ],
+ "after": [
+ {
+ "role": "system",
+ "content": "You are a coding agent."
+ },
+ {
+ "role": "user",
+ "content": "Read the file."
+ },
+ {
+ "role": "assistant",
+ "content": "Reading it.",
+ "tool_calls": [
+ {
+ "id": "call_gem_a",
+ "type": "function",
+ "function": {
+ "name": "Read",
+ "arguments": "{\"file_path\":\"/tmp/call_gem_a\"}"
+ }
+ }
+ ],
+ "reasoning_details": [
+ {
+ "type": "reasoning.text",
+ "text": "",
+ "signature": "SIG-A-0",
+ "format": "google-gemini-v1",
+ "index": 0
+ },
+ {
+ "type": "reasoning.text",
+ "text": "",
+ "signature": "SIG-A-1",
+ "format": "google-gemini-v1",
+ "index": 1
+ }
+ ]
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_gem_a",
+ "content": "contents of call_gem_a"
+ },
+ {
+ "role": "assistant",
+ "content": "Reading it.",
+ "tool_calls": [
+ {
+ "id": "call_unknown",
+ "type": "function",
+ "function": {
+ "name": "Read",
+ "arguments": "{\"file_path\":\"/tmp/call_unknown\"}"
+ }
+ }
+ ]
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_unknown",
+ "content": "contents of call_unknown"
+ },
+ {
+ "role": "user",
+ "content": "Thanks."
+ }
+ ]
+ }
+ },
+ {
+ "id": "two-turns",
+ "description": "two streams cache two entries; each assistant message receives the reasoning_details of its own turn only",
+ "streams": [
+ {
+ "input": {
+ "encoding": "utf8",
+ "chunks": [
+ "data: {\"id\":\"gen-golden\",\"object\":\"chat.completion.chunk\",\"model\":\"google/gemini-3.1-pro-preview\",\"provider\":\"Google\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"Reading it.\"},\"finish_reason\":null,\"native_finish_reason\":null}]}\n\n",
+ "data: {\"id\":\"gen-golden\",\"object\":\"chat.completion.chunk\",\"model\":\"google/gemini-3.1-pro-preview\",\"provider\":\"Google\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"\",\"reasoning_details\":[{\"type\":\"reasoning.text\",\"text\":\"\",\"signature\":\"SIG-A-0\",\"format\":\"google-gemini-v1\",\"index\":0}]},\"finish_reason\":null,\"native_finish_reason\":null}]}\n\n",
+ "data: {\"id\":\"gen-golden\",\"object\":\"chat.completion.chunk\",\"model\":\"google/gemini-3.1-pro-preview\",\"provider\":\"Google\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"\",\"reasoning_details\":[{\"type\":\"reasoning.text\",\"text\":\"\",\"signature\":\"SIG-A-1\",\"format\":\"google-gemini-v1\",\"index\":1}],\"tool_calls\":[{\"index\":0,\"id\":\"call_gem_a\",\"type\":\"function\",\"function\":{\"name\":\"Read\",\"arguments\":\"\"}}]},\"finish_reason\":null,\"native_finish_reason\":null}]}\n\n",
+ "data: {\"id\":\"gen-golden\",\"object\":\"chat.completion.chunk\",\"model\":\"google/gemini-3.1-pro-preview\",\"provider\":\"Google\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"{\\\"file_path\\\":\\\"/tmp/a\\\"}\"}}]},\"finish_reason\":null,\"native_finish_reason\":null}]}\n\n",
+ "data: {\"id\":\"gen-golden\",\"object\":\"chat.completion.chunk\",\"model\":\"google/gemini-3.1-pro-preview\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"tool_calls\"}],\"usage\":{\"prompt_tokens\":40,\"completion_tokens\":12,\"total_tokens\":52}}\n\n",
+ "data: [DONE]\n\n"
+ ]
+ },
+ "output": "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_<ID>\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"model\":\"google/gemini-3.1-pro-preview\",\"stop_reason\":null,\"stop_sequence\":null,\"usage\":{\"input_tokens\":100,\"output_tokens\":1,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0}}}\n\nevent: ping\ndata: {\"type\":\"ping\"}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"Reading it.\"}}\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":1,\"content_block\":{\"type\":\"tool_use\",\"id\":\"call_gem_a\",\"name\":\"Read\",\"input\":{}}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":1,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"file_path\\\":\\\"/tmp/a\\\"}\"}}\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":1}\n\nevent: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\",\"stop_sequence\":null},\"usage\":{\"input_tokens\":40,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0,\"output_tokens\":12}}\n\nevent: message_stop\ndata: {\"type\":\"message_stop\"}\n\ndata: [DONE]\n\n\n",
+ "outputUnchangedByMiddleware": true
+ },
+ {
+ "input": {
+ "encoding": "utf8",
+ "chunks": [
+ "data: {\"id\":\"gen-golden\",\"object\":\"chat.completion.chunk\",\"model\":\"google/gemini-3.1-pro-preview\",\"provider\":\"Google\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"Reading it.\"},\"finish_reason\":null,\"native_finish_reason\":null}]}\n\n",
+ "data: {\"id\":\"gen-golden\",\"object\":\"chat.completion.chunk\",\"model\":\"google/gemini-3.1-pro-preview\",\"provider\":\"Google\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"\",\"reasoning_details\":[{\"type\":\"reasoning.text\",\"text\":\"\",\"signature\":\"SIG-B-0\",\"format\":\"google-gemini-v1\",\"index\":0}]},\"finish_reason\":null,\"native_finish_reason\":null}]}\n\n",
+ "data: {\"id\":\"gen-golden\",\"object\":\"chat.completion.chunk\",\"model\":\"google/gemini-3.1-pro-preview\",\"provider\":\"Google\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"\",\"reasoning_details\":[{\"type\":\"reasoning.text\",\"text\":\"\",\"signature\":\"SIG-B-1\",\"format\":\"google-gemini-v1\",\"index\":1}],\"tool_calls\":[{\"index\":0,\"id\":\"call_gem_b\",\"type\":\"function\",\"function\":{\"name\":\"Read\",\"arguments\":\"\"}}]},\"finish_reason\":null,\"native_finish_reason\":null}]}\n\n",
+ "data: {\"id\":\"gen-golden\",\"object\":\"chat.completion.chunk\",\"model\":\"google/gemini-3.1-pro-preview\",\"provider\":\"Google\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"{\\\"file_path\\\":\\\"/tmp/b\\\"}\"}}]},\"finish_reason\":null,\"native_finish_reason\":null}]}\n\n",
+ "data: {\"id\":\"gen-golden\",\"object\":\"chat.completion.chunk\",\"model\":\"google/gemini-3.1-pro-preview\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"tool_calls\"}],\"usage\":{\"prompt_tokens\":40,\"completion_tokens\":12,\"total_tokens\":52}}\n\n",
+ "data: [DONE]\n\n"
+ ]
+ },
+ "output": "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_<ID>\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"model\":\"google/gemini-3.1-pro-preview\",\"stop_reason\":null,\"stop_sequence\":null,\"usage\":{\"input_tokens\":100,\"output_tokens\":1,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0}}}\n\nevent: ping\ndata: {\"type\":\"ping\"}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"Reading it.\"}}\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":1,\"content_block\":{\"type\":\"tool_use\",\"id\":\"call_gem_b\",\"name\":\"Read\",\"input\":{}}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":1,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"file_path\\\":\\\"/tmp/b\\\"}\"}}\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":1}\n\nevent: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\",\"stop_sequence\":null},\"usage\":{\"input_tokens\":40,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0,\"output_tokens\":12}}\n\nevent: message_stop\ndata: {\"type\":\"message_stop\"}\n\ndata: [DONE]\n\n\n",
+ "outputUnchangedByMiddleware": true
+ }
+ ],
+ "nextRequest": {
+ "before": [
+ {
+ "role": "system",
+ "content": "You are a coding agent."
+ },
+ {
+ "role": "user",
+ "content": "Read the file."
+ },
+ {
+ "role": "assistant",
+ "content": "Reading it.",
+ "tool_calls": [
+ {
+ "id": "call_gem_a",
+ "type": "function",
+ "function": {
+ "name": "Read",
+ "arguments": "{\"file_path\":\"/tmp/call_gem_a\"}"
+ }
+ }
+ ]
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_gem_a",
+ "content": "contents of call_gem_a"
+ },
+ {
+ "role": "assistant",
+ "content": "Reading it.",
+ "tool_calls": [
+ {
+ "id": "call_gem_b",
+ "type": "function",
+ "function": {
+ "name": "Read",
+ "arguments": "{\"file_path\":\"/tmp/call_gem_b\"}"
+ }
+ }
+ ]
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_gem_b",
+ "content": "contents of call_gem_b"
+ },
+ {
+ "role": "user",
+ "content": "Thanks."
+ }
+ ],
+ "after": [
+ {
+ "role": "system",
+ "content": "You are a coding agent."
+ },
+ {
+ "role": "user",
+ "content": "Read the file."
+ },
+ {
+ "role": "assistant",
+ "content": "Reading it.",
+ "tool_calls": [
+ {
+ "id": "call_gem_a",
+ "type": "function",
+ "function": {
+ "name": "Read",
+ "arguments": "{\"file_path\":\"/tmp/call_gem_a\"}"
+ }
+ }
+ ],
+ "reasoning_details": [
+ {
+ "type": "reasoning.text",
+ "text": "",
+ "signature": "SIG-A-0",
+ "format": "google-gemini-v1",
+ "index": 0
+ },
+ {
+ "type": "reasoning.text",
+ "text": "",
+ "signature": "SIG-A-1",
+ "format": "google-gemini-v1",
+ "index": 1
+ }
+ ]
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_gem_a",
+ "content": "contents of call_gem_a"
+ },
+ {
+ "role": "assistant",
+ "content": "Reading it.",
+ "tool_calls": [
+ {
+ "id": "call_gem_b",
+ "type": "function",
+ "function": {
+ "name": "Read",
+ "arguments": "{\"file_path\":\"/tmp/call_gem_b\"}"
+ }
+ }
+ ],
+ "reasoning_details": [
+ {
+ "type": "reasoning.text",
+ "text": "",
+ "signature": "SIG-B-0",
+ "format": "google-gemini-v1",
+ "index": 0
+ },
+ {
+ "type": "reasoning.text",
+ "text": "",
+ "signature": "SIG-B-1",
+ "format": "google-gemini-v1",
+ "index": 1
+ }
+ ]
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_gem_b",
+ "content": "contents of call_gem_b"
+ },
+ {
+ "role": "user",
+ "content": "Thanks."
+ }
+ ]
+ }
+ },
+ {
+ "id": "reasoning-without-tool-call",
+ "description": "a recorded OpenRouter Gemini stream with reasoning_details and no tool call caches nothing; the next request is unchanged",
+ "streams": [
+ {
+ "input": {
+ "encoding": "utf8",
+ "chunks": [
+ ": OPENROUTER PROCESSING\n\n",
+ ": OPENROUTER PROCESSING\n\n",
+ ": OPENROUTER PROCESSING\n\n",
+ ": OPENROUTER PROCESSING\n\n",
+ ": OPENROUTER PROCESSING\n\n",
+ ": OPENROUTER PROCESSING\n\n",
+ "data: {\"id\":\"gen-1786019222-rxepGMSzUjKHovdSBvao\",\"object\":\"chat.completion.chunk\",\"created\":1786019222,\"model\":\"google/gemini-3.1-pro-preview\",\"provider\":\"Google\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Hi marker x\",\"role\":\"assistant\"},\"finish_reason\":null,\"native_finish_reason\":null}]}\n\n",
+ "data: {\"id\":\"gen-1786019222-rxepGMSzUjKHovdSBvao\",\"object\":\"chat.completion.chunk\",\"created\":1786019222,\"model\":\"google/gemini-3.1-pro-preview\",\"provider\":\"Google\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"role\":\"assistant\",\"reasoning_details\":[{\"type\":\"reasoning.text\",\"signature\":\"AY89a1/7m6PhGCXRUL92eU92u6fzL+acUwWK5MMWEOheIkr+a3zfbjDXzbacH86H5Lxx1HDY80t9UTpPV+jxs+k1rlFoFvVUYNSsFZ1DyfncDFu6xSwMS+vTg1VpJdrVuCWYGqfu8vNVWoDeImDMsdPMH69ko3v1AwEXJqZkcaSgR1H1GucgSH2NyhLWNTtS9HOjUytLl83GVRSqmjpfS4kPYIdEZ+jkA5pWOKZVPkM+8p5/VQVuBIEOnCOpRh0ICjjB8spGh8RpEAluvDCXNJKS3oucIOPb+U6j3iPEMW6NY7P7/S64TfBTj5kN5qPM6vcLsMBO42QPSlyxX7r0F/RO89k9df8C9UcrZko0beMyGNUqnVdvB9xufikwUxAiutmRaRfXvh6S5TOosXF67PPToZUm\",\"format\":\"google-gemini-v1\",\"index\":0}]},\"finish_reason\":null,\"native_finish_reason\":null}]}\n\n",
+ "data: {\"id\":\"gen-1786019222-rxepGMSzUjKHovdSBvao\",\"object\":\"chat.completion.chunk\",\"created\":1786019222,\"model\":\"google/gemini-3.1-pro-preview\",\"provider\":\"Google\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"role\":\"assistant\",\"reasoning\":null},\"finish_reason\":\"length\",\"native_finish_reason\":\"MAX_TOKENS\"}]}\n\n",
+ "data: {\"id\":\"gen-1786019222-rxepGMSzUjKHovdSBvao\",\"object\":\"chat.completion.chunk\",\"created\":1786019222,\"model\":\"google/gemini-3.1-pro-preview\",\"provider\":\"Google\",\"service_tier\":\"default\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"role\":\"assistant\"},\"finish_reason\":\"length\",\"native_finish_reason\":\"MAX_TOKENS\"}],\"usage\":{\"prompt_tokens\":8,\"completion_tokens\":60,\"total_tokens\":68,\"cost\":0.000736,\"is_byok\":false,\"prompt_tokens_details\":{\"cached_tokens\":0,\"cache_write_tokens\":0,\"audio_tokens\":0,\"video_tokens\":0},\"cost_details\":{\"upstream_inference_cost\":0.000736,\"upstream_inference_prompt_cost\":0.000016,\"upstream_inference_completions_cost\":0.00072},\"completion_tokens_details\":{\"reasoning_tokens\":57,\"image_tokens\":0,\"audio_tokens\":0}}}\n\n",
+ "data: [DONE]\n\n"
+ ]
+ },
+ "output": "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_<ID>\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"model\":\"google/gemini-3.1-pro-preview\",\"stop_reason\":null,\"stop_sequence\":null,\"usage\":{\"input_tokens\":100,\"output_tokens\":1,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0}}}\n\nevent: ping\ndata: {\"type\":\"ping\"}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"Hi marker x\"}}\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\nevent: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"max_tokens\",\"stop_sequence\":null},\"usage\":{\"input_tokens\":8,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0,\"output_tokens\":60}}\n\nevent: message_stop\ndata: {\"type\":\"message_stop\"}\n\ndata: [DONE]\n\n\n",
+ "outputUnchangedByMiddleware": true
+ }
+ ],
+ "nextRequest": {
+ "before": [
+ {
+ "role": "system",
+ "content": "You are a coding agent."
+ },
+ {
+ "role": "user",
+ "content": "Read the file."
+ },
+ {
+ "role": "assistant",
+ "content": "Reading it.",
+ "tool_calls": [
+ {
+ "id": "call_gem_a",
+ "type": "function",
+ "function": {
+ "name": "Read",
+ "arguments": "{\"file_path\":\"/tmp/call_gem_a\"}"
+ }
+ }
+ ]
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_gem_a",
+ "content": "contents of call_gem_a"
+ },
+ {
+ "role": "user",
+ "content": "Thanks."
+ }
+ ],
+ "after": [
+ {
+ "role": "system",
+ "content": "You are a coding agent."
+ },
+ {
+ "role": "user",
+ "content": "Read the file."
+ },
+ {
+ "role": "assistant",
+ "content": "Reading it.",
+ "tool_calls": [
+ {
+ "id": "call_gem_a",
+ "type": "function",
+ "function": {
+ "name": "Read",
+ "arguments": "{\"file_path\":\"/tmp/call_gem_a\"}"
+ }
+ }
+ ]
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_gem_a",
+ "content": "contents of call_gem_a"
+ },
+ {
+ "role": "user",
+ "content": "Thanks."
+ }
+ ]
+ }
+ },
+ {
+ "id": "no-prior-stream",
+ "description": "with an empty cache, beforeRequest leaves the request unchanged",
+ "streams": [],
+ "nextRequest": {
+ "before": [
+ {
+ "role": "system",
+ "content": "You are a coding agent."
+ },
+ {
+ "role": "user",
+ "content": "Read the file."
+ },
+ {
+ "role": "assistant",
+ "content": "Reading it.",
+ "tool_calls": [
+ {
+ "id": "call_gem_a",
+ "type": "function",
+ "function": {
+ "name": "Read",
+ "arguments": "{\"file_path\":\"/tmp/call_gem_a\"}"
+ }
+ }
+ ]
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_gem_a",
+ "content": "contents of call_gem_a"
+ },
+ {
+ "role": "user",
+ "content": "Thanks."
+ }
+ ],
+ "after": [
+ {
+ "role": "system",
+ "content": "You are a coding agent."
+ },
+ {
+ "role": "user",
+ "content": "Read the file."
+ },
+ {
+ "role": "assistant",
+ "content": "Reading it.",
+ "tool_calls": [
+ {
+ "id": "call_gem_a",
+ "type": "function",
+ "function": {
+ "name": "Read",
+ "arguments": "{\"file_path\":\"/tmp/call_gem_a\"}"
+ }
+ }
+ ]
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_gem_a",
+ "content": "contents of call_gem_a"
+ },
+ {
+ "role": "user",
+ "content": "Thanks."
+ }
+ ]
+ }
+ }
+ ]
+}
diff --git a/packages/cli/src/test-fixtures/parser-goldens/golden-format.ts b/packages/cli/src/test-fixtures/parser-goldens/golden-format.ts
new file mode 100644
index 00000000..016730a6
--- /dev/null
+++ b/packages/cli/src/test-fixtures/parser-goldens/golden-format.ts
@@ -0,0 +1,207 @@
+/**
+ * Parser goldens — the format shared by the capture script and every test that
+ * replays them.
+ *
+ * A golden is a snapshot of what the stream parsers emitted BEFORE the hook
+ * system replaced their repair plumbing (captured at base 2e4ac246, phase P0).
+ * The rewrite moves tool-argument repair out of the parsers and into the
+ * response seam; these files are the contract that move must keep. So a golden
+ * is self-contained and names no parser API: the provider wire input, the
+ * repair rule as data, and the Claude SSE that reached the client.
+ *
+ * Never re-capture to make a parity test pass. A golden captured from the new
+ * code proves the new code equals itself. `capture.ts` refuses to overwrite.
+ */
+
+import { readFileSync, readdirSync } from "node:fs";
+import { join } from "node:path";
+
+export const GOLDENS_DIR = import.meta.dir;
+
+/** The five parsers that carried repair plumbing at the capture base. */
+export type GoldenParser =
+ | "openai-sse"
+ | "openai-responses-sse"
+ | "anthropic-sse"
+ | "gemini-sse"
+ | "devin-connect";
+
+/**
+ * The repair rule the parser was armed with, as data.
+ *
+ * - `none`: no repair plumbing passed at all (the baseline a parser has with no
+ * rule armed).
+ * - `replace`: the arguments of `tool` are withheld until the call completes and
+ * replaced by `args`; every other tool passes untouched.
+ * - `throws`: `tool` is withheld, and the repair function throws for every call.
+ * The parser must emit the model's own arguments.
+ */
+export type GoldenRepair =
+ | { kind: "none" }
+ | { kind: "replace"; tool: string; args: string }
+ | { kind: "throws"; tool: string };
+
+/**
+ * Provider wire input, one entry per network chunk in arrival order. Text
+ * formats are stored as text; Devin's Connect-protobuf frames as base64.
+ */
+export type GoldenInput =
+ | { encoding: "utf8"; chunks: string[] }
+ | { encoding: "base64"; chunks: string[] };
+
+/** Claude-format tool schemas the request carried (openai-sse buffers on these). */
+export interface GoldenToolSchema {
+ name: string;
+ input_schema: Record<string, unknown>;
+}
+
+export interface ParserGolden {
+ kind: "parser";
+ id: string;
+ /** Which `behavior-repair.test.ts` case (or devin addition) this captures. */
+ source: string;
+ parser: GoldenParser;
+ modelName: string;
+ toolSchemas?: GoldenToolSchema[];
+ repair: GoldenRepair;
+ input: GoldenInput;
+ /** The Claude SSE the client received, through `normalizeWire`. */
+ output: string;
+ /**
+ * Set when the captured output carries a defect of the pre-hook code. Such a
+ * golden records what shipped, but a rewrite must not reproduce the defect
+ * byte for byte. Its parity check compares `dataPayloads()` (the ordered JSON
+ * payloads, independent of how `event:` lines frame them) instead of bytes.
+ */
+ defect?: string;
+}
+
+/**
+ * A request as the Gemini middleware's `beforeRequest` saw it: the OpenAI-format
+ * message list after Claude → OpenAI conversion. Only `messages` is touched by
+ * the middleware, so only `messages` is recorded.
+ */
+export interface GeminiRequestSnapshot {
+ before: unknown[];
+ after: unknown[];
+}
+
+export interface GeminiMiddlewareScenario {
+ id: string;
+ description: string;
+ /** Streams run, in order, through openai-sse with the middleware active. */
+ streams: Array<{
+ input: GoldenInput;
+ /** Client SSE with the middleware active, through `normalizeWire`. */
+ output: string;
+ /** True when the same stream without the middleware emitted the same bytes. */
+ outputUnchangedByMiddleware: boolean;
+ }>;
+ /** The next request after every stream above, before and after `beforeRequest`. */
+ nextRequest: GeminiRequestSnapshot;
+}
+
+export interface GeminiMiddlewareGolden {
+ kind: "gemini-middleware";
+ id: string;
+ /** The `modelId` and openai-sse `target` the middleware ran under. */
+ modelId: string;
+ toolSchemas: GoldenToolSchema[];
+ /**
+ * `shouldHandle(modelId)` per model id, and the `middleware_names` stats value
+ * (`getActiveNames`) a handler for that model reports. The registration gate in
+ * `ComposedHandler` uses the same substring rule as `shouldHandle`.
+ */
+ applicability: Array<{ modelId: string; shouldHandle: boolean; activeNames: string[] }>;
+ scenarios: GeminiMiddlewareScenario[];
+}
+
+export type Golden = ParserGolden | GeminiMiddlewareGolden;
+
+/** Golden file suffix. One file per golden id. */
+export const GOLDEN_SUFFIX = ".golden.json";
+
+export function goldenPath(id: string): string {
+ return join(GOLDENS_DIR, `${id}${GOLDEN_SUFFIX}`);
+}
+
+export function readGolden(id: string): Golden {
+ return JSON.parse(readFileSync(goldenPath(id), "utf8")) as Golden;
+}
+
+export function listGoldenIds(): string[] {
+ return readdirSync(GOLDENS_DIR)
+ .filter((f) => f.endsWith(GOLDEN_SUFFIX))
+ .map((f) => f.slice(0, -GOLDEN_SUFFIX.length))
+ .sort();
+}
+
+export function decodeInput(input: GoldenInput): Uint8Array[] {
+ const encoder = new TextEncoder();
+ return input.chunks.map((chunk) =>
+ input.encoding === "utf8" ? encoder.encode(chunk) : Buffer.from(chunk, "base64")
+ );
+}
+
+/** A provider response that delivers `input` chunk by chunk, then closes. */
+export function inputResponse(input: GoldenInput): Response {
+ const chunks = decodeInput(input);
+ return new Response(
+ new ReadableStream({
+ start(controller) {
+ for (const chunk of chunks) controller.enqueue(chunk);
+ controller.close();
+ },
+ }),
+ {
+ headers: {
+ "content-type":
+ input.encoding === "utf8" ? "text/event-stream" : "application/connect+proto",
+ },
+ }
+ );
+}
+
+/**
+ * Replace the only clock- and random-derived values the parsers emit, so a
+ * capture is reproducible:
+ *
+ * - message ids: `msg_<ms>` and `msg_<ms>_<random>`
+ * - synthesized tool ids: `tool_<ms>_<n>`, `toolu_<ms>_<n>`, `tool_repaired_<ms>_<n>`
+ *
+ * Everything else, keep-alive `ping` events included, is kept byte for byte.
+ * The capture script runs every case twice, apart in time, and refuses to write
+ * a golden whose two normalized captures differ.
+ */
+export function normalizeWire(wire: string): string {
+ return wire
+ .replace(/"msg_\d+(?:_[a-z0-9]+)?"/g, '"msg_<ID>"')
+ .replace(/\b(tool_repaired|toolu|tool)_\d{10,}_(\d+)\b/g, "$1_<TS>_$2");
+}
+
+const PING_FRAME = 'event: ping\ndata: {"type":"ping"}\n\n';
+
+/** The wire without keep-alive pings, for comparisons where ping timing differs. */
+export function stripPings(wire: string): string {
+ return wire.replaceAll(PING_FRAME, "");
+}
+
+/**
+ * Every `data:` payload in wire order, parsed, pings excluded. The comparison
+ * for a golden with a `defect`: it holds the content and order of what was
+ * sent while ignoring how the frames were delimited.
+ */
+export function dataPayloads(wire: string): unknown[] {
+ const payloads: unknown[] = [];
+ for (const line of wire.split("\n")) {
+ if (!line.startsWith("data: ")) continue;
+ const raw = line.slice(6);
+ if (raw === "[DONE]") {
+ payloads.push("[DONE]");
+ continue;
+ }
+ const parsed = JSON.parse(raw) as { type?: string };
+ if (parsed.type !== "ping") payloads.push(parsed);
+ }
+ return payloads;
+}
diff --git a/packages/cli/src/test-fixtures/parser-goldens/openai-responses-sse.targeted-repaired.golden.json b/packages/cli/src/test-fixtures/parser-goldens/openai-responses-sse.targeted-repaired.golden.json
new file mode 100644
index 00000000..9f478f8c
--- /dev/null
+++ b/packages/cli/src/test-fixtures/parser-goldens/openai-responses-sse.targeted-repaired.golden.json
@@ -0,0 +1,22 @@
+{
+ "kind": "parser",
+ "id": "openai-responses-sse.targeted-repaired",
+ "source": "openai-responses-sse behavior repair > rewrites arguments for a targeted tool",
+ "parser": "openai-responses-sse",
+ "modelName": "test-model",
+ "repair": {
+ "kind": "replace",
+ "tool": "target_tool",
+ "args": "{\"value\":\"behavior-repaired\"}"
+ },
+ "input": {
+ "encoding": "utf8",
+ "chunks": [
+ "data: {\"type\":\"response.output_item.added\",\"item\":{\"type\":\"function_call\",\"id\":\"fc_responses_item_0\",\"call_id\":\"responses_call_0\",\"name\":\"target_tool\"}}\n\n",
+ "data: {\"type\":\"response.function_call_arguments.delta\",\"call_id\":\"responses_call_0\",\"item_id\":\"fc_responses_item_0\",\"delta\":\"{\\\"value\\\":\\\"model-original\\\"}\"}\n\n",
+ "data: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"function_call\",\"id\":\"fc_responses_item_0\",\"call_id\":\"responses_call_0\",\"name\":\"target_tool\"}}\n\n",
+ "data: {\"type\":\"response.completed\",\"response\":{\"usage\":{\"input_tokens\":5,\"output_tokens\":3}}}\n\n"
+ ]
+ },
+ "output": "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_<ID>\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"model\":\"test-model\",\"stop_reason\":null,\"stop_sequence\":null,\"usage\":{\"input_tokens\":100,\"output_tokens\":1,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0}}}\n\nevent: ping\ndata: {\"type\":\"ping\"}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"toolu_responses_call_0\",\"name\":\"target_tool\",\"input\":{}}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"value\\\":\\\"behavior-repaired\\\"}\"}}\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\nevent: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\",\"stop_sequence\":null},\"usage\":{\"input_tokens\":5,\"output_tokens\":3}}\n\nevent: message_stop\ndata: {\"type\":\"message_stop\"}\n\n"
+}
diff --git a/packages/cli/src/test-fixtures/parser-goldens/openai-responses-sse.untargeted-incremental.golden.json b/packages/cli/src/test-fixtures/parser-goldens/openai-responses-sse.untargeted-incremental.golden.json
new file mode 100644
index 00000000..a901838f
--- /dev/null
+++ b/packages/cli/src/test-fixtures/parser-goldens/openai-responses-sse.untargeted-incremental.golden.json
@@ -0,0 +1,23 @@
+{
+ "kind": "parser",
+ "id": "openai-responses-sse.untargeted-incremental",
+ "source": "openai-responses-sse behavior repair > keeps untargeted argument fragments incremental",
+ "parser": "openai-responses-sse",
+ "modelName": "test-model",
+ "repair": {
+ "kind": "replace",
+ "tool": "target_tool",
+ "args": "{\"value\":\"behavior-repaired\"}"
+ },
+ "input": {
+ "encoding": "utf8",
+ "chunks": [
+ "data: {\"type\":\"response.output_item.added\",\"item\":{\"type\":\"function_call\",\"id\":\"fc_responses_item_0\",\"call_id\":\"responses_call_0\",\"name\":\"untargeted_tool\"}}\n\n",
+ "data: {\"type\":\"response.function_call_arguments.delta\",\"call_id\":\"responses_call_0\",\"item_id\":\"fc_responses_item_0\",\"delta\":\"{\\\"value\\\":\\\"incremental-\"}\n\n",
+ "data: {\"type\":\"response.function_call_arguments.delta\",\"call_id\":\"responses_call_0\",\"item_id\":\"fc_responses_item_0\",\"delta\":\"delivery\\\"}\"}\n\n",
+ "data: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"function_call\",\"id\":\"fc_responses_item_0\",\"call_id\":\"responses_call_0\",\"name\":\"untargeted_tool\"}}\n\n",
+ "data: {\"type\":\"response.completed\",\"response\":{\"usage\":{\"input_tokens\":5,\"output_tokens\":3}}}\n\n"
+ ]
+ },
+ "output": "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_<ID>\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"model\":\"test-model\",\"stop_reason\":null,\"stop_sequence\":null,\"usage\":{\"input_tokens\":100,\"output_tokens\":1,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0}}}\n\nevent: ping\ndata: {\"type\":\"ping\"}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"toolu_responses_call_0\",\"name\":\"untargeted_tool\",\"input\":{}}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"value\\\":\\\"incremental-\"}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"delivery\\\"}\"}}\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\nevent: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\",\"stop_sequence\":null},\"usage\":{\"input_tokens\":5,\"output_tokens\":3}}\n\nevent: message_stop\ndata: {\"type\":\"message_stop\"}\n\n"
+}
diff --git a/packages/cli/src/test-fixtures/parser-goldens/openai-responses-sse.untargeted-unchanged.golden.json b/packages/cli/src/test-fixtures/parser-goldens/openai-responses-sse.untargeted-unchanged.golden.json
new file mode 100644
index 00000000..7c6ae993
--- /dev/null
+++ b/packages/cli/src/test-fixtures/parser-goldens/openai-responses-sse.untargeted-unchanged.golden.json
@@ -0,0 +1,22 @@
+{
+ "kind": "parser",
+ "id": "openai-responses-sse.untargeted-unchanged",
+ "source": "openai-responses-sse behavior repair > passes an untargeted tool through unchanged",
+ "parser": "openai-responses-sse",
+ "modelName": "test-model",
+ "repair": {
+ "kind": "replace",
+ "tool": "target_tool",
+ "args": "{\"value\":\"behavior-repaired\"}"
+ },
+ "input": {
+ "encoding": "utf8",
+ "chunks": [
+ "data: {\"type\":\"response.output_item.added\",\"item\":{\"type\":\"function_call\",\"id\":\"fc_responses_item_0\",\"call_id\":\"responses_call_0\",\"name\":\"untargeted_tool\"}}\n\n",
+ "data: {\"type\":\"response.function_call_arguments.delta\",\"call_id\":\"responses_call_0\",\"item_id\":\"fc_responses_item_0\",\"delta\":\"{\\\"value\\\":\\\"model-original\\\"}\"}\n\n",
+ "data: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"function_call\",\"id\":\"fc_responses_item_0\",\"call_id\":\"responses_call_0\",\"name\":\"untargeted_tool\"}}\n\n",
+ "data: {\"type\":\"response.completed\",\"response\":{\"usage\":{\"input_tokens\":5,\"output_tokens\":3}}}\n\n"
+ ]
+ },
+ "output": "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_<ID>\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"model\":\"test-model\",\"stop_reason\":null,\"stop_sequence\":null,\"usage\":{\"input_tokens\":100,\"output_tokens\":1,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0}}}\n\nevent: ping\ndata: {\"type\":\"ping\"}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"toolu_responses_call_0\",\"name\":\"untargeted_tool\",\"input\":{}}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"value\\\":\\\"model-original\\\"}\"}}\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\nevent: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\",\"stop_sequence\":null},\"usage\":{\"input_tokens\":5,\"output_tokens\":3}}\n\nevent: message_stop\ndata: {\"type\":\"message_stop\"}\n\n"
+}
diff --git a/packages/cli/src/test-fixtures/parser-goldens/openai-sse.repair-throws.golden.json b/packages/cli/src/test-fixtures/parser-goldens/openai-sse.repair-throws.golden.json
new file mode 100644
index 00000000..ba7834cc
--- /dev/null
+++ b/packages/cli/src/test-fixtures/parser-goldens/openai-sse.repair-throws.golden.json
@@ -0,0 +1,46 @@
+{
+ "kind": "parser",
+ "id": "openai-sse.repair-throws",
+ "source": "openai-sse behavior repair > emits the model arguments when a repair rule throws",
+ "parser": "openai-sse",
+ "modelName": "test-model",
+ "toolSchemas": [
+ {
+ "name": "target_tool",
+ "input_schema": {
+ "type": "object",
+ "properties": {
+ "value": {
+ "type": "string"
+ }
+ },
+ "required": ["value"]
+ }
+ },
+ {
+ "name": "untargeted_tool",
+ "input_schema": {
+ "type": "object",
+ "properties": {
+ "value": {
+ "type": "string"
+ }
+ },
+ "required": ["value"]
+ }
+ }
+ ],
+ "repair": {
+ "kind": "throws",
+ "tool": "target_tool"
+ },
+ "input": {
+ "encoding": "utf8",
+ "chunks": [
+ "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call_openai_0\",\"type\":\"function\",\"function\":{\"name\":\"target_tool\",\"arguments\":\"{\\\"value\\\":\\\"model-original\\\"}\"}}]},\"finish_reason\":null}]}\n\n",
+ "data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\n\n",
+ "data: [DONE]\n\n"
+ ]
+ },
+ "output": "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_<ID>\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"model\":\"test-model\",\"stop_reason\":null,\"stop_sequence\":null,\"usage\":{\"input_tokens\":100,\"output_tokens\":1,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0}}}\n\nevent: ping\ndata: {\"type\":\"ping\"}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"call_openai_0\",\"name\":\"target_tool\",\"input\":{}}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"value\\\":\\\"model-original\\\"}\"}}\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\nevent: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\",\"stop_sequence\":null},\"usage\":{\"input_tokens\":0,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0,\"output_tokens\":0}}\n\nevent: message_stop\ndata: {\"type\":\"message_stop\"}\n\ndata: [DONE]\n\n\n"
+}
diff --git a/packages/cli/src/test-fixtures/parser-goldens/openai-sse.targeted-repaired.golden.json b/packages/cli/src/test-fixtures/parser-goldens/openai-sse.targeted-repaired.golden.json
new file mode 100644
index 00000000..320d9686
--- /dev/null
+++ b/packages/cli/src/test-fixtures/parser-goldens/openai-sse.targeted-repaired.golden.json
@@ -0,0 +1,47 @@
+{
+ "kind": "parser",
+ "id": "openai-sse.targeted-repaired",
+ "source": "openai-sse behavior repair > rewrites arguments for a targeted tool",
+ "parser": "openai-sse",
+ "modelName": "test-model",
+ "toolSchemas": [
+ {
+ "name": "target_tool",
+ "input_schema": {
+ "type": "object",
+ "properties": {
+ "value": {
+ "type": "string"
+ }
+ },
+ "required": ["value"]
+ }
+ },
+ {
+ "name": "untargeted_tool",
+ "input_schema": {
+ "type": "object",
+ "properties": {
+ "value": {
+ "type": "string"
+ }
+ },
+ "required": ["value"]
+ }
+ }
+ ],
+ "repair": {
+ "kind": "replace",
+ "tool": "target_tool",
+ "args": "{\"value\":\"behavior-repaired\"}"
+ },
+ "input": {
+ "encoding": "utf8",
+ "chunks": [
+ "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call_openai_0\",\"type\":\"function\",\"function\":{\"name\":\"target_tool\",\"arguments\":\"{\\\"value\\\":\\\"model-original\\\"}\"}}]},\"finish_reason\":null}]}\n\n",
+ "data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\n\n",
+ "data: [DONE]\n\n"
+ ]
+ },
+ "output": "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_<ID>\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"model\":\"test-model\",\"stop_reason\":null,\"stop_sequence\":null,\"usage\":{\"input_tokens\":100,\"output_tokens\":1,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0}}}\n\nevent: ping\ndata: {\"type\":\"ping\"}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"call_openai_0\",\"name\":\"target_tool\",\"input\":{}}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"value\\\":\\\"behavior-repaired\\\"}\"}}\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\nevent: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\",\"stop_sequence\":null},\"usage\":{\"input_tokens\":0,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0,\"output_tokens\":0}}\n\nevent: message_stop\ndata: {\"type\":\"message_stop\"}\n\ndata: [DONE]\n\n\n"
+}
diff --git a/packages/cli/src/test-fixtures/parser-goldens/openai-sse.untargeted-unchanged.golden.json b/packages/cli/src/test-fixtures/parser-goldens/openai-sse.untargeted-unchanged.golden.json
new file mode 100644
index 00000000..20d6380e
--- /dev/null
+++ b/packages/cli/src/test-fixtures/parser-goldens/openai-sse.untargeted-unchanged.golden.json
@@ -0,0 +1,47 @@
+{
+ "kind": "parser",
+ "id": "openai-sse.untargeted-unchanged",
+ "source": "openai-sse behavior repair > passes an untargeted tool through unchanged",
+ "parser": "openai-sse",
+ "modelName": "test-model",
+ "toolSchemas": [
+ {
+ "name": "target_tool",
+ "input_schema": {
+ "type": "object",
+ "properties": {
+ "value": {
+ "type": "string"
+ }
+ },
+ "required": ["value"]
+ }
+ },
+ {
+ "name": "untargeted_tool",
+ "input_schema": {
+ "type": "object",
+ "properties": {
+ "value": {
+ "type": "string"
+ }
+ },
+ "required": ["value"]
+ }
+ }
+ ],
+ "repair": {
+ "kind": "replace",
+ "tool": "target_tool",
+ "args": "{\"value\":\"behavior-repaired\"}"
+ },
+ "input": {
+ "encoding": "utf8",
+ "chunks": [
+ "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call_openai_0\",\"type\":\"function\",\"function\":{\"name\":\"untargeted_tool\",\"arguments\":\"{\\\"value\\\":\\\"model-original\\\"}\"}}]},\"finish_reason\":null}]}\n\n",
+ "data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\n\n",
+ "data: [DONE]\n\n"
+ ]
+ },
+ "output": "event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":\"msg_<ID>\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[],\"model\":\"test-model\",\"stop_reason\":null,\"stop_sequence\":null,\"usage\":{\"input_tokens\":100,\"output_tokens\":1,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0}}}\n\nevent: ping\ndata: {\"type\":\"ping\"}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"call_openai_0\",\"name\":\"untargeted_tool\",\"input\":{}}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"value\\\":\\\"model-original\\\"}\"}}\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\nevent: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\",\"stop_sequence\":null},\"usage\":{\"input_tokens\":0,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0,\"output_tokens\":0}}\n\nevent: message_stop\ndata: {\"type\":\"message_stop\"}\n\ndata: [DONE]\n\n\n"
+}
diff --git a/packages/cli/src/test-fixtures/parser-goldens/parser-goldens.test.ts b/packages/cli/src/test-fixtures/parser-goldens/parser-goldens.test.ts
new file mode 100644
index 00000000..a8c61599
--- /dev/null
+++ b/packages/cli/src/test-fixtures/parser-goldens/parser-goldens.test.ts
@@ -0,0 +1,215 @@
+/**
+ * P0 — the current parsers reproduce the parser goldens.
+ *
+ * The goldens are the pre-hook contract that the response seam (P4) and the
+ * Gemini built-in (P5) must keep. This file proves they were captured from, and
+ * still describe, the code at the capture base: every golden replays to its
+ * recorded output through the CURRENT parser API (`drive-current.ts`).
+ *
+ * When P4/P5 delete that API, the replay half moves to a driver over the new
+ * parser + seam; the coverage, drift and meaning checks stay as they are.
+ */
+
+import { describe, expect, it } from "bun:test";
+
+import {
+ GEMINI_APPLICABILITY_MODELS,
+ GEMINI_GOLDEN_ID,
+ GEMINI_SCENARIOS,
+ GEMINI_TOOL_SCHEMAS,
+ ORIGINAL_ARGS,
+ ORIGINAL_VALUE,
+ PARSER_CASES,
+ REPAIRED_ARGS,
+ REPAIRED_VALUE,
+} from "./cases.js";
+import { runGeminiCapture, runParserCase } from "./drive-current.js";
+import {
+ type GeminiMiddlewareGolden,
+ type ParserGolden,
+ dataPayloads,
+ listGoldenIds,
+ readGolden,
+ stripPings,
+} from "./golden-format.js";
+
+const parserGolden = (id: string) => readGolden(id) as ParserGolden;
+const geminiGolden = () => readGolden(GEMINI_GOLDEN_ID) as GeminiMiddlewareGolden;
+
+/** `partial_json` of every `input_json_delta`, in order. */
+function inputJsonDeltas(wire: string): string[] {
+ return dataPayloads(wire)
+ .filter((p: any) => p?.type === "content_block_delta" && p.delta?.type === "input_json_delta")
+ .map((p: any) => p.delta.partial_json);
+}
+
+describe("parser goldens: coverage and drift", () => {
+ it("has exactly one golden file per case", () => {
+ const expected = [...PARSER_CASES.map((c) => c.id), GEMINI_GOLDEN_ID].sort();
+ expect(listGoldenIds()).toEqual(expected);
+ });
+
+ it("stores the same input and repair rule cases.ts defines", () => {
+ for (const c of PARSER_CASES) {
+ const { kind, output, ...stored } = parserGolden(c.id);
+ expect(kind).toBe("parser");
+ expect(stored).toEqual(JSON.parse(JSON.stringify(c)));
+ }
+ });
+
+ it("stores the same Gemini inputs cases.ts defines", () => {
+ const golden = geminiGolden();
+ expect(golden.applicability.map((a) => a.modelId)).toEqual(GEMINI_APPLICABILITY_MODELS);
+ expect(golden.toolSchemas).toEqual(GEMINI_TOOL_SCHEMAS);
+ expect(golden.scenarios.map((s) => s.id)).toEqual(GEMINI_SCENARIOS.map((s) => s.id));
+ for (const [i, s] of GEMINI_SCENARIOS.entries()) {
+ expect(golden.scenarios[i]!.streams.map((x) => x.input)).toEqual(s.streams);
+ expect(golden.scenarios[i]!.nextRequest.before).toEqual(s.nextRequestMessages as any[]);
+ }
+ });
+});
+
+describe("parser goldens: the current parsers reproduce them", () => {
+ for (const c of PARSER_CASES) {
+ it(c.id, async () => {
+ const golden = parserGolden(c.id);
+ expect(await runParserCase(golden)).toBe(golden.output);
+ });
+ }
+
+ it("Gemini thought-signature middleware", async () => {
+ const golden = geminiGolden();
+ const replay = await runGeminiCapture(
+ golden.id,
+ golden.modelId,
+ golden.toolSchemas,
+ golden.applicability.map((a) => a.modelId),
+ golden.scenarios.map((s) => ({
+ id: s.id,
+ description: s.description,
+ streams: s.streams.map((x) => x.input),
+ nextRequestMessages: s.nextRequest.before,
+ }))
+ );
+ expect(replay).toEqual(golden);
+ });
+
+ it("can fail: the targeted input without its rule does not reproduce the repaired golden", async () => {
+ const golden = parserGolden("openai-sse.targeted-repaired");
+ const unarmed = await runParserCase({ ...golden, repair: { kind: "none" } });
+ expect(unarmed).not.toBe(golden.output);
+ expect(unarmed).toContain(ORIGINAL_VALUE);
+ });
+});
+
+describe("parser goldens: each records the behaviour it is named for", () => {
+ const repaired = [
+ "openai-sse.targeted-repaired",
+ "openai-responses-sse.targeted-repaired",
+ "anthropic-sse.targeted-fragmented-repaired",
+ "gemini-sse.targeted-repaired",
+ "devin-connect.targeted-fragmented-repaired",
+ ];
+ const original = [
+ "openai-sse.untargeted-unchanged",
+ "openai-sse.repair-throws",
+ "openai-responses-sse.untargeted-unchanged",
+ "anthropic-sse.untargeted-unchanged",
+ "gemini-sse.untargeted-unchanged",
+ "devin-connect.repair-throws",
+ ];
+
+ for (const id of repaired) {
+ it(`${id}: one complete repaired argument object`, () => {
+ const { output } = parserGolden(id);
+ expect(inputJsonDeltas(output)).toEqual([REPAIRED_ARGS]);
+ expect(output).not.toContain(ORIGINAL_VALUE);
+ });
+ }
+
+ for (const id of original) {
+ it(`${id}: the model's own arguments`, () => {
+ const { output } = parserGolden(id);
+ expect(inputJsonDeltas(output).join("")).toBe(ORIGINAL_ARGS);
+ expect(output).not.toContain(REPAIRED_VALUE);
+ });
+ }
+
+ it("openai-sse.repair-throws still ends the message", () => {
+ expect(parserGolden("openai-sse.repair-throws").output).toContain('"type":"message_stop"');
+ });
+
+ for (const id of [
+ "openai-responses-sse.untargeted-incremental",
+ "devin-connect.untargeted-incremental",
+ ]) {
+ it(`${id}: untargeted fragments stay incremental`, () => {
+ expect(inputJsonDeltas(parserGolden(id).output)).toEqual([
+ '{"value":"incremental-',
+ 'delivery"}',
+ ]);
+ });
+ }
+
+ it("anthropic-sse: an armed rule leaves an untargeted tool byte-identical once pings are stripped", () => {
+ const baseline = parserGolden("anthropic-sse.byte-identical.baseline").output;
+ const armed = parserGolden("anthropic-sse.byte-identical.armed").output;
+ expect(stripPings(armed)).toBe(stripPings(baseline));
+ });
+
+ it("anthropic-sse targeted repair: the recorded defect is real and its payloads are intact", () => {
+ const golden = parserGolden("anthropic-sse.targeted-fragmented-repaired");
+ expect(golden.defect).toBeDefined();
+ expect(golden.output).toContain("event: content_block_stop\nevent: content_block_delta\n");
+ expect(dataPayloads(golden.output).map((p: any) => p.type)).toEqual([
+ "content_block_start",
+ "content_block_delta",
+ "content_block_stop",
+ ]);
+ expect(PARSER_CASES.filter((c) => c.defect).map((c) => c.id)).toEqual([golden.id]);
+ });
+
+ it("Gemini: applicability is the gemini / google/ substring rule", () => {
+ expect(geminiGolden().applicability).toEqual([
+ {
+ modelId: "google/gemini-3.1-pro-preview",
+ shouldHandle: true,
+ activeNames: ["GeminiThoughtSignature"],
+ },
+ { modelId: "gemini-2.5-flash", shouldHandle: true, activeNames: ["GeminiThoughtSignature"] },
+ {
+ modelId: "google/gemma-3-27b-it",
+ shouldHandle: true,
+ activeNames: ["GeminiThoughtSignature"],
+ },
+ { modelId: "gpt-5.6-sol", shouldHandle: false, activeNames: [] },
+ { modelId: "claude-opus-5", shouldHandle: false, activeNames: [] },
+ ]);
+ });
+
+ it("Gemini: the middleware never changes the client stream", () => {
+ for (const s of geminiGolden().scenarios) {
+ for (const stream of s.streams) expect(stream.outputUnchangedByMiddleware).toBe(true);
+ }
+ });
+
+ it("Gemini: each turn's reasoning_details land on its own assistant message only", () => {
+ const byId = Object.fromEntries(geminiGolden().scenarios.map((s) => [s.id, s]));
+ const signatures = (m: any) => m.reasoning_details?.map((d: any) => d.signature);
+ const changed = (id: string) => {
+ const { before, after } = byId[id]!.nextRequest;
+ return after
+ .map((m, i) => [i, m] as const)
+ .filter(([i, m]) => JSON.stringify(m) !== JSON.stringify(before[i]))
+ .map(([i, m]) => [i, signatures(m)]);
+ };
+
+ expect(changed("tool-call-with-reasoning")).toEqual([[2, ["SIG-A-0", "SIG-A-1"]]]);
+ expect(changed("two-turns")).toEqual([
+ [2, ["SIG-A-0", "SIG-A-1"]],
+ [4, ["SIG-B-0", "SIG-B-1"]],
+ ]);
+ expect(changed("reasoning-without-tool-call")).toEqual([]);
+ expect(changed("no-prior-stream")).toEqual([]);
+ });
+});