From 5b57dd1e87e739677e392cde9b5f0915392609c9 Mon Sep 17 00:00:00 2001 From: SSHdotCodes <254190073+SSHdotCodes@users.noreply.github.com> Date: Mon, 23 Nov 2026 14:25:18 -0500 Subject: [PATCH] fix(mobile): show push alerts while the open chat is reconnecting --- .../foregroundNotificationBehavior.test.ts | 37 +++++++++++++++++++ .../foregroundNotificationBehavior.ts | 7 +++- .../agent-awareness/notificationNavigation.ts | 31 +++++++++++++++- 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 apps/mobile/src/features/agent-awareness/foregroundNotificationBehavior.test.ts diff --git a/apps/mobile/src/features/agent-awareness/foregroundNotificationBehavior.test.ts b/apps/mobile/src/features/agent-awareness/foregroundNotificationBehavior.test.ts new file mode 100644 index 0000000000000..80feb0ce94455 --- /dev/null +++ b/apps/mobile/src/features/agent-awareness/foregroundNotificationBehavior.test.ts @@ -0,0 +1,37 @@ +import type { Notification } from "expo-notifications"; +import { describe, expect, it } from "vite-plus/test"; + +import { foregroundNotificationBehavior } from "./foregroundNotificationBehavior"; + +function notificationFor(deepLink: string): Notification { + return { + date: 0, + request: { identifier: "notification-1", content: { data: { deepLink } }, trigger: null }, + } as unknown as Notification; +} + +describe("foregroundNotificationBehavior", () => { + const onScreen = "/threads/env-1/thread-1"; + + it("stays silent for the thread on screen once its feed is live", () => { + const behavior = foregroundNotificationBehavior( + notificationFor(onScreen), + onScreen, + () => true, + ); + expect(behavior.shouldShowBanner).toBe(false); + expect(behavior.shouldPlaySound).toBe(false); + }); + + it("shows the alert while the thread on screen is cached or reconnecting", () => { + const checked: Array = []; + const behavior = foregroundNotificationBehavior(notificationFor(onScreen), onScreen, (link) => { + checked.push(link); + return false; + }); + expect(behavior.shouldShowBanner).toBe(true); + expect(behavior.shouldShowList).toBe(true); + expect(behavior.shouldPlaySound).toBe(true); + expect(checked).toEqual([onScreen]); + }); +}); diff --git a/apps/mobile/src/features/agent-awareness/foregroundNotificationBehavior.ts b/apps/mobile/src/features/agent-awareness/foregroundNotificationBehavior.ts index 99ea064acd2c2..6c3308d4149b6 100644 --- a/apps/mobile/src/features/agent-awareness/foregroundNotificationBehavior.ts +++ b/apps/mobile/src/features/agent-awareness/foregroundNotificationBehavior.ts @@ -16,12 +16,17 @@ const SUPPRESS: NotificationBehavior = { shouldSetBadge: false, }; +/** + * A notification for the thread on screen stays silent once its feed is live. While that thread + * is cached, reconnecting or still catching up, the screen may not have the event yet. + */ export function foregroundNotificationBehavior( notification: Notification, deepLinkOnScreen: string | null, + hasLiveFeed: (deepLink: string) => boolean, ): NotificationBehavior { const target = extractAgentNotificationDeepLink({ notification }); - if (target !== null && target === deepLinkOnScreen) { + if (target !== null && target === deepLinkOnScreen && hasLiveFeed(target)) { return SUPPRESS; } return SHOW; diff --git a/apps/mobile/src/features/agent-awareness/notificationNavigation.ts b/apps/mobile/src/features/agent-awareness/notificationNavigation.ts index 7d2317e58baae..0c80723d917fb 100644 --- a/apps/mobile/src/features/agent-awareness/notificationNavigation.ts +++ b/apps/mobile/src/features/agent-awareness/notificationNavigation.ts @@ -1,5 +1,11 @@ import * as Notifications from "expo-notifications"; +import { EnvironmentId, ThreadId } from "@t3tools/contracts"; +import * as Option from "effect/Option"; +import { AsyncResult } from "effect/reactivity"; +import { appAtomRegistry } from "../../state/atom-registry"; +import { environmentPresentations } from "../../state/presentation"; +import { environmentThreads } from "../../state/threads"; import { setAndroidThreadOnScreen } from "./androidNotifications"; import { foregroundNotificationBehavior } from "./foregroundNotificationBehavior"; import { routeAgentNotificationResponseOnce, threadDeepLinkOnScreen } from "./notificationPayload"; @@ -7,6 +13,27 @@ import { consumeLastAgentNotificationResponse } from "./notificationResponseCons let deepLinkOnScreen: string | null = null; +/** Whether the open thread's screen is connected and showing its live feed, not a cached copy. */ +function threadHasLiveFeed(deepLink: string): boolean { + const [, , environment, thread] = deepLink.split("/"); + if (!environment || !thread) return false; + const environmentId = EnvironmentId.make(decodeURIComponent(environment)); + const presentation = appAtomRegistry.get( + environmentPresentations.presentationAtom(environmentId), + ); + if (presentation?.connection.phase !== "connected") return false; + const threadAtom = environmentThreads.stateAtom( + environmentId, + ThreadId.make(decodeURIComponent(thread)), + ); + // The thread screen holds its detail; reading one nobody holds would start loading it. + if (!appAtomRegistry.getNodes().has(threadAtom)) return false; + return Option.exists( + AsyncResult.value(appAtomRegistry.get(threadAtom)), + ({ status }) => status === "live", + ); +} + /** Called with the workspace pathname on every root navigation state change. */ export function setAgentNotificationPathname(pathname: string): void { deepLinkOnScreen = threadDeepLinkOnScreen(pathname); @@ -22,7 +49,9 @@ export function installAgentNotificationNavigation(navigate: (href: string) => v const handledResponseIds = new Set(); Notifications.setNotificationHandler({ handleNotification: (notification) => - Promise.resolve(foregroundNotificationBehavior(notification, deepLinkOnScreen)), + Promise.resolve( + foregroundNotificationBehavior(notification, deepLinkOnScreen, threadHasLiveFeed), + ), }); const handleResponse = (response: Notifications.NotificationResponse): void => {