Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(device-link): require explicit responsiveness probes
DavidShene6f6641dcaba
diff --git a/apps/desktop/src/main/device-link/__tests__/responsivenessTracker.test.ts b/apps/desktop/src/main/device-link/__tests__/responsivenessTracker.test.tsindex abe71e78b7..d8707248ed 100644--- a/apps/desktop/src/main/device-link/__tests__/responsivenessTracker.test.ts+++ b/apps/desktop/src/main/device-link/__tests__/responsivenessTracker.test.ts@@ -149,6 +149,9 @@ describe('responsivenessTracker', () => {expect(h.probeInvoke).not.toHaveBeenCalled();h.advance(BREAKER_PROBE_BACKOFF_BASE_MS);+ const run = vi.fn(async () => 'business-result');+ await expect(h.tracker.guardInvoke(DEV, 'maker:send', run)).rejects.toThrow('unresponsive');+ expect(run).not.toHaveBeenCalled();h.tracker.probeTick(); // 窗口已到但不合格(relay 掉线 / presence 不可用)expect(h.probeInvoke).not.toHaveBeenCalled();diff --git a/apps/desktop/src/main/device-link/responsivenessTracker.ts b/apps/desktop/src/main/device-link/responsivenessTracker.tsindex 31080c3681..1d937bfc40 100644--- a/apps/desktop/src/main/device-link/responsivenessTracker.ts+++ b/apps/desktop/src/main/device-link/responsivenessTracker.ts@@ -138,7 +138,6 @@ export function createResponsivenessTracker(const log = deps.log ?? { info: () => {}, warn: () => {}, debug: () => {} };const unresponsive = new Set<string>();const linkRecoveryInFlight = new Map<string, Promise<unknown>>();- /** 同一设备并发业务请求共享一个 timeout cohort,避免一次链路故障重复计 strike。 */const breaker = createDeviceResponsivenessBreaker({now: deps.now,onOpenChanged: (deviceId, open) => {diff --git a/apps/mobile/src/device-link/DeviceLinkContext.tsx b/apps/mobile/src/device-link/DeviceLinkContext.tsxindex 315ad72848..2a81e23f12 100644--- a/apps/mobile/src/device-link/DeviceLinkContext.tsx+++ b/apps/mobile/src/device-link/DeviceLinkContext.tsx@@ -283,13 +283,17 @@ export function DeviceLinkProvider({ children }: { children: ReactNode }) {const [connectionEpoch, setConnectionEpoch] = useState(0);const [lastPresenceSnapshot, setLastPresenceSnapshot] = useState<PresenceSnapshot | null>(null);- const sendOpenLinkOnce = useCallback((client: DeviceLinkClient, deviceId: string) => {+ const sendOpenLinkOnce = useCallback((+ client: DeviceLinkClient,+ deviceId: string,+ allowProbe = false,+ ) => {return getOrCreatePresenceTrackedRequest(openLinkInFlightRef.current,presenceAvailabilityEpochsRef.current,remoteResponseEvidenceEpochs,deviceId,- () => sendOpenLinkWithAccessHandling(client, deviceId),+ () => sendOpenLinkWithAccessHandling(client, deviceId, allowProbe),{ retainSuccessful: true },);}, []);@@ -327,12 +331,13 @@ export function DeviceLinkProvider({ children }: { children: ReactNode }) {const probeUnresponsiveDevice = useCallback(async (client: DeviceLinkClient, deviceId: string): Promise<void> => {try {- await sendOpenLinkOnce(client, deviceId).request;+ await sendOpenLinkOnce(client, deviceId, true).request;await sendInvokeWithAccessHandling(client,deviceId,DEVICE_RESPONSIVENESS_PROBE_CHANNEL,buildDeviceResponsivenessProbeArgs(),+ { allowProbe: true },);} catch {// swallow — settle 已在 sendOpenLink / sendInvoke 内完成。@@ -1204,16 +1209,18 @@ function ensureOnlineForRequest(client: DeviceLinkClient): Promise<void> {function sendOpenLinkWithAccessHandling(client: DeviceLinkClient,deviceId: string,+ allowProbe = false,): Promise<LinkAcceptPayload> {- return withAccessRevokedHandling(deviceId, () => sendOpenLink(client, deviceId));+ return withAccessRevokedHandling(deviceId, () => sendOpenLink(client, deviceId, allowProbe));}async function sendOpenLink(client: DeviceLinkClient,deviceId: string,+ allowProbe = false,): Promise<LinkAcceptPayload> {// 熔断门禁放在连接等待之前:open 时快速失败,不消耗 1.5s 重连等待也不上管道。- const slot = acquireDeviceSendSlot(deviceId);+ const slot = acquireDeviceSendSlot(deviceId, undefined, { allowProbe });try {await ensureOnlineForRequest(client);} catch (err) {@@ -1251,6 +1258,8 @@ interface SendInvokeOptions {preSend?: () => void;/** 同一轮明确 fan-out 共享;普通独立请求省略。 */responsivenessCohort?: number;+ /** 仅主动的代表性 half-open 探测允许领取 probe 席位。 */+ allowProbe?: boolean;}function sendInvokeWithAccessHandling<T>(@@ -1272,7 +1281,9 @@ async function sendInvoke<T>(): Promise<T> {// 熔断门禁放在连接等待之前:open 时快速失败,不消耗 1.5s 重连等待也不上管道。// 同一轮显式 fan-out 复用 cohort;普通调用省略时每次 acquire 都是独立观测。- const slot = acquireDeviceSendSlot(deviceId, opts?.responsivenessCohort);+ const slot = acquireDeviceSendSlot(deviceId, opts?.responsivenessCohort, {+ allowProbe: opts?.allowProbe,+ });try {await ensureOnlineForRequest(client);// 连接就绪后、真正发送前的最后检查点:重连等待期间调用方状态可能已失效diff --git a/apps/mobile/src/device-link/unresponsiveDevicesStore.ts b/apps/mobile/src/device-link/unresponsiveDevicesStore.tsindex 0e4b51d3b3..9c1082d234 100644--- a/apps/mobile/src/device-link/unresponsiveDevicesStore.ts+++ b/apps/mobile/src/device-link/unresponsiveDevicesStore.ts@@ -119,8 +119,12 @@ export function createDeviceSendCohort(deviceId: string): number {return breaker.createCohort(deviceId);}-export function acquireDeviceSendSlot(deviceId: string, cohort?: number): BreakerSendSlot {- const slot = breaker.acquire(deviceId, cohort);+export function acquireDeviceSendSlot(+ deviceId: string,+ cohort?: number,+ options?: { allowProbe?: boolean },+): BreakerSendSlot {+ const slot = breaker.acquire(deviceId, cohort, options);if (slot.decision === 'reject') throw createDeviceUnresponsiveError(deviceId);return slot;}diff --git a/packages/maker-shared/src/__tests__/deviceResponsiveness.test.ts b/packages/maker-shared/src/__tests__/deviceResponsiveness.test.tsindex 1af59a5e77..700c8ed98d 100644--- a/packages/maker-shared/src/__tests__/deviceResponsiveness.test.ts+++ b/packages/maker-shared/src/__tests__/deviceResponsiveness.test.ts@@ -45,7 +45,7 @@ function openBreaker(h: ReturnType<typeof harness>): void {/** 到点探测:acquire 必须拿到 probe 席位,返回票据供 settle。 */function acquireProbe(h: ReturnType<typeof harness>, deviceId = DEV): BreakerSendSlot {- const slot = h.breaker.acquire(deviceId);+ const slot = h.breaker.acquire(deviceId, undefined, { allowProbe: true });expect(slot.decision).toBe('probe');return slot;}@@ -372,9 +372,7 @@ describe('probeDue(只读探测窗口判定,给 rehydrate 主动探测用)', ()const h = harness();openBreaker(h);h.advance(BREAKER_PROBE_BACKOFF_BASE_MS);- expect(h.breaker.acquire(DEV, h.breaker.createCohort(DEV), { allowProbe: false }).decision).toBe(- 'reject',- );- expect(h.breaker.acquire(DEV).decision).toBe('probe');+ expect(h.breaker.acquire(DEV, h.breaker.createCohort(DEV)).decision).toBe('reject');+ expect(h.breaker.acquire(DEV, undefined, { allowProbe: true }).decision).toBe('probe');});});diff --git a/packages/maker-shared/src/deviceResponsiveness.ts b/packages/maker-shared/src/deviceResponsiveness.tsindex e2991a25a6..26a4683e81 100644--- a/packages/maker-shared/src/deviceResponsiveness.ts+++ b/packages/maker-shared/src/deviceResponsiveness.ts@@ -71,7 +71,8 @@ export interface DeviceResponsivenessBreaker {createCohort(deviceId: string): number;/*** 发送前门禁:closed → 'allow';同一 fan-out 可传共享 cohort,省略则每次调用- * 创建独立 cohort。open 且探测窗口已到、无在途探测 → 'probe';其余 → 'reject'。+ * 创建独立 cohort。open 且探测窗口已到、无在途探测,且调用方显式 allowProbe:true+ * → 'probe';其余 → 'reject'。*/acquire(deviceId: string, cohort?: number, options?: { allowProbe?: boolean }): BreakerSendSlot;/** 请求收尾上报。slot 必须是 acquire 返回的票据;代数不匹配的旧请求被忽略。 */@@ -145,7 +146,9 @@ export function createDeviceResponsivenessBreaker(if (now() - state.openedAt < state.probeBackoffMs) {return { decision: 'reject', generation, cohort: 0 };}- if (options?.allowProbe === false) {+ // half-open 的唯一席位只能由代表性探测路径显式领取;普通业务请求即使+ // 窗口到点也必须保持 reject,避免抢占探测并错误推进退避。+ if (options?.allowProbe !== true) {return { decision: 'reject', generation, cohort: 0 };}state.probeInFlight = true;