Skip to content

Commit bd13d48

Browse files
committed
fix(kiro): defer the reasoning envelope until the turn's items close
CodeRabbit caught a real defect that made the round-trip a no-op in the streaming path. Kiro sends its reasoning blob at the END of a turn, while the assistant message is still open. Emitting the envelope-only item on arrival was wrong twice over: - `outputIndex` only advances when an item CLOSES, so the open message and the envelope item were emitted under the same output index. - The envelope landed BEFORE the assistant message, and the parser pairs a krc-only item backwards, so it found no preceding assistant turn and dropped the blob as orphaned — silently defeating the fix. Both paths now stash the blob and flush it after every open item has closed: after the closes in the streaming `done` case, and after the trailing flushes in the batch path. Message phase inference is untouched, so a Kiro final answer is still classified `final_answer` rather than being force-closed as commentary. The batch path also released `bytesOf(encrypted)` through `pushOutput` without ever retaining it. It now charges the blob when stashing and lets `pushOutput` release that retained allocation, so the translator budget balances. Adds tests/kiro-reasoning-roundtrip.test.ts, which bridges adapter events and re-parses the emitted items the way Codex replays history — the end-to-end coverage the original tests lacked. Three of its five cases fail against the previous commit. Scope: verified that gpt-5.6-terra and gpt-5.6-luna return `redactedContent` exactly like gpt-5.6-sol, so the whole GPT-5.6 family was affected. Handling keys off the wire field, not the model id.
1 parent cbb5d21 commit bd13d48

4 files changed

Lines changed: 195 additions & 25 deletions

File tree

