An open-source, faster Devin alternative
TypeScript
feat(capacity): keep phase-1 PR additive — defer runtime admission
Igor Zalutski67b84583cfb7
diff --git a/internal/api/sandbox.go b/internal/api/sandbox.goindex 4d59c3e7..685fe624 100644--- a/internal/api/sandbox.go+++ b/internal/api/sandbox.go@@ -52,25 +52,6 @@ func (s *Server) createSandbox(c echo.Context) error {})}- // Memory cap (orgs.max_memory_gb). Same field is used as the- // reservationLimitGb in the capacity calendar — the cap and the- // reservation ceiling are the same number by design.- if org.MaxMemoryGB > 0 {- running, err := s.store.GetOrgRunningMemoryMB(ctx, orgID)- if err == nil {- requestedMB := cfg.MemoryMB- if requestedMB == 0 {- requestedMB = 4096 // matches the default applied below- }- maxMB := org.MaxMemoryGB * 1024- if running+requestedMB > maxMB {- return c.JSON(http.StatusForbidden, map[string]string{- "error": fmt.Sprintf("memory cap exceeded: %d MB running + %d MB requested > %d MB org limit", running, requestedMB, maxMB),- })- }- }- }-// Free-tier: trial credits gate + machine-size restriction.if org.Plan == "free" {if org.FreeCreditsRemainingCents <= 0 {@@ -1590,9 +1571,7 @@ func (s *Server) wakeSandbox(c echo.Context) error {var req types.WakeRequest_ = c.Bind(&req)- // Free-tier credits gate + memory cap admission. Wake admits the sandbox- // back into the running set, so it consumes against orgs.max_memory_gb- // just like a fresh create.+ // Free-tier credits gate: refuse to wake if trial credits are exhausted.if orgID, ok := auth.GetOrgID(c); ok && s.store != nil {if org, err := s.store.GetOrg(ctx, orgID); err == nil {if org.Plan == "free" && org.FreeCreditsRemainingCents <= 0 {@@ -1600,18 +1579,6 @@ func (s *Server) wakeSandbox(c echo.Context) error {"error": "free trial credits exhausted — upgrade to pro to resume sandboxes",})}- if org.MaxMemoryGB > 0 {- wakeMB, err := s.store.GetSandboxLatestMemoryMB(ctx, id)- if err == nil {- running, rerr := s.store.GetOrgRunningMemoryMB(ctx, orgID)- maxMB := org.MaxMemoryGB * 1024- if rerr == nil && running+wakeMB > maxMB {- return c.JSON(http.StatusForbidden, map[string]string{- "error": fmt.Sprintf("memory cap exceeded: %d MB running + %d MB wake > %d MB org limit", running, wakeMB, maxMB),- })- }- }- }}}diff --git a/internal/db/migrations/029_orgs_max_memory_gb.up.sql b/internal/db/migrations/029_orgs_max_memory_gb.up.sqlindex c24e4caf..d07ce506 100644--- a/internal/db/migrations/029_orgs_max_memory_gb.up.sql+++ b/internal/db/migrations/029_orgs_max_memory_gb.up.sql@@ -1,8 +1,10 @@-- Per-org ceiling on concurrent in-memory sandbox capacity, in whole GB.--- Used both as the reservationLimitGb in the capacity calendar and as the--- runtime admission cap when creating or waking sandboxes. Same shape as--- max_disk_mb (migration 019). Default 2 covers free tier; pro orgs are--- backfilled to 16. Per-row admin overrides remain available afterwards.-ALTER TABLE orgs ADD COLUMN max_memory_gb INTEGER NOT NULL DEFAULT 2+-- v1 use: returned as reservationLimitGb on the capacity calendar so+-- customers see how much they can reserve per interval. Runtime admission+-- against this column is deferred to a follow-up phase — see+-- ws-pricing/work/001 "Runtime admission". Same shape as max_disk_mb+-- (migration 019). Default 8 covers free tier; pro orgs are backfilled to+-- 128. Per-row admin overrides remain available afterwards.+ALTER TABLE orgs ADD COLUMN max_memory_gb INTEGER NOT NULL DEFAULT 8CHECK (max_memory_gb >= 0);-UPDATE orgs SET max_memory_gb = 16 WHERE plan = 'pro';+UPDATE orgs SET max_memory_gb = 128 WHERE plan = 'pro';diff --git a/internal/db/store.go b/internal/db/store.goindex b4068fef..5c518dba 100644--- a/internal/db/store.go+++ b/internal/db/store.go@@ -793,37 +793,6 @@ func (s *Store) CountActiveSandboxes(ctx context.Context, orgID uuid.UUID) (int,return count, err}-// GetOrgRunningMemoryMB sums memory_mb of currently-open scale events for-// the org's running sandboxes. Used to enforce orgs.max_memory_gb at create-// and wake admission. Hibernated sandboxes are excluded — their memory is-// only "spent" when they're running.-func (s *Store) GetOrgRunningMemoryMB(ctx context.Context, orgID uuid.UUID) (int, error) {- var total int- err := s.pool.QueryRow(ctx,- `SELECT COALESCE(SUM(e.memory_mb), 0)::int- FROM sandbox_scale_events e- INNER JOIN sandbox_sessions s ON s.sandbox_id = e.sandbox_id- WHERE e.org_id = $1- AND e.ended_at IS NULL- AND s.status = 'running'`, orgID,- ).Scan(&total)- return total, err-}--// GetSandboxLatestMemoryMB returns the memory_mb of the sandbox's most-// recent scale event (open or closed). Used at wake time to size the-// admission check before transitioning the sandbox back to running.-func (s *Store) GetSandboxLatestMemoryMB(ctx context.Context, sandboxID string) (int, error) {- var mem int- err := s.pool.QueryRow(ctx,- `SELECT memory_mb FROM sandbox_scale_events- WHERE sandbox_id = $1- ORDER BY started_at DESC- LIMIT 1`, sandboxID,- ).Scan(&mem)- return mem, err-}-// --- Command Log operations (for NATS sync consumer) ---type CommandLog struct {