feat(sandbox): measure every stage of a cloud workspace boot (P0)

Merged
SA
2mo ago
+11566-39

P0 of plans/20260913-sandbox-start-time.md: one trace per cloud workspace boot, from cloudWorkspace.create to the first host-service 200 and the desktop's first terminal, and a script that turns five creates and five reopens into a stage table. Nothing in the start-time plan ships without these numbers; this PR replaces its estimates with them.

What changed

  • scripts/sandbox/start.sh stamps every phase to /data/boot.log as <epoch ms> <phase> (boot.start, env.loaded, hostdb.ready, checkout.*, docker.started, display.*, host.exec) and exports the log path. Same script shape; PR 2 replaces it.
  • host-service runtime/boot-stamps: parses the current boot's stamps (from the last boot.start), appends its own host.process.start and host.listening. health.check returns sandboxBoot: { stamps, runtime: { node, hostService } } in sandbox mode, undefined elsewhere.
  • API: five nullable timestamp columns on cloud_workspaces (migration 0115_cloud_workspace_boot_stamps, generated with drizzle-kit on the worktree's Neon branch): provision_started_at, sandbox_create_started_at, sandbox_create_finished_at, boot_fired_at, first_healthy_at. The job writes the first four with ready; the first wake that sees host-service answer writes the fifth and nudges the list.
  • Desktop: cloud_workspace_opened event, once per open of a cloud workspace, with kind (create / reopen), create_to_ready_ms, ready_to_access_ms, ready_to_first_200_ms, ready_to_first_terminal_ms, open_to_first_200_ms, and the row's job-side deltas as server_*_ms. The create mutation records its start; the workspace route observes the row turning ready, the sandbox's wake grant, the first served workspace.list, and the first terminal attaching (a new attach broadcast in the terminal transport).
  • scripts/sandbox/measure.ts (bun run sandbox:measure): runs the provisioning job in-process against the newest fork environment, probes health every 100 ms beside the API's one-second wake poll, reads the boot stamps off the response, stops and wakes each sandbox, prints medians and maxes, deletes what it created.

Measured 2026-09-14

Dev golden env-internal-mu0tzcdu (rebuilt image with the stamped script), sfo1, 8 vCPU, environment without variables. Stages spanning two clocks are marked †.

Create (n=5)

Stagemedianmax
create: row write0.03 s0.04 s
row written → job start0.03 s0.03 s
job: name, clone token, environment0.74 s1.12 s
Sandbox.fork0.92 s1.18 s
environment file + fire start.sh14.61 s15.29 s
boot fired → row ready0.03 s0.04 s
boot fired → boot.start †-0.07 s-0.06 s
checkout (fetch + checkout)1.94 s2.91 s
dockerd spawn0.02 s0.02 s
display setup until Xvnc spawned0.06 s0.09 s
Xvnc spawned → X ready (background)0.47 s0.66 s
boot.start → host.exec (whole script)2.03 s3.04 s
host.exec → host.process.start0.13 s0.19 s
host.process.start → host.listening0.94 s1.36 s
boot.start → host.listening3.16 s4.57 s
host.listening → first 200 (100 ms probe) †0.12 s0.14 s
ready → first 200 (100 ms probe)3.16 s4.62 s
ready → wake returned (API, 1 s poll)3.34 s17.49 s
create → first 20019.58 s20.98 s

Reopen, stop + wake (n=5)

Stagemedianmax
wake → boot.start (resume + runCommand) †14.26 s16.15 s
boot.start → host.exec (whole script)0.05 s0.06 s
host.process.start → host.listening0.89 s1.10 s
boot.start → host.listening1.08 s1.36 s
host.listening → first 200 (100 ms probe) †0.06 s0.12 s
wake → first 200 (100 ms probe)15.38 s17.59 s
wake → wake returned (API, 1 s poll)15.39 s18.27 s

A second run (three and three) agreed: create 20.3 s median with one 5.3 s outlier, reopen 17.9 s.

Desktop, real journeys over CDP

cloud_workspace_opened from a create through the new-workspace form (Cloud device, "Superset P0" environment, Claude with a prompt) and from a reopen after stopping that sandbox and navigating back from the sidebar:

createreopen
create → ready4.1 s—
ready → access (wake grant)2.0 s18.4 s
ready → first 2003.9 s18.6 s
ready → first terminal9.5 s22.5 s

Job stamps on that create: 0.06 s to job start, 0.71 s to the fork, 0.88 s fork, 0.99 s to boot fired, 2.97 s boot fired to first healthy wake.

What it says

The boot script we own is 2 s on a create (the fetch and checkout) and 0.05 s on a reopen; host-service listens 1.1 s after exec. Everything else is the platform bringing a VM up: Sandbox.fork returns running in 0.9 s, but the first SDK call after it blocked ~15 s in 7 of 9 brokered-policy boots (1.0 s in the other two), and a resume is ~15 s regardless. Two one-sandbox SDK probes: first writeFiles after a fork took 15.1 s with the credential-brokering firewall policy and 0.6 s with allow-all; resume took 16.8 s with the policy and 15.6 s without. The policy correlation is n=1 and the desktop's own create carried the policy and booted in 1.0 s, so it is a lead, not a finding. P1 and P2 recover about a second; the 3 s create target needs the warm pool (P5) and the 8 s reopen target sits under the measured resume floor.

Verification

  • Unit: boot-stamps.test.ts + health.test.ts (host-service, 6 pass), cloud-workspace-open-timeline.test.ts (desktop, 3 pass); desktop suite around the transport and workspace route 342 pass; trpc suite 276 pass; host-service suite run before push.
  • Image rebuilt and released as a new dev golden (sandbox:release --keep-old under a new environment name so no existing golden or secrets were touched); the release probe passed all checks on a fork.
  • bun run sandbox:measure twice (5+5, then 3+3), every sandbox and row deleted afterwards.
  • Desktop over CDP against this worktree's dev stack (API 9261, renderer 9265, gate and realtime workers running): real form submit and real sidebar reopen; the event was read from the renderer's PostHog client. The desktop-created workspace was deleted through the app's API afterwards.
  • bun run lint:fix clean; typecheck clean for host-service, trpc, db, api and desktop.

Not measured: the QStash hop (prod only). Known from the runs: the GitHub App token mint fails on the dev app against the prod-copied installation row, so the checkout fetched the public repo unauthenticated.

https://claude.ai/code/session_01FK6rLzBEUiHHXZWNmpQ2BP


Summary by cubic

Replaces the sandbox start-time plan's estimated timings with measured ones: every cloud workspace boot is now traced from cloudWorkspace.create to the first host-service 200 and the desktop's first terminal. The boot flow itself is unchanged; the side effects are new nullable columns on cloud_workspaces, one extra health.check field in sandbox mode, and a desktop PostHog event.

What Changed

  • scripts/sandbox/start.sh stamps each phase (boot.start, env.loaded, hostdb.ready, checkout.*, docker.started, display.*, host.exec) to /data/boot.log and exports the log path; a wake appends to the same file, so a boot is the run since the last boot.start.
  • host-service records host.process.start and host.listening in the same log and returns the current boot's stamps plus runtime versions as sandboxBoot from health.check; non-sandbox hosts return undefined as before.
  • The provisioning job writes provision_started_at, sandbox_create_started_at, sandbox_create_finished_at, and boot_fired_at when the row becomes ready, and the first wake that sees host-service answer sets first_healthy_at and nudges the list.
  • Migration 0115_cloud_workspace_boot_stamps adds the five columns as nullable timestamps, so existing rows need no backfill.
  • The desktop emits cloud_workspace_opened once per open with a create/reopen kind, desktop-side stage timings, and the job-side timestamps from the row; a new terminal attach broadcast supplies the first-terminal stage.
  • bun run sandbox:measure runs five creates and five reopens against the newest fork environment, probes health every 100 ms beside the API's one-second poll, prints a median/max table, and deletes what it created.

Measured

  • Create-to-first-200 has a 19.6 s median and reopen a 15.4 s median on the dev golden; the boot script itself is about 2 s on create and 0.05 s on reopen.
  • The remaining time is dominated by the platform: a stopped sandbox takes ~15 s to resume, and the first SDK call after fork also blocks ~15 s in most tested boots.