Skip to content

An open-source, faster Devin alternative

TypeScript

refactor(webhooks): rename sandbox_lifecycle_events → sandbox_lifecycle_outbox

Igor Zalutskid3d53acbff1f
diff --git a/.agents/work/sandbox-webhooks-rearchitecture.md b/.agents/work/sandbox-webhooks-rearchitecture.md
index 4e293b87..98bb30dc 100644
--- a/.agents/work/sandbox-webhooks-rearchitecture.md
+++ b/.agents/work/sandbox-webhooks-rearchitecture.md
@@ -114,7 +114,10 @@ verify path exists here. The edge already has `org_id` on every event and an ide
> *all-Svix-at-edge* decision — they still describe an edge `webhook_outbox`, a CP
> local index, and an older build order. The **authoritative** record is §0
> (Decisions locked) + §8 (dev env) + the as-built code. As built: the CP keeps
-> only an in-tx outbox (`sandbox_lifecycle_events`) drained by the relay to the
+> only an in-tx **transactional outbox** (`sandbox_lifecycle_outbox` — renamed from
+> the earlier `sandbox_lifecycle_events`, which misleadingly read as an event log;
+> rows are inserted in the same tx as the state change, published to the stream by
+> the relay, then **deleted** — never read as history) drained by the relay to the
> stream (no edge outbox; delivery handoff is events-ingest's sync Svix create);
> the destination index + `has_webhooks` live in **D1 at the edge**, not the CP.
diff --git a/cmd/server/main.go b/cmd/server/main.go
index a075bbf3..cf1bde14 100644
--- a/cmd/server/main.go
+++ b/cmd/server/main.go
@@ -287,7 +287,7 @@ func main() {
defer cr.Stop()
}
- // CP-origin lifecycle outbox relay — drains sandbox_lifecycle_events
+ // CP-origin lifecycle outbox relay — drains sandbox_lifecycle_outbox
// (written in-tx by CP transitions) to the events:{cell} stream, where it
// reaches the edge → Svix like worker-origin events. Worker-origin events
// already flow to the edge directly, so no CP ingestion is needed.
diff --git a/internal/controlplane/lifecycle_outbox_relay.go b/internal/controlplane/lifecycle_outbox_relay.go
index dc8539fc..462362d6 100644
--- a/internal/controlplane/lifecycle_outbox_relay.go
+++ b/internal/controlplane/lifecycle_outbox_relay.go
@@ -12,7 +12,7 @@ import (
"github.com/opensandbox/opensandbox/internal/db"
)
-// LifecycleOutboxRelay drains the in-tx lifecycle outbox (sandbox_lifecycle_events,
+// LifecycleOutboxRelay drains the in-tx lifecycle outbox (sandbox_lifecycle_outbox,
// written transactionally by CP-origin transitions) and publishes each event to
// the cell's events:{cell} stream, where it reaches the edge → Svix exactly like
// worker-origin events. Only events that successfully publish are marked relayed,
diff --git a/internal/db/migrations/049_sandbox_webhooks.down.sql b/internal/db/migrations/049_sandbox_webhooks.down.sql
index 32d2e7d8..ba0ce722 100644
--- a/internal/db/migrations/049_sandbox_webhooks.down.sql
+++ b/internal/db/migrations/049_sandbox_webhooks.down.sql
@@ -1 +1 @@
-DROP TABLE IF EXISTS sandbox_lifecycle_events;
+DROP TABLE IF EXISTS sandbox_lifecycle_outbox;
diff --git a/internal/db/migrations/049_sandbox_webhooks.up.sql b/internal/db/migrations/049_sandbox_webhooks.up.sql
index 72b67eb7..f05800fe 100644
--- a/internal/db/migrations/049_sandbox_webhooks.up.sql
+++ b/internal/db/migrations/049_sandbox_webhooks.up.sql
@@ -3,7 +3,7 @@
-- each row to the cell stream (→ edge → Svix) and deletes it. Destination
-- management + delivery state live at the edge (Svix + the D1 index), not here.
-- See .agents/work/sandbox-webhooks-rearchitecture.md.
-CREATE TABLE IF NOT EXISTS sandbox_lifecycle_events (
+CREATE TABLE IF NOT EXISTS sandbox_lifecycle_outbox (
id TEXT PRIMARY KEY, -- stable/deterministic event id (dedupe key)
seq BIGSERIAL NOT NULL, -- FIFO ordering for the relay drain
org_id UUID NOT NULL REFERENCES orgs(id) ON DELETE CASCADE,
@@ -12,4 +12,4 @@ CREATE TABLE IF NOT EXISTS sandbox_lifecycle_events (
data JSONB NOT NULL DEFAULT '{}',
ts TIMESTAMPTZ NOT NULL DEFAULT now()
);
-CREATE INDEX IF NOT EXISTS sandbox_lifecycle_events_seq_idx ON sandbox_lifecycle_events (seq);
+CREATE INDEX IF NOT EXISTS sandbox_lifecycle_outbox_seq_idx ON sandbox_lifecycle_outbox (seq);
diff --git a/internal/db/webhooks.go b/internal/db/webhooks.go
index 9b6656b3..1ed5f24d 100644
--- a/internal/db/webhooks.go
+++ b/internal/db/webhooks.go
@@ -11,16 +11,21 @@ import (
)
// Sandbox lifecycle webhooks — CP source layer. All-Svix-at-edge: the CP only
-// CAPTURES lifecycle events into the outbox (sandbox_lifecycle_events); the relay
+// CAPTURES lifecycle events into the outbox (sandbox_lifecycle_outbox); the relay
// publishes them to the cell stream and the edge delivers via Svix. Destination
// management + delivery state live at the edge (Svix + D1), not here.
+//
+// sandbox_lifecycle_outbox is a TRANSACTIONAL OUTBOX, not an event log: rows are
+// inserted in the same tx as the state change (so a CP crash between COMMIT and
+// publish can't drop a webhook), the relay publishes each row to events:{cell} and
+// then DELETES it. It is never read as history — don't treat it as one.
// See .agents/work/sandbox-webhooks-rearchitecture.md.
// ---------------------------------------------------------------------------
// Lifecycle events (the in-tx outbox capture)
// ---------------------------------------------------------------------------
-// LifecycleEvent is one canonical sandbox lifecycle moment.
+// LifecycleEvent is one sandbox lifecycle moment captured into the outbox.
type LifecycleEvent struct {
ID string // deterministic (once-only) or unique (recurring) event id
OrgID uuid.UUID
@@ -48,7 +53,7 @@ func recordLifecycleEvent(ctx context.Context, tx pgx.Tx, ev LifecycleEvent) err
ts = time.Now().UTC()
}
_, err := tx.Exec(ctx,
- `INSERT INTO sandbox_lifecycle_events (id, org_id, sandbox_id, type, data, ts)
+ `INSERT INTO sandbox_lifecycle_outbox (id, org_id, sandbox_id, type, data, ts)
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (id) DO NOTHING`,
ev.ID, ev.OrgID, ev.SandboxID, ev.Type, string(ev.Data), ts)
@@ -118,7 +123,7 @@ type LifecycleOutboxRow struct {
func (s *Store) DrainLifecycleOutbox(ctx context.Context, batch int) ([]LifecycleOutboxRow, error) {
rows, err := s.pool.Query(ctx,
`SELECT id, org_id, sandbox_id, type, data, ts
- FROM sandbox_lifecycle_events
+ FROM sandbox_lifecycle_outbox
ORDER BY seq
LIMIT $1`, batch)
if err != nil {
@@ -146,6 +151,6 @@ func (s *Store) DeleteLifecycleOutbox(ctx context.Context, ids []string) error {
return nil
}
_, err := s.pool.Exec(ctx,
- `DELETE FROM sandbox_lifecycle_events WHERE id = ANY($1)`, ids)
+ `DELETE FROM sandbox_lifecycle_outbox WHERE id = ANY($1)`, ids)
return err
}