# opencode plugin: risky commands held for your OK

Govern opencode (sst/opencode) in one command. A native plugin checks, scopes, and audits every tool call: allow/deny/ask policy, full audit trail, per-user attribution, plus zero-credential local metering. Free up to 5 agents.

# Stop a force-push and ask before risky commands in opencode

<p style="font-size:17px;line-height:1.6;color:var(--acp-text);max-width:660px;margin:8px 0 6px;">See, control, and price <strong>every tool call opencode makes</strong>: the bash commands, the file edits, the web fetches, and the model calls behind them. One command, no code changes.</p>

Every tool opencode runs (`bash`, `edit`, `webfetch`, MCP tools) is checked against your workspace policy before it executes. **Allow** runs it. **Deny** blocks it and shows the reason. **Ask** blocks it in opencode and holds it for approval on the [console Approvals page](https://cloud.agenticcontrolplane.com/approvals) or from the approval email. Then you ask opencode to retry. The hold reaches opencode as a blocked call, and that applies even to tools your `permission` block sets to allow.

A second part needs no account at all. Local metering records the tokens and cost of every model call to a database on your machine.

## Will it stop the agent from wrecking a box it can reach?

That is usually the question behind the click. With the plugin installed, `bash` set to `ask`, and the workspace in enforce mode:

- A command that runs somewhere the hook cannot see, such as `ssh host 'sudo pct destroy 300'`, `docker exec ...`, `pct exec ...`, or `qm guest exec ...`, is held for your approval.
- `reboot`, `shutdown`, and the rest of the always-blocked list are blocked outright. No approval click reaches them.
- Everything else runs and lands in the activity log. One approval can become a standing rule, so you are not reviewing the same shape twice. [How approvals work &rarr;](/docs/approvals)

In audit mode (the default for a new workspace) the remote-command holds are recorded, not held. The always-blocked list still blocks. The safety floor still holds force-push, destructive SQL, `curl | sh` and recursive deletes outside the project, in every mode. The [rules page](/docs/rules) has the full list.

This is a guard against accidents and casual re-spellings. It is not a sandbox. A restricted account and sudoers on the target machine are still the floor, and this sits on top of them.

## Install





The installer detects opencode (`~/.config/opencode` or the `opencode` binary). It registers the [`acp-opencode`](https://www.npmjs.com/package/acp-opencode) plugin and a permission gate in `~/.config/opencode/opencode.json`, then opens your browser to set up a workspace. Restart opencode. It installs the plugin from npm on the next start. After that, every tool call is checked against your policy and lands in one activity log, with the cost X-ray next to it. Free up to 5 agents.

On Windows, run `irm https://agenticcontrolplane.com/install.ps1 | iex` in PowerShell. It detects opencode and writes the same plugin and permission entries to `%USERPROFILE%\.config\opencode\opencode.json`.

**Want it fully on-device?** opencode is cloud-only today. The `acp-opencode` plugin needs a workspace, so the installer's `--local` flag skips opencode and says so. What does run without an account is [local metering](#local-metering-no-account-required): every model call's tokens and cost go to `~/.acp/opencode-local.db`, and you read them with `npx acp-opencode report`. That ledger stays on the one machine. It has no workspace policy and no shared activity view.

To wire it by hand, add two blocks to `~/.config/opencode/opencode.json`:

```json
{
  "$schema": "https://opencode.ai/config.json",
  "plugin": ["acp-opencode"],
  "permission": {
    "bash": "ask",
    "edit": "ask",
    "webfetch": "ask"
  }
}
```

Then set your ACP key (`export ACP_BEARER_TOKEN="gsk_yourslug_..."`, or write it to `~/.acp/credentials`). The `permission` block matters; the next section says why.

## How it works

Claude Code, Cursor, and Codex run a hook script that reads the call from stdin. opencode is different. It has [a plugin system](https://opencode.ai/docs/plugins/), where a module's hooks run inside the opencode process. `acp-opencode` registers four:

| Hook | What it does |
|---|---|
| `permission.ask` | The main check. It POSTs to `/govern/tool-use`. An ACP `allow` sets the permission to `allow`, so opencode's own prompt is skipped. A `deny` blocks. An `ask` blocks the call until you approve it on the console Approvals page or from the approval email. Then you ask opencode to retry. |
| `tool.execute.before` | A deny-only backstop for tools that don't go through the permission system. An ACP `deny` throws, which is how opencode blocks a call. It also caches the arguments so `permission.ask` can attach them. |
| `tool.execute.after` | Sends the result to `/govern/tool-output` for audit and scanning (PII, injection, secrets). It is best-effort and never blocks. |
| `chat.headers` | Stamps `X-GS-Session` and `X-GS-Client` on every model request, so proxy-metered spend joins the audit rows for the same session. |

All four send `X-GS-Client: opencode-plugin/<version>`, so the dashboard can tell opencode traffic from Claude Code and Cursor.

**Why the `permission` block matters.** `permission.ask` only fires for a tool whose permission is `"ask"`. Without the block you still get the deny-only backstop and the audit trail, but an ACP `allow` can't pre-approve opencode's own prompt away. The installer sets `bash`, `edit` and `webfetch` to `ask` for you. It fills gaps only and never overrides a choice you made yourself.

## What gets installed and where

| Path | Contents |
|---|---|
| `~/.config/opencode/opencode.json` | `plugin` and `permission` entries added (existing config kept) |
| `~/.cache/opencode/.../acp-opencode/` | The plugin package, installed from npm by opencode at startup |
| `~/.acp/credentials` | Your bearer token (the same credential every ACP integration uses) |
| `~/.acp/opencode-local.db` | Local metering database (created on first run, no account needed) |

One `install.sh` run covers opencode alongside Claude Code, Cursor, and the rest. To uninstall, remove the `plugin` entry from `opencode.json`.

<a id="local-metering-no-account-required"></a>

## Local metering: no account required

The checks above need a workspace key. Metering doesn't. With no credentials, `acp-opencode` listens to opencode's events and records every model call to SQLite at `~/.acp/opencode-local.db`. It stores tokens (including cache read and write), the model, and the cost opencode computed. It also stores every tool result. Read it back:

```bash
npx acp-opencode report            # spend by model, cache hit rate, tool errors, per-task cost
npx acp-opencode report --json     # machine-readable, so an agent can read its own economics
npx acp-opencode report --days 30  # widen the window (default 7)
```

It keeps no price table. Cost comes from opencode's own pricing data. When opencode reports no cost (an unknown model, or subscription-included auth), the call is stored with the cost blank and the tokens exact. Money is never guessed. Turn metering off with `ACP_LOCAL_METERING=off`. If storage fails, it disables itself with one warning instead of interrupting your agent.

## Add the cost X-ray

Local metering runs on your machine. The **cost X-ray** puts priced spend, prompt-cache hit rate, and the loop-vs-leaf breakdown on the team dashboard, metered at the gateway. Start opencode with `opencode-acp` instead of `opencode`, the same way you use `claude-acp` and `codex-acp`:

```bash
opencode-acp
```

The installer already wrote the `acp` provider into `~/.config/opencode/opencode.json` and left your default model alone, so plain `opencode` is untouched. `opencode-acp` reads your workspace key from `~/.acp/credentials`, exports it as `ACP_BEARER_TOKEN`, and selects `acp/gemini-3.5-flash` for that launch only. `ACP_OPENCODE_MODEL=<id>` picks another. The proxy routes `gpt-*`, `claude-*` and `gemini-*` by model id and forwards to the real provider unchanged. If the ACP gateway is unreachable, it starts plain `opencode` and says so.

What the installer wrote:

```json
// ~/.config/opencode/opencode.json (provider only, no top-level "model" change)
{
  "provider": {
    "acp": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "Agentic Control Plane",
      "options": {
        "baseURL": "https://api.agenticcontrolplane.com/v1",
        "apiKey": "{env:ACP_BEARER_TOKEN}"
      },
      "models": { "gemini-3.5-flash": {} }
    }
  }
}
```

The plugin's `X-GS-Session` header ties that spend to the tool-audit rows for the same session. Full walkthrough: [Turn on Cost X-Ray](/cost-tracking).

<a id="fail-open-by-default--and-opting-in-to-fail-closed"></a>

## If the gateway is unreachable

On a network error, a timeout (over 2 seconds), a non-2xx response, or a malformed body, the plugin **lets the call through and prints one loud warning per session**. If every call is being blocked, that is a `deny` policy, not an outage. Check the dashboard.

To block whenever the gateway is unreachable, turn on fail-closed. It is the same setting the shared hook uses for the other non-Claude-Code harnesses, so one setting covers them and opencode on that machine:

```bash
export ACP_FAIL_MODE=closed     # or: echo closed > ~/.acp/failmode
```

A policy `deny` always blocks, whatever the fail mode. A machine with no credentials is always a no-op, so fail-closed never blocks before you've connected. To turn the whole plugin off without touching your config:

```bash
export ACP_OPENCODE=off
```

## Setting up policy

Three kinds of policy apply to opencode, as on every harness:

1. **Tool policies** (Policies → Tool Policies): a workspace-wide tool allowlist and required scopes.
2. **Agent policies** (Policies → Agent Policies): rules per tier.
3. **User policies** (Policies → User Policies): overrides per person.

The most restrictive rule wins. Start in audit mode for a few days to see what your agents do, then switch to enforce mode.

**Policy keys for opencode's tools.** opencode spells its tools in lowercase, and workspace rules use canonical names. The gateway maps the native spelling first, so a rule you already have on `Bash.rm` or `Edit` applies to opencode calls with no opencode-specific copy. A rule written against the native spelling keeps working too.

| opencode tool | Policy key |
|---|---|
| `bash` | `Bash`, split by leading command: `Bash.git`, `Bash.rm`, `Bash.curl`, ... |
| `edit`, `patch` | `Edit` |
| `write` | `Write` |
| `read` | `Read` |
| `grep` | `Grep` |
| `webfetch` | `WebFetch` |
| `websearch` | `WebSearch` |
| `task` | `Agent` |

Your `permission` block decides which calls reach `permission.ask` and which only meet the backstop. The [opencode permissions reference](/blog/opencode-permissions-reference) has every key, the defaults, and the config layers.

## Limitations

What the opencode integration cannot do today:

- **Context composition isn't broken out.** Per-call token buckets, cache splits and cost are all recorded. The split of each prompt into system, user, assistant and tool parts is not, because opencode 1.18.4 exposes the outgoing messages only through an unstable experimental hook. The report's composition section will fill in once a stable hook exists.
- **Per-request model duration isn't exposed** per step. Message-level fallback rows carry wall-clock time and step rows record 0. Proxy-side metering covers durations for routed traffic.
- **Tier attribution is coarse.** opencode's hooks don't tell the plugin whether the `build` or `plan` agent is running, so calls report at the `interactive` tier.
- **Output scanning only flags.** The plugin blocks a call or allows it. On the output path it flags findings and does not redact.

## Troubleshooting

**Plugin isn't firing.** Restart opencode completely, because plugins load at startup. Confirm `"plugin": ["acp-opencode"]` is in `~/.config/opencode/opencode.json` and that opencode installed it (look under `~/.cache/opencode`).

**Prompts still appear for every tool.** That is the `ask` path with no ACP decision yet: no credentials, or a policy that says ask. Set `~/.acp/credentials` and check your Tool Policies.

**`acp-opencode report` shows `no price`.** opencode reported no cost for that model (unknown model, or subscription-included auth). Token counts are still exact. Route through the proxy for priced spend.

**Nothing in the dashboard.** The Activity page is per workspace. Check that the workspace tied to your token is the one selected in the top-right dropdown. Local metering is separate and works with or without an account.

## Related integrations

- [Hermes Agent](/integrations/hermes): native Python plugin, the same two-part design
- [Claude Code](/integrations/claude-code): hook-based, the most complete integration
- [OpenClaw](/integrations/openclaw): native plugin, like opencode
- [OpenAI Codex CLI](/integrations/codex): Claude-style hooks, currently Bash-only
- [Turn on Cost X-Ray](/cost-tracking): meter spend across any harness through the proxy
- [opencode permissions reference](/blog/opencode-permissions-reference): every key in the `permission` block, defaults, auto-approve, worked examples
- [opencode permissions, explained](/controls/opencode): the native control model and where it ends

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "HowTo",
  "name": "Install Agentic Control Plane in opencode",
  "description": "Install the acp-opencode plugin (sst/opencode) in one command: allow / ask / deny policy on every tool call, one shared activity log, and the cost X-ray.",
  "totalTime": "PT1M",
  "step": [
    {"@type": "HowToStep", "name": "Run the installer", "text": "curl -sf https://agenticcontrolplane.com/install.sh | bash . On Windows PowerShell: irm https://agenticcontrolplane.com/install.ps1 | iex . It detects opencode and writes the same plugin and permission entries to %USERPROFILE%\\.config\\opencode\\opencode.json."},
    {"@type": "HowToStep", "name": "Authenticate", "text": "Your browser opens to provision a workspace. The API key is saved to ~/.acp/credentials. Free up to 5 agents, and you get team policy plus the cost X-ray."},
    {"@type": "HowToStep", "name": "Restart opencode", "text": "opencode installs the acp-opencode plugin from npm and checks every tool call."},
    {"@type": "HowToStep", "name": "(Optional) Local metering, no account", "text": "acp-opencode also records every model call's tokens and cost to ~/.acp/opencode-local.db with no credentials; read it with npx acp-opencode report. There is no --local mode for opencode: policy decisions and the shared activity log need the workspace, and the installer skips opencode under --local."}
  ]
}
</script>
