Skip to content

fix(bots): keep the user's GEMINI.md bytes and concurrent saves when Orca removes its block - #49122

Merged
AmoabaKelvin merged 2 commits into
mainfrom
fix/gemini-md-cleanup-keeps-user-bytes
Nov 25, 2026
Merged

AmoabaKelvin merged 2 commits into
mainfrom
fix/gemini-md-cleanup-keeps-user-bytes

Conversation

@AmoabaKelvin

@AmoabaKelvin AmoabaKelvin commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

ELI5

Bots that used to run on Gemini CLI left a copy of their instructions in the folder's GEMINI.md. When an Antigravity chat starts, Orca takes that copy back out. That cleanup could undo a save the user made at the same moment, and it could strip the indentation off a code block in the user's own text. Now it removes only Orca's block and leaves every other byte as the user wrote it. If the file changed after Orca read it, Orca starts over from the new version.

Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​55 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​54
Prod 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​152 $\color{#cf222e}{\Huge{\mathbf{−}}}$​32 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​120

What Changed

Before, cleanup read GEMINI.md, built the new text from that read, and then renamed it over the file, or deleted the file if Orca had written all of it. A save that landed between the read and the rename was lost, and a user's replacement file could be deleted. It also ran trimEnd/trimStart on the text on both sides of Orca's block, so a code block right after the block lost its leading spaces.

Now:

  • Cleanup removes Orca's <!-- orca:begin -->…<!-- orca:end --> lines, the line break after them, and the one blank line Orca put in front when it appended them. Nothing else changes: CRLF, tabs, indentation and trailing spaces all stay. A file with no Orca block is not touched.
  • Every change to a folder context file goes through one path in acp-folder-context-file.ts. Orca writes a complete staged copy, re-reads the file, and renames over it only while the file still holds the bytes the change was computed from. A file that didn't exist is linked in, and the link fails rather than replacing a file someone else created. If the file changed, Orca re-reads it and builds the change again, up to 3 times. The file is never missing at any point, so a second launch never mistakes it for absent.
  • The per-agent writers (.hermes.md, QWEN.md, .goosehints, .kimi-code/AGENTS.md, .cline/rules/orca.md, AGENTS.local.md, CODEBUDDY.local.md) used the same read-then-rename path and had the same lost-save window. They now use the new path too. They still rewrite the file on every launch as before, so the 0600 permission repair from STA-16956 still runs.

Why

The original code assumed that renaming a staged file into place made the update safe. A rename does keep readers from ever seeing a half-written file. It does nothing about a save that landed after Orca read the file. The cleanup added when Gemini CLI was removed reused that path, and added the trims to keep the joins tidy. Those trims also run over text the user owns.

An earlier version of this fix moved the file aside before comparing it, which closes the window completely. But the file then went missing for a moment: a second launch running at the same time took it as absent and wrote Orca's own file over the user's text, and a crash at that point would have left the user's file under a temporary name. Comparing a staged copy and then renaming it never leaves the file missing. A save that lands in the microseconds between the final compare and the rename can still be replaced, because Node has no compare-and-swap rename. That is documented in the code. The synchronous guarded helpers in atomic-file-publication.ts aren't allowed here, because this runs on Electron main during chat launch.

Linked Issue

STA-18108

Visual Proof

N/A: nothing on screen changes. The fix is in how a file in the user's folder is rewritten.

Testing

  • acp-folder-context-file.test.ts gained these tests:

    • Removing the block leaves the user's surrounding bytes exact. The test covers CRLF, an indented code block, trailing spaces and a tab.
    • A save that lands during cleanup survives. This runs for both Orca's own file and a user file, and checks that no staged file is left behind. Both tests fail on main: the indentation is stripped, the save is overwritten, and the save is deleted.
    • A 0644 file that already holds Orca's current text still goes back to 0600.
  • Ran 300 rounds of two launches writing the same QWEN.md at once, and two cleanups of the same GEMINI.md at once. User text was never lost and no stray files were left (throwaway script, not committed).

  • Tested on macOS. Linux, Windows and SSH hosts (orcad) run the same code. Filesystems without hard links fall back through the shared isHardLinkUnsupported check.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

  • Every added test fails without this change and avoids the test anti-patterns

Performance

Small and per launch only. Changing an existing context file now reads it twice instead of once (the second read is the compare before the rename). A conflict costs at most 3 passes. Cleanup of a GEMINI.md with no Orca block still does one read and no write. Nothing runs on a timer, per event or per worktree, and all the filesystem work is async.

Risk

Level: Low

The riskiest part is the new publish step for context files on unusual filesystems. If it went wrong, a bot's instructions would not be written for that launch, or the cleanup would leave the old block in place. The user's own text is never replaced. A plain revert backs this out.

AI Disclosure

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Not changed here and existing before this PR: when appending its block, the writer still trims trailing whitespace from the end of a user's file, and it decodes the file as UTF-8.

Ensure no issues in: Security, Cross-platform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • I stated the risk: what could break, for whom, and how to back it out
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Walkthrough

Context-file writes and removals now retry up to three times when the target changes during an update. Creation uses a hard link when supported, with an exclusive-copy fallback. Removal deletes Orca-owned files or removes only Orca’s block from user-authored files while preserving surrounding text. Tests cover restrictive permissions, exact content preservation, invalid UTF-8 files, and saves during removal. A save after the final comparison but before rename or removal can still be replaced.

Merge Risk: Low · up to 3d31e

Removing Orca's block from context files now preserves user bytes and retries when the file changes mid-update. A save landing in the very small gap between the final check and the rename can still be lost, which is a documented limitation. In rare cases, a read failure during the final comparison could also fail an agent launch instead of skipping the update. Both are narrow, so the change is mergeable with owner awareness.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: Orca's removal of its GEMINI.md block now keeps the user's bytes and concurrent saves. It matches the changeset, which adds byte-preserving cleanup and a compare-then-…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description is thorough and follows the repository template. It includes an ELI5 section, a clear What Changed section with the before and after behavior, a Why section that explains the alternati…
Full details: Title check

Explanation

The title names the main change: Orca's removal of its GEMINI.md block now keeps the user's bytes and concurrent saves. It matches the changeset, which adds byte-preserving cleanup and a compare-then-publish update path for folder context files. The title uses the fix() prefix and is specific. It is long (91 characters), but it stays on topic.

Full details: Description check

Explanation

The description is thorough and follows the repository template. It includes an ELI5 section, a clear What Changed section with the before and after behavior, a Why section that explains the alternatives considered (moving the file aside, and the synchronous guarded helpers), a Linked Issue (STA-18108), a Visual Proof marked N/A with a reason, a Testing section with specific tests and stress runs, a Performance line, a Risk section with a Low level and a rollback method, and the Agent skill boundary box checked. The main gaps are the Linked Issue line, which reads 'STA-18108' without the 'Fixes #' form the template uses, and the unchecked lint, typecheck, test, and build item. The Testing section also says Linux, Windows, and SSH run the same code but were not tested. The Performance and Risk sections are filled in. The AI Disclosure section is empty. Because the repository is outside the stablyai team, that section is needed for an outside contributor, but it is not clear whether the author is internal.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 4

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b3e05b48-773b-4323-af71-713052c3214d
Commits

Reviewing files that changed from the base of the PR and between ae7849c and e1187ef.

Files selected for processing: 2

  • src/main/acp/launch/acp-folder-context-file.test.ts
  • src/main/acp/launch/acp-folder-context-file.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: dcd63749-b18f-49d9-93cb-4f0f9b819189
Commits

Reviewing files that changed from the base of the PR and between e1187ef and 3d31e9c.

Files selected for processing: 2

  • src/main/acp/launch/acp-folder-context-file.test.ts
  • src/main/acp/launch/acp-folder-context-file.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.


async function readIfPresent(path: string): Promise<string | null | undefined> {
try {
return await readUtf8(path)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor · Stability & Availability · Quick win

Treat a failed comparison read as a skipped update, not a launch error.

The comparison read in readIfPresent rethrows every error except a definitive absence. publishIfUnchanged removes its staged file and rejects. The rejection then passes through replaceWhileUnchanged, writeFolderContextFile, and the per-agent deliverers, such as deliverHermesInstructions. None of these catch it. acpLaunchInstructions does not catch it either, so launch fails unless its caller handles the rejection.

This path is new. A read error on an existing file was already ignored by the initial read. The comparison read now turns the same error into a launch failure. The doc comment says an unreadable file stays untouched, so the comparison read should match the initial read. Treat a failed comparison as a mismatch. The update loop then stops on its next read.

Suggested fix

-  const unchanged = async (): Promise<boolean> => (await readIfPresent(target)) === expected
+  const unchanged = async (): Promise<boolean> =>
+    (await readIfPresent(target).catch(() => undefined)) === expected

@AmoabaKelvin
AmoabaKelvin merged commit 15b5d95 into main Nov 25, 2026
56 checks passed
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.

1 participant