Skip to content

feat(daemon): observe-only fork gate for automatic terminal restores - #27424

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

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

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Oct 11, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​391 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​391
Prod 8 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​448 $\color{#cf222e}{\Huge{\mathbf{−}}}$​7 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​441

ELI5

When Orca reconnects to a host, it can restart many saved agent sessions at once. Nothing on the host keeps count, so a big restore can start far more processes than the machine can handle. This PR adds a counter at the one place where a terminal process is actually started. For now it only watches and writes down what it would have done. It never slows down or blocks anything.

What Changed

  • Before: the terminal daemon started every terminal any client asked for, immediately. It kept no record of how many were automatic restores or how many of those were still running.
  • After: the same requests start just as fast. The daemon now passes each real process start through a gate that classifies it (interactive shell, user resume, or automatic restore). It counts how many automatic restores are in flight and alive, and logs spawn-admission-observed to the daemon log whenever the policy would have made a restore wait, held it back, or refused it.

Mechanism:

  • src/shared/host-spawn-admission.ts is a pure, clock-injected policy. Defaults: 3 automatic restores in flight, 1 per second with a burst of 3, and 8 automatic sessions alive. A user resume gets its own small per-client budget. It is shared code so that other execution owners can reuse it.
  • The daemon calls the gate only on the branch of createOrAttachTerminalSession that is about to fork, after the live-session attach and attachOnly returns. Attach and agent-session adoption are never charged.
  • Older callers do not say why they are starting a terminal. For them the daemon infers "automatic restore" only when the request carries both an agent-session claim and a history seed. Otherwise it treats the request as interactive.
  • A session that exits frees its live slot. A fork that throws or is cancelled frees its in-flight slot.

Why

This is the observe-only first step of the headless-serve restore budget (#10493: a restore storm that started thousands of processes). The design puts the gate at the creation branch rather than in the main process, because only that branch knows a process is about to be created. The main process cannot tell an attach from a cold restore, and its count would reset whenever it restarts. Shipping the counting first lets us check the 3 / 1 per second / 8 defaults against real logs before anything waits or is deferred. Enforcement, the client "restore paused" state, and the crash pause come in follow-up PRs.

Linked Issue

Refs #10493 (first step; does not close it)

Visual Proof

N/A: observe-only. No user-visible behavior changes; the only output is new daemon log lines.

Testing

  • Automated tests added/updated
  • src/shared/host-spawn-admission.test.ts: inference, pacing, concurrency, the per-client user-resume budget, and pause/pressure/live-cap deferral for callers that negotiated it. Callers that did not are never deferred and are refused only at critical pressure. A seeded model test (M-1) checks the pacing and live-cap invariants over random arrivals.
  • src/main/daemon/terminal-spawn-gate.test.ts: through the real TerminalHost. Every restore is still admitted while would-wait decisions are counted. An attach is never charged. A session exit frees its live slot. A throwing fork frees its in-flight slot. A plain shell is not counted.
  • Existing daemon suites pass, including the spawn-input retention tests: the exit hook does not retain request inputs.
  • pnpm tc, pnpm lint and pnpm run check:code-quality:changed pass.

Review

Agent skill upstream boundary

  • Not applicable

Notes

  • Wire: nothing changes on any protocol. The daemon request gains no field yet. The facts come from what the daemon already receives.
  • SSH: current clients run the same daemon (via managed orcad) on SSH hosts, so the gate sits on the execution host there too. Remote forks are never counted against a local budget.
  • Windows/WSL: WSL panes are wsl.exe forks on the Windows daemon, so they are counted there. No platform-specific code is involved.
  • Folder workspaces: the gate keys on process creation, not Git.
  • Execution boundary: the gate never reports exits. It only listens to the daemon's own exit events.

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

@coderabbitai

coderabbitai Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3571dd3f-14b6-42b1-b90a-5c1192607cd8

📥 Commits

Reviewing files that changed from the base of the PR and between 6529db539b5258a9bec7912e4816225073530ef3 and de52e1b.


You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

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: 0328cc86-fed8-4c5a-b0ff-c6fbfc75ccf2


📥 Commits

Reviewing files that changed from the base of the PR and between 4bb67b3 and 6529db539b5258a9bec7912e4816225073530ef3.



📒 Files selected for processing (4)
  • src/main/daemon/terminal-host-session-create.ts
  • src/main/daemon/terminal-spawn-gate.test.ts
  • src/shared/host-spawn-admission.test.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; 1 remain after this review.




📝 Walkthrough
📝 Walkthrough

Walkthrough

The change adds a shared policy for resolving terminal spawn charges and admission decisions. Daemon terminal creation now supplies client identity and a deadline. TerminalHost passes an optional TerminalSpawnGate to session creation. The gate records policy observations while allowing forks immediately, tracks leases and live automatic sessions, and releases tracked sessions on exit. The change also adds policy and gate tests.



Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to 6529d

No merge-blocking issue is established; this observe-only change is ready for normal checks.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 26.32% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 19 functions across 10 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check Passed The title clearly identifies the main change: an observe-only fork gate for automatic terminal restores.
Description check Passed The description is complete and aligned with the template. It explains the behavior, mechanism, rationale, linked issue, testing, visual proof, cross-platform considerations, and checklist status.
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 💡 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