Repository navigation
Python: fix(python): preserve non-adjacent tool pairs during history truncation - #14658
hylin (linhongyu510) wants to merge 2 commits into
Conversation
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 <linhongyu510@gmail.com>
3cd8e4b to
4c489f2
Compare
|
Rebased onto upstream main snapshot Local verification on this rebased HEAD (
|
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
| for call_index, result_index in get_call_result_pairs(history): | ||
| suffix_min_call_index[result_index] = min(suffix_min_call_index[result_index], call_index) |
There was a problem hiding this comment.
Fixed in c814f99. Removed the early break so every FunctionResultContent item in a message is collected; del call_ids_seen[call_id] already prevents double-matching.
Two new test files: test_chat_history_multi_result_pairing.py (internal helper, 2 cases) and test_chat_history_truncation_reversed_results.py — a public ChatHistoryTruncationReducer regression where calls sit at different message indices (c2 at msg1, c3 at msg3) with results reversed in one tool message, orphaning the earlier call on base.
Local validation on the source-equivalent blob: focused 11 passed (3 new + 8 existing); base ablation 2 fail / 1 pass; restore 11 passed; full contents 428 passed, 66 warnings. Ruff passed on the three changed files; an independent typecheck of the changed production file passed. The earlier producer package-wide check covered 556 files (not independently rerun). Warning set unchanged from base. AI assistance was used.
| break | ||
| if required_call_index < offset_count: | ||
| return None | ||
| candidate_index = required_call_index |
There was a problem hiding this comment.
Investigated the matched-pair invariant: for each matched FunctionResultContent retained in the tail, its paired call is also retained. The local check used one history shape (an adjacent call/result block plus a non-adjacent pair) at target_count 2/3/4/5, with zero matched-pair orphans.
These checks cover ID-matched FunctionResultContent pairs, not the text-only TOOL result in the example you provided. They therefore do not establish that adjacent-block preservation remains intact after the closure moves the cut backward. I have not yet validated that text-only TOOL case or made an adjacency fix for this point, and am leaving this conversation unresolved rather than treating the limited checks as evidence that the concern is invalid. AI assistance was used.
…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.

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