KEMBAR78
Fix GraphFlowManager termination to prevent _StopAgent from polluting conversation context by Copilot · Pull Request #6752 · microsoft/autogen · GitHub
Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jul 6, 2025

Problem

The GraphFlowManager was allowing _StopAgent to speak and add termination messages to the conversation context, which polluted the message history that other agents could see. This violated the principle of clean agent interactions.

Before this fix:

# _StopAgent would produce messages like this in the conversation:
result.messages = [
    TextMessage(source="user", content="Task"),
    TextMessage(source="A", content="Response from A"),
    TextMessage(source="B", content="Response from B"), 
    StopMessage(source="DiGraphStopAgent", content="Digraph execution is complete")  # ❌ Pollutes context
]

After this fix:

# Clean conversation with no stop agent messages:
result.messages = [
    TextMessage(source="user", content="Task"),
    TextMessage(source="A", content="Response from A"),
    TextMessage(source="B", content="Response from B")  # ✅ Clean context
]
# Termination still occurs but without polluting conversation

Solution

  1. Removed _StopAgent entirely - No longer added to participants list
  2. Updated GraphFlowManager.select_speaker() - Directly signals termination when self._ready is empty instead of selecting stop agent
  3. Simplified GraphFlow constructor - Removed StopMessageTermination and stop agent logic
  4. Preserved all functionality - Same termination behavior and message content, just without the polluting agent message

Key Changes

  • GraphFlowManager.select_speaker(): Now handles termination directly when no ready agents remain
  • GraphFlow.__init__(): No longer adds _StopAgent to participants or uses StopMessageTermination
  • Removed unused code: Deleted _StopAgent class and cleaned up imports
  • Updated tests: All tests now expect clean conversation history without stop agent messages

Benefits

Clean conversation context - No stop agent messages pollute agent contexts
Same termination behavior - Graph execution still stops correctly when complete
Preserved functionality - All existing features work exactly the same
Better performance - One fewer participant to manage
Cleaner codebase - Removed unnecessary _StopAgent complexity

Verification

Created comprehensive tests that verify:

  • No StopMessage instances appear in conversation history
  • Agents don't see stop agent messages in their context
  • Termination still occurs at the correct time
  • All existing functionality preserved (resume, state management, etc.)

Fixes #6751.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: ekzhu <320302+ekzhu@users.noreply.github.com>
Copilot AI changed the title [WIP] GraphFlowManager should not let _StopAgent speak and apply termination if no more ready agent Fix GraphFlowManager termination to prevent _StopAgent from polluting conversation context Jul 6, 2025
Copilot finished work on behalf of ekzhu July 6, 2025 07:12
Copilot AI requested a review from ekzhu July 6, 2025 07:12
… in select_speaker

Co-authored-by: ekzhu <320302+ekzhu@users.noreply.github.com>
Copilot AI requested a review from ekzhu July 6, 2025 07:28
Copilot finished work on behalf of ekzhu July 6, 2025 07:28
- Prioritize graph completion check before standard termination conditions
- Properly reset termination conditions and turn count when graph completes
- Fix whitespace in docstring to satisfy linting

Co-authored-by: ekzhu <320302+ekzhu@users.noreply.github.com>
Copilot AI requested a review from ekzhu July 6, 2025 07:37
Copilot finished work on behalf of ekzhu July 6, 2025 07:37
@ekzhu ekzhu marked this pull request as ready for review July 6, 2025 07:41
@codecov
Copy link

codecov bot commented Jul 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.26%. Comparing base (c23b945) to head (f4e17de).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6752      +/-   ##
==========================================
- Coverage   80.27%   80.26%   -0.02%     
==========================================
  Files         232      232              
  Lines       17651    17641      -10     
==========================================
- Hits        14170    14160      -10     
  Misses       3481     3481              
Flag Coverage Δ
unittests 80.26% <100.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ekzhu ekzhu enabled auto-merge (squash) July 6, 2025 07:56
@ekzhu ekzhu disabled auto-merge July 6, 2025 07:56
@ekzhu ekzhu merged commit e107674 into main Jul 6, 2025
68 checks passed
@ekzhu ekzhu deleted the copilot/fix-6751 branch July 6, 2025 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GraphFlowManager should not let _StopAgent speak and apply termination if no more ready agent

2 participants