Report when `TieredCompaction` can't reach `target_tokens` because its tiers keep more than the target

Merged
DO
24d ago
+152-15

Why

TieredCompaction([SlidingWindowCompaction, SummarizingCompaction]) returned a history above target_tokens with no model call and no signal. Escalation is fine: the summarizing tier runs. It has nothing it may summarize, because the window leaves [first user message, current request] and the summarizer keeps both.

Bisecting the issue's repro from v2.54.0 to main lands on #9784 (d19a42c3fd). On 0.54 the summarizer's one request summarized only the first user message, which it also kept verbatim: [summary-of-m0, m0, m4] at ~28016 tokens, up from ~28001. #9784 added the early return that skips that call. With realistic turns, 0.54 didn't escalate either: its keep_tokens was a ceiling, so the window alone trimmed to [U0, Ulast]. #9784 made it a floor that keeps the last answer. Both #9784 rules stay. What was missing is a report when the target can't be reached.

New public surface

None. There are new compact_messages span attributes (compaction.target_unreached, compaction.target_tokens, compaction.estimate) and a warning on the pydantic_ai_harness.compaction._tiered_compaction logger.

User-visible behavior

When every tier has run and the estimate is still above the target, TieredCompaction logs:

`TieredCompaction` ran every tier and left the history at an estimated 199 tokens, above its target of 5. The messages its tiers keep cost more than the target.

Only the outermost chain reports. It sets the attributes above on its span when the history changes; a no-op emits no span, so the warning alone reports the target and estimate. The compaction docs and README now say that each tier's kept messages (the keep_tokens/keep_messages tail with the current turn, the first user message, pins) set a lower bound on the target. A user who wants the first message summarized can pass preserve_first_user_message=False.

Verification

  • test_tiered_reports_a_target_its_tiers_cannot_reach uses the issue's shape. It fails on main with KeyError: 'compaction.target_unreached' and passes here. It also checks that the summarizer makes no call.
  • tests/harness/compaction/test_compaction.py: 419 passed. ruff and single-file pyright are clean.

What changes for existing users

Nothing changes in the compacted history. A chain whose target is out of reach now logs a warning on each compaction.

Checklist

  • 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)