Skip to content

Preserve live PTYs during daemon resolver refresh - #2974

Merged
nwparker merged 1 commit into
mainfrom
Jinwoo-H/p0-issues
May 28, 2026
Merged

nwparker merged 1 commit into
mainfrom
Jinwoo-H/p0-issues

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented May 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR fixes the P0 class of failures where live terminal child processes can be killed even though Orca's detached terminal daemon is expected to survive app restarts.

The important correction from the investigation is that this is not Codex-specific. Codex was just the most visible workload because we keep many long-running Codex processes in Orca terminals. The failure mode is at the local daemon / PTY lifecycle layer, so it can affect any long-running terminal process.

Root cause

PR #2835 added macOS system resolver-health detection for daemon reuse. That was directionally correct, but it introduced two replacement paths that could tear down a live daemon:

  1. App launch path: if the current-protocol daemon passed protocol health but reported an unhealthy macOS resolver, createOutOfProcessLauncher() called cleanupDaemonForProtocol(runtimeDir, PROTOCOL_VERSION).
  2. New-terminal path: DaemonPtyAdapter.replaceUnhealthyMacResolverDaemonBeforeNewPty() could respawn the daemon before creating a new PTY.

Both replacement paths ultimately shut down or respawn the daemon. For the current daemon, that means live PTY child processes can be killed with killSessions: true. From the user's perspective, the terminal surface may still appear to persist, but the actual process inside it is gone.

That matches the clarified report: the issue is not primarily that Orca cannot find old session records. It is that the child processes themselves can exit despite the terminal daemon having a separate lifecycle from the app.

Fix

This PR makes resolver-health replacement non-destructive for daemons that may own live work:

  • On app launch, if resolver health is unhealthy, Orca now asks the daemon for listSessions before cleanup.
    • If live sessions exist, preserve the daemon.
    • If session state cannot be verified, preserve the daemon as the fail-safe.
    • Only replace when the daemon is unhealthy and reports zero live sessions.
  • On new-terminal creation, the adapter now queries the daemon itself before respawning.
    • This is intentionally stronger than checking the adapter's local activeSessionIds, because after an app restart a fresh adapter can have an empty local set while the daemon still owns live sessions.
  • The daemon protocol is bumped from v9 to v10, and v9 is added to the previous-protocol list.
    • This preserves already-running v9 daemons from affected builds by routing them as legacy daemons instead of running them through the current-protocol resolver-health replacement path.

Why the protocol bump matters

Without the v10 bump, users who already have a v9 daemon from an affected build could relaunch into this patched build and still have that daemon treated as the current daemon. The launcher/adapters would then continue evaluating resolver health against it.

By making v10 current and v9 legacy:

  • Existing v9 sessions stay attached to their existing daemon.
  • New terminals use the fresh v10 daemon.
  • Legacy v9 adapters do not get a respawn callback, so they do not resolver-health replace themselves and kill old sessions.

Regression coverage

Added/updated tests for:

  • Resolver-unhealthy app launch preserves a daemon that owns live sessions.
  • Resolver-unhealthy app launch preserves when live session state cannot be verified.
  • Affected v9 daemons are routed as legacy adapters on launch.
  • Resolver-unhealthy new-terminal creation preserves when the current adapter already knows about live sessions.
  • Resolver-unhealthy new-terminal creation preserves when live sessions exist in the daemon but have not been reconciled into the fresh adapter yet. This locks down the cross-restart gap found during review.
  • Resolver-unhealthy replacement still happens when there are zero live sessions, preserving the original DNS-staleness mitigation.

Verification

Ran locally:

pnpm vitest run --config config/vitest.config.ts src/main/daemon/daemon-init.test.ts src/main/daemon/daemon-pty-adapter.test.ts src/main/daemon/daemon-pty-router.test.ts src/main/daemon/daemon-spawner.test.ts src/main/daemon/daemon-server.test.ts
pnpm lint src/main/daemon/types.ts src/main/daemon/daemon-init.ts src/main/daemon/daemon-pty-adapter.ts src/main/daemon/daemon-init.test.ts src/main/daemon/daemon-pty-adapter.test.ts
pnpm typecheck:node
git diff --check

Results:

  • Daemon test suite: 5 files passed, 111 tests passed.
  • Lint: passed.
  • Node typecheck: passed.
  • Diff whitespace check: passed.

Local environment note: commands emit the existing engine warning because this shell is Node v26 while the repo wants Node 24.

Review notes

I asked subagents to review this specifically for daemon lifecycle, regression risk, and cross-platform/SSH implications.

They found one real blocker in the first version of the patch: checking only activeSessionIds was insufficient after app restart because a fresh adapter may not yet know about sessions owned by the daemon. This PR now fixes that by querying daemon listSessions before adapter-side replacement and includes the corresponding regression test.

Cross-platform/SSH review did not find blockers: resolver-health replacement is macOS-only (unknown elsewhere), and the touched paths are local daemon paths rather than SSH provider paths.

Made with Orca 🐋

@nwparker
nwparker merged commit 1371402 into main May 28, 2026
2 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
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.

2 participants