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): refresh quota after inbound peer recovery

qianboe4ae4c4e6332
diff --git a/apps/desktop/src/main/device-link/__tests__/dispatchWeakNetwork.test.ts b/apps/desktop/src/main/device-link/__tests__/dispatchWeakNetwork.test.ts
index 57f8da339e..fd9c1b57ad 100644
--- a/apps/desktop/src/main/device-link/__tests__/dispatchWeakNetwork.test.ts
+++ b/apps/desktop/src/main/device-link/__tests__/dispatchWeakNetwork.test.ts
@@ -70,6 +70,7 @@ import {
flushRemoteInvokeResultOutboxOnReconnect,
handleControllerOffline,
setControllersChangedListener,
+ setOrdinaryPeerLinkAcceptedListener,
setDispatchPresenceOfflineCheck,
wireInboundDispatch,
} from '../dispatch';
@@ -252,7 +253,25 @@ afterEach(() => {
});
describe('[1] link-accept 发送失败的有限重试', () => {
+ it('notifies only the successfully accepted ordinary peer after a failed inbound open', () => {
+ const accepted = vi.fn();
+ setOrdinaryPeerLinkAcceptedListener(accepted);
+ const sendLinkAccept = vi.fn().mockImplementationOnce(() => { throw backpressure(); });
+ const client = mkClient({ sendLinkAccept });
+ __testing.setActiveClient(client as never);
+ __testing.handleLinkOpen(client as never, 'ctrl-a', 'open-a', undefined);
+ expect(accepted).not.toHaveBeenCalled();
+ __testing.handleLinkOpen(client as never, 'ctrl-b', 'open-b', undefined);
+ expect(accepted.mock.calls).toEqual([['ctrl-b']]);
+ accepted.mockClear();
+ vi.advanceTimersByTime(500);
+ expect(accepted.mock.calls).toEqual([['ctrl-a']]);
+ expect(__testing.getActiveControllers().map(c => c.deviceId).sort()).toEqual(['ctrl-a', 'ctrl-b']);
+ });
+
it('refreshes first-join authority and ignores an older failed open after a newer success', async () => {
+ const accepted = vi.fn();
+ setOrdinaryPeerLinkAcceptedListener(accepted);
const client = mkClient();
__testing.setActiveClient(client as never);
const peer = sharedTaskGuestPeer('m', 'g', 'd');
@@ -269,6 +288,7 @@ describe('[1] link-accept 发送失败的有限重试', () => {
await Promise.resolve(); await Promise.resolve();
expect(client.closeLink).not.toHaveBeenCalled();
expect(client.sendLinkAccept).toHaveBeenCalledTimes(1);
+ expect(accepted).not.toHaveBeenCalled();
});
it('declares history projection support in the host accept, including for legacy controllers', () => {
const client = mkClient();
diff --git a/apps/desktop/src/main/device-link/dispatch.ts b/apps/desktop/src/main/device-link/dispatch.ts
index 3f62dca9b7..e455b0ba41 100644
--- a/apps/desktop/src/main/device-link/dispatch.ts
+++ b/apps/desktop/src/main/device-link/dispatch.ts
@@ -1001,6 +1001,12 @@ export function setSessionTextSnapshotReader(reader: typeof readSessionTextSnaps
readSessionTextSnapshot = reader;
}
+// Ordinary same-account inbound links only; shared/provider guests never notify.
+let onOrdinaryPeerLinkAccepted: ((deviceId: string) => void) | null = null;
+export function setOrdinaryPeerLinkAcceptedListener(cb: typeof onOrdinaryPeerLinkAccepted): void {
+ onOrdinaryPeerLinkAccepted = cb;
+}
+
export function setControllersChangedListener(cb: ControllersChangedListener | null): void {
onControllersChanged = cb;
}
@@ -2811,6 +2817,7 @@ function handleLinkOpen(
flushMakerEventBatchesFor(src);
drainOfflinePushQueueTo(src, [LEGACY_TOPIC]);
}
+ onOrdinaryPeerLinkAccepted?.(src);
log.info(`control link opened by ${shortId(src)} (${name})`);
}
@@ -4782,6 +4789,7 @@ export const __testing = {
controllerDisplayNameByDevice.clear();
reportedControllerNameByDevice.clear();
onSessionsSubscribed = null;
+ onOrdinaryPeerLinkAccepted = null;
readSessionTextSnapshot = 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 edb311512e..b5da731e24 100644
--- a/apps/desktop/src/main/device-link/index.ts
+++ b/apps/desktop/src/main/device-link/index.ts
@@ -88,6 +88,7 @@ import { createDnsFallbackLookup } from './dnsFallbackLookup';
import {
wireInboundDispatch,
setControllersChangedListener,
+ setOrdinaryPeerLinkAcceptedListener,
setRemoteInvokeBusyChangedListener,
dropAllControllers,
deactivateAllControllers,
@@ -971,6 +972,9 @@ export function initDeviceLinkService(options: DeviceLinkServiceOptions = {}): v
});
// 被控端:接线入站隧道(link-open / invoke / link-close → 本机 handler dispatch)
+ setOrdinaryPeerLinkAcceptedListener((deviceId) => {
+ broadcast(DEVICE_LINK_PUSH.PEER_LINK_REOPENED, { deviceId });
+ });
wireInboundDispatch(client);
// outbox flush 的 presence 显式离线门禁(运行期接线,模块顶层会撞 import 环 TDZ)
setDispatchPresenceOfflineCheck(isPresenceExplicitlyOffline);
@@ -1210,6 +1214,7 @@ export function initDeviceLinkService(options: DeviceLinkServiceOptions = {}): v
// 已由上面 stop() 的 onDemote 执行过一次,linkTornDown 标记拦截重复清理。
teardownActiveLink();
setControllersChangedListener(null);
+ setOrdinaryPeerLinkAcceptedListener(null);
setRemoteInvokeBusyChangedListener(null);
client = null;
});
diff --git a/docs/dev-rules/protocol-compatibility.md b/docs/dev-rules/protocol-compatibility.md
index 4eb8b3ad26..5947125ca9 100644
--- a/docs/dev-rules/protocol-compatibility.md
+++ b/docs/dev-rules/protocol-compatibility.md
@@ -1771,4 +1771,4 @@ PC 同源 ghostPermissionItems 声明摘要投影并排除 tool,以请求 loca
企业凭据迁移:`/quota` 查询不依赖本地推理凭据;仅在当前进程、当前账号已成功同步服务端凭据,且实际 endpoint 与本次同步一致时,允许 404/明确 501 后的旧直连回退。持久化凭据不能作为本轮同步成功的证明。网关 endpoint 或身份代次变化时,失效当前账号额度并丢弃旧代次在途结果;失效结果通过既有 null push 通知,可选状态读取返回 `{ quotaState: 'invalidated' }`,与 unavailable null 区分。新本地/远程消费者将 invalidated 和 unlimited 都作为权威清缓存;未携带能力参数的旧消费者保持 snapshot/null 返回。此处不变更凭据存储、推理回退或额度计算口径。
额度身份隔离使用主进程 `ownerGeneration` 与 `dataOwnerId`,覆盖同 membership 跨认证 realm 切换及同账号修复;凭据状态与额度快照使用同一身份键。
-远程 transport-timeout 退避重连成功后,Main 通过本地 `device-link:peer-link-reopened` 推送 `{ deviceId }`,preload 暴露可选 `onPeerLinkReopened`,仅目标设备触发排队补读。被取消的重连代次不发通知。该事件不进入 relay wire、不增加远端 IPC allowlist,也不改变 Mobile/旧消费者的请求协议。
+远程 transport-timeout 出站退避重连成功、或普通同账号 peer 入站 link-accept 成功后,Main 通过本地 `device-link:peer-link-reopened` 推送 `{ deviceId }`,preload 暴露可选 `onPeerLinkReopened`,仅目标设备触发排队补读。被取消的重连代次不发通知。该事件不进入 relay wire、不增加远端 IPC allowlist,也不改变 Mobile/旧消费者的请求协议。