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(desktop): only refetch the device directory for relevant presence (#1799)

Zymonody7e9a326b1136b
diff --git a/apps/desktop/src/renderer/features/device-link/__tests__/presenceRefreshRelevance.test.ts b/apps/desktop/src/renderer/features/device-link/__tests__/presenceRefreshRelevance.test.ts
new file mode 100644
index 0000000000..986e25770d
--- /dev/null
+++ b/apps/desktop/src/renderer/features/device-link/__tests__/presenceRefreshRelevance.test.ts
@@ -0,0 +1,180 @@
+/**
+ * presenceRefreshRelevance —— presence 触发目录重拉的相关性判据(issue #1726)。
+ *
+ * 背景:relay 把 presence-changed 广播给同账号所有连接**含本机**(刻意保留的僵尸订阅回收
+ * 信号),而本机每开始 / 跑完一轮任务都翻转 busy。修复前 `onPresenceChanged(() => refresh())`
+ * 不做任何过滤,自己的 busy 绕一圈回来就触发一次全量 listDevices。
+ *
+ * 这里只测纯判据函数;订阅接线本身由 useDeviceLinkReconnectEpoch.test.ts 那类用例覆盖。
+ */
+import { describe, it, expect } from 'vitest';
+
+import { shouldRefreshForPresence } from '../useDeviceLinkDeviceList';
+
+function view(over: Partial<DeviceLinkDeviceView> = {}): DeviceLinkDeviceView {
+ return {
+ deviceId: 'peer-1',
+ name: 'Peer Mac',
+ platform: 'darwin',
+ appVersion: '0.1.27',
+ lastSeenAt: null,
+ online: true,
+ busy: false,
+ remoteControlEnabled: true,
+ controlEnabled: true,
+ isSelf: false,
+ ...over,
+ };
+}
+
+function presence(over: Partial<DeviceLinkPresenceSnapshot> = {}): DeviceLinkPresenceSnapshot {
+ return {
+ deviceId: 'peer-1',
+ online: true,
+ deviceName: 'Peer Mac',
+ platform: 'darwin',
+ appVersion: '0.1.27',
+ lastSeenAt: 1_000,
+ remoteControlEnabled: true,
+ busy: false,
+ ...over,
+ };
+}
+
+describe('shouldRefreshForPresence · 必须重拉', () => {
+ it('尚无首份目录(devices === null)', () => {
+ expect(shouldRefreshForPresence(null, presence(), 'ready')).toBe(true);
+ });
+
+ it('presence 指向目录里没有的设备(新机器上线)', () => {
+ expect(shouldRefreshForPresence([view()], presence({ deviceId: 'peer-新' }), 'ready')).toBe(true);
+ });
+
+ it('对端 online 翻转', () => {
+ expect(shouldRefreshForPresence([view({ online: true })], presence({ online: false }), 'ready')).toBe(true);
+ });
+
+ it('对端 remoteControlEnabled 翻转(「允许被控」开关)', () => {
+ expect(
+ shouldRefreshForPresence(
+ [view({ remoteControlEnabled: true })],
+ presence({ remoteControlEnabled: false }),
+ 'ready',
+ ),
+ ).toBe(true);
+ });
+
+ it('对端 platform 由 null 变成具体值 —— 决定移动端过滤,漏了会让手机留在切换栏', () => {
+ expect(shouldRefreshForPresence([view({ platform: null })], presence({ platform: 'ios' }), 'ready')).toBe(
+ true,
+ );
+ });
+
+ it('对端改名', () => {
+ expect(
+ shouldRefreshForPresence([view({ name: '旧名' })], presence({ deviceName: '新名' }), 'ready'),
+ ).toBe(true);
+ });
+});
+
+describe('shouldRefreshForPresence · 必须跳过', () => {
+ it('本机自 presence:busy 翻转不重拉(issue #1726 的主要浪费来源)', () => {
+ const self = [view({ deviceId: 'self-1', isSelf: true })];
+ expect(shouldRefreshForPresence(self, presence({ deviceId: 'self-1', busy: true }), 'ready')).toBe(false);
+ expect(shouldRefreshForPresence(self, presence({ deviceId: 'self-1', busy: false }), 'ready')).toBe(false);
+ });
+
+ it('本机自 presence:即便 online / remoteControlEnabled 也变了仍跳过 —— 本机不进切换栏', () => {
+ const self = [view({ deviceId: 'self-1', isSelf: true, online: true, remoteControlEnabled: true })];
+ expect(
+ shouldRefreshForPresence(
+ self,
+ presence({ deviceId: 'self-1', online: false, remoteControlEnabled: false }),
+ 'ready',
+ ),
+ ).toBe(false);
+ });
+
+ it('对端只有 busy / lastSeenAt / appVersion / deviceInfo 变化(心跳噪音)', () => {
+ expect(
+ shouldRefreshForPresence(
+ [view()],
+ presence({
+ busy: true,
+ lastSeenAt: 999_999,
+ appVersion: '0.1.28',
+ deviceInfo: { note: 'whatever' } as never,
+ }),
+ 'ready',
+ ),
+ ).toBe(false);
+ });
+
+ it('完全无变化的 presence', () => {
+ expect(shouldRefreshForPresence([view()], presence(), 'ready')).toBe(false);
+ });
+
+ it('名字仅首尾空白差异按未变(与 applyDeviceRename 的 trim 口径一致)', () => {
+ expect(
+ shouldRefreshForPresence(
+ [view({ name: 'Peer Mac' })],
+ presence({ deviceName: ' Peer Mac ' }),
+ 'ready',
+ ),
+ ).toBe(false);
+ });
+
+ it('platform 的 null 与空串等价,不算变化', () => {
+ expect(shouldRefreshForPresence([view({ platform: null })], presence({ platform: '' }), 'ready')).toBe(
+ false,
+ );
+ });
+});
+
+describe('shouldRefreshForPresence · 多设备目录只看命中的那一行', () => {
+ it('对端 A 的心跳不因对端 B 的状态差异而误判为需要重拉', () => {
+ const list = [
+ view({ deviceId: 'peer-a', name: 'A', online: true }),
+ view({ deviceId: 'peer-b', name: 'B', online: false }),
+ ];
+ expect(shouldRefreshForPresence(list, presence({ deviceId: 'peer-a', deviceName: 'A', busy: true }), 'ready')).toBe(
+ false,
+ );
+ // B 自己上线 → 命中 B 那一行,online 有差异,重拉。
+ expect(
+ shouldRefreshForPresence(
+ list,
+ presence({ deviceId: 'peer-b', deviceName: 'B', online: true }),
+ 'ready',
+ ),
+ ).toBe(true);
+ });
+});
+
+/**
+ * 上一次 listDevices 失败(此前已有快照 → devices 非空、requestState 停在 'error')。
+ * push 驱动 + 无轮询兜底,若此时仍按「字段没变」滤掉 presence,侧栏会被钉在错误/陈旧目录上
+ * 直到 status / control-target 事件或手动重试(review: codex P1 on PR #1799)。
+ */
+describe('shouldRefreshForPresence · 上一次请求失败时一律放行', () => {
+ it("requestStatus='error':本机 busy 自回声也当作重试机会", () => {
+ const self = [view({ deviceId: 'self-1', isSelf: true })];
+ expect(shouldRefreshForPresence(self, presence({ deviceId: 'self-1', busy: true }), 'error')).toBe(
+ true,
+ );
+ });
+
+ it("requestStatus='error':对端纯心跳(无相关字段变化)也放行", () => {
+ expect(shouldRefreshForPresence([view()], presence({ lastSeenAt: 999_999 }), 'error')).toBe(true);
+ });
+
+ it("requestStatus='loading':不叠加请求 —— 已有一笔在飞,失败后会落到 error 再由下条 presence 接管", () => {
+ const self = [view({ deviceId: 'self-1', isSelf: true })];
+ expect(
+ shouldRefreshForPresence(self, presence({ deviceId: 'self-1', busy: true }), 'loading'),
+ ).toBe(false);
+ expect(shouldRefreshForPresence([view()], presence({ lastSeenAt: 999_999 }), 'loading')).toBe(
+ false,
+ );
+ });
+});
diff --git a/apps/desktop/src/renderer/features/device-link/useDeviceLinkDeviceList.ts b/apps/desktop/src/renderer/features/device-link/useDeviceLinkDeviceList.ts
index 9933298201..dde520b1b5 100644
--- a/apps/desktop/src/renderer/features/device-link/useDeviceLinkDeviceList.ts
+++ b/apps/desktop/src/renderer/features/device-link/useDeviceLinkDeviceList.ts
@@ -95,6 +95,57 @@ function relevantEqual(a: DeviceLinkDeviceView[] | null, b: DeviceLinkDeviceView
return true;
}
+/** platform 在目录里可为 null、在 presence 里是字符串;比较前归一化,空值等价。 */
+function normalizePlatform(value: string | null | undefined): string {
+ return (value ?? '').trim();
+}
+
+/**
+ * 这条 presence 是否可能改变切换栏关心的字段 —— 不可能就别重拉整份目录(纯函数,可单测)。
+ *
+ * relay 把 presence-changed 广播给同账号所有连接、**含本机**(dispatch.ts 写明这是控制端崩溃 /
+ * 拔网后回收僵尸订阅的兜底信号,刻意保留)。而本机每开始 / 跑完一轮任务都会翻转 busy 并上报,
+ * 于是自己的 busy 绕一圈回来就触发一次全量 listDevices —— 开跑一次、跑完一次,纯浪费(issue #1726)。
+ *
+ * 判据与 `relevantEqual` 同源:只有切换栏真正消费的字段变化才值得重拉。`busy` / `lastSeenAt` /
+ * `appVersion` / `deviceInfo` 虽然在 `DeviceLinkDeviceView` 里,但 `relevantEqual` 刻意不比较
+ * 它们(「忽略 busy / lastSeenAt 等高频字段,避免无谓重渲染」)—— 既然它们变了也不会让快照替换,
+ * 为它们重拉一整份目录就纯是浪费。
+ *
+ * 刻意**不**比较 `controlEnabled`:它由本地 opt-out 决定、不在 presence 里,其改动走
+ * `control-target-changed` 事件的既有刷新路径。
+ *
+ * `isSelf` 直接跳过是安全的:`switcherDevices.ts` 明确「本机(isSelf)排除」,本机行的
+ * online / remoteControlEnabled 不参与列表内容。
+ */
+export function shouldRefreshForPresence(
+ current: readonly DeviceLinkDeviceView[] | null,
+ snap: DeviceLinkPresenceSnapshot,
+ requestStatus: DeviceLinkDeviceListStatus,
+): boolean {
+ // 上一次拉取失败(且此前已有快照 → `devices` 仍非空、状态停在 'error')时**一律放行**:
+ // 这条链路 push 驱动、没有轮询兜底,若此时还按「字段没变」把 presence 滤掉,一次瞬时 REST
+ // 失败就会把侧栏钉在错误 / 陈旧目录上,直到 status / control-target 事件或用户手动重试
+ // (review: codex P1)。presence 是这段时间里唯一稳定到达的信号,必须当成重试机会。
+ //
+ // 'loading' 不放行:那说明已有一笔在飞,`loadGeneration` 会让后到的结果胜出,再叠一次
+ // refresh 只是徒增请求;它若失败会落到 'error',下一条 presence 自然接管重试。
+ if (requestStatus === 'error') return true;
+ // 还没有首份目录 → 照常拉(这条 presence 可能正是「终于连上了」的信号)。
+ if (current === null) return true;
+ const row = current.find((d) => d.deviceId === snap.deviceId);
+ // 目录里没有这台设备 → 它是新出现的,必须重拉才能进列表。
+ if (row === undefined) return true;
+ // 自 presence 自回声:本机不进切换栏,重拉不会改变任何可见内容。
+ if (row.isSelf) return false;
+ return (
+ row.online !== snap.online ||
+ row.remoteControlEnabled !== snap.remoteControlEnabled ||
+ normalizePlatform(row.platform) !== normalizePlatform(snap.platform) ||
+ row.name.trim() !== snap.deviceName.trim()
+ );
+}
+
/**
* 就地改名(纯函数,可单测):返回把 `deviceId` 的 name 改成 `name`(trim 后)的新数组;
* 无需改动(空名 / null 列表 / 设备不在列表 / 名字未变)时**返回原引用**,调用方可用 `===` 判定 no-op。
@@ -238,7 +289,12 @@ function ensureStarted(): void {
};
refreshImpl = () => refresh(true);
// app 生命周期常驻(侧边栏始终有订阅者),不解绑监听。
- window.electronAPI.deviceLink.onPresenceChanged(() => refresh());
+ // 只有相关 presence 才重拉目录(见 shouldRefreshForPresence):本机 busy 自回声与对端的
+ // busy / lastSeenAt 心跳都不改变切换栏内容,却各触发一次全量 listDevices(issue #1726)。
+ window.electronAPI.deviceLink.onPresenceChanged((snap) => {
+ if (!shouldRefreshForPresence(devices, snap, requestState.status)) return;
+ refresh();
+ });
window.electronAPI.deviceLink.onStatusChanged((p) => {
linkStatusRevision += 1;
linkStatus = p.status;