Skip to content

fix(codex): widen the stale-pane sweep retry ladder past the Windows worst case - #10853

Merged
brennanb2025 merged 2 commits into
brennanb2025/fix-codex-account-switchfrom
brennanb2025/codex-sweep-ladder-headroom
Jul 27, 2026
Merged

brennanb2025 merged 2 commits into
brennanb2025/fix-codex-account-switchfrom
brennanb2025/codex-sweep-ladder-headroom

Conversation

@brennanb2025

Copy link
Copy Markdown
Contributor

Stacked on #10770. Ladder only — no change to the bind signal, the eligibility predicate, or the queue mechanics.

The problem

Live Windows 11 validation of #10770 measured store-visible PTY bind → notice raised:

run bind → notice
1 5,140 ms
2 3,959 ms
3 4,064 ms

The ladder was SWEEP_ATTEMPT_DELAYS_MS = [300, 1500, 4000], i.e. attempts at 300ms / 1800ms / 5800ms from bind. On Windows the final rung is what produced the card in all three runs — rungs 1 and 2 never won. macOS by comparison resolved on the first rung in ~0.66s, leaving ~3.3s of ladder unused.

So Windows consumes the entire ladder with essentially zero margin. A slower box, more panes, or a colder daemon reattach and the last attempt also misses — at which point there is no further rung, queueNextRung drops the pane, and the card never appears. The user stays on the old account unaware, which is precisely the bug #10770 exists to fix. The failure mode when the ladder exhausts is "no card", never a dead keyboard, so this is a coverage problem, not a safety one.

The change

- const SWEEP_ATTEMPT_DELAYS_MS = [300, 1500, 4000] as const
+ const SWEEP_ATTEMPT_DELAYS_MS = [300, 1500, 4000, 10_000, 20_000] as const

Cumulative attempt times from bind:

rung old new
1 300 ms 300 ms
2 1.8 s 1.8 s
3 5.8 s 5.8 s
4 — 15.8 s
5 — 35.8 s

Justification against the measurements:

  • Early rungs untouched, so macOS still resolves in <1s and the startup-burst coalescing is unchanged. This is a pure tail extension — no existing behaviour moves.
  • The tail clears the observed worst case by a wide margin. The winning rung on Windows was 5.8s; the new rungs sit at ~2.7× and ~6.2× that. Two extra rungs is enough because the growth is geometric — matching the existing ~2.5× shape — so a large time window costs few attempts and therefore few IPC round-trips.
  • Still bounded. Five attempts, then queueNextRung finds undefined and deletes the pane's attempt state. It is not interval-based and cannot become a polling loop, which the constant's comment calls out explicitly.
  • A late card is strictly better than no card. The pane is still stale at t=35s, and the notice is dismissible.

Per-PTY rungs stay per-PTY: queue/takeDuePtyIds are untouched, so the rung-collapse fix from the earlier review round is not regressed. The sweep still stops for a notified pane (notifiedPtyIds) and for a dismissed one (forgetStaleCodexPanes drops the launch record, so listStalePanes stops reporting it) — a longer tail cannot resurrect either.

Verification

Mutation-proof of the new tail. Reverting the constant to [300, 1500, 4000] with the tests unchanged fails exactly the two tests that assert the tail:

FAIL  'still sweeps a pane whose reattach outlives the first three rungs'
      → codexRestartNoticeByPtyId['pty-1'] is undefined; inspect count 3, expected 4
FAIL  'gives up after a bounded ladder instead of polling forever'
      → expected inspectProcess called 5 times, got 3
Tests  2 failed | 13 passed

The new test drives a pane inconclusive through the first three rungs and asserts the notice is still raised on the fourth — the exact silent-failure scenario the Windows data points at. The bounded-ladder test now advances 120s (well past the 35.8s ceiling) so it pins the ceiling rather than the clock.

