Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
refactor(session): unify model and display history storage (#6336)
chengyongrub32b1327ceb0
diff --git a/conftest.py b/conftest.pyindex fd16878ea..234a74470 100644--- a/conftest.py+++ b/conftest.py@@ -62,6 +62,7 @@ def _isolate_runtime_stores(return pathmonkeypatch.setattr(session_location, "get_runtime_subdir", runtime_subdir)+ monkeypatch.setattr("nanobot.webui.history.get_webui_dir", lambda: runtime_subdir("webui"))monkeypatch.setattr(star_prompt, "get_webui_dir", lambda: runtime_subdir("webui"))monkeypatch.setattr(pairing_store, "_store_path", lambda: data_dir() / "pairing.json")monkeypatch.setattr(llm_usage, "get_data_dir", data_dir)@@ -126,3 +127,69 @@ def create_symlink():raisereturn create+++class DisplayHistoryFixture:+ """Explicit offline setup plus snapshot reads for history projection tests."""++ def __init__(self, workspace: Path) -> None:+ from nanobot.session.manager import SessionManager++ self.sessions = SessionManager(workspace)++ async def metadata(self, chat_id, metadata=None):+ from nanobot.session.display_history import HISTORY_GENERATION_METADATA_KEY++ session = await self.sessions.state.get_header(f"websocket:{chat_id}")+ return {**(metadata or {}), HISTORY_GENERATION_METADATA_KEY: session.generation}++ async def message(self, **kwargs):+ from nanobot.bus.events import OutboundMessage++ kwargs["metadata"] = await self.metadata(kwargs["chat_id"], kwargs.get("metadata"))+ return OutboundMessage(**kwargs)++ async def stream(self, channel, method, chat_id, *args, **kwargs):+ kwargs["metadata"] = await self.metadata(chat_id, kwargs.get("metadata"))+ await getattr(channel, method)(chat_id, *args, **kwargs)++ def append(self, key, record):+ from nanobot.webui.transcript import append_transcript_object++ append_transcript_object(key, record, sessions=self.sessions)++ def _project(self, name, key, *args, **kwargs):+ from nanobot.webui import transcript++ return self.sessions.project_history(key, lambda snapshot: getattr(transcript, name)(+ key, *args, history=snapshot, **kwargs,+ ))++ def read(self, key):+ return self._project("read_transcript_lines", key)++ def build(self, key, **kwargs):+ return self._project("build_webui_thread_response", key, **kwargs)++ def trace(self, key, detail_ref):+ return self._project("build_webui_trace_detail_response", key, detail_ref)++ def revision(self, key, **kwargs):+ return self._project("webui_transcript_revision", key, **kwargs)++ def prompts(self, key, **kwargs):+ return self._project("build_prompt_index", key, **kwargs)++ def prompt_response(self, key, previous_revision, **kwargs):+ return self._project("prompt_index_response", key, previous_revision, **kwargs)++ def unfinished(self, key):+ return self._project("has_unfinished_transcript_tail", key)+++async def history(tmp_path: Path):+ fixture = DisplayHistoryFixture(tmp_path)+ yield fixture+ if fixture.sessions._state is not None:+ await fixture.sessions.state.aclose()diff --git a/docs/architecture.md b/docs/architecture.mdindex c5471a754..97afb09f7 100644--- a/docs/architecture.md+++ b/docs/architecture.md@@ -171,7 +171,15 @@ Runtime code distinguishes the configured agent workspace from the effective proSession history is the near-term conversation replay. Memory is the longer-term workspace state.-`nanobot/session/history.py` reads full persisted session records for `search_sessions` and `read_session`, including messages before summary checkpoints. Tool results contain public user and assistant text with indexes into the original session messages. WebUI transcript events and replay pagination are owned by the display adapter and do not supply tool history.+`nanobot/session/history.py` reads full persisted model history for `search_sessions` and `read_session`, including messages before summary checkpoints. Tool results contain public user and assistant text with indexes into the original session messages. Display facts are stored separately from those messages in the same SQLite database; they never become provider context or session-tool history.++`SessionState` owns both logical histories. `history_entries` stores transport-neutral inputs, completed answer/reasoning segments, activity, historical file diffs, response sources, and completion statistics/outcomes; `history_turns` and `history_heads` provide bounded paging and revision/sidebar projections. WebUI converts these facts into its existing transcript protocol inside an owner-managed read snapshot. Delta frames remain transient. A turn may commit facts as it progresses; this is not a transaction held across a model request or network delivery.++Every append carries the generation admitted before execution, including a distinct display destination under unified-session routing. A missing or replaced session rejects the append instead of creating a new row. Delete/reset cascades through both logical histories; fork copies its canonical and display prefixes in one SQLite transaction. Future in-place history replacement should use that same owner transaction rather than file journals or replay-cache writes.++`nanobot/webui/history.py` resolves legacy `<stem>.jsonl` files and numbered JSONL files inside `<stem>.segments/` directories, then imports them through `SessionState` before recovery or transport admission. Recorded chat identities determine the target session; histories without a recorded identity require an unambiguous existing canonical session. Display-only histories with a recorded identity are imported into the starting workspace namespace. Histories owned by exactly one other namespace are deferred to that namespace; duplicate owners and malformed records fail startup.++`SqliteSessionStore.migrate_history()` commits the imported facts and completion markers in one transaction. Per-session ownership receipts in `storage_meta` survive session deletion, so another namespace cannot restore deleted chats by reimporting the unchanged legacy backups.| Store | File area ||---|---|diff --git a/docs/cli-reference.md b/docs/cli-reference.mdindex 2f0731ea8..17528dca4 100644--- a/docs/cli-reference.md+++ b/docs/cli-reference.md@@ -117,7 +117,11 @@ Session state lives in `<config-dir>/sessions/<workspace-id>/sessions.sqlite3`,After a successful import, JSONL files and their checkpoints from `<workspace>/sessions/` move to `<config-dir>/sessions/<workspace-id>/jsonl-backup/`, preserving their contents outside the agent-readable workspace. JSONL files already in the private session directory remain there. An interrupted migration resumes on startup before nanobot serves conversations; failed imports leave their source files unchanged.-Before changing versions, stop nanobot gracefully and back up both the config directory and workspace. To downgrade, restore a backup compatible with the older release. Copying only `sessions.sqlite3` while nanobot is running can miss recent messages still held in its WAL file.+WebUI chat history is saved in the same session database. The gateway automatically imports old `<config-dir>/webui/*.jsonl` files and numbered JSONL files inside `<config-dir>/webui/*.segments/` directories. If old history is damaged or cannot be matched to one conversation, startup stops with an error; resolve the reported problem before restarting.++The old WebUI history files remain unchanged as backups. Imported chats deleted after the upgrade stay deleted on restart. Keep the `webui` directory: it still contains sidebar preferences and other saved WebUI state.++Before changing versions, stop all nanobot instances using the same config directory and back up that entire directory and the workspace together. Copying only `sessions.sqlite3` while nanobot is running can miss recent messages. To downgrade, restore a complete pre-upgrade backup compatible with the older release. Older releases cannot read newly saved WebUI history; do not combine the current session database with old WebUI history files.Interactive mode uses nanobot's native TypeScript terminal UI. It talks to the same local gateway as the WebUI, so streaming, tool progress, and WebSocket sessions share one protocol instead of maintaining a second agent loop. If no gateway is running, either client starts it on demand. The TUI paints immediately while the local gateway starts, then obtains fresh bootstrap credentials and connects in the background. Exiting one TUI or WebUI launcher releases only that client; the last interactive launcher stops the on-demand gateway. A small gateway watchdog also reclaims an on-demand process if its last client crashes. `/detach` promotes the shared gateway to persistent background mode before closing the TUI, so active agent work continues without a connected client. An explicit `nanobot gateway --background` starts or promotes the gateway the same way before opening a client. `nanobot gateway restart` restarts a detached gateway without changing that lifetime; restart an attached foreground gateway in its owning terminal. `nanobot gateway stop` ends either mode.diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.pyindex d92f1ffe5..1b7a09d8d 100644--- a/nanobot/agent/loop.py+++ b/nanobot/agent/loop.py@@ -1007,7 +1007,7 @@ class AgentLoop:if session is None:return NO_EVENTSreturn self.turn_delivery_factory.session_events(- session_key, session.metadata,+ session_key, session.metadata, generation=session.generation,)def _remember_session_route(@@ -2061,7 +2061,7 @@ class AgentLoop:else:ctx.session = await self.sessions.state.get(ctx.session_key)session = ctx.session- ctx.delivery.bind_session_generation(session.generation)+ await ctx.delivery.bind_session_generation(session.generation)ctx.ephemeral = ctx.ephemeral or not session.policy.persisttools = ctx.tools if ctx.tools is not None else self.toolsif session.policy.disabled_tools:@@ -2141,6 +2141,9 @@ class AgentLoop:ctx.turn_scopes.append(GoalInputScope(ctx.msg))if result is not None:ctx.outbound = result+ if cmd_ctx.raw.lower() == "/new":+ current = await self.sessions.state.get_header(ctx.session_key)+ await ctx.delivery.bind_session_generation(current.generation)# Shortcut commands skip BUILD and SAVE, so we must persist the# turn here so WebUI history hydration after turn_end sees the# message. Mark messages with _command so get_history can filterdiff --git a/nanobot/agent/turn_delivery.py b/nanobot/agent/turn_delivery.pyindex 988e44398..606786723 100644--- a/nanobot/agent/turn_delivery.py+++ b/nanobot/agent/turn_delivery.py@@ -4,7 +4,7 @@ from __future__ import annotationsimport dataclassesimport time-from collections.abc import Callable+from collections.abc import Awaitable, Callablefrom copy import deepcopyfrom dataclasses import dataclass, fieldfrom typing import TYPE_CHECKING, Any, cast@@ -29,6 +29,7 @@ from nanobot.events import (ResponseSourceEvent,)from nanobot.providers.base import LLMUsage+from nanobot.session.display_history import HISTORY_GENERATION_METADATA_KEYfrom nanobot.session.keys import (UNIFIED_SESSION_KEY,is_internal_session,@@ -53,6 +54,7 @@ class TurnRoute:TurnRoutePolicy = Callable[[InboundMessage, str, TurnRoute], TurnRoute]+TurnRouteAdmission = Callable[[TurnRoute, str, str], Awaitable[TurnRoute]]def _bind_events(@@ -94,10 +96,12 @@ class TurnDeliveryFactory:self,bus: MessageBus,route_policy: TurnRoutePolicy | None = None,+ route_admission: TurnRouteAdmission | None = None,) -> None:self.bus = busself.runtime_event_publisher = RuntimeEventPublisher(bus)self.route_policy = route_policy+ self.route_admission = route_admissiondef create(self,@@ -117,6 +121,7 @@ class TurnDeliveryFactory:input_message=msg,session_key=session_key,route=route,+ route_admission=self.route_admission,enable_stream=enable_stream,)@@ -138,6 +143,7 @@ class TurnDeliveryFactory:self,session_key: str,session_metadata: dict[str, Any],+ *, generation: str,) -> EventSink:"""Bind idle notifications to one route without acquiring a turn owner."""if is_internal_session(session_key):@@ -151,7 +157,9 @@ class TurnDeliveryFactory:return EventSink()if not isinstance(metadata, dict):return EventSink()- metadata = cast(dict[str, Any], metadata)+ metadata = dict(cast(dict[str, Any], metadata))+ if isinstance(saved_generation := saved_route.get("generation"), str):+ metadata[HISTORY_GENERATION_METADATA_KEY] = saved_generationelse:# Older sessions have no route snapshot. Only direct clients have a# session key that is also an unambiguous delivery address.@@ -165,7 +173,8 @@ class TurnDeliveryFactory:channel, chat_id = routemetadata = {}metadata = deepcopy(metadata)-+ if channel == "websocket":+ metadata.setdefault(HISTORY_GENERATION_METADATA_KEY, generation)return _bind_events(self.bus, TurnRoute(channel, chat_id, metadata), session_key)@staticmethod@@ -196,6 +205,7 @@ class TurnDelivery:input_message: InboundMessagesession_key: strroute: TurnRoute+ route_admission: TurnRouteAdmission | None = Noneenable_stream: bool = Falsedelivery_message: InboundMessage = field(init=False)lifecycle_message: InboundMessage = field(init=False)@@ -232,10 +242,26 @@ class TurnDelivery:def streaming(self) -> bool:return self._stream_base_id is not None- def bind_session_generation(self, generation: str) -> None:- """Attach the admitted session identity to all later lifecycle events."""+ async def bind_session_generation(self, generation: str) -> None:+ """Admit the destination before publishing any durable turn facts."""+ previous = self.delivery_message.session_generation+ metadata = dict(self.route.metadata)+ stored_generation = metadata.get(HISTORY_GENERATION_METADATA_KEY)+ if self.route.channel == "websocket" and (stored_generation is None or stored_generation == previous):+ metadata[HISTORY_GENERATION_METADATA_KEY] = generation+ route = dataclasses.replace(self.route, metadata=metadata)+ if self.route_admission is not None:+ route = await self.route_admission(route, self.session_key, generation)+ self.route = route+ self.delivery_message.metadata = dict(route.metadata)self.delivery_message.session_generation = generationself.lifecycle_message.session_generation = generation+ if HISTORY_GENERATION_METADATA_KEY in route.metadata:+ for message in (self.lifecycle_message, self.input_message):+ message.metadata[HISTORY_GENERATION_METADATA_KEY] = route.metadata[+ HISTORY_GENERATION_METADATA_KEY+ ]+ self._routed_events = _bind_events(self.bus, self.route, self.session_key)def remember_session_route(self, session_metadata: dict[str, Any]) -> None:"""Keep only routing fields needed to deliver a later idle notification."""@@ -248,6 +274,8 @@ class TurnDelivery:"chat_id": self.route.chat_id,"metadata": notification_metadata(self.route.channel, self.route.metadata),}+ if generation := self.route.metadata.get(HISTORY_GENERATION_METADATA_KEY):+ session_metadata["_compaction_route"]["generation"] = generationasync def started(self) -> None:if self.route.publish_lifecycle:@@ -331,6 +359,9 @@ class TurnDelivery:if response is not None:completed_channel = response.channelcompleted_chat_id = response.chat_id+ generation = self.route.metadata.get(HISTORY_GENERATION_METADATA_KEY)+ if generation is not None and (response.channel, response.chat_id) == (self.route.channel, self.route.chat_id):+ response = dataclasses.replace(response, metadata={**response.metadata, HISTORY_GENERATION_METADATA_KEY: generation})if (self._response_source is not Noneand self._response_content is not Nonediff --git a/nanobot/channels/linear/tests/test_linear.py b/nanobot/channels/linear/tests/test_linear.pyindex d850669ac..c4f3a6cc8 100644--- a/nanobot/channels/linear/tests/test_linear.py+++ b/nanobot/channels/linear/tests/test_linear.py@@ -1251,7 +1251,7 @@ async def test_idle_compaction_does_not_reactivate_a_completed_agent_session(tmp))activity_count = len(client.activities)- idle_events = factory.session_events(inbound.session_key, session_metadata)+ idle_events = factory.session_events(inbound.session_key, session_metadata, generation="generation")for phase in ("started", "succeeded"):await idle_events.emit(ContextCompactionEvent(compaction_id="idle-1", phase=phase))await channel.send(await channel.bus.consume_outbound())diff --git a/nanobot/channels/websocket/runtime.py b/nanobot/channels/websocket/runtime.pyindex e17a736fa..1e681a5f6 100644--- a/nanobot/channels/websocket/runtime.py+++ b/nanobot/channels/websocket/runtime.py@@ -683,6 +683,7 @@ class WebSocketChannel(BaseChannel):)async def start(self) -> None:+ await self._transcripts.initialize()self.gateway.http.remote_instances.resume()from nanobot.utils.logging_bridge import redirect_lib_logging@@ -1119,7 +1120,7 @@ class WebSocketChannel(BaseChannel):# Give an idle writer a chance to start without waiting for physical I/O.await asyncio.sleep(0)- def _persist_turn_transcript_event(+ async def _persist_turn_transcript_event(self,chat_id: str,event: dict[str, Any],@@ -1138,7 +1139,7 @@ class WebSocketChannel(BaseChannel):chat_id, event, metadata=metadata, phase=phase, include_source=include_source,)return True- persisted = self._transcripts.prepare_and_append(+ persisted = await self._transcripts.prepare_and_append(chat_id,event,metadata=metadata,@@ -1169,7 +1170,7 @@ class WebSocketChannel(BaseChannel):)return persisted- def _persist_turn_stream_event(+ async def _persist_turn_stream_event(self,chat_id: str,event: dict[str, Any],@@ -1185,7 +1186,7 @@ class WebSocketChannel(BaseChannel):chat_id, event, metadata=metadata, phase=phase, include_source=include_source,)return True- persisted = self._transcripts.prepare_and_append_stream_event(+ persisted = await self._transcripts.prepare_and_append_stream_event(chat_id,event,completed_text=completed_text,@@ -1247,7 +1248,7 @@ class WebSocketChannel(BaseChannel):elif progress_event:payload["kind"] = "progress"phase = "activity" if payload.get("kind") in ("tool_hint", "progress") else "answer"- self._persist_turn_transcript_event(+ await self._persist_turn_transcript_event(msg.chat_id,payload,metadata=msg.metadata,@@ -1287,7 +1288,7 @@ class WebSocketChannel(BaseChannel):body["stream_id"] = stream_idstream_key = (chat_id, str(stream_id or ""))self._reasoning_text_buffers.setdefault(stream_key, []).append(delta)- self._persist_turn_stream_event(+ await self._persist_turn_stream_event(chat_id,body,completed_text=None,@@ -1318,7 +1319,7 @@ class WebSocketChannel(BaseChannel):body["stream_id"] = stream_idstream_key = (chat_id, str(stream_id or ""))reasoning_text = "".join(self._reasoning_text_buffers.pop(stream_key, []))- self._persist_turn_stream_event(+ await self._persist_turn_stream_event(chat_id,body,completed_text=reasoning_text or None,@@ -1343,7 +1344,7 @@ class WebSocketChannel(BaseChannel):"chat_id": chat_id,"edits": edits,}- self._persist_turn_transcript_event(+ await self._persist_turn_transcript_event(chat_id,payload,metadata=metadata,@@ -1397,7 +1398,7 @@ class WebSocketChannel(BaseChannel):body["resuming"] = Trueif stream_end and merge_next:body["merge_next"] = True- self._persist_turn_stream_event(+ await self._persist_turn_stream_event(chat_id,body,completed_text=completed_text,@@ -1424,7 +1425,7 @@ class WebSocketChannel(BaseChannel):conns = list(self._subs.get(chat_id, ()))body: dict[str, Any] = dict(payload)if persistence == "turn_activity":- self._persist_turn_transcript_event(+ await self._persist_turn_transcript_event(chat_id,body,metadata=metadata,@@ -1436,7 +1437,7 @@ class WebSocketChannel(BaseChannel):canonical_webui_turnand websocket_turn_transcript_persistence_failed(chat_id, turn_owner))- persisted = self._persist_turn_transcript_event(+ persisted = await self._persist_turn_transcript_event(chat_id,body,metadata=metadata,diff --git a/nanobot/channels/websocket/tests/test_restart_control.py b/nanobot/channels/websocket/tests/test_restart_control.pyindex f839ab307..d476aca47 100644--- a/nanobot/channels/websocket/tests/test_restart_control.py+++ b/nanobot/channels/websocket/tests/test_restart_control.py@@ -15,13 +15,12 @@ from nanobot.command.router import CommandContextfrom nanobot.session.manager import SessionManagerfrom nanobot.webui.metadata import WEBUI_RESTART_CONTROL_METADATA_KEYfrom nanobot.webui.session_list_index import list_webui_sessions-from nanobot.webui.transcript import append_transcript_object, read_transcript_linesfrom .test_websocket_channel import _basic_handler, _sent_ws_payloads-def _channel(tmp_path: Path, *, allowed: bool = True):- sessions = SessionManager(tmp_path / "workspace")+def _channel(tmp_path: Path, *, allowed: bool = True, sessions=None):+ sessions = sessions or SessionManager(tmp_path / "workspace")bus = MagicMock()bus.publish_inbound = AsyncMock()channel = WebSocketChannel(@@ -37,8 +36,8 @@ def _channel(tmp_path: Path, *, allowed: bool = True):@pytest.mark.asyncio@pytest.mark.parametrize("existing", ["none", "canonical", "transcript", "both"])-async def test_settings_restart_does_not_create_or_change_chat_history(tmp_path, monkeypatch, existing):- sessions, bus, channel, connection = _channel(tmp_path)+async def test_settings_restart_does_not_create_or_change_chat_history(history, tmp_path, monkeypatch, existing):+ sessions, bus, channel, connection = _channel(tmp_path, sessions=history.sessions)monkeypatch.setattr("nanobot.command.builtin.set_restart_notice_to_env", MagicMock())key = "websocket:existing"if existing in {"canonical", "both"}:@@ -47,12 +46,12 @@ async def test_settings_restart_does_not_create_or_change_chat_history(tmp_path,session.metadata.update({"webui": True, "title": "Keep this title"})sessions.save(session)if existing in {"transcript", "both"}:- append_transcript_object(key, {- "event": "user_message", "chat_id": "existing", "text": "Keep my conversation",+ history.append(key, {+ "event": "user", "chat_id": "existing", "text": "Keep my conversation",})before_rows = list_webui_sessions(sessions)before_files = {p: p.read_bytes() for p in tmp_path.rglob("*.jsonl")}- before_transcript = read_transcript_lines(key)+ before_transcript = history.read(key)# Use each new connection's default ID, then an existing chat when present.chat_ids = ["ready-default-1", "ready-default-2"]@@ -86,9 +85,9 @@ async def test_settings_restart_does_not_create_or_change_chat_history(tmp_path,assert len(replies) == len(chat_ids)assert all(frame["text"] == "Restarting..." and frame["turn_id"].startswith("webui-system:") for frame in replies)assert {p: p.read_bytes() for p in tmp_path.rglob("*.jsonl")} == before_files- assert read_transcript_lines(key) == before_transcript+ assert history.read(key) == before_transcript# A fresh manager uses the public sidebar discovery path, not a cached count.- fresh = SessionManager(tmp_path / "workspace")+ fresh = SessionManager(sessions.workspace)assert list_webui_sessions(fresh) == before_rowsif existing in {"canonical", "both"}:restored = fresh.get_existing(key)@@ -104,8 +103,8 @@ async def test_settings_restart_does_not_create_or_change_chat_history(tmp_path,("/restart later", "webui-system:not-a-restart"),("ordinary message", "webui-system:not-a-command"),])-async def test_control_prefix_does_not_suppress_ordinary_or_session_command_history(tmp_path, text, turn_id):- sessions, bus, channel, connection = _channel(tmp_path)+async def test_control_prefix_does_not_suppress_ordinary_or_session_command_history(history, tmp_path, text, turn_id):+ sessions, bus, channel, connection = _channel(tmp_path, sessions=history.sessions)await channel._dispatch_envelope(connection, "browser", {"type": "message", "chat_id": "chat", "content": text, "webui": True, "turn_id": turn_id,"metadata": {WEBUI_RESTART_CONTROL_METADATA_KEY: True},@@ -114,7 +113,7 @@ async def test_control_prefix_does_not_suppress_ordinary_or_session_command_histassert WEBUI_RESTART_CONTROL_METADATA_KEY not in message.metadataawait channel.send(OutboundMessage(channel="websocket", chat_id="chat", content="Reply", metadata=message.metadata))assert len(list_webui_sessions(sessions)) == 1- assert [row["text"] for row in read_transcript_lines("websocket:chat")] == [text, "Reply"]+ assert [row["text"] for row in history.read("websocket:chat")] == [text, "Reply"]await channel._cleanup_connection(connection)diff --git a/nanobot/channels/websocket/tests/test_websocket_channel.py b/nanobot/channels/websocket/tests/test_websocket_channel.pyindex 2f313f98c..d5908b0cc 100644--- a/nanobot/channels/websocket/tests/test_websocket_channel.py+++ b/nanobot/channels/websocket/tests/test_websocket_channel.py@@ -54,6 +54,7 @@ from nanobot.providers.base import LLMUsagefrom nanobot.runtime_context import RUNTIME_CONTEXT_INPUT_META, WEBUI_QUOTE_SOURCEfrom nanobot.security.workspace_access import WORKSPACE_SCOPE_METADATA_KEYfrom nanobot.session import webui_turns as wth+from nanobot.session.display_history import HistoryKindfrom nanobot.session.manager import Session, SessionManagerfrom nanobot.session.model_selection import SESSION_MODEL_PRESET_METADATA_KEYfrom nanobot.session.session_handles import session_handle_for_name@@ -83,11 +84,6 @@ from nanobot.webui.metadata import (WEBUI_TURN_METADATA_KEY,)from nanobot.webui.settings_api import settings_payload, update_provider_settings-from nanobot.webui.transcript import (- append_transcript_object,- build_webui_thread_response,- read_transcript_lines,-)from .ws_test_client import http_get as _http_get@@ -136,6 +132,7 @@ def _thread_conversation_events(body: dict[str, Any]) -> list[dict[str, Any]]:def _ch(bus: Any, **kw: Any) -> WebSocketChannel:image_generation_reload = kw.pop("image_generation_reload", None)+ session_manager = kw.pop("session_manager", None)cfg: dict[str, Any] = {"enabled": True,"allowFrom": ["*"],@@ -149,7 +146,7 @@ def _ch(bus: Any, **kw: Any) -> WebSocketChannel:gateway = build_gateway_services(config=parsed,bus=bus,- session_manager=None,+ session_manager=session_manager,static_dist_path=None,workspace_path=Path.cwd(),default_restrict_to_workspace=False,@@ -466,8 +463,8 @@ async def test_attach_exposes_the_session_canonical_model_preset(bus, tmp_path)@pytest.mark.asyncio-async def test_temporary_chat_is_transient_and_discarded(bus, tmp_path) -> None:- sessions = SessionManager(tmp_path)+async def test_temporary_chat_is_transient_and_discarded(history, bus, tmp_path) -> None:+ sessions = history.sessionsselected_project = tmp_path / "selected-project"selected_project.mkdir()channel = WebSocketChannel(@@ -518,7 +515,7 @@ async def test_temporary_chat_is_transient_and_discarded(bus, tmp_path) -> None:assert session is not Noneassert session.policy.persist is Falseassert upload.exists()- assert read_transcript_lines(inbound.session_key) == []+ assert history.read(inbound.session_key) == []assert [payload["event"] for payload in _sent_ws_payloads(connection)] == ["message_accepted",]@@ -539,7 +536,7 @@ async def test_temporary_chat_is_transient_and_discarded(bus, tmp_path) -> None:assert chat_id not in channel._subsassert chat_id not in channel._conn_chats.get(connection, set())assert not upload.exists()- assert read_transcript_lines(inbound.session_key) == []+ assert history.read(inbound.session_key) == []@pytest.mark.asyncio@@ -572,8 +569,8 @@ async def test_temporary_chat_rejects_persistent_commands(bus, tmp_path, content@pytest.mark.parametrize("content", ["/tmp", "/new/file.txt", "/model@nanobot fast"])-async def test_temporary_chat_accepts_paths_and_allowed_commands(bus, tmp_path, content) -> None:- sessions = SessionManager(tmp_path)+async def test_temporary_chat_accepts_paths_and_allowed_commands(history, bus, tmp_path, content) -> None:+ sessions = history.sessionschannel = WebSocketChannel({"enabled": True, "allowFrom": ["*"]}, bus,gateway=_basic_handler(bus, session_manager=sessions, workspace_path=tmp_path),@@ -589,7 +586,7 @@ async def test_temporary_chat_accepts_paths_and_allowed_commands(bus, tmp_path,inbound = bus.publish_inbound.await_args.args[0]assert inbound.content == contentassert inbound.require_existing_session is True- assert read_transcript_lines(inbound.session_key) == []+ assert history.read(inbound.session_key) == []session = sessions.get_cached(inbound.session_key)assert session is not None and not session.policy.persist@@ -967,10 +964,10 @@ async def test_token_issue_route_requires_secret_when_static_token_configured(bu@pytest.mark.asyncio-async def test_webui_message_envelope_marks_inbound_metadata(bus: MagicMock) -> None:- from nanobot.webui.transcript import read_transcript_lines+async def test_webui_message_envelope_marks_inbound_metadata(history, bus: MagicMock) -> None:- channel = _ch(bus)++ channel = _ch(bus, session_manager=history.sessions)conn = MagicMock()conn.remote_address = ("127.0.0.1", 50123)@@ -992,7 +989,7 @@ async def test_webui_message_envelope_marks_inbound_metadata(bus: MagicMock) ->assert msg.metadata["webui"] is Trueassert msg.metadata["webui_turn_id"] == "turn-1"assert msg.metadata["_wants_stream"] is True- lines = read_transcript_lines("websocket:chat-1")+ lines = history.read("websocket:chat-1")assert len(lines) == 1assert {key: lines[0].get(key) for key in ("event",@@ -1013,12 +1010,12 @@ async def test_webui_message_envelope_marks_inbound_metadata(bus: MagicMock) ->@pytest.mark.asyncio-async def test_trusted_webui_shell_preserves_display_text_and_hides_dispatch_command(+async def test_trusted_webui_shell_preserves_display_text_and_hides_dispatch_command(history,bus: MagicMock,) -> None:- from nanobot.webui.transcript import read_transcript_lines- channel = _ch(bus)++ channel = _ch(bus, session_manager=history.sessions)conn = MagicMock()conn.remote_address = ("127.0.0.1", 50123)channel._webui_connections.add(conn)@@ -1040,7 +1037,7 @@ async def test_trusted_webui_shell_preserves_display_text_and_hides_dispatch_comassert msg.content == "/__shell printf ok"assert msg.metadata[INBOUND_META_USER_SHELL] is Trueassert msg.metadata["webui_turn_id"] == "shell-turn"- assert read_transcript_lines("websocket:shell-chat")[0]["text"] == "!printf ok"+ assert history.read("websocket:shell-chat")[0]["text"] == "!printf ok"@pytest.mark.asyncio@@ -1068,20 +1065,20 @@ async def test_untrusted_websocket_cannot_enable_user_shell(bus: MagicMock) -> N@pytest.mark.asyncio-async def test_webui_message_envelope_persists_user_transcript_for_refresh(+async def test_webui_message_envelope_persists_user_transcript_for_refresh(history,bus: MagicMock,tmp_path,monkeypatch,) -> None:- from nanobot.webui.transcript import build_webui_thread_response, read_transcript_lines+monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)- channel = _ch(bus)+ channel = _ch(bus, session_manager=history.sessions)conn = AsyncMock()conn.remote_address = ("127.0.0.1", 50123)async def answer_during_publish(_msg: Any) -> None:- await channel.send(OutboundMessage(channel="websocket", chat_id="chat-1", content="hi back"))+ await channel.send(await history.message(channel="websocket", chat_id="chat-1", content="hi back"))bus.publish_inbound.side_effect = answer_during_publish@@ -1091,10 +1088,10 @@ async def test_webui_message_envelope_persists_user_transcript_for_refresh({"type": "message", "chat_id": "chat-1", "content": "hello", "webui": True},)- lines = read_transcript_lines("websocket:chat-1")+ lines = history.read("websocket:chat-1")assert [line["event"] for line in lines] == ["user", "message"]- body = build_webui_thread_response("websocket:chat-1")+ body = history.build("websocket:chat-1")assert body is not Noneassert [message["role"] for message in _thread_conversation_events(body)] == ["user", "assistant",@@ -1105,15 +1102,15 @@ async def test_webui_message_envelope_persists_user_transcript_for_refresh(@pytest.mark.asyncio-async def test_webui_stop_control_message_is_not_persisted_as_user_bubble(+async def test_webui_stop_control_message_is_not_persisted_as_user_bubble(history,bus: MagicMock,tmp_path,monkeypatch,) -> None:- from nanobot.webui.transcript import read_transcript_lines+monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)- channel = _ch(bus)+ channel = _ch(bus, session_manager=history.sessions)conn = AsyncMock()conn.remote_address = ("127.0.0.1", 50123)@@ -1125,7 +1122,7 @@ async def test_webui_stop_control_message_is_not_persisted_as_user_bubble(msg = bus.publish_inbound.await_args.args[0]assert msg.content == "/stop"- assert read_transcript_lines("websocket:chat-1") == []+ assert history.read("websocket:chat-1") == []@pytest.mark.asyncio@@ -1681,7 +1678,7 @@ async def test_webui_message_projects_quote_to_trusted_runtime_context(bus: Magi@pytest.mark.asyncio-async def test_webui_automation_intent_is_hidden_and_not_inherited_by_cron(+async def test_webui_automation_intent_is_hidden_and_not_inherited_by_cron(history,bus: MagicMock, tmp_path: Path,) -> None:from nanobot.cron.service import CronService@@ -1692,7 +1689,7 @@ async def test_webui_automation_intent_is_hidden_and_not_inherited_by_cron(public_history_message,)- channel = _ch(bus)+ channel = _ch(bus, session_manager=history.sessions)conn = MagicMock()channel._webui_connections.add(conn)original = "每天八点提醒我喝水"@@ -1715,7 +1712,7 @@ async def test_webui_automation_intent_is_hidden_and_not_inherited_by_cron(assert public_history_message({"role": "user", "content": content, RUNTIME_CONTEXT_HISTORY_META: marker,}) == {"role": "user", "content": original}- body = build_webui_thread_response("websocket:chat-automation")+ body = history.build("websocket:chat-automation")assert body is not Noneassert [message["content"] for message in _thread_conversation_events(body)] == [original]@@ -2586,9 +2583,9 @@ async def test_send_progress_includes_structured_tool_events() -> None:@pytest.mark.asyncio-async def test_send_progress_omits_binary_tool_results_from_wire_and_transcript() -> None:+async def test_send_progress_omits_binary_tool_results_from_wire_and_transcript(history, ) -> None:bus = MagicMock()- channel = WebSocketChannel({"enabled": True, "allowFrom": ["*"]}, bus, gateway=_basic_handler(bus))+ channel = WebSocketChannel({"enabled": True, "allowFrom": ["*"]}, bus, gateway=_basic_handler(bus, session_manager=history.sessions))mock_ws = AsyncMock()channel._attach(mock_ws, "chat-binary-tool-result")data_url = f"data:image/png;base64,{'A' * (2 * 1024 * 1024)}"@@ -2613,7 +2610,7 @@ async def test_send_progress_omits_binary_tool_results_from_wire_and_transcript(}]- await channel.send(OutboundMessage(+ await channel.send(await history.message(channel="websocket",chat_id="chat-binary-tool-result",content="read_file completed",@@ -2627,7 +2624,7 @@ async def test_send_progress_omits_binary_tool_results_from_wire_and_transcript()assert len(mock_ws.send.await_args.args[0].encode("utf-8")) < 4096- [persisted] = read_transcript_lines("websocket:chat-binary-tool-result")+ [persisted] = history.read("websocket:chat-binary-tool-result")assert persisted["tool_events"] == payload["tool_events"]assert data_url not in json.dumps(persisted)@@ -2747,16 +2744,16 @@ async def test_send_delta_emits_delta_and_stream_end() -> None:@pytest.mark.asyncio-async def test_send_delta_preserves_webui_source_metadata() -> None:+async def test_send_delta_preserves_webui_source_metadata(history, ) -> None:bus = MagicMock()- channel = WebSocketChannel({"enabled": True, "allowFrom": ["*"], "streaming": True}, bus, gateway=_basic_handler(bus))+ channel = WebSocketChannel({"enabled": True, "allowFrom": ["*"], "streaming": True}, bus, gateway=_basic_handler(bus, session_manager=history.sessions))mock_ws = AsyncMock()channel._attach(mock_ws, "chat-source-stream")source = {"kind": "cron", "label": "Repo check"}metadata = {WEBUI_MESSAGE_SOURCE_METADATA_KEY: source}- await channel.send_delta("chat-source-stream", "done", metadata=metadata, stream_id="sid")- await channel.send_delta(+ await history.stream(channel, "send_delta", "chat-source-stream", "done", metadata=metadata, stream_id="sid")+ await history.stream(channel, "send_delta","chat-source-stream","",metadata=metadata,@@ -2770,7 +2767,7 @@ async def test_send_delta_preserves_webui_source_metadata() -> None:assert first["source"] == sourceassert second["event"] == "stream_end"assert second["source"] == source- lines = read_transcript_lines("websocket:chat-source-stream")+ lines = history.read("websocket:chat-source-stream")assert lines[-1]["source"] == sourceassert lines[-1]["event"] == "stream_end"assert lines[-1]["text"] == "done"@@ -2797,20 +2794,20 @@ async def test_send_delta_marks_resuming_stream_end() -> None:@pytest.mark.asyncio-async def test_send_delta_keeps_buffer_across_merged_stream_boundary() -> None:- from nanobot.webui.transcript import build_webui_thread_response, read_transcript_lines+async def test_send_delta_keeps_buffer_across_merged_stream_boundary(history, ) -> None:+bus = MagicMock()channel = WebSocketChannel({"enabled": True, "allowFrom": ["*"], "streaming": True},bus,- gateway=_basic_handler(bus),+ gateway=_basic_handler(bus, session_manager=history.sessions),)mock_ws = AsyncMock()channel._attach(mock_ws, "chat-1")- await channel.send_delta("chat-1", "first ", stream_id="sid")- await channel.send_delta(+ await history.stream(channel, "send_delta", "chat-1", "first ", stream_id="sid")+ await history.stream(channel, "send_delta","chat-1","",stream_id="sid",@@ -2818,8 +2815,8 @@ async def test_send_delta_keeps_buffer_across_merged_stream_boundary() -> None:resuming=True,merge_next=True,)- await channel.send_delta("chat-1", "second", stream_id="sid")- await channel.send_delta("chat-1", "", stream_id="sid", stream_end=True)+ await history.stream(channel, "send_delta", "chat-1", "second", stream_id="sid")+ await history.stream(channel, "send_delta", "chat-1", "", stream_id="sid", stream_end=True)payloads = [json.loads(call.args[0]) for call in mock_ws.send.await_args_list]assert payloads[1]["merge_next"] is True@@ -2829,10 +2826,10 @@ async def test_send_delta_keeps_buffer_across_merged_stream_boundary() -> None:"second",]assert ("chat-1", "sid") not in channel._stream_text_buffers- lines = read_transcript_lines("websocket:chat-1")+ lines = history.read("websocket:chat-1")assert [line["event"] for line in lines] == ["stream_end", "stream_end"]assert [line["text"] for line in lines] == ["first ", "first second"]- body = build_webui_thread_response("websocket:chat-1")+ body = history.build("websocket:chat-1")assert body is not Noneassert _thread_conversation_events(body)[-1]["content"] == "first second"@@ -3006,20 +3003,20 @@ async def test_send_reasoning_without_subscribers_is_noop() -> None:@pytest.mark.asyncio-async def test_stream_transcript_persists_without_subscribers() -> None:- from nanobot.webui.transcript import build_webui_thread_response, read_transcript_lines+async def test_stream_transcript_persists_without_subscribers(history, ) -> None:+bus = MagicMock()channel = WebSocketChannel({"enabled": True, "allowFrom": ["*"], "streaming": True},bus,- gateway=_basic_handler(bus),+ gateway=_basic_handler(bus, session_manager=history.sessions),)- await channel.send_delta("chat-1", "hello", stream_id="s1")- await channel.send_delta("chat-1", " world", stream_id="s1")- await channel.send_delta("chat-1", "", stream_id="s1", stream_end=True)- await channel.send(OutboundMessage(+ await history.stream(channel, "send_delta", "chat-1", "hello", stream_id="s1")+ await history.stream(channel, "send_delta", "chat-1", " world", stream_id="s1")+ await history.stream(channel, "send_delta", "chat-1", "", stream_id="s1", stream_end=True)+ await channel.send(await history.message(channel="websocket",chat_id="chat-1",content="",@@ -3027,10 +3024,10 @@ async def test_stream_transcript_persists_without_subscribers() -> None:))assert channel._subs == {}- lines = read_transcript_lines("websocket:chat-1")+ lines = history.read("websocket:chat-1")assert [line["event"] for line in lines] == ["stream_end", "turn_end"]assert lines[0]["text"] == "hello world"- body = build_webui_thread_response("websocket:chat-1")+ body = history.build("websocket:chat-1")assert body is not Noneassert _thread_conversation_events(body)[-1]["role"] == "assistant"assert _thread_conversation_events(body)[-1]["content"] == "hello world"@@ -3071,16 +3068,17 @@ async def test_stream_policy_does_not_copy_history_on_runtime_loop(return original_copy(value, *args, **kwargs)monkeypatch.setattr(session_state, "deepcopy", copy_off_loop)- append = MagicMock()- monkeypatch.setattr("nanobot.webui.transcript.append_transcript_object", append)+ append = AsyncMock(wraps=sessions.state.append_history)+ monkeypatch.setattr(sessions.state, "append_history", append)+ metadata = {"_history_generation": draft.generation}try:policy = channel.gateway.temporary_chats.message_policy(owner, chat_id, "hello")assert (policy is not None) is temporary- await channel.send_delta(chat_id, "one", stream_id="s1")- await channel.send_delta(chat_id, " two", stream_id="s1")- await channel.send_delta(chat_id, "", stream_id="s1", stream_end=True)- await channel.send_reasoning_delta(chat_id, "plan", stream_id="r1")- await channel.send_reasoning_end(chat_id, stream_id="r1")+ await channel.send_delta(chat_id, "one", stream_id="s1", metadata=metadata)+ await channel.send_delta(chat_id, " two", stream_id="s1", metadata=metadata)+ await channel.send_delta(chat_id, "", stream_id="s1", stream_end=True, metadata=metadata)+ await channel.send_reasoning_delta(chat_id, "plan", stream_id="r1", metadata=metadata)+ await channel.send_reasoning_end(chat_id, stream_id="r1", metadata=metadata)assert append.call_count == (0 if temporary else 2)finally:channel.gateway.temporary_chats.close()@@ -3088,52 +3086,52 @@ async def test_stream_policy_does_not_copy_history_on_runtime_loop(@pytest.mark.asyncio-async def test_stream_transcript_writes_once_per_completed_segment(monkeypatch) -> None:+async def test_stream_transcript_writes_once_per_completed_segment(history, monkeypatch) -> None:bus = MagicMock()channel = WebSocketChannel({"enabled": True, "allowFrom": ["*"], "streaming": True},bus,- gateway=_basic_handler(bus),+ gateway=_basic_handler(bus, session_manager=history.sessions),)- append = MagicMock()- monkeypatch.setattr("nanobot.webui.transcript.append_transcript_object", append)+ append = AsyncMock(wraps=history.sessions.state.append_history)+ monkeypatch.setattr(history.sessions.state, "append_history", append)- await channel.send_delta("chat-write-rate", "one", stream_id="s1")- await channel.send_delta("chat-write-rate", " two", stream_id="s1")- await channel.send_delta("chat-write-rate", " three", stream_id="s1")+ await history.stream(channel, "send_delta", "chat-write-rate", "one", stream_id="s1")+ await history.stream(channel, "send_delta", "chat-write-rate", " two", stream_id="s1")+ await history.stream(channel, "send_delta", "chat-write-rate", " three", stream_id="s1")append.assert_not_called()- await channel.send_delta("chat-write-rate", "", stream_id="s1", stream_end=True)+ await history.stream(channel, "send_delta", "chat-write-rate", "", stream_id="s1", stream_end=True)append.assert_called_once()- persisted = append.call_args.args[1]- assert persisted["event"] == "stream_end"- assert persisted["text"] == "one two three"+ persisted = append.call_args.args[1][0]+ assert persisted.kind is HistoryKind.ANSWER_SEGMENT+ assert persisted.content["text"] == "one two three"@pytest.mark.asyncio-async def test_reasoning_transcript_persists_one_canonical_record(monkeypatch) -> None:+async def test_reasoning_transcript_persists_one_canonical_record(history, monkeypatch) -> None:bus = MagicMock()channel = WebSocketChannel({"enabled": True, "allowFrom": ["*"]},bus,- gateway=_basic_handler(bus),+ gateway=_basic_handler(bus, session_manager=history.sessions),)- append = MagicMock()- monkeypatch.setattr("nanobot.webui.transcript.append_transcript_object", append)+ append = AsyncMock(wraps=history.sessions.state.append_history)+ monkeypatch.setattr(history.sessions.state, "append_history", append)- await channel.send_reasoning_delta("chat-reasoning-write-rate", "plan ", stream_id="r1")- await channel.send_reasoning_delta("chat-reasoning-write-rate", "then act", stream_id="r1")+ await history.stream(channel, "send_reasoning_delta", "chat-reasoning-write-rate", "plan ", stream_id="r1")+ await history.stream(channel, "send_reasoning_delta", "chat-reasoning-write-rate", "then act", stream_id="r1")append.assert_not_called()- await channel.send_reasoning_end("chat-reasoning-write-rate", stream_id="r1")+ await history.stream(channel, "send_reasoning_end", "chat-reasoning-write-rate", stream_id="r1")append.assert_called_once()- persisted = append.call_args.args[1]- assert persisted["event"] == "reasoning_end"- assert persisted["text"] == "plan then act"+ persisted = append.call_args.args[1][0]+ assert persisted.kind is HistoryKind.REASONING_SEGMENT+ assert persisted.content["text"] == "plan then act"@pytest.mark.asyncio@@ -3281,18 +3279,18 @@ async def test_failed_turn_end_exposes_safe_terminal_outcome() -> None:@pytest.mark.asyncio@pytest.mark.parametrize("notify", [False, True])@pytest.mark.parametrize("show_notices", [False, True])-async def test_context_compaction_started_is_live_only(notify, show_notices) -> None:+async def test_context_compaction_started_is_live_only(history, notify, show_notices) -> None:bus = MagicMock()channel = WebSocketChannel({"enabled": True, "allowFrom": ["*"]},bus,- gateway=_basic_handler(bus),+ gateway=_basic_handler(bus, session_manager=history.sessions),)mock_ws = AsyncMock()channel._attach(mock_ws, "chat-compaction-started")channel.show_compaction_notices = show_notices- await channel.send(OutboundMessage(+ await channel.send(await history.message(channel="websocket",chat_id="chat-compaction-started",content="Compressing context…",@@ -3309,25 +3307,25 @@ async def test_context_compaction_started_is_live_only(notify, show_notices) ->"compaction_id": "compact-started","phase": "started",}]- assert read_transcript_lines("websocket:chat-compaction-started") == []+ assert history.read("websocket:chat-compaction-started") == []@pytest.mark.asyncio@pytest.mark.parametrize("notify", [False, True])@pytest.mark.parametrize("show_notices", [False, True])@pytest.mark.parametrize("phase", ["succeeded", "failed", "cancelled"])-async def test_context_compaction_is_structured_persisted_and_summary_free(notify, show_notices, phase) -> None:+async def test_context_compaction_is_structured_persisted_and_summary_free(history, notify, show_notices, phase) -> None:bus = MagicMock()channel = WebSocketChannel({"enabled": True, "allowFrom": ["*"]},bus,- gateway=_basic_handler(bus),+ gateway=_basic_handler(bus, session_manager=history.sessions),)mock_ws = AsyncMock()channel._attach(mock_ws, "chat-compaction")channel.show_compaction_notices = show_notices- await channel.send(OutboundMessage(+ await channel.send(await history.message(channel="websocket",chat_id="chat-compaction",content="Context compacted · LLM summary",@@ -3345,7 +3343,7 @@ async def test_context_compaction_is_structured_persisted_and_summary_free(notif"phase": phase,}assert _sent_ws_payloads(mock_ws) == [expected]- [persisted] = read_transcript_lines("websocket:chat-compaction")+ [persisted] = history.read("websocket:chat-compaction")assert {key: persisted[key] for key in expected} == expected@@ -3429,7 +3427,7 @@ async def test_system_command_turn_end_only_refreshes_session_metadata() -> None("owner-new", "owner-old", False),],)-async def test_turn_end_persists_and_conditionally_clears_when_delivery_fails(+async def test_turn_end_persists_and_conditionally_clears_when_delivery_fails(history,active_owner: str,event_owner: str,expected_cleared: bool,@@ -3438,7 +3436,7 @@ async def test_turn_end_persists_and_conditionally_clears_when_delivery_fails(channel = WebSocketChannel({"enabled": True, "allowFrom": ["*"]},bus,- gateway=_basic_handler(bus),+ gateway=_basic_handler(bus, session_manager=history.sessions),)mock_ws = AsyncMock()mock_ws.send.side_effect = RuntimeError("fanout failed")@@ -3447,7 +3445,7 @@ async def test_turn_end_persists_and_conditionally_clears_when_delivery_fails(await _register_running_turn(chat_id, 1234.5, owner=active_owner)try:- await channel.send(OutboundMessage(+ await channel.send(await history.message(channel="websocket",chat_id=chat_id,content="",@@ -3456,7 +3454,7 @@ async def test_turn_end_persists_and_conditionally_clears_when_delivery_fails())await asyncio.wait_for(_wait_for_connection_cleanup(channel, mock_ws), timeout=1)- assert read_transcript_lines(f"websocket:{chat_id}")[-1]["event"] == "turn_end"+ assert history.read(f"websocket:{chat_id}")[-1]["event"] == "turn_end"assert (wth.websocket_turn_wall_started_at(chat_id) is None) is expected_clearedif not expected_cleared:assert wth._WEBSOCKET_TURN_OWNERS[chat_id] == active_owner@@ -3465,7 +3463,7 @@ async def test_turn_end_persists_and_conditionally_clears_when_delivery_fails(@pytest.mark.asyncio-async def test_turn_end_keeps_registry_when_transcript_persistence_fails(+async def test_turn_end_keeps_registry_when_transcript_persistence_fails(history,monkeypatch,) -> None:from nanobot.bus.events import InboundMessage@@ -3475,7 +3473,7 @@ async def test_turn_end_keeps_registry_when_transcript_persistence_fails(channel = WebSocketChannel({"enabled": True, "allowFrom": ["*"]},bus,- gateway=_basic_handler(bus),+ gateway=_basic_handler(bus, session_manager=history.sessions),)chat_id = "turn-end-persistence-failure"owner = "owner-persist"@@ -3492,10 +3490,10 @@ async def test_turn_end_keeps_registry_when_transcript_persistence_fails(},)await wth.publish_turn_run_status(bus, inbound, "running", started_at=1234.5)- append = MagicMock(side_effect=OSError("disk full"))- monkeypatch.setattr("nanobot.webui.transcript.append_transcript_object", append)+ append = AsyncMock(side_effect=OSError("disk full"))+ monkeypatch.setattr(history.sessions.state, "append_history", append)- await channel.send(OutboundMessage(+ await channel.send(await history.message(channel="websocket",chat_id=chat_id,content="",@@ -3512,7 +3510,7 @@ async def test_turn_end_keeps_registry_when_transcript_persistence_fails(assert wth.websocket_turn_id(chat_id) == turn_idassert wth._WEBSOCKET_TURN_OWNERS[chat_id] == owner- await channel.send(OutboundMessage(+ await channel.send(await history.message(channel="websocket",chat_id=chat_id,content="",@@ -3529,24 +3527,24 @@ async def test_turn_end_keeps_registry_when_transcript_persistence_fails(@pytest.mark.asyncio-async def test_durable_incomplete_marker_stays_pending_without_safe_session_recovery(+async def test_durable_incomplete_marker_stays_pending_without_safe_session_recovery(history,monkeypatch,) -> None:from nanobot.bus.events import InboundMessage- from nanobot.webui.transcript import build_webui_thread_response+bus = MagicMock()bus.publish_outbound = AsyncMock()channel = WebSocketChannel({"enabled": True, "allowFrom": ["*"]},bus,- gateway=_basic_handler(bus),+ gateway=_basic_handler(bus, session_manager=history.sessions),)chat_id = "answer-persistence-failure"key = f"websocket:{chat_id}"owner = "owner-answer"turn_id = "turn-answer"- append_transcript_object(+ history.append(key,{"event": "user", "chat_id": chat_id, "text": "question", "turn_id": turn_id},)@@ -3563,29 +3561,29 @@ async def test_durable_incomplete_marker_stays_pending_without_safe_session_reco)await wth.publish_turn_run_status(bus, inbound, "running", started_at=1234.5)- original_append = append_transcript_object+ original_append = history.sessions.state.append_history- def fail_answer(session_key: str, event: dict[str, Any]) -> None:- if event.get("event") == "message":+ async def fail_answer(session_key, facts, *, expected_generation):+ if any(fact.kind is HistoryKind.ANSWER for fact in facts):raise OSError("transient disk failure")- original_append(session_key, event)+ await original_append(session_key, facts, expected_generation=expected_generation)- monkeypatch.setattr("nanobot.webui.transcript.append_transcript_object", fail_answer)+ monkeypatch.setattr(history.sessions.state, "append_history", fail_answer)- await channel.send(OutboundMessage(+ await channel.send(await history.message(channel="websocket",chat_id=chat_id,content="answer",metadata=dict(inbound.metadata),))- await channel.send(OutboundMessage(+ await channel.send(await history.message(channel="websocket",chat_id=chat_id,content="",metadata=dict(inbound.metadata),event=TurnEndEvent(),))- await channel.send(OutboundMessage(+ await channel.send(await history.message(channel="websocket",chat_id=chat_id,content="",@@ -3599,7 +3597,7 @@ async def test_durable_incomplete_marker_stays_pending_without_safe_session_recowth._WEBSOCKET_TURN_WALL_STARTED_AT.clear()wth._WEBSOCKET_TURN_IDS.clear()wth._WEBSOCKET_TURN_OWNERS.clear()- body = build_webui_thread_response(+ body = history.build(key,active_turn_started_at=wth.websocket_turn_wall_started_at(chat_id),active_turn_id=wth.websocket_turn_id(chat_id),@@ -3608,7 +3606,7 @@ async def test_durable_incomplete_marker_stays_pending_without_safe_session_reco),)assert body is not None- assert read_transcript_lines(key)[-1]["transcript_incomplete"] is True+ assert history.read(key)[-1]["transcript_incomplete"] is Trueassert body["completed_turn_ids"] == []assert [(message["role"], message["content"])@@ -3621,7 +3619,7 @@ async def test_durable_incomplete_marker_stays_pending_without_safe_session_reco@pytest.mark.asyncio-async def test_http_replay_recovers_marked_answer_from_session_after_gateway_restart(+async def test_http_replay_recovers_marked_answer_from_session_after_gateway_restart(history,tmp_path,monkeypatch,) -> None:@@ -3636,13 +3634,13 @@ async def test_http_replay_recovers_marked_answer_from_session_after_gateway_reskey = f"websocket:{chat_id}"owner = "owner-answer-recovery"turn_id = "turn-answer-recovery"- sessions_path = tmp_path / "sessions"- sessions = SessionManager(sessions_path)+ sessions_path = history.sessions.workspace+ sessions = history.sessionssession = sessions.get_or_create(key)session.add_message("user", "question")session.add_message("assistant", "durable answer")sessions.save(session)- append_transcript_object(+ history.append(key,{"event": "user",@@ -3672,21 +3670,21 @@ async def test_http_replay_recovers_marked_answer_from_session_after_gateway_res)await wth.publish_turn_run_status(bus, inbound, "running", started_at=1234.5)- original_append = append_transcript_object+ original_append = history.sessions.state.append_history- def fail_answer(session_key: str, event: dict[str, Any]) -> None:- if event.get("event") == "message":+ async def fail_answer(session_key, facts, *, expected_generation):+ if any(fact.kind is HistoryKind.ANSWER for fact in facts):raise OSError("transient disk failure")- original_append(session_key, event)+ await original_append(session_key, facts, expected_generation=expected_generation)- monkeypatch.setattr("nanobot.webui.transcript.append_transcript_object", fail_answer)- await channel.send(OutboundMessage(+ monkeypatch.setattr(history.sessions.state, "append_history", fail_answer)+ await channel.send(await history.message(channel="websocket",chat_id=chat_id,content="durable answer",metadata=dict(inbound.metadata),))- await channel.send(OutboundMessage(+ await channel.send(await history.message(channel="websocket",chat_id=chat_id,content="",@@ -3694,7 +3692,7 @@ async def test_http_replay_recovers_marked_answer_from_session_after_gateway_resevent=TurnEndEvent(),))- persisted_lines = read_transcript_lines(key)+ persisted_lines = history.read(key)assert persisted_lines[-1]["event"] == "turn_end"assert persisted_lines[-1]["transcript_incomplete"] is True@@ -3718,10 +3716,7 @@ async def test_http_replay_recovers_marked_answer_from_session_after_gateway_resHeaders([("Authorization", "Bearer tok")]),)- response = restarted_channel.gateway.http._handle_webui_thread_get(- request,- encoded_key,- )+ response = await restarted_channel.gateway.http._handle_webui_thread_get_async(request, encoded_key)assert response.status_code == 200body = json.loads(response.body.decode())@@ -5204,10 +5199,10 @@ async def test_midflight_allowlist_revocation_rejects_turn_without_ack(@pytest.mark.asyncio-async def test_authorized_webui_turn_is_acked_after_bus_acceptance(+async def test_authorized_webui_turn_is_acked_after_bus_acceptance(history,bus: MagicMock,) -> None:- channel = _ch(bus)+ channel = _ch(bus, session_manager=history.sessions)conn = AsyncMock()conn.remote_address = ("127.0.0.1", 50123)@@ -5233,7 +5228,7 @@ async def test_authorized_webui_turn_is_acked_after_bus_acceptance(owner,"turn-accepted",)- thread = build_webui_thread_response(+ thread = history.build("websocket:chat-accepted",active_turn_started_at=wth.websocket_turn_wall_started_at("chat-accepted"),active_turn_id=wth.websocket_turn_id("chat-accepted"),@@ -5557,13 +5552,13 @@ async def test_multiplex_new_chat_roundtrip(bus: MagicMock, start_listener) -> N@pytest.mark.asyncio-async def test_fork_chat_copies_only_prefix_session_and_transcript(+async def test_fork_chat_copies_only_prefix_session_and_transcript(history,bus: MagicMock,tmp_path,monkeypatch: pytest.MonkeyPatch,) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)- sessions = SessionManager(tmp_path / "sessions")+ sessions = history.sessionssource = sessions.get_or_create("websocket:source")source.metadata["webui"] = Truesource.add_message("user", "round1")@@ -5575,7 +5570,7 @@ async def test_fork_chat_copies_only_prefix_session_and_transcript({"event": "message", "chat_id": "source", "text": "answer1"},{"event": "user", "chat_id": "source", "text": "future"},):- append_transcript_object("websocket:source", ev)+ history.append("websocket:source", ev)channel = WebSocketChannel({"enabled": True, "allowFrom": ["*"], "host": "127.0.0.1"},@@ -5601,7 +5596,7 @@ async def test_fork_chat_copies_only_prefix_session_and_transcript(saved = sessions.read_session_file(f"websocket:{fork_id}")assert [m["content"] for m in saved["messages"]] == ["round1", "answer1"]assert saved["metadata"]["title"] == "Fork: Old title"- fork_lines = read_transcript_lines(f"websocket:{fork_id}")+ fork_lines = history.read(f"websocket:{fork_id}")assert [line.get("text") for line in fork_lines] == ["round1", "answer1", None]assert fork_lines[-1]["event"] == "fork_marker"assert all(line.get("chat_id") == fork_id for line in fork_lines)@@ -5609,13 +5604,13 @@ async def test_fork_chat_copies_only_prefix_session_and_transcript(bus.publish_inbound.assert_not_awaited()@pytest.mark.asyncio-async def test_webui_message_envelope_appends_user_transcript(+async def test_webui_message_envelope_appends_user_transcript(history,bus: MagicMock,tmp_path,monkeypatch: pytest.MonkeyPatch,) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)- sessions = SessionManager(tmp_path / "sessions")+ sessions = history.sessionschannel = WebSocketChannel({"enabled": True, "allowFrom": ["*"], "host": "127.0.0.1"},bus,@@ -5635,7 +5630,7 @@ async def test_webui_message_envelope_appends_user_transcript(},)- [line] = read_transcript_lines("websocket:source")+ [line] = history.read("websocket:source")assert {"event": line.get("event"),"chat_id": line.get("chat_id"),@@ -5854,23 +5849,24 @@ def test_is_valid_chat_id(value: Any, expected: bool) -> None:assert _is_valid_chat_id(value) is expected-def test_handle_webui_thread_get_returns_json(tmp_path, monkeypatch) -> None:+async def test_handle_webui_thread_get_returns_json(history, tmp_path, monkeypatch) -> None:from urllib.parse import quotefrom websockets.datastructures import Headersfrom websockets.http11 import Request- from nanobot.webui.transcript import append_transcript_object+monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:c1"- append_transcript_object(key, {"event": "user", "chat_id": "c1", "text": "hi"})+ history.append(key, {"event": "user", "chat_id": "c1", "text": "hi"})bus = MagicMock()- channel = _ch(bus)+ channel = _ch(bus, session_manager=history.sessions)channel.gateway.tokens.api_tokens["tok"] = time.monotonic() + 300.0enc = quote(key, safe="")req = Request(f"/api/sessions/{enc}/webui-thread", Headers([("Authorization", "Bearer tok")]))- resp = channel.gateway.http._handle_webui_thread_get(req, enc)+ resp = await channel.gateway.http._handle_webui_thread_get_async(req, enc)assert resp.status_code == 200body = json.loads(resp.body.decode())assert body["sessionKey"] == key@@ -5881,18 +5877,18 @@ def test_handle_webui_thread_get_returns_json(tmp_path, monkeypatch) -> None:@pytest.mark.asyncio-async def test_webui_thread_dispatch_runs_replay_off_event_loop(tmp_path, monkeypatch) -> None:+async def test_webui_thread_dispatch_runs_replay_off_event_loop(history, tmp_path, monkeypatch) -> None:from urllib.parse import quotefrom websockets.http11 import Requestmonkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:threaded-history"- append_transcript_object(+ history.append(key,{"event": "user", "chat_id": "threaded-history", "text": "hi"},)- gateway = _basic_handler(MagicMock(), workspace_path=tmp_path)+ gateway = _basic_handler(MagicMock(), workspace_path=tmp_path, session_manager=history.sessions)gateway.tokens.api_tokens["tok"] = time.monotonic() + 300.0encoded = quote(key, safe="")request = Request(@@ -5919,7 +5915,7 @@ async def test_webui_thread_dispatch_runs_replay_off_event_loop(tmp_path, monkey@pytest.mark.asyncio-async def test_webui_thread_diagnostics_hash_session_key(tmp_path, monkeypatch) -> None:+async def test_webui_thread_diagnostics_hash_session_key(history, tmp_path, monkeypatch) -> None:from urllib.parse import quotefrom websockets.http11 import Request@@ -5932,8 +5928,8 @@ async def test_webui_thread_diagnostics_hash_session_key(tmp_path, monkeypatch){"event": "message", "chat_id": "private-session-name", "text": "hello"},{"event": "turn_end", "chat_id": "private-session-name"},):- append_transcript_object(key, event)- gateway = _basic_handler(MagicMock(), workspace_path=tmp_path)+ history.append(key, event)+ gateway = _basic_handler(MagicMock(), workspace_path=tmp_path, session_manager=history.sessions)gateway.tokens.api_tokens["tok"] = time.monotonic() + 300.0gateway.http._log = MagicMock()encoded = quote(key, safe="")@@ -6002,7 +5998,8 @@ async def test_handle_session_context_get_reads_detached_session() -> None:manager.read_session_snapshot.assert_called_once_with(session.key)-def test_handle_webui_thread_get_reports_registered_turn_as_pending(+async def test_handle_webui_thread_get_reports_registered_turn_as_pending(history,tmp_path,monkeypatch,) -> None:@@ -6011,7 +6008,7 @@ def test_handle_webui_thread_get_reports_registered_turn_as_pending(from websockets.datastructures import Headersfrom websockets.http11 import Request- from nanobot.webui.transcript import append_transcript_object+monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)monkeypatch.setattr(@@ -6023,7 +6020,7 @@ def test_handle_webui_thread_get_reports_registered_turn_as_pending(lambda chat_id: "turn-running" if chat_id == "running" else None,)key = "websocket:running"- append_transcript_object(+ history.append(key,{"event": "user",@@ -6033,12 +6030,12 @@ def test_handle_webui_thread_get_reports_registered_turn_as_pending(},)bus = MagicMock()- channel = _ch(bus)+ channel = _ch(bus, session_manager=history.sessions)channel.gateway.tokens.api_tokens["tok"] = time.monotonic() + 300.0enc = quote(key, safe="")req = Request(f"/api/sessions/{enc}/webui-thread", Headers([("Authorization", "Bearer tok")]))- resp = channel.gateway.http._handle_webui_thread_get(req, enc)+ resp = await channel.gateway.http._handle_webui_thread_get_async(req, enc)assert resp.status_code == 200body = json.loads(resp.body.decode())@@ -6047,7 +6044,7 @@ def test_handle_webui_thread_get_reports_registered_turn_as_pending(@pytest.mark.asyncio-async def test_idle_registry_stays_pending_until_turn_end_is_persisted(+async def test_idle_registry_stays_pending_until_turn_end_is_persisted(history,tmp_path,monkeypatch,) -> None:@@ -6058,12 +6055,12 @@ async def test_idle_registry_stays_pending_until_turn_end_is_persisted(from nanobot.bus.events import InboundMessagefrom nanobot.session import webui_turns as wth- from nanobot.webui.transcript import append_transcript_object+monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:idle-order"turn_id = "turn-idle-order"- append_transcript_object(+ history.append(key,{"event": "user",@@ -6081,7 +6078,7 @@ async def test_idle_registry_stays_pending_until_turn_end_is_persisted(content="hi",metadata={"webui_turn_id": turn_id},)- channel = _ch(bus)+ channel = _ch(bus, session_manager=history.sessions)channel.gateway.tokens.api_tokens["tok"] = time.monotonic() + 300.0enc = quote(key, safe="")request = Request(@@ -6093,10 +6090,10 @@ async def test_idle_registry_stays_pending_until_turn_end_is_persisted(await wth.publish_turn_run_status(bus, inbound, "running")await wth.publish_turn_run_status(bus, inbound, "idle")- before_delivery = channel.gateway.http._handle_webui_thread_get(request, enc)+ before_delivery = await channel.gateway.http._handle_webui_thread_get_async(request, enc)assert json.loads(before_delivery.body.decode())["has_pending_tool_calls"] is True- await channel.send(OutboundMessage(+ await channel.send(await history.message(channel="websocket",chat_id="idle-order",content="",@@ -6104,7 +6101,7 @@ async def test_idle_registry_stays_pending_until_turn_end_is_persisted(event=TurnEndEvent(),))- after_delivery = channel.gateway.http._handle_webui_thread_get(request, enc)+ after_delivery = await channel.gateway.http._handle_webui_thread_get_async(request, enc)assert json.loads(after_delivery.body.decode())["has_pending_tool_calls"] is Falseassert wth.websocket_turn_wall_started_at("idle-order") is Noneassert wth.websocket_turn_id("idle-order") is None@@ -6115,7 +6112,7 @@ async def test_idle_registry_stays_pending_until_turn_end_is_persisted(@pytest.mark.asyncio-async def test_webui_thread_api_restores_older_owner_after_latest_completes() -> None:+async def test_webui_thread_api_restores_older_owner_after_latest_completes(history, ) -> None:from urllib.parse import quotefrom websockets.datastructures import Headers@@ -6125,7 +6122,7 @@ async def test_webui_thread_api_restores_older_owner_after_latest_completes() ->chat_id = "concurrent-projection"key = f"websocket:{chat_id}"- append_transcript_object(+ history.append(key,{"event": "user",@@ -6162,14 +6159,14 @@ async def test_webui_thread_api_restores_older_owner_after_latest_completes() ->await wth.publish_turn_run_status(bus, second, "running", started_at=200.0)assert wth.clear_websocket_turn_if_current(chat_id, "owner-second") is True- channel = _ch(bus)+ channel = _ch(bus, session_manager=history.sessions)channel.gateway.tokens.api_tokens["tok"] = time.monotonic() + 300.0enc = quote(key, safe="")request = Request(f"/api/sessions/{enc}/webui-thread",Headers([("Authorization", "Bearer tok")]),)- response = channel.gateway.http._handle_webui_thread_get(request, enc)+ response = await channel.gateway.http._handle_webui_thread_get_async(request, enc)assert response.status_code == 200payload = json.loads(response.body.decode())@@ -6179,6 +6176,7 @@ async def test_webui_thread_api_restores_older_owner_after_latest_completes() ->assert wth._WEBSOCKET_TURN_OWNERS[chat_id] == "owner-first"@pytest.mark.parametrize(("active_turn_id", "expected_pending"),[@@ -6186,7 +6184,7 @@ async def test_webui_thread_api_restores_older_owner_after_latest_completes() ->("turn-next", True),],)-def test_handle_webui_thread_get_reconciles_registered_turn_with_turn_end(+async def test_handle_webui_thread_get_reconciles_registered_turn_with_turn_end(history,tmp_path,monkeypatch,active_turn_id: str,@@ -6197,7 +6195,7 @@ def test_handle_webui_thread_get_reconciles_registered_turn_with_turn_end(from websockets.datastructures import Headersfrom websockets.http11 import Request- from nanobot.webui.transcript import append_transcript_object+monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)monkeypatch.setattr(@@ -6209,7 +6207,7 @@ def test_handle_webui_thread_get_reconciles_registered_turn_with_turn_end(lambda chat_id: active_turn_id if chat_id == "running" else None,)key = "websocket:running"- append_transcript_object(+ history.append(key,{"event": "user",@@ -6218,7 +6216,7 @@ def test_handle_webui_thread_get_reconciles_registered_turn_with_turn_end("turn_id": "turn-complete",},)- append_transcript_object(+ history.append(key,{"event": "message",@@ -6227,7 +6225,7 @@ def test_handle_webui_thread_get_reconciles_registered_turn_with_turn_end("turn_id": "turn-complete",},)- append_transcript_object(+ history.append(key,{"event": "turn_end",@@ -6236,12 +6234,12 @@ def test_handle_webui_thread_get_reconciles_registered_turn_with_turn_end(},)bus = MagicMock()- channel = _ch(bus)+ channel = _ch(bus, session_manager=history.sessions)channel.gateway.tokens.api_tokens["tok"] = time.monotonic() + 300.0enc = quote(key, safe="")req = Request(f"/api/sessions/{enc}/webui-thread", Headers([("Authorization", "Bearer tok")]))- resp = channel.gateway.http._handle_webui_thread_get(req, enc)+ resp = await channel.gateway.http._handle_webui_thread_get_async(req, enc)assert resp.status_code == 200body = json.loads(resp.body.decode())@@ -6250,29 +6248,30 @@ def test_handle_webui_thread_get_reconciles_registered_turn_with_turn_end(assert body["active_turn_id"] == active_turn_id-def test_handle_webui_thread_get_accepts_pagination_query(tmp_path, monkeypatch) -> None:+async def test_handle_webui_thread_get_accepts_pagination_query(history, tmp_path, monkeypatch) -> None:from urllib.parse import quotefrom websockets.datastructures import Headersfrom websockets.http11 import Request- from nanobot.webui.transcript import append_transcript_object+monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:paged-route"for idx in range(1, 4):- append_transcript_object(+ history.append(key,{"event": "user", "chat_id": "paged-route", "text": f"q{idx}"},)- append_transcript_object(+ history.append(key,{"event": "message", "chat_id": "paged-route", "text": f"a{idx}"},)- append_transcript_object(key, {"event": "turn_end", "chat_id": "paged-route"})+ history.append(key, {"event": "turn_end", "chat_id": "paged-route"})bus = MagicMock()- channel = _ch(bus)+ channel = _ch(bus, session_manager=history.sessions)channel.gateway.tokens.api_tokens["tok"] = time.monotonic() + 300.0enc = quote(key, safe="")req = Request(@@ -6280,7 +6279,7 @@ def test_handle_webui_thread_get_accepts_pagination_query(tmp_path, monkeypatch)Headers([("Authorization", "Bearer tok")]),)- resp = channel.gateway.http._handle_webui_thread_get(req, enc)+ resp = await channel.gateway.http._handle_webui_thread_get_async(req, enc)assert resp.status_code == 200body = json.loads(resp.body.decode())@@ -6291,12 +6290,13 @@ def test_handle_webui_thread_get_accepts_pagination_query(tmp_path, monkeypatch)assert body["page"]["before_cursor"]@pytest.mark.parametrize("sources", [None,[],[{"provider": "xai", "model": "grok", "preset": "saved backup", "fallback": True}],])-def test_handle_webui_thread_get_returns_canonical_events_by_default(+async def test_handle_webui_thread_get_returns_canonical_events_by_default(history,tmp_path,monkeypatch,sources,@@ -6306,24 +6306,24 @@ def test_handle_webui_thread_get_returns_canonical_events_by_default(from websockets.datastructures import Headersfrom websockets.http11 import Request- from nanobot.webui.transcript import append_transcript_object+monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:event-route"- append_transcript_object(+ history.append(key,{"event": "user", "chat_id": "event-route", "text": "question"},)- append_transcript_object(+ history.append(key,{"event": "message", "chat_id": "event-route", "text": "answer",**({"response_sources": sources} if sources is not None else {}),},)- append_transcript_object(key, {"event": "turn_end", "chat_id": "event-route"})+ history.append(key, {"event": "turn_end", "chat_id": "event-route"})- channel = _ch(MagicMock())+ channel = _ch(MagicMock(), session_manager=history.sessions)channel.gateway.tokens.api_tokens["tok"] = time.monotonic() + 300.0encoded = quote(key, safe="")request = Request(@@ -6331,7 +6331,7 @@ def test_handle_webui_thread_get_returns_canonical_events_by_default(Headers([("Authorization", "Bearer tok")]),)- response = channel.gateway.http._handle_webui_thread_get(request, encoded)+ response = await channel.gateway.http._handle_webui_thread_get_async(request, encoded)assert response.status_code == 200body = json.loads(response.body.decode())@@ -6553,7 +6553,8 @@ def test_handle_file_preview_allows_paths_outside_workspace_in_full_access(tmp_passert body["content"].splitlines() == ["value = 42"]-def test_handle_webui_thread_get_backfills_legacy_missing_user_rows(+async def test_handle_webui_thread_get_backfills_legacy_missing_user_rows(history,tmp_path,monkeypatch,) -> None:@@ -6562,17 +6563,17 @@ def test_handle_webui_thread_get_backfills_legacy_missing_user_rows(from websockets.datastructures import Headersfrom websockets.http11 import Request- from nanobot.webui.transcript import append_transcript_object+monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)workspace = tmp_path / "workspace"- sessions = SessionManager(workspace)+ sessions = history.sessionskey = "websocket:c-legacy"session = sessions.get_or_create(key)session.add_message("user", "legacy question")session.add_message("assistant", "legacy answer")sessions.save(session)- append_transcript_object(+ history.append(key,{"event": "message", "chat_id": "c-legacy", "text": "legacy answer"},)@@ -6586,7 +6587,7 @@ def test_handle_webui_thread_get_backfills_legacy_missing_user_rows(channel.gateway.tokens.api_tokens["tok"] = time.monotonic() + 300.0enc = quote(key, safe="")req = Request(f"/api/sessions/{enc}/webui-thread", Headers([("Authorization", "Bearer tok")]))- resp = channel.gateway.http._handle_webui_thread_get(req, enc)+ resp = await channel.gateway.http._handle_webui_thread_get_async(req, enc)assert resp.status_code == 200body = json.loads(resp.body.decode())@@ -6599,7 +6600,8 @@ def test_handle_webui_thread_get_backfills_legacy_missing_user_rows(]-def test_handle_webui_thread_get_does_not_backfill_cron_internal_prompt(+async def test_handle_webui_thread_get_does_not_backfill_cron_internal_prompt(history,tmp_path,monkeypatch,) -> None:@@ -6609,11 +6611,11 @@ def test_handle_webui_thread_get_does_not_backfill_cron_internal_prompt(from websockets.http11 import Requestfrom nanobot.cron.session_turns import CRON_HISTORY_META- from nanobot.webui.transcript import append_transcript_object+monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)workspace = tmp_path / "workspace"- sessions = SessionManager(workspace)+ sessions = history.sessionskey = "websocket:c-cron"session = sessions.get_or_create(key)session.add_message(@@ -6623,7 +6625,7 @@ def test_handle_webui_thread_get_does_not_backfill_cron_internal_prompt()session.add_message("assistant", "提醒已经到期。")sessions.save(session)- append_transcript_object(+ history.append(key,{"event": "message", "chat_id": "c-cron", "text": "提醒已经到期。"},)@@ -6637,7 +6639,7 @@ def test_handle_webui_thread_get_does_not_backfill_cron_internal_prompt(channel.gateway.tokens.api_tokens["tok"] = time.monotonic() + 300.0enc = quote(key, safe="")req = Request(f"/api/sessions/{enc}/webui-thread", Headers([("Authorization", "Bearer tok")]))- resp = channel.gateway.http._handle_webui_thread_get(req, enc)+ resp = await channel.gateway.http._handle_webui_thread_get_async(req, enc)assert resp.status_code == 200body = json.loads(resp.body.decode())@@ -6647,7 +6649,8 @@ def test_handle_webui_thread_get_does_not_backfill_cron_internal_prompt(]-def test_handle_webui_thread_get_does_not_backfill_trigger_internal_prompt(+async def test_handle_webui_thread_get_does_not_backfill_trigger_internal_prompt(history,tmp_path,monkeypatch,) -> None:@@ -6657,11 +6660,11 @@ def test_handle_webui_thread_get_does_not_backfill_trigger_internal_prompt(from websockets.http11 import Requestfrom nanobot.session.automation_turns import AUTOMATION_HISTORY_META- from nanobot.webui.transcript import append_transcript_object+monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)workspace = tmp_path / "workspace"- sessions = SessionManager(workspace)+ sessions = history.sessionskey = "websocket:c-trigger"session = sessions.get_or_create(key)session.add_message(@@ -6671,7 +6674,7 @@ def test_handle_webui_thread_get_does_not_backfill_trigger_internal_prompt()session.add_message("assistant", "PR #4502 已经开始 review。")sessions.save(session)- append_transcript_object(+ history.append(key,{"event": "message", "chat_id": "c-trigger", "text": "PR #4502 已经开始 review。"},)@@ -6685,7 +6688,7 @@ def test_handle_webui_thread_get_does_not_backfill_trigger_internal_prompt(channel.gateway.tokens.api_tokens["tok"] = time.monotonic() + 300.0enc = quote(key, safe="")req = Request(f"/api/sessions/{enc}/webui-thread", Headers([("Authorization", "Bearer tok")]))- resp = channel.gateway.http._handle_webui_thread_get(req, enc)+ resp = await channel.gateway.http._handle_webui_thread_get_async(req, enc)assert resp.status_code == 200body = json.loads(resp.body.decode())@@ -6695,7 +6698,8 @@ def test_handle_webui_thread_get_does_not_backfill_trigger_internal_prompt(]-def test_handle_webui_thread_get_does_not_backfill_hidden_subagent_result(+async def test_handle_webui_thread_get_does_not_backfill_hidden_subagent_result(history,tmp_path,monkeypatch,) -> None:@@ -6705,11 +6709,11 @@ def test_handle_webui_thread_get_does_not_backfill_hidden_subagent_result(from websockets.http11 import Requestfrom nanobot.session.history_visibility import HIDDEN_HISTORY_META- from nanobot.webui.transcript import append_transcript_object+monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)workspace = tmp_path / "workspace"- sessions = SessionManager(workspace)+ sessions = history.sessionskey = "websocket:c-subagent"session = sessions.get_or_create(key)session.add_message(@@ -6719,7 +6723,7 @@ def test_handle_webui_thread_get_does_not_backfill_hidden_subagent_result()session.add_message("assistant", "subagent summary")sessions.save(session)- append_transcript_object(+ history.append(key,{"event": "message", "chat_id": "c-subagent", "text": "subagent summary"},)@@ -6733,7 +6737,7 @@ def test_handle_webui_thread_get_does_not_backfill_hidden_subagent_result(channel.gateway.tokens.api_tokens["tok"] = time.monotonic() + 300.0enc = quote(key, safe="")req = Request(f"/api/sessions/{enc}/webui-thread", Headers([("Authorization", "Bearer tok")]))- resp = channel.gateway.http._handle_webui_thread_get(req, enc)+ resp = await channel.gateway.http._handle_webui_thread_get_async(req, enc)assert resp.status_code == 200body = json.loads(resp.body.decode())diff --git a/nanobot/channels/websocket/tests/test_websocket_http_routes.py b/nanobot/channels/websocket/tests/test_websocket_http_routes.pyindex 5752c0d84..1376f7732 100644--- a/nanobot/channels/websocket/tests/test_websocket_http_routes.py+++ b/nanobot/channels/websocket/tests/test_websocket_http_routes.py@@ -297,23 +297,23 @@ async def test_sessions_list_requires_bearer_token(@pytest.mark.asyncio-async def test_sessions_list_and_thread_restore_transcript_without_canonical_file(+async def test_sessions_list_and_thread_restore_display_without_model_messages(history,bus: MagicMock, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)- sm = SessionManager(tmp_path / "workspace")- from nanobot.webui.transcript import append_transcript_object+ sm = history.sessions+key = "websocket:restored-history"- append_transcript_object(+ history.append(key,{"event": "user", "chat_id": "restored-history", "text": "original question"},)- append_transcript_object(+ history.append(key,{"event": "message", "chat_id": "restored-history", "text": "original answer"},)- assert sm.read_session_snapshot(key) is None+ assert sm.read_session_snapshot(key).messages == []port = _free_port()channel = _ch(bus, session_manager=sm, port=port)@@ -344,7 +344,7 @@ async def test_sessions_list_and_thread_restore_transcript_without_canonical_fil"original question","original answer",]- assert sm.read_session_snapshot(key) is None+ assert sm.read_session_snapshot(key).messages == []finally:await channel.stop()await server_task@@ -2414,21 +2414,23 @@ async def test_webui_sidebar_state_routes_are_config_dir_scoped(@pytest.mark.asyncio-async def test_session_delete_removes_file(+async def test_session_delete_removes_canonical_and_display_history(history,bus: MagicMock, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)- sm = _seed_session(tmp_path, key="websocket:doomed")- from nanobot.webui.transcript import append_transcript_object+ sm = history.sessions+ draft = sm.get_or_create("websocket:doomed")+ draft.add_message("user", "canonical")+ sm.save(draft)- append_transcript_object("websocket:doomed", {"event": "user", "chat_id": "doomed", "text": "x"})++ history.append("websocket:doomed", {"event": "user", "chat_id": "doomed", "text": "x"})channel = _ch(bus, session_manager=sm, port=29903)server_task = asyncio.create_task(channel.start())try:session_key = "websocket:doomed"assert sm.read_session_snapshot(session_key) is not None- webui_path = tmp_path / "webui" / f"{SessionManager.safe_key('websocket:doomed')}.jsonl"- assert webui_path.is_file()+ assert history.read(session_key)[0]["text"] == "x"resp = await _webui_mutate(channel,"session.delete",@@ -2437,28 +2439,27 @@ async def test_session_delete_removes_file(assert resp.status_code == 200assert resp.json()["deleted"] is Trueassert sm.read_session_snapshot(session_key) is None- assert not webui_path.exists()+ assert history.read(session_key) == []finally:await channel.stop()await server_task@pytest.mark.asyncio-async def test_session_delete_removes_transcript_without_canonical_file(+async def test_session_delete_removes_display_without_model_messages(history,bus: MagicMock, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)- sm = SessionManager(tmp_path / "workspace")- from nanobot.webui.transcript import append_transcript_object+ sm = history.sessions+key = "websocket:transcript-only"- append_transcript_object(+ history.append(key,{"event": "user", "chat_id": "transcript-only", "text": "recover me"},)- assert sm.read_session_snapshot(key) is None- webui_path = tmp_path / "webui" / f"{SessionManager.safe_key(key)}.jsonl"- assert webui_path.is_file()+ assert sm.read_session_snapshot(key).messages == []+ assert history.read(key)[0]["text"] == "recover me"channel = _ch(bus, session_manager=sm, port=_free_port())server_task = asyncio.create_task(channel.start())@@ -2471,7 +2472,8 @@ async def test_session_delete_removes_transcript_without_canonical_file(assert response.status_code == 200assert response.json()["deleted"] is True- assert not webui_path.exists()+ assert sm.read_session_snapshot(key) is None+ assert history.read(key) == []finally:await channel.stop()await server_task@@ -3211,10 +3213,10 @@ async def test_session_delete_action_accepts_websocket_keys(@pytest.mark.asyncio-async def test_webui_thread_resigns_assistant_media_urls(+async def test_webui_thread_resigns_assistant_media_urls(history,bus: MagicMock, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:- from nanobot.webui.transcript import append_transcript_object+monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)media_root = tmp_path / "media"@@ -3228,11 +3230,11 @@ async def test_webui_thread_resigns_assistant_media_urls(monkeypatch.setattr("nanobot.webui.media_gateway.get_media_dir", fake_media_dir)- append_transcript_object(+ history.append("websocket:video-replay",{"event": "user", "chat_id": "video-replay", "text": "make a video"},)- append_transcript_object(+ history.append("websocket:video-replay",{"event": "message",@@ -3243,7 +3245,7 @@ async def test_webui_thread_resigns_assistant_media_urls(},)- channel = _ch(bus, port=29914)+ channel = _ch(bus, port=29914, session_manager=history.sessions)server_task = asyncio.create_task(channel.start())try:token = channel.gateway.tokens.issue_api_token(300)@@ -3311,10 +3313,10 @@ async def test_sessions_list_negotiates_gzip_across_repeated_headers(@pytest.mark.asyncio-async def test_webui_thread_complete_transcript_skips_session_history_read(+async def test_webui_thread_complete_transcript_skips_session_history_read(history,bus: MagicMock, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:- from nanobot.webui.transcript import append_transcript_object+monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:fast-thread"@@ -3324,7 +3326,7 @@ async def test_webui_thread_complete_transcript_skips_session_history_read({"event": "message", "chat_id": "fast-thread", "text": "hello back"},{"event": "turn_end", "chat_id": "fast-thread"},):- append_transcript_object(key, event)+ history.append(key, event)read_session_file = MagicMock(side_effect=AssertionError("complete transcripts must not read canonical history")@@ -3361,14 +3363,14 @@ async def test_webui_thread_complete_transcript_skips_session_history_read(@pytest.mark.asyncio-async def test_webui_thread_negotiates_gzip_for_large_payloads(+async def test_webui_thread_negotiates_gzip_for_large_payloads(history,bus: MagicMock, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:- from nanobot.webui.transcript import append_transcript_object+monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)- sm = SessionManager(tmp_path)- append_transcript_object(+ sm = history.sessions+ history.append("websocket:gzip-thread",{"event": "user",@@ -3421,10 +3423,10 @@ async def test_webui_thread_negotiates_gzip_for_large_payloads(@pytest.mark.asyncio-async def test_webui_thread_revalidates_and_loads_large_trace_details(+async def test_webui_thread_revalidates_and_loads_large_trace_details(history,bus: MagicMock, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:- from nanobot.webui.transcript import append_transcript_object+monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:revalidated-thread"@@ -3441,7 +3443,7 @@ async def test_webui_thread_revalidates_and_loads_large_trace_details({"event": "message", "chat_id": "revalidated-thread", "text": "done"},{"event": "turn_end", "chat_id": "revalidated-thread"},):- append_transcript_object(key, event)+ history.append(key, event)port = _free_port()channel = _ch(bus, session_manager=sm, workspace_path=tmp_path, port=port)server_task = asyncio.create_task(channel.start())@@ -3481,7 +3483,7 @@ async def test_webui_thread_revalidates_and_loads_large_trace_details(assert detail.json()["message_id"] == trace_event["projection_id"]assert detail.json()["events"][0]["text"] == trace- append_transcript_object(+ history.append(key,{"event": "user", "chat_id": "revalidated-thread", "text": "again"},)@@ -3497,15 +3499,15 @@ async def test_webui_thread_revalidates_and_loads_large_trace_details(@pytest.mark.asyncio-async def test_webui_thread_omits_validator_when_transcript_changes_during_replay(+async def test_webui_thread_omits_validator_when_transcript_changes_during_replay(history,bus: MagicMock, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:- from nanobot.webui.transcript import append_transcript_object+monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:changing-thread"sm = _seed_session(tmp_path, key=key)- append_transcript_object(+ history.append(key,{"event": "user", "chat_id": "changing-thread", "text": "hello"},)diff --git a/nanobot/channels/websocket/tests/test_websocket_outbound_delivery.py b/nanobot/channels/websocket/tests/test_websocket_outbound_delivery.pyindex 9f9b81e42..e2cb206cd 100644--- a/nanobot/channels/websocket/tests/test_websocket_outbound_delivery.py+++ b/nanobot/channels/websocket/tests/test_websocket_outbound_delivery.py@@ -83,7 +83,7 @@ def _channel() -> WebSocketChannel:)channel = WebSocketChannel(config, bus, gateway=gateway)# Keep disk latency out of every connection delivery synchronization check.- channel._persist_turn_transcript_event = MagicMock(return_value=True)+ channel._persist_turn_transcript_event = AsyncMock(return_value=True)return channeldiff --git a/nanobot/channels/websocket/tests/test_websocket_runtime_lifecycle.py b/nanobot/channels/websocket/tests/test_websocket_runtime_lifecycle.pyindex 64f719538..340521bfa 100644--- a/nanobot/channels/websocket/tests/test_websocket_runtime_lifecycle.py+++ b/nanobot/channels/websocket/tests/test_websocket_runtime_lifecycle.py@@ -44,6 +44,7 @@ class _FakeServer:def _channel() -> WebSocketChannel:gateway = MagicMock()gateway.session_manager = None+ gateway.transcripts.initialize = AsyncMock()gateway.http.settings_routes.close = AsyncMock()gateway.http.remote_instances.close = AsyncMock()return WebSocketChannel(diff --git a/nanobot/channels/websocket/tests/test_webui_session_admission.py b/nanobot/channels/websocket/tests/test_webui_session_admission.pyindex ed4687af3..e1a1f2527 100644--- a/nanobot/channels/websocket/tests/test_webui_session_admission.py+++ b/nanobot/channels/websocket/tests/test_webui_session_admission.py@@ -22,7 +22,6 @@ from nanobot.runtime_context import public_history_messagefrom nanobot.session.keys import UNIFIED_SESSION_KEY, session_key_for_channelfrom nanobot.session.manager import SessionManagerfrom nanobot.session.webui_turns import clear_websocket_turns-from nanobot.webui.transcript import read_transcript_linesclass _Provider(LLMProvider):@@ -155,7 +154,7 @@ async def test_failed_transport_admission_removes_new_sessions(assert await runtime.sessions.state.list_sessions() == []-async def test_temporary_chat_retains_its_own_generation(runtime: _Runtime) -> None:+async def test_temporary_chat_retains_its_own_generation(history, runtime: _Runtime) -> None:await runtime.channel._dispatch_envelope(runtime.connection, "webui-client", {"type": "new_temporary_chat"},)@@ -166,7 +165,7 @@ async def test_temporary_chat_retains_its_own_generation(runtime: _Runtime) -> Nassert runtime.provider.calls == 1assert runtime.loop._effective_session_key(first) == f"websocket:{chat_id}"assert await runtime.sessions.state.list_sessions() == []- assert read_transcript_lines(f"websocket:{chat_id}") == []+ assert history.read(f"websocket:{chat_id}") == []queued = await runtime.message(chat_id, "discarded input")await runtime.channel.gateway.temporary_chats.discard(runtime.connection, chat_id)diff --git a/nanobot/cli/gateway_runtime.py b/nanobot/cli/gateway_runtime.pyindex af1d2c8a1..b1b67e85b 100644--- a/nanobot/cli/gateway_runtime.py+++ b/nanobot/cli/gateway_runtime.py@@ -83,14 +83,26 @@ async def _deliver_to_channel(if session_generation is not Noneelse raw_generation if isinstance(raw_generation, str) else None)+ key = session_key or (raw_key if isinstance(raw_key, str) else None)+ if key is None:+ key = session_key_for_channel(+ msg.channel, msg.chat_id, unified_session=unified_session,+ )+ if msg.channel == "websocket":+ from nanobot.agent.turn_delivery import TurnRoute+ from nanobot.session.display_history import HISTORY_GENERATION_METADATA_KEY+ from nanobot.session.webui_turns import WebuiTurnRoutePolicy+ from nanobot.webui.session_identity import webui_session_key++ route = await WebuiTurnRoutePolicy(session_manager).admit(+ TurnRoute(msg.channel, msg.chat_id, metadata), key, expected_generation,+ )+ metadata = route.metadata+ if key == webui_session_key(msg.chat_id):+ expected_generation = cast(str, metadata[HISTORY_GENERATION_METADATA_KEY])if metadata != (msg.metadata or {}):msg = replace(msg, metadata=metadata)if record and msg.channel != "cli" and msg.content.strip():- key = session_key or (raw_key if isinstance(raw_key, str) else None)- if key is None:- key = session_key_for_channel(- msg.channel, msg.chat_id, unified_session=unified_session,- )if expected_generation is None:# Cross-target sends admit their target before recording; subsequent# deletion or reset must still revoke this delivery's generation.@@ -525,9 +537,11 @@ def _run_gateway(cron = CronService(cron_store_path)trigger_store = LocalTriggerStore(config.workspace_path)+ webui_route_policy = WebuiTurnRoutePolicy(session_manager)turn_delivery_factory = TurnDeliveryFactory(bus,- route_policy=WebuiTurnRoutePolicy(session_manager),+ route_policy=webui_route_policy,+ route_admission=webui_route_policy.admit,)tools = ToolRegistry()diff --git a/nanobot/config/paths.py b/nanobot/config/paths.pyindex 3d8163307..c0ec6c449 100644--- a/nanobot/config/paths.py+++ b/nanobot/config/paths.py@@ -45,7 +45,7 @@ def get_logs_dir() -> Path:def get_webui_dir() -> Path:- """Return the directory for WebUI JSONL transcripts and legacy JSON snapshots."""+ """Return WebUI state and legacy display-history import/backup storage."""return get_runtime_subdir("webui")diff --git a/nanobot/session/display_history.py b/nanobot/session/display_history.pynew file mode 100644index 000000000..a73d35264--- /dev/null+++ b/nanobot/session/display_history.py@@ -0,0 +1,177 @@+"""Durable display facts, separate from provider/model messages and transports."""++from __future__ import annotations++import json+import sqlite3+from collections.abc import Iterator, Mapping+from dataclasses import dataclass, field+from enum import StrEnum+from pathlib import Path+from typing import Any, cast++MAX_HISTORY_FACT_BYTES = 8 * 1024 * 1024+HISTORY_GENERATION_METADATA_KEY = "_history_generation"+++class HistoryKind(StrEnum):+ INPUT = "input"+ ANSWER = "answer"+ ANSWER_SEGMENT = "answer_segment"+ REASONING = "reasoning"+ REASONING_SEGMENT = "reasoning_segment"+ PROGRESS = "progress"+ TOOL_ACTIVITY = "tool_activity"+ FILE_ACTIVITY = "file_activity"+ COMPLETION = "completion"+ FORK_BOUNDARY = "fork_boundary"+ COMPACTION = "compaction"+ # Only old transcripts can contain unfinished transport fragments.+ ANSWER_FRAGMENT = "answer_fragment"+ REASONING_FRAGMENT = "reasoning_fragment"+++@dataclass(frozen=True)+class HistoryFact:+ kind: HistoryKind+ content: Mapping[str, Any] = field(default_factory=lambda: cast(dict[str, Any], {}))+ turn_id: str | None = None+ phase: str | None = None+ sequence: int | None = None+ created_at_ms: int = 0++ def encode(self) -> str:+ record = json.dumps({+ "kind": self.kind.value, "content": dict(self.content),+ "turn_id": self.turn_id, "phase": self.phase,+ "sequence": self.sequence, "created_at_ms": self.created_at_ms,+ }, ensure_ascii=False, allow_nan=False, separators=(",", ":"))+ if len(record.encode("utf-8")) > MAX_HISTORY_FACT_BYTES:+ raise ValueError("history fact exceeds the 8 MiB storage limit")+ return record++ @classmethod+ def decode(cls, record: str) -> HistoryFact:+ value = cast(dict[str, Any], json.loads(record))+ return cls(+ HistoryKind(value["kind"]), value["content"], value["turn_id"],+ value["phase"], value["sequence"], value["created_at_ms"],+ )+++@dataclass(frozen=True)+class HistoryImport:+ key: str+ facts: list[HistoryFact]+ created_at_ms: int = 0+ updated_at_ms: int = 0+++@dataclass(frozen=True)+class HistoryTurn:+ ordinal: int+ record_count: int+ byte_count: int+ user_count: int+++class HistoryReader:+ """A single-session view valid only inside its owner's read transaction."""++ def __init__(self, db: sqlite3.Connection, key: str, storage_path: Path) -> None:+ self._db = db+ self.key = key+ self.storage_path = storage_path++ @property+ def revision(self) -> tuple[str, int] | None:+ row = self._db.execute(+ """SELECT sessions.generation, coalesce(history_heads.revision, -1) FROM sessions+ LEFT JOIN history_heads ON history_heads.session_key=sessions.key WHERE sessions.key=?""",+ (self.key,),+ ).fetchone()+ return (str(row[0]), int(row[1])) if row is not None else None++ @property+ def turn_count(self) -> int:+ return int(self._db.execute(+ "SELECT coalesce(max(ordinal)+1, 0) FROM history_turns WHERE session_key=?",+ (self.key,),+ ).fetchone()[0])++ def turns(+ self, *, before: int | None = None, after: int = 0, reverse: bool = False,+ ) -> Iterator[HistoryTurn]:+ upper = self.turn_count if before is None else before+ order = "DESC" if reverse else "ASC"+ for row in self._db.execute(+ f"""SELECT ordinal, record_count, byte_count, user_count FROM history_turns+ WHERE session_key=? AND ordinal>=? AND ordinal<? ORDER BY ordinal {order}""",+ (self.key, after, upper),+ ):+ yield HistoryTurn(*row)++ def read_turn(self, ordinal: int) -> list[HistoryFact]:+ return [fact for _, fact in self.read_entries(ordinal)]++ def read_entries(+ self, ordinal: int, *, max_records: int | None = None, max_bytes: int | None = None,+ ) -> list[tuple[int, HistoryFact]]:+ clause = "session_key=? AND turn_ordinal=?"+ params = (self.key, ordinal)+ selected: list[int] | None = None+ if max_records is not None and max_bytes is not None:+ # Inspect sizes/positions, not unbounded JSON bodies, before decoding+ # a pathological single turn. Preserve the first input and useful tail.+ priorities: list[tuple[int, int]] = []+ for kinds, order in (((HistoryKind.INPUT,), "ASC"),+ ((HistoryKind.COMPLETION,), "DESC"),+ ((HistoryKind.ANSWER, HistoryKind.ANSWER_SEGMENT, HistoryKind.ANSWER_FRAGMENT), "DESC")):+ row = self._db.execute(+ f"SELECT position, length(CAST(record AS BLOB)) FROM history_entries WHERE {clause} "+ f"AND kind IN ({','.join('?' for _ in kinds)}) ORDER BY position {order} LIMIT 1",+ (*params, *kinds),+ ).fetchone()+ if row is not None:+ priorities.append((row[0], row[1]))+ candidates = self._db.execute(+ f"SELECT position, length(CAST(record AS BLOB)) FROM history_entries WHERE {clause} ORDER BY position DESC LIMIT ?",+ (*params, max_records),+ )+ positions: set[int] = set()+ used = 0+ for position, size in (*priorities, *candidates):+ if position not in positions and len(positions) < max_records and used + size <= max_bytes:+ positions.add(position)+ used += size+ selected = sorted(positions)+ if not selected:+ return []+ clause += f" AND position IN ({','.join('?' for _ in selected)})"+ return [(row[0], HistoryFact.decode(row[1])) for row in self._db.execute(+ f"SELECT position, record FROM history_entries WHERE {clause} ORDER BY position",+ (*params, *(selected or [])),+ )]++ def user_count_before(self, ordinal: int) -> int:+ return int(self._db.execute(+ "SELECT coalesce(sum(user_count), 0) FROM history_turns WHERE session_key=? AND ordinal<?",+ (self.key, ordinal),+ ).fetchone()[0])++ def navigation(self) -> Iterator[tuple[int, int, HistoryFact]]:+ """Read input/answer records only, never file diffs or tool result bodies."""+ kinds = (HistoryKind.INPUT, HistoryKind.ANSWER, HistoryKind.ANSWER_SEGMENT,+ HistoryKind.ANSWER_FRAGMENT, HistoryKind.COMPLETION)+ for row in self._db.execute(+ """SELECT turn_ordinal, position, record FROM history_entries+ WHERE session_key=? AND kind IN (?, ?, ?, ?, ?) ORDER BY turn_ordinal, position""",+ (self.key, *kinds),+ ):+ yield int(row[0]), int(row[1]), HistoryFact.decode(row[2])++ def all_facts(self) -> list[HistoryFact]:+ return [HistoryFact.decode(row[0]) for row in self._db.execute(+ "SELECT record FROM history_entries WHERE session_key=? ORDER BY turn_ordinal, position",+ (self.key,),+ )]diff --git a/nanobot/session/manager.py b/nanobot/session/manager.pyindex d708b136a..28ddd182f 100644--- a/nanobot/session/manager.py+++ b/nanobot/session/manager.py@@ -8,7 +8,7 @@ from copy import deepcopyfrom dataclasses import dataclass, fieldfrom datetime import datetimefrom pathlib import Path-from typing import TYPE_CHECKING, Any, Callable, TypedDict, cast+from typing import TYPE_CHECKING, Any, Callable, TypedDict, TypeVar, castfrom uuid import uuid4from nanobot.providers.base import ProviderConversationState@@ -16,6 +16,7 @@ from nanobot.runtime_context import (RUNTIME_CONTEXT_HISTORY_META,public_history_message,)+from nanobot.session.display_history import HistoryFact, HistoryReaderfrom nanobot.session.history_visibility import HIDDEN_HISTORY_META, is_hidden_history_messagefrom nanobot.session.location import SessionLocationfrom nanobot.session.model_selection import SESSION_MODEL_PRESET_METADATA_KEY@@ -33,6 +34,7 @@ from nanobot.utils.subagent_channel_display import scrub_subagent_announce_bodyif TYPE_CHECKING:from nanobot.session.state import SessionState+_T = TypeVar("_T")SESSION_CACHE_MAX_SIZE = 128_MESSAGE_TIME_PREFIX_RE = re.compile(r"^\[Message Time: [^\]]+\]\n?")_LOCAL_IMAGE_BREADCRUMB_RE = re.compile(r"^\[image: (?:/|~)[^\]]+\]\s*$")@@ -585,10 +587,27 @@ class SessionManager:target = fork_session(source, target_key, before_user_index)if target is not None:self._store.save(target)+ self._store.copy_history_prefix(source_key, target, before_user_index)return targetreturn self._store.run_write(fork)+ def project_history(self, key: str, project: Callable[[HistoryReader], _T]) -> _T:+ """Read an administrative display snapshot; runtime uses state.project_history."""+ return self._store.project_history(key, project)++ def append_history(self, key: str, facts: list[HistoryFact]) -> None:+ """Append offline fixture/import facts with the current admitted generation."""+ self._require_offline_mutation()++ def append() -> None:+ session = self._store.load(key, history=False)+ if session is None:+ session = self.get_or_create(key)+ self._store.save(session)+ self._store.append_history(key, facts, expected_generation=session.generation)+ self._store.run_write(append)+def read_session_file(self, key: str) -> dict[str, Any] | None:"""Read a session without populating the cache."""return cast(dict[str, Any] | None, self._store.read(key))diff --git a/nanobot/session/sqlite_store.py b/nanobot/session/sqlite_store.pyindex 3a489e77a..d19baacfe 100644--- a/nanobot/session/sqlite_store.py+++ b/nanobot/session/sqlite_store.py@@ -13,6 +13,7 @@ from pathlib import Pathfrom typing import Any, TypeVar, castfrom nanobot.providers.base import ProviderConversationState+from nanobot.session.display_history import HistoryFact, HistoryImport, HistoryKind, HistoryReaderfrom nanobot.session.history_visibility import is_hidden_history_messagefrom nanobot.session.manager import (Session,@@ -69,8 +70,36 @@ def _create_schema_v1(db: sqlite3.Connection) -> None:db.execute(statement)+def _create_schema_v2(db: sqlite3.Connection) -> None:+ for statement in (+ """CREATE TABLE history_heads (+ session_key TEXT PRIMARY KEY REFERENCES sessions(key) ON DELETE CASCADE,+ revision INTEGER NOT NULL DEFAULT 0,+ updated_at_ms INTEGER NOT NULL DEFAULT 0,+ preview TEXT NOT NULL DEFAULT '', user_preview TEXT NOT NULL DEFAULT ''+ )""",+ """CREATE TABLE history_turns (+ session_key TEXT NOT NULL REFERENCES sessions(key) ON DELETE CASCADE,+ ordinal INTEGER NOT NULL, record_count INTEGER NOT NULL,+ byte_count INTEGER NOT NULL, user_count INTEGER NOT NULL,+ completed INTEGER NOT NULL,+ PRIMARY KEY (session_key, ordinal)+ )""",+ """CREATE TABLE history_entries (+ session_key TEXT NOT NULL, turn_ordinal INTEGER NOT NULL,+ position INTEGER NOT NULL, kind TEXT NOT NULL, record TEXT NOT NULL,+ PRIMARY KEY (session_key, turn_ordinal, position),+ FOREIGN KEY (session_key, turn_ordinal)+ REFERENCES history_turns(session_key, ordinal) ON DELETE CASCADE+ )""",+ ):+ db.execute(statement)++# Entries upgrade version N to N+1. Preserve released entries and append upgrades.-_SCHEMA_MIGRATIONS: tuple[Callable[[sqlite3.Connection], None], ...] = (_create_schema_v1,)+_SCHEMA_MIGRATIONS: tuple[Callable[[sqlite3.Connection], None], ...] = (+ _create_schema_v1, _create_schema_v2,+)def _schema_version(db: sqlite3.Connection) -> int:@@ -391,6 +420,110 @@ class SqliteSessionStore:"updated_at": session.updated_at.isoformat(),"metadata": session.metadata, "messages": session.messages}+ def migrate_history(+ self, name: str, collect: Callable[[list[SessionInfo]], list[HistoryImport]],+ ) -> None:+ """Commit a channel-owned legacy import and its once-only marker together."""+ with self.transaction() as db:+ marker = db.execute("SELECT value FROM storage_meta WHERE key=?", (name,)).fetchone()+ if marker is not None:+ if marker[0] != "complete":+ raise RuntimeError(f"invalid history import marker: {name}")+ return+ for item in collect(self.list_sessions()):+ session = self.load(item.key, history=False)+ if session is None:+ session = Session(key=item.key)+ if item.created_at_ms:+ session.created_at = datetime.fromtimestamp(item.created_at_ms / 1000)+ if item.updated_at_ms:+ session.updated_at = datetime.fromtimestamp(item.updated_at_ms / 1000)+ self.save(session)+ revision = self.project_history(item.key, lambda reader: reader.revision)+ if revision is not None and revision[1] >= 0:+ raise SessionConflictError(f"history already exists before import: {item.key}")+ self.append_history(item.key, item.facts, expected_generation=session.generation)+ db.execute("UPDATE history_heads SET updated_at_ms=max(updated_at_ms, ?) WHERE session_key=?",+ (item.updated_at_ms, item.key))+ # Import ownership survives deletion: another namespace must not+ # mistake this instance-wide backup for a new orphan later.+ db.execute("INSERT INTO storage_meta (key, value) VALUES (?, 'complete')", (f"{name}:{item.key}",))+ db.execute("INSERT INTO storage_meta (key, value) VALUES (?, 'complete')", (name,))++ def project_history(self, key: str, project: Callable[[HistoryReader], _T]) -> _T:+ """Project durable display facts and canonical messages from one snapshot."""+ with self.transaction(write=False) as db:+ return project(HistoryReader(db, key, self.path))++ def append_history(+ self, key: str, facts: list[HistoryFact], *, expected_generation: str,+ ) -> None:+ encoded = [(fact, fact.encode()) for fact in facts]+ with self.transaction() as db:+ row = db.execute("SELECT generation FROM sessions WHERE key=?", (key,)).fetchone()+ if row is None or row[0] != expected_generation:+ raise SessionConflictError("history belongs to a deleted or replaced session")+ db.execute("INSERT OR IGNORE INTO history_heads (session_key) VALUES (?)", (key,))+ tail = db.execute(+ "SELECT ordinal, record_count, completed FROM history_turns WHERE session_key=? ORDER BY ordinal DESC LIMIT 1",+ (key,),+ ).fetchone()+ ordinal, position = (int(tail[0]), int(tail[1])) if tail is not None else (0, 0)+ completed = tail is not None and bool(tail[2])+ for fact, record in encoded:+ if completed:+ ordinal, position = ordinal + 1, 0+ if position == 0:+ db.execute(+ "INSERT INTO history_turns VALUES (?, ?, 0, 0, 0, 0)", (key, ordinal),+ )+ db.execute("INSERT INTO history_entries VALUES (?, ?, ?, ?, ?)", (+ key, ordinal, position, fact.kind.value, record,+ ))+ completed = fact.kind is HistoryKind.COMPLETION+ db.execute(+ """UPDATE history_turns SET record_count=record_count+1, byte_count=byte_count+?,+ user_count=user_count+?, completed=? WHERE session_key=? AND ordinal=?""",+ (len(record.encode("utf-8")), int(fact.kind is HistoryKind.INPUT),+ int(completed), key, ordinal),+ )+ position += 1+ if fact.kind in {HistoryKind.INPUT, HistoryKind.ANSWER, HistoryKind.ANSWER_SEGMENT}:+ preview = message_preview_text({"content": fact.content.get("text", "")})+ if preview:+ field = "user_preview" if fact.kind is HistoryKind.INPUT else "preview"+ db.execute(f"UPDATE history_heads SET {field}=? WHERE session_key=? AND {field}=''", (preview, key))+ db.execute("UPDATE history_heads SET updated_at_ms=max(updated_at_ms, ?) WHERE session_key=?",+ (fact.created_at_ms, key))+ if encoded:+ db.execute("UPDATE history_heads SET revision=revision+1 WHERE session_key=?", (key,))++ def copy_history_prefix(self, source_key: str, target: Session, before_input: int) -> bool:+ """Copy a display prefix alongside its canonical fork, in the caller's transaction."""+ def select(reader: HistoryReader) -> list[HistoryFact] | None:+ revision = reader.revision+ if revision is None or revision[1] < 0:+ return None+ copied: list[HistoryFact] = []+ inputs = 0+ for fact in reader.all_facts():+ if fact.kind is HistoryKind.FORK_BOUNDARY:+ continue+ if fact.kind is HistoryKind.INPUT:+ if inputs == before_input:+ return copied+ inputs += 1+ copied.append(fact)+ return copied if inputs == before_input else None++ facts = self.project_history(source_key, select)+ if facts is None:+ return False+ facts.append(HistoryFact(HistoryKind.FORK_BOUNDARY, created_at_ms=int(target.created_at.timestamp() * 1000)))+ self.append_history(target.key, facts, expected_generation=target.generation)+ return True++def delete(self, key: str) -> bool:with self.transaction() as db:return db.execute("DELETE FROM sessions WHERE key=?", (key,)).rowcount > 0@@ -433,10 +566,13 @@ class SqliteSessionStore:SELECT json_group_array(json_object('key', key, 'created_at', created_at, 'updated_at', updated_at,'title', title, 'preview', preview, 'visible_updated_at', visible_updated_at,- 'metadata', metadata+ 'metadata', metadata, 'history_updated_at_ms', history_updated_at_ms,+ 'history_preview', history_preview)) FROM (- SELECT key, created_at, updated_at, title, preview, visible_updated_at, metadata- FROM sessions ORDER BY updated_at DESC+ SELECT sessions.*, coalesce(history_heads.updated_at_ms, 0) AS history_updated_at_ms,+ coalesce(nullif(history_heads.user_preview, ''), history_heads.preview, '') AS history_preview+ FROM sessions LEFT JOIN history_heads ON history_heads.session_key=sessions.key+ ORDER BY sessions.updated_at DESC)""").fetchone()[0]# The SQL projection constructs this list and its dictionary fields.diff --git a/nanobot/session/state.py b/nanobot/session/state.pyindex d8e1219c5..b9d9a5744 100644--- a/nanobot/session/state.py+++ b/nanobot/session/state.py@@ -14,8 +14,15 @@ from datetime import datetimefrom typing import Any, TypeVar, castfrom nanobot.bus.events import InboundMessage+from nanobot.session.display_history import HistoryFact, HistoryImport, HistoryKind, HistoryReaderfrom nanobot.session.keys import is_dream_session-from nanobot.session.manager import Session, SessionIdentity, SessionPolicy, fork_session+from nanobot.session.manager import (+ Session,+ SessionIdentity,+ SessionInfo,+ SessionPolicy,+ fork_session,+)from nanobot.session.model_selection import SESSION_MODEL_PRESET_METADATA_KEYfrom nanobot.session.sqlite_store import SessionConflictError, SqliteSessionStorefrom nanobot.session.types import PARENT_SESSION_KEY, SessionTypes@@ -692,6 +699,31 @@ class SessionState:metadata = {**baseline.metadata, _CHECKPOINT_KEY: committed.metadata[_CHECKPOINT_KEY]}draft._baseline = replace(baseline, metadata=metadata, provider_state=committed.provider_state) # pyright: ignore[reportPrivateUsage]+ async def migrate_history(+ self, name: str, collect: Callable[[list[SessionInfo]], list[HistoryImport]],+ ) -> None:+ await self._execute(+ None, lambda: self._store.migrate_history(name, collect), operation_name="migrate_history",+ )++ async def project_history(+ self, key: str, project: Callable[[HistoryReader], _T],+ ) -> _T:+ return await self._execute(+ key, lambda: self._store.project_history(key, project),+ operation_name="project_history", write=False,+ )++ async def append_history(+ self, key: str, facts: Sequence[HistoryFact], *, expected_generation: str,+ ) -> None:+ records = list(facts)+ await self._execute(+ key,+ lambda: self._store.append_history(key, deepcopy(records), expected_generation=expected_generation),+ operation_name="append_history",+ )+async def record_delivery(self,key: str,@@ -933,6 +965,7 @@ class SessionState:async def fork(self, source_key: str, target_key: str, before_user_index: int,*, title: str | None = None,+ history_projection: Callable[[Session], list[HistoryFact]] | None = None,) -> Session | None:def commit() -> Session | None:def write() -> Session | None:@@ -945,6 +978,11 @@ class SessionState:if title:target.metadata["title"] = titleself._commit(target)+ copied = self._store.copy_history_prefix(source_key, target, before_user_index)+ if not copied and history_projection is not None:+ facts = history_projection(target)+ facts.append(HistoryFact(HistoryKind.FORK_BOUNDARY, created_at_ms=int(target.created_at.timestamp() * 1000)))+ self._store.append_history(target.key, facts, expected_generation=target.generation)return targettarget = self._store.run_write(write)diff --git a/nanobot/session/webui_turns.py b/nanobot/session/webui_turns.pyindex 76328bf18..71bf04266 100644--- a/nanobot/session/webui_turns.py+++ b/nanobot/session/webui_turns.py@@ -41,6 +41,7 @@ from nanobot.llm_usage.context import llm_usage_sourcefrom nanobot.providers.base import LLMProvider, LLMUsagefrom nanobot.providers.fallback_provider import FallbackModelObserver, FallbackModelSelectionfrom nanobot.runtime_context import public_history_message+from nanobot.session.display_history import HISTORY_GENERATION_METADATA_KEYfrom nanobot.session.goal_state import goal_state_ws_blobfrom nanobot.session.history_visibility import is_hidden_history_messagefrom nanobot.session.manager import Session, SessionManager@@ -531,6 +532,25 @@ class WebuiTurnRoutePolicy:return routed+ async def admit(self, route: TurnRoute, session_key: str, generation: str | None) -> TurnRoute:+ """Bind replies to the display destination's admitted lifetime."""+ if route.channel != "websocket":+ return route+ target_key = webui_session_key(route.chat_id)+ metadata = dict(route.metadata)+ if target_key == session_key and generation is not None:+ metadata.setdefault(HISTORY_GENERATION_METADATA_KEY, generation)+ return replace(route, metadata=metadata)+ revision = await self.sessions.state.project_history(target_key, lambda history: history.revision)+ if revision is None:+ raise SessionConflictError("display destination is no longer active")+ admitted = metadata.get(HISTORY_GENERATION_METADATA_KEY)+ # A distinct alias admitted by the WebSocket edge must still be current.+ if admitted not in (None, generation, revision[0]):+ raise SessionConflictError("display destination generation was replaced")+ metadata[HISTORY_GENERATION_METADATA_KEY] = revision[0]+ return replace(route, metadata=metadata)+def build_webui_fallback_model_observer(bus: MessageBus) -> FallbackModelObserver:"""Translate provider fallback choices into chat-scoped WebUI events."""@@ -630,6 +650,7 @@ class WebuiTurnCoordinator:content="",metadata=dict(ctx.metadata or {}),session_key_override=ctx.session_key,+ session_generation=ctx.session_generation,)@staticmethod@@ -657,8 +678,11 @@ class WebuiTurnCoordinator:returnpublic_metadata = _session_message_public_metadata(envelope)try:- append_session_message_input(+ assert event.context.session_generation is not None+ await append_session_message_input(session_key,+ sessions=self.sessions,+ expected_generation=event.context.session_generation,content=event.content,created_at_ms=envelope["created_at_ms"],session_message=public_metadata,diff --git a/nanobot/utils/__init__.py b/nanobot/utils/__init__.pyindex c2f8d13f7..6aa7a67ee 100644--- a/nanobot/utils/__init__.py+++ b/nanobot/utils/__init__.py@@ -30,7 +30,6 @@ class _LazyModuleAlias(ModuleType):_LEGACY_MODULE_ALIASES = {- "webui_thread_disk": "nanobot.webui.thread_disk","webui_transcript": "nanobot.webui.transcript","webui_turn_helpers": "nanobot.session.webui_turns",}diff --git a/nanobot/webui/forking.py b/nanobot/webui/forking.pyindex b3c2b0665..9d8935af1 100644--- a/nanobot/webui/forking.py+++ b/nanobot/webui/forking.py@@ -2,7 +2,6 @@from __future__ import annotations-import asyncioimport uuidfrom collections.abc import Mappingfrom typing import TYPE_CHECKING, Any, Protocol@@ -12,12 +11,7 @@ from loguru import loggerfrom nanobot.session.manager import SessionManagerfrom nanobot.session.webui_turns import clean_generated_titlefrom nanobot.webui.session_identity import is_valid_webui_chat_id, webui_session_key-from nanobot.webui.transcript import (- append_fork_marker,- delete_webui_transcript,- fork_transcript_before_user_index,- write_session_messages_as_transcript,-)+from nanobot.webui.transcript import session_display_factsif TYPE_CHECKING:from websockets.asyncio.server import ServerConnection@@ -54,28 +48,12 @@ async def create_webui_chat_fork(new_id = str(uuid.uuid4())source_key = webui_session_key(source_chat_id)target_key = webui_session_key(new_id)- try:- forked = await session_manager.state.fork(- source_key,- target_key,- before_user_index,- title=clean_generated_title(title),- )- if forked is None:- return None-- def write_transcript() -> None:- transcript_ok = fork_transcript_before_user_index(- source_key, target_key, before_user_index,- )- if not transcript_ok:- write_session_messages_as_transcript(target_key, forked.messages)- append_fork_marker(target_key)- await asyncio.to_thread(write_transcript)- except Exception:- await asyncio.to_thread(delete_webui_transcript, target_key)- await session_manager.state.delete(target_key)- raise+ forked = await session_manager.state.fork(+ source_key, target_key, before_user_index,+ title=clean_generated_title(title), history_projection=session_display_facts,+ )+ if forked is None:+ return Nonereturn new_id, target_keydiff --git a/nanobot/webui/gateway_services.py b/nanobot/webui/gateway_services.pyindex 24ec938bf..986868ee3 100644--- a/nanobot/webui/gateway_services.py+++ b/nanobot/webui/gateway_services.py@@ -112,7 +112,7 @@ def build_gateway_services(uploads = AttachmentHTTP(AttachmentStore(get_media_dir("websocket"), limits=ingress.attachments,))- transcripts = WebUITranscriptRecorder(log=logger)+ transcripts = WebUITranscriptRecorder(session_manager, log=logger)workspaces = WebUIWorkspaceController(session_manager=session_manager,default_workspace=workspace_path,diff --git a/nanobot/webui/history.py b/nanobot/webui/history.pynew file mode 100644index 000000000..5df8bff4b--- /dev/null+++ b/nanobot/webui/history.py@@ -0,0 +1,213 @@+"""WebUI projection of durable session facts and strict legacy transcript import."""++from __future__ import annotations++import json+import re+import sqlite3+from collections.abc import Mapping, Sequence+from contextlib import closing+from pathlib import Path+from typing import Any, cast++from nanobot.config.paths import get_webui_dir+from nanobot.session.display_history import HistoryFact, HistoryImport, HistoryKind, HistoryReader+from nanobot.session.manager import SessionInfo, SessionManager+from nanobot.webui.session_identity import is_valid_webui_chat_id, webui_chat_id, webui_session_key++HISTORY_IMPORT_MARKER = "webui_history_import_v1"+_SEGMENT_NAME = re.compile(r"^\d{6}\.jsonl$")+_EVENTS = {+ "user": HistoryKind.INPUT,+ "message": HistoryKind.ANSWER,+ "stream_end": HistoryKind.ANSWER_SEGMENT,+ "reasoning_end": HistoryKind.REASONING_SEGMENT,+ "file_edit": HistoryKind.FILE_ACTIVITY,+ "turn_end": HistoryKind.COMPLETION,+ "fork_marker": HistoryKind.FORK_BOUNDARY,+ "context_compaction": HistoryKind.COMPACTION,+ "delta": HistoryKind.ANSWER_FRAGMENT,+ "reasoning_delta": HistoryKind.REASONING_FRAGMENT,+}+_MESSAGE_KINDS = {+ "reasoning": HistoryKind.REASONING,+ "progress": HistoryKind.PROGRESS,+ "tool_hint": HistoryKind.TOOL_ACTIVITY,+}+_CONTENT_FIELDS = frozenset({+ "text", "media_paths", "media", "media_urls", "reply_to", "agent_ui", "cli_apps", "mcp_presets", "session_mentions",+ "session_message", "tool_events", "edits", "source", "response_sources", "resuming",+ "merge_next", "latency_ms", "usage", "round_usages", "context_window_tokens", "goal_state",+ "outcome", "failure_kind", "failure_error_kind", "failure_attempts", "failure_message",+ "transcript_incomplete", "stream_id", "compaction_id", "phase",+})+++def fact_from_record(record: Mapping[str, Any]) -> HistoryFact:+ """Parse the channel boundary once; core storage never sees event/chat_id keys."""+ event = record.get("event")+ if event is None and record.get("role") in {"user", "assistant"}:+ event = "user" if record["role"] == "user" else "message"+ record = {**record, "text": record.get("text", record.get("content", ""))}+ if event not in _EVENTS:+ raise ValueError(f"unsupported legacy history event: {event!r}")+ kind = _EVENTS[event]+ if event == "message":+ message_kind = record.get("kind")+ if message_kind is not None and message_kind not in _MESSAGE_KINDS:+ raise ValueError(f"unsupported history message kind: {message_kind!r}")+ if isinstance(message_kind, str):+ kind = _MESSAGE_KINDS[message_kind]+ created = record.get("created_at_ms", 0)+ if isinstance(created, bool) or not isinstance(created, int | float) or not 0 <= created < 10**16:+ raise ValueError("invalid history timestamp")+ sequence = record.get("turn_seq")+ if sequence is not None and (isinstance(sequence, bool) or not isinstance(sequence, int) or sequence < 0):+ raise ValueError("invalid history sequence")+ turn_id, phase = record.get("turn_id"), record.get("turn_phase")+ if turn_id is not None and (not isinstance(turn_id, str) or len(turn_id) > 128):+ raise ValueError("invalid history turn identity")+ if phase is not None and not isinstance(phase, str):+ raise ValueError("invalid history phase")+ fact = HistoryFact(kind, {key: value for key, value in record.items() if key in _CONTENT_FIELDS},+ turn_id, phase, sequence, int(created))+ return fact+++def record_from_fact(fact: HistoryFact, key: str) -> dict[str, Any]:+ if fact.kind in _MESSAGE_KINDS.values():+ event = "message"+ message_kind = next(name for name, kind in _MESSAGE_KINDS.items() if kind is fact.kind)+ else:+ event = next(name for name, kind in _EVENTS.items() if kind is fact.kind)+ message_kind = None+ record = {"event": event, "chat_id": webui_chat_id(key), **fact.content,+ "created_at_ms": fact.created_at_ms}+ if message_kind:+ record["kind"] = message_kind+ if fact.turn_id is not None:+ record["turn_id"] = fact.turn_id+ if fact.phase is not None:+ record["turn_phase"] = fact.phase+ if fact.sequence is not None:+ record["turn_seq"] = fact.sequence+ return record+++def turn_records(+ history: HistoryReader, ordinal: int, *, max_records: int | None = None, max_bytes: int | None = None,+) -> list[dict[str, Any]]:+ return [{**record_from_fact(fact, history.key),+ "_webui_replay_identity": f"turn:{ordinal}:record:{position}"}+ for position, fact in history.read_entries(ordinal, max_records=max_records, max_bytes=max_bytes)]+++def _read_legacy_file(path: Path) -> list[dict[str, Any]]:+ if path.is_symlink() or not path.is_file():+ raise ValueError(f"legacy history must be a regular, non-symlink file: {path}")+ before = path.stat()+ records: list[dict[str, Any]] = []+ with path.open(encoding="utf-8") as handle:+ for line_number, line in enumerate(handle, 1):+ if not line.strip():+ continue+ try:+ value: object = json.loads(line)+ if not isinstance(value, dict):+ raise ValueError("record is not an object")+ record = cast(dict[str, Any], value)+ fact_from_record(record).encode()+ except (ValueError, TypeError) as exc:+ raise ValueError(f"cannot import history {path}:{line_number}: {exc}") from exc+ records.append(record)+ after = path.stat()+ if (before.st_ino, before.st_size, before.st_mtime_ns) != (after.st_ino, after.st_size, after.st_mtime_ns):+ raise ValueError(f"legacy history changed during import: {path}")+ return records+++def collect_legacy_history(+ sessions: Sequence[SessionInfo], *, directory: Path | None = None,+ namespace: Path | None = None,+) -> list[HistoryImport]:+ """Resolve recorded identities; a lossy filename alone cannot identify an orphan."""+ from nanobot.webui.transcript import compact_legacy_records++ directory = directory if directory is not None else get_webui_dir()+ if directory.is_symlink():+ raise ValueError(f"legacy history directory must not be a symlink: {directory}")+ if not directory.exists():+ return []+ groups: dict[str, list[Path]] = {}+ for path in sorted(directory.iterdir()):+ if path.name.endswith(".segments"):+ if path.is_symlink() or not path.is_dir():+ raise ValueError(f"invalid legacy history segments directory: {path}")+ groups.setdefault(path.name[:-len(".segments")], []).extend(+ item for item in sorted(path.iterdir()) if _SEGMENT_NAME.fullmatch(item.name)+ )+ elif path.suffix == ".jsonl":+ groups.setdefault(path.stem, [])+ for stem, paths in groups.items():+ active = directory / f"{stem}.jsonl"+ if active.exists() or active.is_symlink():+ paths.append(active)+ keys_by_stem: dict[str, set[str]] = {}+ for session in sessions:+ keys_by_stem.setdefault(SessionManager.safe_key(session["key"]), set()).add(session["key"])+ foreign_owners: dict[str, list[Path]] = {}+ if namespace is not None and groups:+ for database in namespace.parent.glob("*/sessions.sqlite3"):+ if database.parent == namespace:+ continue+ if database.is_symlink() or database.parent.is_symlink():+ raise ValueError(f"invalid session namespace for history migration: {database}")+ with closing(sqlite3.connect(database.as_uri() + "?mode=ro", uri=True)) as db:+ owned = {str(row[0]) for row in db.execute("SELECT key FROM sessions")}+ prefix = f"{HISTORY_IMPORT_MARKER}:"+ owned.update(str(row[0])[len(prefix):] for row in db.execute(+ "SELECT key FROM storage_meta WHERE key LIKE ? AND value='complete'",+ (prefix + "%",),+ ))+ for key in owned:+ foreign_owners.setdefault(key, []).append(database)+ imports: list[HistoryImport] = []+ for stem, paths in groups.items():+ records = [record for path in paths for record in _read_legacy_file(path)]+ if not records:+ continue+ recorded_keys: set[str] = set()+ for record in records:+ chat_id = record.get("chat_id")+ if chat_id is not None:+ if not is_valid_webui_chat_id(chat_id):+ raise ValueError(f"invalid recorded chat identity in legacy history: {stem}")+ recorded_keys.add(webui_session_key(chat_id))+ candidates = recorded_keys or keys_by_stem.get(stem, set())+ if len(candidates) != 1:+ raise ValueError(f"ambiguous legacy history identity: {stem}; expected one recorded chat or canonical session")+ key = next(iter(candidates))+ if SessionManager.safe_key(key) != stem:+ raise ValueError(f"legacy history filename conflicts with its recorded identity: {stem}")+ if key in foreign_owners:+ if key not in keys_by_stem.get(stem, set()) and len(foreign_owners[key]) == 1:+ # This instance-wide backup belongs to another workspace. Its+ # owner will import it when that namespace starts.+ continue+ raise ValueError(+ f"cannot select a SQLite owner for legacy history {key}: also present in "+ f"{foreign_owners[key]}; resolve the duplicate explicitly before restarting"+ )+ facts = [fact_from_record(record) for record in compact_legacy_records(records)]+ times = [fact.created_at_ms for fact in facts if fact.created_at_ms]+ file_times = [int(path.stat().st_mtime * 1000) for path in paths]+ imports.append(HistoryImport(key, facts, min(times or file_times), max(times or file_times)))+ return imports+++async def initialize_history(sessions: SessionManager) -> None:+ """Finish the once-only import before recovery or transport admission."""+ await sessions.state.migrate_history(+ HISTORY_IMPORT_MARKER,+ lambda rows: collect_legacy_history(rows, namespace=sessions.sessions_dir),+ )diff --git a/nanobot/webui/inbound_commands.py b/nanobot/webui/inbound_commands.pyindex bf0cb0b59..aad66c1f6 100644--- a/nanobot/webui/inbound_commands.py+++ b/nanobot/webui/inbound_commands.py@@ -28,6 +28,7 @@ from nanobot.security.workspace_access import (WORKSPACE_SCOPE_METADATA_KEY,WorkspaceScopeError,)+from nanobot.session.display_history import HISTORY_GENERATION_METADATA_KEYfrom nanobot.session.keys import session_key_for_channelfrom nanobot.session.webui_turns import (clear_websocket_turn_if_current,@@ -752,10 +753,12 @@ class WebUICommandRouter:execution_session_key, {},))+ if session_generation is not None:+ metadata[HISTORY_GENERATION_METADATA_KEY] = session_generationif is_webui and not restart_control and (temporary_policy is None or temporary_policy.persist_transcript):- self._transcripts.append_user_message(+ await self._transcripts.append_user_message(chat_id,content,metadata=metadata,diff --git a/nanobot/webui/recovery.py b/nanobot/webui/recovery.pyindex 962e6b667..dca69e7e1 100644--- a/nanobot/webui/recovery.py+++ b/nanobot/webui/recovery.py@@ -86,6 +86,9 @@ class RecoveryCoordinator:async def scan(self) -> None:"""Recover every interrupted WebUI session once at gateway startup."""+ from nanobot.webui.history import initialize_history++ await initialize_history(self.sessions)for key in await self._recovery_candidates():metadata_payload = await self.sessions.state.read_metadata(key)raw_metadata = metadata_payload.get("metadata") if metadata_payload else None@@ -93,7 +96,7 @@ class RecoveryCoordinator:route = self._websocket_route_for(key, metadata)if route is None:continue- unfinished = self._has_unfinished_webui_transcript(key)+ unfinished = await self._has_unfinished_webui_transcript(key)if not self._needs_recovery(metadata) and not unfinished:continuesession = await self.sessions.state.get(key)@@ -115,27 +118,8 @@ class RecoveryCoordinator:await self._publish(route[1], failed)async def _recovery_candidates(self) -> list[str]:- """Discover canonical and transcript-only WebUI sessions cheaply."""- candidates = dict.fromkeys(- key- for item in await self.sessions.state.list_sessions()- if isinstance((key := item.get("key")), str)- )- try:- # Imported lazily because the sidebar index also projects recovery- # metadata. The index is the owner of transcript-only discovery;- # duplicating its filename and migration rules here would drift.- from nanobot.webui.session_list_index import list_webui_sessions-- for item in await asyncio.to_thread(list_webui_sessions, self.sessions):- key = item.get("key")- if isinstance(key, str):- candidates.setdefault(key, None)- except Exception:- # Canonical checkpoint recovery remains available even if the- # optional display-history index is corrupt or unavailable.- logger.exception("failed to discover transcript-only WebUI sessions")- return list(candidates)+ """Legacy display-only sessions have canonical headers after import."""+ return [item["key"] for item in await self.sessions.state.list_sessions()]@staticmethoddef _needs_recovery(metadata: Mapping[str, Any]) -> bool:@@ -293,7 +277,7 @@ class RecoveryCoordinator:)await self.sessions.state.restore_interruption(session)await self._publish(chat_id, next_state)- elif self._has_unfinished_webui_transcript(session.key):+ elif await self._has_unfinished_webui_transcript(session.key):# A normal last-client shutdown can materialize the checkpoint# before the process exits. In that path there is no pending# marker left to classify, but the append-only transcript still@@ -505,23 +489,12 @@ class RecoveryCoordinator:def _session_key(self, chat_id: str) -> str:return UNIFIED_SESSION_KEY if self.unified_session else webui_session_key(chat_id)- @staticmethod- def _has_unfinished_webui_transcript(session_key: str) -> bool:- """Detect a stale WebUI activity tail after an unclean gateway stop.-- The transcript is intentionally consulted only as a last-resort signal:- a durable pending turn or runtime checkpoint always takes precedence.- This keeps browser disconnects harmless while preventing a materialized- partial turn from being presented as active forever after a restart.- """- try:- from nanobot.webui.transcript import has_unfinished_transcript_tail-- return has_unfinished_transcript_tail(session_key)- except (OSError, ValueError, TypeError):- # Recovery must fail closed if the optional display transcript is- # corrupt or unavailable; the normal checkpoint path still applies.- return False+ async def _has_unfinished_webui_transcript(self, session_key: str) -> bool:+ from nanobot.webui.transcript import has_unfinished_transcript_tail++ return await self.sessions.state.project_history(+ session_key, lambda history: has_unfinished_transcript_tail(session_key, history=history),+ )@staticmethoddef _has_saved_continuation_context(session: Session) -> bool:diff --git a/nanobot/webui/session_list_index.py b/nanobot/webui/session_list_index.pyindex 5b93d4d37..c9645d600 100644--- a/nanobot/webui/session_list_index.py+++ b/nanobot/webui/session_list_index.py@@ -1,47 +1,16 @@-"""SQLite sidebar queries and a rebuildable display-transcript cache.--The core ``SessionManager`` owns model context while the WebUI transcript owns-durable display history. The sidebar discovers both without reconstructing one-store from the other, so core session writes stay independent from UI state.-"""+"""Sidebar projection from session headers and SQLite display summaries."""from __future__ import annotationsimport json-import os-import re-import secrets-import stat-from dataclasses import dataclass, fieldfrom datetime import datetime-from pathlib import Pathfrom typing import Any, cast-from loguru import logger--from nanobot.config.paths import get_webui_dirfrom nanobot.security.workspace_access import WORKSPACE_SCOPE_METADATA_KEY-from nanobot.session.manager import (- _SESSION_LIST_PREVIEW_MAX_CHARS, # pyright: ignore[reportPrivateUsage]- _SESSION_LIST_PREVIEW_MAX_RECORDS, # pyright: ignore[reportPrivateUsage]- SessionManager,- message_preview_text, # pyright: ignore[reportPrivateUsage]- )+from nanobot.session.manager import SessionManagerfrom nanobot.session.model_selection import model_preset_from_metadatafrom nanobot.session.recovery import recovery_state_from_metadata-from nanobot.webui.session_identity import (- WEBUI_SESSION_STORAGE_PREFIX,- is_webui_session_key,- webui_chat_id,- webui_session_key,-)-_INDEX_VERSION = 8-_INDEX_FILENAME = ".webui_session_index.json"-_MODEL_PRESET_FIELD = "model_preset"-_ROW_SOURCE_FIELD = "_source"-_SESSION_SOURCE = "session"-_TRANSCRIPT_SOURCE = "webui_transcript"_WORKSPACE_SCOPE_PRESENT_FIELD = "_workspace_scope_present"_WORKSPACE_SCOPE_VALUE_FIELD = "_workspace_scope_value"WEBUI_SESSION_INDEX_INTERNAL_FIELDS = frozenset(@@ -49,205 +18,30 @@ WEBUI_SESSION_INDEX_INTERNAL_FIELDS = frozenset()_INDEXED_WORKSPACE_SCOPE_KEYS = ("project_path", "path", "access_mode")_MAX_INDEXED_WORKSPACE_SCOPE_BYTES = 4096-_WEBUI_ACTIVITY_MTIME_NS = "webui_activity_mtime_ns"-_WEBUI_ACTIVITY_SIZE = "webui_activity_size"-_WEBUI_ACTIVITY_FILES = "webui_activity_files"-_VISIBLE_TRANSCRIPT_ROLES = {"user", "assistant"}-_WEBUI_SESSION_STEM_PREFIX = SessionManager.safe_key(WEBUI_SESSION_STORAGE_PREFIX)-_WEBUI_CHAT_ID_RE = re.compile(r"^[A-Za-z0-9_:-]{1,64}$")-_TRANSCRIPT_SEGMENTS_SUFFIX = ".segments"-_TRANSCRIPT_NON_ANSWER_KINDS = {"progress", "reasoning", "tool_hint"}---@dataclass-class _WebuiFiles:- signature: dict[str, int] = field(default_factory=lambda: {- _WEBUI_ACTIVITY_MTIME_NS: 0, _WEBUI_ACTIVITY_SIZE: 0, _WEBUI_ACTIVITY_FILES: 0,- })- records: list[tuple[Path, float]] = field(default_factory=list)def list_webui_sessions(session_manager: SessionManager,stored_sessions: list[dict[str, Any]] | None = None,) -> list[dict[str, Any]]:- """Return session rows for the WebUI sidebar, backed by a rebuildable cache."""- webui_dir = get_webui_dir()- rows, changed = _reconcile_index(session_manager, stored_sessions, webui_dir)- if changed:- try:- _write_index_rows(session_manager.sessions_dir, [row for row in rows if row.get(_ROW_SOURCE_FIELD) == _TRANSCRIPT_SOURCE])- except Exception as e:- logger.debug("Failed to write WebUI transcript list cache: {}", e)- sessions = [_public_row(session_manager.sessions_dir, webui_dir, row) for row in rows]- return sorted(sessions, key=lambda row: row.get("updated_at", ""), reverse=True)---def _reconcile_index(- session_manager: SessionManager,- stored_sessions: list[dict[str, Any]] | None,- webui_dir: Path,-) -> tuple[list[dict[str, Any]], bool]:- files_by_stem = _read_webui_files(webui_dir)- activity_by_stem: dict[str, dict[str, int]] = {}- for stem, files in files_by_stem.items():- identity = os.path.normcase(stem)- previous = activity_by_stem.get(identity)- activity_by_stem[identity] = files.signature if previous is None else {- _WEBUI_ACTIVITY_MTIME_NS: max(previous[_WEBUI_ACTIVITY_MTIME_NS], files.signature[_WEBUI_ACTIVITY_MTIME_NS]),- _WEBUI_ACTIVITY_SIZE: previous[_WEBUI_ACTIVITY_SIZE] + files.signature[_WEBUI_ACTIVITY_SIZE],- _WEBUI_ACTIVITY_FILES: previous[_WEBUI_ACTIVITY_FILES] + files.signature[_WEBUI_ACTIVITY_FILES],- }+ """Read one database snapshot; no display files or sidebar sidecar cache."""stored = session_manager.list_session_metadata() if stored_sessions is None else stored_sessions- session_keys = {row["key"] for row in stored}- session_stems = {os.path.normcase(SessionManager.safe_key(key)) for key in session_keys if is_webui_session_key(key)}rows: list[dict[str, Any]] = []for row in stored:metadata = row["metadata"]if not session_manager.types.public_history(metadata):continue- signature = activity_by_stem.get(os.path.normcase(SessionManager.safe_key(row["key"])))- activity = _webui_activity_updated_at(signature) if signature is not None else None+ millis = row["history_updated_at_ms"]+ activity = datetime.fromtimestamp(millis / 1000).isoformat() if millis else Nonerows.append({"key": row["key"], "created_at": row["created_at"],"updated_at": _visible_activity_updated_at(row["updated_at"], row["visible_updated_at"], activity),- "title": row["title"], "preview": row["preview"],- _MODEL_PRESET_FIELD: model_preset_from_metadata(metadata),+ "title": row["title"], "preview": row["preview"] or row["history_preview"],+ "model_preset": model_preset_from_metadata(metadata),"recovery_state": recovery_state_from_metadata(metadata),- **_indexed_workspace_scope_fields(metadata),- _ROW_SOURCE_FIELD: _SESSION_SOURCE, "file": "sessions.sqlite3",+ **_indexed_workspace_scope_fields(metadata), "path": row["path"],})-- transcripts = [- (stem, files) for stem, files in sorted(files_by_stem.items())- if stem.startswith(_WEBUI_SESSION_STEM_PREFIX) and files.records- and os.path.normcase(stem) not in session_stems- ]- if not transcripts:- return rows, False- # Only transcript-only sessions use the file cache; SQLite rows are fresh.- existing_rows = _read_index_rows(session_manager.sessions_dir)- existing_by_source = {- (row.get(_ROW_SOURCE_FIELD), row.get("file")): row- for row in existing_rows or []- if isinstance(row.get(_ROW_SOURCE_FIELD), str)- and isinstance(row.get("file"), str)- }- changed = existing_rows is None- expected_sources: set[tuple[str, str]] = set()- for stem, files in transcripts:- files.signature = activity_by_stem[os.path.normcase(stem)]- identity = (_TRANSCRIPT_SOURCE, stem)- row = existing_by_source.get(identity)- cached_key = row.get("key") if row is not None else None- key = (- cached_key- if isinstance(cached_key, str) and _valid_transcript_session_key(cached_key, stem)- else None- )- if key is not None and row is not None and _indexed_transcript_row_matches(- row,- key,- files.signature,- ):- rows.append(row)- expected_sources.add(identity)- continue-- changed = True- scanned = _scan_transcript_row(key, stem, files, webui_dir)- scanned_key = scanned.get("key") if scanned is not None else None- if scanned is not None and scanned_key not in session_keys:- rows.append(scanned)- expected_sources.add(identity)-- if set(existing_by_source) != expected_sources:- changed = True- transcript_rows = [row for row in rows if row.get(_ROW_SOURCE_FIELD) == _TRANSCRIPT_SOURCE]- if existing_rows is not None and transcript_rows != existing_rows:- changed = True- return rows, changed---def _index_path(sessions_dir: Path) -> Path:- return sessions_dir / _INDEX_FILENAME---def _read_index_rows(sessions_dir: Path) -> list[dict[str, Any]] | None:- path = _index_path(sessions_dir)- if not path.is_file():- return None- try:- data = json.loads(path.read_text(encoding="utf-8"))- except (OSError, json.JSONDecodeError):- return None- if not isinstance(data, dict):- return None- index_data = cast(dict[str, Any], data)- if index_data.get("version") != _INDEX_VERSION:- return None- rows = index_data.get("sessions")- if not isinstance(rows, list):- return None- index_rows = cast(list[Any], rows)- if not all(isinstance(row, dict) for row in index_rows):- return None- return [cast(dict[str, Any], row) for row in index_rows]---def _write_index_rows(sessions_dir: Path, rows: list[dict[str, Any]]) -> None:- path = _index_path(sessions_dir)- tmp_path = path.with_name(f"{path.name}.{secrets.token_hex(8)}.tmp")- data = {"version": _INDEX_VERSION, "sessions": rows}- try:- with open(tmp_path, "x", encoding="utf-8") as file:- file.write(json.dumps(data, ensure_ascii=False) + "\n")- os.replace(tmp_path, path)- finally:- tmp_path.unlink(missing_ok=True)----def _indexed_transcript_row_matches(- row: dict[str, Any],- session_key: str,- signature: dict[str, int],-) -> bool:- if not all(isinstance(row.get(key), str) for key in ("key", "created_at", "updated_at")):- return False- if row.get(_ROW_SOURCE_FIELD) != _TRANSCRIPT_SOURCE:- return False- if row.get("key") != session_key or row.get("file") != SessionManager.safe_key(session_key):- return False- if not isinstance(row.get("title", ""), str) or not isinstance(row.get("preview", ""), str):- return False- if not isinstance(row.get(_WORKSPACE_SCOPE_PRESENT_FIELD), bool):- return False- return (- row.get(_WEBUI_ACTIVITY_MTIME_NS) == signature[_WEBUI_ACTIVITY_MTIME_NS]- and row.get(_WEBUI_ACTIVITY_SIZE) == signature[_WEBUI_ACTIVITY_SIZE]- and row.get(_WEBUI_ACTIVITY_FILES) == signature[_WEBUI_ACTIVITY_FILES]- )---def _public_row(sessions_dir: Path, webui_dir: Path, row: dict[str, Any]) -> dict[str, Any]:- file = str(row.get("file", ""))- if row.get(_ROW_SOURCE_FIELD) == _TRANSCRIPT_SOURCE:- path = webui_dir / f"{file}.jsonl"- else:- path = sessions_dir / file- return {- "key": row.get("key"),- "created_at": row.get("created_at"),- "updated_at": row.get("updated_at"),- "title": row.get("title", ""),- "preview": row.get("preview", ""),- _MODEL_PRESET_FIELD: row.get(_MODEL_PRESET_FIELD),- "recovery_state": row.get("recovery_state"),- _WORKSPACE_SCOPE_PRESENT_FIELD: row.get(_WORKSPACE_SCOPE_PRESENT_FIELD, False),- _WORKSPACE_SCOPE_VALUE_FIELD: row.get(_WORKSPACE_SCOPE_VALUE_FIELD),- "path": str(path),- }+ return sorted(rows, key=lambda row: row.get("updated_at", ""), reverse=True)def indexed_workspace_scope(row: dict[str, Any]) -> tuple[bool, object]:@@ -295,67 +89,6 @@ def _indexed_workspace_scope_fields(metadata: object) -> dict[str, object]:}--def _read_webui_files(webui_dir: Path) -> dict[str, _WebuiFiles]:- """Discover present display files once per request, including all segment activity."""- files_by_stem: dict[str, _WebuiFiles] = {}-- def add_file(stem: str, entry: os.DirEntry[str]) -> None:- try:- info = entry.stat(follow_symlinks=False)- except OSError:- return- if not stat.S_ISREG(info.st_mode):- return- files = files_by_stem.setdefault(stem, _WebuiFiles())- signature = files.signature- signature[_WEBUI_ACTIVITY_MTIME_NS] = max(signature[_WEBUI_ACTIVITY_MTIME_NS], info.st_mtime_ns)- signature[_WEBUI_ACTIVITY_SIZE] += info.st_size- signature[_WEBUI_ACTIVITY_FILES] += 1- if os.path.normcase(entry.name).endswith(".jsonl"):- files.records.append((Path(entry.path), info.st_mtime))-- try:- with os.scandir(webui_dir) as entries:- for entry in entries:- name = os.path.normcase(entry.name)- if name.endswith(_TRANSCRIPT_SEGMENTS_SUFFIX) and entry.is_dir(follow_symlinks=False):- stem = entry.name[:-len(_TRANSCRIPT_SEGMENTS_SUFFIX)]- try:- with os.scandir(entry.path) as segments:- for segment in segments:- add_file(stem, segment)- except OSError:- pass- elif name.endswith((".jsonl", ".json")):- add_file(entry.name.rsplit(".", 1)[0], entry)- except OSError:- pass- return files_by_stem---def _transcript_record(line: str) -> dict[str, Any] | None:- try:- value: object = json.loads(line)- except json.JSONDecodeError:- return None- return cast(dict[str, Any], value) if isinstance(value, dict) else None---def _valid_transcript_session_key(key: str, stem: str) -> bool:- chat_id = webui_chat_id(key)- if chat_id is None:- return False- return _WEBUI_CHAT_ID_RE.fullmatch(chat_id) is not None and SessionManager.safe_key(key) == stem---def _webui_activity_updated_at(signature: dict[str, int]) -> str | None:- mtime_ns = signature.get(_WEBUI_ACTIVITY_MTIME_NS, 0)- if mtime_ns <= 0:- return None- return datetime.fromtimestamp(mtime_ns / 1_000_000_000).isoformat()--def _timestamp(value: str | None) -> float:if not value:return 0.0@@ -371,128 +104,9 @@ def _latest_updated_at(stored: str | None, activity: str | None) -> str | None:return stored-def _visible_activity_updated_at(stored: str | None,visible_message_at: str | None,webui_activity: str | None,) -> str | None:return _latest_updated_at(visible_message_at, webui_activity) or stored----def _transcript_preview(record: dict[str, Any]) -> tuple[str, str]:- text = record.get("text")- if not isinstance(text, str) or not text.strip():- return "", ""- preview = message_preview_text({"content": text})- if not preview:- return "", ""- event = record.get("event")- if event == "user" or record.get("role") == "user":- return preview, ""- if (- event == "message"- and record.get("kind") not in _TRANSCRIPT_NON_ANSWER_KINDS- ) or record.get("role") == "assistant":- return "", preview- return "", ""---def _transcript_created_at(record: dict[str, Any]) -> str | None:- value = record.get("created_at_ms")- if (- not isinstance(value, int | float)- or isinstance(value, bool)- or value < 0- ):- return None- try:- return datetime.fromtimestamp(value / 1000).isoformat()- except (OSError, OverflowError, ValueError):- return None---def _scan_transcript_row(- session_key: str | None,- stem: str,- files: _WebuiFiles,- webui_dir: Path,-) -> dict[str, Any] | None:- signature = files.signature- activity_updated_at = _webui_activity_updated_at(signature)- if activity_updated_at is None:- return None-- preview = ""- fallback_preview = ""- created_at: str | None = None- saw_record = False- scanned_records = 0- scanned_chars = 0- paths = [path for path, _ in sorted(files.records, key=lambda item: (item[0].parent == webui_dir, item[0]))]- for path in paths:- try:- with open(path, encoding="utf-8") as handle:- for line in handle:- if not line.strip():- continue- scanned_records += 1- scanned_chars += len(line)- record = _transcript_record(line)- if record is not None:- saw_record = True- chat_id = record.get("chat_id")- if isinstance(chat_id, str) and chat_id.strip():- candidate = webui_session_key(chat_id.strip())- if _valid_transcript_session_key(candidate, stem):- session_key = candidate- if created_at is None:- created_at = _transcript_created_at(record)- user_preview, assistant_preview = _transcript_preview(record)- if user_preview:- preview = user_preview- break- if not fallback_preview and assistant_preview:- fallback_preview = assistant_preview- if (- scanned_records >= _SESSION_LIST_PREVIEW_MAX_RECORDS- or scanned_chars >= _SESSION_LIST_PREVIEW_MAX_CHARS- ):- break- except OSError:- continue- if preview or (- scanned_records >= _SESSION_LIST_PREVIEW_MAX_RECORDS- or scanned_chars >= _SESSION_LIST_PREVIEW_MAX_CHARS- ):- break- if not saw_record:- return None- if session_key is None:- fallback = webui_session_key(stem.removeprefix(_WEBUI_SESSION_STEM_PREFIX))- if not _valid_transcript_session_key(fallback, stem):- return None- session_key = fallback-- if created_at is None:- try:- earliest_mtime = min(mtime for _, mtime in files.records)- created_at = datetime.fromtimestamp(earliest_mtime).isoformat()- except (OSError, OverflowError, ValueError):- created_at = activity_updated_at- return {- "key": session_key,- "created_at": created_at,- "updated_at": activity_updated_at,- "title": "",- "preview": preview or fallback_preview,- _MODEL_PRESET_FIELD: None,- "recovery_state": None,- **_indexed_workspace_scope_fields({}),- _ROW_SOURCE_FIELD: _TRANSCRIPT_SOURCE,- "file": stem,- "mtime_ns": signature[_WEBUI_ACTIVITY_MTIME_NS],- "size": signature[_WEBUI_ACTIVITY_SIZE],- **signature,- }diff --git a/nanobot/webui/thread_disk.py b/nanobot/webui/thread_disk.pydeleted file mode 100644index 03438f8af..000000000--- a/nanobot/webui/thread_disk.py+++ /dev/null@@ -1,31 +0,0 @@-"""Legacy WebUI JSON snapshot path helpers (JSON file); transcripts use transcript."""--from __future__ import annotations--from pathlib import Path--from loguru import logger--from nanobot.config.paths import get_webui_dir-from nanobot.session.manager import SessionManager-from nanobot.webui.transcript import delete_webui_transcript---def webui_thread_file_path(session_key: str) -> Path:- stem = SessionManager.safe_key(session_key)- return get_webui_dir() / f"{stem}.json"---def delete_webui_thread(session_key: str) -> bool:- """Remove legacy WebUI JSON snapshot and append-only transcript for *session_key*."""- removed = False- path = webui_thread_file_path(session_key)- if path.is_file():- try:- path.unlink()- removed = True- except OSError as e:- logger.warning("Failed to delete webui thread file {}: {}", path, e)- if delete_webui_transcript(session_key):- removed = True- return removeddiff --git a/nanobot/webui/transcript.py b/nanobot/webui/transcript.pyindex 9d3c54ced..9b96d06d5 100644--- a/nanobot/webui/transcript.py+++ b/nanobot/webui/transcript.py@@ -1,4 +1,4 @@-"""Append-only WebUI display transcript (JSONL), separate from agent session."""+"""WebUI history projection over durable SQLite session facts."""from __future__ import annotations@@ -6,9 +6,8 @@ import base64import binasciiimport hashlibimport json-import osimport re-import shutil+import sqlite3import threadingimport timeimport uuid@@ -16,7 +15,6 @@ from bisect import bisect_leftfrom collections import OrderedDictfrom dataclasses import dataclassfrom datetime import datetime-from functools import lru_cachefrom pathlib import Pathfrom typing import Any, Callable, Mapping, NamedTuple, NotRequired, Sequence, TypedDict, castfrom urllib.parse import unquote, urlparse@@ -24,12 +22,23 @@ from urllib.parse import unquote, urlparsefrom loguru import loggerfrom nanobot.bus.events import INBOUND_META_TURN_ID-from nanobot.config.paths import get_webui_dirfrom nanobot.runtime_context import public_history_messagefrom nanobot.session.automation_turns import is_automation_kind+from nanobot.session.display_history import (+ HISTORY_GENERATION_METADATA_KEY,+ HistoryFact,+ HistoryReader,+)from nanobot.session.history_visibility import is_hidden_history_messagefrom nanobot.session.manager import Session, SessionManager-from nanobot.utils.helpers import atomic_write_lines+from nanobot.session.sqlite_store import SessionConflictError+from nanobot.session.state import SessionOperationQueueFullError+from nanobot.webui.history import (+ fact_from_record,+ initialize_history,+ record_from_fact,+ turn_records,+)from nanobot.webui.metadata import WEBUI_MESSAGE_SOURCE_METADATA_KEY, WEBUI_TURN_METADATA_KEYfrom nanobot.webui.outbound_wire import project_tool_eventsfrom nanobot.webui.session_identity import webui_chat_id, webui_session_key@@ -37,19 +46,11 @@ from nanobot.webui.session_identity import webui_chat_id, webui_session_keyWEBUI_TRANSCRIPT_SCHEMA_VERSION = 3WEBUI_FORK_MARKER_EVENT = "fork_marker"WEBUI_TRANSCRIPT_INCOMPLETE_KEY = "transcript_incomplete"-_MAX_TRANSCRIPT_FILE_BYTES = 8 * 1024 * 1024-_ACTIVE_TRANSCRIPT_ROTATE_BYTES = 2 * 1024 * 1024-_TARGET_ACTIVE_TRANSCRIPT_BYTES = _ACTIVE_TRANSCRIPT_ROTATE_BYTES // 2-_TRANSCRIPT_SEGMENT_MANIFEST_VERSION = 2-_TRANSCRIPT_ACTIVE_CHUNK_ID = "active"-_TRANSCRIPT_SEGMENT_RE = re.compile(r"^\d{6}\.jsonl$")_DEFAULT_TRANSCRIPT_PAGE_LIMIT = 160_MAX_TRANSCRIPT_PAGE_LIMIT = 1000_MAX_TRANSCRIPT_PAGE_RECORDS = 4_000_MAX_TRANSCRIPT_PAGE_BYTES = 20 * 1024 * 1024_MAX_INLINE_TRACE_DETAIL_BYTES = 32 * 1024-_MANIFEST_REBUILD_LOCKS = tuple(threading.Lock() for _ in range(32))-_ACTIVE_TRANSCRIPTS_WITH_DELTAS: set[str] = set()_WEBUI_TURN_ID_RE = re.compile(r"^[A-Za-z0-9._:-]{1,128}$")_WEBUI_REPLAY_IDENTITY_KEY = "_webui_replay_identity"_WEBUI_TRACE_DETAIL_UNSAFE_KEY = "_webui_trace_detail_unsafe"@@ -175,71 +176,15 @@ def _media_kind_from_name(name: str) -> str:return "file"-def webui_transcript_path(session_key: str) -> Path:- stem = SessionManager.safe_key(session_key)- return get_webui_dir() / f"{stem}.jsonl"---def webui_transcript_segments_dir(session_key: str) -> Path:- stem = SessionManager.safe_key(session_key)- return get_webui_dir() / f"{stem}.segments"---def _webui_transcript_manifest_path(session_key: str) -> Path:- return webui_transcript_segments_dir(session_key) / "manifest.json"---def _legacy_webui_thread_path(session_key: str) -> Path:- stem = SessionManager.safe_key(session_key)- return get_webui_dir() / f"{stem}.json"--def webui_transcript_revision(- session_key: str,- *,- variant: Mapping[str, Any] | None = None,+ session_key: str, *, history: HistoryReader, variant: Mapping[str, Any] | None = None,) -> str | None:- """Return a cheap revision from transcript artifact metadata and response inputs."""- active_path = webui_transcript_path(session_key)- segment_dir = webui_transcript_segments_dir(session_key)- snapshots: list[tuple[str, int, int]] = []- artifacts = (- ("active", active_path),- ("legacy", _legacy_webui_thread_path(session_key)),- ("manifest", segment_dir / "manifest.json"),- ("segments", segment_dir),- )- for label, path in artifacts:- try:- stat = path.stat()- except OSError:- continue- if label != "segments" and not path.is_file():- continue- snapshots.append((label, stat.st_size, stat.st_mtime_ns))- if not snapshots:+ revision = history.revision+ if revision is None:return None-- digest = hashlib.sha256()- digest.update(- json.dumps(- snapshots,- ensure_ascii=False,- separators=(",", ":"),- ).encode("utf-8")- )- if variant:- digest.update(b"\0")- digest.update(- json.dumps(- variant,- ensure_ascii=False,- separators=(",", ":"),- sort_keys=True,- default=str,- ).encode("utf-8")- )- return digest.hexdigest()[:32]+ encoded = json.dumps((revision, variant), ensure_ascii=False, sort_keys=True,+ separators=(",", ":"), default=str)+ return hashlib.sha256(encoded.encode("utf-8")).hexdigest()[:32]class _TranscriptTurnRef(NamedTuple):@@ -248,13 +193,6 @@ class _TranscriptTurnRef(NamedTuple):trace_details_safe: bool = True-class _TranscriptChunkRef(NamedTuple):- chunk_id: str- start_ordinal: int- turn_count: int- user_count: int--class _SessionBackfillTurn(NamedTuple):user_event: dict[str, Any]assistant_signature: tuple[str, ...]@@ -278,8 +216,6 @@ class TranscriptReplayStats:selected_bytes: int = 0selected_records: int = 0compacted_delta_records: int = 0- manifest_rebuilt: bool = False- manifest_rebuild_ms: int = 0replay_ms: int = 0capped_by_bytes: bool = Falsecapped_by_records: bool = False@@ -290,27 +226,6 @@ def _record_json_line(record: dict[str, Any]) -> str:return json.dumps(record, ensure_ascii=False, separators=(",", ":"))-def _read_transcript_file(path: Path) -> list[dict[str, Any]]:- lines_out: list[dict[str, Any]] = []- try:- with open(path, encoding="utf-8") as f:- for line_no, line in enumerate(f, start=1):- line = line.strip()- if not line:- continue- try:- obj = json.loads(line)- except json.JSONDecodeError:- logger.warning("bad jsonl at {} line {}", path, line_no)- continue- if isinstance(obj, dict):- lines_out.append(cast(dict[str, Any], obj))- except OSError as e:- logger.warning("read transcript failed {}: {}", path, e)- return []- return lines_out--def _records_bytes(records: list[dict[str, Any]]) -> int:total = 0for record in records:@@ -380,55 +295,10 @@ def _compact_completed_turns(return compacted, dropped-def _trim_oversized_turn(- turn: list[dict[str, Any]],- *,- max_records: int,- max_bytes: int,-) -> list[dict[str, Any]]:- """Keep the useful tail of one pathological turn within a hard replay budget."""- if not turn or max_records <= 0 or max_bytes <= 0:- return []-- user_index = next(- (index for index, record in enumerate(turn) if _is_user_transcript_row(record)),- None,- )- end_index = next(- (- index- for index in range(len(turn) - 1, -1, -1)- if turn[index].get("event") == "turn_end"- ),- None,- )- answer_index = next(- (- index- for index in range(len(turn) - 1, -1, -1)- if turn[index].get("event") in {"delta", "stream_end", "message"}- ),- None,- )- priority = [user_index, end_index, answer_index]- candidates = [- *[index for index in priority if index is not None],- *range(len(turn) - 1, -1, -1),- ]- selected: set[int] = set()- selected_bytes = 0- for index in candidates:- if index in selected or len(selected) >= max_records:- continue- record_bytes = _records_bytes([turn[index]])- if selected_bytes + record_bytes > max_bytes:- continue- selected.add(index)- selected_bytes += record_bytes- return [record for index, record in enumerate(turn) if index in selected]-def _flatten_turns(turns: list[list[dict[str, Any]]]) -> list[dict[str, Any]]:+def compact_legacy_records(records: list[dict[str, Any]]) -> list[dict[str, Any]]:+ turns, _ = _compact_completed_turns(_split_transcript_turns(records))return [record for turn in turns for record in turn]@@ -441,377 +311,13 @@ def _records_with_replay_identity(return [{**record,- _WEBUI_REPLAY_IDENTITY_KEY: f"turn:{turn_ordinal}:record:{record_index}",+ _WEBUI_REPLAY_IDENTITY_KEY: record.get(_WEBUI_REPLAY_IDENTITY_KEY, f"turn:{turn_ordinal}:record:{record_index}"),**({_WEBUI_TRACE_DETAIL_UNSAFE_KEY: True} if not trace_details_safe else {}),}for record_index, record in enumerate(records)]-def _write_records_to_path(path: Path, rows: list[dict[str, Any]]) -> None:- path.parent.mkdir(parents=True, exist_ok=True)- lines: list[str] = []- for row in rows:- raw = _record_json_line(row)- if len(raw.encode("utf-8")) > _MAX_TRANSCRIPT_FILE_BYTES:- raise ValueError("webui transcript line too large")- lines.append(raw)- atomic_write_lines(path, lines)---def _segment_file_path(session_key: str, segment_id: str) -> Path:- return webui_transcript_segments_dir(session_key) / f"{segment_id}.jsonl"---def _segment_ids_on_disk(session_key: str) -> list[str]:- directory = webui_transcript_segments_dir(session_key)- if not directory.is_dir():- return []- return sorted(- path.stem- for path in directory.iterdir()- if path.is_file() and _TRANSCRIPT_SEGMENT_RE.fullmatch(path.name)- )---def _segment_manifest_entry(- session_key: str,- segment_id: str,- *,- stats: TranscriptReplayStats | None = None,-) -> dict[str, Any]:- path = _segment_file_path(session_key, segment_id)- lines = _read_transcript_file(path)- size = path.stat().st_size if path.exists() else 0- if stats is not None:- stats.source_bytes += size- stats.parsed_records += len(lines)- return {- "id": segment_id,- "bytes": size,- "turn_count": len(_split_transcript_turns(lines)),- "user_count": sum(1 for line in lines if _is_user_transcript_row(line)),- }---def _non_negative_int(value: Any) -> int | None:- if isinstance(value, bool) or not isinstance(value, int) or value < 0:- return None- return value---def _normalize_manifest_entry(session_key: str, entry: Any) -> dict[str, Any] | None:- if not isinstance(entry, dict):- return None- manifest_entry = cast(dict[str, Any], entry)- segment_id = manifest_entry.get("id")- if not isinstance(segment_id, str) or not _TRANSCRIPT_SEGMENT_RE.fullmatch(f"{segment_id}.jsonl"):- return None- segment_path = _segment_file_path(session_key, segment_id)- values = {- key: _non_negative_int(manifest_entry.get(key))- for key in ("bytes", "turn_count", "user_count")- }- if not segment_path.is_file() or values["bytes"] != segment_path.stat().st_size:- return None- if values["turn_count"] is None or values["user_count"] is None:- return None- return {- "id": segment_id,- "bytes": values["bytes"],- "turn_count": values["turn_count"],- "user_count": values["user_count"],- }---def _write_segment_manifest(session_key: str, entries: list[dict[str, Any]]) -> None:- directory = webui_transcript_segments_dir(session_key)- directory.mkdir(parents=True, exist_ok=True)- data = {- "version": _TRANSCRIPT_SEGMENT_MANIFEST_VERSION,- "segments": entries,- }- path = _webui_transcript_manifest_path(session_key)- tmp_path = path.with_name(f"{path.name}.{uuid.uuid4().hex}.tmp")- try:- tmp_path.write_text(json.dumps(data, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")- os.replace(tmp_path, path)- except BaseException:- tmp_path.unlink(missing_ok=True)- raise---def _rebuild_segment_manifest(- session_key: str,- *,- stats: TranscriptReplayStats | None = None,-) -> list[dict[str, Any]]:- started = time.perf_counter()- segment_ids = _segment_ids_on_disk(session_key)- entries = [- _segment_manifest_entry(session_key, segment_id, stats=stats)- for segment_id in segment_ids- ]- if entries:- _write_segment_manifest(session_key, entries)- else:- _webui_transcript_manifest_path(session_key).unlink(missing_ok=True)- if stats is not None:- stats.manifest_rebuilt = True- stats.manifest_rebuild_ms += int((time.perf_counter() - started) * 1000)- return entries---def _load_segment_manifest_entries(session_key: str) -> list[dict[str, Any]] | None:- directory = webui_transcript_segments_dir(session_key)- if not directory.is_dir():- return []- path = _webui_transcript_manifest_path(session_key)- if not path.is_file():- return None- try:- data = json.loads(path.read_text(encoding="utf-8"))- manifest = cast(dict[str, Any], data) if isinstance(data, dict) else None- raw_segments = manifest.get("segments") if manifest is not None else None- if (- manifest is None- or manifest.get("version") != _TRANSCRIPT_SEGMENT_MANIFEST_VERSION- or not isinstance(raw_segments, list)- ):- return None- entries: list[dict[str, Any]] = []- for entry in cast(list[Any], raw_segments):- normalized = _normalize_manifest_entry(session_key, entry)- if normalized is None:- return None- entries.append(normalized)- if [entry["id"] for entry in entries] != _segment_ids_on_disk(session_key):- return None- return entries- except (OSError, json.JSONDecodeError, TypeError, AttributeError):- return None---def _manifest_rebuild_lock(session_key: str) -> threading.Lock:- digest = hashlib.sha256(session_key.encode("utf-8")).digest()- lock_index = int.from_bytes(digest[:2], "big") % len(_MANIFEST_REBUILD_LOCKS)- return _MANIFEST_REBUILD_LOCKS[lock_index]---def _read_segment_manifest_entries(- session_key: str,- *,- stats: TranscriptReplayStats | None = None,-) -> list[dict[str, Any]]:- # Windows requires readers to close the manifest before atomic replacement.- with _manifest_rebuild_lock(session_key):- entries = _load_segment_manifest_entries(session_key)- if entries is not None:- return entries- return _rebuild_segment_manifest(session_key, stats=stats)---def _repair_manifest_chunk_count(- session_key: str,- chunk_id: str,- actual_turn_count: int,- *,- stats: TranscriptReplayStats | None = None,-) -> None:- with _manifest_rebuild_lock(session_key):- entries = _load_segment_manifest_entries(session_key)- if entries is not None and any(- entry["id"] == chunk_id and entry["turn_count"] == actual_turn_count- for entry in entries- ):- return- _rebuild_segment_manifest(session_key, stats=stats)---def _read_segment_ids(session_key: str) -> list[str]:- return [entry["id"] for entry in _read_segment_manifest_entries(session_key)]---def _append_segment_turns(session_key: str, turns: list[list[dict[str, Any]]]) -> None:- if not turns:- return- with _manifest_rebuild_lock(session_key):- entries = _load_segment_manifest_entries(session_key)- if entries is None:- entries = _rebuild_segment_manifest(session_key)- next_id = int(entries[-1]["id"]) + 1 if entries else 1- batch: list[list[dict[str, Any]]] = []- batch_bytes = 0-- def write_batch() -> None:- nonlocal next_id- segment_id = f"{next_id:06d}"- path = _segment_file_path(session_key, segment_id)- _write_records_to_path(path, _flatten_turns(batch))- entries.append({- "id": segment_id,- "bytes": path.stat().st_size,- "turn_count": len(batch),- "user_count": sum(- 1- for turn in batch- for row in turn- if _is_user_transcript_row(row)- ),- })- next_id += 1-- for turn in turns:- turn_bytes = _records_bytes(turn)- if batch and batch_bytes + turn_bytes > _MAX_TRANSCRIPT_FILE_BYTES:- write_batch()- batch = []- batch_bytes = 0- batch.append(turn)- batch_bytes += turn_bytes- if batch:- write_batch()- _write_segment_manifest(session_key, entries)---def _rotate_active_transcript_if_needed(session_key: str) -> None:- path = webui_transcript_path(session_key)- if not path.is_file():- return- try:- if path.stat().st_size <= _ACTIVE_TRANSCRIPT_ROTATE_BYTES:- return- except OSError:- return-- lines = _read_transcript_file(path)- if not lines:- return- turns = _split_transcript_turns(lines)- turns, compacted_delta_records = _compact_completed_turns(turns)- if compacted_delta_records:- _write_records_to_path(path, _flatten_turns(turns))- try:- if path.stat().st_size <= _ACTIVE_TRANSCRIPT_ROTATE_BYTES:- return- except OSError:- return- if len(turns) <= 1:- return-- keep_start = len(turns) - 1- keep_bytes = 0- for idx in range(len(turns) - 1, -1, -1):- turn_bytes = _records_bytes(turns[idx])- if idx == len(turns) - 1 or keep_bytes + turn_bytes <= _TARGET_ACTIVE_TRANSCRIPT_BYTES:- keep_start = idx- keep_bytes += turn_bytes- continue- break-- moved = turns[:keep_start]- kept = turns[keep_start:]- if not moved:- return- _append_segment_turns(session_key, moved)- _write_records_to_path(path, _flatten_turns(kept))---def _chunk_ids(session_key: str) -> list[str]:- ids = _read_segment_ids(session_key)- if webui_transcript_path(session_key).is_file():- ids.append(_TRANSCRIPT_ACTIVE_CHUNK_ID)- return ids---def _read_chunk_turns(session_key: str, chunk_id: str) -> list[list[dict[str, Any]]]:- if chunk_id == _TRANSCRIPT_ACTIVE_CHUNK_ID:- path = webui_transcript_path(session_key)- else:- path = _segment_file_path(session_key, chunk_id)- if not path.is_file():- return []- return _split_transcript_turns(_read_transcript_file(path))---def _compact_segment_turns(- session_key: str,- chunk_id: str,- *,- stats: TranscriptReplayStats | None = None,-) -> list[list[dict[str, Any]]]:- """Compact one immutable legacy segment and keep its manifest entry valid."""- with _manifest_rebuild_lock(session_key):- turns = _read_chunk_turns(session_key, chunk_id)- if stats is not None:- path = _segment_file_path(session_key, chunk_id)- try:- stats.source_bytes += path.stat().st_size- except OSError:- pass- stats.parsed_records += sum(len(turn) for turn in turns)- compacted, dropped = _compact_completed_turns(turns)- if not dropped:- return turns-- entries = _load_segment_manifest_entries(session_key)- path = _segment_file_path(session_key, chunk_id)- _write_records_to_path(path, _flatten_turns(compacted))- if entries is None:- _rebuild_segment_manifest(session_key, stats=stats)- else:- for entry in entries:- if entry["id"] != chunk_id:- continue- entry["bytes"] = path.stat().st_size- entry["turn_count"] = len(compacted)- entry["user_count"] = sum(- 1- for turn in compacted- for record in turn- if _is_user_transcript_row(record)- )- break- _write_segment_manifest(session_key, entries)- if stats is not None:- stats.compacted_delta_records += dropped- return compacted---def _cached_chunk_turns(- session_key: str,- chunk_id: str,- turn_cache: dict[str, list[list[dict[str, Any]]]],- *,- stats: TranscriptReplayStats | None = None,-) -> list[list[dict[str, Any]]]:- if chunk_id not in turn_cache:- if chunk_id == _TRANSCRIPT_ACTIVE_CHUNK_ID:- path = webui_transcript_path(session_key)- else:- path = _segment_file_path(session_key, chunk_id)- turns = _read_chunk_turns(session_key, chunk_id)- needs_compaction = chunk_id != _TRANSCRIPT_ACTIVE_CHUNK_ID and any(- record.get("event") in {"delta", "reasoning_delta"}- for turn in turns- for record in turn- )- if needs_compaction:- turns = _compact_segment_turns(- session_key,- chunk_id,- stats=stats,- )- elif stats is not None:- try:- stats.source_bytes += path.stat().st_size- except OSError:- pass- stats.parsed_records += sum(len(turn) for turn in turns)- turn_cache[chunk_id] = turns- return turn_cache[chunk_id]--def _encode_page_cursor(before_turn_ordinal: int) -> str:raw = json.dumps({"before_turn": before_turn_ordinal},@@ -848,260 +354,73 @@ def _coerce_page_limit(limit: int | None) -> int:return max(1, min(_MAX_TRANSCRIPT_PAGE_LIMIT, int(limit)))-def _chunk_turn_refs(- session_key: str,- turn_cache: dict[str, list[list[dict[str, Any]]]],- *,- stats: TranscriptReplayStats | None = None,-) -> list[_TranscriptChunkRef]:- refs: list[_TranscriptChunkRef] = []- ordinal = 0- for entry in _read_segment_manifest_entries(session_key, stats=stats):- chunk_id = str(entry["id"])- turn_count = int(entry["turn_count"])- if turn_count <= 0:- continue- refs.append(_TranscriptChunkRef(chunk_id, ordinal, turn_count, int(entry["user_count"])))- ordinal += turn_count- if webui_transcript_path(session_key).is_file():- active_turns = _cached_chunk_turns(- session_key,- _TRANSCRIPT_ACTIVE_CHUNK_ID,- turn_cache,- stats=stats,- )- active_turn_count = len(active_turns)- if active_turn_count > 0:- refs.append(- _TranscriptChunkRef(- _TRANSCRIPT_ACTIVE_CHUNK_ID,- ordinal,- active_turn_count,- sum(1 for turn in active_turns for row in turn if _is_user_transcript_row(row)),- ),- )- return refs--def _transcript_turn_at_ordinal(- session_key: str,- ordinal: int,+ session_key: str, ordinal: int, *, history: HistoryReader,) -> list[dict[str, Any]] | None:- turn_cache: dict[str, list[list[dict[str, Any]]]] = {}- for chunk in _chunk_turn_refs(session_key, turn_cache):- local_index = ordinal - chunk.start_ordinal- if local_index < 0 or local_index >= chunk.turn_count:- continue- turns = _cached_chunk_turns(session_key, chunk.chunk_id, turn_cache)- return turns[local_index] if local_index < len(turns) else None- return None---def _count_user_messages_before_ordinal(- session_key: str,- chunks: list[_TranscriptChunkRef],- before_ordinal: int,- turn_cache: dict[str, list[list[dict[str, Any]]]],- *,- stats: TranscriptReplayStats | None = None,-) -> int:- total = 0- for chunk in chunks:- if before_ordinal <= chunk.start_ordinal:- break- local_end = min(chunk.turn_count, before_ordinal - chunk.start_ordinal)- if local_end <= 0:- continue- if local_end >= chunk.turn_count:- total += chunk.user_count- continue- turns = _cached_chunk_turns(- session_key,- chunk.chunk_id,- turn_cache,- stats=stats,- )- total += sum(- 1- for turn in turns[:local_end]- for row in turn- if _is_user_transcript_row(row)- )- return total+ return turn_records(history, ordinal) or Nonedef _select_transcript_page(- session_key: str,- *,- limit: int | None,- before: str | None,- after: str | None = None,- stats: TranscriptReplayStats | None = None,- _manifest_rebuilt: bool = False,+ session_key: str, *, history: HistoryReader, limit: int | None,+ before: str | None, after: str | None = None, stats: TranscriptReplayStats | None = None,) -> tuple[list[dict[str, Any]], dict[str, Any]]:stats = stats or TranscriptReplayStats()page_limit = _coerce_page_limit(limit)stats.effective_limit = page_limit- turn_cache: dict[str, list[list[dict[str, Any]]]] = {}- chunks = _chunk_turn_refs(session_key, turn_cache, stats=stats)- total_turns = sum(chunk.turn_count for chunk in chunks)+ total_turns = history.turn_countbefore_ordinal = decode_transcript_cursor(before)- upper_ordinal = total_turns if before_ordinal is None else min(before_ordinal, total_turns)+ upper = total_turns if before_ordinal is None else min(before_ordinal, total_turns)after_ordinal = decode_transcript_cursor(after)selected: list[_TranscriptTurnRef] = []- selected_event_count = 0- selected_record_count = 0- selected_bytes = 0- budget_reached = False-- for chunk in (chunks if after_ordinal is not None else reversed(chunks)):- if chunk.start_ordinal >= upper_ordinal:- continue- if after_ordinal is not None and chunk.start_ordinal + chunk.turn_count <= after_ordinal:- continue- local_upper = min(chunk.turn_count, upper_ordinal - chunk.start_ordinal)- if local_upper <= 0:- continue- turns = _cached_chunk_turns(- session_key,- chunk.chunk_id,- turn_cache,- stats=stats,- )- if (- chunk.chunk_id != _TRANSCRIPT_ACTIVE_CHUNK_ID- and len(turns) != chunk.turn_count- and not _manifest_rebuilt- ):- _repair_manifest_chunk_count(- session_key,- chunk.chunk_id,- len(turns),- stats=stats,- )- return _select_transcript_page(- session_key,- limit=limit,- before=before,- after=after,- stats=stats,- _manifest_rebuilt=True,- )- local_upper = min(local_upper, len(turns))- indices = (range(max(0, after_ordinal - chunk.start_ordinal), local_upper)- if after_ordinal is not None else range(local_upper - 1, -1, -1))- for turn_index in indices:- ordinal = chunk.start_ordinal + turn_index- trace_details_safe = True- turn, compacted_delta_records = _compact_completed_stream_deltas(- turns[turn_index]- )- stats.compacted_delta_records += compacted_delta_records- turn_record_count = len(turn)- turn_bytes = _records_bytes(turn)- exceeds_records = (- selected_record_count + turn_record_count > _MAX_TRANSCRIPT_PAGE_RECORDS- )- exceeds_bytes = selected_bytes + turn_bytes > _MAX_TRANSCRIPT_PAGE_BYTES- if exceeds_records:- stats.capped_by_records = True- if exceeds_bytes:- stats.capped_by_bytes = True- if selected and (exceeds_records or exceeds_bytes):- budget_reached = True- break- if exceeds_records or exceeds_bytes:- turn = _trim_oversized_turn(- turn,- max_records=_MAX_TRANSCRIPT_PAGE_RECORDS,- max_bytes=_MAX_TRANSCRIPT_PAGE_BYTES,- )- turn_record_count = len(turn)- turn_bytes = _records_bytes(turn)- stats.truncated_oversized_turn = True- trace_details_safe = False- selected.append(_TranscriptTurnRef(ordinal, turn, trace_details_safe))- selected_record_count += turn_record_count- selected_bytes += turn_bytes- selected_event_count += _client_projection_page_event_count(turn)- if selected_event_count >= page_limit:- break- if selected_event_count >= page_limit or budget_reached:+ event_count = record_count = byte_count = 0+ for ref in history.turns(before=upper, after=after_ordinal or 0, reverse=after_ordinal is None):+ exceeds_records = record_count + ref.record_count > _MAX_TRANSCRIPT_PAGE_RECORDS+ exceeds_bytes = byte_count + ref.byte_count > _MAX_TRANSCRIPT_PAGE_BYTES+ stats.capped_by_records |= exceeds_records+ stats.capped_by_bytes |= exceeds_bytes+ if selected and (exceeds_records or exceeds_bytes):break-- selected_chronological = selected if after_ordinal is not None else list(reversed(selected))- lines = [- record- for ref in selected_chronological- for record in _records_with_replay_identity(- ref.records,- turn_ordinal=ref.ordinal,- trace_details_safe=ref.trace_details_safe,+ safe = not (exceeds_records or exceeds_bytes)+ turn = turn_records(+ history, ref.ordinal,+ max_records=None if safe else _MAX_TRANSCRIPT_PAGE_RECORDS,+ max_bytes=None if safe else _MAX_TRANSCRIPT_PAGE_BYTES,)- ]- stats.selected_records = len(lines)- stats.selected_bytes = selected_bytes- if not selected_chronological:- return [], {- "before_cursor": None,- "has_more_before": False,- "after_cursor": None,- "has_more_after": False,- "loaded_event_count": 0,- "user_message_offset": 0,- }-- first_ref = selected_chronological[0]- has_more = first_ref.ordinal > 0- page = {- "before_cursor": _encode_page_cursor(first_ref.ordinal) if has_more else None,- "has_more_before": has_more,- "after_cursor": _encode_page_cursor(selected_chronological[-1].ordinal + 1)- if selected_chronological[-1].ordinal + 1 < total_turns else None,- "has_more_after": selected_chronological[-1].ordinal + 1 < total_turns,- "loaded_event_count": 0,- "user_message_offset": _count_user_messages_before_ordinal(- session_key,- chunks,- first_ref.ordinal,- turn_cache,- stats=stats,- ),+ stats.parsed_records += len(turn)+ stats.source_bytes += ref.byte_count if safe else _records_bytes(turn)+ turn, dropped = _compact_completed_stream_deltas(turn)+ stats.compacted_delta_records += dropped+ if not safe:+ stats.truncated_oversized_turn = True+ selected.append(_TranscriptTurnRef(ref.ordinal, turn, safe))+ record_count += len(turn)+ byte_count += _records_bytes(turn)+ event_count += _client_projection_page_event_count(turn)+ if event_count >= page_limit:+ break+ chronological = selected if after_ordinal is not None else list(reversed(selected))+ records = [record for ref in chronological for record in _records_with_replay_identity(+ ref.records, turn_ordinal=ref.ordinal, trace_details_safe=ref.trace_details_safe,+ )]+ stats.selected_records, stats.selected_bytes = len(records), byte_count+ if not chronological:+ return [], {"before_cursor": None, "has_more_before": False, "after_cursor": None,+ "has_more_after": False, "loaded_event_count": 0, "user_message_offset": 0}+ first, last = chronological[0].ordinal, chronological[-1].ordinal+ page: dict[str, Any] = {+ "before_cursor": _encode_page_cursor(first) if first else None,+ "has_more_before": first > 0,+ "after_cursor": _encode_page_cursor(last + 1) if last + 1 < total_turns else None,+ "has_more_after": last + 1 < total_turns, "loaded_event_count": 0,+ "user_message_offset": history.user_count_before(first),}if stats.truncated_oversized_turn:page["truncated_oversized_turn"] = True- return lines, page---def read_transcript_lines(session_key: str) -> list[dict[str, Any]]:- lines: list[dict[str, Any]] = []- for chunk_id in _chunk_ids(session_key):- if chunk_id == _TRANSCRIPT_ACTIVE_CHUNK_ID:- lines.extend(_read_transcript_file(webui_transcript_path(session_key)))- else:- lines.extend(_read_transcript_file(_segment_file_path(session_key, chunk_id)))- return lines---def _write_transcript_lines(session_key: str, rows: list[dict[str, Any]]) -> None:- delete_webui_transcript(session_key)- path = webui_transcript_path(session_key)- _write_records_to_path(path, rows)- _rotate_active_transcript_if_needed(session_key)+ return records, page-def _append_to_active_transcript(session_key: str, obj: dict[str, Any]) -> None:- raw = _record_json_line(obj)- if len(raw.encode("utf-8")) > _MAX_TRANSCRIPT_FILE_BYTES:- msg = "webui transcript line too large"- raise ValueError(msg)- path = webui_transcript_path(session_key)- path.parent.mkdir(parents=True, exist_ok=True)- line = raw + "\n"- with open(path, "a", encoding="utf-8") as f:- f.write(line)- f.flush()- os.fsync(f.fileno())+def read_transcript_lines(session_key: str, *, history: HistoryReader) -> list[dict[str, Any]]:+ return [record_from_fact(fact, session_key) for fact in history.all_facts()]def _now_ms() -> int:@@ -1124,44 +443,26 @@ def _record_for_append(obj: dict[str, Any]) -> dict[str, Any]:return record-def _compact_active_completed_streams(session_key: str) -> None:- path = webui_transcript_path(session_key)- turns, compacted_delta_records = _compact_completed_turns(- _split_transcript_turns(_read_transcript_file(path))- )- if compacted_delta_records:- _write_records_to_path(path, _flatten_turns(turns))---def append_transcript_object(session_key: str, obj: dict[str, Any]) -> None:- record = _record_for_append(obj)- _append_to_active_transcript(session_key, record)- if record.get("event") in {"delta", "reasoning_delta"}:- _ACTIVE_TRANSCRIPTS_WITH_DELTAS.add(session_key)- if record.get("event") == "turn_end":- if session_key in _ACTIVE_TRANSCRIPTS_WITH_DELTAS:- _compact_active_completed_streams(session_key)- _ACTIVE_TRANSCRIPTS_WITH_DELTAS.discard(session_key)- _rotate_active_transcript_if_needed(session_key)+def append_transcript_object(+ session_key: str, obj: dict[str, Any], *, sessions: SessionManager,+) -> None:+ """Offline import/fixture helper; runtime writes through the async recorder."""+ sessions.append_history(session_key, [fact_from_record(_record_for_append(obj))])-def append_session_message_input(- session_key: str,- *,- content: str,- created_at_ms: int,- session_message: Mapping[str, Any],+async def append_session_message_input(+ session_key: str, *, sessions: SessionManager, content: str, created_at_ms: int,+ session_message: Mapping[str, Any], expected_generation: str,) -> None:- """Append one admitted cross-session user input to its WebUI transcript."""chat_id = _chat_id_from_session_key(session_key)if chat_id is None:returnevent = build_user_transcript_event(chat_id, content)if event is None:return- event["created_at_ms"] = created_at_ms- event["session_message"] = dict(session_message)- append_transcript_object(session_key, event)+ event.update(created_at_ms=created_at_ms, session_message=dict(session_message))+ await sessions.state.append_history(session_key, [fact_from_record(event)],+ expected_generation=expected_generation)def normalize_webui_turn_id(value: Any) -> str:@@ -1188,23 +489,24 @@ def webui_message_source(metadata: dict[str, Any] | None) -> dict[str, str] | Noclass WebUITranscriptRecorder:- """Prepare and persist WebUI wire events without leaking UI rules into channels."""+ """Capture channel facts through the session owner before transport fan-out."""- def __init__(self, log: Any = logger) -> None:+ def __init__(self, sessions: SessionManager | None, log: Any = logger) -> None:+ self.sessions = sessionsself._log = logself._turn_sequences: dict[tuple[str, str], int] = {}+ async def initialize(self) -> None:+ sessions = self.sessions+ if sessions is not None:+ await initialize_history(sessions)+def client_turn_metadata(self, value: Any) -> dict[str, str]:return {WEBUI_TURN_METADATA_KEY: normalize_webui_turn_id(value)}def prepare_event(- self,- chat_id: str,- event: dict[str, Any],- *,- metadata: dict[str, Any] | None = None,- phase: str | None = None,- include_source: bool = False,+ self, chat_id: str, event: dict[str, Any], *, metadata: dict[str, Any] | None = None,+ phase: str | None = None, include_source: bool = False,) -> None:if include_source and (source := webui_message_source(metadata)):event["source"] = source@@ -1212,113 +514,73 @@ class WebUITranscriptRecorder:event["response_sources"] = _response_sources(metadata["response_sources"])self._annotate_turn(chat_id, event, metadata, phase)- def prepare_and_append(- self,- chat_id: str,- event: dict[str, Any],- *,- metadata: dict[str, Any] | None = None,- phase: str | None = None,- include_source: bool = False,+ async def prepare_and_append(+ self, chat_id: str, event: dict[str, Any], *, metadata: dict[str, Any] | None = None,+ phase: str | None = None, include_source: bool = False,transcript_overrides: dict[str, Any] | None = None,) -> bool:- self.prepare_event(- chat_id,- event,- metadata=metadata,- phase=phase,- include_source=include_source,- )- record = dict(event)- if transcript_overrides:- record.update(transcript_overrides)- return self.append(chat_id, record)-- def prepare_and_append_stream_event(- self,- chat_id: str,- event: dict[str, Any],- *,- completed_text: str | None,- metadata: dict[str, Any] | None = None,- phase: str | None = None,- include_source: bool = False,+ self.prepare_event(chat_id, event, metadata=metadata, phase=phase, include_source=include_source)+ record = {**event, **(transcript_overrides or {})}+ return await self.append(chat_id, record, metadata=metadata)++ async def prepare_and_append_stream_event(+ self, chat_id: str, event: dict[str, Any], *, completed_text: str | None,+ metadata: dict[str, Any] | None = None, phase: str | None = None, include_source: bool = False,) -> bool:- """Annotate every live stream event, but persist only completed segments.-- Delta frames are a transport concern: retaining each token-sized chunk- would turn rendering cadence into disk-write cadence. The matching end- event carries the canonical segment text used by history replay.- """- self.prepare_event(- chat_id,- event,- metadata=metadata,- phase=phase,- include_source=include_source,- )+ self.prepare_event(chat_id, event, metadata=metadata, phase=phase, include_source=include_source)if event.get("event") in {"delta", "reasoning_delta"}:return Truerecord = dict(event)if completed_text is not None:record["text"] = completed_text- return self.append(chat_id, record)-- def append_user_message(- self,- chat_id: str,- text: str,- *,- metadata: dict[str, Any],- media_paths: list[str] | None = None,- cli_apps: list[dict[str, Any]] | None = None,+ return await self.append(chat_id, record, metadata=metadata)++ async def append_user_message(+ self, chat_id: str, text: str, *, metadata: dict[str, Any],+ media_paths: list[str] | None = None, cli_apps: list[dict[str, Any]] | None = None,mcp_presets: list[dict[str, Any]] | None = None,session_mentions: Sequence[Mapping[str, Any]] | None = None,) -> bool:if text.strip() == "/stop" and not media_paths:return False- payload = build_user_transcript_event(- chat_id,- text,- media_paths=media_paths,- cli_apps=cli_apps,- mcp_presets=mcp_presets,- session_mentions=session_mentions,- )- if payload is None:+ event = build_user_transcript_event(chat_id, text, media_paths=media_paths,+ cli_apps=cli_apps, mcp_presets=mcp_presets, session_mentions=session_mentions)+ if event is None:return False- return self.prepare_and_append(chat_id, payload, metadata=metadata, phase="user")+ return await self.prepare_and_append(chat_id, event, metadata=metadata, phase="user")- def append(self, chat_id: str, event: dict[str, Any]) -> bool:+ async def append(+ self, chat_id: str, event: dict[str, Any], *, metadata: dict[str, Any] | None,+ ) -> bool:+ if self.sessions is None:+ return True # A transport-only channel has no durable session service.+ generation = (metadata or {}).get(HISTORY_GENERATION_METADATA_KEY)+ if not isinstance(generation, str) or not generation:+ self._log.warning("history event has no admitted generation for {}", chat_id)+ return Falsetry:- dup = json.loads(json.dumps(event, ensure_ascii=False))- append_transcript_object(webui_session_key(chat_id), dup)- except (OSError, ValueError, TypeError) as e:- self._log.warning("webui transcript append failed: {}", e)+ fact = fact_from_record(_record_for_append(event))+ await self.sessions.state.append_history(webui_session_key(chat_id), [fact],+ expected_generation=generation)+ except (OSError, ValueError, TypeError, sqlite3.Error, SessionConflictError, SessionOperationQueueFullError) as exc:+ self._log.warning("history append failed for {}: {}", chat_id, exc)return Falsereturn Truedef _next_turn_seq(self, chat_id: str, turn_id: str) -> int:key = (chat_id, turn_id)- seq = self._turn_sequences.get(key, 0) + 1- self._turn_sequences[key] = seq- return seq+ sequence = self._turn_sequences.get(key, 0) + 1+ self._turn_sequences[key] = sequence+ return sequencedef _annotate_turn(- self,- chat_id: str,- event: dict[str, Any],- metadata: dict[str, Any] | None,+ self, chat_id: str, event: dict[str, Any], metadata: dict[str, Any] | None,phase: str | None,) -> None:- if phase is None:- returnturn_id = (metadata or {}).get(WEBUI_TURN_METADATA_KEY)- if not isinstance(turn_id, str) or not turn_id:+ if phase is None or not isinstance(turn_id, str) or not turn_id:return- event["turn_id"] = turn_id- event["turn_phase"] = phase- event["turn_seq"] = self._next_turn_seq(chat_id, turn_id)+ event.update(turn_id=turn_id, turn_phase=phase, turn_seq=self._next_turn_seq(chat_id, turn_id))if phase == "complete":self._turn_sequences.pop((chat_id, turn_id), None)@@ -1330,94 +592,6 @@ def _chat_id_from_session_key(session_key: str) -> str | None:return chat_id.strip() or None-def _is_user_transcript_row(row: dict[str, Any]) -> bool:- return row.get("event") == "user" or row.get("role") == "user"---def fork_transcript_before_user_index(- source_key: str,- target_key: str,- before_user_index: int,-) -> bool:- """Copy transcript rows before a zero-based global user-message index.-- ``before_user_index == user_count`` copies the full transcript prefix. WebUI- uses that when forking from an assistant reply at the end of a chat.- """- if before_user_index < 0:- return False- lines = read_transcript_lines(source_key)- if not lines:- return False-- target_chat_id = _chat_id_from_session_key(target_key)- copied: list[dict[str, Any]] = []- user_index = 0- found_target = False- for row in lines:- if row.get("event") == WEBUI_FORK_MARKER_EVENT:- continue- if _is_user_transcript_row(row):- if user_index == before_user_index:- found_target = True- break- user_index += 1- dup = json.loads(json.dumps(row, ensure_ascii=False))- if target_chat_id is not None:- dup["chat_id"] = target_chat_id- copied.append(dup)- if user_index == before_user_index:- found_target = True-- if not found_target:- return False-- _write_transcript_lines(target_key, copied)- return True---def append_fork_marker(session_key: str) -> None:- """Mark the UI-only boundary where a WebUI fork starts accepting new turns."""- append_transcript_object(- session_key,- {- "event": WEBUI_FORK_MARKER_EVENT,- "chat_id": _chat_id_from_session_key(session_key),- },- )---def write_session_messages_as_transcript(- target_key: str,- messages: list[dict[str, Any]],-) -> None:- """Write a minimal WebUI transcript from already-truncated session messages."""- rows = [row for msg in messages- if (row := _session_user_event(target_key, msg) or _session_assistant_event(target_key, msg)) is not None]- _write_transcript_lines(target_key, rows)---def delete_webui_transcript(session_key: str) -> bool:- _ACTIVE_TRANSCRIPTS_WITH_DELTAS.discard(session_key)- removed = False- for path in (webui_transcript_path(session_key), _legacy_webui_thread_path(session_key)):- if not path.is_file():- continue- try:- path.unlink()- removed = True- except OSError as e:- logger.warning("Failed to delete webui transcript {}: {}", path, e)- segments_dir = webui_transcript_segments_dir(session_key)- if segments_dir.is_dir():- try:- shutil.rmtree(segments_dir)- removed = True- except OSError as e:- logger.warning("Failed to delete webui transcript segments {}: {}", segments_dir, e)- return removed--def normalize_session_mentions_metadata(raw: object) -> list[dict[str, str]]:"""Validate session-reference metadata crossing a persistence seam."""if not isinstance(raw, Sequence) or isinstance(raw, (str, bytes, bytearray)):@@ -1618,6 +792,13 @@ def _session_assistant_event(return event+def session_display_facts(session: Session) -> list[HistoryFact]:+ """Basic projection for a fork whose source has model records but no display facts."""+ return [fact_from_record(row) for message in session.messages+ if (row := _session_user_event(session.key, message)+ or _session_assistant_event(session.key, message)) is not None]++def _session_backfill_turns(session_key: str,session_messages: list[dict[str, Any]],@@ -2036,17 +1217,9 @@ def has_pending_tool_calls(return False-def has_unfinished_transcript_tail(session_key: str) -> bool:- """Return whether the active transcript ends in an unfinished turn.-- Recovery runs at gateway startup and only needs the newest, still-active- turn. Completed turns are rotated into immutable segment files, so reading- every historical segment here would make restart cost grow with the full- conversation history.- """- return has_pending_tool_calls(- _read_transcript_file(webui_transcript_path(session_key))- )+def has_unfinished_transcript_tail(session_key: str, *, history: HistoryReader) -> bool:+ count = history.turn_count+ return bool(count and has_pending_tool_calls(turn_records(history, count - 1)))def completed_turn_ids(lines: list[dict[str, Any]]) -> list[str]:@@ -2070,6 +1243,7 @@ def completed_turn_ids(lines: list[dict[str, Any]]) -> list[str]:def build_webui_trace_detail_response(session_key: str,detail_ref: str,+ *, history: HistoryReader,) -> dict[str, Any] | None:"""Resolve one deferred activity group as canonical projection events."""match = _WEBUI_TRACE_DETAIL_REF_RE.fullmatch(detail_ref)@@ -2077,7 +1251,7 @@ def build_webui_trace_detail_response(return Noneordinal = int(match.group("turn"))message_id = match.group("message")- turn = _transcript_turn_at_ordinal(session_key, ordinal)+ turn = _transcript_turn_at_ordinal(session_key, ordinal, history=history)if turn is None:return Noneturn, _ = _compact_completed_stream_deltas(turn)@@ -2300,6 +1474,13 @@ def _client_projection_event(context_window = record.get("context_window_tokens")if isinstance(context_window, int | float) and context_window >= 0:projected["context_window_tokens"] = int(context_window)+ for key in ("outcome", "failure_kind", "failure_error_kind", "failure_message"):+ if isinstance(value := record.get(key), str):+ projected[key] = value+ if isinstance(attempts := record.get("failure_attempts"), int) and not isinstance(attempts, bool):+ projected["failure_attempts"] = attempts+ if isinstance(goal := record.get("goal_state"), dict):+ projected["goal_state"] = goalreturn projectedreturn None@@ -2498,6 +1679,7 @@ def build_session_thread_response(def build_webui_thread_response(session_key: str,*,+ history: HistoryReader,augment_user_media: Callable[[list[str]], list[dict[str, Any]]] | None = None,augment_assistant_media: Callable[[list[str]], list[dict[str, Any]]] | None = None,augment_assistant_text: Callable[[str], str] | None = None,@@ -2517,6 +1699,7 @@ def build_webui_thread_response(replay_stats = stats or TranscriptReplayStats()lines, page = _select_transcript_page(session_key,+ history=history,limit=limit,before=before,after=after,@@ -2536,7 +1719,7 @@ def build_webui_thread_response(if _needs_user_event_backfill(lines):view, _ = _prompt_view_snapshot(- session_key, session_turns_loader=load_session_turns,+ session_key, history=history, session_turns_loader=load_session_turns,session_version_loader=session_version_loader,)recovered_lines: list[dict[str, Any]] = []@@ -2614,9 +1797,9 @@ def _assistant_signature_identity(signature: tuple[str, ...]) -> str:return hashlib.sha256(json.dumps(signature, ensure_ascii=False).encode("utf-8")).hexdigest()-def prompt_index_revision(session_key: str, session_version: tuple[str, int] | None = None) -> str | None:+def prompt_index_revision(session_key: str, session_version: tuple[str, int] | None = None, *, history: HistoryReader) -> str | None:"""Validate display inputs without reading canonical history or constructing a view."""- return webui_transcript_revision(session_key, variant={"session_version": session_version})+ return webui_transcript_revision(session_key, history=history, variant={"session_version": session_version})def _prompt_summary(record: dict[str, Any], ordinal: int, answer: str) -> PromptSummary:@@ -2630,33 +1813,25 @@ def _prompt_summary(record: dict[str, Any], ordinal: int, answer: str) -> Prompt}-@lru_cache(maxsize=64)-def _chunk_prompt_summaries(path: Path, identity: tuple[int, int, int], start: int) -> _ChunkPromptView:- # Cache only navigation/recovery inputs, not full answer, trace or tool bodies- # for ordinary complete turns. Page selection still reads bounded chunks.- check_turn_bytes = identity[2] > _MAX_TRANSCRIPT_PAGE_BYTES+def _history_prompt_summaries(history: HistoryReader) -> _ChunkPromptView:result: list[PromptSummary] = []candidates: list[_BackfillCandidate] = []- for ordinal, raw_turn in enumerate(_split_transcript_turns(_read_transcript_file(path)), start):- turn, _ = _compact_completed_stream_deltas(raw_turn)- if len(turn) > _MAX_TRANSCRIPT_PAGE_RECORDS or (check_turn_bytes and _records_bytes(turn) > _MAX_TRANSCRIPT_PAGE_BYTES):- turn = _trim_oversized_turn(- turn, max_records=_MAX_TRANSCRIPT_PAGE_RECORDS,- max_bytes=_MAX_TRANSCRIPT_PAGE_BYTES,- )- records = _records_with_replay_identity(turn, turn_ordinal=ordinal)+ turns: dict[int, list[dict[str, Any]]] = {}+ for ordinal, position, fact in history.navigation():+ record = record_from_fact(fact, history.key)+ record[_WEBUI_REPLAY_IDENTITY_KEY] = f"turn:{ordinal}:record:{position}"+ turns.setdefault(ordinal, []).append(record)+ for ordinal, records in turns.items():has_user = any(record.get("event") == "user" for record in records)signature = _transcript_turn_signature(records)- candidates.append(_BackfillCandidate(- ordinal, _assistant_signature_identity(signature), signature[0][:240] if signature else "", has_user,- ))+ candidates.append(_BackfillCandidate(ordinal, _assistant_signature_identity(signature),+ signature[0][:240] if signature else "", has_user))seen_turns: set[str] = set()seen_messages: set[str] = set()for index, record in enumerate(records):if record.get("event") != "user":continueturn_id = str(record.get("turn_id") or "")- # Match the client projector's existing per-turn delivery deduplication.provenance = normalize_session_message_ui_metadata(record.get("session_message"))if provenance:message_id = str(provenance["message_id"])@@ -2682,25 +1857,12 @@ def _chunk_prompt_summaries(path: Path, identity: tuple[int, int, int], start: idef _build_prompt_view(session_key: str,*,+ history: HistoryReader,session_turns_loader: Callable[[], list[_SessionBackfillTurn]] | None = None,) -> _PromptView:- raw_prompts: list[PromptSummary] = []- candidates: list[_BackfillCandidate] = []- start = 0- chunks = [- (_segment_file_path(session_key, entry["id"]), entry["turn_count"])- for entry in _read_segment_manifest_entries(session_key)- ]- chunks.append((webui_transcript_path(session_key), 0))- for path, count in chunks:- try:- stat = path.stat()- except FileNotFoundError:- continue- chunk = _chunk_prompt_summaries(path, (stat.st_ino, stat.st_mtime_ns, stat.st_size), start)- raw_prompts.extend(chunk.prompts)- candidates.extend(chunk.candidates)- start += count+ chunk = _history_prompt_summaries(history)+ raw_prompts = list(chunk.prompts)+ candidates = list(chunk.candidates)session_turns = Noneneeds_canonical = any(not candidate.has_user and candidate.signature for candidate in candidates)if needs_canonical and session_turns_loader is not None:@@ -2742,11 +1904,13 @@ def _build_prompt_view(def build_prompt_index(session_key: str,*,+ history: HistoryReader,session_messages_loader: Callable[[], list[dict[str, Any]] | None] | None = None,) -> list[PromptSummary]:"""Read the display prompt directory; only missing-user turns load canonical history."""view, _ = _prompt_view_snapshot(session_key,+ history=history,session_turns_loader=lambda: _session_backfill_turns(session_key, (session_messages_loader() if session_messages_loader else None) or [],),@@ -2756,33 +1920,34 @@ def build_prompt_index(# Bounded navigation snapshots retain summaries and recovered users, never full# canonical answers, transcripts, or a second durable conversation representation.-_PROMPT_SNAPSHOTS: OrderedDict[tuple[Path, str], _PromptView] = OrderedDict()+_PROMPT_SNAPSHOTS: OrderedDict[tuple[Path, str, str], _PromptView] = OrderedDict()_PROMPT_SNAPSHOT_LOCK = threading.Lock()def _prompt_view_snapshot(session_key: str,*,+ history: HistoryReader,session_turns_loader: Callable[[], list[_SessionBackfillTurn]] | None = None,session_version_loader: Callable[[], tuple[str, int] | None] | None = None,) -> tuple[_PromptView, str | None]:- path = webui_transcript_path(session_key)+ path = history.storage_pathdef read_revision() -> str | None:version = session_version_loader() if session_version_loader is not None else None- return prompt_index_revision(session_key, version)+ return prompt_index_revision(session_key, version, history=history)revision = read_revision()- cache_key = (path, revision) if revision else None+ cache_key = (path, session_key, revision) if revision else Noneif cache_key:with _PROMPT_SNAPSHOT_LOCK:cached = _PROMPT_SNAPSHOTS.get(cache_key)if cached is not None and (not cached.needs_canonical or session_version_loader is not None):_PROMPT_SNAPSHOTS.move_to_end(cache_key)return cached, revision- view = _build_prompt_view(session_key, session_turns_loader=session_turns_loader)- # A caller supplying only messages cannot validate canonical changes. It may- # still use transcript chunk caches, but must not cache recovered snapshots.+ view = _build_prompt_view(session_key, history=history, session_turns_loader=session_turns_loader)+ # A caller supplying only messages cannot validate canonical changes, so a+ # recovered view needs a version loader before it can be reused.if (view.needs_canonical and session_version_loader is None) or revision != read_revision():return view, Noneif cache_key:@@ -2804,14 +1969,16 @@ def prompt_index_response(session_key: str,previous_revision: str | None,*,+ history: HistoryReader,session_messages_loader: Callable[[], list[dict[str, Any]] | None] | None = None,session_version_loader: Callable[[], tuple[str, int] | None] | None = None,) -> PromptIndexResponse:- path = webui_transcript_path(session_key)+ path = history.storage_pathwith _PROMPT_SNAPSHOT_LOCK:- previous = _PROMPT_SNAPSHOTS.get((path, previous_revision)) if previous_revision else None+ previous = _PROMPT_SNAPSHOTS.get((path, session_key, previous_revision)) if previous_revision else Noneview, revision = _prompt_view_snapshot(session_key,+ history=history,session_turns_loader=lambda: _session_backfill_turns(session_key, (session_messages_loader() if session_messages_loader else None) or [],),diff --git a/nanobot/webui/ws_http.py b/nanobot/webui/ws_http.pyindex 11d34cbd2..475a55830 100644--- a/nanobot/webui/ws_http.py+++ b/nanobot/webui/ws_http.py@@ -18,7 +18,7 @@ import timefrom collections.abc import Awaitable, Callable, Mappingfrom dataclasses import dataclass, fieldfrom pathlib import Path-from typing import TYPE_CHECKING, Any, cast+from typing import TYPE_CHECKING, Any, Protocol, castfrom urllib.parse import quote, unquote, urlsplit, urlunsplitfrom loguru import logger@@ -33,6 +33,7 @@ from nanobot.cron.binding import CronBindingError, binding_revisionfrom nanobot.cron.session_turns import is_bound_cron_jobfrom nanobot.cron.types import CronJob, CronSchedulefrom nanobot.security.workspace_access import WorkspaceScope, WorkspaceScopeError+from nanobot.session.display_history import HistoryReaderfrom nanobot.session.manager import SessionManagerfrom nanobot.session.session_handles import (SessionHandle,@@ -128,7 +129,6 @@ from nanobot.webui.skills_marketplace import (trending_marketplace_skills,)from nanobot.webui.star_prompt import update_star_prompt-from nanobot.webui.thread_disk import delete_webui_threadfrom nanobot.webui.transcript import (TranscriptReplayStats,build_session_thread_response,@@ -345,6 +345,10 @@ def _resolve_bootstrap_model_name(# ---------------------------------------------------------------------------+class _HistoryProjection(Protocol):+ def __call__(self, request: WsRequest, key: str, *, history: HistoryReader) -> Response: ...++class GatewayHTTPHandler:"""Handles all HTTP routes served alongside the WebSocket endpoint.@@ -810,15 +814,11 @@ class GatewayHTTPHandler:async def _dispatch_session_routes(self, request: WsRequest, got: str) -> Response | None:m = re.fullmatch(r"/api/sessions/([^/]+)/webui-thread/prompts", got)if m:- return await asyncio.to_thread(self._handle_prompt_index_get, request, m.group(1))+ return await self._project_session_history(request, m.group(1), self._handle_prompt_index_get)m = re.match(r"^/api/sessions/([^/]+)/webui-thread/trace-detail$", got)if m:- return await asyncio.to_thread(- self._handle_webui_trace_detail_get,- request,- m.group(1),- )+ return await self._project_session_history(request, m.group(1), self._handle_webui_trace_detail_get)m = re.match(r"^/api/sessions/([^/]+)/webui-thread$", got)if m:@@ -1057,7 +1057,23 @@ class GatewayHTTPHandler:def _read_session_version(self, key: str) -> tuple[str, int] | None:return self.session_manager.read_session_version(key) if self.session_manager is not None else None- def _handle_prompt_index_get(self, request: WsRequest, key: str) -> Response:+ async def _project_session_history(+ self, request: WsRequest, key: str, project: _HistoryProjection,+ ) -> Response:+ if not self.check_api_token(request):+ return _http_error(401, "Unauthorized")+ decoded = _decode_api_key(key)+ if decoded is None:+ return _http_error(400, "invalid session key")+ if not _is_websocket_channel_session_key(decoded):+ return _http_error(404, "session not found")+ if self.session_manager is None:+ return _http_error(503, "session manager unavailable")+ return await self.session_manager.state.project_history(+ decoded, lambda history: project(request, key, history=history),+ )++ def _handle_prompt_index_get(self, request: WsRequest, key: str, *, history: HistoryReader) -> Response:from nanobot.webui.transcript import prompt_index_response, prompt_index_revisionif not self.check_api_token(request):@@ -1067,13 +1083,13 @@ class GatewayHTTPHandler:return _http_error(400, "invalid session key")if not _is_websocket_channel_session_key(decoded_key):return _http_error(404, "session not found")- revision = prompt_index_revision(decoded_key, self._read_session_version(decoded_key))+ revision = prompt_index_revision(decoded_key, self._read_session_version(decoded_key), history=history)etag = _quoted_etag(revision) if revision else Noneif etag and _etag_matches(_case_insensitive_header(request.headers, "If-None-Match"), etag):return _http_response(b"", status=304, extra_headers=[*_NO_STORE_HEADERS, ("ETag", etag)])query = _parse_query(request.path)data = prompt_index_response(- decoded_key, _query_first(query, "since"),+ decoded_key, _query_first(query, "since"), history=history,session_messages_loader=lambda: self._read_session_messages(decoded_key),session_version_loader=lambda: self._read_session_version(decoded_key),)@@ -1105,13 +1121,11 @@ class GatewayHTTPHandler:timer = loop.call_later(0.05, sample_event_loop_lag)started = time.perf_counter()+ def project(request: WsRequest, key: str, *, history: HistoryReader) -> Response:+ return self._handle_webui_thread_get(request, key, history=history, diagnostics=diagnostics)+try:- response = await asyncio.to_thread(- self._handle_webui_thread_get,- request,- key,- diagnostics=diagnostics,- )+ response = await self._project_session_history(request, key, project)finally:active = Falsetimer.cancel()@@ -1128,7 +1142,6 @@ class GatewayHTTPHandler:stats = diagnostics.transcriptif not (diagnostics.total_ms >= _SLOW_WEBUI_HTTP_LOG_MS- or stats.manifest_rebuiltor stats.capped_by_bytesor stats.capped_by_records):@@ -1136,7 +1149,7 @@ class GatewayHTTPHandler:self._log.warning("webui thread replay session_hash={} status={} limit={} source_bytes={} ""parsed_records={} selected_bytes={} selected_records={} compacted_deltas={} "- "manifest_rebuilt={} manifest_rebuild_ms={} capped_bytes={} capped_records={} "+ "capped_bytes={} capped_records={} ""truncated_turn={} replay_ms={} build_ms={} json_ms={} gzip_ms={} gzip={} ""response_bytes={} event_loop_lag_ms={} total_ms={}",diagnostics.session_hash or "unknown",@@ -1147,8 +1160,6 @@ class GatewayHTTPHandler:stats.selected_bytes,stats.selected_records,stats.compacted_delta_records,- stats.manifest_rebuilt,- stats.manifest_rebuild_ms,stats.capped_by_bytes,stats.capped_by_records,stats.truncated_oversized_turn,@@ -1167,6 +1178,7 @@ class GatewayHTTPHandler:request: WsRequest,key: str,*,+ history: HistoryReader,diagnostics: _WebUIThreadDiagnostics | None = None,) -> Response:if not self.check_api_token(request):@@ -1224,7 +1236,7 @@ class GatewayHTTPHandler:"session_version": self._read_session_version(decoded_key),"workspace_scope": scope.payload(),}- initial_revision = webui_transcript_revision(decoded_key, variant=revision_variant)+ initial_revision = webui_transcript_revision(decoded_key, history=history, variant=revision_variant)if initial_revision is not None:etag = _quoted_etag(initial_revision)if _etag_matches(_case_insensitive_header(request.headers, "If-None-Match"), etag):@@ -1232,7 +1244,7 @@ class GatewayHTTPHandler:build_started = time.perf_counter()replay_stats = diagnostics.transcript if diagnostics is not None else TranscriptReplayStats()data = build_webui_thread_response(- decoded_key,+ decoded_key, history=history,augment_user_media=self.media.augment_transcript_media,augment_assistant_media=self.media.augment_transcript_media,augment_assistant_text=lambda text: self.media.rewrite_local_markdown_images(@@ -1258,7 +1270,7 @@ class GatewayHTTPHandler:return _http_error(404, "webui thread not found")data["workspace_scope"] = scope.payload()revision_variant["session_version"] = self._read_session_version(decoded_key)- final_revision = webui_transcript_revision(decoded_key, variant=revision_variant)+ final_revision = webui_transcript_revision(decoded_key, history=history, variant=revision_variant)response_headers = list(_NO_STORE_HEADERS)if final_revision is not None and final_revision == initial_revision:response_headers.append(("ETag", _quoted_etag(final_revision)))@@ -1270,7 +1282,7 @@ class GatewayHTTPHandler:metrics=diagnostics.response if diagnostics is not None else None,)- def _handle_webui_trace_detail_get(self, request: WsRequest, key: str) -> Response:+ def _handle_webui_trace_detail_get(self, request: WsRequest, key: str, *, history: HistoryReader) -> Response:if not self.check_api_token(request):return _http_error(401, "Unauthorized")decoded_key = _decode_api_key(key)@@ -1281,7 +1293,7 @@ class GatewayHTTPHandler:detail_ref = _query_first(_parse_query(request.path), "ref")if not detail_ref:return _http_error(400, "missing trace detail ref")- data = build_webui_trace_detail_response(decoded_key, detail_ref)+ data = build_webui_trace_detail_response(decoded_key, detail_ref, history=history)if data is None:return _http_error(404, "trace detail not found")return _http_json_response(@@ -1373,10 +1385,7 @@ class GatewayHTTPHandler:elif self.subagent_manager is not None:await self.subagent_manager.cancel_by_session(decoded_key)session_deleted = await self.session_manager.state.delete(decoded_key)- transcript_deleted = await asyncio.to_thread(delete_webui_thread, decoded_key)- return _http_json_response(- {"deleted": bool(draft_deleted or session_deleted or transcript_deleted)}- )+ return _http_json_response({"deleted": bool(draft_deleted or session_deleted)})# -- Automation routes --------------------------------------------------diff --git a/tests/agent/test_loop_progress.py b/tests/agent/test_loop_progress.pyindex b722a334d..bddb92f1e 100644--- a/tests/agent/test_loop_progress.py+++ b/tests/agent/test_loop_progress.py@@ -848,6 +848,7 @@ class TestToolEventProgress:assert request_ctx.metadata == {"injected_event": "subagent_result","subagent_task_id": "sub-1",+ "_history_generation": request_ctx.session_generation,}statuses = [message.event.status@@ -905,6 +906,7 @@ class TestToolEventProgress:"_wants_stream",WEBSOCKET_TURN_OWNER_METADATA_KEY,WEBUI_TURN_METADATA_KEY,+ "_history_generation","latency_ms",}for message in visible_eventsdiff --git a/tests/agent/test_loop_recovery.py b/tests/agent/test_loop_recovery.pyindex 689bc5c3e..681467363 100644--- a/tests/agent/test_loop_recovery.py+++ b/tests/agent/test_loop_recovery.py@@ -28,8 +28,6 @@ from nanobot.webui.transcript import WebUITranscriptRecorderasync def test_committed_model_failure_continues_with_tool_results_without_resending(tmp_path: Path, monkeypatch: pytest.MonkeyPatch,) -> None:- monkeypatch.setattr("nanobot.webui.transcript.get_webui_dir", lambda: tmp_path / "webui")- monkeypatch.setattr("nanobot.webui.session_list_index.get_webui_dir", lambda: tmp_path / "webui")bus = MessageBus()sessions = SessionManager(tmp_path)session = sessions.get_or_create("websocket:chat")@@ -146,8 +144,7 @@ async def test_recovery_continuation_runs_without_a_sustained_goal(async def test_recovery_preserves_turn_origin_until_the_next_user_turn(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, late_result: bool,) -> None:- monkeypatch.setattr("nanobot.webui.transcript.get_webui_dir", lambda: tmp_path / "webui")- monkeypatch.setattr("nanobot.webui.session_list_index.get_webui_dir", lambda: tmp_path / "webui")+ monkeypatch.setattr("nanobot.webui.history.get_webui_dir", lambda: tmp_path / "webui")bus = MessageBus()provider = MagicMock(aclose=AsyncMock())provider.get_default_model.return_value = "test-model"@@ -208,12 +205,12 @@ async def test_recovery_preserves_turn_origin_until_the_next_user_turn("message_id": None if late_result else "user-message","turn_id": delivery.route.turn_id,}- recorder = WebUITranscriptRecorder()+ recorder = WebUITranscriptRecorder(loop.sessions)async def record_progress(content, **kwargs):- recorder.prepare_and_append(+ await recorder.prepare_and_append("chat", {"event": "message", "chat_id": "chat", "text": content, "kind": "progress"},- metadata=delivery.route.metadata, phase="activity",+ metadata=delivery.delivery_message.metadata, phase="activity",)task = asyncio.create_task(loop._process_message(diff --git a/tests/agent/test_response_source.py b/tests/agent/test_response_source.pyindex b9f5af61c..6ee53fd22 100644--- a/tests/agent/test_response_source.py+++ b/tests/agent/test_response_source.py@@ -23,12 +23,7 @@ from nanobot.providers.base import LLMProvider, LLMResponse, ProviderCallContextfrom nanobot.providers.factory import make_provider, provider_signaturefrom nanobot.providers.fallback_provider import FallbackProviderfrom nanobot.utils.llm_runtime import LLMRuntime-from nanobot.webui.transcript import (- WebUITranscriptRecorder,- append_transcript_object,- build_webui_thread_response,- webui_transcript_path,-)+from nanobot.webui.transcript import WebUITranscriptRecorderclass AnswerProvider(LLMProvider):@@ -65,7 +60,7 @@ def delivery(chat="chat", *, streaming=True):@pytest.mark.parametrize("streaming", [False, True])@pytest.mark.parametrize("failures", [0, 1, 2, 3])-async def test_actual_candidate_is_delivered_and_replayed(+async def test_actual_candidate_is_delivered_and_replayed(history,streaming,failures,tmp_path,@@ -98,7 +93,7 @@ async def test_actual_candidate_is_delivered_and_replayed(channel="websocket", chat_id="chat", content=result.content or "",), publish_completion=False)records = []- recorder = WebUITranscriptRecorder()+ recorder = WebUITranscriptRecorder(None)while not bus.outbound.empty():msg = bus.outbound.get_nowait()if msg.event is not None and not isinstance(msg.event, StreamDeltaEvent | StreamEndEvent):@@ -112,8 +107,8 @@ async def test_actual_candidate_is_delivered_and_replayed(records.append(record)# Completed segment is the durable record; token-sized deltas aren't needed on replay.for record in (r for r in records if r["event"] != "delta"):- append_transcript_object("websocket:chat", record)- replay = build_webui_thread_response("websocket:chat")+ history.append("websocket:chat", record)+ replay = history.build("websocket:chat")if failures == 3:assert all(not r.get("response_sources") for r in records)if replay is None:@@ -300,7 +295,7 @@ def test_factory_captures_names_even_when_models_and_settings_are_identical():assert make_provider(config)._fallback_preset_names == ("renamed",)-def test_old_history_and_malformed_identity_are_not_guessed(tmp_path, monkeypatch):+def test_old_history_and_malformed_identity_are_not_guessed(history, tmp_path, monkeypatch):monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)records = [{"event": "message", "text": "legacy"},@@ -313,8 +308,8 @@ def test_old_history_and_malformed_identity_are_not_guessed(tmp_path, monkeypatc]},]for record in records:- append_transcript_object("websocket:source-history", record)- replay = build_webui_thread_response("websocket:source-history")+ history.append("websocket:source-history", record)+ replay = history.build("websocket:source-history")assert replay is not Noneevents = replay["events"]assert "response_sources" not in events[0]@@ -355,27 +350,28 @@ async def test_runner_records_each_provider_when_streaming_times_out_and_recover@pytest.mark.parametrize("temporary", [False, True])-async def test_websocket_wire_and_disk_replay_preserve_sources(tmp_path, monkeypatch, temporary):+async def test_websocket_wire_and_disk_replay_preserve_sources(history, tmp_path, monkeypatch, temporary):monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)channel = WebSocketChannel({"allowFrom": ["*"]}, MessageBus(), gateway=MagicMock())channel._media.rewrite_local_markdown_images.side_effect = lambda text: text- channel._transcripts = WebUITranscriptRecorder()+ channel._transcripts = WebUITranscriptRecorder(history.sessions)+ generation, _ = await history.sessions.state.open_with_metadata("websocket:source-chat", {})channel._temporary_chats.should_persist_transcript = lambda _: not temporarychannel._safe_send_to = AsyncMock()channel._subs["source-chat"] = {MagicMock()}sources = [{"provider": "xai", "model": "grok", "preset": "snapshot name", "fallback": True}]- metadata = {"webui_turn_id": "source-turn", "response_sources": sources}+ metadata = {"webui_turn_id": "source-turn", "response_sources": sources, "_history_generation": generation}await channel.send_delta("source-chat", "Hello", metadata, stream_id="s")await channel.send_delta("source-chat", "", metadata, stream_id="s", stream_end=True)frames = [json.loads(call.args[1]) for call in channel._safe_send_to.call_args_list]assert [frame["event"] for frame in frames] == ["delta", "stream_end"]assert all(frame["response_sources"] == sources for frame in frames)if temporary:- assert not webui_transcript_path("websocket:source-chat").exists()+ assert history.read("websocket:source-chat") == []else:# A fresh reader after an unrelated preset rename/delete sees the saved snapshot.sources[0]["preset"] = "renamed later"- replay = build_webui_thread_response("websocket:source-chat")+ replay = history.build("websocket:source-chat")assert replay is not Noneend = next(e for e in replay["events"] if e["event"] == "stream_end")assert end["response_sources"][0]["preset"] == "snapshot name"diff --git a/tests/agent/test_session_inputs.py b/tests/agent/test_session_inputs.pyindex a9b6bf578..87ae4fc17 100644--- a/tests/agent/test_session_inputs.py+++ b/tests/agent/test_session_inputs.py@@ -5,7 +5,6 @@ from unittest.mock import AsyncMock, MagicMockimport pytest-from agent.session_helpers import stop_loopfrom nanobot.agent.loop import AgentLoopfrom nanobot.bus.events import InboundMessagefrom nanobot.bus.queue import MessageBus@@ -89,15 +88,14 @@ async def test_session_message_text_is_not_dispatched_as_a_slash_command(loop: AgentLoop,) -> None:loop.sessions.save(loop.sessions.get_or_create("telegram:target"))- task = asyncio.create_task(loop.run())- try:- await loop.bus.publish_inbound(_message("/stop"))- response = await asyncio.wait_for(loop.bus.consume_outbound(), timeout=2)+ await loop._dispatch_inbound(_message("/stop"))+ await asyncio.wait_for(+ asyncio.gather(*loop._active_tasks["telegram:target"]), timeout=2,+ )+ response = await asyncio.wait_for(loop.bus.consume_outbound(), timeout=2)- assert response.content == "Reviewed"- loop.provider.chat_stream_with_retry.assert_awaited_once()- finally:- await stop_loop(loop, task, timeout=2)+ assert response.content == "Reviewed"+ loop.provider.chat_stream_with_retry.assert_awaited_once()async def test_path_followup_reaches_running_turn(loop: AgentLoop) -> None:diff --git a/tests/agent/test_turn_delivery.py b/tests/agent/test_turn_delivery.pyindex 74ba41435..f4fb26e93 100644--- a/tests/agent/test_turn_delivery.py+++ b/tests/agent/test_turn_delivery.py@@ -26,7 +26,7 @@ async def test_internal_compaction_never_reaches_its_result_destination(channel,msg = InboundMessage(channel=channel, sender_id="user", chat_id="chat", content="check")delivery = factory.create(msg, key)metadata = {"_compaction_route": {"channel": channel, "chat_id": "chat"}} if legacy_route else {}- idle_events = factory.session_events(key, metadata)+ idle_events = factory.session_events(key, metadata, generation="generation")for sink in (delivery.events, idle_events):assert not sink.accepts(ContextCompactionEvent)for phase in ("started", "succeeded", "failed", "cancelled"):@@ -39,7 +39,7 @@ async def test_internal_compaction_never_reaches_its_result_destination(channel,# The same destination still receives the user's own compaction lifecycle.user_delivery = factory.create(msg, f"{channel}:chat")user_delivery.remember_session_route(metadata)- for sink in (user_delivery.events, factory.session_events(f"{channel}:chat", metadata)):+ for sink in (user_delivery.events, factory.session_events(f"{channel}:chat", metadata, generation="generation")):await sink.emit(ContextCompactionEvent("user-compact", "succeeded"))assert factory.bus.outbound.get_nowait().event == ContextCompactionEvent("user-compact", "succeeded",@@ -79,7 +79,7 @@ async def test_session_route_delivers_automatic_and_manual_compaction(session_metadata = {}delivery.remember_session_route(session_metadata)- sink = factory.session_events(key, session_metadata)+ sink = factory.session_events(key, session_metadata, generation="generation")assert sink.publish is not Noneawait sink.emit(automatic)await sink.emit(notified)@@ -100,7 +100,7 @@ async def test_idle_compaction_keeps_its_route_when_a_unified_session_moves() ->metadata={"slack": {"thread_ts": "1700000000.000100"}},)factory.create(original, key).remember_session_route(session_metadata)- sink = factory.session_events(key, session_metadata)+ sink = factory.session_events(key, session_metadata, generation="generation")assert sink.publish is not Noneawait sink.emit(ContextCompactionEvent("compact-1", "started", notify=True))@@ -119,7 +119,7 @@ async def test_idle_compaction_keeps_its_route_when_a_unified_session_moves() ->async def test_automatic_compaction_reaches_legacy_websocket_session() -> None:factory = TurnDeliveryFactory(MessageBus())event = ContextCompactionEvent(compaction_id="compact-1", phase="succeeded")- sink = factory.session_events("websocket:chat", {})+ sink = factory.session_events("websocket:chat", {}, generation="generation")assert sink.publish is not Noneawait sink.emit(event)assert factory.bus.outbound.get_nowait().event is event@@ -474,3 +474,32 @@ async def test_late_subagent_route_requires_webui_owned_session(tmp_path: Path)"injected_event": "subagent_result","subagent_task_id": "sub-1",}+++async def test_unified_display_route_admits_alias_generation_and_rejects_stale_lifetime(tmp_path):+ from nanobot.session.display_history import HISTORY_GENERATION_METADATA_KEY+ from nanobot.session.sqlite_store import SessionConflictError++ sessions = SessionManager(tmp_path)+ alias = await sessions.state.get_header("websocket:chat")+ source = await sessions.state.get_header("unified:default")+ policy = WebuiTurnRoutePolicy(sessions)+ factory = TurnDeliveryFactory(MessageBus(), route_policy=policy, route_admission=policy.admit)+ message = InboundMessage(channel="system", sender_id="cron", chat_id="websocket:chat", content="notice")+ delivery = factory.create(message, source.key)+ await delivery.bind_session_generation(source.generation)+ assert delivery.route.metadata[HISTORY_GENERATION_METADATA_KEY] == alias.generation+ assert delivery.delivery_message.session_generation == source.generation+ response = delivery.background_response("done", stop_reason="end_turn", streamed=False, latency_ms=1)+ assert response.metadata[HISTORY_GENERATION_METADATA_KEY] == alias.generation+ await sessions.state.delete(alias.key)+ replacement = await sessions.state.get_header(alias.key)+ stale = factory.create(InboundMessage(+ channel="websocket", sender_id="user", chat_id="chat", content="old queued input",+ metadata={HISTORY_GENERATION_METADATA_KEY: alias.generation},+ ), source.key)+ with pytest.raises(SessionConflictError):+ await stale.bind_session_generation(source.generation)+ assert replacement.generation != alias.generation+ await sessions.state.aclose()diff --git a/tests/agent/tools/test_session_history.py b/tests/agent/tools/test_session_history.pyindex df6620e8c..1750f08cb 100644--- a/tests/agent/tools/test_session_history.py+++ b/tests/agent/tools/test_session_history.py@@ -9,7 +9,6 @@ import pytestfrom nanobot.agent.tools.context import RequestContext, request_contextfrom nanobot.agent.tools.sessions import ReadSessionTool, SearchSessionsToolfrom nanobot.session.manager import SessionManager-from nanobot.webui.transcript import append_transcript_object@pytest.mark.asyncio@@ -57,10 +56,10 @@ async def test_session_tools_find_old_matches_in_full_session(@pytest.mark.asyncio@pytest.mark.parametrize("state", ["canonical", "transcript", "mixed"])-async def test_session_tools_use_canonical_records_after_restart(tmp_path, monkeypatch, state):+async def test_session_tools_use_canonical_records_after_restart(history, tmp_path, monkeypatch, state):webui_dir = tmp_path / "webui"- monkeypatch.setattr("nanobot.webui.transcript.get_webui_dir", lambda: webui_dir)- manager = SessionManager(tmp_path)+ monkeypatch.setattr("nanobot.webui.history.get_webui_dir", lambda: webui_dir)+ manager = history.sessionskey = "websocket:history"if state != "transcript":session = manager.get_or_create(key)@@ -68,10 +67,10 @@ async def test_session_tools_use_canonical_records_after_restart(tmp_path, monkesession.add_message("user", "canonical needle")manager.save(session)if state != "canonical":- append_transcript_object(key, {+ history.append(key, {"event": "user", "chat_id": "history", "text": "display-only needle",})- append_transcript_object(key, {"event": "turn_end", "chat_id": "history"})+ history.append(key, {"event": "turn_end", "chat_id": "history"})def durable_files():return {@@ -82,7 +81,7 @@ async def test_session_tools_use_canonical_records_after_restart(tmp_path, monkeoriginal = durable_files()for _ in range(2):- manager = SessionManager(tmp_path)+ manager = history.sessionswith request_context(RequestContext(channel="websocket", chat_id="current", session_key="websocket:current",)):@@ -91,7 +90,7 @@ async def test_session_tools_use_canonical_records_after_restart(tmp_path, monkedisplay_search = json.loads(await SearchSessionsTool(manager).execute(query="display-only"))if state == "transcript":assert search["results"] == []- assert read.is_error and "session not found" in str(read)+ assert json.loads(read)["messages"] == []else:assert [row["session_key"] for row in search["results"]] == [key]assert search["results"][0]["excerpts"][0]["content"] == "canonical needle"diff --git a/tests/cli/test_channel_delivery.py b/tests/cli/test_channel_delivery.pyindex 1d95d37cd..e55611eb4 100644--- a/tests/cli/test_channel_delivery.py+++ b/tests/cli/test_channel_delivery.py@@ -7,8 +7,12 @@ import pytestfrom nanobot.agent.tools.context import RequestContext, request_contextfrom nanobot.agent.tools.message import MessageToolfrom nanobot.bus.queue import MessageBus+from nanobot.channels.websocket.runtime import WebSocketChannel, WebSocketConfigfrom 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_KEYfrom 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, monkeyassert "replaced session" in resultassert bus.outbound.empty()assert (await sessions.state.read("email:[email protected]")).messages == []+++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)++[email protected]("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"]++[email protected]("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++[email protected]("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 Nonediff --git a/tests/cli/test_commands.py b/tests/cli/test_commands.pyindex 9a81fae92..c913a1a0a 100644--- a/tests/cli/test_commands.py+++ b/tests/cli/test_commands.py@@ -131,6 +131,7 @@ class _EmptyGatewaySessionManager:def state(self):return SimpleNamespace(list_sessions=AsyncMock(return_value=[]),+ migrate_history=AsyncMock(),record_delivery=AsyncMock(),aclose=AsyncMock(),)diff --git a/tests/session/test_display_history.py b/tests/session/test_display_history.pynew file mode 100644index 000000000..25ff72a7c--- /dev/null+++ b/tests/session/test_display_history.py@@ -0,0 +1,358 @@+"""One SQLite owner for model messages and durable, generation-bound display facts."""++from __future__ import annotations++import json+import sqlite3+from contextlib import closing+from pathlib import Path++import pytest++from nanobot.session.display_history import HistoryFact, HistoryKind+from nanobot.session.manager import SessionManager+from nanobot.session.sqlite_store import SessionConflictError+from nanobot.webui.forking import create_webui_chat_fork+from nanobot.webui.history import HISTORY_IMPORT_MARKER, collect_legacy_history+from nanobot.webui.transcript import WebUITranscriptRecorder, build_webui_thread_response+++def _manager(tmp_path: Path) -> SessionManager:+ return SessionManager(tmp_path / "workspace", sessions_root=tmp_path / "sessions")+++def _legacy(directory: Path, key: str, records: list[dict]) -> Path:+ directory.mkdir(parents=True, exist_ok=True)+ path = directory / f"{SessionManager.safe_key(key)}.jsonl"+ path.write_text("".join(json.dumps(record, ensure_ascii=False) + "\n" for record in records), encoding="utf-8")+ return path+++async def test_display_facts_survive_reopen_without_entering_model_history(tmp_path):+ sessions = _manager(tmp_path)+ session = await sessions.state.get("websocket:durable")+ session.add_message("user", "question")+ session.add_message("assistant", "answer")+ await sessions.state.finish_turn(session)+ facts = [+ HistoryFact(HistoryKind.INPUT, {"text": "question"}, "turn-1", "user", 1, 1000),+ HistoryFact(HistoryKind.REASONING_SEGMENT, {"text": "reasoning"}, "turn-1", "reasoning", 2, 1001),+ HistoryFact(HistoryKind.FILE_ACTIVITY, {"edits": [{"path": "a.py", "diff": "-old\n+new"}]}, "turn-1", "activity", 3, 1002),+ HistoryFact(HistoryKind.ANSWER_SEGMENT, {"text": "answer", "response_sources": [{+ "provider": "test", "model": "actual-fallback", "preset": "default", "fallback": True,+ }]}, "turn-1", "answer", 4, 1003),+ HistoryFact(HistoryKind.COMPLETION, {"usage": {"total_tokens": 30}, "round_usages": [{"total_tokens": 30}],+ "latency_ms": 40, "outcome": "failed", "failure_message": "model unavailable"},+ "turn-1", "complete", 5, 1004),+ ]+ await sessions.state.append_history(session.key, facts, expected_generation=session.generation)+ with closing(sqlite3.connect(sessions.sessions_dir / "sessions.sqlite3")) as db:+ stored = db.execute("SELECT record FROM history_entries ORDER BY position").fetchall()+ assert [json.loads(row[0])["kind"] for row in stored] == [fact.kind for fact in facts]+ assert all("chat_id" not in json.loads(row[0]) and "event" not in json.loads(row[0]) for row in stored)+ await sessions.state.aclose()++ reopened = _manager(tmp_path)+ assert reopened.project_history(session.key, lambda history: history.all_facts()) == facts+ model = reopened.read_session_file(session.key)+ assert [message["content"] for message in model["messages"]] == ["question", "answer"]+ payload = reopened.project_history(session.key, lambda history: build_webui_thread_response(session.key, history=history))+ assert payload["completed_turn_ids"] == ["turn-1"]+ terminal = next(event for event in payload["events"] if event["event"] == "turn_end")+ assert terminal["failure_message"] == "model unavailable" and terminal["outcome"] == "failed"+ assert terminal["round_usages"] == [{"total_tokens": 30}]+ assert any(event.get("edits") == facts[2].content["edits"] for event in payload["events"])+ assert next(event for event in payload["events"] if event["event"] == "stream_end")["response_sources"] == facts[3].content["response_sources"]+++async def test_delete_reset_reject_old_display_generation(tmp_path):+ sessions = _manager(tmp_path)+ key = "websocket:generation"+ first = await sessions.state.get(key)+ first.add_message("user", "old")+ await sessions.state.finish_turn(first)+ fact = HistoryFact(HistoryKind.INPUT, {"text": "old"})+ await sessions.state.append_history(key, [fact], expected_generation=first.generation)+ assert await sessions.state.delete(key)+ with pytest.raises(SessionConflictError):+ await sessions.state.append_history(key, [fact], expected_generation=first.generation)+ assert await sessions.state.project_history(key, lambda history: history.revision) is None+ second = await sessions.state.get(key)+ second.add_message("user", "new")+ await sessions.state.finish_turn(second)+ with pytest.raises(SessionConflictError):+ await sessions.state.append_history(key, [fact], expected_generation=first.generation)+ assert await sessions.state.project_history(key, lambda history: history.all_facts()) == []+ await sessions.state.aclose()+++async def test_fork_rolls_back_both_histories_if_display_copy_fails(tmp_path, monkeypatch):+ sessions = _manager(tmp_path)+ source = await sessions.state.get("websocket:source")+ source.add_message("user", "question")+ source.add_message("assistant", "answer")+ await sessions.state.finish_turn(source)+ monkeypatch.setattr("nanobot.webui.forking.uuid.uuid4", lambda: "failed-fork")+ original = sessions._store.append_history++ def fail(key, facts, *, expected_generation):+ original(key, facts, expected_generation=expected_generation)+ raise OSError("disk full")++ monkeypatch.setattr(sessions._store, "append_history", fail)+ with pytest.raises(OSError, match="disk full"):+ await create_webui_chat_fork(sessions, source_chat_id="source", before_user_index=1)+ assert await sessions.state.read_metadata("websocket:failed-fork") is None+ assert await sessions.state.project_history("websocket:failed-fork", lambda history: history.all_facts()) == []+ assert (await sessions.state.get(source.key)).messages == source.messages+ await sessions.state.aclose()+++async def test_legacy_import_is_atomic_repeatable_and_deletion_cannot_resurrect_backup(tmp_path):+ sessions = _manager(tmp_path)+ directory = tmp_path / "legacy"+ path = _legacy(directory, "websocket:imported", [+ {"event": "user", "chat_id": "imported", "text": "hello", "created_at_ms": 1000},+ {"event": "delta", "chat_id": "imported", "text": "an"},+ {"event": "delta", "chat_id": "imported", "text": "swer"},+ {"event": "stream_end", "chat_id": "imported"},+ {"event": "turn_end", "chat_id": "imported"},+ ])+ before = path.read_bytes()+ def collect(rows):+ return collect_legacy_history(rows, directory=directory)+ await sessions.state.migrate_history(HISTORY_IMPORT_MARKER, collect)+ facts = await sessions.state.project_history("websocket:imported", lambda history: history.all_facts())+ assert [fact.kind for fact in facts] == [HistoryKind.INPUT, HistoryKind.ANSWER_SEGMENT, HistoryKind.COMPLETION]+ assert facts[1].content["text"] == "answer"+ assert path.read_bytes() == before+ await sessions.state.migrate_history(HISTORY_IMPORT_MARKER, collect)+ assert await sessions.state.project_history("websocket:imported", lambda history: history.all_facts()) == facts+ assert await sessions.state.delete("websocket:imported")+ await sessions.state.aclose()+ reopened = _manager(tmp_path)+ await reopened.state.migrate_history(HISTORY_IMPORT_MARKER, collect)+ assert await reopened.state.read_metadata("websocket:imported") is None+ await reopened.state.aclose()+++async def test_corrupt_legacy_input_rolls_back_import_marker_and_all_records(tmp_path):+ sessions = _manager(tmp_path)+ directory = tmp_path / "legacy"+ _legacy(directory, "websocket:first", [{"event": "user", "chat_id": "first", "text": "safe"}])+ corrupt = _legacy(directory, "websocket:second", [{"event": "user", "chat_id": "second", "text": "also safe"}])+ with corrupt.open("a", encoding="utf-8") as handle:+ handle.write('{"event":\n')+ with pytest.raises(ValueError, match="cannot import history"):+ await sessions.state.migrate_history(HISTORY_IMPORT_MARKER, lambda rows: collect_legacy_history(rows, directory=directory))+ assert await sessions.state.list_sessions() == []+ with closing(sqlite3.connect(sessions.sessions_dir / "sessions.sqlite3")) as db:+ assert db.execute("SELECT value FROM storage_meta WHERE key=?", (HISTORY_IMPORT_MARKER,)).fetchone() is None+ _legacy(directory, "websocket:second", [{"event": "user", "chat_id": "second", "text": "fixed"}])+ await sessions.state.migrate_history(HISTORY_IMPORT_MARKER, lambda rows: collect_legacy_history(rows, directory=directory))+ assert {row["key"] for row in await sessions.state.list_sessions()} == {"websocket:first", "websocket:second"}+ await sessions.state.aclose()+++async def test_segments_import_in_order_without_trusting_damaged_manifest(tmp_path):+ sessions = _manager(tmp_path)+ directory = tmp_path / "legacy"+ segments = directory / "websocket_order.segments"+ segments.mkdir(parents=True)+ for number in (2, 1):+ (segments / f"{number:06}.jsonl").write_text(json.dumps({"event": "message", "chat_id": "order", "text": str(number)}) + "\n", encoding="utf-8")+ (segments / "manifest.json").write_text("broken cached manifest", encoding="utf-8")+ _legacy(directory, "websocket:order", [{"event": "message", "chat_id": "order", "text": "3"}])+ await sessions.state.migrate_history(HISTORY_IMPORT_MARKER, lambda rows: collect_legacy_history(rows, directory=directory))+ assert await sessions.state.project_history("websocket:order", lambda history: [fact.content["text"] for fact in history.all_facts()]) == ["1", "2", "3"]+ await sessions.state.aclose()+++def test_orphan_lossy_filename_requires_recorded_identity(tmp_path):+ directory = tmp_path / "legacy"+ _legacy(directory, "websocket:a_b", [{"event": "message", "text": "unknown whether a_b or a:b"}])+ with pytest.raises(ValueError, match="ambiguous legacy history identity"):+ collect_legacy_history([], directory=directory)+++async def test_recorder_keeps_completed_segments_not_every_transport_delta(tmp_path):+ sessions = _manager(tmp_path)+ key = "websocket:stream"+ generation, _ = await sessions.state.open_with_metadata(key, {})+ recorder = WebUITranscriptRecorder(sessions)+ metadata = {"_history_generation": generation, "_webui_turn_id": "turn"}+ for _ in range(8):+ assert await recorder.prepare_and_append_stream_event("stream", {"event": "delta", "text": "x"}, completed_text=None, metadata=metadata, phase="answer")+ assert await recorder.prepare_and_append_stream_event("stream", {"event": "stream_end"}, completed_text="x" * 8, metadata=metadata, phase="answer")+ facts = await sessions.state.project_history(key, lambda history: history.all_facts())+ assert [fact.kind for fact in facts] == [HistoryKind.ANSWER_SEGMENT]+ assert facts[0].content["text"] == "x" * 8+ await sessions.state.aclose()++[email protected]("committed", [False, True])+async def test_real_process_exit_cannot_split_fork_model_and_display_histories(tmp_path, committed):+ import subprocess+ import sys++ sessions = _manager(tmp_path)+ source = await sessions.state.get("websocket:source")+ source.add_message("user", "question")+ source.add_message("assistant", "answer")+ await sessions.state.finish_turn(source)+ await sessions.state.append_history(source.key, [+ HistoryFact(HistoryKind.INPUT, {"text": "question"}),+ HistoryFact(HistoryKind.ANSWER, {"text": "answer"}),+ HistoryFact(HistoryKind.COMPLETION),+ ], expected_generation=source.generation)+ await sessions.state.aclose()+ child = tmp_path / "crash.py"+ child.write_text('''import asyncio, os, sys+from pathlib import Path+from nanobot.session.manager import SessionManager+root = Path(sys.argv[1])+sessions = SessionManager(root / "workspace", sessions_root=root / "sessions")+original = sessions._store.append_history+def append(key, facts, *, expected_generation):+ original(key, facts, expected_generation=expected_generation)+ if sys.argv[2] == "False":+ os._exit(91)+sessions._store.append_history = append+async def run():+ await sessions.state.fork("websocket:source", "websocket:fork", 1)+ os._exit(92)+asyncio.run(run())+''', encoding="utf-8")+ result = subprocess.run([sys.executable, str(child), str(tmp_path), str(committed)], capture_output=True, timeout=15)+ assert result.returncode == (92 if committed else 91), result.stderr.decode("utf-8", errors="replace")+ reopened = _manager(tmp_path)+ fork = reopened.read_session_file("websocket:fork")+ facts = reopened.project_history("websocket:fork", lambda history: history.all_facts())+ if committed:+ assert [message["content"] for message in fork["messages"]] == ["question", "answer"]+ assert [fact.kind for fact in facts] == [HistoryKind.INPUT, HistoryKind.ANSWER, HistoryKind.COMPLETION, HistoryKind.FORK_BOUNDARY]+ else:+ assert fork is None and facts == []+ assert reopened.read_session_file(source.key)["messages"] == source.messages+++async def test_legacy_import_owner_survives_delete_and_new_workspace(tmp_path):+ from nanobot.webui.history import collect_legacy_history++ root = tmp_path / "sessions"+ legacy = tmp_path / "webui"+ legacy.mkdir()+ key = "websocket:owned"+ (legacy / f"{SessionManager.safe_key(key)}.jsonl").write_text(+ '{"event":"user","chat_id":"owned","text":"keep ownership"}\n', encoding="utf-8",+ )+ first = SessionManager(tmp_path / "first", sessions_root=root)+ await first.state.migrate_history(HISTORY_IMPORT_MARKER, lambda rows: collect_legacy_history(+ rows, directory=legacy, namespace=first.sessions_dir,+ ))+ await first.state.delete(key)+ await first.state.aclose()+ second = SessionManager(tmp_path / "second", sessions_root=root)+ await second.state.migrate_history(HISTORY_IMPORT_MARKER, lambda rows: collect_legacy_history(+ rows, directory=legacy, namespace=second.sessions_dir,+ ))+ assert await second.state.read(key) is None+ await second.state.aclose()+++async def test_large_display_fact_serialization_and_copy_stay_off_runtime_loop(tmp_path, monkeypatch):+ import threading++ import nanobot.session.state as state_module++ sessions = _manager(tmp_path)+ session = await sessions.state.get_header("websocket:large-activity")+ runtime_thread = threading.get_ident()+ encode, copy = HistoryFact.encode, state_module.deepcopy++ def worker_encode(fact):+ assert threading.get_ident() != runtime_thread+ return encode(fact)++ def worker_copy(value, *args, **kwargs):+ if isinstance(value, list) and value and isinstance(value[0], HistoryFact):+ assert threading.get_ident() != runtime_thread+ return copy(value, *args, **kwargs)++ monkeypatch.setattr(HistoryFact, "encode", worker_encode)+ monkeypatch.setattr(state_module, "deepcopy", worker_copy)+ recorder = WebUITranscriptRecorder(sessions)+ assert await recorder.prepare_and_append("large-activity", {+ "event": "message", "kind": "tool_hint", "text": "exec",+ "tool_events": [{"tool": "exec", "result": "x" * 1_000_000}],+ }, metadata={"_history_generation": session.generation})+ facts = await sessions.state.project_history(session.key, lambda history: history.all_facts())+ assert len(facts[0].content["tool_events"][0]["result"]) == 1_000_000+ await sessions.state.aclose()+++def test_message_projection_adapter_preserves_reply_and_agent_activity():+ from nanobot.webui.history import fact_from_record, record_from_fact++ record = {+ "event": "message", "chat_id": "activity", "kind": "progress",+ "text": "Inspecting workspace", "reply_to": "message-1", "created_at_ms": 1000,+ "agent_ui": {"version": 1, "status": "working", "label": "Inspecting workspace"},+ }+ fact = HistoryFact.decode(fact_from_record(record).encode())+ assert record_from_fact(fact, "websocket:activity") == record+++async def test_legacy_activity_without_record_timestamps_preserves_file_time(tmp_path):+ import os++ from nanobot.webui.session_list_index import list_webui_sessions++ sessions = _manager(tmp_path)+ session = await sessions.state.get_header("websocket:old-activity")+ directory = tmp_path / "legacy"+ path = _legacy(directory, session.key, [{+ "event": "message", "chat_id": "old-activity", "text": "historical display activity",+ }])+ os.utime(path, (1760000000, 1760000000))+ await sessions.state.migrate_history(HISTORY_IMPORT_MARKER, lambda rows: collect_legacy_history(+ rows, directory=directory,+ ))+ rows = list_webui_sessions(sessions, await sessions.state.list_metadata())+ from datetime import datetime++ assert datetime.fromisoformat(rows[0]["updated_at"]).timestamp() == 1760000000+ assert rows[0]["preview"] == "historical display activity"+ await sessions.state.aclose()++[email protected]("stored_display", [False, True])+async def test_new_fork_activity_uses_creation_time_not_inherited_history(tmp_path, stored_display):+ from datetime import datetime++ from nanobot.webui.session_list_index import list_webui_sessions++ sessions = _manager(tmp_path)+ source = await sessions.state.get("websocket:old-source")+ old_time = datetime(2020, 1, 1)+ source.add_message("user", "old question", timestamp=old_time.isoformat())+ source.add_message("assistant", "old answer", timestamp=old_time.isoformat())+ await sessions.state.finish_turn(source)+ if stored_display:+ old_ms = int(old_time.timestamp() * 1000)+ await sessions.state.append_history(source.key, [+ HistoryFact(HistoryKind.INPUT, {"text": "old question"}, created_at_ms=old_ms),+ HistoryFact(HistoryKind.ANSWER, {"text": "old answer"}, created_at_ms=old_ms),+ HistoryFact(HistoryKind.COMPLETION, created_at_ms=old_ms),+ ], expected_generation=source.generation)+ forked = await create_webui_chat_fork(sessions, source_chat_id="old-source", before_user_index=1)+ assert forked is not None+ _, fork_key = forked+ fork = await sessions.state.get(fork_key)+ facts = await sessions.state.project_history(fork_key, lambda history: history.all_facts())+ assert facts[-1].kind is HistoryKind.FORK_BOUNDARY+ assert facts[-1].created_at_ms == int(fork.created_at.timestamp() * 1000)+ rows = list_webui_sessions(sessions, await sessions.state.list_metadata())+ assert rows[0]["key"] == fork_key+ await sessions.state.aclose()diff --git a/tests/session/test_followup_commit.py b/tests/session/test_followup_commit.pyindex 971a0fdd5..fc5187f84 100644--- a/tests/session/test_followup_commit.py+++ b/tests/session/test_followup_commit.py@@ -35,7 +35,7 @@ def _input(text, turn_id):@pytest.mark.parametrize("count", [1, 2])@pytest.mark.parametrize("leave_unconsumed", [False, True])async def test_late_consumption_commits_history_and_only_its_ids(tmp_path, monkeypatch, count, leave_unconsumed):- monkeypatch.setattr("nanobot.webui.transcript.get_webui_dir", lambda: tmp_path / "webui")+ monkeypatch.setattr("nanobot.webui.history.get_webui_dir", lambda: tmp_path / "webui")sessions = SessionManager(tmp_path / "workspace", sessions_root=tmp_path / "sessions")state = sessions.statetry:diff --git a/tests/session/test_recovery.py b/tests/session/test_recovery.pyindex b81481bd5..f12238aba 100644--- a/tests/session/test_recovery.py+++ b/tests/session/test_recovery.py@@ -19,7 +19,7 @@ from nanobot.session.recovery import (pending_followups,record_pending_followup,)-from nanobot.webui import session_list_index, transcript+from nanobot.webui import history as history_modulefrom nanobot.webui.recovery import RecoveryActionError, RecoveryCoordinator@@ -28,8 +28,8 @@ def _isolate_webui_transcripts(tmp_path: Path, monkeypatch: pytest.MonkeyPatch)"""Recovery scans must discover only test-owned transcripts, not active user chats."""webui_dir = tmp_path / "isolated-webui"webui_dir.mkdir()- monkeypatch.setattr(session_list_index, "get_webui_dir", lambda: webui_dir)- monkeypatch.setattr(transcript, "get_webui_dir", lambda: webui_dir)+ monkeypatch.setattr(history_module, "get_webui_dir", lambda: webui_dir)+ monkeypatch.setattr(history_module, "get_webui_dir", lambda: webui_dir)def _persist(manager: SessionManager, session: Session) -> None:@@ -69,7 +69,7 @@ async def test_stale_incomplete_transcript_waits_for_confirmation(tmp_path: Path_persist(sessions, session)monkeypatch.setattr("nanobot.webui.transcript.has_unfinished_transcript_tail",- lambda _key: True,+ lambda _key, **_kwargs: True,)coordinator, bus, restarted = _coordinator(tmp_path)@@ -102,7 +102,7 @@ async def test_materialized_interruption_can_continue_from_saved_context(_persist(sessions, session)monkeypatch.setattr("nanobot.webui.transcript.has_unfinished_transcript_tail",- lambda _key: True,+ lambda _key, **_kwargs: True,)coordinator, bus, restarted = _coordinator(tmp_path)@@ -132,8 +132,8 @@ async def test_transcript_only_interruption_is_discovered_without_materializing_) -> None:webui_dir = tmp_path / "webui"webui_dir.mkdir()- monkeypatch.setattr(session_list_index, "get_webui_dir", lambda: webui_dir)- monkeypatch.setattr(transcript, "get_webui_dir", lambda: webui_dir)+ monkeypatch.setattr(history_module, "get_webui_dir", lambda: webui_dir)+ monkeypatch.setattr(history_module, "get_webui_dir", lambda: webui_dir)unfinished_key = "websocket:unfinished"completed_key = "websocket:completed"(webui_dir / f"{SessionManager.safe_key(unfinished_key)}.jsonl").write_text(@@ -155,7 +155,9 @@ async def test_transcript_only_interruption_is_discovered_without_materializing_assert restored.metadata[RECOVERY_METADATA_KEY]["status"] == "awaiting_user"assert restored.metadata[RECOVERY_METADATA_KEY]["reason"] == "interrupted_without_checkpoint"assert restored.metadata[RECOVERY_METADATA_KEY]["can_continue"] is False- assert sessions.read_session_metadata(completed_key) is None+ completed = sessions.get_existing(completed_key)+ assert completed is not None and completed.messages == []+ assert RECOVERY_METADATA_KEY not in completed.metadataevent = bus.outbound.get_nowait().eventassert isinstance(event, RecoveryStateEvent)assert event.status == "awaiting_user"@@ -196,7 +198,7 @@ async def test_scan_loads_only_sessions_that_need_webui_recovery(monkeypatch.setattr(restarted.state, "get", tracked_get_or_create)monkeypatch.setattr("nanobot.webui.transcript.has_unfinished_transcript_tail",- lambda _key: False,+ lambda _key, **_kwargs: False,)await coordinator.scan()diff --git a/tests/session/test_sqlite_schema.py b/tests/session/test_sqlite_schema.pyindex 7dc3b9adf..937c7a529 100644--- a/tests/session/test_sqlite_schema.py+++ b/tests/session/test_sqlite_schema.py@@ -86,7 +86,7 @@ def test_ordered_upgrades_preserve_records_and_do_not_repeat_import(tmp_path, mosource.write_text("stale malformed source must not be imported again", encoding="utf-8")upgraded = SqliteSessionStore(tmp_path)upgraded.initialize(workspace)- assert _database_snapshot(store.path)[0] == 3+ assert _database_snapshot(store.path)[0] == len(sqlite_store._SCHEMA_MIGRATIONS)loaded = upgraded.load(original.key)assert loaded is not Noneassert loaded.messages == original.messages@@ -138,7 +138,7 @@ def test_failed_upgrade_rolls_back_schema_version_and_records_then_can_retry(tmp*migrations, add_archived, complete_upgrade,))store.initialize(workspace)- assert _database_snapshot(store.path)[0] == 3+ assert _database_snapshot(store.path)[0] == len(sqlite_store._SCHEMA_MIGRATIONS)loaded = store.load(original.key)assert loaded is not Noneassert loaded.messages == original.messagesdiff --git a/tests/utils/test_webui_compat_imports.py b/tests/utils/test_webui_compat_imports.pyindex ccb97e288..58179286c 100644--- a/tests/utils/test_webui_compat_imports.py+++ b/tests/utils/test_webui_compat_imports.py@@ -1,14 +1,12 @@import importlibfrom nanobot.session import webui_turns-from nanobot.webui import thread_disk, transcript+from nanobot.webui import transcriptdef test_legacy_webui_utils_imports_resolve_to_new_modules() -> None:- legacy_thread_disk = importlib.import_module("nanobot.utils.webui_thread_disk")legacy_transcript = importlib.import_module("nanobot.utils.webui_transcript")legacy_turn_helpers = importlib.import_module("nanobot.utils.webui_turn_helpers")- assert legacy_thread_disk.delete_webui_thread is thread_disk.delete_webui_threadassert legacy_transcript.append_transcript_object is transcript.append_transcript_objectassert legacy_turn_helpers.mark_webui_session is webui_turns.mark_webui_sessiondiff --git a/tests/utils/test_webui_thread_disk.py b/tests/utils/test_webui_thread_disk.pydeleted file mode 100644index 8486beb74..000000000--- a/tests/utils/test_webui_thread_disk.py+++ /dev/null@@ -1,38 +0,0 @@-"""Tests for WebUI on-disk cleanup (legacy JSON + transcript JSONL)."""--from __future__ import annotations--from nanobot.webui.thread_disk import delete_webui_thread, webui_thread_file_path-from nanobot.webui.transcript import (- append_transcript_object,- webui_transcript_path,- webui_transcript_segments_dir,-)---def test_delete_webui_thread_removes_legacy_json_and_transcript(tmp_path, monkeypatch) -> None:- monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)- monkeypatch.setattr("nanobot.webui.transcript._MAX_TRANSCRIPT_FILE_BYTES", 520)- monkeypatch.setattr("nanobot.webui.transcript._ACTIVE_TRANSCRIPT_ROTATE_BYTES", 520)- monkeypatch.setattr("nanobot.webui.transcript._TARGET_ACTIVE_TRANSCRIPT_BYTES", 260)- key = "websocket:k1"- json_path = webui_thread_file_path(key)- json_path.parent.mkdir(parents=True, exist_ok=True)- json_path.write_text('{"x":1}', encoding="utf-8")- for idx in range(1, 5):- append_transcript_object(- key,- {"event": "user", "chat_id": "k1", "text": f"question {idx} " + ("x" * 24)},- )- append_transcript_object(- key,- {"event": "message", "chat_id": "k1", "text": f"answer {idx} " + ("y" * 24)},- )- append_transcript_object(key, {"event": "turn_end", "chat_id": "k1"})- assert webui_transcript_path(key).is_file()- assert webui_transcript_segments_dir(key).is_dir()- assert delete_webui_thread(key) is True- assert not json_path.is_file()- assert not webui_transcript_path(key).is_file()- assert not webui_transcript_segments_dir(key).exists()- assert delete_webui_thread(key) is Falsediff --git a/tests/utils/test_webui_transcript.py b/tests/utils/test_webui_transcript.pyindex 17b055f85..40c68c29a 100644--- a/tests/utils/test_webui_transcript.py+++ b/tests/utils/test_webui_transcript.py@@ -3,72 +3,51 @@from __future__ import annotationsimport json-from concurrent.futures import ThreadPoolExecutor-from contextlib import contextmanager-from pathlib import Path-from threading import Event, Lockimport pytestimport nanobot.webui.transcript as transcript_modulefrom nanobot.session.history_visibility import HIDDEN_HISTORY_META-from nanobot.webui.transcript import (- WEBUI_TRANSCRIPT_SCHEMA_VERSION,- TranscriptReplayStats,- append_fork_marker,- append_transcript_object,- build_webui_thread_response,- build_webui_trace_detail_response,- fork_transcript_before_user_index,- read_transcript_lines,- webui_transcript_revision,- webui_transcript_segments_dir,-)+from nanobot.webui.history import fact_from_record+from nanobot.webui.transcript import WEBUI_TRANSCRIPT_SCHEMA_VERSION, TranscriptReplayStats-def test_append_and_read_roundtrip(tmp_path, monkeypatch) -> None:+def test_append_and_read_roundtrip(history, tmp_path, monkeypatch) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:t1"- append_transcript_object(key, {"event": "user", "chat_id": "t1", "text": "hello"})- lines = read_transcript_lines(key)+ history.append(key, {"event": "user", "chat_id": "t1", "text": "hello"})+ lines = history.read(key)assert len(lines) == 1assert lines[0]["text"] == "hello"-def test_append_stamps_created_at_ms(tmp_path, monkeypatch) -> None:+def test_append_stamps_created_at_ms(history, tmp_path, monkeypatch) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)monkeypatch.setattr("nanobot.webui.transcript.time.time", lambda: 1_700_000_000.0)key = "websocket:t-created-at"- append_transcript_object(key, {"event": "user", "chat_id": "t-created-at", "text": "hello"})+ history.append(key, {"event": "user", "chat_id": "t-created-at", "text": "hello"})- lines = read_transcript_lines(key)+ lines = history.read(key)assert lines[0]["created_at_ms"] == 1_700_000_000_000-def _force_small_transcript_budget(monkeypatch, *, limit: int = 520, target: int = 260) -> None:- monkeypatch.setattr("nanobot.webui.transcript._MAX_TRANSCRIPT_FILE_BYTES", limit)- monkeypatch.setattr("nanobot.webui.transcript._ACTIVE_TRANSCRIPT_ROTATE_BYTES", limit)- monkeypatch.setattr("nanobot.webui.transcript._TARGET_ACTIVE_TRANSCRIPT_BYTES", target)---def _append_numbered_turn(key: str, chat_id: str, idx: int) -> None:- append_transcript_object(+def _append_numbered_turn(history, key: str, chat_id: str, idx: int) -> None:+ history.append(key,{"event": "user", "chat_id": chat_id, "text": f"question {idx} " + ("x" * 24)},)- append_transcript_object(+ history.append(key,{"event": "message", "chat_id": chat_id, "text": f"answer {idx} " + ("y" * 24)},)- append_transcript_object(key, {"event": "turn_end", "chat_id": chat_id})+ history.append(key, {"event": "turn_end", "chat_id": chat_id})-def _write_segmented_turns(tmp_path, monkeypatch, key: str, chat_id: str, count: int) -> None:+def _write_history_turns(history, tmp_path, monkeypatch, key: str, chat_id: str, count: int) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)- _force_small_transcript_budget(monkeypatch)for idx in range(1, count + 1):- _append_numbered_turn(key, chat_id, idx)+ _append_numbered_turn(history, key, chat_id, idx)def _conversation_events(payload: dict) -> list[dict]:@@ -103,33 +82,20 @@ def _numbered_turn_texts(start: int, end: int) -> list[str]:]-def test_segmented_transcript_rotation_preserves_full_history(tmp_path, monkeypatch) -> None:- key = "websocket:segmented"- _write_segmented_turns(tmp_path, monkeypatch, key, "segmented", 6)-- segment_dir = webui_transcript_segments_dir(key)- assert segment_dir.is_dir()- assert (segment_dir / "manifest.json").is_file()-- lines = read_transcript_lines(key)- contents = [str(line.get("text") or "") for line in lines if line.get("event") in {"user", "message"}]- assert contents == _numbered_turn_texts(1, 6)---def test_segmented_transcript_paginates_latest_and_older_without_overlap(+def test_sqlite_history_paginates_latest_and_older_without_overlap(history,tmp_path,monkeypatch,) -> None:key = "websocket:paged"- _write_segmented_turns(tmp_path, monkeypatch, key, "paged", 6)+ _write_history_turns(history, tmp_path, monkeypatch, key, "paged", 6)- latest = build_webui_thread_response(key, limit=4, direction="latest")+ latest = history.build(key, limit=4, direction="latest")assert latest is not Noneassert latest["page"]["has_more_before"] is Trueassert latest["page"]["user_message_offset"] == 4assert _message_contents(latest) == _numbered_turn_texts(5, 6)- older = build_webui_thread_response(+ older = history.build(key,limit=4,before=latest["page"]["before_cursor"],@@ -138,8 +104,8 @@ def test_segmented_transcript_paginates_latest_and_older_without_overlap(assert older["page"]["user_message_offset"] == 2assert _message_contents(older) == _numbered_turn_texts(3, 4)- latest_again = build_webui_thread_response(key, limit=4, direction="latest")- full = build_webui_thread_response(key)+ latest_again = history.build(key, limit=4, direction="latest")+ full = history.build(key)assert latest_again is not Noneassert full is not Noneassert [message["id"] for message in _conversation_events(latest_again)] == [@@ -156,14 +122,14 @@ def test_segmented_transcript_paginates_latest_and_older_without_overlap(]-def test_missing_page_query_uses_bounded_default(tmp_path, monkeypatch) -> None:+def test_missing_page_query_uses_bounded_default(history, tmp_path, monkeypatch) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)monkeypatch.setattr("nanobot.webui.transcript._DEFAULT_TRANSCRIPT_PAGE_LIMIT", 4)key = "websocket:bounded-default"for idx in range(1, 5):- _append_numbered_turn(key, "bounded-default", idx)+ _append_numbered_turn(history, key, "bounded-default", idx)- latest = build_webui_thread_response(key)+ latest = history.build(key)assert latest is not Noneassert _message_contents(latest) == _numbered_turn_texts(3, 4)@@ -171,7 +137,7 @@ def test_missing_page_query_uses_bounded_default(tmp_path, monkeypatch) -> None:assert latest["page"]["loaded_event_count"] == 6-def test_large_trace_details_are_deferred_and_resolved(tmp_path, monkeypatch) -> None:+def test_large_trace_details_are_deferred_and_resolved(history, tmp_path, monkeypatch) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:deferred-trace"trace = f'exec({json.dumps({"command": "x" * 40_000})})'@@ -186,9 +152,9 @@ def test_large_trace_details_are_deferred_and_resolved(tmp_path, monkeypatch) ->{"event": "message", "chat_id": "deferred-trace", "text": "done"},{"event": "turn_end", "chat_id": "deferred-trace"},):- append_transcript_object(key, event)+ history.append(key, event)- payload = build_webui_thread_response(key, limit=40, direction="latest")+ payload = history.build(key, limit=40, direction="latest")assert payload is not Nonetrace_event = next(event for event in payload["events"] if event.get("kind") == "progress")@@ -196,7 +162,7 @@ def test_large_trace_details_are_deferred_and_resolved(tmp_path, monkeypatch) ->assert trace_event["trace_detail"]["bytes"] > 32 * 1024assert len(json.dumps(payload)) < len(trace)- detail = build_webui_trace_detail_response(key, trace_event["trace_detail"]["ref"])+ detail = history.trace(key, trace_event["trace_detail"]["ref"])assert detail is not Noneassert detail["message_id"] == trace_event["projection_id"]@@ -204,9 +170,7 @@ def test_large_trace_details_are_deferred_and_resolved(tmp_path, monkeypatch) ->assert "trace_detail" not in detail["events"][0]---def test_event_projection_augments_complete_stream_text(tmp_path, monkeypatch) -> None:+def test_event_projection_augments_complete_stream_text(history, tmp_path, monkeypatch) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:project-stream-media"for event in (@@ -221,9 +185,9 @@ def test_event_projection_augments_complete_stream_text(tmp_path, monkeypatch) -"text": "",},):- append_transcript_object(key, event)+ history.append(key, event)- payload = build_webui_thread_response(+ payload = history.build(key,augment_assistant_text=lambda text: text.replace("output.png", "/api/media/signed"),)@@ -235,7 +199,7 @@ def test_event_projection_augments_complete_stream_text(tmp_path, monkeypatch) -]-def test_event_projection_defers_trace_details_without_legacy_fallback(+def test_event_projection_defers_trace_details_without_legacy_fallback(history,tmp_path, monkeypatch,) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)@@ -250,11 +214,11 @@ def test_event_projection_defers_trace_details_without_legacy_fallback(},{"event": "turn_end", "chat_id": "event-trace-fallback"},):- append_transcript_object(key, event)+ history.append(key, event)trace = f'exec({json.dumps({"command": "x" * 40_000})})'- payload = build_webui_thread_response(key)+ payload = history.build(key)assert payload is not Noneassert "messages" not in payload@@ -265,7 +229,7 @@ def test_event_projection_defers_trace_details_without_legacy_fallback(assert trace_event["trace_detail"]["traceCount"] == 1assert len(json.dumps(payload)) < len(trace)- detail = build_webui_trace_detail_response(key, trace_event["trace_detail"]["ref"])+ detail = history.trace(key, trace_event["trace_detail"]["ref"])assert detail is not Noneassert detail["message_id"] == trace_event["projection_id"]@@ -277,7 +241,7 @@ def test_event_projection_defers_trace_details_without_legacy_fallback(("delta_event", "end_event"),[("reasoning_delta", "reasoning_end"), ("delta", "stream_end")],)-def test_active_trace_details_match_compacted_event_page(+def test_active_trace_details_match_compacted_event_page(history,tmp_path, monkeypatch, delta_event: str, end_event: str,) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)@@ -294,12 +258,11 @@ def test_active_trace_details_match_compacted_event_page({"event": "message", "text": "done"},{"event": "turn_end"},]- path = transcript_module.webui_transcript_path(key)- path.parent.mkdir(parents=True, exist_ok=True)- path.write_text("".join(json.dumps(row) + "\n" for row in rows), encoding="utf-8")- original_bytes = path.read_bytes()+ for record in rows:+ history.append(key, record)+ original_rows = history.read(key)- payload = build_webui_thread_response(key, limit=40, direction="latest")+ payload = history.build(key, limit=40, direction="latest")assert payload is not Nonetrace_events = [@@ -308,14 +271,14 @@ def test_active_trace_details_match_compacted_event_page(]assert len(trace_events) == 2for event, trace in zip(trace_events, traces, strict=True):- detail = build_webui_trace_detail_response(key, event["trace_detail"]["ref"])+ detail = history.trace(key, event["trace_detail"]["ref"])assert detail is not Noneassert detail["message_id"] == event["projection_id"]assert detail["events"][0]["text"] == trace- assert path.read_bytes() == original_bytes+ assert history.read(key) == original_rows-def test_large_structured_trace_error_is_bounded_and_resolved(tmp_path, monkeypatch) -> None:+def test_large_structured_trace_error_is_bounded_and_resolved(history, tmp_path, monkeypatch) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:deferred-trace-error"full_error = "failure: " + "界" * 40_000@@ -338,16 +301,16 @@ def test_large_structured_trace_error_is_bounded_and_resolved(tmp_path, monkeypa{"event": "message", "chat_id": "deferred-trace-error", "text": "done"},{"event": "turn_end", "chat_id": "deferred-trace-error"},):- append_transcript_object(key, event)+ history.append(key, event)- payload = build_webui_thread_response(key, limit=40, direction="latest")+ payload = history.build(key, limit=40, direction="latest")assert payload is not Nonetrace_event = next(event for event in payload["events"] if event.get("kind") == "progress")assert len(json.dumps(trace_event).encode("utf-8")) < 4_096- event_detail = build_webui_trace_detail_response(+ event_detail = history.trace(key,trace_event["trace_detail"]["ref"],)@@ -355,7 +318,7 @@ def test_large_structured_trace_error_is_bounded_and_resolved(tmp_path, monkeypaassert event_detail["events"][0]["tool_events"][0]["error"] == full_error-def test_many_short_trace_rows_are_bounded_and_resolved(tmp_path, monkeypatch) -> None:+def test_many_short_trace_rows_are_bounded_and_resolved(history, tmp_path, monkeypatch) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:deferred-many-traces"trace_count = 2_000@@ -374,19 +337,19 @@ def test_many_short_trace_rows_are_bounded_and_resolved(tmp_path, monkeypatch) -{"event": "turn_end", "chat_id": "deferred-many-traces"},]# Replay needs the full stored input, not one durable append per trace row.- transcript_module._write_records_to_path(- transcript_module.webui_transcript_path(key),- [{**row, "created_at_ms": 1_700_000_000_000} for row in rows],+ history.sessions.append_history(+ key,+ [fact_from_record({**row, "created_at_ms": 1_700_000_000_000}) for row in rows],)- payload = build_webui_thread_response(key, limit=40, direction="latest")+ payload = history.build(key, limit=40, direction="latest")assert payload is not Nonetrace_event = next(event for event in payload["events"] if event.get("kind") == "progress")assert trace_event["trace_detail"]["traceCount"] == trace_countassert len(json.dumps(trace_event).encode("utf-8")) < 4_096- detail = build_webui_trace_detail_response(key, trace_event["trace_detail"]["ref"])+ detail = history.trace(key, trace_event["trace_detail"]["ref"])assert detail is not Noneassert len(detail["events"]) == trace_count@@ -394,16 +357,16 @@ def test_many_short_trace_rows_are_bounded_and_resolved(tmp_path, monkeypatch) -assert detail["events"][-1]["text"] == f"tool_{trace_count - 1}(value)"-def test_transcript_revision_tracks_artifacts_and_variants(tmp_path, monkeypatch) -> None:+def test_transcript_revision_tracks_artifacts_and_variants(history, tmp_path, monkeypatch) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:revision"- append_transcript_object(key, {"event": "user", "chat_id": "revision", "text": "one"})+ history.append(key, {"event": "user", "chat_id": "revision", "text": "one"})- first = webui_transcript_revision(key, variant={"limit": 40})- same = webui_transcript_revision(key, variant={"limit": 40})- other_variant = webui_transcript_revision(key, variant={"limit": 80})- append_transcript_object(key, {"event": "message", "chat_id": "revision", "text": "two"})- changed = webui_transcript_revision(key, variant={"limit": 40})+ first = history.revision(key, variant={"limit": 40})+ same = history.revision(key, variant={"limit": 40})+ other_variant = history.revision(key, variant={"limit": 80})+ history.append(key, {"event": "message", "chat_id": "revision", "text": "two"})+ changed = history.revision(key, variant={"limit": 40})assert first is not Noneassert same == first@@ -411,17 +374,17 @@ def test_transcript_revision_tracks_artifacts_and_variants(tmp_path, monkeypatchassert changed != first-def test_single_oversized_turn_is_bounded_and_marked(tmp_path, monkeypatch) -> None:+def test_single_oversized_turn_is_bounded_and_marked(history, tmp_path, monkeypatch) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)monkeypatch.setattr("nanobot.webui.transcript._MAX_TRANSCRIPT_PAGE_RECORDS", 5)monkeypatch.setattr("nanobot.webui.transcript._MAX_TRANSCRIPT_PAGE_BYTES", 1_200)key = "websocket:oversized-turn"- append_transcript_object(+ history.append(key,{"event": "user", "chat_id": "oversized-turn", "text": "question"},)for idx in range(20):- append_transcript_object(+ history.append(key,{"event": "message",@@ -430,14 +393,14 @@ def test_single_oversized_turn_is_bounded_and_marked(tmp_path, monkeypatch) -> N"text": f"progress {idx} " + ("x" * 160),},)- append_transcript_object(+ history.append(key,{"event": "message", "chat_id": "oversized-turn", "text": "final answer"},)- append_transcript_object(key, {"event": "turn_end", "chat_id": "oversized-turn"})+ history.append(key, {"event": "turn_end", "chat_id": "oversized-turn"})stats = TranscriptReplayStats()- latest = build_webui_thread_response(key, stats=stats)+ latest = history.build(key, stats=stats)assert latest is not Noneassert stats.selected_records <= 5@@ -449,7 +412,7 @@ def test_single_oversized_turn_is_bounded_and_marked(tmp_path, monkeypatch) -> Nassert _message_contents(latest) == ["question", "final answer"]-def test_truncated_turn_does_not_advertise_ambiguous_trace_detail(+def test_truncated_turn_does_not_advertise_ambiguous_trace_detail(history,tmp_path,monkeypatch,) -> None:@@ -457,13 +420,13 @@ def test_truncated_turn_does_not_advertise_ambiguous_trace_detail(monkeypatch.setattr("nanobot.webui.transcript._MAX_TRANSCRIPT_PAGE_RECORDS", 6)monkeypatch.setattr("nanobot.webui.transcript._MAX_TRANSCRIPT_PAGE_BYTES", 1_000_000)key = "websocket:truncated-trace-detail"- append_transcript_object(+ history.append(key,{"event": "user", "chat_id": "truncated-trace-detail", "text": "question"},)for prefix in ("first", "second"):for idx in range(4):- append_transcript_object(+ history.append(key,{"event": "message",@@ -473,7 +436,7 @@ def test_truncated_turn_does_not_advertise_ambiguous_trace_detail(},)if prefix == "first":- append_transcript_object(+ history.append(key,{"event": "message",@@ -481,16 +444,16 @@ def test_truncated_turn_does_not_advertise_ambiguous_trace_detail("text": "intermediate",},)- append_transcript_object(+ history.append(key,{"event": "message", "chat_id": "truncated-trace-detail", "text": "done"},)- append_transcript_object(+ history.append(key,{"event": "turn_end", "chat_id": "truncated-trace-detail"},)- payload = build_webui_thread_response(key, limit=40, direction="latest")+ payload = history.build(key, limit=40, direction="latest")assert payload is not Noneassert payload["page"]["truncated_oversized_turn"] is True@@ -500,314 +463,40 @@ def test_truncated_turn_does_not_advertise_ambiguous_trace_detail(assert len(json.dumps(trace_event).encode("utf-8")) < 4_096-def test_latest_page_reads_active_chunk_once(tmp_path, monkeypatch) -> None:- monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)- key = "websocket:single-active-read"- for idx in range(1, 7):- _append_numbered_turn(key, "single-active-read", idx)-- original = transcript_module._read_chunk_turns- read_chunk_ids: list[str] = []-- def track_read(session_key: str, chunk_id: str) -> list[list[dict]]:- read_chunk_ids.append(chunk_id)- return original(session_key, chunk_id)-- monkeypatch.setattr(transcript_module, "_read_chunk_turns", track_read)-- latest = build_webui_thread_response(key, limit=4, direction="latest")-- assert latest is not None- assert _message_contents(latest) == _numbered_turn_texts(5, 6)- assert read_chunk_ids == ["active"]---def test_cold_complete_page_does_not_parse_unselected_transcript_files(tmp_path, monkeypatch) -> None:- key = "websocket:cold-bounded-page"- _write_segmented_turns(tmp_path, monkeypatch, key, "cold-bounded-page", 12)- original = transcript_module._read_transcript_file- reads: list[Path] = []-- def track_read(path):- reads.append(path)- return original(path)-- def fail_if_loaded():- raise AssertionError("complete pages must not load canonical history")-- monkeypatch.setattr(transcript_module, "_read_transcript_file", track_read)- latest = build_webui_thread_response(key, limit=1, session_messages_loader=fail_if_loaded)-- assert latest is not None- assert _message_contents(latest) == _numbered_turn_texts(12, 12)- assert reads == [transcript_module.webui_transcript_path(key)]- transcript_module.build_prompt_index(key, session_messages_loader=fail_if_loaded)- reads.clear()- warmed = build_webui_thread_response(key, limit=1, session_messages_loader=fail_if_loaded)- assert warmed == latest- assert reads == [transcript_module.webui_transcript_path(key)]---def test_page_cursor_survives_active_rotation_after_latest_page(+def test_page_cursor_survives_appends_after_latest_page(history,tmp_path,monkeypatch,) -> None:key = "websocket:stable-cursor"- _write_segmented_turns(tmp_path, monkeypatch, key, "stable-cursor", 7)+ _write_history_turns(history, tmp_path, monkeypatch, key, "stable-cursor", 7)- latest = build_webui_thread_response(key, limit=4, direction="latest")+ latest = history.build(key, limit=4, direction="latest")assert latest is not Nonecursor = latest["page"]["before_cursor"]assert cursorassert _message_contents(latest) == _numbered_turn_texts(6, 7)for idx in range(8, 13):- _append_numbered_turn(key, "stable-cursor", idx)+ _append_numbered_turn(history, key, "stable-cursor", idx)- older = build_webui_thread_response(key, limit=4, before=cursor)+ older = history.build(key, limit=4, before=cursor)assert older is not Noneassert _message_contents(older) == _numbered_turn_texts(4, 5)-def test_segment_manifest_can_be_rebuilt_when_missing_or_corrupt(tmp_path, monkeypatch) -> None:- key = "websocket:manifest"- _write_segmented_turns(tmp_path, monkeypatch, key, "manifest", 4)-- segment_dir = webui_transcript_segments_dir(key)- segment_names = sorted(path.name for path in segment_dir.glob("*.jsonl"))- assert segment_names- original = transcript_module._read_transcript_file- segment_reads: list[str] = []-- def track_read(path):- if path.parent == segment_dir and path.suffix == ".jsonl":- segment_reads.append(path.name)- return original(path)-- monkeypatch.setattr(transcript_module, "_read_transcript_file", track_read)- manifest = segment_dir / "manifest.json"- manifest.write_text("{not json", encoding="utf-8")-- entries = transcript_module._read_segment_manifest_entries(key)-- assert [entry["id"] for entry in entries] == [path.removesuffix(".jsonl") for path in segment_names]- assert segment_reads == segment_names-- lines = read_transcript_lines(key)-- assert len([line for line in lines if line.get("event") == "user"]) == 4- assert manifest.read_text(encoding="utf-8").lstrip().startswith("{")---def test_manifest_repair_is_single_flight(tmp_path, monkeypatch) -> None:- key = "websocket:manifest-single-flight"- _write_segmented_turns(tmp_path, monkeypatch, key, "manifest-single-flight", 4)- manifest = webui_transcript_segments_dir(key) / "manifest.json"- manifest.write_text("{not json", encoding="utf-8")- original = transcript_module._rebuild_segment_manifest- original_read_text = Path.read_text- original_replace = transcript_module.os.replace- manifest_lock = transcript_module._manifest_rebuild_lock(key)- rebuild_started = Event()- second_attempt = Event()- manifest_open = Event()- release_read = Event()- calls = 0- calls_lock = Lock()-- @contextmanager- def track_lock(_session_key):- if rebuild_started.is_set():- second_attempt.set()- with manifest_lock:- yield-- def hold_manifest_read(path, *args, **kwargs):- if path != manifest or not rebuild_started.is_set() or release_read.is_set():- return original_read_text(path, *args, **kwargs)- with path.open(encoding="utf-8") as handle:- manifest_open.set()- second_attempt.set()- try:- assert release_read.wait(5)- return handle.read()- finally:- manifest_open.clear()-- def replace_manifest(source, destination):- if destination != manifest:- return original_replace(source, destination)- try:- # Windows cannot replace a file held open by a concurrent reader.- if manifest_open.is_set():- raise PermissionError("manifest is open for reading")- return original_replace(source, destination)- finally:- release_read.set()-- def slow_rebuild(*args, **kwargs):- nonlocal calls- with calls_lock:- calls += 1- rebuild_started.set()- assert second_attempt.wait(5)- return original(*args, **kwargs)-- monkeypatch.setattr(transcript_module, "_manifest_rebuild_lock", track_lock)- monkeypatch.setattr(Path, "read_text", hold_manifest_read)- monkeypatch.setattr(transcript_module.os, "replace", replace_manifest)- monkeypatch.setattr(transcript_module, "_rebuild_segment_manifest", slow_rebuild)- with ThreadPoolExecutor(max_workers=2) as executor:- first = executor.submit(transcript_module._read_segment_manifest_entries, key)- assert rebuild_started.wait(5)- second = executor.submit(transcript_module._read_segment_manifest_entries, key)- assert first.result(timeout=5) == second.result(timeout=5)-- assert calls == 1---def test_page_read_compacts_legacy_immutable_segment(tmp_path, monkeypatch) -> None:- monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)- key = "websocket:compact-segment"- segment_id = "000001"- rows = [- {"event": "user", "chat_id": "compact-segment", "text": "q"},- {- "event": "delta",- "chat_id": "compact-segment",- "stream_id": "answer-1",- "text": "answer ",- },- {- "event": "delta",- "chat_id": "compact-segment",- "stream_id": "answer-1",- "text": "done",- },- {- "event": "stream_end",- "chat_id": "compact-segment",- "stream_id": "answer-1",- },- {"event": "turn_end", "chat_id": "compact-segment"},- ]- segment_path = transcript_module._segment_file_path(key, segment_id)- transcript_module._write_records_to_path(segment_path, rows)- transcript_module._write_segment_manifest(- key,- [transcript_module._segment_manifest_entry(key, segment_id)],- )- stats = TranscriptReplayStats()-- latest = build_webui_thread_response(key, stats=stats)- persisted = transcript_module._read_transcript_file(segment_path)-- assert latest is not None- assert _message_contents(latest) == ["q", "answer done"]- assert [record["event"] for record in persisted] == [- "user",- "stream_end",- "turn_end",- ]- assert stats.compacted_delta_records == 2- assert transcript_module._load_segment_manifest_entries(key) is not None---def test_rotation_does_not_reread_existing_segments(tmp_path, monkeypatch) -> None:- key = "websocket:manifest-append"- _write_segmented_turns(tmp_path, monkeypatch, key, "manifest-append", 4)- segment_dir = webui_transcript_segments_dir(key)- assert list(segment_dir.glob("*.jsonl"))-- original = transcript_module._read_transcript_file- segment_reads: list[str] = []-- def track_read(path):- if path.parent == segment_dir and path.suffix == ".jsonl":- segment_reads.append(path.name)- return original(path)-- monkeypatch.setattr(transcript_module, "_read_transcript_file", track_read)- for idx in range(5, 9):- _append_numbered_turn(key, "manifest-append", idx)-- assert segment_reads == []---def test_delete_webui_transcript_removes_segments(tmp_path, monkeypatch) -> None:- from nanobot.webui.thread_disk import webui_thread_file_path- from nanobot.webui.transcript import delete_webui_transcript, webui_transcript_path-- key = "websocket:delete-segments"- _write_segmented_turns(tmp_path, monkeypatch, key, "delete-segments", 4)- legacy_path = webui_thread_file_path(key)- legacy_path.parent.mkdir(parents=True, exist_ok=True)- legacy_path.write_text('{"messages":[]}', encoding="utf-8")-- assert webui_transcript_segments_dir(key).is_dir()- assert delete_webui_transcript(key) is True- assert not legacy_path.exists()- assert not webui_transcript_path(key).exists()- assert not webui_transcript_segments_dir(key).exists()---def test_fork_transcript_reads_across_segments(tmp_path, monkeypatch) -> None:- source = "websocket:seg-source"- _write_segmented_turns(tmp_path, monkeypatch, source, "seg-source", 5)-- ok = fork_transcript_before_user_index(source, "websocket:seg-fork", 3)-- assert ok is True- forked = build_webui_thread_response("websocket:seg-fork")- assert forked is not None- assert _message_contents(forked) == _numbered_turn_texts(1, 3)---def test_fork_transcript_before_user_index_copies_only_prefix(tmp_path, monkeypatch) -> None:- monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)- source = "websocket:source"- for ev in (- {"event": "user", "chat_id": "source", "text": "round1"},- {"event": "message", "chat_id": "source", "text": "answer1"},- {"event": "turn_end", "chat_id": "source"},- {"event": "user", "chat_id": "source", "text": "round2 fork me"},- {"event": "message", "chat_id": "source", "text": "answer2"},- {"event": "user", "chat_id": "source", "text": "round3 must not appear"},- ):- append_transcript_object(source, ev)-- ok = fork_transcript_before_user_index(source, "websocket:fork", 1)-- assert ok is True- lines = read_transcript_lines("websocket:fork")- assert [line.get("text") for line in lines] == ["round1", "answer1", None]- assert all(line.get("chat_id") == "fork" for line in lines)- assert "round2 fork me" not in "\n".join(str(line.get("text")) for line in lines)- assert "round3 must not appear" not in "\n".join(str(line.get("text")) for line in lines)---def test_fork_transcript_rejects_out_of_range_user_index(tmp_path, monkeypatch) -> None:- monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)- source = "websocket:source"- append_transcript_object(source, {"event": "user", "chat_id": "source", "text": "round1"})-- assert fork_transcript_before_user_index(source, "websocket:fork", 2) is False- assert read_transcript_lines("websocket:fork") == []---def test_build_response_reports_fork_boundary_from_marker(tmp_path, monkeypatch) -> None:+def test_build_response_reports_fork_boundary_from_marker(history, tmp_path, monkeypatch) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:fork"for ev in ({"event": "user", "chat_id": "fork", "text": "round1"},{"event": "message", "chat_id": "fork", "text": "answer1"},):- append_transcript_object(key, ev)- append_fork_marker(key)- append_transcript_object(key, {"event": "user", "chat_id": "fork", "text": "new branch"})+ history.append(key, ev)+ history.append(key, {"event": "fork_marker"})+ history.append(key, {"event": "user", "chat_id": "fork", "text": "new branch"})- out = build_webui_thread_response(key)+ out = history.build(key)assert out is not Noneassert [m["content"] for m in _conversation_events(out)] == [@@ -816,88 +505,7 @@ def test_build_response_reports_fork_boundary_from_marker(tmp_path, monkeypatch)assert out["fork_boundary_event_index"] == 2-def test_nested_fork_drops_inherited_fork_marker(tmp_path, monkeypatch) -> None:- monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)- source = "websocket:source"- for ev in (- {"event": "user", "chat_id": "source", "text": "round1"},- {"event": "message", "chat_id": "source", "text": "answer1"},- ):- append_transcript_object(source, ev)- append_fork_marker(source)- for ev in (- {"event": "user", "chat_id": "source", "text": "round2"},- {"event": "message", "chat_id": "source", "text": "answer2"},- ):- append_transcript_object(source, ev)-- ok = fork_transcript_before_user_index(source, "websocket:nested", 2)- append_fork_marker("websocket:nested")-- lines = read_transcript_lines("websocket:nested")- out = build_webui_thread_response("websocket:nested")-- assert ok is True- assert [line.get("event") for line in lines] == [- "user",- "message",- "user",- "message",- "fork_marker",- ]- assert out is not None- assert [m["content"] for m in _conversation_events(out)] == [- "round1", "answer1", "round2", "answer2",- ]- assert out["fork_boundary_event_index"] == 4---------def test_completed_turn_persists_canonical_stream_end_text(tmp_path, monkeypatch) -> None:- monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)- key = "websocket:compact-deltas"- for event in (- {"event": "user", "chat_id": "compact-deltas", "text": "q"},- {"event": "reasoning_delta", "chat_id": "compact-deltas", "text": "think "},- {"event": "reasoning_delta", "chat_id": "compact-deltas", "text": "more"},- {"event": "reasoning_end", "chat_id": "compact-deltas"},- {"event": "delta", "chat_id": "compact-deltas", "text": "answer "},- {"event": "delta", "chat_id": "compact-deltas", "text": "done"},- {"event": "stream_end", "chat_id": "compact-deltas"},- {"event": "turn_end", "chat_id": "compact-deltas"},- ):- append_transcript_object(key, event)-- lines = read_transcript_lines(key)-- assert [record["event"] for record in lines] == [- "user",- "reasoning_end",- "stream_end",- "turn_end",- ]- assert lines[1]["text"] == "think more"- assert lines[2]["text"] == "answer done"---------------def test_thread_response_does_not_mark_completed_message_tool_tail_pending(+def test_thread_response_does_not_mark_completed_message_tool_tail_pending(history,tmp_path,monkeypatch,) -> None:@@ -953,9 +561,9 @@ def test_thread_response_does_not_mark_completed_message_tool_tail_pending("turn_seq": 8,},):- append_transcript_object(key, ev)+ history.append(key, ev)- out = build_webui_thread_response(key)+ out = history.build(key)assert out is not Noneassert out["has_pending_tool_calls"] is False@@ -964,10 +572,10 @@ def test_thread_response_does_not_mark_completed_message_tool_tail_pending(assert _conversation_events(out)[-1]["content"] == "Cron test"-def test_thread_response_marks_unfinished_tool_tail_pending(tmp_path, monkeypatch) -> None:+def test_thread_response_marks_unfinished_tool_tail_pending(history, tmp_path, monkeypatch) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:active-tail"- append_transcript_object(+ history.append(key,{"event": "message",@@ -977,36 +585,20 @@ def test_thread_response_marks_unfinished_tool_tail_pending(tmp_path, monkeypatc},)- out = build_webui_thread_response(key)+ out = history.build(key)assert out is not Noneassert out["has_pending_tool_calls"] is Trueassert out["completed_turn_ids"] == []-def test_recovery_tail_check_reads_only_the_active_transcript(tmp_path, monkeypatch) -> None:- monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)- key = "websocket:recovery-tail"- active_path = transcript_module.webui_transcript_path(key)- reads: list[Path] = []-- def read(path: Path) -> list[dict[str, object]]:- reads.append(path)- return [{"event": "message", "kind": "progress", "text": "running"}]-- monkeypatch.setattr(transcript_module, "_read_transcript_file", read)-- assert transcript_module.has_unfinished_transcript_tail(key) is True- assert reads == [active_path]---def test_thread_response_reports_active_registry_without_transcript(+def test_thread_response_reports_active_registry_without_transcript(history,tmp_path,monkeypatch,) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)- out = build_webui_thread_response(+ out = history.build("websocket:active-without-transcript",active_turn_started_at=1_700_000_000.0,active_turn_id="turn-active",@@ -1019,23 +611,23 @@ def test_thread_response_reports_active_registry_without_transcript(assert out["active_turn_id"] == "turn-active"-def test_thread_response_reports_explicit_completion_without_assistant_row(+def test_thread_response_reports_explicit_completion_without_assistant_row(history,tmp_path,monkeypatch,) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:empty-answer"turn_id = "turn-empty-answer"- append_transcript_object(+ history.append(key,{"event": "user", "chat_id": "empty-answer", "text": "stop", "turn_id": turn_id},)- append_transcript_object(+ history.append(key,{"event": "turn_end", "chat_id": "empty-answer", "turn_id": turn_id},)- out = build_webui_thread_response(key)+ out = history.build(key)assert out is not Noneassert _conversation_events(out)[-1]["role"] == "user"@@ -1043,14 +635,14 @@ def test_thread_response_reports_explicit_completion_without_assistant_row(assert out["completed_turn_ids"] == [turn_id]-def test_incomplete_turn_with_ambiguous_session_match_stays_pending(+def test_incomplete_turn_with_ambiguous_session_match_stays_pending(history,tmp_path,monkeypatch,) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:ambiguous-incomplete"turn_id = "turn-ambiguous"- append_transcript_object(+ history.append(key,{"event": "user",@@ -1059,7 +651,7 @@ def test_incomplete_turn_with_ambiguous_session_match_stays_pending("turn_id": turn_id,},)- append_transcript_object(+ history.append(key,{"event": "turn_end",@@ -1069,7 +661,7 @@ def test_incomplete_turn_with_ambiguous_session_match_stays_pending(},)- out = build_webui_thread_response(+ out = history.build(key,session_messages=[{"role": "user", "content": "repeat"},@@ -1090,7 +682,7 @@ def test_incomplete_turn_with_ambiguous_session_match_stays_pending(assert out["has_pending_tool_calls"] is True-def test_later_completion_does_not_hide_older_incomplete_turn(+def test_later_completion_does_not_hide_older_incomplete_turn(history,tmp_path,monkeypatch,) -> None:@@ -1107,19 +699,19 @@ def test_later_completion_does_not_hide_older_incomplete_turn({"event": "message", "text": "second answer", "turn_id": "turn-second"},{"event": "turn_end", "turn_id": "turn-second"},):- append_transcript_object(+ history.append(key,{"chat_id": "older-incomplete", **event},)- out = build_webui_thread_response(key)+ out = history.build(key)assert out is not Noneassert out["completed_turn_ids"] == ["turn-second"]assert out["has_pending_tool_calls"] is True-def test_active_registry_does_not_hide_a_newer_queued_turn(tmp_path, monkeypatch) -> None:+def test_active_registry_does_not_hide_a_newer_queued_turn(history, tmp_path, monkeypatch) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:queued-tail"for event in (@@ -1128,9 +720,9 @@ def test_active_registry_does_not_hide_a_newer_queued_turn(tmp_path, monkeypatch{"event": "turn_end", "turn_id": "turn-old"},{"event": "user", "text": "queued next", "turn_id": "turn-new"},):- append_transcript_object(key, {"chat_id": "queued-tail", **event})+ history.append(key, {"chat_id": "queued-tail", **event})- out = build_webui_thread_response(+ out = history.build(key,active_turn_started_at=1_700_000_000.0,active_turn_id="turn-old",@@ -1140,34 +732,24 @@ def test_active_registry_does_not_hide_a_newer_queued_turn(tmp_path, monkeypatchassert out["has_pending_tool_calls"] is True-----------def test_build_response_restores_session_users_for_legacy_transcript(+def test_build_response_restores_session_users_for_legacy_transcript(history,tmp_path,monkeypatch,) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:legacy-users"- append_transcript_object(+ history.append(key,{"event": "message", "chat_id": "legacy-users", "text": "assistant one"},)- append_transcript_object(key, {"event": "turn_end", "chat_id": "legacy-users"})- append_transcript_object(+ history.append(key, {"event": "turn_end", "chat_id": "legacy-users"})+ history.append(key,{"event": "message", "chat_id": "legacy-users", "text": "assistant two"},)- append_transcript_object(key, {"event": "turn_end", "chat_id": "legacy-users"})+ history.append(key, {"event": "turn_end", "chat_id": "legacy-users"})- out = build_webui_thread_response(+ out = history.build(key,session_messages=[{"role": "user", "content": "prompt one", "timestamp": "2026-06-02T10:00:00"},@@ -1186,7 +768,7 @@ def test_build_response_restores_session_users_for_legacy_transcript(]-def test_complete_transcript_does_not_load_session_messages(tmp_path, monkeypatch) -> None:+def test_complete_transcript_does_not_load_session_messages(history, tmp_path, monkeypatch) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:complete-fast-path"for event in (@@ -1194,12 +776,12 @@ def test_complete_transcript_does_not_load_session_messages(tmp_path, monkeypatc{"event": "message", "chat_id": "complete-fast-path", "text": "answer"},{"event": "turn_end", "chat_id": "complete-fast-path"},):- append_transcript_object(key, event)+ history.append(key, event)def fail_if_loaded() -> list[dict]:raise AssertionError("complete transcripts must not read canonical session history")- out = build_webui_thread_response(+ out = history.build(key,limit=4,direction="latest",@@ -1216,17 +798,17 @@ def test_complete_transcript_does_not_load_session_messages(tmp_path, monkeypatc]-def test_legacy_recovery_loads_session_and_builds_backfill_turns_once(+def test_legacy_recovery_loads_session_and_builds_backfill_turns_once(history,tmp_path,monkeypatch,) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:lazy-legacy-recovery"- append_transcript_object(+ history.append(key,{"event": "message", "chat_id": "lazy-legacy-recovery", "text": "answer"},)- append_transcript_object(+ history.append(key,{"event": "turn_end",@@ -1254,7 +836,7 @@ def test_legacy_recovery_loads_session_and_builds_backfill_turns_once(monkeypatch.setattr(transcript_module, "_session_backfill_turns", track_backfill)- out = build_webui_thread_response(key, session_messages_loader=load_session_messages)+ out = history.build(key, session_messages_loader=load_session_messages)assert out is not Noneassert loader_calls == 1@@ -1269,25 +851,25 @@ def test_legacy_recovery_loads_session_and_builds_backfill_turns_once(assert out["has_pending_tool_calls"] is False-def test_build_response_restores_session_users_without_duplicating_new_transcript_users(+def test_build_response_restores_session_users_without_duplicating_new_transcript_users(history,tmp_path,monkeypatch,) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:mixed-users"- append_transcript_object(+ history.append(key,{"event": "message", "chat_id": "mixed-users", "text": "old assistant"},)- append_transcript_object(key, {"event": "turn_end", "chat_id": "mixed-users"})- append_transcript_object(key, {"event": "user", "chat_id": "mixed-users", "text": "new prompt"})- append_transcript_object(+ history.append(key, {"event": "turn_end", "chat_id": "mixed-users"})+ history.append(key, {"event": "user", "chat_id": "mixed-users", "text": "new prompt"})+ history.append(key,{"event": "message", "chat_id": "mixed-users", "text": "new assistant"},)- append_transcript_object(key, {"event": "turn_end", "chat_id": "mixed-users"})+ history.append(key, {"event": "turn_end", "chat_id": "mixed-users"})- out = build_webui_thread_response(+ out = history.build(key,session_messages=[{"role": "user", "content": "old prompt"},@@ -1306,11 +888,7 @@ def test_build_response_restores_session_users_without_duplicating_new_transcrip]-----def test_build_response_backfills_legacy_sse_only_transcripts(tmp_path, monkeypatch) -> None:+def test_build_response_backfills_legacy_sse_only_transcripts(history, tmp_path, monkeypatch) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:t-legacy"for ev in (@@ -1320,9 +898,9 @@ def test_build_response_backfills_legacy_sse_only_transcripts(tmp_path, monkeypa{"event": "message", "chat_id": "t-legacy", "text": "second answer"},{"event": "turn_end", "chat_id": "t-legacy"},):- append_transcript_object(key, ev)+ history.append(key, ev)- out = build_webui_thread_response(+ out = history.build(key,session_messages=[{"role": "user", "content": "first question"},@@ -1347,7 +925,7 @@ def test_build_response_backfills_legacy_sse_only_transcripts(tmp_path, monkeypa]-def test_backfill_does_not_duplicate_existing_user_transcript(tmp_path, monkeypatch) -> None:+def test_backfill_does_not_duplicate_existing_user_transcript(history, tmp_path, monkeypatch) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:t-current"for ev in (@@ -1355,9 +933,9 @@ def test_backfill_does_not_duplicate_existing_user_transcript(tmp_path, monkeypa{"event": "message", "chat_id": "t-current", "text": "answer"},{"event": "turn_end", "chat_id": "t-current"},):- append_transcript_object(key, ev)+ history.append(key, ev)- out = build_webui_thread_response(+ out = history.build(key,session_messages=[{"role": "user", "content": "already stored"}],)@@ -1369,7 +947,7 @@ def test_backfill_does_not_duplicate_existing_user_transcript(tmp_path, monkeypaassert _conversation_events(out)[0]["content"] == "already stored"-def test_backfill_does_not_misalign_when_session_only_has_transcript_tail(+def test_backfill_does_not_misalign_when_session_only_has_transcript_tail(history,tmp_path,monkeypatch,) -> None:@@ -1381,9 +959,9 @@ def test_backfill_does_not_misalign_when_session_only_has_transcript_tail({"event": "message", "chat_id": "t-tail", "text": "tail answer"},{"event": "turn_end", "chat_id": "t-tail"},):- append_transcript_object(key, ev)+ history.append(key, ev)- out = build_webui_thread_response(+ out = history.build(key,session_messages=[{"role": "user", "content": "tail question"},@@ -1404,7 +982,7 @@ def test_backfill_does_not_misalign_when_session_only_has_transcript_tail(]-def test_backfill_skips_internal_subagent_results(tmp_path, monkeypatch) -> None:+def test_backfill_skips_internal_subagent_results(history, tmp_path, monkeypatch) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:t-subagent"for ev in (@@ -1413,7 +991,7 @@ def test_backfill_skips_internal_subagent_results(tmp_path, monkeypatch) -> None{"event": "message", "chat_id": "t-subagent", "text": "summary two"},{"event": "turn_end", "chat_id": "t-subagent"},):- append_transcript_object(key, ev)+ history.append(key, ev)legacy_raw = ("[Subagent 'legacy' completed successfully]\n\n"@@ -1421,7 +999,7 @@ def test_backfill_skips_internal_subagent_results(tmp_path, monkeypatch) -> None"Result:\nr\n\n""Summarize this naturally for the user.")- out = build_webui_thread_response(+ out = history.build(key,session_messages=[{"role": "user", "content": legacy_raw},@@ -1448,45 +1026,11 @@ def test_backfill_skips_internal_subagent_results(tmp_path, monkeypatch) -> None]-----------------------------------def test_build_response_schema(monkeypatch, tmp_path) -> None:+def test_build_response_schema(history, monkeypatch, tmp_path) -> None:monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:t3"- append_transcript_object(key, {"event": "user", "chat_id": "t3", "text": "x"})- out = build_webui_thread_response(key, augment_user_media=None)+ history.append(key, {"event": "user", "chat_id": "t3", "text": "x"})+ out = history.build(key, augment_user_media=None)assert out is not Noneassert out["schemaVersion"] == WEBUI_TRANSCRIPT_SCHEMA_VERSIONassert out["sessionKey"] == keydiff --git a/tests/utils/test_webui_turn_helpers.py b/tests/utils/test_webui_turn_helpers.pyindex 145aa956f..d56591a49 100644--- a/tests/utils/test_webui_turn_helpers.py+++ b/tests/utils/test_webui_turn_helpers.py@@ -310,7 +310,7 @@ async def test_session_input_is_projected_by_the_webui_coordinator("source_session_key": "websocket:source","target_session_key": "websocket:target",}- append_input = MagicMock()+ append_input = AsyncMock()monkeypatch.setattr(wth, "append_session_message_input", append_input)coordinator = wth.WebuiTurnCoordinator(bus=bus,@@ -324,12 +324,14 @@ async def test_session_input_is_projected_by_the_webui_coordinator(channel="system",chat_id="websocket:target",session_key="websocket:target",+ session_generation=target_session.generation,metadata={SESSION_MESSAGE_METADATA_KEY: envelope},),content="Review this",))- append_input.assert_called_once()+ append_input.assert_awaited_once()+ assert append_input.await_args.kwargs["expected_generation"] == target_session.generationoutbound = bus.publish_outbound.await_args.args[0]assert outbound.channel == "websocket"assert outbound.chat_id == "target"diff --git a/tests/webui/test_forking.py b/tests/webui/test_forking.pyindex fcede0a97..5c14f43fd 100644--- a/tests/webui/test_forking.py+++ b/tests/webui/test_forking.py@@ -8,81 +8,6 @@ import pytestimport nanobot.webui.forking as forking-async def test_create_fork_rebuilds_missing_transcript_and_saves_clean_title(- monkeypatch: pytest.MonkeyPatch,-) -> None:- forked = SimpleNamespace(messages=[{"role": "user", "content": "hello"}], metadata={})- manager = MagicMock()- manager.state.fork = AsyncMock(return_value=forked)- rebuild = MagicMock()- marker = MagicMock()- monkeypatch.setattr(forking.uuid, "uuid4", lambda: "fork-id")- monkeypatch.setattr(forking, "fork_transcript_before_user_index", lambda *_args: False)- monkeypatch.setattr(forking, "write_session_messages_as_transcript", rebuild)- monkeypatch.setattr(forking, "append_fork_marker", marker)-- result = await forking.create_webui_chat_fork(- manager,- source_chat_id="source",- before_user_index=2,- title=" Useful fork ",- )-- assert result == ("fork-id", "websocket:fork-id")- manager.state.fork.assert_awaited_once_with(- "websocket:source",- "websocket:fork-id",- 2,- title="Useful fork",- )- rebuild.assert_called_once_with("websocket:fork-id", forked.messages)- marker.assert_called_once_with("websocket:fork-id")---async def test_create_fork_rolls_back_session_and_transcript_together(- monkeypatch: pytest.MonkeyPatch,-) -> None:- manager = MagicMock()- manager.state.fork = AsyncMock(return_value=SimpleNamespace(messages=[], metadata={}))- manager.state.delete = AsyncMock()- delete_transcript = MagicMock()- monkeypatch.setattr(forking.uuid, "uuid4", lambda: "failed-fork")- monkeypatch.setattr(- forking,- "fork_transcript_before_user_index",- MagicMock(side_effect=OSError("disk full")),- )- monkeypatch.setattr(forking, "delete_webui_transcript", delete_transcript)-- with pytest.raises(OSError, match="disk full"):- await forking.create_webui_chat_fork(- manager,- source_chat_id="source",- before_user_index=1,- )-- delete_transcript.assert_called_once_with("websocket:failed-fork")- manager.state.delete.assert_awaited_once_with("websocket:failed-fork")---async def test_create_fork_stops_before_transcript_work_when_source_is_invalid(- monkeypatch: pytest.MonkeyPatch,-) -> None:- manager = MagicMock()- manager.state.fork = AsyncMock(return_value=None)- fork_transcript = MagicMock()- monkeypatch.setattr(forking, "fork_transcript_before_user_index", fork_transcript)-- result = await forking.create_webui_chat_fork(- manager,- source_chat_id="missing",- before_user_index=1,- )-- assert result is None- fork_transcript.assert_not_called()--@pytest.mark.parametrize(("envelope", "detail"),[@@ -157,3 +82,36 @@ async def test_fork_handler_maps_invalid_source_and_internal_failure_to_stable_ewarning.assert_called_once_with("fork_chat failed: {}", ANY)channel.send_webui_protocol_error.assert_awaited_once_with(connection, "fork_chat_failed")++[email protected]("with_display", [False, True])+async def test_fork_copies_prefix_and_boundary_in_the_session_transaction(history, monkeypatch, with_display):+ sessions = history.sessions+ source = sessions.get_or_create("websocket:source")+ source.metadata.update(title="source title", session_handle="original", webui=True)+ for number in range(3):+ source.add_message("user", f"question {number}")+ source.add_message("assistant", f"answer {number}")+ sessions.save(source)+ if with_display:+ for number in range(3):+ history.append(source.key, {"event": "user", "text": f"question {number}"})+ history.append(source.key, {"event": "message", "text": f"answer {number}"})+ history.append(source.key, {"event": "turn_end"})+ monkeypatch.setattr(forking.uuid, "uuid4", lambda: "fork-id")+ result = await forking.create_webui_chat_fork(sessions, source_chat_id="source", before_user_index=2, title=" Useful fork ")+ assert result == ("fork-id", "websocket:fork-id")+ forked = await sessions.state.get(result[1])+ assert [message["content"] for message in forked.messages] == ["question 0", "answer 0", "question 1", "answer 1"]+ assert forked.metadata["title"] == "Useful fork"+ assert forked.metadata.get("session_handle") != "original"+ payload = history.build(forked.key)+ visible = [event["text"] for event in payload["events"] if "text" in event]+ assert visible == ["question 0", "answer 0", "question 1", "answer 1"]+ assert payload["fork_boundary_event_index"] == len(payload["events"])+ assert (await sessions.state.get(source.key)).messages == source.messages+++async def test_fork_missing_source_leaves_no_partial_session(history):+ assert await forking.create_webui_chat_fork(history.sessions, source_chat_id="missing", before_user_index=1) is None+ assert await history.sessions.state.list_sessions() == []diff --git a/tests/webui/test_prompt_index.py b/tests/webui/test_prompt_index.pyindex 06b38c8f5..4f0f21b30 100644--- a/tests/webui/test_prompt_index.py+++ b/tests/webui/test_prompt_index.py@@ -15,60 +15,57 @@ from nanobot.webui.client_contract import webui_contractfrom nanobot.webui.gateway_services import build_gateway_services-def test_complete_index_and_bidirectional_pages_without_loading_sqlite(tmp_path, monkeypatch):+def test_complete_index_and_bidirectional_pages_without_loading_model_messages(history, tmp_path, monkeypatch):monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)- monkeypatch.setattr(transcript, "_ACTIVE_TRANSCRIPT_ROTATE_BYTES", 2000)- monkeypatch.setattr(transcript, "_TARGET_ACTIVE_TRANSCRIPT_BYTES", 1000)key = "websocket:navigation"for i in range(120):- transcript.append_transcript_object(key, {"event": "user", "chat_id": "navigation", "text": f"question {i}", "turn_id": f"t{i}"})- transcript.append_transcript_object(key, {"event": "message", "chat_id": "navigation", "text": "answer " + "x" * 200, "turn_id": f"t{i}"})- transcript.append_transcript_object(key, {"event": "turn_end", "chat_id": "navigation", "turn_id": f"t{i}"})- index = transcript.build_prompt_index(key)+ history.append(key, {"event": "user", "chat_id": "navigation", "text": f"question {i}", "turn_id": f"t{i}"})+ history.append(key, {"event": "message", "chat_id": "navigation", "text": "answer " + "x" * 200, "turn_id": f"t{i}"})+ history.append(key, {"event": "turn_end", "chat_id": "navigation", "turn_id": f"t{i}"})+ index = history.prompts(key)assert len(index) == 120- assert not (tmp_path / "sessions" / "sessions.sqlite3").exists()- latest = transcript.build_webui_thread_response(key, limit=3)+ latest = history.build(key, limit=3)assert len(latest["events"]) < len(index)target = index[42]stats = transcript.TranscriptReplayStats()- page = transcript.build_webui_thread_response(key, after=target["cursor"], limit=6, stats=stats)+ page = history.build(key, after=target["cursor"], limit=6, stats=stats)assert page["events"][0]["projection_id"] == target["id"]assert page["events"][0]["text"] == "question 42"assert page["page"]["user_message_offset"] == 42assert page["page"]["has_more_before"] and page["page"]["has_more_after"]assert stats.parsed_records < 40 # Not the 360-record transcript.- newer = transcript.build_webui_thread_response(key, after=page["page"]["after_cursor"], limit=6)- older = transcript.build_webui_thread_response(key, before=newer["page"]["before_cursor"], limit=6)+ newer = history.build(key, after=page["page"]["after_cursor"], limit=6)+ older = history.build(key, before=newer["page"]["before_cursor"], limit=6)assert older["events"] == page["events"]- # Warm navigation uses file metadata + cached summaries, not replay.+ # Warm navigation uses the committed revision and cached summaries.with monkeypatch.context() as patch:- patch.setattr(transcript, "_read_transcript_file", lambda _: (_ for _ in ()).throw(AssertionError("replayed history")))- assert transcript.build_prompt_index(key) == index- transcript.append_transcript_object(key, {"event": "user", "chat_id": "navigation", "text": "new question"})- assert len(transcript.build_prompt_index(key)) == 121+ patch.setattr("nanobot.session.display_history.HistoryReader.navigation", lambda _: (_ for _ in ()).throw(AssertionError("replayed history")))+ assert history.prompts(key) == index+ history.append(key, {"event": "user", "chat_id": "navigation", "text": "new question"})+ assert len(history.prompts(key)) == 121-def test_oversized_turn_keeps_matching_navigation_identity(tmp_path, monkeypatch):+def test_oversized_turn_keeps_matching_navigation_identity(history, tmp_path, monkeypatch):monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)monkeypatch.setattr(transcript, "_MAX_TRANSCRIPT_PAGE_RECORDS", 6)key = "websocket:large-turn"- transcript.append_transcript_object(key, {"event": "user", "chat_id": "large-turn", "text": "target"})+ history.append(key, {"event": "user", "chat_id": "large-turn", "text": "target"})for i in range(20):- transcript.append_transcript_object(key, {"event": "message", "chat_id": "large-turn", "kind": "tool_hint", "text": str(i)})- transcript.append_transcript_object(key, {"event": "message", "chat_id": "large-turn", "text": "done"})- transcript.append_transcript_object(key, {"event": "turn_end", "chat_id": "large-turn"})- target = transcript.build_prompt_index(key)[0]- page = transcript.build_webui_thread_response(key, after=target["cursor"], limit=40)+ history.append(key, {"event": "message", "chat_id": "large-turn", "kind": "tool_hint", "text": str(i)})+ history.append(key, {"event": "message", "chat_id": "large-turn", "text": "done"})+ history.append(key, {"event": "turn_end", "chat_id": "large-turn"})+ target = history.prompts(key)[0]+ page = history.build(key, after=target["cursor"], limit=40)assert page["page"]["truncated_oversized_turn"]assert page["events"][0]["projection_id"] == target["id"]assert len(target["answerPreview"]) <= 240@pytest.mark.asyncio-async def test_navigation_public_routes_auth_validation_and_old_reads(tmp_path, monkeypatch):+async def test_navigation_public_routes_auth_validation_and_old_reads(history, tmp_path, monkeypatch):monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)gateway = build_gateway_services(- config=WebSocketConfig(), bus=MessageBus(), session_manager=None,+ config=WebSocketConfig(), bus=MessageBus(), session_manager=history.sessions,static_dist_path=None, workspace_path=tmp_path, config_path=tmp_path / "config.json",default_restrict_to_workspace=False, runtime_model_name=None,runtime_surface="browser", runtime_capabilities_overrides=None,@@ -78,7 +75,7 @@ async def test_navigation_public_routes_auth_validation_and_old_reads(tmp_path,assert denied.status_code == 401gateway.tokens.api_tokens["test-token"] = time.monotonic() + 60headers = Headers([("Authorization", "Bearer test-token")])- transcript.append_transcript_object("websocket:test", {"event": "user", "chat_id": "test", "text": "hello"})+ history.append("websocket:test", {"event": "user", "chat_id": "test", "text": "hello"})response = await gateway.http._dispatch_session_routes(Request(path + "/prompts", headers), path + "/prompts")prompt = json.loads(response.body)["prompts"][0]revision = json.loads(response.body)["revision"]@@ -96,37 +93,37 @@ async def test_navigation_public_routes_auth_validation_and_old_reads(tmp_path,assert "webui.prompt-navigation.v1" in webui_contract()["capabilities"]-def test_summary_deltas_rebuild_after_append_rewrite_eviction_and_delete(tmp_path, monkeypatch):+def test_summary_deltas_rebuild_after_append_rewrite_eviction_and_delete(history, tmp_path, monkeypatch):monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:index-delta"for i in range(100):- transcript.append_transcript_object(key, {"event": "user", "text": f"question {i}", "turn_id": f"t{i}"})- transcript.append_transcript_object(key, {"event": "turn_end", "turn_id": f"t{i}"})- initial = transcript.prompt_index_response(key, None)+ history.append(key, {"event": "user", "text": f"question {i}", "turn_id": f"t{i}"})+ history.append(key, {"event": "turn_end", "turn_id": f"t{i}"})+ initial = history.prompt_response(key, None)assert len(initial["prompts"]) == 100- transcript.append_transcript_object(key, {"event": "user", "text": "next", "turn_id": "next"})- appended = transcript.prompt_index_response(key, initial["revision"])+ history.append(key, {"event": "user", "text": "next", "turn_id": "next"})+ appended = history.prompt_response(key, initial["revision"])assert appended["replaceFrom"] == 100assert len(appended["prompts"]) == 1- assert [*initial["prompts"], *appended["prompts"]] == transcript.build_prompt_index(key)+ assert [*initial["prompts"], *appended["prompts"]] == history.prompts(key)# A late answer changes the last preview; it must replace that prompt, not append it.- transcript.append_transcript_object(key, {"event": "message", "text": "answer", "turn_id": "next"})- changed = transcript.prompt_index_response(key, appended["revision"])+ history.append(key, {"event": "message", "text": "answer", "turn_id": "next"})+ changed = history.prompt_response(key, appended["revision"])assert changed["replaceFrom"] == 100assert changed["prompts"][0]["answerPreview"] == "answer"- transcript.delete_webui_transcript(key)- transcript.append_transcript_object(key, {"event": "user", "text": "replacement"})- replaced = transcript.prompt_index_response(key, changed["revision"])+ history.sessions.delete_session(key)+ history.append(key, {"event": "user", "text": "replacement"})+ replaced = history.prompt_response(key, changed["revision"])assert replaced["replaceFrom"] == 0assert len(replaced["prompts"]) == 1transcript._PROMPT_SNAPSHOTS.clear() # Restart/eviction sends a complete response.- assert transcript.prompt_index_response(key, replaced["revision"])["replaceFrom"] == 0- transcript.delete_webui_transcript(key)- assert transcript.prompt_index_response(key, replaced["revision"])["prompts"] == ()+ assert history.prompt_response(key, replaced["revision"])["replaceFrom"] == 0+ history.sessions.delete_session(key)+ assert history.prompt_response(key, replaced["revision"])["prompts"] == ()@pytest.mark.parametrize("input_id", [None, "injected-guidance"])-def test_recovered_prompt_identity_time_and_cursor_survive_pages(tmp_path, monkeypatch, input_id):+def test_recovered_prompt_identity_time_and_cursor_survive_pages(history, tmp_path, monkeypatch, input_id):monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:recovered-navigation"messages = [{"role": "system", "content": "context"}]@@ -135,31 +132,31 @@ def test_recovered_prompt_identity_time_and_cursor_survive_pages(tmp_path, monkeif input_id:user["webui_turn_id"] = f"{input_id}-{i}"messages.extend([user, {"role": "assistant", "content": f"answer {i}"}])- transcript.append_transcript_object(key, {"event": "message", "text": f"answer {i}", "turn_id": f"execution-{i}"})- transcript.append_transcript_object(key, {"event": "turn_end", "turn_id": f"execution-{i}"})+ history.append(key, {"event": "message", "text": f"answer {i}", "turn_id": f"execution-{i}"})+ history.append(key, {"event": "turn_end", "turn_id": f"execution-{i}"})def loader():return messages- cold_page = transcript.build_webui_thread_response(key, limit=1, session_messages_loader=loader)- index = transcript.build_prompt_index(key, session_messages_loader=loader)- assert transcript.build_webui_thread_response(key, limit=1, session_messages_loader=loader) == cold_page+ cold_page = history.build(key, limit=1, session_messages_loader=loader)+ index = history.prompts(key, session_messages_loader=loader)+ assert history.build(key, limit=1, session_messages_loader=loader) == cold_pageassert len(index) == 2assert index[0]["id"] != index[1]["id"]assert [prompt["createdAt"] for prompt in index] == [1780394400000, 1780394460000]for position, prompt in enumerate(index):- page = transcript.build_webui_thread_response(key, after=prompt["cursor"], limit=1, session_messages_loader=loader)+ page = history.build(key, after=prompt["cursor"], limit=1, session_messages_loader=loader)user = next(event for event in page["events"] if event["event"] == "user_message")assert user["projection_id"] == prompt["id"]assert user["created_at_ms"] == prompt["createdAt"]assert user.get("turn_id") == (f"{input_id}-{position}" if input_id else None)- assert transcript.build_prompt_index(key, session_messages_loader=loader) == index+ assert history.prompts(key, session_messages_loader=loader) == index@pytest.mark.asyncio-async def test_recovered_public_index_and_page_validators_include_canonical_timestamp(tmp_path, monkeypatch):+async def test_recovered_public_index_and_page_validators_include_canonical_timestamp(history, tmp_path, monkeypatch):monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:canonical-revision"- manager = SessionManager(tmp_path)+ manager = history.sessionssession = manager.get_or_create(key)session.messages = [{"role": "user", "content": "guidance", "webui_turn_id": "input", "timestamp": "2026-06-02T10:00:00+00:00"},@@ -167,7 +164,7 @@ async def test_recovered_public_index_and_page_validators_include_canonical_time]manager.save(session)for event in ({"event": "message", "text": "answer"}, {"event": "turn_end"}):- transcript.append_transcript_object(key, event)+ history.append(key, event)gateway = build_gateway_services(config=WebSocketConfig(), bus=MessageBus(), session_manager=manager,static_dist_path=None, workspace_path=tmp_path, config_path=tmp_path / "config.json",@@ -188,8 +185,9 @@ async def test_recovered_public_index_and_page_validators_include_canonical_timeassert unchanged.status_code == 304# A canonical-only update must invalidate the recovered display even when# transcript artifacts and the session's updated_at are unchanged.+ session = await manager.state.get(key)session.messages[0]["timestamp"] = "2026-06-02T10:01:00+00:00"- manager.save(session)+ await manager.state.finish_turn(session)changed = {}for path, previous in initial.items():request_path = path + "?since=" + json.loads(previous.body)["revision"]@@ -208,20 +206,21 @@ async def test_recovered_public_index_and_page_validators_include_canonical_time@pytest.mark.asyncio@pytest.mark.parametrize("missing_user", [False, True])-async def test_cached_history_routes_skip_replay_and_canonical_reads(tmp_path, monkeypatch, missing_user):+async def test_cached_history_routes_skip_replay_and_canonical_reads(history, tmp_path, monkeypatch, missing_user):monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:cached-history"- manager = SessionManager(tmp_path)+ manager = history.sessionssession = manager.get_or_create(key)+ manager.save(session)for i in range(3):session.messages.extend([{"role": "user", "content": f"question {i}", "webui_turn_id": f"input-{i}"},{"role": "assistant", "content": f"answer {i}"},])if not missing_user or i < 2:- transcript.append_transcript_object(key, {"event": "user", "text": f"question {i}", "turn_id": f"input-{i}"})- transcript.append_transcript_object(key, {"event": "message", "text": f"answer {i}"})- transcript.append_transcript_object(key, {"event": "turn_end"})+ history.append(key, {"event": "user", "text": f"question {i}", "turn_id": f"input-{i}"})+ history.append(key, {"event": "message", "text": f"answer {i}"})+ history.append(key, {"event": "turn_end"})manager.save(session)gateway = build_gateway_services(config=WebSocketConfig(), bus=MessageBus(), session_manager=manager,@@ -244,7 +243,7 @@ async def test_cached_history_routes_skip_replay_and_canonical_reads(tmp_path, mpytest.fail("unchanged history was rebuilt")monkeypatch.setattr(transcript, "_build_prompt_view", unexpected_work)- monkeypatch.setattr(transcript, "_read_transcript_file", unexpected_work)+ monkeypatch.setattr("nanobot.session.display_history.HistoryReader.navigation", unexpected_work)monkeypatch.setattr(manager, "read_session_file", unexpected_work)monkeypatch.setattr("nanobot.webui.ws_http.build_webui_thread_response", unexpected_work)for path, previous in initial.items():@@ -271,10 +270,10 @@ async def test_cached_history_routes_skip_replay_and_canonical_reads(tmp_path, m@pytest.mark.asyncio@pytest.mark.parametrize("endpoint", ["", "/prompts"])-async def test_canonical_change_during_recovery_is_not_cached(tmp_path, monkeypatch, endpoint):+async def test_canonical_change_during_recovery_keeps_a_consistent_snapshot(history, tmp_path, monkeypatch, endpoint):monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:racing-history"- manager = SessionManager(tmp_path)+ manager = history.sessionssession = manager.get_or_create(key)session.messages = [{"role": "user", "content": "question", "timestamp": "2026-06-02T10:00:00+00:00"},@@ -282,7 +281,7 @@ async def test_canonical_change_during_recovery_is_not_cached(tmp_path, monkeypa]manager.save(session)for event in ({"event": "message", "text": "answer"}, {"event": "turn_end"}):- transcript.append_transcript_object(key, event)+ history.append(key, event)gateway = build_gateway_services(config=WebSocketConfig(), bus=MessageBus(), session_manager=manager,static_dist_path=None, workspace_path=tmp_path, config_path=tmp_path / "config.json",@@ -292,6 +291,7 @@ async def test_canonical_change_during_recovery_is_not_cached(tmp_path, monkeypagateway.tokens.api_tokens["test-token"] = time.monotonic() + 60auth = Headers([("Authorization", "Bearer test-token")])original_read = manager.read_session_file+ external = SessionManager(tmp_path)changed = Falsedef read_and_commit(key):@@ -299,17 +299,18 @@ async def test_canonical_change_during_recovery_is_not_cached(tmp_path, monkeypadata = original_read(key)if not changed:changed = True- session.messages[0]["timestamp"] = "2026-06-02T10:01:00+00:00"- manager.save(session)+ update = external.get_or_create(key)+ update.messages[0]["timestamp"] = "2026-06-02T10:01:00+00:00"+ external.save(update)return datamonkeypatch.setattr(manager, "read_session_file", read_and_commit)path = "/api/sessions/websocket%3Aracing-history/webui-thread" + endpointmixed = await gateway.http._dispatch_session_routes(Request(path, auth), path)assert changed and mixed.status_code == 200- assert "ETag" not in mixed.headers and "revision" not in json.loads(mixed.body)+ assert mixed.headers["ETag"] and json.loads(mixed.body)["revision"]current = await gateway.http._dispatch_session_routes(Request(path, auth), path)- assert current.status_code == 200 and current.headers["ETag"]+ assert current.status_code == 200 and current.headers["ETag"] != mixed.headers["ETag"]data = json.loads(current.body)if endpoint:assert data["prompts"][0]["createdAt"] == 1780394460000@@ -319,7 +320,7 @@ async def test_canonical_change_during_recovery_is_not_cached(tmp_path, monkeypa@pytest.mark.parametrize("with_anchor", [False, True])-def test_indexed_backfill_preserves_unique_in_suffix_matching(tmp_path, monkeypatch, with_anchor):+def test_indexed_backfill_preserves_unique_in_suffix_matching(history, tmp_path, monkeypatch, with_anchor):monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)key = "websocket:repeated-answers"answers = ["same answer", "anchor answer", "same answer"] if with_anchor else ["same answer"] * 2@@ -328,13 +329,13 @@ def test_indexed_backfill_preserves_unique_in_suffix_matching(tmp_path, monkeypauser = {"role": "user", "content": f"question {position}", "webui_turn_id": f"input-{position}"}messages.extend([user, {"role": "assistant", "content": answer}])if position < len(answers) - 1:- transcript.append_transcript_object(key, {"event": "user", "text": user["content"], "turn_id": user["webui_turn_id"]})- transcript.append_transcript_object(key, {"event": "message", "text": answer})- transcript.append_transcript_object(key, {"event": "turn_end"})- prompts = transcript.build_prompt_index(key, session_messages_loader=lambda: messages)+ history.append(key, {"event": "user", "text": user["content"], "turn_id": user["webui_turn_id"]})+ history.append(key, {"event": "message", "text": answer})+ history.append(key, {"event": "turn_end"})+ prompts = history.prompts(key, session_messages_loader=lambda: messages)# A unique intervening answer advances the alignment past the first duplicate.assert len(prompts) == (len(answers) if with_anchor else 1)- page = transcript.build_webui_thread_response(key, limit=1, session_messages=messages)+ page = history.build(key, limit=1, session_messages=messages)users = [event for event in page["events"] if event["event"] == "user_message"]assert [user["text"] for user in users] == (["question 2"] if with_anchor else [])if with_anchor:diff --git a/tests/webui/test_session_list_index.py b/tests/webui/test_session_list_index.pyindex 369639d79..543f8412a 100644--- a/tests/webui/test_session_list_index.py+++ b/tests/webui/test_session_list_index.py@@ -1,8 +1,6 @@from __future__ import annotations-import ioimport json-import osfrom concurrent.futures import ThreadPoolExecutorfrom datetime import datetimefrom pathlib import Path@@ -21,13 +19,6 @@ from nanobot.session.recovery import RECOVERY_METADATA_KEYfrom nanobot.session.types import SESSION_TYPE_KEY, SessionType[email protected](autouse=True)-def _isolate_webui_dir(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:- webui_dir = tmp_path / "webui"- webui_dir.mkdir(exist_ok=True)- monkeypatch.setattr(session_list_index, "get_webui_dir", lambda: webui_dir)--def test_webui_session_list_reads_previews_without_loading_history(tmp_path, monkeypatch):manager = SessionManager(tmp_path)session = manager.get_or_create("websocket:indexed")@@ -41,164 +32,12 @@ def test_webui_session_list_reads_previews_without_loading_history(tmp_path, monmonkeypatch.setattr(manager._store, "load", fail_load)monkeypatch.setattr(manager, "read_session_metadata", fail_load)- monkeypatch.setattr(session_list_index, "_read_index_rows", fail_load)- monkeypatch.setattr(session_list_index, "_write_index_rows", fail_load)rows = list_webui_sessions(manager)assert rows[0]["key"] == "websocket:indexed"assert rows[0]["preview"] == "indexed preview"assert rows[0]["model_preset"] == "fast"-def test_webui_session_list_private_rows_suppress_transcript_fallback(tmp_path, monkeypatch):- manager = SessionManager(tmp_path)- manager.types.register(SessionType("internal", needs_handle=False, public_history=False))- webui_dir = tmp_path / "webui"- private_key, transcript_key = "websocket:internal", "websocket:transcript-only"- for key in (private_key, transcript_key):- (webui_dir / f"{SessionManager.safe_key(key)}.jsonl").write_text(- json.dumps({"event": "user", "chat_id": key.removeprefix("websocket:"),- "text": "transcript preview"}) + "\n",- encoding="utf-8",- )- assert {row["key"] for row in list_webui_sessions(manager)} == {private_key, transcript_key}-- public = manager.get_or_create("websocket:public")- public.add_message("user", "public preview")- manager.save(public)- private = manager.get_or_create(private_key)- private.metadata[SESSION_TYPE_KEY] = "internal"- private.add_message("user", "private preview")- manager.save(private)- unknown = manager.get_or_create("websocket:unregistered")- unknown.metadata[SESSION_TYPE_KEY] = "unregistered"- manager.save(unknown)-- def individual_metadata_unavailable(*_args, **_kwargs):- pytest.fail("sidebar visibility must use the existing metadata query snapshot")-- monkeypatch.setattr(manager, "read_session_metadata", individual_metadata_unavailable)- rows = {row["key"]: row for row in list_webui_sessions(manager)}- assert set(rows) == {public.key, transcript_key}- assert rows[public.key]["preview"] == "public preview"- assert rows[transcript_key]["preview"] == "transcript preview"- assert {row["key"] for row in list_webui_sessions(manager)} == set(rows)---def test_webui_session_list_scans_empty_display_directory_once_per_request(tmp_path, monkeypatch):- manager = SessionManager(tmp_path)- keys = {"websocket:first", "websocket:second", "websocket:third"}- for key in keys:- manager.save(manager.get_or_create(key))- list_webui_sessions(manager)- webui_dir = tmp_path / "webui"- original_stat, original_scandir = Path.stat, os.scandir- scans = []-- def stored_files_only(path, *args, **kwargs):- assert path.parent != webui_dir, "sidebar must not probe absent per-session display files"- return original_stat(path, *args, **kwargs)-- def scan(path):- scans.append(Path(path))- return original_scandir(path)-- with monkeypatch.context() as patch:- patch.setattr(Path, "stat", stored_files_only)- patch.setattr(session_list_index.os, "scandir", scan)- for _ in range(2):- assert {row["key"] for row in list_webui_sessions(manager)} == keys- assert scans == [webui_dir, webui_dir]---def test_webui_session_list_snapshot_tracks_all_display_artifacts(tmp_path):- manager = SessionManager(tmp_path)- webui_dir = tmp_path / "webui"- key = "websocket:artifacts"- stem = SessionManager.safe_key(key)- segments = webui_dir / f"{stem}.segments"- segments.mkdir()- active = webui_dir / f"{stem}.jsonl"- active.write_text('{"event":"user","chat_id":"artifacts","text":"active question"}\n', encoding="utf-8")- segment = segments / "000001.jsonl"- segment.write_text('{"event":"user","chat_id":"artifacts","text":"older question"}\n', encoding="utf-8")- other_files = [webui_dir / f"{stem}.json", segments / "manifest.json",- segments / "state.json", segments / "delivery.json"]- for path in other_files:- path.write_text("{}\n", encoding="utf-8")- all_files = [active, segment, *other_files]- base_ns = int(datetime(2026, 1, 1).timestamp() * 1_000_000_000)- for path in all_files:- os.utime(path, ns=(base_ns, base_ns))-- row, = list_webui_sessions(manager)- assert row["preview"] == "older question"- cache = json.loads((manager.sessions_dir / ".webui_session_index.json").read_text(encoding="utf-8"))- cached, = cache["sessions"]- assert cached["webui_activity_files"] == len(all_files)- assert cached["webui_activity_size"] == sum(path.stat().st_size for path in all_files)- state_file = segments / "state.json"- state_file.write_text('{"active":true}\n', encoding="utf-8")- newer_ns = base_ns + 1_000_000_000- os.utime(state_file, ns=(newer_ns, newer_ns))- refreshed, = list_webui_sessions(manager)- assert refreshed["updated_at"] == datetime.fromtimestamp(newer_ns / 1_000_000_000).isoformat()- other_files[0].unlink()- other_files[-1].unlink()- list_webui_sessions(manager)- cached, = json.loads((manager.sessions_dir / ".webui_session_index.json").read_text(encoding="utf-8"))["sessions"]- assert cached["webui_activity_files"] == len(all_files) - 2- assert cached["webui_activity_size"] == sum(path.stat().st_size for path in all_files if path.exists())---def test_webui_session_list_activity_uses_host_filename_case_semantics(tmp_path):- manager = SessionManager(tmp_path)- key = "websocket:MixedCase"- session = manager.get_or_create(key)- session.add_message("user", "stored question")- session.messages[0]["timestamp"] = "2026-01-01T00:00:00"- session.updated_at = datetime(2026, 1, 1)- manager.save(session)- path = tmp_path / "webui" / f"{SessionManager.safe_key(key).swapcase()}.jsonl"- path.write_text('{"event":"turn_end","chat_id":"MixedCase"}\n', encoding="utf-8")- activity = datetime(2026, 1, 2)- activity_ns = int(activity.timestamp() * 1_000_000_000)- os.utime(path, ns=(activity_ns, activity_ns))- row, = list_webui_sessions(manager)- assert row["updated_at"] == (activity.isoformat() if os.name == "nt" else "2026-01-01T00:00:00")---def test_webui_session_list_keeps_transcript_only_key_case(tmp_path):- key = "websocket:MixedCase"- path = tmp_path / "webui" / f"{SessionManager.safe_key(key)}.jsonl"- path.write_text('{"event":"user","chat_id":"MixedCase","text":"question"}\n', encoding="utf-8")- row, = list_webui_sessions(SessionManager(tmp_path))- assert row["key"] == key and row["preview"] == "question"---def test_webui_session_list_rejects_display_file_and_segment_symlinks(tmp_path):- manager = SessionManager(tmp_path)- webui_dir = tmp_path / "webui"- outside = tmp_path / "outside"- outside.mkdir()- target = outside / "000000.jsonl"- target.write_text('{"event":"user","chat_id":"linked","text":"outside secret"}\n', encoding="utf-8")- safe_segments = webui_dir / "websocket_safe.segments"- safe_segments.mkdir()- (safe_segments / "000001.jsonl").write_text(- '{"event":"user","chat_id":"safe","text":"safe question"}\n', encoding="utf-8",- )- try:- (webui_dir / "websocket_linked.jsonl").symlink_to(target)- (webui_dir / "websocket_directory.segments").symlink_to(outside, target_is_directory=True)- (safe_segments / "000000.jsonl").symlink_to(target)- except OSError:- pytest.skip("symlink creation is unavailable")-- row, = list_webui_sessions(manager)- assert row["key"] == "websocket:safe" and row["preview"] == "safe question"---def test_webui_session_list_refreshes_after_model_preset_rename(tmp_path: Path) -> None:manager = SessionManager(tmp_path)session = manager.get_or_create("websocket:renamed-preset")@@ -237,30 +76,6 @@ def test_webui_session_list_surfaces_pending_recovery_state(tmp_path: Path) -> N}-def test_webui_session_index_uses_unique_temp_file(tmp_path: Path) -> None:- key = "websocket:unique-index-temp"- transcript = tmp_path / "webui" / f"{SessionManager.safe_key(key)}.jsonl"- transcript.write_text(- '{"event":"user","chat_id":"unique-index-temp","text":"hello"}\n',- encoding="utf-8",- )- manager = SessionManager(tmp_path)- stale_shared_tmp = manager.sessions_dir / ".webui_session_index.json.tmp"- stale_bytes = b"pre-existing shared temp\x00\xff"- stale_shared_tmp.write_bytes(stale_bytes)-- row, = list_webui_sessions(manager)-- assert row["key"] == key- assert row["preview"] == "hello"- cache = json.loads((manager.sessions_dir / ".webui_session_index.json").read_text(encoding="utf-8"))- cached_row, = cache["sessions"]- assert cached_row["key"] == key- assert cached_row["preview"] == "hello"- assert stale_shared_tmp.read_bytes() == stale_bytes- assert not list(manager.sessions_dir.glob(".webui_session_index.json.*.tmp"))--def test_webui_session_list_indexes_workspace_scope_and_preserves_null(tmp_path: Path,) -> None:@@ -321,7 +136,6 @@ def test_webui_session_list_refreshes_after_concurrent_metadata_change(tmp_path,assert session_list_index.indexed_workspace_scope(second)[1]["access_mode"] == "restricted"-def test_webui_session_list_sees_committed_snapshot_during_write(tmp_path):manager = SessionManager(tmp_path)session = manager.get_or_create("websocket:concurrent")@@ -338,7 +152,6 @@ def test_webui_session_list_sees_committed_snapshot_during_write(tmp_path):assert list_webui_sessions(manager)[0]["preview"] == "after"-def test_webui_session_list_rejects_invalid_internal_model_preset_metadata(tmp_path: Path,) -> None:@@ -379,7 +192,6 @@ def test_webui_session_list_skips_provider_state_before_preview_budget(tmp_path: Path,monkeypatch,) -> None:- monkeypatch.setattr(session_list_index, "_SESSION_LIST_PREVIEW_MAX_CHARS", 100)manager = SessionManager(tmp_path)session = manager.get_or_create("websocket:private-state")session.provider_state = ProviderConversationState(@@ -408,283 +220,6 @@ def test_webui_session_list_drops_deleted_index_rows(tmp_path: Path) -> None:assert list_webui_sessions(manager) == []-def test_webui_session_list_recovers_transcript_without_canonical_session(- tmp_path: Path,- monkeypatch: pytest.MonkeyPatch,-) -> None:- webui_dir = tmp_path / "webui"- webui_dir.mkdir(exist_ok=True)- monkeypatch.setattr(session_list_index, "get_webui_dir", lambda: webui_dir)- key = "websocket:restored"- transcript = webui_dir / f"{SessionManager.safe_key(key)}.jsonl"- transcript.write_text(- '{"event":"user","chat_id":"restored","text":"original question",'- '"created_at_ms":1785502800000}\n'- '{"event":"message","chat_id":"restored","text":"original answer",'- '"created_at_ms":1785502801000}\n'- '{"event":"turn_end","chat_id":"restored","created_at_ms":1785502802000}\n',- encoding="utf-8",- )- manager = SessionManager(tmp_path / "workspace")-- [row] = list_webui_sessions(manager)-- assert row["key"] == key- assert row["preview"] == "original question"- assert row["created_at"] == datetime.fromtimestamp(1785502800).isoformat()- assert manager.read_session_snapshot(key) is None- assert manager.list_sessions() == []-- reloaded = SessionManager(tmp_path / "workspace")- assert [row["key"] for row in list_webui_sessions(reloaded)] == [key]---def test_webui_session_list_recovers_colon_chat_id_from_transcript(- tmp_path: Path,- monkeypatch: pytest.MonkeyPatch,-) -> None:- webui_dir = tmp_path / "webui"- webui_dir.mkdir(exist_ok=True)- monkeypatch.setattr(session_list_index, "get_webui_dir", lambda: webui_dir)- key = "websocket:scope:child"- transcript = webui_dir / f"{SessionManager.safe_key(key)}.jsonl"- transcript.write_text(- '{"event":"user","chat_id":"scope:child","text":"scoped history"}\n',- encoding="utf-8",- )-- [row] = list_webui_sessions(SessionManager(tmp_path / "workspace"))-- assert row["key"] == key- assert row["preview"] == "scoped history"---def test_webui_session_list_normalizes_transcript_preview(tmp_path: Path) -> None:- key = "websocket:long-preview"- transcript = tmp_path / "webui" / f"{SessionManager.safe_key(key)}.jsonl"- transcript.write_text(- json.dumps(- {- "event": "user",- "chat_id": "long-preview",- "text": "first\n\n" + "word " * 100,- }- )- + "\n",- encoding="utf-8",- )-- [row] = list_webui_sessions(SessionManager(tmp_path / "workspace"))-- assert row["preview"].startswith("first word")- assert "\n" not in row["preview"]- assert row["preview"].endswith("…")---def test_webui_session_list_tolerates_invalid_transcript_timestamp(- tmp_path: Path,-) -> None:- key = "websocket:bad-time"- transcript = tmp_path / "webui" / f"{SessionManager.safe_key(key)}.jsonl"- transcript.write_text(- '{"event":"user","chat_id":"bad-time","text":"still visible",'- '"created_at_ms":1e100}\n',- encoding="utf-8",- )-- [row] = list_webui_sessions(SessionManager(tmp_path / "workspace"))-- assert row["preview"] == "still visible"- datetime.fromisoformat(row["created_at"])---def test_webui_session_list_ignores_invalid_transcript_chat_id(tmp_path: Path) -> None:- transcript = tmp_path / "webui" / "websocket_.._outside.jsonl"- transcript.write_text(- '{"event":"user","chat_id":"../outside","text":"do not expose"}\n',- encoding="utf-8",- )-- assert list_webui_sessions(SessionManager(tmp_path / "workspace")) == []---def test_webui_session_list_recovers_segment_only_transcript(- tmp_path: Path,- monkeypatch: pytest.MonkeyPatch,-) -> None:- webui_dir = tmp_path / "webui"- webui_dir.mkdir(exist_ok=True)- monkeypatch.setattr(session_list_index, "get_webui_dir", lambda: webui_dir)- key = "websocket:segmented"- segments = webui_dir / f"{SessionManager.safe_key(key)}.segments"- segments.mkdir()- (segments / "000001.jsonl").write_text(- '{"event":"user","chat_id":"segmented","text":"older segment"}\n'- '{"event":"turn_end","chat_id":"segmented"}\n',- encoding="utf-8",- )-- [row] = list_webui_sessions(SessionManager(tmp_path / "workspace"))-- assert row["key"] == key- assert row["preview"] == "older segment"---def test_webui_session_list_prefers_canonical_metadata_without_duplicate(- tmp_path: Path,- monkeypatch: pytest.MonkeyPatch,-) -> None:- webui_dir = tmp_path / "webui"- webui_dir.mkdir(exist_ok=True)- monkeypatch.setattr(session_list_index, "get_webui_dir", lambda: webui_dir)- key = "websocket:canonical"- (webui_dir / f"{SessionManager.safe_key(key)}.jsonl").write_text(- '{"event":"user","chat_id":"canonical","text":"display copy"}\n',- encoding="utf-8",- )- manager = SessionManager(tmp_path / "workspace")- session = manager.get_or_create(key)- session.metadata["title"] = "Canonical title"- session.add_message("user", "canonical preview")- manager.save(session)-- rows = list_webui_sessions(manager)-- assert len(rows) == 1- assert rows[0]["key"] == key- assert rows[0]["preview"] == "canonical preview"---def test_webui_session_list_reuses_unchanged_transcript_index(- tmp_path: Path,- monkeypatch: pytest.MonkeyPatch,-) -> None:- webui_dir = tmp_path / "webui"- webui_dir.mkdir(exist_ok=True)- monkeypatch.setattr(session_list_index, "get_webui_dir", lambda: webui_dir)- key = "websocket:cached-transcript"- (webui_dir / f"{SessionManager.safe_key(key)}.jsonl").write_text(- '{"event":"user","chat_id":"cached-transcript","text":"cached"}\n',- encoding="utf-8",- )- manager = SessionManager(tmp_path / "workspace")- assert list_webui_sessions(manager)[0]["preview"] == "cached"-- def fail_scan(*args, **kwargs):- raise AssertionError("unchanged transcript should reuse its index row")-- monkeypatch.setattr(session_list_index, "_scan_transcript_row", fail_scan)-- assert list_webui_sessions(manager)[0]["preview"] == "cached"---def test_webui_session_list_does_not_cache_changed_transcript_with_old_signature(- tmp_path: Path,- monkeypatch: pytest.MonkeyPatch,-) -> None:- webui_dir = tmp_path / "webui"- key = "websocket:transcript-race"- transcript = webui_dir / f"{SessionManager.safe_key(key)}.jsonl"- transcript.write_text(- '{"event":"user","chat_id":"transcript-race","text":"initial"}\n',- encoding="utf-8",- )- manager = SessionManager(tmp_path / "workspace")- assert list_webui_sessions(manager)[0]["preview"] == "initial"- transcript.write_text(- '{"event":"user","chat_id":"transcript-race","text":"first scan"}\n',- encoding="utf-8",- )-- original_open = open- changed = False-- class RacingReader(io.StringIO):- def __next__(self) -> str:- nonlocal changed- if not changed:- changed = True- transcript.write_text(- '{"event":"user","chat_id":"transcript-race","text":"second scan"}\n',- encoding="utf-8",- )- return super().__next__()-- def racing_open(path, *args, **kwargs):- if Path(path) == transcript:- with original_open(path, *args, **kwargs) as source:- return RacingReader(source.read())- return original_open(path, *args, **kwargs)-- monkeypatch.setattr(session_list_index, "open", racing_open, raising=False)-- first = list_webui_sessions(manager)- second = list_webui_sessions(manager)-- assert first[0]["preview"] == "first scan"- assert second[0]["preview"] == "second scan"---def test_webui_session_list_drops_deleted_transcript_index_row(- tmp_path: Path,- monkeypatch: pytest.MonkeyPatch,-) -> None:- webui_dir = tmp_path / "webui"- webui_dir.mkdir(exist_ok=True)- monkeypatch.setattr(session_list_index, "get_webui_dir", lambda: webui_dir)- key = "websocket:deleted-transcript"- transcript = webui_dir / f"{SessionManager.safe_key(key)}.jsonl"- transcript.write_text(- '{"event":"user","chat_id":"deleted-transcript","text":"delete me"}\n',- encoding="utf-8",- )- manager = SessionManager(tmp_path / "workspace")- assert list_webui_sessions(manager)[0]["key"] == key-- transcript.unlink()-- assert list_webui_sessions(manager) == []---def test_webui_session_list_keeps_runtime_instances_isolated(- tmp_path: Path,- monkeypatch: pytest.MonkeyPatch,-) -> None:- first_dir = tmp_path / "instance-a" / "webui"- second_dir = tmp_path / "instance-b" / "webui"- first_dir.mkdir(parents=True)- second_dir.mkdir(parents=True)- (first_dir / "websocket_first.jsonl").write_text(- '{"event":"user","chat_id":"first","text":"first instance"}\n',- encoding="utf-8",- )- (second_dir / "websocket_second.jsonl").write_text(- '{"event":"user","chat_id":"second","text":"second instance"}\n',- encoding="utf-8",- )- manager = SessionManager(tmp_path / "workspace")-- monkeypatch.setattr(session_list_index, "get_webui_dir", lambda: first_dir)- assert [row["key"] for row in list_webui_sessions(manager)] == ["websocket:first"]-- monkeypatch.setattr(session_list_index, "get_webui_dir", lambda: second_dir)- assert [row["key"] for row in list_webui_sessions(manager)] == ["websocket:second"]---def test_webui_session_list_ignores_legacy_stem(tmp_path: Path) -> None:- manager = SessionManager(tmp_path)- legacy_path = manager.sessions_dir / "websocket_legacy.jsonl"- legacy_path.write_text(- '{"_type":"metadata","key":"websocket:legacy",'- '"created_at":"2025-01-01T00:00:00",'- '"updated_at":"2025-01-01T00:00:00","metadata":{}}\n',- encoding="utf-8",- )-- assert list_webui_sessions(manager) == []- assert legacy_path.exists()--def test_webui_session_list_skips_cron_internal_user_preview(tmp_path: Path) -> None:manager = SessionManager(tmp_path)session = manager.get_or_create("websocket:cron-preview")@@ -727,80 +262,6 @@ def test_webui_session_list_skips_hidden_history_user_preview(tmp_path: Path) ->assert list_webui_sessions(manager)[0]["preview"] == "subagent summary"-def test_webui_session_list_uses_webui_transcript_activity_for_sort(- tmp_path: Path,- monkeypatch,-) -> None:- webui_dir = tmp_path / "webui"- webui_dir.mkdir(exist_ok=True)- monkeypatch.setattr(session_list_index, "get_webui_dir", lambda: webui_dir)-- manager = SessionManager(tmp_path)- old_session = manager.get_or_create("websocket:old-metadata")- old_session.created_at = datetime(2026, 6, 15, 10, 0, 0)- old_session.updated_at = datetime(2026, 6, 15, 10, 0, 0)- old_session.add_message("user", "old metadata")- old_session.messages[-1]["timestamp"] = "2026-06-15T10:00:00"- old_session.updated_at = datetime(2026, 6, 15, 10, 0, 0)- manager.save(old_session)-- newer_metadata = manager.get_or_create("websocket:newer-metadata")- newer_metadata.created_at = datetime(2026, 6, 15, 11, 0, 0)- newer_metadata.updated_at = datetime(2026, 6, 15, 11, 0, 0)- newer_metadata.add_message("user", "newer metadata")- newer_metadata.messages[-1]["timestamp"] = "2026-06-15T11:00:00"- newer_metadata.updated_at = datetime(2026, 6, 15, 11, 0, 0)- manager.save(newer_metadata)-- transcript = webui_dir / "websocket_old-metadata.jsonl"- transcript.write_text(- '{"event":"turn_end","chat_id":"old-metadata"}\n',- encoding="utf-8",- )- activity_ns = int(datetime(2026, 6, 15, 12, 0, 0).timestamp() * 1_000_000_000)- os.utime(transcript, ns=(activity_ns, activity_ns))-- rows = list_webui_sessions(manager)-- assert [row["key"] for row in rows] == [- "websocket:old-metadata",- "websocket:newer-metadata",- ]- assert rows[0]["updated_at"].startswith("2026-06-15T12:00:00")---def test_webui_session_list_rescans_when_transcript_changes(- tmp_path: Path,- monkeypatch,-) -> None:- webui_dir = tmp_path / "webui"- webui_dir.mkdir(exist_ok=True)- monkeypatch.setattr(session_list_index, "get_webui_dir", lambda: webui_dir)-- manager = SessionManager(tmp_path)- session = manager.get_or_create("websocket:transcript-change")- session.created_at = datetime(2026, 6, 15, 10, 0, 0)- session.updated_at = datetime(2026, 6, 15, 10, 0, 0)- session.add_message("user", "preview")- session.messages[-1]["timestamp"] = "2026-06-15T10:00:00"- session.updated_at = datetime(2026, 6, 15, 10, 0, 0)- manager.save(session)-- assert list_webui_sessions(manager)[0]["preview"] == "preview"-- transcript = webui_dir / "websocket_transcript-change.jsonl"- transcript.write_text(- '{"event":"turn_end","chat_id":"transcript-change"}\n',- encoding="utf-8",- )- activity_ns = int(datetime(2026, 6, 15, 12, 30, 0).timestamp() * 1_000_000_000)- os.utime(transcript, ns=(activity_ns, activity_ns))-- rows = list_webui_sessions(manager)-- assert rows[0]["updated_at"].startswith("2026-06-15T12:30:00")--def test_webui_session_list_sorts_by_message_activity_not_maintenance_timestamp(tmp_path: Path,) -> None:@@ -927,3 +388,22 @@ def test_session_manager_list_sessions_fallback_time_when_missing(tmp_path: Pathassert sessions[0]["updated_at"] is not Nonedatetime.fromisoformat(sessions[0]["created_at"])datetime.fromisoformat(sessions[0]["updated_at"])+++def test_sidebar_uses_durable_display_summary_and_filters_private_sessions(history):+ sessions = history.sessions+ sessions.types.register(SessionType("internal", needs_handle=False, public_history=False))+ for key in ("websocket:visible", "websocket:private"):+ history.append(key, {"event": "user", "text": " display\n preview ", "created_at_ms": 1760000001000})+ history.append(key, {"event": "message", "text": "answer", "created_at_ms": 1760000002000})+ private = sessions.get_or_create("websocket:private")+ private.metadata[SESSION_TYPE_KEY] = "internal"+ sessions.save(private)+ rows = list_webui_sessions(sessions)+ assert [row["key"] for row in rows] == ["websocket:visible"]+ assert rows[0]["preview"] == "display preview"+ assert datetime.fromisoformat(rows[0]["updated_at"]).timestamp() == 1760000002+ history.append("websocket:visible", {"event": "turn_end", "created_at_ms": 1760000003000})+ assert datetime.fromisoformat(list_webui_sessions(sessions)[0]["updated_at"]).timestamp() == 1760000003+ sessions.delete_session("websocket:visible")+ assert list_webui_sessions(sessions) == []diff --git a/tests/webui/test_session_mentions.py b/tests/webui/test_session_mentions.pyindex 6bf724be0..48f0c4abb 100644--- a/tests/webui/test_session_mentions.py+++ b/tests/webui/test_session_mentions.py@@ -125,8 +125,7 @@ def test_session_mention_context_treats_titles_as_data() -> None:@pytest.mark.asyncioasync def test_session_mentions_do_not_isolate_workspaces(tmp_path, monkeypatch) -> None:webui_dir = tmp_path / "webui"- monkeypatch.setattr("nanobot.webui.transcript.get_webui_dir", lambda: webui_dir)- monkeypatch.setattr("nanobot.webui.session_list_index.get_webui_dir", lambda: webui_dir)+ monkeypatch.setattr("nanobot.webui.history.get_webui_dir", lambda: webui_dir)manager = SessionManager(tmp_path)project_b = tmp_path / "b"project_b.mkdir()