refactor(sessions): persist archive state in SQLite (#6536)
* feat(webui): add archived chat management * fix(webui): keep archived chats in the sidebar * feat(webui): scope archived topics to sidebar groups * fix(webui): reuse group dissolve label * fix(webui): fail fast when adding panes * refactor(sessions): persist archive state in SQLite * refactor: limit archive changes to SQLite storage * perf(sessions): avoid redundant archive I/O and read barriers * fix(sessions): archive only persisted conversations
This commit is contained in:
1 parent
58a05ea3e0
commit
80ac41b1f4
16 files changed
+751
-107
No files matched your search
@@ -60,7 +60,6 @@ from nanobot.webui.session_access import (
|
||||
session_mentions_runtime_context,
|
||||
)
|
||||
from nanobot.webui.session_identity import is_valid_webui_chat_id, webui_session_key
|
||||
from nanobot.webui.sidebar_state import write_webui_sidebar_state
|
||||
from nanobot.webui.temporary_chats import TemporaryChatError
|
||||
from nanobot.webui.transcription_ws import webui_transcription_event
|
||||
|
||||
@@ -405,10 +404,7 @@ class WebUICommandRouter:
|
||||
)
|
||||
return
|
||||
try:
|
||||
saved_state = await asyncio.to_thread(
|
||||
write_webui_sidebar_state,
|
||||
cast(dict[str, Any], state),
|
||||
)
|
||||
saved_state = await self._http_router.sidebar_state.write(cast(dict[str, Any], state))
|
||||
except (OSError, ValueError):
|
||||
await self._transport.webui_send_event(
|
||||
connection,
|
||||
|
||||
@@ -1,23 +1,29 @@
|
||||
"""Persisted WebUI sidebar workspace state.
|
||||
"""Compose sidebar preferences with session archive state.
|
||||
|
||||
This state is UI-only metadata, scoped to the active nanobot instance data
|
||||
directory (the directory containing the current config.json). It deliberately
|
||||
does not modify agent sessions.
|
||||
Layout and display preferences stay in the config-scoped JSON file. Archive
|
||||
flags live in the session database; the public sidebar payload combines both.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
from collections.abc import AsyncGenerator
|
||||
from contextlib import asynccontextmanager
|
||||
from pathlib import Path
|
||||
from typing import Any, cast
|
||||
from typing import TYPE_CHECKING, Any, cast
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from nanobot.config.paths import get_webui_dir
|
||||
from nanobot.utils.cancellation import shield_and_drain
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from nanobot.session.state import SessionState
|
||||
|
||||
WEBUI_SIDEBAR_STATE_SCHEMA_VERSION = 1
|
||||
_MAX_STATE_FILE_BYTES = 256 * 1024
|
||||
@@ -41,7 +47,6 @@ def default_webui_sidebar_state() -> dict[str, Any]:
|
||||
return {
|
||||
"schema_version": WEBUI_SIDEBAR_STATE_SCHEMA_VERSION,
|
||||
"pinned_keys": [],
|
||||
"archived_keys": [],
|
||||
"session_order": [],
|
||||
"title_overrides": {},
|
||||
"project_name_overrides": {},
|
||||
@@ -202,7 +207,6 @@ def normalize_webui_sidebar_state(raw: Any) -> dict[str, Any]:
|
||||
raw = cast(dict[str, Any], raw)
|
||||
state = default_webui_sidebar_state()
|
||||
state["pinned_keys"] = _clean_string_list(raw.get("pinned_keys"))
|
||||
state["archived_keys"] = _clean_string_list(raw.get("archived_keys"))
|
||||
state["session_order"] = _clean_string_list(raw.get("session_order"))
|
||||
state["title_overrides"] = _clean_title_overrides(raw.get("title_overrides"))
|
||||
state["project_name_overrides"] = _clean_title_overrides(
|
||||
@@ -233,13 +237,64 @@ def read_webui_sidebar_state() -> dict[str, Any]:
|
||||
return normalize_webui_sidebar_state(raw)
|
||||
|
||||
|
||||
def read_legacy_archived_keys() -> list[str]:
|
||||
"""Do not mark the import complete when the source cannot be read."""
|
||||
path = webui_sidebar_state_path()
|
||||
try:
|
||||
with path.open(encoding="utf-8") as source:
|
||||
raw = json.load(source)
|
||||
except FileNotFoundError:
|
||||
return []
|
||||
if not isinstance(raw, dict):
|
||||
raise ValueError("sidebar state must be an object")
|
||||
return _clean_string_list(cast(dict[str, Any], raw).get("archived_keys"))
|
||||
|
||||
|
||||
def remove_legacy_archived_keys() -> None:
|
||||
with _SIDEBAR_STATE_WRITE_LOCK:
|
||||
path = webui_sidebar_state_path()
|
||||
try:
|
||||
with path.open(encoding="utf-8") as source:
|
||||
raw = json.load(source)
|
||||
except FileNotFoundError:
|
||||
return
|
||||
if isinstance(raw, dict) and "archived_keys" in raw:
|
||||
_write_webui_sidebar_state(cast(dict[str, Any], raw))
|
||||
|
||||
|
||||
def write_webui_sidebar_state(raw: dict[str, Any]) -> dict[str, Any]:
|
||||
with _SIDEBAR_STATE_WRITE_LOCK:
|
||||
return _write_webui_sidebar_state(raw)
|
||||
|
||||
|
||||
def write_webui_sidebar_preferences(raw: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Archive-only changes must not rewrite the unchanged preference file."""
|
||||
with _SIDEBAR_STATE_WRITE_LOCK:
|
||||
target = normalize_webui_sidebar_state(raw)
|
||||
current: object = None
|
||||
try:
|
||||
with webui_sidebar_state_path().open("rb") as source:
|
||||
encoded = source.read(_MAX_STATE_FILE_BYTES + 1)
|
||||
if len(encoded) <= _MAX_STATE_FILE_BYTES:
|
||||
current = json.loads(encoded)
|
||||
except (OSError, ValueError):
|
||||
# Comparison is optional; the normal write still validates and
|
||||
# reports errors, and can replace a missing or damaged file.
|
||||
pass
|
||||
if isinstance(current, dict) and "archived_keys" not in current:
|
||||
previous_timestamp = cast(dict[str, Any], current).get("updated_at")
|
||||
if previous_timestamp is None or isinstance(previous_timestamp, str):
|
||||
target["updated_at"] = previous_timestamp
|
||||
if target == current:
|
||||
return target
|
||||
return _persist_webui_sidebar_state(target)
|
||||
|
||||
|
||||
def _write_webui_sidebar_state(raw: dict[str, Any]) -> dict[str, Any]:
|
||||
state = normalize_webui_sidebar_state(raw)
|
||||
return _persist_webui_sidebar_state(normalize_webui_sidebar_state(raw))
|
||||
|
||||
|
||||
def _persist_webui_sidebar_state(state: dict[str, Any]) -> dict[str, Any]:
|
||||
state["updated_at"] = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
|
||||
encoded = json.dumps(
|
||||
state,
|
||||
@@ -268,3 +323,75 @@ def _write_webui_sidebar_state(raw: dict[str, Any]) -> dict[str, Any]:
|
||||
finally:
|
||||
os.close(dir_fd)
|
||||
return state
|
||||
|
||||
|
||||
async def ensure_webui_archives_imported(sessions: SessionState) -> None:
|
||||
if not await sessions.archives_imported():
|
||||
legacy = await asyncio.to_thread(read_legacy_archived_keys)
|
||||
await sessions.import_archives(legacy)
|
||||
await asyncio.to_thread(remove_legacy_archived_keys)
|
||||
|
||||
|
||||
async def ensure_webui_session_archives(sessions: SessionState) -> list[str]:
|
||||
await ensure_webui_archives_imported(sessions)
|
||||
return await sessions.archived_keys()
|
||||
|
||||
|
||||
class WebUISidebarState:
|
||||
def __init__(self, sessions: SessionState | None) -> None:
|
||||
self._sessions = sessions
|
||||
self._lock = asyncio.Lock()
|
||||
self._readers = 0
|
||||
self._readers_drained = asyncio.Event()
|
||||
self._readers_drained.set()
|
||||
|
||||
@asynccontextmanager
|
||||
async def _read_snapshot(self) -> AsyncGenerator[None, None]:
|
||||
# The FIFO admission lock lets readers overlap without overtaking a writer.
|
||||
async with self._lock:
|
||||
self._readers += 1
|
||||
self._readers_drained.clear()
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
self._readers -= 1
|
||||
if not self._readers:
|
||||
self._readers_drained.set()
|
||||
|
||||
async def read(self) -> dict[str, Any]:
|
||||
async with self._read_snapshot():
|
||||
if self._sessions is None:
|
||||
preferences = await asyncio.to_thread(read_webui_sidebar_state)
|
||||
return {**preferences, "archived_keys": []}
|
||||
await ensure_webui_archives_imported(self._sessions)
|
||||
preferences, archived = await asyncio.gather(
|
||||
asyncio.to_thread(read_webui_sidebar_state), self._sessions.archived_keys(),
|
||||
)
|
||||
return {**preferences, "archived_keys": archived}
|
||||
|
||||
async def write(self, raw: dict[str, Any]) -> dict[str, Any]:
|
||||
# Keep each successful wire snapshot ordered across the two stores.
|
||||
async with self._lock:
|
||||
await self._readers_drained.wait()
|
||||
return await shield_and_drain(self._write_snapshot(raw))
|
||||
|
||||
async def _write_snapshot(self, raw: dict[str, Any]) -> dict[str, Any]:
|
||||
if self._sessions is None:
|
||||
preferences = await asyncio.to_thread(write_webui_sidebar_preferences, raw)
|
||||
return {**preferences, "archived_keys": []}
|
||||
await ensure_webui_archives_imported(self._sessions)
|
||||
preferences, archived = await asyncio.gather(
|
||||
asyncio.to_thread(write_webui_sidebar_preferences, raw), self._sessions.archived_keys(),
|
||||
return_exceptions=True,
|
||||
)
|
||||
# Settle both branches before releasing admission, including failed reads.
|
||||
if isinstance(preferences, BaseException):
|
||||
raise preferences
|
||||
if isinstance(archived, BaseException):
|
||||
raise archived
|
||||
if "archived_keys" in raw:
|
||||
target = _clean_string_list(raw["archived_keys"])
|
||||
# Unchanged flags complete at the committed read, without a writer.
|
||||
if set(target) != set(archived):
|
||||
archived = await self._sessions.set_archived_keys(target)
|
||||
return {**preferences, "archived_keys": archived}
|
||||
@@ -112,8 +112,8 @@ from nanobot.webui.session_list_index import (
|
||||
list_webui_sessions,
|
||||
)
|
||||
from nanobot.webui.sidebar_state import (
|
||||
read_webui_sidebar_state,
|
||||
write_webui_sidebar_state,
|
||||
WebUISidebarState,
|
||||
ensure_webui_archives_imported,
|
||||
)
|
||||
from nanobot.webui.skills_api import (
|
||||
SkillManagementError,
|
||||
@@ -395,6 +395,7 @@ class GatewayHTTPHandler:
|
||||
) -> None:
|
||||
self.config = config
|
||||
self.session_manager = session_manager
|
||||
self.sidebar_state = WebUISidebarState(session_manager.state if session_manager is not None else None)
|
||||
self.static_dist_path = static_dist_path
|
||||
self.runtime_model_name = runtime_model_name
|
||||
self.bus = bus
|
||||
@@ -1370,6 +1371,7 @@ class GatewayHTTPHandler:
|
||||
"automations": serialize_automation_jobs(automation_jobs),
|
||||
}
|
||||
)
|
||||
await ensure_webui_archives_imported(self.session_manager.state)
|
||||
if automation_jobs:
|
||||
for job in automation_jobs:
|
||||
if isinstance(job, LocalTrigger):
|
||||
@@ -1707,9 +1709,9 @@ class GatewayHTTPHandler:
|
||||
return _http_error(500, "failed to save reminder preference")
|
||||
return _http_json_response({"show": show})
|
||||
if got == "/api/webui/sidebar-state":
|
||||
return self._handle_webui_sidebar_state(request)
|
||||
return await self._handle_webui_sidebar_state(request)
|
||||
if got == "/api/webui/sidebar-state/update":
|
||||
return self._handle_webui_sidebar_state_update(request)
|
||||
return await self._handle_webui_sidebar_state_update(request)
|
||||
return None
|
||||
|
||||
def _handle_commands(self, request: WsRequest) -> Response:
|
||||
@@ -1951,12 +1953,17 @@ class GatewayHTTPHandler:
|
||||
return _http_error(404, "skill not found")
|
||||
return _http_json_response(payload)
|
||||
|
||||
def _handle_webui_sidebar_state(self, request: WsRequest) -> Response:
|
||||
async def _handle_webui_sidebar_state(self, request: WsRequest) -> Response:
|
||||
if not self.check_api_token(request):
|
||||
return _http_error(401, "Unauthorized")
|
||||
return _http_json_response(read_webui_sidebar_state())
|
||||
try:
|
||||
state = await self.sidebar_state.read()
|
||||
except (OSError, ValueError):
|
||||
self._log.exception("failed to read webui sidebar state")
|
||||
return _http_error(500, "failed to read sidebar state")
|
||||
return _http_json_response(state)
|
||||
|
||||
def _handle_webui_sidebar_state_update(self, request: WsRequest) -> Response:
|
||||
async def _handle_webui_sidebar_state_update(self, request: WsRequest) -> Response:
|
||||
if not self.check_api_token(request):
|
||||
return _http_error(401, "Unauthorized")
|
||||
payload = _mutation_payload(request)
|
||||
@@ -1966,7 +1973,7 @@ class GatewayHTTPHandler:
|
||||
if not isinstance(state_value, dict):
|
||||
return _http_error(400, "state must be an object")
|
||||
try:
|
||||
state = write_webui_sidebar_state(cast(dict[str, Any], state_value))
|
||||
state = await self.sidebar_state.write(cast(dict[str, Any], state_value))
|
||||
except ValueError as e:
|
||||
return _http_error(400, str(e))
|
||||
except OSError:
|
||||
|
||||
Reference in new issue
Block a user