Repository navigation
fix(gateway): preserve every client tool call when agent calls multiple tools per turn (#52288) - #75580
Conversation
|
Codex review: needs maintainer review before merge. Summary Reproducibility: yes. Current main has a source-level reproduction through the overwritten runner slot and Next step before merge Security Review detailsBest possible solution: Land this PR after normal maintainer and CI gating so the shipped fix preserves every client tool call in source order and closes the linked bug with regression coverage. Do we have a high-confidence way to reproduce the issue? Yes. Current main has a source-level reproduction through the overwritten runner slot and Is this the best way to solve the issue? Yes. The patch is the narrow maintainable fix: carry an ordered array through the runner metadata, reserve slots before async hook latency can reorder callbacks, and emit every pending call without changing the public schema. What I checked:
Likely related people:
Codex review notes: model gpt-5.5, reasoning high; reviewed against f7549079ceb3. |
9c1a454 to
8a986e9
Compare
|
Thanks for the review. Two things addressed in the latest push: Changelog entry added — single-line CI failures — both pre-existing on my old base (
Branch now has two commits: the original fix + a single-line |
|
Update on the latest CI run after the rebase: my original two failures ( Root:
Other recently-opened PRs aren't tripping on this because they touch only path-filtered subsets (e.g. iMessage-only, plugin-sdk-only) that don't run I'm intentionally not adding the WhatsApp mirror/import fix to this PR — it's an unrelated change and would muddy the multi-tool-call repair. Happy to either:
|
8a986e9 to
4af9cca
Compare
When an agent invoked multiple client tools in a single turn, only the last call survived. The detection slot in the embedded runner was a single nullable variable that each ``onClientToolCall`` callback overwrote, and both the non-streaming and streaming OpenResponses handlers downstream read only ``pendingToolCalls[0]``. Multi-tool plans, graph orchestration calls, and similar batched flows quietly lost every call but the trailing one. Switch the detection variable to an ordered array, rename the runner result field from ``clientToolCall`` to ``clientToolCalls`` (still ``undefined`` when no calls happened so the existing truthiness predicates stay byte-identical), build ``pendingToolCalls`` by mapping over that array, and emit one ``function_call`` output item per pending call from both response modes. The streaming branch now walks an ``output_index`` counter forward instead of hard-coding ``output_index: 1`` so multiple ``output_item.added``/``done`` pairs share a single monotonic stream. Tests: add a runner orchestration test that pushes three tools through the embedded runner result and asserts ordered ``pendingToolCalls``, plus two gateway regression tests (JSON and SSE) covering the exact multi-tool repro from the issue. Closes openclaw#52288 Made-with: Cursor
Made-with: Cursor
4af9cca to
58d3c35
Compare
|
Landed as 2584d0d. Fixup added before landing: reserved client-tool-call slots before the async before-tool-call hook runs, so parallel tool execution cannot reorder the Verified:
|
Summary
Fixes #52288. When an agent invoked multiple client tools in a single turn through
/v1/responses, only the last call ever made it back to the caller. The bug had four layers and the fix walks all four:attempt.tsran everyonClientToolCallcallback through a single nullable variable. Each new call clobbered the previous one. Replaced with an array that pushes in arrival order.EmbeddedAttemptResult.clientToolCallis renamed toclientToolCalls(Array<{ name; params }>). The field staysundefinedwhen no client tool fired, so the existing truthiness predicates inincomplete-turn.tsandrun.tskeep their meaning without a.lengthcheck.run.tsnow buildspendingToolCallsby mapping over the array. Each call gets its own random id, same shape as before./v1/responseshandler iteratespendingToolCallsand emits onefunction_calloutput item per call. The streaming handler walks anoutput_indexcounter forward instead of hard-codingoutput_index: 1, so a turn with N tool calls produces Noutput_item.added/donepairs at indexes 1..N alongside the assistant message at index 0. The finalresponse.completedpayload includes every function_call item in arrival order.I had a draft fix sitting around for a related response-shape issue and was about to open a separate PR when I found this one. Decided this was the better thing to ship since real users (
@bonelliin the thread) reported still hitting it.Closes #52288.
Test plan
pnpm test src/gateway/openresponses-http.test.ts— all 17 tests pass; the two new ones cover three-tool turns in both JSON and SSE modes:pnpm test src/agents/pi-embedded-runner/sessions-yield.orchestration.test.ts src/agents/pi-embedded-runner/run.incomplete-turn.test.ts— 87 tests pass; new orchestration test assertspendingToolCallspreserves order across three calls.pnpm exec tsgo -p tsconfig.core.json— clean.pnpm exec oxlintandpnpm exec oxfmt --checkon all touched files — 0 warnings, 0 errors.Notes
undefined(not an empty array) when no calls were detected, soattempt.clientToolCalls ||predicates inincomplete-turn.tsstay one-token reads.output_indexcounter rather than recomputing per call, making the order/index relationship explicit.clientToolCall->clientToolCallsrename is internal to the embedded runner; everything passes throughpendingToolCallsafterrun.ts.