{"slug": "opencode-plugins-and-permissions-hooks-rules-and-a-working-guard", "title": "OpenCode plugins and permissions: hooks, rules, and a working guard", "summary": "OpenCode's plugin and permission system lets developers fence agent actions with ordered rules where the last matching rule wins, and with TypeScript plugin hooks at execute.before, permission evaluate, and execute.after, according to the project's documentation. The permission evaluate hook runs only for allow and ask decisions, since an explicit configured deny is final and never reaches the hook, and its message becomes the denial reason the model reads. OpenCode 2 renamed the plugin package to @opencode/plugin with Plugin.define, breaking OpenCode 1 plugins that export a function and import @opencode-ai/plugin.", "body_md": "# OpenCode plugins and permissions: hooks, rules, and a working guard\n\nOpenCode plugins are TypeScript modules that hook into the agent's events, and OpenCode permissions are ordered rules that decide whether an action runs, asks you first, or is denied. Plugins extend what the agent can do; permissions fence what it may do. This post covers loading a plugin, the hook points around a tool call, the permission rule format, and a guard example built from them — then the check neither of them performs.\n\n## Loading a plugin\n\nA plugin is loaded from the `plugins` array in `opencode.json(c)`, or discovered automatically under `.opencode/plugins/` in the project and `~/.config/opencode/plugins/` globally. The [plugins documentation](https://opencode.ai/docs/plugins/) lists the accepted forms: an npm package, a package pinned to a version, a scoped package, or a local directory.\n\n```\n// opencode.jsonc\n{\n  \"$schema\": \"https://opencode.ai/config.json\",\n  \"plugins\": [\"[email protected]\", \"./plugins/local\"],\n}\n```\n\nThe CLI manages global packages: `opencode plugin add <name>` installs one, `opencode plugin list` shows the plugins list, `opencode plugin update` updates outdated packages, and `opencode plugin remove` takes one out. A `-` prefix in the config disables a plugin by ID or wildcard, so `\"-acme.reviewer\"` switches one off without deleting it.\n\nOne version note matters before you copy any example. OpenCode 2 renamed the plugin package and the permission fields: the current API is `@opencode/plugin` with `Plugin.define`, while OpenCode 1 plugins export a function and import `@opencode-ai/plugin`. The [V1 documentation](https://opencode.ai/v1/docs/plugins.md) and the [migration guide](https://opencode.ai/docs/build/plugins/migrate-v1) cover the old form. The examples below use the current API.\n\n## The hook points around a tool call\n\nOpenCode hooks are the places where a plugin can inspect or change what the agent does. The [server plugin reference](https://opencode.ai/docs/build/plugins/server) documents three that matter for safety:\n\n- `ctx.tool.hook(\"execute.before\", ...)` runs before a tool executes, with the tool's input.\n- `ctx.permission.hook(\"evaluate\", ...)` runs after the configured permission rules are evaluated and before the action runs or a prompt is shown. It can change the decision to`allow` ,`ask` or`deny` , and set the message the agent sees.\n- `ctx.tool.hook(\"execute.after\", ...)` runs after the tool, with its result or its error.\n\nTwo properties of the evaluate hook shape what a guard can do. It runs only for `allow` and `ask` decisions — an explicit configured `deny` is final and never reaches the hook. And its `message` becomes the denial reason the model reads, so a denied call can explain itself instead of failing silently.\n\n## Permission rules\n\nA permission rule has three fields: an `action` (what kind of operation), a `resource` (the path, command or URL it touches) and an `effect` (`allow`, `deny` or `ask`). The [permissions documentation](https://opencode.ai/docs/permissions/) defines the evaluation: rules are combined in order and **the last matching rule wins**, so specific exceptions follow the broad rule they carve out.\n\n```\n{\n  \"permissions\": [\n    { \"action\": \"shell\", \"resource\": \"*\", \"effect\": \"ask\" },\n    { \"action\": \"shell\", \"resource\": \"git status *\", \"effect\": \"allow\" },\n    { \"action\": \"shell\", \"resource\": \"git diff *\", \"effect\": \"allow\" },\n    { \"action\": \"shell\", \"resource\": \"git push *\", \"effect\": \"deny\" },\n  ],\n}\n```\n\nThe built-in actions cover `read`, `edit`, `shell`, `glob`, `grep`, `webfetch`, `websearch`, `subagent` and `skill`, among others; an MCP tool matches as `<server>_<tool>`. Patterns use whole-value wildcards: `*` matches any characters including `/`, and a shell pattern ending in  `*` also matches the command without arguments. When one operation checks several resources, any `deny` denies it, otherwise any `ask` asks.\n\nEvery agent starts from a base policy that allows everything and asks only for `.env` reads, with `.env.example` allowed again. That default is the answer to the question people ask as \"opencode dangerously skip permissions\": OpenCode has no such flag, because the default already runs most actions without prompting. The work is the opposite of Claude Code's — you add `ask` and `deny` rules to tighten a permissive default, rather than allow rules to loosen a prompting one. If you want the fully unattended posture explicit, it is one rule, `{ \"action\": \"*\", \"resource\": \"*\", \"effect\": \"allow\" }`, and it removes the `.env` guardrail too.\n\nRules can also sit under `agents.<id>.permissions`. Agent rules are appended after the global array, not substituted for it, and a custom subagent uses its own permissions rather than a subset of its parent's. OpenCode 1 used different names — `permission`, `bash` and `task` where version 2 uses `permissions`, `shell` and `subagent` — so check which version a config example was written for.\n\n## A guard example\n\nRules match patterns; a plugin can read the call. This guard denies force-pushes whatever branch they target, using the documented evaluate hook. It is built from the [hook contract](https://opencode.ai/docs/build/plugins/server) rather than run against a live install, so treat it as a starting point:\n\n``` js\n// .opencode/plugins/no-force-push/index.ts\nimport { Plugin } from \"@opencode/plugin\"\n\nexport default Plugin.define({\n  id: \"no-force-push\",\n  setup(ctx) {\n    ctx.permission.hook(\"evaluate\", async (event) => {\n      if (event.action !== \"shell\") return\n      const forced = event.resources.some((cmd) =>\n        /git\\s+push\\s+.*(--force|-f)(\\s|$)/.test(cmd),\n      )\n      if (forced) {\n        event.effect = \"deny\"\n        event.message = \"Force-push is not allowed in this repository.\"\n      }\n    })\n  },\n})\n```\n\nThe directory loads automatically under `.opencode/plugins/`. A configured `deny` rule for `git push *` would stop every push before this hook runs; the hook earns its place where the pattern is narrower than the rule — here, ordinary pushes stay allowed and only forced ones are denied, with a reason the agent can act on.\n\n## What rules and hooks cannot check\n\nA permission rule answers \"may this action run\". A hook answers whatever its code asks. Neither answers \"is this what the developer asked for\" — a call can pass every rule and still break a rule you stated in words, such as \"do not touch the billing module\". That gap is the one [our Claude Code benchmark](https://besydr.com/blog/claude-code-benchmark-with-and-without-besydr) measured: rules from an earlier session were broken 47 times per 100 requests, and 4 times with Besydr.\n\nBesydr (say: besider) is a program that runs beside a coding agent and learns from its sessions. On OpenCode it connects as a plugin: `besydr install opencode` writes one file that runs Besydr at eight events, repeating your stated rules with each request and checking proposed calls against your words. Two limits are documented honestly in the [client table](https://besydr.com/docs/clients): the plugin targets the OpenCode 1.x plugin interface (checked with 1.19.5) and has not been checked against OpenCode 2, and no OpenCode plugin hook stops a prompt, so Besydr cannot block one there. A one-shot `opencode run` also ends before the completion check can send the agent back; `opencode serve` and `opencode run --attach` do continue. The local program is free with octomind and needs a [licence](https://besydr.com/pricing) with OpenCode after a trial.\n\n## Sources and method\n\n- Plugin loading, CLI and version differences: the [plugins](https://opencode.ai/docs/plugins/) ,[building plugins](https://opencode.ai/docs/build/plugins) and[V1 plugins](https://opencode.ai/v1/docs/plugins.md) pages, read 2026-11-30.\n- Hook contract and the guard example: the [server plugin reference](https://opencode.ai/docs/build/plugins/server) , read 2026-11-30. The example follows the documented API; it was not executed for this post.\n- Rule format, defaults and agent rules: the [permissions page](https://opencode.ai/docs/permissions/) , read 2026-11-30.\n- Besydr's OpenCode adapter and its limits: the [install](https://besydr.com/docs/install) and[clients](https://besydr.com/docs/clients) documentation. The 47-to-4 number comes from[our benchmark](https://besydr.com/blog/claude-code-benchmark-with-and-without-besydr) , five projects, three runs per setup.\n- This post was drafted with AI assistance from these sources.", "url": "https://wpnews.pro/news/opencode-plugins-and-permissions-hooks-rules-and-a-working-guard", "canonical_source": "https://besydr.com/blog/opencode-plugins-permissions-guide", "published_at": "2026-11-30 00:00:00+00:00", "updated_at": "2026-12-01 03:44:48.126706+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools", "ai-safety"], "entities": ["OpenCode", "@opencode/plugin", "@opencode-ai/plugin", "Plugin.define"], "also_reported_by": [], "takeaway": "In OpenCode, order permission rules so the last matching rule wins and put specific exceptions after the broad rule they carve out, and use the permission evaluate hook to turn a denial into an explanatory message the model reads, remembering that an explicit deny never reaches the hook.", "recommendation": {"advice": "Fence agent shell access with ordered OpenCode permission rules — ask on shell *, allow git status * and git diff *, deny git push * — and use the evaluate hook to return a denial message the model can read.", "playbook": "agent-security", "playbook_url": "https://wpnews.pro/playbooks/agent-security"}, "alternates": {"html": "https://wpnews.pro/news/opencode-plugins-and-permissions-hooks-rules-and-a-working-guard", "markdown": "https://wpnews.pro/news/opencode-plugins-and-permissions-hooks-rules-and-a-working-guard.md", "text": "https://wpnews.pro/news/opencode-plugins-and-permissions-hooks-rules-and-a-working-guard.txt", "jsonld": "https://wpnews.pro/news/opencode-plugins-and-permissions-hooks-rules-and-a-working-guard.jsonld"}}