Octonote AI
Make your own →

03 | Tool Execution and Permission Boundaries

Sunil Tiwari · Oct 29, 2026 · 15 min read

Expanded from Part 3 of OpenCode Architecture by Vinoth Govindarajan, The Agent Stack.

Post: "OpenCode Architecture, Part 3: Tool Execution and Permission Boundaries", subtitle "What did you actually approve?", published 5 October 2026. Tested on OpenCode v1.18.30 (source pinned at commit 3104c14). Architecture map (canvas): the canvas in this project (Canvas or Split view)

You set a coding agent to ask before it edits a file, and you read that as "ask me first". Vinoth's Part 3 shows that in OpenCode's noninteractive CLI, the same proposed edit under the same permission system produced three very different outcomes, and in none of them did a person approve anything. His central sentence is worth keeping close: ask "is not itself a human approval". It means the permission system needs a decision. The real question is who is allowed to answer it.

He gives one mental model for the whole post:

proposal → policy → responder (only if needed) → execution → independent evidence

The model proposes. The permission service rules. A client answers only a pending ask. The tool performs the write. Checks outside the agent say what actually changed. "Permission granted", "operation executed" and "task succeeded" are three separate claims, and the post is a careful tour of the gaps between them.

Overview: where authority lives between a proposal and a write

Where authority lives between a proposal and a write
Where authority lives between a proposal and a write

One edit, four outcomes

His experiment. The task was a tiny bug in slug.mjs: repeated whitespace should collapse into one separator. Instead of a real model, Vinoth used a scripted provider, a local HTTP endpoint that stands in for the model service and returns predetermined tool calls. OpenCode did the real read and edit. No inference happened, which is the point: any difference in outcome comes from OpenCode's runtime, not from a model changing its mind.

Every case received the same read request and the same edit arguments:

{
  "filePath": "slug.mjs",
  "oldString": ".replaceAll(' ', '-')",
  "newString": ".replace(/\\s+/g, '-')"
}

Each case started from identical source and tests, a fresh process and session, separate workspace and app-state directories, and no remembered approvals carried over. An external Python harness recorded the resolved config, the build agent's permissions and the session rules, so the effective setup was checked, not assumed. Before each run, one test passed and two failed.

Target edit ruleNoninteractive CLI modeRuntime outcomeSource fileFixed tests
allowOrdinaryEdit completedIntended replacement written3 pass, 0 fail
askOrdinaryAsk logged, then rejectedUnchanged1 pass, 2 fail
ask--autoAsk logged, edit completedIntended replacement written3 pass, 0 fail
deny--autoDirect policy denialUnchanged1 pass, 2 fail

Three details make this stronger than a demo:

  • All four OpenCode processes exited zero. Two of them left the file unchanged and the tests failing. Exit codes alone told you nothing.
  • Evidence came from outside. The harness ran fixed Node tests and captured file snapshots; a separate raw checker later rebuilt the comparisons against the current workspace bytes. OpenCode never ran the tests through its own shell tool.
  • Positive controls. The allow and auto-ask cases wrote identical, intended bytes, so an unchanged file in the other cases cannot be blamed on a replacement that never worked. Matching call ids across provider responses, CLI output and session exports tie each proposal to its outcome.

The full receipt, claim matrix and reproduction steps are in his companion lab. This is a pattern worth copying for any agent harness: script the model, run the real tools, and judge the result by artifacts.

The lab harness: who runs what
The lab harness: who runs what

Policy, replies, and automatic responses are different controls

There are three separate knobs here, and most confusion comes from merging them.

  1. Policy evaluation returns allow, deny or ask. The tool requests a check; the permission service matches permission names and patterns. The model proposes the call but never picks the verdict.
  2. Replies are once, always or reject. They only exist for a pending ask.
  3. Automatic reply behaviour is what --auto controls. It is not a verdict and it is not an always grant. It decides how the run command answers asks.

In the code (packages/opencode/src/permission/index.ts), ask() loops over the request's patterns. Any pattern that evaluates to deny fails immediately with PermissionDeniedError, before a pending request exists. Patterns that evaluate to allow are skipped. If at least one pattern needs ask and none is denied, the service creates a Deferred, stores it in pending, publishes permission.asked, and waits.

