Skip to content

Commit c964bdd

Browse files
feat(opencode): add OpenCode support via a tool.execute.before plugin shim (#28)
* feat(opencode): add OpenCode support via a tool.execute.before plugin shim Wire allowlister into OpenCode (sst/opencode) as a first-class harness. OpenCode is the first integration that does not expose a subprocess hook: it blocks a tool call only from an in-process JS plugin. So support has two halves — a Rust adapter and a bundled JS shim that bridges to it. Adapter (`io/harness/opencode.rs`): reads the shim's JSON on stdin (`{"tool_name":"bash","tool_input":{"command":…},"cwd":…}`), evaluates the command, and emits a flat `{"decision":"deny","reason":…}` only on a deny. Allow/defer emit nothing and it always exits 0, so an internal read/parse error can never become a block. Plugin shim (`io/opencode_settings.rs` writes `.opencode/plugin/allowlister.js`): a single-export `tool.execute.before` plugin that gates on command *content* (any tool call carrying a `command` arg — covers OpenCode's bash/shell tools across builds), spawns `allowlister hook opencode` via `Bun.spawn`, and `throw`s on a deny (the only block primitive the hook offers). It fails open if the binary can't run. OpenCode auto-loads the file with no `opencode.json` entry, and the hook fires for sub-agent (`task`) calls too (anomalyco/opencode#5894 is closed not-a-bug). `init` writes it idempotently; rules live in `.allowlister.json`, not the shim. Also: `init --harness opencode`, `hook opencode` dispatch, the `Harness::OpenCode` CLI variant, hermetic unit + e2e tests (incl. the shim file write + content), an opt-in live check (`scripts/e2e-opencode.sh`, `just test-opencode`) that proves the throw holds under `--dangerously-skip-permissions`, a gated `e2e-opencode.yml` workflow, and README/AGENTS updates. Coverage stays above the 95% floor. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> * test(opencode): use a redirect-free allow command in the live e2e The headless model sometimes dropped the `> file` redirection, failing the allow case for a reason unrelated to the gate. Assert allow with `mkdir` instead, and dump the allow transcript on failure. Redirection policy stays covered hermetically. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 7e46ef0 commit c964bdd

11 files changed

Lines changed: 998 additions & 2 deletions

File tree

