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
@@ -71,7 +71,8 @@ async def _execute(tool, ctx: RequestContext, *, allowed: bool = True, **kwargs)
|
||||
async def test_create_goal_records_goal_metadata(tmp_path):
|
||||
sm = SessionManager(tmp_path)
|
||||
create, _update, ctx = _tools(sm)
|
||||
sm.get_or_create("websocket:c1").metadata["_sustained_goal_continuation_rounds"] = 12
|
||||
await sm.state.get("websocket:c1")
|
||||
await sm.state.update_metadata("websocket:c1", {"_sustained_goal_continuation_rounds": 12})
|
||||
|
||||
out = await _execute(
|
||||
create,
|
||||
@@ -87,6 +88,7 @@ async def test_create_goal_records_goal_metadata(tmp_path):
|
||||
assert blob["status"] == "active"
|
||||
assert blob["objective"] == "Do the thing"
|
||||
assert blob["ui_summary"] == "thing"
|
||||
sess = sm.get_or_create("websocket:c1")
|
||||
assert "_sustained_goal_continuation_rounds" not in sess.metadata
|
||||
assert "_sustained_goal_continuation_rounds" not in (
|
||||
SessionManager(tmp_path).get_or_create("websocket:c1").metadata
|
||||
@@ -97,6 +99,36 @@ async def test_create_goal_records_goal_metadata(tmp_path):
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_goal_context_and_mutations_only_read_session_metadata(tmp_path, monkeypatch):
|
||||
sm = SessionManager(tmp_path)
|
||||
stored = sm.get_or_create("websocket:c1")
|
||||
stored.add_message("user", "Keep the previous conversation.")
|
||||
stored.add_message("assistant", "Previous answer.")
|
||||
sm.save(stored)
|
||||
create, update, ctx = _tools(sm)
|
||||
|
||||
def history_unavailable(*_args, **_kwargs):
|
||||
pytest.fail("goal metadata operations must not request conversation history")
|
||||
|
||||
monkeypatch.setattr(sm.state, "get", history_unavailable)
|
||||
monkeypatch.setattr(sm.state, "read", history_unavailable)
|
||||
ordinary = _request_context(metadata={}, original_user_text="Continue.")
|
||||
try:
|
||||
assert await create.runtime_context_provider()(ordinary) is None
|
||||
assert "Goal recorded" in await _execute(create, ctx, objective="Preserve history")
|
||||
context = await create.runtime_context_provider()(ordinary)
|
||||
assert context is not None and "Preserve history" in context.content
|
||||
assert "marked complete" in await _execute(update, ordinary, action="complete")
|
||||
assert await create.runtime_context_provider()(ordinary) is None
|
||||
|
||||
reopened = SessionManager(tmp_path).get_or_create(stored.key)
|
||||
assert reopened.messages == stored.messages
|
||||
assert reopened.metadata[GOAL_STATE_KEY]["status"] == "completed"
|
||||
finally:
|
||||
await sm.state.aclose()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_goal_rejects_without_explicit_goal_permission(tmp_path):
|
||||
sm = SessionManager(tmp_path)
|
||||
@@ -144,9 +176,10 @@ async def test_update_goal_replace_keeps_goal_active_with_new_objective(tmp_path
|
||||
create, update, ctx = _tools(sm)
|
||||
|
||||
await _execute(create, ctx, objective="Old")
|
||||
sess = sm.get_or_create("websocket:c1")
|
||||
sess.metadata["_sustained_goal_continuation_rounds"] = 12
|
||||
sm.save(sess)
|
||||
await sm.state.update_metadata(
|
||||
"websocket:c1",
|
||||
{"_sustained_goal_continuation_rounds": 12},
|
||||
)
|
||||
out = await _execute(
|
||||
update,
|
||||
_request_context(),
|
||||
@@ -161,6 +194,7 @@ async def test_update_goal_replace_keeps_goal_active_with_new_objective(tmp_path
|
||||
assert blob["objective"] == "New"
|
||||
assert blob["previous_objective"] == "Old"
|
||||
assert blob["ui_summary"] == "new"
|
||||
sess = sm.get_or_create("websocket:c1")
|
||||
assert "_sustained_goal_continuation_rounds" not in sess.metadata
|
||||
assert "_sustained_goal_continuation_rounds" not in (
|
||||
SessionManager(tmp_path).get_or_create("websocket:c1").metadata
|
||||
@@ -178,13 +212,14 @@ async def test_goal_state_mutations_roll_back_on_save_failure(tmp_path, monkeypa
|
||||
sess = sm.get_or_create("websocket:c1")
|
||||
sess.metadata["marker"] = {"keep": True}
|
||||
sess.metadata["_sustained_goal_continuation_rounds"] = 12
|
||||
original_save = sm.save
|
||||
sm.save(sess)
|
||||
original_save = sm._store.replace_metadata
|
||||
create_context = _request_context()
|
||||
|
||||
def fail_save(_session, **_kwargs):
|
||||
def fail_save(*_args, **_kwargs):
|
||||
raise OSError("disk unavailable")
|
||||
|
||||
monkeypatch.setattr(sm, "save", fail_save)
|
||||
monkeypatch.setattr(sm._store, "replace_metadata", fail_save)
|
||||
with pytest.raises(OSError, match="disk unavailable"):
|
||||
await _execute(create, create_context, objective="Old")
|
||||
|
||||
@@ -194,13 +229,16 @@ async def test_goal_state_mutations_roll_back_on_save_failure(tmp_path, monkeypa
|
||||
}
|
||||
assert GOAL_STATE_KEY not in SessionManager(tmp_path).get_or_create("websocket:c1").metadata
|
||||
|
||||
monkeypatch.setattr(sm, "save", original_save)
|
||||
monkeypatch.setattr(sm._store, "replace_metadata", original_save)
|
||||
assert "Goal recorded" in await _execute(create, create_context, objective="Old")
|
||||
sess.metadata["_sustained_goal_continuation_rounds"] = 12
|
||||
sm.save(sess)
|
||||
await sm.state.update_metadata(
|
||||
"websocket:c1",
|
||||
{"_sustained_goal_continuation_rounds": 12},
|
||||
)
|
||||
sess = sm.get_or_create("websocket:c1")
|
||||
replace_context = _request_context()
|
||||
|
||||
monkeypatch.setattr(sm, "save", fail_save)
|
||||
monkeypatch.setattr(sm._store, "replace_metadata", fail_save)
|
||||
with pytest.raises(OSError, match="disk unavailable"):
|
||||
await _execute(update, replace_context, action="replace", objective="New")
|
||||
|
||||
@@ -210,13 +248,14 @@ async def test_goal_state_mutations_roll_back_on_save_failure(tmp_path, monkeypa
|
||||
assert persisted[GOAL_STATE_KEY]["objective"] == "Old"
|
||||
assert persisted["_sustained_goal_continuation_rounds"] == 12
|
||||
|
||||
monkeypatch.setattr(sm, "save", original_save)
|
||||
monkeypatch.setattr(sm._store, "replace_metadata", original_save)
|
||||
assert "Goal replaced" in await _execute(
|
||||
update,
|
||||
replace_context,
|
||||
action="replace",
|
||||
objective="New",
|
||||
)
|
||||
sess = sm.get_or_create("websocket:c1")
|
||||
assert "_sustained_goal_continuation_rounds" not in sess.metadata
|
||||
assert (
|
||||
SessionManager(tmp_path).get_or_create("websocket:c1").metadata[GOAL_STATE_KEY]["objective"]
|
||||
@@ -372,7 +411,7 @@ async def test_registry_does_not_reuse_goal_context_after_request_scope(tmp_path
|
||||
leaked_out = await registry.execute("create_goal", {"objective": "Leaked"})
|
||||
|
||||
assert "missing routing context" in str(leaked_out)
|
||||
assert sess.metadata[GOAL_STATE_KEY]["status"] == "completed"
|
||||
assert sm.get_or_create(sess.key).metadata[GOAL_STATE_KEY]["status"] == "completed"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
Reference in new issue
Block a user