fix(core): remove orphaned FunctionExecutionResultMessage after mid-list token truncation - #8035
Open
zerafachris (zerafachris) wants to merge 2 commits into
Conversation
…pply_filter The previous implementation iterated per_source filters and appended matching messages in filter-config order. When multiple sources were configured, messages from a later-listed source that chronologically preceded messages from an earlier-listed source were emitted out of order, breaking the LLM's conversation timeline. Fix: track the original message index for each selected message and emit in original conversation order. The per-source count/position filtering logic is unchanged; only the final order of the merged result is fixed. Fixes microsoft#7971 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ist truncation TokenLimitedChatCompletionContext.get_messages() removes messages from the middle of the list to stay within the token budget. When an AssistantMessage that carries FunctionCall items is removed, its paired FunctionExecutionResultMessage becomes orphaned and will cause an API error. The previous clean-up only caught the degenerate case where a FunctionExecutionResultMessage appeared at position 0 after truncation. Any orphaned result that landed elsewhere in the list was silently passed through. After the truncation loop, collect the call_ids of every FunctionCall that still exists in a surviving AssistantMessage, then filter out any FunctionExecutionResultMessage whose results reference a call_id that is no longer present (or that has no results at all). Fixes microsoft#7955. Prepared with AI assistance (Claude Code, Anthropic), reviewed for correctness before submission. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Author
|
I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer. |
Author
|
@microsoft-github-policy-service agree |
3 tasks
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.
Summary
TokenLimitedChatCompletionContext.get_messages()drops messages from the middle of the list to stay within the token budget. When a middle-listAssistantMessagethat carriesFunctionCallitems is removed, its pairedFunctionExecutionResultMessagebecomes orphaned and will cause an API error.Root cause
The previous clean-up guard was:
This only catches the degenerate case where an orphaned result ends up at position 0. Any orphaned result elsewhere in the list passed through silently.
Fix
After the truncation loop, collect the
call_ids of everyFunctionCallthat still exists in a survivingAssistantMessage, then filter out anyFunctionExecutionResultMessagewhose results reference acall_idthat is no longer present (or that has no results at all).Tests
Added a regression test (
test_token_limited_mid_list_orphaned_function_result_is_removed) that:AssistantMessage(content=[FunctionCall(id="call_1", ...)])at index 3 and a pairedFunctionExecutionResultMessageat index 4.token_limit=6so the truncation loop removes theAssistantMessage, orphaning the result.FunctionExecutionResultMessagesurvives in the returned list.All 10 existing tests in
test_model_context.pycontinue to pass.Closes #7955.