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

+30 -82
View File
@@ -25,91 +25,39 @@ class TestDreamSessionKey:
class TestPruneDreamSessions:
def test_keeps_n_most_recent(self, tmp_path):
import os
import time
async def test_keeps_n_most_recent(self, tmp_path):
manager = SessionManager(tmp_path / "workspace", sessions_root=tmp_path / "runtime")
keys = [f"dream:20260528-{100000 + i:06d}" for i in range(15)]
for i, key in enumerate(keys):
session = manager.get_or_create(key)
session.updated_at = datetime(2026, 5, 28, 10, 0, i)
manager.save(session)
manager.save(manager.get_or_create("telegram:normal"))
manager = SessionManager(
tmp_path / "workspace",
sessions_root=tmp_path / "runtime",
)
sessions_dir = manager.sessions_dir
assert await manager.state.prune_dream_sessions(keep=10) == 5
await manager.state.aclose()
base_time = time.time() - 100
dream_paths = []
assert [manager.read_session_snapshot(key) is not None for key in keys] == [False] * 5 + [True] * 10
assert manager.read_session_snapshot("telegram:normal") is not None
for i in range(15):
key = f"dream:20260528-{100000 + i:06d}"
path = sessions_dir / f"{SessionManager._storage_key(key)}.jsonl"
path.write_text(
f'{{"_type": "metadata", "key": "{key}", '
f'"created_at": "2026-05-28T10:00:{i:02d}", '
f'"updated_at": "2026-05-28T10:00:{i:02d}"}}\n',
encoding="utf-8",
)
os.utime(path, (base_time + i, base_time + i))
dream_paths.append(path)
async def test_leaves_legacy_backup_untouched(self, tmp_path):
manager = SessionManager(tmp_path / "workspace", sessions_root=tmp_path / "runtime")
path = manager.sessions_dir / "dream_20260713-095959.jsonl"
path.write_text('legacy backup', encoding="utf-8")
await manager.state.prune_dream_sessions(keep=0)
await manager.state.aclose()
assert path.read_text(encoding="utf-8") == 'legacy backup'
normal_path = sessions_dir / "telegram_123.jsonl"
normal_path.write_text('{"_type": "metadata"}\n', encoding="utf-8")
MemoryStore.prune_dream_sessions(manager, keep=10)
assert [path.exists() for path in dream_paths] == [False] * 5 + [True] * 10
assert normal_path.exists()
def test_ignores_legacy_dream_filenames(self, tmp_path):
import os
import time
manager = SessionManager(
tmp_path / "workspace",
sessions_root=tmp_path / "runtime",
)
sessions_dir = manager.sessions_dir
base_time = time.time() - 100
current_paths = []
for i in range(2):
key = f"dream:20260713-{100000 + i:06d}"
path = sessions_dir / f"{SessionManager._storage_key(key)}.jsonl"
path.write_text(
f'{{"_type": "metadata", "key": "{key}"}}\n',
encoding="utf-8",
)
os.utime(path, (base_time + i, base_time + i))
current_paths.append(path)
legacy_path = sessions_dir / "dream_20260713-095959.jsonl"
legacy_path.write_text(
'{"_type": "metadata", "key": "dream:20260713-095959"}\n',
encoding="utf-8",
)
os.utime(legacy_path, (base_time - 1, base_time - 1))
MemoryStore.prune_dream_sessions(manager, keep=1)
assert [path.exists() for path in current_paths] == [False, True]
assert legacy_path.exists()
def test_noop_when_under_limit(self, tmp_path):
manager = SessionManager(
tmp_path / "workspace",
sessions_root=tmp_path / "runtime",
)
sessions_dir = manager.sessions_dir
async def test_noop_when_under_limit(self, tmp_path):
manager = SessionManager(tmp_path / "workspace", sessions_root=tmp_path / "runtime")
for i in range(3):
key = f"dream:20260528-{100000 + i:06d}"
path = sessions_dir / f"{SessionManager._storage_key(key)}.jsonl"
path.write_text("{}", encoding="utf-8")
manager.save(manager.get_or_create(f"dream:20260528-{100000 + i:06d}"))
assert await manager.state.prune_dream_sessions(keep=10) == 0
await manager.state.aclose()
assert len(manager.list_sessions()) == 3
MemoryStore.prune_dream_sessions(manager, keep=10)
assert len(list(sessions_dir.glob("*.jsonl"))) == 3
def test_empty_dir_noop(self, tmp_path):
manager = SessionManager(
tmp_path / "workspace",
sessions_root=tmp_path / "runtime",
)
MemoryStore.prune_dream_sessions(manager, keep=10)
assert list(manager.sessions_dir.glob("*.jsonl")) == []
async def test_empty_store_noop(self, tmp_path):
manager = SessionManager(tmp_path / "workspace", sessions_root=tmp_path / "runtime")
assert await manager.state.prune_dream_sessions(keep=10) == 0
await manager.state.aclose()
assert manager.list_sessions() == []