An open-source, faster Devin alternative
TypeScript
checkpoint: revert extract-memory format, restore savevm/loadvm fork path
Brian Reardonc195f16faabb
diff --git a/internal/qemu/checkpoint_memextract.go b/internal/qemu/checkpoint_memextract.godeleted file mode 100644index a3ac8b4a..00000000--- a/internal/qemu/checkpoint_memextract.go+++ /dev/null@@ -1,157 +0,0 @@-package qemu--import (- "context"- "fmt"- "log"- "os"- "os/exec"- "path/filepath"- "time"-)--// extractCheckpointMemory converts a savevm-based checkpoint (memory embedded-// in qcow2 as an internal snapshot) into an external mem.zst + plain qcow2-// drives without internal snapshots. Fork restore paths already prefer the-// external-memory format (see ForkFromCheckpoint restore-mode logic) and-// qemu-img rebase composes cleanly with plain overlays — so cross-golden-// forks work correctly after this runs.-//-// The function is destructive on stagingDir: it may delete the internal-// snapshot from the qcow2 files and create mem.zst. Callers should invoke it-// on a staging directory that has been reflinked from the live VM, not on-// live drives.-//-// Runs out-of-band in the async upload goroutine so user-visible save-// latency is unchanged. On failure, the staging directory is left in its-// savevm-only state — fork restore falls back to loadvm, which still works-// for same-golden forks.-func (m *Manager) extractCheckpointMemory(ctx context.Context, stagingDir, snapshotName, guestMAC string, memMB, cpuCount int) error {- rootfs := filepath.Join(stagingDir, "rootfs.qcow2")- workspace := filepath.Join(stagingDir, "workspace.qcow2")- if !fileExists(rootfs) || !fileExists(workspace) {- return fmt.Errorf("qcow2 drives missing in %s", stagingDir)- }-- memFile := filepath.Join(stagingDir, "mem")- // Linux UNIX socket paths max 108 bytes. Staging dir- // (/data/sandboxes/checkpoint-snapshots/<uuid>.staging/...) is already- // over the limit with a reasonable filename, so put the throwaway- // sockets in /tmp.- shortID := fmt.Sprintf("osb-ex-%d", time.Now().UnixNano())- qmpSock := filepath.Join("/tmp", shortID+".qmp")- agentSock := filepath.Join("/tmp", shortID+".ag")- logFile := filepath.Join(stagingDir, "extract-qemu.log")- os.Remove(memFile)- os.Remove(qmpSock)- os.Remove(agentSock)-- // loadvm requires the runtime device layout to match what savevm recorded- // byte-for-byte (device IDs, order, -serial stdio, even virtio-mem pool- // size). Reuse buildQEMUArgs — same function the live VM was started with- // — plus -S and -loadvm to hold the restored state paused so we can- // migrate the memory out of it.- //- // A temporary TAP is allocated because the virtio-net device in the- // savevm stream expects a netdev backend, and "-netdev user" is a- // different backend type that QEMU rejects during loadvm.- netCfg, err := m.subnets.Allocate()- if err != nil {- return fmt.Errorf("allocate tap: %w", err)- }- if err := CreateTAP(netCfg); err != nil {- m.subnets.Release(netCfg.TAPName)- return fmt.Errorf("create tap: %w", err)- }- defer func() {- DeleteTAP(netCfg.TAPName)- m.subnets.Release(netCfg.TAPName)- }()-- // bootArgs don't matter for loadvm — the guest doesn't actually boot,- // we only load the snapshot's memory+device state and migrate out.- args := m.buildQEMUArgs(cpuCount, memMB, rootfs, workspace, netCfg.TAPName, guestMAC, agentSock, qmpSock, "")- args = append(args, "-S", "-loadvm", snapshotName)-- logf, err := os.Create(logFile)- if err != nil {- return fmt.Errorf("create extract log: %w", err)- }- defer logf.Close()-- cmd := exec.CommandContext(ctx, m.cfg.QEMUBin, args...)- cmd.Stdout = logf- cmd.Stderr = logf- if err := cmd.Start(); err != nil {- return fmt.Errorf("start extract qemu: %w", err)- }-- killQEMU := func() {- if cmd.Process != nil {- _ = cmd.Process.Kill()- cmd.Wait()- }- }-- qmp, err := waitForQMP(qmpSock, 30*time.Second)- if err != nil {- killQEMU()- return fmt.Errorf("extract qmp connect: %w", err)- }-- // Migrate memory+device state to an external file. VM stays paused (-S)- // the whole time; migrate tolerates that.- if err := qmp.Migrate(fmt.Sprintf("exec:cat > %s", memFile)); err != nil {- qmp.Close()- killQEMU()- return fmt.Errorf("extract migrate: %w", err)- }- if err := qmp.WaitMigration(5 * time.Minute); err != nil {- qmp.Close()- killQEMU()- return fmt.Errorf("extract migrate wait: %w", err)- }-- _ = qmp.Quit()- qmp.Close()-- done := make(chan error, 1)- go func() { done <- cmd.Wait() }()- select {- case <-done:- case <-time.After(10 * time.Second):- killQEMU()- }- os.Remove(qmpSock)- os.Remove(agentSock)-- // Compress memory file with zstd -3 (same level golden uses). -3 on an- // uncompressed VM memory dump typically achieves 3-5x reduction at- // ~500MB/s on a single core.- memZst := memFile + ".zst"- zstdOut, zstdErr := exec.Command("zstd", "-3", "--rm", memFile, "-o", memZst).CombinedOutput()- if zstdErr != nil {- os.Remove(memFile)- os.Remove(memZst)- return fmt.Errorf("zstd compress mem: %w (%s)", zstdErr, string(zstdOut))- }-- // Strip the internal savevm snapshot from the qcow2 drives. After this,- // the overlays are plain qcow2 files with only their backing-file- // reference — qemu-img rebase operates on them correctly, and fork- // restore uses the external mem.zst instead of loadvm.- for _, f := range []string{rootfs, workspace} {- if out, derr := exec.Command("qemu-img", "snapshot", "-d", snapshotName, f).CombinedOutput(); derr != nil {- // Workspace may not actually carry the snapshot entry (savevm stores- // VMSTATE in only one drive); a "snapshot not found" error here is- // benign. Log and continue.- log.Printf("qemu: extractCheckpointMemory: snapshot -d %s on %s: %v (%s)", snapshotName, f, derr, string(out))- }- }-- // Remove the snapshot-name file — restore logic only uses it when an- // external mem file is absent, and we've now guaranteed one is present.- // Leave it in place for backward compat: old worker binaries that predate- // this code only understand the loadvm path and would break without it.- return nil-}diff --git a/internal/qemu/manager.go b/internal/qemu/manager.goindex 51402be5..b02de8a8 100644--- a/internal/qemu/manager.go+++ b/internal/qemu/manager.go@@ -2237,28 +2237,16 @@ func (m *Manager) CreateCheckpoint(ctx context.Context, sandboxID, checkpointIDmetaJSON, _ := json.Marshal(meta)_ = os.WriteFile(filepath.Join(stagingDir, "snapshot", "snapshot-meta.json"), metaJSON, 0644)- // Extract memory into an external mem.zst and strip the internal savevm- // snapshot from the staging qcow2s. This converts the checkpoint into a- // format fork restore prefers (-incoming exec:zstdcat over loadvm) and- // that qemu-img rebase composes cleanly with for cross-golden forks.- // Done before the rename so the local cache never exposes a- // half-converted state to concurrent fork readers.- //- // Best-effort: on failure, leave the savevm-based layout in place. Fork- // restore still works via loadvm for same-golden forks; cross-golden- // will fail the same way it does without this step, so we're no worse- // off than before.- extractCtx, extractCancel := context.WithTimeout(context.Background(), 10*time.Minute)- // Pass baseMemoryMB (initial -m value), not MemoryMB (which reflects the- // current virtio-mem balloon). The extract QEMU's cmdline must match the- // original VM's -m for loadvm to restore correctly; virtio-mem state- // inside the snapshot replays the balloon up to the ballooned size.- if extractErr := m.extractCheckpointMemory(extractCtx, stagingDir, snapshotName, vm.guestMAC, vm.baseMemoryMB, vm.CpuCount); extractErr != nil {- log.Printf("qemu: checkpoint %s: memory extract failed (falling back to savevm format): %v", checkpointID, extractErr)- } else {- log.Printf("qemu: checkpoint %s: memory extracted to mem.zst (%dms total)", checkpointID, time.Since(t0).Milliseconds())- }- extractCancel()+ // Note: an earlier version of this code extracted the memory into an+ // external mem.zst and stripped the internal savevm snapshot from the+ // staging qcow2s, intending to make qemu-img rebase compose cleanly for+ // cross-golden forks (loaded via `-incoming exec:zstdcat`). In practice+ // the resulting checkpoints would not load: QEMU sat in `prelaunch`+ // indefinitely while reading the migration stream, never transitioning+ // to `paused`. Reverted to the savevm/loadvm path which has months of+ // production track record. Cross-golden forks rely on pin-to-base+ // (ensureCheckpointRebased) to align the qcow2's backing file at fork+ // time without touching the captured savevm state.// Atomic rename into cache under write lock.m.checkpointCacheMu.Lock()