Skip to content

fix(win): harden startup during partial updates - #11613

Merged
brennanb2025 merged 1 commit into
stablyai:mainfrom
OrcaWin:fix/windows-partial-update-bootstrap
Jul 30, 2026
Merged

brennanb2025 merged 1 commit into
stablyai:mainfrom
OrcaWin:fix/windows-partial-update-bootstrap

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • bundle zod into the Electron main-process output so startup does not depend on the later resources/node_modules deployment
  • prepend a fatal bootstrap guard before Rollup's generated require() prelude, then replace it with the normal durable error guard after the source graph loads
  • cover both the bundling contract and an exact pre-import Cannot find module 'zod' failure

Root cause

The Windows NSIS install updates the live application directory non-transactionally. In the captured failure, the new app.asar and plugin-host chunk were visible 8.343 seconds before resources/node_modules/zod existed. The new main bundle immediately executed require("zod"), lost that race, and Electron displayed its native fatal-error dialog. That failed main process remained resident, held the updater lock, and prevented the runtime/pairing listener from starting.

This change uses the packaging-safe alternative to an installer rewrite: the bootstrap-critical schema dependency is part of the main bundle itself. It also ensures any future exception that occurs before source-level guards load exits instead of stranding a dialog-only Electron process.

User-visible behavior

Before After
Launch during the vulnerable update window Native Cannot find module 'zod' dialog; no Orca window zod is already in the startup bundle, so launch proceeds normally
Remote runtime/pairing Never starts Starts with the normal application runtime
Updater after a bootstrap failure Failed Electron process can remain resident and block replacement Pre-import fatal failures exit with code 1 on the next event-loop turn
Ordinary launches Normal Unchanged; the temporary bootstrap guard is removed when the durable main-process guard installs

Validation

  • pnpm exec vitest run config/scripts/electron-vite-output-contract.test.ts src/main/startup/bootstrap-fatal-exit-guard.test.ts src/main/startup/main-process-error-guards.test.ts (17 tests)
  • pnpm run build:desktop
  • pnpm run typecheck:node
  • changed-file Oxlint, type-aware Oxlint, React Doctor, and Oxfmt checks
  • pnpm run check:max-lines-ratchet
  • inspected emitted out/main: bootstrap guard is the first code in index.js; no bare require("zod") remains

A final electron-builder --dir package was not produced locally because this host lacks the Visual Studio toolchain required to rebuild windows-native-registry; the complete desktop build and packaging-relevant emitted-output checks passed.

Comment thread electron.vite.config.ts
const EXTERNAL_MAIN_DEPENDENCIES = Object.keys(packageJson.dependencies).filter(
(dependency) => !BUNDLED_MAIN_DEPENDENCIES.has(dependency)
)
const BOOTSTRAP_FATAL_EXIT_GUARD_KEY = '__ORCA_BOOTSTRAP_FATAL_EXIT_GUARD__'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 The guard key is declared independently here as a local const and in src/main/startup/bootstrap-fatal-exit-guard.ts as an exported constant. The two values must always match: the build-time code embeds the local copy into the generated banner string, while the runtime and tests import the source-module copy. If they ever diverge the banner will install the guard under a key that removeBootstrapFatalExitGuard never cleans up, silently leaving a stale uncaughtException listener on the process. The contract test catches this only at test-run time, not at the import site. Importing the single exported constant would eliminate the coupling.

Suggested change
const BOOTSTRAP_FATAL_EXIT_GUARD_KEY = '__ORCA_BOOTSTRAP_FATAL_EXIT_GUARD__'
import { BOOTSTRAP_FATAL_EXIT_GUARD_KEY } from './src/main/startup/bootstrap-fatal-exit-guard'

@greptile-apps

greptile-apps Bot commented Jul 30, 2026

Copy link
Copy Markdown

Greptile Summary

This PR hardens Electron startup against the Windows NSIS non-transactional install race where app.asar lands before resources/node_modules. Two complementary changes are made: zod is moved into the bundled main-process output so it is never fetched from the race-prone external tree, and a pre-import uncaughtException guard is prepended to index.js so any future early-bootstrap failure exits with code 1 instead of leaving a dialog-only process that blocks the updater.

  • zod is added to BUNDLED_MAIN_DEPENDENCIES and excluded from externalizeDeps, causing Rollup to inline it into the bundle; a contract test asserts both the external predicate and the externalizeDeps.exclude list.
  • A new createBootstrapFatalExitBanner() function generates a self-removing IIFE that registers a one-shot uncaughtException handler before Rollup's require() prelude; installUncaughtPipeErrorGuard() removes it once the durable source-level handler takes over.
  • BOOTSTRAP_FATAL_EXIT_GUARD_KEY is declared independently in electron.vite.config.ts and src/main/startup/bootstrap-fatal-exit-guard.ts; they must stay in sync, and electron.vite.config.ts could import the exported constant directly to enforce a single source of truth.

