# Plugin runtime

A plugin is a manifest naming skills, commands, agents, hooks, MCP servers and provider directories. The host mounts them through one `ProgramContext`; no plugin code is imported and there is no plugin-only registry. Manifest forms, sources, trust: [Skills and plugins](https://fzkuji.github.io/OpenProgram/docs/reference/design/integrations/extensions/skills-and-plugins.html). Model: [Program Model](https://fzkuji.github.io/OpenProgram/docs/reference/design/function/program-model.html#4-plugins).

## Lifecycle

| Step | What happens |
| --- | --- |
| `rescan` | Builds `Plugin` records, with any code refusal as the error. Keeps a loaded plugin at the same root; a changed root deactivates it first. Enabled set: `~/.openprogram/plugins-enabled.json` |
| `load_plugin` check | No code, trust, compatibility, `requires` (dependencies load first) |
| Activate | `ProgramContext(name, root, options)`, owner `plugin:<name>`; manifest keys become effects. Any exception: `ctx.dispose()`, error kept on the plugin |
| After load | Emit `plugin.enable`, broadcast `plugins:changed` |
| `unload_plugin` | Emit `plugin.disable`, then `ctx.dispose()` |
| `reload_plugin` | Unload, rescan, load. Stays enabled if the new files fail |

At startup `_load_plugins` in `interfaces/server/server.py` runs after the MCP servers, hands the loop to `set_host_loop`, and calls `loader.load_enabled()` on a thread: dependency order, each failure logged without stopping the others.

| Source | Location |
| --- | --- |
| npm | `~/.openprogram/plugins/node_modules/` |
| Path | `~/.openprogram/plugins/<name>`. `install path <dir>` symlinks it: edits apply on reload, uninstall removes only the link |
| Project pins | Later sources win a name clash |

## Effects

| Manifest key | Effect | Host registry | Undo |
| --- | --- | --- | --- |
| `skills` | `ctx.skills(dir)` | `skills` plugin source | Unregister the source |
| `commands` | `ctx.command(name, prompt, description)` | `contrib["_commands"]`, read by the commands registry adapter | Cleared with the contributions |
| `hooks` | `ctx.gate`, `ctx.on` with a shell gate or notifier | `EventBus.subscribe_gate`, `subscribe` | The subscription's disposer |
| `mcpServers` | `ctx.mcp_server(name, config)` | `add_server` as `<plugin>_<name>` on the host loop; never in `mcp_servers.json` | `remove_server` |
| `providers` | `ctx.provider_root(dir)` | `models/providers/catalog/plugin_dirs.py`, behind `provider.json` lookups. Shipped directories win a clash | Remove the root |
| `agents` | `manager.create` as `<plugin>-<name>` when missing | Agent registry | None: deleting an agent deletes its sessions |

`ctx.dispose()` runs disposers in reverse, collects errors instead of stopping, and empties `ctx.contributions`, the per-kind record the Plugins page and `plugin` tool show. Defined in `programs/_host/context.py`.

## Code is refused

`extensions/plugins/manifest.py` records keys naming code in `code`: `python`, `module`, `setup`, `main`, `tools`, `client`, `web`, `sidebar`, a `module:name` `hooks` value, an `__init__.py` beside a Hermes `plugin.yaml`. `code_refusal` names them and says to add an `application.json` and run `openprogram programs install <dir>`.

| Entry | Refusal |
| --- | --- |
| `installer.install` | Refuses a `path` install, removes a git clone, uninstalls an npm package. `pip` is not a source |
| `rescan` | Lists the plugin with the refusal as its error |
| `_check`, `_activate` | Raise `PluginContainsCode` (a `PermissionError`); the toggle route answers `403`, `code: "contains_code"` |

## Hooks

| Aspect | Rule |
| --- | --- |
| Shapes (`plugins/hooks.py`) | Inline or a JSON file. OpenProgram: `{event: [{command, timeout, shell, args}]}`, as in `config.json`; `shell` and `args` are optional ([which shell runs a hook](https://fzkuji.github.io/OpenProgram/docs/reference/design/distribution/windows-command-shell.html#hook-shell)). Claude Code: `{hooks: {PreToolUse: [{matcher, hooks: [{type: "command", command}]}]}}` |
| `CLAUDE_EVENTS` | `PreToolUse` → `tool.before`, `PostToolUse` → `tool.after`, `UserPromptSubmit` → `chat.before_send`, `Stop` → `turn.stop`, `SessionStart` → `session.start` |
| Substitution | `${CLAUDE_PLUGIN_ROOT}` is the plugin directory; `matcher` is a regular expression over the event's `tool` |
| Execution (`runtime/events/shell_hooks.py`) | Event as JSON on stdin. On a gate event, exit 2 denies with stderr as the reason; any other exit allows |
| Skipped | Unknown events, non-command hook types, transform events |

| Event kind (`runtime/events/registry.py`) | Behavior |
| --- | --- |
| notify | Observers; return values ignored |
| gate | May veto (`tool.before`, `turn.stop`) |
| transform | Each rewriter gets `(event, value)` and returns the new value; `None` keeps it; one that raises is skipped |

`tool.result` is a transform: `_rewrite_tool_result` joins the text blocks and replaces the text, keeping images and audio. Applications subscribe with `ctx.transform`; plugins cannot.

## Self-description, tool, CLI

| Part | Behavior |
| --- | --- |
| `extensions/plugins/inspect.py` | `describe()` from live registries: allowed and refused keys, each event with kind and hook eligibility, the Claude Code map, exposure tiers, every entry with owner, tier and approval flag. `guide()` is the same as text. Backs `GET /api/plugins/inspect`, `openprogram plugins inspect [--json]`, the `plugin` tool |
| `plugin` tool (`programs/tools/runtime/plugin.py`) | `guide`, `inspect`, `list`, `install` (link, trust as community, enable), `enable`, `disable`, `reload`, `uninstall`. Runs in the worker (`_run_in_worker`), requires approval, unavailable on chat channels and in plan mode. Code is refused with the same message |
| CLI | `enable`, `disable`, `install`, `uninstall`, `reload` call the worker's HTTP API first, else edit the state files |
| Web | No UI from plugins: no routes or shell slots for Web modules, pages, sidebar rows. The plugins page lists contributions and refreshes on `plugins:changed`. A screen is an application's `application.json` UI |

## Implementation status

Implemented as described, except: auto-discovery of Claude Code's default directories (`commands/`, `agents/`, `skills/`, `hooks/hooks.json`) when the manifest omits them; Claude Code's `prompt` hook type; `lspServers` and `outputStyles`, parsed and ignored.
