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(mobile): refresh pending reply handshake on foreground

DavidShen3c99092ba29f
diff --git a/apps/mobile/src/__tests__/historyViewProvider.test.tsx b/apps/mobile/src/__tests__/historyViewProvider.test.tsx
index 3c2c7cd41b..c823d5cfc3 100644
--- a/apps/mobile/src/__tests__/historyViewProvider.test.tsx
+++ b/apps/mobile/src/__tests__/historyViewProvider.test.tsx
@@ -127,7 +127,7 @@ describe('pending probe reply recovery', () => {
resetDeviceResponsivenessTracking();
revokedDevicesStore.clearAll();
});
- it('reopens the reply link after reconnect while the old business probe still owns its slot', async () => {
+ it.each(['relay reconnect', 'short background'] as const)('reopens the reply link after %s while the old business probe still owns its slot', async recovery => {
vi.useFakeTimers();
const client = transport.clients[0];
client.openLink.mockResolvedValue(accepted(supported));
@@ -143,13 +143,26 @@ describe('pending probe reply recovery', () => {
expect(client.invoke).toHaveBeenCalledExactlyOnceWith('host', expect.objectContaining({ channel: 'local-db:sessions:list' }), expect.any(Number));
expect(unresponsiveDevicesStore.has('host')).toBe(true);
client.hasPendingRequestsTo.mockImplementation(device => device === 'host');
- await act(async () => { client.status = 'connecting'; client.statusChanged('connecting'); });
- await act(async () => { client.status = 'online'; client.statusChanged('online'); });
+ const freshAccept = deferredAccept();
+ client.openLink.mockReturnValue(freshAccept.promise);
+ if (recovery === 'relay reconnect') {
+ await act(async () => { client.status = 'connecting'; client.statusChanged('connecting'); });
+ await act(async () => { client.status = 'online'; client.statusChanged('online'); });
+ } else {
+ // The relay stays online, so the successful pre-background handshake is still cached.
+ await act(async () => { networkEvents.state = 'background'; networkEvents.app('background'); });
+ await act(async () => { networkEvents.state = 'active'; networkEvents.app('active'); });
+ expect(client.stop).not.toHaveBeenCalled();
+ expect(client.restartConnection).not.toHaveBeenCalled();
+ }
// The serial recovery scheduler is still awaiting probe; only its reply link may reopen.
expect(client.openLink).toHaveBeenCalledTimes(2);
expect(client.invoke).toHaveBeenCalledTimes(1);
expect(unresponsiveDevicesStore.has('host')).toBe(true);
- // Another foreground hint shares the accepted transport; it cannot start a second probe.
+ // Repeated foreground hints share both pending and accepted handshakes.
+ await act(async () => networkEvents.app('active'));
+ expect(client.openLink).toHaveBeenCalledTimes(2);
+ await act(async () => freshAccept.resolve(accepted(supported)));
await act(async () => networkEvents.app('active'));
expect(client.openLink).toHaveBeenCalledTimes(2);
expect(client.invoke).toHaveBeenCalledTimes(1);
diff --git a/apps/mobile/src/device-link/DeviceLinkContext.tsx b/apps/mobile/src/device-link/DeviceLinkContext.tsx
index 3bbf4c29f7..994ffd5d44 100644
--- a/apps/mobile/src/device-link/DeviceLinkContext.tsx
+++ b/apps/mobile/src/device-link/DeviceLinkContext.tsx
@@ -521,7 +521,7 @@ export function DeviceLinkProvider({ children }: { children: ReactNode }) {
);
}, []);
- const restorePendingReplyLinks = useCallback((client: DeviceLinkClient) => {
+ const restorePendingReplyLinks = useCallback((client: DeviceLinkClient, refreshSettled = false) => {
// A running business probe owns the breaker/scheduler slot until its reply
// arrives. Restore only its transport: queuing this handshake behind that
// probe would make each wait for the other until timeout + backoff.
@@ -529,7 +529,7 @@ export function DeviceLinkProvider({ children }: { children: ReactNode }) {
// and neither consume nor settle the existing probe's slot.
for (const deviceId of unresponsiveDevicesStore.getSnapshot()) {
if (!client.hasPendingRequestsTo(deviceId)) continue;
- void sendOpenLinkOnce(client, deviceId, false, false, true).request.catch(() => undefined);
+ void sendOpenLinkOnce(client, deviceId, false, refreshSettled, true).request.catch(() => undefined);
}
}, [sendOpenLinkOnce]);
@@ -1295,6 +1295,7 @@ export function DeviceLinkProvider({ children }: { children: ReactNode }) {
const sub = AppState.addEventListener('change', (next) => {
if (next === 'active') {
diagnostics.foreground();
+ const resumingFromBackground = backgroundReleaseInFlightRef.current;
backgroundReleaseInFlightRef.current = false;
// 回前台立刻重连:绕开断线后遗留的指数退避计时器(可能 park 到 30s),
// 让"打开 App → 打开会话"路径快速恢复在线,而不是干等退避。
@@ -1304,7 +1305,9 @@ export function DeviceLinkProvider({ children }: { children: ReactNode }) {
// A short background stay can preserve a socket whose route changed.
// Replace a known old path; otherwise probe without the hint cooldown.
if (client.getStatus() === 'online') recoverNetwork(true);
- restorePendingReplyLinks(client);
+ // A short background stay retains successful handshakes on the same relay.
+ // Refresh those once per return, while sharing pending opens and repeated active hints.
+ restorePendingReplyLinks(client, resumingFromBackground);
// 快速切换(连接被宽限保住、始终 online)不会有 online 状态转换,这条显式
// 补齐就是断档回填的唯一触发点;其余路径下它因 status 未 online 而空转。
void rehydrateWithClient(client);