Skip to content

An open-source, faster Devin alternative

TypeScript

more eventing gap fix

Brian Reardonbd07e7605e2f
diff --git a/cloudflare-workers/api-edge/src/index.ts b/cloudflare-workers/api-edge/src/index.ts
index de25f555..bc501035 100644
--- a/cloudflare-workers/api-edge/src/index.ts
+++ b/cloudflare-workers/api-edge/src/index.ts
@@ -542,6 +542,21 @@ async function proxyToCellSDK(req: Request, env: Env, ctx: ExecutionContext, cal
postUpdate = { status: "hibernated", setStopped: false };
} else if (req.method === "POST" && path === `/api/sandboxes/${id}/wake`) {
postUpdate = { status: "running", setStopped: false };
+ // Halt-gate the wake. D1 is authoritative for is_halted. The cell-side
+ // gate that used to do this read the dropped orgs table post-041 and
+ // silently fell through, letting halted orgs wake. Mirror the create
+ // flow's halt check here so wake gets the same treatment.
+ const haltRow = await env.OPENCOMPUTER_DB.prepare(
+ "SELECT is_halted FROM orgs WHERE id = ?1",
+ )
+ .bind(caller.orgID)
+ .first<{ is_halted: number }>();
+ if (haltRow?.is_halted === 1) {
+ return json(
+ { error: "org is halted — upgrade to pro or wait for credit refill" },
+ 402,
+ );
+ }
}
// WebSocket upgrade — preserve the upgrade context by cloning the inbound
diff --git a/cmd/worker/main.go b/cmd/worker/main.go
index 8df8b684..b20f1239 100644
--- a/cmd/worker/main.go
+++ b/cmd/worker/main.go
@@ -767,9 +767,25 @@ func main() {
OnHibernateIdle: func(sandboxIDs []string) {
for _, id := range sandboxIDs {
if checkpointStore != nil {
- _, err := mgr.Hibernate(context.Background(), id, checkpointStore)
+ result, err := mgr.Hibernate(context.Background(), id, checkpointStore)
if err != nil {
log.Printf("pressure-hibernate %s: %v", id, err)
+ continue
+ }
+ // Mirror the per-sandbox SQLite event the gRPC Hibernate
+ // handler writes — without this, auto-hibernate writes
+ // the SUCCEED to local PG but never publishes "hibernated"
+ // to events-ingest, so D1 sandboxes_index drifts to
+ // "running" while the cell PG says "hibernated".
+ if sandboxDBMgr != nil {
+ if sdb, dbErr := sandboxDBMgr.Get(id); dbErr == nil {
+ _ = sdb.LogEvent("hibernated", map[string]string{
+ "sandbox_id": id,
+ "checkpoint_key": result.HibernationKey,
+ "reason": "pressure_auto",
+ })
+ }
+ _ = sandboxDBMgr.Remove(id)
}
}
}
diff --git a/internal/api/admin_org.go b/internal/api/admin_org.go
index cc38ce37..5a839fe2 100644
--- a/internal/api/admin_org.go
+++ b/internal/api/admin_org.go
@@ -85,14 +85,10 @@ func (s *Server) HaltOrg(ctx context.Context, orgIDStr, reason string) (int, err
return 0, fmt.Errorf("invalid org_id: %w", err)
}
- // Mirror DO halt state to cell PG first — the wake handler reads this
- // column synchronously, so doing it before kicking off the async halt
- // closes the race where a wake arrives while the halt goroutine is
- // still iterating.
- if err := s.store.UpdateOrgHaltState(ctx, orgID, true); err != nil {
- // Non-fatal — log and continue. The reconciler will re-issue if needed.
- log.Printf("admin: halt-org %s: UpdateOrgHaltState failed: %v", orgIDStr, err)
- }
+ // Halt state now lives in D1 only (migration 041 dropped the local orgs
+ // table). The edge gates create + wake against D1.is_halted before the
+ // request ever reaches the cell, so the cell-PG mirror this used to keep
+ // is no longer needed. Sandbox hibernation below still proceeds.
sessions, err := s.store.ListSandboxSessions(ctx, orgID, "running", 1000, 0)
if err != nil {
@@ -148,9 +144,8 @@ func (s *Server) ResumeOrg(ctx context.Context, orgIDStr string, skipResume bool
return 0, fmt.Errorf("invalid org_id: %w", err)
}
- if err := s.store.UpdateOrgHaltState(ctx, orgID, false); err != nil {
- log.Printf("admin: resume-org %s: UpdateOrgHaltState failed: %v", orgIDStr, err)
- }
+ // Cell-PG orgs table is gone post-041; D1 is authoritative for is_halted.
+ // Resume of running sandboxes still proceeds below.
if skipResume {
return 0, nil
diff --git a/internal/api/sandbox.go b/internal/api/sandbox.go
index 4879e397..c953ee65 100644
--- a/internal/api/sandbox.go
+++ b/internal/api/sandbox.go
@@ -1734,20 +1734,10 @@ func (s *Server) wakeSandbox(c echo.Context) error {
var req types.WakeRequest
_ = c.Bind(&req)
- // Credit gate: refuse to wake when the org is halted. The CreditAccount
- // DO is authoritative; `is_halted` is the cell-local mirror set by the
- // /admin/halt-org webhook handler. Pro orgs are never halted by credits,
- // so checking is_halted naturally lets them through. Falls through if
- // the org row doesn't exist locally (new app2 user with no backfill) —
- // the edge already gated on D1's is_halted before this request was
- // routed to us, so a missing row means the edge said yes.
- if orgID, ok := auth.GetOrgID(c); ok && s.store != nil {
- if org, err := s.store.GetOrg(ctx, orgID); err == nil && org.IsHalted {
- return c.JSON(http.StatusPaymentRequired, map[string]string{
- "error": "org is halted — upgrade to pro or wait for credit refill",
- })
- }
- }
+ // Halt-gate moved to the edge (proxyToCellSDK on /wake) post-041 — the
+ // cell-PG orgs table this used to read is gone, and D1 is authoritative.
+ // halt_reconciler still enforces against running sandboxes; this wake
+ // path is reached only after the edge has gated on D1.is_halted.
// Server mode: pick any worker, dispatch via gRPC
if s.workerRegistry != nil {