An open-source, faster Devin alternative
TypeScript
fix(webhooks): live-probe follow-ups (review round 3b)
Igor Zalutski23e2496d114c
diff --git a/cloudflare-workers/api-edge/src/webhooks.ts b/cloudflare-workers/api-edge/src/webhooks.tsindex b5b5b24f..badbf38f 100644--- a/cloudflare-workers/api-edge/src/webhooks.ts+++ b/cloudflare-workers/api-edge/src/webhooks.ts@@ -211,6 +211,8 @@ async function createDestination(await refreshHasWebhooks(env, orgID);return {+ // Same shape as toWire (list/get) + the one-time secret, so create/list/get+ // are consistent (hasSecret + updatedAt included).wire: {id,url: spec.url,@@ -218,7 +220,9 @@ async function createDestination(sandboxId: spec.sandboxId ?? null,name: spec.name ?? null,enabled: disabled === 0,+ hasSecret: true,createdAt: new Date(ts * 1000).toISOString(),+ updatedAt: new Date(ts * 1000).toISOString(),},secret,};@@ -429,10 +433,27 @@ async function testWebhook(env: WebhookEnv, sx: SvixClient, caller: Caller, id:}const expanded = expandEventTypes(stored);const eventType = expanded && expanded.length ? expanded[0] : "sandbox.created";+ const sandboxId = row.sandbox_id ?? "sb-test";+ const eventId = `test.${id}.${randomHex(6)}`;+ // Send the REAL delivered envelope (the WebhookDelivery<SandboxLifecycleEvent>+ // shape verifyWebhook returns), marked with data.test=true, so a consumer+ // exercises its actual parser — not a reduced synthetic body.const msg = await sx.createMessage(row.svix_app_id, {eventType,- payload: { type: eventType, sandboxId: row.sandbox_id ?? "sb-test", test: true },- eventId: `test.${id}.${randomHex(6)}`,+ payload: {+ type: eventType,+ sandboxId,+ eventId,+ event: {+ id: eventId,+ ts: new Date().toISOString(),+ orgId: caller.orgID,+ sandboxId,+ type: eventType,+ data: { test: true },+ },+ },+ eventId,channels: row.sandbox_id ? [row.sandbox_id] : undefined,});return json({ ok: true, eventType, messageId: msg.id });diff --git a/cloudflare-workers/schema.sql b/cloudflare-workers/schema.sqlindex 703e9aae..d2a93958 100644--- a/cloudflare-workers/schema.sql+++ b/cloudflare-workers/schema.sql@@ -145,6 +145,10 @@ CREATE TABLE IF NOT EXISTS sandboxes_index (worker_id TEXT,status TEXT NOT NULL, -- running | hibernated | stopped | errortemplate_id TEXT,+ -- Requested resources, projected by api-edge's POST /api/sandboxes (SSE) path+ -- and surfaced by GET /api/sandboxes. Nullable: not every create path sets them.+ cpu_count INTEGER,+ memory_mb INTEGER,created_at INTEGER NOT NULL,last_event_at INTEGER,stopped_at INTEGERdiff --git a/docs/api-reference/webhooks/delivery-get.mdx b/docs/api-reference/webhooks/delivery-get.mdxindex f395d01f..9c800f44 100644--- a/docs/api-reference/webhooks/delivery-get.mdx+++ b/docs/api-reference/webhooks/delivery-get.mdx@@ -5,6 +5,12 @@ api: 'GET /api/webhooks/{id}/deliveries/{deliveryId}'Fetch one delivered message by id. See [Webhooks](/sandboxes/webhooks#deliveries).+<Note>+The delivery provider's attempt index and message store are eventually consistent: a message id can+appear in [list deliveries](/api-reference/webhooks/deliveries-list) a few seconds before this+endpoint can fetch it, so a `404` immediately after a delivery is transient — retry shortly.+</Note>+<ParamField path="id" type="string" required>Destination ID (`whk_…`).</ParamField>diff --git a/docs/api-reference/webhooks/test.mdx b/docs/api-reference/webhooks/test.mdxindex 03ee4eb1..347fd856 100644--- a/docs/api-reference/webhooks/test.mdx+++ b/docs/api-reference/webhooks/test.mdx@@ -3,11 +3,19 @@ title: 'Test Webhook'api: 'POST /api/webhooks/{id}/test'----Enqueue a sample event to the destination to exercise the endpoint. It sends a real, signed-message of a concrete type the destination is subscribed to (the first of its `eventTypes`,-defaulting to `sandbox.created`), so it respects the destination's filters. Delivery is-**asynchronous** — this returns once the message is accepted; check-[deliveries](/api-reference/webhooks/deliveries-list) for the outcome. See [Webhooks](/sandboxes/webhooks).+Enqueue a sample event to exercise your endpoint. It sends a real, signed message of a concrete type+the destination is subscribed to (the first of its `eventTypes`, defaulting to `sandbox.created`),+carrying the **normal delivery envelope** with `event.data.test = true` — so you exercise your real+verifier and parser. Delivery is **asynchronous** — this returns once the message is accepted; check+[deliveries](/api-reference/webhooks/deliveries-list) for the outcome.++<Note>+The sample is published through your org's webhook app, so it is **not** isolated to this one+destination: any other destination subscribed to the same event type (and matching scope) also+receives it (likewise marked `event.data.test = true`).+</Note>++See [Webhooks](/sandboxes/webhooks).<ParamField path="id" type="string" required>Destination ID (`whk_…`).diff --git a/docs/sandboxes/webhooks.mdx b/docs/sandboxes/webhooks.mdxindex 8ceec981..23a01e83 100644--- a/docs/sandboxes/webhooks.mdx+++ b/docs/sandboxes/webhooks.mdx@@ -174,7 +174,7 @@ Redelivery re-sends the **same** message to the endpoint (same `svix-id`) — fo## Signing, retries, and dedupe- **HTTPS only**, checked at registration. The delivery layer (Svix) additionally blocks private, loopback, link-local, and cloud-metadata addresses at send time.-- **Always signed** with [Standard Webhooks](https://www.standardwebhooks.com) (delivery is by [Svix](https://www.svix.com)). Every request carries `svix-id`, `svix-timestamp`, and `svix-signature` = `v1,<base64(HMAC-SHA256(secret, "{svix-id}.{svix-timestamp}.{rawBody}"))>`. Verify against the **raw request body, before parsing it**; the timestamp guards replay. Any custom headers you attached as destination `metadata` ride along too. Your `secret` is write-only.+- **Always signed** with [Standard Webhooks](https://www.standardwebhooks.com) (delivery is by [Svix](https://www.svix.com)). Every request carries `svix-id`, `svix-timestamp`, and `svix-signature` = `v1,<base64(HMAC-SHA256(secret, "{svix-id}.{svix-timestamp}.{rawBody}"))>`. Verify against the **raw request body, before parsing it**; the timestamp guards replay. Any custom headers you attached as destination `metadata` ride along too. Your signing `secret` is re-fetchable any time via `GET /api/webhooks/{id}/secret` and rotatable via update (`rotateSecret: true`).- **At-least-once.** Once an event is handed off it's retried until it succeeds or the delivery layer gives up. A duplicate can still arrive, so **dedupe on `svix-id`** (the verifier exposes it as both `deliveryId` and `dedupeId`).- **Not strictly ordered.** Events for one sandbox can arrive out of order (e.g. `sandbox.ready` before `sandbox.created`). Treat each event independently and key off `event.type`; don't assume an ordering.- **Success / retry / give-up.** A delivery succeeds on HTTP `2xx`. Failures are retried on Svix's managed backoff (immediately, then 5s, 5m, 30m, 2h, 5h, 10h, 10h); an endpoint that keeps failing for ~5 days is auto-disabled. Inspect attempts and recover failed messages via the [deliveries API](#deliveries). Unsafe targets (private / loopback / cloud-metadata addresses) are blocked by the delivery layer.