Skip to content

fix(hooks): load a host's permission rules once, OpenCode included - #4458

Open
KuSh wants to merge 5 commits into
developfrom
fix/host-rules-loaded-once
Open

KuSh wants to merge 5 commits into
developfrom
fix/host-rules-loaded-once

Conversation

@KuSh

@KuSh KuSh commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator

Follow-up to #4349. Refs #4651.

Summary

  • permissions::HostRules holds a host's rules, read from disk once: deny/ask/allow lists, or OpenCode's ordered last-match-wins list, or a marker that rtk cannot read OpenCode's rules the way OpenCode does (an agent markdown file declaring rules, a legacy key only one generation applies, a config rtk cannot read or parse). It also holds the configured exclude_commands and transparent prefixes, compiled once, so the verdict, the hook's rewrite and rtk discover's estimate use one copy instead of each reading the config (the hooks recompiled the exclusions on every decision). HostRules::load(host, agent) replaces load_rules_for and the OpenCode branch in check_command_for_agent. Whether a host judges the command its hook hands back is stated per host in permissions::judges_final_command.
  • Every rtk hook <agent> processor and rtk hook check decide through decision::decide_for_host: the verdict of the typed command, the shared decision, and, on a host that judges the final command (OpenCode), no rewrite whose verdict differs, and none at all when the rules are unreadable. rtk rewrite, which the hermes, omp, pi and OpenClaw hooks run, answers through the same path as rtk hook check for those agents instead of computing its verdict and decision itself; only the hooks drop a rewrite that changes nothing.
  • Claude Code's own hook loads its rules with HostRules::claude_hook(), in place of check_claude_hook_command, which repeated check_command_for_agent's load: the walk of the settings that reads the rules also says whether a fish script may be wrapped. One load serves the whole rtk hook claude run: the engine pipeline's swap gate decides against it too, including whether an ask rule is what makes a command ask. In a managed worktree it swaps no command whose replacement contains git: Claude Code refuses an rtk command there whenever its text contains git, even inside another word, and the replacement hands the typed command to sh (on develop it swapped git status too).
  • Every host reads its settings files through one reader. No file, a path under a regular file, or a BOM and whitespace hold nothing; anything else rtk cannot read or parse is an error, and what that costs stays per host (Claude Code and Gemini ask, Cursor and Droid deny). rtk hook cursor and rtk hook droid no longer step aside for every command when ~/.cursor or ~/.factory is a regular file or the settings file holds only a BOM. Gemini and OpenCode used to read a file rtk cannot parse (a commented one, which both accept) as absent, so a rewrite or another file's allow could silence the ask it holds: Gemini now asks for every command, and rtk hook opencode answers {}; like every hook, it still counts a read of a tee file the rules do not deny.
  • Gemini's project .gemini/settings.json is read under folder trust the way Gemini CLI decides it. Gemini now turns folder trust on unless the global settings turn it off, and rtk assumed it was off, so it auto-approved a command a project file allowed in a folder Gemini does not trust. rtk now reads the project file under GEMINI_CLI_TRUST_WORKSPACE=true or when the global file sets security.folderTrust.enabled to false, together with the global file's confirmations, which Gemini still applies there, and the global file alone under GEMINI_CLI_TRUST_WORKSPACE=false or GEMINI_RESTRICTED_MODE=true. Otherwise the user may have trusted the folder in Gemini's dialog, which rtk does not read, so it asks wherever either file asks and allows only what both allow.
  • rtk hook check now gives the hook's answer: for --agent opencode the plugin's when it runs without an agent (it printed rtk git status where rtk hook opencode answers {}, e.g. git status under {"*": "deny", "git status": "allow"}), and for --agent claude from the directory it runs in.
  • HookDecision::Rewrite { command, approval } replaces AllowRewrite and AskRewrite(_, AskOwner). Approval is Allow (an allow rule matched, RTK allows), Ask (RTK asks itself: the fish wrap) or Host (left to whoever judges next); every host renders the three as before.
  • rtk discover judges transcript commands through the same HostRules (Claude Code's, still read once per run) instead of its own deny/ask/allow struct.

When the skip applies, hook check prints the generic No rewrite for: <cmd>; saying that the rewrite would change the verdict could be a follow-up.

Test plan

  • cargo fmt --all && cargo clippy --all-targets && cargo test --all, with and without default features (5841 and 5623 passed); clippy and the unit tests also pass on each commit
  • The fish wrap tests under fish 4.9.1, with and without the engine feature (19 and 18 passed); CI covers fish 3.7.0
  • Differential against develop (762708c): the 9 hook processors (Claude with no payload cwd, a project one and a managed worktree), rtk rewrite with and without RTK_REWRITE_HOST=openclaw, rtk hook opencode and rtk hook check for all 18 agents from the same three directories, over 19 settings layouts × 12 commands (15542 runs per side). rtk rewrite is byte-identical. The other differences are the ones above: Cursor and Droid rewriting beside a regular file or a BOM-only file, OpenCode answering {} under a config rtk cannot read, hook check --agent opencode under the [OpenCode] RTK rewrite breaks existing Bash permission allowlists by adding rtk before permission evaluation #4195 policy, a "*": "ask" policy and a legacy key, hook check --agent claude and the pipeline swap leaving git alone in a managed worktree, Gemini's decisions are unchanged where its file cannot be read (it already asks when no rule matches), and parse warnings are no longer printed for files that hold nothing. hook check and the hook agree on all 636 pairs compared (develop: 75 disagree).
  • Swap gate against develop, over a rule that matches every command: 39 commands with and without a filter × 8 settings files × 3 configs × 3 directories (2808 runs per side). Under a settings file rtk cannot parse the hook warns half as often, and in a managed worktree it swaps no command containing git; nothing else differs.
  • New tests: hook check vs the plugin over 7 policies × 6 commands in both directions, each host reading its own rule file and no other, every host reading a settings file the same way, a commented OpenCode config, OpenCode counting a tee read under rules rtk cannot judge but not under a deny, a trusted Gemini project file rtk cannot parse, Gemini's project file under each folder-trust setting, the per-host judges_final_command table, the rewrite looking past the prefixes the verdict was judged with and leaving the rules' own excluded commands alone, a declined pipeline swap reading the settings no more often than no pipeline, the swap gate declining any command containing git in a managed worktree, hook check --agent claude there, and test homes that never read settings above TMPDIR
  • Claude Code 2.1.312 in a -w worktree session against a mock API (7 runs): plain git status runs, and so do the hook's own rtk cargo build && git status and rtk read .gitignore; rtk git status is refused, and so is a swapped rtk <task> -- sh -c '…' whose script contains git (git status && cargo build, xargs -n1 git add, find -exec git, echo digit, echo GIT), as is rtk git-digest -- sh -c 'cargo build'; a swapped command without it runs
  • Gemini CLI 0.70.0 against a mock API: with no folder-trust setting a project allow does not apply (headless runs refuse to start in the folder); a folder trusted in Gemini's dialog applies the project file, whose bare run_shell_command confirmation beats a global allow; GEMINI_CLI_TRUST_WORKSPACE=false and GEMINI_RESTRICTED_MODE=true ignore the project file and keep the global allow, even with folder trust off; in a trusted folder a global bare run_shell_command confirmation still applies when the project file allows the command; a confirmation naming a command (run_shell_command(git push)) has no effect in Gemini, so rtk asking on it is stricter
  • Mutation checks: reverting the shared load, the swap gate reading the settings again or ignoring the verdict, the ask rules or the prefixes they hold, the unreadable-rules early return, the legacy-key mapping, the shared reader's handling of a path under a regular file or a BOM, the unreadable OpenCode or Gemini config, Gemini's unknown folder trust, each input to Gemini's folder trust (either environment variable, the default, an undecided folder read as trusted or as untrusted), the allows both Gemini files must share or the asks either one holds, the global confirmations a trusted folder keeps, the swap gate's worktree check (removed, narrowed to a word, or reading the typed command), or any reader of the transparent prefixes or exclude_commands going back to the config each fails a test

🤖 Generated with Claude Code

@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Introduces HostRules::load to read a host's permission rules (deny/ask/allow lists or OpenCode's ordered list) from disk exactly once, replacing load_rules_for and the ad-hoc OpenCode branch. Adds decide_for_host, shared by every rtk hook <agent> processor and rtk hook check, which also drops a rewrite on hosts (OpenCode) that judge the final command if the rewrite's verdict differs from the typed command's verdict, fixing a prior discrepancy where rtk hook check --agent opencode di…

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Analyzed automatically by wshm · This is an automated analysis, not a human review.

