Skip to content

Commit 7cc5781

Browse files
fix(relay): preserve pending alerts across thread deletion
1 parent 68c7081 commit 7cc5781

4 files changed

Lines changed: 38 additions & 6 deletions

File tree

‎infra/relay/src/agentActivity/AgentActivityPublisher.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ describe("AgentActivityPublisher", () => {
158158
});
159159
expect(fcmCalls).toEqual([
160160
{ target: android, state },
161-
{ target: android, state: null },
161+
{ target: android, state: null, replay: true },
162162
]);
163163
expect(appleDevices).toEqual(["ios"]);
164164
}).pipe(

‎infra/relay/src/agentActivity/AgentActivityPublisher.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export const make = Effect.gen(function* () {
128128
return null;
129129
}
130130
if (target.platform === "android") {
131-
return yield* fcmDeliveries.enqueue({ target, state: null });
131+
return yield* fcmDeliveries.enqueue({ target, state: null, replay: true });
132132
}
133133
const now = yield* DateTime.now;
134134
const aggregate = makeAggregateState({

‎infra/relay/src/agentActivity/FcmDeliveries.test.ts‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,39 @@ describe("Android delivery routing", () => {
299299
}).pipe(Effect.provide(h.layer));
300300
});
301301

302+
for (const phase of [
303+
"completed",
304+
"waiting_for_approval",
305+
"waiting_for_input",
306+
"failed",
307+
] as const) {
308+
it.effect(`deleting one thread preserves another thread's ${phase} alert`, () => {
309+
const h = harness();
310+
h.current.otherStates = [secondState];
311+
return Effect.gen(function* () {
312+
const delivery = yield* FcmDeliveries;
313+
yield* delivery.process(h.job);
314+
h.current.otherStates = [];
315+
h.current.state = { ...state, phase };
316+
yield* delivery.enqueue({ target, state: null });
317+
yield* delivery.process(h.queued[0]);
318+
expect(h.sent.at(-1)?.alert).toBe(false);
319+
yield* delivery.process({ ...h.job, state: h.current.state });
320+
expect(h.sent.filter((message) => message.alert)).toHaveLength(1);
321+
yield* delivery.process({ ...h.job, state: h.current.state });
322+
expect(h.sent.filter((message) => message.alert)).toHaveLength(1);
323+
}).pipe(Effect.provide(h.layer));
324+
});
325+
}
326+
302327
it.effect("registration replay establishes a baseline without alerting", () => {
303328
const h = harness();
304329
h.current.state = { ...state, phase: "waiting_for_approval" };
305330
h.current.otherStates = [{ ...secondState, phase: "waiting_for_input" }];
306331
return Effect.gen(function* () {
307332
const delivery = yield* FcmDeliveries;
308-
yield* delivery.process({ ...h.job, state: null });
333+
yield* delivery.enqueue({ target, state: null, replay: true });
334+
yield* delivery.process(h.queued[0]);
309335
yield* delivery.process({ ...h.job, state: h.current.state });
310336
expect(h.sent.every((message) => !message.alert)).toBe(true);
311337
expect(h.marked[0]?.aggregate?.activities).toHaveLength(2);
@@ -558,11 +584,11 @@ describe("Android delivery routing", () => {
558584
activity_expires_at: "900000",
559585
});
560586
yield* TestClock.adjust("5 minutes");
561-
yield* delivery.process({ ...h.job, queuedAt: 300000, state: null });
587+
yield* delivery.process({ ...h.job, queuedAt: 300000, state: null, replay: true });
562588
expect(h.sent[1]?.data.activity_expires_at).toBe("900000");
563589
expect(h.sent[1]?.alert).toBe(false);
564590
yield* TestClock.adjust("11 minutes");
565-
yield* delivery.process({ ...h.job, queuedAt: 960000, state: null });
591+
yield* delivery.process({ ...h.job, queuedAt: 960000, state: null, replay: true });
566592
expect(h.sent[2]?.data).toMatchObject({ active: "false", activity_expires_at: "0" });
567593
expect(h.marked.at(-1)?.aggregate).toBeNull();
568594
}).pipe(Effect.provide(h.layer));

‎infra/relay/src/agentActivity/FcmDeliveries.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export const FcmDeliveryJob = Schema.Struct({
3838
token: Schema.String,
3939
state: Schema.NullOr(RelayAgentActivityState),
4040
queuedAt: Schema.Number,
41+
replay: Schema.optional(Schema.Boolean),
4142
});
4243
export type FcmDeliveryJob = typeof FcmDeliveryJob.Type;
4344
const decodeJob = Schema.decodeUnknownEffect(FcmDeliveryJob);
@@ -116,6 +117,7 @@ export class FcmDeliveries extends Context.Service<
116117
readonly enqueue: (input: {
117118
readonly target: LiveActivities.TargetRow;
118119
readonly state: RelayAgentActivityState | null;
120+
readonly replay?: boolean;
119121
}) => Effect.Effect<RelayDeliveryResult | null, FcmDeliveryError>;
120122
readonly process: (
121123
body: unknown,
@@ -164,6 +166,7 @@ export const make = Effect.gen(function* () {
164166
deviceId: input.target.device_id,
165167
token: input.target.push_token,
166168
state: input.state,
169+
...(input.replay ? { replay: true } : {}),
167170
queuedAt: now.epochMilliseconds,
168171
})
169172
.pipe(Effect.mapError((cause) => new FcmDeliveryError({ operation: "enqueue", cause })));
@@ -208,7 +211,10 @@ export const make = Effect.gen(function* () {
208211
? Option.getOrNull(decodePreviousActivity(target.last_aggregate_json))
209212
: null;
210213
let alert: ReturnType<typeof androidAlertForState> = null;
211-
let acknowledgeAggregate = true;
214+
// Deletion jobs can observe another thread's newly completed state. They
215+
// update the card, but must leave that transition for its own alert job.
216+
// Registration replay deliberately establishes a silent baseline.
217+
let acknowledgeAggregate = job.state !== null || job.replay === true || aggregate === null;
212218
if (job.state && preferences.value.notificationsEnabled) {
213219
const state = yield* rows.getForUserThread({
214220
userId: job.userId,

0 commit comments

Comments
 (0)