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): ignore placeholder controller presence names

Chris086fb2ac9579
diff --git a/apps/desktop/src/main/__tests__/deviceLinkVoiceCredentialBootstrap.test.ts b/apps/desktop/src/main/__tests__/deviceLinkVoiceCredentialBootstrap.test.ts
index 1d4daf8fb6..137de38ac1 100644
--- a/apps/desktop/src/main/__tests__/deviceLinkVoiceCredentialBootstrap.test.ts
+++ b/apps/desktop/src/main/__tests__/deviceLinkVoiceCredentialBootstrap.test.ts
@@ -71,6 +71,35 @@ describe('mobile voice credential sync desktop bootstrap path', () => {
);
});
+ it('presence 占位名或空名不覆盖已有展示名,无有效名时保留 dispatch 回退链', () => {
+ const deviceLinkHost = readFileSync(resolve(mainRoot, 'device-link/index.ts'), 'utf8');
+ const dispatch = readFileSync(resolve(mainRoot, 'device-link/dispatch.ts'), 'utf8');
+
+ const presenceHandler = deviceLinkHost.indexOf('client.onPresenceChanged');
+ const normalizePresenceName = deviceLinkHost.indexOf(
+ 'const presenceDisplayName = normalizeCachedDeviceName(snap.deviceName);',
+ presenceHandler,
+ );
+ const guardedDisplayNameUpdate = deviceLinkHost.indexOf(
+ 'if (presenceDisplayName) {\n setControllerDisplayName(snap.deviceId, presenceDisplayName);\n }',
+ normalizePresenceName,
+ );
+ expect(presenceHandler).toBeGreaterThanOrEqual(0);
+ expect(normalizePresenceName).toBeGreaterThan(presenceHandler);
+ expect(guardedDisplayNameUpdate).toBeGreaterThan(normalizePresenceName);
+ expect(deviceLinkHost.slice(presenceHandler, guardedDisplayNameUpdate)).not.toContain(
+ 'setControllerDisplayName(snap.deviceId, snap.deviceName)',
+ );
+
+ // guard 不写缓存时,dispatch 继续按「数据库名 → 自报名 → 短 ID」回退。
+ expect(dispatch).toContain(
+ 'return controllerDisplayNameByDevice.get(deviceId)\n ?? normalizedReportedName\n ?? reportedControllerNameByDevice.get(deviceId);',
+ );
+ expect(dispatch).toContain(
+ 'const displayName = normalized\n ?? reportedControllerNameByDevice.get(deviceId)\n ?? deviceId.slice(0, 8);',
+ );
+ });
+
it('keeps device-link:voice:credential-sync matched but rejected (feature removed, readable error for old mobile)', () => {
const dispatch = readFileSync(resolve(mainRoot, 'device-link/dispatch.ts'), 'utf8');
diff --git a/apps/desktop/src/main/device-link/index.ts b/apps/desktop/src/main/device-link/index.ts
index 609006318e..37d942736b 100644
--- a/apps/desktop/src/main/device-link/index.ts
+++ b/apps/desktop/src/main/device-link/index.ts
@@ -55,7 +55,7 @@ import {
shouldAbortTransportTimeoutReopen,
} from './transportTimeoutReopen';
import {
- isPlaceholderDeviceName,
+ normalizeCachedDeviceName,
readDeviceLinkSettings,
readLastKnownDeviceNames,
rememberLastKnownDeviceName,
@@ -173,12 +173,13 @@ async function refreshControllerDisplayNamesFromDirectory(generation: number): P
for (const device of result.devices ?? []) {
if (typeof device.deviceId !== 'string' || !device.deviceId.trim()) continue;
const deviceId = device.deviceId.trim();
- const serverName = typeof device.name === 'string' ? device.name.trim() : '';
- const hasDisplayName = !!serverName && !isPlaceholderDeviceName(serverName);
- const displayName = hasDisplayName ? serverName : cachedNames[deviceId];
+ const serverName = typeof device.name === 'string'
+ ? normalizeCachedDeviceName(device.name)
+ : null;
+ const displayName = serverName ?? cachedNames[deviceId];
if (!displayName) continue;
setControllerDisplayName(deviceId, displayName);
- if (hasDisplayName) void rememberLastKnownDeviceName(deviceId, serverName);
+ if (serverName) void rememberLastKnownDeviceName(deviceId, serverName);
}
} catch (err) {
// 目录补齐是展示层 best-effort;失败时保留控制帧自报名 / 短 ID 回退,不影响建链。
@@ -611,7 +612,10 @@ export function initDeviceLinkService(options: DeviceLinkServiceOptions = {}): v
// (review P2)。翻转判据统一为「观察到进入某状态(含从未知)即触发一次」。
if (!available && wasAvailable !== false) responsivenessTracker?.clearDevice(snap.deviceId);
setControllerPlatform(snap.deviceId, snap.platform);
- setControllerDisplayName(snap.deviceId, snap.deviceName);
+ const presenceDisplayName = normalizeCachedDeviceName(snap.deviceName);
+ if (presenceDisplayName) {
+ setControllerDisplayName(snap.deviceId, presenceDisplayName);
+ }
presenceNameByDevice.set(snap.deviceId, snap.selfName || snap.deviceName);
void rememberLastKnownDeviceName(snap.deviceId, snap.deviceName); // best-effort 名称缓存,不阻塞 presence 处理
broadcast(DEVICE_LINK_PUSH.PRESENCE_CHANGED, snap);