agent-orchestrator icon indicating copy to clipboard operation
agent-orchestrator copied to clipboard

bug(codex): account switch recovery can silently retain the global fence

Open harshitsinghbhandari opened this issue 2 months ago • 6 comments

After switching the device Codex account to another saved account, AO remained stuck on "Switching the device Codex account and restarting AO sessions." The credential changed successfully, but several Codex sessions did not restart and the app remained blocked in recovery. Pressing the recovery action did not clear the state or show an actionable error.

Expected behavior: AO should either finish restarting sessions and unblock the app, or report which sessions failed and allow recovery to make visible progress without leaving the entire app blocked indefinitely.

harshitsinghbhandari avatar Sep 12 '26 09:09 harshitsinghbhandari

Agent-collected evidence

Collected on 12 September 2026 from the affected macOS installation. The repository checkout was reset to origin/main commit 15e9ea971f1711ec8b50e157d6eb300db6cbe0d6. The installed app reported version 0.13.0. Sensitive credential material and unrelated session data are omitted.

Persisted switch state

Read-only SQLite queries against ~/.ao/data/ao.db showed that credential activation completed, with credentials_committed_at populated. Seven session rows reached restart_state = restarted. Three Chat sessions reached restart_state = failed with only restart_unconfirmed:<generation> stored. The parent switch remained in phase = recovery_required with failure_code = restart_unconfirmed.

The relevant query shape was:

SELECT id, phase, failure_code, credentials_committed_at, created_at, updated_at
FROM codex_account_switches
ORDER BY created_at DESC;

SELECT session_id, interface_mode, stop_state, restart_state, error_code
FROM codex_account_switch_sessions
WHERE switch_id = ?;

The affected switch began at 2026-09-12 08:30:41 UTC. The final unchanged recovery update was 2026-09-12 08:45:02 UTC.

Code evidence

The exact original per-session cause cannot be recovered from durable state. restartCodexSwitchSessions replaces every underlying worker error with restart_unconfirmed:<generation> in backend/internal/session_manager/codex_account_switch.go, around line 898. The wrapped cause is neither persisted nor logged there.

Chat restart is serial. Native history settlement may wait 45 seconds in backend/internal/service/chat/controller.go, around line 34. restartCodexSwitchSessions retries ErrChatHistoryUnsettled once, around line 881. This makes a multi-session switch slow and leaves the overall worker dependent on each serial restart.

The switch and recovery goroutines run on the daemon background context without an overall switch deadline, around lines 265 and 1178 of backend/internal/session_manager/codex_account_switch.go.

The confirmed recovery defect is around lines 1185 through 1192. After the HTTP handler accepts recovery and starts a goroutine, recoverCodexAccountSwitch silently returns when journal loading or session-operation acquisition fails. It does not persist a recovery attempt, update the failure, or log the cause. Deferred cleanup retains the global fence because the switch phase remains nonterminal. The UI therefore remains blocked in recovery_required, while the accepted recovery request appears to have succeeded.

An existing codex_account_switch operation can be reclaimed. A conflicting different operation can make acquisition fail. That failure is currently swallowed.

Suggested verification coverage

  1. A blocking Chat launcher is bounded by a switch-level deadline.
  2. Several serial ErrChatHistoryUnsettled results cannot block indefinitely.
  3. A conflicting session operation produces a durable, visible recovery result.
  4. Failure to load the switch journal is logged and reflected in recovery state.
  5. A recovery request never returns apparent success when its worker immediately exits without progress.
  6. Sanitized per-session failure classifications are persisted without credentials or private conversation content.

The separate CODEX_HOME mismatch observed later was introduced during manual diagnostic relaunch and is not part of the original report.

harshitsinghbhandari avatar Sep 12 '26 09:09 harshitsinghbhandari

New Report

Reported by: @Ayash-Bera | Date: 2026-09-12 | Analyzed against: 15e9ea971 Env: Linux 6.19.6-arch1-1 (Arch), AppImage daemon, node v22.22.0

Same failure here, and I traced it through live state in ~/.ao/data/ao.db. Adding the mechanism, since the fence is not "silently retained" by accident: it is structurally unreachable from recovery_required.

Live state

codex_account_switches, still stuck 9 hours later and across a daemon restart:

                       id = 03abe9d8-e24b-4b86-b8f8-2265cfec22c2
        source_account_id = b050deab-2424-41d0-ac55-d4ca01e983d4
        target_account_id = 1b2ab0b9-86d1-47ee-a7d4-3ee0fa424af4
