Skip to content

feat(daemon): pace automatic terminal restores on headless hosts - #27428

Merged
OrcaWin merged 2 commits into
mainfrom
np/d1-spawn-gate-enforce
Oct 11, 2026
Merged

OrcaWin merged 2 commits into
mainfrom
np/d1-spawn-gate-enforce

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Oct 11, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​147 $\color{#cf222e}{\Huge{\mathbf{−}}}$​6 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​141
Prod 14 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​232 $\color{#cf222e}{\Huge{\mathbf{−}}}$​65 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​167

ELI5

On a headless Orca host (orca serve, or Orca running on an SSH machine), reconnecting a client can restart dozens of saved agents at once. With this PR the host restarts them a few at a time instead of all together. Shells you open yourself still start immediately.

What Changed

Stacked on #27424, which added an observe-only gate. Review only the top commit.

  • Before: on a headless host every automatic restore forked at once, so a reconnect with 40 saved agents forked 40 agents together.
  • After, headless hosts (Electron orca serve and orcad, including orcad on SSH hosts): automatic restores pass through the gate in enforce mode. At most 3 are in flight at once, at 1 per second after a burst of 3.
    • A request that has to wait sits inside the daemon's existing spawn preparation. It leaves the queue without forking when its caller cancels.
    • A paced request is admitted no later than halfway through its caller's cancellation window. That leaves the rest of the window for the fork itself, and it is never refused because of pacing.
  • After, desktop: unchanged. The gate still only observes, for one release, until the desktop client handles deferral.
  • Never queued: interactive shells and attaches are never queued. A user resume has its own small budget and is admitted at its deadline rather than refused.

Mechanism:

  • DaemonLaunchPolicy.spawnAdmission is set by the launcher: enforce when state.isServeMode or in orcad, observe on desktop. It reaches the daemon as --spawn-admission enforce on the daemon's own command line, not as an environment variable, because the daemon's environment becomes every shell's environment.
  • TerminalSpawnGate.admit returns synchronously when no wait is needed, so an unpaced fork keeps today's timing against the host's shutdown fence. When it does wait, the creation re-checks the shutdown fence and cancellation afterwards.
  • New optional wire field spawnIntent on PtySpawnOptions and on the daemon createOrAttach payload. Nothing sets it yet. When present, it lets the host answer terminal_spawn_deferred:<reason> (paused, live cap, pressure, or deadline) instead of pacing. The follow-up client PR sets it and keeps the pane sleeping when it gets that answer.
  • A caller that did not declare an intent and hits critical pressure gets a distinct terminal_spawn_refused error. It is deliberately not the "Daemon temporarily unavailable; reconnect" message: main treats that message as "daemon gone" and would replace a healthy daemon. Nothing produces critical pressure yet; the growth breaker PR adds it.

Why

This is the restore budget for #10493 (a restore storm forked thousands of processes and panicked the machine), applied where it is safe today.

  • Enforced only on headless hosts: only there do automatic restores come from remote clients that the host cannot slow down any other way. SSH hosts have had no cap since the relay was removed.
  • Pacing only for current clients: a client that has not declared an intent clears its sleeping record before it spawns. Refusing it could lose the user's resume handle, so it is only ever paced.
  • Live cap and crash pause wait for the client PR: they need a client that keeps the pane sleeping, so they apply only to declared requests.

Linked Issue

Refs #10493

Visual Proof

N/A: no UI change. On a headless host, automatic restores after a reconnect now appear over a few seconds instead of all at once.

Testing

  • Automated tests added/updated
  • terminal-spawn-gate.test.ts (enforce):
    • an undeclared storm of 5 forks 3, then 1 per second;
    • an undeclared restore is admitted at its deadline rather than refused;
    • a declared restore beyond the live cap of 8 gets terminal_spawn_deferred:live_cap, and is admitted once a live one exits;
    • an interactive shell is never queued behind a storm;
    • a cancelled waiter leaves the queue without forking and leaks no slot.
  • daemon-pty-adapter-spawn-intent.test.ts: spawnIntent reaches the daemon payload and is absent when not declared. The launcher flag is parsed, and any other value means observe.
  • Existing daemon, orcad and startup suites pass, including concurrent-create shutdown fencing, which is why admission stays synchronous when unpaced.
  • pnpm tc, pnpm lint and pnpm run check:code-quality:changed pass.

Review

Agent skill upstream boundary

  • Not applicable

Notes

  • Mixed versions (docs/reference/remote-wire-compatibility.md): spawnIntent is an optional field.
    • An old daemon ignores it.
    • An old main omits it and gets inference plus pacing.
    • A preserved old daemon has no gate until it restarts.
    • Remote RPC params are untouched in this PR.
  • SSH: orcad on an SSH host enforces on that host. Remote forks never count against a local budget.
  • WSL / Windows: WSL panes are wsl.exe forks on the Windows host's daemon, and are paced there. No platform-specific code.
  • Folder workspaces: covered, because the gate keys on process creation.
  • Execution boundary: a waiting or deferred request has no process and never produces an exit event. Only the daemon's own exit events release live slots.

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
  • pnpm lint, pnpm typecheck, and focused tests pass locally; CI covers the rest

@OrcaWin
OrcaWin force-pushed the np/d1-spawn-gate-enforce branch from a92fcb5 to f91e598 Compare October 11, 2026 09:35
@OrcaWin
OrcaWin changed the base branch from np/d1-spawn-gate-observe to main October 11, 2026 09:35
@coderabbitai

coderabbitai Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 694b394c-9b56-4bbe-9a11-4e6483ea434d


📥 Commits

Reviewing files that changed from the base of the PR and between f8ea867 and f91e598.



📒 Files selected for processing (17)
  • src/main/daemon/daemon-launched-child-spawn.ts
  • src/main/daemon/daemon-main.ts
  • src/main/daemon/daemon-out-of-process-launcher.ts
  • src/main/daemon/daemon-pty-adapter-spawn-intent.test.ts
  • src/main/daemon/daemon-pty-spawn-request.ts
  • src/main/daemon/daemon-server-options.ts
  • src/main/daemon/daemon-server.ts
  • src/main/daemon/daemon-terminal-admission.ts
  • src/main/daemon/terminal-host-session-create.ts
  • src/main/daemon/terminal-spawn-gate.test.ts
  • src/main/daemon/terminal-spawn-gate.ts
  • src/main/daemon/types.ts
  • src/main/orcad/orcad-daemon-supervision.test.ts
  • src/main/orcad/orcad-daemon-supervision.ts
  • src/main/providers/pty-provider-contract.ts
  • src/main/startup/main-process-pty-startup.ts
  • src/shared/host-spawn-admission.ts


Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.




📝 Walkthrough
📝 Walkthrough

Walkthrough

Spawn requests can now include a validated intent, and the daemon can enforce spawn-gate decisions instead of only observing them. Enforced admission supports pacing, deadlines, cancellation, and lease settlement. Startup and child-launch options configure the admission mode; serve mode and Orcad startup select enforcement.



Priority: ➖ Normal

Merge Risk: 🟡 Moderate · up to f91e5

Headless hosts that reuse an existing daemon may not pace automatic terminal restores. Reconcile the daemon’s admission mode on adoption before merging, unless this limitation is explicitly accepted.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 21.43% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 14 functions across 17 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
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.
Title check Passed The title clearly and concisely describes the main change: pacing automatic terminal restores on headless hosts.
Description check Passed The description is mostly complete and explains the user impact, implementation, rationale, linked issue reference, testing, compatibility, and checklist items. The issue reference uses “Refs #10493” …

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR



  • 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.

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