The noninteractive handler in packages/opencode/src/cli/cmd/run.ts is short enough to quote in spirit: on each permission.asked event for its sessions, if auto is set it replies once; otherwise it prints a warning and replies reject. The flag's own help text says "auto-approve permissions that are not explicitly denied (dangerous!)". In the ordinary ask case Vinoth captured:

permission requested: edit (slug.mjs); auto-rejecting

The exported error read: "The user rejected permission to use this specific tool call." No user did. The client answered for the run. That message is the literal text of PermissionRejectedError in packages/core/src/v1/permission.ts, which is shared by human and automatic rejections alike. He is careful to note that in the auto case he captured evaluation and ask creation, but not the reply payload; that the reply was once comes from the handler source.

Denied and rejected are different errors, and the loop treats them differently. In session/processor.ts, a PermissionRejectedError marks the turn as blocked (unless experimental.continue_loop_on_deny is set), so the session stops. A PermissionDeniedError is reported back to the model as a tool error, and the loop can go on. That matches his recorded outcome: the ordinary ask stopped before another provider request, while direct denial reached a later request. He frames these as recorded outcomes, not universal call-count rules, which is the right caution. One more variant exists in the source: a reject that carries a message becomes PermissionCorrectedError, so feedback reaches the model.

One edit call through deny, allow and ask
One edit call through deny, allow and ask

Availability comes before the particular decision

Before any call is judged, OpenCode decides which tools the model even sees. tool/registry.ts picks implementations first; for model ids containing gpt- (but not oss or gpt-4) it offers apply_patch instead of edit and write. Then request preparation (session/llm/request.ts) drops tools the user disabled and tools that Permission.disabled() hides.

The hiding rule is precise. disabled() maps edit, write and apply_patch onto the edit permission, finds the last rule matching that permission, and hides the tool only if that rule is a catch-all "*": "deny". Vinoth's reduced fragment shows why this matters:

{
  "permission": {
    "edit": {
      "*": "deny",
      "slug.mjs": "ask"
    }
  }
}

The last edit rule targets slug.mjs, not *, so edit stays advertised even though most files are denied, and even if you change that last action to deny. In his runs every captured request advertised edit, read and write, so the deny case was a real call-time refusal, not a missing tool. Availability is not authorization: a visible tool can still be refused per call, and a hidden tool tells you only about the final catch-all rule.

He adds a second caution: effective policy is bigger than any one config file. The agent service (agent/agent.ts) starts from defaults that are broadly permissive ("*": "allow", with ask for external_directory, doom_loop and .env reads), then layers user config and agent overrides. The tool context (session/tools.ts) passes the agent's rules followed by the session's rules. The evaluator's own fallback when nothing matches is ask, but you rarely reach it because the defaults start with *: allow.

From ruleset to advertised tool to call-time verdict
From ruleset to advertised tool to call-time verdict

Once is one request, always changes later decisions

once sounds like an approval budget: allow the first, refuse the next. The code does not work that way. In reply(), after removing the request from pending and publishing permission.replied, a once succeeds that one Deferred and then simply returns. Nothing is recorded. Meanwhile the CLI keeps answering every applicable permission.asked event with once for as long as --auto is on. There is no counter anywhere.

So a later ask gets its own fresh answer. The first approval neither pre-authorises it nor uses up the CLI's willingness to approve. If a retry creates a new invocation and a new ask, that too gets a fresh answer. Vinoth is explicit that this comes from source inspection; his four cases each made one edit, and he did not test retries.

A practical consequence: --auto behaves like "approve every non-denied ask for the life of this run", not "approve the next one". If you want a budget, you have to build it in the responder.

What --auto answers: once, for every ask
What --auto answers: once, for every ask

Remembering a decision changes its reach

always takes a different path. It resolves the current request, then pushes the tool's always patterns into the service's approved list as allow rules. It then re-checks other pending requests from the same session and releases those that now evaluate to allow. Future matching asks resolve without prompting.

What does "always" cover? For the edit tool, the always patterns are ["*"] (see tool/edit.ts and tool/write.ts). The wildcard is inside the edit permission, so it covers every file edit, not just the diff or file you were shown, but it does not touch bash or read. The shell tool is narrower: its always patterns are the command prefix plus * (for example git commit *), built by BashArity.prefix.

