Repository navigation
fix(agents): turns fail when a provider sends a malformed tool call after earlier tools ran - #164274
Conversation
…fter earlier tools ran When a provider streams a tool call whose arguments are incomplete or not valid JSON, the transport removes that call before dispatch and ends the turn with stopReason=error. The existing silent-error retry resubmits the attempt, so it only applies while the attempt is replay-safe. Once an earlier tool in the same turn has completed, the turn surfaced "couldn't generate a response" even though the rejected call never ran. Continue from the current transcript instead of replaying: when the rejected turn produced no visible text, every started tool settled, and no async work, tool failure, client tool call or delivered reply is pending, ask the model to re-issue the call with valid arguments or answer, without repeating completed tool calls. The continuation uses the existing empty-response retry budget. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. Reviewed October 4, 2026, 1:47 AM ET / 05:47 UTC (Revision 3). ClawSweeper reviewWhat this changesThe agent runner adds one continuation after a malformed tool call is rejected following completed tools, with regression coverage for recovery and its safety guards. Merge readiness✅ Ready for maintainer review Keep open: current main still lacks this recovery path, and the PR provides useful, bounded behavior with convincing production-boundary proof. No actionable patch defect remains. Likely related people: namest504, obviyus, and Peter Steinberger are routing candidates based on recent recovery work. Priority: P2 Review scores
Verification
How this fits togetherThe embedded agent runner turns provider responses into tool execution and user replies. Its terminal recovery owner decides whether to continue from the existing transcript or surface an error. flowchart TD
A[Provider response] --> B[Transport validates tool calls]
B --> C[Malformed call rejected]
C --> D[Runner checks completed tools and pending work]
D --> E[One transcript continuation]
E --> A
D --> F[Existing terminal error]
B --> G[Valid tool execution and reply]
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +29, Tests +164. Total +193 across 2 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep malformed-call recovery in the existing terminal owner, continuing from settled transcript state while preserving replay fences and retry budgets. Do we have a high-confidence way to reproduce the issue? Yes: source establishes the side-effect retry veto, and recorded installed-Gateway traces reproduce rejection after one completed tool. This read-only review did not execute target code. Is this the best way to solve the issue? Yes: the patch extends the existing continuation owner without replaying the original attempt or weakening tool-call validation, and the no-prior control preserves existing behavior. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against dac9277a4126. LabelsLabel changes: No label changes. Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
History |
|
@clawsweeper re-review Head 3c5c377 preserves the contributor commits and narrows the continuation to prior completed-tool evidence. Current-main Gateway negative control, candidate recovery/exactly-once effects, persistent bound, and no-prior 4-request parity are observed; the new guard regression fails on the contributor head and all 9 cases pass after it. PR body now records focused wall time, CI seconds, the exact continuation bytes, live smoke, and the non-human trailer being dropped on squash without rewriting contributor history. |
|
🦞👀 Re-review progress:
|
|
@clawsweeper re-review Head 97a80aa integrates current main for the named terminal test-contract CI failure, migrates the fixture to sessionPromptState, and documents auth-profile mock isolation. All three real Gateway scenarios pass again (recovery 4 requests; persistent bound 6; no-prior parity 4), 9 focused tests pass in 33.589 s, and the agent test-types project passes. Exact-head gpt-5-mini passive-proxy live smoke passes with low reasoning, 4096 output budget and the requested final marker. Production recovery logic is unchanged from the previously ready review. |
|
🦞👀 Re-review progress:
|
|
Maintainer override for the one red check, |
|
Landed as c5fcae2. Thanks @fransqaas! We narrowed the continuation to turns where an earlier tool had already completed. |
…fter earlier tools ran (openclaw#164274) A turn ended with an error when the provider streamed a malformed or incomplete tool call after another tool in the same turn had already run. The incomplete-turn recovery now grants one bounded continuation, so the model can re-issue the call, but only when earlier tools completed. Completed tools are never repeated, persistently malformed calls still end with the existing error, and turns with no prior tool are unchanged. The only new model-facing text is the 390-byte continuation message. Proof: on a real Gateway with a scripted stub, base failed after 2 requests, and this change recovered after 4 with the first side-effecting tool run exactly once. The persistent-malformed control ends after 6 requests with the existing error, and the no-prior-tool control matches main at 4 requests. A live gpt-5-mini smoke call passed. Related: openclaw#142176 openclaw#156342 openclaw#157469.
|
Thanks for this fix (c5fcae2). We hit exactly this on 2026.9.9: a malformed tool call after earlier exec calls ended a long agent turn. As of 10 Oct, v2026.10.1-beta.2 does not contain the commit ( |
|
Thanks @r3n3x, confirmed: the 2026.10.1 betas were cut from main on 2026-10-03, and this landed on 2026-10-04, so neither |
|
Follow-up from the same setup: the continuation added here is still skipped when an earlier tool in the same run failed. |
Related: #142176 (replay-safe silent retry for the same rejection), #156342 (continuation for a tool-use stop without a call), #157469 (persistent malformed tool calls from another model)
What Problem This Solves
Fixes: a turn ends with an error when the provider streams a malformed or incomplete tool call after another tool in the same turn has already run.
User Impact
User impact: when a model occasionally corrupts a tool call's arguments mid-turn (seen with DeepSeek V4 drifting into its native DSML markup on a second tool-call round), the agent now gets one chance to re-issue the call instead of the user receiving "couldn't finish this reply". Completed tool calls are never repeated. A model that keeps sending malformed calls still ends with the existing error after bounded retries.
Why This Change Was Made
When terminal tool-call arguments are not valid JSON, the transport removes the call before dispatch and ends the turn with
stopReason=error. #142176 retries that case by resubmitting the attempt, which is only safe while the attempt has no side effects. Once an earlier tool in the same turn has completed, the attempt is not replay-safe, so the turn fails even though the rejected call never ran.This adds a continuation, not a replay, for exactly that case: the rejected turn produced no visible text, every started tool settled, and there is no async work, tool failure, client tool call, spawn or delivered reply. The model is asked to re-issue the call with complete, valid JSON arguments, or answer, without repeating completed tool calls. It uses the existing empty-response continuation budget (one attempt) and log line, like the tool-use-without-call continuation in #156342. Error turns are never treated as silent replies, so the check reads model output directly rather than the empty-response classification.
Evidence
Contributor implementation:
eefde2c625bon main641f774c407. Maintainer follow-up:97a80aa8ad0da5e6a7d5385386a6bcd884d3ee9dpreserves those commits and limits the new continuation to turns with at least one completed tool.src/agents/embedded-agent-runner/run/terminal-resolution.rejected-tool-call.test.ts(8 cases): a rejected call after a settled side-effecting tool continues once and then surfacesincomplete_turnwhen the budget is spent; the malformed-arguments error code is covered; no continuation while a tool is still running, after a tool failure, after async work, for an unrelated error, when the model already wrote visible text, or when the assistant still carries a tool call. On unpatchedmainthe two continuation cases fail; with this change all 8 pass.tsgo(core) passed;oxlintandoxfmtare clean on the touched files.Real behavior proof
Behavior or issue addressed: after an earlier tool call completed in the same turn, a streamed tool call with malformed arguments is rejected before dispatch and the turn fails with "Agent run failed" / "couldn't finish this reply", although the rejected call never ran. First observed in production on 2026.9.3 with
deepseek-v4-flashoveropenai-completions: anexeccall completed, then the next call's arguments drifted into DSML markup (..."cover">false</||DSML||:,finish_reason=tool_calls, HTTP 200).Real environment tested:
[email protected](c074824a27c9, includes fix(agents): retry silent malformed tool rejections #142176), installed with npm.eefde2c625bc(tarball integrity check passed), installed with npm over the baseline;build-info.jsonreports the same commit.openclaw gateway run --bind loopback, a deterministic loopback Chat Completions streaming server as the model (api: openai-completions), and a local plugin with two tools:proof_prepare(side effect: appends to a ledger) andproof_publish.Exact steps or command run after this patch: for each run the driver started the Gateway and the fake model, sent
chat.send("Prepare the weekly briefing, then publish it in Spanish."), awaitedagent.wait, and readchat.history, the tool ledger and the model request log. The fake model callsproof_prepare, then streamsproof_publishwith the DSML-corrupted arguments; if the request carries the host's continuation instruction it re-issues the call with valid arguments (recovers) or corrupts it again (persistent).Evidence after fix: harness, model request logs, tool ledgers, Gateway signals and histories
Observed result after fix: the rejected call is re-issued once from the current transcript, the publish tool runs with valid arguments and the turn completes. The side-effecting
proof_prepareran exactly once in every run, so nothing completed was replayed. When the model keeps corrupting the call, the continuation is followed by the existing bounded resubmits of #142176 (the continuation attempt itself ran no tools) and the error is surfaced as before.What was not tested:
Maintainer verification
A current-main isolated Gateway reproduction uses a local scripted OpenAI-compatible streaming endpoint (no paid DeepSeek calls). The first call uses
execto append to a side-effect file, the second has truncated JSON arguments, and the recovery response re-issues only the rejected call.43295e864c3RECOVERED_164274; first append once and re-issued append onceThe original contributor head made 5 requests in the no-prior control. The follow-up adds the completed-tool condition and a regression at the terminal owner; the regression fails on the contributor head and the Gateway fixture independently checks the 4-request boundary. The original recovery tests fail on current main (2 failures, 6 passes).
Safety: no completed tool was repeated. The continuation remains limited to one per turn. The only added model-facing prompt text is the following 390 UTF-8 bytes (no newline; SHA-256
925628c4e067b0310718f3dc83cf18e1f3ede8e8e1a1480f4e4966728e6cddf4):Live smoke on
97a80aa8ad0: one realgpt-5-minirequest through a passive proxy, reasoninglow, output budget 4096, HTTP 200, terminalstop, Gateway sessiondone, exact responseLIVE_GATE_OK_164274. The fresh isolated workspace skips bootstrap-file creation.Maintainer intent: checked Sarah's #149642 and Pash's
a2bbcbf40684; authoredNO_REPLY, completed-reaction silence, connected apps and quiet Slack previews remain unchanged. The new condition only gates pre-dispatch error turns, preserving the existing async/unsettled-work checks.Commit hygiene: the contributor's original commit contains a non-human co-author trailer. Contributor history is preserved; that trailer will be dropped from the rewritten squash message and will not land. Credit remains with fransqaas.
Focused cost:
pnpm test src/agents/embedded-agent-runner/run/terminal-resolution.rejected-tool-call.test.ts --maxWorkers=1passed all 9 cases on97a80aa8ad0in 33.589 s wall time (isolated Linux/Node 24.16.0; pre-integration follow-up: 19.381 s; original 8-case contributor suite: 27.860 s cold). On the contributor head, CI job checks-node-changed-compact-large-23 took 110 CI seconds, including the broader selected shard; the 8 individual regression cases totaled 0.059 s in its log. The integrated agent test-types project also passed.Current-main integration: merged
86e75bc60d2to repair the named CI test-contract failure after terminal prompt callbacks moved undersessionPromptState; the regression now uses that owner API. The auth-profile mock documents its persistent-state isolation. No retry jobs or green-seeking pushes were used.Final-head CI: run 37180724724 completed. The focused regression shard passed all 9 cases (individual case durations total 0.046 s; containing job 127 CI seconds). All PR-owned failures from the earlier run are fixed.
The only remaining failing work job is check-test-types-core-2:
ui/src/components/mcp-app-bridge.cursor.test.ts(13,57)reports TS2559 becausetrueis passed where MCP feature options are expected. The same exact diagnostic occurs locally on this head and with the complete PR diff reverted to main86e75bc60d2(recovery owner restored, added test removed), usingnode scripts/run-tsgo.mjs -p test/tsconfig/tsconfig.core.test.ui-components.json --noEmit; both exit 2. UI AppBridge capability types do not consume embedded terminal recovery. The aggregateopenclaw/ci-gateis therefore red. No selected CI jobs were cancelled; five superseded auxiliary automation checks remain cancelled in the PR rollup. No CI jobs were rerun.ClawSweeper revision 3 is ready on exact head
97a80aa8ad0da5e6a7d5385386a6bcd884d3ee9d, with no remaining actionable findings.