Restore admission consults Codex's writer lock before starting a second writer
Codex keeps a kernel flock on $CODEX_HOME/thread-writer-locks/<thread>.lock for the life of the process that owns the thread. cmux's restore gates consulted only their own PID and hook-store scan, which misses a previous Codex still shutting down after quit and reopen. cmux then exec'd a second codex resume, and Codex opened read-only with "This conversation is open in another app / R to Retry".
Mechanism: one CodexWriterRestorePreflight (a non-blocking LOCK_EX|LOCK_NB probe on the exact lock file, account resolved through the same CodexHomeResolver the verification path uses, plus a bounded same-user descriptor scan to name the holder) backs three entrypoints. The app's agent.restore.admit RPC answers admitted:false, writer_lock_held:true, lock_path, retryable:true and adds live_owner_pid when one verified holder is found. The CLI runs the full-detail guard before the binding claim, and a cheap re-probe at the exec boundary immediately before execve, which runs regardless of the app's admission flag (an older app with a newer CLI is covered). The CLI retries a held lock through the first four busy delays (0.5 + 1 + 2 + 3 s) so a lingering previous Codex can release it, then reports the lock path and holder PID instead of starting a read-only writer. Nothing deletes a lock file or signals a holder.
Principled: it consults Codex's own truth instead of a second model of it, and the decision is shared by all three gates. Residual risk: the probe is point-in-time, so a writer starting between the probe and Codex's own lock acquisition still loses to Codex's lock (Codex stays the final authority, as before). The plan-time gates in DockSplitStore+SessionRestore and Workspace+AgentLifecycle are unchanged; the RPC and exec boundary are authoritative.
Commits: the first adds cmuxTests/CodexWriterLockAdmissionTests alone and fails on main (admission admits while a real flock is held); the second adds the fix, the ported package sources and tests, and the CLI guards. Package tests (CMUXAgentLaunch, 37 tests) pass locally with swift test.
The quit-side half (terminate and wait for agent children before the app exits) is a separate PR on issue-12805-codex-writer-lock-quit.
Lock inspection ported from austinywang's closed https://github.com/manaflow-ai/cmux/pull/11994, trimmed to restore admission (no Vault navigation).
Part of https://github.com/manaflow-ai/cmux/issues/12805
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.
Summary by cubic
Restore admission now probes Codex's own thread writer lock before starting a codex resume, so a previous Codex still shutting down no longer causes the new copy to open read-only. A held lock is retried for ~6.5 s, then reported with its path and holder PID instead of launching.
Restore gate
agent.restore.admitreturnswriter_lock_held:truewith the lock path and best-effort holder PID when a unique verified owner exists.- The CLI runs the guard before the binding claim and re-probes at the exec boundary, covering older apps that don't implement the RPC.
- Remote-provider sessions skip the local lock check; a lock that can't be inspected safely fails closed.
- The probe is advisory and point-in-time; nothing deletes a lock file or signals a holder.
Also in this PR
- Re-applies the mobile pairing preparation deadline dropped by #12754 (fixes #12827).
- Fixes two cmuxTests compile errors so the test bundle builds on
main.
Summary by CodeRabbit
-
New Features
- Added protection against restoring a Codex session while it is already active elsewhere.
- Restore checks now identify active sessions and, when available, show the lock location and responsible process.
- Remote Codex sessions continue without local lock checks.
- Legacy Codex resume commands receive safer validation before restoration.
-
Bug Fixes
- Prevented restores from launching in read-only or conflicting states.
- Added bounded retries when a Codex session is temporarily busy.
- Improved handling of stale or unavailable session ownership information.
-
Localization
- Added translated messages for Codex restore conflicts and unavailable checks.