Skip to content

Commit 367ff85

Browse files
committed
fix(bridge): cancel open tool calls on terminal error (#765)
Adapter stream paths can already surface malformed tool arguments, but the bridge was still closing the open call as completed before response.failed. Add a cancel path (status incomplete, no arguments.done), refuse to complete unparseable assembled args at tool_call_end, and restore Anthropic stream validation that errors instead of ending the call.
1 parent ba740df commit 367ff85

5 files changed

Lines changed: 273 additions & 9 deletions

File tree

‎src/adapters/anthropic.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,23 @@ function toolUseArguments(input: unknown): string {
284284
return JSON.stringify(input ?? {});
285285
}
286286

287+
/**
288+
* Whether arguments assembled from a stream's `input_json_delta` fragments are usable.
289+
* A tool block that sent no fragments at all is fine — that is a no-argument call. Anything
290+
* else has to parse, because unlike the non-stream path the fragments have already been
291+
* forwarded to the client and cannot be repaired after the fact.
292+
*/
293+
function streamedToolArgumentsParse(assembled: string): boolean {
294+
const trimmed = assembled.trim();
295+
if (!trimmed) return true;
296+
try {
297+
JSON.parse(trimmed);
298+
return true;
299+
} catch {
300+
return false;
301+
}
302+
}
303+
287304
function anthropicKeyUsesBearer(provider: OcxProviderConfig): boolean {
288305
return provider.apiKeyTransport === "bearer";
289306
}
@@ -765,6 +782,7 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
765782
let currentBlockType = "";
766783
let currentToolCallId = "";
767784
let currentToolCallName = "";
785+
let currentToolCallJson = "";
768786
let pendingUsage: Record<string, number> | undefined;
769787
let pendingStopReason: string | undefined;
770788
let emittedDone = false;
@@ -808,6 +826,7 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
808826
if (block.type === "tool_use") {
809827
currentToolCallId = block.id ?? synthesizeToolUseId();
810828
currentToolCallName = toolNames.fromWire(block.name ?? "");
829+
currentToolCallJson = "";
811830
yield { type: "tool_call_start", id: currentToolCallId, name: currentToolCallName };
812831
}
813832
if (block.type === "redacted_thinking" && typeof block.data === "string") {
@@ -833,14 +852,32 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
833852
// so a stray signature on a non-thinking block can never be captured.
834853
yield { type: "thinking_signature", signature: delta.signature };
835854
} else if (delta.type === "input_json_delta" && typeof delta.partial_json === "string" && currentBlockType === "tool_use") {
855+
// Forwarded immediately: the bridge maps each delta to a client-visible
856+
// response.function_call_arguments.delta frame, so withholding fragments until
857+
// block close would leave a started call showing empty arguments. A copy is kept
858+
// to validate the assembled payload at content_block_stop.
859+
currentToolCallJson += delta.partial_json;
836860
yield { type: "tool_call_delta", arguments: delta.partial_json };
837861
}
838862
break;
839863
}
840864
case "content_block_stop": {
841865
if (currentBlockType === "tool_use") {
866+
// The non-stream path repairs an unparseable payload in toolUseArguments(); the
867+
// stream cannot, because the fragments are already downstream. Fail the turn
868+
// instead of ending a tool call whose arguments will not parse — the bridge's
869+
// terminal-error path cancels the open call (status incomplete) rather than
870+
// completing it before response.failed (#765).
871+
if (!streamedToolArgumentsParse(currentToolCallJson)) {
872+
yield {
873+
type: "error",
874+
message: "Anthropic stream sent malformed tool_use arguments (invalid JSON)",
875+
};
876+
return;
877+
}
842878
yield { type: "tool_call_end" };
843879
currentToolCallId = "";
880+
currentToolCallJson = "";
844881
}
845882
currentBlockType = "";
846883
break;

‎src/bridge.ts‎

Lines changed: 104 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,23 @@ function responseError(status: number, type: string, message: string): OcxErrorP
6363
return classifyError(status, type, message);
6464
}
6565