How long does it last? The approved list lives in a directory-keyed instance cache (effect/instance-state.ts, a ScopedCache keyed by directory). It is not written to global config, and disposing the instance drops it. But it is not tied to one conversation either: other sessions in the same directory instance consult the same live list.

Does it beat a configured deny? In v1.18.30, yes, potentially. evaluate() flattens ruleset then approved, and takes findLast, the last matching rule, not the most specific one. Remembered allows come after your config, so a later matching remembered allow can win over a configured deny. Vinoth states the precise claim: a final evaluated deny refuses before asking and --auto cannot override it, but a configured deny is not immutable against later rules. His fresh-state cases did not test remembered approvals.

He turns this into a design point about approval fatigue. Asking for every routine edit pushes people towards clicking "always" just to keep working, which delegates more authority than they meant to. He would prefer bounded pre-authorisation for operations you understand, plus approval choices whose scope and lifetime are visible. Auto-answering and remembering both reduce interruptions, but "neither means the model learned which actions were safe". The question to ask is what authority you delegated, to which responder, for how long.

Replies, the approved cache and a later ask
Replies, the approved cache and a later ask

The edit prepares a change before it waits

This section walks the edit tool line by line, and it holds up against tool/edit.ts:

  1. The tool wrapper (tool/tool.ts) decodes arguments.
  2. execute rejects empty paths and identical old/new strings, resolves a relative path against the instance directory, and runs assertExternalDirectoryEffect (which may itself ask for external_directory).
  3. It takes a per-path semaphore: lock(filePath) returns a one-permit Semaphore from a module-level Map, keyed by the resolved path.
  4. Inside the lock it stats and reads the current file, preserves line endings and BOM, runs the replacement (matching can fail here, before any permission request), and builds a unified diff.
  5. It calls ctx.ask({ permission: "edit", patterns: [worktree-relative path], always: ["*"], metadata: { filepath, diff } }) while still holding the lock.
  6. On success it writes the prepared bytes, optionally runs the formatter (format/index.ts) and re-reads, publishes edit and watcher events, and recomputes the diff. After the lock, it touches the file in LSP and appends diagnostics.

Note that this current-file read is the edit tool's own, separate from the earlier read the provider asked for.

His cautions, each worth repeating:

  • The evaluator matches names and patterns, not correctness. A permit says nothing about whether the diff fixes the bug.
  • The semaphore is narrow. It serialises participating edits to the same path inside this process. It is not a cross-process file lock or a transaction. Shell commands and your editor do not take it.
  • No re-read between the wait and the write. If an external writer changes the file while the ask is pending, the prepared bytes overwrite it. He calls this an ordering limit in the source, not a race he reproduced. In the interactive TUI, where a person may take minutes to answer, that window is real.
  • Formatting is not a shell call proposed by the model; it was disabled in the lab, so no formatter bytes were demonstrated.
  • Refusal does not mean nothing happened. Preparatory reads still ran, and a later error alone would not prove the file is unchanged.
Inside the edit tool: read, ask, then write
Inside the edit tool: read, ask, then write

The gate does not replace the host boundary

His lab layout itself separates responsibilities: the scripted provider runs as HTTP threads inside the Python runner; OpenCode and the Node tests are subprocesses; the raw checker runs later. Inside OpenCode, the local CLI adapter dispatches requests in-process and the AI SDK path (session/llm.ts) wires provider tool calls to registered callbacks. The provider proposes; OpenCode's tools act.

The bigger point concerns the shell. Its compatibility id and permission key is bash, even when the selected executable is PowerShell (his Windows lab logged PowerShell start-up, though no shell command was proposed). In tool/shell.ts OpenCode parses the command with tree-sitter, asks external_directory for directories outside the project, then asks bash with the parsed command patterns. That analysis recognises particular forms, not every effect a program can have. Once it runs, the process has your environment and your user's reach.

So restricting edit is not a ban on file changes. write shares the edit permission, but sed -i through bash is judged by the bash rules, not the edit gate. OpenCode's SECURITY.md is blunt: OpenCode "does not sandbox the agent", and the permission system "is not designed to provide security isolation"; for real isolation, run it in a container or VM. The gate still mattered in his lab, it controlled the requested writes, but a rule like "this file must be untouchable no matter which program tries" belongs to the execution environment.

