orca icon indicating copy to clipboard operation
orca copied to clipboard

fix: detect daemon forked against stale node-pty helper (#4365)

Open sudoeren opened this issue 6 months ago • 0 comments

Problem

The protocol-healthy daemon could outlive the node-pty spawn-helper it was loaded against. A \pnpm install\ that re-linked node-pty, a worktree delete that took the helper with it, or a native rebuild that replaced \uild/Release/spawn-helper\ all left the long-lived daemon holding an FD or path that's no longer on disk. The pid-file previously captured only \pid + start time + entry path + app version, so \killStaleDaemon()\ had no way to detect that the helper underneath the running daemon was a phantom. \preflightMacNodePtySpawnEnvironment\ only inspects the on-disk candidates from the current package root — it cannot see what the running daemon was loaded against.

Fix

Capture the existing on-disk helper path at fork time and persist it as
odePtyHelperPath\ on \DaemonPidFile. \isDaemonStaleForCurrentNodePtyHelper()\ reads the pid file, resolves the persisted path with \ s.existsSync, and short-circuits to false on Windows (ConPTY has no spawn-helper) and on legacy daemons that predate the field (fail open rather than kill unverifiable processes).

Wire the new check into the launcher flow alongside \stalePackagedBundle, gated by the same \shouldPreserveDaemonWithLiveSessions()\ so live sessions still get first refusal.

What changed

  • New module \src/main/daemon/daemon-node-pty-snapshot.ts: \pickExistingNodePtyHelper\ + \getLoadedNodePtyHelperSnapshot\ resolve the first on-disk candidate from \getNodePtySpawnHelperCandidates()\ at fork time. Returns
    ull\ on Windows and when nothing exists.
  • *\DaemonPidFile* (\daemon-spawner.ts): optional
    odePtyHelperPath: string | null. Backward-compatible (older daemons parse to
    ull, treated as fail-open).
  • \ParsedDaemonPid\ + parser (\daemon-health.ts): add the new field.
  • *\isDaemonStaleForCurrentNodePtyHelper* (\daemon-health.ts): POSIX-only, returns false on Windows, false on legacy pid files, true when !existsSync(persistedPath).
  • **\daemon-init.ts**: capture snapshot at fork; \staleNodePtyHelper\ joins the \stalePackagedBundle\ branch in the launcher flow with a per-cause \ eplacementLabel.

Tests

  • \daemon-node-pty-snapshot.test.ts\ (new): 7 cases — empty candidates, missing path, existing path, platform gating, return-type contract.
  • \daemon-health.test.ts: parser cases for the new field + 5 new cases for \isDaemonStaleForCurrentNodePtyHelper\ (Windows, legacy, missing-pid-file, present helper, missing helper).
  • \daemon-init.test.ts: \ i.mock\ updated to include \isDaemonStaleForCurrentNodePtyHelper\ and the new snapshot module.

Run results on this branch:

  • \daemon-node-pty-snapshot.test.ts: 7/7 pass
  • \daemon-health.test.ts: 29 pass / 1 fail — the failing case (\passes when a daemon answers ping) is a pre-existing Windows-only \EACCES\ on Unix-socket \listen, verified identical on \main.
  • \daemon-bundle-staleness.test.ts: 3/3 pass.
  • \ sc --noEmit -p config/tsconfig.node.json: clean.
  • \oxlint src/main/daemon: clean.
  • \oxfmt --check: clean.

\daemon-init.test.ts\ has 11 pre-existing Windows path-doubling failures (\out/main/out/main) that reproduce on \main\ without these changes; not addressed here.

sudoeren avatar Jun 05 '26 15:06 sudoeren