Skip to content

rtk init -g deletes the legacy hook script and either unregisters rtk entirely or leaves a dangling reference — both reported as "RTK hook registered (global)" #3693

Open

Description

@mattbuot

Related issues (searched before filing)

Summary

rtk init -g deletes ~/.claude/hooks/rtk-rewrite.sh before it has written any replacement registration, and prints RTK hook registered (global). at a point in the flow where settings.json has not been touched yet. Depending on what else lives in the Bash matcher group, the user is left in one of two broken states — both with exit 0 and a success banner:

Precondition Result
A. rtk's legacy entry is the only hook in the Bash matcher group Script deleted and the entry removed. settings.json ends as "PreToolUse": []. rtk is fully unregistered.
B. another hook shares the Bash matcher group Script deleted, entry kept. settings.json still points at a file that no longer exists — the state reported in #1962.

Variant A is the quieter of the two: Claude Code raises no hook error, because there is no hook. Every subsequent agent command runs unfiltered with nothing to signal it.

Environment

  • rtk 0.45.0 (Homebrew), macOS arm64 (Darwin 25.6.0)
  • Command: rtk init -g, stdin redirected from /dev/null (non-TTY)
  • Both variants reproduced in an isolated HOME; targeting verified with --dry-run first
  • Root-caused against develop, so this is not specific to the 0.45.0 build

Reproduce — variant A (rtk alone in the matcher group)

Before:

$ ls -l $SB/.claude/hooks/
-rwxr-xr-x  53  rtk-rewrite.sh

$ cat $SB/.claude/settings.json
{
  "hooks": {
    "PreToolUse": [
      { "matcher": "Bash",
        "hooks": [ { "type": "command", "command": "$SB/.claude/hooks/rtk-rewrite.sh" } ] }
    ]
  }
}

Run:

$ HOME=$SB rtk init -g < /dev/null

RTK hook registered (global).

  Command:   rtk hook claude
  RTK.md:    $SB/.claude/RTK.md (10 lines)
  CLAUDE.md: @RTK.md reference added

Patch existing $SB/.claude/settings.json? [y/N]
(non-interactive mode, defaulting to N)

  MANUAL STEP: Add this to $SB/.claude/settings.json:
  ...

$ echo $?
0

After:

$ ls -la $SB/.claude/hooks/
(empty)

$ cat $SB/.claude/settings.json
{
  "hooks": {
    "PreToolUse": []
  }
}

$ grep -rl "rtk" $SB/.claude --include="*.json"
(no match)

Reproduce — variant B (a second hook shares the group)

Same starting point, except the Bash group also contains /opt/acme/audit-hook.sh, and a Write group contains /opt/acme/write-guard.sh. After the same command:

  • settings.json is byte-identical — the legacy entry, the co-resident hook and the Write group all survive;
  • ~/.claude/hooks/rtk-rewrite.sh is gone;
  • so settings.json now registers a PreToolUse command whose script does not exist. This is rtk-rewrite.sh: No such file or directory #1962.

The foreign hooks surviving is correct behaviour and should be preserved by any fix.

Root cause (line numbers verified against develop, src/hooks/init.rs)

  1. init.rs:1160 — migrate_old_hook_script(ctx) runs as step 1.
  2. init.rs:1249 — deletes ~/.claude/hooks/rtk-rewrite.sh. On success, init.rs:1257 calls remove_legacy_settings_entries. The deletion is unconditional; nothing has been written in its place yet.
  3. init.rs:1344 — remove_legacy_hook_entries_from_json drops a matcher entry only when dominated_by_legacy holds, i.e. when every command in the group is the legacy script. That single predicate is what splits variant A from variant B.
  4. init.rs:1179 — println!("\nRTK hook registered (global).\n") fires at step 4, before settings.json is touched at all at step 5. The banner is unconditional and cannot reflect an outcome that has not happened.
  5. init.rs:1198 → 1001 → 445 — under the default PatchMode::Ask, prompt_user_consent returns Ok(false) on non-TTY stdin ((non-interactive mode, defaulting to N)) → PatchResult::Declined.
  6. init.rs:1211 — the Declined | Skipped arm prints nothing further, so the success banner stands as the run's last status line.

To be fair to the current behaviour: the MANUAL STEP block is printed, and a human watching a terminal can act on it. The defect is that the banner above it says the opposite, the exit code is 0, and the previously working install has already been destroyed by the time either is printed — so nothing recoverable is left if the output is not read.

--dry-run does not disclose either effect

rtk init -g --dry-run on the variant-A fixture prints only:

