Skip to content

Python: Prevent compaction from emitting empty projections#7219

Merged
eavanvalkenburg merged 3 commits into
microsoft:mainfrom
ronronner02:codex/7213-compaction-context-floor
Jul 21, 2026
Merged

Python: Prevent compaction from emitting empty projections#7219
eavanvalkenburg merged 3 commits into
microsoft:mainfrom
ronronner02:codex/7213-compaction-context-floor

Conversation

@ronronner02

@ronronner02 ronronner02 commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Motivation & Context

Fixes #7213.

Compaction must not return an empty model-visible projection when the newest message group alone exceeds the target budget. An empty projection causes providers to reject an otherwise healthy agent run.

Description & Review Guide

  • What are the major changes? Add a shared floor calculation that retains the latest included non-system group, or the last system group when no non-system group exists. Apply it to TruncationStrategy and both fallback paths in TokenBudgetComposedStrategy.
  • What is the impact of these changes? Oversized final groups remain visible instead of being dropped, so the projection can remain over budget but stays valid and actionable. Existing cases that can reach their target budget continue to do so.
  • What is the intentional trade-off? When the minimum-retained group alone exceeds the budget, the strict fallback may remove system anchors rather than remove that final group. The budget remains over target, but the projection is non-empty and preserves the newest model-visible context; this is preferable to sending an invalid empty request.
  • What do you want reviewers to focus on? The floor behavior when system-only context, a single oversized group, or an already-compacted message list reaches either fallback path.

The patch was developed with AI assistance and manually reviewed against the existing compaction annotations and test conventions.

Related Issue

Fixes #7213

Contribution Checklist

  • The changed module builds clean without any errors or warnings; package-wide Pyright is currently blocked by pre-existing optional dependency resolution errors in unrelated modules (_workflows/_viz.py and observability.py).
  • Focused unit tests and the complete packages/core/tests suite pass; regression tests were added.
  • Ruff lint/format, MyPy for the changed test and source files, and Pyright for the changed source module pass.
  • The PR follows the Contribution Guidelines.
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change.

Copilot AI review requested due to automatic review settings July 20, 2026 16:09
@giles17 giles17 added the python Usage: [Issues, PRs], Target: Python label Jul 20, 2026

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

This PR fixes a Python compaction edge case where truncation and token-budget fallback paths could exclude every model-visible message (or every non-system message), producing an empty projection that providers reject. It introduces a shared “minimum retained group” floor so the newest included non-system group (or the last system group if no non-system group exists) is always kept.

Changes:

  • Add _minimum_retained_group_ids(...) and apply it to TruncationStrategy protection logic.
  • Apply the same floor to TokenBudgetComposedStrategy fallback and strict-fallback exclusion loops.
  • Add/adjust regression tests in test_compaction.py to cover oversized-latest-group and system-only scenarios.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
python/packages/core/agent_framework/_compaction.py Adds and applies a minimum-retained-group floor to prevent empty projections during truncation and token-budget fallback paths.
python/packages/core/tests/core/test_compaction.py Updates token-budget thresholds and adds regression tests for “keep latest group” / system-only behaviors.

Comment thread python/packages/core/tests/core/test_compaction.py
@ronronner02
ronronner02 force-pushed the codex/7213-compaction-context-floor branch from f7ed863 to 0044f11 Compare July 21, 2026 03:38
@ronronner02

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@github-actions

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework
   _compaction.py7105891%117–118, 126, 193–194, 212–213, 230, 264, 278, 285, 300–301, 355, 362, 378, 428, 452, 483, 532, 538, 540, 559, 603–608, 620, 704, 706, 721, 766, 828, 954, 960–964, 967–969, 977, 1052, 1054, 1070, 1077, 1082, 1097, 1105, 1204, 1227, 1239, 1335, 1362, 1446
TOTAL44842510288% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
9140 33 💤 0 ❌ 0 🔥 2m 25s ⏱️

@eavanvalkenburg
eavanvalkenburg added this pull request to the merge queue Jul 21, 2026
Merged via the queue into microsoft:main with commit afdf8af Jul 21, 2026
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: compaction can exclude EVERY message — the empty projection crashes the provider mid-run

5 participants