Drive every live `clai2` session from the web UI with per-session routes, `watch`, and `live`

Merged
MP
2mo ago
+2027-669

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 in web-ui/src/protocol.ts:
    • Per-session messages carry "session": "<conversation id>". A snapshot names its session in session.id.
    • New global live message: {"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 a snapshot. 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}/open and POST /api/sessions/new answer {"id": ...}.
    • The unprefixed routes and a /ws without watch use the default session: the one the page opened last while live, else the newest.
  • Backend (internal): remote.py is split into WebService (process-wide, service.py), SessionChannel per live session (channel.py), HostSessions for a host without host.live (host_session.py), sidebar.py, and menu_forms.py. Hub tracks what each browser watches.
  • The sidebar shows running for any live session's turn here, and busy only 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's LiveRuntime), PluginHost(default_model=...), and test_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: two FunctionModel turns blocked on anyio.Events stream at once; each watcher gets only its session's messages, all tagged; live shows 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 and asking, 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 --web in tmux with two scratch repos: curl on the new and legacy routes; a websockets client per session saw only its own deltas while both ran; lsof -p showed 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.