Skip to content

feat(hooks): detect Cursor 3.x hook registration in ~/.cursor/hooks.json - #1806

Closed
cristianrojas07 wants to merge 1 commit into
rtk-ai:developfrom
cristianrojas07:feat/cursor-3x-hook-detection
Closed

cristianrojas07 wants to merge 1 commit into
rtk-ai:developfrom
cristianrojas07:feat/cursor-3x-hook-detection

Conversation

@cristianrojas07

Copy link
Copy Markdown

Summary

Adds detection of Cursor 3.x hook registration so users who configure rtk via ~/.cursor/hooks.json (instead of ~/.claude/settings.json) no longer see the [rtk] /!\ No hook installed — run rtk init -g warning every time they run rtk git status, rtk gain, etc.

Why

Cursor 3.x introduced its own hook config surface (separate from Claude Code's settings.json):

// ~/.cursor/hooks.json
{
  "version": 1,
  "hooks": {
    "preToolUse": [
      { "command": "rtk hook cursor", "matcher": "Shell" }
    ]
  }
}

Cursor loads this file directly and pipes the hook payload to the configured command. From the user's perspective the hook is "installed" — they ran the equivalent of rtk init -g --agent cursor, the file exists, the hook gets invoked, and rewrites work end-to-end.

But hook_check::status() only inspects ~/.claude/settings.json and the legacy ~/.claude/hooks/rtk-rewrite.sh. So Cursor-only users (no .claude/ directory at all, or .claude/ exists but without rtk hook entries) get the warning on every command — even though their setup is correct.

What this PR changes

  • New helper cursor_3x_hook_registered(home: &Path) -> bool in src/hooks/hook_check.rs. It reads ~/.cursor/hooks.json, parses the JSON, and returns true if any entry under hooks.preToolUse or hooks.beforeShellExecution has a command that:
    • Equals rtk hook cursor (the constant CURSOR_HOOK_COMMAND), OR
    • Contains rtk hook cursor as a substring (covers wrapper invocations like bash -c "rtk hook cursor"), OR
    • Contains rtk (covers thin wrapper scripts that invoke the rtk binary, e.g. powershell.exe -File ~/.cursor/hooks/rtk-cursor-allow.ps1 on Windows).
  • status() calls the new helper first, before the existing Claude path. If the Cursor 3.x hook is registered, HookStatus::Ok is returned and the warning is suppressed regardless of whether ~/.claude/ exists.
  • Graceful degradation: missing file, invalid JSON, or empty hooks object → returns false (falls through to the existing Claude detection).

Why the wrapper substring match

Some Windows + Cursor users currently need a small PowerShell shim around rtk hook cursor because Cursor 3.x silently ignores permission: "ask" and Cursor on Windows ships hook stdin with leading UTF-8 BOMs. That issue is being addressed in #1718 — once it lands, the wrapper goes away and users go back to the native rtk hook cursor command directly. Until then, recognizing the wrapper as an rtk-aware hook avoids a confusing UX where the warning fires even though token savings are happening.

Test plan

  • 7 new unit tests in src/hooks/hook_check.rs::tests:
    • test_cursor_3x_hook_registered_native_command — rtk hook cursor directly
    • test_cursor_3x_hook_registered_wrapper_script — PowerShell wrapper invoking rtk
    • test_cursor_3x_hook_registered_before_shell_execution — alternate hook key
    • test_cursor_3x_hook_not_registered_no_file — missing hooks.json
    • test_cursor_3x_hook_not_registered_unrelated_command — non-rtk command
    • test_cursor_3x_hook_not_registered_invalid_json — malformed JSON
    • test_cursor_3x_hook_not_registered_empty_hooks — hooks: {} empty
  • Local Windows reproduction:
    • With ~/.cursor/hooks.json registering rtk hook cursor and no ~/.claude/ directory → rtk gain runs without the "No hook installed" warning.
    • Same setup but with ~/.claude/ present (no rtk entries) → still no warning (Cursor detection wins).
    • ~/.cursor/hooks.json missing → falls through to existing behavior.

Related issues

Notes for reviewers

  • Scope. The fix is intentionally ~/.cursor/hooks.json-only. Project-level <workspace>/.cursor/hooks.json is also a Cursor 3.x surface but I left it alone here — would prefer to do it as a follow-up if maintainers want, since it requires resolving the workspace cwd at the moment status() is called, which is non-trivial in the existing code.
  • Substring match permissiveness. The cmd.contains("rtk") branch is intentionally permissive to handle wrapper scripts whose path may not contain the literal rtk hook cursor substring (e.g. ~/.cursor/hooks/my-rtk-shim.ps1). False positives here only suppress the warning — they don't change rewrite behavior. If maintainers prefer a stricter match (e.g. only the exact rtk hook cursor substring), happy to tighten it.
  • No breaking change. Existing Claude Code detection path is untouched. Users with ~/.claude/settings.json registering rtk hook claude still see HookStatus::Ok exactly as before.

@CLAassistant

CLAassistant commented May 8, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

…json

Cursor 3.x ships its own hook config format at `~/.cursor/hooks.json` with
`version: 1` and a `hooks` map keyed by event names (`preToolUse`,
`beforeShellExecution`). Users who configure that file correctly with
`rtk hook cursor` (or a wrapper invoking rtk) were still seeing the
"No hook installed" warning because `hook_check::status()` only inspected
`~/.claude/settings.json`.

This adds `cursor_3x_hook_registered()`, called from `status()` before the
existing Claude check. It returns `HookStatus::Ok` when the Cursor hooks
file references the `rtk hook cursor` binary directly or via a thin shell
wrapper (PowerShell/bash/batch). The wrapper case matters on Windows users
running into the BOM/"ask" issue tracked in rtk-ai#1718, where a small shim
returning `permission: "allow"` keeps rewrites working until that PR lands.

Tests cover:
- Native `rtk hook cursor` command in `preToolUse`
- Wrapper script (PowerShell .ps1 invoking rtk)
- `beforeShellExecution` hook key
- Missing file
- Unrelated command (no match)
- Invalid JSON (graceful fail)
- Empty hooks object
@rtk-release-bot

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as stale due to 90 days of inactivity. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. Your contribution is very much appreciated — we're sorry we haven't been able to review it yet.

@rtk-release-bot rtk-release-bot Bot added the stale label Sep 3, 2026
@rtk-release-bot

Copy link
Copy Markdown
Contributor

Closing this pull request after a prolonged period of inactivity. If this is still relevant, please ask for it to be reopened. Thank you!

@rtk-release-bot rtk-release-bot Bot closed this Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants