Fix Codex writer ownership during quit and restore
Summary
A Codex child can outlive cmux and retain its writer lock after quit. Reopening then starts codex resume against that lock and opens the conversation read-only. This change saves the session before terminating validated local Codex children, waits for their exit, and checks the writer lock again at the CLI's final exec boundary.
Closes #12805. Issue: https://github.com/manaflow-ai/cmux/issues/12805
The branch incorporates Lawrence's quit coordinator from #12818 and the shared lock-probe direction from #12815. Current main already supplies event-driven restore admission and Vault recovery; this patch builds on that implementation. The split PRs remain untouched, and the coordination plan is on the issue thread.
- Quit deferral depends on local terminal ownership, so an empty cached agent index cannot skip the fresh scan. Signals require a complete Codex process scope, matching process generation, this app's ancestry, current panel ownership, and the existing hibernation process-group/TTY validation. The canonical hibernation path owns SIGTERM, bounded exit waits, and validated SIGKILL escalation.
- The shared lock inspector uses nonblocking shared probes, which coexist with other probes while detecting Codex's exclusive writer. Admission and CLI diagnostics report sanitized executable/PID candidates with the exact lock inode open. Those observations never authorize signaling or focusing another process.
- The final CLI guard checks the effective account immediately before exec, including legacy records and older apps that omit
agent_restore_admission_supported. Current apps retain their event-driven recovery. No new socket fields or relay permissions are introduced; the existing admission contract remains available to #12775.
Testing
Validation after merging origin/main (9e61fc283c8) in 1bf12e0391a:
- Passed: Swift file-length budget, Swift 6.0 syntax checks for all 20 changed Swift files, test-target wiring (1,052 files), project normalization, localization catalog validation, package-resolution policy, workspace package groups, and whitespace checks. Neither budget TSV is changed. The two new recovery strings were audited in all 20 catalog locales, including English and Japanese;
a72944d88fbcompletes the broader catalog coverage requested in review. - The reported reproduction is “after quit and reopen.” A deterministic PTY/flock experiment reproduced the underlying lifetime bug: the child acknowledged SIGHUP and retained the lock after PTY closure; SIGTERM plus waiting for exit released it. Evidence is retained at
artifacts/issue-12805/repro/pty-lock-lifetime.json. This is kernel-lifetime evidence, not a completed app quit/reopen test. - Regression-first commit
9d93ec9cc74adds valid held-then-released CLI controls for structured/legacy records and advertised/unadvertised admission, acknowledged PTY signals, PID-generation-safe fixture cleanup, and concurrent shared probes.62cb9aa0603adds the corresponding fixes plus admission/recovery, ownership, and sanitized diagnostic tests. New app tests are wired into the Xcode target. - Hosted
swift-package-testspassed on the latest head.CMUXAgentLaunchhas 416 tests in 60 suites, including both writer-lock suites (job). - The app and all test targets compiled successfully at
1bf12e0391a, with the Swift warning budget respected and no diagnostics in this patch's files (compile). - All 8 focused app-host tests in 4 suites passed at
1bf12e0391a, including PTY/SIGHUP lock release, SIGKILL escalation, stale-generation rejection, local ownership, four held/released CLI cases, and native held-lock admission/recovery (execution). The two earlier fixture failures were fixed in740d9e994d8: reset inherited signal masks and preserve CI's relocated package-loader paths. - The CLI product lane passed all 113 tests in 22 suites, including
codexInstalledStopUsesQueueAdmissionunchanged (execution). Requiredtestsandci-statusare green at1bf12e0391a. - Final controller job
c9434b9203bab5967f97025ais queued fora72944d88fb790d79303a1c555a235d8372680e3with tagissue-12805-restored-codex-readonly. The healthy worker has about 218 GiB free, below the explicit 250 GiB task floor. Submission receipt:artifacts/fleet/a72944d88fb790d79303a1c555a235d8372680e3-submit.json. Real quit/reopen plus external-owner dogfood remains pending; no final app/HQ opener exists and no local app build or XCUITest was run.
Trade-offs
-
Any local terminal causes async quit cleanup, adding a fresh-index turn even when it contains no Codex process. Only validated local Codex scopes are signaled, keeping this fix scoped to the reported provider.
-
Missing or changing process evidence leaves that process untouched. An unresponsive owned Codex can receive SIGKILL after 2.5 seconds, so its unflushed in-memory output cannot be guaranteed. Grace and post-kill waits total 3.5 seconds, excluding validation; the existing hard termination watchdog bounds a stuck cleanup. Normal completion owns the AppKit reply, avoiding a canceled quit followed by a late success reply. A survivor can still hold the lock, so restore also needs the independent lock gate.
-
macOS does not expose an authoritative flock-owner PID through this probe. An open descriptor is only an owner candidate, so the message says “Possible owners.” Discovery is best effort and bounded to two seconds; unknown owners remain protected.
-
The CLI probe is advisory: Codex must make the atomic final acquisition, and another writer can win after the probe. If an older app cannot provide event-driven admission, a held or unverifiable lock produces a retryable error instead of launching a known read-only restore. cmux never removes the lock or kills a process inferred from it.
-
The quit adapter stays beside the app-owned hibernation lifecycle and reuses its signal/exit authority. A whole-domain hibernation package extraction is deferred; the reusable app/CLI provider logic lives in
CMUXAgentLaunch.
Checklist
- Behavior-level regression tests added and wired.
- New user-facing strings localized and audited in all 20 catalog locales.
- Native tests passed at
1bf12e0391a; the follow-up changes translations only, and current-head CI is rerunning. - Final tagged build and real quit/reopen plus external-owner dogfood recorded.
- Latest bot review bodies and threads addressed; final audit rechecked against HEAD.