Per-PTY independence test passes unchanged — spends a pane its full retry ladder even when another pane binds later still asserts 2 then 3 inspects for pty-1 at t=3310/6310, which the longer ladder does not perturb. Only its stale "its 3 attempts" comment was reworded.

Gates, all green on this branch:

  • npm run typecheck (node + tc.cli + tc.web) — exit 0, 0 errors
  • npx oxlint — exit 0, no findings in touched files
  • check:max-lines-ratchet — OK, no new bypasses
  • codex-stale-pane-sweep.test.ts + codex-session-restart.test.ts — 33/33
  • pty-connection.test.ts (the sweep's only caller) — 484/484

Known-noise on the validation box, unrelated to this change: Node 26 vs the repo-pinned 24 fails ~41 renderer tests (browser-pane/markup, right-sidebar, sidebar) on clean checkouts, and verify:localization-coverage exits 1 on 6 pre-existing Ghostty strings.

…worst case

Live Windows 11 validation of #10770 measured bind -> notice at 5140ms, 3959ms
and 4064ms across three runs: the third and final rung is what raised the card
every time, with essentially zero margin. macOS resolved on the first rung and
left ~3.3s unused. A slower box, more panes, or a colder daemon reattach pushes
past the last rung, and the sweep then has nothing left to fire — no card, and
the pane silently keeps running the old account, which is the exact bug #10770
exists to fix.

Adds two tail rungs, so the ladder reaches 15.8s and 35.8s from bind instead of
ending at 5.8s. The fast early rungs are untouched, so macOS still resolves in
under a second. Still bounded: five attempts and then the pane is dropped, never
an interval or a poll.
The bounded-ladder test asserted only that inspectProcess ran 5 times within
a 120s window. A sixth rung at 200s passes that unchanged — the count cannot
tell "the ladder terminated" from "the next rung moved past the window", which
is exactly the polling-loop property the test is named for.

Assert the fake-timer queue is empty instead. Mutation-proved: passes as-is,
and a sixth rung at 200_000 fails it with `expected 1 to be +0` while leaving
the call-count assertion green.
@brennanb2025

Copy link
Copy Markdown
Contributor Author

Review: LAND-READY

Reviewed against the correct base (brennanb2025/fix-codex-account-switch), not main. Verdict on the ladder itself: clean, no defects. I pushed one test hardening (944b7ce) and opened a separate PR for an unrelated bug this PR amplifies.

The five things I was asked to judge

1. Cumulative arithmetic — CONFIRMED. Not just read; measured with an instrumented copy of the module. Rungs fire at 300ms / 1.8s / 5.8s / 15.8s / 35.8s, with attemptsByPtyId stepping 1..5 and terminating on SWEEP_ATTEMPT_DELAYS_MS[5] === undefined. The PR body's table is exact.

2. Still bounded — CONFIRMED. Deterministic across 10/10 runs. The attempt counter, not the clock, is the bound, so the catch path (:140-143), the early-fire re-arm (:125-128, which cannot spin because takeDuePtyIds returns [] only when every entry is strictly in the future), and a pane whose scan never returns all terminate.

3. Per-PTY rung independence — NOT regressed. queue/takeDuePtyIds/armForEarliestDue are untouched by the diff. The longer ladder cannot reintroduce the collapse by another route either: a pane on rung 5 has attemptsByPtyId set, so takeDuePtyIds' !attemptsByPtyId.has(ptyId) early-fold branch skips it. Measured with pty-2 binding at t=16s while pty-1 sat on dueAt=35800 — pty-1's ladder came through undamaged.

4. The 35.8s tail is safe against stale targets.

  • Closed pane: scanCodexPanes walks ptyIdsByTabId, so a closed pane yields no ptyIds — the remaining rungs cost zero IPC and no notice is reachable.
  • Dismissed: the ladder is already over when the notice is raised (scan?.notified → notifiedPtyIds.add + attemptsByPtyId.delete). For the in-flight race, markCodexRestartNotices preserves dismissed when the labels match. A late rung cannot re-raise a dismissal.
  • ptyId reuse: not possible — ids are UUID-derived and teardown drops the record.
  • Manual in-shell codex restart at t=20s: the t=35.8s notice is correct, not bogus — CODEX_HOME is baked at spawn, so re-running codex in the same shell is still the old account.

5. Mutation proof — REPRODUCED exactly. Reverting to [300, 1500, 4000] fails precisely the two tail tests the description claims, no more and no fewer. Also probed the boundary properly: moving the 4th rung 10_000 → 11_000 fails the new test, which proves advance(5_800) then +10_000 lands exactly on 15.8s rather than passing on slack.

One thing I changed (944b7ce, test-only)

gives up after a bounded ladder instead of polling forever asserted only "5 inspects within 120s". That does not actually prove termination — a 6th rung at 200s passes it unchanged, because the count cannot distinguish "the ladder ended" from "the next rung moved past the window", which is the exact property the test is named for. Added expect(vi.getTimerCount()).toBe(0). Mutation-proved: passes as-is, and a 6th rung at 200_000 fails it with expected 1 to be +0 while the call-count assertion stays green.

On the rebind interaction — I was wrong, and the PR is better than I thought

I flagged that a mid-ladder rebind of the same ptyId consumes a rung rather than extending the ladder (queue's Math.min pulls the due time forward while attemptsByPtyId is untouched), collapsing 35.8s to 26.3s. That is real but pre-existing, and the widening strictly improves it. Measured both sides on identical rebind plans:

plan 5-rung (this PR) 3-rung (base)
no rebind 300 1800 5800 15800 35800 300 1800 5800
rebind@6000 300 1800 5800 6300 26300 300 1800 5800 6300 7800 11800

At rebind@6000 the base has already exhausted, so the rebind starts a fresh ladder: more IPC, and it still stops earlier. There is no rebind sequence where the widening is worse. It also doesn't fire on the path this PR targets — the cold Windows reattach mounts the pane once, connectPanePty runs once, one notify.

⚠️ Separate bug this PR amplifies — #10863, fix in #10870

Not a defect in this PR, and deliberately not folded into it, but it should land alongside.

#10863 is filed as "the card steals focus from the pane it covers". The real scope is wider and that matters for severity. CodexRestartChip computes its focus scope as root.parentElement, and Terminal.tsx:2146/:2394 mount the card as a sibling of TabGroupSplitLayout — so that scope is the entire worktree surface, not one pane. shouldFocusMobileDriverAction's xterm branch then matches any terminal in the worktree. Verified against the helper: stealsFocusFromSiblingPane: true, stealsFocusFromOtherWorktree: false.

So a notice for pane A yanks focus out of healthy pane B — whose input is not blocked and which the user may be typing in — onto the default Restart button. handleRestart queues restarts for every stale pty in the worktree, so one stray Space/Enter destroys those sessions.

Why it belongs with this PR: this change moves the first-notice window from ≤5.8s after bind to ≤35.8s. At 5.8s nobody is typing yet; at 35.8s they plausibly are. It turns a rare race into a likely one. Fix is in #10870.

Gates (local)

codex-stale-pane-sweep + codex-session-restart 33/33 · pty-connection 484/484 · typecheck exit 0 · oxlint clean · max-lines ratchet OK. Note this branch is one commit behind its base (missing #10804), which is harmless — #10804 doesn't touch the sweep and the PR is MERGEABLE.

Not verified: the live Windows 11 field numbers (3,959 / 4,064 / 5,140 ms) are taken on the author's word — no Windows or SSH host was run. The 10s/20s sizing rests on that premise.

@brennanb2025
brennanb2025 merged commit c460de6 into brennanb2025/fix-codex-account-switch Jul 27, 2026
1 check passed
@brennanb2025

Copy link
Copy Markdown
Contributor Author

Post-merge Windows validation: the widening was load-bearing, not precautionary

Validated on Windows 11 at c460de6089 (this change merged). The measurements split into two
regimes, and only one of them matched the numbers this PR was sized from.

Warm renderer restart (daemon already up, panes rebind fast) — the method used for the original
sizing:

Run Bind Card Bind → card
1 6,563 ms 10,785 ms 4,221 ms
2 6,166 ms 9,859 ms 3,693 ms
3 6,132 ms 9,857 ms 3,724 ms

Essentially unchanged from the pre-fix numbers (3,959 / 4,064 / 5,140 ms). Still resolved by rung 3
(5.8s cumulative).

Cold full restart (quit Orca entirely; daemon reattaches ~14 panes):

Run Card raised at
1 27,693 ms after renderer start
2 27,822 ms after renderer start

Bind is consistently ~6.1–6.6s, so bind → card is ~21 seconds — past rung 3 (5.8s), resolved by
rung 4 (15.8s), plus ~5–6s for the eligibility scan across the tab set.

What that means

Under the old three-rung ladder the cold-restart card would not have appeared at all. This change
should not be read as over-provisioning; rung 4 is doing real work on the exact path #10770 exists to
fix. The only remaining cushion is rung 5 at 35.8s — roughly one more rung.

The original 3,959–5,140 ms figures were all warm-reload measurements, which is why they looked
comfortable. The cold path is ~5× slower.

Caveats

  • In the cold runs the pane was already bound before the poller attached (~22–23s), so bind is
    inferred from the warm runs
    ; the card timestamps (27.7s / 27.8s) are directly measured.
  • A cold-parked pane never binds at all, so it never gets a card. A first attempt using a
    backgrounded pane correctly produced nothing (terminal_gone). Not a defect in this change, but it
    means the sweep only ever covers panes that actually bind — worth knowing before anyone sizes the
    ladder again.
  • No delayed false positive: a pane running vim after exiting Codex got no card and still none 45s
    later, covering rung 4 and part of rung 5.

brennanb2025 added a commit that referenced this pull request Jul 28, 2026
…count switch (#10770)

Fixes #10757. Switching Codex accounts broke three ways, all rooted in the
self-contained per-account CODEX_HOME from #9501.

HISTORY DISAPPEARED. Codex's own /resume picker only lists rollouts under the
launch CODEX_HOME, and nothing bridged history into a per-account home — only
the AI Vault's discovery scan knew about the other homes. Every other
Orca-visible home's rollouts are now hardlinked in, on selection and again at
launch, so one physical log is listed everywhere.

THE RESTART PANEL STUCK. A queued restart was only drained by a mounted
TerminalPane, but the prompt covered every stale pane in the worktree including
parked and cold-deferred tabs. Requesting a restart now answers the prompt
immediately while the pane keeps its pending restart, and a pane drains it when
its reconnected PTY binds.

PANES STAYED ON THE OLD ACCOUNT. CODEX_HOME is fixed in a shell's environment at
spawn and the daemon keeps those shells alive across app restarts, while the
restart notices are renderer state and are discarded. Each PTY's launch account
is now recorded on disk and compared against the current selection at startup.

Also merged in: #10802 (a dismissed notice no longer kills the pane's keyboard),
#10803 (the sweep arms on real PTY binds, and launcher Codex panes are no longer
filtered out by Windows deepest-process reporting), #10804 (a resume-pinned pane
now says which account it is on), #10870 (the restart card no longer parks focus
on its destructive Restart button), #10853 (the retry ladder is widened past the
Windows worst case).

