fix(cloud): meter every box session (M8)

Merged
SA
20h ago
+792-127

What and why

79 ws- box sessions in 7 days ran with no cloud_workspace_boots row, so they were never billed (about 6,256 GB-minutes; "W0: measurements", item 7). This PR gives every session that runs a boot row with its vercel_session_id, billed once.

  • Promote restart (69 sessions). promoteSandboxToEnvironment took a snapshot, which ends the session, and then ran the boot command on the source with no authorizeBoot. Now it leaves the source stopped and returns the session it ended. After saving the environment row, environment.promote starts the box again with wakeAfterStop, which is the same path restartCloudWorkspace uses: record asleep, settle the old boot, then a normal wake (authorizeBoot in beforeResume, then attachBootSession). If the restart is refused or fails, it writes a console line (and a tagged Sentry capture unless it is a usage refusal), the box stays asleep, and the environment is still saved.

  • Resume race (9 sessions). A call on a Sandbox object that was read as running resumes the session if it has just stopped (the SDK's withResume). wakeSandbox now passes onResume. For such a session it runs the wake's own beforeResume once its calls end, so the wake's existing authorize and attach do the work. On a refusal for a session this call resumed (and only then), the wake records that session awake, stops it, records it asleep for that session, and throws the refusal. A refusal for a box another wake authorized and resumed stops nothing.

  • Reconcile backstop. adoptRunningBoxes missed these sessions for three reasons:

    1. It skipped any workspace with an open boot, and the boot of the session that had just ended stayed open until a settle that the same run queues later.
    2. It looked only at the newest session, and only while it was running.
    3. It billed an adopted session from the adopt time, not from the session's start.

    Now the run queues every open boot's settle first. Then the adopt pass (RECONCILE_CONCURRENCY workspaces at a time) opens a boot for every recent session that no boot row names, billed from the session's start, and queues its settle. It leaves the newest session to an unattached boot that is still in flight. A running session takes its lock before the insert, and the insert writes lockOpen in the same statement. A conflict on the unique vercel_session_id, or a failed insert, releases the lock.

  • Preparation commit 36626c31 (no behaviour change): one reserveLock(boot, memoryMb, user) replaces the copies of the Autumn lock call in authorize and in the settle renewal; the adopt in the fix commit uses it too.

Diff against main (after merging #9928, #9940, #9929, #9941 and #9944): 13 files, +788 / −121. Production code is 5 files, +276 / −108; the rest is tests (+483) and docs. No control-log changes: the new paths use logControl("meter.cap_stopped", …, "warn"), console lines, and Sentry.captureException with controlTags, as #9936 does. The unused adopt_failed release reason (now in meter.ts) is removed.

Billing decision for the owner

Sessions that our own control-plane calls resumed are billed to the customer from the session's start. Examples: a promote's snapshot() on a box that had just stopped, or a withResume call (writeFiles, runCommand, extendTimeout) that lands just after a timeout stop, including on archive or delete paths. The reconcile adopts every such session and bills the workspace's organization. The customer did not ask for those minutes; our code started them. Whether to bill them, absorb them, or skip short ones is your decision. This PR bills them, so that the counts match Vercel's.

RECONCILE_LOOKBACK_MS is now 2 h (the attach grace plus one period), so one missed reconcile run cannot leave a short session unbilled.

What breaks, for whom

Nothing for clients: no tRPC procedure input or output changed. promoteSandboxToEnvironment now takes organizationId instead of claim (internal only). environment.promote now waits for host-service: the restart is a full wake, awaited after the environments row is written. The mutation returns only after host-service in the restarted box answers and its managed environment is pushed. On a cold boot this can take a minute or more: the boot itself, plus up to the wake's 60 s host-ready timeout. Because the row is written first, a promote that runs past the route's 300 s still leaves the environment saved, not a golden without a row. A restart that fails or is refused does not fail the promote. Adopted sessions are now billed from their start, which changes the design page's note that adopted boxes are billed only from adoption.

Evidence

Checks: the CI commands run locally, all green except 3 test failures in agent-setup/host-service that also fail on main on that machine (details on the evidence page); packages/trpc 1010 pass, 0 fail, typecheck for trpc and sandbox, biome check, check:plugins. verify-sandbox-asleep: no error with 1,624,931 states; the broken config fails as expected.

Not verified: a real Vercel resume or a real promote, and real Autumn calls. The past unbilled usage is not backfilled.