What: joinToolRuns (responses.go), which mends a relayed Responses
request's tool runs in attempt before every destination, now moves an
assistant message the model wrote between its own calls, before the run's
first result, to just ahead of the run's first call, together with the
reasoning items right before it (so a reasoning item keeps the item that
followed it). The reporter's Codex rollout was
[text, call_A, text, call_B, out_A, out_B]; it reaches the upstream as
[text, text, call_A, call_B, out_A, out_B]. Reasoning alone between calls,
hosted calls and anything carrying a call_id stay where they were (#1752's
review note), and V2 (text after out_A, between whole exchanges) and V3
(calls together) go byte for byte.
Why: magpie relays deepseek-v4.1-flash at OpenCode Go to its /responses
(models.dev names no SDK for it, so every API may be tried, and #1822's
turn shows Go's DeepSeek answering there). Go builds a chat form in which
an assistant message after a call opens a new assistant message, so the
first one's tool_calls were followed by no tool message: 400 "No tool
output found for tool call call_aaaa1111" - the same text joinToolRuns'
comment already lists for Go. The translation to a Chat upstream was
already right on main (joinSplitCalls + mergeTurns join the step into one
assistant message); TestTextBetweenABatchsCallsStaysInItsStep now guards
it with the reporter's shape. Native ChatGPT sign-ins don't go through
joinToolRuns. Moved plugins get the relayed body after attempt, so they
get the change too; no plugin translator was touched.
Run:
- TestRelayedTextBetweenABatchsCallsIsAnswered (V1-V4, fake upstream that
checks the exchange as Go does) and the new TestJoinToolRunsOnlyMoves...
cases fail on origin/main 2fbbd1694 with the fix stashed: V1 and V4
"status 400: Fake: No tool output found for tool call call_aaaa1111",
and got [function_call:a function_call:b message: ...] want
[message: function_call:a ...]; pass with it.
- gofmt -l clean; go vet ./internal/gateway (darwin), GOOS=linux -tags
nogui, GOOS=windows: clean.
- go test -tags nogui ./internal/gateway/... under a scratch HOME with
pinned caches: ok (2011s at load ~230; a first run with the default
10m -timeout was killed by the timeout under that load, not a failure).
Not tried: the real OpenCode Go. The key on this machine answers "This Go
model requires Global regions" / "An active OpenCode Go subscription is
required" for every Go model, and Zen's free tier refuses non-OpenCode
clients, so the fix rests on the reporter's V1-V4 table and the fake.
What: follow-up to Edward4226's joinToolRuns (previous commit), after review
against Codex rust-v0.162.1 and the code around it.
- An item carrying a call_id is never moved, whatever its type. Before, only
function_call/custom_tool_call/tool_search_call counted as calls, so a
local_shell_call (or any call type Codex adds) inside a run was moved after
results, its own output's among them.
- Before the run's first result, what the model wrote between its own calls
(reasoning, assistant text, a hosted tool's call) stays where it is. Before,
[call_a, reasoning, call_b, out_a, out_b] sent the reasoning after the
results, where it reads as the next step's: DeepSeek, whose step must lead
with its reasoning (reasonFirst, f4cc7459), would get a step that isn't.
Anything else there (a subagent's agent_message, a note) still moves after
the run, so TestGrokGetsAgentMessageAsUser's expectation from the previous
commit stands.
- A call opens a run only when a result after it answers it. Before, a result
written before its call (an odd replay) left the run open for the rest of
the request, and every later message was moved to its end.
Where it happens, checked in Codex's source: prepare_response_items
(core/src/image_preparation.rs) pushes the developer <image_resize_notice>
right after each function_call_output / custom_tool_call_output whose image
it resized, so a batch of two image results has it between them, as #1822's
rollout shows (lines 347-349).
Routes: joinToolRuns sits in attempt before every destination, so the
Responses relay, plugins (relayed through passthrough) and translations all
get it. Translations rebuilt the order themselves for a developer or user
item (the developer one is lifted into the system prompt, user turns merge
and results go first), but an assistant message between two results closed
the chat exchange: pairToolMessages answered the second call with the
synthetic "unavailable" result and sent the real one as user text
(TestTranslatedToolRunKeepsItsRealResults, red without the call site).
Docs: gateway-routing.md says what attempt mends in a Responses request's
tool exchanges (joinToolRuns, orphanedToolOutputs, pairToolItems).
Run:
- TestRelayedCodexImageBatchKeepsItsResultsTogether (#1822's call ids, two
view_image results, the notice between; the fake answers DeepSeek's "No
tool output found for tool call …") and TestTranslatedToolRunKeepsItsRealResults
fail with the call site removed (400 / synthetic result) and pass with it.
TestJoinToolRunsOnlyMovesWhatBreaksARun fails on the previous commit's
joinToolRuns (reasoning moved, local_shell_call moved, a request rewritten
for a result before its call) and passes now.
- go test -tags nogui -count=1 ./internal/gateway/ under a sandbox HOME with
a private GOCACHE: ok (376s). gofmt -l: nothing. go vet (darwin),
GOOS=linux go vet -tags nogui, GOOS=windows go vet on ./internal/gateway/:
clean.
- Real DeepSeek (api.deepseek.com/v1/responses, deepseek-v4-flash), direct:
a developer note between the two results 400 "No tool output found for
tool call call_01_x2"; the same note after the results 200. Through a
sandbox magpie (own HOME, :3591, only the deepseek provider), streamed and
whole: origin/main's build 400 with that refusal, this build 200. The
<image_resize_notice> text itself between the results is answered 200 by
DeepSeek directly, before and after, with text results.
Not tried: OpenCode Go (this account's workspace isn't set to Global
regions, so every request there is refused before the model); the
reporter's image results and full-length history; Volcengine; a real Grok
subscription for the agent_message's new place.
Co-authored-by: Edward4226 <234070868+Edward4226@users.noreply.github.com>
What: the normalization a relayed Responses request goes through before it
leaves (gateway.go, attempt) gains joinToolRuns beside orphanedToolOutputs
and pairToolItems: an item that sits inside a run of tool calls and results
- between a call and the result that answers it - is moved to the end of
that run. A run is open while one of its calls has been seen and the result
answering it is still ahead, so an item between two whole exchanges stays
where it is. Codex writes such an item itself: when a tool returned an
image it resized, its <image_resize_notice> goes between one batch's two
results (the report from the Codex app on Windows, one assistant step with
view_image and exec_command).
Before, the run reached the upstream with the item inside it and a strict
one refused the whole request with 400: Volcengine's Agent Plan "An
assistant message with 'tool_calls' must be followed by tool messages
responding to each 'tool_call_id'", OpenCode Zen/Go "No tool output found
for tool call <id>" - both naming a result that is in the request. After,
the results follow the calls, the item goes after them, and both answer
200. This is the rule the chat path has had since #1275 (pairToolMessages
closes a run with its results, interrupted ones included, before another
message) and the same family as #1341 (pairToolItems, a call nothing
answers), and it is applied before the request is relayed or translated,
so every destination whose own form is strict gets it in the same change.
The item's own bytes, its role and its order among the other non-tool items
are as they were. A request with no tool items, one whose exchanges are each
whole, and a body that is not a Responses request come back byte for byte
(TestJoinToolRunsLeavesWholeExchangesAlone). One expectation changed on
purpose: TestGrokGetsAgentMessageAsUser's turn holds a subagent's reply
between the send_message call and its output, and Grok is now given that
reply after the run. Its conversion is unchanged (same user message, same
"From <author> to <recipient>" header, same text and images) and only its
place moved, the way every other item Grok is handed keeps the place magpie
gave it; a review that knows Grok's backend needs the reply in place can
narrow the rule to messages other than agent_message.
Run:
- go test -tags nogui ./internal/gateway/: 2 failures, both red on
origin/main 6c1c0a7 as well and neither from this change -
TestClaudeAsRootEnv (a Linux-only expectation with no Windows guard) and
TestSweepBridgeProjectsTakesOnlyTheBridgesFolders (needs a symlink
privilege this Windows box doesn't hold). Everything else passes,
TestQueuedStreamKeptAliveOnceSentToItsVendor among them (that one failed
once on main under load; it is the queued-stream timing in the standards).
- the same run under a temporary HOME with GOPATH/GOMODCACHE/GOCACHE pinned
from go env: the same two failures.
- gofmt -l on the four changed files: nothing. go vet ./..., GOOS=linux go
vet -tags nogui ./..., GOOS=windows go vet ./...: clean. go build,
GOOS=windows go build, GOOS=linux go build -tags nogui: ok.
- without the change: with the one call-site line taken out,
TestResponsesRelayKeepsAToolRunTogether prints
`status 400: ... "Fake: An assistant message with 'tool_calls' must be
followed by tool messages responding to each 'tool_call_id'."`; the line
back, it passes.
- real upstreams, the reporter's shape (two parallel calls, an image
result, Codex's <image_resize_notice>, the second result), the same build
with the line and without it, streamed the way Codex sends it:
Volcengine Agent Plan / deepseek-v4.1-flash - run whole 200/200; the
notice between the results 400 "insufficient tool messages following
tool_calls message" then 200; any other note there 400 then 200.
OpenCode Zen/Go / deepseek-v4.1-flash - run whole 200/200; a note between
the results 400 "No tool output found for tool call call_b" then 200; the
notice between them 200/200, their gateway recognizing that one item.
- the reporter's own rebuilt turn (1399 items, from their rollout) to
Volcengine: refused "insufficient tool messages following tool_calls
message" without the change, past that check with it (what is left is the
input's length, 1.7M tokens, over the model's limit).
Not tried: a real Grok subscription - the reply's new place is unverified
against xAI's backend, though its conversion is unchanged and its plugin
(community plugins packages/grok) converts in place the same way; a real
ChatGPT (native Codex account) destination, which takes standalone tool
outputs itself and is left as it came; the Windows GUI. The OpenCode Zen/Go
400 of the original report is not reproduced by the reporter's own bytes:
with both results and the notice between them that gateway answers 200
before and after this change, so the report's refusal may be the follow-up
body assembled before the batch's last output was in it
(openai/codex#44604).
Refs: #1341, #1275. Reported as anomalyco/opencode#54698 and
openai/codex#44604.