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): align remote controller display names

Chris5e29a3bb3ded
diff --git a/apps/desktop/src/main/__tests__/deviceLinkDispatch.test.ts b/apps/desktop/src/main/__tests__/deviceLinkDispatch.test.ts
index 8846eae34e..faf4b6e5d4 100644
--- a/apps/desktop/src/main/__tests__/deviceLinkDispatch.test.ts
+++ b/apps/desktop/src/main/__tests__/deviceLinkDispatch.test.ts
@@ -448,6 +448,7 @@ import {
setControllersChangedListener,
setRemoteInvokeBusyChangedListener,
setSessionsSubscribedListener,
+ setControllerDisplayName,
getActiveControllers,
getUpdateRelaunchControllers,
hasInFlightRemoteInvokes,
@@ -539,6 +540,52 @@ describe('被控端控制链路生命周期', () => {
expect(hasBroadcastTapListener()).toBe(false);
});
+ it('被控提示优先使用 presence 的数据库展示名,并在重命名后立即更新', () => {
+ remoteControlEnabled = true;
+ const changes: ActiveController[][] = [];
+ setControllersChangedListener((controllers) => changes.push(controllers));
+ const { client, feed } = makeFakeClient();
+ wireInboundDispatch(client);
+
+ // presence 先到:控制帧仍携带设备自报主机名,但展示应以数据库名为准。
+ setControllerDisplayName('ctrl-a', 'MacBook-Pro-2');
+ feed(subFrame('ctrl-a', SUB, ['session:s1'], 'Chriss-MacBook-Pro-2.local'));
+ expect(getActiveControllers()).toEqual([
+ { deviceId: 'ctrl-a', name: 'MacBook-Pro-2' },
+ ]);
+
+ // 设置页改名会广播新 presence;活跃横幅无需等重连或重订阅即可更新。
+ setControllerDisplayName('ctrl-a', '工作电脑');
+ expect(getActiveControllers()).toEqual([{ deviceId: 'ctrl-a', name: '工作电脑' }]);
+ expect(changes.at(-1)).toEqual([{ deviceId: 'ctrl-a', name: '工作电脑' }]);
+ });
+
+ it('数据库展示名为空或被清空时回退到控制端自报名,再缺失时回退到设备 ID 短码', () => {
+ remoteControlEnabled = true;
+ const { client, feed } = makeFakeClient();
+ wireInboundDispatch(client);
+
+ setControllerDisplayName('ctrl-reported', '数据库名称');
+ feed(subFrame('ctrl-reported', SUB, ['session:s1'], 'Host.local'));
+ expect(getActiveControllers()).toContainEqual({
+ deviceId: 'ctrl-reported',
+ name: '数据库名称',
+ });
+
+ setControllerDisplayName('ctrl-reported', ' ');
+ expect(getActiveControllers()).toContainEqual({
+ deviceId: 'ctrl-reported',
+ name: 'Host.local',
+ });
+
+ setControllerDisplayName('1234567890abcdef', '');
+ feed(subFrame('1234567890abcdef', SUB, ['session:s2']));
+ expect(getActiveControllers()).toContainEqual({
+ deviceId: '1234567890abcdef',
+ name: '12345678',
+ });
+ });
+
it('link-open(开关关)→ 不 accept、不记录', () => {
remoteControlEnabled = false;
const { client, calls, feed } = makeFakeClient();
diff --git a/apps/desktop/src/main/__tests__/deviceLinkSubscriptions.test.ts b/apps/desktop/src/main/__tests__/deviceLinkSubscriptions.test.ts
index a5922eda0f..aa1b0ef2b6 100644
--- a/apps/desktop/src/main/__tests__/deviceLinkSubscriptions.test.ts
+++ b/apps/desktop/src/main/__tests__/deviceLinkSubscriptions.test.ts
@@ -86,4 +86,13 @@ describe('subscriptions registry', () => {
subs.subscribe('dev-1234567890', ['session:s2'], 'RealName');
expect(subs.getControlControllers()[0].name).toBe('RealName');
});
+
+ it('updateControllerMetadata 只更新已有控制端并报告展示名是否变化', () => {
+ expect(subs.updateControllerMetadata('missing', 'Ignored')).toBe(false);
+
+ subs.subscribe('ctrl', ['session:s1'], 'Old');
+ expect(subs.updateControllerMetadata('ctrl', 'Old')).toBe(false);
+ expect(subs.updateControllerMetadata('ctrl', 'New')).toBe(true);
+ expect(subs.getControlControllers()).toEqual([{ deviceId: 'ctrl', name: 'New' }]);
+ });
});
diff --git a/apps/desktop/src/main/device-link/dispatch.ts b/apps/desktop/src/main/device-link/dispatch.ts
index 001ba83475..15196182dd 100644
--- a/apps/desktop/src/main/device-link/dispatch.ts
+++ b/apps/desktop/src/main/device-link/dispatch.ts
@@ -555,6 +555,14 @@ const remoteInvokeLinkEpoch = new Map<string, number>();
const topicSubscriptionControllers = new Set<string>();
/** 已成功 accept、尚未显式 close 的控制端;可无 active topic(现代重连等待 subscribe)。 */
const acceptedLinkControllers = new Set<string>();
+/**
+ * relay presence 按 server 盖章的 deviceId 提供设备数据库展示名。它比控制端在
+ * link-open / subscribe 里自报的主机名更权威,用于让被控提示与设备列表一致。
+ * 仅作展示,不参与任何授权判断;账号 / 链路边界由 host 显式清空。
+ */
+const controllerDisplayNameByDevice = new Map<string, string>();
+/** 控制帧自报名称仅作数据库展示名缺失时的兼容回退。 */
+const reportedControllerNameByDevice = new Map<string, string>();
/** `sessions` 订阅出现时通知 host replay 当前列表级轻量状态。 */
type SessionsSubscribedListener = (controllerDeviceId: string) => void;
@@ -574,6 +582,46 @@ export function setSessionsSubscribedListener(cb: SessionsSubscribedListener | n
onSessionsSubscribed = cb;
}
+function normalizeControllerName(value: unknown): string | undefined {
+ return typeof value === 'string' && value.trim()
+ ? value.trim().slice(0, MAX_CONTROLLER_NAME_LEN)
+ : undefined;
+}
+
+function resolveControllerName(deviceId: string, reportedName: unknown): string | undefined {
+ const normalizedReportedName = normalizeControllerName(reportedName);
+ if (normalizedReportedName) {
+ reportedControllerNameByDevice.set(deviceId, normalizedReportedName);
+ }
+ return controllerDisplayNameByDevice.get(deviceId)
+ ?? normalizedReportedName
+ ?? reportedControllerNameByDevice.get(deviceId);
+}
+
+/**
+ * presence 设备名变化(含设置页重命名)时更新展示真相;已有活跃订阅立即重发
+ * controlled-state,让横幅不用等下一次 subscribe / 重连才改名。
+ */
+export function setControllerDisplayName(deviceId: string, name: string): void {
+ const normalized = normalizeControllerName(name);
+ if (normalized) {
+ if (controllerDisplayNameByDevice.get(deviceId) === normalized) return;
+ controllerDisplayNameByDevice.set(deviceId, normalized);
+ } else {
+ if (!controllerDisplayNameByDevice.delete(deviceId)) return;
+ }
+ const displayName = normalized
+ ?? reportedControllerNameByDevice.get(deviceId)
+ ?? deviceId.slice(0, 8);
+ if (subscriptions.updateControllerMetadata(deviceId, displayName)) syncForwarding();
+}
+
+/** 账号切换 / 链路 teardown 时清空 presence 展示名,避免串到下一段身份。 */
+export function clearControllerDisplayNames(): void {
+ controllerDisplayNameByDevice.clear();
+ reportedControllerNameByDevice.clear();
+}
+
export function getActiveControllers(): ActiveController[] {
return subscriptions.getControlControllers();
}
@@ -1623,10 +1671,7 @@ function handleLinkOpen(
client.closeLink(src, 'revoked', 'inbound');
return;
}
- const name =
- typeof payload?.controllerName === 'string' && payload.controllerName.trim()
- ? payload.controllerName.trim().slice(0, MAX_CONTROLLER_NAME_LEN)
- : src.slice(0, 8);
+ const name = resolveControllerName(src, payload?.controllerName) ?? src.slice(0, 8);
// 老控制端无 subscribe 能力:link-open 视作订阅 legacy '*'(全量转发 + 横幅),向后兼容。
// 已在当前 link 上证明支持 topic 的客户端可能重复 open;不能重新装回兼容 wildcard。
const capabilities = sanitizeControllerCapabilities(payload?.capabilities);
@@ -2578,10 +2623,7 @@ function handleSubscriptionFrame(src: string, payload: InvokePayload): InvokeRes
: [];
// legacy `'*'`(全量 firehose + 点亮被控横幅)只允许走 link-open 路径,不接受 subscribe 帧
// 携带 —— 上面 filter 已剔除,防控制端一帧订全部会话流。
- const name =
- typeof o.controllerName === 'string' && o.controllerName.trim()
- ? o.controllerName.trim().slice(0, MAX_CONTROLLER_NAME_LEN)
- : undefined;
+ const name = resolveControllerName(src, o.controllerName);
const isSub = payload.channel === DL_SUBSCRIBE_CHANNEL;
if (isSub) {
// link-open provisionally installs legacy '*' for old clients. A non-empty modern subscribe
@@ -2901,6 +2943,8 @@ export const __testing = {
remoteInvokeLinkEpoch.clear();
topicSubscriptionControllers.clear();
acceptedLinkControllers.clear();
+ controllerDisplayNameByDevice.clear();
+ reportedControllerNameByDevice.clear();
onSessionsSubscribed = null;
activeClient = null;
offlinePushQueue.clear();
diff --git a/apps/desktop/src/main/device-link/index.ts b/apps/desktop/src/main/device-link/index.ts
index 01089eb22e..2b539c4fa7 100644
--- a/apps/desktop/src/main/device-link/index.ts
+++ b/apps/desktop/src/main/device-link/index.ts
@@ -72,6 +72,8 @@ import {
forgetControllerInvokeState,
handleControllerOffline,
purgeRevokedController,
+ setControllerDisplayName,
+ clearControllerDisplayNames,
setDispatchPresenceOfflineCheck,
} from './dispatch';
import {
@@ -555,6 +557,7 @@ 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);
presenceNameByDevice.set(snap.deviceId, snap.selfName || snap.deviceName);
void rememberLastKnownDeviceName(snap.deviceId, snap.deviceName); // best-effort 名称缓存,不阻塞 presence 处理
broadcast(DEVICE_LINK_PUSH.PRESENCE_CHANGED, snap);
@@ -947,6 +950,7 @@ function teardownActiveLink(): void {
// client 为 null 时 sendPush 也是 no-op。
presenceOnlineByDevice.clear();
clearControllerPlatforms();
+ clearControllerDisplayNames();
presenceNameByDevice.clear();
resetSubscriptionRefs();
resetBusyDedupe(); // 重置 busy dedupe,避免重连后首个真实 busy 状态被旧值压掉
diff --git a/apps/desktop/src/main/device-link/subscriptions.ts b/apps/desktop/src/main/device-link/subscriptions.ts
index 99afbaca6d..772e9e7189 100644
--- a/apps/desktop/src/main/device-link/subscriptions.ts
+++ b/apps/desktop/src/main/device-link/subscriptions.ts
@@ -142,7 +142,7 @@ export function updateControllerMetadata(
deviceId: string,
name: string,
capabilities?: readonly string[],
-): void {
+): boolean {
const e = registry.get(deviceId);
if (capabilities) {
const normalized = normalizeCapabilities(capabilities);
@@ -151,7 +151,9 @@ export function updateControllerMetadata(
}
// Do not recreate a topic entry during link-open; modern controllers must still
// replay subscribe before their remembered topics become active.
- if (e) e.name = name;
+ if (!e || e.name === name) return false;
+ e.name = name;
+ return true;
}
export function controllerHasTopic(deviceId: string, topic: string): boolean {