Skip to content

perf(orcad): spawn the terminal daemon as soon as the login env lands - #35341

Merged
nwparker merged 2 commits into
mainfrom
nwparker/orcad-daemon-early
Oct 28, 2026
Merged

nwparker merged 2 commits into
mainfrom
nwparker/orcad-daemon-early

Conversation

@nwparker

@nwparker nwparker commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​38 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​37
Prod 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​22 $\color{#cf222e}{\Huge{\mathbf{−}}}$​11 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​11

ELI5

When Orca's server (orcad) starts, it also starts a background "terminal daemon" that owns the terminals. Today it waits to start the daemon until its settings store and hook server are up, even though the daemon doesn't need either. This starts the daemon earlier, at the same time as those, so orcad is ready sooner after a WSL distro restart or an SSH host reconnect.

What Changed

  • startOrcadRuntime now starts startOrcadDaemon() as soon as the login-shell environment adoption resolves. That runs in parallel with the CLI launcher, secret store, profile store and hook server startup. Startup still awaits the daemon at the same point as before, before PTY handlers register, so the provider is installed before the first PTY call, as today.
  • The daemon cleanup waits for an in-flight spawn to settle (ignoring its failure) before stopOrcadDaemon() disconnects.
  • The login-env adoption and its logging move into adoptWslHostLoginEnvironmentLogged() (same behavior). This keeps orcad-entry.ts under its line cap.

Why

orcad startup is now the dominant phase on the WSL host restart path: 1.1–1.75 s to readiness on lane run 40763093979. The daemon step takes 190–250 ms of that and depended on nothing between the login env and its old position:

  • The daemon and every PTY inherit process.env at spawn. I checked every production process.env write, and between orcad's start and the old daemon position the only one is the login-shell env overlay (adoptWslHostLoginEnvironment). The daemon still waits for it.
  • initDaemonPtyProvider reads no profile settings and no hook-server state. PTY hook coordinates are injected per spawn, later.

Terminal restore: unchanged. Adoption of surviving daemon sessions happens inside the same startOrcadDaemon(), and reconciliation (reconcileLegacyWorkerTerminals, refreshRestoredOrchestrationAuthority) still runs after it, at the same point. Only the daemon's spawn and connect move earlier.

Failure ordering:

  • The daemon fails while the hook server is starting: startOrcadDaemon reports unavailable/degraded without throwing, which startup reads at the old await point, as before. If it does throw (only mocks do), startup fails at that await, as before. The existing test covers this.
  • The hook server (or the profile) fails while the daemon is spawning: startup rejects, and the daemon cleanup waits for the spawn to settle, then disconnects. New test.
  • If startup aborts after the daemon has spawned, the detached daemon keeps running, and the next orcad start adopts it like any surviving daemon. That is the same outcome as an orcad crash after the old daemon step. Accepted per review.
  • The daemon promise is observed (.catch(() => {})) as soon as it is created, so a future change that makes it reject can't become an unhandled rejection on an early abort.

SSH effect: orcad-wide. Managed SSH hosts get the same saving on every orcad (re)start. Terminal adoption, the env the daemon inherits and failure reporting are unchanged.
Flag-off: applies whether or not ORCA_INTERNAL_WSL_HOSTS is set, because orcad on SSH hosts and the desktop's bundled orcad run this code.
User-risk: low to medium. This is an ordering change in orcad startup. The daemon's inputs are unchanged, and the await point that gates PTY routing is unchanged. Two reviewers, please.

Linked Issue

Internal WSL-via-orcad tracker, D3 bar (restart → first usable prompt).

Visual Proof

N/A: no UI.

Testing

  • orcad-push-startup.test.ts:
    • new: the daemon spawns while the hook server is still starting;
    • new: if the hook server fails during a daemon spawn, the disconnect waits for the spawn to settle;
    • existing: a daemon failure after hook setup still fails startup and releases the profile;
    • all 9 pass.
  • Duplicate-pty repair startup, stop-terminal admission, login-env, daemon-supervision and startup-timing suites pass. pnpm tc, typecheck:ci, code-quality, anti-slop, ast-grep and the ast-grep gates pass.
  • Lane re-measure: this PR's WSL2 run. The orcad startup phase (orcadStartup timing: daemon start right after login-env) and medianHostPhases.orcadStartupMs should drop by about 100–150 ms. I'll post the numbers here.
  • Automated tests added/updated

Status

Ready for two reviewers. Waiting for the lane re-measure.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • N/A for visuals with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote impact considered (orcad-wide; same inputs and await point)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (CI runs the rest)

orcad started its terminal daemon only after the secret store, profile and hook server were up.
The daemon (and every PTY) inherits process.env, which startup changes only through the
login-shell env adoption, so it now spawns as soon as that resolves, in parallel with the stores
and the hook server, and startup awaits it where it did before. A cleanup waits for an in-flight
spawn to settle before disconnecting. The login-env adoption and its logging move into
adoptWslHostLoginEnvironmentLogged.
@nwparker

Copy link
Copy Markdown
Contributor Author

STATUS: ready for two reviewers. Head 545e708.

Checks run: pnpm tc, typecheck:ci, code-quality:changed, anti-slop, oxlint, ast-grep and its gates, plus each touched orcad suite run on its own (push-startup 9/9).

Open question: none. I'll post the lane re-measure of the orcad startup phase on this PR when the run finishes.

@coderabbitai

coderabbitai Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 22 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 45be52a2-c76e-41c9-96fc-384ab0cc47af
Commits

Reviewing files that changed from the base of the PR and between 7b3fb54 and 6273490.

Files selected for processing: 3
  • src/main/orcad/orcad-entry.ts
  • src/main/orcad/orcad-push-startup.test.ts
  • src/main/orcad/orcad-wsl-host-login-environment.ts
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@greptile-apps

greptile-apps Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The latest change adds no blocking issue; the earlier cleanup gap remains non-blocking.

Findings

  1. P2 Daemon can miss cleanup ▶

Summary

Starts the terminal daemon after the login environment is ready, alongside other startup work.

  • Orcad starts its terminal daemon while the rest of startup runs.

Reviews (2) · Last reviewed commit: "fix(orcad): observe the daemon startup p..." · Reviewed by Greptile

const { startOrcadDaemon, stopOrcadDaemon } = await import('./orcad-daemon-supervision')
// The daemon and its PTYs inherit the adopted login env and nothing later startup writes, so it
// spawns as soon as that lands, in parallel with the stores and the hook server. Never rejects.
const daemonStartup = loginEnvironment.then(() => timed('daemon', () => startOrcadDaemon()))

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 Daemon can miss cleanup

daemonStartup now starts before its cleanup is registered. If one of the following imports fails, startup runs cleanup without waiting for or disconnecting the daemon. The pending work can then install a provider after startup has failed and released the host lock.

Register cleanup before yielding to those imports, and add a test for that failure window.

An early abort may never reach the await, so a future change that makes the spawn reject must
not surface as an unhandled rejection.
@nwparker
nwparker merged commit cf794d9 into main Oct 28, 2026
85 of 87 checks passed
@nwparker

Copy link
Copy Markdown
Contributor Author

STATUS: lane re-measure, run 40766963243 at 6273490, compared with main run 40763093979.

The daemon now starts with the cli-launcher and overlaps the profile, as intended. For example, ubuntu runs went from daemon 773–962 to daemon 972–1245, where cli-launcher starts at 967.

The net saving is small:

  • Profile-end to orchestration-start dropped from ~250–270 ms to ~170–190 ms.
  • But the profile step slowed from ~75–120 ms to ~100–170 ms, because the daemon's Node start competes for CPU on the 2-vCPU runner.
  • Net about 50–100 ms. readyMs medians, ubuntu / debian / fedora: 1160 / 1609 / 1720 before, 1511 / 1601 / 1185 after. Run-to-run variance is about ±0.3 s, so the gain is within noise.

Expected was 100–150 ms.

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