Skip to content
TypeScript

fix(mobile): preserve notification responses when navigation fails (#22084)

Victor Vogel52df13ea9371
diff --git a/apps/mobile/src/features/agent-awareness/notificationNavigation.test.ts b/apps/mobile/src/features/agent-awareness/notificationNavigation.test.ts
index 92512bd2c3e..47aed622569 100644
--- a/apps/mobile/src/features/agent-awareness/notificationNavigation.test.ts
+++ b/apps/mobile/src/features/agent-awareness/notificationNavigation.test.ts
@@ -105,6 +105,56 @@ describe("extractAgentNotificationDeepLink", () => {
});
describe("routeAgentNotificationResponseOnce", () => {
+ it("retries a failed initial response and deduplicates it after navigation succeeds", async () => {
+ vi.spyOn(console, "error").mockImplementation(() => undefined);
+ const response = responseWithData({ deepLink: "/threads/env/thread" }) as NotificationResponse;
+ const handledResponseIds = new Set<string>();
+ const navigate = vi.fn().mockImplementationOnce(() => {
+ throw new Error("navigation not ready");
+ });
+ const clearLastResponse = vi.fn(() => Promise.resolve());
+ const consume = () =>
+ consumeLastAgentNotificationResponse({
+ getLastResponse: () => Promise.resolve(response),
+ clearLastResponse,
+ handleResponse: (response) =>
+ routeAgentNotificationResponseOnce({ handledResponseIds, response, navigate }),
+ });
+
+ await consume();
+ expect(handledResponseIds.size).toBe(0);
+ expect(clearLastResponse).not.toHaveBeenCalled();
+
+ await consume();
+ expect(navigate).toHaveBeenCalledTimes(2);
+ expect(navigate).toHaveBeenLastCalledWith("/threads/env/thread");
+ expect(clearLastResponse).toHaveBeenCalledOnce();
+
+ routeAgentNotificationResponseOnce({ handledResponseIds, response, navigate });
+ expect(navigate).toHaveBeenCalledTimes(2);
+ });
+
+ it("discards malformed destination ids without leaving the response pending", async () => {
+ const response = responseWithData({
+ environmentId: "env",
+ threadId: "\ud800",
+ }) as NotificationResponse;
+ const handledResponseIds = new Set<string>();
+ const navigate = vi.fn();
+ const clearLastResponse = vi.fn(() => Promise.resolve());
+
+ await consumeLastAgentNotificationResponse({
+ getLastResponse: () => Promise.resolve(response),
+ clearLastResponse,
+ handleResponse: (response) =>
+ routeAgentNotificationResponseOnce({ handledResponseIds, response, navigate }),
+ });
+
+ expect(navigate).not.toHaveBeenCalled();
+ expect(clearLastResponse).toHaveBeenCalledOnce();
+ expect(handledResponseIds.has("notification-1")).toBe(true);
+ });
+
it("does not navigate twice when the initial and listener responses refer to one notification", () => {
const handledResponseIds = new Set<string>();
const navigations: Array<string> = [];
diff --git a/apps/mobile/src/features/agent-awareness/notificationPayload.ts b/apps/mobile/src/features/agent-awareness/notificationPayload.ts
index a9dfe803693..a86d1c5c14b 100644
--- a/apps/mobile/src/features/agent-awareness/notificationPayload.ts
+++ b/apps/mobile/src/features/agent-awareness/notificationPayload.ts
@@ -41,7 +41,11 @@ function encodeThreadDeepLink(input: {
if (input.environmentId.length === 0 || input.threadId.length === 0) {
return null;
}
- return `/threads/${encodeURIComponent(input.environmentId)}/${encodeURIComponent(input.threadId)}`;
+ try {
+ return `/threads/${encodeURIComponent(input.environmentId)}/${encodeURIComponent(input.threadId)}`;
+ } catch {
+ return null;
+ }
}
function normalizeThreadDeepLink(value: string): string | null {
@@ -104,11 +108,11 @@ export function routeAgentNotificationResponseOnce(input: {
if (responseId && input.handledResponseIds.has(responseId)) {
return;
}
- if (responseId) {
- input.handledResponseIds.add(responseId);
- }
const deepLink = extractAgentNotificationDeepLink(input.response);
if (deepLink) {
input.navigate(deepLink);
}
+ if (responseId) {
+ input.handledResponseIds.add(responseId);
+ }
}