Skip to content

Commit 8b7b65c

Browse files
committed
fix(responses): keep tool-search item classification past output_item.done
The routed and ordinary id sets were cleared at output_item.done, but done ends the item, not the id's relevance. For a ROUTED id that was a leak: an upstream that emits a trailing function_call_arguments.done after the item closes found the classification gone, fell through to the unknown-id branch, and the frame reached the client as a public argument event for an item the client was told is a private tool_search_call. That mixed lifecycle is the defect this rewrite exists to prevent. For an ORDINARY id it was not a leak but was not free either: the same fall-through buffered its trailing frames as unknown and held them for an item that would never arrive. Both are now retained until the terminal event releases everything. Found by CodeRabbit on #2145.
1 parent 16345ab commit 8b7b65c

2 files changed

Lines changed: 63 additions & 4 deletions

File tree

‎src/server/responses-tool-search-repair.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,16 @@ export function createRoutedToolSearchRestoreBlockRewrite(
183183
const restoredBlock = restored.changed
184184
? replaceSseDataPayload(block, JSON.stringify(restored.value))
185185
: block;
186-
if (type === "response.output_item.done" && itemId) {
187-
routedItemIds.delete(itemId);
188-
ordinaryItemIds.delete(itemId);
189-
}
186+
// Classification is retained past `output_item.done` for BOTH kinds, until the terminal
187+
// event releases everything.
188+
//
189+
// `done` ends the item, not the id's relevance. Forgetting a ROUTED id let a trailing
190+
// `function_call_arguments.*` — which some upstreams emit after done — fall through to
191+
// the unknown-id branch and reach the client as a public frame for an item the client
192+
// was told is a private `tool_search_call`: the mixed lifecycle this rewrite exists to
193+
// prevent. Forgetting an ORDINARY id is not leak-shaped but is not free either, because
194+
// the same fall-through buffers its trailing frames as unknown and delays them until an
195+
// item that will never arrive. Neither id is dropped early.
190196
return routed ? [restoredBlock] : [...pending, restoredBlock];
191197
}
192198

‎tests/responses-tool-search-repair.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,4 +575,57 @@ describe("routed Responses tool-search compatibility", () => {
575575
const other = rewrite(frame("response.output_text.delta", { output_index: 2, delta: "hi" }));
576576
expect(other).toHaveLength(1);
577577
});
578+
579+
// `output_item.done` ends the ITEM, not the id's relevance. Forgetting the routed id there
580+
// meant a trailing argument frame — which some upstreams emit after done — fell through to
581+
// the unknown-id branch and reached the client as a public `function_call_arguments.*` for
582+
// an item the client was told is a private `tool_search_call`.
583+
test("a routed id stays suppressed after output_item.done", () => {
584+
const rewrite = createRoutedToolSearchRestoreBlockRewrite(new Set(["tool_search"]));
585+
586+
rewrite(frame("response.output_item.added", {
587+
output_index: 0,
588+
item: { type: "function_call", id: "fc_search", name: "tool_search", arguments: "" },
589+
}));
590+
const done = rewrite(frame("response.output_item.done", {
591+
output_index: 0,
592+
item: { type: "function_call", id: "fc_search", name: "tool_search", arguments: "{}" },
593+
}));
594+
expect(dataPayload(done[0]!).item).toMatchObject({ type: "tool_search_call" });
595+
596+
const trailing = rewrite(frame("response.function_call_arguments.done", {
597+
output_index: 0,
598+
item_id: "fc_search",
599+
arguments: "{}",
600+
}));
601+
expect(trailing).toEqual([]);
602+
603+
const trailingDelta = rewrite(frame("response.function_call_arguments.delta", {
604+
output_index: 0,
605+
item_id: "fc_search",
606+
delta: "x",
607+
}));
608+
expect(trailingDelta).toEqual([]);
609+
});
610+
611+
test("an ordinary item's frames still pass through after its done", () => {
612+
const rewrite = createRoutedToolSearchRestoreBlockRewrite(new Set(["tool_search"]));
613+
614+
rewrite(frame("response.output_item.added", {
615+
output_index: 0,
616+
item: { type: "function_call", id: "fc_plain", name: "not_routed", arguments: "" },
617+
}));
618+
rewrite(frame("response.output_item.done", {
619+
output_index: 0,
620+
item: { type: "function_call", id: "fc_plain", name: "not_routed", arguments: "{}" },
621+
}));
622+
623+
// Retaining routed ids must not accidentally start swallowing ordinary ones.
624+
const trailing = rewrite(frame("response.function_call_arguments.delta", {
625+
output_index: 0,
626+
item_id: "fc_plain",
627+
delta: "x",
628+
}));
629+
expect(trailing).toHaveLength(1);
630+
});
578631
});

0 commit comments

Comments
 (0)