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

Danteb8fe0424e4af
diff --cc apps/mobile/src/__tests__/historyViewProvider.test.tsx
index 3407e76df9,a1a3235370..86cc419176
--- a/apps/mobile/src/__tests__/historyViewProvider.test.tsx
+++ b/apps/mobile/src/__tests__/historyViewProvider.test.tsx
@@@ -132,150 -128,39 +136,183 @@@ 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('fences stale rosters without disturbing another device or a second controller sharing the host', async () => {
+ const secondRoot = createRoot(document.createElement('div'));
+ let secondContext!: DeviceLinkContextValue;
+ function SecondProbe() { secondContext = useDeviceLink(); return null; }
+ await act(async () => secondRoot.render(createElement(DeviceLinkProvider, null, createElement(SecondProbe))));
+ try {
+ const client = transport.clients[0];
+ const secondClient = transport.clients[1];
+ const stale = deferredRoster();
+ const current = deferredRoster();
+ auth.apiFetch.mockReturnValueOnce(stale.promise).mockReturnValueOnce(current.promise);
+ client.openLink.mockResolvedValue(accepted(supported));
+ secondClient.openLink.mockResolvedValue(accepted(supported));
+ let settleOtherDevice!: (value: { ok: true; result: string }) => void;
+ let settleOtherController!: (value: { ok: true; result: string }) => void;
+ client.invoke.mockImplementationOnce(() => new Promise(resolve => { settleOtherDevice = resolve; }));
+ secondClient.invoke.mockImplementationOnce(() => new Promise(resolve => { settleOtherController = resolve; }));
+ const presence = (deviceId: string): PresenceSnapshot => ({
+ deviceId, deviceName: deviceId, platform: 'darwin', appVersion: 'test',
+ online: true, remoteControlEnabled: true, busy: false, lastSeenAt: 1,
+ });
+ // Seed authoritative presence before opening links: a real presence event
+ // intentionally invalidates that device's cached handshake. Roster fencing
+ // itself must leave these accepted links and outstanding calls untouched.
+ await act(async () => {
+ client.presenceChanged(presence('other-host'));
+ secondClient.presenceChanged(presence('desktop'));
+ });
+ let otherDeviceRead!: Promise<unknown>;
+ let otherControllerRead!: Promise<unknown>;
+ await act(async () => {
+ otherDeviceRead = context.invoke('other-host', historyChannel);
+ otherControllerRead = secondContext.invoke('desktop', historyChannel);
+ });
+ expect(client.invoke).toHaveBeenCalledTimes(1);
+ expect(secondClient.invoke).toHaveBeenCalledTimes(1);
+ const oldRead = context.readDeviceList();
+ const freshRead = context.readDeviceList({ fresh: true });
+ const row = (deviceId: string, online: boolean): DeviceView => ({
+ deviceId, name: deviceId, platform: 'darwin', appVersion: 'test',
+ online, remoteControlEnabled: true, busy: false, lastSeenAt: new Date(1).toISOString(), isSelf: false,
+ });
+ const staleRows = [row('desktop', false), row('other-host', false)];
+ await act(async () => stale.resolve({ devices: staleRows }));
+ expect(await oldRead).toEqual({ devices: staleRows });
+ expect(context.getPresenceAvailability('desktop')).toBeNull();
+ expect(context.getPresenceAvailability('other-host')).toBe(true);
+ expect(secondContext.getPresenceAvailability('desktop')).toBe(true);
+ const currentRows = [row('desktop', true), row('other-host', true)];
+ await act(async () => current.resolve({ devices: currentRows }));
+ expect(await freshRead).toEqual({ devices: currentRows });
+ expect(context.getPresenceAvailability('desktop')).toBe(true);
+ expect(context.getPresenceAvailability('other-host')).toBe(true);
+ expect(secondContext.getPresenceAvailability('desktop')).toBe(true);
+ await act(async () => {
+ settleOtherDevice({ ok: true, result: 'other device result' });
+ settleOtherController({ ok: true, result: 'other controller result' });
+ });
+ expect(await otherDeviceRead).toBe('other device result');
+ expect(await otherControllerRead).toBe('other controller result');
+ // Reading again reuses each accepted handshake rather than reopening links.
+ await act(async () => {
+ await context.invoke('other-host', historyChannel);
+ await secondContext.invoke('desktop', historyChannel);
+ });
+ for (const peer of [client, secondClient]) {
+ expect(peer.openLink).toHaveBeenCalledTimes(1);
+ expect(peer.invoke).toHaveBeenCalledTimes(2);
+ expect(peer.stop).not.toHaveBeenCalled();
+ expect(peer.restartConnection).not.toHaveBeenCalled();
+ expect(peer.connectNow).not.toHaveBeenCalled();
+ }
+ } finally { await act(async () => secondRoot.unmount()); }
+ });
+
+ 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 queued send guards', () => {
+ it('rejects a write cancelled after admission without sending or counting a remote timeout', async () => {
+ resetDeviceResponsivenessTracking();
+ const client = transport.clients[0];
+ const send = vi.fn();
+ const cancelled = new DeviceLinkError('INVOKE_TIMEOUT', 'local operation expired');
+ // Two actual remote timeouts: a third would open the breaker if the local
+ // guard rejection were incorrectly classified as a failed remote request.
+ for (let i = 0; i < 2; i++) settleDeviceSend('host', acquireDeviceSendSlot('host'), 'timeout');
+ let current = true;
+ let dispatch!: () => void;
+ client.invoke.mockImplementation((_deviceId, _payload, _timeout, options) => new Promise((resolve, reject) => {
+ dispatch = () => {
+ try { options?.preSend?.(); send(); resolve({ ok: true, result: null }); }
+ catch (error) { reject(error); }
+ };
+ }));
+ let result!: Promise<unknown>;
+ await act(async () => {
+ result = context.invoke('host', 'maker:send', [], {
+ preSend: () => { if (!current) throw cancelled; },
+ }).catch(error => error);
+ });
+ expect(client.invoke).toHaveBeenCalledTimes(1);
+ current = false;
+ await act(async () => dispatch());
+ expect(await result).toBe(cancelled);
+ expect(send).not.toHaveBeenCalled();
+ expect(unresponsiveDevicesStore.getSnapshot().has('host')).toBe(false);
+ resetDeviceResponsivenessTracking();
+ });
+ });
+
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();