Skip to content

Commit 649912c

Browse files
authored
Merge pull request #2180 from lidge-jun/codex/openai-chat-tool-call-heartbeat
fix(openai-chat): heartbeat while buffering tool-call deltas
2 parents 8f974f0 + 2a8b81e commit 649912c

5 files changed

Lines changed: 110 additions & 2 deletions

File tree

‎src/adapters/openai-chat.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,21 @@ 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.
1751+
//
1752+
// Found while investigating #2156, but it is NOT that bug: a stall abort emits
1753+
// `response.incomplete` with `upstream_stall_timeout` from the bridge, whereas
1754+
// that report shows the adapter's own end-of-stream error after `reader.read()`
1755+
// returned EOF with tool calls still pending. Different path, different frame.
1756+
//
1757+
// A heartbeat is invisible downstream — the bridge consumes it to re-arm the
1758+
// watchdog and emits nothing — which is the same remedy the Cursor, Anthropic,
1759+
// Google, and Kiro adapters already use for their own silent phases.
1760+
yield { type: "heartbeat" };
17461761
if (typeof rawArguments === "string" && rawArguments) {
17471762
const previousBytes = call.argsBytes;
17481763
const nextBytes = previousBytes + budgetEncoder.encode(rawArguments).byteLength;

‎src/server/responses/terminal-guard.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,16 @@ export async function* guardTerminalEventStream(options: GuardedEventStreamOptio
193193
const seen: AdapterEvent[] = [];
194194
let terminalSeen = false;
195195
for await (const event of source) {
196+
// A heartbeat is adapter liveness, not turn content: it exists so the bridge watchdog
197+
// can tell a buffering adapter from a hung one. Retaining it here would put an
198+
// unbounded number of empty markers into `seen`, which feeds both the continuation
199+
// analysis and the rebuilt request — and the openai-chat adapter now emits one per
200+
// tool-call delta, so a long argument payload alone could grow this array without
201+
// limit. The empty-completion guard already passes them through unretained; match it.
202+
if (event.type === "heartbeat") {
203+
yield event;
204+
continue;
205+
}
196206
if (event.type === "done") {
197207
terminalSeen = true;
198208
const analysis = (options.adapterName === "anthropic" || options.adapterName === "openai-chat")

‎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: 33 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.
46+
for await (const event of stream) if (event.type !== "heartbeat") events.push(event);
4447
return events;
4548
}
4649

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

‎tests/terminal-guard.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,54 @@ describe("terminal guard", () => {
188188
expect(actual.at(-1)).toMatchObject({ usage: { inputTokens: 30, outputTokens: 5, totalTokens: 35 } });
189189
});
190190

191+
192+
// A heartbeat is adapter liveness, not turn content. The openai-chat adapter emits one per
193+
// tool-call delta while it buffers, so retaining them here would let a single large argument
194+
// payload grow `seen` without bound — and `seen` is what both the continuation analysis and
195+
// the rebuilt request read. Passing them through unretained is what the empty-completion
196+
// guard already does.
197+
// A heartbeat is adapter liveness, not turn content. The openai-chat adapter emits one per
198+
// tool-call delta while it buffers, so retaining them would grow the guard's record without
199+
// bound on a large argument payload. `analyzeTerminalTurn` and `buildContinuationRequest`
200+
// both read that record, so pin the contract on the pure functions that consume it plus the
201+
// observable passthrough.
202+
test("a retained heartbeat would corrupt the continuation record", () => {
203+
const clean: AdapterEvent[] = [
204+
{ type: "text_delta", text: "我接下来会修改相关文件。" },
205+
];
206+
const padded: AdapterEvent[] = [
207+
{ type: "text_delta", text: "我接下来会修改相关文件。" },
208+
...Array.from({ length: 50 }, () => ({ type: "heartbeat" }) as AdapterEvent),
209+
];
210+
const request = parsed("继续检查");
211+
// The guard must not let liveness markers change what the continuation decides or sends.
212+
expect(analyzeTerminalTurn(request, padded).assistantText)
213+
.toBe(analyzeTerminalTurn(request, clean).assistantText);
214+
expect(JSON.stringify(buildContinuationRequest(request, padded).context.messages))
215+
.toBe(JSON.stringify(buildContinuationRequest(request, clean).context.messages));
216+
});
217+
218+
test("heartbeats reach the consumer so the bridge watchdog stays armed", async () => {
219+
const actual: AdapterEvent[] = [];
220+
for await (const event of guardTerminalEventStream({
221+
parsed: parsed("继续检查"),
222+
firstEvents: (async function* () {
223+
yield { type: "text_delta", text: "我接下来会修改相关文件。" } as AdapterEvent;
224+
for (let i = 0; i < 50; i++) yield { type: "heartbeat" } as AdapterEvent;
225+
yield { type: "tool_call_start", id: "call_1", name: "exec_command" } as AdapterEvent;
226+
yield { type: "tool_call_end" } as AdapterEvent;
227+
yield { type: "done", usage: { inputTokens: 10, outputTokens: 2 } } as AdapterEvent;
228+
})(),
229+
continuation: () => (async function* () {
230+
yield { type: "done" } as AdapterEvent;
231+
})(),
232+
adapterName: "openai-chat",
233+
})) actual.push(event);
234+
235+
expect(actual.filter(event => event.type === "heartbeat")).toHaveLength(50);
236+
expect(actual.filter(event => event.type === "done")).toHaveLength(1);
237+
});
238+
191239
test("stops after the configured continuation bound", async () => {
192240
let continuations = 0;
193241
const actual: AdapterEvent[] = [];

0 commit comments

Comments
 (0)