Skip to content

fix(init): upsert GEMINI.md instead of clobbering user content - #2804

Open
pszymkowiak wants to merge 3 commits into
developfrom
fix/gemini-md-clobbering-takeover
Open

pszymkowiak wants to merge 3 commits into
developfrom
fix/gemini-md-clobbering-takeover

Conversation

@pszymkowiak

Copy link
Copy Markdown
Collaborator

Problem (#834)

rtk init -g --gemini overwrites the entire ~/.gemini/GEMINI.md with RTK's awareness content (write_if_changed), destroying any user instructions already in the file. Uninstall is just as destructive — it fs::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 through write_rtk_block() — the same append/update-in-place 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.

No new helpers — this reuses write_rtk_block / upsert_rtk_block / remove_rtk_block already on develop.

Verification

Unit test added (test_gemini_md_upsert_and_remove_preserve_user_content) + end-to-end against a real GEMINI.md with pre-existing user content:

Step Result
install [ok] Added GEMINI.md — user content intact, block appended (137 → 1157 bytes)
re-install idempotent — single block, user content intact
uninstall removes only the block; user content preserved byte-for-byte

cargo fmt / cargo clippy --all-targets clean; init test suite green.

Credit

Takeover of #835 (abandoned by its author @bnaylor); same approach, rebased onto the current init.rs. Credited via Co-authored-by.

Closes #834

🤖 Generated with Claude Code

`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]>
@pszymkowiak
pszymkowiak requested a review from aeppling July 3, 2026 17:23
@aeppling

aeppling commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Hey @pszymkowiak

Blockers

Existing 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).
Strip legacy content before upserting; delete the file on uninstall when it's RTK-only

remove_rtk_block finds the end marker from offset 0, not content[start..] like upsert_rtk_block.
End marker anywhere above the block cause duplicated user content, block left in place, still reports success.

@rtk-release-bot

Copy link
Copy Markdown
Contributor

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.
@pszymkowiak

Copy link
Copy Markdown
Collaborator Author

Merged develop (init.rs was split into src/hooks/init/ since; the change now lives in gemini.rs) and addressed both blockers:

  • Legacy unmarked GEMINI.md: a file that is exactly one of the awareness documents (what a pre-[BUG] rtk init -g --gemini clobbers ~/.gemini/GEMINI.md instead of appending #834 init wrote) is detected as RTK-only. Install replaces it instead of appending a second copy; uninstall deletes it like before.
  • Uninstall: strips only the marker block from a user file, deletes the file when nothing else remains, and leaves a file without RTK content untouched.
  • End marker above the block: remove_rtk_block on develop now searches the end marker from the start marker, same as upsert_rtk_block. Added a regression test for that case.

5 unit tests in gemini_md_tests; cargo clippy --all-targets clean.

@rtk-release-bot rtk-release-bot Bot removed the stale label Oct 26, 2026

@KuSh KuSh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

  1. 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_md matches 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 full level that is two # RTK sections, one saying rtk proxy <cmd> and the other rtk 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 to hooks/rtk-awareness*.md will 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_FULL from the list (m04c) and comparing without trim() (m05) both pass all 342 init tests.
  2. 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 run rtk init -g --gemini --uninstall. Head exits 1 with Failed to read …: stream did not contain valid UTF-8. By then the hook script is already deleted, and settings.json still has its BeforeTool entry pointing at it. Develop exits 0 and completes the uninstall. Expected for that file: exit 0, hook script gone, no rtk entry left in settings.json, GEMINI.md left byte for byte with a warning.

  3. src/hooks/init/gemini.rs:69 — unpinned behaviour. Each of these mutations passes all 342 hooks::init:: tests:

    • Entry points. install_gemini_md(&gemini_md_path, ctx)? → the old write_if_changed(&gemini_md_path, awareness_content(ctx.awareness), GEMINI_MD, ctx)? (m13), so nothing pins the headline fix where run_gemini calls it. Dropping removed.push(msg) in uninstall_gemini (m12) also passes. Test must: run run_gemini and uninstall_gemini over a user GEMINI.md, assert the user text survives the install, and assert the GEMINI.md: removed rtk-instructions block line in the uninstall report.
    • Dry-run arms. if dry_run → if false && dry_run in the legacy install arm (m02), and if dry_run → if false in both uninstall arms (m08a delete, m08b strip). Test must: with dry_run: true on 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 true in install_gemini_md (m11), and removing the !path.exists() early return in uninstall_gemini_md (m10). Test must: install on an absent path → one block; uninstall on an absent path → Ok(None).

Optional — will not hold merge

  • gemini_md_block re-implements rtk_block() (src/hooks/init/mod.rs:272) and hardcodes v2 instead of RTK_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, ""), then write_rtk_block reads 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_block take the first <!-- rtk-instructions in 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-only doesn'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_block on develop searches from the start marker. Searching from 0 instead is caught by end_marker_above_the_block_does_not_eat_user_content and by two agents_md tests.

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.

Comment thread src/hooks/init/gemini.rs
/// 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 {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/hooks/init/gemini.rs
return Ok(None);
}
let content =
fs::read_to_string(path).with_context(|| format!("Failed to read {}", path.display()))?;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/hooks/init/gemini.rs
GEMINI_MD,
ctx,
)?;
install_gemini_md(&gemini_md_path, ctx)?;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] rtk init -g --gemini clobbers ~/.gemini/GEMINI.md instead of appending

3 participants