fix(relay): end idle Live Activities after the display window #11943
Mergedishaanko(GitHub profile, opens in a new tab)merged 6 commits into
main fromrefs/pull/11943/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- infra/relay/src/agentActivity/LiveActivities.ts line 287OutdatedResolvedby ishaanko(GitHub profile, opens in a new tab)@@ -250,3 +268,20 @@250268 );251269 }),252270271+ // Armed cards whose last delivered content had no live work (or no272+ // content yet) and that have heard nothing since the finished-row display273+ // window. Nothing else ends such a card: deliveries only run when an274+ // environment publishes or the app re-registers, so a Done card with no275+ // follow-up activity would otherwise sit on the lock screen for hours.276+ listIdleArmedTargets: Effect.fn("relay.live_activities.list_idle_armed_targets")(277+ function* (input) {278+ return yield* db279+ .select({280+ user_id: relayLiveActivities.userId,281+ device_id: relayLiveActivities.deviceId,282+ })283+ .from(relayLiveActivities)284+ .where(285+ and(286+ isNotNull(relayLiveActivities.activityPushToken),287+ sql`coalesce(${relayLiveActivities.lastAggregateJson} ->> 'activeCount', '0') = '0'`,
- infra/relay/src/agentActivity/AgentActivityPublisher.ts lines 184 to 188OutdatedResolvedby ishaanko(GitHub profile, opens in a new tab)@@ -145 +168,21 @@168+ return yield* replayForTarget({ userId: input.userId, deviceId: input.deviceId });145169 }),170+ // Deliveries only happen when an environment publishes or the app171+ // re-registers, so a card left showing finished work would otherwise keep172+ // its Done rows past the display window until one of those happens.173+ // Replaying the aggregate once the window has passed ends it.174+ endIdleLiveActivities: Effect.fn("relay.agent_activity_publisher.end_idle_live_activities")(175+ function* (input) {176+ const deliveredBefore = DateTime.formatIso(177+ DateTime.makeUnsafe(input.nowMs - TERMINAL_AGENT_ACTIVITY_DISPLAY_TTL_MS),178+ );179+ const targets = yield* liveActivities.listIdleArmedTargets({ deliveredBefore });180+ yield* Effect.annotateCurrentSpan({ "relay.live_activities.idle_count": targets.length });181+ yield* Effect.forEach(182+ targets,183+ (target) =>184+ replayForTarget({185+ userId: target.user_id,186+ deviceId: target.device_id,187+ endOnly: true,188+ }).pipe(
- infra/relay/src/agentActivity/AgentActivityPublisher.ts lines 170 to 200OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -145,0 +180,21 @@180+ yield* Effect.annotateCurrentSpan({ "relay.live_activities.idle_count": targets.length });181+ yield* Effect.forEach(182+ targets,183+ (target) =>184+ replayForTarget({185+ userId: target.user_id,186+ deviceId: target.device_id,187+ endOnly: true,188+ }).pipe(189+ Effect.tapError((error) =>190+ Effect.logWarning("idle live activity replay failed", {191+ deviceId: target.device_id,192+ errorTag: error._tag,193+ }),194+ ),195+ Effect.ignore,196+ ),197+ { concurrency: 4, discard: true },198+ );199+ },200+ ),
juliusmarminge added labelsmacroscope-review
- infra/relay/src/agentActivity/ApnsDeliveries.ts line 777OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -768,3 +768,10 @@768768 return staleJobResult({ deviceId: input.target.device_id, kind: input.kind });769769 }770770 }771+ // A contentless end retires the token. Work that started after the end772+ // was queued may already have painted the card through its own update;773+ // ending now would drop that card and strand the new work.774+ if (775+ input.kind === "live_activity_end" &&776+ aggregate === null &&777+ (yield* userStillHasLiveWork(input.target.user_id))
- infra/relay/src/agentActivity/ApnsDeliveries.ts line 777OutdatedResolvedby ishaanko(GitHub profile, opens in a new tab)@@ -767,3 +767,11 @@767767 });768768 return staleJobResult({ deviceId: input.target.device_id, kind: input.kind });769769 }770+ // A contentless end was decided while nothing was running. Work that771+ // started since owns the card, so ending it now would strand that work.772+ // An end for a device that turned Live Activities off still goes out.773+ if (774+ input.kind === "live_activity_end" &&775+ aggregate === null &&776+ parsePreferences(currentTarget.preferences_json)?.liveActivitiesEnabled !== false &&777+ (yield* userStillHasLiveWork(input.target.user_id))
juliusmarminge assignedjuliusmarminge
juliusmarminge merged thisd1da235
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed