Skip to content

ci(daemon): fail builds, packaging, and CI when the terminal daemon cannot start — and stop failing silently - #7849

Merged
Jinwoo-H merged 3 commits into
mainfrom
Jinwoo-H/daemon-startup-failure-hardening
Jul 8, 2026
Merged

Jinwoo-H merged 3 commits into
mainfrom
Jinwoo-H/daemon-startup-failure-hardening

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Why

v1.4.129-rc.1 shipped a terminal daemon that exited at module load (an electron require leaked into its bundle graph — fixed in #7844) while every CI check passed. The app fell back to local PTYs silently, nothing executed the built daemon, and its startup stderr was discarded — so the outage was invisible until adopted old daemons died in the field.

This PR adds the defense layers around daemon startup that would have caught it:

build guard (#7844) → per-PR CI boot → packaging smoke → field observability.

Stacked on #7844 — its commits appear in this PR until it merges. Merge #7844 first.

A — native-smoke CI boots the built daemon (per PR)

config/scripts/daemon-boot-smoke.mjs forks the built out/main/daemon-entry.js under plain Node (the way production forks it under ELECTRON_RUN_AS_NODE) and:

  • hard-asserts the { type: 'ready' } IPC within 30s, and a clean shutdown (no hang/zombie);
  • best-effort runs the daemon's own ptySpawnHealth RPC over the control socket (a real end-to-end PTY spawn), logging a skip rather than failing since node-pty spawn can be flaky on constrained runners.

Wired into the native-smoke job in computer-e2e.yml (matrix ubuntu-22.04 + windows-latest, per PR) right after build:electron-vite. Daemon bundle-graph paths (src/main/daemon/**, electron.vite.config.ts, build-plugins/**, the smoke script) now re-trigger the workflow, and computer-e2e-workflow.test.mjs asserts the step + triggers exist.

node-pty is N-API (ABI-stable), so it loads under plain Node even when rebuilt for Electron — the ready assertion is safe on both runners.

B — packaging-time daemon smoke (electron-builder afterPack)

config/scripts/verify-packaged-daemon-entry.cjs boots the packaged app.asar.unpacked/out/main/daemon-entry.js under plain Node in the existing afterPack hook, requiring the Usage: daemon-entry stderr and failing the build on MODULE_NOT_FOUND / a missing usage line. This is a module-load proof against the real packaged layout — it catches asar-unpack/bundling regressions the repo-tree build guard cannot see. Arch-gated to the host slice (daemon-entry.js is JS but requires the target-arch native node-pty, which the host Node cannot load cross-arch); cross-arch slices are skipped with a log.

C — daemon startup stderr is no longer discarded

The daemon fork previously used stdio: ['ignore','ignore','ignore','ipc'], so a module-load crash surfaced only as "exited with code 1". Now:

  • stderr is piped, a bounded (~8KB) tail is captured during the startup window, attached to the thrown failure error, and logged;
  • the pipe is released (off + destroy) on ready so the detached daemon does not keep Electron's event loop alive;
  • daemon-entry installs a process.stderr.on('error', ...) guard so a broken pipe (after the parent releases its end) cannot kill a healthy daemon.

Unit tests in daemon-init.test.ts cover both the capture-into-error path (the scope-C negative test) and the release-on-ready path.

D — daemon-unavailable is loud, not silent

onDaemonError now logs a loud reason (carrying the captured stderr tail from C) and emits a low-cardinality daemon_start_failed telemetry event via the existing track() pipeline — error_class enum only, no raw stderr on the wire (paths/usernames stay local). A spike in this event is the fleet-wide signal for a daemon outage.

No new UI: there is no existing lightweight main->renderer toast/banner channel to reuse, and the spec said not to build new UI — surfacing this in-app is noted as a follow-up.

Verification

Local (quick, targeted): typecheck:node passes; daemon-init + telemetry + computer-e2e-workflow suites pass; the boot smoke and the packaged-entry verifier were exercised against the built/synthetic layouts. Full build/test verification is delegated to this PR's CI (native-smoke on both OSes exercises A; unit suites exercise C/D).

Jinwoo-H added 3 commits July 8, 2026 18:56
The terminal daemon in v1.4.129-rc.1 could not start on any platform. #7642
added `import { ensureShellReadyWrappersAt }` to windows-shell-args.ts, which
pulled local-pty-shell-ready.ts — and its top-level `import { app } from
'electron'` — into daemon-entry's shared chunk graph. The daemon is forked as
a plain Node process (ELECTRON_RUN_AS_NODE), where `require("electron")` throws
MODULE_NOT_FOUND at module load, so the daemon exited code 1 instantly. It was
masked by daemon adoption until a live old daemon died, at which point
terminals froze and silently fell back to local PTYs.

Minimal fix: local-pty-shell-ready.ts no longer imports electron. The wrapper
root now resolves from ORCA_USER_DATA_PATH, which main canonicalizes to its own
userData at startup (configureOrcaUserDataPathEnv) and the daemon fork sets
explicitly, so it still matches this instance's root.

Build guard: a new rollup plugin (build-plugins/plain-node-entry-guard.ts) runs
on the main build and fails it if any chunk reachable from a plain-node fork
entry (daemon-entry, parcel-watcher-process-entry, computer-sidecar, the CLI
agent-hooks entry) requires electron, plus smoke-loads daemon-entry under plain
node to prove its graph resolves. CI never executed the built daemon-entry
under plain node, so nothing caught the regression before.
…oot assertions

The daemon-entry fix resolves the shell-ready wrapper root from
ORCA_USER_DATA_PATH instead of electron's app.getPath('userData'). This
suite mocked getPath but never set the env var, so ZDOTDIR assertions
resolved to tmpdir. Mirror production (main canonicalizes the env var at
startup before any spawn) in the suite env.
…annot start — and stop failing silently

v1.4.129-rc.1 shipped a terminal daemon that exited at module load (an electron
require leaked into its bundle, fixed in #7844) while every CI check passed and
the app fell back to local PTYs silently. Nothing executed the built daemon, and
its startup stderr was discarded, so the outage was invisible until adopted old
daemons died in the field. This adds defense-in-depth around daemon startup.

A. Per-PR CI boot: config/scripts/daemon-boot-smoke.mjs forks the BUILT
   daemon-entry.js under plain Node (the way production forks it), hard-asserts
   the 'ready' IPC and a clean shutdown, and best-effort runs the daemon's own
   ptySpawnHealth RPC. Wired into the native-smoke job (ubuntu-22.04 +
   windows-latest); daemon bundle-graph paths now re-trigger the workflow.

B. Packaging smoke: config/scripts/verify-packaged-daemon-entry.cjs boots the
   packaged daemon-entry under plain Node from the asar-unpacked layout in the
   electron-builder afterPack hook (arch-gated to the host slice), failing the
   build on MODULE_NOT_FOUND or a missing usage line — catches asar-unpack
   regressions the repo-tree build guard cannot see.

C. Startup stderr is captured, not discarded: the daemon fork now pipes stderr,
   keeps a bounded tail during startup, attaches it to the failure error and
   logs it, and releases the pipe on ready (so the detached daemon does not keep
   Electron alive). daemon-entry swallows stderr EPIPE so a dead pipe cannot
   kill a healthy daemon. Unit tests cover both the capture and the release.

D. Loud daemon-unavailable: onDaemonError logs a loud reason (carrying the
   captured stderr tail) and emits a low-cardinality daemon_start_failed
   telemetry event (error_class only; no raw stderr on the wire). No new UI —
   there is no existing lightweight main->renderer surface to reuse.

Stacked on #7844 (its commits appear here until it merges).
@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds guards ensuring plain-Node daemon entries (built without Electron) can load and boot correctly: a Rollup build plugin detects require("electron") in reachable chunks and smoke-loads the built daemon entry, a packaged-app afterPack check verifies the unpacked daemon entry boots, and a new CI smoke-test script forks the daemon, waits for readiness, optionally checks PTY health, and validates shutdown. The computer-e2e workflow is updated with new trigger paths and a smoke step. Daemon startup now captures a bounded stderr tail, appends it to failure errors, and reports a new daemon_start_failed telemetry event. Shell-ready path resolution switches from Electron's app.getPath to an ORCA_USER_DATA_PATH environment variable, with corresponding test updates.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description has useful detail, but it misses the required template sections and checklist headings. Rewrite it to match the repo template with Summary, Screenshots, Testing, AI Review Report, Security Audit, and Notes sections.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the daemon-startup hardening and silent-failure fix.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/main/daemon/daemon-init.ts (1)

396-418: 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Move releaseStderr() before writeFileSync to prevent resource leak on throw.

In onReadyMessage, releaseStderr() (line 415) is called after writeFileSync (lines 401–410). If writeFileSync throws (disk full, permission error, I/O failure), releaseStderr() is never reached — the piped stderr stream stays open, keeping the parent event loop alive and preventing clean Electron exit. child.disconnect() and child.unref() are also skipped, leaving the IPC channel referenced. Meanwhile the daemon is actually running and ready, but the caller sees a rejection.

The pid file write doesn't depend on the stderr pipe, so releaseStderr() can safely move before it.

🔧 Proposed fix
       settled = true
       cleanupStartupListeners()
+      // Why: release the stderr pipe before any remaining work so a throw
+      // (e.g. writeFileSync I/O error) can't leave a live handle on the
+      // detached daemon's stderr that blocks Electron exit.
+      releaseStderr()
       if (child.pid) {
         writeFileSync(
           getDaemonPidPath(runtimeDir),
           serializeDaemonPidFile({
             pid: child.pid,
             startedAtMs: getProcessStartedAtMs(child.pid),
             entryPath,
             appVersion: app.getVersion()
           }),
           { mode: 0o600 }
         )
       }
-      // Why: disconnect IPC channel, release the stderr pipe, and unref so
-      // Electron can exit without waiting for the daemon. The daemon keeps
-      // running detached.
-      releaseStderr()
       child.disconnect()
       child.unref()
       resolve()

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: a24fd567-8feb-49b2-be1a-08bf5dfe3478

📥 Commits

Reviewing files that changed from the base of the PR and between 19f1ad4 and 63e19c7.

📒 Files selected for processing (16)
  • .github/workflows/computer-e2e.yml
  • build-plugins/plain-node-entry-guard.ts
  • config/electron-builder.config.cjs
  • config/scripts/computer-e2e-workflow.test.mjs
  • config/scripts/daemon-boot-smoke.mjs
  • config/scripts/verify-packaged-daemon-entry.cjs
  • config/tsconfig.node.json
  • electron.vite.config.ts
  • src/main/daemon/daemon-entry.ts
  • src/main/daemon/daemon-init.test.ts
  • src/main/daemon/daemon-init.ts
  • src/main/index.ts
  • src/main/ipc/pty.test.ts
  • src/main/providers/local-pty-shell-ready.test.ts
  • src/main/providers/local-pty-shell-ready.ts
  • src/shared/telemetry-events.ts

@Jinwoo-H
Jinwoo-H merged commit 20832ef into main Jul 8, 2026
7 checks passed
@Jinwoo-H
Jinwoo-H deleted the Jinwoo-H/daemon-startup-failure-hardening branch July 8, 2026 23:46
Jinwoo-H added a commit that referenced this pull request Jul 9, 2026
Clean auto-merge, no conflicts. Brought in from main:
- daemon boot hardening: fail builds/CI when terminal daemon can't start;
  plain-node-entry-guard + verify-packaged-daemon-entry (electron require
  leaked into daemon-entry chunk, #7844/#7849)
- perf(windows): relax whole-process-table scans for terminals with no
  agent evidence (#7607) — foreground-scan-cadence
- fix(agents): route WSL OMP hooks to the Windows listener (#7565/#7641)
- fix: recover fresh local terminal spawns from a deleted saved cwd (#7847)
- native-chat: upload composer attachments over SSH (#7832)
- mobile: keyboard dismiss control; agent-tab launch fix
- new GitHub issue draft persistence (#7778)

Perf-branch structure preserved (clean auto-merge; no restructured terminal
files conflicted). Gates: typecheck 0 errors; terminal-pane + ipc/pty +
daemon + pane-manager suites 4497 passed / 14 skipped / 0 failed.

Co-authored-by: Orca <help@stably.ai>
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.

1 participant