Skip to content

Python: Implement annotation-based context compaction - #4469

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 15 commits into
microsoft:mainfrom
eavanvalkenburg:python_compaction
Mar 11, 2026
Merged

Eduard van Valkenburg (eavanvalkenburg) merged 15 commits into
microsoft:mainfrom
eavanvalkenburg:python_compaction

Conversation

@eavanvalkenburg

@eavanvalkenburg Eduard van Valkenburg (eavanvalkenburg) commented Mar 4, 2026 •

Copy link
Copy Markdown
Member

Summary

  • implement annotation-based context compaction primitives and built-in strategies in packages/core/agent_framework/_compaction.py
  • wire compaction through BaseChatClient
  • add and update core compaction/function-loop tests, plus compaction samples under samples/02-agents/compaction

@markwallace-microsoft Mark Wallace (markwallace-microsoft) added documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python labels Mar 4, 2026
@github-actions github-actions Bot changed the title Implement annotation-based context compaction Python: Implement annotation-based context compaction Mar 4, 2026
@markwallace-microsoft

Mark Wallace (markwallace-microsoft) commented Mar 4, 2026 •

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework
   _agents.py3564288%447, 451, 513, 918, 954, 970, 1064–1068, 1137, 1264, 1280, 1282, 1295, 1301, 1337, 1339, 1348–1353, 1358, 1360, 1366–1367, 1374, 1376–1377, 1385–1386, 1389–1391, 1399–1400, 1402, 1407, 1409
   _clients.py125695%321, 373, 521–524
   _compaction.py6336889%104, 150–162, 169–171, 177–178, 193–196, 203–205, 229, 243, 250, 265–266, 320, 327, 343, 393, 417, 447, 491, 497, 499, 518, 562–567, 579, 651, 653, 668, 712, 774, 896, 957, 959, 975, 982, 987, 1002, 1010, 1104, 1107, 1125, 1141, 1237, 1264, 1269
   _middleware.py3381894%61, 64, 69, 770, 786, 788, 790, 913, 916, 943, 945, 970, 1064, 1068, 1216, 1220, 1286, 1360
   _sessions.py1751690%67–69, 71–74, 91–92, 94–98, 583, 599
   _skills.py4061596%183, 912, 914–916, 970, 1009–1010, 1083, 1088, 1137, 1142, 1294–1295, 1543
   _tools.py7878089%169–170, 329, 331, 349–351, 359, 377, 391, 398, 405, 421, 423, 430, 438, 470, 495, 499, 516–518, 565–567, 589, 645, 667, 734–740, 776, 787–798, 817–819, 823, 827, 841–843, 1182, 1202, 1278–1282, 1406, 1410, 1434, 1552, 1582, 1602, 1604, 1657, 1720, 1923–1924, 1975, 2044–2045, 2105, 2110, 2117
   _types.py10328691%58, 67–68, 122, 127, 146, 148, 152, 156, 158, 160, 162, 180, 184, 210, 232, 237, 242, 246, 276, 684–685, 1193, 1264, 1299, 1317, 1327, 1371, 1503–1505, 1695, 1786–1791, 1816, 1984, 1996, 2247, 2271, 2366, 2597, 2803, 2872, 2883, 2885–2889, 2891, 2894–2902, 2912, 3117–3119, 3122–3124, 3128, 3133, 3137, 3221–3223, 3252, 3306, 3325–3326, 3329–3333, 3339
   observability.py6703195%370–371, 398, 400–402, 405–407, 412–413, 419–420, 426–427, 717, 721–723, 725, 1068–1072, 1262–1263, 1497, 1677, 2061, 2063
packages/core/agent_framework/openai
   _responses_client.py80112884%303–306, 310–311, 314–315, 321–322, 327, 340–346, 367, 375, 398, 494, 496, 593, 648, 652, 654, 656, 658, 726, 740, 820, 830, 835, 878, 957, 974, 987, 1048, 1139, 1144, 1148–1150, 1154–1155, 1198, 1227, 1233, 1243, 1249, 1254, 1260, 1265–1266, 1327, 1349–1350, 1365–1366, 1384–1385, 1426–1429, 1591, 1646, 1648, 1728–1736, 1855, 1910, 1925, 1945–1955, 1968, 1979–1983, 1997, 2007–2018, 2027, 2059–2062, 2070–2071, 2073–2075, 2089–2091, 2101–2102, 2108, 2123
TOTAL23534266088% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
5125 20 💤 0 ❌ 0 🔥 1m 24s ⏱️

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Implements Python annotation-based context compaction primitives and strategies, wires in-run compaction through BaseChatClient, and adds tests + samples demonstrating usage.

