Repository navigation
Python: Add agent-triggered in-run compaction - #9384
Eduard van Valkenburg (eavanvalkenburg) wants to merge 5 commits into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Skill unloading can remove unrelated hosted-tool batches, while malformed trigger arguments can crash or unload all skills.
3 open findings
What changed in this PR
Adds agent-requested in-run context compaction while retaining developer-controlled compaction policies.
Changes:
- Adds generic triggered compaction and skill-unloading strategies.
- Exports and tests streaming, retention, and atomic-group behavior.
- Adds Foundry samples and documentation.
| File | Description |
|---|---|
python/packages/core/agent_framework/_compaction.py |
Implements both strategies and trigger tools. |
python/packages/core/agent_framework/__init__.py |
Adds runtime exports. |
python/packages/core/agent_framework/__init__.pyi |
Adds typed exports. |
python/packages/core/tests/core/test_compaction.py |
Tests strategy semantics. |
python/packages/core/tests/core/test_agents.py |
Tests streaming and non-streaming integration. |
python/packages/core/AGENTS.md |
Documents compaction behavior. |
python/samples/02-agents/compaction/README.md |
Lists the new samples. |
python/samples/02-agents/compaction/agent_triggered_compaction.py |
Demonstrates skill unloading. |
python/samples/02-agents/compaction/agent_triggered_compaction_basic.py |
Demonstrates generic triggering. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
Code coverage overviewLanguages: Python Python / code-coverage/python
View coverage of the 4 most impacted files.
ℹ️ Calculations represent line coverage. Results may change as calculations are updated. Updated |
There was a problem hiding this comment.
MAF Automated Review — Iteration 1
Result: Findings reported
Scope: full PR (1 commit(s)): 7004916523b4
Model: gpt-5.6-sol
Overview
The PR composes agent-triggered compaction with the existing grouping and projection pipeline, with strong guards for one-shot consumption, failure restoration, mixed trigger batches, and streaming parity. The skill-unloading strategy also preserves ordinary mixed function-call groups and normalizes selected skill names. Two lifecycle gaps remain: rejected approval results are accepted as successful triggers, and completed non-adjacent trigger groups can be skipped because recency is based on the declaration's first occurrence.
Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
2 verified findings remained after source verification (2 medium) across 1 file. Details are attached to the affected lines below.
Affected areas: python/packages/core/agent_framework/_compaction.py
c5a095a to
8fe72ab
Compare
73e53c2 to
7088eca
Compare



Motivation & Context
Agents can recognize semantic context boundaries—such as finishing planning or completing work that required a loaded skill—but existing compaction strategies run only according to developer-configured conditions. This change lets an agent request in-run compaction while preserving the existing responsibility split: the agent chooses when to compact, and the configured strategy controls what is removed and how.
The implementation composes with the existing function-calling and compaction pipelines and does not introduce persisted-history mutation.
Description & Review Guide
AgentTriggeredCompactionStrategy, which wraps one existing strategy and exposes one explicitly registered trigger tool.UnloadSkillsCompactionStrategyand itsunload_skilltrigger for one skill, multiple skills, or all loaded skills.CompactionStrategyprotocol, Agent tool discovery, sessions, or history providers.Related Issue
Fixes #9381
Part of #9379
AI Assistance
AI assistance details: GitHub Copilot assisted with the design, Python implementation, tests, samples, and this pull request description.
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and the title prefix in sync automatically.