Skip to content

[Bug]: Claude, Codex Sessions Restoration enhancement #5232

Closed

Description

@NirajRChaudhari

Problem or use case

Hi @AmethystLiang / @Jinwoo-H , attached below are few bugs that can be resolved to make the restore session more robust. In addition to your PR fix #5230

Root Cause Analysis: Claude / Codex / Gemini Sessions Not Restored After Quit

Two independent bugs combine to produce empty terminals on reopen. They are at different layers and need separate fixes.


Bug 1 — Daemon startup race causes Local PTY fallback

Symptom

Terminals reopen empty. PTY IDs in orca-data.json are small integers (1, 2, 3, 5, 6) instead of UUID-format daemon session IDs.

Root Cause

src/main/startup/first-window-startup-services.ts resolves localPtyReady on either the daemon becoming ready OR the first-window timeout, whichever comes first:

// before fix
const localPtyReady = allServicesReady // includes firstWindowTimeout

When the daemon is slow to start, the first-window timeout fires first.

pty:spawn waits on localPtyReady, then selects a provider. After timeout it picks LocalPtyProvider instead of DaemonPtyProvider.

LocalPtyProvider assigns numeric integer IDs.

On quit:

killAllPty()

in pty.ts:2906 destroys all local PTYs. There is nothing for warm reattach to reconnect to on restart.

Fix

File: src/main/startup/first-window-startup-services.ts

// after fix
const localPtyReady = daemon.ready // wait for daemon specifically; first-window can still fail-open

The first window can still open after timeout (user sees the app), but terminal spawning blocks until the daemon is actually ready rather than falling back to non-restorable Local PTYs.

Evidence

Local data shows:

ptyId: 1
ptyId: 3
ptyId: 6

all pointing to LocalPtyProvider sessions.

The v12 daemon is running but reports:

0 live sessions

terminal-history/ contains daemon checkpoint files keyed by:

worktreeId@uuid

but there are no corresponding entries for those numeric IDs.


Bug 2 — Agent session IDs never persisted on quit

Symptom

Even when daemon PTYs are used correctly, Claude/Codex/Gemini sessions are not resumed via:

claude --resume
codex resume
gemini --resume

after restart.

The terminal opens a fresh shell instead of resuming the conversation.

Root Cause

sleepingAgentSessionsByPaneKey (the map that holds provider session_id values for resumable agents) is only ever populated by:

captureSleepingAgentSessionsByWorktree()

which is called at a single callsite:

src/renderer/src/store/slices/terminals.ts:1716

Specifically:

  • only when keepIdentifiers=true
  • only during workspace sleep/wake (switchWorktree with sleep semantics)
  • never on app quit

The shutdown handler in App.tsx calls:

buildWorkspaceSessionPayload()

which reads sleepingAgentSessionsByPaneKey directly from Zustand.

Because captureSleepingAgentSessionsByWorktree() was never called during the session (user went directly to quit without switching workspaces), the map is empty.

Nothing gets written to disk for the resume path.

Evidence

sleepingAgentSessionsByPaneKey is completely absent from:

  • orca-data.json
  • all 5 backup files

across every session.

Three tabs have launchAgent set:

Claude × 1
Codex × 2

proving the agent ran, but no session_id was ever saved.


Fix — Two Part Solution

Part A: Capture all active agent sessions at quit time

Add a new action:

captureAllSleepingAgentSessions()

to the agent-status slice.

The action should iterate:

agentStatusByPaneKey
retainedAgentsByPaneKey

across all worktrees.

Use the same logic as:

captureSleepingAgentSessionsByWorktree()

but without the worktree filter.

Call it inside captureAndFlush() before the fresh state is read.

File: src/renderer/src/App.tsx

// src/renderer/src/App.tsx — captureAndFlush()

useAppStore.getState().captureAllSleepingAgentSessions() // add before freshState read

const freshState = useAppStore.getState()

window.api.session.setSync(
  buildWorkspaceSessionPayload(freshState)
)

Part B (Critical Gate)

Do not consume the persisted session IDs through the existing:

resumeSleepingAgentSessionsForWorktree()

path in worktree-activation.ts.

That function:

  1. Unconditionally creates a new tab
  2. Queues the resume command

If daemon warm reattach also succeeds (Claude is still running), the user ends up with two Claude sessions for the same terminal.

Instead, wire the persisted session IDs into the cold-restore path in:

