An open-source, faster Devin alternative
TypeScript
solve readiness issue
Brian Reardon06549eb509be
diff --git a/internal/api/router.go b/internal/api/router.goindex fd260085..c9ee7470 100644--- a/internal/api/router.go+++ b/internal/api/router.go@@ -1,6 +1,7 @@package apiimport (+ "context""io/fs""log""net/http"@@ -105,6 +106,24 @@ func NewServer(mgr sandbox.Manager, ptyMgr *sandbox.PTYManager, apiKey string, os.ecrConfig = opts.ECRConfigs.cfClient = opts.CFClients.sandboxAPIProxy = opts.SandboxAPIProxy++ // Wire up readiness waiting so the proxy blocks until async creates finish+ if s.sandboxAPIProxy != nil {+ s.sandboxAPIProxy.SetWaitForReady(func(ctx context.Context, sandboxID string) error {+ val, ok := s.pendingCreates.Load(sandboxID)+ if !ok {+ return nil // not a pending create — proceed normally+ }+ pending := val.(*pendingCreate)+ select {+ case <-pending.ready:+ s.pendingCreates.Delete(sandboxID)+ return pending.err+ case <-ctx.Done():+ return ctx.Err()+ }+ })+ }}// Global middlewarediff --git a/internal/proxy/sandbox_api_proxy.go b/internal/proxy/sandbox_api_proxy.goindex bdf58d2d..6cf8302a 100644--- a/internal/proxy/sandbox_api_proxy.go+++ b/internal/proxy/sandbox_api_proxy.go@@ -26,9 +26,10 @@ import (// control plane forwards exec/files/pty/agent requests to workers over the// internal VPC network.type SandboxAPIProxy struct {- store *db.Store- registry *controlplane.RedisWorkerRegistry- jwtIssuer *auth.JWTIssuer+ store *db.Store+ registry *controlplane.RedisWorkerRegistry+ jwtIssuer *auth.JWTIssuer+ waitForReady func(ctx context.Context, sandboxID string) error // blocks until async sandbox creation completes; nil = no-op}// NewSandboxAPIProxy creates a new sandbox API proxy.@@ -40,6 +41,13 @@ func NewSandboxAPIProxy(store *db.Store, registry *controlplane.RedisWorkerRegis}}+// SetWaitForReady sets a callback that blocks until an async sandbox creation+// completes. The proxy calls this before forwarding requests to avoid proxying+// to a worker that hasn't finished booting the sandbox yet.+func (p *SandboxAPIProxy) SetWaitForReady(fn func(ctx context.Context, sandboxID string) error) {+ p.waitForReady = fn+}+// ProxyHandler forwards requests for a sandbox to the worker that owns it.// It extracts the sandbox ID from the path param ":id", looks up the worker,// mints a short-lived JWT, and proxies the request.@@ -53,6 +61,15 @@ func (p *SandboxAPIProxy) ProxyHandler(c echo.Context) error {ctx := c.Request().Context()+ // If this sandbox is still being created asynchronously, wait for it.+ if p.waitForReady != nil {+ if err := p.waitForReady(ctx, sandboxID); err != nil {+ return c.JSON(http.StatusBadGateway, map[string]string{+ "error": fmt.Sprintf("sandbox %s: creation failed: %v", sandboxID, err),+ })+ }+ }+// Look up which worker owns this sandboxsession, err := p.store.GetSandboxSession(ctx, sandboxID)if err != nil {