refactor(agent): centralize session execution ownership (#6699)
This commit is contained in:
1 parent
53a556585e
commit
5997a714f2
16 files changed
+642
-286
No files matched your search
@@ -1,7 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import dataclasses
|
||||
from collections.abc import Awaitable, Callable, Iterable
|
||||
|
||||
from nanobot.bus.events import InboundMessage, OutboundMessage
|
||||
@@ -20,10 +19,9 @@ class AutomationTurnCoordinator:
|
||||
enqueue: Callable[[InboundMessage], Awaitable[None]],
|
||||
turn_id: Callable[[InboundMessage], str | None],
|
||||
pending_id: Callable[[InboundMessage], str | None],
|
||||
should_defer_turn: Callable[[InboundMessage, str, Iterable[str]], bool],
|
||||
should_defer_turn: Callable[[InboundMessage], bool],
|
||||
missing_id_error: str,
|
||||
duplicate_id_error: Callable[[str], str],
|
||||
deferred_queues: dict[str, list[InboundMessage]] | None = None,
|
||||
) -> None:
|
||||
self._enqueue = enqueue
|
||||
self._turn_id = turn_id
|
||||
@@ -31,7 +29,6 @@ class AutomationTurnCoordinator:
|
||||
self._should_defer_turn = should_defer_turn
|
||||
self._missing_id_error = missing_id_error
|
||||
self._duplicate_id_error = duplicate_id_error
|
||||
self.deferred_queues = deferred_queues if deferred_queues is not None else {}
|
||||
self._waiters: dict[str, asyncio.Future[OutboundMessage | None]] = {}
|
||||
self._pending_messages_by_turn_id: dict[str, InboundMessage] = {}
|
||||
|
||||
@@ -65,24 +62,9 @@ class AutomationTurnCoordinator:
|
||||
self._waiters.pop(turn_id, None)
|
||||
self._pending_messages_by_turn_id.pop(turn_id, None)
|
||||
|
||||
def defer_if_active(
|
||||
self,
|
||||
msg: InboundMessage,
|
||||
*,
|
||||
session_key: str,
|
||||
active_session_keys: Iterable[str],
|
||||
) -> bool:
|
||||
"""Defer an automation turn when its target session is already active."""
|
||||
if not self._should_defer_turn(msg, session_key, active_session_keys):
|
||||
return False
|
||||
pending_msg = msg
|
||||
if session_key != msg.session_key:
|
||||
pending_msg = dataclasses.replace(
|
||||
msg,
|
||||
session_key_override=session_key,
|
||||
)
|
||||
self.deferred_queues.setdefault(session_key, []).append(pending_msg)
|
||||
return True
|
||||
def should_defer(self, msg: InboundMessage) -> bool:
|
||||
"""Whether this turn waits for an already-active inbox to become idle."""
|
||||
return self._should_defer_turn(msg)
|
||||
|
||||
def complete(
|
||||
self,
|
||||
@@ -104,10 +86,12 @@ class AutomationTurnCoordinator:
|
||||
else:
|
||||
future.set_result(response)
|
||||
|
||||
def pending_ids_for_session(self, session_key: str) -> set[str]:
|
||||
def pending_ids_for_session(
|
||||
self, session_key: str, deferred: Iterable[InboundMessage],
|
||||
) -> set[str]:
|
||||
"""Return automation IDs that are waiting for or running in *session_key*."""
|
||||
pending_ids: set[str] = set()
|
||||
for msg in self.deferred_queues.get(session_key, []):
|
||||
for msg in deferred:
|
||||
pending_id = self._pending_id(msg)
|
||||
if pending_id:
|
||||
pending_ids.add(pending_id)
|
||||
|
||||
@@ -18,29 +18,21 @@ class CronTurnCoordinator(AutomationTurnCoordinator):
|
||||
self,
|
||||
*,
|
||||
enqueue: Callable[[InboundMessage], Awaitable[None]],
|
||||
deferred_queues: dict[str, list[InboundMessage]] | None = None,
|
||||
) -> None:
|
||||
super().__init__(
|
||||
enqueue=enqueue,
|
||||
turn_id=lambda msg: cron_run_id(msg.metadata),
|
||||
pending_id=_cron_job_id,
|
||||
should_defer_turn=_should_defer_cron_turn,
|
||||
should_defer_turn=lambda msg: defer_cron_until_session_idle(msg.metadata),
|
||||
missing_id_error="cron turn metadata must include a run_id",
|
||||
duplicate_id_error=lambda run_id: f"cron run {run_id!r} is already pending",
|
||||
deferred_queues=deferred_queues,
|
||||
)
|
||||
|
||||
def pending_job_ids_for_session(self, session_key: str) -> set[str]:
|
||||
def pending_job_ids_for_session(
|
||||
self, session_key: str, deferred: Iterable[InboundMessage],
|
||||
) -> set[str]:
|
||||
"""Return cron jobs that are waiting for or running in *session_key*."""
|
||||
return self.pending_ids_for_session(session_key)
|
||||
|
||||
|
||||
def _should_defer_cron_turn(
|
||||
msg: InboundMessage,
|
||||
session_key: str,
|
||||
active_session_keys: Iterable[str],
|
||||
) -> bool:
|
||||
return defer_cron_until_session_idle(msg.metadata) and session_key in active_session_keys
|
||||
return self.pending_ids_for_session(session_key, deferred)
|
||||
|
||||
|
||||
def _cron_job_id(msg: InboundMessage) -> str | None:
|
||||
|
||||
+61
-68
@@ -28,6 +28,7 @@ from nanobot.agent.memory import Consolidator
|
||||
from nanobot.agent.model_runtime import ModelRuntimeResolver
|
||||
from nanobot.agent.pending_inputs import PendingInputs
|
||||
from nanobot.agent.runner import AgentCheckpoint, AgentRunner, AgentRunResult, AgentRunSpec
|
||||
from nanobot.agent.session_inbox import SessionExecutionClosedError, SessionInbox, SessionInboxes
|
||||
from nanobot.agent.subagent import SubagentManager
|
||||
from nanobot.agent.tools.context import RequestContext, bind_request_context, reset_request_context
|
||||
from nanobot.agent.tools.exec_session import ExecSessionManager
|
||||
@@ -409,7 +410,6 @@ class AgentLoop:
|
||||
self._running = False
|
||||
self._runtime_context_providers: list[RuntimeContextProvider] = []
|
||||
self._active_tasks: dict[str, set[asyncio.Task[Any]]] = {}
|
||||
self._discarding_sessions: set[str] = set()
|
||||
self._background_tasks: set[asyncio.Task[Any]] = set()
|
||||
self._close_lock = asyncio.Lock()
|
||||
self._session_locks: weakref.WeakValueDictionary[str, asyncio.Lock] = (
|
||||
@@ -418,17 +418,10 @@ class AgentLoop:
|
||||
# A queue is installed before its single session worker is scheduled.
|
||||
# It remains the only admission path until that worker atomically
|
||||
# observes the queue empty and removes it.
|
||||
self._pending_queues: dict[str, asyncio.Queue[InboundMessage]] = {}
|
||||
self._pending_queues = SessionInboxes()
|
||||
self._preserve_inflight_turns_on_shutdown = False
|
||||
self._deferred_automation_turns: dict[str, list[InboundMessage]] = {}
|
||||
self._cron_turns = CronTurnCoordinator(
|
||||
enqueue=self._enqueue_session_message,
|
||||
deferred_queues=self._deferred_automation_turns,
|
||||
)
|
||||
self._local_trigger_turns = LocalTriggerTurnCoordinator(
|
||||
enqueue=self._enqueue_session_message,
|
||||
deferred_queues=self._deferred_automation_turns,
|
||||
)
|
||||
self._cron_turns = CronTurnCoordinator(enqueue=self._enqueue_session_message)
|
||||
self._local_trigger_turns = LocalTriggerTurnCoordinator(enqueue=self._enqueue_session_message)
|
||||
self._automation_turn_coordinators = (
|
||||
self._cron_turns,
|
||||
self._local_trigger_turns,
|
||||
@@ -690,10 +683,14 @@ class AgentLoop:
|
||||
return await self._local_trigger_turns.submit(msg)
|
||||
|
||||
def pending_cron_job_ids_for_session(self, session_key: str) -> set[str]:
|
||||
return self._cron_turns.pending_job_ids_for_session(session_key)
|
||||
inbox = self._pending_queues.get(session_key)
|
||||
return self._cron_turns.pending_job_ids_for_session(session_key, inbox.deferred if inbox else ())
|
||||
|
||||
def pending_local_trigger_ids_for_session(self, session_key: str) -> set[str]:
|
||||
return self._local_trigger_turns.pending_trigger_ids_for_session(session_key)
|
||||
inbox = self._pending_queues.get(session_key)
|
||||
return self._local_trigger_turns.pending_trigger_ids_for_session(
|
||||
session_key, inbox.deferred if inbox else (),
|
||||
)
|
||||
|
||||
async def _persist_user_message_early(
|
||||
self,
|
||||
@@ -899,17 +896,25 @@ class AgentLoop:
|
||||
if not tasks and self._active_tasks.get(key) is tasks:
|
||||
self._active_tasks.pop(key, None)
|
||||
|
||||
@staticmethod
|
||||
def _request_worker_cancellation(tasks: Iterable[asyncio.Task[Any]]) -> int:
|
||||
# A cancelling worker still owns its cleanup. Another controller should
|
||||
# join it instead of interrupting that cleanup with a second cancel().
|
||||
return sum(1 for task in tasks if not task.done() and not task.cancelling() and task.cancel())
|
||||
|
||||
async def _cancel_active_tasks(self, key: str) -> int:
|
||||
with self._pending_queues.controlling(key):
|
||||
return await self._cancel_session_work(key)
|
||||
|
||||
async def _cancel_session_work(self, key: str) -> int:
|
||||
pending = self._pending_queues.get(key)
|
||||
current = asyncio.current_task()
|
||||
tracked = self._active_tasks.get(key, set())
|
||||
# A command running on the session worker must keep its task and inbox
|
||||
# registered until the worker finishes; only other tasks can be stopped.
|
||||
owns_queue = current in tracked
|
||||
if not owns_queue:
|
||||
self._active_tasks.pop(key, None)
|
||||
tasks = tuple(task for task in tracked if task is not current)
|
||||
cancelled = sum(1 for t in tasks if not t.done() and t.cancel())
|
||||
cancelled = self._request_worker_cancellation(tasks)
|
||||
journal_session = await self.sessions.state.read(key)
|
||||
journaled_followup_ids = (
|
||||
tuple(
|
||||
@@ -923,17 +928,13 @@ class AgentLoop:
|
||||
if journal_session is not None
|
||||
else ()
|
||||
)
|
||||
for t in tasks:
|
||||
try:
|
||||
await t
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
if tasks:
|
||||
await asyncio.shield(asyncio.gather(*tasks, return_exceptions=True))
|
||||
for task in tasks:
|
||||
self._active_task_done(key, tracked, task)
|
||||
if tasks and not owns_queue and pending is not None and self._pending_queues.get(key) is pending:
|
||||
# A task cancelled before its first step never enters the worker's
|
||||
# cleanup handler. Only reclaim that worker's original inbox.
|
||||
self._pending_queues.pop(key, None)
|
||||
await self._cancel_pending_messages(key, pending, asyncio.CancelledError())
|
||||
if journaled_followup_ids and journal_session is not None:
|
||||
# Explicit session cancellation owns the follow-ups accepted before
|
||||
@@ -946,14 +947,12 @@ class AgentLoop:
|
||||
|
||||
async def discard_session(self, key: str) -> None:
|
||||
"""Stop active work for *key* and forget its cached session."""
|
||||
with self.bus.admission.activity():
|
||||
self._discarding_sessions.add(key)
|
||||
with self.bus.admission.activity(), self._pending_queues.controlling(key, discard=True):
|
||||
try:
|
||||
await self.sessions.state.discard(key)
|
||||
await self._cancel_active_tasks(key)
|
||||
await self._cancel_session_work(key)
|
||||
finally:
|
||||
self.discard_session_file_state(key)
|
||||
self._discarding_sessions.discard(key)
|
||||
|
||||
def discard_session_file_state(self, key: str) -> None:
|
||||
"""Forget ephemeral file-read state for a reset or removed session."""
|
||||
@@ -1345,6 +1344,8 @@ class AgentLoop:
|
||||
with self.bus.admission.activity():
|
||||
try:
|
||||
await self._dispatch_inbound(msg)
|
||||
except SessionExecutionClosedError:
|
||||
break
|
||||
except (SessionOperationQueueFullError, SessionConflictError, ExecutionControlError) as exc:
|
||||
key = msg.session_key
|
||||
logger.warning("Session input rejected for {}: {}", key, exc)
|
||||
@@ -1398,7 +1399,7 @@ class AgentLoop:
|
||||
|
||||
def _has_pending_work(self) -> bool:
|
||||
return bool(
|
||||
self.bus.inbound_size or self._pending_queues or self._deferred_automation_turns
|
||||
self.bus.inbound_size or self._pending_queues
|
||||
or self.bus.has_pending_events or self._active_tasks or self._background_tasks
|
||||
or self._close_lock.locked()
|
||||
or self.subagents.has_pending_work or self._exec_session_manager.has_pending_work
|
||||
@@ -1434,50 +1435,41 @@ class AgentLoop:
|
||||
return pending_msg
|
||||
|
||||
async def _enqueue_session_message(self, msg: InboundMessage) -> None:
|
||||
self._pending_queues.check_admission()
|
||||
self.bus.admission.check()
|
||||
session_key = msg.session_key
|
||||
if not self.subagents.accepts_result(msg, session_key):
|
||||
return
|
||||
|
||||
for coordinator in self._automation_turn_coordinators:
|
||||
if coordinator.defer_if_active(
|
||||
msg,
|
||||
session_key=session_key,
|
||||
active_session_keys=self._pending_queues.keys(),
|
||||
):
|
||||
return
|
||||
|
||||
if session_key in self._pending_queues:
|
||||
msg = await self._journal_pending_message(session_key, msg)
|
||||
pending = self._pending_queues.get(session_key)
|
||||
if pending is not None:
|
||||
if any(coordinator.should_defer(msg) for coordinator in self._automation_turn_coordinators):
|
||||
pending.defer(msg)
|
||||
return
|
||||
msg = await self._journal_pending_message(session_key, msg)
|
||||
self._pending_queues.check_admission()
|
||||
pending = self._pending_queues.get(session_key)
|
||||
if pending is not None:
|
||||
pending.put_nowait(msg)
|
||||
return
|
||||
|
||||
new_pending: asyncio.Queue[InboundMessage] = asyncio.Queue()
|
||||
new_pending = SessionInbox()
|
||||
new_pending.put_nowait(msg)
|
||||
self._pending_queues[session_key] = new_pending
|
||||
self._pending_queues.install(session_key, new_pending)
|
||||
task = asyncio.create_task(self._run_session_queue(session_key, new_pending))
|
||||
self._track_active_task(session_key, task)
|
||||
|
||||
async def _run_session_queue(
|
||||
self,
|
||||
session_key: str,
|
||||
pending: asyncio.Queue[InboundMessage],
|
||||
pending: SessionInbox,
|
||||
) -> None:
|
||||
try:
|
||||
await self._pending_queues.wait_for_controls(session_key)
|
||||
while self._pending_queues.get(session_key) is pending:
|
||||
try:
|
||||
item = pending.get_nowait()
|
||||
except asyncio.QueueEmpty:
|
||||
# Idle-only automation follows all accepted user input on
|
||||
# this same worker, including before the bus loop starts.
|
||||
deferred = self._deferred_automation_turns.get(session_key)
|
||||
if not deferred:
|
||||
break
|
||||
item = deferred.pop(0)
|
||||
if not deferred:
|
||||
self._deferred_automation_turns.pop(session_key)
|
||||
item = pending.next_turn()
|
||||
if item is None:
|
||||
break
|
||||
session = self.sessions.state.peek_header(session_key)
|
||||
log_content = session is None or (
|
||||
session.policy.persist and session.policy.log_content
|
||||
@@ -1497,20 +1489,18 @@ class AgentLoop:
|
||||
await self._cancel_pending_messages(session_key, pending, exc)
|
||||
raise
|
||||
finally:
|
||||
owns_queue = self._pending_queues.get(session_key) is pending
|
||||
if owns_queue:
|
||||
self._pending_queues.pop(session_key, None)
|
||||
self._pending_queues.detach(session_key, pending)
|
||||
|
||||
async def _cancel_pending_messages(
|
||||
self,
|
||||
session_key: str,
|
||||
pending: asyncio.Queue[InboundMessage],
|
||||
pending: SessionInbox,
|
||||
error: asyncio.CancelledError,
|
||||
) -> None:
|
||||
for msg in self._deferred_automation_turns.pop(session_key, []):
|
||||
pending.put_nowait(msg)
|
||||
while not pending.empty():
|
||||
item = pending.get_nowait()
|
||||
# End admission before publishing any cancellation notifications. Those
|
||||
# callbacks can yield to another submitter for the same session key.
|
||||
self._pending_queues.detach(session_key, pending)
|
||||
for item in pending.drain():
|
||||
for coordinator in self._automation_turn_coordinators:
|
||||
coordinator.complete(item, error=error)
|
||||
if item.channel != "system" and self.commands.requires_session_queue(command_text(item)):
|
||||
@@ -1588,7 +1578,7 @@ class AgentLoop:
|
||||
# shutdown keeps the durable checkpoint untouched instead,
|
||||
# allowing RecoveryCoordinator to offer Continue safely.
|
||||
if (
|
||||
session_key in self._discarding_sessions
|
||||
self._pending_queues.is_discarding(session_key)
|
||||
or self._preserve_inflight_turns_on_shutdown
|
||||
):
|
||||
raise exc
|
||||
@@ -1661,6 +1651,8 @@ class AgentLoop:
|
||||
await self._aclose_unlocked()
|
||||
|
||||
async def _aclose_unlocked(self) -> None:
|
||||
self._pending_queues.close_admission()
|
||||
self._running = False
|
||||
errors: list[BaseException] = []
|
||||
active_task_groups = getattr(self, "_active_tasks", {})
|
||||
current_task = asyncio.current_task()
|
||||
@@ -1670,18 +1662,19 @@ class AgentLoop:
|
||||
if key in self._pending_queues and any(task is not current_task for task in tasks)
|
||||
}
|
||||
active_tasks = tuple({task for tasks in active_task_groups.values() for task in tasks})
|
||||
active_task_groups.clear()
|
||||
active_tasks = tuple(task for task in active_tasks if task is not current_task)
|
||||
for task in active_tasks:
|
||||
if not task.done():
|
||||
task.cancel()
|
||||
self._request_worker_cancellation(active_tasks)
|
||||
try:
|
||||
if active_tasks:
|
||||
await asyncio.gather(*active_tasks, return_exceptions=True)
|
||||
await asyncio.shield(asyncio.gather(*active_tasks, return_exceptions=True))
|
||||
for key, tasks in tuple(active_task_groups.items()):
|
||||
for task in tuple(tasks):
|
||||
if task.done():
|
||||
self._active_task_done(key, tasks, task)
|
||||
for key, pending in pending_queues.items():
|
||||
if self._pending_queues.get(key) is pending:
|
||||
self._pending_queues.pop(key)
|
||||
await self._cancel_pending_messages(key, pending, asyncio.CancelledError())
|
||||
await self._pending_queues.join_controls()
|
||||
# A scan can schedule archival while shutdown waits for it.
|
||||
while self._background_tasks:
|
||||
await asyncio.gather(*self._background_tasks, return_exceptions=True)
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
"""Pending input and idle-only turns for one session worker's lifetime."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from collections import deque
|
||||
from collections.abc import Generator, Iterator, Mapping
|
||||
from contextlib import contextmanager
|
||||
|
||||
from nanobot.bus.events import InboundMessage
|
||||
|
||||
|
||||
class SessionInbox(asyncio.Queue[InboundMessage]):
|
||||
"""Runner injection sees only the input FIFO; the worker also drains idle-only turns."""
|
||||
|
||||
def __init__(self, maxsize: int = 0) -> None:
|
||||
super().__init__(maxsize)
|
||||
self._deferred: deque[InboundMessage] = deque()
|
||||
|
||||
@property
|
||||
def deferred(self) -> tuple[InboundMessage, ...]:
|
||||
return tuple(self._deferred)
|
||||
|
||||
def defer(self, msg: InboundMessage) -> None:
|
||||
self._deferred.append(msg)
|
||||
|
||||
def next_turn(self) -> InboundMessage | None:
|
||||
if not self.empty():
|
||||
return self.get_nowait()
|
||||
return self._deferred.popleft() if self._deferred else None
|
||||
|
||||
def drain(self) -> Iterator[InboundMessage]:
|
||||
"""Only for a detached inbox that can no longer accept work."""
|
||||
while (msg := self.next_turn()) is not None:
|
||||
yield msg
|
||||
|
||||
|
||||
class SessionExecutionClosedError(RuntimeError):
|
||||
"""Work cannot enter the session dispatcher once it starts shutting down."""
|
||||
|
||||
|
||||
class SessionInboxes(Mapping[str, SessionInbox]):
|
||||
"""Own inbox admission and the handoff from control cleanup to a new worker."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._active: dict[str, SessionInbox] = {}
|
||||
self._controls: dict[str, dict[asyncio.Event, tuple[asyncio.Task[object] | None, bool]]] = {}
|
||||
self._closed = False
|
||||
|
||||
def __getitem__(self, key: str) -> SessionInbox:
|
||||
return self._active[key]
|
||||
|
||||
def __iter__(self) -> Iterator[str]:
|
||||
return iter(self._active)
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self._active)
|
||||
|
||||
def close_admission(self) -> None:
|
||||
self._closed = True
|
||||
|
||||
def check_admission(self) -> None:
|
||||
if self._closed:
|
||||
raise SessionExecutionClosedError("Session execution is closing")
|
||||
|
||||
def install(self, key: str, inbox: SessionInbox) -> None:
|
||||
self.check_admission()
|
||||
if key in self:
|
||||
raise RuntimeError("Cannot replace a live session inbox")
|
||||
self._active[key] = inbox
|
||||
|
||||
def detach(self, key: str, inbox: SessionInbox) -> None:
|
||||
if self.get(key) is inbox:
|
||||
del self._active[key]
|
||||
|
||||
@contextmanager
|
||||
def controlling(self, key: str, *, discard: bool = False) -> Generator[None]:
|
||||
"""Accept work into a replacement inbox without starting its worker yet."""
|
||||
self.check_admission()
|
||||
finished = asyncio.Event()
|
||||
controls = self._controls.setdefault(key, {})
|
||||
controls[finished] = (asyncio.current_task(), discard)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
del controls[finished]
|
||||
if not controls:
|
||||
del self._controls[key]
|
||||
finished.set()
|
||||
|
||||
def is_discarding(self, key: str) -> bool:
|
||||
return any(discard for _, discard in self._controls.get(key, {}).values())
|
||||
|
||||
async def wait_for_controls(self, key: str) -> None:
|
||||
while controls := self._controls.get(key):
|
||||
await next(iter(controls)).wait()
|
||||
|
||||
async def join_controls(self) -> None:
|
||||
"""After admission closes, wait for controls still using runtime resources."""
|
||||
current = asyncio.current_task()
|
||||
pending = [
|
||||
finished for controls in self._controls.values()
|
||||
for finished, (owner, _) in controls.items() if owner is not current
|
||||
]
|
||||
if pending:
|
||||
await asyncio.gather(*(finished.wait() for finished in pending))
|
||||
Reference in new issue
Block a user