Don't commit an automatic `SummarizingCompaction` summary that would make the history larger

Merged
DO
1mo ago
+394-29

Closes #11436

Why. When SummarizingCompaction runs on its own trigger, or inside a triggered FallbackCompaction (clai2's automatic chain), it committed the compacted history even when it was larger than the input: system prompts, the first user turn, the kept tail and pins stay verbatim, so a short summarized span plus a long summary grows the request. The issue's repro sends ~213 estimated tokens for a ~62-token history. The approach follows @NicoYang1984's in their comment: reject a summary that doesn't shrink the full history, discard the receipts the attempt made, record a skip, and let FallbackCompaction try the next strategy, while direct compact() calls keep their behavior. The size check is the one clai2's /compact already makes (#9940), with the same text estimate the strategies use for their reclaim and spans (estimate_token_count in _shared.py), measured with the tokenizer of the capability whose trigger fired (FallbackCompaction's inside the chain, so the check agrees with the chain's own reclaim).

New public surface: none. The span gains a compaction.skipped attribute.

User-visible behavior

 SummarizingCompaction.before_model_request()       # or FallbackCompaction.before_model_request()
 └─ compact_if_smaller(compact=...)                  # new, in _shared.py
    └─ SummarizingCompaction.compact()               # unchanged
-      → committed even when larger: request ~62 → ~213 tokens
+      → not smaller: history unchanged (~62 tokens), receipts dropped,
+        `compact_messages` span with compaction.skipped='not_smaller'
+        inside FallbackCompaction: next strategy runs (SlidingWindow → ~11 tokens)
  • The reclaim is recorded honestly: a rejected attempt commits nothing, so record_compaction_reclaim sees before == after (0), and the attempted growth is on the skip span's compaction.tokens_before/tokens_after. A committed summary is now always smaller, so the max(..., 0) clamp never hides a growth on this path. The shared clamp itself is untouched, since other strategies weren't shown to grow.
  • compact(), compact_now() (so /compact, which keeps its own guard), TieredCompaction tiers and the background compaction (already discarded when it reclaims nothing) are unchanged.
  • When no strategy in the chain succeeds and one was a rejected summary, the history is left unchanged instead of re-raising another strategy's error: that summary completed, it just didn't help. If every strategy raised, the last error is re-raised as before.
  • After a rejection the history is unchanged, so while the trigger stays exceeded the next request in the run tries again, with another summary call. The docs say so, and that a SlidingWindowCompaction after the summarizer in a FallbackCompaction bounds both the history and the cost. Remembering a rejected input to skip the retry would be a follow-up (an anti-thrash guard, item 3d in the benchmark notes).
  • Core's compaction step (#11454, step 1 of #11395) will enforce the same no-shrink invariant for every compact_history handler. This is the narrow harness fix until the strategies move onto that hook; #11454 isn't touched here.

Verification

  • New tests/harness/compaction/test_no_shrink.py: the issue's repro through Agent.run for both keep_messages and keep_tokens (request sent equals the original history, skip span snapshotted), fall-through to SlidingWindowCompaction with only the kept strategy's receipt on the span, a shrinking summary still kept, an unchanged history when nothing shrinks it, and the same when a rejected summary sits next to a strategy that raises, in both orders.
  • Existing fixtures that relied on a tiny history growing (single-letter turns, 'ok' replies) now summarize a longer reply, and test_capability_loads.py's fixture summary is shorter. Their assertions are unchanged.
  • tests/harness/compaction, tests/clai2/test_compaction.py and the other harness/clai2 files using these strategies pass locally; branch coverage of the three changed modules is 100%. Ruff and single-file pyright clean.
  • The local pre-push review subagent wasn't run, per Douwe's instructions for this task.

What changes for existing users: an automatic summary that would make the history bigger is no longer applied; the summary call's usage still counts.

Checklist

  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.

(Claude, Douwe's coding agent)

Opened by Claude (Douwe's coding agent).