Skip to content

Resume an active provider machine whose daemon is gone - #6160

Closed
alexandre-vl wants to merge 1 commit into
get-bb:mainfrom
alexandre-vl:fix/resume-active-machine-without-daemon
Closed

alexandre-vl wants to merge 1 commit into
get-bb:mainfrom
alexandre-vl:fix/resume-active-machine-without-daemon

Conversation

@alexandre-vl

Copy link
Copy Markdown
Contributor

Human comments

What was wrong

When the daemon of a provider-managed machine dies while core still records the machine as active, nothing ever repairs it:

  • resumeMachineWithIntent returns early for every phase except suspended, suspending and resuming (provider-orchestration.ts).
  • The lifecycle sweep resumes a machine only when suspendedAt is set and a follow-up waits on it (provider-orchestration.ts).
  • A follow-up sent to a thread on the machine parks on a host-offline wait with no schedule (dispatch-attempt.ts). Only a daemon reconnect releases it.

This happened on a berth sandbox: earlyoom sent SIGTERM to the daemon, its launcher exited 0, and the thread stayed waiting-for-host with its message queued until someone restarted the daemon by hand.

#6159 makes the launcher restart a daemon after such a kill. This PR is the server-side recovery for every case the launcher cannot cover: the launcher itself stopped, or a daemon that exited for any other reason on a machine with no service manager.

What changed

  • When a machine needs it (apps/server/src/services/machines/provider-orchestration.ts): activeMachineNeedsResume(deps, hostId) is true for a machine that meets all of these:

    • it is managed by a provider that implements resume;
    • it is active, with no connected daemon;
    • it was last heard more than HOST_RECONNECT_GRACE_MS (30 s) ago;
    • it is past suspendRetryAt;
    • it is not the server's own machine;
    • it has work: a host-offline queued message, or an active, starting or stopping thread. The thread check is machineHasRunningThreads, new in packages/db/src/data/machines.ts.

    The 30 s is measured from lastSeenAt, which heartbeats refresh every 5 s, so it also covers a daemon that died while the server was down.

  • How it is repaired: resumeMachine(deps, hostId, { includeActive: true }) runs the provider's existing resume for such a machine.

    • The resume contract already requires reusing compute that still runs.
    • The bootstrap that resume ends with runs the installer's --start, which returns as soon as a daemon answers.
  • Who asks for it:

    • dispatch: a follow-up that parks on host-offline now requests machine readiness (dispatch-attempt.ts);
    • the readiness chain resumes the machine before ensureHostSessionReadyForWork (queued-message-dispatch.ts);
    • the lifecycle sweep, every 10 s;
    • POST /hosts/:id/resume and bb machine resume, which did nothing for an active machine before.
  • Failures:

    • A failed attempt leaves the machine active with Machine resume failed: … and retries after 60 s. A suspended machine keeps its 10 s retry.
    • The sweep logs the failure instead of marking teardown failed.
    • bb machine resume exits 1 with that message instead of printing that the machine resumed. Its description now covers an active machine whose daemon is gone.
  • Docs:

    • packages/templates/src/templates/bb-guide-machines.md;
    • plugins/bb-guide/skills/bb-plugin-authoring/references/backend-machines.md;
    • the resume declaration comment in packages/plugin-sdk/src/machine-provider.ts;
    • the bb.experimental_machines and "Machine enrollment and bootstrap" entries in docs/api_to_audit.md. They gain a stabilization criterion: resuming an active machine must reuse its running compute, and must not restore an older snapshot over compute that is still there.
  • Compatibility:

    • No wire change: HOST_DAEMON_PROTOCOL_VERSION stays 251.
    • No new API member.
    • The SDK change is a comment. @get-bb/plugin-sdk 0.6.73 is not on npm yet, so the version guard passes without a bump. If 0.6.73 ships first, this needs node scripts/bump-plugin-sdk.mjs --patch.

For review

  • A machine whose daemon comes back some other way keeps its Machine resume failed: … message until the next successful resume (machine-status.ts). Until the retry time passes, a suspend of it is refused, for up to 60 s, because maintainMachine checks suspendRetryAt. Clearing both when the daemon reconnects would be a small follow-up.
  • After 30 s or more of server downtime, the sweep can resume a machine before its daemon redials; the daemon's reconnect backoff caps at 10 s. The resume is idempotent, but the machine shows "Resuming…" briefly and it costs one provider call.
  • A provider whose compute is already gone restores its last snapshot. For Modal, that rolls the filesystem back (plugins/environment-modal-sandbox/providers/modal/backend.ts). Hence the new stabilization criterion.
  • Work requests that need the machine now wait for a resume in flight instead of failing fast with 502, as they already do for a suspended machine.

How you verified

New tests, each failing on 1e80ca7b4 and passing here:

  • apps/server/test/services/machines/lifecycle-recovery.test.ts, 6 cases:
    • resumes when a follow-up waits, or when a thread runs;
    • does nothing within the grace, without work, or when the provider has no resume;
    • a failed resume stays active until its retry time, then retries.
  • apps/server/test/public/public-thread-offline-followup.test.ts: a follow-up sent after the grace resumes the machine and dispatches turn.submit. With only the dispatch trigger removed, it fails again (Tests 1 failed | 3 passed (4)), so it exercises dispatch, not the sweep.
  • apps/server/test/threads/machine-lifecycle.test.ts, 2 cases: the resume route resumes an active machine only when its daemon is gone.
  • apps/cli/src/__tests__/command-output/machine.test.ts: bb machine resume exits 1 with the failure message.

Failures on 1e80ca7b4:

expected "vi.fn()" to be called once, but got 0 times
Error: Timed out waiting for queued command; captured: none
AssertionError: expected { Object (id, name, ...) } to match object { lifecycle: { phase: 'resuming' } }
AssertionError: promise resolved "undefined" instead of rejecting

Totals:

Suite On 1e80ca7b4 Here
the 3 server files Tests 5 failed | 20 passed (25) Tests 25 passed (25)
the CLI file Tests 1 failed | 23 passed (24) Tests 24 passed (24)

Commands:

  • pnpm exec turbo run typecheck --filter=@bb/server --filter=@bb/cli --filter=@bb/db --filter=@get-bb/plugin-sdk --filter=@bb/templates: 11/11 tasks pass.
  • pnpm exec turbo run test with the same filters, plus --filter=bb-plugin-bb-guide --filter=bb-plugin-plugin-api-docs: 16/16 tasks pass; @bb/server has 3684 passed and 1 skipped.
  • node packages/plugin-sdk/scripts/check-npm-version-guard.mjs: pass.

Not run live: a real provider machine whose daemon dies.

AGENT GENERATED

A provider machine whose daemon died while its lifecycle phase stayed
active was never repaired: resume returned early for active machines, the
lifecycle sweep only resumed suspended ones, and a follow-up parked on a
host-offline wait that only a reconnect releases.

Core now calls the provider's resume for such a machine once its daemon
has gone unheard for the 30-second reconnect grace while a thread runs
there or a message waits for it: from dispatch, from the lifecycle sweep,
and from an explicit resume request. A failed attempt keeps the machine
active with the error and retries after a minute, and bb machine resume
reports that failure instead of printing success.
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

This is an automated message. We have closed this pull request because it has been open for more than two weeks.

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