Repository navigation
Description
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)
- Use the
content[start..].find(end_marker)form fromupsert_rtk_block, and treat "end marker found before start" as "no block present" rather than splicing. - Compare the whole trimmed line for equality, as
has_rtk_referencedoes, instead ofstarts_with. - 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
pszymkowiak commentedon Aug 4, 2026
🔍 Automated Triage
bughighSummary
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.
📁 Relevant files
src/hooks/init.rsTriaged automatically by wshm · This is an automated analysis, not a human review.
KuSh commentedon Oct 18, 2026
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_blockstarts the cut at the quote, so everything from there to the end of the real block is deleted. Reproduced withrtk init -g --uninstallon develop e542dca and on #4405's head, which give identical output:becomes
The function returns
trueand uninstall reportsCLAUDE.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.KuSh commentedon Oct 23, 2026
#4405 is now merged on develop, not in a release yet. It anchors the closing-marker search after the opening marker and limits
@RTK.mdremoval to exact lines.Two forms are still broken on develop 2dabd83:
rtk init -g --uninstalldelete the text in between, and uninstall still reports that it removed the block.CLAUDE.mdcomes back with mixed line endings, because the block removal joins the remaining parts with\n\n.KuSh commentedon Oct 31, 2026
A related case in the same helpers, still present on develop (19ece88): the start marker mentioned in user text.
remove_rtk_blockandupsert_rtk_blocktake the first<!-- rtk-instructionsin 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 --geminibuilt from #2804, which routes GEMINI.md through these helpers. The same path serves CLAUDE.md and Copilot instructions:After
rtk init -g --gemini --uninstallthe file is:keep Band the rest of the note line are gone. A reinstall with a different awareness level goes throughupsert_rtk_block, which matches the same span and overwrites it.