Skip to content

feat(daemon): pause automatic restores after a same-boot crash - #27433

Merged
OrcaWin merged 2 commits into
mainfrom
np/d1-crash-pause
Oct 11, 2026
Merged

OrcaWin merged 2 commits into
mainfrom
np/d1-crash-pause

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Oct 11, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​211 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​211
Prod 7 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​251 $\color{#cf222e}{\Huge{\mathbf{−}}}$​4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​247

ELI5

If the terminal host crashes while it is restarting agents, restarting them all again straight away can crash it again. This happens while the machine itself is still up: the daemon was killed or ran out of memory. With this PR the host remembers that it died in the middle of automatic restores. After such a crash it stops restarting agents on its own until someone resumes them. Panes the user opens still start, and so do requests from current clients. A real reboot does not pause anything. After a reboot, restores go through the normal pacing.

What Changed

Stacked on #27428. Review only the top commit.

  • Before: nothing recorded how the daemon's previous run ended, so every restart restored at full width again.
  • After: the daemon keeps a small marker, automatic-work-run.json, in its runtime directory. The marker records:
    • the boot identity: Linux boot_id; on macOS, boot time derived from uptime, compared with a 2-minute tolerance. On Windows the boot is recorded as unknown, because Fast Startup keeps the uptime counter across a shutdown. A Windows daemon therefore never pauses until a reliable boot signal exists.
    • the run state: running (written durably, before the first automatic fork of the run) or clean-exit (written after an orderly shutdown has disposed every session);
    • the automatic-work mode.
  • At startup: if the previous daemon was running in the same boot, the new daemon starts with automatic work paused (unclean_host_death) and logs automatic-work-paused. A pause already on record is kept until an explicit resume. A new boot is never paused.
  • What a pause does: the gate defers automatic restores from callers that declared their intent (terminal_spawn_deferred:paused).
    • User resumes are still admitted.
    • Callers that did not declare an intent are never deferred for a pause (the Q9 rule). Their resume handle is cleared before they spawn, so they are only paced.
    • Nothing declares an intent yet, so this changes no behavior until the client PR.

Mechanism details:

  • There is one marker per daemon protocol version (daemon-v<N>.automatic-work-run.json, next to the PID record). An old-protocol daemon kept alive beside a new one can therefore never read the other's run as a crash.
  • The marker opens only after the daemon has won its endpoint (DaemonServer.start). A daemon that later loses its endpoint stops writing, so it never rewrites its successor's record.
  • A macOS login-session retirement exits without tearing down terminals (on purpose). It records an orderly exit first, so logging out and back in does not pause.
  • Writes are synchronous and durable (fsync, then rename). An asynchronous running write could otherwise land after the clean-exit write and fake a crash.
  • Fail-open: a marker that cannot be read or written never blocks a restore. Foreign or corrupt content reads as "no previous run", never as a pause.

Why

This implements the owner's decision on #10493 Q2: pause durably after an unclean death, but only when the same boot continued. A new boot without a clean exit goes through the normal budget, because Windows ends a detached, console-less daemon at reboot without any shutdown event. A "clean exit at shutdown" rule would therefore have paused every Windows reboot.

  • Why in the daemon: the execution host owns the marker, so a client that only lost contact (for example over SSH) can never record "unclean".
  • Why the daemon and not the CLI supervisor: the daemon is the process that dies with its terminals, and this covers desktop, orca serve, and orcad on SSH hosts alike.

Linked Issue

Refs #10493

Visual Proof

N/A: no UI change and no behavior change until a client declares intent. The only visible output is the automatic-work-paused daemon log line.

Testing

  • Automated tests added/updated
  • automatic-work-run-marker.test.ts: covers the following.
    • A pause only for running in the same boot.
    • No pause after a clean exit or on a new boot (boot id or boot time).
    • A recorded pause carries over until resumed.
    • Boot-time tolerance.
    • The parser rejects foreign content.
  • automatic-work-run-marker-store.test.ts: covers the following.
    • A real temp dir: crash with automatic work running, then pause.
    • An orderly exit, then no pause.
    • Nothing automatic ran, then no pause.
    • A new boot, then no pause.
    • A fork admitted before the endpoint was won arms on open.
    • Inert without a path or before open.
  • terminal-spawn-gate.test.ts: covers the following.
    • While paused, a declared restore is deferred, a user resume is admitted, and an undeclared caller is admitted.
    • The marker is armed before the first automatic fork only.
  • pnpm tc, pnpm lint, pnpm run check:code-quality:changed and the daemon suites pass.

Review

Agent skill upstream boundary

  • Not applicable

Notes

  • Mixed versions: the marker is a new file, and old daemons ignore it. A preserved old daemon writes none, so its crash is not remembered; protection starts with the next daemon. No wire change.
  • SSH: orcad on an SSH host keeps its own marker with that host's boot identity. Losing contact with a host is never recorded as a crash.
  • Windows: no boot guess, so no pause on Windows yet. A follow-up needs a boot identity that Fast Startup cannot fool. No PowerShell and no child process.
  • Folder workspaces: unaffected (keyed on terminal creation).
  • Execution boundary: the marker never reports any terminal as exited. It only decides whether automatic work may start.

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 changed the base branch from np/d1-spawn-gate-enforce to main October 11, 2026 10:01
@coderabbitai

coderabbitai Bot commented Oct 11, 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 5 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
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7e688759-28f7-4e60-b4e3-cfbd7001f20e

📥 Commits

Reviewing files that changed from the base of the PR and between 5651141 and 0706f29.


📒 Files selected for processing (10)
  • src/main/daemon/automatic-work-run-marker-store.test.ts
  • src/main/daemon/automatic-work-run-marker-store.ts
  • src/main/daemon/daemon-entry.ts
  • src/main/daemon/daemon-main.ts
  • src/main/daemon/daemon-server-options.ts
  • src/main/daemon/daemon-server.ts
  • src/main/daemon/terminal-spawn-gate.test.ts
  • src/main/daemon/terminal-spawn-gate.ts
  • src/shared/automatic-work-run-marker.test.ts
  • src/shared/automatic-work-run-marker.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.

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