Finish the destroy work where a Cloud machine is first found gone

Merged
TE
2mo ago
+290-59

Three code paths retire a Cloud machine's row when the provider reports it no longer has the machine: the status read (getVm, behind GET /api/vm/[id]), the resume preflight that every access operation runs (attach, exec, ssh, open-port, fork, resize), and the stats read. Each one wrote the row's new status and stopped there. The reconcile cron performs the same transition and also revokes the machine's model-plane route tokens and records a vm.destroyed ledger event.

That gap could not be repaired later. destroyed is terminal: findUserVm filters destroyed rows out, so a DELETE /api/vm/[id] for that machine answers 404 without running destroyVm's revoke or its ledger write, and reconciliationCandidates also filters them out, so the cron never looks at the row again. Whichever of the three paths noticed the machine first left a machine that is absent from the destroy ledger for good, with its route-token rows still marked unrevoked.

After this change all three go through one applyObservedProviderStatus, which performs the status write and, when that write lands on destroyed, does the same revoke and ledger event the cron does. The status and stats routes hand their workflows the model-plane revoker they already build for DELETE. The provider_status_read, provider_status_access and provider_status_stats reasons join the analytics allowlist, so the event says which read noticed instead of degrading to unknown.

A provider 404 does not always mean destroyed

The helper derives the row's new status itself, from observedDbStatus, rather than accepting one from the caller. That is the part worth reading closely, because getting it wrong is worse than the bug this PR set out to fix.

A 404 from the provider on a machine with a persistent home volume means the compute is gone and the volume is not. observedDbStatus, which getVm and the reconcile cron have always used, maps that case to paused rather than to a terminal status. The preflight and stats paths used to hardcode destroyed, disagreeing with both of them, which terminalized such a row and would have billed a vm.destroyed for a machine the provider never destroyed. Nothing revisits a terminal row, so neither could be taken back. An earlier revision of this PR kept that hardcoding and added the ledger write on top of it, which would have made the damage permanent and billable instead of merely permanent; the review caught it.

reopenBaseIfProviderDeleted is the one caller that overrides the mapping, through an explicit forceStatus, and the reason is in a comment at the call site: its row is a Base's active generation, and beginBaseOpen only allocates a replacement once this row has stopped being a machine the Base could open. Leaving it paused would hand the same dead provider id back on every later open, forever. The home volume is not lost by that: beginBaseOpen retains the old generation rather than deleting it, which is where a normal reset leaves it too.

What this does and does not fix

The ledger consequence is the substantive one. vm.destroyed is what cloud_vm_destroyed product analytics is built from, including machine lifetime, so machines whose disappearance was noticed by a read rather than the cron were missing from it entirely.

The credential side is narrower than it looks, and worth stating plainly rather than overselling: a route token for a row that reached destroyed was already unusable. authenticateVmAuthorization joins the token to cloud_vms and requires status in ('provisioning','running','paused'), and no write can move a row out of destroyed, so the missing revoke left an inaccurate revoked_at IS NULL row rather than a working credential. The revoke is now accurate, and it still matters for the interval before the row goes terminal. No billing or money path depends on the missing event; billing stop keys on the row's own status and destroyed_at, which these paths did already write.

Two consequences of routing the preflight through the shared mapping are worth naming, because both are behavior changes and neither is an improvement on its own.

Route tokens now outlive the observation on a volume-backed machine. authenticateRouteToken and authenticateVmAuthorization accept provisioning, running and paused, so a row that lands on paused keeps usable route tokens for the rest of their 30-day lifetime. The old hardcoded destroyed at this one call site killed them immediately. The preflight still threads no revoker, deliberately: revoking here would put one of its eight call sites at odds with getVm and the cron, which leave tokens alive for exactly the same observation, and a sleeping machine is supposed to keep its tokens. A volume-backed row that must lose its tokens has to be destroyed, by the user or by account deletion, both of which revoke.

The preflight was also the last automatic path that retired a volume-backed row whose sandbox the provider had removed. Such rows now stay paused: the cron's reconciliationCandidates sees the observed status already matches and reports unchanged, and they keep counting against a Go plan's saved-machine allowance. A user can still delete them. I left this as is because the alternative is to keep one entrypoint disagreeing with the other two about what a 404 means, which is the bug at the top of this description; the durable fix is a recovery or retirement path for volume-backed rows whose compute is gone, which is larger than this PR.

Validation

Run from web/, against a real PostgreSQL 16 with the repo's migrations applied, so the CMUX_DB_TEST=1 database cases execute rather than skip.

Red, at the tests-only commit ecaf0697f14, bun test tests/vm-workflows.test.ts:

(fail) status read that observes a gone machine > revokes the model plane and records vm.destroyed, like the cron reconcile does
error: expect(received).toEqual(expected)
- [
-   "00000000-0000-4000-8000-000000000150",
- ]
+ []

(fail) status read that observes a gone machine > an access preflight that retires the row records vm.destroyed too
error: expect(received).toEqual(expected)
- [
-   "vm.destroyed",
- ]
+ []

Red again for the home-volume defect the review found, with the tests of the final commit against the code of the one before it (fa6b69c58cf), bun test tests/vm-stats-not-found.test.ts:

(fail) stats provider missing classification > a machine with a home volume is paused rather than destroyed, and bills no destroy
error: expect(received).toEqual(expected)

@@ -3,3 +3,3 @@
    "providerVmId": "vm-fixture",
-   "status": "paused",
+   "status": "destroyed",
  }

 7 pass
 1 fail

Green at the head of this PR: bun test tests/vm-workflows.test.ts is 148 pass, 0 fail across 148 tests, with the database cases executing; bun test tests/vm-stats-not-found.test.ts is 8 pass, 0 fail.

Whole-suite, because the previous revision of this PR shipped two regressions in files I had not run. bun test over all of web/ at this branch is 3695 pass, 304 fail, 4000 tests across 370 files. At the merge base it is 3679 pass, 305 fail, 3985 tests across 369 files. Those ~300 failures are an artifact of running 370 files in parallel against one database, where the dbTest files truncate shared tables out from under each other; the number is what matters and the comparison is the point. Diffing the two runs by failing test name, no test fails on this branch that does not already fail at the merge base.

Also from web/: bun x tsc --noEmit clean; bun run lint:complexity passes with the baseline untouched at 42 grandfathered findings and no new entry.

No fleet dogfood. cmux#8029 disabled Vercel branch previews while keeping main deployments, so an unmerged change to this app has no preview URL an app build could talk to, and a fleet build would exercise main rather than this branch. The database-backed runs above are the substitute.

Changelog

Fixed: a Cloud machine the provider had already removed could be recorded as gone without its destroy being logged or its model-plane tokens marked revoked, and a sleeping machine with a saved home directory could be wrongly recorded as destroyed.

🤖 Generated with Claude Code