Changes:

  • Add new compaction core module with grouping annotations, token counting, built-in strategies, and a CompactionProvider.
  • Integrate optional compaction into BaseChatClient/Agent and update history provider to optionally skip excluded messages.
  • Add comprehensive compaction and function-loop tests plus new compaction samples and ADR rollout note.

Reviewed changes

Copilot reviewed 19 out of 20 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
python/uv.lock Updates locked dependency versions.
python/samples/02-agents/compaction/README.md Documents new compaction samples and how to run them.
python/samples/02-agents/compaction/basics.py Demonstrates individual built-in compaction strategies.
python/samples/02-agents/compaction/advanced.py Demonstrates composed compaction under a token budget.
python/samples/02-agents/compaction/custom.py Demonstrates a custom strategy implementing the compaction protocol.
python/samples/02-agents/compaction/compaction_provider.py Demonstrates provider-based compaction in an agent pipeline with history.
python/samples/02-agents/compaction/tiktoken_tokenizer.py Demonstrates a TokenizerProtocol implementation using tiktoken.
python/packages/core/tests/openai/test_openai_responses_client.py Adds regression test for parsing reasoning + function calls into one message.
python/packages/core/tests/core/test_types.py Adds roundtrip serialization coverage for compaction annotations.
python/packages/core/tests/core/test_function_invocation_logic.py Adds tests ensuring compaction is applied within function-call loops and under token budgets.
python/packages/core/tests/core/test_compaction.py Adds extensive tests for grouping, strategies, apply/projection, and provider behavior.
python/packages/core/tests/core/test_clients.py Adds tests ensuring BaseChatClient applies compaction before inner calls (streaming + non-streaming).
python/packages/core/agent_framework/openai/_responses_client.py Adjusts OpenAI Responses request shaping for function calls.
python/packages/core/agent_framework/_types.py Ensures additional_properties are normalized to plain dicts for annotation persistence.
python/packages/core/agent_framework/_sessions.py Adds skip_excluded option to InMemoryHistoryProvider to omit excluded messages on load.
python/packages/core/agent_framework/_compaction.py Adds the compaction implementation (annotations, strategies, provider, utilities).
python/packages/core/agent_framework/_clients.py Wires optional compaction into BaseChatClient and agent factory.
python/packages/core/agent_framework/_agents.py Plumbs compaction strategy + tokenizer through Agent and into the client.
python/packages/core/agent_framework/init.py Re-exports compaction APIs from the top-level package.
docs/decisions/0019-python-context-compaction-strategy.md Adds rollout/phase note for the implementation plan.

Copilot AI review requested due to automatic review settings March 11, 2026 15:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 19 out of 20 changed files in this pull request and generated 2 comments.

@eavanvalkenburg
Eduard van Valkenburg (eavanvalkenburg) removed this pull request from the merge queue due to a manual request Mar 11, 2026
Add ToolResultCompactionStrategy that collapses older tool-call groups
into short summary messages (e.g. [Tool calls: get_weather]) while
keeping the most recent groups verbatim. This mirrors the .NET
ToolResultCompactionStrategy from PR microsoft#4533.

Add CompactionProvider as a context-provider that auto-applies compaction
before each agent turn and stores compacted history in session state
after each turn.

Includes tests and samples for both features.

Co-authored-by: Copilot <[email protected]>
…p_excluded

- ToolResultCompactionStrategy collapses older tool-call groups into
  [Tool results: func_name: result] summaries with bidirectional tracing
  (same pattern as SummarizationStrategy).
- CompactionProvider as BaseContextProvider with separate before_strategy
  and after_strategy parameters. before_strategy compacts loaded context;
  after_strategy compacts stored history via history_source_id.
- InMemoryHistoryProvider gains skip_excluded flag to filter out messages
  marked as excluded by compaction strategies.
- Tests, samples, and exports updated.

Co-authored-by: Copilot <[email protected]>
… annotations

SummarizationStrategy was not calling annotate_message_groups after
inserting its summary message, so the summary lacked core group
annotations (id, kind, index, has_reasoning, _excluded). Added the
missing call. ToolResultCompactionStrategy already had it.

Added tests verifying both strategies produce fully annotated summaries.

Co-authored-by: Copilot <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 28 out of 29 changed files in this pull request and generated 2 comments.


You can also share your feedback on Copilot code review. Take the survey.

Merged via the queue into microsoft:main with commit 3e03a30 Mar 11, 2026
36 checks passed
@eavanvalkenburg
Eduard van Valkenburg (eavanvalkenburg) deleted the python_compaction branch June 30, 2026 09:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants