Keep the last five sessions shown in the `clai2` web UI watched and mounted, so switching back to one is instant
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, andopenruns in the background so the session still becomes the default. - Each kept transcript stays mounted under React's
<Activity mode="hidden">. - A
snapshotnow carriesreplaces, the conversation the session held before a/newor/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 newsnapshot, and doesn't wait on a heldPOST /open. It fails without the fix.test_a_watched_session_switching_conversation_in_the_background_stays_hidden:/newin a kept background session leaves the shown session in the main pane. It fails without thereplaceshandling.test_a_session_that_switches_conversation_takes_its_watchersnow assertsreplaces.- Run locally: web-ui
tscandnpm run build(Node 24.22.1); 109tests/clai2web 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_linksand 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 -
- Decision: remediated: show shortcut requires live, dedupe watched on replaces, guard background open error, scroll sync on reveal, docs for replaces/watched/idle closing, background /new browser test
- Why: tree accfd6271ee6c334cb487014fc1a11823f195eb7; scope 0852ea766..baa8ff5d7 (full branch)
- Source: https://github.com/pydantic/pydantic-ai/tree/clai2-fix/issue-11303
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 -
- Decision: Kept: the last five sessions a page showed count as watched and are not idle-closed; documented in clai2_web_README, PLUGINS.md, docs/reference.md
- Why: Watching is what keeps kept chats current, the design the issue suggests
- Source: https://github.com/pydantic/pydantic-ai/tree/clai2-fix/issue-11303
2026-10-26 · pre-push review 2 of 3 at 11e9a13c6 · iter -
- Decision: remediated: rewrite watched on replaces only when the replaced id is kept (d772265fa)
- Why: tree 0835baf63f7a5b02a5084e8532905965003ae513; scope baa8ff5d7..11e9a13c6 plus prior findings
- Source: https://github.com/pydantic/pydantic-ai/tree/clai2-fix/issue-11303
2026-10-26 · pre-push review 3 of 3 at d772265fa · iter -
- Decision: no findings
- Why: tree 44ea1db2e4c7aa1c831671a74c83f6fb5f80b37e; scope 11e9a13c6..d772265fa plus prior finding
- Source: https://github.com/pydantic/pydantic-ai/tree/clai2-fix/issue-11303
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.