Split `PluginLoader` into `ProcessPlugins` and `SessionPlugins`, with process-scoped plugins

Merged
MP
2mo ago
+1436-320

Part 4 of the clai2 multi-session stack (#10468 → #10469 → #10477 → this PR → #10494 → #10495 → #10504 → #10541 → #10549). Based on #10477. No linked issue: requested by the CLAI maintainer.

Why. Several live sessions in one process need to share plugin declarations and changes, and some plugins (observability, web, workspaces) must exist once per process, not once per session. Behaviour stays the same with one session, which is all production runs today.

What changes

  • plugins/loader.py: PluginLoader is split. The per-session part is now SessionPlugins (PluginLoader stays as an alias): loaded instances, guards, turn(), fire(), run_capabilities(), renderers.
  • New plugins/process_plugins.py: ProcessPlugins[DepsT](*, store, builtin=()), held by ClaiProcess.plugins. It owns declarations, staged and editing state, the change lock, imports, and process-scoped plugins. enable, disable, remove, and reload run in the origin session under the lock (change(name, apply, *, origin, again)). Every other live session then catches up with follow(name, *, origin, again), applying at its next turn under the existing turn() rule.
  • The existing PluginCatalog protocol behind host.plugins stays per session, because loaded, included, and failed state differs between sessions.
  • New plugin API in pydantic_clai2.plugins:
    • PluginScope = Literal['session', 'process'] and Plugin.scope: ClassVar[PluginScope] = 'session'.
    • PluginHost(..., live: LiveSessions | None = None), read as host.live.
    • LiveSessions protocol: listing(), get(live_id, /), current().
    • LiveSession protocol: id, session_id, workspace, conversation, status, sessions, tasks, running, send_input(), interrupt(), complete().
  • How a process-scoped plugin runs:
    • It is built once, and on_session_start fires once. It then joins every live session, and each session's moments are dispatched to it with host.live.current() naming that session.
    • It ends after the last running turn that bound it, or when the last session closes.
    • Its host's session_id, workspace, tasks, plugins, setting, send_input, submit_prompt, and interrupt follow the session being handled.
  • observability, web, and workspaces are now process-scoped. web still keeps ask_user off in every session.
  • herdr builds no reporter without a terminal, and ask_user contributes nothing without one.

Verification.

  • Tests in tests/clai2/test_plugin_loader.py run two SessionPlugins over one ProcessPlugins. They cover fan-out of each change, deferral during a turn, failures reported in each session, a process plugin built once even when sessions load it together, and its deferred end.
  • Also: test_logfire.py (one configuration, a root per session), test_herdr.py and test_ask_user_menu.py (no terminal), test_web.py.
  • Full tests/clai2 passes, pyright is clean, and changed lines have 100% branch coverage.
  • Checked by hand in tmux:
    • In the terminal, the /plugins panel turns plugins on, off, reload, and configure.
    • In clai2 --web, /api/plugins enable, disable, and reload work, and /workspace keeps web, observability, and workspaces loaded.

What changes for existing users. Nothing visible, apart from two details. With one session, process-scoped plugins end after the others on exit. Under --web, /workspace keeps observability as loaded at launch.

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