Skip to content

An open-source, faster Devin alternative

TypeScript

address D1 authoritative

Brian Reardon9fe991fd8f91
diff --git a/internal/api/sandbox.go b/internal/api/sandbox.go
index a3832b6f..3f09ae2f 100644
--- a/internal/api/sandbox.go
+++ b/internal/api/sandbox.go
@@ -16,6 +16,7 @@ import (
"github.com/opensandbox/opensandbox/internal/controlplane"
"github.com/opensandbox/opensandbox/internal/db"
"github.com/opensandbox/opensandbox/internal/edgeclient"
+ "github.com/opensandbox/opensandbox/internal/observability"
"github.com/opensandbox/opensandbox/pkg/types"
pb "github.com/opensandbox/opensandbox/proto/worker"
)
@@ -574,7 +575,18 @@ func (s *Server) createSandboxRemote(c echo.Context, ctx context.Context, cfg ty
}
cfgJSON, _ := json.Marshal(cfgForPersistence(cfg))
metadataJSON, _ := json.Marshal(cfg.Metadata)
- _, _ = s.store.CreateSandboxSessionWithStatus(ctx, sandboxID, orgID, auth.GetUserID(c), template, region, worker.ID, cfgJSON, metadataJSON, "pending", secretStoreID)
+ // Log + report (Sentry) any session-insert failure loudly. The old
+ // `_, _ =` swallow produced D1-only sandboxes: the edge wrote the
+ // sandboxes_index row, the cell PG row never materialized, and every
+ // subsequent kill/hibernate/exec returned "sandbox not found" via
+ // GetSandboxSession. Continuing past the error keeps the user-visible
+ // create path 200 (the gRPC call below still creates the VM), but the
+ // failure now surfaces in logs and Sentry so we can chase root causes
+ // instead of silently producing orphans.
+ if _, insErr := s.store.CreateSandboxSessionWithStatus(ctx, sandboxID, orgID, auth.GetUserID(c), template, region, worker.ID, cfgJSON, metadataJSON, "pending", secretStoreID); insErr != nil {
+ log.Printf("sandbox: PG session insert failed for %s: %v — sandbox will be D1-only until reconciled", sandboxID, insErr)
+ observability.CaptureError(insErr, "area", "sandbox_create", "op", "pg_session_insert", "sandbox_id", sandboxID, "org_id", uuid.UUID(orgID).String())
+ }
if worker.GoldenVersion != "" {
_ = s.store.SetSandboxGoldenVersion(ctx, sandboxID, worker.GoldenVersion)
}
diff --git a/internal/db/migrations/044_drop_secret_store_fk.down.sql b/internal/db/migrations/044_drop_secret_store_fk.down.sql
new file mode 100644
index 00000000..91f9d85e
--- /dev/null
+++ b/internal/db/migrations/044_drop_secret_store_fk.down.sql
@@ -0,0 +1,7 @@
+-- Restoring the FK requires that every value in sandbox_sessions.secret_store_id
+-- map to a row in cell PG's secret_stores table. Post-cutover that's not true —
+-- D1 is the only source of new stores — so this down migration would fail on
+-- any cell that has accepted post-cutover writes. Left as documentation.
+-- ALTER TABLE sandbox_sessions
+-- ADD CONSTRAINT sandbox_sessions_secret_store_id_fkey
+-- FOREIGN KEY (secret_store_id) REFERENCES secret_stores(id);
diff --git a/internal/db/migrations/044_drop_secret_store_fk.up.sql b/internal/db/migrations/044_drop_secret_store_fk.up.sql
new file mode 100644
index 00000000..ea9b320c
--- /dev/null
+++ b/internal/db/migrations/044_drop_secret_store_fk.up.sql
@@ -0,0 +1,18 @@
+-- Post-cutover: secret_stores moved to D1 (authoritative). The cell-PG FK
+-- from sandbox_sessions.secret_store_id → secret_stores(id) now rejects every
+-- INSERT for sandboxes bound to a D1-managed store, because those stores
+-- don't (and won't) exist in cell PG.
+--
+-- The failure was masked by `_, _ = s.store.CreateSandboxSessionWithStatus(...)`
+-- in createSandboxRemote — D1 got the sandbox row from the edge, cell PG got
+-- nothing, and every subsequent kill/hibernate/exec call 404'd via
+-- GetSandboxSession. Confirmed by reproducing the INSERT manually:
+-- ERROR: insert or update on table "sandbox_sessions" violates foreign key
+-- constraint "sandbox_sessions_secret_store_id_fkey"
+--
+-- Dropping the FK is the right call: cell PG is no longer authoritative for
+-- secret_stores, the column is informational (used by ListRunningSandboxesByStore
+-- for fan-out, which can tolerate stale ids), and the alternative (mirroring
+-- every D1 store into every cell PG) defeats the whole edge-first cutover.
+ALTER TABLE sandbox_sessions
+ DROP CONSTRAINT IF EXISTS sandbox_sessions_secret_store_id_fkey;
diff --git a/internal/db/store.go b/internal/db/store.go
index aec40c97..08f31878 100644
--- a/internal/db/store.go
+++ b/internal/db/store.go
@@ -141,6 +141,7 @@ func (s *Store) Migrate(ctx context.Context) error {
{41, "migrations/041_updated_at_triggers_remaining.up.sql"},
{42, "migrations/042_global_sync_outbox.up.sql"},
{43, "migrations/043_credit_halt.up.sql"},
+ {44, "migrations/044_drop_secret_store_fk.up.sql"},
}
for _, m := range migrations {