Commit 0c471504 by sunqi02

feat: 添加去重机制,防止同一子Agent重复调用,并更新测试用例

1 parent 240f2105
......@@ -672,6 +672,7 @@ class AgentHub:
room_id=room_id, user_id=user_id, bot_id_mapping=bot_id_mapping or {},
session_id=session_id,
call_count=0,
called_agents=set(),
))
session = self.session_service.get_or_create_session(
......@@ -1042,9 +1043,19 @@ class AgentHub:
f"正确示例:call_sub_agent(agent_code=\"{sub_example}\", instruction=\"请帮我处理任务\")"
))]
)
# 新增:递增调用计数
ctx = _route_context_var.get(None)
if ctx:
# 去重:同一 agent_code 只允许调用一次
called_agents = ctx.setdefault("called_agents", set())
if agent_code in called_agents:
return ToolResponse(
content=[TextBlock(type="text", text=(
f"错误:子 Agent '{agent_code}' 已被调用过,请勿重复调用。"
f"如需补充内容,请调用发言人Agent(调用码: -1)。"
))]
)
called_agents.add(agent_code)
ctx["call_count"] = ctx.get("call_count", 0) + 1
call_no = ctx["call_count"]
else:
......@@ -1108,6 +1119,10 @@ class AgentHub:
data["intent_session"] = []
self.session_service.store.set(session_id, data)
# 新消息可能携带新意图,重置去重状态,允许重新调度子Agent
ctx["called_agents"] = set()
ctx["call_count"] = 0
# 构造返回文本
lines = [f"检测到期间有 {len(intent_msgs)} 条新消息:"]
for i, m in enumerate(intent_msgs, 1):
......
......@@ -1036,6 +1036,57 @@ class TestCallSubAgentCounter:
finally:
_route_context_var.reset(token)
@pytest.mark.asyncio
async def test_dedup_rejects_repeated_agent_code(self):
"""同一 agent_code 第二次调用返回去重错误。"""
hub = _make_test_hub()
token = _route_context_var.set(dict(
room_id="r1", user_id="u1", bot_id_mapping={},
call_count=0, called_agents=set(),
))
try:
with patch.object(hub, '_call_sub_agent', new=AsyncMock(return_value="子Agent结果")):
tool = hub._make_call_sub_agent_tool()
# 第一次调用成功
r1 = await tool(agent_code=MEETING_KEY, instruction="测试指令")
assert "错误" not in r1.content[0].get("text", "")
ctx = _route_context_var.get()
assert ctx["call_count"] == 1
assert MEETING_KEY in ctx["called_agents"]
# 第二次调用同一 agent_code → 去重拒绝
r2 = await tool(agent_code=MEETING_KEY, instruction="再试一次")
assert "已被调用过" in r2.content[0].get("text", "")
# call_count 不变
assert ctx["call_count"] == 1
finally:
_route_context_var.reset(token)
@pytest.mark.asyncio
async def test_dedup_allows_different_agent_code(self):
"""不同 agent_code 不受去重影响。"""
hub = _make_test_hub()
# 找另一个非 steward 的 agent code
sub_keys = [k for k in hub.agents if not hub.agents[k].is_steward and k != MEETING_KEY]
if not sub_keys:
pytest.skip("需要至少两个不同的子Agent")
other_key = sub_keys[0]
token = _route_context_var.set(dict(
room_id="r1", user_id="u1", bot_id_mapping={},
call_count=0, called_agents=set(),
))
try:
with patch.object(hub, '_call_sub_agent', new=AsyncMock(return_value="子Agent结果")):
tool = hub._make_call_sub_agent_tool()
r1 = await tool(agent_code=MEETING_KEY, instruction="任务一")
r2 = await tool(agent_code=other_key, instruction="任务二")
assert "已被调用过" not in r2.content[0].get("text", "")
ctx = _route_context_var.get()
assert ctx["call_count"] == 2
finally:
_route_context_var.reset(token)
class TestSubAgentPostReply:
"""测试子 Agent 独立发送 post_reply。"""
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!