[dry-run] would migrate legacy hook script: …/rtk-rewrite.sh
[dry-run] would create RTK.md: …
[dry-run] would add @RTK.md reference to CLAUDE.md: …
[dry-run] would create global filters template: …

[dry-run] Nothing written.

There is no would remove legacy rtk-rewrite.sh entry from settings.json line (that branch at init.rs:1320 is only reached from the non-dry-run delete path) and no would prompt before patching. So the one command a cautious user would run to preview this cannot show them the destructive part.

Interaction with #2588

PR #2588 ("Fixes #1962") hoists remove_legacy_settings_entries out of the deletion-success branch, so the entry is stripped even when the script removal failed and when no legacy script exists. It does not touch the banner at init.rs:1179 or the Declined path.

That converts variant B into variant A for more users rather than fixing either: it removes the dangling reference by removing the registration, while the replacement write can still be declined. Worth resolving the ordering here first, or folding it in.

Suggested fix

  • Move the RTK hook registered (global). banner after step 5 and derive its wording from PatchResult. Declined and Skipped must not print a registration banner, and arguably should not exit 0.
  • Write before deleting: patch settings.json with rtk hook claude first, and remove the legacy script and legacy entry only after that write succeeds — the ordering PR fix(init): migrate Cursor hooks.json before deleting the legacy hook script #3570 adopted for Cursor. This fixes both variants at once and preserves co-resident foreign hooks.
  • Make --dry-run report the legacy-entry removal and the consent prompt, so the preview matches the real run.

I have the fixture scripts for both variants and am happy to send a PR with regression coverage if that is useful — flagging first since src/hooks/init.rs is a Tier-1 file under SECURITY.md and #2588 already claims part of this code.

Activity

  1. pszymkowiak commented on Aug 24, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 93%

    Summary

    rtk init -g deletes the legacy hook script before writing a replacement and prints a false success banner, leaving rtk either fully unregistered or pointing at a missing script (exit 0, no error signal) — reorder write-before-delete and fix the banner/dry-run reporting in src/hooks/init.rs.

    📁 Relevant files
    • src/hooks/init.rs

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

  2. KuSh commented on Oct 3, 2026

    @KuSh
    Collaborator

    Two more states from the same migration, found while checking the Windows docs in #4350 (develop 4f9015f, isolated HOME, legacy setups made with v0.36.0).

    1. rtk init --show reports any legacy entry as the native hook. This applies whether or not the script is still on disk. Before any migration, with ~/.claude/hooks/rtk-rewrite.sh present and settings.json pointing at it:

    $ rtk init --show
    [ok] Hook: rtk hook claude (native binary command)
    [ok] settings.json: RTK hook configured
    

    The same output appears after the script has been deleted, which is the state variant B of this issue ends in. There, rtk init -g also says settings.json: hook already present and leaves the entry pointing at the missing file:

    $ grep -o '"command": *"[^"]*"' ~/.claude/settings.json
    "command": "$HOME/.claude/hooks/rtk-rewrite.sh"
    $ rtk init --show
    [ok] Hook: rtk hook claude (native binary command)
    [ok] settings.json: RTK hook configured
    

    --show calls hook_already_present (src/hooks/init/mod.rs:1448), which matches any command containing rtk-rewrite.sh. Its result is checked first, so the script-file branches below it ("legacy script v…", "outdated") are never reached while such an entry exists. So the check that the docs point people to after rtk init -g gives a green result both before the migration and for a hook that fails on every Bash call.

    2. Declining the prompt on the CLAUDE.md-fallback state. The same order of operations shows up on the state that native Windows installs before v0.37.2 were left in (init -g --claude-md from v0.36.0, so the full block in ~/.claude/CLAUDE.md and no hook). rtk init -g replaces that block with @RTK.md before it asks about settings.json. Declining, or running without a TTY, then leaves:

      [ok] Migrated: removed 137-line RTK block from CLAUDE.md
    Patch existing ~/.claude/settings.json? [y/N]
    (non-interactive mode, defaulting to N)
    
    $ rtk init --show
    [--] Hook: not found
    [ok] Global (~/.claude/CLAUDE.md): @RTK.md reference
    

    The default-awareness RTK.md assumes the hook is in place ("Command output here is condensed…"; it never says to prefix commands). So that setup goes from rtk used via the instructions to rtk not used at all, under the same RTK hook registered (global). banner.

    3. The backup loses the original. Answering y on a legacy setup writes settings.json.bak twice: first when the legacy entry is removed, then again when rtk hook claude is added. The final .bak holds the in-between file ("PreToolUse": []), not the original. Other keys survive in both, but the backup no longer shows what the user had before running init.

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