refactor(agent): centralize session execution ownership (#6699)

This commit is contained in:
chengyongru authored and GitHub committed 2026-11-18 13:49:41 +08:00
1 parent 53a556585e
commit 5997a714f2
16 files changed
+642 -286

No files matched your search

+271 -8
View File
@@ -7,6 +7,7 @@ import pytest
from agent.session_helpers import run_session, stop_loop, visible_message
from nanobot.agent.automation_turns import AutomationTurnError
from nanobot.agent.loop import AgentLoop
from nanobot.agent.session_inbox import SessionInbox
from nanobot.agent.tools.context import current_request_context
from nanobot.agent.tools.cron import CronTool
from nanobot.bus.events import InboundMessage, OutboundMessage
@@ -315,9 +316,8 @@ def _automation_message(kind: str, name: str) -> InboundMessage:
@pytest.mark.parametrize("kind", ["cron", "local_trigger"])
async def test_automation_turn_is_deferred_while_session_active(loop, kind):
key = "websocket:test"
pending = asyncio.Queue()
loop._pending_queues[key] = pending
coordinator = loop._cron_turns if kind == "cron" else loop._local_trigger_turns
pending = SessionInbox()
loop._pending_queues.install(key, pending)
pending_ids = (
loop.pending_cron_job_ids_for_session
if kind == "cron"
@@ -329,17 +329,17 @@ async def test_automation_turn_is_deferred_while_session_active(loop, kind):
try:
await loop.bus.publish_inbound(msg)
async with asyncio.timeout(3):
while not coordinator.deferred_queues.get(key):
while not pending.deferred:
await asyncio.sleep(0)
assert pending.empty()
assert loop._active_tasks == {}
assert coordinator.deferred_queues[key] == [msg]
assert pending.deferred == (msg,)
assert pending_ids(key) == {"deferred"}
loop._process_message = AsyncMock(return_value=None)
await loop._run_session_queue(key, pending)
assert loop._process_message.await_args.args[0] is msg
assert key not in coordinator.deferred_queues
assert not pending.deferred
assert pending_ids(key) == set()
finally:
loop.stop()
@@ -466,7 +466,7 @@ async def test_cancelled_session_completes_queued_automation_waiters(loop):
))
tasks.extend([cron, trigger])
await asyncio.sleep(0)
assert len(loop._deferred_automation_turns["websocket:test"]) == 2
assert len(loop._pending_queues["websocket:test"].deferred) == 2
worker.cancel()
with pytest.raises(asyncio.CancelledError):
await worker
@@ -583,6 +583,270 @@ async def test_cancel_preserves_followups_accepted_after_cancellation_started(lo
)] == ["new followup"]
@pytest.fixture
def handoff_started(loop, monkeypatch):
"""Exercise the real dispatcher and inbox without waiting on turn storage."""
started = asyncio.Event()
async def process_message(msg, **_kwargs):
if msg.content == "original turn":
started.set()
await asyncio.Event().wait()
return OutboundMessage(channel=msg.channel, chat_id=msg.chat_id, content="automation finished")
monkeypatch.setattr(loop.sessions.state, "read", AsyncMock(return_value=None))
monkeypatch.setattr(loop, "_process_message", process_message)
return started
@pytest.mark.parametrize("kind", ["cron", "local_trigger"])
async def test_stop_does_not_strand_automation_submitted_during_cleanup(
loop, monkeypatch, kind, handoff_started,
):
cleaning_exec = asyncio.Event()
submit_turn = loop.submit_cron_turn if kind == "cron" else loop.submit_local_trigger_turn
cancel_launches = loop._exec_session_manager._cancel_launches
async def observe_launch_cleanup(launches):
# Wake the submitter from the real exec cleanup without adding a
# suspension that could hide the late queue removal this test catches.
cleaning_exec.set()
return await cancel_launches(launches)
monkeypatch.setattr(loop._exec_session_manager, "_cancel_launches", observe_launch_cleanup)
async with asyncio.timeout(5), asyncio.TaskGroup() as tasks:
await loop._dispatch_inbound(InboundMessage(
channel="websocket", sender_id="user", chat_id="test", content="original turn",
))
await handoff_started.wait()
stop = tasks.create_task(loop._dispatch_inbound(InboundMessage(
channel="websocket", sender_id="user", chat_id="test", content="/stop",
)))
await cleaning_exec.wait()
submitted = tasks.create_task(submit_turn(_automation_message(kind, "new work")))
await stop
while True:
reply = await loop.bus.consume_outbound()
if reply.event is None and reply.content.startswith("Stopped "):
break
result = await submitted
assert result is not None and result.content == "automation finished"
async def test_automation_arriving_during_queue_cancellation_is_not_stranded(loop, handoff_started):
notifying = asyncio.Event()
release_notification = asyncio.Event()
async def observe_completion(event):
if event.context.metadata.get("message_id") == "queued-compact":
notifying.set()
await release_notification.wait()
loop.bus.subscribe(observe_completion, TurnCompleted)
async with asyncio.timeout(5), asyncio.TaskGroup() as tasks:
await loop._dispatch_inbound(InboundMessage(
channel="websocket", sender_id="user", chat_id="test", content="original turn",
))
await handoff_started.wait()
await loop._dispatch_inbound(InboundMessage(
channel="websocket", sender_id="user", chat_id="test", content="/compact",
metadata={"message_id": "queued-compact"},
))
stop = tasks.create_task(loop._dispatch_inbound(InboundMessage(
channel="websocket", sender_id="user", chat_id="test", content="/stop",
)))
try:
await notifying.wait()
msg = _automation_message("cron", "after queue cancellation")
submitted = tasks.create_task(loop.submit_cron_turn(msg))
while "after queue cancellation" not in loop.pending_cron_job_ids_for_session(msg.session_key):
await asyncio.sleep(0)
finally:
release_notification.set()
await stop
while True:
reply = await loop.bus.consume_outbound()
if reply.event is None and reply.content.startswith("Stopped "):
break
result = await submitted
assert result is not None and result.content == "automation finished"
async def test_shutdown_rejects_automation_before_starting_cleanup(loop):
started = asyncio.Event()
cleaning = asyncio.Event()
release = asyncio.Event()
async def chat(**_kwargs):
if not started.is_set():
started.set()
try:
await asyncio.Event().wait()
finally:
cleaning.set()
await release.wait()
return LLMResponse(content="too late to start")
loop.provider.chat_stream_with_retry = chat
await loop._enqueue_session_message(InboundMessage(
channel="websocket", sender_id="user", chat_id="already-active", content="original turn",
))
await asyncio.wait_for(started.wait(), timeout=3)
close = asyncio.create_task(loop.aclose())
try:
await asyncio.wait_for(cleaning.wait(), timeout=3)
# The snapshot contains another session; both same-key and cross-session
# submitters must see the closed runtime, not just its active inboxes.
with pytest.raises(RuntimeError, match="Session execution is closing"):
await asyncio.wait_for(loop.submit_local_trigger_turn(
_automation_message("local_trigger", "during shutdown"),
), timeout=3)
finally:
release.set()
await asyncio.wait_for(close, timeout=3)
async def test_shutdown_waits_for_worker_already_cancelling_from_stop(loop, monkeypatch):
started = asyncio.Event()
cancelling = asyncio.Event()
release = asyncio.Event()
closing_resources = asyncio.Event()
close_subagents = loop.subagents.close
async def chat(**_kwargs):
started.set()
try:
await asyncio.Event().wait()
finally:
cancelling.set()
await release.wait()
async def observe_close():
closing_resources.set()
await close_subagents()
loop.provider.chat_stream_with_retry = chat
monkeypatch.setattr(loop.subagents, "close", observe_close)
dispatcher = asyncio.create_task(loop.run())
close = None
try:
async with asyncio.timeout(5):
await loop.bus.publish_inbound(InboundMessage(
channel="websocket", sender_id="user", chat_id="test", content="original turn",
))
await started.wait()
await loop.bus.publish_inbound(InboundMessage(
channel="websocket", sender_id="user", chat_id="test", content="/stop",
))
await cancelling.wait()
close = asyncio.create_task(loop.aclose())
# With no other workers or background tasks, a close that does not
# join this worker goes straight to shared resource teardown.
await asyncio.sleep(0)
assert not closing_resources.is_set()
finally:
release.set()
if close is not None:
await asyncio.wait_for(close, timeout=3)
await stop_loop(loop, dispatcher, timeout=3)
assert closing_resources.is_set()
async def test_replacement_waits_for_same_session_resource_cleanup(loop, monkeypatch):
started = asyncio.Event()
cleaning = asyncio.Event()
release = asyncio.Event()
cleanup_at_dispatch = []
dispatch_one = loop._dispatch_one
cancel_by_session = loop.subagents.cancel_by_session
async def chat(**_kwargs):
if not started.is_set():
started.set()
await asyncio.Event().wait()
return LLMResponse(content="automation finished")
async def observe_dispatch(msg, pending):
if msg.content == "same session":
cleanup_at_dispatch.append(release.is_set())
await dispatch_one(msg, pending)
async def blocked_cleanup(key):
cleaning.set()
await release.wait()
return await cancel_by_session(key)
loop.provider.chat_stream_with_retry = chat
monkeypatch.setattr(loop, "_dispatch_one", observe_dispatch)
monkeypatch.setattr(loop.subagents, "cancel_by_session", blocked_cleanup)
dispatcher = asyncio.create_task(loop.run())
submitted = None
try:
async with asyncio.timeout(5):
await loop.bus.publish_inbound(InboundMessage(
channel="websocket", sender_id="user", chat_id="test", content="original turn",
))
await started.wait()
await loop.bus.publish_inbound(InboundMessage(
channel="websocket", sender_id="user", chat_id="test", content="/stop",
))
await cleaning.wait()
submitted = asyncio.create_task(loop.submit_cron_turn(
_automation_message("cron", "same session"),
))
other = _automation_message("cron", "other session")
other.chat_id = "other"
assert (await loop.submit_cron_turn(other)).content == "automation finished"
release.set()
assert (await submitted).content == "automation finished"
assert cleanup_at_dispatch == [True]
finally:
release.set()
if submitted is not None:
submitted.cancel()
await asyncio.gather(submitted, return_exceptions=True)
await stop_loop(loop, dispatcher, timeout=3)
async def test_shutdown_joins_control_when_no_worker_remains(loop, monkeypatch):
cleaning = asyncio.Event()
release = asyncio.Event()
closing_resources = asyncio.Event()
seen_released = []
cancel_by_session = loop.subagents.cancel_by_session
close_subagents = loop.subagents.close
async def blocked_cleanup(key):
cleaning.set()
await release.wait()
return await cancel_by_session(key)
async def observe_close():
seen_released.append(release.is_set())
closing_resources.set()
await close_subagents()
monkeypatch.setattr(loop.subagents, "cancel_by_session", blocked_cleanup)
monkeypatch.setattr(loop.subagents, "close", observe_close)
dispatcher = asyncio.create_task(loop.run())
close = None
try:
async with asyncio.timeout(5):
await loop.bus.publish_inbound(InboundMessage(
channel="websocket", sender_id="user", chat_id="test", content="/stop",
))
await cleaning.wait()
close = asyncio.create_task(loop.aclose())
await asyncio.sleep(0)
assert not closing_resources.is_set()
finally:
release.set()
if close is not None:
await asyncio.wait_for(close, timeout=3)
await stop_loop(loop, dispatcher, timeout=3)
assert seen_released and all(seen_released)
@pytest.mark.parametrize("action", ["stop", "close"])
async def test_cancel_before_worker_runs_completes_automation(loop, action):
loop.provider.chat_stream_with_retry = AsyncMock(return_value=LLMResponse(content="unused"))
@@ -692,7 +956,6 @@ async def test_ingress_sources_share_one_session_worker(loop, bus_running):
assert len(set(workers)) == 1
assert loop.bus.inbound_size == 0
assert key not in loop._pending_queues
assert key not in loop._deferred_automation_turns
finally:
release.set()
for task in tasks: