Skip to content

Commit 85fbf9a

Browse files
committed
fix(openai-chat): heartbeat while buffering tool-call deltas
Tool-call deltas are buffered until a terminal signal, so this adapter can consume upstream frames for a long time while yielding nothing. The Responses bridge arms its stall watchdog on ADAPTER activity, not socket activity, so a model streaming a large argument payload was indistinguishable from a hung upstream and could have its turn aborted while it was progressing normally. Found while investigating #2156. It is not the reported error -- that one is the EOF fail-closed guard, and the guard is correct: a stream that ends mid tool call with neither finish_reason nor [DONE] may have truncated the arguments, and promoting them would execute a partial call. But the silent buffering phase is our own hazard and it is worth closing on its own. A heartbeat is invisible downstream: the bridge consumes it to re-arm the watchdog and emits nothing. The Cursor, Anthropic, Google, and Kiro adapters already use exactly this for their own silent phases. The two test collectors now drop heartbeats, which keeps their assertions about the wire the client actually sees.
1 parent 749ab22 commit 85fbf9a

3 files changed

Lines changed: 43 additions & 2 deletions

File tree

‎src/adapters/openai-chat.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,16 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
17431743
if (idDelta && !call.id) call.id = idDelta;
17441744
if (typeof rawName === "string" && rawName && !call.name) call.name = rawName;
17451745
if (typeof rawArguments === "string") call.sawArgumentsString = true;
1746+
// Tool-call deltas are BUFFERED until a terminal signal, so this adapter can
1747+
// consume upstream frames for a long time while yielding nothing. The Responses
1748+
// bridge reads adapter activity, not socket activity, so a model that streams a
1749+
// large argument payload looks identical to a hung upstream and the stall
1750+
// watchdog can abort a turn that was progressing normally (#2156).
1751+
//
1752+
// A heartbeat is invisible downstream — the bridge consumes it to re-arm the
1753+
// watchdog and emits nothing — which is the same remedy the Cursor, Anthropic,
1754+
// Google, and Kiro adapters already use for their own silent phases.
1755+
yield { type: "heartbeat" };
17461756
if (typeof rawArguments === "string" && rawArguments) {
17471757
const previousBytes = call.argsBytes;
17481758
const nextBytes = previousBytes + budgetEncoder.encode(rawArguments).byteLength;

‎tests/openai-chat-eof.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ const provider = { adapter: "openai-chat", baseUrl: "https://example.test/v1", a
1111

1212
async function collect(gen: AsyncGenerator<AdapterEvent>): Promise<AdapterEvent[]> {
1313
const out: AdapterEvent[] = [];
14-
for await (const e of gen) out.push(e);
14+
// Heartbeats are invisible downstream: the bridge consumes them to re-arm its stall
15+
// watchdog and emits nothing. Dropping them here keeps these assertions about the wire
16+
// the client actually sees (#2156).
17+
for await (const e of gen) if (e.type !== "heartbeat") out.push(e);
1518
return out;
1619
}
1720

‎tests/openai-chat-hardening.test.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ function provider(overrides: Partial<OcxProviderConfig> = {}): OcxProviderConfig
4040

4141
async function collect(stream: AsyncGenerator<AdapterEvent>): Promise<AdapterEvent[]> {
4242
const events: AdapterEvent[] = [];
43-
for await (const event of stream) events.push(event);
43+
// Heartbeats are invisible downstream: the bridge consumes them to re-arm its stall
44+
// watchdog and emits nothing. Dropping them here keeps these assertions about the wire
45+
// the client actually sees (#2156).
46+
for await (const event of stream) if (event.type !== "heartbeat") events.push(event);
4447
return events;
4548
}
4649

@@ -920,4 +923,29 @@ describe("openai-chat response_format emission", () => {
920923
.toEqual({ type: "json_object" });
921924
});
922925
});
926+
927+
// #2156: tool-call deltas are BUFFERED until a terminal signal, so this adapter can consume
928+
// upstream frames for a long time while yielding nothing downstream. The Responses bridge
929+
// arms its stall watchdog on ADAPTER activity, not socket activity, so a model streaming a
930+
// large argument payload was indistinguishable from a hung upstream.
931+
test("tool-call deltas emit heartbeats so a long buffering phase is not read as a stall", async () => {
932+
const adapter = createOpenAIChatAdapter(provider());
933+
const frames = ['data: ' + JSON.stringify({ choices: [{ delta: { tool_calls: [{ index: 0, id: "call_a", function: { name: "shell", arguments: "" } }] } }] }) + '\n\n'];
934+
// Many argument chunks and nothing else: exactly the shape that looked like silence.
935+
for (let i = 0; i < 12; i += 1) {
936+
frames.push('data: ' + JSON.stringify({ choices: [{ delta: { tool_calls: [{ index: 0, function: { arguments: '"x"' } }] } }] }) + '\n\n');
937+
}
938+
frames.push('data: {"choices":[{"delta":{},"finish_reason":"tool_calls"}]}\n\n', "data: [DONE]\n\n");
939+
940+
const raw: AdapterEvent[] = [];
941+
for await (const event of adapter.parseStream(new Response(frames.join("")))) raw.push(event);
942+
943+
// One per consumed tool-call delta: the watchdog sees activity for the whole phase.
944+
expect(raw.filter(e => e.type === "heartbeat").length).toBeGreaterThanOrEqual(12);
945+
// And the client-visible wire is unchanged -- a heartbeat is consumed by the bridge.
946+
const visible = raw.filter(e => e.type !== "heartbeat");
947+
expect(visible.some(e => e.type === "error")).toBe(false);
948+
expect(visible).toContainEqual({ type: "tool_call_start", id: "call_a", name: "shell" });
949+
expect(visible.at(-1)).toMatchObject({ type: "done" });
950+
});
923951
});

0 commit comments

Comments
 (0)