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(device-link): preserve backpressure for large queued results

DavidShen69e716f49ad2
diff --git a/docs/dev-rules/remote-and-mobile-adaptation.md b/docs/dev-rules/remote-and-mobile-adaptation.md
index 0430d09ae5..249fd516f3 100644
--- a/docs/dev-rules/remote-and-mobile-adaptation.md
+++ b/docs/dev-rules/remote-and-mobile-adaptation.md
@@ -124,6 +124,8 @@ socket 背压和聚合公平预算约束。慢速在途消息也可能得到一
不等于对端已收到,不能据此删除可靠副本;重连补发同 requestId 的结果,不重新执行
原操作。副本沿用容量、过期、撤权清理与恢复预算,不缩短业务超时或重启共享 relay。
队列被不可丢弃的在途帧占满时,保留失败仍须尝试原有裸帧直发,不扩容或驱逐在途帧。
+若结果可可靠分片、但超过裸帧上限,仍返回原 `BACKPRESSURE` 供上层 outbox 重试,
+不能误报 `PAYLOAD_TOO_LARGE` 并将原结果压缩或替换成错误。
实现见 `packages/device-link/src/client.ts` 的 `sendInvokeResult`,多控制端回归见同包
`src/__tests__/client.test.ts`。
diff --git a/packages/device-link/src/__tests__/client.test.ts b/packages/device-link/src/__tests__/client.test.ts
index 0de2642aca..26f240bab9 100644
--- a/packages/device-link/src/__tests__/client.test.ts
+++ b/packages/device-link/src/__tests__/client.test.ts
@@ -6445,6 +6445,31 @@ describe('DeviceLinkClient', () => {
}
});
+ it.each([
+ { megabytes: 3, code: 'BACKPRESSURE' },
+ { megabytes: 5, code: 'PAYLOAD_TOO_LARGE' },
+ ])('reports $code for a $megabytes MB result when the unlinked reliable queue is full', async ({ megabytes, code }) => {
+ 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 });
+ }
+ // 3 MB exceeds the legacy frame limit but fits reliable fragmentation.
+ // Only a result beyond the reliable message limit (5 MB) is truly oversized.
+ expect(() => h.client.sendInvokeResult('ctrl-1', 'large-result', {
+ ok: true, result: 'x'.repeat(megabytes * 1024 * 1024),
+ })).toThrow(expect.objectContaining({ code }));
+ expect(h.current().sent.some(env => env.id === 'large-result')).toBe(false);
+ } 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.ts
index 0684d8a71c..9f2f802090 100644
--- a/packages/device-link/src/client.ts
+++ b/packages/device-link/src/client.ts
@@ -1366,7 +1366,13 @@ export class DeviceLinkClient {
} catch (err) {
if (!(err instanceof DeviceLinkError && err.code === 'BACKPRESSURE')) throw err;
// 保留副本不能堵住既有死锁绕行;容量耗尽时仍尝试直发,不扩容或驱逐 live 帧。
- this.sendBestEffortRoutedEnvelope(env);
+ try {
+ this.sendBestEffortRoutedEnvelope(env);
+ } catch (directError) {
+ // 可分片的大结果只是不适合裸帧;保留队列拥塞错误,让上层 outbox 重试原结果。
+ if (directError instanceof DeviceLinkError && directError.code === 'PAYLOAD_TOO_LARGE') throw err;
+ throw directError;
+ }
peer.unlinkedLegacyResponseIds.delete(requestId);
return;
}