feat(sandbox): measure every stage of a cloud workspace boot (P0)
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.shstamps every phase to/data/boot.logas<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 lastboot.start), appends its ownhost.process.startandhost.listening.health.checkreturnssandboxBoot: { stamps, runtime: { node, hostService } }in sandbox mode,undefinedelsewhere. - API: five nullable timestamp columns on
cloud_workspaces(migration0115_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 withready; the first wake that sees host-service answer writes the fifth and nudges the list. - Desktop:
cloud_workspace_openedevent, once per open of a cloud workspace, withkind(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 asserver_*_ms. The create mutation records its start; the workspace route observes the row turning ready, the sandbox's wake grant, the first servedworkspace.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)
| Stage | median | max |
|---|---|---|
| create: row write | 0.03 s | 0.04 s |
| row written → job start | 0.03 s | 0.03 s |
| job: name, clone token, environment | 0.74 s | 1.12 s |
| Sandbox.fork | 0.92 s | 1.18 s |
| environment file + fire start.sh | 14.61 s | 15.29 s |
| boot fired → row ready | 0.03 s | 0.04 s |
| boot fired → boot.start † | -0.07 s | -0.06 s |
| checkout (fetch + checkout) | 1.94 s | 2.91 s |
| dockerd spawn | 0.02 s | 0.02 s |
| display setup until Xvnc spawned | 0.06 s | 0.09 s |
| Xvnc spawned → X ready (background) | 0.47 s | 0.66 s |
| boot.start → host.exec (whole script) | 2.03 s | 3.04 s |
| host.exec → host.process.start | 0.13 s | 0.19 s |
| host.process.start → host.listening | 0.94 s | 1.36 s |
| boot.start → host.listening | 3.16 s | 4.57 s |
| host.listening → first 200 (100 ms probe) † | 0.12 s | 0.14 s |
| ready → first 200 (100 ms probe) | 3.16 s | 4.62 s |
| ready → wake returned (API, 1 s poll) | 3.34 s | 17.49 s |
| create → first 200 | 19.58 s | 20.98 s |
Reopen, stop + wake (n=5)
| Stage | median | max |
|---|---|---|
| wake → boot.start (resume + runCommand) † | 14.26 s | 16.15 s |
| boot.start → host.exec (whole script) | 0.05 s | 0.06 s |
| host.process.start → host.listening | 0.89 s | 1.10 s |
| boot.start → host.listening | 1.08 s | 1.36 s |
| host.listening → first 200 (100 ms probe) † | 0.06 s | 0.12 s |
| wake → first 200 (100 ms probe) | 15.38 s | 17.59 s |
| wake → wake returned (API, 1 s poll) | 15.39 s | 18.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:
| create | reopen | |
|---|---|---|
| create → ready | 4.1 s | — |
| ready → access (wake grant) | 2.0 s | 18.4 s |
| ready → first 200 | 3.9 s | 18.6 s |
| ready → first terminal | 9.5 s | 22.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-oldunder a new environment name so no existing golden or secrets were touched); the release probe passed all checks on a fork. bun run sandbox:measuretwice (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:fixclean; 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.shstamps each phase (boot.start,env.loaded,hostdb.ready,checkout.*,docker.started,display.*,host.exec) to/data/boot.logand exports the log path; a wake appends to the same file, so a boot is the run since the lastboot.start.- host-service records
host.process.startandhost.listeningin the same log and returns the current boot's stamps plus runtime versions assandboxBootfromhealth.check; non-sandbox hosts returnundefinedas before. - The provisioning job writes
provision_started_at,sandbox_create_started_at,sandbox_create_finished_at, andboot_fired_atwhen the row becomesready, and the first wake that sees host-service answer setsfirst_healthy_atand nudges the list. - Migration
0115_cloud_workspace_boot_stampsadds the five columns as nullable timestamps, so existing rows need no backfill. - The desktop emits
cloud_workspace_openedonce per open with a create/reopenkind, 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:measureruns 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.