Skip to content

An open-source, faster Devin alternative

TypeScript

more durable golden versioning -> add worker reported fallback and record golden version in session

Brian Reardon026a8ac745c9
diff --git a/internal/api/sandbox.go b/internal/api/sandbox.go
index df0f9faa..1e236a6f 100644
--- a/internal/api/sandbox.go
+++ b/internal/api/sandbox.go
@@ -472,6 +472,9 @@ func (s *Server) createSandboxRemote(c echo.Context, ctx context.Context, cfg ty
cfgJSON, _ := json.Marshal(cfgForPersistence(cfg))
metadataJSON, _ := json.Marshal(cfg.Metadata)
_, _ = s.store.CreateSandboxSessionWithStatus(ctx, sandboxID, orgID, auth.GetUserID(c), template, region, worker.ID, cfgJSON, metadataJSON, "pending")
+ if worker.GoldenVersion != "" {
+ _ = s.store.SetSandboxGoldenVersion(ctx, sandboxID, worker.GoldenVersion)
+ }
if templateID != nil {
_ = s.store.UpdateSandboxSessionTemplate(ctx, sandboxID, *templateID)
}
@@ -2251,6 +2254,12 @@ func (s *Server) createFromCheckpointCore(c echo.Context, userEnvs map[string]st
cfgJSON, _ := json.Marshal(cfgForPersistence(originalCfg))
metadataJSON, _ := json.Marshal(originalCfg.Metadata)
_, _ = s.store.CreateSandboxSession(ctx, sandboxID, orgID, auth.GetUserID(c), template, region, workerID, cfgJSON, metadataJSON)
+ // Set golden version from worker heartbeat
+ if s.workerRegistry != nil {
+ if w := s.workerRegistry.GetWorker(workerID); w != nil && w.GoldenVersion != "" {
+ _ = s.store.SetSandboxGoldenVersion(ctx, sandboxID, w.GoldenVersion)
+ }
+ }
// Track checkpoint lineage for patch system
_ = s.store.SetSandboxCheckpointID(ctx, sandboxID, checkpointID)
}
diff --git a/internal/controlplane/scaler.go b/internal/controlplane/scaler.go
index 6ed3dfb0..21afe056 100644
--- a/internal/controlplane/scaler.go
+++ b/internal/controlplane/scaler.go
@@ -1097,13 +1097,28 @@ func (s *Scaler) liveMigrateSandbox(ctx context.Context, sandboxID, sourceWorker
return fmt.Errorf("pre-copy drives: %w", err)
}
- // If the source has no goldenVersion, we can't safely rebase on the target.
- // Fall back to hibernate → wake which handles legacy checkpoints.
- if preCopyResp.GoldenVersion == "" {
- return fmt.Errorf("source sandbox has no goldenVersion — cannot live migrate safely; use hibernate/wake instead")
+ // Resolve golden version with three-tier fallback:
+ // 1. PreCopyDrives response (worker in-memory)
+ // 2. PG sandbox_sessions.golden_version (durable, set at creation)
+ // 3. Worker heartbeat (all sandboxes on a worker share its golden base)
+ sourceGoldenVersion := preCopyResp.GoldenVersion
+ if sourceGoldenVersion == "" && s.store != nil {
+ if session, err := s.store.GetSandboxSession(ctx, sandboxID); err == nil && session.GoldenVersion != "" {
+ sourceGoldenVersion = session.GoldenVersion
+ log.Printf("scaler: migrate %s: goldenVersion from PG: %s", sandboxID, sourceGoldenVersion)
+ }
+ }
+ if sourceGoldenVersion == "" {
+ sourceWorker := s.getWorkerInfo(sourceWorkerID)
+ if sourceWorker != nil && sourceWorker.GoldenVersion != "" {
+ sourceGoldenVersion = sourceWorker.GoldenVersion
+ log.Printf("scaler: migrate %s: sandbox missing goldenVersion, using worker's: %s", sandboxID, sourceGoldenVersion)
+ } else {
+ return fmt.Errorf("source sandbox has no goldenVersion — not in gRPC response, PG, or worker heartbeat")
+ }
}
- log.Printf("scaler: migrate %s: drives pre-copied to S3 (%dms, golden=%s)", sandboxID, time.Since(t0).Milliseconds(), preCopyResp.GoldenVersion)
+ log.Printf("scaler: migrate %s: drives pre-copied to S3 (%dms, golden=%s)", sandboxID, time.Since(t0).Milliseconds(), sourceGoldenVersion)
// Step 2: Prepare target (downloads thin overlay from S3, rebases if needed, starts QEMU -incoming)
// CPU and memory come from the source worker — must match exactly for QEMU migration.
@@ -1136,7 +1151,7 @@ func (s *Scaler) liveMigrateSandbox(ctx context.Context, sandboxID, sourceWorker
RootfsS3Key: preCopyResp.RootfsKey,
WorkspaceS3Key: preCopyResp.WorkspaceKey,
OverlayMode: true,
- SourceGoldenVersion: preCopyResp.GoldenVersion,
+ SourceGoldenVersion: sourceGoldenVersion,
TargetMemoryMb: 4096,
})
if err != nil {
diff --git a/internal/controlplane/server.go b/internal/controlplane/server.go
index f17fda65..6ec984c2 100644
--- a/internal/controlplane/server.go
+++ b/internal/controlplane/server.go
@@ -211,6 +211,12 @@ func (s *Server) createSandbox(c echo.Context) error {
metadataJSON, _ := json.Marshal(req.Metadata)
_, _ = s.store.CreateSandboxSession(ctx, grpcResp.SandboxId, orgID, auth.GetUserID(c), template, region, worker.ID, cfgJSON, metadataJSON)
+ // Persist golden version from worker heartbeat so the scaler can read it
+ // from PG instead of relying on in-memory state via gRPC.
+ if worker.GoldenVersion != "" {
+ _ = s.store.SetSandboxGoldenVersion(ctx, grpcResp.SandboxId, worker.GoldenVersion)
+ }
+
// Issue sandbox-scoped JWT (24h TTL — independent of sandbox idle timeout)
token, err := s.jwtIssuer.IssueSandboxToken(orgID, grpcResp.SandboxId, worker.ID, 24*time.Hour)
if err != nil {
diff --git a/internal/db/migrations/025_sandbox_golden_version.up.sql b/internal/db/migrations/025_sandbox_golden_version.up.sql
new file mode 100644
index 00000000..4f3c15ac
--- /dev/null
+++ b/internal/db/migrations/025_sandbox_golden_version.up.sql
@@ -0,0 +1,3 @@
+-- Store golden snapshot version on sandbox sessions so the scaler can read it
+-- from PG instead of relying on in-memory state via gRPC.
+ALTER TABLE sandbox_sessions ADD COLUMN IF NOT EXISTS golden_version TEXT;
diff --git a/internal/db/store.go b/internal/db/store.go
index def59295..324c3655 100644
--- a/internal/db/store.go
+++ b/internal/db/store.go
@@ -118,6 +118,7 @@ func (s *Store) Migrate(ctx context.Context) error {
{25, "migrations/023_checkpoints_public.up.sql"},
{26, "migrations/023_free_credits_remaining_cents.up.sql"},
{27, "migrations/024_patch_error_tracking.up.sql"},
+ {28, "migrations/025_sandbox_golden_version.up.sql"},
}
for _, m := range migrations {
@@ -485,6 +486,7 @@ type SandboxSession struct {
LastPatchSequence int `json:"lastPatchSequence"`
MigratingToWorker string `json:"migratingToWorker,omitempty"`
PatchError *string `json:"patchError,omitempty"`
+ GoldenVersion string `json:"goldenVersion,omitempty"`
}
func (s *Store) CreateSandboxSession(ctx context.Context, sandboxID string, orgID uuid.UUID, userID *uuid.UUID, template, region, workerID string, config, metadata json.RawMessage) (*SandboxSession, error) {
@@ -496,11 +498,11 @@ func (s *Store) CreateSandboxSessionWithStatus(ctx context.Context, sandboxID st
err := s.pool.QueryRow(ctx,
`INSERT INTO sandbox_sessions (sandbox_id, org_id, user_id, template, region, worker_id, config, metadata, status)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
- RETURNING id, sandbox_id, org_id, user_id, template, region, worker_id, status, config, metadata, started_at, based_on_checkpoint_id, last_patch_sequence, patch_error`,
+ RETURNING id, sandbox_id, org_id, user_id, template, region, worker_id, status, config, metadata, started_at, based_on_checkpoint_id, last_patch_sequence, patch_error, golden_version`,
sandboxID, orgID, userID, template, region, workerID, config, metadata, status,
).Scan(&session.ID, &session.SandboxID, &session.OrgID, &session.UserID, &session.Template,
&session.Region, &session.WorkerID, &session.Status, &session.Config, &session.Metadata, &session.StartedAt,
- &session.BasedOnCheckpointID, &session.LastPatchSequence, &session.PatchError)
+ &session.BasedOnCheckpointID, &session.LastPatchSequence, &session.PatchError, &session.GoldenVersion)
if err != nil {
return nil, fmt.Errorf("failed to create sandbox session: %w", err)
}
@@ -642,7 +644,7 @@ func (s *Store) GetSandboxSession(ctx context.Context, sandboxID string) (*Sandb
FROM sandbox_sessions WHERE sandbox_id = $1 ORDER BY started_at DESC LIMIT 1`, sandboxID,
).Scan(&session.ID, &session.SandboxID, &session.OrgID, &session.UserID, &session.Template,
&session.Region, &session.WorkerID, &session.Status, &session.Config, &session.Metadata,
- &session.StartedAt, &session.StoppedAt, &session.ErrorMsg, &session.BasedOnCheckpointID, &session.LastPatchSequence, &session.PatchError)
+ &session.StartedAt, &session.StoppedAt, &session.ErrorMsg, &session.BasedOnCheckpointID, &session.LastPatchSequence, &session.PatchError, &session.GoldenVersion)
if err != nil {
return nil, fmt.Errorf("sandbox session not found: %w", err)
}
@@ -673,7 +675,7 @@ func (s *Store) ListSandboxSessions(ctx context.Context, orgID uuid.UUID, status
var sess SandboxSession
if err := rows.Scan(&sess.ID, &sess.SandboxID, &sess.OrgID, &sess.UserID, &sess.Template,
&sess.Region, &sess.WorkerID, &sess.Status, &sess.Config, &sess.Metadata,
- &sess.StartedAt, &sess.StoppedAt, &sess.ErrorMsg, &sess.BasedOnCheckpointID, &sess.LastPatchSequence, &sess.PatchError); err != nil {
+ &sess.StartedAt, &sess.StoppedAt, &sess.ErrorMsg, &sess.BasedOnCheckpointID, &sess.LastPatchSequence, &sess.PatchError, &sess.GoldenVersion); err != nil {
return nil, err
}
sessions = append(sessions, sess)
@@ -1373,6 +1375,14 @@ func (s *Store) SetSandboxPatchError(ctx context.Context, sandboxID string, patc
return err
}
+// SetSandboxGoldenVersion sets the golden_version on a sandbox session.
+func (s *Store) SetSandboxGoldenVersion(ctx context.Context, sandboxID, goldenVersion string) error {
+ _, err := s.pool.Exec(ctx,
+ `UPDATE sandbox_sessions SET golden_version = $1 WHERE sandbox_id = $2`,
+ goldenVersion, sandboxID)
+ return err
+}
+
// SetSandboxCheckpointID sets the based_on_checkpoint_id for a sandbox session.
func (s *Store) SetSandboxCheckpointID(ctx context.Context, sandboxID string, checkpointID uuid.UUID) error {
_, err := s.pool.Exec(ctx,
@@ -1397,7 +1407,7 @@ func (s *Store) ListSandboxesByCheckpoint(ctx context.Context, checkpointID uuid
var sess SandboxSession
if err := rows.Scan(&sess.ID, &sess.SandboxID, &sess.OrgID, &sess.UserID, &sess.Template,
&sess.Region, &sess.WorkerID, &sess.Status, &sess.Config, &sess.Metadata,
- &sess.StartedAt, &sess.StoppedAt, &sess.ErrorMsg, &sess.BasedOnCheckpointID, &sess.LastPatchSequence, &sess.PatchError); err != nil {
+ &sess.StartedAt, &sess.StoppedAt, &sess.ErrorMsg, &sess.BasedOnCheckpointID, &sess.LastPatchSequence, &sess.PatchError, &sess.GoldenVersion); err != nil {
return nil, err
}
sessions = append(sessions, sess)