Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(device-link): retain results completed during peer reconnect
DavidShen8624ca66af85
diff --git a/docs/dev-rules/remote-and-mobile-adaptation.md b/docs/dev-rules/remote-and-mobile-adaptation.mdindex 204abb8bfd..0430d09ae5 100644--- a/docs/dev-rules/remote-and-mobile-adaptation.md+++ b/docs/dev-rules/remote-and-mobile-adaptation.md@@ -119,6 +119,14 @@ socket 背压和聚合公平预算约束。慢速在途消息也可能得到一此修复只作用于该 peer,不重启共享 relay、不缩短业务超时,也不改变 wire 格式;多控制端故障隔离用例见 `packages/device-link/src/__tests__/client.test.ts`。+可靠请求已确认接收后,若异步结果在控制端断线期间才完成,回包必须保留在该 peer+原有的有界可靠队列中。链路未就绪时的裸帧直发仍用于避免等待建链的死锁,但直发成功+不等于对端已收到,不能据此删除可靠副本;重连补发同 requestId 的结果,不重新执行+原操作。副本沿用容量、过期、撤权清理与恢复预算,不缩短业务超时或重启共享 relay。+队列被不可丢弃的在途帧占满时,保留失败仍须尝试原有裸帧直发,不扩容或驱逐在途帧。+实现见 `packages/device-link/src/client.ts` 的 `sendInvokeResult`,多控制端回归见同包+`src/__tests__/client.test.ts`。+## 模块通过 Remote Resource 接入移动端面向移动端新增独立产品入口时,默认通过 `@cindy/device-link` 的 Remote Resourcediff --git a/packages/device-link/src/__tests__/client.test.ts b/packages/device-link/src/__tests__/client.test.tsindex 0719616b97..0de2642aca 100644--- a/packages/device-link/src/__tests__/client.test.ts+++ b/packages/device-link/src/__tests__/client.test.ts@@ -565,6 +565,62 @@ function makeRelayClient(}describe('DeviceLinkClient', () => {+ it('retains a result completed after the controller disconnects while another controller stays usable', async () => {+ vi.useFakeTimers();+ const relay = new MemoryRelay();+ const sockets = vi.spyOn(relay, 'makeWebSocket');+ const host = makeRelayClient(relay, 'desktop');+ const a = makeRelayClient(relay, 'viewer-a');+ const b = makeRelayClient(relay, 'viewer-b');+ const pump = () => relay.settle(async () => { await vi.advanceTimersByTimeAsync(1); });+ const received: Envelope[] = [];+ host.onFrame(env => {+ if (env.kind === 'link-open') host.sendLinkAccept(env.src!, env.id!, { appVersion: '1', allowlistHash: 'hash' });+ if (env.kind === 'invoke') received.push(env);+ });+ try {+ for (const client of [host, a, b]) client.start();+ await pump();+ for (const client of [a, b]) {+ const opening = client.openLink('desktop', { controllerName: 'Viewer', protocolVersion: 1, appVersion: '1' });+ await pump(); await opening;+ }+ let result: unknown;+ const history = a.invoke('desktop', { channel: 'local-db:messages:view', args: ['session'] });+ void history.then(value => { result = value; }, () => {});+ const healthy = b.invoke('desktop', { channel: 'local-db:sessions:get', args: ['session'] });+ void healthy.catch(() => {});+ await pump();+ const request = received.find(env => env.src === 'viewer-a')!;+ const other = received.find(env => env.src === 'viewer-b')!;+ relay.disconnect('viewer-a');+ const hostSocket = sockets.mock.results.find(entry => entry.value.ownerId === 'desktop')!.value;+ hostSocket.push({ v: PROTOCOL_VERSION, kind: 'relay-error', payload: {+ code: 'DEVICE_OFFLINE', message: 'target device offline', dst: 'viewer-a',+ } });+ // The request was ACKed before disconnection; its asynchronous result completes after it.+ host.sendInvokeResult('viewer-a', request.id!, { ok: true, result: 'history page' });+ host.sendInvokeResult('viewer-b', other.id!, { ok: true, result: 'healthy' });+ await pump();+ await expect(healthy).resolves.toEqual({ ok: true, result: 'healthy' });+ expect(result).toBeUndefined();+ await vi.advanceTimersByTimeAsync(25);+ await pump();+ expect(a.getStatus()).toBe('online');+ const reopening = a.openLink('desktop', { controllerName: 'Viewer', protocolVersion: 1, appVersion: '1' });+ await pump(); await reopening;+ expect(result).toEqual({ ok: true, result: 'history page' });+ expect(received.filter(env => env.src === 'viewer-a')).toHaveLength(1);+ expect(host.getStatus()).toBe('online');+ expect(hostSocket.closed).toBeNull();+ expect(b.isLinkReady('desktop')).toBe(true);+ expect(sockets.mock.results.filter(entry => entry.value.ownerId === 'viewer-b')).toHaveLength(1);+ } finally {+ for (const client of [host, a, b]) client.stop();+ vi.useRealTimers();+ }+ });+it('keeps a second controller link and in-flight invoke alive while shared host ICE config times out', async () => {vi.useFakeTimers();const relay = new MemoryRelay();@@ -6368,6 +6424,27 @@ describe('DeviceLinkClient', () => {h.client.stop();});+ it('keeps the direct result escape path usable when retained live replies fill the bounded queue', async () => {+ const h = makeHarness({ timing: {+ reconnectBaseMs: 5, reconnectMaxMs: 10,+ pingIntervalMs: 60_000, stalledLinkPendingMaxAgeMs: 60_000,+ } });+ h.client.start();+ try {+ await makeLinkDownPeer(h);+ for (let i = 0; i < MAX_TRANSPORT_PENDING_MESSAGES; i++) {+ h.client.sendInvokeResult('ctrl-1', `retained-${i}`, { ok: true, result: i });+ }+ expect(() => h.client.sendInvokeResult('ctrl-1', 'overflow-result', { ok: true, result: 42 })).not.toThrow();+ const direct = h.current().sent.find(env => env.id === 'overflow-result');+ expect(direct?.payload).toEqual({ ok: true, result: 42 });+ // Live retained replies were neither evicted nor allowed to exceed the existing cap.+ expect(() => h.client.sendPush('ctrl-1', 'non-discardable-event', {})).toThrow(/buffer is full/);+ } finally {+ h.client.stop();+ }+ });+it('A:link 断开状态下 pending 滞留超阈值 → 新帧入队前整队放弃,不再 BACKPRESSURE', async () => {const proto = DeviceLinkClient.prototype as unknown as { monotonicNow(): number };let nowMs = 1_000_000;diff --git a/packages/device-link/src/client.ts b/packages/device-link/src/client.tsindex fbd6b2d1c8..0684d8a71c 100644--- a/packages/device-link/src/client.ts+++ b/packages/device-link/src/client.ts@@ -1353,19 +1353,32 @@ export class DeviceLinkClient {sendInvokeResult(dst: string, requestId: string, payload: InvokeResultPayload): void {const peer = this.peerTransport.get(dst);const env: Envelope = { v: PROTOCOL_VERSION, kind: 'invoke-result', id: requestId, dst, payload };- // 死锁绕行:relay 在线但控制链路未就绪时,result 不进可靠队列等一个可能永远+ // 死锁绕行:relay 在线但控制链路未就绪时,result 不能只进可靠队列等一个可能永远// 不来的 link-accept(2026-08-03 线上实锤:队列冻结 30+ 分钟,每个执行成功的// 结果都等 120s 过期丢弃),改为 legacy 裸帧即时直发 —— 控制端按 id 配对- // 不依赖可靠层。送达失败(对端恰好离线)时对端本来就会超时,不比旧行为差。- // 超过单帧上限的大 result 只能靠可靠层分片,回落入队等 link 重建。+ // 不依赖可靠层。同时先保留有界可靠副本:原 invoke 可能已 ACK,若直发时+ // 对端刚好离线,请求不会重发,必须能在 link 重建后补回结果。+ // 超过单帧上限的大 result 只靠可靠层分片。重复结果按 requestId 配对,+ // 不会重新执行原操作;副本仍受原队列容量、过期和重放预算约束。if (peer?.reliable && !this.isPeerSendReady(peer) && this.status === 'online') {try {+ this.sendPeerEnvelope(env);+ } catch (err) {+ if (!(err instanceof DeviceLinkError && err.code === 'BACKPRESSURE')) throw err;+ // 保留副本不能堵住既有死锁绕行;容量耗尽时仍尝试直发,不扩容或驱逐 live 帧。this.sendBestEffortRoutedEnvelope(env);peer.unlinkedLegacyResponseIds.delete(requestId);return;+ }+ try {+ this.sendBestEffortRoutedEnvelope(env);+ peer.unlinkedLegacyResponseIds.delete(requestId);} catch (err) {- if (!(err instanceof DeviceLinkError && err.code === 'PAYLOAD_TOO_LARGE')) throw err;+ if (!(err instanceof DeviceLinkError && err.code === 'PAYLOAD_TOO_LARGE')) {+ this.log.debug('unlinked result direct send failed; reliable copy retained', err);+ }}+ return;}const allowClosedLegacyResponse = peer?.unlinkedLegacyResponseIds.has(requestId) === true;this.sendPeerEnvelope(env, allowClosedLegacyResponse);