An open-source, faster Devin alternative
TypeScript
fix(webhooks): review round — functional gaps (ordering, filters, fields)
Igor Zalutski4e492c4bb8ec
diff --git a/cloudflare-workers/api-edge/src/webhooks.ts b/cloudflare-workers/api-edge/src/webhooks.tsindex ffd04587..8f850c5d 100644--- a/cloudflare-workers/api-edge/src/webhooks.ts+++ b/cloudflare-workers/api-edge/src/webhooks.ts@@ -79,7 +79,9 @@ function toWire(r: DestRow): Record<string, unknown> {sandboxId: r.sandbox_id,name: r.name,enabled: r.disabled === 0,+ hasSecret: true, // Svix always generates a signing secretcreatedAt: new Date(r.created_at * 1000).toISOString(),+ updatedAt: new Date(r.updated_at * 1000).toISOString(),};}@@ -114,20 +116,56 @@ function isHttpsURL(u: unknown): u is string {}}+// The concrete sandbox lifecycle event types. Svix filterTypes are exact (no+// wildcards) and must be registered before use — keep in sync with the docs/SDK.+const SANDBOX_EVENT_TYPES = [+ "sandbox.created",+ "sandbox.ready",+ "sandbox.hibernated",+ "sandbox.resumed",+ "sandbox.stopped",+ "sandbox.migrated",+ "sandbox.checkpoint.created",+ "sandbox.forked",+ "sandbox.scaled",+ "sandbox.preview_url.changed",+];++// expandEventTypes turns a user filter (which may use "prefix.*", e.g. "sandbox.*")+// into concrete Svix filterTypes. undefined / empty → no filter (all types).+function expandEventTypes(filters?: string[] | null): string[] | undefined {+ if (!filters || filters.length === 0) return undefined;+ const out = new Set<string>();+ for (const f of filters) {+ if (f.endsWith(".*")) {+ const prefix = f.slice(0, -1); // keep the trailing "."+ for (const t of SANDBOX_EVENT_TYPES) if (t.startsWith(prefix)) out.add(t);+ } else {+ out.add(f);+ }+ }+ return out.size ? [...out] : undefined;+}+// Create a Svix endpoint for (org, optional sandbox) + persist the index row.// Shared by POST /api/webhooks and the inline-create internal endpoint.async function createDestination(env: WebhookEnv,sx: SvixClient,orgID: string,- spec: { url: string; eventTypes?: string[]; sandboxId?: string | null; name?: string | null; secret?: string; metadata?: Record<string, string> },+ spec: { url: string; eventTypes?: string[]; sandboxId?: string | null; name?: string | null; secret?: string; metadata?: Record<string, string>; enabled?: boolean },): Promise<{ wire: Record<string, unknown>; secret: string }> {const app = await sx.ensureApplication(orgID, `org:${orgID}`);+ // Svix filterTypes are exact + must exist: expand wildcards and register each.+ const filterTypes = expandEventTypes(spec.eventTypes);+ if (filterTypes) await Promise.all(filterTypes.map((t) => sx.ensureEventType(t)));+const epParams: EndpointParams = { url: spec.url };if (spec.name) epParams.description = spec.name;- if (spec.eventTypes && spec.eventTypes.length) epParams.filterTypes = spec.eventTypes;+ if (filterTypes) epParams.filterTypes = filterTypes;if (spec.sandboxId) epParams.channels = [spec.sandboxId];if (spec.secret) epParams.secret = spec.secret;+ if (spec.enabled === false) epParams.disabled = true;const ep = await sx.createEndpoint(app.id, epParams);if (spec.metadata && Object.keys(spec.metadata).length) {await sx.setEndpointHeaders(app.id, ep.id, spec.metadata);@@ -136,23 +174,20 @@ async function createDestination(const id = "whk_" + randomHex(12);const ts = nowSec();- await env.OPENCOMPUTER_DB.prepare(- `INSERT INTO webhook_destinations- (id, org_id, svix_app_id, svix_endpoint_id, url, event_types, sandbox_id, name, disabled, created_at, updated_at)- VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, 0, ?9, ?9)`,- )- .bind(- id,- orgID,- app.id,- ep.id,- spec.url,- JSON.stringify(spec.eventTypes ?? []),- spec.sandboxId ?? null,- spec.name ?? null,- ts,+ const disabled = spec.enabled === false ? 1 : 0;+ try {+ await env.OPENCOMPUTER_DB.prepare(+ `INSERT INTO webhook_destinations+ (id, org_id, svix_app_id, svix_endpoint_id, url, event_types, sandbox_id, name, disabled, created_at, updated_at)+ VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?10)`,)- .run();+ .bind(id, orgID, app.id, ep.id, spec.url, JSON.stringify(spec.eventTypes ?? []), spec.sandboxId ?? null, spec.name ?? null, disabled, ts)+ .run();+ } catch (e) {+ // Roll back the Svix endpoint so a D1 failure doesn't leave an orphan.+ await sx.deleteEndpoint(app.id, ep.id).catch(() => {});+ throw e;+ }await refreshHasWebhooks(env, orgID);return {@@ -162,7 +197,7 @@ async function createDestination(eventTypes: spec.eventTypes ?? [],sandboxId: spec.sandboxId ?? null,name: spec.name ?? null,- enabled: true,+ enabled: disabled === 0,createdAt: new Date(ts * 1000).toISOString(),},secret,@@ -236,7 +271,15 @@ export async function handleWebhooksAPI(}async function createWebhook(req: Request, env: WebhookEnv, sx: SvixClient, caller: Caller): Promise<Response> {- let body: { url?: string; eventTypes?: string[]; sandboxId?: string; name?: string; metadata?: Record<string, string> };+ let body: {+ url?: string;+ eventTypes?: string[];+ sandboxId?: string;+ name?: string;+ metadata?: Record<string, string>;+ secret?: string;+ enabled?: boolean;+ };try {body = await req.json();} catch {@@ -250,6 +293,8 @@ async function createWebhook(req: Request, env: WebhookEnv, sx: SvixClient, callsandboxId: body.sandboxId ?? null,name: body.name ?? null,metadata: body.metadata,+ secret: body.secret,+ enabled: body.enabled,});// Secret is returned once on create (Svix-generated; always re-fetchable via Svix).return json({ ...wire, secret }, 201);@@ -267,32 +312,44 @@ async function listWebhooks(env: WebhookEnv, caller: Caller): Promise<Response>async function patchWebhook(req: Request, env: WebhookEnv, sx: SvixClient, caller: Caller, id: string): Promise<Response> {const row = await getDest(env, caller.orgID, id);if (!row) return json({ error: "webhook not found" }, 404);- let body: { url?: string; eventTypes?: string[]; enabled?: boolean };+ let body: { url?: string; eventTypes?: string[] | null; enabled?: boolean; name?: string };try {body = await req.json();} catch {return json({ error: "invalid JSON body" }, 400);}- const ep: EndpointParams = { url: body.url ?? row.url };- if (body.eventTypes !== undefined) ep.filterTypes = body.eventTypes;+ if (body.url !== undefined && !isHttpsURL(body.url)) return json({ error: "url must be a valid https URL" }, 400);++ // Index filter (original strings): null clears it, undefined keeps the stored+ // one. The Svix endpoint gets the expanded + registered concrete types.+ let originalTypes: string[];+ if (body.eventTypes === null) originalTypes = [];+ else if (body.eventTypes !== undefined) originalTypes = body.eventTypes;else {try {- ep.filterTypes = JSON.parse(row.event_types || "[]");+ originalTypes = JSON.parse(row.event_types || "[]");} catch {- ep.filterTypes = [];+ originalTypes = [];}}+ const filterTypes = expandEventTypes(originalTypes);+ if (filterTypes) await Promise.all(filterTypes.map((t) => sx.ensureEventType(t)));++ // updateEndpoint is a full PUT replace — send the complete desired state so+ // untouched fields (enabled, name, scope) aren't reset.+ const disabledNum = body.enabled !== undefined ? (body.enabled ? 0 : 1) : row.disabled;+ const newName = body.name !== undefined ? body.name : row.name;+ const ep: EndpointParams = { url: body.url ?? row.url, disabled: disabledNum === 1 };+ if (filterTypes) ep.filterTypes = filterTypes; // omit → all typesif (row.sandbox_id) ep.channels = [row.sandbox_id];- if (body.enabled !== undefined) ep.disabled = !body.enabled;- if (body.url !== undefined && !isHttpsURL(body.url)) return json({ error: "url must be a valid https URL" }, 400);+ if (newName) ep.description = newName;await sx.updateEndpoint(row.svix_app_id, row.svix_endpoint_id, ep);- const disabled = body.enabled === undefined ? row.disabled : body.enabled ? 0 : 1;await env.OPENCOMPUTER_DB.prepare(- "UPDATE webhook_destinations SET url = ?1, event_types = ?2, disabled = ?3, updated_at = ?4 WHERE id = ?5",+ "UPDATE webhook_destinations SET url = ?1, event_types = ?2, name = ?3, disabled = ?4, updated_at = ?5 WHERE id = ?6",)- .bind(ep.url, JSON.stringify(ep.filterTypes ?? []), disabled, nowSec(), id)+ .bind(ep.url, JSON.stringify(originalTypes), newName, disabledNum, nowSec(), id).run();const updated = await getDest(env, caller.orgID, id);@@ -318,13 +375,15 @@ async function deleteWebhook(env: WebhookEnv, sx: SvixClient, caller: Caller, idasync function testWebhook(env: WebhookEnv, sx: SvixClient, caller: Caller, id: string): Promise<Response> {const row = await getDest(env, caller.orgID, id);if (!row) return json({ error: "webhook not found" }, 404);- let eventType = "sandbox.created";+ // Pick a concrete type the endpoint will accept (expand any wildcard filter).+ let stored: string[] = [];try {- const types: string[] = JSON.parse(row.event_types || "[]");- if (types.length) eventType = types[0];+ stored = JSON.parse(row.event_types || "[]");} catch {- /* keep default */+ /* none */}+ const expanded = expandEventTypes(stored);+ const eventType = expanded && expanded.length ? expanded[0] : "sandbox.created";const msg = await sx.createMessage(row.svix_app_id, {eventType,payload: { type: eventType, sandboxId: row.sandbox_id ?? "sb-test", test: true },diff --git a/cloudflare-workers/events-ingest/src/index.ts b/cloudflare-workers/events-ingest/src/index.tsindex 1384a5e5..2716d90f 100644--- a/cloudflare-workers/events-ingest/src/index.ts+++ b/cloudflare-workers/events-ingest/src/index.ts@@ -499,7 +499,13 @@ export default {// transient Svix error → 503 so the CP forwarder retries the batch (the// event id is the Idempotency-Key, so re-sends dedupe); a permanent (4xx)// error for one event is logged and skipped. (rearchitecture §3.1)- if (env.SVIX_API_TOKEN) {+ if (!env.SVIX_API_TOKEN) {+ // Misconfiguration guard: api-edge can register webhooks while this Worker+ // silently drops deliveries if the token is only set on one side.+ if (fresh.some((e) => e.type.startsWith("sandbox."))) {+ console.warn("events-ingest: SVIX_API_TOKEN unset — sandbox webhook events are NOT being delivered to Svix");+ }+ } else {const webhookEvents = fresh.filter((e) => e.type.startsWith("sandbox.") && e.org_id && e.sandbox_id,);diff --git a/cloudflare-workers/shared/svix.ts b/cloudflare-workers/shared/svix.tsindex 55c23c7b..9c3b3cde 100644--- a/cloudflare-workers/shared/svix.ts+++ b/cloudflare-workers/shared/svix.ts@@ -113,6 +113,17 @@ export class SvixClient {return this.do("ensure_application", "POST", "/api/v1/app/?get_if_exists=true", { uid, name });}+ // EnsureEventType registers an event type (idempotent). Svix requires every+ // type referenced by an endpoint's filterTypes to exist first, else create 422s.+ async ensureEventType(name: string): Promise<void> {+ try {+ await this.do("ensure_event_type", "POST", "/api/v1/event-type/", { name, description: name });+ } catch (e) {+ if (e instanceof SvixError && e.status === 409) return; // already exists+ throw e;+ }+ }+async createEndpoint(appID: string, p: EndpointParams): Promise<Endpoint> {return this.do("create_endpoint", "POST", `/api/v1/app/${appID}/endpoint/`, p);}diff --git a/internal/api/sandbox.go b/internal/api/sandbox.goindex 0700418f..2692965a 100644--- a/internal/api/sandbox.go+++ b/internal/api/sandbox.go@@ -642,6 +642,15 @@ func (s *Server) createSandboxRemote(c echo.Context, ctx context.Context, cfg ty// session row, which must exist before the worker looks it up.sandboxID := "sb-" + uuid.New().String()[:8]+ // Register inline webhooks at the edge (Svix) BEFORE the session is created+ // (CreateSandboxSessionWithStatus records sandbox.created) and before the+ // worker boots, so the endpoint exists before `created` is relayed. Cleaned up+ // below if the gRPC create fails.+ var inlineWebhooks []types.SandboxWebhookResult+ if hasOrg && len(cfg.Webhooks) > 0 {+ inlineWebhooks = s.registerInlineWebhooksEdge(ctx, orgID, sandboxID, cfg.Webhooks)+ }+// Create session with "pending" status before dispatching to worker.if s.store != nil && hasOrg {template := cfg.Template@@ -670,15 +679,6 @@ func (s *Server) createSandboxRemote(c echo.Context, ctx context.Context, cfg ty}}- // Register inline webhooks BEFORE the worker boots, pinned to the- // pre-generated sandbox id — so they reliably catch sandbox.created (the- // worker can't emit it before the destination exists). Cleaned up below if- // the gRPC create fails.- var inlineWebhooks []types.SandboxWebhookResult- if hasOrg && len(cfg.Webhooks) > 0 {- inlineWebhooks = s.registerInlineWebhooksEdge(ctx, orgID, sandboxID, cfg.Webhooks)- }-grpcResp, err := grpcClient.CreateSandbox(grpcCtx, &pb.CreateSandboxRequest{SandboxId: sandboxID,Template: cfg.Template,