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): 新一轮发送时清空首次发送时间,避免离线时长进入往返估计

Dash56067723723d
diff --git a/packages/device-link/src/__tests__/client.test.ts b/packages/device-link/src/__tests__/client.test.ts
index c8271fb9d9..eec936840d 100644
--- a/packages/device-link/src/__tests__/client.test.ts
+++ b/packages/device-link/src/__tests__/client.test.ts
@@ -9825,6 +9825,64 @@ describe('定时重发的单趟预算(TRANSPORT_RETRY_PASS_BUDGET)', () => {
});
}, 10_000);
+ it('重连后未在本轮重发就被确认的消息不把离线时长算进往返估计', async () => {
+ await withFakeTimers(async (h, advance) => {
+ const internals = h.client as unknown as {
+ monotonicNow(): number;
+ peerTransport: Map<string, { sendPhase: string; receiveReady: boolean; rtt: { srttMs: number | null } }>;
+ };
+ const realNow = internals.monotonicNow.bind(h.client);
+ let offset = 0;
+ internals.monotonicNow = () => realNow() + offset;
+ h.client.sendInvokeResult('dev-b', 'before-0', { ok: true, result: 0 });
+ h.client.sendInvokeResult('dev-b', 'before-1', { ok: true, result: 1 });
+ const ws = h.current();
+ const seqs = [...sendsBySeq(ws).keys()].sort((a, b) => a - b);
+ const streamId = parseTransportPayload(ws.sent.find((env) => env.kind === 'invoke-result')!.payload)!.meta.streamId;
+ internals.peerTransport.get('dev-b')!.sendPhase = 'down';
+ internals.peerTransport.get('dev-b')!.receiveReady = false;
+ offset = 60_000; // long offline gap
+
+ const id = `inbound-link-${++inboundLinkId}`;
+ const off = h.client.onFrame((env) => {
+ if (env.kind !== 'link-open' || env.id !== id || !env.src) return;
+ h.client.sendLinkAccept(env.src, env.id, { appVersion: '1', allowlistHash: 'hash' });
+ });
+ ws.push({
+ v: PROTOCOL_VERSION,
+ kind: 'link-open',
+ id,
+ src: 'dev-b',
+ payload: {
+ controllerName: 'Remote',
+ protocolVersion: 1,
+ appVersion: '1',
+ capabilities: [DEVICE_LINK_CAPABILITY_RELIABLE_TRANSPORT, DEVICE_LINK_CAPABILITY_TRANSPORT_TIMEOUT_CLOSE],
+ transportStreamId: 'remote-stream-2',
+ transportBaseSeq: 1,
+ },
+ });
+ await advance(1);
+ off();
+ // The replay budget only re-wrote the head; the tail was delivered before the drop.
+ expect(seqs.map((seq) => sendsBySeq(ws).get(seq))).toEqual([2, 1]);
+ ws.push({
+ v: PROTOCOL_VERSION,
+ kind: 'push',
+ src: 'dev-b',
+ payload: { channel: DEVICE_LINK_TRANSPORT_ACK_CHANNEL, payload: { streamId, ackSeq: seqs[1] } },
+ });
+ await advance(1);
+ const srtt = internals.peerTransport.get('dev-b')!.rtt.srttMs;
+ expect(srtt === null || srtt < 1_000).toBe(true);
+ }, {
+ pingIntervalMs: 600_000,
+ transportRetryIntervalMs: 10_000,
+ transportRetryPassBudget: 1,
+ transportMaxRetryAttempts: 50,
+ });
+ }, 10_000);
+
it('多 peer 拓扑:慢链路 peer 的往返估计只放宽它自己的重发,另一个 peer 节奏不变', async () => {
await withFakeTimers(async (h, advance) => {
const linked = establishInboundReliableLink(h, 'stream-c', 1, 'dev-c');
diff --git a/packages/device-link/src/client.ts b/packages/device-link/src/client.ts
index c0cd7ab580..8c29c91f6e 100644
--- a/packages/device-link/src/client.ts
+++ b/packages/device-link/src/client.ts
@@ -1828,8 +1828,7 @@ export class DeviceLinkClient {
peer.pendingBytes += skipBytes - pending.bytes;
pending.envelope = skipEnvelope;
pending.bytes = skipBytes;
- pending.attempts = 0;
- pending.lastSentAt = 0;
+ resetReliableSendRound(pending);
pending.sent = false;
// **定向**发这一帧,不借道 retryPending:后者从队头遍历,skip 前面若压着超过预算的
// pending,预算会在到达它之前用完 —— 注释说的「立刻发」就没发生,而接收端正等这个
@@ -4571,10 +4570,7 @@ export class DeviceLinkClient {
if (peer.pending.size === 0) return;
if (resumedLink || peer.lastReplayEpoch !== this.connEpoch) {
peer.lastReplayEpoch = this.connEpoch;
- for (const pending of peer.pending.values()) {
- pending.attempts = 0;
- pending.lastSentAt = 0;
- }
+ for (const pending of peer.pending.values()) resetReliableSendRound(pending);
}
if (peer.recoveryTrace) peer.recoveryTrace.replayThroughSeq = peer.nextSeq - 1;
this.retryPending(dst, { ignoreInterval: true });
@@ -5126,6 +5122,17 @@ function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
+/**
+ * Start a new send round. firstSentAt belongs to the round: a message the peer
+ * acknowledges before it is written again in this round (ACK lost across a
+ * reconnect) must not turn the offline gap into an RTT sample.
+ */
+function resetReliableSendRound(pending: PendingReliableMessage): void {
+ pending.attempts = 0;
+ pending.lastSentAt = 0;
+ pending.firstSentAt = undefined;
+}
+
function isReliableKind(kind: Envelope['kind']): kind is 'invoke' | 'invoke-result' | 'push' {
return kind === 'invoke' || kind === 'invoke-result' || kind === 'push';
}