Skip to content

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

Open

Description

@sungod808

**Environment:**rtk 1.0.1, OpenCode plugin (rtk init -g --opencode), Debian-based Kali Linux.

**Summary:**rtk hook opencode -- returns {} (no rewrite) for kubectl, oc, and npm commands, even though rtk rewrite correctly produces a rewrite for the same input. git and aws are unaffected — both rewrite correctly through hook opencode.

Repro::
`$ rtk hook opencode -- kubectl get pods
{}
$ rtk rewrite kubectl get pods; echo "exit: $?"
rtk kubectl get pods
exit: 3

$ rtk hook opencode -- npm run build
{}
$ rtk rewrite npm run build; echo "exit: $?"
rtk npm run build
exit: 0

$ rtk hook opencode -- git status
{"command":"rtk git status"}
$ rtk hook opencode -- aws sts get-caller-identity
{"command":"rtk aws sts get-caller-identity"}`

Impact:: OpenCode’s permission system never sees the rtk-prefixed form for these three command types, so any rtk -only allow rules are silently inert — only bare-command permission rules take effect, regardless of what’s configured.

Config: : none of kubectl, oc, npm were in exclude_commands at time of testing (confirmed via cat ~/.config/rtk/config.toml).

Untested:: gcloud, az, oci weren’t checked against hook opencode at time of filing — gcloud is still in exclude_commands on this box, az/oci config wasn’t present to test. Scope of affected commands beyond the five checked (git, aws, kubectl, oc, npm) is unconfirmed.

