Keep deferred capabilities loaded when `SummarizingCompaction`, `SlidingWindowCompaction` and `RelevanceCompaction` remove their `load_capability` exchange
Builds on #10573 (the summary as a CompactionPart), now merged.
- Closes #11264
Pydantic AI counts a deferred capability as loaded while its load_capability exchange is in the post-compaction window. A harness strategy that summarized or dropped that exchange unloaded the capability: its tools left the request, its instructions stopped being sent, the summary still said it was loaded, and the model found out from a refused tool call. Per the decision on the issue, the rule stays and the strategies fix it.
What changes
SummarizingCompaction,SlidingWindowCompactionandRelevanceCompaction(both its dropping and summarizing paths) now record each lost load again: aLoadCapabilityCallPartand aLoadCapabilityReturnPartwith the original return's content (the fixed confirmation since #11103), plus the originalToolAvailabilityDeltaPart, placed after the boundary (see below). One strategy-agnostic helper does this, and each strategy calls it on its result. Core then keeps the capability loaded and states its current instructions again as anInstructionDeltaPart, as for a real load.- The recorded exchange is marked
{'pydantic-ai-harness.compaction.recorded-load.v1': True}on itsModelResponse.metadataand its return'smetadata. - One source of truth for load state: the summarizer transcript leaves out every
load_capabilityexchange, the model's own included, so a summary never describes which capabilities are loaded; the recorded exchange (or the note below) carries that. A history holding only the summary and load exchanges doesn't trigger a new summary underkeep_tokens. (Relevant to the summarizer prompt work in #11436-#11438, which this PR doesn't touch.) - Core-callable:
keep_capability_loads(original, compacted, *, kept_from, deferred_ids, preserve)imports only Pydantic AI and is a plain function of the messages and the deferred capability ids, so it runs workflow-side in a durable run and can move into core's compaction step (#11454) unchanged. Calling it again on its own result is a no-op (tested), the note included. - Placement: right after a request-side summary; after a provider's
CompactionPartresponse and the requests that follow it (never before it, so an Anthropic signed block from #11432 stays first; tested); else after the leading requests, ahead ofkept_from. ATODO(#7449)marks that a sliding-window truncation marker will count as a boundary once it's aCompactionPart. - Only loads of capabilities the run still registers as deferred are recorded (a capability that is now always-on, or no longer registered, is skipped).
compact_nowhas no registry, so it records every load; the next run ignores loads of capabilities it doesn't register. - New option
preserve_loaded_capabilities: bool = Trueon those three strategies.Falselets the loads go and leaves a harness-data note (origin: 'pydantic-ai-harness.compaction.unloaded-capabilities.v1',capabilities: [...]) after the summary naming the capabilities that are no longer loaded. ClearToolResults(clear_tool_inputs=True)blanked theload_capabilitycall's{'id': ...}argument, which also unloaded the capability (reproduced). It now keeps that argument.
Checked by reproduction, not changed
ClearToolResults(withoutclear_tool_inputs),ClampOversizedMessages,DeduplicateFileReads: leave loads intact.TieredCompaction/FallbackCompactioncompose the strategies above, so they get the fix.- Provider-native compaction (a
CompactionPartin a response) still unloads capabilities. That's core's rule and no harness strategy runs there. - Tool search has the same loss (reproduced): a tool discovered with
search_toolsbefore aSummarizingCompaction(or a native compaction) is "not available yet" afterwards. Not fixed here, with aTODOinkeep_capability_loads: a localsearch_toolsexchange could be recorded again the same way, but a provider's native search parts (NativeToolSearchReturnPart, in responses) can't be synthesized, so that's left for the core compaction step (#11454).
Tests in tests/harness/compaction/test_capability_loads.py run Agents with FunctionModel and check, on the first request after compaction, that the capability's tools are offered and its instructions are stated in the system voice, or with the opt-out that they're gone and the note is there. They also cover two successive compactions with two capabilities (load order kept, recorded loads never summarized), skipped loads, compact_now, and the keep_tokens no-op.
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.
š¤ Generated with Claude Code