Two paths to a file: edit gate vs bash
Two paths to a file: edit gate vs bash

What to steal

Vinoth closes with "What I would check before automating this": three failure patterns worth carrying into any harness, and his own plan.

  • An ask without the expected reviewer. Record effective policy and client mode together. If human review is required, prove the request reaches a human, rather than assuming ask supplies one.
  • A remembered grant broader than the displayed action. Inspect the permission category, the patterns, the lifetime and the precedence of remembered approvals. Fewer interruptions only help if the resulting authority is still understandable.
  • A successful process with an unchecked artifact. Every exit here was zero. Make direct denial and client rejection explicit workflow outcomes, then accept against the artifact and suitable checks.
  • Decide which operations can be pre-authorised, who answers the remaining asks, and what scope a remembered grant may carry.
  • Choose the host boundary (container, VM, OS permissions) separately for resource restrictions that must hold against any program.
  • Verify effects; never treat a quiet run as a successful one.
  • Bonus from the lab design: test agent runtimes with a scripted provider so outcomes reflect the runtime, not the model.

He is careful about scope even here: the fixed tests support this small replacement and are not a full specification, and the bounded manifest found no extra top-level fixture file, which is not the same as proving no side effects anywhere.

Checklist: before automating, after each run
Checklist: before automating, after each run

Experiment note, and what has changed since v1.18.30

Vinoth tested OpenCode v1.18.30 with a scripted local provider, isolating runtime behaviour from model decisions. He expected an edit set to ask to ask a person; the result "exposed the missing owner: the client that answers."

I checked his claims against the pinned commit and they match the code line for line. OpenCode has moved fast since (the repo now lives at anomalyco/opencode, with a v2 line; v2.2.1 at the time of writing, and v1.19.5 still on the v1 line with the same permission code). In v2.2.1 the permission service moved to packages/core/src/permission.ts and the CLI handler to packages/cli/src/run/noninteractive.ts. Four differences matter for this post:

  • Configured deny now wins over remembered allows. evaluateInput checks the configured rules for deny first, and only then adds saved rules. The "remembered allow beats configured deny" ordering applies to v1.x.
  • always is now persisted per project. Saved approvals live in a SQLite table (permission/saved.ts), not the in-memory directory cache, so they outlive the process.
  • A plugin hook can change the verdict (permission.evaluate), adding another party to "who decides".
  • The noninteractive client still replies once with --auto and reject without it, and now also interrupts the session on rejection. The edit tool still holds a per-path lock across the permission wait and still does not re-read before writing, though it now asks before checking whether oldString matched.
What changed from v1.18.30 to v2.2.1
What changed from v1.18.30 to v2.2.1

Quick self-check

  1. In opencode run without --auto, an edit set to ask reaches the permission service. What happens, and who decided?
  2. Why can an edit tool still be advertised to the model when most edit paths are denied?
  3. You run with --auto and the agent makes five edits. How many does once approve?
  4. In v1.18.30, how can a configured deny lose?
  5. Your config denies edits to prod/values.yaml. Can the agent still change that file?
  6. All four runs exited zero. What evidence did Vinoth use instead?

Answers:

  1. The CLI handler prints "auto-rejecting" and replies reject; the edit fails with PermissionRejectedError and the turn stops. The client decided; no person did.
  2. Permission.disabled only hides a tool when the last matching rule is a catch-all * deny. A later file-specific rule keeps it visible, and the refusal happens at call time.
  3. All five that evaluate to ask. once resolves one request and records nothing; the CLI answers every ask while --auto is on.
  4. Remembered always allows are appended after config rules and findLast picks the last match, so a remembered allow can win. (v2.x checks configured deny first.)
  5. Yes, through bash (for example sed -i) if bash rules allow it. The edit gate covers edit/write, not every program; isolation is the host's job.
  6. Fixed Node tests run by the harness, file snapshots, and a separate raw checker comparing captures with current bytes, plus matching call ids across logs.

Source and credit

All credit for the experiment, the framing and the checklist goes to Vinoth Govindarajan. This note expands and checks his work; read the original.

Make a page like this. Free.Notes and diagrams side by side, written by you or by Claude Code and Codex. Publish it in one click.
Make your own, free