Skip to content

fix(hooks): make Cursor preToolUse rewrites work and stay visible - #1718

Merged
pszymkowiak merged 1 commit into
rtk-ai:developfrom
kamilkaczmareksolutions:fix/cursor-hook-permission-ask
May 10, 2026
Merged

pszymkowiak merged 1 commit into
rtk-ai:developfrom
kamilkaczmareksolutions:fix/cursor-hook-permission-ask

Conversation

@kamilkaczmareksolutions

@kamilkaczmareksolutions kamilkaczmareksolutions commented May 5, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR

On Windows + Cursor, RTK's preToolUse hook silently no-ops on every Shell command — and the panel shows Output: {}, which looks like the hook fired correctly. It didn't. This PR fixes the actual rewrite (root cause: Cursor sends UTF-8 BOM in stdin and serde_json rejects it) and the two visibility gaps that were hiding the breakage.

What users saw vs. what was actually happening

Before this PR After this PR
preToolUse panel Output: {} for every Shell call full JSON: continue, permission, updated_input
Command Cursor actually ran original (e.g. raw git status) rewritten (rtk git status)
rtk gain --history no entries from Cursor rewrites with 30–94% token reduction
User signal "looks like the hook fires but does nothing" "I can see exactly what RTK rewrote, output got smaller"

This was particularly confusing because Output: {} is also the legitimate response when RTK intentionally skips a command (e.g. htop, kill, already-rtk-prefixed). So users had no signal that the hook was actually broken — it looked indistinguishable from "RTK chose not to rewrite this one".

Before — rtk hook cursor (raw config from README)

before — Output: {} for git status, hook command is rtk hook cursor

Investigation — wrapped rtk hook cursor with a Python tracer

The tracer logged stdin byte-for-byte and forwarded to RTK unchanged. Same Output: {}, but the log proved what Cursor was actually sending.

before — same Output: {} via tracer wrapper

