Skip to content

Commit b9b723c

Browse files
baileyh8Wibias
authored andcommitted
fix(responses): guard late custom tool frames
1 parent d20aae4 commit b9b723c

2 files changed

Lines changed: 70 additions & 3 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,10 @@ export function createRoutedCustomToolRestoreBlockRewrite(
121121
const matched: PendingArgumentBlock[] = [];
122122
const remaining: PendingArgumentBlock[] = [];
123123
for (const pending of pendingArguments) {
124-
const sameItem = itemId !== undefined && pending.itemId === itemId;
125-
const sameIndex = outputIndex !== undefined && pending.outputIndex === outputIndex;
126-
(sameItem || sameIndex ? matched : remaining).push(pending);
124+
const matches = pending.itemId !== undefined
125+
? itemId !== undefined && pending.itemId === itemId
126+
: outputIndex !== undefined && pending.outputIndex === outputIndex;
127+
(matches ? matched : remaining).push(pending);
127128
}
128129
pendingArguments = remaining;
129130
const retainedBytes = matched.reduce((total, pending) => total + pending.retainedBytes, 0);
@@ -134,6 +135,7 @@ export function createRoutedCustomToolRestoreBlockRewrite(
134135
};
135136

136137
const rewrite: SseBlockRewrite = (block: string): readonly string[] => {
138+
if (disposed) return [block];
137139
const payload = sseDataPayload(block);
138140
if (payload === null || payload === "[DONE]") return [block];
139141
let parsed: unknown;

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,68 @@ describe("routed Responses custom-tool compatibility", () => {
214214
rewrite.dispose?.();
215215
});
216216

217+
test("does not match a known pending item id by output index alone", () => {
218+
const budget = createTestTranslatorBudget();
219+
const chargeRetained = budget.chargeRetained.bind(budget);
220+
const releaseRetained = budget.releaseRetained.bind(budget);
221+
let charges = 0;
222+
let releases = 0;
223+
budget.chargeRetained = (...args) => {
224+
charges += 1;
225+
chargeRetained(...args);
226+
};
227+
budget.releaseRetained = (...args) => {
228+
releases += 1;
229+
releaseRetained(...args);
230+
};
231+
const rewrite = createRoutedCustomToolRestoreBlockRewrite(new Set(["exec"]), budget);
232+
233+
expect(rewrite(frame("response.function_call_arguments.delta", {
234+
output_index: 0,
235+
item_id: "fc_a",
236+
delta: "{\"input\":\"a",
237+
}))).toEqual([]);
238+
expect(charges).toBe(1);
239+
240+
const added = rewrite(frame("response.output_item.added", {
241+
output_index: 0,
242+
item: {
243+
type: "function_call",
244+
id: "fc_b",
245+
call_id: "call_b",
246+
name: "exec",
247+
arguments: "",
248+
status: "in_progress",
249+
},
250+
}));
251+
expect(added.map(block => dataPayload(block).type)).toEqual(["response.output_item.added"]);
252+
expect(JSON.stringify(added)).not.toContain('"item_id":"ctc_b"');
253+
expect(charges).toBe(1);
254+
expect(releases).toBe(0);
255+
256+
rewrite.dispose?.();
257+
expect(budget.snapshot().currentBytes).toBe(0);
258+
});
259+
260+
test("does not retain argument events that arrive after a terminal event", () => {
261+
const budget = createTestTranslatorBudget();
262+
const rewrite = createRoutedCustomToolRestoreBlockRewrite(new Set(["exec"]), budget);
263+
264+
rewrite(frame("response.completed", {
265+
response: { id: "resp_1", status: "completed", output: [] },
266+
}));
267+
expect(budget.snapshot().currentBytes).toBe(0);
268+
269+
rewrite(frame("response.function_call_arguments.delta", {
270+
output_index: 0,
271+
item_id: "fc_late",
272+
delta: "{\"input\":\"late",
273+
}));
274+
rewrite.dispose?.();
275+
276+
expect(budget.snapshot().currentBytes).toBe(0);
277+
});
278+
217279
test("replays buffered events unchanged when item done identifies an ordinary function", () => {
218280
const budget = createTestTranslatorBudget();
219281
const rewrite = createRoutedCustomToolRestoreBlockRewrite(new Set(["exec"]), budget);
@@ -398,6 +460,7 @@ describe("routed Responses custom-tool compatibility", () => {
398460
test("handleResponses leaves custom tools native for forward-auth passthrough", async () => {
399461
const savedFetch = globalThis.fetch;
400462
let outboundBody: Record<string, unknown> | undefined;
463+
let outboundAuthorization: string | null = null;
401464
const upstreamItem = {
402465
type: "function_call",
403466
id: "fc_exec",
@@ -408,6 +471,7 @@ describe("routed Responses custom-tool compatibility", () => {
408471
};
409472
globalThis.fetch = (async (_input, init) => {
410473
outboundBody = JSON.parse(String(init?.body)) as Record<string, unknown>;
474+
outboundAuthorization = new Headers(init?.headers).get("authorization");
411475
return new Response(JSON.stringify({ id: "resp_forward", status: "completed", output: [upstreamItem] }), {
412476
headers: { "content-type": "application/json" },
413477
});
@@ -438,6 +502,7 @@ describe("routed Responses custom-tool compatibility", () => {
438502
const clientBody = await response.json() as { output: Array<Record<string, unknown>> };
439503
const outboundTools = outboundBody?.tools as Array<Record<string, unknown>> | undefined;
440504

505+
expect(outboundAuthorization).toBe("Bearer caller-token");
441506
expect(outboundTools?.[0]).toMatchObject({ type: "custom", name: "exec" });
442507
expect(clientBody.output[0]).toMatchObject({ type: "function_call", name: "exec" });
443508
expect(clientBody.output[0]).not.toHaveProperty("input");

0 commit comments

Comments
 (0)