Emit the core compaction lifecycle events from every strategy, with a cancellable `CompactionStartEvent`
Summary
Every compaction strategy attempt now emits the core compaction lifecycle events (pydantic_ai.capabilities.compaction, kinds compaction.start / compaction.end) — no Harness-local event definitions; provider-native compaction and Harness strategies share one family:
- The shared seam is
compact_with_events, called fromcompact_with_spanand from the composing strategies' per-candidate loops, soSummarizingCompaction,TieredCompaction(outer + per tier),SlidingWindowCompaction,ClearToolResults,DeduplicateFileReads,ClampOversizedMessages, andFallbackCompaction(per candidate) are all covered.strategycarries the class name, matching thecompaction.strategyspan attribute. CompactionStartEventis inline-dispatched and cancellable: any capability (@on_event) or app code (@hooks.on.event) cancancel()an attempt — e.g. while activity state hasn't been saved. Cancellation is per-attempt (the trigger re-fires later), returns the history unchanged with no receipt/span, and a cancelledFallbackCompactioncandidate is a deliberate skip that does not advance the fallback chain (a configured exception still does; both pinned in tests).CompactionEndEventis emitted only when history actually changed, with before/after message counts and token estimates. Events are the live coordination signal; receipts remain the in-history note for the model.compact_nowsuppresses events (it runs outside an agent event stream), via anemitsflag that also propagates to nested composing strategies.
The event's estimated_tokens legitimately invokes the strategy tokenizer even when the tracer isn't recording; the span-laziness test now pins that claim with events disabled.
Requires an unreleased pydantic-ai (pydantic/pydantic-ai#6258 + pydantic/pydantic-ai#7801); don't merge before a core release carries both.
Linked Issue
Part of #711.
Checklist
- Linked issue exists and is referenced above
- Tests added/updated for new behavior
-
make lint && make typecheck && make testpasses locally (against pydantic-aicompaction-lifecycle-events) -
pyproject.tomlanduv.lockare unchanged, or a maintainer addeddependencies:approvedto the current head - Docstrings use single backticks (not RST double backticks)
Blocked on a Pydantic AI release. This PR uses the event API added in pydantic-ai#6258, which is merged to main but not yet in any release — the newest tag is v2.37.0. Before this can come out of draft, pydantic-ai-slim in pyproject.toml has to be bumped from >=2.33.0 to the first release that contains it.
Verified locally against pydantic-ai main (plus #7801 where relevant) rather than the pinned release, so CI here will fail on import until that bump lands.