STDIN_RAW first bytes (hex): ef bb bf ef bb bf 7b 22 63 6f 6e 76 65 72 73 ...
                              ^^^^^^^^ ^^^^^^^^ {  "  c  o  n  v  e  r  s
                              BOM #1   BOM #2

STDIN_PARSE_ERROR: JSONDecodeError('Unexpected UTF-8 BOM (decode using utf-8-sig)')
RTK_STDOUT_RAW: {}

Cursor on Windows ships hook payloads with two leading UTF-8 BOMs. serde_json::from_str refuses to parse them, so run_cursor falls into the "no command" branch and returns {} — silently, on every single hook call.

After — same binary path, same Cursor, just patched RTK

after — full JSON output: continue: true, permission: allow, updated_input.command shows rtk injected into a compound command

The panel above is for a real compound command Cursor sent (export PATH=... && cd ... && rtk cargo build --release 2>&1 | tail -8). RTK correctly identifies the rewritable segment (cargo build), preserves the rest, returns continue: true so the panel renders, and Cursor honors permission: "allow" so the rewrite actually executes.

What this PR changes

Three issues, fixed together because each one alone produces the same user-visible symptom:

  1. permission: "ask" is silently ignored by Cursor. Cursor preToolUse only enforces allow/deny. Returning ask with updated_input could drop the rewrite, so the original command would run unmodified. Always return allow for rewritten commands. Deny rules still take precedence and are evaluated first.

  2. Panel collapses to Output: {} without continue: true. Every other Cursor hook (afterShellExecution, beforeSubmitPrompt, stop, ...) returns { "continue": true, ... } and renders correctly. The flat { permission, updated_input } payload was technically valid but the UI didn't surface it. Mirror the working shape so the rewrite is visible to the user.

  3. Cursor stdin on Windows is BOM-prefixed. Strip up to N leading UTF-8 BOMs in run_cursor before parsing. This is the actual root cause of the long-standing "RTK silently does nothing in Cursor" reports — the rewrite path was never reached. Fixes Release v0.2.1 #1 and rtk git log --oneline fails with argument parsing error #2 are correctness/UX gaps exposed once parsing succeeds.

Test plan

  • cargo test test_cursor_ -- --nocapture — 13 passed (2 new: test_cursor_compound_rewrite_includes_continue, test_cursor_strips_leading_utf8_bom)
  • cargo build --release clean
  • Local Windows smoke against the patched binary:
    • git status (no BOM) → {"continue":true,"permission":"allow","updated_input":{"command":"rtk git status"}}
    • git status with EF BB BF EF BB BF prepended (matches what Cursor actually sends) → same output
    • Compound cd "/tmp/proj" && git status → rewrites only the right segment
    • Skipped commands (htop, kill 12345, already-rtk-prefixed) → still {} (intended no-op)
  • End-to-end in Cursor 3.2.16 on Windows: panel renders the rewrite payload (see screenshot above), and rtk gain --history confirms subsequent shell calls hit the rewrite path with 30–94% token reduction (e.g. rtk git push -93%, rtk git commit -94%, rtk git status -73%). Before this PR: zero entries from Cursor.

Related issues

Notes for reviewers

  • Scope. BOM-strip is intentionally Cursor-only (in run_cursor). I did not observe the same BOM behaviour on Claude/Copilot/Gemini stdin during this debugging, so I left those paths alone. Happy to widen to a shared helper across all hook handlers if maintainers prefer.
  • Cursor-side bug. Sending double UTF-8 BOM on stdin is its own bug worth reporting upstream to Cursor — but RTK should be robust to it regardless, and shipping the fix here unblocks every existing Windows + Cursor user immediately rather than waiting on a Cursor release.
  • Why bundle the three changes. They share the same user-visible symptom (Output: {} + commands not rewritten). Fixing only Add pnpm and Vitest support for modern JavaScript stacks #3 would make the rewrite work but the panel would still mislead users. Fixing only Release v0.2.1 #1+rtk git log --oneline fails with argument parsing error #2 (which is what an earlier version of this PR did) doesn't help at all on Windows because parsing still fails before the new logic runs. The three together are the minimal change that actually closes the loop.
  • Screenshots are hosted on a parking-lot branch in my fork (pr-1718-assets) so this PR's diff stays purely the code change.

@CLAassistant

CLAassistant commented May 5, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@kamilkaczmareksolutions
kamilkaczmareksolutions changed the base branch from master to develop May 5, 2026 09:08
@kamilkaczmareksolutions
kamilkaczmareksolutions force-pushed the fix/cursor-hook-permission-ask branch 2 times, most recently from 7efb293 to b2f7028 Compare May 5, 2026 09:38
@kamilkaczmareksolutions kamilkaczmareksolutions changed the title fix(hooks): force allow for Cursor rewrites fix(hooks): make Cursor preToolUse rewrites visible May 5, 2026
@kamilkaczmareksolutions
kamilkaczmareksolutions force-pushed the fix/cursor-hook-permission-ask branch from b2f7028 to 82d0bc4 Compare May 5, 2026 09:58
@kamilkaczmareksolutions kamilkaczmareksolutions changed the title fix(hooks): make Cursor preToolUse rewrites visible fix(hooks): make Cursor preToolUse rewrites work and stay visible May 5, 2026
@mhstudioos

Copy link
Copy Markdown

🙏

cristianrojas07 added a commit to cristianrojas07/rtk that referenced this pull request May 8, 2026
On Windows + Cursor, RTK's `preToolUse` hook silently no-ops on every Shell
command — and the panel shows `Output: {}`, which looks like the hook fired
correctly. It didn't. This rebases the upstream investigation in rtk-ai#1718 onto
current `develop` so users on `rtk 0.39.x` aren't blocked while that branch
catches up.

Three issues, fixed together because each one alone produces the same
user-visible symptom:

1) `permission: "ask"` is silently ignored by Cursor.
   Cursor preToolUse only enforces `allow`/`deny`. Returning `ask` with
   `updated_input` drops the rewrite, so the original command runs unmodified.
   Always return `allow` for rewritten commands. Deny rules still take
   precedence and are evaluated first.

2) Panel collapses to `Output: {}` without `continue: true`.
   Every other Cursor hook (`afterShellExecution`, `beforeSubmitPrompt`,
   `stop`, ...) returns `{ "continue": true, ... }` and renders correctly.
   The flat `{ permission, updated_input }` payload was technically valid
   but the UI didn't surface it. Mirror the working shape so the rewrite is
   visible to the user.

3) Cursor stdin on Windows is BOM-prefixed.
   Strip leading UTF-8 BOMs in `run_cursor` before parsing. This is the
   actual root cause of the long-standing "RTK silently does nothing in
   Cursor" reports — the rewrite path was never reached. Fixes rtk-ai#1 and rtk-ai#2
   are correctness/UX gaps exposed once parsing succeeds. The strip loop
   covers the observed double-BOM case (`EF BB BF EF BB BF`) and is
   defensive against future N-BOM variants.

Test plan:
- Rewrite test now asserts `permission: "allow"` + `continue: true`
- New `test_cursor_strips_single_utf8_bom` (one leading BOM)
- New `test_cursor_strips_double_utf8_bom` (two leading BOMs, real-world case)
- New `test_strip_utf8_boms_helper` (unit test on the helper)
- Existing tests for passthrough/heredoc/already-rtk unchanged

