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): 非队头的大消息发送前就按分片数拦下,把上限收成 max(预算, 队头分片数)
Dash507e1d9adb60
diff --git a/packages/device-link/src/__tests__/client.test.ts b/packages/device-link/src/__tests__/client.test.tsindex fc756caee6..d0e6e25e54 100644--- a/packages/device-link/src/__tests__/client.test.ts+++ b/packages/device-link/src/__tests__/client.test.ts@@ -4545,12 +4545,15 @@ describe('定时重发的单趟预算(TRANSPORT_RETRY_PASS_BUDGET)', () => {expect(framesSent(ws)).toBeGreaterThan(8);expect([...sendsBySeq(ws).values()]).toEqual([1, 1, 1, 1]);- // 预算 4 帧:第一条消息(3 片)发完还剩 1 帧 → 再发第二条后超预算停手;- // 关键是**不会**把 4 条全部重发。+ // 预算 4 帧、每条 3 片:队头那条发完(3 帧)后,第二条会超预算 → 发送前就被拦下,+ // 留到下一趟。所以本趟只重发队头 1 条、只写出 3 帧。+ const framesBefore = framesSent(ws);await advance(200);const retried = retriedSeqs(sendsBySeq(ws));- expect(retried.length).toBeLessThan(4);- expect(retried).toEqual(seqs.slice(0, retried.length));+ expect(retried).toEqual([seqs[0]]);+ // 本趟真实写出的帧数不超过 max(预算, 队头分片数) —— 这才是「上限」的准确表述+ const passFrames = framesSent(ws) - framesBefore;+ expect(passFrames).toBeLessThanOrEqual(4);}, {pingIntervalMs: 600_000,transportRetryIntervalMs: 200,@@ -4559,6 +4562,33 @@ describe('定时重发的单趟预算(TRANSPORT_RETRY_PASS_BUDGET)', () => {});}, 15_000);+ it('队头单条就超预算时:本趟只发它一条,后面的一条都不带(上限 = max(预算, 队头分片数))', async () => {+ // review 两位都指出「预算不是硬上限」。准确表述是 max(预算, 队头分片数):队头那条+ // 压不下去(分片不能跨趟拆,而它又必须先送到),但它绝不能顺带把后面的也拖出去。+ await withFakeTimers(async (h, advance) => {+ const chunky = 'x'.repeat(3 * 128 * 1024 + 1_000); // ~4 片,单条即超预算(2)+ h.client.sendInvokeResult('dev-b', 'big', { ok: true, result: chunky });+ for (let i = 0; i < 5; i += 1) {+ h.client.sendInvokeResult('dev-b', `small-${i}`, { ok: true, result: i });+ }+ const ws = h.current();+ const seqs = [...sendsBySeq(ws).keys()].sort((a, b) => a - b);+ const framesBefore = framesSent(ws);++ await advance(200);+ // 只有队头那条大消息被重发,5 条小消息一条都没被带出去+ expect(retriedSeqs(sendsBySeq(ws))).toEqual([seqs[0]]);+ // 溢出被限制在「队头这一条的分片数」内,而不是预算 + 队头+ const passFrames = framesSent(ws) - framesBefore;+ expect(passFrames).toBeLessThanOrEqual(6);+ }, {+ pingIntervalMs: 600_000,+ transportRetryIntervalMs: 200,+ transportRetryPassBudget: 2,+ transportMaxRetryAttempts: 50,+ });+ }, 15_000);+it('预算字段被显式塞成 undefined 时回退到常量默认,不是静默关掉限流', async () => {// copilot review:Partial<DeviceLinkTiming> 的「可选值直塞」会 spread 覆盖默认值,// Math.max(1, undefined) = NaN → 判据恒 false → 预算被静默关闭。diff --git a/packages/device-link/src/client.ts b/packages/device-link/src/client.tsindex 49f97d898d..8f65deebdd 100644--- a/packages/device-link/src/client.ts+++ b/packages/device-link/src/client.ts@@ -2787,11 +2787,18 @@ export class DeviceLinkClient {// 前移之后,后面的消息自然轮到(有交错用例锚定这两段行为)。//// 预算按**帧**计而不是按逻辑消息计:压垮 relay 的是帧数,而一条 4MB 消息会被分成- // 32 片,按消息计数会让 8 条预算放出 ~256 帧,等于没限(greptile P1)。分片不能跨趟- // 拆开(接收端要整条重组),所以每趟**至少发一条**,发完再按帧数结算是否超预算。- let framesLeft = opts.unlimited+ // 32 片,按消息计数会让 8 条预算放出 ~256 帧,等于没限(greptile P1)。+ //+ // 本趟实际上限是 **max(预算, 队头那一条消息的分片数)**,不是预算本身:+ // - 非队头的大消息**发送前**就按预估分片数拦下(不许挤爆本趟),留到下一趟;+ // - 队头那一条无法再压 —— 分片不能跨趟拆(接收端按 seq 整条重组),而它又必须先送+ // 到(累计 ACK 不推进,后面的 seq 谁也消费不了)。要把它也压进预算就得引入+ // per-message 分片游标与跨趟续传进度,那是可靠层的新机制,不在本 PR 范围;+ // 而且它是用「大消息交付延迟 ×N 趟」换「突发再小一点」,取舍并不明显更优。+ const budget = opts.unlimited? Number.POSITIVE_INFINITY: normalizeRetryPassBudget(this.timing.transportRetryPassBudget);+ let framesSpent = 0;for (const pending of peer.pending.values()) {if (!opts.ignoreInterval && now - pending.lastSentAt < this.timing.transportRetryIntervalMs) {continue;@@ -2800,6 +2807,10 @@ export class DeviceLinkClient {this.handleReliableRetryExhausted(dst, pending.seq);return;}+ // 发送前先按预估分片数结算:已经发过东西、且这一条会超预算时,把它留到下一趟。+ // 用预估而非真实编码结果是刻意的 —— 这是流控决策,不需要精确,重新编码一条 4MB+ // 消息只为数分片数不划算;发送后再用真实帧数扣减。+ if (framesSpent > 0 && framesSpent + this.estimateReliableFrameCount(pending) > budget) break;let sentFrames = 0;try {sentFrames = this.sendReliableFrames(peer, pending);@@ -2807,11 +2818,19 @@ export class DeviceLinkClient {this.log.debug(`reliable transport retry failed for ${dst.slice(0, 8)}`, err);break;}- framesLeft -= Math.max(1, sentFrames);- if (framesLeft <= 0) break;+ framesSpent += Math.max(1, sentFrames);+ if (framesSpent >= budget) break;}}+ /**+ * 预估一条 pending 消息会写出多少帧(流控用,不要求精确)。`pending.bytes` 是入队时+ * 量好的保留字节数,按分片上限向上取整即可;真实帧数由 sendReliableFrames 返回。+ */+ private estimateReliableFrameCount(pending: PendingReliableMessage): number {+ return Math.max(1, Math.ceil(pending.bytes / MAX_TRANSPORT_CHUNK_BYTES));+ }+private replayPending(dst: string, resumedLink = false): void {const peer = this.peerTransport.get(dst);if (!peer || !peer.reliable || !peer.linkReady || peer.pending.size === 0) return;