feat(cloud): backfill legacy cloud workspace statuses (ready by a full Vercel listing, deleted by migration)

Merged
SA
7d ago
+18130-3

Links

  • Issue: SUPER-3018 · follows #9800 (expand step)
  • Stacked contract step: the draft PR that removes the old-spelling reads (linked in a comment below)

Summary

  • One-off job POST /api/cloud-workspaces/jobs/backfill-status moves every cloud workspace still at the old ready to awake or asleep. It classifies from a full listing of the Vercel team.
  • Migration 0195 moves every deleted row to archived, about 5,425 in production.
  • SDK bumped to 0.0.1-alpha.13, and the release runbook now names the API's legacy-status floors.

Why / Context

#9800 added awake/asleep/archived next to ready/deleted and made the API read both. This PR is the data half of the contract step.

  • ready can't simply be renamed. A ready row with a null expiry reads as awake, and the reconcile only lists boxes whose state changed in its one-hour lookback, so a box that stopped weeks ago is never reclassified by the sweep. The job asks Vercel about every box instead.
  • deleted has no such ambiguity, so a migration handles it.

At branch time, a fresh production copy had ready=349, deleted=5425, awake=44, asleep=356, archived=3, failed=1.

How It Works

The job (router/cloud-workspace/backfill-status.ts)

It reads every ready row, then listAllSandboxes(). That is the same Sandbox.list as the reconcile, but with no lookback cut-off and no tag filter, matching rows to boxes by name.

BoxWrite
running, expiry in the futurerecordSandboxAwake (the existing guarded path): awake with the box's expiry and session, a client nudge, and the expiry watch queued
stopped, or not listedbatched UPDATE … FROM (VALUES …), 200 rows per statement: asleep, sandbox_expires_at = now(), and the listed session id when Vercel gives one
starting, stopping or snapshottingleft at ready for the next run

Every write requires the row to still be ready, and every live writer moves a row off ready. So a wake that lands after the listing wins, and the job can be re-run safely. It returns and logs { ready, awake, asleep, missing, settling, remaining }.

The route is QStash-signed like the other cloud-workspaces/jobs/* routes and is not scheduled.

The migration (0195_cloud_workspace_status_archived.sql)

UPDATE "cloud_workspaces" SET "status" = 'archived' WHERE "status" = 'deleted';, generated with drizzle-kit generate --custom.

It is one statement, not batches. drizzle runs every pending migration in one transaction (db-migrations skill, rule 1), so batches inside it would not release any lock sooner. The statement locks only the rows it changes.

SDK and runbook

  • The SDK is unpublished, and its VERSION had stayed at 0.0.1-alpha.12. So main's own SDK, whose types already say awake | asleep | archived, got the legacy names from the mapper. Bumping it to its floor fixes that.
  • The other floors were checked against the repo and the runbook: the next desktop/CLI release is cut from main at 2.0.10 or above, and mobile 1.2.3 is a safe lower bound. They are unchanged.

Rollout

  1. Merge: the deploy runs 0195, then ships the API with the job route.
  2. In the QStash console, publish once to https://api.superset.sh/api/cloud-workspaces/jobs/backfill-status (POST, empty body). The URL must match production's NEXT_PUBLIC_API_URL exactly, because the signature is verified against it. Repeat until the response says "remaining": 0. A run can leave rows only when Vercel was starting or stopping a box at that moment.
  3. Confirm with select count(*) from cloud_workspaces where status in ('ready','deleted'), which must return 0. Then the stacked draft PR can merge.

Testing

  • Rehearsal: on fresh Neon children of production, with lock_timeout=5s and statement_timeout=60s, under concurrent psql load (4 readers on the list query, 2 writers on live rows, 1 writer holding locks on deleted/archived rows):
    • drizzle-kit migrate passed: 5,425 rows became archived, other statuses were untouched, and the load saw 0 errors.
    • A second pass ran the statement alone: UPDATE 5425 in 205 ms, with 0 load errors.
  • The job's real SQL against real data: on a production copy, with the listing stubbed to return nothing (see Known Limitations), all 349 ready rows became asleep in two batches with remaining: 0. A re-run found ready: 0 and wrote nothing.
  • Unit tests (backfill-status.test.ts): each class of box gets the right write, the batch is guarded by status = 'ready', a settling box is left for the next run, and a run with no ready rows never lists Vercel.
  • Checks:
    • bun run lint:fix: no fixes needed.
    • Typecheck (trpc, db, sdk, api, shared): clean.
    • Tests: trpc 965, shared 1686, api 260, 0 fail.
    • check:i18n, sherif, check:plugins, check:versions: clean.

Known Limitations

  • The job's run against real Vercel data is unrehearsed. This workspace's .env Vercel token is rejected (403 invalidToken). The live listing is the same Sandbox.list the reconcile uses every 30 minutes; only the cut-off is removed.
  • Each run lists the whole team. If that ever takes longer than the route's window, QStash retries, and a second run is harmless because both are guarded by status = 'ready'.

Rollback

  • Code: reverting removes the route; nothing else depends on it.
  • Migration: 0195 is forward-only. #9800's API reads archived and deleted alike, so there is nothing to undo unless the API itself is rolled back past #9800 (see that PR's rollback note).

Summary by cubic

Backfills the legacy cloud workspace statuses after #9800 added awake/asleep/archived alongside ready/deleted.

  • Adds a one-off, QStash-signed job (POST /api/cloud-workspaces/jobs/backfill-status) that moves every row still at ready to awake or asleep, classified from a full Vercel team listing. The reconcile can't do this because it only lists boxes that changed in its one-hour lookback.
  • Migration 0195 moves deleted rows to archived (~5,425 in production).
  • Bumps the SDK to 0.0.1-alpha.13 so main's SDK stops receiving the legacy names, and documents the status floors in the release runbook.

The job is not scheduled: publish it once from the QStash console and repeat until it reports remaining: 0. Every write requires the row to still be ready, so re-running is safe. The run against real Vercel data is unrehearsed (the dev Vercel token is rejected), but it uses the same Sandbox.list as the reconcile without the lookback cut-off.

Rollout

  • Merge: the deploy runs 0195, then ships the job route.
  • Publish to https://api.paradev.com/api/cloud-workspaces/jobs/backfill-status with an empty body; repeat until remaining: 0.
  • Confirm nothing remains at ready or deleted, then merge the stacked PR that removes the legacy reads.

Rollback

  • Reverting removes the route; nothing else depends on it.
  • Migration 0195 is forward-only, but the API reads archived and deleted alike, so nothing to undo unless the API itself rolls back past #9800.