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:
chengyongru authored and GitHub committed 2026-10-16 17:28:47 +08:00
1 parent 33ff520d12
commit 8f6a5594fb
144 files changed
+8293 -4912

No files matched your search

+38 -120
View File
@@ -8,12 +8,13 @@ import shutil
import subprocess
import uuid
from pathlib import Path
from unittest.mock import patch
import pytest
from nanobot.config.loader import load_config
from nanobot.session.manager import JsonlSessionStore, SessionManager
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(
@@ -25,7 +26,7 @@ def _write_legacy_session(
) -> Path:
"""Write a valid session file in the legacy in-workspace location."""
old_dir.mkdir(parents=True, exist_ok=True)
path = old_dir / f"{JsonlSessionStore.storage_key(key)}.jsonl"
path = old_dir / f"{SessionLocation.storage_key(key)}.jsonl"
path.write_text(
json.dumps(
{
@@ -126,7 +127,7 @@ def test_sessions_follow_active_custom_config_data_root(
manager.save(session)
assert manager.sessions_dir.parent == custom_instance / "sessions"
assert manager._get_session_path(session.key).exists()
assert (manager.sessions_dir / "sessions.sqlite3").exists()
assert not (default_home / ".nanobot" / "sessions").exists()
@@ -220,126 +221,43 @@ def test_equivalent_workspace_paths_share_one_store(tmp_path: Path) -> None:
assert via_link.messages[-1]["content"] == "via-real"
def test_legacy_in_workspace_sessions_are_migrated(tmp_path: Path) -> None:
def test_migration_rejects_symlinked_session_file(tmp_path, create_symlink):
workspace = tmp_path / "workspace"
key = "telegram:1"
old_file = _write_legacy_session(workspace / "sessions", key, "migrated-msg")
manager = SessionManager(workspace=workspace)
# The session is readable through the normal store.
loaded = manager.get_or_create(key)
assert loaded.messages[-1]["content"] == "migrated-msg"
# The legacy in-workspace file has been moved away...
assert not old_file.exists()
# ...into the out-of-workspace store.
assert (manager.sessions_dir / old_file.name).exists()
# Migration is idempotent: a second construction must not corrupt anything.
again = SessionManager(workspace=workspace).get_or_create(key)
assert again.messages[-1]["content"] == "migrated-msg"
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_keeps_source_when_install_fails(tmp_path: Path) -> None:
def test_migration_rejects_symlinked_source_directory(tmp_path, create_symlink):
workspace = tmp_path / "workspace"
key = "telegram:partial"
old_file = _write_legacy_session(workspace / "sessions", key, "still-safe")
with patch.object(JsonlSessionStore, "_install_snapshot", side_effect=OSError("disk full")):
manager = SessionManager(workspace=workspace)
assert old_file.exists()
assert not (manager.sessions_dir / old_file.name).exists()
retried = SessionManager(workspace=workspace)
assert retried.get_or_create(key).messages[-1]["content"] == "still-safe"
assert not old_file.exists()
def test_migration_preserves_newest_valid_conflict(tmp_path: Path) -> None:
workspace = tmp_path / "workspace"
key = "telegram:conflict"
old_file = _write_legacy_session(workspace / "sessions", key, "newer-workspace")
manager = SessionManager(workspace=workspace)
# Recreate an older legacy source while a newer destination already exists.
manager_session = manager.get_or_create(key)
manager_session.add_message("assistant", "newer-destination")
manager.save(manager_session)
_write_legacy_session(
workspace / "sessions",
key,
"older-workspace",
updated_at="2025-01-01T00:00:00",
)
retried = SessionManager(workspace=workspace)
loaded = retried.get_or_create(key)
assert loaded.messages[-1]["content"] == "newer-destination"
conflicts = list((retried.sessions_dir / ".migration-conflicts").glob("*.jsonl"))
assert len(conflicts) == 1
assert "older-workspace" in conflicts[0].read_text(encoding="utf-8")
assert not old_file.exists()
def test_explicit_rollback_restore_copies_sessions_back_without_deleting_new_store(
tmp_path: Path,
) -> None:
workspace = tmp_path / "workspace"
manager = SessionManager(workspace=workspace)
session = manager.get_or_create("telegram:rollback")
session.add_message("user", "available-to-old-version")
manager.save(session, fsync=True)
result = manager.restore_sessions_to_workspace()
legacy_file = workspace / "sessions" / manager._get_session_path(session.key).name
assert result.restored == 1
assert result.unchanged == 0
assert result.conflicts == ()
assert legacy_file.exists()
assert manager._get_session_path(session.key).exists()
assert "available-to-old-version" in legacy_file.read_text(encoding="utf-8")
repeated = manager.restore_sessions_to_workspace()
assert repeated.restored == 0
assert repeated.unchanged == 1
assert repeated.conflicts == ()
def test_legacy_migration_rejects_symlinked_session_file(tmp_path: Path) -> None:
workspace = tmp_path / "workspace"
old_dir = workspace / "sessions"
old_dir.mkdir(parents=True)
key = "telegram:symlink"
outside = _write_legacy_session(tmp_path / "outside", key, "outside-secret")
source = old_dir / outside.name
try:
source.symlink_to(outside)
except OSError as exc:
pytest.skip(f"file symlink unavailable: {exc}")
manager = SessionManager(workspace=workspace)
assert source.is_symlink()
assert not (manager.sessions_dir / source.name).exists()
assert manager.get_or_create(key).messages == []
def test_legacy_migration_rejects_symlinked_sessions_directory(tmp_path: Path) -> None:
workspace = tmp_path / "workspace"
outside = tmp_path / "outside"
key = "telegram:directory-symlink"
outside_file = _write_legacy_session(outside, key, "outside-secret")
workspace.mkdir()
try:
(workspace / "sessions").symlink_to(outside, target_is_directory=True)
except OSError as exc:
pytest.skip(f"directory symlink unavailable: {exc}")
outside = tmp_path / "outside"
outside.mkdir()
create_symlink(workspace / "sessions", outside, target_is_directory=True)
with pytest.raises(ValueError, match="symlink"):
SessionManager(workspace)
manager = SessionManager(workspace=workspace)
assert outside_file.exists()
assert not (manager.sessions_dir / outside_file.name).exists()
assert manager.get_or_create(key).messages == []
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