Skip to content

Cursor hook silently drops all rewrites — rtk rewrite exit code 3 treated as failure #2372

Closed

Description

@euclidesgc

Summary

The Cursor integration (rtk init --global --agent cursor) does not rewrite shell commands. Both rtk hook cursor and hooks/cursor/rtk-rewrite.sh treat rtk rewrite exit code 3 as a failure and return {} (pass-through), so commands run unchanged and RTK filtering never applies.

This appears to be the same root cause as #2200 (OpenClaw plugin), but it also affects the official Cursor hook shipped with RTK.

Environment

  • RTK: 0.42.3
  • OS: macOS (darwin 25.4.0)
  • Agent: Cursor IDE (Agent mode, preToolUse hook)
  • Hook config: ~/.cursor/hooks.json with matcher: "Shell" and command: "rtk hook cursor"

Steps to reproduce

  1. Install RTK and run:
    rtk init --global --agent cursor
  2. Restart Cursor.
  3. Ask the agent to run:
    git status
  4. Observe raw git status output instead of RTK-compact output.

Expected behavior

git status should be transparently rewritten to rtk git status, and the agent should see compact output like:

* branch-name
clean — nothing to commit

Actual behavior

The command runs as plain git status. rtk gain --history does not record the command. Simulating the hook also returns {}:

printf '%s' '{"tool_input":{"command":"git status"}}' | rtk hook cursor
# → {}

Root cause

rtk rewrite returns exit code 3 when it has a rewrite suggestion, but the hook treats any non-zero exit as failure:

# hooks/cursor/rtk-rewrite.sh (line 47)
REWRITTEN=$(rtk rewrite "$CMD" 2>/dev/null) || { echo '{}'; exit 0; }

Even though stdout contains the correct rewrite (rtk git status), the || branch runs and discards it.

Verified manually:

$ rtk rewrite "git status"
rtk git status
$ echo $?
3

The script comment says "rtk rewrite exits 1 when there's no rewrite", but successful rewrites also use a non-zero exit code (3), which breaks the hook logic.

Workaround (confirmed working)

Replace the hook command with a wrapper that captures stdout regardless of exit code:

REWRITTEN=$(rtk rewrite "$CMD" 2>/dev/null || true)

if [ -z "$REWRITTEN" ] || [ "$CMD" = "$REWRITTEN" ]; then
  echo '{}'
  exit 0
fi

jq -n --arg cmd "$REWRITTEN" '{
  "permission": "allow",
  "updated_input": { "command": $cmd }
}'

After applying this workaround, git status is correctly rewritten and RTK-compact output is returned.

Suggested fix

  1. Fix rtk rewrite to exit 0 on successful rewrite (breaking change for callers that rely on exit 3), or
  2. Update all hook integrations (hooks/cursor/rtk-rewrite.sh, rtk hook cursor, OpenClaw, etc.) to read stdout on exit code 3 instead of treating it as an error.

Related: #2200, PR #2202

Activity

  1. pszymkowiak commented on Jun 11, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 97%

    Summary

    Cursor hook discards all rewrites because rtk rewrite exit code 3 is treated as failure; fix by capturing stdout regardless of exit code in rtk-rewrite.sh

    This looks like a simple fix that could be auto-resolved. Repo maintainers can use /wshm fix to attempt it.

    📁 Relevant files
    • hooks/cursor/rtk-rewrite.sh

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

  2. TuttiFruty commented on Jun 24, 2026

    @TuttiFruty

    I fixed this by adding a .cursor/cli-config.json with :

    {
      "permissions": {
        "allow": [
          "Shell(*)"
        ]
      }
    }
    
    

    Root cause is that if you run Cursor in a Unsandboxed you will never have this config file that rtk check to know if it can wrap the command or not. Hence the issue.

    Could be the same for your setup.

  3. noamgal-c commented on Jun 24, 2026

    @noamgal-c

    another possible workaround is to create the following script for example at ~/.cursor/hooks/rtk-cursor.sh:

    #!/bin/bash
    set -euo pipefail
    
    input=$(cat)
    cmd=$(printf '%s' "$input" | python3 -c "import sys, json; data = json.load(sys.stdin); print(data.get('tool_input', {}).get('command', ''))")
    
    if [ -z "$cmd" ]; then
      echo '{}'
      exit 0
    fi
    
    rewritten=$(rtk rewrite "$cmd" 2>/dev/null || true)
    
    if [ -z "$rewritten" ] || [ "$cmd" = "$rewritten" ]; then
      echo '{}'
      exit 0
    fi
    
    python3 -c 'import json, sys; print(json.dumps({"permission": "allow", "updated_input": {"command": sys.argv[1]}}))' "$rewritten"

    and to change the cursor user hooks (~/.curosr/hooks.json) to "command": "./hooks/rtk-cursor.sh",

  4. added 2 commits that reference this issue on Jun 25, 2026
    4f87f1e
    65d52ce
  5. added a commit that references this issue on Jun 29, 2026
    ff0b9ac
  6. added a commit that references this issue on Jul 20, 2026
    83629eb
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