Skip to content

fix(codex): stop overwriting and deleting Codex files that were merely unreadable (STA-4737) - #15287

Merged
brennanb2025 merged 2 commits into
mainfrom
brennanb2025/sta-4737-codex-shared-observations
Aug 18, 2026
Merged

brennanb2025 merged 2 commits into
mainfrom
brennanb2025/sta-4737-codex-shared-observations

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

If a virus scanner or indexer holds one of your Codex config files open for half a second, Orca reads "that file isn't there" — and then helpfully replaces it, or deletes its copy of it. Your model choice, your MCP servers, your AGENTS.md mirror.

What Changed

Three modules shared by the host and WSL Codex lanes act on absence — they overwrite a config that "isn't there yet", or delete a mirror whose source "is gone". All three learned that absence from existsSync or from a catch returning a default, and both report exactly the same thing for a file that is merely locked.

Module What the collapse cost
codex-config-mirror.ts An unreadable runtime config.toml took the "seed a fresh runtime config" branch, replacing the user's config wholesale
config-settings-promotion.ts An unreadable ~/.codex/config.toml counted as having no promoted settings, and the write path rebuilt the user's canonical config from Orca's runtime copy
codex-home-paths.ts Both delete branches in linkSystemCodexResource fire when the source "is not there" — one denied read on ~/.codex/AGENTS.md removed the managed mirror on the next launch

src/shared/definitive-filesystem-absence.ts now owns the one errno allowlist: ENOENT and ENOTDIR are absence, and every other code — including unrecognised ones — is indeterminate. host-codex-managed-home-ownership.ts drops its private copy rather than letting two allowlists drift. codex-path-observation.ts builds the three-valued observation on top.

One incidental simplification: the resource sync's separate existsSync and statSync probes collapse into a single resolved stat, which answers reachability and regular-file-ness together and closes the window between them.

config-settings-promotion.ts crossed its max-lines budget, so the write-target resolution moved to its own module. No lint exemption.

Why

STA-4606 needs these four modules correct before it can build on them — they run on every WSL launch — but they are shared with the host lane, so fixing them inside a WSL-only change would put an all-platforms blast radius inside a PR nobody would review that way. Landing them separately is the whole reason this ticket exists.

Linked Issue

Fixes STA-4737. Blocks STA-4606. Same defect class as #15046, #15093 and #15279.

Visual Proof

N/A — no UI change. The observable difference is that a file survives, which is what the tests assert.

Review round 1 — three real gaps found

A Codex reviewer went over this and found three more instances of the same defect that my first pass missed. All three are in scope for this ticket ("every target delete moves behind the target observation") and are now fixed in df93a7d:

  1. syncSystemConfigIntoLegacySharedCodexHome — a second mirror function in the same file, carrying the identical existsSync ? read : '' collapse on both configs. I fixed only syncSystemConfigIntoManagedCodexHomeUnsafe and never checked its sibling. It throws on indeterminate, which is safe: its single caller (legacy-shared-config-compatibility.ts:12-24) already wraps it in a best-effort try/catch.
  2. pathEntryExists(targetPath) — catch { return false }, so an unreadable target read as "not there" and fell through to the copy/symlink path that overwrites it.
  3. copiedFileContentsMatch — catch { return false } meant "different", and different leads to rmSync(targetPath). A failed comparison was deleting the only readable copy. It now returns 'match' | 'different' | 'indeterminate'.

The rig also gained a denyReads mode — metadata readable, content reads failing — which is the shape a Windows sharing violation actually takes, and is more faithful than the all-or-nothing denial I wrote.

Testing

src/main/codex/sta-4737-unreadable-is-not-absent.test.ts — 10 tests, 6 driving the fix and 4 anchoring it.

The rig models one denial coherently: a denied path throws from readFileSync/statSync/lstatSync/openSync and reports false from existsSync. Faulting one call but not another lets a test pass against code that still consults the one left healthy.

Mutation-checked per module, not just in aggregate:

Reverted Result
all three the 4 fix-driving tests go red, the 3 anchors stay green
config-settings-promotion.ts only "leaves a denied system config exactly as it found it" → red
codex-home-paths.ts only "keeps the mirrored copy when the source is denied" → red
codex-config-mirror.ts only initially still green — see below
codex-config-mirror.ts to main + "refuses the retained-pane mirror when its runtime config is denied" → red
codex-home-paths.ts to main + "source becomes unreadable after stat" and "unreadable owned target" → red

