Skip to content

Conversation

@lbbniu
Copy link

@lbbniu lbbniu commented Dec 9, 2025

Fixes #2711

Summary

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.

Problem

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

Testing

  • ✅ All handoff tests pass (20/20)
  • ✅ All checkpoint tests pass (47/47)

…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
Copilot AI review requested due to automatic review settings December 9, 2025 03:36
Copy link
Contributor

Copilot AI left a 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.
Copy link
Contributor

@moonbox3 moonbox3 left a 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

@markwallace-microsoft
Copy link
Member

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework/_workflows
   _handoff.py52513574%58, 71–73, 80–81, 83, 85, 159, 167–172, 175–176, 190, 199, 218–221, 230–232, 243, 246, 256–267, 269, 275, 281, 338, 384, 477, 486–491, 493–494, 505, 530, 548, 563, 575, 603, 615–617, 623, 647–649, 652–655, 657–659, 902, 908, 912, 918, 922, 934, 984, 987, 1075, 1080, 1090, 1096–1099, 1107–1108, 1112–1114, 1116–1126, 1128–1129, 1131, 1133, 1148–1149, 1152–1153, 1156, 1176–1182, 1184, 1190, 1224–1225, 1278–1279, 1364, 1525, 1533, 1605, 1617, 1621, 1626–1627
TOTAL16295254684% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
2325 130 💤 0 ❌ 0 🔥 57.613s ⏱️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: Duplicate Tool Result Messages in Handoff Workflow

3 participants