Drive every live `clai2` session from the web UI with per-session routes, `watch`, and `live`
Merged
MP
2mo ago+2027+2027+0123456789012345678901234567890123456789-669-669-012345678901234567890123456789
Part 6 of the clai2 multi-session stack (#10468 → #10469 → #10477 → #10485 → #10494 → this PR → #10504 → #10541 → #10549). Based on #10494. No linked issue: requested by the CLAI maintainer.
Why. PR 5 keeps several live sessions in one process. The web UI still drove one: every route and WebSocket message meant "the session". This PR addresses each live session by its conversation ID, so a page can show and drive any of them while others run turns. Older pages and web/client.py keep working on a default session.
What changes
- Wire protocol, documented in
clai2_web_README.md("Several live sessions") and mirrored inweb-ui/src/protocol.ts:- Per-session messages carry
"session": "<conversation id>". Asnapshotnames its session insession.id. - New global
livemessage:{"type": "live", "sessions": [{"id", "state": "running" | "queued" | "asking" | "idle", "title", "workspace"}]}. - Client
{"type": "watch", "sessions": [...]}replaces what a WebSocket watches. Each newly followed session gets asnapshot. A watched browser keeps a session from idle closing. /api/sessions/{id}/state|input|interrupt|complete|model|switch|questions/{rid}|dialogs/{did}act on that live session (404 otherwise).POST /api/sessions/{id}/openandPOST /api/sessions/newanswer{"id": ...}.- The unprefixed routes and a
/wswithoutwatchuse the default session: the one the page opened last while live, else the newest.
- Per-session messages carry
- Backend (internal):
remote.pyis split intoWebService(process-wide,service.py),SessionChannelper live session (channel.py),HostSessionsfor a host withouthost.live(host_session.py),sidebar.py, andmenu_forms.py.Hubtracks what each browser watches. - The sidebar shows
runningfor any live session's turn here, andbusyonly for another process's. - The orchestrator runs on the saved default model, through the new
await host.default_model() -> Model | str | None. - Lower-PR changes made here:
LiveSession.setting(key, /)(#10485's protocol, #10494'sLiveRuntime),PluginHost(default_model=...), andtest_each_session_works_in_its_own_folder(#10494) now creates the sessions database first, as its sibling test does, since two first saves to a new database can fail with "database is locked".
Verification.
tests/clai2/test_web_sessions.py, in-process on one portal:test_each_browser_gets_only_the_sessions_it_watches: twoFunctionModelturns blocked onanyio.Events stream at once; each watcher gets only its session's messages, all tagged;liveshows running and queued.test_watching_switches_sessions_and_keeps_them_open,test_a_session_that_switches_conversation_takes_its_watchers,test_a_conversation_watched_before_a_session_resumes_it_gets_one_snapshot,test_closing_a_watched_session_or_the_web_ui_stops_counting_its_browsers.test_session_routes(404s, per-session questions andasking, legacy routes on the default),test_open_makes_a_saved_session_live_once,test_the_orchestrator_runs_on_the_saved_default_model, and the bare-host tests.
- The existing web tests,
test_web_client.py(legacy routes), and the orchestrator tests pass. Pyright is clean, and changed lines have 100% branch coverage. clai2 --webin tmux with two scratch repos:curlon the new and legacy routes; awebsocketsclient per session saw only its own deltas while both ran;lsof -pshowed the cwd unchanged.
What changes for existing users. Older pages and other CLAI versions keep the unprefixed routes, and POST /api/sessions/{id}/open and POST /api/sessions/new now answer 200 with {"id": ...}.
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
- PR title is fit for the release changelog.