Skip to content

Commit f33b060

Browse files
feat(relay,server,web,mobile): opt-in to hold webhooks while offline (pingdotgg#15487)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent ea4b443 commit f33b060

73 files changed

Lines changed: 5491 additions & 436 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@ import type {
44
ScheduledTask,
55
ScheduledTaskUpsertInput,
66
} from "@t3tools/contracts";
7-
import { resolveEnvironmentMachineKind } from "@t3tools/contracts";
7+
import {
8+
MAX_WEBHOOK_DELIVERY_AGE_MINUTES,
9+
resolveEnvironmentMachineKind,
10+
} from "@t3tools/contracts";
811
import type { MenuAction } from "@react-native-menu/menu";
912
import { DateTimePicker } from "@expo/ui/community/datetime-picker";
1013
import {
1114
isAtomCommandInterrupted,
1215
squashAtomCommandFailure,
1316
type AtomCommandResult,
1417
} from "@t3tools/client-runtime/state/runtime";
18+
import {
19+
DEFAULT_WEBHOOK_PROMPT,
20+
parseMaxDeliveryAge,
21+
} from "@t3tools/client-runtime/scheduled-task-webhook";
1522
import {
1623
useCallback,
1724
useEffect,
@@ -59,7 +66,6 @@ import { SettingsSection } from "./components/SettingsSection";
5966
import { useSettingsEnvironmentFilter, type SettingsTarget } from "./settings-environment-filter";
6067
import {
6168
editDraft,
62-
DEFAULT_WEBHOOK_PROMPT,
6369
scheduledTaskDefaultModel,
6470
scheduleFromDraft,
6571
type ScheduledTaskDraft as Draft,
@@ -132,7 +138,7 @@ function FormField(props: {
132138
readonly label: string;
133139
readonly value: string;
134140
readonly onChange: (value: string) => void;
135-
readonly keyboardType?: "decimal-pad";
141+
readonly keyboardType?: "decimal-pad" | "number-pad";
136142
readonly disabled?: boolean;
137143
readonly placeholder?: string;
138144
readonly borderTop?: boolean;
@@ -611,6 +617,16 @@ function TaskForm({
611617
? { ...draft.schedule, signature: liveTask.schedule.signature }
612618
: draft.schedule,
613619
);
620+
if (
621+
draft.schedule.mode === "webhook" &&
622+
parseMaxDeliveryAge(draft.schedule.maxDeliveryAgeMinutes) === undefined
623+
) {
624+
Alert.alert(
625+
"Invalid age limit",
626+
`Enter whole minutes from 1 to ${MAX_WEBHOOK_DELIVERY_AGE_MINUTES}, or leave it blank.`,
627+
);
628+
return;
629+
}
614630
if (
615631
!draft.title.trim() ||
616632
!draft.prompt.trim() ||
@@ -823,7 +839,7 @@ function TaskForm({
823839
options={[
824840
{ value: "fixed_time", label: "At a time" },
825841
{ value: "interval", label: "Interval" },
826-
{ value: "webhook", label: "Webhook" },
842+
{ value: "webhook", label: "On webhook" },
827843
]}
828844
selected={draft.schedule.mode}
829845
onSelect={(mode) => {
@@ -910,13 +926,31 @@ function TaskForm({
910926
/>
911927
</>
912928
) : draft.schedule.mode === "webhook" ? (
913-
<WebhookScheduleDetails
914-
environmentId={environmentId}
915-
task={
916-
tasks.data?.tasks.find((task) => task.id === draft.task?.id) ?? draft.task ?? null
917-
}
918-
signatureConfigured={draft.schedule.signature !== null}
919-
/>
929+
<>
930+
<WebhookScheduleDetails
931+
environmentId={environmentId}
932+
// The live row, so a rotated URL shows up without reopening the form.
933+
// Once the list has loaded, a missing task is gone; don't keep showing its URL.
934+
task={
935+
tasks.data
936+
? (tasks.data.tasks.find((task) => task.id === draft.task?.id) ?? null)
937+
: draft.task
938+
}
939+
signatureConfigured={draft.schedule.signature !== null}
940+
disabled={saving || environmentUnavailable}
941+
/>
942+
<FormField
943+
label="Skip requests older than (minutes)"
944+
value={draft.schedule.maxDeliveryAgeMinutes}
945+
placeholder="Run every request"
946+
keyboardType="number-pad"
947+
disabled={saving}
948+
borderTop
949+
onChange={(maxDeliveryAgeMinutes) =>
950+
setDraft({ ...draft, schedule: { ...draft.schedule, maxDeliveryAgeMinutes } })
951+
}
952+
/>
953+
</>
920954
) : (
921955
<>
922956
<FormField
@@ -976,11 +1010,14 @@ function WebhookScheduleDetails({
9761010
environmentId,
9771011
task,
9781012
signatureConfigured,
1013+
disabled,
9791014
}: {
9801015
readonly environmentId: EnvironmentId;
9811016
readonly task: ScheduledTask | null;
9821017
readonly signatureConfigured: boolean;
1018+
readonly disabled: boolean;
9831019
}) {
1020+
const [rotating, setRotating] = useState(false);
9841021
const rotate = useAtomCommand(serverEnvironment.rotateScheduledTaskWebhookToken, {
9851022
label: "scheduled task rotate webhook token",
9861023
reportFailure: false,
@@ -1023,27 +1060,34 @@ function WebhookScheduleDetails({
10231060
) : null}
10241061
<Pressable
10251062
accessibilityRole="button"
1063+
accessibilityState={{ disabled: disabled || rotating }}
1064+
disabled={disabled || rotating}
10261065
onPress={() =>
10271066
Alert.alert("Rotate URL?", "The current URL stops working immediately.", [
10281067
{ text: "Cancel", style: "cancel" },
10291068
{
10301069
text: "Rotate",
10311070
style: "destructive",
1032-
onPress: () =>
1071+
onPress: () => {
1072+
setRotating(true);
10331073
void rotate({ environmentId, input: { id: task.id } }).then((result) => {
1074+
setRotating(false);
10341075
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
10351076
Alert.alert(
10361077
"Could not rotate URL",
10371078
String(squashAtomCommandFailure(result)),
10381079
);
10391080
}
1040-
}),
1081+
});
1082+
},
10411083
},
10421084
])
10431085
}
1044-
className="min-h-11 justify-center active:opacity-70"
1086+
className="min-h-11 justify-center active:opacity-70 disabled:opacity-50"
10451087
>
1046-
<Text className="text-base text-danger-foreground">Rotate URL</Text>
1088+
<Text className="text-base text-danger-foreground">
1089+
{rotating ? "Rotating…" : "Rotate URL"}
1090+
</Text>
10471091
</Pressable>
10481092
</>
10491093
)}

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,25 @@ describe("scheduleDraftForTask", () => {
3232
it("round-trips a webhook schedule without a signature", () => {
3333
const draft = scheduleDraftForTask({ schedule: { type: "webhook", signature: null } });
3434
expect(draft.mode).toBe("webhook");
35-
expect(scheduleFromDraft(draft)).toEqual({ type: "webhook", signature: null });
35+
expect(scheduleFromDraft(draft)).toEqual({
36+
type: "webhook",
37+
signature: null,
38+
maxDeliveryAgeMinutes: null,
39+
});
40+
});
41+
42+
it("round-trips a webhook max age and treats blank input as no limit", () => {
43+
const draft = scheduleDraftForTask({
44+
schedule: { type: "webhook", signature: null, maxDeliveryAgeMinutes: 45 },
45+
});
46+
expect(draft.maxDeliveryAgeMinutes).toBe("45");
47+
expect(scheduleFromDraft(draft)).toMatchObject({ maxDeliveryAgeMinutes: 45 });
48+
// An invalid limit is an invalid schedule, never a silently removed one.
49+
expect(scheduleFromDraft({ ...draft, maxDeliveryAgeMinutes: "1.5" })).toBeNull();
50+
expect(scheduleFromDraft({ ...draft, maxDeliveryAgeMinutes: "0" })).toBeNull();
51+
expect(scheduleFromDraft({ ...draft, maxDeliveryAgeMinutes: "" })).toMatchObject({
52+
maxDeliveryAgeMinutes: null,
53+
});
3654
});
3755

3856
it("keeps a webhook signature on save without sending a secret", () => {
@@ -44,7 +62,7 @@ describe("scheduleDraftForTask", () => {
4462
const saved = scheduleFromDraft(
4563
scheduleDraftForTask({ schedule: { type: "webhook", signature } }),
4664
);
47-
expect(saved).toEqual({ type: "webhook", signature });
65+
expect(saved).toEqual({ type: "webhook", signature, maxDeliveryAgeMinutes: null });
4866
expect(saved?.type === "webhook" && saved.signature && "secret" in saved.signature).toBe(false);
4967
});
5068
});

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
} from "@t3tools/contracts";
1010

1111
import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts";
12+
import { parseMaxDeliveryAge } from "@t3tools/client-runtime/scheduled-task-webhook";
1213
import {
1314
resolveProjectSettings,
1415
type LegacyProjectSettingsFields,
@@ -44,6 +45,8 @@ export type ScheduleDraft = {
4445
readonly intervalMinutes: string;
4546
/** A webhook signature check configured elsewhere; mobile keeps it but does not edit it. */
4647
readonly signature: ScheduledTaskWebhookSignature | null;
48+
/** Minutes as typed; empty runs every held request regardless of age. */
49+
readonly maxDeliveryAgeMinutes: string;
4750
};
4851

4952
export const DEFAULT_SCHEDULE: ScheduleDraft = {
@@ -52,11 +55,9 @@ export const DEFAULT_SCHEDULE: ScheduleDraft = {
5255
weekdays: [1, 2, 3, 4, 5],
5356
intervalMinutes: "15",
5457
signature: null,
58+
maxDeliveryAgeMinutes: "",
5559
};
5660

57-
/** Prompt a new webhook task starts with: the whole request, which the user can narrow down. */
58-
export const DEFAULT_WEBHOOK_PROMPT = "Handle this webhook:\n{{request}}";
59-
6061
export function scheduleDraftForTask(task: Pick<ScheduledTask, "schedule">): ScheduleDraft {
6162
switch (task.schedule.type) {
6263
case "fixed_time":
@@ -74,12 +75,22 @@ export function scheduleDraftForTask(task: Pick<ScheduledTask, "schedule">): Sch
7475
intervalMinutes: String(Math.max(1, task.schedule.everyMs / 60_000)),
7576
};
7677
case "webhook":
77-
return { ...DEFAULT_SCHEDULE, mode: "webhook", signature: task.schedule.signature };
78+
return {
79+
...DEFAULT_SCHEDULE,
80+
mode: "webhook",
81+
signature: task.schedule.signature,
82+
maxDeliveryAgeMinutes:
83+
task.schedule.maxDeliveryAgeMinutes == null
84+
? ""
85+
: String(task.schedule.maxDeliveryAgeMinutes),
86+
};
7887
}
7988
}
8089

8190
export function scheduleFromDraft(draft: ScheduleDraft): ScheduledTaskUpsertSchedule | null {
8291
if (draft.mode === "webhook") {
92+
const maxDeliveryAgeMinutes = parseMaxDeliveryAge(draft.maxDeliveryAgeMinutes);
93+
if (maxDeliveryAgeMinutes === undefined) return null;
8394
// No secret is sent, so the server keeps the stored one.
8495
return {
8596
type: "webhook",
@@ -91,6 +102,7 @@ export function scheduleFromDraft(draft: ScheduleDraft): ScheduledTaskUpsertSche
91102
encoding: draft.signature.encoding,
92103
prefix: draft.signature.prefix,
93104
},
105+
maxDeliveryAgeMinutes,
94106
};
95107
}
96108
if (draft.mode === "interval") {
@@ -145,6 +157,7 @@ function draftSignature(draft: ScheduledTaskDraft): string {
145157
draft.schedule.timeOfDay,
146158
[...draft.schedule.weekdays].sort((a, b) => a - b),
147159
draft.schedule.intervalMinutes,
160+
draft.schedule.maxDeliveryAgeMinutes,
148161
draft.workspace,
149162
draft.baseRef,
150163
draft.checkoutPath,

‎apps/server/src/cloud/ManagedEndpointRuntime.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,36 @@ describe("CloudManagedEndpointRuntime", () => {
209209
}),
210210
);
211211

212+
it.effect("signals each registered tunnel connection", () =>
213+
Effect.gen(function* () {
214+
const output = yield* Queue.unbounded<Uint8Array>();
215+
const spawner = ChildProcessSpawner.make(() =>
216+
Effect.gen(function* () {
217+
const handle = makeHandle({
218+
pid: 700,
219+
onKill: () => {},
220+
output: Stream.fromQueue(output),
221+
});
222+
yield* Effect.addFinalizer(() => handle.kill().pipe(Effect.ignore));
223+
return handle;
224+
}),
225+
);
226+
const runtime = yield* buildCloudManagedEndpointRuntime(spawner);
227+
yield* runtime.applyConfig({
228+
providerKind: "cloudflare_tunnel",
229+
connectorToken: "token",
230+
tunnelId: "tunnel-1",
231+
});
232+
yield* Queue.offer(
233+
output,
234+
new TextEncoder().encode(
235+
"2026-10-04T06:30:43Z INF Registered tunnel connection connIndex=0 event=0\n",
236+
),
237+
);
238+
expect(Option.isSome(yield* Stream.runHead(runtime.tunnelConnected))).toBe(true);
239+
}),
240+
);
241+
212242
it.effect("recovers a rejected tunnel without waiting for the connector to exit", () =>
213243
Effect.gen(function* () {
214244
const output = yield* Queue.unbounded<Uint8Array>();

‎apps/server/src/cloud/ManagedEndpointRuntime.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ export class CloudManagedEndpointRuntime extends Context.Service<
4747
) => Effect.Effect<CloudManagedEndpointRuntimeStatus>;
4848
readonly recoveryRequests: Stream.Stream<RelayManagedEndpointRuntimeConfig>;
4949
readonly requestRecovery: (config: RelayManagedEndpointRuntimeConfig) => Effect.Effect<void>;
50+
/** Emits when the connector registers a tunnel connection, i.e. the relay can reach us again. */
51+
readonly tunnelConnected: Stream.Stream<void>;
5052
readonly withLinkStateLock: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
5153
}
5254
>()("t3/cloud/ManagedEndpointRuntime/CloudManagedEndpointRuntime") {}
@@ -134,6 +136,7 @@ export const make = Effect.gen(function* () {
134136
const activeRef = yield* Ref.make<ActiveConnector | null>(null);
135137
const desiredConfigRef = yield* Ref.make<RelayManagedEndpointRuntimeConfig | null>(null);
136138
const recoveryRequests = yield* Queue.sliding<RelayManagedEndpointRuntimeConfig>(1);
139+
const tunnelConnections = yield* Queue.sliding<void>(1);
137140
const reconcileSemaphore = yield* Semaphore.make(1);
138141
const restartDelayRef = yield* Ref.make(0);
139142
const linkStateSemaphore = yield* Semaphore.make(1);
@@ -236,7 +239,10 @@ export const make = Effect.gen(function* () {
236239
switch (classifyRelayClientOutput(line)) {
237240
case "connected":
238241
rejectedRegistrations = 0;
239-
return Effect.logInfo("Relay client tunnel connection registered", attributes);
242+
return Effect.logInfo("Relay client tunnel connection registered", attributes).pipe(
243+
Effect.andThen(Queue.offer(tunnelConnections, undefined)),
244+
Effect.asVoid,
245+
);
240246
case "warning":
241247
if (isRejectedRelayClientTunnelOutput(line)) {
242248
rejectedRegistrations += 1;
@@ -412,6 +418,7 @@ export const make = Effect.gen(function* () {
412418
applyConfig,
413419
recoveryRequests: Stream.fromQueue(recoveryRequests),
414420
requestRecovery: (config) => Queue.offer(recoveryRequests, config).pipe(Effect.asVoid),
421+
tunnelConnected: Stream.fromQueue(tunnelConnections),
415422
withLinkStateLock: linkStateSemaphore.withPermits(1),
416423
});
417424

‎apps/server/src/cloud/config.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const RELAY_URL_SECRET = "cloud-relay-url";
1616
export const RELAY_ISSUER_SECRET = "cloud-relay-issuer";
1717
export const RELAY_ENVIRONMENT_CREDENTIAL_SECRET = "cloud-relay-environment-credential";
1818
export const PUBLISH_AGENT_ACTIVITY_SECRET = "cloud-publish-agent-activity";
19+
export const HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET = "cloud-hold-webhooks-while-offline";
1920

2021
export const encodeEndpointRuntimeConfigJson = Schema.encodeEffect(
2122
Schema.fromJsonString(RelayManagedEndpointRuntimeConfig),
@@ -73,3 +74,33 @@ export const readAgentActivityPublishingActive = (
7374
environmentCredential !== ""
7475
);
7576
}).pipe(Effect.orElseSucceed(() => false));
77+
78+
const readSecretString = (
79+
secrets: ServerSecretStore.ServerSecretStore["Service"],
80+
name: string,
81+
): Effect.Effect<string | null> =>
82+
secrets.get(name).pipe(
83+
Effect.map((bytes) =>
84+
Option.isSome(bytes) && bytes.value.length > 0 ? new TextDecoder().decode(bytes.value) : null,
85+
),
86+
Effect.orElseSucceed(() => null),
87+
);
88+
89+
/** The relay URL and environment credential, or null when not linked to T3 Connect. */
90+
export const readRelayConnection = (secrets: ServerSecretStore.ServerSecretStore["Service"]) =>
91+
Effect.all([
92+
readSecretString(secrets, RELAY_URL_SECRET),
93+
readSecretString(secrets, RELAY_ENVIRONMENT_CREDENTIAL_SECRET),
94+
]).pipe(
95+
Effect.map(([url, environmentCredential]) =>
96+
url && environmentCredential ? { url, environmentCredential } : null,
97+
),
98+
);
99+
100+
/** Whether this environment opted in to T3 Connect holding webhooks while it is offline. */
101+
export const readHoldWebhooksWhileOffline = (
102+
secrets: ServerSecretStore.ServerSecretStore["Service"],
103+
) =>
104+
readSecretString(secrets, HOLD_WEBHOOKS_WHILE_OFFLINE_SECRET).pipe(
105+
Effect.map((value) => value === "true"),
106+
);

‎apps/server/src/cloud/http.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ describe("reconcileDesiredCloudLink", () => {
249249
applyConfig: unusedSecretStoreOperation,
250250
recoveryRequests: Stream.empty,
251251
requestRecovery: () => Effect.void,
252+
tunnelConnected: Stream.empty,
252253
withLinkStateLock: (effect) => effect,
253254
} satisfies ManagedEndpointRuntime.CloudManagedEndpointRuntime["Service"]),
254255
),
@@ -410,6 +411,7 @@ describe("releaseManagedTunnelOnShutdown", () => {
410411
}),
411412
recoveryRequests: Stream.empty,
412413
requestRecovery: () => Effect.void,
414+
tunnelConnected: Stream.empty,
413415
withLinkStateLock: (effect) => effect,
414416
}),
415417
),

0 commit comments

Comments
 (0)