Run each `clai2 --web` session in its own directory, and ask where a new one runs

Merged
MP
2mo ago
+802-85

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

Why. clai2 --web stayed tied to the directory it was launched from: a session saved in another repository or worktree opened read-only ("Start CLAI there to continue it"), and every new session ran in the launch directory. With part 35's sidebar listing every project and worktree, the browser should be able to continue any of them, and start a new session wherever the user picks.

What changes

  • Under clai2 --web only, each session runs in its own directory. runtime/session_workspace.py's SessionWorkspace.move switches the one live session between turns, the way --worktree starts CLAI in a checkout: os.chdir (the headless web process has no terminal and holds one live session at a time, and CLAI's plugin contract is that the working directory is the session's), that directory's .clai/settings.json resolved again with saved settings and launch flags (CommandContext.use_project), and every plugin but web loaded again (PluginLoader.restart). So the tools, repo_context/AGENTS.md, skills, MCP project files, and the system prompt are built for that directory.
  • run_web attaches it: /resume ID first moves to the session's saved directory (Sessions.relocate), clai2 --web --resume ID starts there, and a web-only /workspace DIRECTORY starts a new session in an existing absolute directory. Nothing moves while a turn, a background fork, or a delegated task runs. A directory that no longer exists is refused with a clear error, so its session stays read-only (and --web --resume of it fails at startup naming the session).
  • host.sessions.follows_workspace (SessionCatalog), Snapshot.follows_workspace, Transcript.missing; POST /api/sessions/new takes an optional workspace, validated as an existing absolute directory, and only the token-guarded UI supplies it.
  • Web UI: New session (and Cmd/Ctrl+Shift+O) opens a picker with the current directory first, then the projects and worktrees part 35 lists (removed ones left out), or a typed absolute path with inline errors. A session from another existing directory opens directly with no read-only banner; the header shows its directory; one whose directory is gone shows "Saved in X, which no longer exists. You can read it here."
  • Docs: README web UI, PLUGINS.md (web, host.sessions, worktree startup), AGENTS.md file map.

Verification.

  • New tests: test_sessions_continue_in_their_own_directories, test_a_session_moves_only_between_turns, test_a_terminal_session_stays_where_it_started (tests/clai2/test_web.py), test_run_web_works_in_each_sessions_directory, test_run_web_refuses_a_session_whose_directory_is_gone, test_run_web_reports_an_unknown_session, and test_web_flag_runs_the_web_ui now checks settings_for (tests/clai2/test_web_parts.py). tests/clai2: 4181 passed. 100% branch coverage of every added or changed line (session_workspace.py, cli/web.py, remote.py, app.py, protocol.py at 100%; the new lines in _cli.py, command_context.py, loader.py, sessions.py covered). Pyright, ruff, tsc --noEmit clean.
  • Headed Chromium against an isolated clai2 --web -m test launched in repo alpha, with sessions in alpha, two linked worktrees, a removed worktree, a second repo beta, and a plain folder: opening each moved the process's working directory there (checked with lsof), a prompt run in beta sent no alpha AGENTS.md or project instructions, the removed worktree opened read-only, the picker listed beta · main, plain, alpha · feature-two, alpha · feature-one, alpha · main, a relative path was refused inline, and new sessions started in plain and alpha-feature-two. Dark and light screenshots.
  • Interactive clai2 in tmux: /workspace is an unknown command and /resume ID of another directory still refuses, as before.

What changes for existing users. Only clai2 --web changes: sessions from other directories can now be continued there, and a new session asks where it runs. clai2, clai2 -p, and /web started from a terminal keep working in the directory they started in, with other directories' sessions read-only as before.

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