Add `CompactOnCacheExpiry`, which asks the agent's compaction to run once the prompt cache has expired

Draft
AD
1mo ago
+2539-54

No issue yet: opened as a draft for design review.

  • Adds CompactOnCacheExpiry to pydantic_ai_harness.compaction. On the first request after the provider's prompt cache expired (core prompt_cache_outlook is 'cold') and the history is above 20% of the context window, it asks the agent's compaction to run. That request pays a full cache write anyway, so the edit adds no cache cost.
  • Adds core CompactionRequestedEvent (immediate dispatch, fields reason and compacted_by) and request_compaction(ctx, request_context, reason=...). Listeners replace event.request_context.messages and set compacted_by; the first in capability order to change the history handles the request, and the result is written to the run's history.
  • Listeners added to ClearToolResults, SlidingWindowCompaction, SummarizingCompaction, RelevanceCompaction, TieredCompaction (runs at least its first tier), FallbackCompaction (uses a held background compaction when there is one) and core OpenAICompaction (calls /responses/compact on an OpenAI Responses model; a compaction its own trigger already made for the request answers it).
  • When no listener compacts, CompactOnCacheExpiry.fallback runs: by default ClearToolResults(keep_pairs=5) with a placeholder telling the model it can call the tool again. None disables it. It runs as the capability's compact durable operation, so replays reuse it.
  • It emits from before_model_request, after durable execution has swapped in its recorded model, so listeners that call a model are recorded. When a compaction's own trigger already compacted the request, it doesn't ask again.
  • It has a default id='compact_on_cache_expiry' (needed for the durable operation), so two instances on one agent merge, as with CompactOnRequest.
  • Size triggers later in the same request subtract what the requested compaction reclaimed (new estimate_request_tokens, exceeds(reclaimed=)), so the history isn't compacted twice.
  • Spans get compaction.reason; the fallback also records compaction.idle_seconds.
  • Core behaviour change: has_on_event no longer counts @on_event methods that only listen to immediate-dispatch events (subclasses included), so a compaction capability doesn't switch runs to streamed model requests. Listeners registered through Hooks still do. Four test_on_event.py tests relied on the old streaming and now use non-streaming models.
  • Unknown retention (Gemini direct, DeepSeek, xAI, Azure, Bedrock Nova, OpenAI before GPT-5.6, FallbackModel) compacts nothing unless fallback_retention is set.
  • Not in this PR: AnthropicCompaction listening through AnthropicModel.compact_messages() (until then the fallback runs there); compacting shortly before expiry so the summary reads the warm cache.
  • Tests: recorded OpenAI cassettes for a requested compaction and for the expiry path, each asserting one /responses/compact call; every harness listener handling the request in run and run_stream; listener order, fallback, pins, same-request reclaim for Tiered and Summarizing, an already-compacted request, SelectModel, a resumed response, and the event API.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
  • PR title is fit for the release changelog.