The best way to control Claude Code, Codex and more from any computer or phone
fix(relay): reopening the app ends a finished iOS Live Activity (#20265)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Mohtasham Murshid committed
Oct 19, 2026 at 03:19 UTC
992022b9c21b20b3d450f25082c5fc814074e446
2 files changed
+84
-2
infra/relay/src/agentActivity/LiveActivities.test.ts
+62
@@ -52,6 +52,13 @@ describe("LiveActivities", () => {
52
const dialect = new PgDialect();
53
54
const fakeDb = {
55
+ select: () => ({
56
+ from: (table: unknown) => {
57
+ expect(table).toBe(relayLiveActivities);
58
+ calls.push("select");
59
+ return { where: () => Effect.succeed([]) };
60
+ },
61
+ }),
62
update: (table: unknown) => {
63
expect(table).toBe(relayLiveActivities);
64
calls.push("update");
@@ -95,6 +102,7 @@ describe("LiveActivities", () => {
102
yield* liveActivities.register({ userId: "user-2", registration });
103
104
expect(calls).toEqual([
105
+ "select",
106
"update",
107
"update.set",
108
"update.where",
@@ -145,6 +153,59 @@ describe("LiveActivities", () => {
153
},
154
);
155
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
+
209
it.effect("preserves ended state when a delayed update delivery is marked", () => {
210
const insertedValues: Array<Record<string, unknown>> = [];
211
const conflictConfigs: Array<{
@@ -263,6 +324,7 @@ describe("LiveActivities", () => {
324
select: () => ({
325
from: () => ({
326
leftJoin: () => ({ where: () => Effect.fail(cause) }),
327
+ where: () => Effect.fail(cause),
328
}),
329
}),
330
} as unknown as RelayDb.RelayDb["Service"];
infra/relay/src/agentActivity/LiveActivities.ts
+22
-2
@@ -140,6 +140,26 @@ export const make = Effect.gen(function* () {
140
yield* Effect.gen(function* () {
141
const updatedAt = DateTime.formatIso(yield* DateTime.now);
142
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;
163
164
yield* db
165
.update(relayLiveActivities)
@@ -159,7 +179,7 @@ export const make = Effect.gen(function* () {
179
deviceId: registration.deviceId,
180
activityPushToken: registration.activityPushToken,
181
remoteStartQueuedAt: null,
162
- remoteStartedAt: updatedAt,
182
+ remoteStartedAt: armedAt,
183
endedAt: null,
184
lastAggregateJson: null,
185
lastLiveActivityDeliveryAt: null,
@@ -171,7 +191,7 @@ export const make = Effect.gen(function* () {
191
set: {
192
activityPushToken: registration.activityPushToken,
193
remoteStartQueuedAt: null,
174
- remoteStartedAt: updatedAt,
194
+ remoteStartedAt: armedAt,
195
endedAt: null,
196
lastAggregateJson: null,
197
lastLiveActivityDeliveryAt: null,