Repository navigation
fix: keep the reply of long tool-heavy Claude CLI turns - #200501
Conversation
…ly to the stream limits The Claude stream-json budgets charged frames the parser discards: every ordinary frame counted toward the 20,000-frame limit, and assistant snapshots, tool-result records, block starts, message starts and status records were charged their full envelope metadata against 8 MiB. A healthy turn of a few thousand small tool calls hit either limit and lost its reply. The budget now charges what the parser keeps (text, thinking and tool-input payloads, assistant content blocks, block headers, tool ids) plus a fixed 32-character allowance per discounted frame. The frame limit counts only frames that leave more than 1 KiB of their raw line uncharged, so it keeps bounding discarded parse work while small frames are bounded by the character budget alone.
|
🦞👀 Pull request received. I will update this pull request when review starts. |
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
Codex review: needs maintainer review before merge. What this changesThis PR discounts discarded Claude CLI framing while retaining character, individual-line, and large-discarded-frame limits. Example: A Claude CLI turn makes 3,600 sequential tool calls.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review Keep open: this PR addresses a current Claude CLI reply-loss bug with maintainer-backed direction; no actionable correctness or security defect was found. Priority: P1 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherClaude CLI stdout enters the shared JSONL parser, which accounts for output, projects tool and text events, and returns the completed reply to the agent runner. flowchart LR
A[Claude CLI stdout] --> B[Bounded line framing]
B --> C[Claude budget accounting]
C --> D[JSONL event projection]
D --> E[Tool callbacks]
D --> F[Completed agent reply]
Technical reviewBest possible solution: Merge the focused accounting repair once the existing required checks pass. Do we have a high-confidence way to reproduce the issue? Current-main source establishes the refusal and empty-reply path, and the author supplies repeated shipped-CLI base/head reproductions; this read-only review did not rerun them. Is this the best way to solve the issue? The patch repairs accounting in its existing owner, removes obsolete payload measurement, and preserves bounded parsing without adding configuration or terminal-recovery machinery. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 981ef4511485. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
PR surfaceSource +17, Tests -8, Docs 0. Total +9 across 7 files. Added test files: 0. View PR surface stats
Preflight sourceSource unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified. Review metrics
LabelsLabel changes:
Label justifications:
Rating scale6/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. WorkflowClawSweeper edits this one comment on every review. Comment Reviewed November 17, 2026, 3:36 AM ET / 08:36 UTC. |
|
Thanks to @Trinity-AI-Coder for the report. This is merged in 96ce195: the Claude CLI output limits no longer count stream frames and envelope data the parser discards, so long tool-heavy turns keep their final reply. |
Root cause: src/agents/cli-output-stream-limits.ts:232 (`chargeClaudeStreamJsonLine`) charged every Claude frame the parser discards: each ordinary frame counted toward the 20,000-frame limit, and assistant snapshots, tool-result records, block starts, message starts and status records were charged their whole envelope against the 8 MiB budget, so a healthy turn of a few thousand small tool calls ran out of budget it never retained. Fixes #186967 Related: #159929, #192735, #192734 ## What Problem This Solves A long Claude CLI turn that makes many tool calls (a subagent building a repo, a scheduled job editing files) finishes its work, but OpenClaw drops the final reply and reports `CLI JSONL output exceeded 20000 lines` or `CLI JSONL output exceeded 8388608 characters`. The work is on disk; the requester sees a failure. ## User Impact User impact: Claude CLI turns with thousands of tool calls keep their final reply. The limits still stop turns that retain more than 8 MiB of text, thinking or tool input, and turns that stream a large volume of discarded output. ## Why This Change Was Made The two budgets were meant to bound what the parser keeps, but they counted stream volume. Every tool call costs about five frames of envelope metadata (message ids, uuids, timestamps, usage, tool-result metadata) that the parser throws away, so the limits tripped after roughly 3,300 sequential or 4,900 parallel small tool calls. The budget now charges what the parser keeps (text, thinking and tool-input payloads, assistant content blocks, block headers, tool ids), plus a fixed 32-character allowance for each discounted frame, so 8 MiB still bounds the number of frames. The 20,000-frame limit now counts only frames that leave more than 1 KiB of their raw line uncharged (for example large tool results), which is the parse work the character budget cannot see. The old payload re-serialization for tool results is gone; tool results are charged by their ids. No limit value, config or error text changes. #187125 rewords the limit error; this PR removes the cause instead, so that wording is not needed. ## Evidence Real Claude Code 2.1.322 run by `openclaw agent --local` (built `dist/`, isolated state, `claude-cli/claude-sonnet-4-6`) against a local Anthropic Messages fixture that returns `mcp__openclaw__read` calls of a tiny file, then a final reply. Base `d979dba02457`, candidate `48f1eb236794`. Fixture, harness and captured stdout breakdown are kept with the proof. | Turn | Base | Candidate | | --- | --- | --- | | 3,600 sequential tool calls | fails after 3,338 calls: `CLI JSONL output exceeded 20000 lines` (twice), no reply | `FINAL_REPLY_OK after 3600 tool calls` (twice) | | 7,000 tool calls, 10 per message | fails after about 4,900 calls: `CLI JSONL output exceeded 8388608 characters` (twice), no reply | `FINAL_REPLY_OK after 7000 tool calls` (twice) | | Control: 10 MB of reply text | | still fails with `exceeded 8388608 characters` | Measured from the captured stdout of the same CLI, the budget charge per small tool call drops from about 1,640 characters to about 500, and none of those frames counts toward the frame limit. Tests: `cli-output-framing.test.ts` › "keeps the reply of a turn with thousands of small tool calls" fails on base (`CLI JSONL output exceeded 20000 lines`) and passes here. The control-frame, partial-message, subagent, framing, output-limit, stream and JSONL suites pass (173 tests). On #159929: it no longer happens on main. #166127 already discounts the `tool_use_result` file copies, and the claude-cli path now edits through OpenClaw's MCP `edit` tool, whose result records are about 650 characters with no file echo. With this PR a tool result costs its id plus 32 characters. Changes: production +74/-57 (net +17) in `cli-output-stream-limits.ts` and `cli-output-stream.ts`; tests +81/-89 (net -8); docs updated. Merge danger: two-way door, a revert restores the old accounting; no state or config is written. Blast radius: Claude CLI turns only. If the accounting were too loose, a misbehaving CLI could stream more discarded frames before the limits stop it (bounded by about 254,000 small frames per turn). Thanks @Terwox for the measured report in #159929, @Trinity-AI-Coder for #186967, and @slatem for the follow-up reports. Co-authored-by: Ayaan Zaidi <[email protected]> Stable adaptation: retain stable argument fixtures; include the missing bounded control-frame owner and its contracts. Allocate 294 net regression lines for long-turn completion and rejection controls. Numeric limits and provider routing unchanged. (cherry picked from commit 96ce195) Co-authored-by: RomneyDa <[email protected]>
) Root cause: src/agents/cli-output-stream-limits.ts:232 (`chargeClaudeStreamJsonLine`) charged every Claude frame the parser discards: each ordinary frame counted toward the 20,000-frame limit, and assistant snapshots, tool-result records, block starts, message starts and status records were charged their whole envelope against the 8 MiB budget, so a healthy turn of a few thousand small tool calls ran out of budget it never retained. Fixes openclaw#186967 Related: openclaw#159929, openclaw#192735, openclaw#192734 ## What Problem This Solves A long Claude CLI turn that makes many tool calls (a subagent building a repo, a scheduled job editing files) finishes its work, but OpenClaw drops the final reply and reports `CLI JSONL output exceeded 20000 lines` or `CLI JSONL output exceeded 8388608 characters`. The work is on disk; the requester sees a failure. ## User Impact User impact: Claude CLI turns with thousands of tool calls keep their final reply. The limits still stop turns that retain more than 8 MiB of text, thinking or tool input, and turns that stream a large volume of discarded output. ## Why This Change Was Made The two budgets were meant to bound what the parser keeps, but they counted stream volume. Every tool call costs about five frames of envelope metadata (message ids, uuids, timestamps, usage, tool-result metadata) that the parser throws away, so the limits tripped after roughly 3,300 sequential or 4,900 parallel small tool calls. The budget now charges what the parser keeps (text, thinking and tool-input payloads, assistant content blocks, block headers, tool ids), plus a fixed 32-character allowance for each discounted frame, so 8 MiB still bounds the number of frames. The 20,000-frame limit now counts only frames that leave more than 1 KiB of their raw line uncharged (for example large tool results), which is the parse work the character budget cannot see. The old payload re-serialization for tool results is gone; tool results are charged by their ids. No limit value, config or error text changes. openclaw#187125 rewords the limit error; this PR removes the cause instead, so that wording is not needed. ## Evidence Real Claude Code 2.1.322 run by `openclaw agent --local` (built `dist/`, isolated state, `claude-cli/claude-sonnet-4-6`) against a local Anthropic Messages fixture that returns `mcp__openclaw__read` calls of a tiny file, then a final reply. Base `f7230e5c0acb`, candidate `48f1eb236794`. Fixture, harness and captured stdout breakdown are kept with the proof. | Turn | Base | Candidate | | --- | --- | --- | | 3,600 sequential tool calls | fails after 3,338 calls: `CLI JSONL output exceeded 20000 lines` (twice), no reply | `FINAL_REPLY_OK after 3600 tool calls` (twice) | | 7,000 tool calls, 10 per message | fails after about 4,900 calls: `CLI JSONL output exceeded 8388608 characters` (twice), no reply | `FINAL_REPLY_OK after 7000 tool calls` (twice) | | Control: 10 MB of reply text | | still fails with `exceeded 8388608 characters` | Measured from the captured stdout of the same CLI, the budget charge per small tool call drops from about 1,640 characters to about 500, and none of those frames counts toward the frame limit. Tests: `cli-output-framing.test.ts` › "keeps the reply of a turn with thousands of small tool calls" fails on base (`CLI JSONL output exceeded 20000 lines`) and passes here. The control-frame, partial-message, subagent, framing, output-limit, stream and JSONL suites pass (173 tests). On openclaw#159929: it no longer happens on main. openclaw#166127 already discounts the `tool_use_result` file copies, and the claude-cli path now edits through OpenClaw's MCP `edit` tool, whose result records are about 650 characters with no file echo. With this PR a tool result costs its id plus 32 characters. Changes: production +74/-57 (net +17) in `cli-output-stream-limits.ts` and `cli-output-stream.ts`; tests +81/-89 (net -8); docs updated. Merge danger: two-way door, a revert restores the old accounting; no state or config is written. Blast radius: Claude CLI turns only. If the accounting were too loose, a misbehaving CLI could stream more discarded frames before the limits stop it (bounded by about 254,000 small frames per turn). Thanks @Terwox for the measured report in openclaw#159929, @Trinity-AI-Coder for openclaw#186967, and @slatem for the follow-up reports. Co-authored-by: Ayaan Zaidi <[email protected]>
Root cause: src/agents/cli-output-stream-limits.ts:232 (`chargeClaudeStreamJsonLine`) charged every Claude frame the parser discards: each ordinary frame counted toward the 20,000-frame limit, and assistant snapshots, tool-result records, block starts, message starts and status records were charged their whole envelope against the 8 MiB budget, so a healthy turn of a few thousand small tool calls ran out of budget it never retained. Fixes #186967 Related: #159929, #192735, #192734 ## What Problem This Solves A long Claude CLI turn that makes many tool calls (a subagent building a repo, a scheduled job editing files) finishes its work, but OpenClaw drops the final reply and reports `CLI JSONL output exceeded 20000 lines` or `CLI JSONL output exceeded 8388608 characters`. The work is on disk; the requester sees a failure. ## User Impact User impact: Claude CLI turns with thousands of tool calls keep their final reply. The limits still stop turns that retain more than 8 MiB of text, thinking or tool input, and turns that stream a large volume of discarded output. ## Why This Change Was Made The two budgets were meant to bound what the parser keeps, but they counted stream volume. Every tool call costs about five frames of envelope metadata (message ids, uuids, timestamps, usage, tool-result metadata) that the parser throws away, so the limits tripped after roughly 3,300 sequential or 4,900 parallel small tool calls. The budget now charges what the parser keeps (text, thinking and tool-input payloads, assistant content blocks, block headers, tool ids), plus a fixed 32-character allowance for each discounted frame, so 8 MiB still bounds the number of frames. The 20,000-frame limit now counts only frames that leave more than 1 KiB of their raw line uncharged (for example large tool results), which is the parse work the character budget cannot see. The old payload re-serialization for tool results is gone; tool results are charged by their ids. No limit value, config or error text changes. #187125 rewords the limit error; this PR removes the cause instead, so that wording is not needed. ## Evidence Real Claude Code 2.1.322 run by `openclaw agent --local` (built `dist/`, isolated state, `claude-cli/claude-sonnet-4-6`) against a local Anthropic Messages fixture that returns `mcp__openclaw__read` calls of a tiny file, then a final reply. Base `d979dba02457`, candidate `48f1eb236794`. Fixture, harness and captured stdout breakdown are kept with the proof. | Turn | Base | Candidate | | --- | --- | --- | | 3,600 sequential tool calls | fails after 3,338 calls: `CLI JSONL output exceeded 20000 lines` (twice), no reply | `FINAL_REPLY_OK after 3600 tool calls` (twice) | | 7,000 tool calls, 10 per message | fails after about 4,900 calls: `CLI JSONL output exceeded 8388608 characters` (twice), no reply | `FINAL_REPLY_OK after 7000 tool calls` (twice) | | Control: 10 MB of reply text | | still fails with `exceeded 8388608 characters` | Measured from the captured stdout of the same CLI, the budget charge per small tool call drops from about 1,640 characters to about 500, and none of those frames counts toward the frame limit. Tests: `cli-output-framing.test.ts` › "keeps the reply of a turn with thousands of small tool calls" fails on base (`CLI JSONL output exceeded 20000 lines`) and passes here. The control-frame, partial-message, subagent, framing, output-limit, stream and JSONL suites pass (173 tests). On #159929: it no longer happens on main. #166127 already discounts the `tool_use_result` file copies, and the claude-cli path now edits through OpenClaw's MCP `edit` tool, whose result records are about 650 characters with no file echo. With this PR a tool result costs its id plus 32 characters. Changes: production +74/-57 (net +17) in `cli-output-stream-limits.ts` and `cli-output-stream.ts`; tests +81/-89 (net -8); docs updated. Merge danger: two-way door, a revert restores the old accounting; no state or config is written. Blast radius: Claude CLI turns only. If the accounting were too loose, a misbehaving CLI could stream more discarded frames before the limits stop it (bounded by about 254,000 small frames per turn). Thanks @Terwox for the measured report in #159929, @Trinity-AI-Coder for #186967, and @slatem for the follow-up reports. Co-authored-by: Ayaan Zaidi <[email protected]> (cherry picked from commit 96ce195) Co-authored-by: RomneyDa <[email protected]> (cherry picked from commit 15c8c29d207f2bf9a278ffb46b0b4ce771b13209)
Root cause: src/agents/cli-output-stream-limits.ts:232 (
chargeClaudeStreamJsonLine) charged every Claude frame the parser discards: each ordinary frame counted toward the 20,000-frame limit, and assistant snapshots, tool-result records, block starts, message starts and status records were charged their whole envelope against the 8 MiB budget, so a healthy turn of a few thousand small tool calls ran out of budget it never retained.Fixes #186967
Related: #159929, #192735, #192734
What Problem This Solves
A long Claude CLI turn that makes many tool calls (a subagent building a repo, a scheduled job editing files) finishes its work, but OpenClaw drops the final reply and reports
CLI JSONL output exceeded 20000 linesorCLI JSONL output exceeded 8388608 characters. The work is on disk; the requester sees a failure.User Impact
User impact: Claude CLI turns with thousands of tool calls keep their final reply. The limits still stop turns that retain more than 8 MiB of text, thinking or tool input, and turns that stream a large volume of discarded output.
Why This Change Was Made
The two budgets were meant to bound what the parser keeps, but they counted stream volume. Every tool call costs about five frames of envelope metadata (message ids, uuids, timestamps, usage, tool-result metadata) that the parser throws away, so the limits tripped after roughly 3,300 sequential or 4,900 parallel small tool calls.
The budget now charges what the parser keeps (text, thinking and tool-input payloads, assistant content blocks, block headers, tool ids), plus a fixed 32-character allowance for each discounted frame, so 8 MiB still bounds the number of frames. The 20,000-frame limit now counts only frames that leave more than 1 KiB of their raw line uncharged (for example large tool results), which is the parse work the character budget cannot see. The old payload re-serialization for tool results is gone; tool results are charged by their ids.
No limit value, config or error text changes. #187125 rewords the limit error; this PR removes the cause instead, so that wording is not needed.
Evidence
Real Claude Code 2.1.322 run by
openclaw agent --local(builtdist/, isolated state,claude-cli/claude-sonnet-4-6) against a local Anthropic Messages fixture that returnsmcp__openclaw__readcalls of a tiny file, then a final reply. Based979dba02457, candidate48f1eb236794. Fixture, harness and captured stdout breakdown are kept with the proof.CLI JSONL output exceeded 20000 lines(twice), no replyFINAL_REPLY_OK after 3600 tool calls(twice)CLI JSONL output exceeded 8388608 characters(twice), no replyFINAL_REPLY_OK after 7000 tool calls(twice)exceeded 8388608 charactersMeasured from the captured stdout of the same CLI, the budget charge per small tool call drops from about 1,640 characters to about 500, and none of those frames counts toward the frame limit.
Tests:
cli-output-framing.test.ts› "keeps the reply of a turn with thousands of small tool calls" fails on base (CLI JSONL output exceeded 20000 lines) and passes here. The control-frame, partial-message, subagent, framing, output-limit, stream and JSONL suites pass (173 tests).On #159929: it no longer happens on main. #166127 already discounts the
tool_use_resultfile copies, and the claude-cli path now edits through OpenClaw's MCPedittool, whose result records are about 650 characters with no file echo. With this PR a tool result costs its id plus 32 characters.Changes: production +74/-57 (net +17) in
cli-output-stream-limits.tsandcli-output-stream.ts; tests +81/-89 (net -8); docs updated.Merge danger: two-way door, a revert restores the old accounting; no state or config is written. Blast radius: Claude CLI turns only. If the accounting were too loose, a misbehaving CLI could stream more discarded frames before the limits stop it (bounded by about 254,000 small frames per turn).
Thanks @Terwox for the measured report in #159929, @Trinity-AI-Coder for #186967, and @slatem for the follow-up reports.