From ab38bc7ac6df7d978ca1409a03218d5e8b0125e2 Mon Sep 17 00:00:00 2001 From: Brendan Date: Wed, 16 Sep 2026 19:20:00 +0000 Subject: [PATCH] GEN-2004 route webhooks through job invocation --- packages/app/src/flow-schemas.ts | 11 +- packages/app/src/flow-service.ts | 16 ++- packages/app/src/index.ts | 4 + packages/app/src/job-domain.ts | 6 + packages/app/src/protected-api.ts | 3 + packages/app/src/tenant.ts | 161 ++++++++++++++++------ packages/app/src/webhook-trigger.ts | 41 ++++++ packages/app/test/webhook-trigger.test.ts | 20 +++ 8 files changed, 214 insertions(+), 48 deletions(-) create mode 100644 packages/app/src/webhook-trigger.ts create mode 100644 packages/app/test/webhook-trigger.test.ts diff --git a/packages/app/src/flow-schemas.ts b/packages/app/src/flow-schemas.ts index 5129a1e0..d64beab7 100644 --- a/packages/app/src/flow-schemas.ts +++ b/packages/app/src/flow-schemas.ts @@ -31,13 +31,22 @@ export const listEventsSchema = z.object({ }); const stringRecordSchema = z.record(z.string(), z.string()); +const webhookCommonSchema = z.object({ + context: z.string().max(50_000).optional(), parameters: stringRecordSchema.default({}), +}); +export const webhookTriggerConfigSchema = z.discriminatedUnion("provider", [ + webhookCommonSchema.extend({ provider: z.literal("linear"), projectId: z.string().min(1), matchRules: z.array(matchRuleSchema).min(1) }).strict(), + webhookCommonSchema.extend({ provider: z.literal("github"), installationId: z.number().int().positive(), repositoryId: z.number().int().positive(), event: z.string().min(1).default("pull_request"), action: z.string().min(1).default("dequeued") }).strict(), + webhookCommonSchema.extend({ provider: z.literal("cloudflareTail"), signingSecret: z.string().min(16), handlerCode: z.string().max(16_384).optional() }).strict(), + webhookCommonSchema.extend({ provider: z.literal("custom"), secret: z.string().min(16).optional() }).strict(), +]); export const triggerSchema = z.discriminatedUnion("kind", [ z.object({ kind: z.literal("manual"), config: z.object({}).strict().default({}) }).strict(), z.object({ kind: z.literal("schedule"), config: z.object({ cron: z.string().trim().min(1), timezone: z.string().trim().min(1), context: z.string().max(50_000).optional(), parameters: stringRecordSchema.default({}), }).strict() }).strict(), - z.object({ kind: z.literal("webhook"), config: z.record(z.string(), z.unknown()) }).strict(), + z.object({ kind: z.literal("webhook"), config: webhookTriggerConfigSchema }).strict(), ]); export const jobInputSchema = z.object({ name: z.string().trim().min(1).max(120), diff --git a/packages/app/src/flow-service.ts b/packages/app/src/flow-service.ts index ee9d77e9..f7df7a84 100644 --- a/packages/app/src/flow-service.ts +++ b/packages/app/src/flow-service.ts @@ -49,6 +49,13 @@ export class FlowService { }; } + private publicJob(value: any) { + const provider = value?.trigger?.kind === "webhook" ? value.trigger.config?.provider : undefined; + if (provider === "cloudflareTail") value.trigger.config.destination = `${this.env.APP_ORIGIN}/webhooks/cloudflare/${encodeURIComponent(this.auth.tenantId)}/${encodeURIComponent(value.id)}`; + if (provider === "custom") value.trigger.config.destination = `${this.env.APP_ORIGIN}/webhooks/custom/${encodeURIComponent(this.auth.tenantId)}/${encodeURIComponent(value.id)}`; + return value; + } + async listFlows() { return (await this.call("flows:read", "/pipes") as any[]).map(row => this.publicFlow(row)); } async getFlow(flowId: string) { const value = await this.call("flows:read", `/v1/flows/${encodeURIComponent(flowId)}`) as any; @@ -76,10 +83,11 @@ export class FlowService { getRun(runId: string) { return this.call("runs:read", `/v1/runs/${encodeURIComponent(runId)}`); } listFlowEvents(query: URLSearchParams) { return this.call("runs:read", `/v1/events?${query}`); } stopRun(runId: string) { return this.call("runs:write", `/v1/runs/${encodeURIComponent(runId)}/stop`, { method: "POST" }); } - listJobs() { return this.call("flows:read", "/v1/jobs"); } - getJob(jobId: string) { return this.call("flows:read", `/v1/jobs/${encodeURIComponent(jobId)}`); } - createJob(input: JobInput) { return this.call("flows:write", "/v1/jobs", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(input) }); } - updateJob(jobId: string, input: JobInput) { return this.call("flows:write", `/v1/jobs/${encodeURIComponent(jobId)}`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(input) }); } + async listJobs() { return (await this.call("flows:read", "/v1/jobs") as any[]).map(value => this.publicJob(value)); } + async getJob(jobId: string) { return this.publicJob(await this.call("flows:read", `/v1/jobs/${encodeURIComponent(jobId)}`)); } + listJobEvents(jobId: string, limit = 50) { return this.call("runs:read", `/v1/jobs/${encodeURIComponent(jobId)}/events?limit=${limit}`); } + async createJob(input: JobInput) { return this.publicJob(await this.call("flows:write", "/v1/jobs", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(input) })); } + async updateJob(jobId: string, input: JobInput) { return this.publicJob(await this.call("flows:write", `/v1/jobs/${encodeURIComponent(jobId)}`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(input) })); } deleteJob(jobId: string) { return this.call("flows:write", `/v1/jobs/${encodeURIComponent(jobId)}`, { method: "DELETE" }); } setJobEnabled(jobId: string, enabled: boolean) { return this.call("flows:write", `/v1/jobs/${encodeURIComponent(jobId)}/${enabled ? "enable" : "disable"}`, { method: "POST" }); } invokeJob(jobId: string, input: ManualInvocationInput) { return this.call("runs:write", `/v1/jobs/${encodeURIComponent(jobId)}/invocations`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(input) }); } diff --git a/packages/app/src/index.ts b/packages/app/src/index.ts index d1079c90..9a895d80 100644 --- a/packages/app/src/index.ts +++ b/packages/app/src/index.ts @@ -262,6 +262,10 @@ app.post("/webhooks/cloudflare/:tenantId/:flowId", async (c) => { }); }); +app.post("/webhooks/custom/:tenantId/:jobId", async (c) => { + return tenant(c, c.req.param("tenantId")).fetch(`https://tenant/webhook/custom/${encodeURIComponent(c.req.param("jobId"))}`, { method: "POST", headers: c.req.raw.headers, body: await c.req.text() }); +}); + app.post("/webhooks/linear", async (c) => { if (!c.env.LINEAR_WEBHOOK_SIGNING_SECRET) return c.text("Webhook verification is not configured", 503); const raw = await c.req.text(); diff --git a/packages/app/src/job-domain.ts b/packages/app/src/job-domain.ts index 0130bfd6..a567ca25 100644 --- a/packages/app/src/job-domain.ts +++ b/packages/app/src/job-domain.ts @@ -167,6 +167,12 @@ export const JOB_SCHEMA = ` ); CREATE INDEX IF NOT EXISTS job_runs_queue ON job_runs(state, created_at); CREATE INDEX IF NOT EXISTS job_runs_active ON job_runs(job_id, state); + CREATE TABLE IF NOT EXISTS job_events ( + id TEXT PRIMARY KEY, job_id TEXT NOT NULL REFERENCES jobs(id) ON DELETE CASCADE, + provider TEXT NOT NULL, delivery_id TEXT NOT NULL, outcome TEXT NOT NULL, + detail TEXT NOT NULL, received_at TEXT NOT NULL + ); + CREATE INDEX IF NOT EXISTS job_events_by_job ON job_events(job_id, received_at DESC); CREATE TABLE IF NOT EXISTS schedule_state ( trigger_id TEXT PRIMARY KEY REFERENCES triggers(id) ON DELETE CASCADE, job_id TEXT NOT NULL UNIQUE REFERENCES jobs(id) ON DELETE CASCADE, diff --git a/packages/app/src/protected-api.ts b/packages/app/src/protected-api.ts index a6a25d06..1ebc1de8 100644 --- a/packages/app/src/protected-api.ts +++ b/packages/app/src/protected-api.ts @@ -53,6 +53,8 @@ export class ProtectedApiHandler extends WorkerEntrypoint { if (request.method === "POST" && path === "/api/v1/jobs") return Response.json(await service.createJob(jobInputSchema.parse(await request.json())), { status: 201 }); const invocationMatch = path.match(/^\/api\/v1\/jobs\/([^/]+)\/invocations$/); if (invocationMatch && request.method === "POST") return Response.json(await service.invokeJob(decodeURIComponent(invocationMatch[1]), manualInvocationSchema.parse(await request.json())), { status: 202 }); + const jobEventsMatch = path.match(/^\/api\/v1\/jobs\/([^/]+)\/events$/); + if (jobEventsMatch && request.method === "GET") return Response.json(await service.listJobEvents(decodeURIComponent(jobEventsMatch[1]), z.coerce.number().int().min(1).max(100).default(50).parse(url.searchParams.get("limit") ?? 50))); const enabledMatch = path.match(/^\/api\/v1\/jobs\/([^/]+)\/(enable|disable)$/); if (enabledMatch && request.method === "POST") return Response.json(await service.setJobEnabled(decodeURIComponent(enabledMatch[1]), enabledMatch[2] === "enable")); const jobMatch = path.match(/^\/api\/v1\/jobs\/([^/]+)$/); @@ -95,6 +97,7 @@ export class ProtectedApiHandler extends WorkerEntrypoint { tool("enable_job", "Enable a job", jobIdSchema, ({ jobId }: any) => service.setJobEnabled(jobId, true)); tool("disable_job", "Disable a job", jobIdSchema, ({ jobId }: any) => service.setJobEnabled(jobId, false)); tool("invoke_job", "Manually invoke any enabled job, overriding string parameters and optional reserved context", manualInvocationSchema.extend({ jobId: z.string().min(1) }), ({ jobId, ...input }: any) => service.invokeJob(jobId, input)); + tool("list_job_webhook_activity", "List matching, rejected, duplicate, and accepted webhook activity for a job", z.object({ jobId: z.string().min(1), limit: z.number().int().min(1).max(100).default(50) }), ({ jobId, limit }: any) => service.listJobEvents(jobId, limit)); tool("list_execution_targets", "List non-secret execution target metadata and capabilities", z.object({}), () => service.listExecutionTargets()); const transport = new WebStandardStreamableHTTPServerTransport({ sessionIdGenerator: undefined }); await server.connect(transport); diff --git a/packages/app/src/tenant.ts b/packages/app/src/tenant.ts index 38249526..75934faf 100644 --- a/packages/app/src/tenant.ts +++ b/packages/app/src/tenant.ts @@ -17,6 +17,7 @@ import { renderSourcePrompt } from "./source-lifecycle"; import { JOB_SCHEMA, renderJobPrompt } from "./job-domain"; import { normalizeExecutionState, type RunHandle } from "./execution"; import { catchUpOccurrence, nextOccurrence, validateScheduleConfig, type ScheduleConfig } from "./schedule"; +import { adaptWebhook, publicWebhookConfig, type WebhookProvider, type WebhookTriggerConfig } from "./webhook-trigger"; export { DEFAULT_CONTEXT_TEMPLATE, linearTicketPrompt, renderContextTemplate } from "./linear-source"; @@ -115,6 +116,8 @@ export class Tenant extends DurableObject { if (request.method === "GET" && jobMatch) return await this.getJobV1(decodeURIComponent(jobMatch[1])); if (request.method === "PUT" && jobMatch) return await this.updateJob(decodeURIComponent(jobMatch[1]), await request.json()); if (request.method === "DELETE" && jobMatch) return this.deleteJob(decodeURIComponent(jobMatch[1])); + const jobEventsMatch = url.pathname.match(/^\/v1\/jobs\/([^/]+)\/events$/); + if (request.method === "GET" && jobEventsMatch) return this.listJobEvents(decodeURIComponent(jobEventsMatch[1]), url); if (request.method === "GET" && /^\/v1\/flows\/[^/]+$/.test(url.pathname)) return this.getFlow(decodeURIComponent(url.pathname.split("/")[3] ?? "")); if (request.method === "GET" && url.pathname === "/connections/status") return await this.connectionStatus(); if (request.method === "POST" && url.pathname === "/members") return await this.upsertMember(await request.json()); @@ -130,6 +133,8 @@ export class Tenant extends DurableObject { if (request.method === "DELETE" && url.pathname.startsWith("/pipes/")) return this.deletePipe(url.pathname.split("/")[2] ?? ""); if (request.method === "POST" && url.pathname === "/webhook/linear") return await this.acceptLinearWebhook(await request.json() as Record, request.headers.get("linear-delivery")); if (request.method === "POST" && url.pathname === "/webhook/github") return await this.acceptGitHubWebhook(await request.json() as Record, request.headers.get("github-delivery"), request.headers.get("github-event")); + const customJobMatch = url.pathname.match(/^\/webhook\/custom\/([^/]+)$/); + if (request.method === "POST" && customJobMatch) return await this.acceptCustomJobWebhook(decodeURIComponent(customJobMatch[1]), await request.text(), request.headers); const tailMatch = url.pathname.match(/^\/webhook\/cloudflare\/([^/]+)$/); if (request.method === "POST" && tailMatch) return await this.acceptCloudflareTail(decodeURIComponent(tailMatch[1]), await request.text(), request.headers); const tailTestMatch = url.pathname.match(/^\/cloudflare-tail\/([^/]+)\/test$/); @@ -295,12 +300,17 @@ export class Tenant extends DurableObject { try { parameterDefaults = JSON.parse(String(row.parameter_defaults)); } catch { /* malformed internal state is presented safely */ } try { executionTarget = JSON.parse(String(row.execution_target)); } catch { /* malformed internal state is presented safely */ } try { triggerConfig = JSON.parse(String(trigger?.config ?? "{}")); } catch { /* malformed internal state is presented safely */ } + if (trigger?.kind === "webhook") { + const rawConfig = triggerConfig as WebhookTriggerConfig; + triggerConfig = publicWebhookConfig(rawConfig); + } + const activity = this.one("SELECT count(*) AS count,max(received_at) AS last_received_at FROM job_events WHERE job_id=?", row.id) as Row | undefined; return { id: row.id, name: row.name, promptTemplate: await decrypt(String(row.encrypted_prompt_template), this.env.CREDENTIAL_ENCRYPTION_KEY), parameterDefaults, concurrencyLimit: Number(row.concurrency_limit), executionTargetId: String(executionTarget.connectionId ?? ""), - trigger: { kind: String(trigger?.kind ?? "manual"), config: triggerConfig, + trigger: { kind: String(trigger?.kind ?? "manual"), config: triggerConfig, activity: { count: Number(activity?.count ?? 0), lastReceivedAt: activity?.last_received_at ?? null }, ...(trigger?.kind === "schedule" ? (() => { const state = this.one("SELECT next_run_at,last_triggered_at FROM schedule_state WHERE job_id=?", row.id) as Row | undefined; return { nextRunAt: state?.next_run_at == null ? null : new Date(Number(state.next_run_at)).toISOString(), lastTriggeredAt: state?.last_triggered_at ?? null }; })() : {}) }, enabled: Boolean(row.enabled), createdAt: row.created_at, updatedAt: row.updated_at, }; @@ -372,28 +382,37 @@ export class Tenant extends DurableObject { } private async invokeJob(jobId: string, input: any): Promise { + const job = this.one("SELECT enabled FROM jobs WHERE id=?", jobId) as Row | undefined; + if (!job) return new Response("Not found", { status: 404 }); + if (!Boolean(job.enabled)) return Response.json({ error: "Job is disabled" }, { status: 409 }); + const result = await this.invokeCanonicalJob(jobId, "manual", input.idempotencyKey ? `manual:${input.idempotencyKey}` : `manual:${id()}`, input.context, object(input.parameters)); + if (!result) return Response.json({ error: "Job is disabled" }, { status: 409 }); + return json(result); + } + + /** Single persistence boundary used by manual, schedule, and webhook adapters. */ + private async invokeCanonicalJob(jobId: string, source: "manual" | "schedule" | "webhook", claimKey: string, context?: string, parameters: Record = {}, occurrence?: Record): Promise | null> { const row = this.one("SELECT * FROM jobs WHERE id = ?", jobId) as Row | undefined; - if (!row) return new Response("Not found", { status: 404 }); - if (!Boolean(row.enabled)) return Response.json({ error: "Job is disabled" }, { status: 409 }); - const claimKey = input.idempotencyKey ? `manual:${input.idempotencyKey}` : `manual:${id()}`; + if (!row || !Boolean(row.enabled)) return null; const existing = this.one("SELECT i.id AS invocation_id,r.id AS run_id,r.state FROM invocations i JOIN job_runs r ON r.invocation_id=i.id WHERE i.job_id=? AND i.claim_key=?", jobId, claimKey) as Row | undefined; - if (existing) return json({ invocationId: existing.invocation_id, runId: existing.run_id, state: existing.state, duplicate: true }); - const job = await this.publicJob(row), parameters = object(input.parameters); - const prompt = renderJobPrompt({ promptTemplate: String(job.promptTemplate), parameterDefaults: job.parameterDefaults as Record }, { context: input.context, parameters }); + if (existing) return { invocationId: existing.invocation_id, runId: existing.run_id, state: existing.state, duplicate: true }; + const job = await this.publicJob(row); + const prompt = renderJobPrompt({ promptTemplate: String(job.promptTemplate), parameterDefaults: job.parameterDefaults as Record }, { context, parameters }); const invocationId = id(), runId = id(), timestamp = now(); - this.ctx.storage.sql.exec("INSERT OR IGNORE INTO invocations (id,job_id,source,claim_key,context,parameters,created_at) VALUES (?,?,?,?,?,?,?)", invocationId, jobId, "manual", claimKey, input.context ?? null, JSON.stringify(parameters), timestamp); + this.ctx.storage.sql.exec("INSERT OR IGNORE INTO invocations (id,job_id,source,claim_key,context,parameters,occurrence,created_at) VALUES (?,?,?,?,?,?,?,?)", invocationId, jobId, source, claimKey, context ?? null, JSON.stringify(parameters), occurrence ? JSON.stringify(occurrence) : null, timestamp); const winner = this.one("SELECT id FROM invocations WHERE job_id=? AND claim_key=?", jobId, claimKey) as Row; if (winner.id !== invocationId) { const duplicate = this.one("SELECT id,state FROM job_runs WHERE invocation_id=?", winner.id) as Row; - return json({ invocationId: winner.id, runId: duplicate.id, state: duplicate.state, duplicate: true }); + return { invocationId: winner.id, runId: duplicate.id, state: duplicate.state, duplicate: true }; } const encryptedPrompt = await encrypt(prompt, this.env.CREDENTIAL_ENCRYPTION_KEY); this.ctx.storage.sql.exec("INSERT INTO job_runs (id,job_id,invocation_id,state,encrypted_prompt,created_at,updated_at) VALUES (?,?,?,?,?,?,?)", runId, jobId, invocationId, "queued", encryptedPrompt, timestamp, timestamp); let target: Record = {}; try { target = JSON.parse(String(row.execution_target)); } catch { /* validated when the job is saved */ } - this.ctx.storage.sql.exec("INSERT INTO runs (id,pipe_id,issue_id,issue_title,issue_url,claim_key,agent_name,workspace_name,agent_kind,state,prompt,prompt_delivery_state,provider,execution_backend_kind,execution_capabilities,destination_url,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", runId, jobId, invocationId, String(row.name), null, claimKey, `factorize-${runId}`, String(target.workspace ?? ""), String(target.agentKind ?? ""), "queued", encryptedPrompt, "pending", "manual", "exe-herdr", JSON.stringify(["output", "prompt-delivery", "recovery", "stop"]), "https://exe.dev/", timestamp, timestamp); + const provider = source === "webhook" ? String((occurrence as any)?.metadata?.provider ?? "webhook") : source; + this.ctx.storage.sql.exec("INSERT INTO runs (id,pipe_id,issue_id,issue_title,issue_url,claim_key,agent_name,workspace_name,agent_kind,state,prompt,prompt_delivery_state,provider,execution_backend_kind,execution_capabilities,destination_url,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", runId, jobId, invocationId, String(row.name), null, claimKey, `factorize-${runId}`, String(target.workspace ?? ""), String(target.agentKind ?? ""), "queued", encryptedPrompt, "pending", provider, "exe-herdr", JSON.stringify(["output", "prompt-delivery", "recovery", "stop"]), "https://exe.dev/", timestamp, timestamp); await this.ctx.storage.setAlarm(Date.now()); - return json({ invocationId, runId, state: "queued", duplicate: false }); + return { invocationId, runId, state: "queued", duplicate: false }; } private async processDueSchedules(): Promise { @@ -410,20 +429,7 @@ export class Tenant extends DurableObject { } private async enqueueScheduledJob(jobId: string, occurredAt: string, config: ScheduleConfig): Promise { - const row = this.one("SELECT * FROM jobs WHERE id=? AND enabled=1", jobId) as Row | undefined; - if (!row) return; - const claimKey = `schedule:${occurredAt}`; - if (this.one("SELECT id FROM invocations WHERE job_id=? AND claim_key=?", jobId, claimKey)) return; - const job = await this.publicJob(row), parameters = config.parameters ?? {}; - const prompt = renderJobPrompt({ promptTemplate: String(job.promptTemplate), parameterDefaults: job.parameterDefaults as Record }, { context: config.context, parameters }); - const invocationId = id(), runId = id(), timestamp = now(), occurrence = JSON.stringify({ occurredAt }); - this.ctx.storage.sql.exec("INSERT OR IGNORE INTO invocations (id,job_id,source,claim_key,context,parameters,occurrence,created_at) VALUES (?,?,?,?,?,?,?,?)", invocationId, jobId, "schedule", claimKey, config.context ?? null, JSON.stringify(parameters), occurrence, timestamp); - const winner = this.one("SELECT id FROM invocations WHERE job_id=? AND claim_key=?", jobId, claimKey) as Row; - if (winner.id !== invocationId) return; - const encryptedPrompt = await encrypt(prompt, this.env.CREDENTIAL_ENCRYPTION_KEY); - this.ctx.storage.sql.exec("INSERT INTO job_runs (id,job_id,invocation_id,state,encrypted_prompt,created_at,updated_at) VALUES (?,?,?,?,?,?,?)", runId, jobId, invocationId, "queued", encryptedPrompt, timestamp, timestamp); - let target: Record = {}; try { target = JSON.parse(String(row.execution_target)); } catch {} - this.ctx.storage.sql.exec("INSERT INTO runs (id,pipe_id,issue_id,issue_title,issue_url,claim_key,agent_name,workspace_name,agent_kind,state,prompt,prompt_delivery_state,provider,execution_backend_kind,execution_capabilities,destination_url,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", runId, jobId, invocationId, String(row.name), null, claimKey, `factorize-${runId}`, String(target.workspace ?? ""), String(target.agentKind ?? ""), "queued", encryptedPrompt, "pending", "schedule", "exe-herdr", JSON.stringify(["output", "prompt-delivery", "recovery", "stop"]), "https://exe.dev/", timestamp, timestamp); + await this.invokeCanonicalJob(jobId, "schedule", `schedule:${occurredAt}`, config.context, config.parameters ?? {}, { occurredAt }); } private deletePipe(pipeId: string): Response { @@ -606,6 +612,30 @@ export class Tenant extends DurableObject { return json({ statuses: statusData.workflowStates.nodes, users: userData.users.nodes, labels: labelData.issueLabels.nodes }); } + private webhookJobs(provider: WebhookProvider): Array<{ job: Row; config: WebhookTriggerConfig }> { + return this.rows("SELECT j.*,t.config AS trigger_config FROM jobs j JOIN triggers t ON t.job_id=j.id WHERE j.enabled=1 AND t.kind='webhook'") + .flatMap(job => { try { const config = JSON.parse(String(job.trigger_config)) as WebhookTriggerConfig; return config.provider === provider ? [{ job, config }] : []; } catch { return []; } }); + } + + private recordJobEvent(jobId: string, provider: string, deliveryId: string, outcome: string, detail: string): void { + this.ctx.storage.sql.exec("INSERT INTO job_events VALUES (?,?,?,?,?,?)", id(), jobId, provider, deliveryId, outcome, detail, now()); + } + + private async invokeWebhookJobs(provider: WebhookProvider, deliveryId: string, payload: Record, eventName?: string): Promise { + for (const { job, config } of this.webhookJobs(provider)) { + const invocation = adaptWebhook(config, provider, deliveryId, payload, eventName); + if (!invocation) { this.recordJobEvent(String(job.id), provider, deliveryId, "ignored", "Webhook did not match this job trigger."); continue; } + const result = await this.invokeCanonicalJob(String(job.id), "webhook", invocation.claimKey, invocation.context, invocation.parameters, invocation.occurrence); + this.recordJobEvent(String(job.id), provider, deliveryId, result?.duplicate ? "duplicate" : result ? "accepted" : "ignored", result?.duplicate ? "Webhook occurrence was already claimed." : result ? "Webhook occurrence queued through canonical job invocation." : "Job was unavailable."); + } + } + + private listJobEvents(jobId: string, url: URL): Response { + if (!this.one("SELECT id FROM jobs WHERE id=?", jobId)) return new Response("Not found", { status: 404 }); + const limit = Math.max(1, Math.min(100, Number(url.searchParams.get("limit") ?? 50))); + return json(this.rows("SELECT id,provider,delivery_id AS deliveryId,outcome,detail,received_at AS receivedAt FROM job_events WHERE job_id=? ORDER BY received_at DESC,id DESC LIMIT ?", jobId, limit)); + } + private async acceptLinearWebhook(event: Record, deliveryId: string | null): Promise { if (!deliveryId) return new Response("Missing delivery ID", { status: 400 }); if (this.one("SELECT id FROM deliveries WHERE id = ?", deliveryId)) return new Response(null, { status: 200 }); @@ -629,6 +659,7 @@ export class Tenant extends DurableObject { matchingEvent = { ...matchingEvent, data }; } } + await this.invokeWebhookJobs("linear", deliveryId, matchingEvent); for (const pipe of pipes) { if (projectId !== pipe.project_id) continue; @@ -651,9 +682,16 @@ export class Tenant extends DurableObject { this.ctx.storage.sql.exec("INSERT INTO deliveries (id,pipe_id,received_at) VALUES (?,?,?)", deliveryId, "github-app", now()); const custom = this.rows("SELECT * FROM pipes WHERE enabled=1 AND source_kind='custom'"); await Promise.all(custom.map((pipe) => this.evaluateCustom(pipe, "github", deliveryId, event))); + if (eventName !== "pull_request" || event.action !== "dequeued") await this.invokeWebhookJobs("github", deliveryId, event, eventName); if (eventName !== "pull_request" || event.action !== "dequeued") { await this.ctx.storage.setAlarm(Date.now()); return new Response(null, { status: 202 }); } const installationId = event.installation?.id, repositoryId = event.repository?.id, pull = event.pull_request; if (!Number.isSafeInteger(installationId) || !Number.isSafeInteger(repositoryId) || !Number.isSafeInteger(pull?.number)) return new Response(null, { status: 202 }); + for (const { job, config } of this.webhookJobs("github")) { + if (config.installationId !== installationId || config.repositoryId !== repositoryId || (config.event ?? "pull_request") !== eventName || (config.action ?? "dequeued") !== event.action || pull.state !== "open" || pull.base?.ref !== "main") continue; + const minimal = { installation: { id: installationId }, repository: { id: repositoryId }, pull_request: { number: pull.number, url: pull.html_url, title: pull.title ?? "", body: pull.body ?? "", base: { ref: pull.base.ref } }, action: event.action }; + this.ctx.storage.sql.exec("INSERT INTO pending_verifications VALUES (?,?,?,?,?,?,?,?,?,?,?)", id(), job.id, deliveryId, installationId, repositoryId, event.repository.owner?.login ?? "", event.repository.name ?? "", pull.number, 0, Date.now(), JSON.stringify(minimal)); + this.recordJobEvent(String(job.id), "github", deliveryId, "candidate", "Candidate received; waiting for GitHub mergeability verification."); + } const pipes = this.rows("SELECT * FROM pipes WHERE enabled=1 AND source_kind='github'"); for (const pipe of pipes) { const source = this.sourceFor(pipe); @@ -668,7 +706,18 @@ export class Tenant extends DurableObject { private async acceptCloudflareTail(flowId: string, raw: string, headers: Headers): Promise { const pipe = this.one("SELECT * FROM pipes WHERE id=? AND enabled=1", flowId) as Row | undefined; - if (!pipe) return new Response("Not found", { status: 404 }); + if (!pipe) { + const item = this.webhookJobs("cloudflareTail").find(({ job }) => job.id === flowId); + if (!item?.config.signingSecret) return new Response("Not found", { status: 404 }); + const timestamp = headers.get("x-factorize-timestamp") ?? "", delivery = headers.get("x-factorize-delivery") ?? "", signature = headers.get("x-factorize-signature") ?? ""; + if (!timestamp || !delivery || !signature) return new Response("Missing delivery metadata", { status: 400 }); + const verification = await verifyTailDelivery(item.config.signingSecret, timestamp, delivery, raw, signature); + if (verification !== "valid") { this.recordJobEvent(flowId, "cloudflareTail", delivery, verification, "Tail delivery verification failed."); return new Response(verification === "stale" ? "Stale delivery" : "Invalid signature", { status: 401 }); } + let payload: Record; try { payload = sanitizeTailEvent(JSON.parse(raw)) as Record; } catch { return new Response("Invalid JSON", { status: 400 }); } + if (suppressTailEvent(payload)) { this.recordJobEvent(flowId, "cloudflareTail", delivery, "rejected", "Tail delivery was suppressed to prevent an ingestion loop."); return new Response(null, { status: 202 }); } + await this.invokeWebhookJobs("cloudflareTail", delivery, payload, "tail"); + return new Response(null, { status: 202 }); + } const source = this.sourceFor(pipe); if (source.kind !== "custom" || source.origin !== "cloudflare" || !source.tail?.signingSecret) return new Response("Not found", { status: 404 }); const timestamp = headers.get("x-factorize-timestamp") ?? "", delivery = headers.get("x-factorize-delivery") ?? "", signature = headers.get("x-factorize-signature") ?? ""; @@ -691,6 +740,17 @@ export class Tenant extends DurableObject { return new Response(null, { status: 202 }); } + private async acceptCustomJobWebhook(jobId: string, raw: string, headers: Headers): Promise { + const item = this.webhookJobs("custom").find(({ job }) => job.id === jobId); + if (!item) return new Response("Not found", { status: 404 }); + if (item.config.secret && headers.get("x-factorize-secret") !== item.config.secret) { this.recordJobEvent(jobId, "custom", "unknown", "invalid", "Custom webhook secret was invalid."); return new Response("Invalid secret", { status: 401 }); } + let payload: Record; try { payload = JSON.parse(raw); } catch { return new Response("Invalid JSON", { status: 400 }); } + let delivery = headers.get("x-factorize-delivery"); + if (delivery === null) delivery = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(raw)).then(value => [...new Uint8Array(value)].map(byte => byte.toString(16).padStart(2, "0")).join("")); + await this.invokeWebhookJobs("custom", delivery!, payload, headers.get("x-factorize-event") ?? undefined); + return new Response(null, { status: 202 }); + } + private async testCloudflareTail(flowId: string): Promise { const pipe = this.one("SELECT * FROM pipes WHERE id=? AND enabled=1", flowId) as Row | undefined; if (!pipe) return new Response("Not found", { status: 404 }); @@ -756,41 +816,56 @@ export class Tenant extends DurableObject { const delays = [0, 5_000, 15_000, 30_000, 60_000]; for (const verification of pending) { const pipe = this.one("SELECT * FROM pipes WHERE id=?", verification.flow_id) as Row | undefined; - if (!pipe) { this.ctx.storage.sql.exec("DELETE FROM pending_verifications WHERE id=?", verification.id); continue; } + const job = pipe ? undefined : this.one("SELECT j.*,t.config AS trigger_config FROM jobs j JOIN triggers t ON t.job_id=j.id WHERE j.id=? AND j.enabled=1 AND t.kind='webhook'", verification.flow_id) as Row | undefined; + const target = pipe ?? job; + if (!target) { this.ctx.storage.sql.exec("DELETE FROM pending_verifications WHERE id=?", verification.id); continue; } const installation = this.one("SELECT state FROM github_installations WHERE installation_id=?", verification.installation_id) as Row | undefined; - if (installation?.state !== "active") { this.finishVerification(verification, pipe, "verification_failed", "GitHub installation is disconnected."); continue; } + if (installation?.state !== "active") { this.finishVerification(verification, target, "verification_failed", "GitHub installation is disconnected.", Boolean(job)); continue; } try { const token = await installationToken(this.env, Number(verification.installation_id)); const response = await fetch(`https://api.github.com/repos/${encodeURIComponent(String(verification.repository_owner))}/${encodeURIComponent(String(verification.repository_name))}/pulls/${verification.pull_number}`, { headers: githubHeaders(token) }); - if (response.status === 404) { this.finishVerification(verification, pipe, "ignored", "Pull request no longer exists or is inaccessible."); continue; } + if (response.status === 404) { this.finishVerification(verification, target, "ignored", "Pull request no longer exists or is inaccessible.", Boolean(job)); continue; } const rateLimited = response.status === 429 || (response.status === 403 && (response.headers.get("x-ratelimit-remaining") === "0" || response.headers.has("retry-after"))); - if (response.status === 401 || (response.status === 403 && !rateLimited)) { this.finishVerification(verification, pipe, "verification_failed", `GitHub access failed permanently (${response.status}).`); continue; } - if (!response.ok) { await this.retryVerification(verification, pipe, delays, `Transient GitHub API failure (${response.status}).`, rateLimited ? Number(response.headers.get("retry-after")) * 1000 : undefined); continue; } + if (response.status === 401 || (response.status === 403 && !rateLimited)) { this.finishVerification(verification, target, "verification_failed", `GitHub access failed permanently (${response.status}).`, Boolean(job)); continue; } + if (!response.ok) { await this.retryVerification(verification, target, delays, `Transient GitHub API failure (${response.status}).`, rateLimited ? Number(response.headers.get("retry-after")) * 1000 : undefined, Boolean(job)); continue; } const pull = await response.json() as any; - if (pull.state !== "open" || pull.base?.ref !== "main") { this.finishVerification(verification, pipe, "ignored", "Pull request is closed or no longer targets main."); continue; } - if (pull.mergeable === null) { await this.retryVerification(verification, pipe, delays, "GitHub is still calculating mergeability."); continue; } - if (pull.mergeable === true) { this.finishVerification(verification, pipe, "ignored", "GitHub reports that the pull request is mergeable."); continue; } - const source = this.sourceFor(pipe); - if (source.kind !== "github" || source.repositoryId !== Number(verification.repository_id)) { this.finishVerification(verification, pipe, "ignored", "Flow source changed while verification was pending."); continue; } + if (pull.state !== "open" || pull.base?.ref !== "main") { this.finishVerification(verification, target, "ignored", "Pull request is closed or no longer targets main.", Boolean(job)); continue; } + if (pull.mergeable === null) { await this.retryVerification(verification, target, delays, "GitHub is still calculating mergeability.", undefined, Boolean(job)); continue; } + if (pull.mergeable === true) { this.finishVerification(verification, target, "ignored", "GitHub reports that the pull request is mergeable.", Boolean(job)); continue; } + if (job) { + const config = JSON.parse(String(job.trigger_config)) as WebhookTriggerConfig; + const saved = JSON.parse(String(verification.payload)); + const payload = { ...saved, pull_request: { ...saved.pull_request, ...pull } }; + const invocation = adaptWebhook(config, "github", String(verification.delivery_id), payload, "pull_request"); + this.ctx.storage.sql.exec("DELETE FROM pending_verifications WHERE id=?", verification.id); + if (invocation) await this.invokeCanonicalJob(String(job.id), "webhook", invocation.claimKey, invocation.context, invocation.parameters, invocation.occurrence); + this.recordJobEvent(String(job.id), "github", String(verification.delivery_id), invocation ? "accepted" : "ignored", invocation ? "Verified webhook queued through canonical job invocation." : "Job trigger changed while verification was pending."); + continue; + } + const legacyPipe = pipe!; + const source = this.sourceFor(legacyPipe); + if (source.kind !== "github" || source.repositoryId !== Number(verification.repository_id)) { this.finishVerification(verification, legacyPipe, "ignored", "Flow source changed while verification was pending."); continue; } const saved = JSON.parse(String(verification.payload)); const workItem: WorkItem = { provider: "github", claimKey: githubClaimKey(source.repositoryId, pull.number), identifier: `${source.repositoryFullName}#${pull.number}`, title: pull.title ?? saved.title, description: pull.body ?? saved.body, url: pull.html_url, event: { type: "pull_request", name: "pull_request", action: "dequeued", delivery: verification.delivery_id }, repository: { id: source.repositoryId, owner: source.repositoryOwner, name: source.repositoryName, fullName: source.repositoryFullName }, pullRequest: { number: pull.number, url: pull.html_url, title: pull.title ?? "", body: pull.body ?? "", author: pull.user?.login ?? "", base: { ref: pull.base.ref, sha: pull.base.sha }, head: { ref: pull.head?.ref ?? "", sha: pull.head?.sha ?? "", repository: pull.head?.repo?.full_name ?? "" } } }; this.ctx.storage.sql.exec("DELETE FROM pending_verifications WHERE id=?", verification.id); - await this.queueWorkItem(pipe, String(verification.delivery_id), workItem, renderGitHubPrompt(workItem, { id: String(pipe.id), name: String(pipe.name) })); - } catch (error) { await this.retryVerification(verification, pipe, delays, error instanceof Error ? error.message : "GitHub verification failed."); } + await this.queueWorkItem(legacyPipe, String(verification.delivery_id), workItem, renderGitHubPrompt(workItem, { id: String(legacyPipe.id), name: String(legacyPipe.name) })); + } catch (error) { await this.retryVerification(verification, target, delays, error instanceof Error ? error.message : "GitHub verification failed.", undefined, Boolean(job)); } } } - private async retryVerification(verification: Row, pipe: Row, delays: number[], detail: string, overrideDelay?: number): Promise { + private async retryVerification(verification: Row, pipe: Row, delays: number[], detail: string, overrideDelay?: number, isJob = false): Promise { const nextAttempt = Number(verification.attempt) + 1; - if (nextAttempt >= delays.length) { this.finishVerification(verification, pipe, "verification_failed", `Verification retries exhausted: ${detail}`); return; } + if (nextAttempt >= delays.length) { this.finishVerification(verification, pipe, "verification_failed", `Verification retries exhausted: ${detail}`, isJob); return; } const delay = Math.max(delays[nextAttempt]!, Number.isFinite(overrideDelay) ? overrideDelay! : 0); this.ctx.storage.sql.exec("UPDATE pending_verifications SET attempt=?,next_attempt_at=? WHERE id=?", nextAttempt, Date.now() + delay, verification.id); - this.recordFlowEvent(pipe, String(verification.delivery_id), githubClaimKey(Number(verification.repository_id), Number(verification.pull_number)), null, { type: "pull_request", action: "dequeued" }, "waiting", detail, "github"); + if (isJob) this.recordJobEvent(String(pipe.id), "github", String(verification.delivery_id), "waiting", detail); + else this.recordFlowEvent(pipe, String(verification.delivery_id), githubClaimKey(Number(verification.repository_id), Number(verification.pull_number)), null, { type: "pull_request", action: "dequeued" }, "waiting", detail, "github"); } - private finishVerification(verification: Row, pipe: Row, outcome: string, detail: string): void { + private finishVerification(verification: Row, pipe: Row, outcome: string, detail: string, isJob = false): void { this.ctx.storage.sql.exec("DELETE FROM pending_verifications WHERE id=?", verification.id); - this.recordFlowEvent(pipe, String(verification.delivery_id), githubClaimKey(Number(verification.repository_id), Number(verification.pull_number)), null, { type: "pull_request", action: "dequeued" }, outcome, detail, "github"); + if (isJob) this.recordJobEvent(String(pipe.id), "github", String(verification.delivery_id), outcome, detail); + else this.recordFlowEvent(pipe, String(verification.delivery_id), githubClaimKey(Number(verification.repository_id), Number(verification.pull_number)), null, { type: "pull_request", action: "dequeued" }, outcome, detail, "github"); } private async startRun(run: Row, pipe: Row): Promise { diff --git a/packages/app/src/webhook-trigger.ts b/packages/app/src/webhook-trigger.ts new file mode 100644 index 00000000..b15978c5 --- /dev/null +++ b/packages/app/src/webhook-trigger.ts @@ -0,0 +1,41 @@ +import { matchingIssue } from "./matcher"; +import type { MatchRule } from "./types"; + +export type WebhookProvider = "linear" | "github" | "cloudflareTail" | "custom"; +export type WebhookTriggerConfig = { + provider: WebhookProvider; context?: string; parameters?: Record; + projectId?: string; matchRules?: MatchRule[]; installationId?: number; repositoryId?: number; + event?: string; action?: string; signingSecret?: string; secret?: string; handlerCode?: string; +}; +export interface WebhookInvocation { + claimKey: string; context: string; parameters: Record; + occurrence: { externalId: string; metadata: Record }; +} + +const strings = (value: unknown): Record => value && typeof value === "object" && !Array.isArray(value) + ? Object.fromEntries(Object.entries(value).filter((entry): entry is [string, string] => typeof entry[1] === "string")) : {}; + +/** Pure provider adapter: matching and normalization only; it never launches execution. */ +export function adaptWebhook(config: WebhookTriggerConfig, provider: WebhookProvider, deliveryId: string, payload: Record, eventName?: string): WebhookInvocation | null { + if (config.provider !== provider) return null; + if (provider === "linear") { + const data = payload.data ?? {}, projectId = data.project?.id ?? data.issue?.project?.id; + if (!config.projectId || projectId !== config.projectId || !matchingIssue(payload, { projectId: config.projectId, matchRules: config.matchRules ?? [] })) return null; + } + if (provider === "github") { + if (payload.installation?.id !== config.installationId || payload.repository?.id !== config.repositoryId) return null; + if ((eventName ?? "") !== (config.event ?? "pull_request") || payload.action !== (config.action ?? "dequeued")) return null; + } + const normalizedProvider = provider === "cloudflareTail" ? "cloudflare" : provider; + return { + claimKey: `webhook:${normalizedProvider}:${deliveryId}`, + context: config.context ?? JSON.stringify(payload), + parameters: { ...strings(config.parameters), provider: normalizedProvider, event: eventName ?? String(payload.type ?? "webhook"), deliveryId }, + occurrence: { externalId: deliveryId, metadata: { provider: normalizedProvider, event: eventName ?? payload.type ?? "webhook", action: payload.action ?? null } }, + }; +} + +export function publicWebhookConfig(config: WebhookTriggerConfig): Record { + const { signingSecret: _signingSecret, secret: _secret, ...safe } = config; + return { ...safe, ...(config.signingSecret || config.secret ? { secretConfigured: true } : {}) }; +} diff --git a/packages/app/test/webhook-trigger.test.ts b/packages/app/test/webhook-trigger.test.ts new file mode 100644 index 00000000..d1aac423 --- /dev/null +++ b/packages/app/test/webhook-trigger.test.ts @@ -0,0 +1,20 @@ +import { describe, expect, it } from "vitest"; +import { adaptWebhook, publicWebhookConfig } from "../src/webhook-trigger"; + +describe("webhook Job trigger adapters", () => { + it("matches Linear and derives a stable claim plus string invocation inputs", () => { + const config = { provider: "linear" as const, projectId: "p1", matchRules: [{ type: "status" as const, targetId: "done" }], parameters: { mode: "fix" } }; + const payload = { type: "Issue", action: "update", data: { id: "i1", project: { id: "p1" }, state: { id: "done" } }, updatedFrom: { stateId: "todo" } }; + expect(adaptWebhook(config, "linear", "linear:d1", payload)).toMatchObject({ claimKey: "webhook:linear:linear:d1", parameters: { mode: "fix", provider: "linear", deliveryId: "linear:d1" }, occurrence: { externalId: "linear:d1" } }); + }); + + it("matches GitHub installation, repository, event, and action", () => { + const config = { provider: "github" as const, installationId: 7, repositoryId: 9, event: "pull_request", action: "dequeued" }; + expect(adaptWebhook(config, "github", "github:d1", { installation: { id: 7 }, repository: { id: 9 }, action: "dequeued" }, "pull_request")).not.toBeNull(); + expect(adaptWebhook(config, "github", "github:d2", { installation: { id: 7 }, repository: { id: 10 }, action: "dequeued" }, "pull_request")).toBeNull(); + }); + + it("never exposes webhook secrets", () => { + expect(publicWebhookConfig({ provider: "cloudflareTail", signingSecret: "super-secret-value" })).toEqual({ provider: "cloudflareTail", secretConfigured: true }); + }); +});