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

* refactor(session): isolate async persistence boundary

* refactor(session): keep a single session manager

* refactor(session): use stateless async adapters

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

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

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

* refactor(session): remove redundant async forwarding helpers

* fix(command): offload compact session persistence

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

* fix(webui): offload session metadata persistence

* refactor(session): centralize state ownership in SQLite

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

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

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

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

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

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

* fix: reject unresolved home paths and stabilize platform tests

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

* docs: focus configuration guide on user-facing behavior

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

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

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

* fix(session): support compatible SQLite schema evolution

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

* test: stabilize Windows lifecycle and browser checks

* refactor(session): remove premature JSONL file export

* ci: bound Windows full-suite worker concurrency

* chore: remove session state refactor notes

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

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

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

* docs: remove internal session storage guide

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

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

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

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

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

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

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

* fix(agent): preserve cancellation settlement after rebase

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

499 lines
19 KiB
Python

"""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.
"""
from __future__ import annotations
import json
import os
import re
import secrets
import stat
from dataclasses import dataclass, field
from datetime import datetime
from pathlib import Path
from typing import Any, cast
from loguru import logger
from nanobot.config.paths import get_webui_dir
from 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.model_selection import model_preset_from_metadata
from 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(
{_WORKSPACE_SCOPE_PRESENT_FIELD, _WORKSPACE_SCOPE_VALUE_FIELD}
)
_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],
}
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
rows.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),
"recovery_state": recovery_state_from_metadata(metadata),
**_indexed_workspace_scope_fields(metadata),
_ROW_SOURCE_FIELD: _SESSION_SOURCE, "file": "sessions.sqlite3",
})
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),
}
def indexed_workspace_scope(row: dict[str, Any]) -> tuple[bool, object]:
"""Return the cached sidebar scope value while preserving missing vs null."""
return (
row.get(_WORKSPACE_SCOPE_PRESENT_FIELD) is True,
cast(object, row.get(_WORKSPACE_SCOPE_VALUE_FIELD)),
)
def _indexed_workspace_scope_fields(metadata: object) -> dict[str, object]:
if not isinstance(metadata, dict):
return {
_WORKSPACE_SCOPE_PRESENT_FIELD: False,
_WORKSPACE_SCOPE_VALUE_FIELD: None,
}
metadata_data = cast(dict[str, Any], metadata)
if WORKSPACE_SCOPE_METADATA_KEY not in metadata_data:
return {
_WORKSPACE_SCOPE_PRESENT_FIELD: False,
_WORKSPACE_SCOPE_VALUE_FIELD: None,
}
raw_scope = metadata_data.get(WORKSPACE_SCOPE_METADATA_KEY)
indexed_scope: object = False
if raw_scope is None:
indexed_scope = None
elif isinstance(raw_scope, dict):
scope_data = cast(dict[object, object], raw_scope)
recognized = {
key: scope_data[key]
for key in _INDEXED_WORKSPACE_SCOPE_KEYS
if key in scope_data
}
try:
encoded = json.dumps(recognized, ensure_ascii=False)
except (TypeError, ValueError):
pass
else:
if len(encoded.encode("utf-8")) <= _MAX_INDEXED_WORKSPACE_SCOPE_BYTES:
indexed_scope = cast(object, json.loads(encoded))
return {
_WORKSPACE_SCOPE_PRESENT_FIELD: True,
_WORKSPACE_SCOPE_VALUE_FIELD: indexed_scope,
}
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
try:
return datetime.fromisoformat(value).timestamp()
except ValueError:
return 0.0
def _latest_updated_at(stored: str | None, activity: str | None) -> str | None:
if _timestamp(activity) > _timestamp(stored):
return activity
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,
}