66+
/**
67+
* Whether assembled function-call arguments are usable JSON.
68+
* An empty buffer is valid (no-arg tools send no deltas). Non-empty must parse —
69+
* once fragments have been streamed to the client they cannot be repaired the way
70+
* non-stream adapters degrade a bad payload to `{}`.
71+
*/
72+
function toolCallArgumentsUsable(args: string): boolean {
73+
const trimmed = args.trim();
74+
if (!trimmed) return true;
75+
try {
76+
JSON.parse(trimmed);
77+
return true;
78+
} catch {
79+
return false;
80+
}
81+
}
82+
6683
function adapterFailureFromEvent(event: Extract<AdapterEvent, { type: "error" }>): { httpStatus: number; error: OcxErrorPayload } {
6784
if (event.status === undefined && event.errorType === undefined && event.code === undefined) {
6885
return adapterFailureFromMessage(event.message);
@@ -417,6 +434,39 @@ export function bridgeToResponsesSSE(
417434
currentToolCall = null;
418435
};
419436

437+
// Terminal-error / incomplete path for an open tool call (#765 remainder).
438+
// Closing via closeCurrentToolCall() would emit function_call_arguments.done and
439+
// status:"completed" BEFORE response.failed — the client still sees an issued call.
440+
// Cancel instead: no *.done argument frames, status:"incomplete" (same pattern as an
441+
// in-flight web_search_call closing as "failed"). Args still serialize as "{}" when
442+
// empty so echoed items cannot poison the next turn with JSON.parse("").
443+
const failCurrentToolCall = () => {
444+
if (!currentToolCall) return;
445+
const argsStr = currentToolCall.args || "{}";
446+
const item = currentToolCall.toolSearch
447+
? {
448+
type: "tool_search_call", id: currentToolCall.itemId,
449+
call_id: currentToolCall.callId, execution: "client",
450+
arguments: parseArgsObj(currentToolCall.args), status: "incomplete",
451+
}
452+
: currentToolCall.freeform
453+
? {
454+
type: "custom_tool_call", id: currentToolCall.itemId,
455+
call_id: currentToolCall.callId, name: currentToolCall.name,
456+
input: freeformInput(currentToolCall.args), status: "incomplete",
457+
}
458+
: {
459+
type: "function_call", id: currentToolCall.itemId,
460+
call_id: currentToolCall.callId, name: currentToolCall.name,
461+
arguments: argsStr, status: "incomplete",
462+
...(currentToolCall.namespace ? { namespace: currentToolCall.namespace } : {}),
463+
};
464+
emit("response.output_item.done", { output_index: currentToolCall.outputIndex, item });
465+
finishedItems.push(item as OutputItem);
466+
outputIndex++;
467+
currentToolCall = null;
468+
};
469+
420470
// Finalize an open web-search cell. `status` is "completed" on a normal end, or "failed" when
421471
// the stream terminates (error/incomplete) while a search was still in flight, so Codex never
422472
// leaves a "Searching the web" spinner spinning forever.
@@ -653,6 +703,32 @@ export function bridgeToResponsesSSE(
653703
break;
654704
}
655705
case "tool_call_end": {
706+
// Fragments already streamed cannot be repaired. Refuse to complete a function call
707+
// whose assembled arguments do not parse — cancel the item and fail the turn so the
708+
// client never sees status:"completed" for unusable args (#765 stream remainder).
709+
if (
710+
currentToolCall
711+
&& !currentToolCall.freeform
712+
&& !currentToolCall.toolSearch
713+
&& !toolCallArgumentsUsable(currentToolCall.args)
714+
) {
715+
failCurrentToolCall();
716+
const failure = responseError(
717+
502,
718+
"upstream_error",
719+
"upstream stream produced malformed tool call arguments",
720+
);
721+
emit("response.failed", {
722+
response: {
723+
...responseSnapshot("failed", finishedItems),
724+
error: failure,
725+
last_error: failure,
726+
},
727+
});
728+
reportTerminal("failed");
729+
terminalEvent = true;
730+
break;
731+
}
656732
closeCurrentToolCall();
657733
break;
658734
}
@@ -749,7 +825,7 @@ export function bridgeToResponsesSSE(
749825
if (currentReasoning) closeCurrentReasoning();
750826
if (currentRawReasoning) closeCurrentRawReasoning();
751827
flushHiddenRawReasoning();
752-
if (currentToolCall) closeCurrentToolCall();
828+
if (currentToolCall) failCurrentToolCall();
753829
if (currentWebSearch) closeCurrentWebSearch("failed", []);
754830
flushHiddenReasoningEnvelope();
755831
options?.onUsage?.(event.usage);
@@ -773,7 +849,7 @@ export function bridgeToResponsesSSE(
773849
if (currentReasoning) closeCurrentReasoning();
774850
if (currentRawReasoning) closeCurrentRawReasoning();
775851
flushHiddenRawReasoning();
776-
if (currentToolCall) closeCurrentToolCall();
852+
if (currentToolCall) failCurrentToolCall();
777853
if (currentWebSearch) closeCurrentWebSearch("failed", []);
778854
const failure = adapterFailureFromEvent(event);
779855
if (event.usage) options?.onUsage?.(event.usage);
@@ -803,6 +879,7 @@ export function bridgeToResponsesSSE(
803879
} catch (err) {
804880
if (!terminated) {
805881
flushHiddenRawReasoning();
882+
if (currentToolCall) failCurrentToolCall();
806883
if (currentWebSearch) closeCurrentWebSearch("failed", []);
807884
emit("response.failed", {
808885
response: {
@@ -832,7 +909,7 @@ export function bridgeToResponsesSSE(
832909
if (currentReasoning) closeCurrentReasoning();
833910
if (currentRawReasoning) closeCurrentRawReasoning();
834911
flushHiddenRawReasoning();
835-
if (currentToolCall) closeCurrentToolCall();
912+
if (currentToolCall) failCurrentToolCall();
836913
if (currentWebSearch) closeCurrentWebSearch("failed", []);
837914
options?.onUsage?.(undefined);
838915
emit("response.incomplete", {
@@ -872,7 +949,7 @@ export function bridgeToResponsesSSE(
872949
if (currentReasoning) closeCurrentReasoning();
873950
if (currentRawReasoning) closeCurrentRawReasoning();
874951
flushHiddenRawReasoning();
875-
if (currentToolCall) closeCurrentToolCall();
952+
if (currentToolCall) failCurrentToolCall();
876953
if (currentWebSearch) closeCurrentWebSearch("failed", []);
877954
emit("response.incomplete", {
878955
response: {
@@ -1021,7 +1098,7 @@ export function buildResponseJSON(
10211098
});
10221099
currentRawReasoning = "";
10231100
};
1024-
const flushToolCall = () => {
1101+
const flushToolCall = (status: "completed" | "incomplete" = "completed") => {
10251102
if (!currentToolCallId) return;
10261103
const mapped = options?.toolNsMap?.get(currentToolCallName);
10271104
const realName = mapped?.name ?? currentToolCallName;
@@ -1032,19 +1109,19 @@ export function buildResponseJSON(
10321109
output.push({
10331110
type: "tool_search_call", id: `tsc_${uuid()}`,
10341111
call_id: currentToolCallId, execution: "client",
1035-
arguments: parseArgsObj(currentToolCallArgs), status: "completed",
1112+
arguments: parseArgsObj(currentToolCallArgs), status,
10361113
});
10371114
} else if (freeform) {
10381115
output.push({
10391116
type: "custom_tool_call", id: `ctc_${uuid()}`,
10401117
call_id: currentToolCallId, name: realName,
1041-
input: freeformInput(currentToolCallArgs), status: "completed",
1118+
input: freeformInput(currentToolCallArgs), status,
10421119
});
10431120
} else {
10441121
output.push({
10451122
type: "function_call", id: `fc_${uuid()}`,
10461123
call_id: currentToolCallId, name: realName,
1047-
arguments: currentToolCallArgs || "{}", status: "completed",
1124+
arguments: currentToolCallArgs || "{}", status,
10481125
...(ns ? { namespace: ns } : {}),
10491126
});
10501127
}
@@ -1110,6 +1187,23 @@ export function buildResponseJSON(
11101187
currentToolCallArgs += e.arguments;
11111188
break;
11121189
case "tool_call_end":
1190+
if (!toolCallArgumentsUsable(currentToolCallArgs) && currentToolCallId) {
1191+
// Mirror the streaming path: refuse to complete unusable arguments.
1192+
const mapped = options?.toolNsMap?.get(currentToolCallName);
1193+
const realName = mapped?.name ?? currentToolCallName;
1194+
const toolSearch = options?.toolSearchToolNames?.has(realName) ?? false;
1195+
const freeform = !toolSearch && (options?.freeformToolNames?.has(realName) ?? false);
1196+
if (!freeform && !toolSearch) {
1197+
flushToolCall("incomplete");
1198+
errorEvent = {
1199+
type: "error",
1200+
message: "upstream stream produced malformed tool call arguments",
1201+
status: 502,
1202+
errorType: "upstream_error",
1203+
};
1204+
break;
1205+
}
1206+
}
11131207
flushToolCall();
11141208
break;
11151209
case "web_search_call_begin":
@@ -1155,7 +1249,8 @@ export function buildResponseJSON(
11551249
flushText(cleanDone && !errorEvent && !incompleteEvent ? "final_answer" : undefined);
11561250
flushSummaryReasoning();
11571251
flushRawReasoning();
1158-
flushToolCall();
1252+
// Open tool call on a failed/incomplete turn must not land as status:"completed".
1253+
if (currentToolCallId) flushToolCall(errorEvent || incompleteEvent ? "incomplete" : "completed");
11591254
// A truncated turn must never be installed as replacement history: emit the
11601255
// compaction item only when the turn actually completed (#422).
11611256
if (

‎structure/01_runtime.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ The bridge enforces a heartbeat stall deadline. It defaults to 300 seconds sampl
6363
invariant; sidecars keep their own clocks. On expiry the stream is closed and the upstream request
6464
cancelled. If the adapter generator ends without an explicit done/error event, the response is marked
6565
`incomplete` rather than `completed` so Codex can distinguish a clean finish from a truncated stream.
66+
On `error` / incomplete / stall / EOF — and when assembled non-freeform tool arguments fail to parse —
67+
an open tool call is cancelled as `status: "incomplete"` without `function_call_arguments.done`, so
68+
the client never sees a completed call ahead of `response.failed` / `response.incomplete`.
6669

6770
The server exposes `POST /api/stop` which restores native Codex config, stops any installed service
6871
(to prevent respawn), and exits the process. The GUI sidebar stop button calls this endpoint.

‎tests/anthropic-stream-hardening.test.ts‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,92 @@ describe("anthropic non-stream tool_use input", () => {
104104
});
105105
});
106106

107+
test("malformed streamed tool arguments fail the turn instead of completing the call", async () => {
108+
// The stream forwards fragments as they arrive (the bridge turns each into a client-visible
109+
// frame), so a malformed payload cannot be repaired the way the non-stream path repairs it.
110+
// It must not end the call normally either: a client that already received `not json` would
111+
// be told the tool call completed. The turn errors instead; the bridge cancels the open call.
112+
const response = new Response([
113+
"event: content_block_start\n",
114+
'data: {"type":"content_block_start","content_block":{"type":"tool_use","id":"toolu_9","name":"get_weather"}}\n\n',
115+
"event: content_block_delta\n",
116+
'data: {"type":"content_block_delta","delta":{"type":"input_json_delta","partial_json":"not json"}}\n\n',
117+
"event: content_block_stop\n",
118+
'data: {"type":"content_block_stop"}\n\n',
119+
"event: message_stop\n",
120+
'data: {"type":"message_stop"}\n\n',
121+
].join(""));
122+
const events = await collect(createAnthropicAdapter(provider).parseStream(response));
123+
expect(events.at(-1)?.type).toBe("error");
124+
expect(events.some(e => e.type === "tool_call_end")).toBe(false);
125+
expect(events.some(e => e.type === "done")).toBe(false);
126+
});
127+
128+
test("valid streamed tool arguments still arrive as incremental deltas", async () => {
129+
// The bridge maps each adapter delta to response.function_call_arguments.delta, so the
130+
// fragments must keep flowing as they arrive rather than being withheld until block close.
131+
const response = new Response([
132+
"event: content_block_start\n",
133+
'data: {"type":"content_block_start","content_block":{"type":"tool_use","id":"toolu_10","name":"get_weather"}}\n\n',
134+
"event: content_block_delta\n",
135+
'data: {"type":"content_block_delta","delta":{"type":"input_json_delta","partial_json":"{\\"city\\":"}}\n\n',
136+
"event: content_block_delta\n",
137+
'data: {"type":"content_block_delta","delta":{"type":"input_json_delta","partial_json":"\\"Paris\\"}"}}\n\n',
138+
"event: content_block_stop\n",
139+
'data: {"type":"content_block_stop"}\n\n',
140+
"event: message_stop\n",
141+
'data: {"type":"message_stop"}\n\n',
142+
].join(""));
143+
const events = await collect(createAnthropicAdapter(provider).parseStream(response));
144+
expect(events.filter(e => e.type === "tool_call_delta")).toMatchObject([
145+
{ type: "tool_call_delta", arguments: '{"city":' },
146+
{ type: "tool_call_delta", arguments: '"Paris"}' },
147+
]);
148+
expect(events.some(e => e.type === "tool_call_end")).toBe(true);
149+
expect(events.at(-1)?.type).toBe("done");
150+
});
151+
152+
test("two tool_use blocks in one message keep separate argument buffers", async () => {
153+
const response = new Response([
154+
"event: content_block_start\n",
155+
'data: {"type":"content_block_start","content_block":{"type":"tool_use","id":"toolu_a","name":"one"}}\n\n',
156+
"event: content_block_delta\n",
157+
'data: {"type":"content_block_delta","delta":{"type":"input_json_delta","partial_json":"{\\"one\\":1}"}}\n\n',
158+
"event: content_block_stop\n",
159+
'data: {"type":"content_block_stop"}\n\n',
160+
"event: content_block_start\n",
161+
'data: {"type":"content_block_start","content_block":{"type":"tool_use","id":"toolu_b","name":"two"}}\n\n',
162+
"event: content_block_delta\n",
163+
'data: {"type":"content_block_delta","delta":{"type":"input_json_delta","partial_json":"{\\"two\\":2}"}}\n\n',
164+
"event: content_block_stop\n",
165+
'data: {"type":"content_block_stop"}\n\n',
166+
"event: message_stop\n",
167+
'data: {"type":"message_stop"}\n\n',
168+
].join(""));
169+
const events = await collect(createAnthropicAdapter(provider).parseStream(response));
170+
expect(events.filter(e => e.type === "tool_call_delta")).toMatchObject([
171+
{ type: "tool_call_delta", arguments: '{"one":1}' },
172+
{ type: "tool_call_delta", arguments: '{"two":2}' },
173+
]);
174+
expect(events.filter(e => e.type === "tool_call_end")).toHaveLength(2);
175+
expect(events.at(-1)?.type).toBe("done");
176+
});
177+
178+
test("a tool_use block with no argument fragments is not treated as malformed", async () => {
179+
const response = new Response([
180+
"event: content_block_start\n",
181+
'data: {"type":"content_block_start","content_block":{"type":"tool_use","id":"toolu_c","name":"now"}}\n\n',
182+
"event: content_block_stop\n",
183+
'data: {"type":"content_block_stop"}\n\n',
184+
"event: message_stop\n",
185+
'data: {"type":"message_stop"}\n\n',
186+
].join(""));
187+
const events = await collect(createAnthropicAdapter(provider).parseStream(response));
188+
expect(events.some(e => e.type === "error")).toBe(false);
189+
expect(events.some(e => e.type === "tool_call_end")).toBe(true);
190+
expect(events.at(-1)?.type).toBe("done");
191+
});
192+
107193
test("EOF after message_delta.stop_reason settles instead of erroring", async () => {
108194
// The other two EOF tests assert the pre-existing error path and never send a stop reason,
109195
// so they stay green with this fallback reverted -- they do not test the change they shipped

0 commit comments

Comments
 (0)