fix: AG2 single-agent coordination issues#804
Conversation
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📝 WalkthroughWalkthroughAdded a coordination-aware single-agent streaming execution path in AG2Adapter that handles INITIAL_ANSWER, ENFORCEMENT, and PRESENTATION stages; updated streaming cleanup to only unregister workflow tools in group-chat mode. Orchestrator vote handling now accepts a single-agent vote when restart_pending exists but the agent already has an answer. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant AG2Adapter
participant Orchestrator
participant Tools
Client->>AG2Adapter: start streaming single-agent run (messages, tools)
AG2Adapter->>AG2Adapter: _execute_single_agent_with_coordination()
AG2Adapter->>AG2Adapter: INITIAL_ANSWER -> store last_answer
AG2Adapter->>Tools: ENFORCEMENT -> synthesize vote tool call (self)
Tools->>Orchestrator: submit vote
Orchestrator->>Orchestrator: if single-agent & restart_pending & has_answer -> clear restart_pending, accept vote
Orchestrator->>AG2Adapter: vote accepted / proceed
AG2Adapter->>Client: PRESENTATION -> stream final chunks
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 5 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
38825f0 to
9f7afb9
Compare
d4bd679 to
b75cec4
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@massgen/adapters/ag2_adapter.py`:
- Around line 330-352: The docstring is out of sync with the implementation:
when coordination_stage == CoordinationStage.ENFORCEMENT the code synthesizes a
"vote" tool call rather than a "new_answer" and never uses the stored
_last_single_agent_answer; update the docstring near coordination_stage handling
to describe that ENFORCEMENT synthesizes a "vote" tool call for a single agent
(including the reason text) and either (A) change the implementation to
synthesize a "new_answer" tool call that includes _last_single_agent_answer if
that behavior is intended, or (B) keep the current "vote" behavior and
remove/clarify the stored _last_single_agent_answer usage (or reference it in
the vote payload) so code and docstring match (refer to coordination_stage,
CoordinationStage.ENFORCEMENT, _last_single_agent_answer, and the
vote/new_answer tool call).
🧹 Nitpick comments (2)
massgen/adapters/ag2_adapter.py (2)
306-308: Unusedtoolsparameter.The
toolsparameter is declared but never used in this method. Either prefix it with underscore to indicate it's intentionally unused, or remove it if not needed.Suggested fix
async def _execute_single_agent_with_coordination( - self, messages: List[Dict[str, Any]], tools: List[Dict[str, Any]] + self, messages: List[Dict[str, Any]], _tools: List[Dict[str, Any]] ) -> AsyncGenerator[StreamChunk, None]:
326-326: Consider initializing_last_single_agent_answerin__init__.The attribute
_last_single_agent_answeris dynamically created (line 335) and checked withhasattrhere. For better clarity and IDE support, consider initializing it in__init__or_setup_single_agent.Suggested initialization in _setup_single_agent
def _setup_single_agent(self): """Set up a single AG2 agent.""" self.agent = setup_agent_from_config(self.agent_config) - self.is_group_chat = False + self._last_single_agent_answer: str | None = NoneThen update the check:
- {"stage": str(self.coordination_stage), "has_stored_answer": hasattr(self, "_last_single_agent_answer")}, + {"stage": str(self.coordination_stage), "has_stored_answer": self._last_single_agent_answer is not None},
Three bugs were preventing AG2 single-agent configs from working: 1. AttributeError in ag2_adapter.py - `unregister_tools_for_agent` was called unconditionally but `workflow_tools` and `user_agent` only exist in group chat mode. Now guarded with `if self.is_group_chat`. 2. Missing coordination stage handling - AG2 single-agent mode didn't handle MassGen's coordination stages (INITIAL_ANSWER, ENFORCEMENT, PRESENTATION). Added `_execute_single_agent_with_coordination()` that synthesizes a vote during ENFORCEMENT phase. 3. Vote rejection for single agent - Orchestrator was ignoring votes from agents with `restart_pending=True`, but single agents always had this flag set after producing their answer. Added exception to accept votes from single agents that have already produced an answer. Tested with ag2_coder.yaml config - code execution and coordination now work correctly.
b75cec4 to
fd8be4e
Compare
Summary
workflow_toolsnot defined)Test plan
ag2_coder.yamlconfiguv run python -m massgen.cli --automation --config massgen/configs/ag2/ag2_coder.yaml "Create a Python function that calculates factorial of 5 and print the result"Changes
massgen/adapters/ag2_adapter.py_execute_single_agent_with_coordination()method to handle MassGen coordination stagesunregister_tools_for_agentcall withif self.is_group_chatmassgen/orchestrator.pySummary by CodeRabbit
Bug Fixes
New Features
✏️ Tip: You can customize this high-level summary in your review settings.