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
This commit is contained in:
1 parent
33ff520d12
commit
8f6a5594fb
144 files changed
+8293
-4912
No files matched your search
@@ -7,7 +7,8 @@ from nanobot.security.workspace_access import (
|
||||
WorkspaceScopeError,
|
||||
default_workspace_scope,
|
||||
)
|
||||
from nanobot.session.manager import SessionManager, SessionStore
|
||||
from nanobot.session.manager import SessionManager
|
||||
from nanobot.session.sqlite_store import SqliteSessionStore
|
||||
from nanobot.webui.workspaces import (
|
||||
WebUIWorkspaceController,
|
||||
read_webui_default_access_mode,
|
||||
@@ -87,7 +88,7 @@ def test_legacy_restricted_webui_default_access_mode_maps_to_default(tmp_path, m
|
||||
assert read_webui_default_access_mode() == "default"
|
||||
|
||||
|
||||
def test_webui_default_access_applies_to_unscoped_old_sessions(tmp_path, monkeypatch) -> None:
|
||||
async def test_webui_default_access_applies_to_unscoped_old_sessions(tmp_path, monkeypatch) -> None:
|
||||
monkeypatch.setattr("nanobot.webui.workspaces.get_webui_dir", lambda: tmp_path / "webui")
|
||||
default = tmp_path / "default"
|
||||
default.mkdir()
|
||||
@@ -101,7 +102,7 @@ def test_webui_default_access_applies_to_unscoped_old_sessions(tmp_path, monkeyp
|
||||
)
|
||||
|
||||
scope = controller.scope_for_session_key("websocket:old-chat")
|
||||
new_scope = controller.scope_for_new_chat(
|
||||
new_scope = await controller.scope_for_new_chat(
|
||||
{},
|
||||
can_change_project=True,
|
||||
)
|
||||
@@ -138,7 +139,7 @@ def test_indexed_scope_preserves_missing_and_explicit_null_semantics(tmp_path, m
|
||||
assert explicit_null.access_mode == "restricted"
|
||||
|
||||
|
||||
def test_webui_default_access_does_not_override_explicit_session_scope(tmp_path, monkeypatch) -> None:
|
||||
async 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"
|
||||
project = tmp_path / "project"
|
||||
@@ -151,7 +152,7 @@ def test_webui_default_access_does_not_override_explicit_session_scope(tmp_path,
|
||||
default_restrict_to_workspace=True,
|
||||
)
|
||||
explicit = default_workspace_scope(project, restrict_to_workspace=False)
|
||||
controller.persist_scope("explicit-chat", explicit)
|
||||
await controller.persist_scope("explicit-chat", explicit)
|
||||
|
||||
scope = controller.scope_for_session_key("websocket:explicit-chat")
|
||||
|
||||
@@ -159,7 +160,7 @@ def test_webui_default_access_does_not_override_explicit_session_scope(tmp_path,
|
||||
assert scope.access_mode == "full"
|
||||
|
||||
|
||||
def test_scope_for_session_key_reads_metadata_without_full_history(
|
||||
async def test_scope_for_session_key_reads_metadata_without_full_history(
|
||||
tmp_path,
|
||||
monkeypatch,
|
||||
) -> None:
|
||||
@@ -175,7 +176,7 @@ def test_scope_for_session_key_reads_metadata_without_full_history(
|
||||
default_restrict_to_workspace=True,
|
||||
)
|
||||
explicit = default_workspace_scope(project, restrict_to_workspace=False)
|
||||
controller.persist_scope("metadata-only", explicit)
|
||||
await controller.persist_scope("metadata-only", explicit)
|
||||
|
||||
def fail_full_read(_key: str) -> None:
|
||||
raise AssertionError("scope lookup should not read full session history")
|
||||
@@ -188,7 +189,7 @@ def test_scope_for_session_key_reads_metadata_without_full_history(
|
||||
assert scope.access_mode == "full"
|
||||
|
||||
|
||||
def test_new_chat_scope_is_persisted_only_after_first_message(tmp_path, monkeypatch) -> None:
|
||||
async def test_new_chat_scope_is_persisted_only_after_first_message(tmp_path, monkeypatch) -> None:
|
||||
monkeypatch.setattr("nanobot.webui.workspaces.get_webui_dir", lambda: tmp_path / "webui")
|
||||
default = tmp_path / "default"
|
||||
project = tmp_path / "project"
|
||||
@@ -202,12 +203,12 @@ def test_new_chat_scope_is_persisted_only_after_first_message(tmp_path, monkeypa
|
||||
)
|
||||
scope = default_workspace_scope(project, restrict_to_workspace=False)
|
||||
|
||||
controller.stage_scope("draft-chat", scope)
|
||||
await controller.stage_scope("draft-chat", scope)
|
||||
|
||||
assert sessions.list_sessions() == []
|
||||
assert controller.scope_for_session_key("websocket:draft-chat") == scope
|
||||
|
||||
controller.persist_scope("draft-chat", scope)
|
||||
await controller.persist_scope("draft-chat", scope)
|
||||
|
||||
assert [item["key"] for item in sessions.list_sessions()] == ["websocket:draft-chat"]
|
||||
|
||||
@@ -227,7 +228,7 @@ def test_scope_for_session_key_always_reads_the_active_store(tmp_path, monkeypat
|
||||
residual.metadata[WORKSPACE_SCOPE_METADATA_KEY] = full_scope.metadata()
|
||||
residual_sessions.save(residual)
|
||||
|
||||
store = MagicMock(spec=SessionStore)
|
||||
store = MagicMock(spec=SqliteSessionStore)
|
||||
store.read_metadata.side_effect = [
|
||||
{
|
||||
"key": "websocket:cached",
|
||||
@@ -242,7 +243,8 @@ def test_scope_for_session_key_always_reads_the_active_store(tmp_path, monkeypat
|
||||
"metadata": {WORKSPACE_SCOPE_METADATA_KEY: restricted_scope.metadata()},
|
||||
},
|
||||
]
|
||||
sessions = SessionManager(workspace, store=store)
|
||||
sessions = SessionManager(workspace)
|
||||
monkeypatch.setattr(sessions._store, "read_metadata", store.read_metadata)
|
||||
controller = WebUIWorkspaceController(
|
||||
session_manager=sessions,
|
||||
default_workspace=default,
|
||||
@@ -259,7 +261,7 @@ def test_scope_for_session_key_always_reads_the_active_store(tmp_path, monkeypat
|
||||
assert store.read_metadata.call_count == 2
|
||||
|
||||
|
||||
def test_remote_existing_chat_can_reduce_its_workspace_access(tmp_path, monkeypatch) -> None:
|
||||
async 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"
|
||||
project = tmp_path / "project"
|
||||
@@ -271,12 +273,12 @@ def test_remote_existing_chat_can_reduce_its_workspace_access(tmp_path, monkeypa
|
||||
default_workspace=default,
|
||||
default_restrict_to_workspace=True,
|
||||
)
|
||||
controller.persist_scope(
|
||||
await controller.persist_scope(
|
||||
"remote-chat",
|
||||
default_workspace_scope(project, restrict_to_workspace=False),
|
||||
)
|
||||
|
||||
scope = controller.scope_for_set_request(
|
||||
scope = await controller.scope_for_set_request(
|
||||
{
|
||||
"workspace_scope": {
|
||||
"project_path": str(project),
|
||||
@@ -301,7 +303,7 @@ def test_remote_existing_chat_can_reduce_its_workspace_access(tmp_path, monkeypa
|
||||
(False, "other", "full"),
|
||||
],
|
||||
)
|
||||
def test_new_chat_allows_project_selection_in_either_access_mode(
|
||||
async def test_new_chat_allows_project_selection_in_either_access_mode(
|
||||
tmp_path,
|
||||
monkeypatch,
|
||||
default_restricted: bool,
|
||||
@@ -320,7 +322,7 @@ def test_new_chat_allows_project_selection_in_either_access_mode(
|
||||
)
|
||||
requested_path = tmp_path / project_name
|
||||
|
||||
scope = controller.scope_for_new_chat(
|
||||
scope = await controller.scope_for_new_chat(
|
||||
{
|
||||
"workspace_scope": {
|
||||
"project_path": str(requested_path),
|
||||
@@ -333,7 +335,7 @@ def test_new_chat_allows_project_selection_in_either_access_mode(
|
||||
assert scope.access_mode == access_mode
|
||||
|
||||
|
||||
def test_project_selection_can_remain_disabled_for_untrusted_connections(
|
||||
async def test_project_selection_can_remain_disabled_for_untrusted_connections(
|
||||
tmp_path,
|
||||
monkeypatch,
|
||||
) -> None:
|
||||
@@ -352,7 +354,7 @@ def test_project_selection_can_remain_disabled_for_untrusted_connections(
|
||||
WorkspaceScopeError,
|
||||
match="project selection is unavailable for this connection",
|
||||
):
|
||||
controller.scope_for_new_chat(
|
||||
await controller.scope_for_new_chat(
|
||||
{
|
||||
"workspace_scope": {
|
||||
"project_path": str(other),
|
||||
@@ -363,7 +365,7 @@ def test_project_selection_can_remain_disabled_for_untrusted_connections(
|
||||
)
|
||||
|
||||
|
||||
def test_recent_projects_survive_controller_restart_without_changing_access(tmp_path, monkeypatch) -> None:
|
||||
async def test_recent_projects_survive_controller_restart_without_changing_access(tmp_path, monkeypatch) -> None:
|
||||
monkeypatch.setattr("nanobot.webui.workspaces.get_webui_dir", lambda: tmp_path / "webui")
|
||||
default = tmp_path / "default"
|
||||
project = tmp_path / "project"
|
||||
@@ -372,12 +374,12 @@ def test_recent_projects_survive_controller_restart_without_changing_access(tmp_
|
||||
controller = WebUIWorkspaceController(
|
||||
session_manager=None, default_workspace=default, default_restrict_to_workspace=True,
|
||||
)
|
||||
scope = controller.scope_for_new_chat(
|
||||
scope = await controller.scope_for_new_chat(
|
||||
{WORKSPACE_SCOPE_METADATA_KEY: {"project_path": str(project), "access_mode": "restricted"}},
|
||||
can_change_project=True,
|
||||
)
|
||||
controller.stage_scope("first", scope)
|
||||
controller.stage_scope("second", scope)
|
||||
await controller.stage_scope("first", scope)
|
||||
await controller.stage_scope("second", scope)
|
||||
restarted = WebUIWorkspaceController(
|
||||
session_manager=None, default_workspace=default, default_restrict_to_workspace=True,
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user