Repository navigation
fix(hooks): make Cursor preToolUse rewrites work and stay visible - #1718
pszymkowiak merged 1 commit into
Conversation
7efb293 to
b2f7028
Compare
b2f7028 to
82d0bc4
Compare
|
🙏 |
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)
…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: {}`.
82d0bc4 to
f00977a
Compare
|
Update — rebased onto current Following @cristianrojas07's parallel rebase in #1807 (thanks!), this PR is now also rebased onto current I also took the opportunity to adopt the test structure from #1807, which is genuinely better than my original:
Independent verification: @cristianrojas07 confirmed the same fix works end-to-end on a notably newer stack (Cursor 3.3.27 + Current state of this PR:
#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
left a comment
There was a problem hiding this comment.
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.
TL;DR
On Windows + Cursor, RTK's
preToolUsehook silently no-ops on every Shell command — and the panel showsOutput: {}, 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
preToolUsepanelOutput: {}for every Shell callcontinue,permission,updated_inputgit status)rtk git status)rtk gain --historyThis 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)Investigation — wrapped
rtk hook cursorwith a Python tracerThe tracer logged stdin byte-for-byte and forwarded to RTK unchanged. Same
Output: {}, but the log proved what Cursor was actually sending.Cursor on Windows ships hook payloads with two leading UTF-8 BOMs.
serde_json::from_strrefuses to parse them, sorun_cursorfalls into the "no command" branch and returns{}— silently, on every single hook call.After — same binary path, same Cursor, just patched RTK
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, returnscontinue: trueso the panel renders, and Cursor honorspermission: "allow"so the rewrite actually executes.What this PR changes
Three issues, fixed together because each one alone produces the same user-visible symptom:
permission: "ask"is silently ignored by Cursor. Cursor preToolUse only enforcesallow/deny. Returningaskwithupdated_inputcould drop the rewrite, so the original command would run unmodified. Always returnallowfor rewritten commands. Deny rules still take precedence and are evaluated first.Panel collapses to
Output: {}withoutcontinue: 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.Cursor stdin on Windows is BOM-prefixed. Strip up to N leading UTF-8 BOMs in
run_cursorbefore 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 --releasecleangit status(no BOM) →{"continue":true,"permission":"allow","updated_input":{"command":"rtk git status"}}git statuswithEF BB BF EF BB BFprepended (matches what Cursor actually sends) → same outputcd "/tmp/proj" && git status→ rewrites only the right segmenthtop,kill 12345, already-rtk-prefixed) → still{}(intended no-op)rtk gain --historyconfirms 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
bug, P1-critical, effort-medium) — "Cursor hook: rtk rewrite success exit code causes rewrites to be dropped". The original report is Linux-focused (exit-code handling in the legacyrtk-rewrite.shscript +~/.local/binPATH), but the comment thread already includes a Windows user (@lxy94, 2026-04-21: "windows系统也没有触发rewrite 命令" — "Windows also does not trigger the rewrite command"). This PR addresses the Windows variant of the same family: the nativertk hook cursorpath silently no-ops because Cursor sends BOM-prefixed stdin. The Linux exit-code/PATH parts of Cursor hook: rtk rewrite success exit code causes rewrites to be dropped; optional PATH note for Linux #1272 remain, so this isRefsrather thanCloses.bug, awaiting-information) — "Cursor agents doesn't use RTK on Ubuntu 24.04.4 LTS". Same external symptom as the Windows case (rtk init -g --agent cursorruns cleanly, hook is registered, but agent commands never get thertkprefix). Worth retesting on Ubuntu with this branch — if Cursor on Linux also prepends a UTF-8 BOM to hook stdin, this PR likely closes Bug Report: Cursor agents doesn't use RTK on Ubuntu 24.04.4 LTS #1463 as well; if not, at least narrows the search.Notes for reviewers
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.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.pr-1718-assets) so this PR's diff stays purely the code change.