Repository navigation
Python: Preserve compaction through middleware rewrites - #8671
Conversation
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Partial replacement coverage can lose content, and duplicate summary IDs can be persisted ambiguously.
Get a fresh assessment by requesting another Copilot review.
Review effort: Balanced
Findings: 2
Open (2)
What changed in this PR
Adds opt-in provenance tracking so middleware-generated replacements can participate in durable compaction without persisting provider-local rewrites.
Changes:
- Adds replacement provenance recording and transitive reconciliation.
- Preserves summaries across termination/restoration paths and logs rejections.
- Adds streaming, validation, and reconciliation tests.
| File | Description |
|---|---|
python/packages/core/agent_framework/_middleware.py |
Adds provenance API and fallback snapshots. |
python/packages/core/agent_framework/_compaction.py |
Resolves replacement lineage and transfers exclusions. |
python/packages/core/tests/core/test_middleware.py |
Tests provenance API validation. |
python/packages/core/tests/core/test_clients.py |
Tests reconciliation scenarios. |
python/packages/core/AGENTS.md |
Documents replacement registration. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
MAF Automated Review — Iteration 1
Result: Findings reported
Scope: full PR (1 commit(s)): 0a0d0c0a966f
Model: gpt-5.6-sol-fast
Overview
The review found 2 verified inline finding(s).
Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
2 verified findings remained after source verification (1 high, 1 medium) across 2 files. Details are attached to the affected lines below.
Affected areas: python/packages/core/agent_framework/_compaction.py, python/packages/core/agent_framework/_middleware.py

Motivation & Context
Chat middleware may need to replace messages for a provider-specific request shape. When downstream compaction summarizes those replacements, the ownership-aware reconciliation cannot connect the replacement IDs to caller-owned history, so it rejects the summary, removes exclusions, and silently leaves the original history unchanged.
This adds an explicit opt-in provenance contract so middleware replacements remain call-local while compaction derived from them can become durable.
Description & Review Guide
ChatContext.record_message_replacement(...)to record invocation-local replacement provenance without persisting replacement messages._reconcile_compaction_summaries, especially transitive replacement roots and exclusion transfer.ChatMiddlewareLayer, which uses middleware snapshots only when no downstream list was captured.Related Issue
Fixes #8313
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after a language prefix) — a workflow keeps the label and title prefix in sync automatically.