Group `clai2` sessions by project and worktree, keeping removed worktrees under their repository

Merged
MP
2mo ago
+650-152

Part 35 of the clai2 web UI stack (… → #10334 → this PR). Based on #10334. No linked issue: requested by the CLAI maintainer.

Why. The web sidebar grouped sessions by project_identity(workspace), keyed by the Git common dir. That only works while the folder exists: a session from a removed worktree (or from <checkout>/src/pydantic_clai2 inside one) fell back to key=workspace, name=<folder>, so every such session became its own group, e.g. eight separate pydantic_clai2 groups. Sessions should be grouped by project (the Git repository), with each project's worktrees inside it.

What changes

  • ConversationSummary gains an optional checkout: GitCheckout | None (repository common dir, name, checkout root, branch). CLAI's Session records it when it saves a session, reading Git once per session off the event loop; a fork keeps its source's, a session resumed from another directory keeps what it saved. Old rows read as None; older CLAIs ignore the field.
  • runtime/project_identity.placements decides where each workspace belongs: an existing folder keeps its live identity (as before); a removed one takes the checkout saved with its newest session, else the innermost known checkout that held it, else a worktree layout (ROOT/.worktrees/NAME from --worktree, .../worktrees/REPO/NAME as herdr does, or a folder named after the repository beside its other worktrees); the rest go to one "Removed folders" group, never one group per session.
  • Project name stays the main checkout's folder (the common dir's parent): stable across worktrees, no extra Git call, and independent of the remote's name (origin is not always the upstream).
  • /api/sessions returns Project.worktrees[] (root, label = branch or folder, current, missing, sessions). The sidebar shows project, then worktree, then sessions: both levels collapse, a lone checkout gets no heading unless it is removed, the current project and worktree come first, search shows every match whatever is collapsed, and a collapsed group still shows a running dot. Rebuilt bundle included.
  • The terminal /resume browser shares placements: a removed worktree now sits under its repository as BRANCH (removed) instead of in "missing folders".
  • Docs: README web UI section, PLUGINS.md web section, AGENTS.md file map, and the harness conversation-store docs.

Verification.

  • New tests: test_placements_of_removed_folders, test_removed_worktree_stays_in_its_repository (tests/clai2/test_session_browser.py), test_a_session_saved_in_a_git_checkout_records_it (tests/clai2/test_sessions.py), test_sidebar_groups_sessions_by_project_then_worktree (tests/clai2/test_web.py, real repo, linked and removed worktrees). 155 targeted tests pass, plus 1284 other session-related tests/clai2 tests; branch coverage 100% for project_identity.py, remote.py, protocol.py, conversations.py and every changed line of _session.py and session_browser.py. Pyright and ruff clean, tsc --noEmit clean.
  • Headed Chromium against an isolated clai2 --web -m test with a scratch repo, two herdr-style worktrees, one removed worktree with saved checkouts and one removed before this change, a second repo, a plain folder, and a removed non-Git folder. Before: 5 groups, the removed worktree's root and its src/pkg as separate groups. After: alpha (feat-a current, feat-b, doomed-two removed, doomed removed, main), beta, plain, Removed folders; 13 sessions; collapse and search checked, dark and light.

What changes for existing users. The web sidebar and /resume group a removed worktree's sessions under its repository; new saves record the checkout.

  • 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.