Skip to content

fix(hooks): treat OpenCode 1.x default verdict as allow when rule sources are readable (#4638) - #4926

Open
feliperm17 wants to merge 3 commits into
rtk-ai:developfrom
feliperm17:fix/4638-opencode-1x-on-4458
Open

feliperm17 wants to merge 3 commits into
rtk-ai:developfrom
feliperm17:fix/4638-opencode-1x-on-4458

Conversation

@feliperm17

Copy link
Copy Markdown

Note: Stacked on top of #4458 (9a271f26b9). Only the top commit (ec472e52f8) belongs to this change; once #4458 merges into develop, this branch will be rebased cleanly onto develop.

Closes #4638

Summary

  • Maps PermissionVerdict::Default to PermissionVerdict::Allow in HostRules::verdict for OpenCode (Rules::Ordered), matching OpenCode 1.x's built-in * -> allow rule so rtk-prefixed allow policies (e.g., "rtk *": "allow" or selective bare allows) rewrite kubectl, oc, npm, gcloud, git, and aws instead of returning {}.
  • Fails closed to Rules::Unjudgeable ({}) in load_judgeable_opencode_rules whenever a selected config source (OPENCODE_CONFIG, global config, nearest project config, or inspected agent markdown file) exists on disk but fails to be read or parsed as JSON, preventing an unreadable file from falling back to the built-in allow.
  • Adds unit and subprocess integration tests in src/hooks/* and tests/hook_decision_protocol_test.rs covering the 16-case (before, after) transition matrix, #4195 (* -> deny), explicit ask/deny rules, compound commands, unattestable constructs, and unreadable/malformed config sources.

Test plan

  • cargo fmt --all && cargo clippy --all-targets && cargo test (5,302 passed, 0 failed, 8 ignored; zero clippy warnings or fmt diffs)
  • Manual testing: rtk <command> output inspected (rtk hook opencode -- kubectl get pods, oc get pods, npm run build, gcloud compute instances list, git status, and aws sts get-caller-identity verified across default, "rtk *": "allow", and selective "rtk *": "allow" + "git *": "allow" + "aws *": "allow" configs)

Important: All PRs must target the develop branch (not master).

See CONTRIBUTING.md for details.

KuSh and others added 3 commits October 29, 2026 03:28
`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`, whose single walk of the
settings also says whether a fish script may be wrapped, so the fish wrap
no longer reads them a second time.
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, so it
leaves git as typed in a managed worktree just as the hook does.
`rtk hook check --agent claude` gives that answer too, from the directory
it runs in.

`rtk hook check --agent opencode` now gives the plugin's answer: 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.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CLAassistant

CLAassistant commented Nov 1, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Maps OpenCode 1.x's default PermissionVerdict::Default to Allow in HostRules::verdict so rtk's allow-rewrites of kubectl/oc/npm/gcloud/git/aws actually apply, while failing closed to Unjudgeable when a selected config source exists but can't be read or parsed. Also refactors the Claude Code discover coverage path to use the shared HostRules abstraction instead of a bespoke PermissionRules struct, and adds extensive unit/integration tests.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #4638


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

@feliperm17

Copy link
Copy Markdown
Author

Hi @KuSh! I'd like to share an update on #4638 and ask for your guidance before making further changes to this PR.

During independent validation, I found that the Default -> Allow fix depends on accurately reconstructing OpenCode 1.x's effective permission policy.

The current implementation inherits some limitations from the historical #4458 base, particularly:

  • Permission precedence between global and project configurations.
  • OPENCODE_CONFIG being treated as a replacement rather than an additional source.
  • Multiple project configuration files and ancestor configurations not being fully considered.
  • Permission shorthands not being normalized consistently across sources.

In certain valid configurations, these limitations can cause the hook to return a rewrite even when OpenCode would evaluate the original command as ask or deny and the rewritten command as allow.

I've developed a local experimental solution combining the configuration discovery work from #4932 with additional permission-handling improvements. The previously identified 16 permission-mismatch cases now pass independent validation, along with 5,377 Rust tests and additional regression scenarios.

However, the experimental patch has grown considerably, and I haven't pushed it or ported it to the latest #4458 implementation. There are also limitations involving remote/managed configurations and session-resolved permissions that aren't fully addressed.

Before making this PR significantly larger, I'd appreciate your opinion on the best approach:

Would you prefer that we first reconcile #4458 and #4932, then keep #4926 focused on the remaining Default -> Allow behavior? Or would a separate, smaller PR for the configuration-loading improvements be more appropriate?

I'm happy to adapt the implementation to whichever approach fits the project's architecture and review process best.

Thanks for your time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working hooks opencode

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rtk hook opencode returns empty {} for kubectl/oc/npm despite working rewrites in rtk rewrite (1.0.1)

3 participants