refactor: promote history compaction to a first-class pydantic-ai capability - #829
Open
mpfaffenberger wants to merge 2 commits into
Open
refactor: promote history compaction to a first-class pydantic-ai capability#829mpfaffenberger wants to merge 2 commits into
mpfaffenberger wants to merge 2 commits into
Conversation
…ability The in-run compaction pipeline was an async closure (make_history_processor) jammed into the generic ProcessHistory adapter at two call sites (main builder + sub-agent invocation). Promote it to HistoryCompaction, a proper AbstractCapability subclass in _compaction.py, matching harness house style. - before_model_request override with byte-identical ProcessHistory semantics: request_context.messages is REPLACED with the processed durable history, so registration order against steer injection and the response clamp is preserved exactly - get_serialization_name() -> None (holds a live agent reference) - Both call sites now wire HistoryCompaction(agent) directly - one class, zero closure indirection, DRY across main + sub-agent paths - Feature parity: start/end hooks, hash-dedup merge with newest-message collision protection, forced /compact via pause controller, dropped-hash bookkeeping, empty-ThinkingPart stripping, trailing-ModelResponse trim, tool_call_id sanitization - Tests exercise the real before_model_request seam plus a TestModel-backed Agent.run() proving dispatch through the actual capability chain; stale make_history_processor mock patches removed (constructing the dataclass is side-effect-free)
… proof, stale docstring - The replace-not-append test now asserts list IDENTITY: the processed durable history object itself becomes the outbound list - The capability-chain dispatch test captures the messages the model actually received via FunctionModel and asserts they are exactly the processed durable history (the docstring no longer overclaims) - Refresh the stale ProcessHistory(compaction) wiring prose in test_runtime_pause_leakage.py
This was referenced Aug 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The in-run compaction pipeline was an async closure (
make_history_processor) wrapped in the genericProcessHistoryadapter at two call sites (main builder + sub-agent invocation). This promotes it toHistoryCompaction, a properAbstractCapabilitysubclass living in_compaction.py— the same house-style upgrade #828 gave the steering injector.Why
capabilities=[...]; compaction was the biggest feature still hiding behind a closure-in-an-adapter.HistoryCompaction(agent)replaces closure construction +ProcessHistorywrapping at both call sites.Design notes
before_model_requestREPLACESrequest_context.messageswith the processed durable history — exactly whatProcessHistorydoes internally — so registration order against steer injection and the response clamp is preserved (compaction still fires first; a fresh steer can't be compacted away).get_serialization_name() -> None— not spec-serializable, it holds a live agent reference (same rationale asProcessHistoryitself)./compactvia pause controller dropped-hash bookkeeping empty-ThinkingPart stripping trailing-ModelResponse trim (Anthropic prefill)tool_call_idsanitization spinner context badge updatesTests
before_model_requestseam.TestModel-backedAgent.run()proving dispatch through the actual capability chain lands the user prompt in the durable history.make_history_processormock patches — constructing the dataclass is side-effect-free.test_render_version_check_current) is pre-existing on main.Heads-up
Touches the same
capabilities=[...]block as #828 (steering capability). Whichever lands second gets a trivial rebase.Do not merge — awaiting review.