Six independent reviews found real defects in every original PR, several of them
dead-keyboard bugs and three introduced by the fix for another defect in the same
loop. Live QA on macOS covered every PR; Windows was validated three times.

WINDOWS: pass 1 found two defects that made the stale-account fix a no-op there
(the sweep fired before any PTY was bound and never retried; launcher panes were
filtered out). Pass 3 at the merged head: the prompt appears on its own after a
restart — warm ~3.7-4.2s, cold ~21s needing rung 4, so #10853's widening was
load-bearing rather than precautionary; an ordinary sentence typed into a healthy
pane while another pane's card is up reaches that pane and kills nothing; a pane
running vim after exiting Codex gets no card, still none 45s later. auth.json
byte-identical across every pass.

KNOWN GAPS, stated rather than implied: #10804 is unverified on Windows
(auto-resume could not be manufactured there); cross-volume Windows is untested
and expected to yield no bridged history (EXDEV, and Codex ignores symlinked
rollouts); a cold-parked pane never binds so the sweep never covers it; the
subagent-deepest launcher shape could not be reproduced on Windows, so that
branch is fixture-verified only; WSL passed isolation but the resume mechanism is
host-lane only. A host-account switch also marks and mutes live SSH remote panes
— confirmed pre-existing on main by two independent QA runs — tracked separately
in #10992. Related pre-existing defect filed as #10863.
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…count switch (stablyai#10770)

Fixes stablyai#10757. Switching Codex accounts broke three ways, all rooted in the
self-contained per-account CODEX_HOME from stablyai#9501.

HISTORY DISAPPEARED. Codex's own /resume picker only lists rollouts under the
launch CODEX_HOME, and nothing bridged history into a per-account home — only
the AI Vault's discovery scan knew about the other homes. Every other
Orca-visible home's rollouts are now hardlinked in, on selection and again at
launch, so one physical log is listed everywhere.

THE RESTART PANEL STUCK. A queued restart was only drained by a mounted
TerminalPane, but the prompt covered every stale pane in the worktree including
parked and cold-deferred tabs. Requesting a restart now answers the prompt
immediately while the pane keeps its pending restart, and a pane drains it when
its reconnected PTY binds.

PANES STAYED ON THE OLD ACCOUNT. CODEX_HOME is fixed in a shell's environment at
spawn and the daemon keeps those shells alive across app restarts, while the
restart notices are renderer state and are discarded. Each PTY's launch account
is now recorded on disk and compared against the current selection at startup.

Also merged in: stablyai#10802 (a dismissed notice no longer kills the pane's keyboard),
stablyai#10803 (the sweep arms on real PTY binds, and launcher Codex panes are no longer
filtered out by Windows deepest-process reporting), stablyai#10804 (a resume-pinned pane
now says which account it is on), stablyai#10870 (the restart card no longer parks focus
on its destructive Restart button), stablyai#10853 (the retry ladder is widened past the
Windows worst case).

