Skip to content

Uninstall can duplicate user prose in CLAUDE.md, delete user lines starting with @RTK.md, and rewrite the whole file's line endings #3403

Open

Description

@HetCreep

Three defects in how rtk edits the user's own instruction files. All three have a correct counterpart elsewhere in the same file, so none looks intentional.

Provenance: AI-assisted source audit, human-directed, at v0.44.2 (700bdde). The three behaviours below were reproduced by extracting the functions and running them with rustc; rtk init itself was never executed (it writes to a real home directory).

1. remove_rtk_block searches for the end marker from position 0

src/hooks/init.rs:2792:

if let (Some(start), Some(end)) = (content.find(RTK_BLOCK_START), content.find(RTK_BLOCK_END)) {

end is found from the beginning of the file, not from start. If the closing marker text appears anywhere above the real block — for example a user quoting rtk's markers in their own notes, or a leftover from a previous edit — then end < start, and the resulting splice of content[..start] + content[end_pos..] duplicates the span between them while leaving the actual block in place. The function still returns true, so uninstall reports success.

The correct version is 330 lines earlier in the same file — upsert_rtk_block (init.rs:2464-2466):

if let Some(start) = content.find(start_marker) {
    if let Some(relative_end) = content[start..].find(end_marker) {
        let end = start + relative_end;

Reproduced with the extracted functions: with the closing marker quoted in prose above the block, removed=true, a span of user text was duplicated, and the block was not removed. This is reachable on install too (:2596, :2662).

2. @RTK.md is matched by prefix, so user prose is deleted

src/hooks/init.rs:773:

.filter(|line| !line.trim().starts_with(RTK_MD_REF))

Any line beginning with @RTK.md is removed. Reproduced — this user line is deleted on uninstall:

@RTK.md is where I keep my own token rules; do not delete.

The same file does the same job correctly with equality, in has_rtk_reference (:2741):

.any(|line| refs.contains(&line))

3. The whole file's line endings are rewritten

init.rs:777 and :2764 rebuild content via .lines() + join("\n") (through clean_double_blanks:1082). Reproduced: CRLF input comes back LF, and the trailing newline is dropped.

On Windows that means editing one block reformats the entire file — a large, noisy diff in a file that is usually committed, and a file that no longer ends with a newline.

Suggested fixes (untested — no programmer has reviewed these)

  1. Use the content[start..].find(end_marker) form from upsert_rtk_block, and treat "end marker found before start" as "no block present" rather than splicing.
  2. Compare the whole trimmed line for equality, as has_rtk_reference does, instead of starts_with.
  3. Detect the dominant line ending before rebuilding and join with it; preserve a trailing newline if the input had one.

Why this matters more than a normal formatting bug

CLAUDE.md, AGENTS.md and copilot-instructions.md are user-authored and usually committed. Items 1 and 2 silently modify the user's own prose during an operation they expect to be purely subtractive, and item 3 turns any edit into a whole-file diff. There is no .bak on the CLAUDE.md path (init.rs:2618, :2640, :819 use bare fs::write), while the settings.json path in the same file uses atomic_write and takes a backup — so there is nothing to restore from.

Activity

  1. pszymkowiak commented on Aug 4, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 90%

    Summary

    Uninstall/init logic in init.rs has three related bugs (wrong marker search direction, prefix-match deletion, full line-ending rewrite) that silently duplicate or delete user prose in CLAUDE.md/AGENTS.md with no backup — fix by mirroring the correct patterns already present elsewhere in the same file.

    This looks like a simple fix that could be auto-resolved. Repo maintainers can use /wshm fix to attempt it.

    📁 Relevant files
    • src/hooks/init.rs

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

  2. KuSh commented on Oct 18, 2026

    @KuSh
    Collaborator

    One more case of item 1, on the opening side. It is unchanged by #4405, which anchors the closing-marker search after the opening marker.

    When the opening marker text is quoted in prose above the real block, remove_rtk_block starts the cut at the quote, so everything from there to the end of the real block is deleted. Reproduced with rtk init -g --uninstall on develop e542dca and on #4405's head, which give identical output:

    # Notes
    I quote `<!-- rtk-instructions` here.
    KEEP ME
    
    <!-- rtk-instructions v2 -->
    ...
    <!-- /rtk-instructions -->
    
    tail
    

    becomes

    # Notes
    I quote `
    
    tail
    

    The function returns true and uninstall reports CLAUDE.md: removed rtk-instructions block. The same helper runs on install migration and in the Copilot and Codex uninstall paths. One possible fix is to anchor the opening marker to the start of a line (rtk always writes it as its own line), or to take the last opening marker before the closing marker.

  3. KuSh commented on Oct 23, 2026

    @KuSh
    Collaborator

    #4405 is now merged on develop, not in a release yet. It anchors the closing-marker search after the opening marker and limits @RTK.md removal to exact lines.

    Two forms are still broken on develop 2dabd83:

    • The opening-marker case from my previous comment: an opening marker quoted in prose above the block still makes rtk init -g --uninstall delete the text in between, and uninstall still reports that it removed the block.
    • Item 3: a CRLF CLAUDE.md comes back with mixed line endings, because the block removal joins the remaining parts with \n\n.
  4. KuSh commented on Oct 31, 2026

    @KuSh
    Collaborator

    A related case in the same helpers, still present on develop (19ece88): the start marker mentioned in user text.

    remove_rtk_block and upsert_rtk_block take the first <!-- rtk-instructions in the file as the block start. If the user adds a note above RTK's block that mentions that marker, everything from the mention to the real block's end marker is treated as the block.

    Reproduced with rtk init -g --gemini built from #2804, which routes GEMINI.md through these helpers. The same path serves CLAUDE.md and Copilot instructions:

    # Mine
    keep A
    Note: RTK's section starts at `<!-- rtk-instructions`.
    keep B
    
    <!-- rtk-instructions v2 -->
    …
    <!-- /rtk-instructions -->
    

    After rtk init -g --gemini --uninstall the file is:

    # Mine
    keep A
    Note: RTK's section starts at `
    

    keep B and the rest of the note line are gone. A reinstall with a different awareness level goes through upsert_rtk_block, which matches the same span and overwrites it.

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