Refs rtk-ai#1718 (original PR by @kamilkaczmareksolutions, rebased here)
Refs rtk-ai#1272 (the long-standing "rewrites dropped" issue)
Refs rtk-ai#1463 (Cursor on Linux — same external symptom; worth retesting)
cristianrojas07 added a commit to cristianrojas07/rtk that referenced this pull request May 8, 2026
…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
Three related issues prevented RTK from working with the Cursor
preToolUse hook on Windows. Each one alone produced the same
user-visible symptom (`Output: {}` and the original command running
unmodified), so they're fixed together:

1. Cursor preToolUse only enforces allow/deny — `permission: "ask"` is
   accepted by the schema but ignored at runtime, so `updated_input`
   rewrites could silently be dropped. Always return `allow` for
   rewritten commands (deny rules still take precedence and are
   evaluated before this point).

2. Cursor's preToolUse panel renders the JSON returned by the hook.
   Without a `continue: true` field the panel collapses to
   `Output: {}` even when the rewrite ran successfully, which makes
   the hook look broken from the user's perspective. Every other
   Cursor hook (`afterShellExecution`, `beforeSubmitPrompt`, `stop`,
   ...) returns `continue: true`; mirror that shape here so the panel
   surfaces the actual `permission` and `updated_input` payload.

3. Cursor on Windows prepends one or two UTF-8 BOMs (`EF BB BF`,
   sometimes doubled) to the JSON it pipes into the hook process.
   serde_json refuses to parse BOM-prefixed input, so `run_cursor`
   bailed out into the "no command" branch and returned `{}` for
   every invocation. Strip leading BOMs before parsing. This was the
   root cause of the long-standing "RTK silently no-ops in Cursor"
   reports — the rewrite path was never reached.

Tests cover the flat allow output, `continue: true` on both single
and compound (`cd ... && git status`) rewrites, the BOM-strip on
double-BOM payloads matching what Cursor actually sends, and assert
that the legacy hookSpecificOutput envelope is not emitted.

Verified end to end on Windows by tracing real Cursor stdin payloads:
the hook now returns `{"continue":true,"permission":"allow",
"updated_input":{"command":"rtk git status"}}` for `git status` and
the panel renders it instead of `Output: {}`.
@kamilkaczmareksolutions
kamilkaczmareksolutions force-pushed the fix/cursor-hook-permission-ask branch from 82d0bc4 to f00977a Compare May 9, 2026 09:03
@kamilkaczmareksolutions

Copy link
Copy Markdown
Contributor Author

Update — rebased onto current develop + improved test split

Following @cristianrojas07's parallel rebase in #1807 (thanks!), this PR is now also rebased onto current develop HEAD. No merge conflicts — the transparent_prefixes work that landed in hooks::hook_cmd since this PR was opened touches get_rewritten() and run_gemini() but not the Cursor path, so the rebase was clean.

I also took the opportunity to adopt the test structure from #1807, which is genuinely better than my original:

  • Split the single test_cursor_strips_leading_utf8_bom into separate test_cursor_strips_single_utf8_bom and test_cursor_strips_double_utf8_bom — clearer failure isolation.
  • Added test_strip_leading_bom_helper as a direct unit test on the helper (covers 0 / 1 / 2 / 3 leading BOMs + mid-string BOM preservation), so future refactors can't regress the loop semantics without an obvious failure signal.

Independent verification: @cristianrojas07 confirmed the same fix works end-to-end on a notably newer stack (Cursor 3.3.27 + rtk 0.39.0 vs my original Cursor 3.2.16 + 0.34.3). The BOM behavior has been stable across at least 3 Cursor releases and 5 RTK releases — this isn't a transient quirk.

Current state of this PR:

  • Base: develop (fresh)
  • Diff: still 1 file (src/hooks/hook_cmd.rs), now +91/-12
  • Tests: 3 new (single BOM / double BOM / helper unit) + assertions added to 2 existing — all 43 hooks::hook_cmd::tests pass
  • Mergeable: yes
  • cargo build --release: clean

#1807 by @cristianrojas07 carries the same three fixes with equivalent intent. Maintainers can land either — they're functionally identical now. The other will be closed in deference to whichever lands first.

@pszymkowiak pszymkowiak left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Verified deny path short-circuits at hook_cmd.rs:432-437 before the decision = "allow" change, so safety model is preserved. Behavioral diff is limited to default "ask" mode → rewrite, matching how Claude/Copilot already behave. BOM-strip helper is loop-based (handles double BOM) and well unit-tested. Solid root-cause work with hex byte evidence and end-to-end Cursor screenshots.

@pszymkowiak
pszymkowiak merged commit 2d6e10a into rtk-ai:develop May 10, 2026
1 check passed
@aeppling aeppling mentioned this pull request May 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug Report: Cursor agents doesn't use RTK on Ubuntu 24.04.4 LTS

4 participants