Add the `workspaces` plugin: Git worktrees with their own `clai2 --web`
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 withrepo,ref,root,branch_prefix,teardown(remove|keep), andmodel. A declaredSettingsMenuedits 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, plustokenandsession.logwhile its CLAI runs), the same state directoryaoealready uses (nowconfig.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. Backendprotocol (create,start,alive,stop,destroy) andLocalWorktrees. A URL is cloned once and fetched afterwards. Git work goes through newruntime.worktreeshelpers (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; noPluginHostchange is needed, because the registry is on disk and the kinds come fromhost.plugins.
- Settings
clai2 --web --port PORT --token-file PATH:--port 0picks 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.portandWebPlugin.listenhold 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
/workspacesoutput. start,stop, anddestroyhold a per-workspaceflock, so CLAIs in several processes take turns.stopsignals a pid only after that process answered with its token.destroyremoves onlyROOT/ID, throughgit worktree remove.
- Each child runs as
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.