refactor(agent): unify session message admission

This commit is contained in:
chengyongru authored and chengyongru committed 2026-09-17 22:18:38 +08:00
1 parent 7208fb663f
commit b669c2036c
17 files changed
+284 -257

No files matched your search

+21 -20
View File
@@ -6,6 +6,7 @@ from unittest.mock import AsyncMock, MagicMock
import pytest
from agent.session_helpers import run_session
from nanobot.agent.context import TranscriptInput
from nanobot.agent.hooks import create_file_edit_activity_hook
from nanobot.agent.loop import AgentLoop
@@ -298,7 +299,7 @@ class TestToolEventProgress:
chat_id="chat1",
content="run ls",
)
await loop._dispatch(msg)
await run_session(loop, msg)
# Drain all outbound messages and find the one carrying tool events.
outbound = []
@@ -421,7 +422,7 @@ class TestToolEventProgress:
),
)
await loop._dispatch(InboundMessage(
await run_session(loop, InboundMessage(
channel="websocket",
sender_id="u1",
chat_id="chat1",
@@ -468,7 +469,7 @@ class TestToolEventProgress:
loop = AgentLoop(bus=bus, provider=provider, workspace=tmp_path, model="openai-codex/gpt-5.5")
loop.tools.get_definitions = MagicMock(return_value=[])
await loop._dispatch(InboundMessage(
await run_session(loop, InboundMessage(
channel="whatsapp",
sender_id="u1",
chat_id="chat1",
@@ -506,7 +507,7 @@ class TestToolEventProgress:
_attach_webui_runtime_events(loop, bus)
loop.tools.get_definitions = MagicMock(return_value=[])
await loop._dispatch(InboundMessage(
await run_session(loop, InboundMessage(
channel="websocket",
sender_id="u1",
chat_id="chat1",
@@ -559,7 +560,7 @@ class TestToolEventProgress:
_attach_webui_runtime_events(loop, bus)
loop.tools.get_definitions = MagicMock(return_value=[])
await loop._dispatch(InboundMessage(
await run_session(loop, InboundMessage(
channel="websocket",
sender_id="u1",
chat_id="chat1",
@@ -595,7 +596,7 @@ class TestToolEventProgress:
calls += 1
if calls == 1:
await on_content_delta("old partial")
await loop._dispatch(InboundMessage(
loop._enqueue_session_message(InboundMessage(
channel="websocket", sender_id="u", chat_id="test", content="new question",
))
return LLMResponse(content="old partial", finish_reason="length")
@@ -605,7 +606,7 @@ class TestToolEventProgress:
loop.provider.chat_stream_with_retry = chat
try:
await loop._dispatch(InboundMessage(
await run_session(loop, InboundMessage(
channel="websocket", sender_id="u", chat_id="test", content="old question",
metadata={"_wants_stream": True},
))
@@ -652,7 +653,7 @@ class TestToolEventProgress:
_attach_webui_runtime_events(loop, bus)
loop.tools.get_definitions = MagicMock(return_value=[])
await loop._dispatch(InboundMessage(
await run_session(loop, InboundMessage(
channel="websocket",
sender_id="u1",
chat_id="chat1",
@@ -694,7 +695,7 @@ class TestToolEventProgress:
loop.max_iterations = 1
loop.tools.get_definitions = MagicMock(return_value=[])
await loop._dispatch(InboundMessage(
await run_session(loop, InboundMessage(
channel="websocket",
sender_id="u1",
chat_id="chat1",
@@ -739,7 +740,7 @@ class TestToolEventProgress:
loop._process_message = cancel_after_merge # type: ignore[method-assign]
with pytest.raises(asyncio.CancelledError):
await loop._dispatch(InboundMessage(
await run_session(loop, InboundMessage(
channel="websocket",
sender_id="u1",
chat_id="chat1",
@@ -781,7 +782,7 @@ class TestToolEventProgress:
_attach_webui_runtime_events(loop, bus)
loop.tools.get_definitions = MagicMock(return_value=[])
await loop._dispatch(InboundMessage(
await run_session(loop, InboundMessage(
channel="websocket",
sender_id="u1",
chat_id="chat1",
@@ -852,7 +853,7 @@ class TestToolEventProgress:
session.add_message("user", "Run this in the background")
session.metadata.update({"webui": True, "title": "Existing title"})
loop.sessions.save(session)
dispatch = asyncio.create_task(loop._dispatch(InboundMessage(
dispatch = asyncio.create_task(run_session(loop, InboundMessage(
channel="system",
sender_id="subagent",
chat_id=session_key,
@@ -978,7 +979,7 @@ class TestToolEventProgress:
_attach_webui_runtime_events(loop, bus)
loop.tools.get_definitions = MagicMock(return_value=[])
await loop._dispatch(InboundMessage(
await run_session(loop, InboundMessage(
channel="websocket",
sender_id="u1",
chat_id="chat1",
@@ -1076,7 +1077,7 @@ class TestToolEventProgress:
_attach_webui_runtime_events(loop, bus)
loop.tools.get_definitions = MagicMock(return_value=[])
await loop._dispatch(InboundMessage(
await run_session(loop, InboundMessage(
channel="websocket",
sender_id="u1",
chat_id="chat1",
@@ -1113,7 +1114,7 @@ class TestToolEventProgress:
loop._process_message = raise_from_turn # type: ignore[method-assign]
await loop._dispatch(InboundMessage(
await run_session(loop, InboundMessage(
channel="websocket",
sender_id="u1",
chat_id="chat1",
@@ -1159,7 +1160,7 @@ class TestToolEventProgress:
_attach_webui_runtime_events(loop, bus)
loop.tools.get_definitions = MagicMock(return_value=[])
await asyncio.wait_for(loop._dispatch(InboundMessage(
await asyncio.wait_for(run_session(loop, InboundMessage(
channel="websocket",
sender_id="u1",
chat_id="chat1",
@@ -1228,7 +1229,7 @@ class TestToolEventProgress:
loop.schedule_background = schedule_background # type: ignore[method-assign]
await loop._dispatch(InboundMessage(
await run_session(loop, InboundMessage(
channel="websocket",
sender_id="u1",
chat_id="chat1",
@@ -1310,7 +1311,7 @@ class TestToolEventProgress:
loop.schedule_background = schedule_background # type: ignore[method-assign]
await loop._dispatch(InboundMessage(
await run_session(loop, InboundMessage(
channel="websocket",
sender_id="u1",
chat_id="chat1",
@@ -1345,7 +1346,7 @@ class TestToolEventProgress:
scheduled: list[object] = []
loop.schedule_background = scheduled.append # type: ignore[method-assign]
await loop._dispatch(InboundMessage(
await run_session(loop, InboundMessage(
channel="websocket",
sender_id="u1",
chat_id="chat1",
@@ -1364,7 +1365,7 @@ class TestToolEventProgress:
loop = AgentLoop(bus=bus, provider=provider, workspace=tmp_path, model="test-model")
loop.tools.get_definitions = MagicMock(return_value=[])
await loop._dispatch(InboundMessage(
await run_session(loop, InboundMessage(
channel="slack",
sender_id="u1",
chat_id="chat1",