Cloud: bake a warm first terminal into the devbox snapshot

Merged
LA
2mo ago
+3774-184

New Cloud machines now boot with the first terminal's shell already running. Before this change, each clone started the daemon cold, created the Cloud workspace, spawned a PTY, and then started bash.

Design. The bake keeps the first terminal's host process and its initialized shell in the memory snapshot. It then parks the daemon and wipes every per-machine file: machine id, receipt pepper, session registry and journal, and remote identity. The terminal-host record directory is kept. On a clone, cmux-devbox-boot reseeds the kernel CRNG with the instance id mixed in, and starts the daemon with the Cloud template settings. The daemon then:

  • reads those settings and removes them from its own environment before any thread starts, so no terminal inherits them;
  • creates a fresh identity through the normal code path;
  • claims the live host through a named path, claim_template_terminal (launch spec {"template_terminal":true}), in a workspace named Cloud;
  • commits the placement to the public topology, then writes the new session and terminal ids to /run/cmux/bound. This runs after any successful template adoption: on the startup pass and on the asynchronous retry. If it fails, it is retried in the background with backoff; it never aborts startup.

The template shell's first prompt waits for that file (bounded at 3 s) and for the machine name, so the first prompt shows cmux@<slug>. If the wait times out, a later prompt imports the binding when it appears. The prompt sync uses the bound terminal and creates a workspace only when the daemon answers with no terminal.

Cloud compatibility removed (Cloud has not shipped):

  • openCmuxRemote refuses a row without the snapshot-v2 contract or its recorded addresses. It does not read them from the provider.
  • The attach-time address backfill is removed.
  • cmuxTuiAgentHooksInstallCommand had no caller and is removed.

Kept, because local (npm/brew) cmux-tui users reach it on a daemon upgrade:

  • the terminal-host import for hosts with no registry row;
  • the placement for hosts that predate public content ids.

Kept for Cloud: a clone still drops inherited remote auth and connection state, because checkpoint restores and forks carry the parent's identity.

Trade-offs

  • The daemon cold start is still paid.
  • All clones share the host's durable terminal id and its local-socket owner token.
  • Bash $RANDOM state is shared between clones.

Verified on warm-template9 (2× sm, 1× md):

  • each clone has its own machine id, pepper, session id, and remote identity;
  • each has 1 terminal and 1 workspace named Cloud, with durable spec {"template_terminal":true};
  • the two sm clones have the same host pid as the snapshot;
  • the shell env has the new ids and no template settings;
  • the prompt shows the slug and 1337 listens.

Latency. Interleaved raw probe, create-return to listener:

  • template5 vs template6, n=6 each: median 660 vs 560 ms;
  • template5 vs template7, n=5 each: median 509 vs 560 ms;
  • template5 vs template9, n=5 each: median 932 vs 519 ms.

The outliers in both follow slow server creates.

Tests

  • Focused cmux-tui template_terminal_host: green at 67d7ce31a9a (run 36080226342).
  • Green at ed554c82b71 with all four template tests (run 36100284855).
  • The red runs are real:
    • launch-spec test commit: run 36080228779;
    • env-leak test commit with formatting only: run 36092554770 ("CMUX_TUI_ADOPT_TEMPLATE_TERMINAL leaked");
    • retry-path test commit: run 36097777266 ("never published its binding");
    • completion-failure test commit: run 36100282320 (startup aborted, retry gave up).
  • Web tests/vm-*: 1137 pass.
  • No DB migration.

Merging deploys web/ and switches New Machine to the warm-template9 snapshots, which resolve from the checked-in manifest only.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • VM snapshots now include a warm terminal that can be adopted automatically when a clone starts, with its session and terminal IDs bound to the new machine.
    • Added new warm-template VM image options across six sizes.
  • Improvements

    • Cloned terminals retain their warm shell while receiving fresh machine identity.
    • Remote connections now require supported snapshot metadata and recorded network addresses; older machines may need to be recreated.