Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): fence stale roster consumers

Dante8cb4f69bb717
diff --git a/apps/mobile/src/__tests__/historyViewProvider.test.tsx b/apps/mobile/src/__tests__/historyViewProvider.test.tsx
index 5181595316..566314bc30 100644
--- a/apps/mobile/src/__tests__/historyViewProvider.test.tsx
+++ b/apps/mobile/src/__tests__/historyViewProvider.test.tsx
@@ -154,6 +154,38 @@ describe('Provider device roster reads', () => {
expect(await joined).toEqual({ devices: [] });
expect(await fresh).toEqual({ devices: [desktop] });
});
+
+ it('prevents an older roster response from superseding a newer fresh read', async () => {
+ const stale = deferredRoster();
+ const current = deferredRoster();
+ auth.apiFetch.mockReturnValueOnce(stale.promise).mockReturnValueOnce(current.promise);
+ const client = transport.clients[0];
+
+ await act(async () => {
+ client.status = 'online';
+ client.statusChanged('online');
+ });
+ const joined = context.readDeviceList();
+ const fresh = context.readDeviceList({ fresh: true });
+
+ const offlineDesktop: DeviceView = {
+ deviceId: 'desktop', name: 'Desktop', platform: 'darwin', appVersion: 'test',
+ online: false, remoteControlEnabled: true, busy: false, lastSeenAt: new Date(1).toISOString(),
+ isSelf: false,
+ };
+ const onlineDesktop: DeviceView = { ...offlineDesktop, online: true };
+ await act(async () => {
+ stale.resolve({ devices: [offlineDesktop] });
+ });
+ expect(context.getPresenceAvailability('desktop')).toBeNull();
+ await act(async () => {
+ current.resolve({ devices: [onlineDesktop] });
+ });
+
+ expect(await joined).toEqual({ devices: [offlineDesktop] });
+ expect(await fresh).toEqual({ devices: [onlineDesktop] });
+ expect(context.getPresenceAvailability('desktop')).toBe(true);
+ });
});
describe('Provider network recovery priority', () => {
diff --git a/apps/mobile/src/device-link/DeviceLinkContext.tsx b/apps/mobile/src/device-link/DeviceLinkContext.tsx
index ecffc9e868..50ab1b3d02 100644
--- a/apps/mobile/src/device-link/DeviceLinkContext.tsx
+++ b/apps/mobile/src/device-link/DeviceLinkContext.tsx
@@ -376,6 +376,7 @@ export function DeviceLinkProvider({ children }: { children: ReactNode }) {
const presenceAvailableByDeviceRef = useRef(new Map<string, boolean>());
const rosterConsumerRef = useRef<(() => (devices: DeviceView[]) => void) | null>(null);
const rosterRequestRef = useRef<{ client: DeviceLinkClient | null; epoch: number; promise: Promise<{ devices: DeviceView[] }> } | null>(null);
+ const rosterRequestGenerationRef = useRef(0);
const presenceAvailabilityEpochsRef = useRef(createPresenceAvailabilityEpochs());
const presencePendingRecoveryDeviceIdsRef = useRef(new Set<string>());
const presenceUnavailableVerdictsRef = useRef(
@@ -404,10 +405,16 @@ export function DeviceLinkProvider({ children }: { children: ReactNode }) {
const epoch = connectionEpochRef.current;
const pending = rosterRequestRef.current;
if (!options.fresh && pending?.client === client && pending.epoch === epoch) return pending.promise;
+ const generation = ++rosterRequestGenerationRef.current;
const apply = rosterConsumerRef.current?.();
const promise = auth.apiFetch<{ devices: DeviceView[] }>('/api/device-link/devices', {
baseUrl: DEVICE_LINK_API_BASE_URL, timeoutMs: 12_000,
- }).then((result) => { apply?.(result.devices); return result; });
+ }).then((result) => {
+ // A fresh read supersedes every older roster consumer, even when the older
+ // response arrives first and would otherwise outrank the new snapshot.
+ if (rosterRequestGenerationRef.current === generation) apply?.(result.devices);
+ return result;
+ });
rosterRequestRef.current = { client, epoch, promise };
void promise.finally(() => {
if (rosterRequestRef.current?.promise === promise) rosterRequestRef.current = null;