That last row is worth reading. My first version of the mirror test passed with the mirror change reverted, because promotion runs first and refuses on the same denied file, so the mirror's own guard was never reached. Rather than ship a guard no test drives, I added a test that stubs promotion to "succeeded, nothing to do" and then denies the runtime config — the real shape of a scanner taking the file in the window between promotion reading it and the mirror rewriting it. With that test, reverting the mirror alone goes red.

The anchors are the other half: a genuinely absent runtime config is still seeded, a readable system config is still promoted into, and a genuinely deleted source still has its mirrored copy removed. Refusing in those cases would break first launch and strand stale instructions.

  • 1755/1755 across codex + codex-accounts + agent-hooks.

  • check-changed-code-quality.mjs: 0 findings.

  • Full pnpm typecheck clean, including the CLI project — the new modules are added to config/tsconfig.cli.json, which is an explicit allowlist rather than a glob.

  • Automated tests added

  • Manual: not run. The trigger is a file-denial race reproduced deterministically by the rig.

Merge note

#15279 (STA-4734) touches the same predicate. Both PRs carry byte-identical copies of src/shared/definitive-filesystem-absence.ts and of the host-codex-managed-home-ownership.ts import change, so they merge cleanly in either order. Neither depends on the other.

AI Disclosure

N/A — internal.

@coderabbitai

coderabbitai Bot commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 60997e99-5204-4f21-950c-4c1ea9765f38

📥 Commits

Reviewing files that changed from the base of the PR and between e7210749082a5ce3a1f45ae738ec5ed1e66cc0fe and df93a7d.

📒 Files selected for processing (2)
  • src/main/codex/codex-home-paths.ts
  • src/main/codex/sta-4737-unreadable-is-not-absent.test.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/main/codex/sta-4737-unreadable-is-not-absent.test.ts
  • src/main/codex/codex-home-paths.ts

Included review availability: Your plan includes up to 10 reviews per rolling hour; 1 remains after this review.


📝 Walkthrough

Walkthrough

Added filesystem observation helpers that classify paths as present, definitively absent, or indeterminate. Updated configuration promotion and mirroring to preserve state when reads or statistics fail. Added symlink-aware promotion write-target resolution with permission handling and cycle detection. Updated managed-resource synchronization to remove resources only for definitive absence. Added regression tests for denied and absent files.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 37.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
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.
Title check ✅ Passed The title clearly and concisely describes the primary fix: preventing unreadable Codex files from being overwritten or deleted.
Description check ✅ Passed The description covers the change, rationale, linked issue, testing, visual proof, AI disclosure, and known limitations with sufficient detail.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch

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

Comment @coderabbitai help to get the list of available commands.

@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: 3

🧹 Nitpick comments (1)
src/shared/definitive-filesystem-absence.ts (1)

1-13: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Shorten the added explanatory comments. Keep only concise, non-obvious rationale; remove implementation walkthroughs and repeated justification at the listed sites.

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: a6b5efbb-1347-407e-9936-d7ed773e94ce

📥 Commits

Reviewing files that changed from the base of the PR and between 96d88dd and a791069.

📒 Files selected for processing (9)
  • config/tsconfig.cli.json
  • src/main/codex-accounts/host-codex-managed-home-ownership.ts
  • src/main/codex/codex-config-mirror.ts
  • src/main/codex/codex-home-paths.ts
  • src/main/codex/codex-path-observation.ts
  • src/main/codex/config-settings-promotion-write-target.ts
  • src/main/codex/config-settings-promotion.ts
  • src/main/codex/sta-4737-unreadable-is-not-absent.test.ts
  • src/shared/definitive-filesystem-absence.ts

Included review availability: Your plan includes up to 10 reviews per rolling hour; 6 remain after this review.

