Skip to content

fix(context): one line budget per tool result, shared by its text blocks - #359

Merged
yuanhao merged 3 commits into
mainfrom
fix/tool-result-line-budget
Oct 20, 2026
Merged

yuanhao merged 3 commits into
mainfrom
fix/tool-result-line-budget

Conversation

@yuanhao

@yuanhao yuanhao commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

Follow-up to #347's review (finding 5): since 0.25.9 sends every text block of a tool result, tool_output_max_lines — applied per block — let an N-block result (an MCP server's) reach the model as N × the budget.

Change (context::block_budgets, used by truncate_tool_output_keyed):

  • If the text blocks fit the budget together, nothing changes.
  • Otherwise blocks shorter than an even share stay whole and the long ones split what is left (remainder to the earliest), each cut head + tail with its own marker — so per-block stash keys (-b{i}) and the loop's stash code are unchanged.
  • A cut block keeps at least 5 lines (head, marker, tail): a result with more long blocks than the budget can mark goes over it by that much rather than losing lines without a marker.
  • One-block results are unchanged. Idempotent (a cut block is exactly its budget; the floor case reassigns the same budgets), so truncated history is never rewritten — prefix cache intact.

Tests (src/context.rs): 20 × 200-line blocks fit 200 lines with a marker each; blocks that fit together are untouched; a short block stays whole between long ones, an image keeps its place and the keyed marker names k-b3; ten long blocks under a 20-line budget each keep 5 lines and a marker; every case truncated twice is unchanged.

Docs: docs/concepts/context-management.md, CHANGELOG ## Unreleased.

🤖 Generated with Claude Code

https://claude.ai/code/session_01T7iq5hpndSiHQcnAsywKuG

yuanhao and others added 2 commits October 20, 2026 17:06
`tool_output_max_lines` was applied to each text block on its own, so
since #347 (every block sent) an N-block result reached the model as up
to N x the budget. Blocks now share it: short blocks stay whole, long ones
split the rest, each cut with its own marker and stash key; a cut block
keeps at least 5 lines so it is never cut without a marker. Idempotent.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01T7iq5hpndSiHQcnAsywKuG
…wording

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01T7iq5hpndSiHQcnAsywKuG
@yuanhao

yuanhao commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator Author

Review (code-reviewer, with a brute-force idempotence check over max_lines 0–12/20/25/MAX, 1–6 blocks, \r\n / trailing-newline / blank-line text, images between): no code issues; keyed and unkeyed passes mark the same blocks. Fixed in 133a8f1: the CHANGELOG now says multi-block results stored by earlier versions are re-cut once at their first level-1 compaction (one prefix-cache rewrite); floor wording limited to budgets of 5 or more; block_budgets doc says a cut block is at most its budget.

…t overshoot wording

- Tests: three long blocks share 200 as [67, 67, 66]; a block over the
  first even share that fits once a shorter one is placed stays whole;
  an override exempts a many-block result; sixty 4-line blocks stay
  whole; the loop's append path cuts MultimodalTool's two blocks to
  [10, 10].
- Docs: overshoot happens when the even share is under 5 lines (short
  blocks included), and below a budget of 5 nothing is shared; stale
  "truncated independently" wording updated.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01T7iq5hpndSiHQcnAsywKuG
@yuanhao

yuanhao commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator Author

Test and comment reviews addressed in 75b8e6b: tests for the remainder rule ([67, 67, 66]), freed slack ([10, 60, 80]), an override on a many-block result, short blocks past the floor, and line counts on the loop's append path ([10, 10]); docs now state the overshoot exactly (even share under 5 lines, short blocks included; no sharing below a budget of 5) and stale per-block wording is updated.

@yuanhao
yuanhao merged commit 4050b68 into main Oct 20, 2026
16 checks passed
@yuanhao
yuanhao deleted the fix/tool-result-line-budget branch October 20, 2026 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant