Skip to content

Commit b85b75b

Browse files
fix(relay): reopening the app ends a finished iOS Live Activity
Every foreground re-registration of the card's current token restamped remote_started_at, so the registration replay always saw a "freshly armed" card and the 2-minute arming grace suppressed the end. A card whose work had finished more than 15 minutes earlier could never be ended. Keep the original arming time when the device re-registers the token it already holds; new cards still get the grace. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent cfafb4b commit b85b75b

2 files changed

Lines changed: 84 additions & 2 deletions

File tree

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ describe("LiveActivities", () => {
5252
const dialect = new PgDialect();
5353

5454
const fakeDb = {
55+
select: () => ({
56+
from: (table: unknown) => {
57+
expect(table).toBe(relayLiveActivities);
58+
calls.push("select");
59+
return { where: () => Effect.succeed([]) };
60+
},
61+
}),
5562
update: (table: unknown) => {
5663
expect(table).toBe(relayLiveActivities);
5764
calls.push("update");
@@ -95,6 +102,7 @@ describe("LiveActivities", () => {
95102
yield* liveActivities.register({ userId: "user-2", registration });
96103

97104
expect(calls).toEqual([
105+
"select",
98106
"update",
99107
"update.set",
100108
"update.where",
@@ -145,6 +153,59 @@ describe("LiveActivities", () => {
145153
},
146154
);
147155

156+
it.effect("keeps the arming time when the current card re-registers its token", () => {
157+
const armedAt = "2026-05-25T00:00:00.000Z";
158+
const insertedValues: Array<Record<string, unknown>> = [];
159+
const conflictSets: Array<Record<string, unknown> | undefined> = [];
160+
const fakeDb = {
161+
select: () => ({
162+
from: () => ({
163+
where: () =>
164+
Effect.succeed([{ activityPushToken: "current-token", remoteStartedAt: armedAt }]),
165+
}),
166+
}),
167+
update: () => ({ set: () => ({ where: () => Effect.void }) }),
168+
insert: () => ({
169+
values: (values: Record<string, unknown>) => {
170+
insertedValues.push(values);
171+
return {
172+
onConflictDoUpdate: (config: { readonly set?: Record<string, unknown> }) => {
173+
conflictSets.push(config.set);
174+
return Effect.void;
175+
},
176+
};
177+
},
178+
}),
179+
} as unknown as RelayDb.RelayDb["Service"];
180+
const register = (activityPushToken: string) =>
181+
Effect.flatMap(LiveActivities.LiveActivities, (liveActivities) =>
182+
liveActivities.register({
183+
userId: "user-1",
184+
registration: {
185+
deviceId: "device-1" as RelayLiveActivityRegistrationRequest["deviceId"],
186+
activityPushToken:
187+
activityPushToken as RelayLiveActivityRegistrationRequest["activityPushToken"],
188+
},
189+
}),
190+
);
191+
192+
return Effect.gen(function* () {
193+
yield* register("current-token");
194+
yield* register("new-card-token");
195+
196+
// The foreground refresh still clears the delivered baseline so the
197+
// replay repaints the card, but only a new card counts as freshly armed.
198+
expect(insertedValues[0]).toMatchObject({ remoteStartedAt: armedAt });
199+
expect(conflictSets[0]).toMatchObject({ remoteStartedAt: armedAt, lastAggregateJson: null });
200+
expect(insertedValues[1]?.remoteStartedAt).not.toBe(armedAt);
201+
expect(conflictSets[1]?.remoteStartedAt).not.toBe(armedAt);
202+
}).pipe(
203+
Effect.provide(
204+
LiveActivities.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb))),
205+
),
206+
);
207+
});
208+
148209
it.effect("preserves ended state when a delayed update delivery is marked", () => {
149210
const insertedValues: Array<Record<string, unknown>> = [];
150211
const conflictConfigs: Array<{
@@ -263,6 +324,7 @@ describe("LiveActivities", () => {
263324
select: () => ({
264325
from: () => ({
265326
leftJoin: () => ({ where: () => Effect.fail(cause) }),
327+
where: () => Effect.fail(cause),
266328
}),
267329
}),
268330
} as unknown as RelayDb.RelayDb["Service"];

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,26 @@ export const make = Effect.gen(function* () {
140140
yield* Effect.gen(function* () {
141141
const updatedAt = DateTime.formatIso(yield* DateTime.now);
142142
const registration = input.registration;
143+
const [current] = yield* db
144+
.select({
145+
activityPushToken: relayLiveActivities.activityPushToken,
146+
remoteStartedAt: relayLiveActivities.remoteStartedAt,
147+
})
148+
.from(relayLiveActivities)
149+
.where(
150+
and(
151+
eq(relayLiveActivities.userId, input.userId),
152+
eq(relayLiveActivities.deviceId, registration.deviceId),
153+
),
154+
);
155+
// The app re-registers its current card's token on every foreground.
156+
// Restamping the arming time would make that card look freshly armed,
157+
// and the arming grace in ApnsDeliveries would swallow the end that
158+
// the registration replay owes a card whose work is gone.
159+
const armedAt =
160+
(current?.activityPushToken === registration.activityPushToken
161+
? current.remoteStartedAt
162+
: null) ?? updatedAt;
143163

144164
yield* db
145165
.update(relayLiveActivities)
@@ -159,7 +179,7 @@ export const make = Effect.gen(function* () {
159179
deviceId: registration.deviceId,
160180
activityPushToken: registration.activityPushToken,
161181
remoteStartQueuedAt: null,
162-
remoteStartedAt: updatedAt,
182+
remoteStartedAt: armedAt,
163183
endedAt: null,
164184
lastAggregateJson: null,
165185
lastLiveActivityDeliveryAt: null,
@@ -171,7 +191,7 @@ export const make = Effect.gen(function* () {
171191
set: {
172192
activityPushToken: registration.activityPushToken,
173193
remoteStartQueuedAt: null,
174-
remoteStartedAt: updatedAt,
194+
remoteStartedAt: armedAt,
175195
endedAt: null,
176196
lastAggregateJson: null,
177197
lastLiveActivityDeliveryAt: null,

0 commit comments

Comments
 (0)