rtk icon indicating copy to clipboard operation
rtk copied to clipboard

OpenCode permission rules: rtk's config loader diverges from OpenCode's config resolution

Open KuSh opened this issue 2 months ago โ€ข 4 comments

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.json and opencode.jsonc when 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_CONFIG is added on top of the global config. rtk reads it instead of the global config.
  • OPENCODE_CONFIG_CONTENT and 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"} gives ask in OpenCode and allow in rtk for git 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 {}), while ls -la is 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.json and $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).

KuSh avatar Oct 03 '26 11:10 KuSh

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

  1. Rewrite config loader to mirror OpenCode's merge order: global config.json/opencode.json(c), OPENCODE_CONFIG, project fโ€ฆ
  2. Switch JSON parsing to a JSONC-tolerant parser (comments/trailing commas) to match OpenCode 1.18.34 behavior
  3. Implement key-by-key deep merge instead of rule-list append to fix last-match-wins divergence
  4. 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.

rtk-wshm-sync-bot avatar Oct 03 '26 11:10 rtk-wshm-sync-bot

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.

KuSh avatar Oct 05 '26 16:10 KuSh

Hi @KuSh , If this is not finished or backlog I can take this up If any priority issues please assign me , Looking forward Thankss!!

TechWizard9999 avatar Oct 30 '26 09:10 TechWizard9999

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" }
] }
  1. opencode run --agent general "Run exactly: git push": the push runs (fatal: No configured push destination), the deny is lost.
  2. Same with --standalone (server started in the project): Permission denied: shell.
  3. Deny changed to rtk git push *: denied, so OpenCode judges the rewritten command.
  4. 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.

Tarek-9 avatar Nov 11 '26 08:11 Tarek-9