Confidence Score: 4/5

Safe to merge; the fix is targeted, well-tested, and the generated guard integrates cleanly with the existing source-level error handling.

The logic is correct: zod is bundled in, the bootstrap listener is installed before any require calls, and it is removed without a coverage gap when the durable handler takes over. The one thing worth cleaning up before long-term maintenance becomes a burden is that BOOTSTRAP_FATAL_EXIT_GUARD_KEY lives independently in two files; if they diverge the stale listener silently persists, but the contract test would catch it at test-run time. No functional defects were found.

Files Needing Attention: electron.vite.config.ts — the locally re-declared BOOTSTRAP_FATAL_EXIT_GUARD_KEY should ideally be imported from bootstrap-fatal-exit-guard.ts to avoid a hidden coupling.

Important Files Changed

Filename Overview
electron.vite.config.ts Adds zod to BUNDLED_MAIN_DEPENDENCIES, exports createBootstrapFatalExitBanner, and renames the bootstrap plugin; BOOTSTRAP_FATAL_EXIT_GUARD_KEY is re-declared locally instead of imported from the source module.
src/main/startup/bootstrap-fatal-exit-guard.ts New module that exports the guard key constant and removeBootstrapFatalExitGuard(); logic is correct and safe with optional-chaining on the undefined guard case.
src/main/startup/main-process-error-guards.ts Adds removeBootstrapFatalExitGuard() call at the end of installUncaughtPipeErrorGuard(); ordering (on then remove-bootstrap) is correct with no gap in coverage.
config/scripts/electron-vite-output-contract.test.ts Adds VM-sandboxed test for the bootstrap banner and a contract assertion that zod is excluded from externalization; test logic and assertions are correct.
src/main/startup/bootstrap-fatal-exit-guard.test.ts Unit test for removeBootstrapFatalExitGuard; covers the installed-guard path; the no-guard-present path is implicitly safe due to optional-chaining.

Sequence Diagram

sequenceDiagram
    participant NSIS as NSIS Installer
    participant asar as app.asar (new)
    participant res as resources/node_modules
    participant EM as Electron Main

    NSIS->>asar: "deploy at t=0"
    asar->>EM: bootstrap banner runs, installs uncaughtException guard
    asar->>EM: source graph loads (zod bundled inline, no race)
    EM->>EM: installUncaughtPipeErrorGuard installs source-level handler
    EM->>EM: removeBootstrapFatalExitGuard removes bootstrap listener
    NSIS->>res: "deploy at t=8s later"
    Note over EM: App already running normally
Loading

Reviews (1): Last reviewed commit: "fix(win): harden startup during partial ..." | Re-trigger Greptile

@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The main-process build now bundles zod and prepends a fatal bootstrap-exit banner alongside startup diagnostics. The banner handles uncaught bootstrap exceptions by setting exit code 1, scheduling process termination, and installing a global guard. Normal error-guard setup removes and invokes that guard. Tests cover dependency bundling, fatal bootstrap behavior, and guard removal.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description clearly explains the root cause, user impact, implementation, and validation, but omits several required template sections. Add the Screenshots, Testing checklist, AI Review Report with cross-platform confirmation, Security Audit, and Notes sections.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes hardening startup behavior during partial Windows updates.
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.

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.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: ce9cc65c-05df-41d3-9381-d273895a1ebd

📥 Commits

Reviewing files that changed from the base of the PR and between 5fe3aaf and 8e6f05c.

📒 Files selected for processing (5)
  • config/scripts/electron-vite-output-contract.test.ts
  • electron.vite.config.ts
  • src/main/startup/bootstrap-fatal-exit-guard.test.ts
  • src/main/startup/bootstrap-fatal-exit-guard.ts
  • src/main/startup/main-process-error-guards.ts

