Keep deferred capabilities loaded when `SummarizingCompaction`, `SlidingWindowCompaction` and `RelevanceCompaction` remove their `load_capability` exchange

Merged
DO
1mo ago
+910-19

Builds on #10573 (the summary as a CompactionPart), now merged.

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, SlidingWindowCompaction and RelevanceCompaction (both its dropping and summarizing paths) now record each lost load again: a LoadCapabilityCallPart and a LoadCapabilityReturnPart with the original return's content (the fixed confirmation since #11103), plus the original ToolAvailabilityDeltaPart, 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 an InstructionDeltaPart, as for a real load.
  • The recorded exchange is marked {'pydantic-ai-harness.compaction.recorded-load.v1': True} on its ModelResponse.metadata and its return's metadata.
  • One source of truth for load state: the summarizer transcript leaves out every load_capability exchange, 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 under keep_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 CompactionPart response 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 of kept_from. A TODO(#7449) marks that a sliding-window truncation marker will count as a boundary once it's a CompactionPart.
  • 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_now has no registry, so it records every load; the next run ignores loads of capabilities it doesn't register.
  • New option preserve_loaded_capabilities: bool = True on those three strategies. False lets 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 the load_capability call's {'id': ...} argument, which also unloaded the capability (reproduced). It now keeps that argument.

Checked by reproduction, not changed

  • ClearToolResults (without clear_tool_inputs), ClampOversizedMessages, DeduplicateFileReads: leave loads intact.
  • TieredCompaction / FallbackCompaction compose the strategies above, so they get the fix.
  • Provider-native compaction (a CompactionPart in 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_tools before a SummarizingCompaction (or a native compaction) is "not available yet" afterwards. Not fixed here, with a TODO in keep_capability_loads: a local search_tools exchange 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