‎src/adapters/kiro-events.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,9 @@ export function parseKiroEvent(eventType: string, payload: Uint8Array): ParsedKi
118118
: {}),
119119
};
120120
case "reasoningContentEvent":
121-
// `text` is plaintext reasoning; `redactedContent` is the encrypted blob the model actually
122-
// returns for Sol-family models. Both may be absent on a bare event.
121+
// `text` is plaintext reasoning; `redactedContent` is the encrypted blob the GPT-5.6 family
122+
// (sol/terra/luna) actually returns — they never send `text`. Keyed off the wire field, not
123+
// the model id. Both may be absent on a bare event.
123124
return {
124125
type: "reasoning",
125126
...(optionalString(eventType, parsed, "text") !== undefined

‎src/bridge.ts‎

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,29 @@ export function bridgeToResponsesSSE(
442442
retainFinishedItem(item as OutputItem, bytesOf(encrypted), "reasoning");
443443
outputIndex++;
444444
};
445+
// Kiro reasoning round-trip. Kiro sends its encrypted blob at the END of a turn, while the
446+
// assistant message is still open, so this CANNOT emit on arrival: the open message still
447+
// owns `outputIndex` (it only advances on close), and an item emitted here would both reuse
448+
// that index and land BEFORE the message — where the parser's backwards pairing drops it as
449+
// orphaned. Stash it and flush after `done` has closed every open item instead.
450+
let pendingKiroRedacted: string | undefined;
451+
let pendingKiroRedactedBytes = 0;
452+
const flushKiroRedactedReasoning = () => {
453+
if (!pendingKiroRedacted) return;
454+
const previousBytes = pendingKiroRedactedBytes;
455+
const encrypted = encodeReasoningEnvelope({ krc: pendingKiroRedacted });
456+
const reservation = budget?.reserveTransient(bytesOf(encrypted), { kind: "reasoning" });
457+
pendingKiroRedacted = undefined;
458+
pendingKiroRedactedBytes = 0;
459+
reservation?.commitRetained();
460+
budget?.releaseRetained(previousBytes, { kind: "reasoning" });
461+
const itemId = `rs_${uuid()}`;
462+
const item = { type: "reasoning", id: itemId, summary: [] as never[], encrypted_content: encrypted };
463+
emit("response.output_item.added", { output_index: outputIndex, item });
464+
emit("response.output_item.done", { output_index: outputIndex, item });
465+
retainFinishedItem(item as OutputItem, bytesOf(encrypted), "reasoning");
466+
outputIndex++;
467+
};
445468
// Full assistant text of a compaction turn (across message boundaries) — becomes the
446469
// synthetic compaction item's payload on done.
447470
let compactionText = "";
@@ -870,18 +893,9 @@ export function bridgeToResponsesSSE(
870893
break;
871894
}
872895
case "kiro_redacted_reasoning": {
873-
// Opaque and unrenderable: emit an envelope-only reasoning item (no summary, no text
874-
// deltas) so it stays invisible in the app while still round-tripping. Kiro sends it
875-
// once the turn's content and tool calls are done, so nothing open is disturbed.
876-
const encrypted = encodeReasoningEnvelope({ krc: event.data });
877-
const reservation = budget?.reserveTransient(bytesOf(encrypted), { kind: "reasoning" });
878-
const itemId = `rs_${uuid()}`;
879-
const item = { type: "reasoning", id: itemId, summary: [] as never[], encrypted_content: encrypted };
880-
emit("response.output_item.added", { output_index: outputIndex, item });
881-
emit("response.output_item.done", { output_index: outputIndex, item });
882-
reservation?.commitRetained();
883-
retainFinishedItem(item as OutputItem, bytesOf(encrypted), "reasoning");
884-
outputIndex++;
896+
// Stash only — see flushKiroRedactedReasoning. One blob per turn, so last wins.
897+
pendingKiroRedactedBytes = replaceRetainedString(pendingKiroRedactedBytes, event.data, "reasoning");
898+
pendingKiroRedacted = event.data;
885899
break;
886900
}
887901
case "reasoning_raw_delta": {
@@ -1054,6 +1068,9 @@ export function bridgeToResponsesSSE(
10541068
// Redacted-only turns (or hidden thinking without a trailing signature event) still
10551069
// need their envelope-only reasoning item so the blocks replay next turn.
10561070
flushHiddenReasoningEnvelope();
1071+
// After every close above, so the blob lands AFTER the assistant message it belongs
1072+
// to and the parser's backwards pairing finds it.
1073+
flushKiroRedactedReasoning();
10571074
if (options?.compaction) {
10581075
// Exactly one compaction item per turn; codex-rs takes the first and fatals on 0.
10591076
const item = {
@@ -1376,6 +1393,10 @@ function buildResponseJSONWithBudget(
13761393
let batchSignatureBytes = 0;
13771394
let batchRedacted: string[] = [];
13781395
let batchRedactedBytes = 0;
1396+
// Kiro reasoning blob, held until after the trailing flushes so it lands AFTER the assistant
1397+
// message (see the streaming path). Retained because it outlives releaseTranslatedEvent.
1398+
let batchKiroRedacted: string | undefined;
1399+
let batchKiroRedactedBytes = 0;
13791400
let currentToolCallId = "";
13801401
let currentToolCallName = "";
13811402
let currentToolCallArgs = "";
@@ -1544,13 +1565,14 @@ function buildResponseJSONWithBudget(
15441565
batchRedacted.push(e.data);
15451566
break;
15461567
case "kiro_redacted_reasoning":
1547-
// Envelope-only reasoning item, same contract as the streaming path.
1568+
// Stash only — pushed after the trailing flushes. One blob per turn, so last wins.
15481569
{
1549-
const encrypted = encodeReasoningEnvelope({ krc: e.data });
1550-
pushOutput({
1551-
type: "reasoning", id: `rs_${uuid()}`, summary: [], encrypted_content: encrypted,
1552-
}, bytesOf(encrypted), "reasoning");
1570+
const dataBytes = bytesOf(e.data);
1571+
budget?.chargeRetained(dataBytes, { kind: "reasoning" });
1572+
if (batchKiroRedactedBytes > 0) budget?.releaseRetained(batchKiroRedactedBytes, { kind: "reasoning" });
1573+
batchKiroRedactedBytes = dataBytes;
15531574
}
1575+
batchKiroRedacted = e.data;
15541576
break;
15551577
case "reasoning_raw_delta":
15561578
if (currentText) flushText("commentary");
@@ -1650,6 +1672,15 @@ function buildResponseJSONWithBudget(
16501672
flushRawReasoning();
16511673
// Open tool call on a failed/incomplete turn must not land as status:"completed".
16521674
if (currentToolCallId) flushToolCall(errorEvent || incompleteEvent ? "incomplete" : "completed");
1675+
if (batchKiroRedacted) {
1676+
// pushOutput reserves the item itself and releases the retained raw blob it replaces.
1677+
pushOutput({
1678+
type: "reasoning", id: `rs_${uuid()}`, summary: [],
1679+
encrypted_content: encodeReasoningEnvelope({ krc: batchKiroRedacted }),
1680+
}, batchKiroRedactedBytes, "reasoning");
1681+
batchKiroRedacted = undefined;
1682+
batchKiroRedactedBytes = 0;
1683+
}
16531684
// A truncated turn must never be installed as replacement history: emit the
16541685
// compaction item only when the turn actually completed (#422).
16551686
if (

‎structure/04_transports-and-sidecars.md‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,12 +383,20 @@ Grounded in the open-sourced official client (xai-org/grok-build); unit + eviden
383383

384384
## Kiro reasoning round-trip (`redactedContent`)
385385

386-
Kiro never returns plaintext reasoning for its Sol-family models: `reasoningContentEvent` carries a
387-
KMS-encrypted `redactedContent` blob, and `gpt-5.6-sol`'s `additionalModelRequestFieldsSchema`
388-
(`ListAvailableModels`) accepts only `reasoning.effort` — there is no display/summary opt-in. Kiro's
389-
own CLI replays that blob on the matching `assistantResponseMessage.reasoningContent` to preserve
390-
model reasoning across turns; dropping it makes every turn restart without the previous turn's
391-
reasoning. Verified against kiro-cli 2.14.1 and 2.16.0.
386+
Kiro never returns plaintext reasoning for its **GPT-5.6 family** (`gpt-5.6-sol`, `-terra`,
387+
`-luna`): `reasoningContentEvent` carries a KMS-encrypted `redactedContent` blob, never `text`.
388+
Their `additionalModelRequestFieldsSchema` (`ListAvailableModels`) accepts only `reasoning.effort`
389+
with `additionalProperties: false` — there is no display/summary opt-in, so this is the only
390+
reasoning these models can return. Kiro's own CLI replays the blob on the matching
391+
`assistantResponseMessage.reasoningContent` to preserve model reasoning across turns; dropping it
392+
makes every turn restart without the previous turn's reasoning. Verified on kiro-cli 2.14.1 and
393+
2.16.0, all three models.
394+
395+
The Claude 4.6+/5 entries advertise a different, richer contract (`thinking.type` adaptive/disabled,
396+
`thinking.display` summarized/omitted, `output_config.effort`, `max_tokens`) and are not covered by
397+
that measurement; older Claude, deepseek, minimax, glm, and qwen entries advertise no additional
398+
fields at all. The handling below keys off the wire field, not the model id, so any model that
399+
sends `redactedContent` round-trips.
392400

393401
- The blob rides the existing `ocxr1:` envelope as `krc` (`src/responses/reasoning-envelope.ts`) on
394402
an envelope-only reasoning item — `summary: []`, no text deltas — so it stays invisible in the
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { bridgeToResponsesSSE, buildResponseJSON } from "../src/bridge";
3+
import { parseRequest } from "../src/responses/parser";
4+
import { decodeReasoningEnvelope } from "../src/responses/reasoning-envelope";
5+
import type { AdapterEvent } from "../src/types";
6+
import { createTranslatorBudget } from "../src/lib/translator-budget";
7+
8+
const BLOB = "LktUUn5+ZXlKbGJtTnllWEIwYVc5dVVtVm5hVzl1SWpvaQ==";
9+
10+
async function* replay(events: AdapterEvent[]): AsyncGenerator<AdapterEvent> {
11+
for (const event of events) yield event;
12+
}
13+
14+
async function drain(stream: ReadableStream<Uint8Array>): Promise<string> {
15+
const reader = stream.getReader();
16+
const decoder = new TextDecoder();
17+
let out = "";
18+
while (true) {
19+
const { done, value } = await reader.read();
20+
if (done) break;
21+
out += decoder.decode(value, { stream: true });
22+
}
23+
return out;
24+
}
25+
26+
/** Output items in emission order, as Codex reconstructs them from the SSE stream. */
27+
function doneItems(sse: string): Record<string, unknown>[] {
28+
const items: Record<string, unknown>[] = [];
29+
for (const line of sse.split("\n")) {
30+
if (!line.startsWith("data: ")) continue;
31+
try {
32+
const json = JSON.parse(line.slice(6)) as { type?: string; item?: Record<string, unknown>; output_index?: number };
33+
if (json.type === "response.output_item.done" && json.item) {
34+
items.push({ ...json.item, __index: json.output_index });
35+
}
36+
} catch { /* partial frame */ }
37+
}
38+
return items;
39+
}
40+
41+
/** Feed emitted items back as Responses input, the way Codex replays history next turn. */
42+
function reparse(items: Record<string, unknown>[]) {
43+
return parseRequest({
44+
model: "kiro/gpt-5.6-sol",
45+
input: [
46+
{ type: "message", role: "user", content: [{ type: "input_text", text: "hi" }] },
47+
...items.map(({ __index, status, ...item }) => item),
48+
],
49+
});
50+
}
51+
52+
// Kiro emits its reasoning blob at the END of a turn, while the assistant message is still open.
53+
// Emitting the envelope item on arrival reused the open message's output_index AND placed the blob
54+
// before the message, where the parser's backwards pairing drops it as orphaned — silently
55+
// defeating the round-trip. Both paths must defer it until the message has closed.
56+
describe("kiro redacted-reasoning round-trip (bridge → parse)", () => {
57+
const events: AdapterEvent[] = [
58+
{ type: "text_delta", text: "the answer" },
59+
{ type: "kiro_redacted_reasoning", data: BLOB },
60+
{ type: "done", usage: { inputTokens: 1, outputTokens: 2 }, endTurn: true },
61+
];
62+
63+
test("SSE: the blob lands after the assistant message, on its own output index", async () => {
64+
const items = doneItems(await drain(bridgeToResponsesSSE(replay(events), "kiro/gpt-5.6-sol")));
65+
const types = items.map(i => i.type);
66+
expect(types).toEqual(["message", "reasoning"]);
67+
68+
const indexes = items.map(i => i.__index);
69+
expect(new Set(indexes).size).toBe(indexes.length); // no output_index collision
70+
71+
const envelope = decodeReasoningEnvelope(items[1].encrypted_content as string);
72+
expect(envelope?.krc).toBe(BLOB);
73+
expect(items[1].summary).toEqual([]);
74+
});
75+
76+
test("SSE: replayed history attaches the blob to the assistant turn that produced it", async () => {
77+
const items = doneItems(await drain(bridgeToResponsesSSE(replay(events), "kiro/gpt-5.6-sol")));
78+
const assistant = reparse(items).context.messages.find(m => m.role === "assistant");
79+
expect((assistant as { kiroRedactedReasoning?: string }).kiroRedactedReasoning).toBe(BLOB);
80+
});
81+
82+
test("batch: the blob lands after the assistant message and survives replay", () => {
83+
const response = buildResponseJSON(
84+
[
85+
{ type: "text_delta", text: "the answer" },
86+
{ type: "kiro_redacted_reasoning", data: BLOB },
87+
{ type: "done", usage: { inputTokens: 1, outputTokens: 2 }, endTurn: true },
88+
],
89+
"kiro/gpt-5.6-sol",
90+
);
91+
const output = response.output as Record<string, unknown>[];
92+
expect(output.map(i => i.type)).toEqual(["message", "reasoning"]);
93+
94+
const assistant = reparse(output).context.messages.find(m => m.role === "assistant");
95+
expect((assistant as { kiroRedactedReasoning?: string }).kiroRedactedReasoning).toBe(BLOB);
96+
});
97+
98+
test("batch: the retained blob is released, leaving no translator-budget drift", () => {
99+
const budget = createTranslatorBudget();
100+
buildResponseJSON(
101+
[
102+
{ type: "text_delta", text: "the answer" },
103+
{ type: "kiro_redacted_reasoning", data: BLOB },
104+
{ type: "done", usage: { inputTokens: 1, outputTokens: 2 }, endTurn: true },
105+
],
106+
"kiro/gpt-5.6-sol",
107+
undefined,
108+
undefined,
109+
{ budget },
110+
);
111+
// Only the finalized output items stay retained; the raw blob must not be double-counted or
112+
// released without ever having been charged.
113+
expect(budget.snapshot().currentBytes).toBeGreaterThanOrEqual(0);
114+
expect(budget.snapshot().overflows).toBe(0);
115+
});
116+
117+
test("a turn ending in a tool call still pairs the blob with that assistant turn", async () => {
118+
const items = doneItems(await drain(bridgeToResponsesSSE(replay([
119+
{ type: "tool_call_start", id: "call_1", name: "bash" },
120+
{ type: "tool_call_delta", arguments: "{\"command\":\"ls\"}" },
121+
{ type: "tool_call_end" },
122+
{ type: "kiro_redacted_reasoning", data: BLOB },
123+
{ type: "done", usage: { inputTokens: 1, outputTokens: 1 }, endTurn: false },
124+
]), "kiro/gpt-5.6-sol")));
125+
expect(items.map(i => i.type)).toEqual(["function_call", "reasoning"]);
126+
127+
const assistant = reparse(items).context.messages.find(m => m.role === "assistant");
128+
expect((assistant as { kiroRedactedReasoning?: string }).kiroRedactedReasoning).toBe(BLOB);
129+
});
130+
});

0 commit comments

Comments
 (0)