mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-08-07 21:08:34 +03:00
perf(webui): accelerate JSONL session list and thread loading (#5194)
This commit is contained in:
@@ -13,6 +13,7 @@ from nanobot.webui.transcript import (
|
||||
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)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import nanobot.webui.transcript as transcript_module
|
||||
from nanobot.session.history_visibility import HIDDEN_HISTORY_META
|
||||
from nanobot.webui.transcript import (
|
||||
WEBUI_TRANSCRIPT_SCHEMA_VERSION,
|
||||
@@ -38,6 +39,7 @@ def test_append_stamps_created_at_ms(tmp_path, monkeypatch) -> None:
|
||||
|
||||
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)
|
||||
|
||||
|
||||
@@ -122,6 +124,28 @@ def test_segmented_transcript_paginates_latest_and_older_without_overlap(
|
||||
]
|
||||
|
||||
|
||||
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_page_cursor_survives_active_rotation_after_latest_page(
|
||||
tmp_path,
|
||||
monkeypatch,
|
||||
@@ -148,15 +172,53 @@ def test_segment_manifest_can_be_rebuilt_when_missing_or_corrupt(tmp_path, monke
|
||||
key = "websocket:manifest"
|
||||
_write_segmented_turns(tmp_path, monkeypatch, key, "manifest", 4)
|
||||
|
||||
manifest = webui_transcript_segments_dir(key) / "manifest.json"
|
||||
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_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
|
||||
@@ -786,6 +848,83 @@ def test_build_response_restores_session_users_for_legacy_transcript(
|
||||
]
|
||||
|
||||
|
||||
def test_complete_transcript_does_not_load_session_messages(tmp_path, monkeypatch) -> None:
|
||||
monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)
|
||||
key = "websocket:complete-fast-path"
|
||||
for event in (
|
||||
{"event": "user", "chat_id": "complete-fast-path", "text": "question"},
|
||||
{"event": "message", "chat_id": "complete-fast-path", "text": "answer"},
|
||||
{"event": "turn_end", "chat_id": "complete-fast-path"},
|
||||
):
|
||||
append_transcript_object(key, event)
|
||||
|
||||
def fail_if_loaded() -> list[dict]:
|
||||
raise AssertionError("complete transcripts must not read canonical session history")
|
||||
|
||||
out = build_webui_thread_response(
|
||||
key,
|
||||
limit=4,
|
||||
direction="latest",
|
||||
session_messages_loader=fail_if_loaded,
|
||||
)
|
||||
|
||||
assert out is not None
|
||||
assert [(message["role"], message["content"]) for message in out["messages"]] == [
|
||||
("user", "question"),
|
||||
("assistant", "answer"),
|
||||
]
|
||||
|
||||
|
||||
def test_legacy_recovery_loads_session_and_builds_backfill_turns_once(
|
||||
tmp_path,
|
||||
monkeypatch,
|
||||
) -> None:
|
||||
monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)
|
||||
key = "websocket:lazy-legacy-recovery"
|
||||
append_transcript_object(
|
||||
key,
|
||||
{"event": "message", "chat_id": "lazy-legacy-recovery", "text": "answer"},
|
||||
)
|
||||
append_transcript_object(
|
||||
key,
|
||||
{
|
||||
"event": "turn_end",
|
||||
"chat_id": "lazy-legacy-recovery",
|
||||
"transcript_incomplete": True,
|
||||
},
|
||||
)
|
||||
|
||||
loader_calls = 0
|
||||
backfill_calls = 0
|
||||
original = transcript_module._session_backfill_turns
|
||||
|
||||
def load_session_messages() -> list[dict]:
|
||||
nonlocal loader_calls
|
||||
loader_calls += 1
|
||||
return [
|
||||
{"role": "user", "content": "question"},
|
||||
{"role": "assistant", "content": "answer"},
|
||||
]
|
||||
|
||||
def track_backfill(session_key: str, session_messages: list[dict]):
|
||||
nonlocal backfill_calls
|
||||
backfill_calls += 1
|
||||
return original(session_key, session_messages)
|
||||
|
||||
monkeypatch.setattr(transcript_module, "_session_backfill_turns", track_backfill)
|
||||
|
||||
out = build_webui_thread_response(key, session_messages_loader=load_session_messages)
|
||||
|
||||
assert out is not None
|
||||
assert loader_calls == 1
|
||||
assert backfill_calls == 1
|
||||
assert [(message["role"], message["content"]) for message in out["messages"]] == [
|
||||
("user", "question"),
|
||||
("assistant", "answer"),
|
||||
]
|
||||
assert out["has_pending_tool_calls"] is False
|
||||
|
||||
|
||||
def test_build_response_restores_session_users_without_duplicating_new_transcript_users(
|
||||
tmp_path,
|
||||
monkeypatch,
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
import json
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from nanobot.security.workspace_access import WorkspaceScopeError, default_workspace_scope
|
||||
from nanobot.session.manager import SessionManager
|
||||
from nanobot.security.workspace_access import (
|
||||
WORKSPACE_SCOPE_METADATA_KEY,
|
||||
WorkspaceScopeError,
|
||||
default_workspace_scope,
|
||||
)
|
||||
from nanobot.session.manager import SessionManager, SessionStore
|
||||
from nanobot.webui.workspaces import (
|
||||
WebUIWorkspaceController,
|
||||
read_webui_default_access_mode,
|
||||
@@ -135,6 +140,33 @@ def test_webui_default_access_applies_to_unscoped_old_sessions(tmp_path, monkeyp
|
||||
assert new_scope.access_mode == "full"
|
||||
|
||||
|
||||
def test_indexed_scope_preserves_missing_and_explicit_null_semantics(tmp_path, monkeypatch) -> None:
|
||||
monkeypatch.setattr("nanobot.webui.workspaces.get_webui_dir", lambda: tmp_path / "webui")
|
||||
default = tmp_path / "default"
|
||||
default.mkdir()
|
||||
write_webui_default_access_mode("full")
|
||||
controller = WebUIWorkspaceController(
|
||||
session_manager=None,
|
||||
default_workspace=default,
|
||||
default_restrict_to_workspace=True,
|
||||
)
|
||||
webui_default = controller.default_scope()
|
||||
|
||||
missing = controller.scope_for_indexed_metadata(
|
||||
None,
|
||||
scope_present=False,
|
||||
default_scope=webui_default,
|
||||
)
|
||||
explicit_null = controller.scope_for_indexed_metadata(
|
||||
None,
|
||||
scope_present=True,
|
||||
default_scope=webui_default,
|
||||
)
|
||||
|
||||
assert missing.access_mode == "full"
|
||||
assert explicit_null.access_mode == "restricted"
|
||||
|
||||
|
||||
def test_webui_default_access_does_not_override_explicit_session_scope(tmp_path, monkeypatch) -> None:
|
||||
monkeypatch.setattr("nanobot.webui.workspaces.get_webui_dir", lambda: tmp_path / "webui")
|
||||
default = tmp_path / "default"
|
||||
@@ -185,6 +217,53 @@ def test_scope_for_session_key_reads_metadata_without_full_history(
|
||||
assert scope.access_mode == "full"
|
||||
|
||||
|
||||
def test_scope_for_session_key_always_reads_the_active_store(tmp_path, monkeypatch) -> None:
|
||||
monkeypatch.setattr("nanobot.webui.workspaces.get_webui_dir", lambda: tmp_path / "webui")
|
||||
default = tmp_path / "default"
|
||||
project = tmp_path / "project"
|
||||
default.mkdir()
|
||||
project.mkdir()
|
||||
workspace = tmp_path / "session-data"
|
||||
full_scope = default_workspace_scope(project, restrict_to_workspace=False)
|
||||
restricted_scope = default_workspace_scope(project, restrict_to_workspace=True)
|
||||
|
||||
residual_sessions = SessionManager(workspace)
|
||||
residual = residual_sessions.get_or_create("websocket:cached")
|
||||
residual.metadata[WORKSPACE_SCOPE_METADATA_KEY] = full_scope.metadata()
|
||||
residual_sessions.save(residual)
|
||||
|
||||
store = MagicMock(spec=SessionStore)
|
||||
store.read_metadata.side_effect = [
|
||||
{
|
||||
"key": "websocket:cached",
|
||||
"created_at": None,
|
||||
"updated_at": None,
|
||||
"metadata": {WORKSPACE_SCOPE_METADATA_KEY: full_scope.metadata()},
|
||||
},
|
||||
{
|
||||
"key": "websocket:cached",
|
||||
"created_at": None,
|
||||
"updated_at": None,
|
||||
"metadata": {WORKSPACE_SCOPE_METADATA_KEY: restricted_scope.metadata()},
|
||||
},
|
||||
]
|
||||
sessions = SessionManager(workspace, store=store)
|
||||
controller = WebUIWorkspaceController(
|
||||
session_manager=sessions,
|
||||
default_workspace=default,
|
||||
default_restrict_to_workspace=True,
|
||||
)
|
||||
|
||||
first = controller.scope_for_session_key("websocket:cached")
|
||||
second = controller.scope_for_session_key("websocket:cached")
|
||||
|
||||
assert first.project_path == project.resolve()
|
||||
assert first.access_mode == "full"
|
||||
assert second.project_path == project.resolve()
|
||||
assert second.access_mode == "restricted"
|
||||
assert store.read_metadata.call_count == 2
|
||||
|
||||
|
||||
def test_remote_existing_chat_can_reduce_its_workspace_access(tmp_path, monkeypatch) -> None:
|
||||
monkeypatch.setattr("nanobot.webui.workspaces.get_webui_dir", lambda: tmp_path / "webui")
|
||||
default = tmp_path / "default"
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
"""Tests for shared embedded WebUI HTTP helpers."""
|
||||
|
||||
import gzip
|
||||
import json
|
||||
|
||||
from nanobot.webui.http_utils import http_json_response
|
||||
|
||||
|
||||
def test_http_json_response_compresses_large_payload_when_gzip_is_accepted() -> None:
|
||||
payload = {"message": "响应内容" * 2_000}
|
||||
|
||||
response = http_json_response(payload, accept_encoding="br, gzip; q=0.5")
|
||||
|
||||
assert response.headers["Content-Encoding"] == "gzip"
|
||||
assert response.headers["Vary"] == "Accept-Encoding"
|
||||
assert int(response.headers["Content-Length"]) == len(response.body)
|
||||
assert json.loads(gzip.decompress(response.body)) == payload
|
||||
|
||||
|
||||
def test_http_json_response_preserves_identity_when_gzip_is_rejected() -> None:
|
||||
payload = {"message": "x" * 8_000}
|
||||
|
||||
response = http_json_response(payload, accept_encoding="gzip;q=0, br")
|
||||
|
||||
assert "Content-Encoding" not in response.headers
|
||||
assert response.headers["Vary"] == "Accept-Encoding"
|
||||
assert int(response.headers["Content-Length"]) == len(response.body)
|
||||
assert json.loads(response.body) == payload
|
||||
|
||||
|
||||
def test_http_json_response_does_not_compress_small_payload() -> None:
|
||||
payload = {"ok": True}
|
||||
|
||||
response = http_json_response(payload, accept_encoding="gzip")
|
||||
|
||||
assert "Content-Encoding" not in response.headers
|
||||
assert response.headers["Vary"] == "Accept-Encoding"
|
||||
assert json.loads(response.body) == payload
|
||||
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import os
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
@@ -9,6 +10,7 @@ import pytest
|
||||
import nanobot.webui.session_list_index as session_list_index
|
||||
from nanobot.cron.session_turns import CRON_HISTORY_META
|
||||
from nanobot.providers.base import ProviderConversationState
|
||||
from nanobot.security.workspace_access import WORKSPACE_SCOPE_METADATA_KEY
|
||||
from nanobot.session.automation_turns import AUTOMATION_HISTORY_META
|
||||
from nanobot.session.history_visibility import HIDDEN_HISTORY_META
|
||||
from nanobot.session.manager import SessionManager
|
||||
@@ -28,7 +30,7 @@ def test_webui_session_list_reuses_valid_index_without_scanning_files(
|
||||
assert list_webui_sessions(manager)[0]["preview"] == "indexed preview"
|
||||
assert list_webui_sessions(manager)[0]["model_preset"] == "fast"
|
||||
|
||||
def fail_scan(session_manager: SessionManager, path: Path) -> None:
|
||||
def fail_scan(session_manager: SessionManager, path: Path, webui_dir: Path) -> None:
|
||||
raise AssertionError(f"unexpected session file scan: {path}")
|
||||
|
||||
monkeypatch.setattr(session_list_index, "_scan_session_row", fail_scan)
|
||||
@@ -40,6 +42,89 @@ def test_webui_session_list_reuses_valid_index_without_scanning_files(
|
||||
assert rows[0]["model_preset"] == "fast"
|
||||
|
||||
|
||||
def test_webui_session_list_indexes_workspace_scope_and_preserves_null(
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
manager = SessionManager(tmp_path)
|
||||
project = tmp_path / "project"
|
||||
project.mkdir()
|
||||
|
||||
scoped = manager.get_or_create("websocket:scoped")
|
||||
scoped.metadata[WORKSPACE_SCOPE_METADATA_KEY] = {
|
||||
"project_path": str(project),
|
||||
"access_mode": "full",
|
||||
"future_extension": "x" * 5000,
|
||||
}
|
||||
manager.save(scoped)
|
||||
explicit_null = manager.get_or_create("websocket:null")
|
||||
explicit_null.metadata[WORKSPACE_SCOPE_METADATA_KEY] = None
|
||||
manager.save(explicit_null)
|
||||
manager.save(manager.get_or_create("websocket:missing"))
|
||||
|
||||
rows = {row["key"]: row for row in list_webui_sessions(manager)}
|
||||
|
||||
assert session_list_index.indexed_workspace_scope(rows["websocket:scoped"]) == (
|
||||
True,
|
||||
{"project_path": str(project), "access_mode": "full"},
|
||||
)
|
||||
assert session_list_index.indexed_workspace_scope(rows["websocket:null"]) == (True, None)
|
||||
assert session_list_index.indexed_workspace_scope(rows["websocket:missing"]) == (False, None)
|
||||
|
||||
scoped.metadata[WORKSPACE_SCOPE_METADATA_KEY]["access_mode"] = "restricted"
|
||||
manager.save(scoped)
|
||||
|
||||
refreshed = {row["key"]: row for row in list_webui_sessions(manager)}
|
||||
assert session_list_index.indexed_workspace_scope(refreshed["websocket:scoped"])[1] == {
|
||||
"project_path": str(project),
|
||||
"access_mode": "restricted",
|
||||
}
|
||||
|
||||
|
||||
def test_webui_session_list_does_not_cache_old_snapshot_with_new_signature(
|
||||
tmp_path: Path,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
manager = SessionManager(tmp_path)
|
||||
session_key = "websocket:scope-race"
|
||||
session = manager.get_or_create(session_key)
|
||||
session.metadata[WORKSPACE_SCOPE_METADATA_KEY] = {
|
||||
"project_path": str(tmp_path),
|
||||
"access_mode": "full",
|
||||
}
|
||||
session.add_message("user", "hello")
|
||||
manager.save(session)
|
||||
session_path = manager._get_session_path(session_key)
|
||||
original_open = open
|
||||
scope_changed = False
|
||||
|
||||
class RacingReader(io.StringIO):
|
||||
def __next__(self) -> str:
|
||||
nonlocal scope_changed
|
||||
if not scope_changed:
|
||||
scope_changed = True
|
||||
current = manager.get_or_create(session_key)
|
||||
current.metadata[WORKSPACE_SCOPE_METADATA_KEY] = {
|
||||
"project_path": str(tmp_path),
|
||||
"access_mode": "restricted",
|
||||
}
|
||||
manager.save(current)
|
||||
return super().__next__()
|
||||
|
||||
def racing_open(path, *args, **kwargs):
|
||||
if Path(path) == session_path:
|
||||
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)[0]
|
||||
second = list_webui_sessions(manager)[0]
|
||||
|
||||
assert session_list_index.indexed_workspace_scope(first)[1]["access_mode"] == "full"
|
||||
assert session_list_index.indexed_workspace_scope(second)[1]["access_mode"] == "restricted"
|
||||
|
||||
|
||||
def test_webui_session_list_rejects_invalid_internal_model_preset_metadata(
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
@@ -74,9 +159,13 @@ def test_webui_session_list_rescans_only_changed_file(tmp_path: Path, monkeypatc
|
||||
original_scan = session_list_index._scan_session_row
|
||||
scanned: list[str] = []
|
||||
|
||||
def record_scan(session_manager: SessionManager, path: Path) -> dict | None:
|
||||
def record_scan(
|
||||
session_manager: SessionManager,
|
||||
path: Path,
|
||||
webui_dir: Path,
|
||||
) -> dict | None:
|
||||
scanned.append(path.name)
|
||||
return original_scan(session_manager, path)
|
||||
return original_scan(session_manager, path, webui_dir)
|
||||
|
||||
monkeypatch.setattr(session_list_index, "_scan_session_row", record_scan)
|
||||
|
||||
@@ -247,9 +336,13 @@ def test_webui_session_list_rescans_when_transcript_changes(
|
||||
original_scan = session_list_index._scan_session_row
|
||||
scanned: list[str] = []
|
||||
|
||||
def record_scan(session_manager: SessionManager, path: Path) -> dict | None:
|
||||
def record_scan(
|
||||
session_manager: SessionManager,
|
||||
path: Path,
|
||||
webui_dir: Path,
|
||||
) -> dict | None:
|
||||
scanned.append(path.name)
|
||||
return original_scan(session_manager, path)
|
||||
return original_scan(session_manager, path, webui_dir)
|
||||
|
||||
monkeypatch.setattr(session_list_index, "_scan_session_row", record_scan)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user