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): restore pending probe reply links after reconnect

DavidShend1e2aa5eae3a
diff --git a/apps/mobile/src/__tests__/historyViewProvider.test.tsx b/apps/mobile/src/__tests__/historyViewProvider.test.tsx
index 77a55ca489..3c2c7cd41b 100644
--- a/apps/mobile/src/__tests__/historyViewProvider.test.tsx
+++ b/apps/mobile/src/__tests__/historyViewProvider.test.tsx
@@ -12,6 +12,8 @@ import {
} from '@cindy/device-link';
import { isHistoryViewUnavailable } from '@cindy/maker-shared/message-window';
import { DeviceLinkProvider, useDeviceLink, type DeviceLinkContextValue } from '../device-link/DeviceLinkContext';
+import { acquireDeviceSendSlot, resetDeviceResponsivenessTracking, settleDeviceSend, unresponsiveDevicesStore } from '../device-link/unresponsiveDevicesStore';
+import { revokedDevicesStore } from '../device-link/revokedDevicesStore';
const auth = vi.hoisted(() => ({
isAuthenticated: true,
@@ -48,6 +50,11 @@ vi.mock('@react-native-async-storage/async-storage', () => ({ default: {
vi.mock('@/device-link/rnWebSocket', () => ({ createRnWebSocket: vi.fn() }));
vi.mock('@/debug/mobileDebugLog', () => ({ mobileDebugLog: vi.fn() }));
vi.mock('@/debug/visualMock', () => ({ createVisualMockDeviceLinkContext: vi.fn(), seedVisualMockStore: vi.fn() }));
+vi.mock('@cindy/maker-shared/device-responsiveness', async (importOriginal) => {
+ const actual = await importOriginal<typeof import('@cindy/maker-shared/device-responsiveness')>();
+ return { ...actual, createDeviceResponsivenessBreaker: (options: Parameters<typeof actual.createDeviceResponsivenessBreaker>[0]) =>
+ actual.createDeviceResponsivenessBreaker({ ...options, now: () => Date.now() }) };
+});
// Only the transport boundary is fake. Mount the real Provider, including its
// handshake single-flight cache, invalidation callbacks and invoke send path.
@@ -67,9 +74,9 @@ const transport = vi.hoisted(() => {
getStatus = () => this.status;
serverCapabilities: string[] = [];
hasServerCapability = (capability: string) => this.serverCapabilities.includes(capability);
- isOutboundExplicitlyClosed = () => false;
+ isOutboundExplicitlyClosed = vi.fn((_deviceId: string) => false);
// No background recovery owner; each test explicitly starts the fresh read.
- hasPendingRequestsTo = () => false;
+ hasPendingRequestsTo = vi.fn((_deviceId: string) => false);
onStatusChange(listener: Client['statusChanged']) { this.statusChanged = listener; return () => {}; }
onPresenceChanged(listener: Client['presenceChanged']) { this.presenceChanged = listener; return () => {}; }
onPeerTransportReset(listener: Client['peerReset']) { this.peerReset = listener; return () => {}; }
@@ -115,6 +122,90 @@ beforeEach(async () => {
});
afterEach(async () => { await act(async () => root.unmount()); });
+describe('pending probe reply recovery', () => {
+ afterEach(() => {
+ resetDeviceResponsivenessTracking();
+ revokedDevicesStore.clearAll();
+ });
+ it('reopens the reply link after reconnect while the old business probe still owns its slot', async () => {
+ vi.useFakeTimers();
+ const client = transport.clients[0];
+ client.openLink.mockResolvedValue(accepted(supported));
+ let resolveProbe!: (value: { ok: true; result: string }) => void;
+ const probe = new Promise<{ ok: true; result: string }>(resolve => { resolveProbe = resolve; });
+ client.invoke.mockImplementation(() => probe);
+ try {
+ await act(async () => { await context.openLink('host'); });
+ await act(async () => {
+ for (let i = 0; i < 3; i++) settleDeviceSend('host', acquireDeviceSendSlot('host'), 'timeout');
+ await vi.advanceTimersByTimeAsync(14_000);
+ });
+ 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'); });
+ // 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.
+ await act(async () => networkEvents.app('active'));
+ expect(client.openLink).toHaveBeenCalledTimes(2);
+ expect(client.invoke).toHaveBeenCalledTimes(1);
+ await act(async () => {
+ client.hasPendingRequestsTo.mockReturnValue(false);
+ resolveProbe({ ok: true, result: 'probe response' });
+ });
+ expect(unresponsiveDevicesStore.has('host')).toBe(false);
+ } finally {
+ await act(async () => { resolveProbe({ ok: true, result: 'cleanup' }); });
+ vi.useRealTimers();
+ }
+ });
+
+ it.each(['no pending request', 'revoked', 'explicitly closed', 'background', 'settled before send', 'offline before send'] as const)(
+ 'does not bypass lifecycle guards: %s', async reason => {
+ vi.useFakeTimers();
+ const client = transport.clients[0];
+ client.openLink.mockResolvedValue(accepted(supported));
+ let resolveProbe!: (value: { ok: true; result: string }) => void;
+ client.invoke.mockImplementation(() => new Promise(resolve => { resolveProbe = resolve; }));
+ try {
+ await act(async () => { await context.openLink('host'); });
+ await act(async () => {
+ for (let i = 0; i < 3; i++) settleDeviceSend('host', acquireDeviceSendSlot('host'), 'timeout');
+ await vi.advanceTimersByTimeAsync(14_000);
+ });
+ expect(client.invoke).toHaveBeenCalledTimes(1);
+ client.hasPendingRequestsTo.mockReturnValue(reason !== 'no pending request');
+ await act(async () => {
+ client.status = 'connecting'; client.statusChanged('connecting');
+ if (reason === 'revoked') revokedDevicesStore.markRevoked('host');
+ if (reason === 'explicitly closed') client.isOutboundExplicitlyClosed.mockReturnValue(true);
+ if (reason === 'background') { networkEvents.state = 'background'; networkEvents.app('background'); }
+ client.status = 'online'; client.statusChanged('online');
+ // Mutate after the initial guard but before the async send continuation.
+ if (reason === 'settled before send') client.hasPendingRequestsTo.mockReturnValue(false);
+ if (reason === 'offline before send') { client.status = 'connecting'; client.statusChanged('connecting'); }
+ });
+ expect(client.openLink).toHaveBeenCalledTimes(1);
+ expect(client.invoke).toHaveBeenCalledTimes(1);
+ if (reason === 'background') {
+ await act(async () => { networkEvents.state = 'active'; networkEvents.app('active'); });
+ expect(client.openLink).toHaveBeenCalledTimes(2);
+ expect(client.invoke).toHaveBeenCalledTimes(1);
+ expect(unresponsiveDevicesStore.has('host')).toBe(true);
+ }
+ } finally {
+ client.hasPendingRequestsTo.mockReturnValue(false);
+ await act(async () => resolveProbe?.({ ok: true, result: 'cleanup' }));
+ vi.useRealTimers();
+ }
+ },
+ );
+});
+
describe('Provider shared-task relay compatibility', () => {
it('distinguishes an old online relay from disconnection and re-negotiates after upgrade', async () => {
const client = transport.clients[0];
diff --git a/apps/mobile/src/device-link/DeviceLinkContext.tsx b/apps/mobile/src/device-link/DeviceLinkContext.tsx
index c1b5314fe8..3935b6ab13 100644
--- a/apps/mobile/src/device-link/DeviceLinkContext.tsx
+++ b/apps/mobile/src/device-link/DeviceLinkContext.tsx
@@ -486,8 +486,25 @@ export function DeviceLinkProvider({ children }: { children: ReactNode }) {
deviceId: string,
allowProbe = false,
refreshSettled = false,
+ pendingReplyRecovery = false,
) => {
+ const connectionEpoch = connectionEpochRef.current;
+ const releaseGeneration = backgroundReleaseGenerationRef.current;
const checkAvailability = () => {
+ if (pendingReplyRecovery && (
+ clientRef.current !== client
+ || connectionEpochRef.current !== connectionEpoch
+ || backgroundReleaseGenerationRef.current !== releaseGeneration
+ || backgroundReleaseInFlightRef.current
+ || AppState.currentState !== 'active'
+ || client.getStatus() !== 'online'
+ || revokedDevicesStore.has(deviceId)
+ || client.isOutboundExplicitlyClosed(deviceId)
+ || rehydrateSuppressedDeviceIds.has(deviceId)
+ || !client.hasPendingRequestsTo(deviceId)
+ )) {
+ throw new DeviceLinkError('NOT_CONNECTED', 'pending reply recovery is no longer current');
+ }
if (presenceAvailableByDeviceRef.current.get(deviceId) === false) {
const disabled = presenceUnavailableVerdictsRef.current.get(deviceId)?.kind === 'disabled';
throw new DeviceLinkError(disabled ? 'REMOTE_DISABLED' : 'DEVICE_OFFLINE', 'remote device is unavailable');
@@ -498,11 +515,23 @@ export function DeviceLinkProvider({ children }: { children: ReactNode }) {
presenceAvailabilityEpochsRef.current,
remoteResponseEvidenceEpochs,
deviceId,
- () => sendOpenLinkWithAccessHandling(client, deviceId, allowProbe, checkAvailability),
+ () => sendOpenLinkWithAccessHandling(client, deviceId, allowProbe, checkAvailability, pendingReplyRecovery),
{ retainSuccessful: true, refreshSettled },
);
}, []);
+ const restorePendingReplyLinks = useCallback((client: DeviceLinkClient) => {
+ // 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.
+ // Reuse the connection's single-flight cache; send no new business request
+ // 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);
+ }
+ }, [sendOpenLinkOnce]);
+
const sendTrackedSubscribe = useCallback(async (
client: DeviceLinkClient,
deviceId: string,
@@ -982,6 +1011,7 @@ export function DeviceLinkProvider({ children }: { children: ReactNode }) {
connectionEpochRef.current = ++nextDeviceLinkConnectionEpoch;
setConnectionEpoch(connectionEpochRef.current);
resetRemoteProjectOrderPushFence();
+ restorePendingReplyLinks(client);
void rehydrateWithClient(client);
// A new controller receives only presence deltas. Read the roster once so
// an already-offline host is known even when opening directly into history.
@@ -1272,6 +1302,7 @@ 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);
// 快速切换(连接被宽限保住、始终 online)不会有 online 状态转换,这条显式
// 补齐就是断档回填的唯一触发点;其余路径下它因 status 未 online 而空转。
void rehydrateWithClient(client);
@@ -1369,6 +1400,7 @@ export function DeviceLinkProvider({ children }: { children: ReactNode }) {
clearPerAccountDeviceLinkState,
publishPresenceAvailabilityMutation,
rehydrateWithClient,
+ restorePendingReplyLinks,
requestForcedPeerRecovery,
readDeviceList,
]);
@@ -1809,8 +1841,9 @@ function sendOpenLinkWithAccessHandling(
deviceId: string,
allowProbe = false,
preSend?: () => void,
+ pendingReplyRecovery = false,
): Promise<LinkAcceptPayload> {
- return withAccessRevokedHandling(deviceId, () => sendOpenLink(client, deviceId, allowProbe, preSend));
+ return withAccessRevokedHandling(deviceId, () => sendOpenLink(client, deviceId, allowProbe, preSend, pendingReplyRecovery));
}
async function sendOpenLink(
@@ -1818,14 +1851,17 @@ async function sendOpenLink(
deviceId: string,
allowProbe = false,
preSend?: () => void,
+ pendingReplyRecovery = false,
): Promise<LinkAcceptPayload> {
preSend?.();
// 熔断门禁放在连接等待之前:open 时快速失败,不消耗 1.5s 重连等待也不上管道。
- const slot = acquireDeviceSendSlot(deviceId, undefined, { allowProbe });
+ // Reply transport must not compete with the business request it is restoring.
+ // The provider's preSend guard limits this exemption to current pending work.
+ const slot = pendingReplyRecovery ? null : acquireDeviceSendSlot(deviceId, undefined, { allowProbe });
try {
await ensureOnlineForRequest(client);
} catch (err) {
- settleDeviceSend(deviceId, slot, 'inconclusive');
+ if (slot) settleDeviceSend(deviceId, slot, 'inconclusive');
throw err;
}
try {
@@ -1842,7 +1878,7 @@ async function sendOpenLink(
// 超时后再 open,形成周期性风暴。这里按不定论处理:不关熔断也不计失败;
// openLink 若是探测,单飞席位随之释放、退避窗口不动,紧随其后的 subscribe
// (真实 invoke 通道)会立即接棒成为新探测,由它的回包决定开合。
- settleDeviceSend(deviceId, slot, 'inconclusive');
+ if (slot) settleDeviceSend(deviceId, slot, 'inconclusive');
markRemoteResponseEvidence(deviceId);
// 显式 openLink 成功 = 链路已重建:解除永久关闭后的重建抑制。
liftRehydrateSuppressionOnExplicitOpen(rehydrateSuppressedDeviceIds, deviceId);
@@ -1851,7 +1887,7 @@ async function sendOpenLink(
// 超时仍计失败:link-open 都等不到回包说明被控端连链路层都没在应答。
// 终态 relay 应答(REMOTE_DISABLED / DEVICE_OFFLINE / VERSION_MISMATCH)
// 关熔断,把 UI 让给对应的可操作错误态(review P1:否则设备被永远探测)。
- settleDeviceSend(deviceId, slot, classifyLinkOpenFailure(err));
+ if (slot) settleDeviceSend(deviceId, slot, classifyLinkOpenFailure(err));
throw err;
}
}
diff --git a/docs/dev-rules/remote-and-mobile-adaptation.md b/docs/dev-rules/remote-and-mobile-adaptation.md
index 249fd516f3..f530f70c63 100644
--- a/docs/dev-rules/remote-and-mobile-adaptation.md
+++ b/docs/dev-rules/remote-and-mobile-adaptation.md
@@ -75,6 +75,12 @@ Desktop 和 Mobile 的按设备排队、并发上限、退避、取消代次由
在线判断、Mobile 的后台生命周期与页面恢复仍由各端适配器负责。同一设备取消后重新
请求恢复,须等待旧请求结算;旧结果不能取消新请求,也不能恢复已取消的重试。
+Mobile 重连时,若熔断设备仍有在途业务请求,先在调度器外恢复该设备的回包链路,
+避免旧探测等待回包、新建链又等待旧探测的循环依赖。此例外只发送单飞的 `link-open`,
+不占用或结算业务探测席位,不增加业务请求,也不缩短原请求预算;建链成功不能解除熔断。
+连接代次、前后台、撤权、显式关闭、设备可用性和在途请求在发送前再次核验。
+后台新连接暂不恢复,回前台再复用同一建链入口;其它设备及被控端共享连接不受影响。
+
Desktop 清理某设备的熔断状态不等于该设备已恢复。停用、离线和重置时只清理 UI
状态,不触发订阅重放或内容刷新;响应性事件只有携带真实业务回包证据才立即触发恢复。
清理事件本身不启动恢复,但不能永久关闭后续读取:聚焦、手动重试或在线通知再次触发时,