Comment thread electron.vite.config.ts
Comment on lines +9 to +19
const BUNDLED_MAIN_DEPENDENCIES = new Set([
'@xterm/headless',
'@xterm/addon-serialize',
// Why: Windows NSIS deploys app.asar before external resources; bootstrap must
// not race the later resources/node_modules copy.
'zod'
])
const EXTERNAL_MAIN_DEPENDENCIES = Object.keys(packageJson.dependencies).filter(
(dependency) => !BUNDLED_MAIN_DEPENDENCIES.has(dependency)
)
const BOOTSTRAP_FATAL_EXIT_GUARD_KEY = '__ORCA_BOOTSTRAP_FATAL_EXIT_GUARD__'

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.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Guard key is duplicated instead of imported from the canonical module.

BOOTSTRAP_FATAL_EXIT_GUARD_KEY is redeclared here with the same literal already exported from src/main/startup/bootstrap-fatal-exit-guard.ts. The values currently match, but if they ever drift, removeBootstrapFatalExitGuard() would silently fail to find the installed guard (the runtime lookup uses a different key than the one baked into the bundled banner), leaving the process.once('uncaughtException', ...) handler permanently attached alongside the durable handler installed later — the first post-bootstrap uncaught exception would then hit process.exit(1) before the durable handler's crash recording/re-throw can run.

Import the constant from the single source instead of redefining it.

♻️ Proposed fix
+import { BOOTSTRAP_FATAL_EXIT_GUARD_KEY } from './src/main/startup/bootstrap-fatal-exit-guard'
+
 const BUNDLED_MAIN_DEPENDENCIES = new Set([
   '`@xterm/headless`',
   '`@xterm/addon-serialize`',
   // Why: Windows NSIS deploys app.asar before external resources; bootstrap must
   // not race the later resources/node_modules copy.
   'zod'
 ])
 const EXTERNAL_MAIN_DEPENDENCIES = Object.keys(packageJson.dependencies).filter(
   (dependency) => !BUNDLED_MAIN_DEPENDENCIES.has(dependency)
 )
-const BOOTSTRAP_FATAL_EXIT_GUARD_KEY = '__ORCA_BOOTSTRAP_FATAL_EXIT_GUARD__'
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const BUNDLED_MAIN_DEPENDENCIES = new Set([
'@xterm/headless',
'@xterm/addon-serialize',
// Why: Windows NSIS deploys app.asar before external resources; bootstrap must
// not race the later resources/node_modules copy.
'zod'
])
const EXTERNAL_MAIN_DEPENDENCIES = Object.keys(packageJson.dependencies).filter(
(dependency) => !BUNDLED_MAIN_DEPENDENCIES.has(dependency)
)
const BOOTSTRAP_FATAL_EXIT_GUARD_KEY = '__ORCA_BOOTSTRAP_FATAL_EXIT_GUARD__'
import { BOOTSTRAP_FATAL_EXIT_GUARD_KEY } from './src/main/startup/bootstrap-fatal-exit-guard'
const BUNDLED_MAIN_DEPENDENCIES = new Set([
'`@xterm/headless`',
'`@xterm/addon-serialize`',
// Why: Windows NSIS deploys app.asar before external resources; bootstrap must
// not race the later resources/node_modules copy.
'zod'
])
const EXTERNAL_MAIN_DEPENDENCIES = Object.keys(packageJson.dependencies).filter(
(dependency) => !BUNDLED_MAIN_DEPENDENCIES.has(dependency)
)

@OrcaWin

OrcaWin commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator Author

Electron validation (Windows)

Validated PR head 8e6f05c from the intended prowfish @ OrcaWin/diagnose-git-issue dev instance.

  • Visible behavior: the full Orca window rendered normally, then navigated through the real Orca Mobile surface; Playwright reported 0 renderer console errors.
  • Backing state: first-window startup services settled, runtime graph status was ready, desktop status was available, and the Windows runtime WebSocket listener reported ready on port 6769.
  • Build evidence: pnpm run build:desktop passed; emitted out/main/index.js starts with the fatal guard before diagnostics/generated code and has no bare require("zod"). Targeted startup/error-guard tests passed 17/17.
  • Remaining gap: the live NSIS partial-copy timing window was not manually recreated in the dev app; the packaged Windows crash-survival CI job covers that scenario and passed.
Normal startup Interactive Mobile surface
Orca normal startup Orca Mobile surface

@brennanb2025
brennanb2025 merged commit d73c613 into stablyai:main Jul 30, 2026
49 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