The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 24, 02:45 UTC
fix(relay): recheck queued iOS alerts and retain fast completions (#10849)
Julius Marminge committed
Sep 8, 2026 at 16:39 UTC
3dfc134e6c1e2359400c56ea415e74fecf9d5c12
3 files changed
+192
-34
infra/relay/src/agentActivity/AgentActivityPublisher.ts
+1
@@ -130,6 +130,7 @@ export const make = Effect.gen(function* () {
130
target,
131
aggregate,
132
nowMs: now.epochMilliseconds,
133
+ replay: true,
134
});
135
}),
136
publish: Effect.fn("relay.agent_activity_publisher.publish")(function* (input) {
infra/relay/src/agentActivity/ApnsDeliveries.test.ts
+110
@@ -1862,3 +1862,113 @@ describe("live activity alert decisions", () => {
1862
).toBeNull();
1863
});
1864
});
1865
+
1866
+describe("queued iOS alert policy", () => {
1867
+ for (const scenario of ["enabled", "muted", "late"] as const) {
1868
+ it.effect(`checks the current policy for a ${scenario} completion`, () => {
1869
+ let sent = 0;
1870
+ const completed = { ...state, phase: "completed" as const };
1871
+ const prefs = JSON.parse(enabledPreferences);
1872
+ if (scenario === "muted") prefs.notifyOnCompletion = false;
1873
+ const payload = makeApnsDeliveryJobPayload({
1874
+ kind: "push_notification",
1875
+ userId: target.user_id,
1876
+ deviceId: target.device_id,
1877
+ token: "push",
1878
+ aggregate: null,
1879
+ notification: {
1880
+ title: "Thread",
1881
+ body: "Done: Project",
1882
+ environmentId: "env",
1883
+ threadId: "thread",
1884
+ deepLink: "/",
1885
+ phase: "completed",
1886
+ updatedAt: completed.updatedAt,
1887
+ },
1888
+ createdAt: completed.updatedAt,
1889
+ expiresAt: "1970-01-01T00:10:00.000Z",
1890
+ jobId: `delivery-policy-${scenario}`,
1891
+ });
1892
+ const signed = signApnsDeliveryJob({ secret: config.apnsDeliveryJobSigningSecret, payload });
1893
+ return Effect.gen(function* () {
1894
+ if (scenario === "late") yield* TestClock.adjust("3 minutes");
1895
+ const d = yield* ApnsDeliveries.ApnsDeliveries;
1896
+ yield* d.processSignedJob(signed);
1897
+ expect(sent).toBe(scenario === "enabled" ? 1 : 0);
1898
+ }).pipe(
1899
+ Effect.provide(
1900
+ makeLayer({
1901
+ attempts: [],
1902
+ config: signingConfig,
1903
+ currentTargets: [
1904
+ { ...target, push_token: "push", preferences_json: JSON.stringify(prefs) },
1905
+ ],
1906
+ currentActivityStates: [completed],
1907
+ execute: (request) =>
1908
+ Effect.sync(() => {
1909
+ sent++;
1910
+ return HttpClientResponse.fromWeb(request, new Response("", { status: 200 }));
1911
+ }),
1912
+ }),
1913
+ ),
1914
+ );
1915
+ });
1916
+ }
1917
+});
1918
+
1919
+describe("fast completion delivery", () => {
1920
+ it.effect("keeps a completion alert when work finishes before running delivery", () => {
1921
+ const queuedJobs: SignedApnsDeliveryJob[] = [];
1922
+ const old = {
1923
+ ...aggregate,
1924
+ activeCount: 0,
1925
+ activities: [
1926
+ {
1927
+ ...aggregate.activities[0]!,
1928
+ threadId: "old" as RelayAgentActivityState["threadId"],
1929
+ phase: "completed" as const,
1930
+ },
1931
+ ],
1932
+ };
1933
+ const device = { ...target, last_aggregate_json: JSON.stringify(old) };
1934
+ const done = {
1935
+ ...aggregate,
1936
+ activeCount: 0,
1937
+ activities: [{ ...aggregate.activities[0]!, phase: "completed" as const }],
1938
+ };
1939
+ return Effect.gen(function* () {
1940
+ const d = yield* ApnsDeliveries.ApnsDeliveries;
1941
+ yield* d.sendForTarget({ target: device, aggregate, nowMs: 0 });
1942
+ yield* d.sendForTarget({ target: device, aggregate: done, nowMs: 0 });
1943
+ expect(
1944
+ queuedJobs.some((x) => x.payload.alert !== null && x.payload.alert !== undefined),
1945
+ ).toBe(true);
1946
+ }).pipe(Effect.provide(makeLayer({ attempts: [], queuedJobs, currentTargets: [device] })));
1947
+ });
1948
+ it.effect("replays a newly visible completion without alerting", () => {
1949
+ const queuedJobs: SignedApnsDeliveryJob[] = [];
1950
+ const done = {
1951
+ ...aggregate,
1952
+ activeCount: 0,
1953
+ activities: [{ ...aggregate.activities[0]!, phase: "completed" as const }],
1954
+ };
1955
+ const previous = {
1956
+ ...aggregate,
1957
+ activities: [
1958
+ { ...aggregate.activities[0]!, threadId: "other" as RelayAgentActivityState["threadId"] },
1959
+ ],
1960
+ };
1961
+ const device = { ...target, last_aggregate_json: JSON.stringify(previous) };
1962
+ return Effect.gen(function* () {
1963
+ const deliveries = yield* ApnsDeliveries.ApnsDeliveries;
1964
+ yield* deliveries.sendForTarget({
1965
+ target: device,
1966
+ aggregate: done,
1967
+ nowMs: 0,
1968
+ replay: true,
1969
+ });
1970
+ expect(queuedJobs).toHaveLength(1);
1971
+ expect(queuedJobs[0]?.payload.alert).toBeUndefined();
1972
+ }).pipe(Effect.provide(makeLayer({ attempts: [], queuedJobs })));
1973
+ });
1974
+});
infra/relay/src/agentActivity/ApnsDeliveries.ts
+81
-34
@@ -181,7 +181,7 @@ function shouldUpdateLiveActivity(input: {
181
// A thread finishing must never be throttled away: when a completion and a
182
// new start land in the same window, activeCount is unchanged and the Done
183
// transition (and its alert) would otherwise be suppressed.
184
- if (newlyTerminalRows(input.previousAggregate, input.nextAggregate).length > 0) {
184
+ if (newlyTerminalRows(input.previousAggregate, input.nextAggregate, true).length > 0) {
185
return true;
186
}
187
const lastDeliveryAtMs =
@@ -228,6 +228,7 @@ function chooseLiveActivityDelivery(input: {
228
readonly target: LiveActivities.TargetRow;
229
readonly aggregate: RelayAgentActivityAggregateState | null;
230
readonly nowMs: number;
231
+ readonly replay?: boolean;
232
}): ChosenLiveActivityDelivery | "suppressed" | null {
233
const preferences = parsePreferences(input.target.preferences_json);
234
if (preferences?.liveActivitiesEnabled === false) {
@@ -286,18 +287,20 @@ function chooseLiveActivityDelivery(input: {
287
kind: "live_activity_update",
288
token: input.target.activity_push_token,
289
aggregate: nextAggregate,
289
- alert:
290
- alertForAttentionTransition({
291
- previousAggregate,
292
- nextAggregate,
293
- preferences,
294
- }) ??
295
- alertForNewlyTerminal({
296
- previousAggregate,
297
- nextAggregate,
298
- preferences,
299
- nowMs: input.nowMs,
300
- }),
290
+ alert: input.replay
291
+ ? null
292
+ : (alertForAttentionTransition({
293
+ previousAggregate,
294
+ nextAggregate,
295
+ preferences,
296
+ }) ??
297
+ alertForNewlyTerminal({
298
+ previousAggregate,
299
+ nextAggregate,
300
+ preferences,
301
+ nowMs: input.nowMs,
302
+ includeUnobserved: true,
303
+ })),
304
}
305
: "suppressed";
306
}
@@ -306,6 +309,7 @@ function chooseDelivery(input: {
309
readonly target: LiveActivities.TargetRow;
310
readonly aggregate: RelayAgentActivityAggregateState | null;
311
readonly nowMs: number;
312
+ readonly replay?: boolean;
313
}): ChosenDelivery | null {
314
const liveActivityDelivery = chooseLiveActivityDelivery(input);
315
if (liveActivityDelivery === "suppressed") {
@@ -314,7 +318,7 @@ function chooseDelivery(input: {
318
if (liveActivityDelivery) {
319
return liveActivityDelivery;
320
}
317
- const notification = notificationForAggregate(input);
321
+ const notification = input.replay ? null : notificationForAggregate(input);
322
return notification && input.target.push_token
323
? {
324
kind: "push_notification",
@@ -519,6 +523,7 @@ export class ApnsDeliveries extends Context.Service<
523
readonly target: LiveActivities.TargetRow;
524
readonly aggregate: RelayAgentActivityAggregateState | null;
525
readonly nowMs: number;
526
+ readonly replay?: boolean;
527
}) => Effect.Effect<RelayDeliveryResult | null, ApnsDeliveryError>;
528
readonly sendPushNotificationForTarget: (input: {
529
readonly target: LiveActivities.TargetRow;
@@ -652,7 +657,7 @@ export const make = Effect.gen(function* () {
657
});
658
});
659
655
- const isCurrentSignedJobToken = Effect.fnUntraced(function* (input: {
660
+ const currentSignedJobTarget = Effect.fnUntraced(function* (input: {
661
readonly target: LiveActivityDeliveryTarget;
662
readonly kind: RelayDeliveryKind;
663
readonly token: string;
@@ -660,10 +665,10 @@ export const make = Effect.gen(function* () {
665
return yield* liveActivities.listTargets({ userId: input.target.user_id }).pipe(
666
Effect.map((targets) => {
667
const currentTarget = targets.find((row) => row.device_id === input.target.device_id);
663
- return (
664
- currentTarget !== undefined &&
668
+ return currentTarget &&
669
expectedCurrentToken({ target: currentTarget, kind: input.kind }) === input.token
666
- );
670
+ ? currentTarget
671
+ : null;
672
}),
673
);
674
});
@@ -679,11 +684,7 @@ export const make = Effect.gen(function* () {
684
const now = yield* DateTime.now;
685
const aggregate =
686
input.aggregate === null ? null : sanitizeAgentActivityAggregateState(input.aggregate);
682
- const { epochSeconds, iso, request } = makeLiveActivityDeliveryRequest(
683
- apns,
684
- { ...input, aggregate } as SendLiveActivityDeliveryInput,
685
- now,
686
- );
687
+ let alert = input.alert ?? null;
688
const recoverTransportError = (cause: Apns.ApnsError) =>
689
recoverApnsDeliveryTransportError(
690
{
@@ -709,18 +710,37 @@ export const make = Effect.gen(function* () {
710
if (claim === "in_flight") {
711
return yield* new ApnsDeliveryJobClaimInFlight({ sourceJobId: input.sourceJobId });
712
}
712
- const tokenIsCurrent = yield* isCurrentSignedJobToken({
713
+ const currentTarget = yield* currentSignedJobTarget({
714
target: input.target,
715
kind: input.kind,
716
token: input.token,
717
});
717
- if (!tokenIsCurrent) {
718
+ if (!currentTarget) {
719
yield* attempts.completeSourceJob({
720
sourceJobId: input.sourceJobId,
721
apnsReason: "Stale APNs delivery job skipped.",
722
});
723
return staleJobResult({ deviceId: input.target.device_id, kind: input.kind });
724
}
725
+ if (alert) {
726
+ const preferences = parsePreferences(currentTarget.preferences_json);
727
+ const previousAggregate = parseAggregate(currentTarget.last_aggregate_json);
728
+ alert =
729
+ !preferences?.notificationsEnabled || !aggregate
730
+ ? null
731
+ : (alertForAttentionTransition({
732
+ previousAggregate,
733
+ nextAggregate: aggregate,
734
+ preferences,
735
+ }) ??
736
+ alertForNewlyTerminal({
737
+ previousAggregate,
738
+ nextAggregate: aggregate,
739
+ preferences,
740
+ nowMs: now.epochMilliseconds,
741
+ includeUnobserved: true,
742
+ }));
743
+ }
744
if (
745
input.kind !== "live_activity_start" &&
746
aggregate !== null &&
@@ -752,6 +772,11 @@ export const make = Effect.gen(function* () {
772
}
773
return staleJobResult({ deviceId: input.target.device_id, kind: input.kind });
774
}
775
+ const { epochSeconds, iso, request } = makeLiveActivityDeliveryRequest(
776
+ apns,
777
+ { ...input, aggregate, alert } as SendLiveActivityDeliveryInput,
778
+ now,
779
+ );
780
const result = yield* apns
781
.sendLiveActivityRequest({
782
credentials: credentialsForTarget(config.apns, input.target),
@@ -859,12 +884,12 @@ export const make = Effect.gen(function* () {
884
if (claim === "in_flight") {
885
return yield* new ApnsDeliveryJobClaimInFlight({ sourceJobId: input.sourceJobId });
886
}
862
- const tokenIsCurrent = yield* isCurrentSignedJobToken({
887
+ const currentTarget = yield* currentSignedJobTarget({
888
target: input.target,
889
kind: "push_notification",
890
token: input.token,
891
});
867
- if (!tokenIsCurrent) {
892
+ if (!currentTarget) {
893
yield* attempts.completeSourceJob({
894
sourceJobId: input.sourceJobId,
895
apnsReason: "Stale APNs delivery job skipped.",
@@ -889,6 +914,24 @@ export const make = Effect.gen(function* () {
914
kind: "push_notification",
915
});
916
}
917
+ const preferences = parsePreferences(currentTarget.preferences_json);
918
+ const alertAllowed =
919
+ notification.phase !== undefined && notification.updatedAt !== undefined
920
+ ? shouldAlertForActivity({
921
+ ...notification,
922
+ phase: notification.phase,
923
+ updatedAt: notification.updatedAt,
924
+ preferences,
925
+ nowMs: now.epochMilliseconds,
926
+ })
927
+ : preferences?.notificationsEnabled === true;
928
+ if (!alertAllowed) {
929
+ yield* attempts.completeSourceJob({
930
+ sourceJobId: input.sourceJobId,
931
+ apnsReason: "Notification is disabled or no longer fresh.",
932
+ });
933
+ return staleJobResult({ deviceId: input.target.device_id, kind: "push_notification" });
934
+ }
935
}
936
const result = yield* apns
937
.sendPushNotificationRequest({
@@ -1057,6 +1100,7 @@ export const make = Effect.gen(function* () {
1100
target: input.target,
1101
aggregate: input.aggregate,
1102
nowMs: input.nowMs,
1103
+ replay: input.replay ?? false,
1104
});
1105
if (!delivery) {
1106
return null;
@@ -1072,17 +1116,20 @@ export const make = Effect.gen(function* () {
1116
});
1117
return result;
1118
}
1075
- const notification = notificationForAggregate({
1076
- target: input.target,
1077
- aggregate: input.aggregate,
1078
- nowMs: input.nowMs,
1079
- });
1119
+ const notification = input.replay
1120
+ ? null
1121
+ : notificationForAggregate({
1122
+ target: input.target,
1123
+ aggregate: input.aggregate,
1124
+ nowMs: input.nowMs,
1125
+ });
1126
// The end event doubles as the "task finished" moment. When a companion
1127
// push notification is about to ring the device (below), the activity end
1128
// stays silent; otherwise the end itself carries the alert so LA-only
1129
// users still get the buzz.
1084
- const alert =
1085
- delivery.kind === "live_activity_end"
1130
+ const alert = input.replay
1131
+ ? null
1132
+ : delivery.kind === "live_activity_end"
1133
? notification && input.target.push_token
1134
? null
1135
: alertForTerminalAggregate({