refactor(sessions): persist archive state in SQLite (#6536)

* feat(webui): add archived chat management

* fix(webui): keep archived chats in the sidebar

* feat(webui): scope archived topics to sidebar groups

* fix(webui): reuse group dissolve label

* fix(webui): fail fast when adding panes

* refactor(sessions): persist archive state in SQLite

* refactor: limit archive changes to SQLite storage

* perf(sessions): avoid redundant archive I/O and read barriers

* fix(sessions): archive only persisted conversations
This commit is contained in:
chengyongru authored and GitHub committed 2026-11-03 15:58:41 +08:00
1 parent 58a05ea3e0
commit 80ac41b1f4
16 files changed
+751 -107

No files matched your search

@@ -1579,66 +1579,63 @@ async def test_webui_persists_sidebar_state_larger_than_http_request_line(
"event": "webui_response",
"request_id": request_id,
"ok": True,
"result": saved,
"result": {**saved, "archived_keys": []},
}
@pytest.mark.asyncio
@pytest.mark.parametrize("command_type", ["webui_request", "set_sidebar_state"])
async def test_webui_sidebar_state_update_broadcasts_workbench_to_other_devices(
bus: MagicMock,
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
command_type: str,
) -> None:
monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)
channel = _ch(bus)
sessions = SessionManager(tmp_path)
await sessions.state.import_messages("websocket:a", [{"role": "user", "content": "saved"}])
await sessions.state.fork("websocket:a", "websocket:b", 0)
sessions.get_or_create("websocket:draft")
channel = _ch(bus, session_manager=sessions)
source = AsyncMock()
source.request = SimpleNamespace(headers=Headers())
other_device = AsyncMock()
channel._webui_connections.update({source, other_device})
channel._register_connection_outbound(other_device)
request_id = "sidebar-workbench-state"
await channel._dispatch_envelope(
source,
"webui-client",
{
"type": "webui_request",
"request_id": request_id,
"action": "sidebar.update",
"payload": {
"state": {
"workbench": {
"version": 1,
"tabs": {
"tab:websocket:a": {
"explicit": True,
"title": "Research",
"paneKeys": ["websocket:a", "websocket:b"],
"layoutPaneKeys": ["websocket:b", "websocket:a"],
"layout": "columns",
"splitRatios": [0.35],
}
},
}
state = {
"archived_keys": ["websocket:b", "websocket:draft"],
"workbench": {
"version": 1,
"tabs": {
"tab:websocket:a": {
"explicit": True,
"title": "Research",
"paneKeys": ["websocket:a", "websocket:b"],
"layoutPaneKeys": ["websocket:b", "websocket:a"],
"layout": "columns",
"splitRatios": [0.35],
}
},
},
)
await asyncio.gather(*tuple(channel._webui_request_tasks.values()))
event = json.loads(other_device.send.await_args.args[0])
assert event["event"] == "sidebar_state_updated"
assert event["state"]["workbench"]["tabs"]["tab:websocket:a"]["paneKeys"] == [
"websocket:a",
"websocket:b",
]
assert event["state"]["workbench"]["tabs"]["tab:websocket:a"]["layoutPaneKeys"] == [
"websocket:b",
"websocket:a",
]
assert event["state"]["workbench"]["tabs"]["tab:websocket:a"]["splitRatios"] == [
0.35
]
}
envelope = {"type": command_type, "state": state} if command_type == "set_sidebar_state" else {
"type": command_type, "request_id": "sidebar-workbench-state",
"action": "sidebar.update", "payload": {"state": state},
}
try:
await channel._dispatch_envelope(source, "webui-client", envelope)
await asyncio.gather(*tuple(channel._webui_request_tasks.values()))
event = json.loads(other_device.send.await_args.args[0])
assert event["event"] == "sidebar_state_updated"
assert event["state"]["archived_keys"] == ["websocket:b"]
assert await sessions.state.archived_keys() == ["websocket:b"]
assert event["state"]["workbench"] == state["workbench"]
persisted = json.loads((tmp_path / "webui" / "sidebar-state.json").read_text(encoding="utf-8"))
assert "archived_keys" not in persisted
assert persisted["workbench"] == state["workbench"]
finally:
await channel.stop()
await sessions.state.aclose()
@pytest.mark.asyncio
@@ -2442,6 +2442,8 @@ async def test_webui_sidebar_state_routes_are_config_dir_scoped(
) -> None:
monkeypatch.setattr("nanobot.config.paths.get_data_dir", lambda: tmp_path)
sm = _seed_session(tmp_path, key="websocket:sidebar")
await sm.state.fork("websocket:sidebar", "websocket:old", 0)
sm.get_or_create("websocket:draft")
channel = _ch(bus, session_manager=sm, port=29911)
server_task = asyncio.create_task(channel.start())
try:
@@ -2458,7 +2460,7 @@ async def test_webui_sidebar_state_routes_are_config_dir_scoped(
payload = {
"pinned_keys": ["websocket:sidebar"],
"archived_keys": ["websocket:old"],
"archived_keys": ["websocket:old", "websocket:draft"],
"session_order": ["websocket:old", "websocket:sidebar"],
"title_overrides": {"websocket:sidebar": "Pinned work"},
"view": {"density": "compact", "show_archived": True},
@@ -2471,15 +2473,19 @@ async def test_webui_sidebar_state_routes_are_config_dir_scoped(
assert updated.status_code == 200
body = updated.json()
assert body["pinned_keys"] == ["websocket:sidebar"]
assert body["archived_keys"] == ["websocket:old"]
assert await sm.state.archived_keys() == ["websocket:old"]
assert body["session_order"] == ["websocket:old", "websocket:sidebar"]
assert body["title_overrides"] == {"websocket:sidebar": "Pinned work"}
assert body["view"]["density"] == "compact"
state_path = tmp_path / "webui" / "sidebar-state.json"
assert state_path.is_file()
assert json.loads(state_path.read_text(encoding="utf-8"))["pinned_keys"] == [
"websocket:sidebar"
]
persisted = json.loads(state_path.read_text(encoding="utf-8"))
assert persisted["pinned_keys"] == ["websocket:sidebar"]
assert "archived_keys" not in persisted
read_back = await _http_get("http://127.0.0.1:29911/api/webui/sidebar-state", headers=auth)
assert read_back.json()["archived_keys"] == ["websocket:old"]
finally:
await channel.stop()
await server_task