# OpenCode plugins and permissions: hooks, rules, and a working guard

> OpenCode plugins and permissions: how to load a plugin, the hook events, ordered permission rules, and a guard example that denies a forbidden call.

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

## Loading a plugin

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

```jsonc
// opencode.jsonc
{
  "$schema": "https://opencode.ai/config.json",
  "plugins": ["opencode-acme-plugin@1.2.0", "./plugins/local"],
}
```

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

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

## The hook points around a tool call

OpenCode 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:

- `ctx.tool.hook("execute.before", ...)` runs before a tool executes, with the tool's input.
- `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.
- `ctx.tool.hook("execute.after", ...)` runs after the tool, with its result or its error.

![Diagram of the hook points around one OpenCode tool call: the model proposes a call, the ordered permission rules decide, a plugin's evaluate hook may change that decision, the tool runs, and the execute.after hook sees the result. A guard lives at the evaluate hook; the rules come from opencode.jsonc.](/blog/opencode-plugins-permissions-guide/hook-points.svg)

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

## Permission rules

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

```jsonc
{
  "permissions": [
    { "action": "shell", "resource": "*", "effect": "ask" },
    { "action": "shell", "resource": "git status *", "effect": "allow" },
    { "action": "shell", "resource": "git diff *", "effect": "allow" },
    { "action": "shell", "resource": "git push *", "effect": "deny" },
  ],
}
```

![Diagram of last-match-wins evaluation with four ordered rules: shell star asks, git status star allows, git diff star allows, git push star denies. The command git status --short ends at allow through rule two; git push origin main ends at deny through rule four.](/blog/opencode-plugins-permissions-guide/rule-order.svg)

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

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

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

## A guard example

Rules 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:

```ts
// .opencode/plugins/no-force-push/index.ts
import { Plugin } from "@opencode/plugin"

export default Plugin.define({
  id: "no-force-push",
  setup(ctx) {
    ctx.permission.hook("evaluate", async (event) => {
      if (event.action !== "shell") return
      const forced = event.resources.some((cmd) =>
        /git\s+push\s+.*(--force|-f)(\s|$)/.test(cmd),
      )
      if (forced) {
        event.effect = "deny"
        event.message = "Force-push is not allowed in this repository."
      }
    })
  },
})
```

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

## What rules and hooks cannot check

A 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](/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.

Besydr (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](/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](/pricing) with OpenCode after a trial.

## Sources and method

- 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.
- 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.
- Rule format, defaults and agent rules: the [permissions page](https://opencode.ai/docs/permissions/), read 2026-11-30.
- Besydr's OpenCode adapter and its limits: the [install](/docs/install) and [clients](/docs/clients) documentation. The 47-to-4 number comes from [our benchmark](/blog/claude-code-benchmark-with-and-without-besydr), five projects, three runs per setup.
- This post was drafted with AI assistance from these sources.
