fix(webui): deliver late subagent results as new turns (#4992)

This commit is contained in:
chengyongru
2026-07-22 23:04:36 +08:00
committed by GitHub
parent aa8387fb4d
commit 66690fdb0c
12 changed files with 740 additions and 218 deletions
+42
View File
@@ -689,6 +689,8 @@ async def test_waiting_dispatch_does_not_replace_active_pending_queue(tmp_path):
from nanobot.bus.events import InboundMessage
loop = _make_loop(tmp_path)
route_policy = MagicMock(side_effect=lambda _msg, _key, route: route)
loop.turn_delivery_factory.route_policy = route_policy
session_key = "cli:c"
lock = loop._session_locks.setdefault(session_key, asyncio.Lock())
await lock.acquire()
@@ -712,10 +714,12 @@ async def test_waiting_dispatch_does_not_replace_active_pending_queue(tmp_path):
await asyncio.wait_for(waiting_at_lock.wait(), timeout=2.0)
assert loop._pending_queues[session_key] is active_pending
route_policy.assert_not_called()
waiting.cancel()
with pytest.raises(asyncio.CancelledError):
await waiting
route_policy.assert_not_called()
lock.release()
@@ -746,6 +750,44 @@ async def test_followup_routed_to_pending_queue(tmp_path):
assert queued_msg.session_key == UNIFIED_SESSION_KEY
@pytest.mark.asyncio
async def test_mid_turn_subagent_result_does_not_resolve_a_new_turn_route(tmp_path):
"""Injected results stay inside the active turn instead of opening a side turn."""
from nanobot.bus.events import InboundMessage
loop = _make_loop(tmp_path)
loop._dispatch = AsyncMock() # type: ignore[method-assign]
route_policy = MagicMock(side_effect=lambda _msg, _key, route: route)
loop.turn_delivery_factory.route_policy = route_policy
session_key = "websocket:chat-1"
pending = asyncio.Queue(maxsize=20)
loop._pending_queues[session_key] = pending
run_task = asyncio.create_task(loop.run())
msg = InboundMessage(
channel="system",
sender_id="subagent",
chat_id=session_key,
content="background result",
metadata={
"injected_event": "subagent_result",
"subagent_task_id": "sub-1",
},
session_key_override=session_key,
)
await loop.bus.publish_inbound(msg)
queued_msg = await asyncio.wait_for(pending.get(), timeout=2)
loop.stop()
await asyncio.wait_for(run_task, timeout=2)
assert queued_msg is msg
assert loop._dispatch.await_count == 0
route_policy.assert_not_called()
@pytest.mark.asyncio
async def test_cron_turn_deferred_while_session_active(tmp_path):
"""Cron turns wait for the active session instead of becoming injections."""