* 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
161 lines
5.3 KiB
Python
161 lines
5.3 KiB
Python
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import pytest
|
|
|
|
from nanobot.providers.base import LLMUsage
|
|
from nanobot.session.model_selection import SESSION_MODEL_PRESET_METADATA_KEY
|
|
from nanobot.session.recovery import RECOVERY_METADATA_KEY
|
|
from nanobot.webui.session_projection import WebUISessionProjection
|
|
|
|
|
|
async def test_attach_fields_restore_session_runtime_metadata() -> None:
|
|
usage = LLMUsage.reported(input_tokens=120, output_tokens=8, total_tokens=175)
|
|
sessions = MagicMock()
|
|
sessions.state.read_metadata = AsyncMock(side_effect=lambda key: sessions.read_session_metadata(key))
|
|
sessions.read_session_metadata.return_value = {
|
|
"metadata": {
|
|
SESSION_MODEL_PRESET_METADATA_KEY: "Deep Research",
|
|
RECOVERY_METADATA_KEY: {
|
|
"status": "recovered",
|
|
"recovery_id": "recovery-1",
|
|
"reason": "answer_restored",
|
|
},
|
|
"_last_usage": usage.to_dict(),
|
|
}
|
|
}
|
|
|
|
projection = WebUISessionProjection(sessions)
|
|
|
|
assert await projection.attach_fields("websocket:chat-1") == {
|
|
"model_preset": "Deep Research",
|
|
"recovery_state": {
|
|
"status": "recovered",
|
|
"recovery_id": "recovery-1",
|
|
"reason": "answer_restored",
|
|
},
|
|
"usage": usage.to_turn_dict(),
|
|
}
|
|
sessions.read_session_metadata.assert_called_once_with("websocket:chat-1")
|
|
|
|
|
|
async def test_attach_fields_tolerate_missing_or_invalid_session_metadata() -> None:
|
|
sessions = MagicMock()
|
|
sessions.state.read_metadata = AsyncMock(side_effect=lambda key: sessions.read_session_metadata(key))
|
|
sessions.read_session_metadata.return_value = {
|
|
"metadata": {SESSION_MODEL_PRESET_METADATA_KEY: 42}
|
|
}
|
|
log = MagicMock()
|
|
projection = WebUISessionProjection(sessions, log=log)
|
|
|
|
assert await projection.attach_fields("websocket:invalid") == {"model_preset": None}
|
|
log.warning.assert_called_once()
|
|
assert await WebUISessionProjection(None).attach_fields("websocket:missing") == {}
|
|
|
|
|
|
async def test_hydration_events_restore_goal_and_running_turn(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
sessions = MagicMock()
|
|
sessions.state.read_metadata = AsyncMock(side_effect=lambda key: sessions.read_session_metadata(key))
|
|
sessions.read_session_metadata.return_value = {
|
|
"metadata": {
|
|
"goal_state": {
|
|
"status": "active",
|
|
"objective": "finish boundary split",
|
|
"ui_summary": "Refactoring",
|
|
}
|
|
}
|
|
}
|
|
monkeypatch.setattr(
|
|
"nanobot.webui.session_projection.websocket_turn_wall_started_at",
|
|
lambda _chat_id: 42.5,
|
|
)
|
|
monkeypatch.setattr(
|
|
"nanobot.webui.session_projection.websocket_turn_id",
|
|
lambda _chat_id: "turn-1",
|
|
)
|
|
|
|
events = await WebUISessionProjection(sessions).hydration_events(
|
|
"websocket:chat-1",
|
|
"chat-1",
|
|
)
|
|
|
|
assert events == (
|
|
{
|
|
"event": "goal_state",
|
|
"chat_id": "chat-1",
|
|
"goal_state": {
|
|
"active": True,
|
|
"status": "active",
|
|
"ui_summary": "Refactoring",
|
|
"objective": "finish boundary split",
|
|
},
|
|
},
|
|
{
|
|
"event": "goal_status",
|
|
"chat_id": "chat-1",
|
|
"status": "running",
|
|
"started_at": 42.5,
|
|
"turn_id": "turn-1",
|
|
},
|
|
)
|
|
|
|
|
|
async def test_hydration_events_are_quiet_without_actionable_state(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
sessions = MagicMock()
|
|
sessions.state.read_metadata = AsyncMock(side_effect=lambda key: sessions.read_session_metadata(key))
|
|
sessions.read_session_metadata.return_value = {"metadata": {}}
|
|
monkeypatch.setattr(
|
|
"nanobot.webui.session_projection.websocket_turn_wall_started_at",
|
|
lambda _chat_id: None,
|
|
)
|
|
|
|
assert await WebUISessionProjection(sessions).hydration_events(
|
|
"websocket:chat-1",
|
|
"chat-1",
|
|
) == ()
|
|
|
|
|
|
async def test_hydration_yields_during_metadata_read_and_reads_live_turn_on_loop(
|
|
tmp_path, monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
import asyncio
|
|
import threading
|
|
|
|
loop_thread = threading.get_ident()
|
|
entered = threading.Event()
|
|
release = threading.Event()
|
|
from nanobot.session.manager import SessionManager
|
|
sessions = SessionManager(tmp_path)
|
|
|
|
def read_metadata(_key):
|
|
assert threading.get_ident() != loop_thread
|
|
entered.set()
|
|
assert release.wait(timeout=3)
|
|
return {"metadata": {}}
|
|
|
|
def active_turn(_chat_id):
|
|
assert threading.get_ident() == loop_thread
|
|
return 42.5
|
|
|
|
monkeypatch.setattr(sessions._store, "read_metadata", read_metadata)
|
|
monkeypatch.setattr(
|
|
"nanobot.webui.session_projection.websocket_turn_wall_started_at", active_turn,
|
|
)
|
|
projection = WebUISessionProjection(sessions)
|
|
task = asyncio.create_task(projection.hydration_events("websocket:chat-io", "chat-io"))
|
|
try:
|
|
async with asyncio.timeout(2):
|
|
while not entered.is_set() and not task.done():
|
|
await asyncio.sleep(0)
|
|
assert entered.is_set()
|
|
for _ in range(5):
|
|
await asyncio.sleep(0)
|
|
assert not task.done()
|
|
finally:
|
|
release.set()
|
|
events = await task
|
|
assert events[0]["status"] == "running"
|