Files
nanobot/tests/cli/test_channel_delivery.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

105 lines
4.3 KiB
Python

"""Message tools preserve target identity through gateway delivery recording."""
from functools import partial
import pytest
from nanobot.agent.tools.context import RequestContext, request_context
from nanobot.agent.tools.message import MessageTool
from nanobot.bus.queue import MessageBus
from nanobot.cli.gateway_runtime import _deliver_to_channel
from nanobot.session.manager import SessionManager
@pytest.fixture
async def delivery(tmp_path):
workspace = tmp_path / "workspace"
workspace.mkdir()
(workspace / "attachment.txt").write_text("attachment", encoding="utf-8")
sessions = SessionManager(workspace, sessions_root=tmp_path / "sessions")
bus = MessageBus()
tool = MessageTool(
send_callback=partial(_deliver_to_channel, bus=bus, session_manager=sessions),
workspace=workspace,
)
try:
yield sessions, bus, tool
finally:
await sessions.state.aclose()
@pytest.mark.parametrize("channel,chat_id,key,metadata", [
("slack", "C123", "slack:C123:111.222", {"slack": {"thread_ts": "111.222"}}),
("telegram", "-100123", "telegram:-100123:topic:42", {"message_thread_id": 42}),
])
async def test_thread_media_is_recorded_in_its_admitted_session(delivery, channel, chat_id, key, metadata):
sessions, bus, tool = delivery
session = await sessions.state.get(key)
coarse_key = f"{channel}:{chat_id}"
await sessions.state.get(coarse_key)
with request_context(RequestContext(
channel, chat_id, session_key=key, session_generation=session.generation, metadata=metadata,
)):
result = await tool.execute(content="caption", media=["attachment.txt"])
assert result == f"Message sent to {channel}:{chat_id} with 1 attachments"
sent = bus.outbound.get_nowait()
assert sent.metadata == metadata
assert len(sent.media) == 1
assert [message["content"] for message in (await sessions.state.read(key)).messages] == ["caption"]
assert (await sessions.state.read(coarse_key)).messages == []
async def test_first_cross_target_media_creates_its_own_session(delivery):
sessions, bus, tool = delivery
origin = await sessions.state.get("websocket:origin")
with request_context(RequestContext(
"websocket", "origin", session_key=origin.key, session_generation=origin.generation,
)):
result = await tool.execute(
content="caption", channel="email", chat_id="fresh@example.invalid",
media=["attachment.txt"],
)
assert result == "Message sent to email:fresh@example.invalid with 1 attachments"
sent = bus.outbound.get_nowait()
assert sent.metadata == {}
target = await sessions.state.read("email:fresh@example.invalid")
assert [message["content"] for message in target.messages] == ["caption"]
assert (await sessions.state.read(origin.key)).messages == []
@pytest.mark.parametrize("operation", ["reset", "delete"])
async def test_old_turn_media_cannot_recreate_or_write_a_replaced_target(delivery, operation):
sessions, bus, tool = delivery
session = await sessions.state.get("slack:C123:111.222")
await getattr(sessions.state, operation)(session.key)
with request_context(RequestContext(
"slack", "C123", session_key=session.key, session_generation=session.generation,
)):
result = await tool.execute(content="late caption", media=["attachment.txt"])
assert result.startswith("Error sending message:")
assert bus.outbound.empty()
target = await sessions.state.read(session.key)
assert target is None or target.messages == []
assert await sessions.state.read("slack:C123") is None
async def test_cross_target_reset_after_admission_revokes_media(delivery, monkeypatch):
sessions, bus, tool = delivery
original = sessions.state.record_delivery
async def reset_before_recording(key, *args, **kwargs):
await sessions.state.reset(key)
await original(key, *args, **kwargs)
monkeypatch.setattr(sessions.state, "record_delivery", reset_before_recording)
result = await tool.execute(
content="late caption", channel="email", chat_id="fresh@example.invalid",
media=["attachment.txt"],
)
assert "replaced session" in result
assert bus.outbound.empty()
assert (await sessions.state.read("email:fresh@example.invalid")).messages == []