Skip to content

Python: Ignore excluded tool results during compaction - #7391

Open
dk3yyyy wants to merge 2 commits into
microsoft:mainfrom
dk3yyyy:fix/tool-result-compaction-excluded-messages
Open

Python: Ignore excluded tool results during compaction#7391
dk3yyyy wants to merge 2 commits into
microsoft:mainfrom
dk3yyyy:fix/tool-result-compaction-excluded-messages

Conversation

@dk3yyyy

@dk3yyyy dk3yyyy commented Jul 29, 2026

Copy link
Copy Markdown

Motivation & Context

ToolResultCompactionStrategy could summarize messages that had already been marked as excluded when only part of a tool group remained in context. This could restore a large excluded tool result inside the generated summary and increase context token usage instead of reducing it.

This change keeps excluded content out of generated summaries while preserving the existing provenance links between each original message and its summary.

Description & Review Guide

  • What are the major changes? Summary content and function-call mappings are now built only from included messages in the selected group. The complete group is still used for provenance IDs and backlinks. A regression test covers excluded-content restoration, token growth, and bidirectional provenance.
  • What is the impact of these changes? Partially included tool groups compact without reintroducing excluded payloads. Existing function-call and MCP result handling remains unchanged, and there is no public API change.
  • What do you want reviewers to focus on? Please review the boundary between included summary content and full-group provenance, particularly the use of filtered messages for content while retaining all original message IDs for traceability.

Related Issue

Fixes #7387

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • 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. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

Copilot AI review requested due to automatic review settings July 29, 2026 11:10
@dk3yyyy
dk3yyyy temporarily deployed to github-app-auth July 29, 2026 11:10 — with GitHub Actions Inactive
@dk3yyyy
dk3yyyy temporarily deployed to github-app-auth July 29, 2026 11:10 — with GitHub Actions Inactive
@dk3yyyy
dk3yyyy temporarily deployed to github-app-auth July 29, 2026 11:10 — with GitHub Actions Inactive
@dk3yyyy

dk3yyyy commented Jul 29, 2026

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@dk3yyyy
dk3yyyy temporarily deployed to github-app-auth July 29, 2026 11:11 — with GitHub Actions Inactive
@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Jul 29, 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 compaction bug in the Python core where ToolResultCompactionStrategy could re-materialize tool result payloads that were already marked excluded, causing context token usage to grow during compaction. The update ensures generated tool-result summaries are built only from still-included messages while preserving full-group provenance links for traceability.

Changes:

  • Build tool-result summary text and call-id→name mappings from included messages only within a selected tool-call group.
  • Preserve provenance/backlinks using the full group’s original message IDs (including excluded messages).
  • Add a regression test covering excluded-payload restoration, token growth prevention, and bidirectional provenance.

Reviewed changes

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

File Description
python/packages/core/agent_framework/_compaction.py Filters group messages used to build tool-result digests to avoid summarizing excluded tool results while keeping full-group provenance.
python/packages/core/tests/core/test_compaction.py Adds a regression test ensuring excluded tool results are not restored into the inserted summary and provenance links remain correct.

Comment thread python/packages/core/agent_framework/_compaction.py Outdated
@github-actions

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework
   _compaction.py7115891%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, 955, 961–965, 968–970, 978, 1053, 1055, 1071, 1078, 1083, 1098, 1106, 1205, 1228, 1240, 1336, 1363, 1447
TOTAL45596447990% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
9416 34 💤 0 ❌ 0 🔥 2m 26s ⏱️

@dk3yyyy
dk3yyyy temporarily deployed to github-app-auth July 30, 2026 10:34 — with GitHub Actions Inactive
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]: ToolResultCompactionStrategy digests EXCLUDED messages, so collapsing a partly-included group GROWS the context

3 participants