mirror of
https://github.com/hanzoai/dev-desktop.git
synced 2026-11-22 00:14:30 +00:00
1100 lines
37 KiB
TypeScript
1100 lines
37 KiB
TypeScript
import type {
|
|
RelayAgentActivityAggregateState,
|
|
RelayAgentActivityState,
|
|
RelayAgentAwarenessPreferences,
|
|
RelayDeliveryKind,
|
|
RelayDeliveryResult,
|
|
} from "@hanzodev/contracts/relay";
|
|
import {
|
|
RelayAgentActivityAggregateState as RelayAgentActivityAggregateStateSchema,
|
|
RelayAgentAwarenessPreferences as RelayAgentAwarenessPreferencesSchema,
|
|
RelayDeliveryKind as RelayDeliveryKindSchema,
|
|
} from "@hanzodev/contracts/relay";
|
|
import * as Arr from "effect/Array";
|
|
import * as Context from "effect/Context";
|
|
import * as DateTime from "effect/DateTime";
|
|
import * as Mac from "effect/crypto/Mac";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Layer from "effect/Layer";
|
|
import * as Option from "effect/Option";
|
|
import * as Redacted from "effect/Redacted";
|
|
import * as Schema from "effect/Schema";
|
|
import * as Stream from "effect/Stream";
|
|
|
|
import {
|
|
isExpiredAgentActivityState,
|
|
notificationForActivity,
|
|
sanitizeAgentActivityAggregateState,
|
|
sanitizeApnsNotificationPayload,
|
|
} from "./agentActivityPayloads.ts";
|
|
import { makeAggregateState, statusForPhase } from "./agentActivityAggregate.ts";
|
|
import * as Apns from "./ApnsClient.ts";
|
|
import {
|
|
ApnsDeliveryJobLiveActivityAggregateMissing,
|
|
ApnsDeliveryJobPushNotificationMissing,
|
|
ApnsDeliveryJobQueuePayloadInvalid,
|
|
type ApnsLiveActivityAlert,
|
|
type ApnsNotificationPayload,
|
|
SignedApnsDeliveryJob,
|
|
verifySignedApnsDeliveryJob,
|
|
type ApnsDeliveryJobVerificationError,
|
|
} from "./apnsDeliveryJobs.ts";
|
|
import * as AgentActivityRows from "./AgentActivityRows.ts";
|
|
import * as DeliveryAttempts from "./DeliveryAttempts.ts";
|
|
import * as LiveActivities from "./LiveActivities.ts";
|
|
import * as RelayConfiguration from "../Config.ts";
|
|
import * as ApnsDeliveryQueue from "./ApnsDeliveryQueue.ts";
|
|
import { withSpanAttributes } from "../observability.ts";
|
|
|
|
import {
|
|
alertForAttentionTransition,
|
|
alertForNewlyTerminal,
|
|
alertForTerminalAggregate,
|
|
rowPhasesChanged,
|
|
shouldAlertForActivity,
|
|
} from "./agentActivityAlerts.ts";
|
|
const MIN_LIVE_ACTIVITY_UPDATE_INTERVAL_MS = 15_000;
|
|
const FRESHLY_ARMED_GRACE_MS = 2 * 60 * 1_000;
|
|
const PERMANENT_APNS_TOKEN_REASONS = new Set([
|
|
"BadDeviceToken",
|
|
"DeviceTokenNotForTopic",
|
|
"Unregistered",
|
|
]);
|
|
|
|
type LiveActivityDeliveryKind = Extract<
|
|
RelayDeliveryKind,
|
|
"live_activity_update" | "live_activity_end"
|
|
>;
|
|
|
|
type ChosenLiveActivityDelivery =
|
|
| {
|
|
readonly kind: "live_activity_update";
|
|
readonly token: string;
|
|
readonly aggregate: RelayAgentActivityAggregateState;
|
|
readonly alert: ApnsLiveActivityAlert | null;
|
|
}
|
|
| {
|
|
readonly kind: "live_activity_end";
|
|
readonly token: string;
|
|
readonly aggregate: RelayAgentActivityAggregateState | null;
|
|
readonly alert: ApnsLiveActivityAlert | null;
|
|
};
|
|
|
|
type ChosenPushNotificationDelivery = {
|
|
readonly kind: "push_notification";
|
|
readonly token: string;
|
|
readonly notification: ApnsNotificationPayload;
|
|
};
|
|
|
|
type ChosenDelivery = ChosenLiveActivityDelivery | ChosenPushNotificationDelivery;
|
|
|
|
export type ApnsDeliveryError =
|
|
| ApnsDeliveryQueue.ApnsDeliveryQueueError
|
|
| ApnsDeliveryJobVerificationError
|
|
| ApnsDeliveryJobClaimInFlight
|
|
| DeliveryAttempts.DeliveryAttemptRecordPersistenceError
|
|
| LiveActivities.LiveActivityTargetListPersistenceError
|
|
| LiveActivities.LiveActivityDeliveryMarkPersistenceError;
|
|
|
|
export class ApnsDeliveryJobClaimInFlight extends Schema.TaggedError<ApnsDeliveryJobClaimInFlight>()(
|
|
"ApnsDeliveryJobClaimInFlight",
|
|
{
|
|
sourceJobId: Schema.String,
|
|
},
|
|
) {
|
|
override get message(): string {
|
|
return `APNs delivery job '${this.sourceJobId}' is already in flight`;
|
|
}
|
|
}
|
|
|
|
export class ApnsDeliveryTransportError extends Schema.TaggedError<ApnsDeliveryTransportError>()(
|
|
"ApnsDeliveryTransportError",
|
|
{
|
|
deviceId: Schema.String,
|
|
kind: RelayDeliveryKindSchema,
|
|
sourceJobId: Schema.NullOr(Schema.String),
|
|
apnsErrorTag: Schema.Literals([
|
|
"ApnsJwtEncodingError",
|
|
"ApnsJwtSigningError",
|
|
"ApnsHttpRequestError",
|
|
]),
|
|
requestStage: Schema.NullOr(Schema.Literals(["send", "read-response"])),
|
|
cause: Schema.Defect(),
|
|
},
|
|
) {
|
|
override get message(): string {
|
|
return `APNs ${this.kind} delivery failed for device ${this.deviceId}.`;
|
|
}
|
|
}
|
|
|
|
const decodeRelayAgentActivityAggregateStateJson = Schema.decodeUnknownOption(
|
|
Schema.fromJsonString(RelayAgentActivityAggregateStateSchema),
|
|
);
|
|
const decodeRelayAgentAwarenessPreferencesJson = Schema.decodeUnknownOption(
|
|
Schema.fromJsonString(RelayAgentAwarenessPreferencesSchema),
|
|
);
|
|
const decodeSignedApnsDeliveryJob = Schema.decodeUnknownEffect(SignedApnsDeliveryJob);
|
|
const decodeSignedApnsDeliveryJobOption = Schema.decodeUnknownOption(SignedApnsDeliveryJob);
|
|
|
|
function parseAggregate(value: string | null): RelayAgentActivityAggregateState | null {
|
|
if (!value) {
|
|
return null;
|
|
}
|
|
return Option.getOrNull(decodeRelayAgentActivityAggregateStateJson(value));
|
|
}
|
|
|
|
function parsePreferences(value: string): RelayAgentAwarenessPreferences | null {
|
|
return Option.getOrNull(decodeRelayAgentAwarenessPreferencesJson(value));
|
|
}
|
|
|
|
function aggregateNeedsAttention(aggregate: RelayAgentActivityAggregateState): boolean {
|
|
return aggregate.activities.some(
|
|
(row) => row.phase === "waiting_for_approval" || row.phase === "waiting_for_input",
|
|
);
|
|
}
|
|
|
|
function shouldUpdateLiveActivity(input: {
|
|
readonly previousAggregate: RelayAgentActivityAggregateState | null;
|
|
readonly nextAggregate: RelayAgentActivityAggregateState;
|
|
readonly lastDeliveryAt: string | null;
|
|
readonly nowMs: number;
|
|
}): boolean {
|
|
if (!input.previousAggregate) {
|
|
return true;
|
|
}
|
|
if (JSON.stringify(input.previousAggregate) === JSON.stringify(input.nextAggregate)) {
|
|
return false;
|
|
}
|
|
if (input.previousAggregate.activeCount !== input.nextAggregate.activeCount) {
|
|
return true;
|
|
}
|
|
if (aggregateNeedsAttention(input.nextAggregate)) {
|
|
return true;
|
|
}
|
|
// Phase and row changes may be the last update until the next transition.
|
|
// Throttling them would leave the card showing a stale state indefinitely.
|
|
if (rowPhasesChanged(input.previousAggregate, input.nextAggregate)) {
|
|
return true;
|
|
}
|
|
const lastDeliveryAtMs =
|
|
input.lastDeliveryAt === null
|
|
? null
|
|
: Option.match(DateTime.make(input.lastDeliveryAt), {
|
|
onNone: () => Number.NaN,
|
|
onSome: (dt) => dt.epochMilliseconds,
|
|
});
|
|
return (
|
|
lastDeliveryAtMs === null ||
|
|
Number.isNaN(lastDeliveryAtMs) ||
|
|
input.nowMs - lastDeliveryAtMs >= MIN_LIVE_ACTIVITY_UPDATE_INTERVAL_MS
|
|
);
|
|
}
|
|
|
|
// Aggregate rows may belong to unrelated threads; fallback pushes use the published state.
|
|
function notificationForState(input: {
|
|
readonly target: LiveActivities.TargetRow;
|
|
readonly state?: RelayAgentActivityState | null;
|
|
readonly nowMs: number;
|
|
}): ApnsNotificationPayload | null {
|
|
const state = input.state;
|
|
if (!input.target.push_token || !state || isExpiredAgentActivityState(state, input.nowMs)) {
|
|
return null;
|
|
}
|
|
const preferences = parsePreferences(input.target.preferences_json);
|
|
if (!shouldAlertForActivity({ ...state, preferences, nowMs: input.nowMs })) return null;
|
|
return notificationForActivity({ ...state, status: statusForPhase(state.phase) });
|
|
}
|
|
|
|
// Suppressed Live Activity updates must not fall back to alert pushes or unchanged replays would ring.
|
|
function chooseLiveActivityDelivery(input: {
|
|
readonly target: LiveActivities.TargetRow;
|
|
readonly aggregate: RelayAgentActivityAggregateState | null;
|
|
readonly nowMs: number;
|
|
readonly replay?: boolean;
|
|
}): ChosenLiveActivityDelivery | "suppressed" | null {
|
|
const preferences = parsePreferences(input.target.preferences_json);
|
|
if (preferences?.liveActivitiesEnabled === false) {
|
|
return input.target.activity_push_token
|
|
? {
|
|
kind: "live_activity_end",
|
|
token: input.target.activity_push_token,
|
|
aggregate: null,
|
|
alert: null,
|
|
}
|
|
: null;
|
|
}
|
|
if (!input.target.activity_push_token) {
|
|
return null;
|
|
}
|
|
if (input.aggregate === null) {
|
|
// Registration replay can precede a new thread's first publish; preserve a freshly armed card until state arrives.
|
|
const armedAtMs = Option.match(
|
|
input.target.remote_started_at === null
|
|
? Option.none()
|
|
: DateTime.make(input.target.remote_started_at),
|
|
{ onNone: () => null, onSome: (dt) => dt.epochMilliseconds },
|
|
);
|
|
if (armedAtMs !== null && input.nowMs - armedAtMs < FRESHLY_ARMED_GRACE_MS) {
|
|
return null;
|
|
}
|
|
return {
|
|
kind: "live_activity_end",
|
|
token: input.target.activity_push_token,
|
|
aggregate: null,
|
|
alert: null,
|
|
};
|
|
}
|
|
const nextAggregate = input.aggregate;
|
|
const previousAggregate = parseAggregate(input.target.last_aggregate_json);
|
|
return shouldUpdateLiveActivity({
|
|
previousAggregate,
|
|
nextAggregate,
|
|
lastDeliveryAt: input.target.last_live_activity_delivery_at,
|
|
nowMs: input.nowMs,
|
|
})
|
|
? {
|
|
kind: "live_activity_update",
|
|
token: input.target.activity_push_token,
|
|
aggregate: nextAggregate,
|
|
alert: input.replay
|
|
? null
|
|
: (alertForAttentionTransition({
|
|
previousAggregate,
|
|
nextAggregate,
|
|
preferences,
|
|
}) ??
|
|
alertForNewlyTerminal({
|
|
previousAggregate,
|
|
nextAggregate,
|
|
preferences,
|
|
nowMs: input.nowMs,
|
|
includeUnobserved: true,
|
|
})),
|
|
}
|
|
: "suppressed";
|
|
}
|
|
|
|
function chooseDelivery(input: {
|
|
readonly target: LiveActivities.TargetRow;
|
|
readonly aggregate: RelayAgentActivityAggregateState | null;
|
|
readonly state?: RelayAgentActivityState | null;
|
|
readonly nowMs: number;
|
|
readonly replay?: boolean;
|
|
}): ChosenDelivery | null {
|
|
const liveActivityDelivery = chooseLiveActivityDelivery(input);
|
|
if (liveActivityDelivery === "suppressed") {
|
|
return null;
|
|
}
|
|
if (liveActivityDelivery) {
|
|
return liveActivityDelivery;
|
|
}
|
|
const notification = input.replay ? null : notificationForState(input);
|
|
return notification && input.target.push_token
|
|
? {
|
|
kind: "push_notification",
|
|
token: input.target.push_token,
|
|
notification,
|
|
}
|
|
: null;
|
|
}
|
|
|
|
function deliveryEvent(kind: LiveActivityDeliveryKind): Apns.ApnsLiveActivityEvent {
|
|
switch (kind) {
|
|
case "live_activity_update":
|
|
return "update";
|
|
case "live_activity_end":
|
|
return "end";
|
|
}
|
|
}
|
|
|
|
function isPermanentApnsTokenFailure(result: Apns.ApnsDeliveryResult): boolean {
|
|
return (
|
|
!result.ok &&
|
|
(result.status === 410 ||
|
|
(result.status === 400 &&
|
|
result.reason !== undefined &&
|
|
PERMANENT_APNS_TOKEN_REASONS.has(result.reason)))
|
|
);
|
|
}
|
|
|
|
function duplicateJobResult(input: {
|
|
readonly deviceId: string;
|
|
readonly kind: RelayDeliveryKind;
|
|
}): RelayDeliveryResult {
|
|
return {
|
|
deviceId: input.deviceId,
|
|
kind: input.kind,
|
|
ok: true,
|
|
apnsStatus: null,
|
|
apnsReason: "Duplicate APNs delivery job skipped.",
|
|
apnsId: null,
|
|
};
|
|
}
|
|
|
|
function staleJobResult(input: {
|
|
readonly deviceId: string;
|
|
readonly kind: RelayDeliveryKind;
|
|
}): RelayDeliveryResult {
|
|
return {
|
|
deviceId: input.deviceId,
|
|
kind: input.kind,
|
|
ok: true,
|
|
apnsStatus: null,
|
|
apnsReason: "Stale APNs delivery job skipped.",
|
|
apnsId: null,
|
|
};
|
|
}
|
|
|
|
function deliveryAttemptOutcome(result: Apns.ApnsDeliveryResult) {
|
|
return {
|
|
...(result.status === 0 ? {} : { apnsStatus: result.status }),
|
|
...(result.reason === undefined ? {} : { apnsReason: result.reason }),
|
|
apnsId: result.apnsId,
|
|
...(result.status === 0 ? { transportError: result.reason ?? "APNs request failed." } : {}),
|
|
};
|
|
}
|
|
|
|
const recoverApnsDeliveryTransportError = (
|
|
input: {
|
|
readonly deviceId: string;
|
|
readonly kind: RelayDeliveryKind;
|
|
readonly sourceJobId: string | null;
|
|
},
|
|
cause: Apns.ApnsError,
|
|
): Effect.Effect<Apns.ApnsDeliveryResult> => {
|
|
const error = new ApnsDeliveryTransportError({
|
|
deviceId: input.deviceId,
|
|
kind: input.kind,
|
|
sourceJobId: input.sourceJobId,
|
|
apnsErrorTag: cause._tag,
|
|
requestStage: cause._tag === "ApnsHttpRequestError" ? cause.stage : null,
|
|
cause,
|
|
});
|
|
return Effect.logError(error.message).pipe(
|
|
Effect.annotateLogs({
|
|
error: Redacted.make(error, { label: error._tag }),
|
|
"error.type": error._tag,
|
|
"error.apns_error_tag": error.apnsErrorTag,
|
|
...(error.requestStage === null ? {} : { "error.request_stage": error.requestStage }),
|
|
...(error.stack === undefined ? {} : { "error.stack": error.stack }),
|
|
"relay.mobile.device_id": error.deviceId,
|
|
"relay.delivery.kind": error.kind,
|
|
...(error.sourceJobId === null ? {} : { "relay.delivery.job_id": error.sourceJobId }),
|
|
}),
|
|
Effect.as({
|
|
ok: false,
|
|
status: 0,
|
|
reason: cause.message,
|
|
apnsId: null,
|
|
}),
|
|
);
|
|
};
|
|
|
|
interface LiveActivityDeliveryTarget {
|
|
readonly user_id: string;
|
|
readonly device_id: string;
|
|
readonly bundle_id?: string | null;
|
|
readonly aps_environment?: "sandbox" | "production" | null;
|
|
}
|
|
|
|
// Bundle ids and APS environments vary by build; route per device to avoid DeviceTokenNotForTopic/BadDeviceToken.
|
|
function credentialsForTarget(
|
|
credentials: RelayConfiguration.ApnsCredentials,
|
|
target: LiveActivityDeliveryTarget,
|
|
): RelayConfiguration.ApnsCredentials {
|
|
return {
|
|
...credentials,
|
|
...(target.bundle_id ? { bundleId: target.bundle_id } : {}),
|
|
...(target.aps_environment ? { environment: target.aps_environment } : {}),
|
|
};
|
|
}
|
|
|
|
function expectedCurrentToken(input: {
|
|
readonly target: LiveActivities.TargetRow;
|
|
readonly kind: Exclude<RelayDeliveryKind, "live_activity_start">;
|
|
}): string | null {
|
|
switch (input.kind) {
|
|
case "live_activity_update":
|
|
case "live_activity_end":
|
|
return input.target.activity_push_token;
|
|
case "push_notification":
|
|
return input.target.push_token;
|
|
}
|
|
}
|
|
|
|
interface SendLiveActivityDeliveryInputBase {
|
|
readonly target: LiveActivityDeliveryTarget;
|
|
readonly token: string;
|
|
readonly sourceJobId: string;
|
|
}
|
|
|
|
export type SendLiveActivityDeliveryInput =
|
|
| (SendLiveActivityDeliveryInputBase & {
|
|
readonly kind: "live_activity_update";
|
|
readonly aggregate: RelayAgentActivityAggregateState;
|
|
readonly alert?: ApnsLiveActivityAlert | null;
|
|
})
|
|
| (SendLiveActivityDeliveryInputBase & {
|
|
readonly kind: "live_activity_end";
|
|
readonly aggregate: RelayAgentActivityAggregateState | null;
|
|
readonly alert?: ApnsLiveActivityAlert | null;
|
|
});
|
|
|
|
function makeLiveActivityDeliveryRequest(
|
|
apns: Apns.ApnsClient["Service"],
|
|
input: SendLiveActivityDeliveryInput,
|
|
now: DateTime.DateTime,
|
|
) {
|
|
const epochSeconds = Math.floor(now.epochMilliseconds / 1_000);
|
|
const base = {
|
|
token: input.token,
|
|
nowEpochSeconds: epochSeconds,
|
|
nowIso: DateTime.formatIso(now),
|
|
};
|
|
switch (input.kind) {
|
|
case "live_activity_update":
|
|
return {
|
|
epochSeconds,
|
|
iso: base.nowIso,
|
|
request: apns.makeLiveActivityRequest({
|
|
...base,
|
|
event: deliveryEvent(input.kind),
|
|
state: input.aggregate,
|
|
alert: input.alert ?? null,
|
|
}),
|
|
};
|
|
case "live_activity_end":
|
|
return {
|
|
epochSeconds,
|
|
iso: base.nowIso,
|
|
request: apns.makeLiveActivityRequest({
|
|
...base,
|
|
event: "end",
|
|
state: input.aggregate,
|
|
alert: input.alert ?? null,
|
|
}),
|
|
};
|
|
}
|
|
}
|
|
|
|
export class ApnsDeliveries extends Context.Service<
|
|
ApnsDeliveries,
|
|
{
|
|
readonly sendForTarget: (input: {
|
|
readonly target: LiveActivities.TargetRow;
|
|
readonly aggregate: RelayAgentActivityAggregateState | null;
|
|
readonly state?: RelayAgentActivityState | null;
|
|
readonly nowMs: number;
|
|
readonly replay?: boolean;
|
|
}) => Effect.Effect<RelayDeliveryResult | null, ApnsDeliveryError>;
|
|
readonly sendLiveActivity: (
|
|
input: SendLiveActivityDeliveryInput,
|
|
) => Effect.Effect<RelayDeliveryResult, ApnsDeliveryError>;
|
|
readonly processSignedJob: (
|
|
body: unknown,
|
|
) => Effect.Effect<RelayDeliveryResult, ApnsDeliveryError>;
|
|
readonly processBatch: (
|
|
stream: Stream.Stream<{ readonly body: unknown }>,
|
|
) => Effect.Effect<void, Arr.NonEmptyArray<ApnsDeliveryError>>;
|
|
readonly sendPushNotification: (input: {
|
|
readonly target: LiveActivityDeliveryTarget;
|
|
readonly token: string;
|
|
readonly sourceJobId: string;
|
|
readonly notification: ApnsNotificationPayload;
|
|
}) => Effect.Effect<RelayDeliveryResult, ApnsDeliveryError>;
|
|
}
|
|
>()("hanzodev-relay/agentActivity/ApnsDeliveries") {}
|
|
|
|
const make = Effect.gen(function* () {
|
|
const attempts = yield* DeliveryAttempts.DeliveryAttempts;
|
|
const liveActivities = yield* LiveActivities.LiveActivities;
|
|
const deliveryQueue = yield* ApnsDeliveryQueue.ApnsDeliveryQueue;
|
|
const config = yield* RelayConfiguration.RelayConfiguration;
|
|
const mac = yield* Mac.Mac;
|
|
const apns = yield* Apns.ApnsClient;
|
|
const activityRows = yield* AgentActivityRows.AgentActivityRows;
|
|
|
|
const userHasContentToShow = Effect.fnUntraced(function* (userId: string) {
|
|
const now = yield* DateTime.now;
|
|
return yield* activityRows.listForUser({ userId }).pipe(
|
|
Effect.map(
|
|
(activeStates) =>
|
|
makeAggregateState({
|
|
activeStates,
|
|
terminalState: null,
|
|
nowMs: now.epochMilliseconds,
|
|
}) !== null,
|
|
),
|
|
// Keep the card on persistence failures; a later sweep can retry.
|
|
Effect.catch((error) =>
|
|
Effect.logWarning("Content recheck failed; keeping the card", { error }).pipe(
|
|
Effect.as(true),
|
|
),
|
|
),
|
|
);
|
|
});
|
|
|
|
const stateIdentityIsCurrent = Effect.fnUntraced(function* (input: {
|
|
readonly userId: string;
|
|
readonly environmentId: string;
|
|
readonly threadId: string;
|
|
readonly phase: RelayAgentActivityAggregateState["activities"][number]["phase"];
|
|
readonly updatedAt: string;
|
|
}) {
|
|
return yield* activityRows
|
|
.getForUserThread({
|
|
userId: input.userId,
|
|
environmentId: input.environmentId,
|
|
threadId: input.threadId,
|
|
})
|
|
.pipe(
|
|
Effect.map(
|
|
(current) =>
|
|
current !== null &&
|
|
current.phase === input.phase &&
|
|
current.updatedAt === input.updatedAt,
|
|
),
|
|
// Fail closed: this recheck is what stops a transferred environment's
|
|
// content from reaching the previous owner's devices.
|
|
Effect.catchCause((cause) =>
|
|
Effect.logWarning("agent-activity state recheck failed; skipping queued delivery", {
|
|
cause,
|
|
environmentId: input.environmentId,
|
|
threadId: input.threadId,
|
|
}).pipe(Effect.as(false)),
|
|
),
|
|
);
|
|
});
|
|
|
|
const aggregateRowsAreCurrent = Effect.fnUntraced(function* (input: {
|
|
readonly userId: string;
|
|
readonly aggregate: RelayAgentActivityAggregateState;
|
|
}) {
|
|
return yield* activityRows.listForUser({ userId: input.userId }).pipe(
|
|
Effect.map((currentStates) => {
|
|
const currentByThread = new Map(
|
|
currentStates.map((current) => [
|
|
`${current.environmentId}\u0000${current.threadId}`,
|
|
current,
|
|
]),
|
|
);
|
|
return input.aggregate.activities.every((row) => {
|
|
const current = currentByThread.get(`${row.environmentId}\u0000${row.threadId}`);
|
|
return (
|
|
current !== undefined &&
|
|
current.phase === row.phase &&
|
|
current.updatedAt === row.updatedAt
|
|
);
|
|
});
|
|
}),
|
|
Effect.catchCause((cause) =>
|
|
Effect.logWarning("agent-activity aggregate recheck failed; skipping queued delivery", {
|
|
cause,
|
|
userId: input.userId,
|
|
}).pipe(Effect.as(false)),
|
|
),
|
|
);
|
|
});
|
|
|
|
const notificationStateIsCurrent = Effect.fnUntraced(function* (input: {
|
|
readonly userId: string;
|
|
readonly notification: ApnsNotificationPayload;
|
|
}) {
|
|
if (input.notification.phase === undefined || input.notification.updatedAt === undefined) {
|
|
return true;
|
|
}
|
|
return yield* stateIdentityIsCurrent({
|
|
userId: input.userId,
|
|
environmentId: input.notification.environmentId,
|
|
threadId: input.notification.threadId,
|
|
phase: input.notification.phase,
|
|
updatedAt: input.notification.updatedAt,
|
|
});
|
|
});
|
|
|
|
const currentSignedJobTarget = Effect.fnUntraced(function* (input: {
|
|
readonly target: LiveActivityDeliveryTarget;
|
|
readonly kind: Exclude<RelayDeliveryKind, "live_activity_start">;
|
|
readonly token: string;
|
|
}) {
|
|
return yield* liveActivities.listTargets({ userId: input.target.user_id }).pipe(
|
|
Effect.map((targets) => {
|
|
const currentTarget = targets.find((row) => row.device_id === input.target.device_id);
|
|
return currentTarget &&
|
|
expectedCurrentToken({ target: currentTarget, kind: input.kind }) === input.token
|
|
? currentTarget
|
|
: null;
|
|
}),
|
|
);
|
|
});
|
|
|
|
const sendLiveActivity: ApnsDeliveries["Service"]["sendLiveActivity"] = Effect.fn(
|
|
"relay.apns_deliveries.send_live_activity",
|
|
)(function* (input) {
|
|
if (!config.apns) {
|
|
return {
|
|
deviceId: input.target.device_id,
|
|
kind: input.kind,
|
|
ok: false,
|
|
apnsStatus: null,
|
|
apnsReason: "APNs is disabled for this relay.",
|
|
apnsId: null,
|
|
};
|
|
}
|
|
yield* Effect.annotateCurrentSpan({
|
|
"relay.mobile.device_id": input.target.device_id,
|
|
"relay.delivery.kind": input.kind,
|
|
...(input.sourceJobId ? { "relay.delivery.job_id": input.sourceJobId } : {}),
|
|
});
|
|
let deliveryTarget = input.target;
|
|
const now = yield* DateTime.now;
|
|
const aggregate =
|
|
input.aggregate === null ? null : sanitizeAgentActivityAggregateState(input.aggregate);
|
|
let alert = input.alert ?? null;
|
|
const recoverTransportError = (cause: Apns.ApnsError) =>
|
|
recoverApnsDeliveryTransportError(
|
|
{
|
|
deviceId: input.target.device_id,
|
|
kind: input.kind,
|
|
sourceJobId: input.sourceJobId ?? null,
|
|
},
|
|
cause,
|
|
);
|
|
const claim = yield* attempts.claimSourceJob({
|
|
userId: input.target.user_id,
|
|
environmentId: null,
|
|
threadId: null,
|
|
deviceId: input.target.device_id,
|
|
kind: input.kind,
|
|
sourceJobId: input.sourceJobId,
|
|
token: input.token,
|
|
});
|
|
if (claim === "completed") {
|
|
return duplicateJobResult({ deviceId: input.target.device_id, kind: input.kind });
|
|
}
|
|
if (claim === "in_flight") {
|
|
return yield* new ApnsDeliveryJobClaimInFlight({ sourceJobId: input.sourceJobId });
|
|
}
|
|
const currentTarget = yield* currentSignedJobTarget({
|
|
target: input.target,
|
|
kind: input.kind,
|
|
token: input.token,
|
|
});
|
|
if (!currentTarget) {
|
|
yield* attempts.completeSourceJob({
|
|
sourceJobId: input.sourceJobId,
|
|
apnsReason: "Stale APNs delivery job skipped.",
|
|
});
|
|
return staleJobResult({ deviceId: input.target.device_id, kind: input.kind });
|
|
}
|
|
deliveryTarget = currentTarget;
|
|
if (alert) {
|
|
const preferences = parsePreferences(currentTarget.preferences_json);
|
|
const previousAggregate = parseAggregate(currentTarget.last_aggregate_json);
|
|
alert =
|
|
!preferences?.notificationsEnabled || !aggregate
|
|
? null
|
|
: (alertForAttentionTransition({
|
|
previousAggregate,
|
|
nextAggregate: aggregate,
|
|
preferences,
|
|
}) ??
|
|
alertForNewlyTerminal({
|
|
previousAggregate,
|
|
nextAggregate: aggregate,
|
|
preferences,
|
|
nowMs: now.epochMilliseconds,
|
|
includeUnobserved: true,
|
|
}));
|
|
}
|
|
if (
|
|
aggregate !== null &&
|
|
!(yield* aggregateRowsAreCurrent({
|
|
userId: input.target.user_id,
|
|
aggregate,
|
|
}))
|
|
) {
|
|
yield* attempts.completeSourceJob({
|
|
sourceJobId: input.sourceJobId,
|
|
apnsReason: "Stale agent activity state skipped.",
|
|
});
|
|
return staleJobResult({ deviceId: input.target.device_id, kind: input.kind });
|
|
}
|
|
// A contentless end must not retire a card that newer work now owns. An
|
|
// end for a device that turned Live Activities off still goes out.
|
|
if (
|
|
input.kind === "live_activity_end" &&
|
|
aggregate === null &&
|
|
parsePreferences(currentTarget.preferences_json)?.liveActivitiesEnabled !== false &&
|
|
(yield* userHasContentToShow(input.target.user_id))
|
|
) {
|
|
yield* attempts.completeSourceJob({
|
|
sourceJobId: input.sourceJobId,
|
|
apnsReason: "Stale APNs end job skipped.",
|
|
});
|
|
return staleJobResult({ deviceId: input.target.device_id, kind: input.kind });
|
|
}
|
|
const { epochSeconds, iso, request } = makeLiveActivityDeliveryRequest(
|
|
apns,
|
|
{ ...input, aggregate, alert } as SendLiveActivityDeliveryInput,
|
|
now,
|
|
);
|
|
const result = yield* apns
|
|
.sendLiveActivityRequest({
|
|
credentials: credentialsForTarget(config.apns, deliveryTarget),
|
|
request,
|
|
issuedAtUnixSeconds: epochSeconds,
|
|
})
|
|
.pipe(
|
|
Effect.catchTags({
|
|
ApnsJwtEncodingError: recoverTransportError,
|
|
ApnsJwtSigningError: recoverTransportError,
|
|
ApnsHttpRequestError: recoverTransportError,
|
|
}),
|
|
);
|
|
if (result.ok) {
|
|
yield* liveActivities.markDelivery({
|
|
userId: input.target.user_id,
|
|
deviceId: input.target.device_id,
|
|
kind: input.kind,
|
|
aggregate,
|
|
deliveredAt: iso,
|
|
});
|
|
} else if (isPermanentApnsTokenFailure(result)) {
|
|
yield* liveActivities.invalidateDeliveryToken({
|
|
userId: input.target.user_id,
|
|
deviceId: input.target.device_id,
|
|
kind: input.kind,
|
|
invalidatedAt: iso,
|
|
});
|
|
}
|
|
yield* attempts.completeSourceJob({
|
|
sourceJobId: input.sourceJobId,
|
|
...deliveryAttemptOutcome(result),
|
|
});
|
|
return {
|
|
deviceId: input.target.device_id,
|
|
kind: input.kind,
|
|
ok: result.ok,
|
|
apnsStatus: result.status === 0 ? null : result.status,
|
|
apnsReason: result.reason ?? null,
|
|
apnsId: result.apnsId,
|
|
};
|
|
});
|
|
|
|
const sendPushNotification: ApnsDeliveries["Service"]["sendPushNotification"] = Effect.fn(
|
|
"relay.apns_deliveries.send_push_notification",
|
|
)(function* (input) {
|
|
if (!config.apns) {
|
|
return {
|
|
deviceId: input.target.device_id,
|
|
kind: "push_notification",
|
|
ok: false,
|
|
apnsStatus: null,
|
|
apnsReason: "APNs is disabled for this relay.",
|
|
apnsId: null,
|
|
};
|
|
}
|
|
yield* Effect.annotateCurrentSpan({
|
|
"relay.mobile.device_id": input.target.device_id,
|
|
"relay.delivery.kind": "push_notification",
|
|
...(input.sourceJobId ? { "relay.delivery.job_id": input.sourceJobId } : {}),
|
|
});
|
|
let deliveryTarget = input.target;
|
|
const now = yield* DateTime.now;
|
|
const epochSeconds = Math.floor(now.epochMilliseconds / 1_000);
|
|
const notification = sanitizeApnsNotificationPayload(input.notification);
|
|
yield* Effect.annotateCurrentSpan({
|
|
"relay.environment_id": notification.environmentId,
|
|
"relay.thread_id": notification.threadId,
|
|
});
|
|
const request = apns.makePushNotificationRequest({
|
|
token: input.token,
|
|
notification,
|
|
});
|
|
const recoverTransportError = (cause: Apns.ApnsError) =>
|
|
recoverApnsDeliveryTransportError(
|
|
{
|
|
deviceId: input.target.device_id,
|
|
kind: "push_notification",
|
|
sourceJobId: input.sourceJobId ?? null,
|
|
},
|
|
cause,
|
|
);
|
|
const claim = yield* attempts.claimSourceJob({
|
|
userId: input.target.user_id,
|
|
environmentId: notification.environmentId,
|
|
threadId: notification.threadId,
|
|
deviceId: input.target.device_id,
|
|
kind: "push_notification",
|
|
sourceJobId: input.sourceJobId,
|
|
token: input.token,
|
|
});
|
|
if (claim === "completed") {
|
|
return duplicateJobResult({
|
|
deviceId: input.target.device_id,
|
|
kind: "push_notification",
|
|
});
|
|
}
|
|
if (claim === "in_flight") {
|
|
return yield* new ApnsDeliveryJobClaimInFlight({ sourceJobId: input.sourceJobId });
|
|
}
|
|
const currentTarget = yield* currentSignedJobTarget({
|
|
target: input.target,
|
|
kind: "push_notification",
|
|
token: input.token,
|
|
});
|
|
if (!currentTarget) {
|
|
yield* attempts.completeSourceJob({
|
|
sourceJobId: input.sourceJobId,
|
|
apnsReason: "Stale APNs delivery job skipped.",
|
|
});
|
|
return staleJobResult({
|
|
deviceId: input.target.device_id,
|
|
kind: "push_notification",
|
|
});
|
|
}
|
|
if (
|
|
!(yield* notificationStateIsCurrent({
|
|
userId: input.target.user_id,
|
|
notification,
|
|
}))
|
|
) {
|
|
yield* attempts.completeSourceJob({
|
|
sourceJobId: input.sourceJobId,
|
|
apnsReason: "Stale agent activity state skipped.",
|
|
});
|
|
return staleJobResult({
|
|
deviceId: input.target.device_id,
|
|
kind: "push_notification",
|
|
});
|
|
}
|
|
deliveryTarget = currentTarget;
|
|
const preferences = parsePreferences(currentTarget.preferences_json);
|
|
const alertAllowed =
|
|
notification.phase !== undefined && notification.updatedAt !== undefined
|
|
? shouldAlertForActivity({
|
|
...notification,
|
|
phase: notification.phase,
|
|
updatedAt: notification.updatedAt,
|
|
preferences,
|
|
nowMs: now.epochMilliseconds,
|
|
})
|
|
: preferences?.notificationsEnabled === true;
|
|
if (!alertAllowed) {
|
|
yield* attempts.completeSourceJob({
|
|
sourceJobId: input.sourceJobId,
|
|
apnsReason: "Notification is disabled or no longer fresh.",
|
|
});
|
|
return staleJobResult({ deviceId: input.target.device_id, kind: "push_notification" });
|
|
}
|
|
const result = yield* apns
|
|
.sendPushNotificationRequest({
|
|
credentials: credentialsForTarget(config.apns, deliveryTarget),
|
|
request,
|
|
issuedAtUnixSeconds: epochSeconds,
|
|
})
|
|
.pipe(
|
|
Effect.catchTags({
|
|
ApnsJwtEncodingError: recoverTransportError,
|
|
ApnsJwtSigningError: recoverTransportError,
|
|
ApnsHttpRequestError: recoverTransportError,
|
|
}),
|
|
);
|
|
if (isPermanentApnsTokenFailure(result)) {
|
|
yield* liveActivities.invalidateDeliveryToken({
|
|
userId: input.target.user_id,
|
|
deviceId: input.target.device_id,
|
|
kind: "push_notification",
|
|
invalidatedAt: DateTime.formatIso(now),
|
|
});
|
|
}
|
|
yield* attempts.completeSourceJob({
|
|
sourceJobId: input.sourceJobId,
|
|
...deliveryAttemptOutcome(result),
|
|
});
|
|
return {
|
|
deviceId: input.target.device_id,
|
|
kind: "push_notification" as const,
|
|
ok: result.ok,
|
|
apnsStatus: result.status === 0 ? null : result.status,
|
|
apnsReason: result.reason ?? null,
|
|
apnsId: result.apnsId,
|
|
};
|
|
});
|
|
|
|
const processSignedJob: ApnsDeliveries["Service"]["processSignedJob"] = Effect.fn(
|
|
"relay.apns_deliveries.process_signed_job",
|
|
)(function* (body) {
|
|
const signedJob = yield* decodeSignedApnsDeliveryJob(body).pipe(
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new ApnsDeliveryJobQueuePayloadInvalid({
|
|
receivedType: Array.isArray(body) ? "array" : body === null ? "null" : typeof body,
|
|
cause,
|
|
}),
|
|
),
|
|
);
|
|
const now = yield* DateTime.now;
|
|
const payload = yield* verifySignedApnsDeliveryJob({
|
|
secret: config.apnsDeliveryJobSigningSecret,
|
|
job: signedJob,
|
|
nowMs: now.epochMilliseconds,
|
|
}).pipe(Effect.provideService(Mac.Mac, mac));
|
|
yield* Effect.annotateCurrentSpan({
|
|
"relay.mobile.device_id": payload.target.deviceId,
|
|
"relay.delivery.kind": payload.kind,
|
|
"relay.delivery.job_id": payload.jobId,
|
|
});
|
|
return yield* Effect.suspend(() => {
|
|
switch (payload.kind) {
|
|
case "live_activity_update":
|
|
if (payload.aggregate === null) {
|
|
return Effect.fail(
|
|
new ApnsDeliveryJobLiveActivityAggregateMissing({
|
|
jobId: payload.jobId,
|
|
kind: payload.kind,
|
|
userId: payload.target.userId,
|
|
deviceId: payload.target.deviceId,
|
|
}),
|
|
);
|
|
}
|
|
return sendLiveActivity({
|
|
target: {
|
|
user_id: payload.target.userId,
|
|
device_id: payload.target.deviceId,
|
|
bundle_id: payload.target.bundleId ?? null,
|
|
aps_environment: payload.target.apsEnvironment ?? null,
|
|
},
|
|
token: payload.target.token,
|
|
sourceJobId: payload.jobId,
|
|
kind: payload.kind,
|
|
aggregate: payload.aggregate,
|
|
alert: payload.alert ?? null,
|
|
});
|
|
case "live_activity_end":
|
|
return sendLiveActivity({
|
|
target: {
|
|
user_id: payload.target.userId,
|
|
device_id: payload.target.deviceId,
|
|
bundle_id: payload.target.bundleId ?? null,
|
|
aps_environment: payload.target.apsEnvironment ?? null,
|
|
},
|
|
token: payload.target.token,
|
|
sourceJobId: payload.jobId,
|
|
kind: payload.kind,
|
|
aggregate: payload.aggregate,
|
|
alert: payload.alert ?? null,
|
|
});
|
|
case "push_notification":
|
|
if (payload.notification === null) {
|
|
return Effect.fail(
|
|
new ApnsDeliveryJobPushNotificationMissing({
|
|
jobId: payload.jobId,
|
|
userId: payload.target.userId,
|
|
deviceId: payload.target.deviceId,
|
|
}),
|
|
);
|
|
}
|
|
return sendPushNotification({
|
|
target: {
|
|
user_id: payload.target.userId,
|
|
device_id: payload.target.deviceId,
|
|
bundle_id: payload.target.bundleId ?? null,
|
|
aps_environment: payload.target.apsEnvironment ?? null,
|
|
},
|
|
token: payload.target.token,
|
|
sourceJobId: payload.jobId,
|
|
notification: payload.notification,
|
|
});
|
|
}
|
|
}).pipe(withSpanAttributes({ "user.id": payload.target.userId }));
|
|
});
|
|
|
|
const processBatch: ApnsDeliveries["Service"]["processBatch"] = Effect.fn(
|
|
"relay.apn_delivery_queue.process_batch",
|
|
)(function* (stream) {
|
|
const messages = yield* Stream.runCollect(stream);
|
|
const groups = Arr.groupBy(messages, (message) => {
|
|
const job = decodeSignedApnsDeliveryJobOption(message.body);
|
|
return Option.isSome(job)
|
|
? JSON.stringify([job.value.payload.target.userId, job.value.payload.target.deviceId])
|
|
: "";
|
|
});
|
|
// Finish other devices before surfacing failures so their in-flight sends aren't cancelled.
|
|
yield* Effect.validate(
|
|
Object.values(groups),
|
|
(group) =>
|
|
Effect.forEach(
|
|
group,
|
|
(message) =>
|
|
processSignedJob(message.body).pipe(
|
|
Effect.withSpan("relay.apn_delivery_queue.process_message"),
|
|
),
|
|
{ discard: true },
|
|
),
|
|
{ concurrency: 10, discard: true },
|
|
);
|
|
});
|
|
|
|
return ApnsDeliveries.of({
|
|
sendLiveActivity,
|
|
sendPushNotification,
|
|
processSignedJob,
|
|
processBatch,
|
|
sendForTarget: Effect.fnUntraced(function* (input) {
|
|
if (!config.apns) return null;
|
|
const delivery = chooseDelivery(input);
|
|
if (!delivery) {
|
|
return null;
|
|
}
|
|
if (delivery.kind === "push_notification") {
|
|
const result = yield* deliveryQueue.enqueuePushNotification({
|
|
userId: input.target.user_id,
|
|
deviceId: input.target.device_id,
|
|
token: delivery.token,
|
|
bundleId: input.target.bundle_id,
|
|
apsEnvironment: input.target.aps_environment,
|
|
notification: delivery.notification,
|
|
});
|
|
return result;
|
|
}
|
|
const notification = input.replay ? null : notificationForState(input);
|
|
// Use the end alert only when no companion notification will ring, preserving feedback for Live-Activity-only users.
|
|
const alert = input.replay
|
|
? null
|
|
: delivery.kind === "live_activity_end"
|
|
? notification && input.target.push_token
|
|
? null
|
|
: alertForTerminalAggregate({
|
|
aggregate: delivery.aggregate,
|
|
preferences: parsePreferences(input.target.preferences_json),
|
|
})
|
|
: delivery.alert;
|
|
const result = yield* deliveryQueue.enqueueLiveActivity({
|
|
userId: input.target.user_id,
|
|
deviceId: input.target.device_id,
|
|
kind: delivery.kind,
|
|
token: delivery.token,
|
|
bundleId: input.target.bundle_id,
|
|
apsEnvironment: input.target.aps_environment,
|
|
aggregate: delivery.aggregate,
|
|
alert,
|
|
});
|
|
if (delivery.kind === "live_activity_end" && notification && input.target.push_token) {
|
|
yield* deliveryQueue.enqueuePushNotification({
|
|
userId: input.target.user_id,
|
|
deviceId: input.target.device_id,
|
|
token: input.target.push_token,
|
|
bundleId: input.target.bundle_id,
|
|
apsEnvironment: input.target.aps_environment,
|
|
notification,
|
|
});
|
|
}
|
|
return result;
|
|
}),
|
|
});
|
|
});
|
|
|
|
export const layer = Layer.effect(ApnsDeliveries, make);
|