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): 回前台保留连接按握手看门狗当前截止时刻判断

Dash32bbf154f1eb
diff --git a/packages/device-link/src/__tests__/client.test.ts b/packages/device-link/src/__tests__/client.test.ts
index f0894ca6e6..c8271fb9d9 100644
--- a/packages/device-link/src/__tests__/client.test.ts
+++ b/packages/device-link/src/__tests__/client.test.ts
@@ -6191,6 +6191,31 @@ describe('DeviceLinkClient', () => {
h.client.stop();
});
+ it('connectNow keepInFlightAttempt:慢升级后 hello/ack 新窗口内回前台仍保留连接', async () => {
+ vi.useFakeTimers();
+ // A 1ms token budget keeps the token phase from masking the handshake deadline.
+ const h = makeHarness({ timing: { handshakeTimeoutMs: 15, getTokenTimeoutMs: 1, pingIntervalMs: 60_000 } });
+ try {
+ h.client.start();
+ await vi.advanceTimersByTimeAsync(0);
+ const socket = h.current();
+ await vi.advanceTimersByTimeAsync(12);
+ socket.emit('open'); // the watchdog re-arms a full hello/ack window here
+ await vi.advanceTimersByTimeAsync(10); // 22ms since connect(), 10ms into the new window
+ h.client.connectNow('appstate-active', { keepInFlightAttempt: true });
+ await vi.advanceTimersByTimeAsync(0);
+ expect(h.sockets).toHaveLength(1);
+ expect(socket.terminated).toBe(false);
+ socket.push({ v: PROTOCOL_VERSION, kind: 'hello-ack', payload: {
+ serverProtocolVersion: PROTOCOL_VERSION, deviceId: 'dev-self', userId: 'u1',
+ } });
+ expect(h.client.getStatus()).toBe('online');
+ } finally {
+ h.client.stop();
+ vi.useRealTimers();
+ }
+ });
+
it('connectNow keepInFlightAttempt:挂起后超过握手窗口的旧尝试与退避等待照常立即重连', async () => {
const h = makeHarness({ timing: { handshakeTimeoutMs: 12_000, reconnectBaseMs: 10_000, reconnectMaxMs: 30_000 } });
const realNow = Date.now;
diff --git a/packages/device-link/src/client.ts b/packages/device-link/src/client.ts
index f0f60d038e..c0cd7ab580 100644
--- a/packages/device-link/src/client.ts
+++ b/packages/device-link/src/client.ts
@@ -751,8 +751,11 @@ export class DeviceLinkClient {
private networkChangeHintedAt = 0;
private networkProbeNextAllowedAt = 0;
private connectionStartedAt = 0;
- /** Wall clock of the current connect attempt; unlike timers it keeps counting through app suspension. */
- private connectionStartedWallAt = 0;
+ /**
+ * Wall-clock deadline of the current attempt's active watchdog (token wait, then
+ * each handshake window). Unlike timers it keeps counting through app suspension.
+ */
+ private connectAttemptDeadlineWallAt = 0;
/** 当前连接的代号,用于丢弃过期 socket 的事件回调 */
private connEpoch = 0;
/** 本轮连接的最后一条 socket error message(升级失败 401 只在 error 事件里可见) */
@@ -877,7 +880,7 @@ export class DeviceLinkClient {
opts?.keepInFlightAttempt
&& this.status === 'connecting'
&& !this.reconnectTimer
- && Date.now() - this.connectionStartedWallAt < this.effectiveHandshakeTimeoutMs()
+ && Date.now() < this.connectAttemptDeadlineWallAt
) {
this.log.debug(`connectNow(${reason}) kept in-flight connect attempt`);
return;
@@ -1862,7 +1865,7 @@ export class DeviceLinkClient {
this.clearNetworkProbe();
this.resetLegacyInboundQueue();
this.connectionStartedAt = this.monotonicNow();
- this.connectionStartedWallAt = Date.now();
+ this.connectAttemptDeadlineWallAt = Date.now() + this.timing.getTokenTimeoutMs + this.effectiveHandshakeTimeoutMs();
this.setStatus('connecting');
const epoch = ++this.connEpoch;
this.lastSocketErrorMessage = null;
@@ -2064,6 +2067,8 @@ export class DeviceLinkClient {
private armHandshakeTimeout(epoch: number): void {
if (this.handshakeTimer) clearTimeout(this.handshakeTimer);
const timeoutMs = this.effectiveHandshakeTimeoutMs();
+ // keepInFlightAttempt judges by the same deadline the watchdog enforces.
+ this.connectAttemptDeadlineWallAt = Date.now() + timeoutMs;
this.handshakeTimer = setTimeout(() => {
this.handshakeTimer = null;
if (this.stopped || epoch !== this.connEpoch || this.status === 'online') return;