Comment thread src/main/codex/codex-config-mirror.ts Resolved
Comment on lines +111 to 120
const sourceObservation = observeResolvedPathEntry(sourcePath)
if (sourceObservation.kind === 'indeterminate') {
return
}
if (sourceObservation.kind === 'absent') {
removeCopiedResourceIfOwned(targetPath, managedHomePath, entryName, sourcePath)
return
}
if (entryName === CODEX_GLOBAL_INSTRUCTIONS_ENTRY && !systemResourceIsRegularFile(sourcePath)) {
if (entryName === CODEX_GLOBAL_INSTRUCTIONS_ENTRY && !sourceObservation.value.isFile()) {
removeCopiedResourceIfOwned(targetPath, managedHomePath, entryName, sourcePath)

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.

🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Keep the fallback copy until replacement succeeds.

A source can become unreadable after Line 111 succeeds. For an owned AGENTS.md fallback copy, copiedFileContentsMatch then returns false, the refresh path removes targetPath, and cpSync can fail. The cleanup leaves no managed copy. Stage the replacement in a sibling temporary path and replace the target only after the source copy succeeds. Add a test that denies the source after observation.

…y unreadable (STA-4737)

Three modules shared by the host and WSL Codex lanes decided a file was absent
from a read that had only failed, and then wrote over it or removed it.

- `codex-config-mirror`: `existsSync` on the RUNTIME config.toml returned false
  for a locked file exactly as for an absent one, so the mirror took the
  "seed a fresh runtime config" branch and replaced the user's config wholesale.
- `config-settings-promotion`: an unreadable ~/.codex/config.toml counted as
  having no promoted settings, and the write path then rebuilt the user's
  canonical Codex config from Orca's runtime copy.
- `codex-home-paths`: both delete branches in `linkSystemCodexResource` remove
  Orca's mirrored copy because the system resource "is not there". `existsSync`
  and `systemResourceIsRegularFile`'s `catch { return false }` both reported
  that for a source nobody could read, so one denied read on ~/.codex/AGENTS.md
  removed the managed copy on the next launch.

`src/shared/definitive-filesystem-absence.ts` now owns the one errno allowlist —
ENOENT and ENOTDIR, with every other code including unrecognised ones treated as
indeterminate — and `host-codex-managed-home-ownership.ts` drops its private
copy rather than letting the two drift. `codex-path-observation.ts` builds the
three-valued observation on top of it.

The resource sync's two `existsSync`/`statSync` probes collapse into one
resolved stat, which answers reachability and regular-file-ness together and
closes the window between them.

`config-settings-promotion.ts` crossed its max-lines budget, so the write-target
resolution moves to its own module rather than taking a lint exemption.

Deliberately not here: the hook-service trust writes that run after a refused
mirror, and the promotion write target's own classification, which is
unreachable because it always resolves to the same file the read above already
refused. Both are noted in comments rather than half-built.
@brennanb2025
brennanb2025 force-pushed the brennanb2025/sta-4737-codex-shared-observations branch 4 times, most recently from a8cf83e to e721074 Compare August 18, 2026 09:26
@brennanb2025
brennanb2025 force-pushed the brennanb2025/sta-4737-codex-shared-observations branch from e721074 to df93a7d Compare August 18, 2026 09:28
@brennanb2025

brennanb2025 commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Verdict

Pass after fixes. The review found real data-loss races in the shared Codex filesystem paths; they are fixed in df93a7d4a5, pushed to the PR head, and covered by focused tests. No Electron/UI/mobile validation applies: these are main-process config/resource paths.

Findings and changes

  • src/main/codex/codex-config-mirror.ts:119-149 and :167-190: the legacy retained-pane mirror and managed-home mirror treated a failed read as absence via existsSync/default reads. A locked runtime config could therefore be replaced by a fresh system copy. Both paths now use a three-state observation and refuse indeterminate reads; the legacy path also reuses the observed bytes.
  • src/main/codex/config-settings-promotion.ts:111-117, :227-235, and :271-275: an unreadable system config was treated as an empty/missing config, allowing promotion to reconstruct and overwrite the canonical file. Indeterminate observations now throw through the existing stall/retry path; only ENOENT/ENOTDIR are absence.
  • src/main/codex/codex-home-paths.ts:111-154, :191-206, and :222-234: resource sync could delete an owned AGENTS.md fallback when source metadata/content reads or target lstat failed. Source reachability is now tri-state; target lstat failures refuse refresh; content comparison distinguishes match/different/indeterminate and never deletes on indeterminate.
  • src/shared/definitive-filesystem-absence.ts:14-16 is the shared errno allowlist, and src/main/codex-accounts/host-codex-managed-home-ownership.ts:3 uses it so the account-selection lane and config/resource lanes cannot drift.

Sweep / clean

  • Full src/main/codex + src/main/codex-accounts suites: 101 files, 1,073 passed, 6 skipped.
  • pnpm typecheck: clean for node, CLI, and web projects.
  • node config/scripts/check-changed-code-quality.mjs: 0 native, type-aware, and React Doctor findings.
  • The denial rig faults read/stat/lstat/open/copy coherently and includes source-denied, source-read-after-stat failure, target-denied, runtime-config, system-config, legacy-mirror, genuine-absence, and readable-seeding cases.
  • Swept sibling consumers/callers, including the retained-pane legacy mirror entry point and the host managed-home ownership predicate; no additional action-authorizing collapse was left in the affected paths.
  • No performance issue: this removes redundant probes/re-reads and adds no polling, retry loop, or UI work.
  • Deliberately left alone: the promotion write-target indeterminate arm remains a documented backstop because the target resolves to the same file already refused by the earlier read; the WSL writeRuntimeAuthAtPath call sites remain scoped to STA-4606 lane bookkeeping; the sibling fileContentsMatchExpected !existsSync guard remains unchanged because the guarded writer re-checks the real file and the guard is unreachable in the exercised path. The shared absence predicate remains byte-identical across the sibling PRs to avoid merge conflicts.

Mutation results

The per-module mutation matrix was checked against named tests:

  • Reverting config-settings-promotion.ts made “leaves a denied system config exactly as it found it” fail.
  • Reverting codex-home-paths.ts made “keeps the mirrored copy when the source is denied” fail.
  • Reverting codex-config-mirror.ts made “refuses its own write when the runtime config is denied after promotion succeeded” fail (the promotion stub is required to reach the mirror’s own guard).
  • Reverting all three made those four fix-driving tests fail, while the three original anchor tests stayed green: “still seeds a runtime config that is genuinely absent”, “still promotes into a readable system config”, and “still removes the mirrored copy when the source is genuinely gone.”
  • The added post-review cases for legacy mirror, source-read-after-stat, and target-denied behavior all pass in the focused suite; they are additional regression coverage for the newly found races.

@brennanb2025
brennanb2025 merged commit 0b80a77 into main Aug 18, 2026
46 checks passed
paidaxingyo666 pushed a commit to paidaxingyo666/Manta that referenced this pull request Aug 21, 2026
…y unreadable (STA-4737) (stablyai#15287)

* fix(codex): stop overwriting and deleting Codex files that were merely unreadable (STA-4737)

Three modules shared by the host and WSL Codex lanes decided a file was absent
from a read that had only failed, and then wrote over it or removed it.

- `codex-config-mirror`: `existsSync` on the RUNTIME config.toml returned false
  for a locked file exactly as for an absent one, so the mirror took the
  "seed a fresh runtime config" branch and replaced the user's config wholesale.
- `config-settings-promotion`: an unreadable ~/.codex/config.toml counted as
  having no promoted settings, and the write path then rebuilt the user's
  canonical Codex config from Orca's runtime copy.
- `codex-home-paths`: both delete branches in `linkSystemCodexResource` remove
  Orca's mirrored copy because the system resource "is not there". `existsSync`
  and `systemResourceIsRegularFile`'s `catch { return false }` both reported
  that for a source nobody could read, so one denied read on ~/.codex/AGENTS.md
  removed the managed copy on the next launch.

`src/shared/definitive-filesystem-absence.ts` now owns the one errno allowlist —
ENOENT and ENOTDIR, with every other code including unrecognised ones treated as
indeterminate — and `host-codex-managed-home-ownership.ts` drops its private
copy rather than letting the two drift. `codex-path-observation.ts` builds the
three-valued observation on top of it.

The resource sync's two `existsSync`/`statSync` probes collapse into one
resolved stat, which answers reachability and regular-file-ness together and
closes the window between them.

`config-settings-promotion.ts` crossed its max-lines budget, so the write-target
resolution moves to its own module rather than taking a lint exemption.

Deliberately not here: the hook-service trust writes that run after a refused
mirror, and the promotion write target's own classification, which is
unreachable because it always resolves to the same file the read above already
refused. Both are noted in comments rather than half-built.

* fix(codex): preserve resource copies on indeterminate reads
dallascrilley pushed a commit to dallascrilley/orca that referenced this pull request Aug 27, 2026
…y unreadable (STA-4737) (stablyai#15287)

* fix(codex): stop overwriting and deleting Codex files that were merely unreadable (STA-4737)

Three modules shared by the host and WSL Codex lanes decided a file was absent
from a read that had only failed, and then wrote over it or removed it.

- `codex-config-mirror`: `existsSync` on the RUNTIME config.toml returned false
  for a locked file exactly as for an absent one, so the mirror took the
  "seed a fresh runtime config" branch and replaced the user's config wholesale.
- `config-settings-promotion`: an unreadable ~/.codex/config.toml counted as
  having no promoted settings, and the write path then rebuilt the user's
  canonical Codex config from Orca's runtime copy.
- `codex-home-paths`: both delete branches in `linkSystemCodexResource` remove
  Orca's mirrored copy because the system resource "is not there". `existsSync`
  and `systemResourceIsRegularFile`'s `catch { return false }` both reported
  that for a source nobody could read, so one denied read on ~/.codex/AGENTS.md
  removed the managed copy on the next launch.

`src/shared/definitive-filesystem-absence.ts` now owns the one errno allowlist —
ENOENT and ENOTDIR, with every other code including unrecognised ones treated as
indeterminate — and `host-codex-managed-home-ownership.ts` drops its private
copy rather than letting the two drift. `codex-path-observation.ts` builds the
three-valued observation on top of it.

The resource sync's two `existsSync`/`statSync` probes collapse into one
resolved stat, which answers reachability and regular-file-ness together and
closes the window between them.

`config-settings-promotion.ts` crossed its max-lines budget, so the write-target
resolution moves to its own module rather than taking a lint exemption.

Deliberately not here: the hook-service trust writes that run after a refused
mirror, and the promotion write target's own classification, which is
unreachable because it always resolves to the same file the read above already
refused. Both are noted in comments rather than half-built.

* fix(codex): preserve resource copies on indeterminate reads
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…y unreadable (STA-4737) (stablyai#15287)

* fix(codex): stop overwriting and deleting Codex files that were merely unreadable (STA-4737)

Three modules shared by the host and WSL Codex lanes decided a file was absent
from a read that had only failed, and then wrote over it or removed it.

- `codex-config-mirror`: `existsSync` on the RUNTIME config.toml returned false
  for a locked file exactly as for an absent one, so the mirror took the
  "seed a fresh runtime config" branch and replaced the user's config wholesale.
- `config-settings-promotion`: an unreadable ~/.codex/config.toml counted as
  having no promoted settings, and the write path then rebuilt the user's
  canonical Codex config from Orca's runtime copy.
- `codex-home-paths`: both delete branches in `linkSystemCodexResource` remove
  Orca's mirrored copy because the system resource "is not there". `existsSync`
  and `systemResourceIsRegularFile`'s `catch { return false }` both reported
  that for a source nobody could read, so one denied read on ~/.codex/AGENTS.md
  removed the managed copy on the next launch.

`src/shared/definitive-filesystem-absence.ts` now owns the one errno allowlist —
ENOENT and ENOTDIR, with every other code including unrecognised ones treated as
indeterminate — and `host-codex-managed-home-ownership.ts` drops its private
copy rather than letting the two drift. `codex-path-observation.ts` builds the
three-valued observation on top of it.

The resource sync's two `existsSync`/`statSync` probes collapse into one
resolved stat, which answers reachability and regular-file-ness together and
closes the window between them.

`config-settings-promotion.ts` crossed its max-lines budget, so the write-target
resolution moves to its own module rather than taking a lint exemption.

Deliberately not here: the hook-service trust writes that run after a refused
mirror, and the promotion write target's own classification, which is
unreachable because it always resolves to the same file the read above already
refused. Both are noted in comments rather than half-built.

* fix(codex): preserve resource copies on indeterminate reads
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