fix(core): count dropped function call messages in head-and-tail skipped placeholder - #8070
Open
madanmishra1223 wants to merge 1 commit into
Open
fix(core): count dropped function call messages in head-and-tail skipped placeholder#8070madanmishra1223 wants to merge 1 commit into
madanmishra1223 wants to merge 1 commit into
Conversation
…eholder HeadAndTailChatCompletionContext.get_messages() drops a trailing tool call AssistantMessage from the head and a leading tool result message from the tail so the returned view never splits a function call pair. The "Skipped N messages." placeholder, however, was computed as len(messages) - head_size - tail_size, which ignores those two drops. When either guard fires, the placeholder undercounts by one or two and tells the model fewer messages were omitted than actually were. With 6 messages and head_size=tail_size=2, a head ending on a tool call and a tail starting on a tool result yield 2 retained messages but a placeholder reading "Skipped 2 messages." when 4 were left out. Derive the count from the retained head and tail lengths instead. The early return for the not-enough-messages case still uses the configured sizes, so contexts that need no truncation are unaffected. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Contributor
|
madanmishra1223 please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
Author
|
@microsoft-github-policy-service agree |
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.
Why are these changes needed?
HeadAndTailChatCompletionContext.get_messages()deliberately drops two kinds of message so the returned view never contains a dangling half of a function call pair:AssistantMessagewhose content is a list ofFunctionCalls, removed from the headFunctionExecutionResultMessage, removed from the tailThe
"Skipped N messages."placeholder that replaces the omitted middle was computed before those drops were taken into account:That expression only counts the messages between the head and tail windows. When either guard fires, one or two additional messages are dropped but never counted, so the placeholder understates how much history was removed — by one if a single guard fires, by two if both do.
The placeholder is a
UserMessagethat goes to the model, so this is wrong information in the prompt: the model is told less context was elided than actually was.Repro
6 messages,
head_size=2,tail_size=2, wheremessages[1]is a tool-callAssistantMessage(trimmed from the head) andmessages[4]is a tool-result message (trimmed from the tail):Two messages are kept out of six, so four were omitted — the placeholder says two.
What changed
Derive the count from the head and tail lists that are actually returned:
The early return for the "not enough messages to fill head and tail" case still uses the configured
head_size/tail_size, so a context that needs no truncation keeps returning all messages unchanged. Only the placeholder count changes, and only when a truncating call also dropped a function call message.After the fix the same repro reports
Skipped 4 messages.Checks
test_head_and_tail_model_context_placeholder_counts_dropped_function_call_messages, which covers the both-guards-fire path that existing tests did not reach. It fails onmainand passes with this change.pytest packages/autogen-core/tests— 227 passed.pytest packages/autogen-agentchat/tests/test_agent.py packages/autogen-agentchat/tests/test_declarative_components.py(the dependents that construct this context) — 14 passed.ruff format --check,ruff check, andmypyclean on both touched files.Related issue number
None — found by inspection.
Checks