Skip to content

An open-source, faster Devin alternative

TypeScript

add scaling lock

Brian Reardon3ac574ec1dc0
diff --git a/internal/api/router.go b/internal/api/router.go
index 1fa5df03..23b0cbe3 100644
--- a/internal/api/router.go
+++ b/internal/api/router.go
@@ -233,6 +233,8 @@ func NewServer(mgr sandbox.Manager, ptyMgr *sandbox.PTYManager, apiKey string, o
api.POST("/sandboxes/:id/scale", s.scaleSandbox)
api.PUT("/sandboxes/:id/autoscale", s.setAutoscale)
api.GET("/sandboxes/:id/autoscale", s.getAutoscale)
+ api.PUT("/sandboxes/:id/scaling-lock", s.setScalingLock)
+ api.GET("/sandboxes/:id/scaling-lock", s.getScalingLock)
// Checkpoints
api.POST("/sandboxes/:id/checkpoints", s.createCheckpoint)
diff --git a/internal/api/sandbox.go b/internal/api/sandbox.go
index 602cad0d..e47e270b 100644
--- a/internal/api/sandbox.go
+++ b/internal/api/sandbox.go
@@ -1117,6 +1117,18 @@ func (s *Server) scaleSandbox(c echo.Context) error {
}
}
+ // Scaling lock: refuse if the user has explicitly pinned this sandbox's
+ // resources. Same code that the autoscale endpoint and the autoscaler
+ // loop use, so SDK consumers can branch on a single error code.
+ if s.store != nil {
+ if locked, err := s.store.GetScalingLock(c.Request().Context(), id); err == nil && locked {
+ return c.JSON(http.StatusForbidden, map[string]any{
+ "error": "scaling is locked on this sandbox — unlock via PUT /scaling-lock to allow size changes",
+ "code": "scaling_locked",
+ })
+ }
+ }
+
cpuPercent := vcpus * 100
maxMemoryBytes := int64(req.MemoryMB) * 1024 * 1024
cpuMaxUsec := int64(cpuPercent) * 1000
diff --git a/internal/api/sandbox_autoscale.go b/internal/api/sandbox_autoscale.go
index bb34b2db..6a56b14e 100644
--- a/internal/api/sandbox_autoscale.go
+++ b/internal/api/sandbox_autoscale.go
@@ -45,6 +45,16 @@ func (s *Server) setAutoscale(c echo.Context) error {
}
if req.Enabled {
+ // Refuse if the sandbox is scaling-locked. The lock auto-disables
+ // autoscale on toggle; refusing here prevents a user from
+ // re-enabling it while the lock is still on (which would be a
+ // confusing two-state mismatch).
+ if locked, err := s.store.GetScalingLock(c.Request().Context(), sandboxID); err == nil && locked {
+ return c.JSON(http.StatusForbidden, map[string]any{
+ "error": "scaling is locked on this sandbox — unlock via PUT /scaling-lock to enable autoscale",
+ "code": "scaling_locked",
+ })
+ }
if _, err := types.ValidateMemoryMB(req.MinMemoryMB); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "minMemoryMB: " + err.Error()})
}
diff --git a/internal/api/sandbox_scaling_lock.go b/internal/api/sandbox_scaling_lock.go
new file mode 100644
index 00000000..ca84b8de
--- /dev/null
+++ b/internal/api/sandbox_scaling_lock.go
@@ -0,0 +1,74 @@
+package api
+
+import (
+ "log"
+ "net/http"
+
+ "github.com/labstack/echo/v4"
+)
+
+// PUT /api/sandboxes/:id/scaling-lock
+//
+// Locks (or unlocks) a sandbox against any size change. When locked:
+//
+// - POST /scale returns 403 with code "scaling_locked"
+// - PUT /autoscale {enabled:true} returns 403 with code "scaling_locked"
+// - The per-sandbox autoscaler skips the sandbox (filter in
+// ListAutoscaleEnabled, defense in depth)
+//
+// Locking ALSO disables autoscale on the row — a user who doesn't want
+// their sandbox to scale almost certainly doesn't want autoscale either,
+// and leaving it half-on would be confusing the next time they unlock.
+// Unlocking does NOT auto-re-enable autoscale; the user re-enables
+// explicitly via PUT /autoscale.
+func (s *Server) setScalingLock(c echo.Context) error {
+ sandboxID := c.Param("id")
+ if s.store == nil {
+ return c.JSON(http.StatusServiceUnavailable, errSandboxNotAvailable)
+ }
+
+ var req struct {
+ Locked bool `json:"locked"`
+ }
+ if err := c.Bind(&req); err != nil {
+ return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid body"})
+ }
+
+ if err := s.store.SetScalingLock(c.Request().Context(), sandboxID, req.Locked); err != nil {
+ // Distinguish "not found" from internal errors.
+ if err.Error() == "sandbox "+sandboxID+" not found" {
+ return c.JSON(http.StatusNotFound, map[string]string{"error": "sandbox not found"})
+ }
+ return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
+ }
+
+ if req.Locked {
+ // Disable autoscale alongside the lock — single user-visible state
+ // instead of two overlapping toggles. Best-effort, logged on failure.
+ if err := s.store.SetSandboxAutoscale(c.Request().Context(), sandboxID, false, 0, 0); err != nil {
+ log.Printf("scaling-lock: failed to disable autoscale on %s after lock: %v", sandboxID, err)
+ }
+ }
+
+ return c.JSON(http.StatusOK, map[string]any{
+ "sandboxID": sandboxID,
+ "locked": req.Locked,
+ })
+}
+
+// GET /api/sandboxes/:id/scaling-lock
+func (s *Server) getScalingLock(c echo.Context) error {
+ sandboxID := c.Param("id")
+ if s.store == nil {
+ return c.JSON(http.StatusServiceUnavailable, errSandboxNotAvailable)
+ }
+
+ locked, err := s.store.GetScalingLock(c.Request().Context(), sandboxID)
+ if err != nil {
+ return c.JSON(http.StatusNotFound, map[string]string{"error": "sandbox not found"})
+ }
+ return c.JSON(http.StatusOK, map[string]any{
+ "sandboxID": sandboxID,
+ "locked": locked,
+ })
+}
diff --git a/internal/db/migrations/036_sandbox_scaling_lock.down.sql b/internal/db/migrations/036_sandbox_scaling_lock.down.sql
new file mode 100644
index 00000000..3f60ac4a
--- /dev/null
+++ b/internal/db/migrations/036_sandbox_scaling_lock.down.sql
@@ -0,0 +1 @@
+ALTER TABLE sandbox_sessions DROP COLUMN scaling_locked;
diff --git a/internal/db/migrations/036_sandbox_scaling_lock.up.sql b/internal/db/migrations/036_sandbox_scaling_lock.up.sql
new file mode 100644
index 00000000..dfaccb2d
--- /dev/null
+++ b/internal/db/migrations/036_sandbox_scaling_lock.up.sql
@@ -0,0 +1,11 @@
+-- Sandbox-level "scaling lock". When true, blocks BOTH manual scaling
+-- (POST /scale) AND automatic scaling (per-sandbox autoscaler). Useful when
+-- the user wants predictable, fixed resources for a sandbox — e.g. a
+-- benchmark, a long-running pinned workload, or a billing-sensitive setup.
+--
+-- Locking auto-disables autoscale (the lock handler clears autoscale_enabled
+-- alongside setting scaling_locked=TRUE), but the partial-index filter here
+-- is defense in depth in case the two columns ever drift.
+
+ALTER TABLE sandbox_sessions
+ ADD COLUMN scaling_locked BOOLEAN NOT NULL DEFAULT FALSE;
diff --git a/internal/db/sandbox_autoscale.go b/internal/db/sandbox_autoscale.go
index 68feb619..708464c3 100644
--- a/internal/db/sandbox_autoscale.go
+++ b/internal/db/sandbox_autoscale.go
@@ -85,6 +85,7 @@ func (s *Store) ListAutoscaleEnabled(ctx context.Context) ([]AutoscaleSandbox, e
WHERE autoscale_enabled = TRUE
AND status = 'running'
AND migrating_to_worker = ''
+ AND scaling_locked = FALSE
`)
if err != nil {
return nil, fmt.Errorf("list autoscale-enabled: %w", err)
diff --git a/internal/db/sandbox_scaling_lock.go b/internal/db/sandbox_scaling_lock.go
new file mode 100644
index 00000000..317f5e2c
--- /dev/null
+++ b/internal/db/sandbox_scaling_lock.go
@@ -0,0 +1,40 @@
+package db
+
+import (
+ "context"
+ "fmt"
+)
+
+// SetScalingLock toggles the per-sandbox scaling lock. When true, the sandbox
+// is exempt from both manual /scale calls and the per-sandbox autoscaler.
+// The handler that calls this should also clear autoscale_enabled when
+// locking — done at the API layer, not here, so this function is a pure
+// column setter.
+func (s *Store) SetScalingLock(ctx context.Context, sandboxID string, locked bool) error {
+ tag, err := s.pool.Exec(ctx, `
+ UPDATE sandbox_sessions
+ SET scaling_locked = $1
+ WHERE sandbox_id = $2
+ `, locked, sandboxID)
+ if err != nil {
+ return fmt.Errorf("set scaling lock on %s: %w", sandboxID, err)
+ }
+ if tag.RowsAffected() == 0 {
+ return fmt.Errorf("sandbox %s not found", sandboxID)
+ }
+ return nil
+}
+
+// GetScalingLock returns the current lock state for a sandbox. Returns false
+// (and no error) if the sandbox doesn't exist — the caller is expected to
+// have already validated the sandbox via GetSandboxSession.
+func (s *Store) GetScalingLock(ctx context.Context, sandboxID string) (bool, error) {
+ var locked bool
+ err := s.pool.QueryRow(ctx, `
+ SELECT scaling_locked FROM sandbox_sessions WHERE sandbox_id = $1
+ `, sandboxID).Scan(&locked)
+ if err != nil {
+ return false, err
+ }
+ return locked, nil
+}
diff --git a/internal/db/store.go b/internal/db/store.go
index 013b1a36..986c070d 100644
--- a/internal/db/store.go
+++ b/internal/db/store.go
@@ -126,6 +126,7 @@ func (s *Store) Migrate(ctx context.Context) error {
{33, "migrations/030_billable_events.up.sql"},
{34, "migrations/031_orgs_billing_mode.up.sql"},
{35, "migrations/035_sandbox_autoscale.up.sql"},
+ {36, "migrations/036_sandbox_scaling_lock.up.sql"},
}
for _, m := range migrations {