Add the `workspaces` plugin: Git worktrees with their own `clai2 --web`

Merged
MP
2mo ago
+1932-16

Part 1 of the clai2 orchestrator stack (#10352 → this PR). Based on #10352. No linked issue: requested by the CLAI maintainer.

Why. The orchestrator ("clone pydantic-ai off main and open a PR to fix issue #xyz") needs somewhere to run each job: a workspace with its own full CLAI session that another CLAI can start, watch, and tear down. This PR is Part B1 of the workers-and-orchestrator plan: the workspaces plugin, which configures which classes of workspace exist and which workspaces are live. The first backend is a local Git worktree; Docker, SSH with bubblewrap, and Modal come later behind the same protocol.

What changes

  • New opt-in built-in plugin workspaces (pydantic_clai2.builtin_plugins.workspaces), off by default and declarative.
    • Settings WorkspacesSettings(kinds: dict[str, WorktreeKind]): named kinds, each with repo, ref, root, branch_prefix, teardown (remove | keep), and model. A declared SettingsMenu edits them, adds kinds, and removes them, in the terminal and in the web plugins panel.
    • /workspaces [list | create KIND [REPO [REF]] | open ID | stop ID | destroy ID].
    • Registry: one 0600 folder per workspace under $XDG_STATE_HOME/pydantic-clai2/workspaces/registry/ (workspace.json, plus token and session.log while its CLAI runs), the same state directory aoe already uses (now config.settings_store.state_dir()). It holds the id, kind, repo, ref, branch, path, status, creation time, teardown, and session (pid, port, token file). Every CLAI shares it.
    • Backend protocol (create, start, alive, stop, destroy) and LocalWorktrees. A URL is cloned once and fetched afterwards. Git work goes through new runtime.worktrees helpers (add_worktree, remove_worktree, clone_or_fetch, commit_of, repository_root).
    • Workspaces(kinds=..., directory=None, launcher=None): listing(), get(id), create(kind=, repo=, ref=), start(id), stop(id), destroy(id), token(id). Other plugins import it; no PluginHost change is needed, because the registry is on disk and the kinds come from host.plugins.
  • clai2 --web --port PORT --token-file PATH: --port 0 picks a free port. With --token-file, the token comes from a 0600 file, CLAI listens on 127.0.0.1 whatever host is saved, the printed address leaves the token out, and no browser opens. WebPlugin.port and WebPlugin.listen hold the launch overrides.
  • Security:
    • Each child runs as python -P -m pydantic_clai2, so the checkout cannot shadow CLAI.
    • It binds a free 127.0.0.1 port itself (--port 0, read back from its 0600 log).
    • It has its own random token, which stays off command lines, logs, and /workspaces output.
    • start, stop, and destroy hold a per-workspace flock, so CLAIs in several processes take turns.
    • stop signals a pid only after that process answered with its token.
    • destroy removes only ROOT/ID, through git worktree remove.

Verification. tests/clai2/test_workspaces.py uses real Git on temp repos (local and file:// clones), a faked process spawner and probe, a real sleep child for LocalProcesses, and the real web server for probe. tests/clai2/test_web_parts.py covers the new flags and run_web on a launcher's port and token. Branch coverage is 100% on the new files and changed lines; pyright and ruff are clean. A real run with child processes is in part 3.

What changes for existing users. Nothing until they enable workspaces. clai2 --web gains two optional flags.

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