Skip to content

An open-source, faster Devin alternative

TypeScript

feat(webhooks): inline-on-create via CP -> edge /internal/webhooks/register (#40)

Igor Zalutskiedc65367bbf1
diff --git a/internal/api/sandbox.go b/internal/api/sandbox.go
index d56cea0b..2f2a88b0 100644
--- a/internal/api/sandbox.go
+++ b/internal/api/sandbox.go
@@ -257,13 +257,14 @@ func (s *Server) createSandbox(c echo.Context) error {
if template == "" {
template = "default"
}
- _, _ = s.store.CreateSandboxSession(ctx, sb.ID, orgID, auth.GetUserID(c), template, region, workerID, cfgJSON, metadataJSON, secretStoreID)
-
- // Inline webhooks: register them pinned to this sandbox so they catch
- // created/ready. Best-effort (see registerInlineWebhooks).
+ // Inline webhooks: register the Svix endpoints (via the edge) pinned to
+ // this sandbox BEFORE CreateSandboxSession records sandbox.created, so the
+ // endpoint exists before that event is relayed. Best-effort + nil-safe.
if len(cfg.Webhooks) > 0 {
- sb.Webhooks = s.registerInlineWebhooks(ctx, orgID, sb.ID, cfg.Webhooks)
+ sb.Webhooks = s.registerInlineWebhooksEdge(ctx, orgID, sb.ID, cfg.Webhooks)
}
+ _, _ = s.store.CreateSandboxSession(ctx, sb.ID, orgID, auth.GetUserID(c), template, region, workerID, cfgJSON, metadataJSON, secretStoreID)
+
// Combined-mode create writes the session directly as 'running' (no
// pending→running promotion), so emit sandbox.ready here; the remote
// path emits it in-tx on the promotion. Deterministic id → at most one.
@@ -674,8 +675,8 @@ func (s *Server) createSandboxRemote(c echo.Context, ctx context.Context, cfg ty
// worker can't emit it before the destination exists). Cleaned up below if
// the gRPC create fails.
var inlineWebhooks []types.SandboxWebhookResult
- if s.store != nil && hasOrg && len(cfg.Webhooks) > 0 {
- inlineWebhooks = s.registerInlineWebhooks(ctx, orgID, sandboxID, cfg.Webhooks)
+ if hasOrg && len(cfg.Webhooks) > 0 {
+ inlineWebhooks = s.registerInlineWebhooksEdge(ctx, orgID, sandboxID, cfg.Webhooks)
}
grpcResp, err := grpcClient.CreateSandbox(grpcCtx, &pb.CreateSandboxRequest{
diff --git a/internal/api/webhooks.go b/internal/api/webhooks.go
index 4eedc74a..0b1ad980 100644
--- a/internal/api/webhooks.go
+++ b/internal/api/webhooks.go
@@ -155,6 +155,22 @@ func (s *Server) recordLifecycle(ctx context.Context, orgID uuid.UUID, sandboxID
fmt.Sprintf("%s:%s:%d", sandboxID, evType, time.Now().UnixNano()), data)
}
+// registerInlineWebhooksEdge registers inline webhooks via the edge (Svix) at
+// create time — the all-Svix-at-edge replacement for registerInlineWebhooks.
+// Nil-safe + best-effort: returns nil (logging) if there's no edge client or the
+// edge call fails, so a webhook hiccup never fails the sandbox create.
+func (s *Server) registerInlineWebhooksEdge(ctx context.Context, orgID uuid.UUID, sandboxID string, specs []types.SandboxWebhookSpec) []types.SandboxWebhookResult {
+ if s.edge == nil || len(specs) == 0 {
+ return nil
+ }
+ res, err := s.edge.RegisterInlineWebhooks(ctx, orgID, sandboxID, specs)
+ if err != nil {
+ log.Printf("sandbox %s: inline webhook edge register failed: %v", sandboxID, err)
+ return nil
+ }
+ return res
+}
+
// registerInlineWebhooks registers webhooks supplied inline on sandbox create,
// each pinned to the sandbox with no watermark floor (created_after_event_seq=0
// → full lifecycle from `created`). Best-effort: a bad URL or register error is
diff --git a/internal/edgeclient/client.go b/internal/edgeclient/client.go
index 9f0abb96..88c6be01 100644
--- a/internal/edgeclient/client.go
+++ b/internal/edgeclient/client.go
@@ -31,6 +31,7 @@ import (
"github.com/opensandbox/opensandbox/internal/crypto"
"github.com/opensandbox/opensandbox/internal/db"
+ "github.com/opensandbox/opensandbox/pkg/types"
)
// ErrNotFound — returned for any 404 from the edge so callers can match it
@@ -392,6 +393,46 @@ func (c *Client) LookupSecretStoreByID(ctx context.Context, id uuid.UUID, enc *c
return c.lookupStoreByQuery(ctx, "/internal/secret-stores/"+escapeQuery(id.String()), enc)
}
+// ── Webhooks (inline-on-create) ──────────────────────────────────────────
+
+// RegisterInlineWebhooks registers inline sandbox webhooks at the edge (Svix
+// endpoints + the D1 index) at sandbox-create time — before the sandbox emits
+// `created`. The CP never talks to Svix; it forwards the specs over the
+// HMAC-auth'd /internal/webhooks/register endpoint. The edge skips invalid specs
+// best-effort and returns whatever it created (secret present once iff generated).
+func (c *Client) RegisterInlineWebhooks(ctx context.Context, orgID uuid.UUID, sandboxID string, specs []types.SandboxWebhookSpec) ([]types.SandboxWebhookResult, error) {
+ wh := make([]map[string]any, 0, len(specs))
+ for _, s := range specs {
+ m := map[string]any{"url": s.URL}
+ if len(s.EventTypes) > 0 {
+ m["eventTypes"] = s.EventTypes
+ }
+ if s.Secret != "" {
+ m["secret"] = s.Secret
+ }
+ wh = append(wh, m)
+ }
+ body, _ := json.Marshal(map[string]any{
+ "orgId": orgID.String(),
+ "sandboxId": sandboxID,
+ "webhooks": wh,
+ })
+ respBody, status, err := c.do(ctx, "POST", "/internal/webhooks/register", body)
+ if err != nil {
+ return nil, err
+ }
+ if status != 200 {
+ return nil, fmt.Errorf("edge status %d: %s", status, string(respBody))
+ }
+ var resp struct {
+ Webhooks []types.SandboxWebhookResult `json:"webhooks"`
+ }
+ if err := json.Unmarshal(respBody, &resp); err != nil {
+ return nil, fmt.Errorf("decode webhooks: %w", err)
+ }
+ return resp.Webhooks, nil
+}
+
// ── helpers ────────────────────────────────────────────────────────────
// escapeQuery is a minimal URL-querystring encoder for values we control