Skip to content

feat(codex): one-time backfill of managed-home sessions into the real Codex home - #8802

Closed
brennanb2025 wants to merge 6 commits into
mainfrom
brennanb2025/codex-session-backfill
Closed

brennanb2025 wants to merge 6 commits into
mainfrom
brennanb2025/codex-session-backfill

Conversation

@brennanb2025

Copy link
Copy Markdown
Contributor

Summary

One-time, non-destructive backfill of Codex session rollout files from Orca's managed runtime home into the user's real ~/.codex/sessions/YYYY/MM/DD/ tree.

Codex sessions launched inside Orca currently land only in the Orca-managed CODEX_HOME, so the user's own codex resume picker and Codex app history never see them (#4444, #8612). This PR makes the historical backlog visible in the provider-native home while changing nothing else about where new sessions are written. It complements the account-ownership direction in #8606 (credit to @jellychoco for the routing direction): once system-default launches move to the native home, this backfill is what keeps pre-existing Orca history discoverable there.

Behavior

  • Walks <userData>/codex-runtime-home/home/sessions/** incrementally (same batched walker the existing system→managed bridge uses) and places each rollout at the identical relative path under the real Codex home. The codex resume picker enumerates and parses this exact layout.
  • Hardlink first so resume sees one physical JSONL log; copy fallback (COPYFILE_EXCL) for cross-volume homes.
  • Never overwrites, deletes, or moves anything in either home: an existing target path (including a broken symlink) is always skipped, and a target appearing mid-run is caught by EEXIST handling.
  • Symlinked managed entries (bridge-created links that already point into the user's own home) are not materialized.
  • Once per host behind a versioned marker (<userData>/codex-session-backfill/backfill-complete.json). Per-file failures leave the marker unset so the next startup retries; the retry is cheap because everything already placed is skipped. Runs deferred (15s after service init) so startup and first PTY spawns never compete with the tree walk.
  • Audit log (<userData>/codex-session-backfill/audit.jsonl): one JSONL record per hardlink/copy/failure plus a per-run summary with skip counts.
  • Honors the existing custom Codex session source home override (users who run Codex with their own CODEX_HOME get history where their codex resume actually looks).
  • The existing system→managed session bridge is unchanged; the two directions compose safely (both are skip-existing and EEXIST-tolerant).

Not in this PR (follow-ups)

  • WSL: managed WSL runtime homes are distro-local; the backfill needs an in-distro hardlink variant (mirroring wsl-codex-session-bridge.ts). Host (macOS/Linux/Windows) only for now.
  • Sessions created after this backfill while launches still target the managed home keep flowing through the existing bridge; they become native-home sessions with the system-default routing work (Use native CODEX_HOME for system-default Codex without losing Orca hooks #8606 direction).
  • Codex also maintains a sqlite thread index in its home; surfaces driven purely by that index get healed in a follow-up (thread/read warm-up per backfilled session).

Testing

Unit (src/main/codex/codex-session-backfill.test.ts, 11 tests, all green): layout-preserving hardlinks (inode equality), collision skip with byte-identical target preservation, broken-symlink target treated as taken, symlinked source not materialized, idempotent second run, copy fallback on EXDEV, per-file failure accounting without aborting, no target-tree creation when there is nothing to backfill, marker write/skip semantics, failure→no-marker→retry, custom source home override.

Live, fully sandboxed (dev build, isolated ORCA_DEV_USER_DATA_PATH + overridden HOME; the developer's real ~/.codex untouched):

  1. Managed home seeded with 3 rollouts (one colliding with a pre-existing user file of different content); real home seeded with the user's own session.
    • After startup: both managed-only rollouts hardlinked into the real home (stat shows identical inode), collision skipped with the user's variant preserved byte-exactly, user's own session untouched.
    • Audit log: 2 hardlink records + run-summary {scanned 3, linked 2, skipped 1, failed 0}; marker v1 written.
  2. Restart: app relaunched with a new managed rollout planted post-marker → marker honored, no re-walk, audit unchanged, new file not backfilled (by design; see follow-ups).
  3. Resume picker visibility (codex-cli 0.144.4 against the sandboxed home): a genuine-format rollout placed into the real home's sessions tree after the codex sqlite index already existed appears in the codex resume picker on next open (picker output captured: 1 / 2, previews shown). Since the backfill places byte-identical hardlinks of genuine rollout files, backfilled Orca sessions are discoverable the same way. (Minimal hand-crafted JSONL fixtures do not pass the picker's parse — visibility requires real rollout content, which real managed sessions have.)

tsc (node project) clean, oxlint clean on touched files.

…once per host

Orca-launched Codex sessions currently land only in the Orca-managed
runtime home, so the user's own `codex resume` picker and app history
never see them (#4444, #8612). Backfill the managed sessions tree into
the real ~/.codex/sessions/YYYY/MM/DD layout once per host:

- hardlink first (one physical rollout log), copy as the cross-volume
  fallback; existing target files are always skipped, nothing in either
  home is deleted or moved
- idempotent; per-file failures leave the completion marker unset so the
  next startup retries cheaply
- JSONL audit log of every link/copy/failure under
  <userData>/codex-session-backfill/
- honors the custom Codex session source home override, mirroring the
  existing system->managed bridge

WSL managed homes are distro-local and need an in-distro variant; that
is a follow-up.
@coderabbitai

coderabbitai Bot commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 7b5921ac-3d5f-4286-9112-0c35486f7ed1

📥 Commits

Reviewing files that changed from the base of the PR and between 6e4214a and 4b5ff26.

📒 Files selected for processing (3)
  • src/main/codex/codex-session-backfill-copy.ts
  • src/main/codex/codex-session-backfill.test.ts
  • src/main/codex/codex-session-backfill.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/main/codex/codex-session-backfill.test.ts
  • src/main/codex/codex-session-backfill.ts

📝 Walkthrough

Walkthrough

Adds a Codex session backfill utility that scans managed session JSONL files, hardlinks or copies them into the system Codex home, records audit data, and avoids overwriting existing entries. Completion markers, retry behavior, concurrency protection, custom home resolution, and startup scheduling are included. Tests cover filesystem behavior, failures, idempotency, marker handling, retries, and delayed application startup execution.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: a one-time backfill of managed-home Codex sessions into the real home.
Description check ✅ Passed The description is detailed and covers summary, behavior, testing, and follow-up notes, though several template sections are missing.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches
📝 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.

Use asynchronous, sequential filesystem operations for the one-time rollout backfill, and avoid repeated target-directory probes. Treat inaccessible managed session roots as retryable failures instead of writing a false completion marker.

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

🧹 Nitpick comments (1)
src/main/codex/codex-session-backfill.ts (1)

308-324: 🚀 Performance & Scalability | 🔵 Trivial | 💤 Low value

Prefer asynchronous readFile to avoid blocking the main thread.

To fully adhere to the PR objective of performing filesystem operations off the main thread, consider making this function async and using readFile from node:fs/promises instead of readFileSync.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 486195b5-249e-4874-8d4f-613fad3d9524

📥 Commits

Reviewing files that changed from the base of the PR and between b0b63c6 and 6e4214a.

📒 Files selected for processing (5)
  • src/main/codex/codex-session-backfill-audit.ts
  • src/main/codex/codex-session-backfill-copy.ts
  • src/main/codex/codex-session-backfill.test.ts
  • src/main/codex/codex-session-backfill.ts
  • src/main/codex/codex-session-file-listing.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/main/codex/codex-session-file-listing.ts
  • src/main/codex/codex-session-backfill.test.ts

On a real Codex home whose filesystem supports no hardlinks (exFAT/FAT,
some network mounts), the staged cross-volume copy was installed with a
non-atomic copyFile(..., COPYFILE_EXCL) straight into the final
rollout-*.jsonl name. An install interrupted mid-copy (app quit, crash,
ENOSPC during the deferred run) could strand a truncated rollout that the
next run then skips as already-present, defeating the staging design's own
guarantee that a failed copy never leaves a partial session behind.

Install the fully-staged copy with an atomic rename instead, guarded by an
existence re-check so it keeps the never-overwrite contract (and the rename
source is the same immutable managed rollout, so any clobber would be
byte-identical). Cover the no-hardlink-support target and an interrupted
install that must leave no partial in the user's sessions tree.
@brennanb2025

Copy link
Copy Markdown
Contributor Author

Live E2E validation — PASS (6/6)

Validated live against the real Electron app (branch head 4b5ff2658, codex-cli 0.144.4) using sandboxed app instances (HOME + ORCA_DEV_USER_DATA_PATH overridden, leaked env scrubbed). The real ~/.codex was never read or written (checksum-guarded before/after — unchanged; all sandboxes removed after the run).

  1. Hardlink backfill — PASS. Managed sessions copied into the sandbox real ~/.codex/sessions as hardlinks: matching source/target inodes, nlink=2, unchanged SHA-256.
  2. Collision safety — PASS. A pre-existing same-name file was skipped, not overwritten; original bytes/hash intact.
  3. Idempotency / marker — PASS. Marker + audit hashes/mtimes unchanged after restart; a session added post-marker was not re-backfilled.
  4. Atomic cross-volume fallback — PASS. UUID staging + atomic no-replace hardlink installation verified; hardlink-unsupported filesystems fail closed. Focused suite 18/18. (Note: the fallback uses atomic no-replace hardlink installation rather than a rename — behaviorally equivalent/safe, flagged for visibility.)
  5. Layout / indexing — PASS. codex 0.144.4 app-server confirmed the sandbox codexHome and returned both backfilled sessions from the correct YYYY/MM/DD paths.
  6. Brand-new user — PASS. Empty managed home → zero-count marker, no errors, clean runtime.

Automated live validation run as part of the pre-RC hardening sweep.

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Closing as superseded: this work landed on main via #9501 (real-home routing + self-contained multi-account homes) and shipped in v1.4.149 stable. Content verified present on main (module + wiring spot-checked).

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