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): 回前台闪烁时保留仍在握手窗口内的连接

Dash8a16f33039ea
diff --git a/apps/mobile/src/__tests__/historyViewProvider.test.tsx b/apps/mobile/src/__tests__/historyViewProvider.test.tsx
index a1a3235370..3228929eed 100644
--- a/apps/mobile/src/__tests__/historyViewProvider.test.tsx
+++ b/apps/mobile/src/__tests__/historyViewProvider.test.tsx
@@ -424,7 +424,10 @@ describe('Provider network recovery priority', () => {
await act(async () => {
networkEvents.state = 'active'; networkEvents.app('active');
});
- expect(client.connectNow).toHaveBeenCalledWith('appstate-active', { overrideCongestionCooldown: true });
+ expect(client.connectNow).toHaveBeenCalledWith('appstate-active', {
+ overrideCongestionCooldown: true,
+ keepInFlightAttempt: true,
+ });
if (status === 'online') expect(client.notifyNetworkChanged).toHaveBeenCalledWith({ urgent: true });
else expect(client.notifyNetworkChanged).not.toHaveBeenCalled();
expect(client.stop).not.toHaveBeenCalled();
diff --git a/apps/mobile/src/device-link/DeviceLinkContext.tsx b/apps/mobile/src/device-link/DeviceLinkContext.tsx
index 0a93671dc0..bf3eae9672 100644
--- a/apps/mobile/src/device-link/DeviceLinkContext.tsx
+++ b/apps/mobile/src/device-link/DeviceLinkContext.tsx
@@ -1331,7 +1331,10 @@ export function DeviceLinkProvider({ children }: { children: ReactNode }) {
releaseTopics: releaseHeavyTopics,
pendingTransitions: () => [...backgroundTransitions.current],
stop: () => client.stop(),
- connect: () => client.connectNow('appstate-active', { overrideCongestionCooldown: true }),
+ connect: () => client.connectNow('appstate-active', {
+ overrideCongestionCooldown: true,
+ keepInFlightAttempt: true,
+ }),
graceMs: BACKGROUND_STOP_GRACE_MS,
releaseWaitMs: BACKGROUND_FINAL_UNSUBSCRIBE_WAIT_MS,
suspendMs: BACKGROUND_SUSPEND_SUSPECT_MS,
diff --git a/packages/device-link/src/__tests__/client.test.ts b/packages/device-link/src/__tests__/client.test.ts
index 78027034a0..f0894ca6e6 100644
--- a/packages/device-link/src/__tests__/client.test.ts
+++ b/packages/device-link/src/__tests__/client.test.ts
@@ -6167,6 +6167,59 @@ describe('DeviceLinkClient', () => {
h.client.stop();
});
+ it('connectNow keepInFlightAttempt:回前台闪烁不丢弃仍在握手窗口内的连接', async () => {
+ const h = makeHarness({ timing: { handshakeTimeoutMs: 12_000 } });
+ h.client.start();
+ await tick();
+ expect(h.sockets.length).toBe(1);
+ expect(h.client.getStatus()).toBe('connecting');
+
+ h.client.connectNow('appstate-active', { keepInFlightAttempt: true });
+ await tick();
+ expect(h.sockets.length).toBe(1);
+ h.current().ack();
+ expect(h.client.getStatus()).toBe('online');
+
+ // Without the opt-in (e.g. a network change) the attempt is still restarted.
+ h.current().emit('close', 1006);
+ h.client.connectNow('network-change');
+ await tick();
+ const before = h.sockets.length;
+ h.client.connectNow('network-change');
+ await tick();
+ expect(h.sockets.length).toBe(before + 1);
+ h.client.stop();
+ });
+
+ it('connectNow keepInFlightAttempt:挂起后超过握手窗口的旧尝试与退避等待照常立即重连', async () => {
+ const h = makeHarness({ timing: { handshakeTimeoutMs: 12_000, reconnectBaseMs: 10_000, reconnectMaxMs: 30_000 } });
+ const realNow = Date.now;
+ try {
+ h.client.start();
+ await tick();
+ expect(h.sockets.length).toBe(1);
+ // App suspended mid-handshake: timers froze, wall time moved on.
+ const resumedAt = realNow() + 60_000;
+ Date.now = () => resumedAt;
+ h.client.connectNow('appstate-active', { keepInFlightAttempt: true });
+ await tick();
+ expect(h.sockets.length).toBe(2);
+ Date.now = realNow;
+
+ // A parked backoff timer is not an in-flight attempt.
+ h.current().ack();
+ h.current().emit('close', 1006);
+ await tick(20);
+ const parked = h.sockets.length;
+ h.client.connectNow('appstate-active', { keepInFlightAttempt: true });
+ await tick();
+ expect(h.sockets.length).toBe(parked + 1);
+ } finally {
+ Date.now = realNow;
+ h.client.stop();
+ }
+ });
+
it('connectNow:online 时为空操作,不打断健康连接', async () => {
const h = makeHarness();
h.client.start();
diff --git a/packages/device-link/src/client.ts b/packages/device-link/src/client.ts
index 781132ab5c..f0f60d038e 100644
--- a/packages/device-link/src/client.ts
+++ b/packages/device-link/src/client.ts
@@ -751,6 +751,8 @@ 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;
/** 当前连接的代号,用于丢弃过期 socket 的事件回调 */
private connEpoch = 0;
/** 本轮连接的最后一条 socket error message(升级失败 401 只在 error 事件里可见) */
@@ -858,11 +860,28 @@ export class DeviceLinkClient {
* 只有显式用户意图(移动端回前台)传 overrideCongestionCooldown 保留立即重连;
* 被 park 的调用方等冷却计时器到点自然重连(封顶 congestionBackoffMaxMs)。
*/
- connectNow(reason = 'connect-now', opts?: { overrideCongestionCooldown?: boolean }): void {
+ connectNow(
+ reason = 'connect-now',
+ opts?: { overrideCongestionCooldown?: boolean; keepInFlightAttempt?: boolean },
+ ): void {
// online 时强制重建请用 restartConnection —— 它才是「半开假活」场景的入口,
// 且已包含 resetLinkStateForReconnect(此处曾有一个等价的 { force } 分支,
// 生产代码从未使用,只有测试在调,故收敛为单一入口)。
if (this.status === 'online') return;
+ // A foreground flicker (Face ID, notification, Control Center) re-fires this
+ // within seconds. Restarting a handshake that is still inside its window only
+ // throws away the progress made on a slow link (2026-10-12 in-flight Wi-Fi:
+ // 5 of 5 such restarts discarded a 1–3s old attempt). Wall time covers app
+ // suspension, so an attempt that sat frozen in the background is restarted.
+ if (
+ opts?.keepInFlightAttempt
+ && this.status === 'connecting'
+ && !this.reconnectTimer
+ && Date.now() - this.connectionStartedWallAt < this.effectiveHandshakeTimeoutMs()
+ ) {
+ this.log.debug(`connectNow(${reason}) kept in-flight connect attempt`);
+ return;
+ }
if (this.stopped) {
// stopped → 等价 start():全新生命周期不背上一世代的拥塞冷却(review P1,
// 与 start() 的清零语义对齐)。
@@ -1843,6 +1862,7 @@ export class DeviceLinkClient {
this.clearNetworkProbe();
this.resetLegacyInboundQueue();
this.connectionStartedAt = this.monotonicNow();
+ this.connectionStartedWallAt = Date.now();
this.setStatus('connecting');
const epoch = ++this.connEpoch;
this.lastSocketErrorMessage = null;