Repository navigation
Python: fix(python): preserve non-adjacent tool pairs during history truncation - #14658
Merged
Eduard van Valkenburg (eavanvalkenburg) merged 5 commits intoNov 2, 2026
Conversation
hylin (linhongyu510)
temporarily deployed
to
github-app-auth
October 24, 2026 07:04 — with
GitHub Actions
Inactive
Preserve matched FunctionCallContent and FunctionResultContent pairs when ChatHistoryTruncationReducer cuts between a call and its result. Co-authored-by: AI assistance was used and reviewed. Signed-off-by: hylin <[email protected]>
hylin (linhongyu510)
force-pushed
the
fix/nonadjacent-tool-pair-truncation
branch
from
October 27, 2026 17:14
3cd8e4b to
4c489f2
Compare
hylin (linhongyu510)
temporarily deployed
to
github-app-auth
October 27, 2026 17:14 — with
GitHub Actions
Inactive
Contributor
Author
|
Rebased onto upstream main snapshot Local verification on this rebased HEAD (
|
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
History truncation can still orphan tool results because only the first matched result per message is collected and backward cut adjustments can split adjacent tool blocks.
2 open issues
What changed in this PR
This PR updates Python chat-history truncation to retain matching function calls when their results appear later, separated by other messages.
Changes:
- Adds matched-ID boundary adjustment after existing adjacency and user-threshold selection.
- Handles chained pair dependencies and rejects cuts that require crossing the offset boundary.
- Adds regression tests for pair preservation, thresholds, offsets, and system-message retention.
| File | Description |
|---|---|
python/tests/unit/contents/test_chat_history_truncation_pair_invariant.py |
Adds focused truncation and pair-preservation regression tests. |
python/semantic_kernel/contents/history_reducer/chat_history_reducer_utils.py |
Adjusts truncation boundaries using suffix minima of matched call indices. |
Review effort: Balanced
…t_pairs Remove the early break that skipped subsequent FunctionResultContent items in a single tool message. When parallel tool calls share one result message with results ordered differently, the break caused earlier call results to be orphaned from pair tracking, which could leave a result in the retained tail without its call after truncation. Adds regression tests for multiple results per message (internal) and a public ChatHistoryTruncationReducer test demonstrating the orphan with reversed result ordering. Responds to Copilot review r4415468144 on PR microsoft#14658.
hylin (linhongyu510)
temporarily deployed
to
github-app-auth
October 31, 2026 07:55 — with
GitHub Actions
Inactive
…lock split When the ID-closure pass moves the cut back to keep a matched pair, the new candidate may land inside an adjacent call/result block — e.g. a TOOL message carrying plain text with no FunctionResultContent for the earlier call. Re-walk adjacency from the candidate and repeat ID closure until a fixed point, so a text-only TOOL result is never orphaned from its call. Covers Copilot review r4415468227 (text-only TOOL orphan) and a chained fixed-point case. Existing pair-invariant controls unchanged.
hylin (linhongyu510)
temporarily deployed
to
github-app-auth
November 1, 2026 07:25 — with
GitHub Actions
Inactive
…den LF content Follow-up to 0152f3c: the inline transcription drifted byte-wise from the golden LF source (blob mismatch). Replace with the exact verified content. No logic change.
hylin (linhongyu510)
temporarily deployed
to
github-app-auth
November 1, 2026 07:28 — with
GitHub Actions
Inactive
Clarify docstring by correcting phrasing regarding SUMMARY_METADATA_KEY and end index description.
hylin (linhongyu510)
temporarily deployed
to
github-app-auth
November 1, 2026 07:31 — with
GitHub Actions
Inactive
Eduard van Valkenburg (eavanvalkenburg)
approved these changes
Nov 2, 2026
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Title
fix(python): preserve non-adjacent tool pairs during history truncation
Motivation and Context
ChatHistoryTruncationReducercould retain aFunctionResultContentwhile dropping its matchingFunctionCallContentwhen the two were separated by non-function messages.Description
locate_safe_reduction_indexnow builds an O(n) suffix minimum-call map fromget_call_result_pairs(history). After the existing adjacency and threshold-window candidate selection, it moves the cut back only when a retained result requires an earlier matched call. Chained non-adjacent pairs are closed transitively. If the required call is beforeoffset_count, reduction returnsNonerather than crossing offset/summary territory.Offset, threshold, system/developer preservation, unknown/unpaired content, and existing user-boundary search semantics are otherwise unchanged.
Local verification
483ffb487a33a1644886992d56599feae3b69b76.4 failed, 4 passed.8 passed.1, restore green exit0.105 passed.0.9.10; check and format check pass.Contribution Checklist