Skip to content

Commit 2799d13

Browse files
fix(mobile): a token lookup that overlaps a rotation keeps the rotated token
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 40dae02 commit 2799d13

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

‎apps/mobile/src/features/agent-awareness/remoteRegistration.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,32 @@ describe("makeRelayDeviceRegistrationRequest", () => {
775775

776776
expect(registrations).toMatchObject([{ preferences: { notificationsEnabled: true } }]);
777777
expect(getAgentAwarenessRegistrationStatus()).toBe("registered");
778+
779+
// A lookup that started before a rotation must not replace the rotated token.
780+
let resolveStaleLookup: (token: { type: "android"; data: string }) => void = () => {};
781+
let markLookupStarted: () => void = () => {};
782+
const lookupStarted = new Promise<void>((resolve) => {
783+
markLookupStarted = resolve;
784+
});
785+
vi.mocked(Notifications.getDevicePushTokenAsync).mockImplementationOnce(() => {
786+
markLookupStarted();
787+
return new Promise((resolve) => {
788+
resolveStaleLookup = resolve;
789+
});
790+
});
791+
const staleRefresh = yield* Effect.forkChild(refreshAgentAwarenessRegistration());
792+
yield* Effect.promise(() => lookupStarted);
793+
const listener = vi.mocked(Notifications.addPushTokenListener).mock.calls.at(-1)![0];
794+
listener({ type: "android", data: "rotated-fcm-token" } as never);
795+
yield* runBackgroundOperations();
796+
resolveStaleLookup({ type: "android", data: "fcm-token" });
797+
yield* Fiber.join(staleRefresh);
798+
vi.mocked(Notifications.getDevicePushTokenAsync).mockRejectedValueOnce(
799+
new Error("SERVICE_NOT_AVAILABLE"),
800+
);
801+
yield* refreshAgentAwarenessRegistration();
802+
803+
expect(registrations.at(-1)).toMatchObject({ pushToken: "rotated-fcm-token" });
778804
}).pipe(
779805
Effect.provideService(FetchHttpClient.Fetch, globalThis.fetch),
780806
Effect.provide(layerRelayTest),

‎apps/mobile/src/features/agent-awareness/remoteRegistration.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ const registeredActivityPushTokens = new Map<string, number>();
9696
let widgetSnapshotReads = 0;
9797
let androidDeviceReplayedAt: number | null = null;
9898
let lastNativePushToken: string | null = null;
99+
let nativePushTokenRotations = 0;
99100
let pushTokenSubscription: { remove: () => void } | null = null;
100101
let appStateSubscription: { remove: () => void } | null = null;
101102

@@ -293,12 +294,14 @@ const nativePushTokenRegistration = Effect.fn("remoteRegistration.nativePushToke
293294
: true);
294295
if (observedPushToken) {
295296
lastNativePushToken = observedPushToken;
297+
nativePushTokenRotations++;
296298
return {
297299
notificationsEnabled: alertsEnabled,
298300
pushToken: observedPushToken,
299301
notificationPermissionGranted: permissions.granted,
300302
};
301303
}
304+
const rotationsBeforeLookup = nativePushTokenRotations;
302305
const token = yield* Effect.tryPromise({
303306
try: () => Notifications.getDevicePushTokenAsync(),
304307
catch: (cause) =>
@@ -314,11 +317,15 @@ const nativePushTokenRegistration = Effect.fn("remoteRegistration.nativePushToke
314317
),
315318
Effect.orElseSucceed(() => null),
316319
);
317-
const pushToken =
320+
const lookedUpPushToken =
318321
token?.type === Platform.OS && typeof token.data === "string" && token.data.trim().length > 0
319322
? token.data.trim()
320-
: lastNativePushToken;
321-
lastNativePushToken = pushToken;
323+
: null;
324+
// A rotation observed while this lookup ran is newer than what the lookup returned.
325+
if (lookedUpPushToken && rotationsBeforeLookup === nativePushTokenRotations) {
326+
lastNativePushToken = lookedUpPushToken;
327+
}
328+
const pushToken = lastNativePushToken;
322329
// A missing token must not disable notifications the relay accepted.
323330
return {
324331
notificationsEnabled: alertsEnabled,
@@ -868,6 +875,7 @@ export function __resetAgentAwarenessRemoteRegistrationForTest(): void {
868875
registrationStatus = "unknown";
869876
androidDeviceReplayedAt = null;
870877
lastNativePushToken = null;
878+
nativePushTokenRotations = 0;
871879
registrationStatusListeners.clear();
872880
registeredActivityPushTokens.clear();
873881
activityPushTokenLocks.clear();

0 commit comments

Comments
 (0)