Skip to content

perf(session): shrink old tool results in batches, not every step - #431

Merged
mrnasil merged 2 commits into
mainfrom
perf/batch-shrink-old-results
Nov 7, 2026
Merged

mrnasil merged 2 commits into
mainfrom
perf/batch-shrink-old-results

Conversation

@mrnasil

@mrnasil mrnasil commented Nov 7, 2026

Copy link
Copy Markdown
Member

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

  • Batching. Spill and prune run only when the old results they would shrink can free at least SHRINK_BATCH_CHARS (16 KiB). The check is Session::old_tool_results_due, which counts what spill actually leaves (a 400-char head).
  • Subagents. Workers use the same gate.
  • Unchanged. Shake under context pressure (¾ of the threshold) and the explicit /compact path 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.

Six turns, 25 requests Total input Uncached Cache misses Cost with cache* Last request
v0.6.33 1,120,750 287,390 14 442,574 51,902
v0.6.34, every step 586,193 98,900 8 172,354 26,747
never shrink 828,982 48,811 0 139,031 48,811
this PR, ≥16 KiB 610,840 (+4%) 62,463 (−37%) 2 132,916 (−23%) 29,609 (+11%)

* 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.
  • session 175 and agent 768 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) and docs/internals/compaction.md are updated.

🤖 Generated with Claude Code

mrnasil and others added 2 commits November 7, 2026 19:43
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]>
@mrnasil
mrnasil merged commit 97491ab into main Nov 7, 2026
7 checks passed
@mrnasil
mrnasil deleted the perf/batch-shrink-old-results branch November 7, 2026 17:14
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