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): stop a failing busy probe from killing the main process (#1516)

Zymonody7ec30abb7f6a1
diff --git a/apps/desktop/src/main/__tests__/busyReporter.test.ts b/apps/desktop/src/main/__tests__/busyReporter.test.ts
index 1b797d334c..39e1db5e89 100644
--- a/apps/desktop/src/main/__tests__/busyReporter.test.ts
+++ b/apps/desktop/src/main/__tests__/busyReporter.test.ts
@@ -2,7 +2,20 @@
* busyReporter 单测 —— 被控端 busy presence 的 dedupe 与重连补正(PR #166 review New-F)。
* 核心:hello 必须报当前真实 busy 并同步 dedupe 基线,否则 turn 进行中重连会让其它设备整轮看成空闲。
*/
-import { describe, it, expect, beforeEach } from 'vitest';
+import { describe, it, expect, beforeEach, vi } from 'vitest';
+
+const logSpies = vi.hoisted(() => ({ warn: vi.fn() }));
+vi.mock('../logger', () => ({
+ createLogger: () => ({
+ trace: vi.fn(),
+ debug: vi.fn(),
+ info: vi.fn(),
+ warn: logSpies.warn,
+ error: vi.fn(),
+ fatal: vi.fn(),
+ }),
+}));
+
import {
setBusyProbe,
currentBusy,
@@ -12,7 +25,10 @@ import {
__testing,
} from '../device-link/busyReporter';
-beforeEach(() => __testing.reset());
+beforeEach(() => {
+ __testing.reset();
+ logSpies.warn.mockClear();
+});
describe('busyReporter', () => {
it('无 probe → currentBusy=false', () => {
@@ -78,3 +94,101 @@ describe('busyReporter', () => {
expect(__testing.getLastReported()).toBe(false);
});
});
+
+/**
+ * probe 抛错(账号 / owner 边界切换期 maker facade 抛 PRECONDITION_FAILED)。
+ * 修复前:异常从 pollBusyChange 逃到 5s setInterval,成为主进程 uncaughtException →
+ * beginShutdown → exitCode=1,冷启动崩溃循环(issue #1358)。
+ */
+describe('busyReporter · probe 不可用(issue #1358)', () => {
+ const boundaryError = () =>
+ new Error('[PRECONDITION_FAILED] App session is switching; retry after the owner boundary settles.');
+
+ it('pollBusyChange 不外抛,返回 null(不上报)', () => {
+ setBusyProbe(() => {
+ throw boundaryError();
+ });
+ expect(() => pollBusyChange()).not.toThrow();
+ expect(pollBusyChange()).toBeNull();
+ });
+
+ it('probe 抛错时基线保持不动 —— 不退化成「不忙」', () => {
+ // turn 正在跑并已上报过 → 基线 true。
+ let probe: () => boolean = () => true;
+ setBusyProbe(() => probe());
+ expect(pollBusyChange()).toBe(true);
+ expect(__testing.getLastReported()).toBe(true);
+
+ // 边界切换:probe 开始抛错。若把未知当 false,基线会被推成 false,
+ // turn 仍在跑时其它设备整轮把本机看成空闲(同 New-F 那个坑)。
+ probe = () => {
+ throw boundaryError();
+ };
+ expect(pollBusyChange()).toBeNull();
+ expect(__testing.getLastReported()).toBe(true);
+ });
+
+ it('probe 恢复后照常工作:未错过的翻转仍会上报', () => {
+ let probe: () => boolean = () => {
+ throw boundaryError();
+ };
+ setBusyProbe(() => probe());
+ expect(pollBusyChange()).toBeNull(); // 边界期:跳过,基线仍 false
+ expect(__testing.getLastReported()).toBe(false);
+
+ // 边界稳定,期间 turn 已经起来了 → 恢复后第一拍就补报 true。
+ probe = () => true;
+ expect(pollBusyChange()).toBe(true);
+ expect(__testing.getLastReported()).toBe(true);
+ });
+
+ it('同一段连续失败只记一条 warn,probe 恢复后重新武装', () => {
+ let probe: () => boolean = () => {
+ throw boundaryError();
+ };
+ setBusyProbe(() => probe());
+ pollBusyChange();
+ pollBusyChange();
+ pollBusyChange();
+ expect(logSpies.warn).toHaveBeenCalledTimes(1); // 边界期每 5s 一条纯噪音,抑制掉
+
+ probe = () => true;
+ expect(pollBusyChange()).toBe(true); // 恢复 → 抑制解除
+ probe = () => {
+ throw boundaryError();
+ };
+ pollBusyChange();
+ expect(logSpies.warn).toHaveBeenCalledTimes(2); // 新一段失败重新记一条
+ });
+
+ it('换 probe 会重置失败日志抑制 —— 新一段的首条 warn 不被上一段吃掉', () => {
+ const throwing = () => {
+ throw boundaryError();
+ };
+ setBusyProbe(throwing);
+ pollBusyChange();
+ expect(logSpies.warn).toHaveBeenCalledTimes(1);
+
+ // 账号切换后重建 maker 会走「解绑 → 重新注入」;若不在这里重置抑制标志,
+ // 新 probe 仍不可用时的首条 warn 会被上一段的标志静默吃掉。
+ setBusyProbe(null);
+ setBusyProbe(throwing);
+ pollBusyChange();
+ expect(logSpies.warn).toHaveBeenCalledTimes(2);
+ });
+
+ it('currentBusy / helloBusy 在 probe 不可用时按 false,且能自我纠正', () => {
+ let probe: () => boolean = () => {
+ throw boundaryError();
+ };
+ setBusyProbe(() => probe());
+ // hello 帧必须带一个具体值,未知只能按 false 发;基线随之为 false。
+ expect(currentBusy()).toBe(false);
+ expect(helloBusy()).toBe(false);
+ expect(__testing.getLastReported()).toBe(false);
+
+ // 边界稳定后若实际在跑 turn,下一拍轮询就把它补正回 true(不会被 dedupe 压掉)。
+ probe = () => true;
+ expect(pollBusyChange()).toBe(true);
+ });
+});
diff --git a/apps/desktop/src/main/device-link/busyReporter.ts b/apps/desktop/src/main/device-link/busyReporter.ts
index 53f5573aa0..1b662a036c 100644
--- a/apps/desktop/src/main/device-link/busyReporter.ts
+++ b/apps/desktop/src/main/device-link/busyReporter.ts
@@ -11,19 +11,62 @@
* 登出 / 断连时 `resetBusyDedupe()` 把基线清回 false。
*
* probe 由 register.ts 在 maker 就绪后注入(返回本机当前是否有任意 session 在 turn 中)。
+ *
+ * **probe 抛错 = 未知,不等于 false**:注入的 probe 走 `anySessionInTurn(maker)`,而那个
+ * maker 是 bootstrap 的动态 facade —— 账号/owner 边界切换期间它按契约抛
+ * `PRECONDITION_FAILED: App session is switching`。此处必须把它当「这一拍问不出来」处理:
+ * - 不能让它逃出去:调用方是 5s `setInterval`,异常会成为主进程 uncaughtException 并触发
+ * beginShutdown(冷启动崩溃循环,见 issue #1358);
+ * - 也不能退化成 false:那会把 dedupe 基线推成「不忙」,turn 正在跑时其它设备整轮把本机
+ * 看成空闲(本文件开头 PR #166 review New-F 记的就是这个坑)。
+ * 因此 `probeBusy()` 用 null 表示未知,轮询遇到 null 直接跳过本拍、基线保持不动。
*/
+import { createLogger } from '../logger';
+
+const log = createLogger('device-link-busy');
+
let busyProbe: (() => boolean) | null = null;
let lastReportedBusy = false;
+/** 是否已为当前这段连续 probe 失败记过日志(边界切换期每 5s 一条纯噪音)。 */
+let probeFailureLogged = false;
-/** register.ts 注入:返回本机当前是否有任意 session 在 turn 中。传 null 解绑。 */
+/**
+ * register.ts 注入:返回本机当前是否有任意 session 在 turn 中。传 null 解绑。
+ *
+ * 换 probe 一并重置失败日志抑制:抑制只应作用于**同一段**连续失败。否则解绑后再注入
+ * 一个仍会抛错的 probe(账号切换后重建 maker 即走这条路),新一段的首条 warn 会被上一
+ * 段的标志吃掉,排障时看不到它曾经不可用。
+ */
export function setBusyProbe(probe: (() => boolean) | null): void {
busyProbe = probe;
+ probeFailureLogged = false;
}
-/** 当前真实 busy(无 probe 时按 false)。 */
+/**
+ * 探一次 busy:无 probe 按 false;probe 抛错返回 null(未知,见文件头)。
+ * 同一段连续失败只记一条 warn,恢复后重新武装。
+ */
+function probeBusy(): boolean | null {
+ if (!busyProbe) return false;
+ try {
+ const busy = busyProbe();
+ probeFailureLogged = false;
+ return busy;
+ } catch (err) {
+ if (!probeFailureLogged) {
+ probeFailureLogged = true;
+ log.warn('busy probe unavailable; skipping presence ticks until it recovers', {
+ error: err instanceof Error ? err.message : String(err),
+ });
+ }
+ return null;
+ }
+}
+
+/** 当前真实 busy(无 probe 或 probe 不可用时按 false)。 */
export function currentBusy(): boolean {
- return busyProbe?.() ?? false;
+ return probeBusy() ?? false;
}
/**
@@ -38,9 +81,11 @@ export function helloBusy(): boolean {
/**
* 轮询用:当前 busy 与基线不同 → 更新基线并返回新值(需上报);相同 → 返回 null(无需上报)。
+ * probe 不可用(边界切换期)同样返回 null,且**不动基线** —— 等下一拍问得出来再说。
*/
export function pollBusyChange(): boolean | null {
- const busy = currentBusy();
+ const busy = probeBusy();
+ if (busy === null) return null;
if (busy === lastReportedBusy) return null;
lastReportedBusy = busy;
return busy;
@@ -55,6 +100,7 @@ export const __testing = {
reset(): void {
busyProbe = null;
lastReportedBusy = false;
+ probeFailureLogged = false;
},
getLastReported: (): boolean => lastReportedBusy,
};