Keep the last five sessions shown in the `clai2` web UI watched and mounted, so switching back to one is instant

Merged
MP
1mo ago
+330-127

Closes #11303

Why we make these changes

Switching sessions in the clai2 web UI threw away the shown transcript and rebuilt the target from scratch. The page watched one session at a time, so going back to one meant POST /open, a WebSocket watch, a full snapshot, and a React remount. The server already supports watching several sessions and streams each one's messages to its watchers, so this PR implements the issue's main suggestion: a keep-alive cache for warm switches. The cold-path items (openable in the sidebar, hover prefetch, a markdown HTML LRU) are left for follow-ups.

  • The page watches the last five sessions it showed (state.watched) and keeps each one's chat current from the streamed messages. Showing a kept live session takes effect at once, with no new snapshot, and open runs in the background so the session still becomes the default.
  • Each kept transcript stays mounted under React's <Activity mode="hidden">.
  • A snapshot now carries replaces, the conversation the session held before a /new or /resume. A session kept in the background that switches conversation renames its kept chat instead of taking over the main pane.

New public surface

replaces: str | None = None on the web plugin's Snapshot wire message. It is null on every snapshot except a conversation switch's. The field is additive, so older pages ignore it.

User-visible behavior

Local Playwright run with a long session (67 kB snapshot) and a short one, 12 switches each way, median time from click to the target transcript on screen, on a heavily loaded machine: short to long 444 ms to 138 ms, long to short 112 ms to 44 ms. Most of what remains is Chrome laying out the revealed transcript, about 100 ms on its own on that machine.

Verification

  • test_switching_back_to_a_watched_session_shows_it_at_once: switching back shows the same transcript DOM node, receives no new snapshot, and doesn't wait on a held POST /open. It fails without the fix.
  • test_a_watched_session_switching_conversation_in_the_background_stays_hidden: /new in a kept background session leaves the shown session in the main pane. It fails without the replaces handling.
  • test_a_session_that_switches_conversation_takes_its_watchers now asserts replaces.
  • Run locally: web-ui tsc and npm run build (Node 24.22.1); 109 tests/clai2 web tests; ruff, pyright and pre-commit.
  • Browser tests locally: this machine lacks Chromium's system libraries and is heavily loaded, so I ran them with extracted libraries and 6× Playwright timeouts from a plugin that isn't committed. The new tests pass. test_hostile_markdown_and_external_links and whichever test opens the first page still time out on their first click here, on origin/main as well. CI is the authority for the browser job.

What changes for existing users

Switching back to a recently shown session is immediate. The last five sessions a page showed now count as watched, so they are not idle-closed while the page stays open; the docs now say this.

PR decisions

2026-10-26 · pre-push review 1 of 3 at baa8ff5d771abe9815062a6eee846e49a926092c · iter -

2026-10-26 · reviewer finding: restore scroll position on reveal · iter -

  • Decision: Not restoring the reader scroll position; a kept transcript follows to the bottom on reveal, as every switch did before
  • Why: Same as pre-change behaviour; restoring is new scope. The top ref now syncs on reveal so a scrollTop reset while hidden is not taken as the reader scrolling up
  • Source: https://github.com/pydantic/pydantic-ai/tree/clai2-fix/issue-11303

2026-10-26 · reviewer finding: idle closing of kept sessions · iter -

2026-10-26 · pre-push review 2 of 3 at 11e9a13c6 · iter -

2026-10-26 · pre-push review 3 of 3 at d772265fa · iter -

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.