-
Notifications
You must be signed in to change notification settings - Fork 892
Python: Fix duplicate tool result messages in handoff workflow #2712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Python: Fix duplicate tool result messages in handoff workflow #2712
Conversation
…soft#2711) When using HandoffBuilder, the _HandoffCoordinator._append_tool_acknowledgement method was creating duplicate tool result messages in the conversation history. This occurred because the method didn't check if a tool result with the same call_id already existed before appending a new one. The duplication happened when: 1. _AutoHandoffMiddleware creates a synthetic tool result 2. ChatAgent appends this result to the conversation 3. _HandoffCoordinator._append_tool_acknowledgement creates another result with the same call_id but different author_name This led to: - Polluted conversation history - Unnecessary token usage - Increased checkpoint storage - Confusing debugging experience Changes: - Add _has_tool_result_for_call() module-level helper function to check if a tool result with the given call_id already exists in the conversation - Modify _append_tool_acknowledgement() to skip adding tool result if one already exists with the same call_id - Add debug logging when skipping duplicate tool acknowledgement Fixes microsoft#2711
There was a problem hiding this comment.
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 fixes a bug where duplicate tool result messages were being created in the conversation history when using HandoffBuilder. The fix adds deduplication logic to prevent the _HandoffCoordinator from appending a tool acknowledgement when one already exists with the same call_id.
- Introduces a helper function to check for existing tool results with the same
call_id - Modifies the tool acknowledgement logic to skip duplicates created by
_AutoHandoffMiddleware - Adds debug logging when skipping duplicate tool acknowledgements
…off workflow This commit addresses Copilot's review comment on PR microsoft#2711 by adding comprehensive test coverage for the duplicate detection logic introduced in commit d2e9776. Test coverage includes: 1. Verification that _has_tool_result_for_call() correctly detects existing tool results 2. Verification that _has_tool_result_for_call() returns False for non-existent call_ids 3. Verification that _append_tool_acknowledgement() skips adding duplicates 4. Verification that debug log messages are emitted when duplicates are detected 5. Verification that tool results are added when no duplicate exists The test ensures that the fix for issue microsoft#2711 (preventing duplicate tool result messages in the conversation history) remains effective and prevents regression.
moonbox3
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for fixing, @lbbniu
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
Fixes #2711
Summary
When using HandoffBuilder, the
_HandoffCoordinator._append_tool_acknowledgementmethod was creating duplicate tool result messages in the conversation history. This occurred because the method didn't check if a tool result with the samecall_idalready existed before appending a new one.Problem
The duplication happened when:
_AutoHandoffMiddlewarecreates a synthetic tool resultChatAgentappends this result to the conversation_HandoffCoordinator._append_tool_acknowledgementcreates another result with the samecall_idbut differentauthor_nameThis led to:
Changes
_has_tool_result_for_call()module-level helper function to check if a tool result with the given call_id already exists in the conversation_append_tool_acknowledgement()to skip adding tool result if one already exists with the same call_idTesting