Repository navigation
perf(session): shrink old tool results in batches, not every step - #431
Merged
Merged
Conversation
Spill and prune rewrite messages inside the provider's cached prefix. They ran before every step and usually shrank a single result, so a long session missed the prompt cache on most steps. On a captured six-turn session (25 requests) that cost more, with a cache, than never shrinking at all. They now run only when the old results they would shrink can free at least SHRINK_BATCH_CHARS (16 KiB, `Session::old_tool_results_due`); the same gate applies to subagents. Shake under context pressure and the explicit /compact path are unchanged. The threshold was picked by replaying the captured requests at 4-64 KiB, then checked on a release build. Same session, against v0.6.34: cache misses 8 -> 2 uncached 98,900 -> 62,463 (-37%) cache cost 172,354 -> 132,916 (-23%; read 0.1x, write 1.25x) total input 586,193 -> 610,840 (+4%) last request 26,747 -> 29,609 (+11%) On scripts/bench_tokens.py: uncached -35% for +2% input. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… M2) Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Nov 7, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #420 and #430. This resolves the cache miss that the v0.6.34 benchmark had documented as "by design", after measuring it.
Problem
Spill and prune rewrite messages inside the provider's cached prefix. They ran before every step and usually shrank a single result, so a long session missed the prompt cache on most steps. On a captured six-turn session (25 requests) this cost more, with a prompt cache, than never shrinking at all.
Change
SHRINK_BATCH_CHARS(16 KiB). The check isSession::old_tool_results_due, which counts what spill actually leaves (a 400-char head)./compactpath still shrink immediately.How the threshold was picked
I took the v0.6.34 requests captured on the six-turn session and replayed them with every shrink policy from "every step" to "never": thresholds from 4 KiB to 64 KiB. I picked 16 KiB from that replay and then checked it on a release build. The release build reproduced the replay row exactly.
* Base-rate units: cache read 0.1×, write 1.25×.
On the standard two-turn
scripts/bench_tokens.py: uncached −35% (49,657 → 32,401) for +2% total input.Tests
old_results_shrink_in_batches: below the batch, the pass does nothing. It also covers block content and already-spilled bodies (which are not counted), and checks the gate clears after a pass.worker_shrinks_old_results_once_a_batch_is_due: a worker reading seven ~20 KiB files reaches the gate.old_tool_outputs_are_spilled_to_scratch_before_the_step: the fixture outputs are larger, so this test still spills.session175 andagent768 lib tests pass; fmt, clippy and ratchets are ok. Local llvm-cov on session and agent: every changed line is covered.docs/benchmarks.md(with the replay table) anddocs/internals/compaction.mdare updated.🤖 Generated with Claude Code