pty-connection.ts

The existing cold-restore helper at:

pty-connection.ts:1795

already knows how to build a:

claude --resume

startup command.

However it currently reads only from:

agentStatusByPaneKey

which is in-memory only and disappears after restart.

The fix is to also check:

sleepingAgentSessionsByPaneKey

for the matching paneKey when agentStatusByPaneKey has no entry.

This ensures resume only fires when the daemon PTY is actually dead / cold-restored and not when warm reattach already succeeded.


Fix Priority

Bug 1 (daemon race) Bug 2 (missing capture)
Impact All terminals open empty (even shell) Agent conversation not resumed (shell works, Claude doesn't)
Affected sessions Every terminal when daemon is slow at startup All Claude/Codex/Gemini sessions on every normal quit
Fix complexity One-line change, existing test updated Two parts: new store action + cold-restore wiring
Applies to All terminal tabs Only tabs with launchAgent set

Proposed solution

Proposed solution shared above

Alternatives or additional context

No response

Activity

  1. added
    enhancement
    on Jun 12, 2026
  2. changed the title [-][Feature]: Claude, Codex Sessions Restoration enhancement[/-] [+][Bug]: Claude, Codex Sessions Restoration enhancement[/+] on Jun 12, 2026
  3. self-assigned this
    on Jun 12, 2026
  4. Jinwoo-H commented on Jun 12, 2026

    @Jinwoo-H
    Contributor

    Thanks for the detailed RCA — both bugs verified against the code and now fixed:

    • Bug 1 → Keep restored terminals on daemon PTYs when startup is slow #5234. Confirmed, with one note: it's worse than described — once the fallback fires, the local numeric ptyIds overwrite the persisted daemon session ids, so restoration stays broken on every later restart, not just the slow one. The proposed one-liner (localPtyReady = daemon.ready) wasn't sufficient on its own: the 12s timeout also aborts daemon init (so daemon.ready would still resolve onto the fallback), and it dropped the agent-hook-server gate. The merged approach keeps the window fail-open at 12s but holds the PTY spawn gate for the service attempts themselves, with a 60s abort backstop. Verified with an e2e that delays daemon init to 15s (fails on pre-fix code with exactly your symptom — numeric ids, empty terminal).

    • Bug 2 → Persist agent provider sessions at quit and resume them on cold restore #5240. Confirmed: quit never captured provider session ids, and your Part B duplicate-tab warning was correct (worktree activation unconditionally creates a tab). Implemented as capture-at-quit with an origin: 'quit' tag; quit-origin records are only consumable by the pane-level cold-restore resume in pty-connection.ts, never by activation. One correction to the RCA's framing: tab existence can't be the gate, because slept worktrees keep their tabs (only PTYs die) — the origin tag is what preserves sleep/wake behavior exactly. Verified with an e2e that SIGKILLs the real daemon between launches and asserts the restored pane injects codex … resume <session-id> with no duplicate tab (fails on pre-fix code).

    Together with #5230 (health-check daemon kill), all three session-loss paths from this investigation are closed.

  5. taifunk commented on Jun 14, 2026

    @taifunk

    Reproducing the transition-edge case on Windows 10, upgrading 1.4.65 → 1.4.68.

    Had 2 floating terminal sessions active at update time — one was a plain shell, one had an active Claude agent mid-session (launchAgent: "claude"). After the 1.4.68 installer ran and relaunched Orca, both sessions were replaced with 2 brand-new blank sessions created ~132ms apart (programmatic batch creation, not user action). The Claude agent session was unrecoverable.

    Why the fix in this release didn't help:
    The cold-restore fix in PRs #5234/#5240 requires that the previous version wrote sleepingAgentSessionsByPaneKey at quit time. But 1.4.65 never wrote this (that's the original bug). So on first launch after upgrading from a pre-fix version, 1.4.68's restore mechanism has no payload to work from — it falls back to spawning fresh sessions. The fix only takes effect from the second restart post-upgrade, once 1.4.68 itself has written the quit-time state.

    First-time upgraders from any pre-fix version will always lose their in-progress agent sessions once at the transition point. No workaround — sessions are gone before the user can act.

    Platform: Windows 10. Orca 1.4.68.

  6. taifunk commented on Jun 14, 2026

    @taifunk

    The transition-edge case above has been filed as a separate issue: #5356

  7. added theissue type on Jul 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions