Skip to content

fix: Claude CLI turns hit the output limit after a few edits of large files - #159930

Closed
Terwox wants to merge 1 commit into
openclaw:mainfrom
Terwox:fix/claude-cli-edit-echo-budget
Closed

Terwox wants to merge 1 commit into
openclaw:mainfrom
Terwox:fix/claude-cli-edit-echo-budget

Conversation

@Terwox

@Terwox Terwox commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Closes #159929
Related: #150132, #153545, #159931, #119445

What Problem This Solves

Fixes: Claude CLI (claude-cli) turns fail with CLI JSONL output exceeded 8388608 characters; refusing to parse output. when the agent edits large files a handful of times in one turn.

User Impact

User impact: agents on the Claude CLI backend can edit and rewrite large files (hundreds of KB) repeatedly in one turn without spending the per-turn stream-json output budget on copies of those files.

Why This Change Was Made

For every Edit and Write, Claude Code's user record carries the model-facing tool_result message (a one-line "has been updated successfully" string) and a tool_use_result object meant for SDK consumers. For Edit and Write, that object holds:

  • originalFile: the whole file as it was before the edit.
  • structuredPatch: a diff that can span the whole file.
  • For Write, content: the new file.

OpenClaw never reads tool_use_result; the only reference is the existing normalizer that strips echoed image and PDF bytes (#119486, #128699). The stream-json parser still charged these copies in full against the 8 MiB turn budget. Editing an 800 KB file three times used roughly 2.4 MiB of budget on text the model never sees.

This change drops those three fields in normalizeClaudeCliStreamJsonRecord, in the same way that function already omits echoed binary data. Details:

  • What is dropped. tool_use_result.originalFile, .structuredPatch and .content.
  • When. Only when tool_use_result has a string filePath and an originalFile key; Edit, Write-update and Write-create all carry both. Other tool_use_result shapes keep full accounting. That includes Read, whose file.content also duplicates the tool_result; it is left for a separate change.
  • What is kept. filePath, oldString, newString, type, userModified and the model-facing message.content tool_result. The tool result OpenClaw emits is unchanged.
  • Per-line cap. The raw per-line cap still applies unchanged. frameBoundedCliJsonlChunk checks each raw line against 8 MiB before any parsing, so a single echo over 8 MiB still fails with CLI JSONL line exceeded …. Only the per-turn total stops counting the echoes.
  • No new setting. Configurability stays out, per refactor(config): config-surface reduction tranche 2 — purge tuning knobs behind built-in defaults #111382.

Overlap with #153545: none at the file level. #153545 changes partial-message delta accounting in cli-output-stream.ts and cli-output-stream-limits.ts. This PR touches only cli-output-echoed-binary.ts and adds tests in cli-output-framing.test.ts. The two fixes are complementary: #153545 makes streamed text cheaper, and this PR makes Edit and Write results cheaper. The residual Math.max(normalized.line.length, rawLine.length - omittedRawChars) accounting in the parser is unchanged.

Evidence

Real capture from Claude Code 2.1.281 (claude -p … --output-format stream-json --verbose --permission-mode acceptEdits, working on a 109,890-character file). The numbers are characters charged by the parser before and after this change:

Record Raw line Charged before Charged after Model-facing tool_result
Edit 113,870 113,870 617 147 bytes
Write (update, 6-byte content) 232,433 232,433 565 —
Whole 55-line session 761,870 761,870 416,743 —

On a Write update, structuredPatch held one hunk covering all 3,001 old lines, so a Write costs about twice the file size. The rest of the post-fix session total is Read results and ordinary output.

Production failures. OpenClaw 2026.9.5, claude-cli backend, Opus, WSL2, overnight scheduled turns:

  • Sep 26: made three edits each to an 831 KB and a 761 KB JavaScript file.
  • Sep 27: made seven edits to a 599 KB test file.

By the per-record sizes above, those edits alone charged about 4.8 MB and 4.2 MB of the 8 MiB budget. The rest of the budget went to --include-partial-messages deltas, which #153545 addresses: about 6 MB and 11k lines by minute 130 in the Sep 27 turn.

Both turns then stopped showing progress, and stuck-session recovery ended them about 15 minutes later (after 45.8 and 147.8 minutes) as CLI run aborted, without logging the limit error. That silent stall is the failure mode in #143396, which #159931 addresses.

Tests (src/agents/cli-output-framing.test.ts, three new cases):

  • 20 alternating Edit and Write results on a 560,000-character file (far over 8 MiB raw in total) parse without error and still emit the short tool results. This case fails on main with CLI JSONL output exceeded 8388608 characters.
  • tool_use_result payloads that are not file echoes (a filePath with no originalFile) are still charged and still trip the budget.
  • A single 8 MiB originalFile echo still trips the raw per-line cap.

Commands run:

  • pnpm test src/agents/cli-output-framing.test.ts --maxWorkers=1: 24/24 pass in about 2.3 s wall time. The new cases take 122, 7 and 13 ms, so CI cost is well under 1 s in the existing shard.
  • node scripts/check-changed.mjs (core and core-test lanes) passed: format, the ratchets, tsgo:core, the changed core-test typecheck shards, oxlint on the changed files, and the repo guards.
  • The knip dead-export scan did not finish within the local 25-minute timeout. This change adds no exports.

Not captured: MultiEdit and NotebookEdit results. If their tool_use_result has no originalFile, it keeps full accounting, which is today's behavior, so there is no regression either way.

@clawsweeper

clawsweeper Bot commented Sep 27, 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 agents Agent runtime and tooling size: S labels Sep 27, 2026
@clawsweeper clawsweeper Bot added P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Sep 27, 2026
@clawsweeper

clawsweeper Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

The Claude CLI output parser excludes duplicated Edit and Write file contents from its turn budget so repeated large-file edits can continue.

Example: Claude edits a 109,890-character file.

  • Before: The captured Edit record consumes 113,870 characters of the turn budget.
  • After: The same record consumes 617 characters while preserving the short tool result.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The focused patch appears correct and useful, with sufficient capture-based accounting evidence but limited proof through the shipped OpenClaw entry point.
Proof confidence 🦐 gold shrimp (3/6) Sufficient (live_output): The contributor's real Claude Code 2.1.281 Edit/Write capture was measured through the changed accounting boundary, reducing charges to 617 and 565 characters while preserving model-facing results. This proves the targeted accounting improvement; cumulative-limit coverage is supplemental parser testing rather than a complete OpenClaw turn.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Claude CLI turns stop progressing or lose their final reply after repeatedly editing large files.
Reason: The paired report establishes that unused SDK file copies consume the output budget. Repairing that accounting is valuable and follows the existing normalization design without adding settings or public APIs.

Merge readiness

✅ Ready for maintainer review

This PR remains useful: current main still charges unused file echoes, and the merged partial-message fix does not address them. No actionable patch defect was found.

Priority: P1
Reviewed head: f5056d3e23b0746672c91c85d2abe12e50b8ab2e

Before merge

None.

Findings

None.

Tests

  • Missing end-to-end proof: The supplied evidence does not show a registered OpenClaw Claude CLI turn completing repeated large-file edits and delivering its final reply after exceeding 8 MiB of raw output.
Agent review details

How this fits together

OpenClaw receives newline-separated JSON records from Claude Code and converts them into tool progress and agent replies. Its parser bounds individual records and cumulative turn output before delivering those events.

flowchart LR
  A[Claude Code output] --> B[Raw line limit]
  B --> C[Remove duplicated file contents]
  C --> D[Turn output budget]
  D --> E[Tool progress and replies]
  B --> F[Output limit error]
  D --> F
Loading

Technical review

Best possible solution:

Keep the existing parser-owned normalization boundary and exclude unused file echoes while preserving tool delivery and raw line limits.

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

Yes, source establishes a focused reproduction: repeated Edit/Write records carrying whole-file echoes exhaust main's 8 MiB cumulative budget. The contributor reports the added parser regression failing on main; this review did not execute it.

Is this the best way to solve the issue?

Yes. Extending the existing normalization owner removes unused duplicate contents without increasing global limits or changing the delivered tool result.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 4ad94b56c5b1.

Provenance checked

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

  • Introduced change: The pinned delta selectively removes originalFile, structuredPatch, and content from user records whose SDK result has filePath and originalFile; message content remains intact. (src/agents/cli-output-echoed-binary.ts:10, f5056d3e23b0)
  • Current main still needs this repair: Fetched main normalizes binary echoes but has no Edit/Write file-echo omission. Neither touched file changed between the merge base and fetched main. (src/agents/cli-output-echoed-binary.ts:5, 4ad94b56c5b1)
  • Accounting and delivery boundary: The parser charges the larger of normalized length and raw length minus omitted contents. Raw framing independently enforces the 8 MiB line limit, and tool-result dispatch reads message.content rather than tool_use_result. (src/agents/cli-output-stream.ts:636, f5056d3e23b0)
  • Real Claude capture: The fully supplied PR body, captured with sourceRevision f618268a1d204c9e219b52381ec98f74038770c233161f8b92bd4375f9d05476, reports Claude Code 2.1.281 editing a 109,890-character file. Measured parser charges fall from 113,870 to 617 for Edit and from 232,433 to 565 for Write. The complete captured session is below the cumulative limit; synthetic parser regressions cover crossing it.
  • Focused regression coverage: Three added parser cases cover 20 alternating large Edit/Write results with delivered tool results, continued charging of non-file echoes, and rejection of an oversized raw file-echo line. The contributor reports the repeated-edit case failing on main and the focused suite passing after the change; this read-only review did not execute tests. (src/agents/cli-output-framing.test.ts:361, f5056d3e23b0)
  • Original normalization intent: fix(cli): normalize echoed binary tool outputs #128699 established one stack-safe normalization boundary for duplicated binary payloads while preserving projected tool metadata. Its discussion documents the parser replay and subsequent maintainer repair. (src/agents/cli-output-echoed-binary.ts:38, 636b00cb0467)

Likely related people:

  • Vladislav Smolensky: Raw commit 636b00c adds src/agents/cli-output-echoed-binary.ts:5 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 636b00cb0467; files: src/agents/cli-output-echoed-binary.ts)