‎.github/workflows/e2e-opencode.yml‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: E2E (OpenCode)
2+
3+
# Live end-to-end check that drives the real OpenCode CLI against allowlister
4+
# wired as a tool.execute.before plugin shim (scripts/e2e-opencode.sh). Unlike
5+
# ci.yml this needs network, a model call, and an LLM provider key
6+
# (ANTHROPIC_API_KEY), so it lives in its own workflow gated to trusted contexts
7+
# only.
8+
#
9+
# Security: a secret in a public repo must never be reachable by untrusted code.
10+
# GitHub already withholds secrets from fork pull requests; the job `if` below is
11+
# defense in depth — it runs only in the canonical repo and never for a PR whose
12+
# head is a fork, so this workflow can be edited in a fork PR without exposing
13+
# the key.
14+
15+
on:
16+
push:
17+
branches: [main]
18+
pull_request:
19+
workflow_dispatch:
20+
21+
permissions:
22+
contents: read
23+
24+
concurrency:
25+
group: e2e-opencode-${{ github.ref }}
26+
cancel-in-progress: true
27+
28+
env:
29+
CARGO_TERM_COLOR: always
30+
31+
jobs:
32+
live:
33+
name: opencode live e2e
34+
if: >-
35+
github.repository == 'nickderobertis/allowlister' &&
36+
(github.event_name != 'pull_request' ||
37+
github.event.pull_request.head.repo.full_name == github.repository)
38+
runs-on: ubuntu-latest
39+
timeout-minutes: 20
40+
steps:
41+
- uses: actions/checkout@v4
42+
43+
- name: Verify the provider key is present
44+
env:
45+
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
46+
run: |
47+
if [ -z "${ANTHROPIC_API_KEY:-}" ]; then
48+
echo "::error::ANTHROPIC_API_KEY secret is not set (or set ALLOWLISTER_E2E_MODEL + its provider key). Add it: gh secret set ANTHROPIC_API_KEY --repo ${{ github.repository }}"
49+
exit 1
50+
fi
51+
52+
- run: rustup show
53+
- uses: Swatinem/rust-cache@v2
54+
- uses: taiki-e/install-action@v2
55+
with:
56+
tool: just
57+
58+
- uses: actions/setup-node@v4
59+
with:
60+
node-version: lts/*
61+
62+
- name: Install OpenCode CLI
63+
run: |
64+
npm install -g opencode-ai
65+
opencode --version
66+
67+
- name: Live hook e2e against the real CLI
68+
env:
69+
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
70+
ALLOWLISTER_E2E_MODEL: ${{ vars.OPENCODE_E2E_MODEL }}
71+
run: just test-opencode

‎justfile‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ test-qwen:
122122
test-goose:
123123
@bash scripts/e2e-goose.sh
124124

125+
# Live check against the real `opencode` CLI (needs OpenCode + a provider key + network; opt-in, not in full-check).
126+
test-opencode:
127+
@bash scripts/e2e-opencode.sh
128+
125129
# Enforce line, function, and region coverage across all tests; a miss in any
126130
# one fails the command.
127131
test-cov:

‎scripts/e2e-opencode.sh‎

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Live end-to-end check: drive the real OpenCode CLI against allowlister wired as
4+
# a `tool.execute.before` plugin shim, and assert that a denied command is blocked
5+
# and an allowed command runs.
6+
#
7+
# This is deliberately NOT part of `just full-check` or CI: it needs the
8+
# `opencode` binary, network access, an LLM provider key, and a (cheap) model
9+
# call, so it is neither hermetic nor deterministic the way the `tests/` suite is.
10+
# Run it by hand (or via `just test-opencode`) to verify the integration against a
11+
# real harness after changing the adapter or the plugin shim.
12+
#
13+
# What it proves, using the command's side effect (a sentinel file):
14+
# * deny -> the command never executes (sentinel is absent) EVEN under
15+
# `--dangerously-skip-permissions`: that flag only relaxes OpenCode's
16+
# own permission system; our plugin's `throw` fires regardless, so it
17+
# is the sole gate. This is the core security claim.
18+
# * allow -> the command executes (sentinel is written with its marker): an
19+
# allow verdict emits nothing, so the plugin does not throw.
20+
# The hook's reason string is checked best-effort: OpenCode's event schema is not
21+
# pinned here, so a missing reason is a note, not a failure. The side effects are
22+
# the hard assertions.
23+
#
24+
# `defer` is not asserted: it lets the call proceed, which has no deterministic
25+
# headless outcome. That path is covered hermetically in tests/e2e.
26+
#
27+
# Environment overrides:
28+
# ALLOWLISTER_E2E_MODEL model passed to `opencode run -m` (default: anthropic/claude-haiku-4-5)
29+
# ALLOWLISTER_E2E_KEEP set to 1 to keep the temp sandbox for inspection
30+
# OPENCODE_BIN opencode binary name/path (default: opencode)
31+
# ANTHROPIC_API_KEY / provider key OpenCode uses to run the model (match it
32+
# OPENAI_API_KEY to ALLOWLISTER_E2E_MODEL's provider)
33+
34+
set -euo pipefail
35+
36+
agent_bin="${OPENCODE_BIN:-opencode}"
37+
model="${ALLOWLISTER_E2E_MODEL:-anthropic/claude-haiku-4-5}"
38+
repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
39+
bin="$repo_root/target/release/allowlister"
40+
41+
note() { printf '%s\n' "$*"; }
42+
fail() { printf 'FAIL: %s\n' "$*" >&2; exit 1; }
43+
44+
# A missing `opencode` is a skip, not a failure: this script is opt-in and the
45+
# rest of the project must build and test on machines without the harness.
46+
if ! command -v "$agent_bin" >/dev/null 2>&1; then
47+
note "SKIP: \`$agent_bin\` not found on PATH (install OpenCode — \`npm i -g opencode-ai\` — to run this check)."
48+
exit 0
49+
fi
50+
51+
note "» building release binary"
52+
( cd "$repo_root" && cargo build --release --locked --quiet )
53+
[ -x "$bin" ] || fail "release binary not found at $bin"
54+
55+
# allowlister must be on PATH so the plugin shim's `allowlister hook opencode`
56+
# resolves when OpenCode spawns it. The plugin inherits this process's PATH.
57+
bindir="$repo_root/target/release"
58+
export PATH="$bindir:$PATH"
59+
60+
sandbox="$(mktemp -d)"
61+
cleanup() { [ "${ALLOWLISTER_E2E_KEEP:-0}" = "1" ] || rm -rf "$sandbox"; }
62+
trap cleanup EXIT
63+
64+
proj="$sandbox/project"
65+
mkdir -p "$proj/.git"
66+
67+
# Isolate OpenCode's user state under the sandbox so no ambient ~/.config/opencode
68+
# plugins or data leak in. Auth comes from the provider env key, so no auth.json
69+
# is needed. The project plugin under <proj>/.opencode/plugin is discovered from
70+
# the cwd regardless of HOME.
71+
export HOME="$sandbox/home"
72+
mkdir -p "$HOME"
73+
74+
# Deterministic rules: deny `touch`, allow `mkdir`. The allow case is a
75+
# redirect-free command so a headless model reproduces it verbatim — some models
76+
# silently drop a `> file` redirection, which would make the allow case flaky for
77+
# reasons unrelated to the gate. (Redirection policy is covered hermetically in
78+
# the unit and tests/e2e suites.)
79+
rules="$sandbox/rules.json"
80+
cat > "$rules" <<JSON
81+
{
82+
"rules": [
83+
{ "name": "deny touch", "match": "touch *", "action": "deny" },
84+
{ "name": "allow mkdir", "match": "mkdir *", "action": "allow" }
85+
]
86+
}
87+
JSON
88+
89+
# Set the project up exactly the way a user would: `init` writes the project
90+
# `.allowlister.json` (here from our deterministic rules file) AND writes the
91+
# OpenCode plugin shim into .opencode/plugin/. Exercising init here means the live
92+
# check also covers the plugin-write path end to end.
93+
note "» wiring the project with \`allowlister init --harness opencode\`"
94+
( cd "$proj" && "$bin" init --local --profile "$rules" --harness opencode --hooks --force ) >/dev/null \
95+
|| fail "allowlister init failed to set the project up"
96+
[ -f "$proj/.allowlister.json" ] || fail "init did not write the project config"
97+
grep -q 'allowlister hook opencode' "$proj/.opencode/plugin/allowlister.js" \
98+
|| fail "init did not write the OpenCode plugin shim"
99+
100+
# Run one headless turn steered toward a single exact command.
101+
# * `opencode run` is the non-interactive entry point.
102+
# * --dangerously-skip-permissions relaxes OpenCode's OWN permission system (it
103+
# otherwise auto-rejects every request headless). Our plugin's throw fires
104+
# regardless, so this makes the deny case a true test of the plugin's authority.
105+
# * -m sets the model; --format json keeps output machine-readable.
106+
# * stdin from /dev/null avoids any interactive "waiting for stdin" delay.
107+
run_agent() {
108+
local prompt="$1" stream="$2"
109+
( cd "$proj" && timeout 180 "$agent_bin" run \
110+
--dangerously-skip-permissions \
111+
--format json \
112+
-m "$model" \
113+
"$prompt" \
114+
</dev/null ) >"$stream" 2>"$stream.err" || {
115+
note " ($agent_bin exited non-zero; stderr tail:)"; tail -3 "$stream.err" >&2 || true
116+
}
117+
}
118+
119+
# True if allowlister's own reason text reached the OpenCode transcript. OpenCode
120+
# may render the block differently, so this is a bonus signal, not a gate.
121+
reason_surfaced_in() { grep -aq 'allowlister' "$1" "$1.err" 2>/dev/null; }
122+
123+
# Diagnostic: print a transcript so a CI run reveals exactly how OpenCode handled
124+
# the turn — a plugin throw on deny, or what the model actually ran on allow.
125+
dump_transcript() {
126+
local stream="$1" label="$2"
127+
note " ── $label transcript diagnostic ─────────────────────────"
128+
note " stdout (<=200 lines):"
129+
head -200 "$stream" | sed 's/^/ /'
130+
note " stderr tail:"
131+
tail -12 "$stream.err" 2>/dev/null | sed 's/^/ /'
132+
note " ────────────────────────────────────────────────────────"
133+
}
134+
135+
note "» case 1/2: deny — \`touch\` must be blocked even under --dangerously-skip-permissions"
136+
deny_sentinel="$sandbox/sentinel-deny.txt"
137+
rm -f "$deny_sentinel"
138+
run_agent "Use the shell to run exactly this one command, then stop: touch $deny_sentinel — if it is blocked, stop immediately and do not try any alternative." \
139+
"$sandbox/deny.stream"
140+
[ -e "$deny_sentinel" ] && {
141+
dump_transcript "$sandbox/deny.stream" deny
142+
fail "denied command executed: $deny_sentinel was created (the plugin throw did not hold)"
143+
}
144+
note " ok: command blocked — the sentinel was never created"
145+
if reason_surfaced_in "$sandbox/deny.stream"; then
146+
note " bonus: allowlister's reason text reached the OpenCode transcript"
147+
else
148+
note " note: allowlister's reason was not surfaced in the transcript (OpenCode rendered its own error)"
149+
fi
150+
151+
note "» case 2/2: allow — \`mkdir\` must run"
152+
allow_sentinel="$proj/sentinel-allow.d"
153+
rm -rf "$allow_sentinel"
154+
run_agent "Use the shell to run exactly this one command, then stop: mkdir $allow_sentinel" \
155+
"$sandbox/allow.stream"
156+
[ -d "$allow_sentinel" ] || {
157+
dump_transcript "$sandbox/allow.stream" allow
158+
fail "allowed command did not execute: $allow_sentinel was not created"
159+
}
160+
note " ok: command executed (allow did not trip the plugin)"
161+
162+
note "✓ opencode live e2e passed (deny blocked under --dangerously-skip-permissions, allow ran)"

‎src/cli.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ pub enum Harness {
126126
Qwen,
127127
/// Goose (`PreToolUse` hook).
128128
Goose,
129+
/// OpenCode (`tool.execute.before` plugin shim).
130+
#[value(name = "opencode")]
131+
OpenCode,
129132
/// GitHub Copilot CLI (`preToolUse` hook).
130133
Copilot,
131134
}

‎src/commands/hook.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::cli::Harness;
44
use crate::errors::Result;
5-
use crate::io::harness::{claude_code, codex, copilot, crush, cursor, goose, qwen};
5+
use crate::io::harness::{claude_code, codex, copilot, crush, cursor, goose, opencode, qwen};
66

77
/// Run the hook adapter for the requested harness.
88
pub fn run(harness: Harness) -> Result<i32> {
@@ -13,6 +13,7 @@ pub fn run(harness: Harness) -> Result<i32> {
1313
Harness::Crush => crush::run(),
1414
Harness::Qwen => qwen::run(),
1515
Harness::Goose => goose::run(),
16+
Harness::OpenCode => opencode::run(),
1617
Harness::Copilot => copilot::run(),
1718
}
1819
}

0 commit comments

Comments
 (0)