* 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
264 lines
9.6 KiB
Python
264 lines
9.6 KiB
Python
"""Session storage location: outside the agent workspace (ADR-0001)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import uuid
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from nanobot.config.loader import load_config
|
|
from nanobot.session.jsonl_migration import WORKSPACE_BACKUP_DIR
|
|
from nanobot.session.location import SessionLocation
|
|
from nanobot.session.manager import SessionManager
|
|
|
|
|
|
def _write_legacy_session(
|
|
old_dir: Path,
|
|
key: str,
|
|
content: str,
|
|
*,
|
|
updated_at: str = "2026-01-01T00:00:00",
|
|
) -> Path:
|
|
"""Write a valid session file in the legacy in-workspace location."""
|
|
old_dir.mkdir(parents=True, exist_ok=True)
|
|
path = old_dir / f"{SessionLocation.storage_key(key)}.jsonl"
|
|
path.write_text(
|
|
json.dumps(
|
|
{
|
|
"_type": "metadata",
|
|
"key": key,
|
|
"created_at": "2026-01-01T00:00:00",
|
|
"updated_at": updated_at,
|
|
"metadata": {},
|
|
"last_consolidated": 0,
|
|
}
|
|
)
|
|
+ "\n"
|
|
+ json.dumps({"role": "user", "content": content})
|
|
+ "\n",
|
|
encoding="utf-8",
|
|
)
|
|
return path
|
|
|
|
|
|
def test_sessions_are_stored_outside_workspace(tmp_path: Path) -> None:
|
|
workspace = tmp_path / "workspace"
|
|
manager = SessionManager(workspace=workspace)
|
|
|
|
session = manager.get_or_create("telegram:1")
|
|
session.add_message("user", "hello")
|
|
manager.save(session)
|
|
|
|
# The session file must NOT live inside the workspace.
|
|
workspace_sessions = workspace / "sessions"
|
|
assert not workspace_sessions.exists() or not any(workspace_sessions.glob("*.jsonl"))
|
|
|
|
# The out-of-workspace store records which workspace it belongs to and the
|
|
# workspace carries only a non-secret stable identity marker.
|
|
marker = manager.sessions_dir / ".workspace"
|
|
assert marker.read_text(encoding="utf-8").strip() == str(workspace.resolve())
|
|
workspace_id = (workspace / ".nanobot" / "workspace-id").read_text(encoding="utf-8").strip()
|
|
assert manager.sessions_dir.name == workspace_id
|
|
assert manager.sessions_dir.parent.name == "sessions"
|
|
|
|
# And it must still round-trip through a fresh manager for the same workspace.
|
|
reloaded = SessionManager(workspace=workspace).get_or_create("telegram:1")
|
|
assert reloaded.messages[-1]["content"] == "hello"
|
|
|
|
|
|
def test_workspace_identity_marker_contains_no_session_content(tmp_path: Path) -> None:
|
|
workspace = tmp_path / "workspace"
|
|
secret = f"session-secret-{uuid.uuid4()}"
|
|
manager = SessionManager(workspace=workspace)
|
|
session = manager.get_or_create("telegram:secret")
|
|
session.add_message("user", secret)
|
|
manager.save(session)
|
|
|
|
marker = workspace / ".nanobot" / "workspace-id"
|
|
assert marker.read_text(encoding="utf-8").strip() == manager.sessions_dir.name
|
|
assert secret not in marker.read_text(encoding="utf-8")
|
|
assert not any(
|
|
secret in path.read_text(encoding="utf-8")
|
|
for path in workspace.rglob("*")
|
|
if path.is_file()
|
|
)
|
|
|
|
|
|
def test_different_workspaces_are_isolated(tmp_path: Path) -> None:
|
|
workspace_a = tmp_path / "ws_a"
|
|
workspace_b = tmp_path / "ws_b"
|
|
|
|
manager_a = SessionManager(workspace=workspace_a)
|
|
session = manager_a.get_or_create("telegram:1")
|
|
session.add_message("user", "secret-for-a")
|
|
manager_a.save(session)
|
|
|
|
# A second workspace must not see A's session.
|
|
assert manager_a.sessions_dir != SessionManager(workspace=workspace_b).sessions_dir
|
|
in_b = SessionManager(workspace=workspace_b).get_or_create("telegram:1")
|
|
assert in_b.messages == []
|
|
|
|
|
|
def test_sessions_follow_active_custom_config_data_root(
|
|
tmp_path: Path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
custom_instance = tmp_path / "instance-b"
|
|
custom_config = custom_instance / "config.json"
|
|
default_home = tmp_path / "read-only-home"
|
|
default_home.mkdir()
|
|
default_home.chmod(0o500)
|
|
monkeypatch.setenv("HOME", str(default_home))
|
|
config = load_config(custom_config)
|
|
data_dir = config.runtime_data_dir
|
|
assert data_dir == custom_instance
|
|
|
|
manager = SessionManager(
|
|
workspace=tmp_path / "workspace-b",
|
|
sessions_root=data_dir / "sessions",
|
|
)
|
|
session = manager.get_or_create("telegram:custom")
|
|
session.add_message("user", "custom-instance")
|
|
manager.save(session)
|
|
|
|
assert manager.sessions_dir.parent == custom_instance / "sessions"
|
|
assert (manager.sessions_dir / "sessions.sqlite3").exists()
|
|
assert not (default_home / ".nanobot" / "sessions").exists()
|
|
|
|
|
|
def test_session_root_inside_workspace_fails_closed(tmp_path: Path) -> None:
|
|
workspace = tmp_path / "workspace"
|
|
|
|
with pytest.raises(RuntimeError, match="must be outside the agent workspace"):
|
|
SessionManager(workspace=workspace, sessions_root=workspace / "sessions")
|
|
|
|
|
|
def test_workspace_move_preserves_session_identity(tmp_path: Path) -> None:
|
|
original = tmp_path / "project-old"
|
|
manager = SessionManager(workspace=original)
|
|
session = manager.get_or_create("telegram:1")
|
|
session.add_message("user", "survives-move")
|
|
manager.save(session)
|
|
|
|
moved = tmp_path / "project-new"
|
|
original.rename(moved)
|
|
reloaded = SessionManager(workspace=moved)
|
|
|
|
assert reloaded.sessions_dir == manager.sessions_dir
|
|
assert reloaded.get_or_create("telegram:1").messages[-1]["content"] == "survives-move"
|
|
assert (reloaded.sessions_dir / ".workspace").read_text(encoding="utf-8").strip() == str(
|
|
moved.resolve()
|
|
)
|
|
|
|
|
|
def test_deleted_workspace_identity_marker_is_recovered(tmp_path: Path) -> None:
|
|
workspace = tmp_path / "project"
|
|
manager = SessionManager(workspace=workspace)
|
|
session = manager.get_or_create("telegram:1")
|
|
session.add_message("user", "survives-cleanup")
|
|
manager.save(session)
|
|
|
|
shutil.rmtree(workspace / ".nanobot")
|
|
reloaded = SessionManager(workspace=workspace)
|
|
|
|
assert reloaded.sessions_dir == manager.sessions_dir
|
|
assert reloaded.get_or_create("telegram:1").messages[-1]["content"] == "survives-cleanup"
|
|
assert (workspace / ".nanobot" / "workspace-id").read_text(encoding="utf-8").strip() == (
|
|
manager.sessions_dir.name
|
|
)
|
|
|
|
|
|
def test_copied_workspace_gets_isolated_session_identity(tmp_path: Path) -> None:
|
|
original = tmp_path / "project-a"
|
|
original.mkdir()
|
|
manager = SessionManager(workspace=original)
|
|
session = manager.get_or_create("telegram:1")
|
|
session.add_message("user", "secret-for-a")
|
|
manager.save(session)
|
|
|
|
copied = tmp_path / "project-b"
|
|
shutil.copytree(original, copied)
|
|
copied_manager = SessionManager(workspace=copied)
|
|
|
|
assert copied_manager.sessions_dir != manager.sessions_dir
|
|
assert copied_manager.get_or_create("telegram:1").messages == []
|
|
assert (copied / ".nanobot" / "workspace-id").read_text(encoding="utf-8") != (
|
|
original / ".nanobot" / "workspace-id"
|
|
).read_text(encoding="utf-8")
|
|
|
|
|
|
def test_equivalent_workspace_paths_share_one_store(tmp_path: Path) -> None:
|
|
real_workspace = tmp_path / "real_ws"
|
|
real_workspace.mkdir()
|
|
link_workspace = tmp_path / "link_ws"
|
|
if os.name == "nt":
|
|
completed = subprocess.run(
|
|
["cmd", "/c", "mklink", "/J", str(link_workspace), str(real_workspace)],
|
|
capture_output=True,
|
|
text=True,
|
|
check=False,
|
|
)
|
|
if completed.returncode != 0:
|
|
pytest.skip(completed.stderr.strip() or completed.stdout.strip())
|
|
else:
|
|
try:
|
|
link_workspace.symlink_to(real_workspace, target_is_directory=True)
|
|
except OSError as exc:
|
|
pytest.skip(f"directory symlink unavailable: {exc}")
|
|
|
|
# Save via the real path, then read via an equivalent directory link.
|
|
manager = SessionManager(workspace=real_workspace)
|
|
session = manager.get_or_create("telegram:1")
|
|
session.add_message("user", "via-real")
|
|
manager.save(session)
|
|
|
|
via_link = SessionManager(workspace=link_workspace).get_or_create("telegram:1")
|
|
assert via_link.messages[-1]["content"] == "via-real"
|
|
|
|
|
|
def test_migration_rejects_symlinked_session_file(tmp_path, create_symlink):
|
|
workspace = tmp_path / "workspace"
|
|
path = _write_legacy_session(workspace / "sessions", "cli:test", "original")
|
|
target = tmp_path / "outside.jsonl"
|
|
path.rename(target)
|
|
create_symlink(path, target)
|
|
with pytest.raises(RuntimeError, match="migration failed"):
|
|
SessionManager(workspace)
|
|
assert "original" in target.read_text()
|
|
|
|
|
|
def test_migration_rejects_symlinked_source_directory(tmp_path, create_symlink):
|
|
workspace = tmp_path / "workspace"
|
|
workspace.mkdir()
|
|
outside = tmp_path / "outside"
|
|
outside.mkdir()
|
|
create_symlink(workspace / "sessions", outside, target_is_directory=True)
|
|
with pytest.raises(ValueError, match="symlink"):
|
|
SessionManager(workspace)
|
|
|
|
|
|
def test_workspace_migration_rejects_symlinked_backup_directory(tmp_path, create_symlink):
|
|
workspace = tmp_path / "workspace"
|
|
root = tmp_path / "root"
|
|
source = _write_legacy_session(workspace / "sessions", "cli:test", "original")
|
|
original = source.read_bytes()
|
|
location = SessionLocation(workspace, sessions_root=root)
|
|
outside = tmp_path / "outside"
|
|
outside.mkdir()
|
|
backup = location.sessions_dir / WORKSPACE_BACKUP_DIR
|
|
create_symlink(backup, outside, target_is_directory=True)
|
|
|
|
with pytest.raises(RuntimeError, match="Could not move legacy sessions"):
|
|
SessionManager(workspace, sessions_root=root)
|
|
assert source.read_bytes() == original
|
|
assert list(outside.iterdir()) == []
|
|
backup.unlink()
|
|
manager = SessionManager(workspace, sessions_root=root)
|
|
assert not source.exists()
|
|
assert (manager.sessions_dir / WORKSPACE_BACKUP_DIR / source.name).read_bytes() == original
|