TypeScript
fix(relay): reopening the app ends a finished iOS Live Activity (#20265)
Mohtasham Murshid992022b9c21b
diff --git a/infra/relay/src/agentActivity/LiveActivities.test.ts b/infra/relay/src/agentActivity/LiveActivities.test.tsindex 1baa511f5e3..692b554a644 100644--- a/infra/relay/src/agentActivity/LiveActivities.test.ts+++ b/infra/relay/src/agentActivity/LiveActivities.test.ts@@ -52,6 +52,13 @@ describe("LiveActivities", () => {const dialect = new PgDialect();const fakeDb = {+ select: () => ({+ from: (table: unknown) => {+ expect(table).toBe(relayLiveActivities);+ calls.push("select");+ return { where: () => Effect.succeed([]) };+ },+ }),update: (table: unknown) => {expect(table).toBe(relayLiveActivities);calls.push("update");@@ -95,6 +102,7 @@ describe("LiveActivities", () => {yield* liveActivities.register({ userId: "user-2", registration });expect(calls).toEqual([+ "select","update","update.set","update.where",@@ -145,6 +153,59 @@ describe("LiveActivities", () => {},);+ it.effect("keeps the arming time when the current card re-registers its token", () => {+ const armedAt = "2026-05-25T00:00:00.000Z";+ const insertedValues: Array<Record<string, unknown>> = [];+ const conflictSets: Array<Record<string, unknown> | undefined> = [];+ const fakeDb = {+ select: () => ({+ from: () => ({+ where: () =>+ Effect.succeed([{ activityPushToken: "current-token", remoteStartedAt: armedAt }]),+ }),+ }),+ update: () => ({ set: () => ({ where: () => Effect.void }) }),+ insert: () => ({+ values: (values: Record<string, unknown>) => {+ insertedValues.push(values);+ return {+ onConflictDoUpdate: (config: { readonly set?: Record<string, unknown> }) => {+ conflictSets.push(config.set);+ return Effect.void;+ },+ };+ },+ }),+ } as unknown as RelayDb.RelayDb["Service"];+ const register = (activityPushToken: string) =>+ Effect.flatMap(LiveActivities.LiveActivities, (liveActivities) =>+ liveActivities.register({+ userId: "user-1",+ registration: {+ deviceId: "device-1" as RelayLiveActivityRegistrationRequest["deviceId"],+ activityPushToken:+ activityPushToken as RelayLiveActivityRegistrationRequest["activityPushToken"],+ },+ }),+ );++ return Effect.gen(function* () {+ yield* register("current-token");+ yield* register("new-card-token");++ // The foreground refresh still clears the delivered baseline so the+ // replay repaints the card, but only a new card counts as freshly armed.+ expect(insertedValues[0]).toMatchObject({ remoteStartedAt: armedAt });+ expect(conflictSets[0]).toMatchObject({ remoteStartedAt: armedAt, lastAggregateJson: null });+ expect(insertedValues[1]?.remoteStartedAt).not.toBe(armedAt);+ expect(conflictSets[1]?.remoteStartedAt).not.toBe(armedAt);+ }).pipe(+ Effect.provide(+ LiveActivities.layer.pipe(Layer.provide(Layer.succeed(RelayDb.RelayDb, fakeDb))),+ ),+ );+ });+it.effect("preserves ended state when a delayed update delivery is marked", () => {const insertedValues: Array<Record<string, unknown>> = [];const conflictConfigs: Array<{@@ -263,6 +324,7 @@ describe("LiveActivities", () => {select: () => ({from: () => ({leftJoin: () => ({ where: () => Effect.fail(cause) }),+ where: () => Effect.fail(cause),}),}),} as unknown as RelayDb.RelayDb["Service"];diff --git a/infra/relay/src/agentActivity/LiveActivities.ts b/infra/relay/src/agentActivity/LiveActivities.tsindex 5f741f2057b..eea8dc90f33 100644--- a/infra/relay/src/agentActivity/LiveActivities.ts+++ b/infra/relay/src/agentActivity/LiveActivities.ts@@ -140,6 +140,26 @@ export const make = Effect.gen(function* () {yield* Effect.gen(function* () {const updatedAt = DateTime.formatIso(yield* DateTime.now);const registration = input.registration;+ const [current] = yield* db+ .select({+ activityPushToken: relayLiveActivities.activityPushToken,+ remoteStartedAt: relayLiveActivities.remoteStartedAt,+ })+ .from(relayLiveActivities)+ .where(+ and(+ eq(relayLiveActivities.userId, input.userId),+ eq(relayLiveActivities.deviceId, registration.deviceId),+ ),+ );+ // The app re-registers its current card's token on every foreground.+ // Restamping the arming time would make that card look freshly armed,+ // and the arming grace in ApnsDeliveries would swallow the end that+ // the registration replay owes a card whose work is gone.+ const armedAt =+ (current?.activityPushToken === registration.activityPushToken+ ? current.remoteStartedAt+ : null) ?? updatedAt;yield* db.update(relayLiveActivities)@@ -159,7 +179,7 @@ export const make = Effect.gen(function* () {deviceId: registration.deviceId,activityPushToken: registration.activityPushToken,remoteStartQueuedAt: null,- remoteStartedAt: updatedAt,+ remoteStartedAt: armedAt,endedAt: null,lastAggregateJson: null,lastLiveActivityDeliveryAt: null,@@ -171,7 +191,7 @@ export const make = Effect.gen(function* () {set: {activityPushToken: registration.activityPushToken,remoteStartQueuedAt: null,- remoteStartedAt: updatedAt,+ remoteStartedAt: armedAt,endedAt: null,lastAggregateJson: null,lastLiveActivityDeliveryAt: null,