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): distinguish null controller self names

Chris6c5f8888f658
diff --git a/apps/desktop/src/main/__tests__/deviceLinkControllerDisplayNameFreshness.test.ts b/apps/desktop/src/main/__tests__/deviceLinkControllerDisplayNameFreshness.test.ts
index 5353481758..17776c5eac 100644
--- a/apps/desktop/src/main/__tests__/deviceLinkControllerDisplayNameFreshness.test.ts
+++ b/apps/desktop/src/main/__tests__/deviceLinkControllerDisplayNameFreshness.test.ts
@@ -241,6 +241,45 @@ describe('controller display-name directory freshness', () => {
expect(forgetName).not.toHaveBeenCalled();
});
+ it('selfName 字段存在但为 null 时,有效 deviceName 仍是权威更新', () => {
+ const freshness = createControllerDisplayNameFreshnessTracker();
+ const requestEpoch = freshness.epoch;
+ const setDisplayName = vi.fn();
+ const rememberName = vi.fn();
+ const forgetName = vi.fn();
+
+ seedControllerDisplayNamesFromCache({ 'dev-1': '旧数据库名' }, freshness, setDisplayName);
+ setDisplayName.mockClear();
+
+ applyControllerDisplayNamePresence({
+ deviceId: 'dev-1',
+ name: '新数据库名',
+ selfName: null,
+ freshness,
+ normalizeName,
+ setDisplayName,
+ rememberName,
+ forgetName,
+ });
+ applyControllerDisplayNameDirectorySnapshot({
+ devices: [{ deviceId: 'dev-1', name: '旧数据库名' }],
+ cachedNames: { 'dev-1': '旧数据库名' },
+ freshness,
+ requestEpoch,
+ normalizeName,
+ setDisplayName,
+ rememberName,
+ forgetName,
+ });
+
+ expect(freshness.epoch).toBe(1);
+ expect(freshness.authoritativeNameByDevice.get('dev-1')).toBe('新数据库名');
+ expect(setDisplayName).toHaveBeenCalledTimes(1);
+ expect(setDisplayName).toHaveBeenCalledWith('dev-1', '新数据库名');
+ expect(rememberName).toHaveBeenCalledWith('dev-1', '新数据库名');
+ expect(forgetName).not.toHaveBeenCalled();
+ });
+
it('无权威名时主机名 presence 仅作内存回退,不阻断后到目录名', () => {
const freshness = createControllerDisplayNameFreshnessTracker();
const requestEpoch = freshness.epoch;
diff --git a/apps/desktop/src/main/__tests__/deviceLinkVoiceCredentialBootstrap.test.ts b/apps/desktop/src/main/__tests__/deviceLinkVoiceCredentialBootstrap.test.ts
index 6f55d9febb..d288ce3200 100644
--- a/apps/desktop/src/main/__tests__/deviceLinkVoiceCredentialBootstrap.test.ts
+++ b/apps/desktop/src/main/__tests__/deviceLinkVoiceCredentialBootstrap.test.ts
@@ -75,7 +75,9 @@ describe('mobile voice credential sync desktop bootstrap path', () => {
expect(deviceLinkHost).toContain(
'applyControllerDisplayNamePresence({',
);
- expect(deviceLinkHost).toContain('selfName: snap.selfName,');
+ expect(deviceLinkHost).toContain(
+ "Object.prototype.hasOwnProperty.call(snap, 'selfName')",
+ );
expect(deviceLinkHost).toContain('applyControllerDisplayNameDirectorySnapshot({');
});
diff --git a/apps/desktop/src/main/device-link/controllerDisplayNameFreshness.ts b/apps/desktop/src/main/device-link/controllerDisplayNameFreshness.ts
index b45fe18550..950ee740a7 100644
--- a/apps/desktop/src/main/device-link/controllerDisplayNameFreshness.ts
+++ b/apps/desktop/src/main/device-link/controllerDisplayNameFreshness.ts
@@ -55,7 +55,8 @@ function classifyControllerDisplayName(
/**
* presence.deviceName 可能是数据库展示名,也可能是旧 relay 直接转发的主机名:
- * - 现代 presence 携带 selfName,说明 deviceName 是 relay 已解析的当前展示名;
+ * - 现代 presence 携带 selfName 字段(值可为 null),说明 deviceName 是 relay
+ * 已解析的当前展示名;
* 即使两者恰好相同,也可能是用户刚把数据库名改成了自报名,必须权威更新;
* - 旧 presence 缺少 selfName 时无法区分展示名与主机名,只在无目录/缓存时临时回退;
* - 空名是显式清除,必须推进新鲜度以挡住在途旧目录响应;
@@ -73,9 +74,8 @@ export function applyControllerDisplayNamePresence(options: {
}): void {
const candidate = classifyControllerDisplayName(options.name, options.normalizeName);
if (candidate.kind === 'valid') {
- const hasResolvedSelfName =
- typeof options.selfName === 'string' && options.selfName.trim().length > 0;
- if (!hasResolvedSelfName) {
+ const hasSelfNameField = Object.prototype.hasOwnProperty.call(options, 'selfName');
+ if (!hasSelfNameField) {
if (options.freshness.authoritativeNameByDevice.has(options.deviceId)) return;
options.setDisplayName(options.deviceId, candidate.name);
return;
diff --git a/apps/desktop/src/main/device-link/index.ts b/apps/desktop/src/main/device-link/index.ts
index 100b22c7b1..e20e3c1f82 100644
--- a/apps/desktop/src/main/device-link/index.ts
+++ b/apps/desktop/src/main/device-link/index.ts
@@ -631,7 +631,9 @@ export function initDeviceLinkService(options: DeviceLinkServiceOptions = {}): v
applyControllerDisplayNamePresence({
deviceId: snap.deviceId,
name: snap.deviceName,
- selfName: snap.selfName,
+ ...(Object.prototype.hasOwnProperty.call(snap, 'selfName')
+ ? { selfName: snap.selfName }
+ : {}),
freshness: controllerDisplayNameFreshness,
normalizeName: normalizeCachedDeviceName,
setDisplayName: setControllerDisplayName,