Repository navigation
feat(#996): opencode fidelity — fail-closed hook shim, subagent defs, MCP translation, error classes, CI smoke - #1013
Merged
admarble merged 15 commits intoSep 8, 2026
Conversation
Entire-Checkpoint: 4cc25fe1fb78
Entire-Checkpoint: 2bf71a362299
…on (AC-3, AC-4) Entire-Checkpoint: 69e0723c0252
…ht (AC-2, AC-5, AC-7) Entire-Checkpoint: 0f1cbefe2003
Entire-Checkpoint: ab944a20e9ae
Entire-Checkpoint: 83bfb8e18479
Entire-Checkpoint: f15442ec6bb7
…xport only functions) Entire-Checkpoint: f215c75ab1d5
…phase (AC-7) Entire-Checkpoint: ae8f9b34b78b
…m (AC-6) Entire-Checkpoint: ccce2d76f027
Entire-Checkpoint: 40782a1bae31
Entire-Checkpoint: 8df9e39aa1c5
…ssertion) Both failures were in gates this PR added, not in the shipped behaviour. The AC-6 secret scan ran `git grep` over the whole repo, so it matched a docs placeholder and — self-defeatingly — the `mcp-config` tests that assert a key is NOT serialized. It failed loudest exactly when the protection worked. Scoped to the generated `.opencode/` tree plus the workflow directory, with bracket classes in the pattern so the scan cannot match its own source line. Verified both ways locally: no false positive on a clean tree, and a planted `sk-ant-`/`sk-or-v1-` key is still caught. `862 AC-8 injects no /tmp or $TMPDIR permission rule` began failing on Linux CI after 36addff pointed the fixture at a shim-provisioned mkdtemp cwd. `--dir <cwd>` then legitimately carried `/tmp/...`, which the blanket `args.join(" ")` assertion read as an injected rule. It passed on macOS only because tmpdir() there is /var/folders. The assertion now drops that one value, so a /tmp rule injected anywhere else in the argv still fails it. Entire-Checkpoint: b6d61bf23ae1
6 tasks
AC-3 offers two ways to settle sub-agent fan-out: a verified dogfood table on #997, or documenting the fallback in the driver's doc comment. #997 has no such table, and the driver's doc comment had zero matches for fan-out/parallel/sequential/subagent — so neither satisfier was taken. QA caught it. Documents what actually happens rather than inventing a fallback. There is no driver-conditional branch anywhere in the driver or the exec skill, so claiming an implemented "sequential fallback" would have been false. The honest disposition: provisioning works (a hermetic `opencode debug agent` resolves mode/steps/permission on all three defs), fan-out itself is unverified, and the de facto behaviour is sequential inline execution arrived at by absence rather than design. Also records a boundary found while probing this: `opencode debug agent <name> --tool <id>` executes a tool WITHOUT invoking `tool.execute.before`, so the hook shim never runs on that path. Verified on 1.18.27 — the plugin's load sentinel appears but an instrumented hook-entry probe does not. It is a debug harness rather than the phase path, but anything run through it is unguarded, and it is the reason OQ-4 cannot be settled for free that way. The two gate tests are scoped to the leading block comment (everything before the first import) per CLAUDE.md, so an unrelated inline comment elsewhere in the file cannot satisfy them. Entire-Checkpoint: 8773b696557e
…code-fidelity-hook-shim-fail-closed-subag # Conflicts: # CHANGELOG.md
admarble
deleted the
feature/996-feat-opencode-fidelity-hook-shim-fail-closed-subag
branch
September 8, 2026 15:54
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Gives an opencode phase the same hook guards a Claude Code phase gets, and makes every unprovable path fail closed. Closes #996 (#862 P1).
templates/opencode/plugins/) translates opencode'stool.execute.beforepair into the stdin envelope.claude/hooks/pre-tool.shexpects, spawns the real 1,576-line hook, and throws on exit 2.sequant init --agent opencodewrites three subagent defs, the plugin, and the MCP entry under opencode's flatmcpkey.--pure; the driver fails closed when the shim is absent from the phase worktree or never announced itself at runtime.The
security-reviewphase ran before implementation and its findings shaped the design — see "Security review findings addressed" below.Pre-PR AC Verification
{tool,args}→ hook stdin, blocks on exit 2, fails closed on malformed argsopencode-hooks.test.ts— 33 passeddoctor.test.ts -t "opencode plugin"— 5 passedinit.opencode.integration.test.ts— 11 passed; 3 defs (OQ-1). Fan-out clause now satisfied via the driver doc comment, gated byopencode.test.ts -t "996 AC-3"(2 tests, both mutation-verified).mcpkey,type: local, arraycommandmcp-config.test.ts -t "opencode mcp"— 3 passedopencode.test.ts -t "862 P1 errors"— 3 passedgrep -c '1.18.27'→ 6 ≥ 1 ✅. The $0 structural job is green ona8d275a2(run) and gates every PR — but it runsopencode debug …, notopencode run --format json. The job matching that clause ismodel-backed, which isworkflow_dispatch-only and gated onOPENCODE_SMOKE_API_KEY(not configured) — it has never run. See "AC-6, precisely" below.hooks-not-installedopencode.test.ts -t "862 P1 hooks preflight"— 8 passed.opencode/init.tsprints the hint after provisioningVerify output
Open-question resolutions (as recommended by
/spec)sequant-explorerwas deleted in fix(spec): the explorer fan-out prescription has a 0% execution rate over 169 runs — delete it or make it real #927; a fourth would have no Claude-side counterpart. The AC'sgrep -c 'mode: subagent'gate reads 3.serve.test.tsdoesn't exist. Implemented insrc/lib/mcp-config.ts, gated atmcp-config.test.ts -t "opencode mcp".--max-coston 1.18.27. Two-job design: $0 structural job gates the PR; the model-backed job is secret-gated withtimeout-minutes: 5.model-backedjob prompts agit push --forceand assertsHOOK_BLOCKED. Until that job runs green, the end-to-end guarantee rests on opencode's documented behaviour, not on evidence.post-tool.sh/tool.execute.after. Out of scope here, as recommended; worth a follow-up.Verified against real opencode 1.18.27 ($0, no model calls)
tools→permissionderivation works as the spec predicted: writing thetoolsmap alone produces the deny rules.Corrected after QA re-ran this hermetically. An earlier revision of this table listed a
writedeny that opencode 1.18.27 does not emit — it derivespermissiondenies forbashandedit, notwrite. The security outcome is unchanged, because the resolvedtoolsmap does carrywrite: falseand that is the actual enablement mechanism; but the deny list above is now what opencode really returns.patch: falseis dropped entirely from the resolved map (unknown tool id) — the shim covers it by omittingpatchfromPASSTHROUGH_TOOLS, so it throws as an unknown tool.Security review findings addressed
The pre-exec
security-reviewreturned ISSUES_FOUND with two P0s. Both were gaps in the ACs, not implementation slips, and both are now closed and mutation-verified:Plugin load failure is silent. Verified: a plugin throwing at load gives exit 0, empty stderr, resolved tool map. File-presence checks (AC-2, AC-7 as written) cannot see it. → The shim announces itself on stderr and the driver fails the phase when that line is absent (
hooks-not-active).This was not hypothetical — the first version of this shim hit it. It exported constants, and opencode rejects any scanned plugin file that exports a non-function (
"Plugin export is not a function"), logging at ERROR and otherwise swallowing it. The shim is now split into a function-only entry pluslib/sequant-hooks-core.ts, since the plugin scan is non-recursive.filePathvsfile_path. opencode'sedit/writeargs are camelCase;pre-tool.sh:1495,1539reads snake_case. Forwardingargsverbatim leavesFILE_PATHempty, skips the worktree-boundary and file-lock guards, and falls through toexit 0. → The shim translates key-by-key; M1 below proves the suite catches a regression.Also addressed:
bash.workdirrouted into the envelopecwd(#3); relativefilePathresolved against opencode's own base (#4);CLAUDE_HOOKS_DISABLED/CLAUDE_HOOKS_FILE_LOCKINGstripped from the hook env (#5); MCP built from the secret-free overload (#6);patch/apply_patchexplicitly denied and barred from passthrough (#7);jqabsence fails closed (#8).Mutation verification
Per CLAUDE.md §Testing. Each mutation applied to a committed tree, verified, restored.
Mutation-verified: AC-1 — forwarded opencode
argsverbatim instead of mappingfilePath→file_path;maps edit's filePath onto the file_path key pre-tool.sh reads+blocks an edit whose file_path escapes the worktreefailed; restored.Mutation-verified: AC-1 — removed the
STRIPPED_ENV_VARSdeletion loop frombuildHookEnv;strips the guard-disabling vars before spawning the hook+still blocks a force push when CLAUDE_HOOKS_DISABLED is set ambientlyfailed; restored.Mutation-verified: AC-1 — made an unrecognised tool pass through instead of throwing;
blocks an unrecognised tool rather than passing it throughfailed; restored.Mutation-verified: AC-1 — deleted the
SHIM_LOADED_SENTINELstderr write;writes the sentinel to stderr when the plugin loadsfailed; restored.Mutation-verified: AC-1 — made
decideFromHookallow undocumented exit codes;blocks on the undocumented exit 3/127/255/nullfailed (4 cases); restored.Mutation-verified: AC-2 — disabled doctor's shim-presence check;
996 AC-2 fails with guidance when the opencode plugin is absentfailed; restored.Mutation-verified: AC-2 — disabled doctor's
--purecheck;996 AC-2 fails when --pure is configured, which disables pluginsfailed; restored.Mutation-verified: AC-3 — emitted
maxSteps:instead ofsteps:;translates maxTurns into stepsfailed; restored.Mutation-verified: AC-3 — stopped denying unlisted mutating tools;
denies unlisted mutating tools rather than leaving them unsetfailed; restored.Mutation-verified: AC-4 — spread the base MCP config so
envcarried through;never emits an env key, so no provider secret can reach a committed filefailed; restored.Mutation-verified: AC-5 — set
OpencodeDriver.usesSdkMcp = true;declares that it does not use the SDK's MCP plumbingfailed; restored.Mutation-verified: AC-5 — set
ClaudeCodeDriver.usesSdkMcp = false; 5phase-executor.test.tsMCP-fallback tests failed, proving the new gate does not silently disable the fallback for claude-code; restored.Mutation-verified: AC-7 — disabled the
findShimInpreflight;fails closed with hooks-not-installed when the shim is absent+does not spawn opencode at all when the shim is missingfailed; restored.Mutation-verified: AC-7 — disabled the
requireShimHandshakecheck;fails a run whose shim never announced itself, even on a clean exitfailed; restored.Mutation-verified: AC-3 — deleted the
## Sub-agent fan-outsection from the driver header comment; both996 AC-3gate tests failed; restored.Mutation-verified: AC-3 — rewrote the honest prose into a claim that the driver "transparently switches to a sequential fallback";
996 AC-3 does not claim an implemented driver-conditional fallbackfailed while the other gate test still passed; restored.Mutation-verified: AC-6 — pushed
--permission bash:/tmp/**=allowinto the opencode argv;862 AC-8 injects no /tmp or $TMPDIR permission rulefailed; restored.One mutation that did not fail, reported honestly
Swapping
getSequantMcpConfig()→getSequantMcpConfig({clientType: "claude-desktop"})did not break the AC-4 secret test. The builder whitelistscommand/argsrather than spreading the base object, soenvcannot survive regardless ofclientType. That is defense in depth, but it means the test gates "the entry never carriesenv", not "the clientType-free overload was called". M10b (spreading the base) is the mutation that does fail, and is the one recorded above.Checks
npm run buildnpm run lintsrc/lib/workflow/drivers/src/lib/workflow/phase-executor*src/commands/init*,doctor,mcp-configPer the runner's launch discipline, the full suite was not run inside the phase — CI is the gate for it.
Test coverage
templates/opencode/plugins/**opencode-hooks.test.ts(33)src/lib/workflow/drivers/opencode.tsopencode.test.ts(40)src/commands/doctor.tsdoctor.test.tssrc/commands/init.tsinit.opencode.integration.test.tssrc/lib/mcp-config.tsmcp-config.test.tssrc/lib/workflow/phase-executor.tsphase-executor.test.ts(fallback gate mutation-verified)src/lib/workflow/drivers/agent-driver.tsCI repairs (commit
a8d275a2)The first CI run turned two of this PR's own gates red. Neither was a defect in
the shipped behaviour; both were defects in the gates.
1. The AC-6 secret scan matched itself. It ran
git grepacross the wholerepo, so it hit a docs placeholder and — self-defeatingly — the
mcp-configtests that assert a key is not serialized. The gate failed loudest exactly
when the protection was working. It is now scoped to the generated
.opencode/tree plus.github/workflows, and the pattern uses bracketclasses so the scan cannot match its own source line. Verified locally in both
directions: no false positive on a clean tree, and a planted
sk-ant-/sk-or-v1-key is still caught.2.
862 AC-8 injects no /tmp or $TMPDIR permission rulebroke on Linux.Commit
36addff7repointed the driver fixture at a shim-provisionedmkdtempcwd.
--dir <cwd>then legitimately carried/tmp/..., and the blanketargs.join(" ")assertion read that as an injected rule. It stayed green onmacOS only because
tmpdir()there is/var/folders, which is why the failurefirst appeared in CI. The assertion now drops that one value, so a
/tmpruleinjected anywhere else in the argv still fails it — mutation-verified above.
Reproduced locally by running the suite under
TMPDIR=/tmp/....AC-6, precisely (owner decision needed)
QA caught me overstating this, so here is the exact state.
AC-6 has three clauses:
grep -c '1.18.27' .github/workflows/*.yml≥ 1opencode run --format jsonon a trivial prompt with a budgetmodel-backed,timeout-minutes: 5) but has never executedstructuraljob, which never invokesopencode runmodel-backedisif: github.event_name == 'workflow_dispatch'and gated onsecrets.OPENCODE_SMOKE_API_KEY, which is not configured on this repo. Dispatching it today would hit its own "Skip when unconfigured" branch and skip again.I also tried to settle this for $0, without a model or a key, via
opencode debug agent <name> --tool bash --params '{"command":"git push --force origin main"}'. The force push was not blocked — but that is a false alarm, and the reason matters: instrumenting the fixture's plugin copy with a hook-entry probe showedtool.execute.beforeis never invoked on thedebug --toolpath. The plugin's load sentinel appears; the hook does not fire. So that path bypasses the shim and cannot serve as evidence either way. It is now recorded in the driver's doc comment as a real (if narrow) unguarded surface.This is a decision for you, not something
/loopshould decide:OPENCODE_SMOKE_API_KEYand dispatch the workflow once — costs a few cents, closes AC-6 and OQ-4 properly.I have not done either: (a) spends money and adds a repo secret, and (b) edits the acceptance criteria — both are owner calls. AC-6 is marked⚠️ Partial until one of them happens.
Known gaps
a8d275a2, but it never invokesopencode run --format json; the job that does isworkflow_dispatch-only and gated on an unconfigured secret, so it has never executed. Owner decision needed — see "AC-6, precisely" below.templates/is excluded fromtsconfig.jsonandnpm run lint, so the shim ships untypechecked; vitest catches syntax errors only. Accepted gap, flagged as the spec asked.tsconfig.jsonexcludes**/*.test.ts, so mockAgentDriverliterals were not typechecked against the newusesSdkMcpfield. Covered behaviourally by the M14 mutation instead.Test plan
pre-tool.shCLAUDE_HOOKS_DISABLEDdoes not disable the guardsmode/steps/permissionopencode debug configCloses #996
🤖 Generated with Claude Code