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

+29 -3
View File
@@ -1,7 +1,9 @@
import asyncio
import sqlite3
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import TimeoutError as FutureTimeout
from pathlib import Path
from threading import Event
from threading import Event, Thread
import pytest
@@ -138,7 +140,7 @@ def test_session_snapshot_and_handle_sync_share_one_lock(
resolver.list_all()
snapshot_taken = Event()
release_snapshot = Event()
original_list = manager.list_sessions
original_list = manager._store.list_metadata
def paused_list():
rows = original_list()
@@ -146,7 +148,7 @@ def test_session_snapshot_and_handle_sync_share_one_lock(
assert release_snapshot.wait(timeout=2)
return rows
monkeypatch.setattr(manager, "list_sessions", paused_list)
monkeypatch.setattr(manager._store, "list_metadata", paused_list)
with ThreadPoolExecutor(max_workers=2) as pool:
old_sync = pool.submit(resolver.list_all)
assert snapshot_taken.wait(timeout=2)
@@ -219,3 +221,27 @@ def test_private_type_cannot_be_resolved_through_an_existing_handle(tmp_path: Pa
reopened = SessionManager(tmp_path)
assert SessionHandleResolver(reopened).list_all() == []
async def test_runtime_existing_handles_do_not_wait_for_an_external_write_lock(tmp_path):
manager = SessionManager(tmp_path)
state = manager.state
await state.get("websocket:existing")
expected = await state.ensure_session_handles()
ready, release = Event(), Event()
def hold():
with sqlite3.connect(manager._store.path) as db:
db.execute("BEGIN IMMEDIATE")
ready.set()
assert release.wait(5)
thread = Thread(target=hold)
thread.start()
try:
assert await asyncio.to_thread(ready.wait, 2)
assert await asyncio.wait_for(state.ensure_session_handles(), timeout=2) == expected
finally:
release.set()
thread.join()
await state.aclose()