Activity

  1. rtk-wshm-sync-bot commented on Oct 18, 2026

    @rtk-wshm-sync-bot

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 85%

    Summary

    OpenCode plugin's hook path fails to rewrite kubectl/oc/npm (returns {}) while rtk rewrite works correctly for the same commands, silently defeating rtk-prefixed permission rules.

    📁 Relevant files
    • src/hook/opencode.rs
    • src/rewrite/mod.rs
    • src/commands/kubectl.rs
    • src/commands/npm.rs

    Suggested Actions

    1. Diff hook opencode vs rewrite code paths to find why kubectl/oc/npm diverge from git/aws
    2. Ask reporter to test gcloud/az/oci and share full config.toml to narrow affected-command scope
    3. Check if kubectl/oc/npm share a command-dispatch branch that opencode's hook handler skips
    4. Cross-reference with OpenCode: rtk hook check --agent opencode judges differently from the plugin #4518 (hook check judging differently from plugin) for related opencode hook logic gaps

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

  2. feliperm17 commented on Oct 28, 2026

    @feliperm17

    Hi! I'd like to investigate and work on this issue. I will reproduce the behavior across kubectl, oc, npm (and check other cloud/CLI wrappers) in rtk hook opencode vs rtk rewrite, inspect the dispatch/permission logic, and add regression tests for the fix. Could you assign this to me?

  3. feliperm17 commented on Oct 28, 2026

    @feliperm17

    Hi @sungod808, @KuSh! I investigated and reproduced the exact symptom on develop (7a73af1b).

    1. Root Cause & Reproduction

    There is no dispatch difference between kubectl/oc/npm and git/aws in src/discover/rules.rs — with no permission config files present, rtk hook opencode -- <cmd> rewrites all five commands ({"command":"rtk ..."}).

    The behavior reported here comes from the interaction of two separate permission evaluators:

    1. Why rtk rewrite returned exit: 3 for kubectl vs exit: 0 for npm:
      rtk rewrite (src/hooks/rewrite_cmd.rs:91) evaluates Claude Code rules (Host::Claude, reading .claude/settings.json), where 0 is AllowRewrite and 3 is AskRewrite (which includes PermissionVerdict::Default when no Claude rule matches). Having Bash(npm:*) allowed in .claude/settings.json yields exit: 0 for npm and exit: 3 for kubectl, independent of OpenCode.

    2. Why rtk hook opencode returned {} for kubectl/oc/npm while rewriting git/aws:
      In src/hooks/hook_cmd.rs:1313–1331, opencode_rewrite_answer evaluates before = permissions_opencode::check_command(&raw_cmd) and after = permissions_opencode::check_command(&rewritten), returning {} whenever before != after.
      When opencode.json contains rtk-prefixed allow rules (e.g. "rtk *": "allow" or "rtk kubectl *": "allow") alongside bare "git *": "allow" and "aws *": "allow" (without bare rules for kubectl/oc/npm):

      • git status $\rightarrow$ before = Allow, after = Allow (before == after $\rightarrow$ returns {"command":"rtk git status"}).
      • kubectl get pods $\rightarrow$ before = Default (no rule matched the bare command), after = Allow (matched the rtk allow rule). Because Default != Allow, hook_cmd.rs:1330 suppresses the rewrite and outputs {}.

    Minimal opencode.json + .claude/settings.json that reproduces all outputs from the issue description verbatim:

    • opencode.json: {"permission":{"bash":{"rtk *":"allow","git *":"allow","aws *":"allow"}}}
    • .claude/settings.json: {"permissions":{"allow":["Bash(npm:*)"]}}

    2. Proposed Fix / Question

    As noted in #4422, OpenCode's built-in default action when no rule matches is allow, whereas permissions_opencode represents "no matching rule" as PermissionVerdict::Default (which is unequal to PermissionVerdict::Allow in before != after).

    Would you like a PR that allows the rewrite in opencode_rewrite_answer when (before, after) transitions between Default and Allow (specifically when before == Default && after == Allow, or by aligning Default with OpenCode's built-in Allow when no ask/deny rule matched), while keeping all existing Ask/Deny and #4195 protections intact? I'd be happy to take this on if you can assign it to me!

  4. KuSh commented on Oct 29, 2026

    @KuSh
    Collaborator

    Thanks @feliperm17, the reproduction matches what I see. The rtk rewrite exit codes come from Claude Code's rules, so they don't depend on OpenCode. The {} comes from Default versus Allow in opencode_answer, which I noted in #4422: OpenCode 1.18.34 puts * → allow ahead of the user's rules, and rtk reads "no rule matched" as a verdict of its own.

    Two things bear on where a fix should go:

    If you'd like to work on this, #4521 is the direction I'd point to, built on #4458 once it lands. It needs a 2.x install, a plugin change that forwards the payload on stdin, and a forwarded-rules source in the loader. The 1.x case (Default treated as Allow when every rule source is readable) could be a small follow-up on top of #4458. Either one is fine by me. I'm offering the pointers, not asking anyone to take them on.

    @sungod808, could you share your opencode.json (the permission.bash block) and opencode --version? That tells us whether you're on the 1.x or the 2.x case.

  5. feliperm17 commented on Oct 29, 2026

    @feliperm17

    Thanks for the pointers, @KuSh! That makes total sense.

    I'd like to take on the 1.x follow-up (Default treated as Allow when every rule source is readable) built on top of #4458. I'll fetch #4458 locally (HostRules / decide_for_host) to prepare and test the change so it's ready to open cleanly once #4458 lands on develop.

  6. sungod808 commented on Oct 30, 2026

    @sungod808
    Author

    3. Hi! I'd like to investigate and work on this issue. I will reproduce the behavior across kubectl, oc, npm (and check other cloud/CLI wrappers) in rtk hook opencode vs rtk rewrite, inspect the dispatch/permission logic, and add regression tests for the fix. Could you assign this to me?

    Thanks for the deep dive — this matches our reproduction exactly. We hit the same symptom (kubectl/oc/npm/gcloud silently returning {} from rtk hook opencode while git/aws rewrite fine) across five separate boxes, all on RTK 1.0.1, with the same shape of config you reproduced: "rtk *": "allow" plus bare allow rules for git and aws but none for kubectl/oc/npm/gcloud. Good to have the actual Default-vs-Allow mechanism confirmed — we’d been treating the rtk-prefixed permission twins as functionally inert while this was open.

    The Default→Allow fix sounds right to us — treating “no ask/deny rule matched” the same as explicit Allow when deciding whether to emit the rewrite seems like the correct behavior given OpenCode’s own default-allow semantics, and keeping Ask/Deny and the #4195 protections untouched is the important constraint. Please go ahead with the PR if you’re able to take it on — happy to test it against our setup once it’s up.

  7. feliperm17 commented on Nov 1, 2026

    @feliperm17

    Thanks @sungod808, @KuSh! I've opened #4926 stacked on top of #4458 implementing the 1.x follow-up (Default mapped to Allow in HostRules::verdict when all selected OpenCode rule sources are readable, failing closed to Unjudgeable/{} if a selected config or agent file cannot be read or parsed).

    @sungod808 feel free to pull the branch from #4926 if you'd like to test it against your 5 boxes!

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions