refactor(session): unify model and display history storage (#6336)

* refactor(session): persist display history in SQLite

* fix(session): preserve new fork activity timestamps

* test(agent): use SQLite-owned model failure recovery fixture

* test(agent): isolate session slash routing from loop shutdown

Exercise the real inbound dispatcher and await its session worker. Keep response deadlines and behavioral assertions, while leaving full SQLite cleanup to the existing async fixture rather than an unrelated two-second shutdown deadline.

* fix(webui): admit direct deliveries to display history

* docs: clarify session history migration guidance
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-21 16:13:59 +08:00
1 parent d617b723aa
commit b32b1327ce
50 files changed
+2123 -3235

No files matched your search

+110
View File
@@ -7,8 +7,12 @@ 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.channels.websocket.runtime import WebSocketChannel, WebSocketConfig
from nanobot.cli.gateway_runtime import _deliver_to_channel
from nanobot.session.display_history import HISTORY_GENERATION_METADATA_KEY
from nanobot.session.keys import UNIFIED_SESSION_KEY
from nanobot.session.manager import SessionManager
from nanobot.webui.gateway_services import build_gateway_services
@pytest.fixture
@@ -102,3 +106,109 @@ async def test_cross_target_reset_after_admission_revokes_media(delivery, monkey
assert "replaced session" in result
assert bus.outbound.empty()
assert (await sessions.state.read("email:fresh@example.invalid")).messages == []
@pytest.fixture
async def webui_delivery(delivery, tmp_path, monkeypatch):
sessions, bus, tool = delivery
data = tmp_path / "instance"
data.mkdir()
monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: data)
config = WebSocketConfig.model_validate({
"enabled": True, "allowFrom": ["*"], "host": "127.0.0.1", "port": 0,
})
gateway = build_gateway_services(
config=config, bus=bus, session_manager=sessions, static_dist_path=None,
workspace_path=sessions.workspace, default_restrict_to_workspace=True,
runtime_model_name=None, runtime_surface="browser", runtime_capabilities_overrides=None,
config_path=data / "config.json",
)
return sessions, bus, tool, WebSocketChannel(config, bus, gateway=gateway)
@pytest.mark.parametrize("caption", ["caption", ""])
async def test_unified_webui_media_keeps_its_display_generation(webui_delivery, caption):
sessions, bus, tool, channel = webui_delivery
alias = await sessions.state.get_header("websocket:chat")
canonical = await sessions.state.get_header(UNIFIED_SESSION_KEY)
tool.set_send_callback(partial(
_deliver_to_channel, bus=bus, session_manager=sessions, unified_session=True,
))
with request_context(RequestContext(
"websocket", "chat", session_key=canonical.key,
session_generation=canonical.generation,
metadata={HISTORY_GENERATION_METADATA_KEY: alias.generation},
)):
result = await tool.execute(content=caption, media=["attachment.txt"])
assert result == "Message sent to websocket:chat with 1 attachments"
sent = bus.outbound.get_nowait()
assert sent.metadata[HISTORY_GENERATION_METADATA_KEY] == alias.generation
await channel.send(sent)
facts = await sessions.state.project_history(alias.key, lambda history: history.all_facts())
assert len(facts) == 1
assert facts[0].content["text"] == caption
assert facts[0].content["media"] == sent.media
async def test_cross_channel_text_is_saved_in_webui_history(webui_delivery):
sessions, bus, tool, channel = webui_delivery
alias = await sessions.state.get_header("websocket:chat")
source = await sessions.state.get_header("telegram:origin")
with request_context(RequestContext(
"telegram", "origin", session_key=source.key, session_generation=source.generation,
)):
result = await tool.execute(content="notice", channel="websocket", chat_id="chat")
assert result == "Message sent to websocket:chat"
sent = bus.outbound.get_nowait()
await channel.send(sent)
facts = await sessions.state.project_history(alias.key, lambda history: history.all_facts())
assert [fact.content["text"] for fact in facts] == ["notice"]
@pytest.mark.parametrize("operation", ["reset", "delete"])
async def test_webui_destination_revocation_after_admission_blocks_history(
webui_delivery, monkeypatch, operation,
):
sessions, bus, tool, channel = webui_delivery
target = await sessions.state.get_header("websocket:chat")
original = bus.publish_outbound
async def revoke_before_enqueue(message):
assert message.metadata[HISTORY_GENERATION_METADATA_KEY] == target.generation
await getattr(sessions.state, operation)(target.key)
await original(message)
monkeypatch.setattr(bus, "publish_outbound", revoke_before_enqueue)
result = await tool.execute(content="late notice", channel="websocket", chat_id="chat")
assert result == "Message sent to websocket:chat"
await channel.send(bus.outbound.get_nowait())
assert await sessions.state.project_history(target.key, lambda history: history.all_facts()) == []
current = await sessions.state.read(target.key)
assert current is None or current.generation != target.generation
@pytest.mark.parametrize("media", [None, ["attachment.txt"]])
async def test_direct_webui_send_does_not_create_a_missing_destination(webui_delivery, media):
sessions, bus, tool, _ = webui_delivery
result = await tool.execute(
content="notice", channel="websocket", chat_id="missing", media=media,
)
assert result.startswith("Error sending message:")
assert bus.outbound.empty()
assert await sessions.state.read("websocket:missing") is None
async def test_temporary_webui_media_stays_nonpersistent(webui_delivery):
sessions, bus, tool, channel = webui_delivery
target = await sessions.state.register_transient("websocket:temporary")
with request_context(RequestContext(
"websocket", "temporary", session_key=target.key,
session_generation=target.generation,
metadata={HISTORY_GENERATION_METADATA_KEY: target.generation},
)):
result = await tool.execute(content="caption", media=["attachment.txt"])
assert result == "Message sent to websocket:temporary with 1 attachments"
await channel.send(bus.outbound.get_nowait())
assert await sessions.state.project_history(target.key, lambda history: history.revision) is None
+1
View File
@@ -131,6 +131,7 @@ class _EmptyGatewaySessionManager:
def state(self):
return SimpleNamespace(
list_sessions=AsyncMock(return_value=[]),
migrate_history=AsyncMock(),
record_delivery=AsyncMock(),
aclose=AsyncMock(),
)