PR surface

Source +34, Tests +110. Total +144 across 2 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 37 3 +34
Tests 1 110 0 +110
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 147 3 +144

Review metrics

Metric Value Why it matters
Production and test growth production +34 net lines; tests +110 lines Production growth is justified by extending the existing normalization owner, with focused coverage for delivery and accounting limits.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #159929
Summary: This PR directly addresses the paired file-echo accounting report; broader output accounting and failure handling have separate work.

Members:

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

Labels

Label changes:

  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • remove rating: 🐚 platinum hermit: Current PR rating is rating: 🦐 gold shrimp, so this older rating label is no longer current.

Label justifications:

  • P1: The reported accounting failure interrupts real Claude CLI editing turns and can prevent delivery of their final reply.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • 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 need explicit maintainer commands such as @clawsweeper autofix or @clawsweeper automerge.

History

Review history (1 earlier review cycle)
  • reviewed 2026-09-27T22:35:06.288Z sha f85b75d2b2807468df48636b209878da5aab745b :: needs maintainer review before merge. :: none

Reviewed October 9, 2026, 4:24 AM ET / 08:24 UTC (Revision 2).

…CLI output budget

Claude Code's Edit and Write tool results carry the whole pre-edit file
(tool_use_result.originalFile), a structuredPatch that can span all of it,
and Write's new content. The model only sees the short message tool_result
and nothing in OpenClaw reads these echoes, but the stream-json parser
charged them in full, so a few edits of a large file exhausted the 8 MiB
per-turn budget. Drop them in the Claude record normalizer, like echoed
image bytes. The raw per-line cap still applies before normalization.
@Terwox
Terwox force-pushed the fix/claude-cli-edit-echo-budget branch from f85b75d to f5056d3 Compare October 9, 2026 08:14
@clawsweeper clawsweeper Bot added rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. labels Oct 9, 2026
@roboclaw-bot

Copy link
Copy Markdown
Collaborator

Thanks @Terwox for the precise diagnosis and the captured Claude Code shapes. Current main already fixes this budget problem: #166127 (8828c59, "keep Claude CLI replies after many large tool results") changed chargeClaudeStreamJsonLine in src/agents/cli-output-stream-limits.ts so that user tool-result records are charged without their payloads: both the tool_result content and the whole tool_use_result (including Edit/Write originalFile, structuredPatch and content echoes) are discounted, while IDs, framing and the 8 MiB limit stay in place. Repeated large-file edits therefore no longer exhaust the stream-json budget, so I'm closing this as fixed on main. If you still see the 8388608-character refusal on a release that contains #166127, a fresh issue with the Claude Code version and the record shape would be very welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: claude-cli: Edit/Write tool_use_result file echoes are charged against the 8 MiB stream-json output budget

2 participants