Repository navigation
fix(init): upsert GEMINI.md instead of clobbering user content - #2804
pszymkowiak wants to merge 3 commits into
Conversation
`rtk init -g --gemini` overwrote the whole ~/.gemini/GEMINI.md with RTK's awareness content via write_if_changed(), destroying any user instructions already there. Uninstall likewise fs::remove_file()'d the entire file. Wrap the RTK awareness content in the shared `<!-- rtk-instructions -->` marker block and write it through write_rtk_block(), which appends or updates only that block and preserves the rest of the file (same mechanism already used for CLAUDE.md and Copilot instructions). Uninstall now strips only the marker block via remove_rtk_block(), deleting the file solely when nothing else remains. Verified end-to-end against a GEMINI.md with pre-existing user content: install appends (137 -> 1157 bytes, user content intact), re-install is idempotent (single block), uninstall removes only the block and leaves the user content byte-for-byte. Takeover of #835 (abandoned by its author); same approach, rebased onto the current init.rs. Closes #834 Co-authored-by: bnaylor <[email protected]> Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Hey @pszymkowiak BlockersExisting GEMINI.md files are unmarked RTK_SLIM. Upsert appends beneath them so content id duplicated, and uninstall marker check short-circuits, leaving it orphaned (develop deletes it cleanly). remove_rtk_block finds the end marker from offset 0, not content[start..] like upsert_rtk_block. |
|
This pull request has been automatically marked as stale due to 90 days of inactivity. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. Your contribution is very much appreciated — we're sorry we haven't been able to review it yet. |
Reapplied on the split init module after the develop merge, and addressed the review: - A GEMINI.md written whole by a pre-#834 init (one of the awareness documents, no markers) is RTK-only: install replaces it instead of appending a second copy beneath it, and uninstall deletes it as before. - Uninstall strips only the marker block from a user file and deletes the file when nothing else remains; a file without RTK content is untouched. - remove_rtk_block on develop already searches the end marker from the start marker, so a stray end marker above the block no longer eats user content; covered by a test.
|
Merged develop (init.rs was split into
5 unit tests in |
KuSh
left a comment
There was a problem hiding this comment.
Round 1 — CHANGES REQUESTED
Claim: rtk init -g --gemini upserts a marked RTK block into ~/.gemini/GEMINI.md instead of overwriting it, and uninstall strips only that block, while an RTK-only file from an older rtk is still replaced on install and deleted on uninstall.
Scope: accept.
Ran: merge-base 5ab6291 vs head e5ba512 (distinct binaries), 17 install / reinstall / uninstall / dry-run cases in an isolated HOME. Legacy files came from a real v0.52.0 build (default/high/full), from the merge-base build (the v1.0–v1.1 texts), and from the v0.31–v0.48 texts taken from their git blobs. Invariants: non-default targets (legacy files from all 8 released texts, a symlinked GEMINI.md, a non-UTF-8 GEMINI.md), every decision (pin check below). Split streams, the hook and the savings floor don't apply: this changes rtk init, not a filter.
Pins: 14 decisions, 18 mutations, 9 survived the suite (plus 1 equivalent)
Matrix: executed this round; pin check: executed this round
CI @e5ba51240: all green (21/21). Locally, fmt and clippy are clean on the head and on the head merged with develop 19ece88. cargo test --all --no-fail-fast on the merge: 61 test binaries, and the only failure is go_test_help_is_not_replayed_through_the_injected_json, which depends on a local toolchain shim and is unrelated.
Blocks merge (3)
-
src/hooks/init/gemini.rs:104— legacy detection compares against the live awareness text, so only files from v1.0.0–v1.1.0 are recognised. Every release from v0.31.0 to v1.1.0 wrote GEMINI.md whole and unmarked, using 8 distinct texts:hooks/rtk-awareness.md@0eaf3d52 (v0.31.0–v0.43.0), @f68d972a (v0.44.0–v0.48.0), default/high/full @55ec8858 / dcbec955 / 6b5b43ba (v0.49.0–v0.52.0), and @fca40c21 / 01b5a2b8 / 42404b87 (v1.0.0–v1.1.0).is_legacy_rtk_gemini_mdmatches only the last three, which leaves 33 of the 36 releases that support Gemini unrecognised. Evidence, with a file written by a v0.52.0 build (452 B):- head install → 961 B, holding the old text and then the new block. At
fulllevel that is two# RTKsections, one sayingrtk proxy <cmd>and the otherrtk bypass <cmd>. - head uninstall → strips the block and leaves the 452 B legacy text. Run on the v0.52.0 file directly (no reinstall first), it leaves the file untouched and the report doesn't mention GEMINI.md.
- develop: install replaces the file, uninstall deletes it.
The texts from v0.31–v0.48 behave the same way. Because the check reads
RTK_AWARENESS_*, the next edit tohooks/rtk-awareness*.mdwill also stop v1.0–v1.1 files being recognised. A correct fix has to:- install: for each of the 8 texts above, leave exactly one RTK copy (the marked block);
- uninstall: for each of the 8 texts, delete the untouched file and report it;
- compare against a frozen set (fixtures or hashes of those blobs), not the current constants;
- test each entry of the set. Today, removing
RTK_AWARENESS_FULLfrom the list (m04c) and comparing withouttrim()(m05) both pass all 342 init tests.
- head install → 961 B, holding the old text and then the new block. At
-
src/hooks/init/gemini.rs:159— uninstall stops at an unreadable GEMINI.md, after it has already removed the hook script. To reproduce: install over# Mine\n, then append a Latin-1 line (caf\xe9) and runrtk init -g --gemini --uninstall. Head exits 1 withFailed to read …: stream did not contain valid UTF-8. By then the hook script is already deleted, andsettings.jsonstill has itsBeforeToolentry pointing at it. Develop exits 0 and completes the uninstall. Expected for that file: exit 0, hook script gone, no rtk entry left insettings.json, GEMINI.md left byte for byte with a warning. -
src/hooks/init/gemini.rs:69— unpinned behaviour. Each of these mutations passes all 342hooks::init::tests:- Entry points.
install_gemini_md(&gemini_md_path, ctx)?→ the oldwrite_if_changed(&gemini_md_path, awareness_content(ctx.awareness), GEMINI_MD, ctx)?(m13), so nothing pins the headline fix whererun_geminicalls it. Droppingremoved.push(msg)inuninstall_gemini(m12) also passes. Test must: runrun_geminianduninstall_geminiover a user GEMINI.md, assert the user text survives the install, and assert theGEMINI.md: removed rtk-instructions blockline in the uninstall report. - Dry-run arms.
if dry_run→if false && dry_runin the legacy install arm (m02), andif dry_run→if falsein both uninstall arms (m08a delete, m08b strip). Test must: withdry_run: trueon a legacy file and on a user file with a block, assert the file is byte-identical afterwards (and still exists). - Missing-file guards.
if path.exists()→if trueininstall_gemini_md(m11), and removing the!path.exists()early return inuninstall_gemini_md(m10). Test must: install on an absent path → one block; uninstall on an absent path →Ok(None).
- Entry points.
Optional — will not hold merge
gemini_md_blockre-implementsrtk_block()(src/hooks/init/mod.rs:272) and hardcodesv2instead ofRTK_BLOCK_VERSION.rtk_block(awareness_content(level))would keep the version in one place, as the Copilot path does.- The legacy arm writes twice:
atomic_write(path, ""), thenwrite_rtk_blockreads the file back and writes it again. If the second write fails, GEMINI.md is left empty. Writing the block directly in that arm would be one write.
Filed as follow-ups
- #3403, comment added:
remove_rtk_block/upsert_rtk_blocktake the first<!-- rtk-instructionsin the file as the block start. Suppose the user writes a note above RTK's block that mentions that marker: on uninstall, everything between the mention and the block's end is deleted. Develop deletes the whole file, so this isn't a regression in this PR; the same helpers serve CLAUDE.md and Copilot.
Checked and correct — no need to re-verify
- New user file: install keeps the user text, reinstall is a no-op, uninstall gives back the original byte for byte. A file with no RTK content is left alone. A fresh install followed by uninstall leaves no file.
- v1.0–v1.1 RTK-only files at all three levels: replaced on install, deleted on uninstall. Changing the awareness level updates the block in place.
- Dry-run on a user file and on a v1.x legacy file writes nothing.
--hook-onlydoesn't touch GEMINI.md. A symlinked GEMINI.md keeps the link, and the edit lands in the target, as on develop. - A start marker with no end marker makes install refuse with a recovery command, which is the same policy as the other instruction files.
- The earlier review point about the end marker above the block:
remove_rtk_blockon develop searches from the start marker. Searching from 0 instead is caught byend_marker_above_the_block_does_not_eat_user_contentand by twoagents_mdtests.
Your questions
- None open.
Next
Recognise the 8 released texts (as a frozen set) on install and uninstall, keep the uninstall going past an unreadable GEMINI.md, and add tests that kill the 9 surviving mutations. After that, this merges.
Threads: 3 open (waiting on the author), 0 resolved this round.
| /// True when `content` is exactly what a pre-#834 `rtk init -g --gemini` wrote: | ||
| /// one of the awareness documents, unmarked. Such a file is RTK-only, so it is | ||
| /// safe to replace on install and delete on uninstall. | ||
| fn is_legacy_rtk_gemini_md(content: &str) -> bool { |
There was a problem hiding this comment.
Blocker 1: this compares against the live RTK_AWARENESS_* constants, so only the v1.0.0–v1.1.0 texts match. A GEMINI.md written by v0.31.0–v0.52.0 (5 other texts; blobs listed in the review body) gets a second RTK copy appended on install and is left in place on uninstall, where develop replaced or deleted it. The next edit to hooks/rtk-awareness*.md would drop v1.x files as well. Reproduced with a v0.52.0 build: 452 B → 961 B with both texts after install; 452 B left after uninstall.
| return Ok(None); | ||
| } | ||
| let content = | ||
| fs::read_to_string(path).with_context(|| format!("Failed to read {}", path.display()))?; |
There was a problem hiding this comment.
Blocker 2: a GEMINI.md that isn't valid UTF-8 makes this ? abort uninstall_gemini with exit 1. The hook script has already been deleted at that point, and settings.json keeps its BeforeTool entry. Develop completes the uninstall with exit 0. Expected: leave the file as it is with a warning, and carry on to settings.json.
| GEMINI_MD, | ||
| ctx, | ||
| )?; | ||
| install_gemini_md(&gemini_md_path, ctx)?; |
There was a problem hiding this comment.
Blocker 3: putting the old write_if_changed(&gemini_md_path, awareness_content(ctx.awareness), GEMINI_MD, ctx)? back here passes all 342 hooks::init:: tests. So do the dry-run, missing-file and report mutations listed in the review body. Each needs a test that fails under it.
Problem (#834)
rtk init -g --geminioverwrites the entire~/.gemini/GEMINI.mdwith RTK's awareness content (write_if_changed), destroying any user instructions already in the file. Uninstall is just as destructive — itfs::remove_file()s the whole file. Multiple users have hit this data loss (see #834 / #835).Fix
Wrap the RTK awareness content in the shared
<!-- rtk-instructions -->marker block and write it throughwrite_rtk_block()— the same append/update-in-place mechanism already used forCLAUDE.mdand Copilot instructions. Uninstall now strips only the marker block viaremove_rtk_block(), deleting the file solely when nothing else remains.No new helpers — this reuses
write_rtk_block/upsert_rtk_block/remove_rtk_blockalready ondevelop.Verification
Unit test added (
test_gemini_md_upsert_and_remove_preserve_user_content) + end-to-end against a realGEMINI.mdwith pre-existing user content:[ok] Added GEMINI.md— user content intact, block appended (137 → 1157 bytes)cargo fmt/cargo clippy --all-targetsclean;inittest suite green.Credit
Takeover of #835 (abandoned by its author @bnaylor); same approach, rebased onto the current
init.rs. Credited viaCo-authored-by.Closes #834
🤖 Generated with Claude Code