fix(agent): preserve queued turn semantics and completion

This commit is contained in:
chengyongru authored and chengyongru committed 2026-09-17 22:18:38 +08:00
1 parent b15a5e8cb4
commit 7208fb663f
7 files changed
+614 -11

No files matched your search

+4
View File
@@ -58,6 +58,10 @@ class AutomationTurnCoordinator:
self._waiters: dict[str, asyncio.Future[OutboundMessage | None]] = {}
self._pending_messages_by_turn_id: dict[str, InboundMessage] = {}
def owns_turn(self, msg: InboundMessage) -> bool:
"""Whether this message requires an independent automation completion."""
return bool(self._turn_id(msg))
async def submit(self, msg: InboundMessage) -> OutboundMessage | None:
"""Submit an automation turn and wait for its session response."""
turn_id = self._turn_id(msg)
+76 -7
View File
@@ -73,7 +73,7 @@ from nanobot.security.workspace_access import (
)
from nanobot.session import turn_continuation
from nanobot.session.automation_turns import automation_history_overrides
from nanobot.session.goal_state import goal_state_runtime_lines
from nanobot.session.goal_state import goal_state_runtime_lines, sustained_goal_active
from nanobot.session.history_visibility import HIDDEN_HISTORY_META
from nanobot.session.keys import UNIFIED_SESSION_KEY, remember_last_channel
from nanobot.session.manager import SESSION_CACHE_MAX_SIZE, Session, SessionManager
@@ -406,13 +406,13 @@ class AgentLoop:
self._deferred_automation_turns: dict[str, list[InboundMessage]] = {}
self._cron_turns = CronTurnCoordinator(
publish_inbound=self.bus.publish_inbound,
dispatch=self._dispatch,
dispatch=partial(self._dispatch, wait_for_worker=False),
is_running=lambda: self._running,
deferred_queues=self._deferred_automation_turns,
)
self._local_trigger_turns = LocalTriggerTurnCoordinator(
publish_inbound=self.bus.publish_inbound,
dispatch=self._dispatch,
dispatch=partial(self._dispatch, wait_for_worker=False),
is_running=lambda: self._running,
deferred_queues=self._deferred_automation_turns,
)
@@ -870,11 +870,17 @@ class AgentLoop:
Returns the total number of cancelled tasks, subagents, and exec sessions.
"""
pending = self._pending_queues.get(key)
tasks = tuple(self._active_tasks.pop(key, set()))
cancelled = sum(1 for t in tasks if not t.done() and t.cancel())
for t in tasks:
with suppress(asyncio.CancelledError, Exception):
await t
if tasks 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())
sub_cancelled = await self.subagents.cancel_by_session(key)
exec_cancelled = await self._exec_session_manager.terminate_by_owner(key)
return cancelled + sub_cancelled + exec_cancelled
@@ -899,6 +905,16 @@ class AgentLoop:
return UNIFIED_SESSION_KEY
return msg.session_key
def _can_inject_message(self, msg: InboundMessage) -> bool:
"""Keep independent turns and controls out of user-input batches."""
if turn_continuation.internal_continuation_inbound(msg.metadata) or any(
coordinator.owns_turn(msg) for _, coordinator in self._automation_turn_coordinators
):
return False
return msg.channel == "system" or not self.commands.is_dispatchable_command(
msg.content.strip()
)
def _idle_events(
self,
session_key: str,
@@ -1064,7 +1080,25 @@ class AgentLoop:
row[PENDING_FOLLOWUP_ID_KEY] = followup_id
return row
return [await _to_user_message(pending_msg) for pending_msg in pending_messages]
consumed = 0
try:
converted: list[dict[str, Any]] = []
for pending_msg in pending_messages:
# Independent turns are FIFO barriers. Their worker path
# owns completion, command dispatch, and control metadata.
if not self._can_inject_message(pending_msg):
break
converted.append(await _to_user_message(pending_msg))
consumed = len(converted)
return converted
finally:
# Commit only a successfully converted prefix. On failure or
# cancellation, return the whole snapshot ahead of later arrivals.
unconsumed = pending_messages[consumed:]
if unconsumed:
later = [pending_queue.get_nowait() for _ in range(pending_queue.qsize())]
for pending_msg in [*unconsumed, *later]:
pending_queue.put_nowait(pending_msg)
terminal_wait_deadline: float | None = None
@@ -1373,6 +1407,8 @@ class AgentLoop:
msg: InboundMessage,
) -> InboundMessage:
"""Persist a recoverable WebUI follow-up before adding it to the inbox."""
if not self._can_inject_message(msg):
return msg
session = self.sessions.get_or_create(session_key)
followup_id = record_pending_followup(session, msg)
if followup_id is None:
@@ -1387,8 +1423,8 @@ class AgentLoop:
self.sessions.save(session)
return pending_msg
async def _dispatch(self, msg: InboundMessage) -> None:
"""Submit one direct caller message through the per-session FIFO."""
async def _dispatch(self, msg: InboundMessage, *, wait_for_worker: bool = True) -> None:
"""Enqueue a message, optionally awaiting a newly created session worker."""
session_key = self._effective_session_key(msg)
if session_key != msg.session_key:
msg = dataclasses.replace(msg, session_key_override=session_key)
@@ -1401,7 +1437,13 @@ class AgentLoop:
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)
if wait_for_worker:
await self._run_session_queue(session_key, new_pending)
else:
# Automation callers await their own completion, not every later
# turn handled by this worker. Session cancellation still owns it.
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,
@@ -1424,6 +1466,9 @@ class AgentLoop:
"Session worker failed one message for {}; continuing FIFO",
session_key,
)
except asyncio.CancelledError as exc:
await self._cancel_pending_messages(session_key, pending, exc)
raise
finally:
owns_queue = self._pending_queues.get(session_key) is pending
if owns_queue:
@@ -1431,6 +1476,24 @@ class AgentLoop:
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 _cancel_pending_messages(
self,
session_key: str,
pending: asyncio.Queue[InboundMessage],
error: asyncio.CancelledError,
) -> None:
"""Complete queued independent turns after their session worker stops."""
while not pending.empty():
msg = pending.get_nowait()
for _, coordinator in self._automation_turn_coordinators:
coordinator.complete(msg, error=error)
if (
msg.channel != "system"
and normalize_command_text(msg.content).lower() == "/compact"
):
delivery = self.turn_delivery_factory.unrouted(msg, session_key)
await delivery.complete(None, publish_completion=True)
async def _dispatch_one(
self,
msg: InboundMessage,
@@ -1460,6 +1523,12 @@ class AgentLoop:
completion_published = False
try:
async with lock, gate:
# A preceding user turn may have completed or blocked the goal
# while its older continuation was still waiting in the inbox.
if turn_continuation.internal_continuation_inbound(msg.metadata) and not (
sustained_goal_active(self.sessions.get_or_create(session_key).metadata)
):
return
try:
delivery = self.turn_delivery_factory.create(
msg,
+22 -4
View File
@@ -370,6 +370,7 @@ class AgentRunner:
# Segments from one uninterrupted length-recovery chain. Tool work or
# injected user input starts a new logical answer and clears the chain.
length_recovery_parts: list[str] = []
pending_length_segment: tuple[AgentHookContext, str] | None = None
had_injections = False
injection_cycles = 0
pending_stream_content: str | None = None
@@ -397,6 +398,20 @@ class AgentRunner:
events=spec.events,
)
async def end_length_segment(*, interrupted: bool) -> None:
nonlocal pending_length_segment
if pending_length_segment is None:
return
segment_context, segment_content = pending_length_segment
pending_length_segment = None
if interrupted:
length_recovery_parts.clear()
else:
messages.append(build_length_recovery_message(segment_content))
if hook.wants_streaming():
segment_context.stream_continues_current_message = not interrupted
await hook.on_stream_end(segment_context, resuming=True)
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
@@ -410,6 +425,7 @@ class AgentRunner:
)
if drained_before_request:
had_injections = True
await end_length_segment(interrupted=drained_before_request)
context = AgentHookContext(
iteration=iteration,
messages=messages,
@@ -610,9 +626,6 @@ class AgentRunner:
len(length_recovery_parts),
_MAX_LENGTH_RECOVERIES,
)
if hook.wants_streaming():
context.stream_continues_current_message = True
await hook.on_stream_end(context, resuming=True)
messages.append(conversation_state.project_response_message(
build_assistant_message(
clean,
@@ -621,7 +634,9 @@ class AgentRunner:
),
response,
))
messages.append(build_length_recovery_message(clean or ""))
# The next input snapshot decides whether to continue this
# answer or close its stream before answering a new question.
pending_length_segment = (context, clean or "")
await hook.after_iteration(context)
continue
@@ -782,6 +797,7 @@ class AgentRunner:
)
if drained_after_max_iterations:
had_injections = True
await end_length_segment(interrupted=drained_after_max_iterations)
terminal_content, usage = await self._try_finalize_after_max_iterations(
spec,
hook,
@@ -790,6 +806,8 @@ class AgentRunner:
request_state=request_state,
round_usages=round_usages,
)
else:
await end_length_segment(interrupted=False)
if terminal_content is None:
terminal_content = self._max_iterations_fallback(spec)
if length_recovery_parts:
+49
View File
@@ -579,6 +579,55 @@ class TestToolEventProgress:
assert [event.merge_next for event in endings] == [True, False]
assert {event.stream_id for event in [*deltas, *endings]} == {deltas[0].stream_id}
@pytest.mark.asyncio
@pytest.mark.parametrize("max_iterations", [1, 3])
async def test_followup_after_truncation_starts_a_new_stream(
self, tmp_path: Path, max_iterations: int,
) -> None:
loop = _make_loop(tmp_path)
loop.max_iterations = max_iterations
loop.tools.get_definitions = MagicMock(return_value=[])
_attach_webui_runtime_events(loop, loop.bus)
calls = 0
async def chat(*, on_content_delta=None, **kwargs):
nonlocal calls
calls += 1
if calls == 1:
await on_content_delta("old partial")
await loop._dispatch(InboundMessage(
channel="websocket", sender_id="u", chat_id="test", content="new question",
))
return LLMResponse(content="old partial", finish_reason="length")
if on_content_delta is not None:
await on_content_delta("new answer")
return LLMResponse(content="new answer", finish_reason="stop")
loop.provider.chat_stream_with_retry = chat
try:
await loop._dispatch(InboundMessage(
channel="websocket", sender_id="u", chat_id="test", content="old question",
metadata={"_wants_stream": True},
))
outbound = [loop.bus.outbound.get_nowait() for _ in range(loop.bus.outbound_size)]
deltas = [
message.event for message in outbound if isinstance(message.event, StreamDeltaEvent)
]
endings = [
message.event for message in outbound if isinstance(message.event, StreamEndEvent)
]
assert [event.content for event in deltas] == ["old partial", "new answer"]
assert deltas[0].stream_id != deltas[1].stream_id
assert [(event.resuming, event.merge_next) for event in endings] == [
(True, False), (False, False),
]
assert [
message.content for message in outbound
if isinstance(message.event, StreamedResponseEvent)
] == ["new answer"]
finally:
await loop.aclose()
@pytest.mark.asyncio
async def test_length_recovery_streams_non_delta_terminal_segment(
self,
+154
View File
@@ -530,6 +530,55 @@ async def test_injected_followup_starts_new_length_recovery_chain():
assert call_count == 6
@pytest.mark.asyncio
@pytest.mark.parametrize("max_iterations", [1, 3])
async def test_followup_interrupts_truncated_answer_before_next_request(max_iterations):
from nanobot.agent.hook import AgentHook, AgentHookContext
from nanobot.agent.runner import AgentRunner
provider = MagicMock()
tools = MagicMock()
tools.get_definitions.return_value = []
queue = asyncio.Queue()
requests = []
endings = []
class StreamingHook(AgentHook):
def wants_streaming(self) -> bool:
return True
async def on_stream_end(self, context: AgentHookContext, *, resuming: bool) -> None:
endings.append((resuming, context.stream_continues_current_message))
async def chat_stream_with_retry(*, messages, on_content_delta=None, **kwargs):
requests.append([dict(message) for message in messages])
if len(requests) == 1:
queue.put_nowait({"role": "user", "content": "Never mind. What is 2+2?"})
if on_content_delta is not None:
await on_content_delta("Unfinished old answer: ")
return LLMResponse(content="Unfinished old answer: ", finish_reason="length")
return LLMResponse(content="4", finish_reason="stop")
provider.chat_stream_with_retry = chat_stream_with_retry
result = await AgentRunner().run(make_run_spec(
provider,
initial_messages=[{"role": "user", "content": "old question"}],
tools=tools,
model="test-model",
max_iterations=max_iterations,
max_tool_result_chars=_MAX_TOOL_RESULT_CHARS,
hook=StreamingHook(),
injection_callback=_make_injection_callback(queue),
))
assert result.final_content == "4"
assert len(requests) == 2
assert endings[0] == (True, False)
second_request = "\n".join(str(message.get("content", "")) for message in requests[1])
assert "Never mind. What is 2+2?" in second_request
assert "Continue the same response from its exact endpoint" not in second_request
@pytest.mark.asyncio
async def test_checkpoint2_preserves_final_response_in_history_before_followup():
"""A follow-up injected after a final answer must still see that answer in history."""
@@ -1579,6 +1628,111 @@ async def test_pending_queue_snapshot_excludes_messages_arriving_during_conversi
assert "next snapshot" in second_request
@pytest.mark.asyncio
@pytest.mark.parametrize("cancel_conversion", [False, True])
async def test_pending_snapshot_rolls_back_before_later_arrivals(tmp_path, cancel_conversion):
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"
provider.chat_stream_with_retry = AsyncMock(return_value=LLMResponse(content="answer"))
loop = AgentLoop(bus=MessageBus(), provider=provider, workspace=tmp_path, model="test-model")
loop.tools.get_definitions = MagicMock(return_value=[])
pending = asyncio.Queue()
contents = ["before", "bad", "after"]
for content in contents:
pending.put_nowait(InboundMessage(
channel="cli", sender_id="u", chat_id="c", content=content,
))
failed = False
async def context_provider(request):
nonlocal failed
if request.original_user_text == "bad" and not failed:
failed = True
pending.put_nowait(InboundMessage(
channel="cli", sender_id="u", chat_id="c", content="later arrival",
))
if cancel_conversion:
raise asyncio.CancelledError
raise RuntimeError("context lookup temporarily unavailable")
return None
loop.register_runtime_context_provider(context_provider)
runtime = loop.llm_runtime()
try:
run = loop._run_agent_loop(
TranscriptInput(history=[{"role": "user", "content": "root"}], current_message=None),
runtime=runtime,
request_context=RequestContext(channel="cli", chat_id="c", runtime=runtime),
pending_queue=pending,
)
if cancel_conversion:
with pytest.raises(asyncio.CancelledError):
await run
assert [pending.get_nowait().content for _ in range(pending.qsize())] == [
*contents, "later arrival",
]
else:
result = await run
user_content = [
message["content"] for message in result.messages if message["role"] == "user"
]
assert user_content == ["root", *contents, "later arrival"]
assert pending.empty()
finally:
await loop.aclose()
@pytest.mark.asyncio
async def test_persistent_conversion_error_does_not_drop_later_session_inputs(tmp_path):
from nanobot.agent.loop import AgentLoop
from nanobot.bus.events import InboundMessage
from nanobot.bus.queue import MessageBus
from nanobot.bus.runtime_events import TurnCompleted
provider = MagicMock()
provider.get_default_model.return_value = "test-model"
loop = AgentLoop(bus=MessageBus(), provider=provider, workspace=tmp_path, model="test-model")
loop.tools.get_definitions = MagicMock(return_value=[])
completions = []
loop.bus.subscribe(completions.append, TurnCompleted)
calls = 0
async def chat(**kwargs):
nonlocal calls
calls += 1
if calls == 1:
for content in ["before", "bad", "after"]:
await loop._dispatch(InboundMessage(
channel="cli", sender_id="u", chat_id="c", content=content,
))
return LLMResponse(content="answer", finish_reason="stop")
async def context_provider(request):
if request.original_user_text == "bad":
raise RuntimeError("context unavailable")
return None
provider.chat_stream_with_retry = chat
loop.register_runtime_context_provider(context_provider)
try:
await loop._dispatch(InboundMessage(
channel="cli", sender_id="u", chat_id="c", content="root",
))
loop.sessions.invalidate("cli:c")
history = loop.sessions.get_or_create("cli:c").messages
assert [message["content"] for message in history if message["role"] == "user"] == [
"root", "before", "after",
]
assert sum(event.outcome == "failed" for event in completions) == 1
assert "cli:c" not in loop._pending_queues
finally:
await loop.aclose()
@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."""
+221
View File
@@ -0,0 +1,221 @@
"""Independent turns and internal controls retain their session inbox semantics."""
import asyncio
from unittest.mock import AsyncMock, MagicMock
import pytest
from nanobot.agent.automation_turns import AutomationTurnError
from nanobot.agent.loop import AgentLoop
from nanobot.agent.tools.context import current_request_context
from nanobot.bus.events import InboundMessage
from nanobot.bus.queue import MessageBus
from nanobot.cron.session_turns import CRON_DEFER_UNTIL_IDLE_META, CRON_TRIGGER_META
from nanobot.providers.base import GenerationSettings, LLMResponse, ToolCallRequest
from nanobot.runtime_context import public_history_messages
from nanobot.session.automation_turns import AUTOMATION_HISTORY_META
from nanobot.session.goal_state import GOAL_STATE_KEY
from nanobot.session.recovery import PENDING_FOLLOWUPS_KEY
from nanobot.triggers.local_session_turns import LOCAL_TRIGGER_META
@pytest.fixture
async def loop(tmp_path):
provider = MagicMock()
provider.get_default_model.return_value = "test-model"
provider.generation = GenerationSettings(max_tokens=100)
provider.can_resume_conversation_state.return_value = False
provider.estimate_prompt_tokens.return_value = (100, "test")
agent = AgentLoop(
bus=MessageBus(), provider=provider, workspace=tmp_path, model="test-model",
)
agent.tools.get_definitions = MagicMock(return_value=[])
try:
yield agent
finally:
await agent.aclose()
def _automation_message(kind: str, name: str) -> InboundMessage:
metadata = (
{
CRON_TRIGGER_META: {"job_id": name, "run_id": name, "persist_content": name},
CRON_DEFER_UNTIL_IDLE_META: True,
}
if kind == "cron"
else {
LOCAL_TRIGGER_META: {
"trigger_id": name, "delivery_id": name, "persist_content": name,
},
}
)
return InboundMessage(
channel="websocket", sender_id=kind, chat_id="test", content=name, metadata=metadata,
)
@pytest.mark.parametrize("first_kind", ["cron", "local_trigger"])
@pytest.mark.parametrize("second_fails", [False, True])
async def test_startup_automation_turns_complete_independently(loop, first_kind, second_fails):
started, release = asyncio.Event(), asyncio.Event()
release_second = asyncio.Event()
senders = []
async def chat(**kwargs):
senders.append(current_request_context().sender_id)
if len(senders) == 1:
started.set()
await release.wait()
return LLMResponse(content="first response")
await release_second.wait()
if second_fails:
raise RuntimeError("second task failed")
return LLMResponse(content="second response")
loop.provider.chat_stream_with_retry = chat
second_kind = "local_trigger" if first_kind == "cron" else "cron"
submit = {"cron": loop.submit_cron_turn, "local_trigger": loop.submit_local_trigger_turn}
first = asyncio.create_task(submit[first_kind](_automation_message(first_kind, "first")))
tasks = [first]
try:
await asyncio.wait_for(started.wait(), timeout=3)
second = asyncio.create_task(submit[second_kind](_automation_message(second_kind, "second")))
tasks.append(second)
await asyncio.sleep(0)
assert not second.done()
release.set()
first_response = await asyncio.wait_for(first, timeout=3)
assert first_response.content == "first response"
assert not second.done()
release_second.set()
if second_fails:
with pytest.raises(AutomationTurnError, match="second task failed"):
await asyncio.wait_for(second, timeout=3)
else:
second_response = await asyncio.wait_for(second, timeout=3)
assert second_response.content == "second response"
assert senders == [first_kind, second_kind]
assert loop.pending_cron_job_ids_for_session("websocket:test") == set()
assert loop.pending_local_trigger_ids_for_session("websocket:test") == set()
loop.sessions.invalidate("websocket:test")
session = loop.sessions.get_or_create("websocket:test")
user_rows = [message for message in session.messages if message["role"] == "user"]
assert [message[AUTOMATION_HISTORY_META]["kind"] for message in user_rows] == [
first_kind, second_kind,
]
assert PENDING_FOLLOWUPS_KEY not in session.metadata
finally:
release.set()
release_second.set()
for task in tasks:
if not task.done():
task.cancel()
await asyncio.gather(*tasks, return_exceptions=True)
async def test_cancelled_session_completes_queued_automation_waiters(loop):
started = asyncio.Event()
async def chat(**kwargs):
started.set()
await asyncio.Event().wait()
loop.provider.chat_stream_with_retry = chat
worker = asyncio.create_task(loop._dispatch(InboundMessage(
channel="websocket", sender_id="u", chat_id="test", content="user request",
)))
tasks = [worker]
try:
await asyncio.wait_for(started.wait(), timeout=3)
cron = asyncio.create_task(loop.submit_cron_turn(_automation_message("cron", "cron")))
trigger = asyncio.create_task(loop.submit_local_trigger_turn(
_automation_message("local_trigger", "trigger"),
))
tasks.extend([cron, trigger])
await asyncio.sleep(0)
assert loop._pending_queues["websocket:test"].qsize() == 2
worker.cancel()
with pytest.raises(asyncio.CancelledError):
await worker
for task in (cron, trigger):
with pytest.raises(AutomationTurnError, match="CancelledError"):
await asyncio.wait_for(task, timeout=3)
assert "websocket:test" not in loop._pending_queues
assert loop.pending_cron_job_ids_for_session("websocket:test") == set()
assert loop.pending_local_trigger_ids_for_session("websocket:test") == set()
session = loop.sessions.get_or_create("websocket:test")
assert PENDING_FOLLOWUPS_KEY not in session.metadata
finally:
for task in tasks:
if not task.done():
task.cancel()
await asyncio.gather(*tasks, return_exceptions=True)
async def test_stop_before_startup_worker_runs_completes_automation(loop):
loop.provider.chat_stream_with_retry = AsyncMock(return_value=LLMResponse(content="unused"))
submit = asyncio.create_task(loop.submit_cron_turn(_automation_message("cron", "queued")))
try:
await asyncio.sleep(0)
assert "websocket:test" in loop._pending_queues
await loop._cancel_active_tasks("websocket:test")
with pytest.raises(AutomationTurnError, match="CancelledError"):
await asyncio.wait_for(submit, timeout=3)
loop.provider.chat_stream_with_retry.assert_not_awaited()
assert "websocket:test" not in loop._pending_queues
assert loop.pending_cron_job_ids_for_session("websocket:test") == set()
finally:
if not submit.done():
submit.cancel()
await asyncio.gather(submit, return_exceptions=True)
@pytest.mark.parametrize("complete_on_followup", [False, True])
async def test_goal_continuation_after_followup_keeps_control_identity(loop, complete_on_followup):
loop.max_iterations = 1
loop.tools.prepare_call = MagicMock(return_value=(None, {}, None))
loop.tools.execute = AsyncMock(return_value="ok")
session = loop.sessions.get_or_create("cli:test")
session.metadata[GOAL_STATE_KEY] = {"status": "active", "objective": "Finish the long goal"}
loop.sessions.save(session)
requests = []
async def chat(*, messages, **kwargs):
requests.append([dict(message) for message in messages])
if len(requests) == 1:
await loop.bus.publish_inbound(InboundMessage(
channel="cli", sender_id="u", chat_id="test", content="Inspect file B first",
))
async with asyncio.timeout(3):
while loop._pending_queues["cli:test"].empty():
await asyncio.sleep(0)
elif complete_on_followup or len(requests) == 3:
session.metadata[GOAL_STATE_KEY]["status"] = "complete"
return LLMResponse(content="done")
return LLMResponse(
content="working", finish_reason="tool_calls",
tool_calls=[ToolCallRequest(id=f"call-{len(requests)}", name="noop", arguments={})],
)
loop.provider.chat_stream_with_retry = chat
run_task = asyncio.create_task(loop.run())
try:
await loop.bus.publish_inbound(InboundMessage(
channel="cli", sender_id="u", chat_id="test", content="Start the goal",
))
async with asyncio.timeout(5):
while not requests or "cli:test" in loop._pending_queues:
await asyncio.sleep(0.01)
assert len(requests) == (2 if complete_on_followup else 3)
continuation_text = "Continue the active sustained goal after the previous turn"
assert continuation_text not in str(requests[1])
if not complete_on_followup:
assert continuation_text in str(requests[2])
loop.sessions.invalidate("cli:test")
history = public_history_messages(loop.sessions.get_or_create("cli:test").messages)
assert [message["content"] for message in history if message["role"] == "user"] == [
"Start the goal", "Inspect file B first",
]
finally:
loop.stop()
await asyncio.wait_for(run_task, timeout=3)
+88
View File
@@ -221,6 +221,94 @@ async def test_stop_completes_a_compact_command_waiting_for_the_session_lock(loo
assert completions[0].context.metadata["webui_turn_id"] == "queued-compact"
@pytest.mark.asyncio
async def test_compact_is_a_fifo_barrier_during_an_active_turn(loop) -> None:
key = "cli:test"
started = asyncio.Event()
release = asyncio.Event()
requests = []
compacted_history = []
compact = loop.consolidator.compact_idle_session
async def capture_compaction(*args, **kwargs):
compacted_history.extend(
dict(message) for message in loop.sessions.get_or_create(key).messages
)
return await compact(*args, **kwargs)
loop.consolidator.compact_idle_session = capture_compaction
async def chat(*, messages, **kwargs):
requests.append([dict(message) for message in messages])
if len(requests) == 1:
started.set()
await release.wait()
return LLMResponse(content="answer", finish_reason="stop")
loop.provider.chat_stream_with_retry = chat
task = asyncio.create_task(loop._dispatch(InboundMessage(
channel="cli", sender_id="u", chat_id="test", content="initial question",
)))
try:
await asyncio.wait_for(started.wait(), timeout=5)
await loop._dispatch(InboundMessage(
channel="cli", sender_id="u", chat_id="test", content="before compaction",
))
command = InboundMessage(channel="cli", sender_id="u", chat_id="test", content="/compact")
await loop._dispatch_command_inline(command, key, command.content, loop.commands.dispatch)
await loop._dispatch(InboundMessage(
channel="cli", sender_id="u", chat_id="test", content="after compaction",
))
release.set()
await asyncio.wait_for(task, timeout=5)
finally:
release.set()
if not task.done():
task.cancel()
await asyncio.gather(task, return_exceptions=True)
assert [message["content"] for message in compacted_history if message["role"] == "user"] == [
"initial question", "before compaction",
]
assert all("/compact" != message.get("content") for request in requests for message in request)
assert "after compaction" in str(requests[-1])
events = [loop.bus.outbound.get_nowait().event for _ in range(loop.bus.outbound_size)]
assert [event.phase for event in events if isinstance(event, ContextCompactionEvent)] == [
"started", "succeeded",
]
@pytest.mark.asyncio
async def test_stop_completes_compact_queued_behind_an_active_turn(loop) -> None:
key = "websocket:test"
started = asyncio.Event()
async def chat(**kwargs):
started.set()
await asyncio.Event().wait()
loop.provider.chat_stream_with_retry = chat
completions = []
loop.bus.subscribe(completions.append, TurnCompleted)
task = asyncio.create_task(loop._dispatch(InboundMessage(
channel="websocket", sender_id="u", chat_id="test", content="question",
)))
loop._track_active_task(key, task)
await asyncio.wait_for(started.wait(), timeout=5)
command = InboundMessage(
channel="websocket", sender_id="u", chat_id="test", content="/compact",
metadata={"webui_turn_id": "queued-compact"},
)
await loop._dispatch_command_inline(command, key, command.content, loop.commands.dispatch)
await cmd_stop(CommandContext(msg=command, session=None, key=key, raw="/stop", loop=loop))
assert task.cancelled()
assert sum(
event.context.metadata.get("webui_turn_id") == "queued-compact" for event in completions
) == 1
assert key not in loop._pending_queues
@pytest.mark.asyncio
async def test_stop_finishes_inflight_compaction_as_cancelled(loop) -> None:
key = "websocket:test"