Repository navigation
Give each tool call its own id when a model reuses or omits one - #2255
Merged
Merged
Conversation
A model can send one tool call id in several replies, repeat it within one reply, or leave it empty. fx used the model's id as the call's identity, so the live transcript, the recap, and ACP lost every call after the first, and a repeated or empty id stopped the turn. Each provider adapter now gives a call an fx id (fx_call_<n>) when the model's id is empty, blank, cannot be stored, repeats an id earlier in the reply, or repeats one the conversation already used, before it writes its replay state. A call from an earlier reply sent again with the same id, tool, and arguments keeps its id, so the subagent replay guard still answers it from saved state. Calls the provider ran keep their own ids. Core passes this turn's messages to the adapters, since compaction can drop them from the request.
libfx reads AI Gateway streams through the same parser, so it needs the rule that gives each tool call its own id. That code adds about 4.8 KB to fx-core.wasm, which was already at 2,462,396 of its 2,465,000 bytes on main.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a model reused a tool call id, repeated one in a reply, or left it out, fx lost calls or stopped the turn:
request failed: malformed authoritative tool identity, and no call ran. On a custom OpenAI-compatible or Anthropic-compatible connection the stream failed instead.Now a call gets its own id when the model's id cannot be used, so no call is lost and no turn stops for this. A call that repeats an earlier call exactly keeps its id, so the subagent tool still answers a message it already ran from saved state.
Root cause
fx used the id the model sent as the call's own identity and never made it unique.
authoritativeToolAdmissioninsrc/core/shared/types.zig) compares ids only inside one reply. A reused id passes, and the live transcript, which keys calls by turn and id, drops the second call's start as a late event (src/ui/transcript/runtime.zig,late lifecycle event ignored).chat_completions_protocol.zig(InvalidToolCallId,ConflictingIdentity),anthropic_messages_protocol.zig(InvalidEvent,ConflictingIdentity), andresponses_protocol.zig(ToolCallLimitExceeded; afunction_callwith nocall_idwas dropped).Design
Two designs were weighed.
fx_call_<n>, the first number that no id in the conversation or the reply has.One exception keeps the subagent tool's replay guard. The subagent tool keys a delegation by its call id: a message it already ran is answered from saved state instead of running again. So a call keeps a reused id when it is a call from an earlier reply sent again: the same id, the same tool, and the same arguments. Only one call in a reply can keep an id this way; a second copy in the same reply gets an fx id.
"Same arguments" means:
json_comparison.serializedEqual). Key order and whitespace do not matter, escapes that decode to the same text match, and1and1.0differ. Text that is not JSON matches only byte for byte.{"request": ...}wrapper taken off either side, an object or JSON text of one (tool_args.unwrapRequest). fx takes that wrapper off before it storesshell,subagent, andread_tool_resultcalls, so a stored call and the model's repeat of it can differ by it.std.json.Stringifymust never see them.Where the rule lives:
tool_call_ids.ReplyIds(src/gateway/tool_call_ids.zig) holds the rule. It takes as used every call and result id in the request, the aliases the request sends for non-portable ids, and this turn's earlier calls. Compaction can take those out of the request, so core passes them in the new borrowedcurrent_turn_messagesfield ofRequestDataandModelRequest.client.zig):SseCallIdsrewrites each tool event's id before the parser and the replay builder read it. Streamed input keeps its pairing with its call, and a reused stream id after its call ended starts a new call.chat_completions_protocol.zig): ids are given when the reply ends, before_tool_call_idsis written.anthropic_messages_protocol.zig): ids are given when atool_useblock starts, so the live start already has its id.responses_protocol.zig, used by Codex, Grok, and custom Responses connections): ids are given atoutput_item.added; later events are still checked against the server's owncall_id.provisional_id. The transcript already uses that field to match a start to its final call, and it is never saved.What the model receives
Saved sessions
There is no format change: the fx id is stored in the call's existing id field. Sessions saved before this change, which can hold one id in several steps, resume as before and are sent to the model unchanged. Only a new call that reuses a saved id for different work gets an fx id.
Package size
libfx reads AI Gateway streams through the same parser, so
fx-core.wasmcarries the new rule. It grows by about 4.8 KB: 2,462,396 bytes on mainb59801471, 2,467,236 with this PR, past its 2,465,000-byte budget. The second commit raises that one budget to 2,475,000 bytes. Every other package stays within its budget.Before and after
The macOS binary against a fake gateway, three edits of
g.tsethat each swap seven lines for one. Main is381fb613a, the merge base.dupin 3 replies● Edited g.tse +1/-7,1 file changed +1 -7● Edited g.tse +3/-21 · 3 edits● Edited g.tse +3/-21 · 3 edits,1 file changed +3 -21same3 times in 1 reply✗ system: request failed: malformed authoritative tool identity, 0 of 3 editsResuming the interrupted requestu1tou3Ids the model got back with each result: F
dup,fx_call_1,fx_call_2; Gsame,fx_call_1,fx_call_2; Hfx_call_1,fx_call_2,fx_call_3.Tests
Unit tests, which fail on main for F, G, and H in each adapter:
client.zig):consumeSseStream gives a call id repeated in one reply an fx id,consumeSseStream gives calls with missing, empty, blank, or oversized ids fx ids,consumeSseStream keeps each repeated-id call's replay part on its own id, andan AI Gateway call id an earlier step, this turn, or the wire already uses gets an fx id. The earlier-step test,a call the AI Gateway provider runs keeps its own id, even one already used, andunique AI Gateway call ids come through byte for bytetake the new conversation input, so they do not compile on main; the others fail there.chat completions gives a call the id an earlier step used an fx idandchat completions gives repeated, missing, empty, and oversized call ids fx ids, and sends them back.anthropic messages gives a call the id an earlier step used an fx idandanthropic messages gives repeated, missing, empty, and oversized call ids fx ids.configured responses gives a call the id an earlier step used an fx id,configured responses gives repeated, missing, and empty call ids fx ids, andOpenAI Codex gives an oversized call id an fx id and rejects an oversized tool name.every model request carries this turn's calls so far, which a reply may not reuse(tool_flow.zig).The same-call rule:
tool_call_ids.zig: key order, whitespace, and escapes;1against1.0; another tool; text that is not JSON; a second copy in one reply; an id only a tool result holds; a compacted call from this turn; therequestwrapper as an object or as JSON text, on either side; a wrapper beside another key; arguments nested 300 levels deep.provisional_id.unique AI Gateway call ids come through byte for byteanda reply keeps every provider id no other call has, byte for byte.E2E tests:
tui-tool-story.test.ts:edits whose calls have <case> each run and count, live and after resume, for F, G, and H. All three fail on main.acp.test.ts:each ACP tool call has its own toolCallId when the model reuses, repeats, or leaves out ids. It fails on main.sessions.test.ts:a session saved with one call id in several steps resumes as before, and a new reuse gets an fx id. It writes a session in main's shape and checks that it resumes and is sent unchanged. On main it fails only at the new reuse.gateway-stream-lifecycle.test.ts:subagent replay returns saved success and failure without another workerpasses unchanged. The blank, long-id, and long-provisional cases and the Codex and Grok 257-byte case intui-auth-source-selection.test.tsnow expect the call to run under an fx id.b59801471:tui-tool-story32 pass,sessions115 (30 skip),gateway-stream-lifecycle370 (2 skip),tui-auth-source-selection231,configured-providers116,tui-custom-connection-errors6,acp294 (14 skip),session-recovery35 (1 skip),ask-presentation23,tui-gateway-stream-lifecycle110,tui-full-detail11,config-persistence33, andtui-resume93 with 1 failure. That failure,manual upgrade output links stable notes and dev changes, comes from building a tree with no.gitfolder: mainb59801471built the same way fails it too, and this branch passes it in its git checkout.Other checks:
b59801471in the same sandbox, which runs as root (read-only folders and files, undo of a protected file, a read-only blob).configured_provider,agent_runtime,recovery,auth,gateway_open,saved_turn, andtoolboxchecks pass, and theconfigured_provider,agent_runtime(both drivers),recovery,gateway_open, andsaved_turndrivers accept the change.std.json.Stringify.reply_call_idstarget for the rule. See Check findings for the rounds.Threat review
The model, or the server it speaks through, chooses every call id. An id decides which call a result, a replay part, a live row, an ACP
toolCallId, and a subagent delegation belong to.fx_call_<n>is the first number that no id in the request (calls and results), no alias the request sends (fx_<hash>_<n>), no call from this turn, and no call in this reply has. A model id in either form is an ordinary id: it is kept when no other call has it and replaced when one does. Tests:fx ids skip every id the conversation holds, fx's own forms tooand the alias case in the AI Gateway test.operation_conflict; now it runs as a new message, which the model could already ask for with a new id.requestobject back out. Text nested past 64 levels skips that step, becausestd.json.Stringifystops a safe build past 256 levels. Without the guard, a 300-level wrapper crashed the test binary; the unit test now covers it. Parsing andserializedEqualdo not recurse.Check findings
node ~/.agents/skills/factory/bin/check --base 381fb613areports 76 findings, the same on3bbeb8e05and6cabb9f0c.src/gateway/client.zig(5): the tests that expected a repeated id to stay as sent, a missing id to stay absent and empty, one start, and no identity failure. They are renamed (consumeSseStream gives a call id repeated in one reply an fx id,consumeSseStream gives calls with missing, empty, blank, or oversized ids fx ids) and now check each fx id, each start, and admission.tests/e2e/gateway-stream-lifecycle.test.ts(6): the unstorable-idtest.eachexpected exit 1,MalformedAuthoritativeToolIdentity, no tool calls, one request, no file, and a trace line for every kind;blank current tool id is rejected before file executionexpected the same. The name kinds keep those checks. The long-id and long-provisional kinds (an oversized tool id runs under an fx id beside its sibling) and the blank id (a blank tool id runs under an fx id) now check that the call runs, its result, and the ids sent back.tests/e2e/tui-auth-source-selection.test.ts(7): the Codex and Grok case expected a 257-byte id to be refused. It now expects both lengths to run, with the 256-byte id kept and the 257-byte id replaced byfx_call_1, in the request body and in the saved history.agent_runtime,auth,saved_turn, andtoolboxfororchestrator.zig,gateway_openforclient.zig, andconfigured_providerfor the adapters. None of these models has call ids in its state. The orchestrator change only passes this turn's messages into the request, theclient.zigchange is in the event parser and not in opening the stream, and the adapter change keeps every connection, retry, and resume step the same. All seven checks pass, and their drivers accept the change (see Tests).reply_call_ids,vercel_stream,chat_completions_stream,responses_stream,responses_text,gateway_event_class,gateway_team_display,provider_finish_raw,request_wrapper_text,tool_argument_diagnostic) ran for 60 seconds each on this commit: allok, with no reproducer. The 39 related targets reach the change only throughtypes.zig(where admission now calls the newConversationIdentity.isBlank),stream_provider.zig(the new borrowed field), orbuiltins/gateway.zig; I left them to the nightly run.src/fuzz_targets.zigandtests/fuzz/targets.jsonregisterreply_call_ids.python3 -m unittest scripts.tests.test_fuzz_nightlypasses (21 tests) andzig build fuzzreplays every target (9/9 steps). The Fuzz workflow on this PR is the last check.client.zig, a cancel flag (std.atomic.Value(bool)) in a test helper, the same flag the other stream tests use. The TSan workflow on this branch passed (run 45281468470) with 7WARNING: ThreadSanitizerlines, the same 7 as main's nightly run one27ae3e51(the stuck-lookup test helper, three incapture_store, and ashell_snapshotthread leak among them). None names this helper or code this PR changes.Limits
toolCallIdstill merge with the first call's until a resume. The call itself runs.requestwrapper among fx's argument fixes. A repeat that differs from the stored call only by another fix, such as theactionfx fills in forshell, gets an fx id and runs as a new call. Only the subagent tool keys work by call id, and the wrapper is its only fix.fx_call_<n>can repeat an id from a turn the model no longer sees. The transcript keys calls by turn, and saved sessions pair results step by step, so neither is affected.