mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-08-10 22:38:40 +03:00
perf(webui): accelerate JSONL session list and thread loading (#5194)
This commit is contained in:
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user