Skip to content

fix: stop a long Claude CLI turn from redoing its tool work on the fallback model - #200801

Merged
obviyus merged 4 commits into
openclaw:mainfrom
fede-kamel:fix/cli-output-budget-no-fallback
Nov 18, 2026
Merged

obviyus merged 4 commits into
openclaw:mainfrom
fede-kamel:fix/cli-output-budget-no-fallback

Conversation

@fede-kamel

@fede-kamel fede-kamel commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Root cause: src/agents/cli-runner.ts:410 (main 91614b5) handles a refused CLI stream (CLI JSONL output exceeded …) by keeping the first-turn session and rethrowing, but never applies the existing rule that a turn which already ran tools must not be replayed on another model (cli-runner.ts:466-472, from #162567). The executor also rethrew the refusal without the run's tool facts, so model fallback sent the same user message to the fallback model, which ran the completed tools again.

Fixes #200744

What Problem This Solves

A long Claude CLI turn can edit files and run commands, then produce more output than OpenClaw will parse. OpenClaw refuses that output and, when a fallback model is configured, hands the same request to the fallback model. That model starts from scratch and repeats the work on top of half-applied changes, while queued messages wait behind it. Thanks @jzmudzinski for the detailed report (a 54-minute turn replayed this way).

User Impact

User impact: when a claude-cli turn that already ran a tool hits the stream output limit, the run stops with the limit error instead of replaying the request on another model, so finished tool actions are not repeated. A turn that hits the limit before running any tool still falls back as before.

Why This Change Was Made

OpenClaw already refuses to fall back after tool effects when a CLI turn ends with an empty answer. The output-limit refusal never reached that check, because the refused output did not carry the run's tool summary. The executor now attaches the tool summary and accepted session spawns to the refused output, and the runner applies the same stop to both failures. The refused first turn's native session is still kept for the next message (#192838 unchanged).

The issue also suggests a new failover reason for output budgets. This keeps the existing reason and uses the existing fallback stop, so failover reasons and their consumers stay unchanged.

Fix by @fede-kamel (d2169a3), kept as is; on top: a merge of current main and a shorter regression test.

Evidence

Real Gateway, real Claude Code 2.1.322 as the claude-cli backend, built dist/, isolated state. Claude Code talks to a local Anthropic Messages fixture; the fallback stub/stub is a local OpenAI-compatible fixture that starts the request from scratch. The turn runs exec once (appends a line to a marker file) through the Gateway's MCP, then streams 10 MiB of reply text.

Build Result exec runs
main 91614b5, run 1 and 2 output refused, fallback stub/stub runs exec again and answers 2, 2
this PR, run 1 and 2 output refused, run fails with CLI JSONL output exceeded 8388608 characters, fallback never called 1, 1
this PR, no tool before the overflow output refused, fallback to stub/stub as before 0 from Claude Code

Regression test: execute.output-limit.test.ts › "stops model fallback when a turn that already ran a tool exceeds the output limit" fails on main (expected false to be true) and passes here; the existing first-turn refusal test now also checks that a refusal without tools records no stop.

Gateway and fixture logs: #200801 (comment)

Merge danger: two-way door. Reverting restores the old fallback behavior; nothing is written or migrated. Blast radius: claude-cli turns that hit the output limit after running a tool now end with the error instead of falling back.

A claude-cli turn whose stream exceeds the output limit is refused as a
format FailoverError. Model fallback then sent the same user message to
the next model, which repeated tool calls the refused turn had already
completed (openclaw#200744).

The refused output now carries the run's tool summary and accepted
session spawns, and the CLI runner applies the existing empty-response
rule to it: a turn that completed tool effects records a model fallback
stop. Overflows without tool calls keep falling back as before.
@clawsweeper

clawsweeper Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime agents Agent runtime and tooling size: S labels Nov 17, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. labels Nov 17, 2026
@clawsweeper

clawsweeper Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

Prevents model fallback after refused CLI output when the turn already called tools or accepted session spawns.

Example: Claude CLI executes a command that appends one line to a file, then streams 10 MiB of reply text.

  • Before: The output limit triggers fallback, which repeats the command and leaves two lines.
  • After: The run reports the output-limit error without invoking fallback, leaving one line.

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) A focused repair with production-entry-point fault proof, a reported red-on-base regression, preserved neighboring contracts, and no findings; current-head CI is still underway.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (logs): The supplied built-Gateway proof exercises real Claude Code and Gateway MCP with local provider fixtures: a command followed by 10 MiB of output executes twice on the base and once with the fix in two paired runs, while a no-tool overflow still falls back; the changed failure path remains unchanged at the reviewed head.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: A long CLI turn can change files or execute commands, overflow its output budget, and automatically repeat the task on a fallback model.
Reason: Preventing duplicate tool effects restores an established policy, and the collaborator's review and proof support this direction without introducing a new product contract.

Merge readiness

✅ Ready for maintainer review

This PR fixes a current replay bug through the existing fallback-stop mechanism; no actionable defects were found, and current-head CI remains pending.

Priority: P1
Reviewed head: 5d79cb6032b76de218d7e249a557a162de61a5ec

Before merge

None.

Findings

None.

Agent review details

How this fits together

The CLI executor collects stream and tool facts, the CLI runner settles the attempt, and the shared model-fallback loop consumes its terminal-stop marker.

flowchart TD
User[Agent request] --> CLI[Claude CLI execution]
CLI --> Facts[Stream parser and tool tracking]
Facts --> Refusal[Output refusal with tool facts]
Refusal --> Runner[CLI runner settlement]
Runner --> Stop[Stop after tool effects]
Runner --> Fallback[Fallback when no tool ran]
Loading

Technical review

Best possible solution:

Use finalized execution facts to apply the existing fallback stop while retaining native-session continuity and fallback for refusals before tools run.

Do we have a high-confidence way to reproduce the issue?

The supplied Gateway traces reproduce duplicate execution on the base, whose refusal path remains present on current main; this review inspected source and supplied results without executing target code.

Is this the best way to solve the issue?

The patch restores the existing no-replay invariant at the refused-output boundary without changing failure classification, parser budgets, or session-binding policy.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 2d52a33ef7ac.

Provenance checked

  • Fallback after incomplete CLI output keeps the original intent (4f0541e: Missing final output must not replay completed tool effects; the prior implementation applied that rule to returned empty output.)
  • Refused-stream partial output and first-turn continuity keeps the original intent (2fdabd6: Retain the refused first turn's native session through the existing guarded binding owner so subsequent attempts preserve continuity.)
  • CLI execution facade and fact ownership keeps the original intent (22226bf: Keep execution lifecycle orchestration in the facade while separate owners collect stream and tool facts.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

PR surface

Source +27, Tests +29, Docs +1. Total +57 across 5 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 3 34 7 +27
Tests 1 30 1 +29
Docs 1 1 0 +1
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 5 65 8 +57

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Root-cause cluster

Relationship: same_root_cause
Canonical: #200744
Summary: This PR addresses the canonical replay report; the merged accounting and continuity repairs solve adjacent problems.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Labels

Label changes:

No label changes.

Label justifications:

  • P1: The reported agent workflow repeats already executed commands and file edits after output overflow, risking conflicting changes and delaying queued messages.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

History

Review history (3 earlier review cycles)
  • reviewed 2026-11-17T21:20:08.596Z sha d2169a3 :: needs real behavior proof before merge. :: none
  • reviewed 2026-11-17T22:12:39.491Z sha d2169a3 :: blocked before merge. :: none
  • reviewed 2026-11-17T22:55:37.828Z sha c180c81 :: needs maintainer review before merge. :: none

Reviewed November 17, 2026, 6:57 PM ET / 23:57 UTC (Revision 4).

@fede-kamel

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: ⏳ waiting on maintainer Only explicit maintainer approvals or decisions remain; merge is still blocked. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels Nov 17, 2026
@obviyus

obviyus commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@obviyus

obviyus commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Gateway proof for #200744 (real Claude Code 2.1.322 as claude-cli, built dist, local fixtures; workdir paths shortened)

== l-base-1
root=<base> sha=91614b5c377897783779a55ad82f3e6cb594cb2a profile=p200744base mode=tool-then-flood config=<workdir>/state/openclaw.json state=<workdir>/state
agent exit=0
fixture requests:
{"at":"2026-11-17T22:43:34.153Z","api":"anthropic","model":"claude-opus-4-7","messages":1,"reply":"tool_use mcp__openclaw__exec {\"command\":\"echo side-effect >> <workdir>/side-effect.log\"}"}
{"at":"2026-11-17T22:43:34.310Z","api":"anthropic","model":"claude-opus-4-7","messages":3,"reply":"flood 10485760"}
{"at":"2026-11-17T22:43:35.824Z","api":"openai","model":"stub","messages":3,"reply":"tool_call exec echo side-effect >> <workdir>/side-effect.log"}
{"at":"2026-11-17T22:43:35.901Z","api":"openai","model":"stub","messages":5,"reply":"STUB_DONE"}
[agent/cli-backend] cli terminal failure: provider=claude-cli model=claude-opus-4-7 durationMs=4187 runId=a59ebddb-97b2-48ea-b14f-e0c7eb73b2f7 error=CLI JSONL output exceeded 8388608 characters; refusing to parse output.
side-effect executions: 2
workdir=/tmp/p200744-base-1.jE3D
== l-base-2
root=<base> sha=91614b5c377897783779a55ad82f3e6cb594cb2a profile=p200744base mode=tool-then-flood config=<workdir>/state/openclaw.json state=<workdir>/state
agent exit=0
fixture requests:
{"at":"2026-11-17T22:44:26.023Z","api":"anthropic","model":"claude-opus-4-7","messages":1,"reply":"tool_use mcp__openclaw__exec {\"command\":\"echo side-effect >> <workdir>/side-effect.log\"}"}
{"at":"2026-11-17T22:44:26.204Z","api":"anthropic","model":"claude-opus-4-7","messages":3,"reply":"flood 10485760"}
{"at":"2026-11-17T22:44:28.062Z","api":"openai","model":"stub","messages":3,"reply":"tool_call exec echo side-effect >> <workdir>/side-effect.log"}
{"at":"2026-11-17T22:44:28.155Z","api":"openai","model":"stub","messages":5,"reply":"STUB_DONE"}
[agent/cli-backend] cli terminal failure: provider=claude-cli model=claude-opus-4-7 durationMs=1801 runId=f6ae388a-3e5f-4036-99c6-0d0fd9bf011b error=CLI JSONL output exceeded 8388608 characters; refusing to parse output.
side-effect executions: 2
workdir=/tmp/p200744-base-2.Doh6
agent result: {"status":"ok","summary":"completed","model":"stub","text":"STUB_DONE"} err: 
== l-cand-1
root=<worktree> sha=ff0344f86881ae34f3ee5ff376b08b79f6bece32 profile=p200744cand mode=tool-then-flood config=<workdir>/state/openclaw.json state=<workdir>/state
agent exit=1
fixture requests:
{"at":"2026-11-17T22:44:05.720Z","api":"anthropic","model":"claude-opus-4-7","messages":1,"reply":"tool_use mcp__openclaw__exec {\"command\":\"echo side-effect >> <workdir>/side-effect.log\"}"}
{"at":"2026-11-17T22:44:05.882Z","api":"anthropic","model":"claude-opus-4-7","messages":3,"reply":"flood 10485760"}
[agent/cli-backend] cli terminal failure: provider=claude-cli model=claude-opus-4-7 durationMs=1877 runId=27b49768-7f3b-4745-bbf9-ecf8d9abdbf4 error=CLI JSONL output exceeded 8388608 characters; refusing to parse output.
side-effect executions: 1
workdir=/tmp/p200744-cand-1.LVBs
agent result: {"status":null,"summary":null,"model":null,"text":null} err: CLI JSONL output exceeded 8388608 characters; refusing to parse output.
== l-cand-2
root=<worktree> sha=ff0344f86881ae34f3ee5ff376b08b79f6bece32 profile=p200744cand mode=tool-then-flood config=<workdir>/state/openclaw.json state=<workdir>/state
agent exit=1
fixture requests:
{"at":"2026-11-17T22:44:47.153Z","api":"anthropic","model":"claude-opus-4-7","messages":1,"reply":"tool_use mcp__openclaw__exec {\"command\":\"echo side-effect >> <workdir>/side-effect.log\"}"}
{"at":"2026-11-17T22:44:47.346Z","api":"anthropic","model":"claude-opus-4-7","messages":3,"reply":"flood 10485760"}
[agent/cli-backend] cli terminal failure: provider=claude-cli model=claude-opus-4-7 durationMs=7451 runId=1f136178-a3ed-449c-9b92-8e5798749227 error=CLI JSONL output exceeded 8388608 characters; refusing to parse output.
side-effect executions: 1
workdir=/tmp/p200744-cand-2.U1WY
agent result: {"status":null,"summary":null,"model":null,"text":null} err: CLI JSONL output exceeded 8388608 characters; refusing to parse output.
== l-cand-control-flood
root=<worktree> sha=ff0344f86881ae34f3ee5ff376b08b79f6bece32 profile=p200744cand mode=flood config=<workdir>/state/openclaw.json state=<workdir>/state
agent exit=0
fixture requests:
{"at":"2026-11-17T22:45:59.811Z","api":"anthropic","model":"claude-opus-4-7","messages":1,"reply":"flood 10485760"}
{"at":"2026-11-17T22:46:01.983Z","api":"openai","model":"stub","messages":3,"reply":"tool_call exec echo side-effect >> <workdir>/side-effect.log"}
{"at":"2026-11-17T22:46:02.153Z","api":"openai","model":"stub","messages":5,"reply":"STUB_DONE"}
[agent/cli-backend] cli terminal failure: provider=claude-cli model=claude-opus-4-7 durationMs=2935 runId=4ec366d7-3b02-443e-9572-b7b249671cc3 error=CLI JSONL output exceeded 8388608 characters; refusing to parse output.
side-effect executions: 1
workdir=/tmp/p200744-cand-control-flood.mfHS
agent result: {"status":"ok","summary":"completed","model":"stub","text":"STUB_DONE"} err: 

 × |agents-support| src/agents/cli-runner/execute.output-limit.test.ts > CLI stream-json output limit > stops model fallback when a turn that already ran a tool exceeds the output limit 32ms
   → expected false to be true // Object.is equality
⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯
      Tests  1 failed | 3 passed (4)

@clawsweeper clawsweeper Bot added rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: ⏳ waiting on maintainer Only explicit maintainer approvals or decisions remain; merge is still blocked. labels Nov 17, 2026
@obviyus

obviyus commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Maintainer override: the only red on head c180c81 is checks-ui-e2e (1/2) in CI run 44193529616, cancelled at its job timeout on two attempts while the step "Install selected Control UI E2E WebKit" (playwright install --with-deps webkit, apt downloads) was still running: attempt 1 spent 22:53-23:13 UTC in that step with two mirror stalls of 219 s and 182 s, and attempt 3 spent 23:20-23:44 UTC in it before the cancel. No test failed in either attempt; every test that ran in attempt 1 passed. This PR only changes src/agents/cli-runner* and one docs line, which the Control UI E2E does not load. Exact-head proof: a built Gateway with real Claude Code shows the turn's tool ran twice on main and once with this fix (two runs each), plus a control where fallback still works when no tool ran; the regression test fails on main and passes here; exact-head ClawSweeper review is 5/6 with no findings.

@obviyus

obviyus commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@obviyus
obviyus merged commit fea4a9d into openclaw:main Nov 18, 2026
119 checks passed
@obviyus

obviyus commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Thanks @fede-kamel for the fix and @jzmudzinski for the report. This is merged in fea4a9d: when a Claude CLI turn goes over its output limit after a tool already ran, the turn now stops with the limit error instead of falling back to another model and running the tool work again.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 18, 2026
…llback model (openclaw#200801)

Root cause: `src/agents/cli-runner.ts:410` (main 5a5cb32) handles a refused CLI stream (`CLI JSONL output exceeded …`) by keeping the first-turn session and rethrowing, but never applies the existing rule that a turn which already ran tools must not be replayed on another model (`cli-runner.ts:466-472`, from openclaw#162567). The executor also rethrew the refusal without the run's tool facts, so model fallback sent the same user message to the fallback model, which ran the completed tools again.

Fixes openclaw#200744

## What Problem This Solves

A long Claude CLI turn can edit files and run commands, then produce more output than OpenClaw will parse. OpenClaw refuses that output and, when a fallback model is configured, hands the same request to the fallback model. That model starts from scratch and repeats the work on top of half-applied changes, while queued messages wait behind it. Thanks @jzmudzinski for the detailed report (a 54-minute turn replayed this way).

## User Impact

User impact: when a `claude-cli` turn that already ran a tool hits the stream output limit, the run stops with the limit error instead of replaying the request on another model, so finished tool actions are not repeated. A turn that hits the limit before running any tool still falls back as before.

## Why This Change Was Made

OpenClaw already refuses to fall back after tool effects when a CLI turn ends with an empty answer. The output-limit refusal never reached that check, because the refused output did not carry the run's tool summary. The executor now attaches the tool summary and accepted session spawns to the refused output, and the runner applies the same stop to both failures. The refused first turn's native session is still kept for the next message (openclaw#192838 unchanged).

The issue also suggests a new failover reason for output budgets. This keeps the existing reason and uses the existing fallback stop, so failover reasons and their consumers stay unchanged.

Fix by @fede-kamel (d2169a3), kept as is; on top: a merge of current `main` and a shorter regression test.

## Evidence

Real Gateway, real Claude Code 2.1.322 as the `claude-cli` backend, built `dist/`, isolated state. Claude Code talks to a local Anthropic Messages fixture; the fallback `stub/stub` is a local OpenAI-compatible fixture that starts the request from scratch. The turn runs `exec` once (appends a line to a marker file) through the Gateway's MCP, then streams 10 MiB of reply text.

| Build | Result | `exec` runs |
| --- | --- | --- |
| `main` 5a5cb32, run 1 and 2 | output refused, fallback `stub/stub` runs `exec` again and answers | **2**, **2** |
| this PR, run 1 and 2 | output refused, run fails with `CLI JSONL output exceeded 8388608 characters`, fallback never called | **1**, **1** |
| this PR, no tool before the overflow | output refused, fallback to `stub/stub` as before | 0 from Claude Code |

Regression test: `execute.output-limit.test.ts` › "stops model fallback when a turn that already ran a tool exceeds the output limit" fails on `main` (`expected false to be true`) and passes here; the existing first-turn refusal test now also checks that a refusal without tools records no stop.

Gateway and fixture logs: openclaw#200801 (comment)

Merge danger: two-way door. Reverting restores the old fallback behavior; nothing is written or migrated. Blast radius: `claude-cli` turns that hit the output limit after running a tool now end with the error instead of falling back.

Co-authored-by: Ayaan Zaidi <[email protected]>
RomneyDa added a commit that referenced this pull request Nov 18, 2026
…llback model (#200801)

Root cause: `src/agents/cli-runner.ts:410` (main 91614b5) handles a refused CLI stream (`CLI JSONL output exceeded …`) by keeping the first-turn session and rethrowing, but never applies the existing rule that a turn which already ran tools must not be replayed on another model (`cli-runner.ts:466-472`, from #162567). The executor also rethrew the refusal without the run's tool facts, so model fallback sent the same user message to the fallback model, which ran the completed tools again.

Fixes #200744

## What Problem This Solves

A long Claude CLI turn can edit files and run commands, then produce more output than OpenClaw will parse. OpenClaw refuses that output and, when a fallback model is configured, hands the same request to the fallback model. That model starts from scratch and repeats the work on top of half-applied changes, while queued messages wait behind it. Thanks @jzmudzinski for the detailed report (a 54-minute turn replayed this way).

## User Impact

User impact: when a `claude-cli` turn that already ran a tool hits the stream output limit, the run stops with the limit error instead of replaying the request on another model, so finished tool actions are not repeated. A turn that hits the limit before running any tool still falls back as before.

## Why This Change Was Made

OpenClaw already refuses to fall back after tool effects when a CLI turn ends with an empty answer. The output-limit refusal never reached that check, because the refused output did not carry the run's tool summary. The executor now attaches the tool summary and accepted session spawns to the refused output, and the runner applies the same stop to both failures. The refused first turn's native session is still kept for the next message (#192838 unchanged).

The issue also suggests a new failover reason for output budgets. This keeps the existing reason and uses the existing fallback stop, so failover reasons and their consumers stay unchanged.

Fix by @fede-kamel (d2169a3), kept as is; on top: a merge of current `main` and a shorter regression test.

## Evidence

Real Gateway, real Claude Code 2.1.322 as the `claude-cli` backend, built `dist/`, isolated state. Claude Code talks to a local Anthropic Messages fixture; the fallback `stub/stub` is a local OpenAI-compatible fixture that starts the request from scratch. The turn runs `exec` once (appends a line to a marker file) through the Gateway's MCP, then streams 10 MiB of reply text.

| Build | Result | `exec` runs |
| --- | --- | --- |
| `main` 91614b5, run 1 and 2 | output refused, fallback `stub/stub` runs `exec` again and answers | **2**, **2** |
| this PR, run 1 and 2 | output refused, run fails with `CLI JSONL output exceeded 8388608 characters`, fallback never called | **1**, **1** |
| this PR, no tool before the overflow | output refused, fallback to `stub/stub` as before | 0 from Claude Code |

Regression test: `execute.output-limit.test.ts` › "stops model fallback when a turn that already ran a tool exceeds the output limit" fails on `main` (`expected false to be true`) and passes here; the existing first-turn refusal test now also checks that a refusal without tools records no stop.

Gateway and fixture logs: #200801 (comment)

Merge danger: two-way door. Reverting restores the old fallback behavior; nothing is written or migrated. Blast radius: `claude-cli` turns that hit the output limit after running a tool now end with the error instead of falling back.

Release adaptation: the unchanged shared fallback-stop policy lives in the existing CLI exit-error owner instead of the already-full orchestrator, keeping the 700-line limit intact without exemptions. Both refusal and empty-answer callers use that single policy. Added a 29-line regression allocation. Backported to release/2026.11.6 from baseline 309067b under Dallin's explicit intake authorization.

Companion audit through 9e917f0: later same-path commit 2b7950c (#193550) adds a model-request hook and is not a prerequisite; no later #200801 mention. Existing refusal/session continuity, finalized tool tracking, accepted-spawn evidence, and fallback-stop consumer are present.

Co-authored-by: Ayaan Zaidi <[email protected]>
(cherry picked from commit fea4a9d)
Co-authored-by: RomneyDa <[email protected]>
Co-authored-by: vincentkoc <[email protected]>
@fede-kamel
fede-kamel deleted the fix/cli-output-budget-no-fallback branch November 18, 2026 09:50
RomneyDa pushed a commit that referenced this pull request Nov 19, 2026
…llback model (#200801)

Root cause: `src/agents/cli-runner.ts:410` (main 91614b5) handles a refused CLI stream (`CLI JSONL output exceeded …`) by keeping the first-turn session and rethrowing, but never applies the existing rule that a turn which already ran tools must not be replayed on another model (`cli-runner.ts:466-472`, from #162567). The executor also rethrew the refusal without the run's tool facts, so model fallback sent the same user message to the fallback model, which ran the completed tools again.

Fixes #200744

## What Problem This Solves

A long Claude CLI turn can edit files and run commands, then produce more output than OpenClaw will parse. OpenClaw refuses that output and, when a fallback model is configured, hands the same request to the fallback model. That model starts from scratch and repeats the work on top of half-applied changes, while queued messages wait behind it. Thanks @jzmudzinski for the detailed report (a 54-minute turn replayed this way).

## User Impact

User impact: when a `claude-cli` turn that already ran a tool hits the stream output limit, the run stops with the limit error instead of replaying the request on another model, so finished tool actions are not repeated. A turn that hits the limit before running any tool still falls back as before.

## Why This Change Was Made

OpenClaw already refuses to fall back after tool effects when a CLI turn ends with an empty answer. The output-limit refusal never reached that check, because the refused output did not carry the run's tool summary. The executor now attaches the tool summary and accepted session spawns to the refused output, and the runner applies the same stop to both failures. The refused first turn's native session is still kept for the next message (#192838 unchanged).

The issue also suggests a new failover reason for output budgets. This keeps the existing reason and uses the existing fallback stop, so failover reasons and their consumers stay unchanged.

Fix by @fede-kamel (d2169a3), kept as is; on top: a merge of current `main` and a shorter regression test.

## Evidence

Real Gateway, real Claude Code 2.1.322 as the `claude-cli` backend, built `dist/`, isolated state. Claude Code talks to a local Anthropic Messages fixture; the fallback `stub/stub` is a local OpenAI-compatible fixture that starts the request from scratch. The turn runs `exec` once (appends a line to a marker file) through the Gateway's MCP, then streams 10 MiB of reply text.

| Build | Result | `exec` runs |
| --- | --- | --- |
| `main` 91614b5, run 1 and 2 | output refused, fallback `stub/stub` runs `exec` again and answers | **2**, **2** |
| this PR, run 1 and 2 | output refused, run fails with `CLI JSONL output exceeded 8388608 characters`, fallback never called | **1**, **1** |
| this PR, no tool before the overflow | output refused, fallback to `stub/stub` as before | 0 from Claude Code |

Regression test: `execute.output-limit.test.ts` › "stops model fallback when a turn that already ran a tool exceeds the output limit" fails on `main` (`expected false to be true`) and passes here; the existing first-turn refusal test now also checks that a refusal without tools records no stop.

Gateway and fixture logs: #200801 (comment)

Merge danger: two-way door. Reverting restores the old fallback behavior; nothing is written or migrated. Blast radius: `claude-cli` turns that hit the output limit after running a tool now end with the error instead of falling back.

Co-authored-by: Ayaan Zaidi <[email protected]>
(cherry picked from commit fea4a9d)

Co-authored-by: RomneyDa <[email protected]>

Release adaptation: none; source change applies to the pinned beta baseline.

Follow-up disposition: dependency #192838 is included immediately before this commit to retain refused-stream ownership.

(cherry picked from commit 4ffdd547f489a727eb7ed66ca08a197a899f2eb1)
tideclaw-bot added a commit that referenced this pull request Nov 24, 2026
…llback model (#200801)

Baseline adaptation: Use accepted tool and spawn evidence already tracked by the .33 CLI runner; classify only the parser output-limit refusal.

(cherry picked from commit fea4a9d)

Co-authored-by: RomneyDa <[email protected]>
(cherry picked from commit 65bdcf1fa44c53cc77c9041d14983fa32631004a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling docs Improvements or additions to documentation gateway Gateway runtime P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

2 participants