Skip to content

Commit 7dfb86a

Browse files
feat(server): scheduled tasks can run on a webhook (pingdotgg#15085)
1 parent 54b6b66 commit 7dfb86a

32 files changed

Lines changed: 2079 additions & 53 deletions

‎apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ const DAYS = [
8787
] as const;
8888

8989
function describeSchedule(task: ScheduledTask): string {
90+
if (task.schedule.type === "webhook") return "On webhook";
9091
if (task.schedule.type === "interval") return formatScheduledTaskInterval(task.schedule.everyMs);
9192
const days = task.schedule.weekdays?.length ? repeatLabel(task.schedule.weekdays) : "Every day";
9293
return `${days} at ${formatTime(task.schedule.timeOfDay)}`;
@@ -1024,6 +1025,8 @@ function EnvironmentTasks({
10241025
<Pressable
10251026
accessibilityRole="button"
10261027
accessibilityLabel={`Edit ${task.title}`}
1028+
// Webhook tasks are not editable here yet; saving would drop their URL.
1029+
disabled={task.schedule.type === "webhook"}
10271030
onPress={() => {
10281031
onEdit(task);
10291032
}}
@@ -1048,7 +1051,7 @@ function EnvironmentTasks({
10481051
</Pressable>
10491052
<ControlPillMenu
10501053
actions={[
1051-
{ id: "edit", title: "Edit" },
1054+
...(task.schedule.type === "webhook" ? [] : [{ id: "edit", title: "Edit" }]),
10521055
{ id: "toggle", title: task.enabled ? "Pause" : "Resume" },
10531056
{ id: "run", title: "Run now" },
10541057
{ id: "delete", title: "Delete", attributes: { destructive: true } },

‎apps/mobile/src/features/settings/scheduledTaskDraft.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export const DEFAULT_SCHEDULE: ScheduleDraft = {
5151
};
5252

5353
export function scheduleDraftForTask(task: Pick<ScheduledTask, "schedule">): ScheduleDraft {
54+
// Webhook tasks cannot be edited here yet; show them as the default schedule.
55+
if (task.schedule.type === "webhook") return DEFAULT_SCHEDULE;
5456
return task.schedule.type === "fixed_time"
5557
? {
5658
...DEFAULT_SCHEDULE,

‎apps/server/src/auth/RpcAuthorization.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ describe("RPC authorization scopes", () => {
3838
);
3939
});
4040

41+
it("keeps webhook delivery logs, which hold request bodies, behind operate scope", () => {
42+
for (const method of [
43+
WS_METHODS.scheduledTasksListWebhookDeliveries,
44+
WS_METHODS.scheduledTasksGetWebhookDelivery,
45+
WS_METHODS.scheduledTasksRotateWebhookToken,
46+
]) {
47+
expect(requiredScopeForRpcMethod(method)).toBe(AuthOrchestrationOperateScope);
48+
}
49+
});
50+
4151
it("allows relay status reads without granting relay installation access", () => {
4252
expect(requiredScopeForRpcMethod(WS_METHODS.cloudGetRelayClientStatus)).toBe(
4353
AuthRelayReadScope,

‎apps/server/src/auth/RpcAuthorization.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ export const RPC_REQUIRED_SCOPES = {
9595
[WS_METHODS.scheduledTasksSetEnabled]: AuthOrchestrationOperateScope,
9696
[WS_METHODS.scheduledTasksDelete]: AuthOrchestrationOperateScope,
9797
[WS_METHODS.scheduledTasksRunNow]: AuthOrchestrationOperateScope,
98+
[WS_METHODS.scheduledTasksRotateWebhookToken]: AuthOrchestrationOperateScope,
99+
// Delivery logs hold request bodies, so they need the same scope as the URL.
100+
[WS_METHODS.scheduledTasksListWebhookDeliveries]: AuthOrchestrationOperateScope,
101+
[WS_METHODS.scheduledTasksGetWebhookDelivery]: AuthOrchestrationOperateScope,
98102
[WS_METHODS.cloudGetRelayClientStatus]: AuthRelayReadScope,
99103
[WS_METHODS.cloudInstallRelayClient]: AuthRelayWriteScope,
100104
[WS_METHODS.pullRequestsList]: AuthOrchestrationReadScope,

‎apps/server/src/http.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
failEnvironmentInternal,
4747
} from "./auth/http.ts";
4848
import * as ServerEnvironment from "./environment/ServerEnvironment.ts";
49+
import { WEBHOOK_ROUTE_PREFIX } from "./scheduledTasks/ScheduledTaskService.ts";
4950
import { browserApiCorsAllowedHeaders, browserApiCorsAllowedMethods } from "./httpCors.ts";
5051

5152
const OTLP_TRACES_PROXY_PATH = "/api/observability/v1/traces";
@@ -381,9 +382,9 @@ const UNTRACED_REQUEST_PATHS: ReadonlySet<string> = new Set([OTLP_TRACES_PROXY_P
381382
// ignored, as in routing.
382383
const untracedRequestsLayer = Layer.succeed(HttpMiddleware.TracerDisabledWhen)((request) => {
383384
const queryIndex = request.url.indexOf("?");
384-
return UNTRACED_REQUEST_PATHS.has(
385-
queryIndex === -1 ? request.url : request.url.slice(0, queryIndex),
386-
);
385+
const path = queryIndex === -1 ? request.url : request.url.slice(0, queryIndex);
386+
// Webhook URLs carry their secret token in the path, so they never reach a trace.
387+
return UNTRACED_REQUEST_PATHS.has(path) || path.startsWith(`${WEBHOOK_ROUTE_PREFIX}/`);
387388
});
388389

389390
export const withUntracedRequests = Layer.provide(untracedRequestsLayer);

‎apps/server/src/mcp/OrchestratorMcpService.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ function scheduledTaskSummary(task: ScheduledTask): OrchestratorMcpScheduledTask
212212
schedule: task.schedule,
213213
nextRunAt: task.nextRunAt,
214214
lastRunStatus: task.lastRunStatus,
215+
...(task.webhook === undefined ? {} : { webhookUrl: task.webhook.url ?? task.webhook.path }),
215216
};
216217
}
217218

‎apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,8 @@ function scheduledTaskFromUpsert(input: ScheduledTaskUpsertInput): ScheduledTask
443443
title: input.title,
444444
prompt: input.prompt,
445445
enabled: input.enabled,
446-
schedule: input.schedule,
446+
schedule:
447+
input.schedule.type === "webhook" ? { type: "webhook", signature: null } : input.schedule,
447448
projectId: input.projectId,
448449
threadId: input.threadId ?? null,
449450
workspaceStrategy: input.workspaceStrategy,
@@ -471,6 +472,10 @@ const unusedScheduledTaskStubLayer = Layer.succeed(
471472
setEnabled: () => Effect.die("ScheduledTaskService.setEnabled is unused in this test"),
472473
delete: () => Effect.die("ScheduledTaskService.delete is unused in this test"),
473474
runNow: () => Effect.die("ScheduledTaskService.runNow is unused in this test"),
475+
rotateWebhookToken: () => Effect.die("unused in this test"),
476+
listWebhookDeliveries: () => Effect.die("unused in this test"),
477+
getWebhookDelivery: () => Effect.die("unused in this test"),
478+
triggerWebhook: () => Effect.die("unused in this test"),
474479
}),
475480
);
476481

@@ -628,6 +633,10 @@ describe("orchestrator MCP toolkit", () => {
628633
all.filter((candidate) => candidate.id !== input.id),
629634
).pipe(Effect.as({ id: input.id })),
630635
runNow: () => Effect.die("ScheduledTaskService.runNow is unused in this test"),
636+
rotateWebhookToken: () => Effect.die("unused in this test"),
637+
listWebhookDeliveries: () => Effect.die("unused in this test"),
638+
getWebhookDelivery: () => Effect.die("unused in this test"),
639+
triggerWebhook: () => Effect.die("unused in this test"),
631640
}),
632641
);
633642
const testLayer = Layer.merge(

‎apps/server/src/mcp/toolkits/orchestrator/tools.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ const TaskCancelTool = Tool.make("task_cancel", {
9797

9898
export const ScheduleTaskTool = Tool.make("schedule_task", {
9999
description:
100-
"Create persistent recurring work in the app scheduler, which runs even when no turn is active. Pass schedule as a STRUCTURED OBJECT, never JSON text: {type:'interval', everyMs:3600000} means hourly; {type:'fixed_time', timeOfDay:'09:00', weekdays:[1,2,3,4,5]} means weekday mornings. Omit projectId for this thread's project. In this thread's project, runs post into THIS thread by default (bindToCurrentThread=true); use false only when the user wants a fresh top-level thread per run. Elsewhere each run launches a fresh thread. Provider, model, and runtime settings inherit from this thread, or from the project default when there is no calling thread. Report the returned schedule and nextRunAt after success.",
100+
"Create persistent recurring work in the app scheduler, which runs even when no turn is active. Pass schedule as a STRUCTURED OBJECT, never JSON text: {type:'interval', everyMs:3600000} means hourly; {type:'fixed_time', timeOfDay:'09:00', weekdays:[1,2,3,4,5]} means weekday mornings; {type:'webhook'} runs on each request to a generated URL (returned as webhookUrl), and its prompt may use {{body.path}}, {{headers.name}}, {{query.name}}, {{body}} or {{request}} placeholders, which are the only request data the run sees. Omit projectId for this thread's project. In this thread's project, runs post into THIS thread by default (bindToCurrentThread=true); use false only when the user wants a fresh top-level thread per run. Elsewhere each run launches a fresh thread. Provider, model, and runtime settings inherit from this thread, or from the project default when there is no calling thread. Report the returned schedule and nextRunAt after success.",
101101
parameters: OrchestratorMcpScheduleTaskInput,
102102
success: OrchestratorMcpScheduleTaskResult,
103103
failure: OrchestratorMcpFailure,

‎apps/server/src/persistence/Migrations.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ import Migration0053 from "./Migrations/053_PullRequestFilesViewed.ts";
7070
import Migration0054 from "./Migrations/054_ProjectionThreadsAutoSettleDisabledAt.ts";
7171
import Migration0055 from "./Migrations/055_OrchestrationV2.ts";
7272
import Migration0056 from "./Migrations/056_RemoveRedundantProjectionIndexes.ts";
73+
import Migration0057 from "./Migrations/057_ScheduledTaskWebhooks.ts";
7374

7475
/**
7576
* Migration loader with all migrations defined inline.
@@ -140,6 +141,7 @@ export const migrationEntries = [
140141
// Preserve this migration's schema. Future V2 schema changes need new migrations.
141142
[55, "OrchestrationV2", Migration0055],
142143
[56, "RemoveRedundantProjectionIndexes", Migration0056],
144+
[57, "ScheduledTaskWebhooks", Migration0057],
143145
] as const;
144146

145147
export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);

‎apps/server/src/persistence/Migrations/055_OrchestrationV2.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ layer("055_OrchestrationV2", (it) => {
1313
Effect.sync(() => {
1414
assert.deepStrictEqual(
1515
migrationEntries.map(([id]) => id),
16-
Array.from({ length: 56 }, (_, index) => index + 1),
16+
Array.from({ length: 57 }, (_, index) => index + 1),
1717
);
1818
}),
1919
);
@@ -28,6 +28,7 @@ layer("055_OrchestrationV2", (it) => {
2828
[54, "ProjectionThreadsAutoSettleDisabledAt"],
2929
[55, "OrchestrationV2"],
3030
[56, "RemoveRedundantProjectionIndexes"],
31+
[57, "ScheduledTaskWebhooks"],
3132
]);
3233
assert.deepStrictEqual(yield* runMigrations(), []);
3334

@@ -50,6 +51,7 @@ layer("055_OrchestrationV2", (it) => {
5051
{ migration_id: 54, name: "ProjectionThreadsAutoSettleDisabledAt" },
5152
{ migration_id: 55, name: "OrchestrationV2" },
5253
{ migration_id: 56, name: "RemoveRedundantProjectionIndexes" },
54+
{ migration_id: 57, name: "ScheduledTaskWebhooks" },
5355
]);
5456

5557
const tables = yield* sql<{ readonly name: string }>`

0 commit comments

Comments
 (0)