From 4e2173c9eb7fa59da64e8466b4fc95c1a2a65808 Mon Sep 17 00:00:00 2001 From: chengyongru Date: Wed, 16 Sep 2026 17:53:35 +0800 Subject: [PATCH] fix(agent): serialize session inbox processing --- nanobot/agent/loop.py | 176 ++++---- nanobot/agent/runner.py | 106 ++--- tests/agent/test_attachment_references.py | 3 +- tests/agent/test_loop_save_turn.py | 33 +- tests/agent/test_runner_goal_continue.py | 6 +- tests/agent/test_runner_injections.py | 501 ++++++++++++++-------- tests/tools/test_message_tool_suppress.py | 20 +- 7 files changed, 486 insertions(+), 359 deletions(-) diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.py index a927e2c67..bc20fff50 100644 --- a/nanobot/agent/loop.py +++ b/nanobot/agent/loop.py @@ -29,12 +29,7 @@ from nanobot.agent.cron_turns import CronTurnCoordinator from nanobot.agent.hook import AgentHook, AgentTurnHookFactory from nanobot.agent.memory import Consolidator from nanobot.agent.model_runtime import ModelRuntimeResolver -from nanobot.agent.runner import ( - _MAX_INJECTIONS_PER_TURN, - AgentRunner, - AgentRunResult, - AgentRunSpec, -) +from nanobot.agent.runner import AgentRunner, AgentRunResult, AgentRunSpec 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 @@ -403,9 +398,9 @@ class AgentLoop: self._session_locks: weakref.WeakValueDictionary[str, asyncio.Lock] = ( weakref.WeakValueDictionary() ) - # Per-session pending queues for mid-turn message injection. - # When a session has an active task, new messages for that session - # are routed here instead of creating a new task. + # 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._preserve_inflight_turns_on_shutdown = False self._deferred_automation_turns: dict[str, list[InboundMessage]] = {} @@ -983,13 +978,25 @@ class AgentLoop: async def _drain_pending( *, - limit: int = _MAX_INJECTIONS_PER_TURN, first_msg: InboundMessage | None = None, ) -> list[dict[str, Any]]: - """Drain only messages that are already available.""" + """Drain one atomic snapshot of messages already available.""" if pending_queue is None: return [] + # Capture the boundary before converting any message. Conversion + # resolves runtime context and may await, so reading until QueueEmpty + # afterwards would let later arrivals leak into this model step. + pending_messages: list[InboundMessage] = [] + if first_msg is not None: + pending_messages.append(first_msg) + snapshot_size = pending_queue.qsize() + for _ in range(snapshot_size): + try: + pending_messages.append(pending_queue.get_nowait()) + except asyncio.QueueEmpty: + break + async def _to_user_message(pending_msg: InboundMessage) -> dict[str, Any]: content = pending_msg.content image_paths = pending_msg.media if pending_msg.media else None @@ -1057,27 +1064,15 @@ class AgentLoop: row[PENDING_FOLLOWUP_ID_KEY] = followup_id return row - items: list[dict[str, Any]] = [] - if first_msg is not None: - items.append(await _to_user_message(first_msg)) - while len(items) < limit: - try: - items.append(await _to_user_message(pending_queue.get_nowait())) - except asyncio.QueueEmpty: - break - - return items + return [await _to_user_message(pending_msg) for pending_msg in pending_messages] terminal_wait_deadline: float | None = None - async def _wait_for_pending( - *, - limit: int = _MAX_INJECTIONS_PER_TURN, - ) -> list[dict[str, Any]]: + async def _wait_for_pending() -> list[dict[str, Any]]: """Wait for a pending result only when the runner is ready to exit.""" nonlocal terminal_wait_deadline - items = await _drain_pending(limit=limit) + items = await _drain_pending() if ( items or pending_queue is None @@ -1102,7 +1097,7 @@ class AgentLoop: ) return [] - return await _drain_pending(limit=limit, first_msg=msg) + return await _drain_pending(first_msg=msg) request_ctx = request_context or RequestContext( channel="cli", @@ -1345,27 +1340,19 @@ class AgentLoop: self.commands.dispatch, ) continue - pending_msg = routed_msg - session = self.sessions.get_or_create(effective_key) - followup_id = record_pending_followup(session, pending_msg) - if followup_id is not None: - pending_msg = dataclasses.replace( - pending_msg, - metadata={ - **pending_msg.metadata, - PENDING_FOLLOWUP_ID_KEY: followup_id, - }, - ) - self.sessions.save(session) + pending_msg = self._journal_pending_message(effective_key, routed_msg) self._pending_queues[effective_key].put_nowait(pending_msg) logger.info( "Routed follow-up message to pending queue for session {}", effective_key, ) continue - # Compute the effective session key before dispatching - # This ensures /stop command can find tasks correctly when unified session is enabled - task = asyncio.create_task(self._dispatch(msg)) + # Install the queue before scheduling its worker. Every later + # message now has one FIFO path even if this task has not run yet. + pending: asyncio.Queue[InboundMessage] = asyncio.Queue() + pending.put_nowait(routed_msg) + self._pending_queues[effective_key] = pending + task = asyncio.create_task(self._run_session_queue(effective_key, pending)) self._track_active_task(effective_key, task) finally: await self.aclose() @@ -1380,11 +1367,77 @@ class AgentLoop: """ self._preserve_inflight_turns_on_shutdown = True + def _journal_pending_message( + self, + session_key: str, + msg: InboundMessage, + ) -> InboundMessage: + """Persist a recoverable WebUI follow-up before adding it to the inbox.""" + session = self.sessions.get_or_create(session_key) + followup_id = record_pending_followup(session, msg) + if followup_id is None: + return msg + pending_msg = dataclasses.replace( + msg, + metadata={ + **msg.metadata, + PENDING_FOLLOWUP_ID_KEY: followup_id, + }, + ) + self.sessions.save(session) + return pending_msg + async def _dispatch(self, msg: InboundMessage) -> None: - """Process a message: per-session serial, cross-session concurrent.""" + """Submit one direct caller message through the per-session FIFO.""" session_key = self._effective_session_key(msg) if session_key != msg.session_key: msg = dataclasses.replace(msg, session_key_override=session_key) + + pending = self._pending_queues.get(session_key) + if pending is not None: + pending.put_nowait(self._journal_pending_message(session_key, msg)) + return + + new_pending: asyncio.Queue[InboundMessage] = asyncio.Queue() + new_pending.put_nowait(msg) + self._pending_queues[session_key] = new_pending + await self._run_session_queue(session_key, new_pending) + + async def _run_session_queue( + self, + session_key: str, + pending: asyncio.Queue[InboundMessage], + ) -> None: + """Run the sole worker for a session until its inbox is empty.""" + try: + while self._pending_queues.get(session_key) is pending: + try: + msg = pending.get_nowait() + except asyncio.QueueEmpty: + break + try: + await self._dispatch_one(msg, pending) + except asyncio.CancelledError: + raise + except Exception: + logger.exception( + "Session worker failed one message for {}; continuing FIFO", + session_key, + ) + finally: + owns_queue = self._pending_queues.get(session_key) is pending + if owns_queue: + self._pending_queues.pop(session_key, None) + if owns_queue and self._running and session_key not in self._discarding_sessions: + await self._publish_next_deferred_automation_turn(session_key) + + async def _dispatch_one( + self, + msg: InboundMessage, + pending: asyncio.Queue[InboundMessage], + ) -> None: + """Process one root message while later inputs remain in its session inbox.""" + session_key = self._effective_session_key(msg) recovery_task_registered = False recovery_admission = self._recovery_admission current_task: asyncio.Task[Any] | None = None @@ -1404,18 +1457,9 @@ class AgentLoop: gate = self._concurrency_gate or nullcontext() delivery = self.turn_delivery_factory.unrouted(msg, session_key) - pending: asyncio.Queue[InboundMessage] | None = None completion_published = False try: async with lock, gate: - # Only the task that owns the session lock may publish the - # active mid-turn injection queue for this session. - # The global inbound bus is already unbounded. Keep the session queue - # unbounded too so every message has one FIFO path; a bounded queue - # would need a second dispatch path whose lock waiters can overtake - # older messages left here. - pending = asyncio.Queue() - self._pending_queues[session_key] = pending try: delivery = self.turn_delivery_factory.create( msg, @@ -1483,33 +1527,8 @@ class AgentLoop: for _, coordinator in self._automation_turn_coordinators: coordinator.complete(msg, error=exc) finally: - # Drain any messages still in the pending queue and re-publish - # them to the bus so they are processed as fresh inbound messages - # rather than silently lost. Only remove our own queue; a - # later task waiting on the lock must not be able to steal - # cleanup ownership. - queue = None - if self._pending_queues.get(session_key) is pending: - queue = self._pending_queues.pop(session_key, None) - else: - queue = pending - if queue is not None: - leftover = 0 - while True: - try: - item = queue.get_nowait() - except asyncio.QueueEmpty: - break - await self.bus.publish_inbound(item) - leftover += 1 - if leftover: - logger.info( - "Re-published {} leftover message(s) to bus for session {}", - leftover, session_key, - ) if not turn_continuation.internal_continuation_pending(msg.metadata): await delivery.idle() - await self._publish_next_deferred_automation_turn(session_key) except asyncio.CancelledError: if not completion_published and normalize_command_text(msg.content).lower() == "/compact": await delivery.complete(None, publish_completion=True) @@ -1521,9 +1540,6 @@ class AgentLoop: and recovery_admission is not None ): recovery_admission.unregister_recovery_task(session_key, current_task) - if pending is None: - await delivery.idle() - await self._publish_next_deferred_automation_turn(session_key) async def aclose(self) -> None: """Stop active work, then close resources owned by the agent loop. diff --git a/nanobot/agent/runner.py b/nanobot/agent/runner.py index bfcdcdb54..8c958d940 100644 --- a/nanobot/agent/runner.py +++ b/nanobot/agent/runner.py @@ -3,7 +3,6 @@ from __future__ import annotations import asyncio -import inspect import time from collections.abc import Awaitable, Callable, Iterable from copy import deepcopy @@ -60,7 +59,7 @@ from nanobot.utils.runtime import ( ContinuationCallback = Callable[[], str | None] CheckpointCallback = Callable[[dict[str, Any]], Awaitable[None]] -InjectionCallback = Callable[..., Awaitable[Iterable[Any] | None]] +InjectionCallback = Callable[[], Awaitable[Iterable[Any] | None]] _DEFAULT_ERROR_MESSAGE = "Sorry, I encountered an error calling the AI model." _ARREARAGE_ERROR_MESSAGE = ( @@ -70,8 +69,6 @@ _ARREARAGE_ERROR_MESSAGE = ( _PERSISTED_MODEL_ERROR_PLACEHOLDER = "[Assistant reply unavailable due to model error.]" _MAX_EMPTY_RETRIES = 2 _MAX_LENGTH_RECOVERIES = 3 -_MAX_INJECTIONS_PER_TURN = 3 -_MAX_INJECTION_CYCLES = 5 def _restore_outer_whitespace(content: str, original: str | None) -> str: @@ -164,19 +161,11 @@ class AgentRunner: iteration: int | None = None, allow_continuation: bool = False, wait_at_terminal: bool = False, + drain_callback: bool = True, ) -> tuple[bool, int]: - """Drain pending injections. Returns (should_continue, updated_cycles). - - If injections are found and we haven't exceeded _MAX_INJECTION_CYCLES, - append them to *messages* (and emit a checkpoint if *assistant_message* - and *iteration* are both provided) and return (True, cycles+1) so the - caller continues the iteration loop. Otherwise return (False, cycles). - """ - injections: list[dict[str, Any]] = [] - real_injection = False - if injection_cycles < _MAX_INJECTION_CYCLES: - injections = await self._drain_injections(spec) - real_injection = bool(injections) + """Append one pending-input snapshot and return whether execution continues.""" + injections = await self._drain_injections(spec) if drain_callback else [] + real_injection = bool(injections) if not injections and allow_continuation and assistant_message is not None: continuation = self._build_continuation_message(spec) if continuation is not None: @@ -184,7 +173,7 @@ class AgentRunner: if ( not injections and wait_at_terminal - and injection_cycles < _MAX_INJECTION_CYCLES + and drain_callback ): injections = await self._drain_injections(spec, terminal=True) real_injection = bool(injections) @@ -214,8 +203,8 @@ class AgentRunner: self._append_injected_messages(messages, injections) if real_injection: logger.info( - "Injected {} follow-up message(s) {} ({}/{})", - len(injections), phase, injection_cycles, _MAX_INJECTION_CYCLES, + "Injected {} follow-up message(s) {} (snapshot {})", + len(injections), phase, injection_cycles, ) else: logger.info("Injected caller-requested continuation {}", phase) @@ -241,13 +230,7 @@ class AgentRunner: *, terminal: bool = False, ) -> list[dict[str, Any]]: - """Drain pending user messages via the injection callback. - - Returns normalized user messages (capped by - ``_MAX_INJECTIONS_PER_TURN``), or an empty list when there is - nothing to inject. Messages beyond the cap are logged so they - are not silently lost. - """ + """Drain one pending-input snapshot via the injection callback.""" callback = ( spec.terminal_injection_callback if terminal @@ -256,18 +239,7 @@ class AgentRunner: if callback is None: return [] try: - signature = inspect.signature(callback) - accepts_limit = ( - "limit" in signature.parameters - or any( - parameter.kind is inspect.Parameter.VAR_KEYWORD - for parameter in signature.parameters.values() - ) - ) - if accepts_limit: - items = await callback(limit=_MAX_INJECTIONS_PER_TURN) - else: - items = await callback() + items = await callback() except Exception: logger.exception("injection_callback failed") return [] @@ -286,13 +258,6 @@ class AgentRunner: content = getattr(item, "content") if hasattr(item, "content") else str(item) if self._has_injection_content(content): injected_messages.append({"role": "user", "content": content}) - if len(injected_messages) > _MAX_INJECTIONS_PER_TURN: - dropped = len(injected_messages) - _MAX_INJECTIONS_PER_TURN - logger.warning( - "Injection callback returned {} messages, capping to {} ({} dropped)", - len(injected_messages), _MAX_INJECTIONS_PER_TURN, dropped, - ) - injected_messages = injected_messages[:_MAX_INJECTIONS_PER_TURN] return injected_messages @staticmethod @@ -433,6 +398,18 @@ class AgentRunner: ) for iteration in range(spec.max_iterations): + # The session inbox cuts a finite snapshot before every model call. + # This includes follow-ups that arrived before the first request and + # messages received while the previous request or tools were running. + drained_before_request, injection_cycles = await self._try_drain_injections( + spec, + messages, + None, + injection_cycles, + phase="before model call", + ) + if drained_before_request: + had_injections = True context = AgentHookContext( iteration=iteration, messages=messages, @@ -568,13 +545,6 @@ class AgentRunner: ) empty_content_retries = 0 length_recovery_parts.clear() - # Checkpoint 1: drain injections after tools, before next LLM call - _drained, injection_cycles = await self._try_drain_injections( - spec, messages, None, injection_cycles, - phase="after tool execution", - ) - if _drained: - had_injections = True await hook.after_iteration(context) continue @@ -687,6 +657,10 @@ class AgentRunner: # Check for mid-turn injections BEFORE signaling stream end. # If injections are found we keep the stream alive (resuming=True) # so streaming channels don't prematurely finalize the card. + can_make_followup_request = ( + iteration + 1 < spec.max_iterations + or spec.finalize_on_max_iterations + ) should_continue, injection_cycles = await self._try_drain_injections( spec, messages, assistant_message, injection_cycles, conversation_state=conversation_state, @@ -700,6 +674,7 @@ class AgentRunner: and response.finish_reason not in {"error", "length", "refusal", "content_filter"} ), + drain_callback=can_make_followup_request, ) if should_continue: had_injections = True @@ -727,6 +702,7 @@ class AgentRunner: should_continue, injection_cycles = await self._try_drain_injections( spec, messages, None, injection_cycles, phase="after LLM error", + drain_callback=can_make_followup_request, ) if should_continue: had_injections = True @@ -746,6 +722,7 @@ class AgentRunner: should_continue, injection_cycles = await self._try_drain_injections( spec, messages, None, injection_cycles, phase="after empty response", + drain_callback=can_make_followup_request, ) if should_continue: had_injections = True @@ -789,19 +766,22 @@ class AgentRunner: break else: stop_reason = "max_iterations" - # Drain any remaining injections so they are appended to the - # conversation history instead of being re-published as - # independent inbound messages by _dispatch's finally block. - # We include them before the no-tools finalization pass so the - # final response can account for every known follow-up. - drained_after_max_iterations, injection_cycles = await self._try_drain_injections( - spec, messages, None, injection_cycles, - phase="after max_iterations", - ) - if drained_after_max_iterations: - had_injections = True terminal_content = None if spec.finalize_on_max_iterations: + # The no-tools finalization is a real model boundary, so include + # exactly the inputs waiting before that request. Without this + # request, leave them in the session inbox for its worker. + drained_after_max_iterations, injection_cycles = ( + await self._try_drain_injections( + spec, + messages, + None, + injection_cycles, + phase="before max-iterations finalization", + ) + ) + if drained_after_max_iterations: + had_injections = True terminal_content, usage = await self._try_finalize_after_max_iterations( spec, hook, diff --git a/tests/agent/test_attachment_references.py b/tests/agent/test_attachment_references.py index 0feec5202..fe7ad1211 100644 --- a/tests/agent/test_attachment_references.py +++ b/tests/agent/test_attachment_references.py @@ -158,8 +158,9 @@ async def test_pending_document_attachment_keeps_body_out_of_prompt( pending_queue=pending_queue, ) - assert result.final_content == "answer-2" + assert result.final_content == "answer-1" assert result.had_injections is True + assert call_count == 1 injected_user_content = [ message["content"] for message in captured_messages[-1] diff --git a/tests/agent/test_loop_save_turn.py b/tests/agent/test_loop_save_turn.py index 8f0950337..810f5c0c2 100644 --- a/tests/agent/test_loop_save_turn.py +++ b/tests/agent/test_loop_save_turn.py @@ -52,7 +52,6 @@ from nanobot.session.summary import ( ) from nanobot.session.turn_continuation import ( INTERNAL_CONTINUATION_META, - INTERNAL_CONTINUATION_RUN_STARTED_AT_META, ) from nanobot.session.webui_turns import ( TITLE_GENERATION_MAX_TOKENS, @@ -1524,15 +1523,6 @@ async def test_internal_continuation_preserves_streaming_route_metadata( }, )) - assert loop.bus.outbound_size == 0 - queued = await asyncio.wait_for(loop.bus.consume_inbound(), timeout=0.5) - assert queued.metadata[INTERNAL_CONTINUATION_META] is True - assert queued.metadata["_wants_stream"] is True - assert queued.metadata["message_id"] == "om_001" - assert queued.metadata["origin_message_id"] == "root_001" - - await loop._dispatch(queued) - outbound = [] while loop.bus.outbound_size: outbound.append(await loop.bus.consume_outbound()) @@ -1548,6 +1538,8 @@ async def test_internal_continuation_preserves_streaming_route_metadata( assert ends[0].metadata["origin_message_id"] == "root_001" assert isinstance(ends[0].event.stream_id, str) assert streamed_markers and streamed_markers[-1].content == "done" + assert loop.bus.inbound_size == 0 + assert calls == 2 @pytest.mark.asyncio @@ -1593,26 +1585,15 @@ async def test_websocket_internal_continuation_keeps_single_visible_run( while loop.bus.outbound_size: first_outbound.append(await loop.bus.consume_outbound()) first_statuses = [m.event for m in first_outbound if isinstance(m.event, GoalStatusEvent)] - assert [m.status for m in first_statuses] == ["running"] - assert not [m for m in first_outbound if isinstance(m.event, TurnEndEvent)] + assert [m.status for m in first_statuses] == ["running", "running", "idle"] started_at = first_statuses[0].started_at - - queued = await asyncio.wait_for(loop.bus.consume_inbound(), timeout=0.5) - assert queued.metadata[INTERNAL_CONTINUATION_META] is True - assert queued.metadata[INTERNAL_CONTINUATION_RUN_STARTED_AT_META] == started_at - - await loop._dispatch(queued) - - second_outbound = [] - while loop.bus.outbound_size: - second_outbound.append(await loop.bus.consume_outbound()) - second_statuses = [m.event for m in second_outbound if isinstance(m.event, GoalStatusEvent)] - assert [m.status for m in second_statuses] == ["running", "idle"] - assert second_statuses[0].started_at == started_at - turn_end = [m for m in second_outbound if isinstance(m.event, TurnEndEvent)] + assert first_statuses[1].started_at == started_at + turn_end = [m for m in first_outbound if isinstance(m.event, TurnEndEvent)] assert len(turn_end) == 1 assert isinstance(turn_end[0].event, TurnEndEvent) assert isinstance(turn_end[0].event.latency_ms, int) + assert loop.bus.inbound_size == 0 + assert calls == 2 @pytest.mark.asyncio diff --git a/tests/agent/test_runner_goal_continue.py b/tests/agent/test_runner_goal_continue.py index eb2f8d374..bc7eca58c 100644 --- a/tests/agent/test_runner_goal_continue.py +++ b/tests/agent/test_runner_goal_continue.py @@ -135,9 +135,9 @@ async def test_runner_respects_max_iterations_with_continuation(): @pytest.mark.asyncio -async def test_runner_continuation_not_limited_by_injection_cycle_cap(): +async def test_runner_continuation_is_governed_by_max_iterations(): """Caller-requested continuation is governed by max_iterations.""" - from nanobot.agent.runner import _MAX_INJECTION_CYCLES, AgentRunner + from nanobot.agent.runner import AgentRunner provider = MagicMock(spec=LLMProvider) provider.chat_stream_with_retry = AsyncMock(return_value=LLMResponse( @@ -145,7 +145,7 @@ async def test_runner_continuation_not_limited_by_injection_cycle_cap(): )) tools = MagicMock() tools.get_definitions.return_value = [] - max_iterations = _MAX_INJECTION_CYCLES + 3 + max_iterations = 8 runner = AgentRunner() result = await runner.run(make_run_spec(provider, diff --git a/tests/agent/test_runner_injections.py b/tests/agent/test_runner_injections.py index ca9e947b0..1e5406e80 100644 --- a/tests/agent/test_runner_injections.py +++ b/tests/agent/test_runner_injections.py @@ -99,25 +99,22 @@ async def test_drain_injections_extracts_content_from_inbound_messages(): @pytest.mark.asyncio -async def test_drain_injections_passes_limit_to_callback_when_supported(): - """Limit-aware callbacks can preserve overflow in their own queue.""" - from nanobot.agent.runner import _MAX_INJECTIONS_PER_TURN, AgentRunner +async def test_drain_injections_keeps_entire_callback_snapshot(): + """A callback snapshot is never split by an arbitrary message count.""" + from nanobot.agent.runner import AgentRunner from nanobot.bus.events import InboundMessage provider = MagicMock() runner = AgentRunner() tools = MagicMock() tools.get_definitions.return_value = [] - seen_limits: list[int] = [] - msgs = [ InboundMessage(channel="cli", sender_id="u", chat_id="c", content=f"msg{i}") - for i in range(_MAX_INJECTIONS_PER_TURN + 3) + for i in range(8) ] - async def cb(*, limit: int): - seen_limits.append(limit) - return msgs[:limit] + async def cb(): + return msgs spec = make_run_spec(provider, initial_messages=[], tools=tools, model="m", @@ -125,11 +122,9 @@ async def test_drain_injections_passes_limit_to_callback_when_supported(): injection_callback=cb, ) result = await runner._drain_injections(spec) - assert seen_limits == [_MAX_INJECTIONS_PER_TURN] assert result == [ - {"role": "user", "content": "msg0"}, - {"role": "user", "content": "msg1"}, - {"role": "user", "content": "msg2"}, + {"role": "user", "content": f"msg{i}"} + for i in range(8) ] @@ -272,15 +267,18 @@ async def test_checkpoint1_injects_after_tool_execution(): provider.chat_stream_with_retry = chat_stream_with_retry tools = MagicMock() tools.get_definitions.return_value = [] - tools.execute = AsyncMock(return_value="file content") - injection_queue = asyncio.Queue() inject_cb = _make_injection_callback(injection_queue) - # Put a follow-up message in the queue before the run starts - await injection_queue.put( - InboundMessage(channel="cli", sender_id="u", chat_id="c", content="follow-up question") - ) + async def execute_tool(*_args, **_kwargs): + await injection_queue.put( + InboundMessage( + channel="cli", sender_id="u", chat_id="c", content="follow-up question" + ) + ) + return "file content" + + tools.execute = AsyncMock(side_effect=execute_tool) runner = AgentRunner() result = await runner.run(make_run_spec(provider, @@ -449,6 +447,11 @@ async def test_checkpoint2_injects_after_final_response_with_resuming_stream(): async def chat_stream_with_retry(*, messages, on_content_delta=None, **kwargs): call_count["n"] += 1 if call_count["n"] == 1: + await injection_queue.put( + InboundMessage( + channel="cli", sender_id="u", chat_id="c", content="quick follow-up" + ) + ) return LLMResponse(content="first answer", tool_calls=[], usage=None) return LLMResponse(content="second answer", tool_calls=[], usage=None) @@ -459,11 +462,6 @@ async def test_checkpoint2_injects_after_final_response_with_resuming_stream(): injection_queue = asyncio.Queue() inject_cb = _make_injection_callback(injection_queue) - # Inject a follow-up that arrives during the first response - await injection_queue.put( - InboundMessage(channel="cli", sender_id="u", chat_id="c", content="quick follow-up") - ) - runner = AgentRunner() result = await runner.run(make_run_spec(provider, initial_messages=[{"role": "user", "content": "hello"}], @@ -491,22 +489,31 @@ async def test_injected_followup_starts_new_length_recovery_chain(): from nanobot.bus.events import InboundMessage provider = MagicMock() - provider.chat_stream_with_retry = AsyncMock(side_effect=[ + responses = [ LLMResponse(content="first-1 ", finish_reason="length"), LLMResponse(content="first-2 ", finish_reason="length"), LLMResponse(content="first-3 ", finish_reason="length"), LLMResponse(content="first-final", finish_reason="stop"), LLMResponse(content="follow-up ", finish_reason="length"), LLMResponse(content="answer", finish_reason="stop"), - ]) + ] tools = MagicMock() tools.get_definitions.return_value = [] injection_queue = asyncio.Queue() inject_cb = _make_injection_callback(injection_queue) - await injection_queue.put( - InboundMessage(channel="cli", sender_id="u", chat_id="c", content="follow-up question") - ) + call_count = 0 + + async def chat_stream_with_retry(**_kwargs): + nonlocal call_count + call_count += 1 + if call_count == 4: + await injection_queue.put(InboundMessage( + channel="cli", sender_id="u", chat_id="c", content="follow-up question" + )) + return responses[call_count - 1] + + provider.chat_stream_with_retry = chat_stream_with_retry runner = AgentRunner() result = await runner.run(make_run_spec(provider, @@ -520,7 +527,7 @@ async def test_injected_followup_starts_new_length_recovery_chain(): assert result.had_injections is True assert result.final_content == "follow-up answer" - assert provider.chat_stream_with_retry.await_count == 6 + assert call_count == 6 @pytest.mark.asyncio @@ -537,6 +544,11 @@ async def test_checkpoint2_preserves_final_response_in_history_before_followup() call_count["n"] += 1 captured_messages.append([dict(message) for message in messages]) if call_count["n"] == 1: + await injection_queue.put( + InboundMessage( + channel="cli", sender_id="u", chat_id="c", content="follow-up question" + ) + ) return LLMResponse(content="first answer", tool_calls=[], usage=None) return LLMResponse(content="second answer", tool_calls=[], usage=None) @@ -547,10 +559,6 @@ async def test_checkpoint2_preserves_final_response_in_history_before_followup() injection_queue = asyncio.Queue() inject_cb = _make_injection_callback(injection_queue) - await injection_queue.put( - InboundMessage(channel="cli", sender_id="u", chat_id="c", content="follow-up question") - ) - runner = AgentRunner() result = await runner.run(make_run_spec(provider, initial_messages=[{"role": "user", "content": "hello"}], @@ -600,6 +608,13 @@ async def test_loop_injected_followup_preserves_image_media(tmp_path): call_count["n"] += 1 captured_messages.append(list(messages)) if call_count["n"] == 1: + await pending_queue.put(InboundMessage( + channel="cli", + sender_id="u", + chat_id="c", + content="", + media=[str(image_path)], + )) return LLMResponse(content="first answer", tool_calls=[], usage=None) return LLMResponse(content="second answer", tool_calls=[], usage=None) @@ -608,14 +623,6 @@ async def test_loop_injected_followup_preserves_image_media(tmp_path): loop.tools.get_definitions = MagicMock(return_value=[]) pending_queue = asyncio.Queue() - await pending_queue.put(InboundMessage( - channel="cli", - sender_id="u", - chat_id="c", - content="", - media=[str(image_path)], - )) - runtime = loop.llm_runtime() result = await loop._run_agent_loop( TranscriptInput(history=[{"role": "user", "content": "hello"}], current_message=None), @@ -788,6 +795,19 @@ async def test_subagent_pending_injection_is_hidden_history_and_not_merged(tmp_p async def chat_stream_with_retry(*, messages, **kwargs): call_count["n"] += 1 if call_count["n"] == 1: + await pending_queue.put(InboundMessage( + channel="cli", + sender_id="user", + chat_id="c", + content="visible follow-up", + )) + await pending_queue.put(InboundMessage( + channel="system", + sender_id="subagent", + chat_id="cli:c", + content=payload, + metadata={"injected_event": "subagent_result", "subagent_task_id": "sub-1"}, + )) return LLMResponse(content="first answer", tool_calls=[], usage=None) return LLMResponse(content="second answer", tool_calls=[], usage=None) @@ -802,19 +822,6 @@ async def test_subagent_pending_injection_is_hidden_history_and_not_merged(tmp_p "Summarize this naturally for the user." ) pending_queue = asyncio.Queue() - await pending_queue.put(InboundMessage( - channel="cli", - sender_id="user", - chat_id="c", - content="visible follow-up", - )) - await pending_queue.put(InboundMessage( - channel="system", - sender_id="subagent", - chat_id="cli:c", - content=payload, - metadata={"injected_event": "subagent_result", "subagent_task_id": "sub-1"}, - )) runtime = loop.llm_runtime() result = await loop._run_agent_loop( @@ -976,30 +983,31 @@ def test_model_request_merge_preserves_runtime_markers_with_media() -> None: @pytest.mark.asyncio -async def test_injection_cycles_capped_at_max(): - """Injection cycles should be capped at _MAX_INJECTION_CYCLES.""" - from nanobot.agent.runner import _MAX_INJECTION_CYCLES, AgentRunner +async def test_injection_cycles_are_not_stopped_by_an_arbitrary_cap(): + """Every pending snapshot runs until the normal iteration budget is reached.""" + from nanobot.agent.runner import AgentRunner from nanobot.bus.events import InboundMessage provider = MagicMock() call_count = {"n": 0} + injection_queue = asyncio.Queue() async def chat_stream_with_retry(*, messages, **kwargs): call_count["n"] += 1 + if call_count["n"] <= 7: + await injection_queue.put(InboundMessage( + channel="cli", + sender_id="u", + chat_id="c", + content=f"msg-{call_count['n']}", + )) return LLMResponse(content=f"answer-{call_count['n']}", tool_calls=[], usage=None) provider.chat_stream_with_retry = chat_stream_with_retry tools = MagicMock() tools.get_definitions.return_value = [] - drain_count = {"n": 0} - - async def inject_cb(): - drain_count["n"] += 1 - # Only inject for the first _MAX_INJECTION_CYCLES drains - if drain_count["n"] <= _MAX_INJECTION_CYCLES: - return [InboundMessage(channel="cli", sender_id="u", chat_id="c", content=f"msg-{drain_count['n']}")] - return [] + inject_cb = _make_injection_callback(injection_queue) runner = AgentRunner() result = await runner.run(make_run_spec(provider, @@ -1012,8 +1020,7 @@ async def test_injection_cycles_capped_at_max(): )) assert result.had_injections is True - # Should be capped: _MAX_INJECTION_CYCLES injection rounds + 1 final round - assert call_count["n"] == _MAX_INJECTION_CYCLES + 1 + assert call_count["n"] == 8 @pytest.mark.asyncio @@ -1065,43 +1072,24 @@ async def test_pending_queue_cleanup_on_dispatch(tmp_path): @pytest.mark.asyncio -async def test_waiting_dispatch_does_not_replace_active_pending_queue(tmp_path): - """A queued dispatch must not steal the active task's injection queue.""" +async def test_direct_dispatch_joins_active_session_queue_without_waiting_for_lock(tmp_path): + """Direct callers use the active FIFO instead of creating a lock waiter.""" 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() - active_pending = asyncio.Queue(maxsize=1) + active_pending = asyncio.Queue() loop._pending_queues[session_key] = active_pending - waiting_at_lock = asyncio.Event() - original_acquire = asyncio.Lock.acquire - - async def _patched_acquire(self, *args, **kwargs): - if self is lock: - waiting_at_lock.set() - return await original_acquire(self, *args, **kwargs) - - with patch.object(asyncio.Lock, "acquire", _patched_acquire): - waiting = asyncio.create_task( - loop._dispatch( - InboundMessage(channel="cli", sender_id="u", chat_id="c", content="queued") - ) - ) - await asyncio.wait_for(waiting_at_lock.wait(), timeout=2.0) + await loop._dispatch( + InboundMessage(channel="cli", sender_id="u", chat_id="c", content="queued") + ) 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() + assert (await active_pending.get()).content == "queued" @pytest.mark.asyncio @@ -1458,10 +1446,9 @@ async def test_local_trigger_turn_cancellation_reports_agent_failure(tmp_path): @pytest.mark.asyncio -async def test_pending_queue_preserves_overflow_for_next_injection_cycle(tmp_path): - """Pending queue should leave overflow messages queued for later drains.""" +async def test_pending_queue_batches_full_snapshot_before_first_model_call(tmp_path): + """All messages waiting at the checkpoint enter one model request in order.""" from nanobot.agent.loop import AgentLoop - from nanobot.agent.runner import _MAX_INJECTIONS_PER_TURN from nanobot.bus.events import InboundMessage from nanobot.bus.queue import MessageBus @@ -1481,7 +1468,7 @@ async def test_pending_queue_preserves_overflow_for_next_injection_cycle(tmp_pat loop.tools.get_definitions = MagicMock(return_value=[]) pending_queue = asyncio.Queue() - total_followups = _MAX_INJECTIONS_PER_TURN + 2 + total_followups = 8 for idx in range(total_followups): await pending_queue.put(InboundMessage( channel="cli", @@ -1498,9 +1485,9 @@ async def test_pending_queue_preserves_overflow_for_next_injection_cycle(tmp_pat pending_queue=pending_queue, ) - assert result.final_content == "answer-3" + assert result.final_content == "answer-1" assert result.had_injections is True - assert call_count["n"] == 3 + assert call_count["n"] == 1 flattened_user_content = "\n".join( message["content"] for message in captured_messages[-1] @@ -1508,12 +1495,93 @@ async def test_pending_queue_preserves_overflow_for_next_injection_cycle(tmp_pat ) for idx in range(total_followups): assert f"follow-up-{idx}" in flattened_user_content + raw_followups = [ + message["content"] + for message in result.messages + if message.get("role") == "user" + and isinstance(message.get("content"), str) + and message["content"].startswith("follow-up-") + ] + assert raw_followups == [f"follow-up-{idx}" for idx in range(total_followups)] assert pending_queue.empty() @pytest.mark.asyncio -async def test_busy_session_burst_preserves_fifo(tmp_path): - """Later messages must not bypass older follow-ups when one session is busy.""" +async def test_pending_queue_snapshot_excludes_messages_arriving_during_conversion(tmp_path): + """A checkpoint is a finite snapshot even when message conversion awaits.""" + from nanobot.agent.loop import AgentLoop + from nanobot.bus.events import InboundMessage + from nanobot.bus.queue import MessageBus + + provider = MagicMock() + provider.get_default_model.return_value = "test-model" + captured_messages: list[list[dict]] = [] + + async def chat_stream_with_retry(*, messages, **kwargs): + captured_messages.append([dict(message) for message in messages]) + return LLMResponse( + content=f"answer-{len(captured_messages)}", + tool_calls=[], + usage=None, + ) + + provider.chat_stream_with_retry = chat_stream_with_retry + loop = AgentLoop( + bus=MessageBus(), + provider=provider, + workspace=tmp_path, + model="test-model", + ) + loop.tools.get_definitions = MagicMock(return_value=[]) + + conversion_started = asyncio.Event() + release_conversion = asyncio.Event() + + async def block_first_conversion(request): + if request.original_user_text == "first snapshot": + conversion_started.set() + await release_conversion.wait() + return None + + loop.register_runtime_context_provider(block_first_conversion) + pending_queue = asyncio.Queue() + pending_queue.put_nowait(InboundMessage( + channel="cli", + sender_id="u", + chat_id="c", + content="first snapshot", + )) + + runtime = loop.llm_runtime() + run_task = asyncio.create_task(loop._run_agent_loop( + TranscriptInput(history=[{"role": "user", "content": "hello"}], current_message=None), + runtime=runtime, + request_context=RequestContext(channel="cli", chat_id="c", runtime=runtime), + pending_queue=pending_queue, + )) + await asyncio.wait_for(conversion_started.wait(), timeout=2) + pending_queue.put_nowait(InboundMessage( + channel="cli", + sender_id="u", + chat_id="c", + content="next snapshot", + )) + release_conversion.set() + + result = await asyncio.wait_for(run_task, timeout=2) + + assert result.final_content == "answer-2" + assert len(captured_messages) == 2 + first_request = "\n".join(str(message.get("content", "")) for message in captured_messages[0]) + second_request = "\n".join(str(message.get("content", "")) for message in captured_messages[1]) + assert "first snapshot" in first_request + assert "next snapshot" not in first_request + assert "next snapshot" in second_request + + +@pytest.mark.asyncio +async def test_session_inbox_is_installed_before_worker_start(tmp_path): + """A preloaded burst has one FIFO path before the worker can be scheduled.""" from nanobot.bus.events import InboundMessage, OutboundMessage loop = _make_loop(tmp_path) @@ -1533,19 +1601,23 @@ async def test_busy_session_burst_preserves_fifo(tmp_path): return OutboundMessage(channel="cli", chat_id="c", content=msg.content) loop._process_message = _process_message # type: ignore[method-assign] - run_task = asyncio.create_task(loop.run()) await loop.bus.publish_inbound( InboundMessage(channel="cli", sender_id="u", chat_id="c", content="initial") ) - await asyncio.wait_for(first_started.wait(), timeout=2) - for content in followups: await loop.bus.publish_inbound( InboundMessage(channel="cli", sender_id="u", chat_id="c", content=content) ) + + run_task = asyncio.create_task(loop.run()) + await asyncio.wait_for(first_started.wait(), timeout=2) while loop.bus.inbound_size: await asyncio.sleep(0) + session_key = "cli:c" + assert len(loop._active_tasks[session_key]) == 1 + assert loop._pending_queues[session_key].qsize() == len(followups) + release_first.set() await asyncio.wait_for(all_processed.wait(), timeout=5) loop.stop() @@ -1555,47 +1627,103 @@ async def test_busy_session_burst_preserves_fifo(tmp_path): @pytest.mark.asyncio -async def test_dispatch_republishes_leftover_queue_messages(tmp_path): - """Messages left in the pending queue after _dispatch are re-published to the bus. - - This tests the finally-block cleanup that prevents message loss when - the runner exits early (e.g., max_iterations) with messages - still in the queue. - """ +async def test_busy_session_burst_reaches_next_model_call_as_one_ordered_batch(tmp_path): + """Messages accumulated during a model call share its next request snapshot.""" + from nanobot.agent.loop import AgentLoop from nanobot.bus.events import InboundMessage + from nanobot.bus.queue import MessageBus + + provider = MagicMock() + provider.get_default_model.return_value = "test-model" + first_request_started = asyncio.Event() + release_first_request = asyncio.Event() + second_request_started = asyncio.Event() + captured_messages: list[list[dict]] = [] + + async def chat_stream_with_retry(*, messages, **kwargs): + captured_messages.append([dict(message) for message in messages]) + if len(captured_messages) == 1: + first_request_started.set() + await release_first_request.wait() + return LLMResponse(content="first answer", tool_calls=[], usage=None) + second_request_started.set() + return LLMResponse(content="batch answer", tool_calls=[], usage=None) + + provider.chat_stream_with_retry = chat_stream_with_retry + loop = AgentLoop( + bus=MessageBus(), + provider=provider, + workspace=tmp_path, + model="test-model", + ) + loop.tools.get_definitions = MagicMock(return_value=[]) + followups = [f"follow-up-{index:02}" for index in range(12)] + + run_task = asyncio.create_task(loop.run()) + await loop.bus.publish_inbound(InboundMessage( + channel="cli", + sender_id="u", + chat_id="c", + content="initial", + )) + await asyncio.wait_for(first_request_started.wait(), timeout=2) + for content in followups: + await loop.bus.publish_inbound(InboundMessage( + channel="cli", + sender_id="u", + chat_id="c", + content=content, + )) + while loop.bus.inbound_size: + await asyncio.sleep(0) + + release_first_request.set() + await asyncio.wait_for(second_request_started.wait(), timeout=2) + for _ in range(200): + if "cli:c" not in loop._pending_queues: + break + await asyncio.sleep(0.01) + loop.stop() + await asyncio.wait_for(run_task, timeout=2) + + assert len(captured_messages) == 2 + second_request = "\n".join( + str(message.get("content", "")) for message in captured_messages[1] + ) + positions = [second_request.index(content) for content in followups] + assert positions == sorted(positions) + assert loop.bus.inbound_size == 0 + + +@pytest.mark.asyncio +async def test_session_worker_processes_leftovers_without_republishing(tmp_path): + """Messages left by one turn stay on the same FIFO worker.""" + from nanobot.bus.events import InboundMessage, OutboundMessage loop = _make_loop(tmp_path) - bus = loop.bus - - # Simulate a completed dispatch by manually registering a queue - # with leftover messages, then running the cleanup logic directly. - pending = asyncio.Queue(maxsize=20) session_key = "cli:c" + pending = asyncio.Queue() + processed: list[str] = [] + + async def _process_message(msg, **_kwargs): + processed.append(msg.content) + return OutboundMessage(channel="cli", chat_id="c", content=msg.content) + + loop._process_message = _process_message # type: ignore[method-assign] + for content in ("first", "leftover-1", "leftover-2"): + pending.put_nowait(InboundMessage( + channel="cli", + sender_id="u", + chat_id="c", + content=content, + )) loop._pending_queues[session_key] = pending - pending.put_nowait(InboundMessage(channel="cli", sender_id="u", chat_id="c", content="leftover-1")) - pending.put_nowait(InboundMessage(channel="cli", sender_id="u", chat_id="c", content="leftover-2")) - # Execute the cleanup logic from the finally block - queue = loop._pending_queues.pop(session_key, None) - assert queue is not None - leftover = 0 - while True: - try: - item = queue.get_nowait() - except asyncio.QueueEmpty: - break - await bus.publish_inbound(item) - leftover += 1 + await loop._run_session_queue(session_key, pending) - assert leftover == 2 - - # Verify the messages are now on the bus - msgs = [] - while not bus.inbound.empty(): - msgs.append(await asyncio.wait_for(bus.consume_inbound(), timeout=0.5)) - contents = [m.content for m in msgs] - assert "leftover-1" in contents - assert "leftover-2" in contents + assert processed == ["first", "leftover-1", "leftover-2"] + assert session_key not in loop._pending_queues + assert loop.bus.inbound_size == 0 @pytest.mark.asyncio @@ -1671,6 +1799,14 @@ async def test_drain_injections_on_llm_error(): call_count["n"] += 1 requests.append(messages) if call_count["n"] == 1: + await injection_queue.put( + InboundMessage( + channel="cli", + sender_id="u", + chat_id="c", + content="follow-up after LLM error", + ) + ) return LLMResponse( content=None, tool_calls=[], @@ -1687,10 +1823,6 @@ async def test_drain_injections_on_llm_error(): injection_queue = asyncio.Queue() inject_cb = _make_injection_callback(injection_queue) - await injection_queue.put( - InboundMessage(channel="cli", sender_id="u", chat_id="c", content="follow-up after LLM error") - ) - runner = AgentRunner() result = await runner.run(make_run_spec(provider, initial_messages=None, @@ -1739,6 +1871,13 @@ async def test_drain_injections_on_empty_final_response(): async def chat_stream_with_retry(*, messages, **kwargs): call_count["n"] += 1 if call_count["n"] <= _MAX_EMPTY_RETRIES + 1: + if call_count["n"] == _MAX_EMPTY_RETRIES + 1: + await injection_queue.put(InboundMessage( + channel="cli", + sender_id="u", + chat_id="c", + content="follow-up after empty", + )) return LLMResponse(content="", tool_calls=[], usage=None) # After retries exhausted + injection drain, respond normally return LLMResponse(content="answer after empty", tool_calls=[], usage=None) @@ -1750,10 +1889,6 @@ async def test_drain_injections_on_empty_final_response(): injection_queue = asyncio.Queue() inject_cb = _make_injection_callback(injection_queue) - await injection_queue.put( - InboundMessage(channel="cli", sender_id="u", chat_id="c", content="follow-up after empty") - ) - runner = AgentRunner() result = await runner.run(make_run_spec(provider, initial_messages=[ @@ -1778,13 +1913,9 @@ async def test_drain_injections_on_empty_final_response(): @pytest.mark.asyncio -async def test_drain_injections_on_max_iterations(): - """Pending injections should be drained when the runner hits max_iterations. - - Unlike other error paths, max_iterations cannot continue the loop, so - injections are appended to messages but not processed by the LLM. - The key point is they are consumed from the queue to prevent re-publish. - """ +async def test_max_iterations_without_finalization_keeps_late_injection_queued(): + """Never consume a user message when no later model request can observe it.""" + from nanobot.agent.hook import AgentHook from nanobot.agent.runner import AgentRunner from nanobot.bus.events import InboundMessage @@ -1807,9 +1938,15 @@ async def test_drain_injections_on_max_iterations(): injection_queue = asyncio.Queue() inject_cb = _make_injection_callback(injection_queue) - await injection_queue.put( - InboundMessage(channel="cli", sender_id="u", chat_id="c", content="follow-up after max iters") - ) + class InjectAfterLastIterationHook(AgentHook): + async def after_iteration(self, context) -> None: + if context.iteration == 1: + await injection_queue.put(InboundMessage( + channel="cli", + sender_id="u", + chat_id="c", + content="follow-up after max iters", + )) runner = AgentRunner() result = await runner.run(make_run_spec(provider, @@ -1819,32 +1956,30 @@ async def test_drain_injections_on_max_iterations(): max_iterations=2, max_tool_result_chars=_MAX_TOOL_RESULT_CHARS, injection_callback=inject_cb, + hook=InjectAfterLastIterationHook(), + finalize_on_max_iterations=False, )) assert result.stop_reason == "max_iterations" - assert result.had_injections is True - # The injection was consumed from the queue (preventing re-publish) - assert injection_queue.empty() - # The injection message is appended to conversation history - injected = [ - m for m in result.messages - if m.get("role") == "user" and m.get("content") == "follow-up after max iters" - ] - assert len(injected) == 1 + assert result.had_injections is False + assert injection_queue.qsize() == 1 + assert (await injection_queue.get()).content == "follow-up after max iters" @pytest.mark.asyncio -async def test_drain_injections_set_flag_when_followup_arrives_after_last_iteration(): - """Late follow-ups drained in max_iterations should still flip had_injections.""" +async def test_max_iterations_finalization_consumes_late_injection(): + """The finalization model request receives the snapshot after the last iteration.""" from nanobot.agent.hook import AgentHook from nanobot.agent.runner import AgentRunner from nanobot.bus.events import InboundMessage provider = MagicMock() call_count = {"n": 0} + captured_messages: list[list[dict]] = [] async def chat_stream_with_retry(*, messages, **kwargs): call_count["n"] += 1 + captured_messages.append([dict(message) for message in messages]) return LLMResponse( content="", tool_calls=[ToolCallRequest(id=f"c{call_count['n']}", name="read_file", arguments={"path": "x"})], @@ -1889,6 +2024,11 @@ async def test_drain_injections_set_flag_when_followup_arrives_after_last_iterat assert result.stop_reason == "max_iterations" assert result.had_injections is True assert injection_queue.empty() + assert call_count["n"] == 3 + finalization_request = "\n".join( + str(message.get("content", "")) for message in captured_messages[-1] + ) + assert "late follow-up after max iters" in finalization_request injected = [ m for m in result.messages if m.get("role") == "user" and m.get("content") == "late follow-up after max iters" @@ -1897,16 +2037,24 @@ async def test_drain_injections_set_flag_when_followup_arrives_after_last_iterat @pytest.mark.asyncio -async def test_injection_cycle_cap_on_error_path(): - """Injection cycles should be capped even when every iteration hits an LLM error.""" - from nanobot.agent.runner import _MAX_INJECTION_CYCLES, AgentRunner +async def test_error_path_is_not_stopped_by_an_arbitrary_injection_cap(): + """Error recovery consumes every arrived snapshot until no follow-up remains.""" + from nanobot.agent.runner import AgentRunner from nanobot.bus.events import InboundMessage provider = MagicMock() call_count = {"n": 0} + injection_queue = asyncio.Queue() async def chat_stream_with_retry(*, messages, **kwargs): call_count["n"] += 1 + if call_count["n"] <= 7: + await injection_queue.put(InboundMessage( + channel="cli", + sender_id="u", + chat_id="c", + content=f"msg-{call_count['n']}", + )) return LLMResponse( content=None, tool_calls=[], @@ -1918,13 +2066,7 @@ async def test_injection_cycle_cap_on_error_path(): tools = MagicMock() tools.get_definitions.return_value = [] - drain_count = {"n": 0} - - async def inject_cb(): - drain_count["n"] += 1 - if drain_count["n"] <= _MAX_INJECTION_CYCLES: - return [InboundMessage(channel="cli", sender_id="u", chat_id="c", content=f"msg-{drain_count['n']}")] - return [] + inject_cb = _make_injection_callback(injection_queue) runner = AgentRunner() result = await runner.run(make_run_spec(provider, @@ -1941,5 +2083,4 @@ async def test_injection_cycle_cap_on_error_path(): )) assert result.had_injections is True - # Should cap: _MAX_INJECTION_CYCLES drained rounds + 1 final round that breaks - assert call_count["n"] == _MAX_INJECTION_CYCLES + 1 + assert call_count["n"] == 8 diff --git a/tests/tools/test_message_tool_suppress.py b/tests/tools/test_message_tool_suppress.py index 1b4348e57..e3dcb03ba 100644 --- a/tests/tools/test_message_tool_suppress.py +++ b/tests/tools/test_message_tool_suppress.py @@ -138,7 +138,20 @@ class TestMessageToolSuppressLogic: LLMResponse(content="", tool_calls=[]), LLMResponse(content="", tool_calls=[]), ]) - loop.provider.chat_stream_with_retry = AsyncMock(side_effect=lambda *a, **kw: next(calls)) + pending_queue: asyncio.Queue[InboundMessage] = asyncio.Queue() + + async def next_response(*_args, **_kwargs): + response = next(calls) + if response.content == "First answer": + pending_queue.put_nowait(InboundMessage( + channel="feishu", + sender_id="user1", + chat_id="chat123", + content="follow-up", + )) + return response + + loop.provider.chat_stream_with_retry = AsyncMock(side_effect=next_response) loop.tools.get_definitions = MagicMock(return_value=[]) sent: list[OutboundMessage] = [] @@ -146,11 +159,6 @@ class TestMessageToolSuppressLogic: if isinstance(mt, MessageTool): mt.set_send_callback(AsyncMock(side_effect=lambda m: sent.append(m))) - pending_queue = asyncio.Queue() - await pending_queue.put( - InboundMessage(channel="feishu", sender_id="user1", chat_id="chat123", content="follow-up") - ) - msg = InboundMessage(channel="feishu", sender_id="user1", chat_id="chat123", content="Start") result = await loop._process_message(msg, pending_queue=pending_queue)