Six independent reviews found real defects in every original PR, several of them
dead-keyboard bugs and three introduced by the fix for another defect in the same
loop. Live QA on macOS covered every PR; Windows was validated three times.

WINDOWS: pass 1 found two defects that made the stale-account fix a no-op there
(the sweep fired before any PTY was bound and never retried; launcher panes were
filtered out). Pass 3 at the merged head: the prompt appears on its own after a
restart — warm ~3.7-4.2s, cold ~21s needing rung 4, so stablyai#10853's widening was
load-bearing rather than precautionary; an ordinary sentence typed into a healthy
pane while another pane's card is up reaches that pane and kills nothing; a pane
running vim after exiting Codex gets no card, still none 45s later. auth.json
byte-identical across every pass.

KNOWN GAPS, stated rather than implied: stablyai#10804 is unverified on Windows
(auto-resume could not be manufactured there); cross-volume Windows is untested
and expected to yield no bridged history (EXDEV, and Codex ignores symlinked
rollouts); a cold-parked pane never binds so the sweep never covers it; the
subagent-deepest launcher shape could not be reproduced on Windows, so that
branch is fixture-verified only; WSL passed isolation but the resume mechanism is
host-lane only. A host-account switch also marks and mutes live SSH remote panes
— confirmed pre-existing on main by two independent QA runs — tracked separately
in stablyai#10992. Related pre-existing defect filed as stablyai#10863.
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