Skip to content

perf(session): never shrink tool results the model has not seen - #420

Merged
mrnasil merged 1 commit into
mainfrom
perf/spill-unseen-results
Nov 7, 2026
Merged

mrnasil merged 1 commit into
mainfrom
perf/spill-unseen-results

Conversation

@mrnasil

@mrnasil mrnasil commented Nov 7, 2026

Copy link
Copy Markdown
Member

Third of the token-efficiency PRs (after #418, #419).

Problem

spill_old_tool_results, prune_old_tool_results and shake_old_tool_results keep "the last 4 tool messages" and shrink the rest. That window counts messages, not steps. If the model reads six files in parallel, the first two results are cut to a 400-char head plus a pointer before the request that should carry them. The model never sees those files, so it reads them again: an extra round trip plus a second copy of each file in context.

Change

All three passes now use one helper, shrinkable_tool_indices. It selects every tool message except the last four, and never one that comes after the latest assistant message. Those are the results answering that assistant's calls, and they go out with the next request. This also replaces three copies of the same index computation.

Tests

  • New: unseen_tool_batch_is_never_shrunk. One assistant step with six reads: spill, prune and shake all return 0 before the model replies. After the reply, the two oldest are spilled.
  • Existing session and lifecycle tests queued tool results with no assistant turn between them. A real transcript always has the model's reply after results it has seen, so those fixtures now add one. Their assertions about what shrinks are unchanged.
  • whycodes-session --lib 171 passed; whycodes-agent --lib 764 passed.
  • fmt, clippy (session, agent), ratchets ok. Local llvm-cov on session: every changed line is covered.
  • The docs/internals/compaction.md pass table is updated.

🤖 Generated with Claude Code

Spill, prune and shake kept "the last 4 tool messages" and shrank the
rest. That window counted messages, not steps, so when the model read six
files in parallel, the first two results were cut to a 400-char head with
a pointer before the request that should have carried them. The model
never saw those files and read them again: one wasted round trip plus a
second copy of each file.

The three passes now share `shrinkable_tool_indices`: all but the last
four, and never a result after the latest assistant message (the batch
that answers its calls and goes out with the next request). This also
replaces three copies of the same index computation.

Tests that queued tool results with no assistant turn between them now
add one, since a real transcript always has the model's reply after the
results it saw; `unseen_tool_batch_is_never_shrunk` covers the batch case.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mrnasil
mrnasil force-pushed the perf/spill-unseen-results branch from 1578d0a to 529dac0 Compare November 7, 2026 13:18
@mrnasil
mrnasil merged commit 387ba50 into main Nov 7, 2026
7 checks passed
@mrnasil
mrnasil deleted the perf/spill-unseen-results branch November 7, 2026 13:27
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