Repository navigation
fix(hooks): make Cursor preToolUse rewrites work and stay visible (rebase of #1718) - #1807
cristianrojas07 wants to merge 1 commit into
Conversation
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)
8d4f6aa to
ef997b0
Compare
|
@cristianrojas07 thanks a lot for picking this up and for the explicit credit — really appreciated. Two pieces of good news from your end that strengthen the case for either PR landing:
I just rebased #1718 onto the same So the practical state for maintainers is: either PR is now equivalent in scope and base, and we both agree the other is welcome to land. If maintainers prefer this PR's branch (cleaner base + your improved tests already in place from day one) over mine, I'm equally happy to close #1718. If they prefer the original PR (longer history with screenshots, hex-dump evidence, related-issue links built into the description), I'll update its description to mention this one as parallel work. Either way — thanks for caring enough to rebase rather than wait. That's how OSS is supposed to work. |
Summary
Rebases #1718 ("fix(hooks): make Cursor preToolUse rewrites work and stay visible") onto current
developso the fix is consumable by users onrtk 0.39.xwhile the original PR catches up. Credit goes to @kamilkaczmareksolutions for the original investigation and patch — this PR exists only because I needed the fix ondevelopHEAD and the original branch is based on an older commit. Happy to close this in favor of #1718 the moment that one merges.What's broken (recap of #1718)
On Windows + Cursor,
rtk hook cursorsilently no-ops on every Shell command. The Cursor panel showsOutput: {}, which looks like the hook fired correctly — it didn't. The rewrite path is never reached.preToolUsepanelOutput: {}for every Shell callgit status)rtk gain --historyThe confusion is that
Output: {}is also the legitimate response when RTK intentionally skips a command (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".Three fixes, bundled (same as #1718)
1.
permission: "ask"is silently ignored by CursorCursor
preToolUseonly enforces"allow"/"deny". Returning"ask"withupdated_inputcauses Cursor to drop the rewrite and run the original command. Always return"allow"for rewrites. Deny verdicts still take precedence and are short-circuited above.2. Panel collapses to
Output: {}withoutcontinue: trueEvery other Cursor hook (
afterShellExecution,beforeSubmitPrompt,stop, ...) returns{ "continue": true, ... }and renders the JSON correctly. The flat{ permission, updated_input }payload was technically valid but the Cursor UI didn't surface it. Mirroring the working shape makes the rewrite visible.3. Cursor stdin on Windows is BOM-prefixed
Cursor on Windows ships hook payloads with one or two leading UTF-8 BOMs (
EF BB BF [EF BB BF]).serde_json::from_strrejects them, sorun_cursorfalls into the "no command" branch and returns{}— silently, on every single hook call. This is the actual root cause of "RTK silently does nothing in Cursor". Fixes #1 and #2 above are correctness/UX gaps that only become visible once parsing succeeds.The loop is defensive: covers the observed double-BOM case and any future N-BOM variant without code changes.
Why a separate PR instead of just commenting on #1718
I need the fix on
developHEAD today on Windows + Cursor 3.3.27 +rtk 0.39.0. The original PR's branch is based on a commit ~6 months back, so it doesn't apply cleanly without a rebase, and the upstream tree has moved on. Rather than blocking on the original author's availability to rebase, this PR:develop.strip_utf8_boms) named and shaped identically to make the diff against fix(hooks): make Cursor preToolUse rewrites work and stay visible #1718 obvious (same author intent, just a different base).If the maintainers prefer to land #1718 directly (e.g. after rebasing the original branch), I'll happily close this one. The credit for the design and investigation belongs to @kamilkaczmareksolutions.
Test plan
test_cursor_rewrite_flat_formatupdated to assertpermission: "allow"+continue: true(was"ask").test_cursor_no_hook_specific_outputupdated similarly.test_cursor_strips_single_utf8_bom— one leading BOM → still parses + rewrites.test_cursor_strips_double_utf8_bom— two leading BOMs (real-world case on Windows) → still parses + rewrites.test_strip_utf8_boms_helper— unit test covering 0/1/2/3 BOMs and empty input.htop, heredoc, already-rtk) unchanged — their{}responses are still correct.rtk 0.39.0-rebased: panel renders the rewrite payload (no moreOutput: {});rtk git statusproduces compact output;rtk gain --historyconfirms rewrites land with token reduction.Related issues
~/.cursor/hooks.jsonso the "No hook installed" warning stops firing for users who configure rtk via Cursor's native hooks file. Independent of this PR; can land in any order.Notes for reviewers
run_cursor()body + 4 unit tests + 1 helper function (private to the module).