Split `PluginLoader` into `ProcessPlugins` and `SessionPlugins`, with process-scoped plugins
Merged
MP
2mo ago+1436+1436+0123456789012345678901234567890123456789-320-320-012345678901234567890123456789
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:PluginLoaderis split. The per-session part is nowSessionPlugins(PluginLoaderstays as an alias): loaded instances, guards,turn(),fire(),run_capabilities(), renderers.- New
plugins/process_plugins.py:ProcessPlugins[DepsT](*, store, builtin=()), held byClaiProcess.plugins. It owns declarations, staged and editing state, the change lock, imports, and process-scoped plugins.enable,disable,remove, andreloadrun in the origin session under the lock (change(name, apply, *, origin, again)). Every other live session then catches up withfollow(name, *, origin, again), applying at its next turn under the existingturn()rule. - The existing
PluginCatalogprotocol behindhost.pluginsstays per session, because loaded, included, and failed state differs between sessions. - New plugin API in
pydantic_clai2.plugins:PluginScope = Literal['session', 'process']andPlugin.scope: ClassVar[PluginScope] = 'session'.PluginHost(..., live: LiveSessions | None = None), read ashost.live.LiveSessionsprotocol:listing(),get(live_id, /),current().LiveSessionprotocol: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_startfires once. It then joins every live session, and each session's moments are dispatched to it withhost.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, andinterruptfollow the session being handled.
- It is built once, and
observability,web, andworkspacesare now process-scoped.webstill keepsask_useroff in every session.herdrbuilds no reporter without a terminal, andask_usercontributes nothing without one.
Verification.
- Tests in
tests/clai2/test_plugin_loader.pyrun twoSessionPluginsover oneProcessPlugins. 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.pyandtest_ask_user_menu.py(no terminal),test_web.py. - Full
tests/clai2passes, pyright is clean, and changed lines have 100% branch coverage. - Checked by hand in tmux:
- In the terminal, the
/pluginspanel turns plugins on, off, reload, and configure. - In
clai2 --web,/api/pluginsenable, disable, and reload work, and/workspacekeepsweb,observability, andworkspacesloaded.
- In the terminal, the
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.