Skip to content

OpenCode plugin: tool.execute.before hook never fires — auto-rewrite broken #1706

Closed

Description

@rocordov

Environment

  • RTK: v0.38.0 (Homebrew, macOS arm64)
  • OpenCode: latest (with @opencode-ai/plugin v1.14.30)
  • OS: macOS Darwin

Symptom

After rtk init -g --opencode, the plugin file is created at ~/.config/opencode/plugins/rtk.ts. The plugin loads successfully (verified), but Bash commands are never auto-rewritten. Running git status produces raw output instead of rtk git status.

Root Cause Found

The plugin's tool.execute.before hook is never dispatched by OpenCode. I added file-based debug logging to both tool.execute.before and tool.execute.after hooks in the plugin:

return {
  "tool.execute.before": async (input, output) => {
    log(`BEFORE_ENTER tool=${String(input?.tool)}`)
    // ... rewrite logic
  },
  "tool.execute.after": async (input, output) => {
    log(`AFTER tool=${String(input?.tool)}`)
  },
}

Result: The plugin loads (10+ PLUGIN_LOADED log files created), but zero BEFORE_ENTER or AFTER log files are ever created. The hooks are returned by the plugin initialization function but OpenCode never calls them.

Key Finding

The engram plugin (~/.config/opencode/plugins/engram.ts) uses tool.execute.after in the same project, with the same import (@opencode-ai/plugin), and its hooks do fire correctly. This suggests the issue is specific to how the RTK plugin is structured, not a general OpenCode plugin dispatch problem.

Related Issues

Manual Workaround

Running rtk explicitly works fine:

rtk git status     # works, produces compact output
rtk ls src/        # works
rtk rewrite "git status"  # correctly returns "rtk git status"

Activity

  1. rocordov commented on May 4, 2026

    @rocordov
    Author

    Resolution: Stale Processes, Not a Plugin Bug

    The OpenCode plugin works correctly. The issue was stale opencode web processes that prevented plugin hooks from dispatching to newly installed plugins.

    Root cause: Multiple opencode web processes were running (PIDs: 278, 99331, 99307). The plugin file was created by rtk init, but the hooks were never dispatched because the old processes cached the plugin state from before RTK was installed.

    Fix: Kill all opencode processes and restart cleanly:

    pkill -f "opencode web" && pkill -f "opencode-cli"
    # Then restart opencode

    Verification: After clean restart, the plugin auto-rewrites all bash commands:

    • git status → rtk git status (50% token savings)
    • ls -la → rtk ls -la . (78% savings)
    • date → rtk fallback: date (0% savings, passthrough — proves intercept)

    The plugin code itself (hooks/opencode/rtk.ts) is correct — no modifications needed.

  2. rocordov commented on May 4, 2026

    @rocordov
    Author

    Closing: plugin works correctly. Stale processes were the cause, not a code bug.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions