Files
nanobot/tests/tools/test_session_messages_tool.py
T
chengyongru 8f6a5594fb refactor(session): centralize state ownership in SQLite (#5943)
* fix(session): move persistence off event loop

* refactor(session): isolate async persistence boundary

* refactor(session): keep a single session manager

* refactor(session): use stateless async adapters

* fix(session): serialize local persistence without file-lock contention

* refactor(session): own async persistence and snapshot commits

* refactor(session): narrow persistence changes to async adapters

* refactor(session): remove redundant async forwarding helpers

* fix(command): offload compact session persistence

* fix(session): adapt idle compaction to current main

* fix(webui): offload session metadata persistence

* refactor(session): centralize state ownership in SQLite

* fix(session): skip storage for non-recoverable followups

* test(webui): honor the full gateway event deadline

* perf(session): persist incremental changes and reuse owner snapshots

* test(agent): allow durable worker shutdown in burst test

* fix(gateway): drain session storage on Windows console shutdown

* fix(session): enforce bounded ownership and lifecycle-safe persistence

* fix: reject unresolved home paths and stabilize platform tests

* fix(session): preserve request context compatibility and owner-backed tests

* docs: focus configuration guide on user-facing behavior

* fix(session): adapt goal admission to async ownership after rebase

* fix(session): integrate current gateway and subagent contracts

* fix(session): align rebased workspace admission and async tests

* fix(session): support compatible SQLite schema evolution

* test(agent): use session owner in workspace context regressions

* test: stabilize Windows lifecycle and browser checks

* refactor(session): remove premature JSONL file export

* ci: bound Windows full-suite worker concurrency

* chore: remove session state refactor notes

* fix(session): queue writes and retain worker connections

* perf(webui): read detached metadata for session and task lists

* perf(agent): bound prompt preparation and local token counting

* docs: remove internal session storage guide

* perf(session): skip archived transcripts during idle scans

* fix(session): coordinate turns and isolate legacy session data

Bind WebUI input generations to the execution session and serialize idle
and SDK compaction with turns. Keep turn drafts through preparation so the
latest channel route persists.

Move imported workspace JSONL and checkpoints to durable private backups
before gateway admission, resuming cleanup after interruptions. Remove
committed child sessions when task creation is cancelled.

Validation: focused Python and frontend regressions, Ruff, BasedPyright,
import boundaries, WebUI build, and isolated gateway/browser flows.

* fix(agent): keep cancellation hooks independent of prompt preparation

* fix(session): preserve admission, delivery, and migration contracts

* fix(agent): preserve cancellation settlement after rebase

* fix(session): preserve request attribution through async ownership
2026-10-16 17:28:47 +08:00

393 lines
11 KiB
Python

import asyncio
import json
from pathlib import Path
from typing import Callable
from unittest.mock import patch
import pytest
from nanobot.agent.tools.context import RequestContext, request_context
from nanobot.agent.tools.session_messages import (
ListSessionsTool,
SendSessionMessageTool,
SessionMessageError,
)
from nanobot.bus.queue import MessageBus
from nanobot.config.schema import ToolsConfig
from nanobot.session.manager import SessionManager
from nanobot.session.session_handles import SessionHandle, SessionHandleResolver
from nanobot.session.session_messages import (
SESSION_MESSAGE_METADATA_KEY,
session_message_envelope,
)
def _persist(manager: SessionManager, *keys: str) -> None:
for key in keys:
manager.save(manager.get_or_create(key))
def _handle(manager: SessionManager, key: str) -> SessionHandle:
handle = SessionHandleResolver(manager).handle_for_session(key)
assert handle is not None
return handle
class _Timer:
def __init__(self, callback: Callable[[], None]) -> None:
self.callback = callback
self.cancelled = False
def cancel(self) -> None:
self.cancelled = True
def fire(self) -> None:
if not self.cancelled:
self.callback()
class _Scheduler:
def __init__(self) -> None:
self.calls: list[tuple[float, _Timer]] = []
def __call__(self, delay: float, callback: Callable[[], None]) -> _Timer:
timer = _Timer(callback)
self.calls.append((delay, timer))
return timer
def test_config_and_tool_schema_keep_only_the_basic_reply_contract(
tmp_path: Path,
) -> None:
tool = SendSessionMessageTool(
sessions=SessionManager(tmp_path),
bus=MessageBus(),
)
assert ToolsConfig.model_fields["max_session_messages_per_minute"].default == 6
assert tool.parameters["required"] == ["to", "content", "expect_reply"]
timeout = tool.parameters["properties"]["reply_timeout_seconds"]
assert (timeout["minimum"], timeout["maximum"]) == (5, 60)
@pytest.mark.asyncio
async def test_list_sessions_includes_all_persisted_channels_except_current(
tmp_path: Path,
) -> None:
sessions = SessionManager(tmp_path)
_persist(sessions, "websocket:current", "telegram:other", "slack:team")
tool = ListSessionsTool(sessions)
expected = {
f"@{_handle(sessions, 'telegram:other').name}",
f"@{_handle(sessions, 'slack:team').name}",
}
with request_context(RequestContext(
channel="websocket",
chat_id="current",
session_key="websocket:current",
)):
result = json.loads(await tool.execute())
assert set(result) == expected
@pytest.mark.asyncio
async def test_send_publishes_user_input_to_the_existing_target(
tmp_path: Path,
) -> None:
sessions = SessionManager(tmp_path)
_persist(sessions, "websocket:source", "telegram:target")
bus = MessageBus()
tool = SendSessionMessageTool(sessions=sessions, bus=bus)
target = _handle(sessions, "telegram:target")
sent_to = await tool.enqueue(
source_session_key="websocket:source",
target_handle=f"@{target.name}",
content="Please review this.",
expect_reply=False,
)
inbound = await bus.consume_inbound()
envelope = session_message_envelope(inbound.metadata)
assert sent_to == f"@{target.name}"
assert inbound.channel == "system"
assert inbound.chat_id == "telegram:target"
assert inbound.session_key_override == "telegram:target"
assert inbound.is_user_input
assert inbound.content == "Please review this."
assert envelope is not None
assert envelope["source_session_key"] == "websocket:source"
assert envelope["target_session_key"] == "telegram:target"
assert inbound.metadata == {SESSION_MESSAGE_METADATA_KEY: envelope}
@pytest.mark.asyncio
async def test_send_fails_when_target_does_not_exist(tmp_path: Path) -> None:
sessions = SessionManager(tmp_path)
_persist(sessions, "websocket:source")
bus = MessageBus()
tool = SendSessionMessageTool(sessions=sessions, bus=bus)
with pytest.raises(SessionMessageError, match="was not found"):
await tool.enqueue(
source_session_key="websocket:source",
target_handle="@zzzz",
content="Hello",
expect_reply=False,
)
assert bus.inbound.empty()
@pytest.mark.asyncio
async def test_rate_limit_is_per_source_session_and_uses_a_rolling_minute(
tmp_path: Path,
) -> None:
sessions = SessionManager(tmp_path)
_persist(sessions, "websocket:a", "websocket:b", "websocket:target")
now = 0.0
tool = SendSessionMessageTool(
sessions=sessions,
bus=MessageBus(),
max_messages_per_minute=1,
clock=lambda: now,
)
target = _handle(sessions, "websocket:target").name
await tool.enqueue(
source_session_key="websocket:a",
target_handle=target,
content="A1",
expect_reply=False,
)
await tool.enqueue(
source_session_key="websocket:b",
target_handle=target,
content="B1",
expect_reply=False,
)
with pytest.raises(SessionMessageError, match="rate limit"):
await tool.enqueue(
source_session_key="websocket:a",
target_handle=target,
content="A2",
expect_reply=False,
)
now = 61.0
await tool.enqueue(
source_session_key="websocket:a",
target_handle=target,
content="A3",
expect_reply=False,
)
@pytest.mark.asyncio
async def test_rate_limit_releases_expired_source_state_and_keeps_recent_sources(
tmp_path: Path,
) -> None:
sessions = SessionManager(tmp_path)
_persist(
sessions,
"websocket:a",
"websocket:b",
"websocket:c",
"websocket:target",
)
now = 0.0
tool = SendSessionMessageTool(
sessions=sessions,
bus=MessageBus(),
max_messages_per_minute=2,
clock=lambda: now,
)
target = _handle(sessions, "websocket:target").name
for source in ("websocket:a", "websocket:b"):
await tool.enqueue(
source_session_key=source,
target_handle=target,
content="initial",
expect_reply=False,
)
now = 30.0
await tool.enqueue(
source_session_key="websocket:a",
target_handle=target,
content="recent",
expect_reply=False,
)
now = 61.0
await tool.enqueue(
source_session_key="websocket:c",
target_handle=target,
content="trigger cleanup",
expect_reply=False,
)
assert set(tool._sent_at) == {"websocket:a", "websocket:c"}
await tool.enqueue(
source_session_key="websocket:a",
target_handle=target,
content="within rolling window",
expect_reply=False,
)
with pytest.raises(SessionMessageError, match="rate limit"):
await tool.enqueue(
source_session_key="websocket:a",
target_handle=target,
content="over limit",
expect_reply=False,
)
@pytest.mark.asyncio
async def test_reply_timeout_injects_a_user_input_back_into_the_source(
tmp_path: Path,
) -> None:
sessions = SessionManager(tmp_path)
_persist(sessions, "websocket:source", "websocket:target")
bus = MessageBus()
scheduler = _Scheduler()
tool = SendSessionMessageTool(
sessions=sessions,
bus=bus,
schedule_later=scheduler,
)
target = _handle(sessions, "websocket:target")
await tool.enqueue(
source_session_key="websocket:source",
target_handle=target.name,
content="Question",
expect_reply=True,
reply_timeout_seconds=5,
)
await bus.consume_inbound()
delay, timer = scheduler.calls[0]
assert delay == 5
timer.fire()
await asyncio.sleep(0)
timeout = await bus.consume_inbound()
assert timeout.chat_id == "websocket:source"
assert timeout.is_user_input
assert timeout.content == f"No reply from @{target.name} after 5 seconds."
@pytest.mark.asyncio
async def test_reply_timeout_observes_background_delivery_failure(
tmp_path: Path,
) -> None:
sessions = SessionManager(tmp_path)
_persist(sessions, "websocket:source", "websocket:target")
bus = MessageBus()
scheduler = _Scheduler()
tool = SendSessionMessageTool(
sessions=sessions,
bus=bus,
schedule_later=scheduler,
)
target = _handle(sessions, "websocket:target")
await tool.enqueue(
source_session_key="websocket:source",
target_handle=target.name,
content="Question",
expect_reply=True,
reply_timeout_seconds=5,
)
await bus.consume_inbound()
async def fail_publish(_message) -> None:
raise RuntimeError("queue unavailable")
bus.publish_inbound = fail_publish
with patch("nanobot.agent.tools.session_messages.logger") as logger:
scheduler.calls[0][1].fire()
await asyncio.sleep(0)
await asyncio.sleep(0)
assert tool._expiry_tasks == set()
logger.exception.assert_called_once_with(
"Session reply timeout delivery failed",
)
@pytest.mark.asyncio
async def test_reverse_message_cancels_the_pending_reply_timeout(
tmp_path: Path,
) -> None:
sessions = SessionManager(tmp_path)
_persist(sessions, "websocket:source", "websocket:target")
bus = MessageBus()
scheduler = _Scheduler()
tool = SendSessionMessageTool(
sessions=sessions,
bus=bus,
schedule_later=scheduler,
)
source = _handle(sessions, "websocket:source")
target = _handle(sessions, "websocket:target")
await tool.enqueue(
source_session_key=source.session_key,
target_handle=target.name,
content="Question",
expect_reply=True,
reply_timeout_seconds=5,
)
await tool.enqueue(
source_session_key=target.session_key,
target_handle=source.name,
content="Answer",
expect_reply=False,
)
assert scheduler.calls[0][1].cancelled
@pytest.mark.asyncio
async def test_reply_follows_a_recycled_handle(tmp_path: Path) -> None:
sessions = SessionManager(tmp_path)
_persist(sessions, "websocket:source", "websocket:target")
bus = MessageBus()
tool = SendSessionMessageTool(sessions=sessions, bus=bus)
source = _handle(sessions, "websocket:source")
target = _handle(sessions, "websocket:target")
await tool.enqueue(
source_session_key=source.session_key,
target_handle=target.name,
content="Question",
expect_reply=False,
)
received = await bus.consume_inbound()
assert await sessions.state.delete(source.session_key)
await sessions.state.get("websocket:replacement")
replacement = await SessionHandleResolver(sessions).ahandle_for_session(
"websocket:replacement"
)
assert replacement is not None
assert replacement.name == source.name
with request_context(RequestContext(
channel="system",
chat_id=target.session_key,
session_key=target.session_key,
metadata=received.metadata,
)):
result = await tool.execute(
to=f"@{source.name}",
content="Answer",
expect_reply=False,
)
assert result == f"Sent to @{source.name}."
reply = await bus.consume_inbound()
assert reply.chat_id == replacement.session_key