An open-source, faster Devin alternative
TypeScript
fix workspace, adjust sync vs async
Brian Reardon8e5541170bcd
diff --git a/internal/api/image_builder.go b/internal/api/image_builder.goindex e5a38ec0..fcb4ca01 100644--- a/internal/api/image_builder.go+++ b/internal/api/image_builder.go@@ -518,6 +518,14 @@ func (s *Server) buildImage(ctx context.Context, orgID uuid.UUID, manifest *Imagreturn uuid.Nil, fmt.Errorf("failed to checkpoint build sandbox: %w", err)}+ // Wait for S3 upload to complete before the fork (which may land on another worker)+ type uploader interface {+ WaitUploads(timeout time.Duration)+ }+ if u, ok := s.manager.(uploader); ok {+ u.WaitUploads(5 * time.Minute)+ }+if s.store != nil {_ = s.store.SetCheckpointReady(ctx, checkpointID, rootfsKey, workspaceKey, 0)}diff --git a/internal/qemu/manager.go b/internal/qemu/manager.goindex cd8f1bb1..c4dd9e42 100644--- a/internal/qemu/manager.go+++ b/internal/qemu/manager.go@@ -19,6 +19,7 @@ import ("github.com/google/uuid""github.com/opensandbox/opensandbox/internal/sandbox"+ "github.com/opensandbox/opensandbox/internal/sparse""github.com/opensandbox/opensandbox/internal/storage""github.com/opensandbox/opensandbox/pkg/types"pb "github.com/opensandbox/opensandbox/proto/agent"@@ -1957,31 +1958,46 @@ func (m *Manager) CreateCheckpoint(ctx context.Context, sandboxID, checkpointIDlog.Printf("qemu: checkpoint %s: cache saved (%dms)", checkpointID, time.Since(t0).Milliseconds())- // Upload checkpoint to S3 synchronously so cross-worker forks can download it.- // Without this, forks routed to a different worker fail with "checkpoint not found in S3".+ // Upload checkpoint to S3 in the background so cross-worker forks can download it.+ // Returns a channel that closes when the upload completes — callers that need+ // the upload to finish (e.g. image builder) can wait on it.+ // Rootfs: tar.zst archive (download path uses extractArchiveCmd)+ // Workspace: sparse.zst archive (download path uses extractSparseCmd / OSBSPAR1 format)if checkpointStore != nil {- t1 := time.Now()- for _, upload := range []struct {- key string- file string- }{- {rootfsKey, filepath.Join(cacheDir, "rootfs.qcow2")},- {workspaceKey, filepath.Join(cacheDir, "workspace.qcow2")},- } {- // Create tar.zst archive of the single file- archivePath := upload.file + ".tar.zst"- if err := createArchive(archivePath, cacheDir, []string{filepath.Base(upload.file)}); err != nil {- log.Printf("qemu: checkpoint %s: archive %s failed: %v", checkpointID, filepath.Base(upload.file), err)- continue+ m.uploadWg.Add(1)+ go func() {+ defer m.uploadWg.Done()+ t1 := time.Now()++ // Rootfs → tar.zst+ rootfsArchive := filepath.Join(cacheDir, "rootfs.tar.zst")+ if err := createArchive(rootfsArchive, cacheDir, []string{"rootfs.qcow2"}); err != nil {+ log.Printf("qemu: checkpoint %s: rootfs archive failed: %v", checkpointID, err)+ } else {+ uploadCtx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)+ if _, err := checkpointStore.Upload(uploadCtx, rootfsKey, rootfsArchive); err != nil {+ log.Printf("qemu: checkpoint %s: rootfs S3 upload failed: %v", checkpointID, err)+ }+ cancel()+ os.Remove(rootfsArchive)}- uploadCtx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)- if _, err := checkpointStore.Upload(uploadCtx, upload.key, archivePath); err != nil {- log.Printf("qemu: checkpoint %s: S3 upload %s failed: %v", checkpointID, upload.key, err)++ // Workspace → sparse.zst (OSBSPAR1 format)+ workspaceSrc := filepath.Join(cacheDir, "workspace.qcow2")+ workspaceArchive := filepath.Join(cacheDir, "workspace.sparse.zst")+ if _, err := sparse.Create(workspaceSrc, workspaceArchive); err != nil {+ log.Printf("qemu: checkpoint %s: workspace sparse archive failed: %v", checkpointID, err)+ } else {+ uploadCtx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)+ if _, err := checkpointStore.Upload(uploadCtx, workspaceKey, workspaceArchive); err != nil {+ log.Printf("qemu: checkpoint %s: workspace S3 upload failed: %v", checkpointID, err)+ }+ cancel()+ os.Remove(workspaceArchive)}- cancel()- os.Remove(archivePath)- }- log.Printf("qemu: checkpoint %s: S3 upload complete (%dms)", checkpointID, time.Since(t1).Milliseconds())++ log.Printf("qemu: checkpoint %s: S3 upload complete (%dms)", checkpointID, time.Since(t1).Milliseconds())+ }()}if onReady != nil {diff --git a/internal/worker/grpc_server.go b/internal/worker/grpc_server.goindex 5e85f69b..02a36e7e 100644--- a/internal/worker/grpc_server.go+++ b/internal/worker/grpc_server.go@@ -645,6 +645,20 @@ func (s *GRPCServer) CreateCheckpoint(ctx context.Context, req *pb.CreateCheckporeturn nil, fmt.Errorf("create checkpoint failed: %w", err)}+ // If this checkpoint is for an image build (PrepareGolden), wait for the+ // S3 upload to complete before returning. The image builder will immediately+ // fork from this checkpoint, possibly on a different worker that needs S3.+ // Regular checkpoints upload in the background — same-worker restore uses+ // local cache and doesn't need S3.+ if req.PrepareGolden {+ type uploader interface {+ WaitUploads(timeout time.Duration)+ }+ if u, ok := s.manager.(uploader); ok {+ u.WaitUploads(5 * time.Minute)+ }+ }+return &pb.CreateCheckpointResponse{RootfsS3Key: rootfsKey,WorkspaceS3Key: workspaceKey,