Skip to content

OpenCode 2.x: judge rewrites against the rules OpenCode resolves (ctx.agent.get) #4521

Open

Description

@KuSh

On OpenCode 2.x, rtk hook opencode judges a rewrite against OpenCode's permission rules, which it rebuilds from config files. Every gap in that rebuild is a place where the rewritten command can lose a prompt or a deny that OpenCode applies to the typed one (#4195). After #4187, rtk reads the 2.x rule forms. Where it still can't see the rules, it answers {}, and the command runs as typed:

  • markdown agents (.opencode/agents/<id>.md, .opencode/agent/<id>.md) whose frontmatter declares permission:/permissions:, which rtk doesn't parse;
  • legacy keys the two generations read differently (measured: 1.18.34 applies bash and b* and ignores shell and sh*; 2.0.26 applies bash (renamed to shell), shell and sh* and ignores b*).

Two sources it doesn't see at all:

OpenCode already resolves them

ctx.agent.get({ agentID }) is documented under "API → Agent" in https://opencode.ai/v2/docs/build/plugins. It returns AgentGetOutput = { location, data: AgentInfo } with a required permissions: PermissionRuleset, where PermissionRule = { action: string; resource: string; effect: "allow" | "deny" | "ask" } (AgentGetOutput, AgentInfo).

On 2.0.22 and 2.0.26 the list comes back in OpenCode's evaluation order:

  1. OpenCode's defaults;
  2. global root rules;
  3. the project's legacy permission map, with bash already normalised to shell;
  4. the project's permissions list, with actions as written (sh* included);
  5. agent rules from every file, markdown frontmatter included.

SessionInfo.permissions is optional (null in a plain opencode run). When set, it is evaluated after the agent's rules.

Proposal

The 2.x plugin forwards OpenCode's own data on stdin to rtk hook opencode, the way the native hosts send their hook payload to rtk hook <host>:

  • the execute.before event (tool, sessionID, agent, input);
  • ctx.agent.get({ agentID: event.agent }).data.permissions;
  • the session's permissions when present.

rtk turns that into its ordered rule list and judges the typed and rewritten command against it. It matches action as a wildcard against shell, and no longer reads config files for 2.x. The {} fallbacks above then go away on 2.x. 1.x has no agent field and no such API, so it keeps the argv form and reads config files the 1.x way.

Known limit either way: saved approvals ("Allow always") are project-scoped allow rules outside both lists. Missing them can only make rtk stricter: a pre-approved command can prompt once rewritten. They can never silence an ask or lift a deny.

Rows a fix has to pass on 2.x

Each row must match OpenCode alone:

  • every case above;
  • a global agent ask against a project allow;
  • an agent with no rules is still rewritten;
  • a deny in any layer is still enforced.

Activity

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

    @rtk-wshm-sync-bot

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 78%

    Summary

    On OpenCode 2.x, rtk's config-file-based permission rebuild misses session rules, markdown agent frontmatter, XDG global config, and legacy key semantics, causing {} fallback that lets rewrites bypass OpenCode's actual permission rules; proposes using ctx.agent.get/session data via stdin instead.

    📁 Relevant files
    • src/hook/opencode.rs
    • src/permissions/opencode.rs

    Suggested Actions

    1. Coordinate with OpenCode 1.x: rtk misses global rules under $XDG_CONFIG_HOME/opencode and in config.json #4520 (XDG config) and OpenCode permission rules: rtk's config loader diverges from OpenCode's config resolution #4422 (config loader divergence) since fix overlaps
    2. Review closed PR fix(hooks): judge OpenCode against its own permission rules #4349 (judge OpenCode against its own permission rules) for prior approach/rejection reasons
    3. Confirm via [OpenCode] RTK rewrite breaks existing Bash permission allowlists by adding rtk before permission evaluation #4195 whether ctx.agent.get is stable across OpenCode 2.0.22/2.0.26 before committing to stdin-forwarding d…
    4. Scope out markdown agent frontmatter parsing and legacy bash/shell key handling as separate sub-fixes

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

  2. BramDriesen commented on Oct 19, 2026

    @BramDriesen

    +1 for this! Also upgraded to Opencode V2 today and noticed that the RTK plugin no longer works. The "investigate" option of opencode itself also lead me to the same conclusion as this issue.

  3. djbclark commented on Oct 25, 2026

    @djbclark

    Data point for the ctx.agent.get direction: on OpenCode 2.x, ctx.permission.list() is not a source of effective rules. It returns pending permission requests ({ id, permission, patterns }, no effect), normally an empty list. A plugin that reads an empty list as "no deny/ask rules" rewrites past rules that are configured. We hit this in a local rewrite shim and now decide from config files only, which still misses OPENCODE_CONFIG*, managed config and agent-level rules. So ctx.agent.get({ agentID }).permissions looks like the right source. Anyone adding a host-side check should not use permission.list().

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions