Skip to content

Commit 3a2f057

Browse files
committed
fix(kiro): let the authoritative context usage outrank the fallback
Both `contextUsageEvent` and `metadataEvent.contextUsagePercentage` assigned the same variable with last-write-wins, so arrival order alone decided which value survived and a trailing fallback frame could clobber the authoritative one. Precedence is now by source: once a contextUsageEvent value lands, later metadataEvent percentages are ignored. The fallback still works on its own. Covered in both event orders, with the discriminating values asserted to differ first so the ordering assertions cannot pass vacuously. The test fails against the unguarded assignment. Also disposes the translator budget in the batch regression via try/finally. `createTranslatorBudget` registers in module-global aggregate accounting, so an assertion failure would have leaked retained bytes into every later test.
1 parent 8610630 commit 3a2f057

4 files changed

Lines changed: 70 additions & 23 deletions

File tree

‎src/adapters/kiro.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,10 @@ async function* parseKiroAttemptEvents(
891891
let outputChars = "";
892892
let outputCharsBytes = 0;
893893
let contextUsagePercentage: number | undefined;
894+
// `contextUsageEvent` is the authoritative source; `metadataEvent.contextUsagePercentage` is a
895+
// fallback for wires that carry it there. Once an authoritative value lands, a later fallback
896+
// must not clobber it — otherwise event order alone decides which value survives.
897+
let contextUsageIsAuthoritative = false;
894898
let returnedConversationId = conversationId;
895899
let assistantText = "";
896900
let assistantTextBytes = 0;
@@ -1161,7 +1165,11 @@ async function* parseKiroAttemptEvents(
11611165
switch (ev.type) {
11621166
case "metadata":
11631167
if (ev.usage) authoritativeUsage = ev.usage;
1164-
if (ev.contextUsagePercentage !== undefined && ev.contextUsagePercentage > 0) {
1168+
if (
1169+
!contextUsageIsAuthoritative
1170+
&& ev.contextUsagePercentage !== undefined
1171+
&& ev.contextUsagePercentage > 0
1172+
) {
11651173
contextUsagePercentage = ev.contextUsagePercentage;
11661174
}
11671175
if (ev.stopReason !== undefined) stopReason = ev.stopReason;
@@ -1195,7 +1203,10 @@ async function* parseKiroAttemptEvents(
11951203
}
11961204
break;
11971205
case "context_usage":
1198-
if (ev.contextUsagePercentage > 0) contextUsagePercentage = ev.contextUsagePercentage;
1206+
if (ev.contextUsagePercentage > 0) {
1207+
contextUsagePercentage = ev.contextUsagePercentage;
1208+
contextUsageIsAuthoritative = true;
1209+
}
11991210
break;
12001211
case "tool": {
12011212
for (const contentEvent of thinking.flush()) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,9 @@ Kiro reports context pressure in its own `contextUsageEvent`, which is the autho
413413
every capture taken (2.14.1 and 2.16.0) `metadataEvent` carried only `stopReason` — which is why
414414
reading the percentage from `metadataEvent` alone never saw a value — but the parser still accepts a
415415
finite `contextUsagePercentage` (and a `tokenUsage` block) there as a fallback, so a value parsed
416-
from `metadataEvent` is legitimate rather than impossible. Both feed the same field, and any
417-
positive value overwrites an earlier one.
416+
from `metadataEvent` is legitimate rather than impossible. Precedence is by SOURCE, not arrival
417+
order: once a `contextUsageEvent` value lands, a later `metadataEvent` percentage is ignored, so a
418+
trailing fallback frame cannot clobber the authoritative one.
418419

419420
Spend arrives in `meteringEvent` as **credits, not tokens**. No captured response carried
420421
`tokenUsage` on any event, which is why Kiro usage stays estimated; `meteringEvent` is currently

‎tests/kiro-reasoning-roundtrip.test.ts‎

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -100,25 +100,31 @@ describe("kiro redacted-reasoning round-trip (bridge → parse)", () => {
100100
// the finalized item that embeds its base64.
101101
const bigBlob = "X".repeat(4000);
102102
const budget = createTranslatorBudget();
103-
const response = buildResponseJSON(
104-
[
105-
{ type: "text_delta", text: "the answer" },
106-
{ type: "kiro_redacted_reasoning", data: bigBlob },
107-
{ type: "done", usage: { inputTokens: 1, outputTokens: 2 }, endTurn: true },
108-
],
109-
"kiro/gpt-5.6-sol",
110-
{ translatorBudget: budget },
111-
);
112-
const items = response.output as Record<string, unknown>[];
113-
const finalizedBytes = items.reduce((sum, item) => sum + Buffer.byteLength(JSON.stringify(item)), 0);
114-
const { currentBytes, highWaterBytes, overflows } = budget.snapshot();
115-
116-
// EXACTLY the finalized output items remain retained. A raw blob still held would show up as
117-
// ~4000 extra bytes here; releasing bytes that were never charged would show up as a shortfall.
118-
expect(currentBytes).toBe(finalizedBytes);
119-
// ...and it really was charged while held, rather than never accounted for at all.
120-
expect(highWaterBytes).toBeGreaterThanOrEqual(finalizedBytes + bigBlob.length);
121-
expect(overflows).toBe(0);
103+
// The budget registers in module-global aggregate accounting, so it must be disposed even if an
104+
// assertion throws — otherwise this test leaks retained bytes into every later test.
105+
try {
106+
const response = buildResponseJSON(
107+
[
108+
{ type: "text_delta", text: "the answer" },
109+
{ type: "kiro_redacted_reasoning", data: bigBlob },
110+
{ type: "done", usage: { inputTokens: 1, outputTokens: 2 }, endTurn: true },
111+
],
112+
"kiro/gpt-5.6-sol",
113+
{ translatorBudget: budget },
114+
);
115+
const items = response.output as Record<string, unknown>[];
116+
const finalizedBytes = items.reduce((sum, item) => sum + Buffer.byteLength(JSON.stringify(item)), 0);
117+
const { currentBytes, highWaterBytes, overflows } = budget.snapshot();
118+
119+
// EXACTLY the finalized output items remain retained. A raw blob still held would show up as
120+
// ~4000 extra bytes here; releasing bytes that were never charged would show up as a shortfall.
121+
expect(currentBytes).toBe(finalizedBytes);
122+
// ...and it really was charged while held, rather than never accounted for at all.
123+
expect(highWaterBytes).toBeGreaterThanOrEqual(finalizedBytes + bigBlob.length);
124+
expect(overflows).toBe(0);
125+
} finally {
126+
budget.dispose();
127+
}
122128
});
123129

124130
test("a turn ending in a tool call still pairs the blob with that assistant turn", async () => {

‎tests/kiro-stream.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,35 @@ describe("kiro adapter — parseStream", () => {
14071407
expect(done.contextTotalTokens).toBeGreaterThan(done.inputTokens + done.outputTokens);
14081408
});
14091409

1410+
// contextUsageEvent is authoritative; metadataEvent's percentage is a fallback. Both fed the same
1411+
// field with last-write-wins, so a trailing metadataEvent could clobber the authoritative value
1412+
// and let event order alone decide. Both orders must settle on the contextUsageEvent value.
1413+
test("contextUsageEvent wins over metadataEvent in either event order", async () => {
1414+
const checkpointFor = async (...frames: Uint8Array[]) => {
1415+
const adapter = createKiroAdapter(provider);
1416+
await adapter.buildRequest(parsedWith([{ role: "user", content: "x".repeat(4_000) }]));
1417+
const done = await doneUsage(adapter, eventFrame({ content: "answer" }), ...frames);
1418+
return done.contextTotalTokens;
1419+
};
1420+
const authoritativeOnly = await checkpointFor(eventFrame({ contextUsagePercentage: 80 }, "contextUsageEvent"));
1421+
const fallbackOnly = await checkpointFor(eventFrame({ contextUsagePercentage: 10 }, "metadataEvent"));
1422+
// Guard the guard: 80% and 10% must be distinguishable for the ordering assertions to mean
1423+
// anything, and the fallback must still work on its own.
1424+
expect(fallbackOnly).toBeGreaterThan(0);
1425+
expect(authoritativeOnly).toBeGreaterThan(fallbackOnly!);
1426+
1427+
const fallbackFirst = await checkpointFor(
1428+
eventFrame({ contextUsagePercentage: 10 }, "metadataEvent"),
1429+
eventFrame({ contextUsagePercentage: 80 }, "contextUsageEvent"),
1430+
);
1431+
const authoritativeFirst = await checkpointFor(
1432+
eventFrame({ contextUsagePercentage: 80 }, "contextUsageEvent"),
1433+
eventFrame({ contextUsagePercentage: 10 }, "metadataEvent"),
1434+
);
1435+
expect(fallbackFirst).toBe(authoritativeOnly!);
1436+
expect(authoritativeFirst).toBe(authoritativeOnly!);
1437+
});
1438+
14101439
test("authoritative metadata token usage overrides estimates and preserves cache splits", async () => {
14111440
const adapter = createKiroAdapter(provider);
14121441
await adapter.buildRequest(parsedWith([{ role: "user", content: "x".repeat(700) }]));

0 commit comments

Comments
 (0)