OpenCode permission rules: rtk's config loader diverges from OpenCode's config resolution
src/hooks/permissions_opencode.rs (from #4349) rebuilds OpenCode's bash rules to decide whether a rewrite would change OpenCode's verdict. Where it reads different rules from OpenCode, the plugin falls back to develop's behaviour: it rewrites, and OpenCode judges the rtk โฆ form. Checked against OpenCode 1.18.34's config loader, these divergences remain:
- Both file names in a directory. OpenCode merges
opencode.jsonandopencode.jsoncwhen both exist, in the global directory and in each project directory. rtk reads only the first one it can parse (read_first_config). - Project files. OpenCode merges every
opencode.json(c)from the worktree root down to the cwd. rtk reads only the nearest one, and keeps walking up past the worktree root. OPENCODE_CONFIGis added on top of the global config. rtk reads it instead of the global config.OPENCODE_CONFIG_CONTENTand the managed config are not read.- Merge order. OpenCode deep-merges the files, so a key repeated in a later file keeps its first position, while rtk appends the later file's rules. With last-match-wins the two can disagree: global
{"git *":"deny","git status":"ask"}plus project{"git *":"allow"}givesaskin OpenCode andallowin rtk forgit status. - Built-in defaults. OpenCode puts
{"permission":"*","pattern":"*","action":"allow"}ahead of the user's rules, so a command no rule matches runs. rtk treats "no rule" as its own verdict, which differs from Allow. With a config of allow rules and no catch-all,{"bash":{"git status":"allow","cargo test":"allow"}}, rtk skips the rewrite for exactly those two commands (rtk hook opencode "git status"gives{}), whilels -lais still rewritten. This costs savings, never a verdict. Modelling it needs the per-agent defaults as well, since agents can ship their own.
Tracked in their own issues:
- JSONC (a comment or trailing comma drops every rule of the file): #4524, PR #4528.
- Global
config.jsonand$XDG_CONFIG_HOME/opencode: #4520. - Project
.opencode/directories: #4855, PR #4856.
On OpenCode 2.x, #4521 replaces the file reading with the rules OpenCode resolves itself (ctx.agent.get), which removes every row above there. On 1.x they stay.
A fix reads the same sources in the same order as OpenCode and merges objects key by key, with a test per source. Related: #4282 (an unparseable settings file drops its rules instead of failing closed).
wshm ยท Automated triage by AI
๐ Automated Triage
| ๐ Category | bug |
| ๐ Priority | high |
| ๐ฏ Confidence | 88% |
Summary
rtk's OpenCode config loader misses JSONC parsing, multi-file merge order, OPENCODE_CONFIG/CONTENT, and worktree-root limits, causing permission verdicts to diverge from OpenCode's actual resolution.
๐ Relevant files
src/hooks/permissions_opencode.rs
Suggested Actions
- Rewrite config loader to mirror OpenCode's merge order: global config.json/opencode.json(c), OPENCODE_CONFIG, project fโฆ
- Switch JSON parsing to a JSONC-tolerant parser (comments/trailing commas) to match OpenCode 1.18.34 behavior
- Implement key-by-key deep merge instead of rule-list append to fix last-match-wins divergence
- Add a regression test per config source; cross-reference #4282 for fail-closed behavior on unparseable files
Triaged automatically by wshm ยท This is an automated analysis, not a human review.
One more piece of OpenCode's resolution that rtk does not model: its built-in defaults. OpenCode 1.18.34 puts {"permission":"*","pattern":"*","action":"allow"} ahead of the user's rules, so a command no rule matches runs. rtk treats "no rule" as its own verdict, which differs from Allow. With #4349, a config of allow rules and no catch-all, {"bash":{"git status":"allow","cargo test":"allow"}}, makes rtk skip the rewrite for exactly those two commands: rtk hook opencode "git status" gives {}, while ls -la is still rewritten. OpenCode would have run rtk git status under its default. This costs savings, never a verdict. Modelling it needs the per-agent defaults as well, since agents can ship their own.
Hi @KuSh , If this is not finished or backlog I can take this up If any priority issues please assign me , Looking forward Thankss!!
One more divergence, this time on the plugin side.
Setup: OpenCode 2.2.6 on Windows, rtk 1.1.0, sessions started with opencode run, which goes through OpenCode's background service.
The plugin calls rtk hook opencode --agent <name> <command> via execFile without a cwd, so rtk reads the project config from its own working directory. With the background service that is the directory the service started in, not the session's project, and the project opencode.json never reaches the verdict check.
Repro in a throwaway repo without a remote, project opencode.json:
{ "permissions": [
{ "action": "shell", "resource": "*", "effect": "allow" },
{ "action": "shell", "resource": "git push *", "effect": "deny" }
] }
opencode run --agent general "Run exactly: git push": the push runs (fatal: No configured push destination), the deny is lost.- Same with
--standalone(server started in the project):Permission denied: shell. - Deny changed to
rtk git push *: denied, so OpenCode judges the rewritten command. rtk hook opencode --agent general "git push"run in the project directory answers{}.
Passing the session's directory as cwd to execFile, or judging against the rules OpenCode resolves as proposed in #4521, would close it. Until then, deny rules with a leading * (*git *push*) also match the rewritten form.