Emit the core compaction lifecycle events from every strategy, with a cancellable `CompactionStartEvent`

DO
3mo ago
+538-36

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 from compact_with_span and from the composing strategies' per-candidate loops, so SummarizingCompaction, TieredCompaction (outer + per tier), SlidingWindowCompaction, ClearToolResults, DeduplicateFileReads, ClampOversizedMessages, and FallbackCompaction (per candidate) are all covered. strategy carries the class name, matching the compaction.strategy span attribute.
  • CompactionStartEvent is inline-dispatched and cancellable: any capability (@on_event) or app code (@hooks.on.event) can cancel() 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 cancelled FallbackCompaction candidate is a deliberate skip that does not advance the fallback chain (a configured exception still does; both pinned in tests).
  • CompactionEndEvent is 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_now suppresses events (it runs outside an agent event stream), via an emits flag 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 test passes locally (against pydantic-ai compaction-lifecycle-events)
  • pyproject.toml and uv.lock are unchanged, or a maintainer added dependencies:approved to 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.