@KuSh
KuSh force-pushed the fix/host-rules-loaded-once branch 2 times, most recently from c257fd4 to dad7edc Compare October 16, 2026 20:41
@KuSh
KuSh force-pushed the fix/host-rules-loaded-once branch from dad7edc to fb49507 Compare October 21, 2026 00:20
@KuSh
KuSh force-pushed the fix/host-rules-loaded-once branch from fb49507 to 9a271f2 Compare October 29, 2026 06:52
@KuSh
KuSh force-pushed the fix/host-rules-loaded-once branch from 9a271f2 to e14f503 Compare November 1, 2026 10:45
@KuSh
KuSh force-pushed the fix/host-rules-loaded-once branch from e14f503 to 1583424 Compare November 2, 2026 11:46
@KuSh
KuSh added this pull request to stack #4952 November 2, 2026 12:33
KuSh and others added 5 commits November 23, 2026 01:33
`HostRules` holds a host's rules, either deny/ask/allow lists (with the
configured transparent prefixes) or OpenCode's ordered last-match-wins list,
read from disk once. Every `rtk hook <agent>` processor and `rtk hook check`
decide through `decide_for_host`: the verdict of the typed command, the
shared decision, and, on a host that judges the command its hook hands back
(OpenCode), no rewrite whose verdict differs, and none at all when rtk cannot
read the rules the way OpenCode does. This replaces `load_rules_for` and the
OpenCode branch in `check_command_for_agent`. Claude Code's own hook
loads them with `HostRules::claude_hook`, in place of
`check_claude_hook_command`, which repeated `check_command_for_agent`'s
load: the walk of the settings that reads the rules also says whether a
fish script may be wrapped.
One load serves the whole `rtk hook claude` run: the engine pipeline's swap
gate decides against it too, from the payload's working directory. In a
managed worktree it swaps no command whose replacement contains `git`:
Claude Code refuses an `rtk` command there whenever its text contains
`git`, and the replacement hands the typed command to `sh`, so git reaches
Claude Code as typed, as the hook leaves it.
`rtk hook check --agent claude` gives that answer too, from the directory
it runs in.

