Speed up the `clai2` web UI's cold session switch and reload-to-visible

Merged
MP
1mo ago
+291-60

Fixes #11597

Why we make these changes

Opening a saved session that wasn't live ran three steps one after another: a full GET /api/sessions/{id} just to read openable, then POST /open, then waiting for the snapshot. The header said "New session" the whole time. On reload, the WebSocket only opened after React had mounted, and every Markdown message went through DOMPurify, which parsed its config again on each call. This PR keeps to the data, fetch, and render path from #11597 and leaves the sidebar markup to #11588:

  • SessionEntry.openable: each sidebar row says whether the session opens live, so a click goes straight to POST /open. A row that has gone stale (its folder was removed after the list loaded) falls back to the read-only view when the open fails. The server computes the value once per folder, and the stat runs off the event loop.
  • While a session opens, the header and tab title show the name and folder from its row, never "New session".
  • preconnect() in main.tsx opens the first WebSocket as the bundle loads. connect takes it over and replays what it buffered.
  • DOMPurify's config is set once. Finished Markdown is kept rendered for the page's lifetime, keyed by text and capped by size. Streaming text is not cached.

Left out on purpose: hover prefetch (it needs sidebar row handlers, which #11588 owns) and the IndexedDB snapshot and HTML caches (they would keep transcripts in browser storage, which needs a maintainer decision).

New public surface

None. openable is a new field in the web UI's internal REST response.

User-visible behavior

Measured A/B with the issue's in-page rAF probe on a 306 kB session, against main's bundle and this one (headless Chromium, shared box at load ~45, three paired runs):

beforeafter
cold switch, median276 / 278 / 568 ms118 / 174 / 211 ms
"New session" header seen during cold switch10/100/10
WebSocket created after DOMContentLoaded+17–20 ms+0 ms
reload → long transcript visible, median1062 / 1282 / 1853 ms958 / 1216 / 1221 ms

On this box, reload time is mostly layout of the whole transcript and highlighted Markdown, so the issue's ≤ 60 ms reload target is not shown here. The IndexedDB items above are what remains for that.

Verification

  • New browser tests in tests/clai2/browser/test_web_browser.py: test_a_saved_session_opens_from_its_row_named_at_once and test_the_websocket_opens_before_the_page_renders fail on main's bundle and pass here. test_a_row_that_says_it_opens_shows_read_only_when_it_no_longer_can covers the fallback for a stale row.
  • tests/clai2/test_web.py checks openable on sidebar rows against the transcript's value.
  • Full browser suite (18), tests/clai2/test_web*.py, ruff, pyright, and npm run build (tsc) pass locally. app.js is not committed, per #11563.

What changes for existing users

Nothing breaks. Switching to a saved session and reloading the web UI are faster.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.