Add `CompactOnCacheExpiry`, which asks the agent's compaction to run once the prompt cache has expired
Draft
AD
1mo ago+2539+2539+0123456789012345678901234567890123456789-54-54-01234567890123456789
No issue yet: opened as a draft for design review.
- Adds
CompactOnCacheExpirytopydantic_ai_harness.compaction. On the first request after the provider's prompt cache expired (coreprompt_cache_outlookis'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, fieldsreasonandcompacted_by) andrequest_compaction(ctx, request_context, reason=...). Listeners replaceevent.request_context.messagesand setcompacted_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 coreOpenAICompaction(calls/responses/compacton an OpenAI Responses model; a compaction its own trigger already made for the request answers it). - When no listener compacts,
CompactOnCacheExpiry.fallbackruns: by defaultClearToolResults(keep_pairs=5)with a placeholder telling the model it can call the tool again.Nonedisables it. It runs as the capability'scompactdurable 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 withCompactOnRequest. - 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 recordscompaction.idle_seconds. - Core behaviour change:
has_on_eventno longer counts@on_eventmethods that only listen to immediate-dispatch events (subclasses included), so a compaction capability doesn't switch runs to streamed model requests. Listeners registered throughHooksstill do. Fourtest_on_event.pytests 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 unlessfallback_retentionis set. - Not in this PR:
AnthropicCompactionlistening throughAnthropicModel.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/compactcall; every harness listener handling the request inrunandrun_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.