`rtk rewrite`, which the hermes, omp, pi and OpenClaw hooks run, answers
through the same path as `rtk hook check` for those agents
(`AgentPath::answer`) instead of computing its verdict and decision itself;
only `hook check` then drops a rewrite that changes nothing.
`check_command` and `check_command_for` had no other caller and are gone.

The configured `exclude_commands` and transparent prefixes are read and
compiled once with the rules and held by `HostRules`, so the verdict and
the rewrite of one decision use the same settings, instead of the rewrite
reading the config again and recompiling the exclusions on every call.

`rtk hook check --agent opencode` now gives the answer of the plugin run
without an agent: it reported a rewrite the plugin skips, e.g. `git
status` under `{"*": "deny", "git status": "allow"}`, or any command under
a config with a legacy key only one OpenCode generation applies.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`rtk discover` kept its own deny/ask/allow struct for Claude Code's rules.
It now holds the same `HostRules` the hooks load, still read once per run,
and `load_permission_rules` is private to `permissions` again. Its
rewrite estimate uses the `exclude_commands` and transparent prefixes
those rules were read beside, rather than a second copy of them, so
`config::hook_rewrite_params` loses its last caller and is gone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude Code's loader and `read_json`, which Cursor, Gemini and Droid read
through, each decided what a settings file holds, and only the first knew
that a path under a regular file or a file holding only a BOM holds no
rule. OpenCode read its configs with a third copy. All four now read
through `read_json`: no file, a path under a regular file, or a BOM and
whitespace hold nothing; anything else RTK cannot read or parse is an
error. What an error costs stays each host's own: Claude Code and Gemini
ask for every command, Cursor and Droid deny it.

Gemini and OpenCode read on as if such a file were not there, so a file
RTK cannot parse (a commented one, which both hosts accept) let a rewrite
or another file's allow silence the ask it holds. Gemini now asks for
every command, and OpenCode's rules are unjudgeable: `rtk hook opencode`
answers `{}` and OpenCode judges the command as typed. Like the other
hosts, it now warns on stderr about a config it cannot read or parse.

`rtk hook cursor` and `rtk hook droid` no longer step aside for every
command when `~/.cursor` or `~/.factory` is a regular file, or when the
settings file holds only a BOM.

Refs #4651

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`HookDecision::AllowRewrite` and `AskRewrite(_, AskOwner)` described one
thing, a rewrite, under two variants and a second enum. They become
`HookDecision::Rewrite { command, approval }`, where `Approval` is `Allow`
(an allow rule matched, RTK allows), `Ask` (RTK asks itself: the fish wrap)
or `Host` (left to whoever judges next). Each host renders the three as
before: only `rtk hook claude` tells `Ask` from `Host`, `ApprovalOwner`
relaxes only a `Host` rewrite under a default verdict, and
`suppress_identity` drops only an `Allow` or `Host` rewrite that changed
nothing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ini does

Gemini CLI now turns folder trust on unless the global settings set
`security.folderTrust.enabled` to false, and applies a project's
`.gemini/settings.json` only in a folder it trusts. RTK assumed trust was
off by default: with no global file, or one that leaves the setting out,
it trusted every folder, so it auto-approved a command the project file
allowed where Gemini asks first. It also only read
`GEMINI_CLI_TRUST_WORKSPACE=true`, while `GEMINI_CLI_TRUST_WORKSPACE=false`
and `GEMINI_RESTRICTED_MODE=true` distrust the folder even with folder
trust off.

RTK now reads the project file when the environment trusts the folder
or the global file turns folder trust off, together with the global
file's confirmations, which Gemini still applies there; and the global
file alone when the environment distrusts it. Otherwise the user may have
trusted the folder in Gemini's dialog, which RTK does not read, so it
allows no more than either file would: it asks wherever one file asks,
and allows only what both allow.

Measured with Gemini CLI 0.70.0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants