The best way to control Claude Code, Codex and more from any computer or phone

main
ts 1,099 lines 36.7 KB
Raw
1 import type {
2 RelayAgentActivityAggregateState,
3 RelayAgentActivityState,
4 RelayAgentAwarenessPreferences,
5 RelayDeliveryKind,
6 RelayDeliveryResult,
7 } from "@t3tools/contracts/relay";
8 import {
9 RelayAgentActivityAggregateState as RelayAgentActivityAggregateStateSchema,
10 RelayAgentAwarenessPreferences as RelayAgentAwarenessPreferencesSchema,
11 RelayDeliveryKind as RelayDeliveryKindSchema,
12 } from "@t3tools/contracts/relay";
13 import * as Arr from "effect/Array";
14 import * as Context from "effect/Context";
15 import * as DateTime from "effect/DateTime";
16 import * as Mac from "effect/crypto/Mac";
17 import * as Effect from "effect/Effect";
18 import * as Layer from "effect/Layer";
19 import * as Option from "effect/Option";
20 import * as Redacted from "effect/Redacted";
21 import * as Schema from "effect/Schema";
22 import * as Stream from "effect/Stream";
23
24 import {
25 isExpiredAgentActivityState,
26 notificationForActivity,
27 sanitizeAgentActivityAggregateState,
28 sanitizeApnsNotificationPayload,
29 } from "./agentActivityPayloads.ts";
30 import { makeAggregateState, statusForPhase } from "./agentActivityAggregate.ts";
31 import * as Apns from "./ApnsClient.ts";
32 import {
33 ApnsDeliveryJobLiveActivityAggregateMissing,
34 ApnsDeliveryJobPushNotificationMissing,
35 ApnsDeliveryJobQueuePayloadInvalid,
36 type ApnsLiveActivityAlert,
37 type ApnsNotificationPayload,
38 SignedApnsDeliveryJob,
39 verifySignedApnsDeliveryJob,
40 type ApnsDeliveryJobVerificationError,
41 } from "./apnsDeliveryJobs.ts";
42 import * as AgentActivityRows from "./AgentActivityRows.ts";
43 import * as DeliveryAttempts from "./DeliveryAttempts.ts";
44 import * as LiveActivities from "./LiveActivities.ts";
45 import * as RelayConfiguration from "../Config.ts";
46 import * as ApnsDeliveryQueue from "./ApnsDeliveryQueue.ts";
47 import { withSpanAttributes } from "../observability.ts";
48
49 import {
50 alertForAttentionTransition,
51 alertForNewlyTerminal,
52 alertForTerminalAggregate,
53 rowPhasesChanged,
54 shouldAlertForActivity,
55 } from "./agentActivityAlerts.ts";
56 const MIN_LIVE_ACTIVITY_UPDATE_INTERVAL_MS = 15_000;
57 const FRESHLY_ARMED_GRACE_MS = 2 * 60 * 1_000;
58 const PERMANENT_APNS_TOKEN_REASONS = new Set([
59 "BadDeviceToken",
60 "DeviceTokenNotForTopic",
61 "Unregistered",
62 ]);
63
64 type LiveActivityDeliveryKind = Extract<
65 RelayDeliveryKind,
66 "live_activity_update" | "live_activity_end"
67 >;
68
69 type ChosenLiveActivityDelivery =
70 | {
71 readonly kind: "live_activity_update";
72 readonly token: string;
73 readonly aggregate: RelayAgentActivityAggregateState;
74 readonly alert: ApnsLiveActivityAlert | null;
75 }
76 | {
77 readonly kind: "live_activity_end";
78 readonly token: string;
79 readonly aggregate: RelayAgentActivityAggregateState | null;
80 readonly alert: ApnsLiveActivityAlert | null;
81 };
82
83 type ChosenPushNotificationDelivery = {
84 readonly kind: "push_notification";
85 readonly token: string;
86 readonly notification: ApnsNotificationPayload;
87 };
88
89 type ChosenDelivery = ChosenLiveActivityDelivery | ChosenPushNotificationDelivery;
90
91 export type ApnsDeliveryError =
92 | ApnsDeliveryQueue.ApnsDeliveryQueueError
93 | ApnsDeliveryJobVerificationError
94 | ApnsDeliveryJobClaimInFlight
95 | DeliveryAttempts.DeliveryAttemptRecordPersistenceError
96 | LiveActivities.LiveActivityTargetListPersistenceError
97 | LiveActivities.LiveActivityDeliveryMarkPersistenceError;
98
99 export class ApnsDeliveryJobClaimInFlight extends Schema.TaggedError<ApnsDeliveryJobClaimInFlight>()(
100 "ApnsDeliveryJobClaimInFlight",
101 {
102 sourceJobId: Schema.String,
103 },
104 ) {
105 override get message(): string {
106 return `APNs delivery job '${this.sourceJobId}' is already in flight`;
107 }
108 }
109
110 export class ApnsDeliveryTransportError extends Schema.TaggedError<ApnsDeliveryTransportError>()(
111 "ApnsDeliveryTransportError",
112 {
113 deviceId: Schema.String,
114 kind: RelayDeliveryKindSchema,
115 sourceJobId: Schema.NullOr(Schema.String),
116 apnsErrorTag: Schema.Literals([
117 "ApnsJwtEncodingError",
118 "ApnsJwtSigningError",
119 "ApnsHttpRequestError",
120 ]),
121 requestStage: Schema.NullOr(Schema.Literals(["send", "read-response"])),
122 cause: Schema.Defect(),
123 },
124 ) {
125 override get message(): string {
126 return `APNs ${this.kind} delivery failed for device ${this.deviceId}.`;
127 }
128 }
129
130 const decodeRelayAgentActivityAggregateStateJson = Schema.decodeUnknownOption(
131 Schema.fromJsonString(RelayAgentActivityAggregateStateSchema),
132 );
133 const decodeRelayAgentAwarenessPreferencesJson = Schema.decodeUnknownOption(
134 Schema.fromJsonString(RelayAgentAwarenessPreferencesSchema),
135 );
136 const decodeSignedApnsDeliveryJob = Schema.decodeUnknownEffect(SignedApnsDeliveryJob);
137 const decodeSignedApnsDeliveryJobOption = Schema.decodeUnknownOption(SignedApnsDeliveryJob);
138
139 function parseAggregate(value: string | null): RelayAgentActivityAggregateState | null {
140 if (!value) {
141 return null;
142 }
143 return Option.getOrNull(decodeRelayAgentActivityAggregateStateJson(value));
144 }
145
146 function parsePreferences(value: string): RelayAgentAwarenessPreferences | null {
147 return Option.getOrNull(decodeRelayAgentAwarenessPreferencesJson(value));
148 }
149
150 function aggregateNeedsAttention(aggregate: RelayAgentActivityAggregateState): boolean {
151 return aggregate.activities.some(
152 (row) => row.phase === "waiting_for_approval" || row.phase === "waiting_for_input",
153 );
154 }
155
156 function shouldUpdateLiveActivity(input: {
157 readonly previousAggregate: RelayAgentActivityAggregateState | null;
158 readonly nextAggregate: RelayAgentActivityAggregateState;
159 readonly lastDeliveryAt: string | null;
160 readonly nowMs: number;
161 }): boolean {
162 if (!input.previousAggregate) {
163 return true;
164 }
165 if (JSON.stringify(input.previousAggregate) === JSON.stringify(input.nextAggregate)) {
166 return false;
167 }
168 if (input.previousAggregate.activeCount !== input.nextAggregate.activeCount) {
169 return true;
170 }
171 if (aggregateNeedsAttention(input.nextAggregate)) {
172 return true;
173 }
174 // Phase and row changes may be the last update until the next transition.
175 // Throttling them would leave the card showing a stale state indefinitely.
176 if (rowPhasesChanged(input.previousAggregate, input.nextAggregate)) {
177 return true;
178 }
179 const lastDeliveryAtMs =
180 input.lastDeliveryAt === null
181 ? null
182 : Option.match(DateTime.make(input.lastDeliveryAt), {
183 onNone: () => Number.NaN,
184 onSome: (dt) => dt.epochMilliseconds,
185 });
186 return (
187 lastDeliveryAtMs === null ||
188 Number.isNaN(lastDeliveryAtMs) ||
189 input.nowMs - lastDeliveryAtMs >= MIN_LIVE_ACTIVITY_UPDATE_INTERVAL_MS
190 );
191 }
192
193 // Aggregate rows may belong to unrelated threads; fallback pushes use the published state.
194 function notificationForState(input: {
195 readonly target: LiveActivities.TargetRow;
196 readonly state?: RelayAgentActivityState | null;
197 readonly nowMs: number;
198 }): ApnsNotificationPayload | null {
199 const state = input.state;
200 if (!input.target.push_token || !state || isExpiredAgentActivityState(state, input.nowMs)) {
201 return null;
202 }
203 const preferences = parsePreferences(input.target.preferences_json);
204 if (!shouldAlertForActivity({ ...state, preferences, nowMs: input.nowMs })) return null;
205 return notificationForActivity({ ...state, status: statusForPhase(state.phase) });
206 }
207
208 // Suppressed Live Activity updates must not fall back to alert pushes or unchanged replays would ring.
209 function chooseLiveActivityDelivery(input: {
210 readonly target: LiveActivities.TargetRow;
211 readonly aggregate: RelayAgentActivityAggregateState | null;
212 readonly nowMs: number;
213 readonly replay?: boolean;
214 }): ChosenLiveActivityDelivery | "suppressed" | null {
215 const preferences = parsePreferences(input.target.preferences_json);
216 if (preferences?.liveActivitiesEnabled === false) {
217 return input.target.activity_push_token
218 ? {
219 kind: "live_activity_end",
220 token: input.target.activity_push_token,
221 aggregate: null,
222 alert: null,
223 }
224 : null;
225 }
226 if (!input.target.activity_push_token) {
227 return null;
228 }
229 if (input.aggregate === null) {
230 // Registration replay can precede a new thread's first publish; preserve a freshly armed card until state arrives.
231 const armedAtMs = Option.match(
232 input.target.remote_started_at === null
233 ? Option.none()
234 : DateTime.make(input.target.remote_started_at),
235 { onNone: () => null, onSome: (dt) => dt.epochMilliseconds },
236 );
237 if (armedAtMs !== null && input.nowMs - armedAtMs < FRESHLY_ARMED_GRACE_MS) {
238 return null;
239 }
240 return {
241 kind: "live_activity_end",
242 token: input.target.activity_push_token,
243 aggregate: null,
244 alert: null,
245 };
246 }
247 const nextAggregate = input.aggregate;
248 const previousAggregate = parseAggregate(input.target.last_aggregate_json);
249 return shouldUpdateLiveActivity({
250 previousAggregate,
251 nextAggregate,
252 lastDeliveryAt: input.target.last_live_activity_delivery_at,
253 nowMs: input.nowMs,
254 })
255 ? {
256 kind: "live_activity_update",
257 token: input.target.activity_push_token,
258 aggregate: nextAggregate,
259 alert: input.replay
260 ? null
261 : (alertForAttentionTransition({
262 previousAggregate,
263 nextAggregate,
264 preferences,
265 }) ??
266 alertForNewlyTerminal({
267 previousAggregate,
268 nextAggregate,
269 preferences,
270 nowMs: input.nowMs,
271 includeUnobserved: true,
272 })),
273 }
274 : "suppressed";
275 }
276
277 function chooseDelivery(input: {
278 readonly target: LiveActivities.TargetRow;
279 readonly aggregate: RelayAgentActivityAggregateState | null;
280 readonly state?: RelayAgentActivityState | null;
281 readonly nowMs: number;
282 readonly replay?: boolean;
283 }): ChosenDelivery | null {
284 const liveActivityDelivery = chooseLiveActivityDelivery(input);
285 if (liveActivityDelivery === "suppressed") {
286 return null;
287 }
288 if (liveActivityDelivery) {
289 return liveActivityDelivery;
290 }
291 const notification = input.replay ? null : notificationForState(input);
292 return notification && input.target.push_token
293 ? {
294 kind: "push_notification",
295 token: input.target.push_token,
296 notification,
297 }
298 : null;
299 }
300
301 function deliveryEvent(kind: LiveActivityDeliveryKind): Apns.ApnsLiveActivityEvent {
302 switch (kind) {
303 case "live_activity_update":
304 return "update";
305 case "live_activity_end":
306 return "end";
307 }
308 }
309
310 function isPermanentApnsTokenFailure(result: Apns.ApnsDeliveryResult): boolean {
311 return (
312 !result.ok &&
313 (result.status === 410 ||
314 (result.status === 400 &&
315 result.reason !== undefined &&
316 PERMANENT_APNS_TOKEN_REASONS.has(result.reason)))
317 );
318 }
319
320 function duplicateJobResult(input: {
321 readonly deviceId: string;
322 readonly kind: RelayDeliveryKind;
323 }): RelayDeliveryResult {
324 return {
325 deviceId: input.deviceId,
326 kind: input.kind,
327 ok: true,
328 apnsStatus: null,
329 apnsReason: "Duplicate APNs delivery job skipped.",
330 apnsId: null,
331 };
332 }
333
334 function staleJobResult(input: {
335 readonly deviceId: string;
336 readonly kind: RelayDeliveryKind;
337 }): RelayDeliveryResult {
338 return {
339 deviceId: input.deviceId,
340 kind: input.kind,
341 ok: true,
342 apnsStatus: null,
343 apnsReason: "Stale APNs delivery job skipped.",
344 apnsId: null,
345 };
346 }
347
348 function deliveryAttemptOutcome(result: Apns.ApnsDeliveryResult) {
349 return {
350 ...(result.status === 0 ? {} : { apnsStatus: result.status }),
351 ...(result.reason === undefined ? {} : { apnsReason: result.reason }),
352 apnsId: result.apnsId,
353 ...(result.status === 0 ? { transportError: result.reason ?? "APNs request failed." } : {}),
354 };
355 }
356
357 const recoverApnsDeliveryTransportError = (
358 input: {
359 readonly deviceId: string;
360 readonly kind: RelayDeliveryKind;
361 readonly sourceJobId: string | null;
362 },
363 cause: Apns.ApnsError,
364 ): Effect.Effect<Apns.ApnsDeliveryResult> => {
365 const error = new ApnsDeliveryTransportError({
366 deviceId: input.deviceId,
367 kind: input.kind,
368 sourceJobId: input.sourceJobId,
369 apnsErrorTag: cause._tag,
370 requestStage: cause._tag === "ApnsHttpRequestError" ? cause.stage : null,
371 cause,
372 });
373 return Effect.logError(error.message).pipe(
374 Effect.annotateLogs({
375 error: Redacted.make(error, { label: error._tag }),
376 "error.type": error._tag,
377 "error.apns_error_tag": error.apnsErrorTag,
378 ...(error.requestStage === null ? {} : { "error.request_stage": error.requestStage }),
379 ...(error.stack === undefined ? {} : { "error.stack": error.stack }),
380 "relay.mobile.device_id": error.deviceId,
381 "relay.delivery.kind": error.kind,
382 ...(error.sourceJobId === null ? {} : { "relay.delivery.job_id": error.sourceJobId }),
383 }),
384 Effect.as({
385 ok: false,
386 status: 0,
387 reason: cause.message,
388 apnsId: null,
389 }),
390 );
391 };
392
393 interface LiveActivityDeliveryTarget {
394 readonly user_id: string;
395 readonly device_id: string;
396 readonly bundle_id?: string | null;
397 readonly aps_environment?: "sandbox" | "production" | null;
398 }
399
400 // Bundle ids and APS environments vary by build; route per device to avoid DeviceTokenNotForTopic/BadDeviceToken.
401 function credentialsForTarget(
402 credentials: RelayConfiguration.ApnsCredentials,
403 target: LiveActivityDeliveryTarget,
404 ): RelayConfiguration.ApnsCredentials {
405 return {
406 ...credentials,
407 ...(target.bundle_id ? { bundleId: target.bundle_id } : {}),
408 ...(target.aps_environment ? { environment: target.aps_environment } : {}),
409 };
410 }
411
412 function expectedCurrentToken(input: {
413 readonly target: LiveActivities.TargetRow;
414 readonly kind: Exclude<RelayDeliveryKind, "live_activity_start">;
415 }): string | null {
416 switch (input.kind) {
417 case "live_activity_update":
418 case "live_activity_end":
419 return input.target.activity_push_token;
420 case "push_notification":
421 return input.target.push_token;
422 }
423 }
424
425 interface SendLiveActivityDeliveryInputBase {
426 readonly target: LiveActivityDeliveryTarget;
427 readonly token: string;
428 readonly sourceJobId: string;
429 }
430
431 export type SendLiveActivityDeliveryInput =
432 | (SendLiveActivityDeliveryInputBase & {
433 readonly kind: "live_activity_update";
434 readonly aggregate: RelayAgentActivityAggregateState;
435 readonly alert?: ApnsLiveActivityAlert | null;
436 })
437 | (SendLiveActivityDeliveryInputBase & {
438 readonly kind: "live_activity_end";
439 readonly aggregate: RelayAgentActivityAggregateState | null;
440 readonly alert?: ApnsLiveActivityAlert | null;
441 });
442
443 function makeLiveActivityDeliveryRequest(
444 apns: Apns.ApnsClient["Service"],
445 input: SendLiveActivityDeliveryInput,
446 now: DateTime.DateTime,
447 ) {
448 const epochSeconds = Math.floor(now.epochMilliseconds / 1_000);
449 const base = {
450 token: input.token,
451 nowEpochSeconds: epochSeconds,
452 nowIso: DateTime.formatIso(now),
453 };
454 switch (input.kind) {
455 case "live_activity_update":
456 return {
457 epochSeconds,
458 iso: base.nowIso,
459 request: apns.makeLiveActivityRequest({
460 ...base,
461 event: deliveryEvent(input.kind),
462 state: input.aggregate,
463 alert: input.alert ?? null,
464 }),
465 };
466 case "live_activity_end":
467 return {
468 epochSeconds,
469 iso: base.nowIso,
470 request: apns.makeLiveActivityRequest({
471 ...base,
472 event: "end",
473 state: input.aggregate,
474 alert: input.alert ?? null,
475 }),
476 };
477 }
478 }
479
480 export class ApnsDeliveries extends Context.Service<
481 ApnsDeliveries,
482 {
483 readonly sendForTarget: (input: {
484 readonly target: LiveActivities.TargetRow;
485 readonly aggregate: RelayAgentActivityAggregateState | null;
486 readonly state?: RelayAgentActivityState | null;
487 readonly nowMs: number;
488 readonly replay?: boolean;
489 }) => Effect.Effect<RelayDeliveryResult | null, ApnsDeliveryError>;
490 readonly sendLiveActivity: (
491 input: SendLiveActivityDeliveryInput,
492 ) => Effect.Effect<RelayDeliveryResult, ApnsDeliveryError>;
493 readonly processSignedJob: (
494 body: unknown,
495 ) => Effect.Effect<RelayDeliveryResult, ApnsDeliveryError>;
496 readonly processBatch: (
497 stream: Stream.Stream<{ readonly body: unknown }>,
498 ) => Effect.Effect<void, Arr.NonEmptyArray<ApnsDeliveryError>>;
499 readonly sendPushNotification: (input: {
500 readonly target: LiveActivityDeliveryTarget;
501 readonly token: string;
502 readonly sourceJobId: string;
503 readonly notification: ApnsNotificationPayload;
504 }) => Effect.Effect<RelayDeliveryResult, ApnsDeliveryError>;
505 }
506 >()("t3code-relay/agentActivity/ApnsDeliveries") {}
507
508 const make = Effect.gen(function* () {
509 const attempts = yield* DeliveryAttempts.DeliveryAttempts;
510 const liveActivities = yield* LiveActivities.LiveActivities;
511 const deliveryQueue = yield* ApnsDeliveryQueue.ApnsDeliveryQueue;
512 const config = yield* RelayConfiguration.RelayConfiguration;
513 const mac = yield* Mac.Mac;
514 const apns = yield* Apns.ApnsClient;
515 const activityRows = yield* AgentActivityRows.AgentActivityRows;
516
517 const userHasContentToShow = Effect.fnUntraced(function* (userId: string) {
518 const now = yield* DateTime.now;
519 return yield* activityRows.listForUser({ userId }).pipe(
520 Effect.map(
521 (activeStates) =>
522 makeAggregateState({
523 activeStates,
524 terminalState: null,
525 nowMs: now.epochMilliseconds,
526 }) !== null,
527 ),
528 // Keep the card on persistence failures; a later sweep can retry.
529 Effect.catch((error) =>
530 Effect.logWarning("Content recheck failed; keeping the card", { error }).pipe(
531 Effect.as(true),
532 ),
533 ),
534 );
535 });
536
537 const stateIdentityIsCurrent = Effect.fnUntraced(function* (input: {
538 readonly userId: string;
539 readonly environmentId: string;
540 readonly threadId: string;
541 readonly phase: RelayAgentActivityAggregateState["activities"][number]["phase"];
542 readonly updatedAt: string;
543 }) {
544 return yield* activityRows
545 .getForUserThread({
546 userId: input.userId,
547 environmentId: input.environmentId,
548 threadId: input.threadId,
549 })
550 .pipe(
551 Effect.map(
552 (current) =>
553 current !== null &&
554 current.phase === input.phase &&
555 current.updatedAt === input.updatedAt,
556 ),
557 // Fail closed: this recheck is what stops a transferred environment's
558 // content from reaching the previous owner's devices.
559 Effect.catchCause((cause) =>
560 Effect.logWarning("agent-activity state recheck failed; skipping queued delivery", {
561 cause,
562 environmentId: input.environmentId,
563 threadId: input.threadId,
564 }).pipe(Effect.as(false)),
565 ),
566 );
567 });
568
569 const aggregateRowsAreCurrent = Effect.fnUntraced(function* (input: {
570 readonly userId: string;
571 readonly aggregate: RelayAgentActivityAggregateState;
572 }) {
573 return yield* activityRows.listForUser({ userId: input.userId }).pipe(
574 Effect.map((currentStates) => {
575 const currentByThread = new Map(
576 currentStates.map((current) => [
577 `${current.environmentId}\u0000${current.threadId}`,
578 current,
579 ]),
580 );
581 return input.aggregate.activities.every((row) => {
582 const current = currentByThread.get(`${row.environmentId}\u0000${row.threadId}`);
583 return (
584 current !== undefined &&
585 current.phase === row.phase &&
586 current.updatedAt === row.updatedAt
587 );
588 });
589 }),
590 Effect.catchCause((cause) =>
591 Effect.logWarning("agent-activity aggregate recheck failed; skipping queued delivery", {
592 cause,
593 userId: input.userId,
594 }).pipe(Effect.as(false)),
595 ),
596 );
597 });
598
599 const notificationStateIsCurrent = Effect.fnUntraced(function* (input: {
600 readonly userId: string;
601 readonly notification: ApnsNotificationPayload;
602 }) {
603 if (input.notification.phase === undefined || input.notification.updatedAt === undefined) {
604 return true;
605 }
606 return yield* stateIdentityIsCurrent({
607 userId: input.userId,
608 environmentId: input.notification.environmentId,
609 threadId: input.notification.threadId,
610 phase: input.notification.phase,
611 updatedAt: input.notification.updatedAt,
612 });
613 });
614
615 const currentSignedJobTarget = Effect.fnUntraced(function* (input: {
616 readonly target: LiveActivityDeliveryTarget;
617 readonly kind: Exclude<RelayDeliveryKind, "live_activity_start">;
618 readonly token: string;
619 }) {
620 return yield* liveActivities.listTargets({ userId: input.target.user_id }).pipe(
621 Effect.map((targets) => {
622 const currentTarget = targets.find((row) => row.device_id === input.target.device_id);
623 return currentTarget &&
624 expectedCurrentToken({ target: currentTarget, kind: input.kind }) === input.token
625 ? currentTarget
626 : null;
627 }),
628 );
629 });
630
631 const sendLiveActivity: ApnsDeliveries["Service"]["sendLiveActivity"] = Effect.fn(
632 "relay.apns_deliveries.send_live_activity",
633 )(function* (input) {
634 if (!config.apns) {
635 return {
636 deviceId: input.target.device_id,
637 kind: input.kind,
638 ok: false,
639 apnsStatus: null,
640 apnsReason: "APNs is disabled for this relay.",
641 apnsId: null,
642 };
643 }
644 yield* Effect.annotateCurrentSpan({
645 "relay.mobile.device_id": input.target.device_id,
646 "relay.delivery.kind": input.kind,
647 ...(input.sourceJobId ? { "relay.delivery.job_id": input.sourceJobId } : {}),
648 });
649 let deliveryTarget = input.target;
650 const now = yield* DateTime.now;
651 const aggregate =
652 input.aggregate === null ? null : sanitizeAgentActivityAggregateState(input.aggregate);
653 let alert = input.alert ?? null;
654 const recoverTransportError = (cause: Apns.ApnsError) =>
655 recoverApnsDeliveryTransportError(
656 {
657 deviceId: input.target.device_id,
658 kind: input.kind,
659 sourceJobId: input.sourceJobId ?? null,
660 },
661 cause,
662 );
663 const claim = yield* attempts.claimSourceJob({
664 userId: input.target.user_id,
665 environmentId: null,
666 threadId: null,
667 deviceId: input.target.device_id,
668 kind: input.kind,
669 sourceJobId: input.sourceJobId,
670 token: input.token,
671 });
672 if (claim === "completed") {
673 return duplicateJobResult({ deviceId: input.target.device_id, kind: input.kind });
674 }
675 if (claim === "in_flight") {
676 return yield* new ApnsDeliveryJobClaimInFlight({ sourceJobId: input.sourceJobId });
677 }
678 const currentTarget = yield* currentSignedJobTarget({
679 target: input.target,
680 kind: input.kind,
681 token: input.token,
682 });
683 if (!currentTarget) {
684 yield* attempts.completeSourceJob({
685 sourceJobId: input.sourceJobId,
686 apnsReason: "Stale APNs delivery job skipped.",
687 });
688 return staleJobResult({ deviceId: input.target.device_id, kind: input.kind });
689 }
690 deliveryTarget = currentTarget;
691 if (alert) {
692 const preferences = parsePreferences(currentTarget.preferences_json);
693 const previousAggregate = parseAggregate(currentTarget.last_aggregate_json);
694 alert =
695 !preferences?.notificationsEnabled || !aggregate
696 ? null
697 : (alertForAttentionTransition({
698 previousAggregate,
699 nextAggregate: aggregate,
700 preferences,
701 }) ??
702 alertForNewlyTerminal({
703 previousAggregate,
704 nextAggregate: aggregate,
705 preferences,
706 nowMs: now.epochMilliseconds,
707 includeUnobserved: true,
708 }));
709 }
710 if (
711 aggregate !== null &&
712 !(yield* aggregateRowsAreCurrent({
713 userId: input.target.user_id,
714 aggregate,
715 }))
716 ) {
717 yield* attempts.completeSourceJob({
718 sourceJobId: input.sourceJobId,
719 apnsReason: "Stale agent activity state skipped.",
720 });
721 return staleJobResult({ deviceId: input.target.device_id, kind: input.kind });
722 }
723 // A contentless end must not retire a card that newer work now owns. An
724 // end for a device that turned Live Activities off still goes out.
725 if (
726 input.kind === "live_activity_end" &&
727 aggregate === null &&
728 parsePreferences(currentTarget.preferences_json)?.liveActivitiesEnabled !== false &&
729 (yield* userHasContentToShow(input.target.user_id))
730 ) {
731 yield* attempts.completeSourceJob({
732 sourceJobId: input.sourceJobId,
733 apnsReason: "Stale APNs end job skipped.",
734 });
735 return staleJobResult({ deviceId: input.target.device_id, kind: input.kind });
736 }
737 const { epochSeconds, iso, request } = makeLiveActivityDeliveryRequest(
738 apns,
739 { ...input, aggregate, alert } as SendLiveActivityDeliveryInput,
740 now,
741 );
742 const result = yield* apns
743 .sendLiveActivityRequest({
744 credentials: credentialsForTarget(config.apns, deliveryTarget),
745 request,
746 issuedAtUnixSeconds: epochSeconds,
747 })
748 .pipe(
749 Effect.catchTags({
750 ApnsJwtEncodingError: recoverTransportError,
751 ApnsJwtSigningError: recoverTransportError,
752 ApnsHttpRequestError: recoverTransportError,
753 }),
754 );
755 if (result.ok) {
756 yield* liveActivities.markDelivery({
757 userId: input.target.user_id,
758 deviceId: input.target.device_id,
759 kind: input.kind,
760 aggregate,
761 deliveredAt: iso,
762 });
763 } else if (isPermanentApnsTokenFailure(result)) {
764 yield* liveActivities.invalidateDeliveryToken({
765 userId: input.target.user_id,
766 deviceId: input.target.device_id,
767 kind: input.kind,
768 invalidatedAt: iso,
769 });
770 }
771 yield* attempts.completeSourceJob({
772 sourceJobId: input.sourceJobId,
773 ...deliveryAttemptOutcome(result),
774 });
775 return {
776 deviceId: input.target.device_id,
777 kind: input.kind,
778 ok: result.ok,
779 apnsStatus: result.status === 0 ? null : result.status,
780 apnsReason: result.reason ?? null,
781 apnsId: result.apnsId,
782 };
783 });
784
785 const sendPushNotification: ApnsDeliveries["Service"]["sendPushNotification"] = Effect.fn(
786 "relay.apns_deliveries.send_push_notification",
787 )(function* (input) {
788 if (!config.apns) {
789 return {
790 deviceId: input.target.device_id,
791 kind: "push_notification",
792 ok: false,
793 apnsStatus: null,
794 apnsReason: "APNs is disabled for this relay.",
795 apnsId: null,
796 };
797 }
798 yield* Effect.annotateCurrentSpan({
799 "relay.mobile.device_id": input.target.device_id,
800 "relay.delivery.kind": "push_notification",
801 ...(input.sourceJobId ? { "relay.delivery.job_id": input.sourceJobId } : {}),
802 });
803 let deliveryTarget = input.target;
804 const now = yield* DateTime.now;
805 const epochSeconds = Math.floor(now.epochMilliseconds / 1_000);
806 const notification = sanitizeApnsNotificationPayload(input.notification);
807 yield* Effect.annotateCurrentSpan({
808 "relay.environment_id": notification.environmentId,
809 "relay.thread_id": notification.threadId,
810 });
811 const request = apns.makePushNotificationRequest({
812 token: input.token,
813 notification,
814 });
815 const recoverTransportError = (cause: Apns.ApnsError) =>
816 recoverApnsDeliveryTransportError(
817 {
818 deviceId: input.target.device_id,
819 kind: "push_notification",
820 sourceJobId: input.sourceJobId ?? null,
821 },
822 cause,
823 );
824 const claim = yield* attempts.claimSourceJob({
825 userId: input.target.user_id,
826 environmentId: notification.environmentId,
827 threadId: notification.threadId,
828 deviceId: input.target.device_id,
829 kind: "push_notification",
830 sourceJobId: input.sourceJobId,
831 token: input.token,
832 });
833 if (claim === "completed") {
834 return duplicateJobResult({
835 deviceId: input.target.device_id,
836 kind: "push_notification",
837 });
838 }
839 if (claim === "in_flight") {
840 return yield* new ApnsDeliveryJobClaimInFlight({ sourceJobId: input.sourceJobId });
841 }
842 const currentTarget = yield* currentSignedJobTarget({
843 target: input.target,
844 kind: "push_notification",
845 token: input.token,
846 });
847 if (!currentTarget) {
848 yield* attempts.completeSourceJob({
849 sourceJobId: input.sourceJobId,
850 apnsReason: "Stale APNs delivery job skipped.",
851 });
852 return staleJobResult({
853 deviceId: input.target.device_id,
854 kind: "push_notification",
855 });
856 }
857 if (
858 !(yield* notificationStateIsCurrent({
859 userId: input.target.user_id,
860 notification,
861 }))
862 ) {
863 yield* attempts.completeSourceJob({
864 sourceJobId: input.sourceJobId,
865 apnsReason: "Stale agent activity state skipped.",
866 });
867 return staleJobResult({
868 deviceId: input.target.device_id,
869 kind: "push_notification",
870 });
871 }
872 deliveryTarget = currentTarget;
873 const preferences = parsePreferences(currentTarget.preferences_json);
874 const alertAllowed =
875 notification.phase !== undefined && notification.updatedAt !== undefined
876 ? shouldAlertForActivity({
877 ...notification,
878 phase: notification.phase,
879 updatedAt: notification.updatedAt,
880 preferences,
881 nowMs: now.epochMilliseconds,
882 })
883 : preferences?.notificationsEnabled === true;
884 if (!alertAllowed) {
885 yield* attempts.completeSourceJob({
886 sourceJobId: input.sourceJobId,
887 apnsReason: "Notification is disabled or no longer fresh.",
888 });
889 return staleJobResult({ deviceId: input.target.device_id, kind: "push_notification" });
890 }
891 const result = yield* apns
892 .sendPushNotificationRequest({
893 credentials: credentialsForTarget(config.apns, deliveryTarget),
894 request,
895 issuedAtUnixSeconds: epochSeconds,
896 })
897 .pipe(
898 Effect.catchTags({
899 ApnsJwtEncodingError: recoverTransportError,
900 ApnsJwtSigningError: recoverTransportError,
901 ApnsHttpRequestError: recoverTransportError,
902 }),
903 );
904 if (isPermanentApnsTokenFailure(result)) {
905 yield* liveActivities.invalidateDeliveryToken({
906 userId: input.target.user_id,
907 deviceId: input.target.device_id,
908 kind: "push_notification",
909 invalidatedAt: DateTime.formatIso(now),
910 });
911 }
912 yield* attempts.completeSourceJob({
913 sourceJobId: input.sourceJobId,
914 ...deliveryAttemptOutcome(result),
915 });
916 return {
917 deviceId: input.target.device_id,
918 kind: "push_notification" as const,
919 ok: result.ok,
920 apnsStatus: result.status === 0 ? null : result.status,
921 apnsReason: result.reason ?? null,
922 apnsId: result.apnsId,
923 };
924 });
925
926 const processSignedJob: ApnsDeliveries["Service"]["processSignedJob"] = Effect.fn(
927 "relay.apns_deliveries.process_signed_job",
928 )(function* (body) {
929 const signedJob = yield* decodeSignedApnsDeliveryJob(body).pipe(
930 Effect.mapError(
931 (cause) =>
932 new ApnsDeliveryJobQueuePayloadInvalid({
933 receivedType: Array.isArray(body) ? "array" : body === null ? "null" : typeof body,
934 cause,
935 }),
936 ),
937 );
938 const now = yield* DateTime.now;
939 const payload = yield* verifySignedApnsDeliveryJob({
940 secret: config.apnsDeliveryJobSigningSecret,
941 job: signedJob,
942 nowMs: now.epochMilliseconds,
943 }).pipe(Effect.provideService(Mac.Mac, mac));
944 yield* Effect.annotateCurrentSpan({
945 "relay.mobile.device_id": payload.target.deviceId,
946 "relay.delivery.kind": payload.kind,
947 "relay.delivery.job_id": payload.jobId,
948 });
949 return yield* Effect.suspend(() => {
950 switch (payload.kind) {
951 case "live_activity_update":
952 if (payload.aggregate === null) {
953 return Effect.fail(
954 new ApnsDeliveryJobLiveActivityAggregateMissing({
955 jobId: payload.jobId,
956 kind: payload.kind,
957 userId: payload.target.userId,
958 deviceId: payload.target.deviceId,
959 }),
960 );
961 }
962 return sendLiveActivity({
963 target: {
964 user_id: payload.target.userId,
965 device_id: payload.target.deviceId,
966 bundle_id: payload.target.bundleId ?? null,
967 aps_environment: payload.target.apsEnvironment ?? null,
968 },
969 token: payload.target.token,
970 sourceJobId: payload.jobId,
971 kind: payload.kind,
972 aggregate: payload.aggregate,
973 alert: payload.alert ?? null,
974 });
975 case "live_activity_end":
976 return sendLiveActivity({
977 target: {
978 user_id: payload.target.userId,
979 device_id: payload.target.deviceId,
980 bundle_id: payload.target.bundleId ?? null,
981 aps_environment: payload.target.apsEnvironment ?? null,
982 },
983 token: payload.target.token,
984 sourceJobId: payload.jobId,
985 kind: payload.kind,
986 aggregate: payload.aggregate,
987 alert: payload.alert ?? null,
988 });
989 case "push_notification":
990 if (payload.notification === null) {
991 return Effect.fail(
992 new ApnsDeliveryJobPushNotificationMissing({
993 jobId: payload.jobId,
994 userId: payload.target.userId,
995 deviceId: payload.target.deviceId,
996 }),
997 );
998 }
999 return sendPushNotification({
1000 target: {
1001 user_id: payload.target.userId,
1002 device_id: payload.target.deviceId,
1003 bundle_id: payload.target.bundleId ?? null,
1004 aps_environment: payload.target.apsEnvironment ?? null,
1005 },
1006 token: payload.target.token,
1007 sourceJobId: payload.jobId,
1008 notification: payload.notification,
1009 });
1010 }
1011 }).pipe(withSpanAttributes({ "user.id": payload.target.userId }));
1012 });
1013
1014 const processBatch: ApnsDeliveries["Service"]["processBatch"] = Effect.fn(
1015 "relay.apn_delivery_queue.process_batch",
1016 )(function* (stream) {
1017 const messages = yield* Stream.runCollect(stream);
1018 const groups = Arr.groupBy(messages, (message) => {
1019 const job = decodeSignedApnsDeliveryJobOption(message.body);
1020 return Option.isSome(job)
1021 ? JSON.stringify([job.value.payload.target.userId, job.value.payload.target.deviceId])
1022 : "";
1023 });
1024 // Finish other devices before surfacing failures so their in-flight sends aren't cancelled.
1025 yield* Effect.validate(
1026 Object.values(groups),
1027 (group) =>
1028 Effect.forEach(
1029 group,
1030 (message) =>
1031 processSignedJob(message.body).pipe(
1032 Effect.withSpan("relay.apn_delivery_queue.process_message"),
1033 ),
1034 { discard: true },
1035 ),
1036 { concurrency: 10, discard: true },
1037 );
1038 });
1039
1040 return ApnsDeliveries.of({
1041 sendLiveActivity,
1042 sendPushNotification,
1043 processSignedJob,
1044 processBatch,
1045 sendForTarget: Effect.fnUntraced(function* (input) {
1046 if (!config.apns) return null;
1047 const delivery = chooseDelivery(input);
1048 if (!delivery) {
1049 return null;
1050 }
1051 if (delivery.kind === "push_notification") {
1052 const result = yield* deliveryQueue.enqueuePushNotification({
1053 userId: input.target.user_id,
1054 deviceId: input.target.device_id,
1055 token: delivery.token,
1056 bundleId: input.target.bundle_id,
1057 apsEnvironment: input.target.aps_environment,
1058 notification: delivery.notification,
1059 });
1060 return result;
1061 }
1062 const notification = input.replay ? null : notificationForState(input);
1063 // Use the end alert only when no companion notification will ring, preserving feedback for Live-Activity-only users.
1064 const alert = input.replay
1065 ? null
1066 : delivery.kind === "live_activity_end"
1067 ? notification && input.target.push_token
1068 ? null
1069 : alertForTerminalAggregate({
1070 aggregate: delivery.aggregate,
1071 preferences: parsePreferences(input.target.preferences_json),
1072 })
1073 : delivery.alert;
1074 const result = yield* deliveryQueue.enqueueLiveActivity({
1075 userId: input.target.user_id,
1076 deviceId: input.target.device_id,
1077 kind: delivery.kind,
1078 token: delivery.token,
1079 bundleId: input.target.bundle_id,
1080 apsEnvironment: input.target.aps_environment,
1081 aggregate: delivery.aggregate,
1082 alert,
1083 });
1084 if (delivery.kind === "live_activity_end" && notification && input.target.push_token) {
1085 yield* deliveryQueue.enqueuePushNotification({
1086 userId: input.target.user_id,
1087 deviceId: input.target.device_id,
1088 token: input.target.push_token,
1089 bundleId: input.target.bundle_id,
1090 apsEnvironment: input.target.aps_environment,
1091 notification,
1092 });
1093 }
1094 return result;
1095 }),
1096 });
1097 });
1098
1099 export const layer = Layer.effect(ApnsDeliveries, make);