Skip to content

Python: Keep call and result occurrences atomic in compaction - #7406

Merged
eavanvalkenburg merged 3 commits into
microsoft:mainfrom
eavanvalkenburg:python-compaction-call-result-occurrences
Jul 30, 2026
Merged

Python: Keep call and result occurrences atomic in compaction#7406
eavanvalkenburg merged 3 commits into
microsoft:mainfrom
eavanvalkenburg:python-compaction-call-result-occurrences

Conversation

@eavanvalkenburg

Copy link
Copy Markdown
Member

Motivation & Context

Compaction can separate a function call from a non-adjacent result, especially when completed rounds reuse the
same call_id. That can leave provider-invalid transcripts after sliding-window or incremental compaction.

Description & Review Guide

  • What are the major changes?
    • Pair non-adjacent call/result spans by ordered, unambiguous occurrence.
    • Re-annotate enough prior history when a newly appended result completes an older call.
    • Preserve ambiguity safeguards when several unmatched declarations share one id.
    • Keep completed reused-id rounds atomic under sliding-window compaction.
  • What is the impact of these changes?
    • Compaction no longer retains a result while dropping its matching declaration.
    • Legitimate reused ids remain separate logical rounds.
    • Ambiguous transcripts are not guessed into incorrect pairs.
  • What do you want reviewers to focus on?
    • Ordered occurrence matching and incremental reannotation boundaries.
    • The distinction between completed reused-id rounds and genuinely ambiguous duplicates.

Related Issue

Fixes #7212

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 20:10
@agent-framework-automation agent-framework-automation Bot added documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python labels Jul 29, 2026
@eavanvalkenburg
eavanvalkenburg marked this pull request as ready for review July 29, 2026 20:12
@github-actions

github-actions Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework
   _compaction.py8096192%137–138, 146, 173, 264–265, 283–284, 301, 336, 350, 357, 372–373, 427, 434, 450, 500, 536, 538, 558, 600, 656, 662, 664, 683, 727–732, 744, 828, 830, 845, 890, 952, 1078, 1084–1088, 1091–1093, 1101, 1176, 1178, 1194, 1201, 1206, 1221, 1229, 1328, 1351, 1363, 1459, 1486, 1570
TOTAL45988447990% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
9488 34 💤 0 ❌ 0 🔥 2m 35s ⏱️

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 improves Python message compaction correctness by ensuring function-call declarations and their tool results remain grouped atomically even when the result is non-adjacent (e.g., approval/resume traffic in between) and when call_id values are legitimately reused across completed rounds. It updates both the compaction implementation and its specification/tests to prevent provider-invalid transcripts caused by orphaned call/result halves.

Changes:

  • Link non-adjacent function_call / function_result occurrences by ordered, unambiguous call_id matching, including merging groups when a single tool message returns multiple results.
  • Expand incremental re-annotation boundaries when newly appended tool results complete earlier declarations, and ensure linked groups are kept/excluded atomically.
  • Add comprehensive unit tests and update docs/spec text to cover non-adjacent pairing, reused ids, and ambiguity safeguards.

Reviewed changes

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

File Description
python/packages/core/agent_framework/_compaction.py Implements unambiguous non-adjacent pairing/linking for tool call/result spans and adjusts incremental group re-annotation to preserve atomicity.
python/packages/core/tests/core/test_compaction.py Adds tests covering non-adjacent pairing, multiple results, reused call_id occurrences, ambiguity handling, incremental extension behavior, and sliding-window behavior.
python/packages/core/AGENTS.md Documents the new “ordered unambiguous occurrence” rule for reused call_id pairing during compaction.
docs/specs/004-python-function-calling-loop.md Updates the spec’s compaction integrity coverage references and tracking list to reflect the new behavior and tests.

Comment thread python/packages/core/agent_framework/_compaction.py Outdated

@github-actions github-actions Bot 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.

Automated Code Review

Reviewers: 5 | Confidence: 68% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Failure Modes, Design Approach


Automated review by eavanvalkenburg's agents

@eavanvalkenburg
eavanvalkenburg force-pushed the python-compaction-call-result-occurrences branch from cdf162d to c86cfc2 Compare July 30, 2026 06:59
Comment thread python/packages/core/agent_framework/_compaction.py Outdated
eavanvalkenburg and others added 3 commits July 30, 2026 09:18
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 1ee1d250-d1e2-4c6f-8c36-aae0d94fe7a1
Document why incremental reannotation retains all prior duplicate candidates and strengthen the regression that keeps ambiguous results unpaired without changing existing groups.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 1ee1d250-d1e2-4c6f-8c36-aae0d94fe7a1
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 1ee1d250-d1e2-4c6f-8c36-aae0d94fe7a1
@eavanvalkenburg
eavanvalkenburg force-pushed the python-compaction-call-result-occurrences branch from c86cfc2 to 9414007 Compare July 30, 2026 07:41
@eavanvalkenburg
eavanvalkenburg added this pull request to the merge queue Jul 30, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 30, 2026
@eavanvalkenburg
eavanvalkenburg added this pull request to the merge queue Jul 30, 2026
Merged via the queue into microsoft:main with commit 572a962 Jul 30, 2026
38 checks passed
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.

Python: [Bug]: compaction orphans function_call/result pairs when declaration and result are non-adjacent (group pairing is adjacency-only)

4 participants