expected_account_revision = 3
                    phase = recovery_required
             failure_code = restart_unconfirmed
 credentials_committed_at = 2026-09-12 06:23:33
               created_at = 2026-09-12 06:23:32
               updated_at = 2026-09-12 09:29:45   <-- bumped by Retry recovery, phase unchanged
             completed_at =

codex_account_switch_sessions — 9 of 11 restarted, 2 did not:

session_id             mode  stop_state  restart_state  error_code
agent-orchestrator-2   chat  stopped     failed         restart_unconfirmed:3a6e3587-...
agent-orchestrator-5   chat  stopped     failed         restart_unconfirmed:76365875-...
(9 others)             chat  stopped     restarted

Both failed sessions had been idle since Sep 9. Their Codex rollout files are present on disk, so this is not a missing-history case:

~/.codex/sessions/2026/09/09/rollout-2026-09-09T10-39-30-01a08492-3752-7353-963b-b887db462dc4.jsonl
~/.codex/sessions/2026/09/09/rollout-2026-09-09T11-33-55-01a084c4-0a4b-7071-8bec-bbbb3a5b62b3.jsonl

Why the fence never lifts

domain/codex_account_switch.go:34-36 — only two phases are terminal:

func (p CodexAccountSwitchPhase) Terminal() bool {
	return p == CodexAccountSwitchCompleted || p == CodexAccountSwitchFailed
}

recovery_required is neither, so GetActiveCodexAccountSwitch keeps returning it as the active switch, and session_manager/codex_account_switch.go:165-169 rejects every new switch request:

if _, active, readErr := store.GetActiveCodexAccountSwitch(ctx); readErr != nil {
	return domain.CodexAccountSwitch{}, readErr
} else if active {
	return domain.CodexAccountSwitch{}, ErrCodexAccountSwitchInProgress
}

The fence is also enforced in the schema, so nothing can work around it at the storage layer:

CREATE UNIQUE INDEX idx_codex_account_switches_one_active
ON codex_account_switches((1))
WHERE phase NOT IN ('completed', 'failed');

That is the "account switching doesn't work" symptom: the user is locked out of switching entirely, not just stuck on this one.

Retry recovery cannot escape either. dispatchCodexAccountSwitch at recovery_required (codex_account_switch.go:591-604) sees active.AccountID == sw.TargetAccountID (credentials committed at 06:23:33), so it advances to VerifyingAccount → RestartingSessions → restartCodexSwitchSessions, which retries the same two sessions. They fail again, and line 569 sends it straight back to recovery_required:

case domain.CodexAccountSwitchRestartingSessions:
	if err := m.restartCodexSwitchSessions(ctx, store, sw.ID, sessions); err != nil {
		_ = m.advanceCodexAccountSwitch(ctx, store, sw, domain.CodexAccountSwitchRecoveryRequired, "restart_unconfirmed")
		return
	}

recovery_required → restarting → recovery_required, forever. CodexAccountSwitchFailed is only reachable through the rollback path, which needs active.AccountID == sw.SourceAccountID — and it is not, because the credential swap already succeeded. So no input the user can give reaches a terminal phase.

Suggested direction

The state machine needs an operator-driven give-up edge: mark the unrestartable sessions abandoned, settle the switch Failed (or Completed with partial-failure detail), release the fence, and tell the user which sessions need a manual restart. Right now a partial restart failure is treated as retryable forever, but nothing about it is retryable once the credential is committed and the worker cannot resume.

Secondary: the banner says only "AO could not confirm that every session restarted." It should name agent-orchestrator-2 and agent-orchestrator-5 — the data is already in codex_account_switch_sessions.error_code.

Worth noting the trigger too: the switch was made 22 seconds after a You've hit your usage limit turn failure on agent-orchestrator-12, which is the normal reason to switch accounts. Hitting a usage limit and then being locked out of switching away from it is the worst case for this fence.

Filed separately from this: the failed Codex Chat turns in the same session store an empty error_message, so they render as a bare "The agent ran into a problem" with no cause.

Ayash-Bera avatar Sep 12 '26 10:09 Ayash-Bera

Filed the empty-error_message half separately: #5289. Different subsystem (acp/conversation.go error propagation, not the switch state machine) and it reproduces with no account switch involved, so it should not block this one.

Ayash-Bera avatar Sep 12 '26 10:09 Ayash-Bera

Assigned to @Vansh-Singhal

Rishet11 avatar Sep 14 '26 17:09 Rishet11

@Rishet11 PR #5170 is already under review.

Vansh-Singhal avatar Sep 14 '26 22:09 Vansh-Singhal

closing this issue, it has already been fixed in #5170.

Pulkit7070 avatar Sep 16 '26 22:09 Pulkit7070