Skip to content

An open-source, faster Devin alternative

TypeScript

pre-warmed sandbox pool claim on new create, skip cold restore

Brian Reardon7c8cc6a97d35
diff --git a/internal/api/pool.go b/internal/api/pool.go
index 6c481cab..612bc913 100644
--- a/internal/api/pool.go
+++ b/internal/api/pool.go
@@ -7,6 +7,7 @@ import (
"log"
"os"
"strconv"
+ "strings"
"time"
"github.com/google/uuid"
@@ -22,13 +23,57 @@ import (
// customer), parked paused, never billed. On worker drain they are wiped
// (disposable) rather than migrated/hibernated.
-func poolTargetN() int {
+// poolDefaultTarget is the benign per-region warm-pool size used when a region
+// has no explicit override. From OPENSANDBOX_POOL_TARGET (Infisical-injected env),
+// default 10.
+func (s *Server) poolDefaultTarget() int {
if v := os.Getenv("OPENSANDBOX_POOL_TARGET"); v != "" {
if n, err := strconv.Atoi(v); err == nil && n >= 0 {
return n
}
}
- return 5
+ return 10
+}
+
+// poolTargetForRegion returns the warm-pool target for a region: a per-region
+// override from OPENSANDBOX_POOL_TARGETS="eastus2=100,westus2=10" (settable in
+// Infisical — it's just an env var the CP process reads), else poolDefaultTarget().
+// 0 ⇒ no pool for that region.
+func (s *Server) poolTargetForRegion(region string) int {
+ if raw := os.Getenv("OPENSANDBOX_POOL_TARGETS"); raw != "" {
+ for _, pair := range strings.Split(raw, ",") {
+ kv := strings.SplitN(strings.TrimSpace(pair), "=", 2)
+ if len(kv) == 2 && strings.TrimSpace(kv[0]) == region {
+ if n, err := strconv.Atoi(strings.TrimSpace(kv[1])); err == nil && n >= 0 {
+ return n
+ }
+ }
+ }
+ }
+ return s.poolDefaultTarget()
+}
+
+// poolRegions is the set of regions the reconciler warms: the CP's own region
+// plus any explicitly listed in OPENSANDBOX_POOL_TARGETS.
+func (s *Server) poolRegions() []string {
+ set := map[string]bool{}
+ if s.region != "" {
+ set[s.region] = true
+ }
+ if raw := os.Getenv("OPENSANDBOX_POOL_TARGETS"); raw != "" {
+ for _, pair := range strings.Split(raw, ",") {
+ if kv := strings.SplitN(strings.TrimSpace(pair), "=", 2); len(kv) == 2 {
+ if r := strings.TrimSpace(kv[0]); r != "" {
+ set[r] = true
+ }
+ }
+ }
+ }
+ out := make([]string, 0, len(set))
+ for r := range set {
+ out = append(out, r)
+ }
+ return out
}
func poolTemplateName() string {
@@ -73,16 +118,11 @@ func (s *Server) manufacturePoolBox(ctx context.Context, region, template string
// No-op unless the pool is enabled. Intended to be run in its own goroutine.
func (s *Server) StartPoolReconciler(ctx context.Context, isLeader func() bool) {
if !s.poolEnabled() {
- log.Printf("pool: reconciler disabled (OPENSANDBOX_POOL_ENABLED != 1)")
+ log.Printf("pool: reconciler disabled (OPENSANDBOX_POOL_ENABLED=0)")
return
}
- region := s.region
- if region == "" {
- region = "iad"
- }
template := poolTemplateName()
- target := poolTargetN()
- log.Printf("pool: reconciler started (region=%s template=%s target=%d, leader-gated)", region, template, target)
+ log.Printf("pool: reconciler started (regions=%v default_target=%d template=%s, leader-gated)", s.poolRegions(), s.poolDefaultTarget(), template)
t := time.NewTicker(15 * time.Second)
defer t.Stop()
@@ -94,7 +134,13 @@ func (s *Server) StartPoolReconciler(ctx context.Context, isLeader func() bool)
if isLeader != nil && !isLeader() {
continue
}
- s.reconcilePool(ctx, region, template, target)
+ for _, region := range s.poolRegions() {
+ target := s.poolTargetForRegion(region)
+ if target <= 0 {
+ continue
+ }
+ s.reconcilePool(ctx, region, template, target)
+ }
}
}
}
diff --git a/internal/api/sandbox.go b/internal/api/sandbox.go
index 1e655cb5..13023091 100644
--- a/internal/api/sandbox.go
+++ b/internal/api/sandbox.go
@@ -699,10 +699,11 @@ func isTransientWorkerErr(err error) bool {
// secretStoreID is the resolved store UUID (or nil) from resolveSecretStoreInto;
// passed straight to CreateSandboxSessionWithStatus so the row's secret_store_id
// column is populated and the secret-refresh fanout can find this sandbox.
-// poolEnabled gates the pre-warmed pool claim fast-path. Off unless
-// OPENSANDBOX_POOL_ENABLED=1, so the pool has to be explicitly turned on (and
-// warmed) before creates try to claim from it.
-func (s *Server) poolEnabled() bool { return os.Getenv("OPENSANDBOX_POOL_ENABLED") == "1" }
+// poolEnabled gates the pre-warmed pool (claim fast-path + refill reconciler).
+// ON by default — set OPENSANDBOX_POOL_ENABLED=0 to disable (e.g. an environment
+// whose base image doesn't yet match the target worker version). A pool miss
+// always falls through to a cold create, so "on" is safe.
+func (s *Server) poolEnabled() bool { return os.Getenv("OPENSANDBOX_POOL_ENABLED") != "0" }
// tryClaimPooled attempts the pool fast-path: atomically claim a pre-warmed
// pooled box for (region, template), resume+rebind it on its worker, and return
@@ -730,7 +731,10 @@ func (s *Server) tryClaimPooled(c echo.Context, ctx context.Context, cfg types.S
return false, nil
}
- grpcCtx, cancel := context.WithTimeout(ctx, 60*time.Second)
+ // Tail-latency cap: a healthy claim is ~100ms; if resume+rebind stalls (the
+ // paused-agent-rebind failure mode), abort at 3s and fall through to a cold
+ // create rather than making the customer wait ~40s. Worst case ≈ 3s + cold.
+ grpcCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
claimResp, err := client.ClaimSandbox(grpcCtx, &pb.ClaimSandboxRequest{
SandboxId: box.SandboxID,
@@ -882,7 +886,7 @@ func (s *Server) createSandboxRemote(c echo.Context, ctx context.Context, cfg ty
// try to claim a pre-warmed pooled box — resume + rebind, skipping the ~260ms
// cold restore. A miss or any ineligibility falls through to the cold create
// below, so it's never worse than the status quo.
- if s.store != nil && hasOrg && s.poolEnabled() &&
+ if s.store != nil && hasOrg && s.poolEnabled() && s.poolTargetForRegion(region) > 0 &&
templateRootfsKey == "" && cfg.ImageRef == "" && cfg.CheckpointID == "" &&
len(cfg.EgressAllowlist) == 0 && len(cfg.SecretAllowedHosts) == 0 {
poolTemplate := cfg.Template