test(agent): consolidate session inbox coverage
This commit is contained in:
1 parent
5f378c9aff
commit
a0a99f24da
4 files changed
+94
-263
No files matched your search
@@ -581,12 +581,11 @@ class TestToolEventProgress:
|
||||
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,
|
||||
self, tmp_path: Path,
|
||||
) -> None:
|
||||
loop = _make_loop(tmp_path)
|
||||
loop.max_iterations = max_iterations
|
||||
loop.max_iterations = 1
|
||||
loop.tools.get_definitions = MagicMock(return_value=[])
|
||||
_attach_webui_runtime_events(loop, loop.bus)
|
||||
calls = 0
|
||||
|
||||
@@ -1098,49 +1098,6 @@ async def test_no_injections_flag_is_false_by_default():
|
||||
assert result.had_injections is False
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_pending_queue_cleanup_on_dispatch(tmp_path):
|
||||
"""The session worker removes its inbox after processing all messages."""
|
||||
loop = _make_loop(tmp_path)
|
||||
|
||||
async def chat_stream_with_retry(**kwargs):
|
||||
return LLMResponse(content="done", tool_calls=[], usage=None)
|
||||
|
||||
loop.provider.chat_stream_with_retry = chat_stream_with_retry
|
||||
|
||||
from nanobot.bus.events import InboundMessage
|
||||
|
||||
msg = InboundMessage(channel="cli", sender_id="u", chat_id="c", content="hello")
|
||||
# The queue should not exist before dispatch
|
||||
assert msg.session_key not in loop._pending_queues
|
||||
|
||||
await run_session(loop, msg)
|
||||
|
||||
# The queue should be cleaned up after dispatch
|
||||
assert msg.session_key not in loop._pending_queues
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
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"
|
||||
active_pending = asyncio.Queue()
|
||||
loop._pending_queues[session_key] = active_pending
|
||||
|
||||
loop._enqueue_session_message(
|
||||
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()
|
||||
assert (await active_pending.get()).content == "queued"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_followup_routed_to_pending_queue(tmp_path):
|
||||
"""Unified-session follow-ups should route into the active pending queue."""
|
||||
@@ -1267,215 +1224,6 @@ async def test_mid_turn_subagent_result_does_not_resolve_a_new_turn_route(tmp_pa
|
||||
route_policy.assert_not_called()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cron_turn_deferred_while_session_active(tmp_path):
|
||||
"""Cron turns wait for the active session instead of becoming injections."""
|
||||
from nanobot.bus.events import InboundMessage
|
||||
from nanobot.cron.session_turns import (
|
||||
CRON_DEFER_UNTIL_IDLE_META,
|
||||
CRON_TRIGGER_META,
|
||||
)
|
||||
|
||||
loop = _make_loop(tmp_path)
|
||||
|
||||
session_key = "websocket:chat-1"
|
||||
pending = asyncio.Queue(maxsize=20)
|
||||
loop._pending_queues[session_key] = pending
|
||||
|
||||
run_task = asyncio.create_task(loop.run())
|
||||
msg = InboundMessage(
|
||||
channel="websocket",
|
||||
sender_id="cron",
|
||||
chat_id="chat-1",
|
||||
content="scheduled work",
|
||||
metadata={
|
||||
CRON_TRIGGER_META: {"job_id": "job-1", "run_id": "run-1"},
|
||||
CRON_DEFER_UNTIL_IDLE_META: True,
|
||||
},
|
||||
session_key_override=session_key,
|
||||
)
|
||||
await loop.bus.publish_inbound(msg)
|
||||
|
||||
for _ in range(20):
|
||||
if loop._cron_turns.deferred_queues.get(session_key):
|
||||
break
|
||||
await asyncio.sleep(0.05)
|
||||
|
||||
loop.stop()
|
||||
await asyncio.wait_for(run_task, timeout=2)
|
||||
|
||||
assert pending.empty()
|
||||
assert loop._active_tasks == {}
|
||||
assert loop._cron_turns.deferred_queues[session_key] == [msg]
|
||||
assert loop.pending_cron_job_ids_for_session(session_key) == {"job-1"}
|
||||
|
||||
loop._process_message = AsyncMock(return_value=None)
|
||||
await loop._run_session_queue(session_key, pending)
|
||||
assert loop._process_message.await_args.args[0] is msg
|
||||
assert session_key not in loop._cron_turns.deferred_queues
|
||||
assert loop.pending_cron_job_ids_for_session(session_key) == set()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_local_trigger_turn_deferred_while_session_active(tmp_path):
|
||||
"""Local trigger turns wait for the active session instead of becoming injections."""
|
||||
from nanobot.bus.events import InboundMessage
|
||||
from nanobot.triggers.local_session_turns import LOCAL_TRIGGER_META
|
||||
|
||||
loop = _make_loop(tmp_path)
|
||||
|
||||
session_key = "websocket:chat-1"
|
||||
pending = asyncio.Queue(maxsize=20)
|
||||
loop._pending_queues[session_key] = pending
|
||||
|
||||
run_task = asyncio.create_task(loop.run())
|
||||
msg = InboundMessage(
|
||||
channel="websocket",
|
||||
sender_id="trigger",
|
||||
chat_id="chat-1",
|
||||
content="review failed CI",
|
||||
metadata={
|
||||
LOCAL_TRIGGER_META: {
|
||||
"trigger_id": "trg_123",
|
||||
"trigger_name": "CI review",
|
||||
"delivery_id": "tdl_123",
|
||||
},
|
||||
},
|
||||
session_key_override=session_key,
|
||||
)
|
||||
await loop.bus.publish_inbound(msg)
|
||||
|
||||
for _ in range(20):
|
||||
if loop._local_trigger_turns.deferred_queues.get(session_key):
|
||||
break
|
||||
await asyncio.sleep(0.05)
|
||||
|
||||
loop.stop()
|
||||
await asyncio.wait_for(run_task, timeout=2)
|
||||
|
||||
assert pending.empty()
|
||||
assert loop._active_tasks == {}
|
||||
assert loop._local_trigger_turns.deferred_queues[session_key] == [msg]
|
||||
assert loop.pending_local_trigger_ids_for_session(session_key) == {"trg_123"}
|
||||
|
||||
loop._process_message = AsyncMock(return_value=None)
|
||||
await loop._run_session_queue(session_key, pending)
|
||||
assert loop._process_message.await_args.args[0] is msg
|
||||
assert session_key not in loop._local_trigger_turns.deferred_queues
|
||||
assert loop.pending_local_trigger_ids_for_session(session_key) == set()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_submitted_cron_turn_reports_pending_until_completed(tmp_path):
|
||||
"""Bound cron jobs remain marked pending while their session turn is in flight."""
|
||||
from nanobot.bus.events import InboundMessage, OutboundMessage
|
||||
from nanobot.cron.session_turns import CRON_TRIGGER_META
|
||||
|
||||
loop = _make_loop(tmp_path)
|
||||
loop._cron_turns._enqueue = MagicMock()
|
||||
|
||||
session_key = "websocket:chat-1"
|
||||
msg = InboundMessage(
|
||||
channel="websocket",
|
||||
sender_id="cron",
|
||||
chat_id="chat-1",
|
||||
content="scheduled work",
|
||||
metadata={CRON_TRIGGER_META: {"job_id": "job-1", "run_id": "run-1"}},
|
||||
session_key_override=session_key,
|
||||
)
|
||||
|
||||
submit_task = asyncio.create_task(loop.submit_cron_turn(msg))
|
||||
await asyncio.sleep(0)
|
||||
assert loop.pending_cron_job_ids_for_session(session_key) == {"job-1"}
|
||||
|
||||
response = OutboundMessage(
|
||||
channel="websocket",
|
||||
chat_id="chat-1",
|
||||
content="done",
|
||||
)
|
||||
loop._cron_turns.complete(msg, response=response)
|
||||
|
||||
assert await asyncio.wait_for(submit_task, timeout=0.5) is response
|
||||
assert loop.pending_cron_job_ids_for_session(session_key) == set()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_submitted_local_trigger_turn_reports_pending_until_completed(tmp_path):
|
||||
"""Local triggers remain marked pending while their session turn is in flight."""
|
||||
from nanobot.bus.events import InboundMessage, OutboundMessage
|
||||
from nanobot.triggers.local_session_turns import LOCAL_TRIGGER_META
|
||||
|
||||
loop = _make_loop(tmp_path)
|
||||
loop._local_trigger_turns._enqueue = MagicMock()
|
||||
|
||||
session_key = "websocket:chat-1"
|
||||
msg = InboundMessage(
|
||||
channel="websocket",
|
||||
sender_id="trigger",
|
||||
chat_id="chat-1",
|
||||
content="review failed CI",
|
||||
metadata={
|
||||
LOCAL_TRIGGER_META: {
|
||||
"trigger_id": "trg_123",
|
||||
"trigger_name": "CI review",
|
||||
"delivery_id": "tdl_123",
|
||||
},
|
||||
},
|
||||
session_key_override=session_key,
|
||||
)
|
||||
|
||||
submit_task = asyncio.create_task(loop.submit_local_trigger_turn(msg))
|
||||
await asyncio.sleep(0)
|
||||
assert loop.pending_local_trigger_ids_for_session(session_key) == {"trg_123"}
|
||||
|
||||
response = OutboundMessage(
|
||||
channel="websocket",
|
||||
chat_id="chat-1",
|
||||
content="done",
|
||||
)
|
||||
loop._local_trigger_turns.complete(msg, response=response)
|
||||
|
||||
assert await asyncio.wait_for(submit_task, timeout=0.5) is response
|
||||
assert loop.pending_local_trigger_ids_for_session(session_key) == set()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_local_trigger_turn_cancellation_reports_agent_failure(tmp_path):
|
||||
"""A cancelled agent turn should not cancel the local-trigger worker."""
|
||||
from nanobot.agent.automation_turns import AutomationTurnError
|
||||
from nanobot.bus.events import InboundMessage
|
||||
from nanobot.triggers.local_session_turns import LOCAL_TRIGGER_META
|
||||
|
||||
loop = _make_loop(tmp_path)
|
||||
loop._local_trigger_turns._enqueue = MagicMock()
|
||||
|
||||
session_key = "websocket:chat-1"
|
||||
msg = InboundMessage(
|
||||
channel="websocket",
|
||||
sender_id="trigger",
|
||||
chat_id="chat-1",
|
||||
content="review failed CI",
|
||||
metadata={
|
||||
LOCAL_TRIGGER_META: {
|
||||
"trigger_id": "trg_123",
|
||||
"trigger_name": "CI review",
|
||||
"delivery_id": "tdl_123",
|
||||
},
|
||||
},
|
||||
session_key_override=session_key,
|
||||
)
|
||||
|
||||
submit_task = asyncio.create_task(loop.submit_local_trigger_turn(msg))
|
||||
await asyncio.sleep(0)
|
||||
|
||||
loop._local_trigger_turns.complete(msg, error=asyncio.CancelledError())
|
||||
|
||||
with pytest.raises(AutomationTurnError, match="CancelledError"):
|
||||
await asyncio.wait_for(submit_task, timeout=0.5)
|
||||
assert not submit_task.cancelled()
|
||||
assert loop.pending_local_trigger_ids_for_session(session_key) == set()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
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."""
|
||||
|
||||
@@ -57,9 +57,82 @@ def _automation_message(kind: str, name: str) -> InboundMessage:
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("first_kind", ["cron", "local_trigger"])
|
||||
@pytest.mark.parametrize("second_fails", [False, True])
|
||||
@pytest.mark.parametrize("bus_running", [False, True])
|
||||
@pytest.mark.parametrize("kind", ["cron", "local_trigger"])
|
||||
async def test_automation_turn_is_deferred_while_session_active(loop, kind):
|
||||
key = "websocket:test"
|
||||
pending = asyncio.Queue()
|
||||
loop._pending_queues[key] = pending
|
||||
coordinator = loop._cron_turns if kind == "cron" else loop._local_trigger_turns
|
||||
pending_ids = (
|
||||
loop.pending_cron_job_ids_for_session
|
||||
if kind == "cron"
|
||||
else loop.pending_local_trigger_ids_for_session
|
||||
)
|
||||
msg = _automation_message(kind, "deferred")
|
||||
run_task = asyncio.create_task(loop.run())
|
||||
|
||||
try:
|
||||
await loop.bus.publish_inbound(msg)
|
||||
async with asyncio.timeout(3):
|
||||
while not coordinator.deferred_queues.get(key):
|
||||
await asyncio.sleep(0)
|
||||
loop.stop()
|
||||
await asyncio.wait_for(run_task, timeout=3)
|
||||
|
||||
assert pending.empty()
|
||||
assert loop._active_tasks == {}
|
||||
assert coordinator.deferred_queues[key] == [msg]
|
||||
assert pending_ids(key) == {"deferred"}
|
||||
|
||||
loop._process_message = AsyncMock(return_value=None)
|
||||
await loop._run_session_queue(key, pending)
|
||||
assert loop._process_message.await_args.args[0] is msg
|
||||
assert key not in coordinator.deferred_queues
|
||||
assert pending_ids(key) == set()
|
||||
finally:
|
||||
loop.stop()
|
||||
if not run_task.done():
|
||||
run_task.cancel()
|
||||
await asyncio.gather(run_task, return_exceptions=True)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("kind", ["cron", "local_trigger"])
|
||||
async def test_submitted_automation_reports_pending_until_completed(loop, kind):
|
||||
coordinator = loop._cron_turns if kind == "cron" else loop._local_trigger_turns
|
||||
coordinator._enqueue = MagicMock()
|
||||
submit = loop.submit_cron_turn if kind == "cron" else loop.submit_local_trigger_turn
|
||||
pending_ids = (
|
||||
loop.pending_cron_job_ids_for_session
|
||||
if kind == "cron"
|
||||
else loop.pending_local_trigger_ids_for_session
|
||||
)
|
||||
msg = _automation_message(kind, "pending")
|
||||
submit_task = asyncio.create_task(submit(msg))
|
||||
|
||||
try:
|
||||
await asyncio.sleep(0)
|
||||
assert pending_ids(msg.session_key) == {"pending"}
|
||||
|
||||
response = OutboundMessage(channel="websocket", chat_id="test", content="done")
|
||||
coordinator.complete(msg, response=response)
|
||||
|
||||
assert await asyncio.wait_for(submit_task, timeout=1) is response
|
||||
assert pending_ids(msg.session_key) == set()
|
||||
finally:
|
||||
if not submit_task.done():
|
||||
submit_task.cancel()
|
||||
await asyncio.gather(submit_task, return_exceptions=True)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("first_kind", "second_fails", "bus_running"),
|
||||
[
|
||||
("cron", False, False),
|
||||
("cron", True, True),
|
||||
("local_trigger", False, True),
|
||||
("local_trigger", True, False),
|
||||
],
|
||||
)
|
||||
async def test_automation_turns_complete_independently(loop, first_kind, second_fails, bus_running):
|
||||
started, release = asyncio.Event(), asyncio.Event()
|
||||
release_second = asyncio.Event()
|
||||
@@ -182,9 +255,15 @@ async def test_cancel_before_worker_runs_completes_automation(loop, action):
|
||||
await asyncio.gather(submit, return_exceptions=True)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("kind", ["cron", "local_trigger"])
|
||||
@pytest.mark.parametrize("action", ["stop", "close"])
|
||||
@pytest.mark.parametrize("waiting_for", ["session_lock", "capacity"])
|
||||
@pytest.mark.parametrize(
|
||||
("kind", "action", "waiting_for"),
|
||||
[
|
||||
("cron", "stop", "session_lock"),
|
||||
("cron", "close", "capacity"),
|
||||
("local_trigger", "stop", "capacity"),
|
||||
("local_trigger", "close", "session_lock"),
|
||||
],
|
||||
)
|
||||
async def test_cancel_before_execution_completes_automation(loop, kind, action, waiting_for):
|
||||
key = "websocket:test"
|
||||
if waiting_for == "capacity":
|
||||
|
||||
@@ -89,8 +89,13 @@ async def test_compact_emits_one_lifecycle_and_keeps_the_session(loop, command)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("trigger", ["manual", "idle"])
|
||||
@pytest.mark.parametrize("summary", ["The checkpoint inspection is complete.", "(nothing)"])
|
||||
@pytest.mark.parametrize(
|
||||
("trigger", "summary"),
|
||||
[
|
||||
("manual", "The checkpoint inspection is complete."),
|
||||
("idle", "(nothing)"),
|
||||
],
|
||||
)
|
||||
async def test_compacted_session_waits_for_new_input_without_continuation(
|
||||
loop, trigger, summary,
|
||||
) -> None:
|
||||
|
||||
Reference in new issue
Block a user