refactor(command): declare session queue policy at registration (#6170)

* refactor(command): distinguish handled commands without replies

* refactor(command): declare session queue scheduling at registration
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-12 13:38:14 +08:00
1 parent 61fa3aa83c
commit 67742b2880
4 files changed
+39 -27

No files matched your search

+19 -13
View File
@@ -12,7 +12,7 @@ from nanobot.bus.events import InboundMessage
from nanobot.bus.outbound_events import ContextCompactionEvent
from nanobot.bus.queue import MessageBus
from nanobot.bus.runtime_events import TurnCompleted
from nanobot.command.builtin import cmd_stop
from nanobot.command.builtin import cmd_compact, cmd_stop
from nanobot.command.router import CommandContext
from nanobot.providers.base import GenerationSettings, LLMResponse, ProviderConversationState
from nanobot.runtime_context import public_history_message
@@ -38,6 +38,7 @@ async def loop(tmp_path):
context_window_tokens=128_000,
)
loop.tools.get_definitions = MagicMock(return_value=[])
loop.commands.exact("/summarize", cmd_compact, session_queue=True)
try:
yield loop
finally:
@@ -45,7 +46,7 @@ async def loop(tmp_path):
@pytest.mark.asyncio
@pytest.mark.parametrize("command", ["/compact", " /COMPACT@nanobot "])
@pytest.mark.parametrize("command", ["/compact", " /COMPACT@nanobot ", "/summarize"])
async def test_compact_emits_one_lifecycle_and_keeps_the_session(loop, command) -> None:
bus = loop.bus
session = loop.sessions.get_or_create("cli:test")
@@ -191,10 +192,11 @@ async def test_empty_compact_finishes_silently_and_does_not_schedule_idle_archiv
@pytest.mark.asyncio
async def test_compact_during_active_turn_waits_for_the_session_lock(loop) -> None:
@pytest.mark.parametrize("command", ["/COMPACT@nanobot", "/summarize"])
async def test_compact_during_active_turn_waits_for_the_session_lock(loop, command) -> None:
key = "websocket:test"
msg = InboundMessage(
channel="websocket", sender_id="user", chat_id="test", content="/COMPACT@nanobot",
channel="websocket", sender_id="user", chat_id="test", content=command,
)
lock = loop._get_session_lock(key)
async with lock:
@@ -216,10 +218,11 @@ async def test_compact_during_active_turn_waits_for_the_session_lock(loop) -> No
@pytest.mark.asyncio
async def test_stop_completes_a_compact_command_waiting_for_the_session_lock(loop) -> None:
@pytest.mark.parametrize("command", ["/compact", "/summarize"])
async def test_stop_completes_a_compact_command_waiting_for_the_session_lock(loop, command) -> None:
key = "websocket:test"
msg = InboundMessage(
channel="websocket", sender_id="user", chat_id="test", content="/compact",
channel="websocket", sender_id="user", chat_id="test", content=command,
metadata={"webui_turn_id": "queued-compact"},
)
completions = []
@@ -235,7 +238,8 @@ async def test_stop_completes_a_compact_command_waiting_for_the_session_lock(loo
@pytest.mark.asyncio
async def test_compact_is_a_fifo_barrier_during_an_active_turn(loop) -> None:
@pytest.mark.parametrize("command_text", ["/compact", "/summarize"])
async def test_compact_is_a_fifo_barrier_during_an_active_turn(loop, command_text) -> None:
key = "cli:test"
started = asyncio.Event()
release = asyncio.Event()
@@ -267,7 +271,7 @@ async def test_compact_is_a_fifo_barrier_during_an_active_turn(loop) -> None:
loop._enqueue_session_message(InboundMessage(
channel="cli", sender_id="u", chat_id="test", content="before compaction",
))
command = InboundMessage(channel="cli", sender_id="u", chat_id="test", content="/compact")
command = InboundMessage(channel="cli", sender_id="u", chat_id="test", content=command_text)
await loop._dispatch_command_inline(command, key, command.content, loop.commands.dispatch)
loop._enqueue_session_message(InboundMessage(
channel="cli", sender_id="u", chat_id="test", content="after compaction",
@@ -283,7 +287,7 @@ async def test_compact_is_a_fifo_barrier_during_an_active_turn(loop) -> None:
assert [public_history_message(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 all(command_text != 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)] == [
@@ -292,7 +296,8 @@ async def test_compact_is_a_fifo_barrier_during_an_active_turn(loop) -> None:
@pytest.mark.asyncio
async def test_stop_completes_compact_queued_behind_an_active_turn(loop) -> None:
@pytest.mark.parametrize("command_text", ["/compact", "/summarize"])
async def test_stop_completes_compact_queued_behind_an_active_turn(loop, command_text) -> None:
key = "websocket:test"
started = asyncio.Event()
@@ -309,7 +314,7 @@ async def test_stop_completes_compact_queued_behind_an_active_turn(loop) -> None
task = next(iter(loop._active_tasks[key]))
await asyncio.wait_for(started.wait(), timeout=5)
command = InboundMessage(
channel="websocket", sender_id="u", chat_id="test", content="/compact",
channel="websocket", sender_id="u", chat_id="test", content=command_text,
metadata={"webui_turn_id": "queued-compact"},
)
await loop._dispatch_command_inline(command, key, command.content, loop.commands.dispatch)
@@ -323,7 +328,8 @@ async def test_stop_completes_compact_queued_behind_an_active_turn(loop) -> None
@pytest.mark.asyncio
async def test_stop_finishes_inflight_compaction_as_cancelled(loop) -> None:
@pytest.mark.parametrize("command", ["/compact", "/summarize"])
async def test_stop_finishes_inflight_compaction_as_cancelled(loop, command) -> None:
key = "websocket:test"
session = loop.sessions.get_or_create(key)
session.add_message("user", "important question")
@@ -339,7 +345,7 @@ async def test_stop_finishes_inflight_compaction_as_cancelled(loop) -> None:
completions = []
loop.bus.subscribe(completions.append, TurnCompleted)
msg = InboundMessage(
channel="websocket", sender_id="user", chat_id="test", content="/compact",
channel="websocket", sender_id="user", chat_id="test", content=command,
metadata={"webui_turn_id": "compact-turn"},
)
loop._enqueue_session_message(msg)