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。

Merge current main into mobile device discovery fix

Dante80a35a4db6a8
diff --cc apps/mobile/src/__tests__/historyViewProvider.test.tsx
index 66917fa568,cd68257d75..b0aab58ebf
--- a/apps/mobile/src/__tests__/historyViewProvider.test.tsx
+++ b/apps/mobile/src/__tests__/historyViewProvider.test.tsx
@@@ -122,75 -124,130 +132,199 @@@ beforeEach(async () =>
});
afterEach(async () => { await act(async () => root.unmount()); });
+describe('Provider device roster reads', () => {
+ it('starts a fresh snapshot when Home joins a recovery read that predates its presence fence', 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');
+ });
+ expect(auth.apiFetch).toHaveBeenCalledTimes(1);
+
+ await act(async () => client.presenceChanged({
+ deviceId: 'desktop', deviceName: 'Desktop', platform: 'darwin', appVersion: 'test',
+ online: true, remoteControlEnabled: true, busy: false, lastSeenAt: 1,
+ }));
+ const joined = context.readDeviceList();
+ expect(auth.apiFetch).toHaveBeenCalledTimes(1);
+ const fresh = context.readDeviceList({ fresh: true });
+ expect(auth.apiFetch).toHaveBeenCalledTimes(2);
+
+ const desktop: DeviceView = {
+ deviceId: 'desktop', name: 'Desktop', platform: 'darwin', appVersion: 'test',
+ online: true, remoteControlEnabled: true, busy: false, lastSeenAt: new Date(1).toISOString(),
+ isSelf: false,
+ };
+ await act(async () => {
+ stale.resolve({ devices: [] });
+ current.resolve({ devices: [desktop] });
+ });
+
+ 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('pending catalog recovery', () => {
+ it.each(['foreground', 'peer response'] as const)('wakes a blocked invalidation on %s before its long retry timer expires', async (recovery) => {
+ vi.useFakeTimers();
+ try {
+ const client = transport.clients[0];
+ client.openLink.mockResolvedValue(accepted(supported));
+ await act(async () => {
+ if (recovery === 'foreground') {
+ networkEvents.state = 'background'; networkEvents.app('background');
+ } else unresponsiveDevicesStore.markUnresponsive('host');
+ client.frame({ v: 1, kind: 'push', src: 'host', dst: 'mobile',
+ payload: { channel: 'maker:provider:changed', payload: {} } } as Parameters<typeof client.frame>[0]);
+ await vi.advanceTimersByTimeAsync(60000);
+ });
+ expect(client.invoke).not.toHaveBeenCalled();
+ await act(async () => {
+ if (recovery === 'foreground') {
+ networkEvents.state = 'active'; networkEvents.app('active');
+ } else unresponsiveDevicesStore.clearUnresponsive('host');
+ await vi.advanceTimersByTimeAsync(50);
+ });
+ expect(client.invoke).toHaveBeenCalledWith('host', expect.objectContaining({ channel: 'maker:provider:list' }), undefined);
+ expect(client.invoke).toHaveBeenCalledWith('host', expect.objectContaining({ channel: 'maker:get-capabilities' }), undefined);
+ } finally { unresponsiveDevicesStore.clearAll(); vi.useRealTimers(); }
+ });
+ });
+
+ describe('pending probe reply recovery', () => {
+ afterEach(() => {
+ resetDeviceResponsivenessTracking();
+ revokedDevicesStore.clearAll();
+ });
+ it.each(['relay reconnect', 'short background'] as const)('reopens the reply link after %s while the old business probe still owns its slot', async recovery => {
+ vi.useFakeTimers();
+ const client = transport.clients[0];
+ client.openLink.mockResolvedValue(accepted(supported));
+ let resolveProbe!: (value: { ok: true; result: string }) => void;
+ const probe = new Promise<{ ok: true; result: string }>(resolve => { resolveProbe = resolve; });
+ client.invoke.mockImplementation(() => probe);
+ try {
+ await act(async () => { await context.openLink('host'); });
+ await act(async () => {
+ for (let i = 0; i < 3; i++) settleDeviceSend('host', acquireDeviceSendSlot('host'), 'timeout');
+ await vi.advanceTimersByTimeAsync(14_000);
+ });
+ expect(client.invoke).toHaveBeenCalledExactlyOnceWith('host', expect.objectContaining({ channel: 'local-db:sessions:list' }), expect.any(Number));
+ expect(unresponsiveDevicesStore.has('host')).toBe(true);
+ client.hasPendingRequestsTo.mockImplementation(device => device === 'host');
+ const freshAccept = deferredAccept();
+ client.openLink.mockReturnValue(freshAccept.promise);
+ if (recovery === 'relay reconnect') {
+ await act(async () => { client.status = 'connecting'; client.statusChanged('connecting'); });
+ await act(async () => { client.status = 'online'; client.statusChanged('online'); });
+ } else {
+ // The relay stays online, so the successful pre-background handshake is still cached.
+ await act(async () => { networkEvents.state = 'background'; networkEvents.app('background'); });
+ await act(async () => { networkEvents.state = 'active'; networkEvents.app('active'); });
+ expect(client.stop).not.toHaveBeenCalled();
+ expect(client.restartConnection).not.toHaveBeenCalled();
+ }
+ // The serial recovery scheduler is still awaiting probe; only its reply link may reopen.
+ expect(client.openLink).toHaveBeenCalledTimes(2);
+ expect(client.invoke).toHaveBeenCalledTimes(1);
+ expect(unresponsiveDevicesStore.has('host')).toBe(true);
+ // Repeated foreground hints share both pending and accepted handshakes.
+ await act(async () => networkEvents.app('active'));
+ expect(client.openLink).toHaveBeenCalledTimes(2);
+ await act(async () => freshAccept.resolve(accepted(supported)));
+ await act(async () => networkEvents.app('active'));
+ expect(client.openLink).toHaveBeenCalledTimes(2);
+ expect(client.invoke).toHaveBeenCalledTimes(1);
+ await act(async () => {
+ client.hasPendingRequestsTo.mockReturnValue(false);
+ resolveProbe({ ok: true, result: 'probe response' });
+ });
+ expect(unresponsiveDevicesStore.has('host')).toBe(false);
+ } finally {
+ await act(async () => { resolveProbe({ ok: true, result: 'cleanup' }); });
+ vi.useRealTimers();
+ }
+ });
+
+ it.each(['no pending request', 'revoked', 'explicitly closed', 'background', 'settled before send', 'offline before send'] as const)(
+ 'does not bypass lifecycle guards: %s', async reason => {
+ vi.useFakeTimers();
+ const client = transport.clients[0];
+ client.openLink.mockResolvedValue(accepted(supported));
+ let resolveProbe!: (value: { ok: true; result: string }) => void;
+ client.invoke.mockImplementation(() => new Promise(resolve => { resolveProbe = resolve; }));
+ try {
+ await act(async () => { await context.openLink('host'); });
+ await act(async () => {
+ for (let i = 0; i < 3; i++) settleDeviceSend('host', acquireDeviceSendSlot('host'), 'timeout');
+ await vi.advanceTimersByTimeAsync(14_000);
+ });
+ expect(client.invoke).toHaveBeenCalledTimes(1);
+ client.hasPendingRequestsTo.mockReturnValue(reason !== 'no pending request');
+ await act(async () => {
+ client.status = 'connecting'; client.statusChanged('connecting');
+ if (reason === 'revoked') revokedDevicesStore.markRevoked('host');
+ if (reason === 'explicitly closed') client.isOutboundExplicitlyClosed.mockReturnValue(true);
+ if (reason === 'background') { networkEvents.state = 'background'; networkEvents.app('background'); }
+ client.status = 'online'; client.statusChanged('online');
+ // Mutate after the initial guard but before the async send continuation.
+ if (reason === 'settled before send') client.hasPendingRequestsTo.mockReturnValue(false);
+ if (reason === 'offline before send') { client.status = 'connecting'; client.statusChanged('connecting'); }
+ });
+ expect(client.openLink).toHaveBeenCalledTimes(1);
+ expect(client.invoke).toHaveBeenCalledTimes(1);
+ if (reason === 'background') {
+ await act(async () => { networkEvents.state = 'active'; networkEvents.app('active'); });
+ expect(client.openLink).toHaveBeenCalledTimes(2);
+ expect(client.invoke).toHaveBeenCalledTimes(1);
+ expect(unresponsiveDevicesStore.has('host')).toBe(true);
+ }
+ } finally {
+ client.hasPendingRequestsTo.mockReturnValue(false);
+ await act(async () => resolveProbe?.({ ok: true, result: 'cleanup' }));
+ vi.useRealTimers();
+ }
+ },
+ );
+ });
+
describe('Provider shared-task relay compatibility', () => {
it('distinguishes an old online relay from disconnection and re-negotiates after upgrade', async () => {
const client = transport.clients[0];