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(desktop): Android 自动化禁用时打开电脑使用面板不再拉起 adb server (#1829)

DavidShenXD6f23d022d3bf
diff --git a/apps/desktop/src/renderer/components/settings/ComputerUseSection.tsx b/apps/desktop/src/renderer/components/settings/ComputerUseSection.tsx
index 363bc47809..e02cc11323 100644
--- a/apps/desktop/src/renderer/components/settings/ComputerUseSection.tsx
+++ b/apps/desktop/src/renderer/components/settings/ComputerUseSection.tsx
@@ -426,49 +426,37 @@ export function ComputerUseSection({
});
void window.electronAPI.maker.plugins.getState(ANDROID_PLUGIN_ID)
.then((state) => {
- if (!cancelled) {
- setAndroidEnabled(state.effectiveEnabled);
- if (state.effectiveEnabled) {
- setAndroidPreparePending(true);
- void window.electronAPI.maker.android.prepareAdb()
- .then(() => {
- if (!cancelled) {
- return window.electronAPI.maker.android.status()
- .then((status) => {
- if (!cancelled) setAndroidStatus(status);
- });
- }
- return undefined;
- })
- .catch((err) => {
- log.warn('android.prepareAdb failed', err);
- })
- .finally(() => {
- if (!cancelled) setAndroidPreparePending(false);
- });
- }
+ if (cancelled) return;
+ setAndroidEnabled(state.effectiveEnabled);
+ // 插件禁用时 mount 不做任何 adb 探测:status() 会跑 `adb devices -l`,
+ // 5037 上没有 server 时会顺手 fork 一个 daemon(#1806)。禁用态只展示
+ // 提示文案;探测留到用户开启开关或手动点「刷新」时进行。
+ if (!state.effectiveEnabled) {
+ setAndroidStatusPending(false);
+ return;
}
+ setAndroidPreparePending(true);
+ void window.electronAPI.maker.android.prepareAdb()
+ .then(() => (cancelled ? undefined : window.electronAPI.maker.android.status()))
+ .then((status) => {
+ if (!cancelled && status) setAndroidStatus(status);
+ })
+ .catch((err) => {
+ // 这个 catch 同时兜 prepareAdb 与后续 status 的失败,文案别写死单边。
+ log.warn('android adb probe (prepareAdb/status) failed', err);
+ if (!cancelled) setAndroidStatus(androidStatusFallback(err));
+ })
+ .finally(() => {
+ if (!cancelled) {
+ setAndroidPreparePending(false);
+ setAndroidStatusPending(false);
+ }
+ });
})
.catch((err) => {
log.warn('plugins.getState(android) failed', err);
if (!cancelled) {
setAndroidEnabled(false);
- }
- });
- void window.electronAPI.maker.android.status()
- .then((status) => {
- if (!cancelled) {
- setAndroidStatus(status);
- }
- })
- .catch((err) => {
- log.warn('android.status failed', err);
- if (!cancelled) {
- setAndroidStatus(androidStatusFallback(err));
- }
- })
- .finally(() => {
- if (!cancelled) {
setAndroidStatusPending(false);
}
});
@@ -903,6 +891,10 @@ export function ComputerUseSection({
setAndroidPreparePending(true);
await window.electronAPI.maker.android.prepareAdb();
await handleRefreshAndroidStatus(false);
+ } else {
+ // 关闭时清掉旧探测结果:状态区回到禁用提示,设备选择入口一并禁用,
+ // 不再展示已过时的就绪/设备状态(#1829 review)。
+ setAndroidStatus(null);
}
toast.success(
next
@@ -1194,7 +1186,12 @@ export function ComputerUseSection({
configuredDefaultAndroidDevice
&& !androidDevices.some((device) => device.device_serial === configuredDefaultAndroidDevice),
);
- const androidDeviceStatusText = describeAndroidDeviceStatus(androidStatus, t);
+ // 禁用且尚无任何探测结果时显示禁用提示,避免落在 describeAndroidDeviceStatus
+ // 的「正在检查…」上(禁用态 mount 不探测,#1806)。用户手动「刷新」拿到结果后
+ // 仍按真实 status 展示。
+ const androidDeviceStatusText = androidEnabled === false && !androidStatus
+ ? t('settings.computerUse.android.status.disabled')
+ : describeAndroidDeviceStatus(androidStatus, t);
const androidConnectionGuideKind = getAndroidConnectionGuideKind(androidStatus);
const androidAdbSource = androidStatus?.adb_path_source ?? androidStatus?.adb_preparation?.source ?? null;
const androidAdbSourceText = androidPreparePending
diff --git a/apps/desktop/src/renderer/components/settings/__tests__/ComputerUseSection.androidDisabledGate.test.tsx b/apps/desktop/src/renderer/components/settings/__tests__/ComputerUseSection.androidDisabledGate.test.tsx
new file mode 100644
index 0000000000..1de83f93c7
--- /dev/null
+++ b/apps/desktop/src/renderer/components/settings/__tests__/ComputerUseSection.androidDisabledGate.test.tsx
@@ -0,0 +1,166 @@
+// @vitest-environment jsdom
+
+/**
+ * Issue #1806 回归:Android 自动化插件禁用时,打开「电脑使用」面板不得触发
+ * 任何 adb 探测(status() 会跑 `adb devices -l`,5037 无 server 时会顺手 fork
+ * 一个 daemon)。禁用态只展示提示文案;启用态保持原有 prepareAdb → status 链路。
+ */
+
+import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+
+vi.mock('react-i18next', () => ({
+ useTranslation: () => ({ t: (key: string) => key }),
+}));
+
+vi.mock('@/lib/toast', () => ({
+ toast: {
+ info: vi.fn(),
+ success: vi.fn(),
+ warning: vi.fn(),
+ error: vi.fn(),
+ },
+}));
+
+import { ComputerUseSection } from '../ComputerUseSection';
+
+function installElectronApi(options: { androidEnabled: boolean }) {
+ const androidStatus = vi.fn(async () => ({
+ adb_available: true,
+ adb_path: '/sdk/platform-tools/adb',
+ adb_path_source: 'sdk',
+ version: 'Android Debug Bridge version 1.0.41',
+ devices: [],
+ default_device_serial: null,
+ configured_default_device_serial: null,
+ issue: 'NO_DEVICE',
+ error: null,
+ }));
+ const androidPrepareAdb = vi.fn(async () => ({ status: 'ready', source: 'sdk' }));
+ const androidGetConfig = vi.fn(async () => ({
+ value: { defaultDeviceSerial: null, adbPathOverride: null },
+ }));
+ const pluginsGetState = vi.fn(async (pluginId: string) => ({
+ pluginId,
+ effectiveEnabled: pluginId === 'android' ? options.androidEnabled : false,
+ }));
+
+ Object.defineProperty(window, 'electronAPI', {
+ configurable: true,
+ value: {
+ platform: 'win32',
+ openExternal: vi.fn(async () => ({ success: true })),
+ maker: {
+ plugins: {
+ getState: pluginsGetState,
+ setEnabled: vi.fn(async () => ({ codexMcpRefreshed: true })),
+ setProjectEnabled: vi.fn(),
+ },
+ browser: {
+ status: vi.fn(async () => ({
+ detected: false,
+ browserKind: null,
+ executablePath: null,
+ })),
+ },
+ computer: {
+ status: vi.fn(async () => ({
+ installed: false,
+ executablePath: null,
+ version: null,
+ daemonRunning: false,
+ installCommand: 'noop',
+ docsUrl: 'https://example.invalid/docs',
+ })),
+ checkUpdate: vi.fn(async () => ({ updateAvailable: false, updating: false })),
+ onPermissionGuideStatusChanged: vi.fn(() => () => {}),
+ onPermissionGuideCancelled: vi.fn(() => () => {}),
+ onUpdateProgress: vi.fn(() => () => {}),
+ cancelPermissionGrant: vi.fn(async () => {}),
+ },
+ android: {
+ getConfig: androidGetConfig,
+ status: androidStatus,
+ prepareAdb: androidPrepareAdb,
+ setDefaultDevice: vi.fn(),
+ setAdbPath: vi.fn(),
+ },
+ },
+ } as unknown as Window['electronAPI'],
+ });
+
+ return { androidStatus, androidPrepareAdb, androidGetConfig, pluginsGetState };
+}
+
+afterEach(() => {
+ cleanup();
+ Reflect.deleteProperty(window, 'electronAPI');
+ vi.clearAllMocks();
+});
+
+describe('ComputerUseSection android disabled gate (#1806)', () => {
+ it('does not probe adb on mount while the android plugin is disabled', async () => {
+ const api = installElectronApi({ androidEnabled: false });
+
+ render(<ComputerUseSection workingDir="/repo" />);
+
+ // 等 mount 数据链路完全落地(插件状态 + 配置都已返回)再断言。
+ await waitFor(() => {
+ expect(api.pluginsGetState).toHaveBeenCalledWith('android');
+ });
+ await waitFor(() => {
+ expect(
+ screen.getByText('settings.computerUse.android.status.disabled'),
+ ).toBeTruthy();
+ });
+
+ // 核心断言:禁用态 mount 绝不触碰 adb(status 会拉起 adb server)。
+ expect(api.androidStatus).not.toHaveBeenCalled();
+ expect(api.androidPrepareAdb).not.toHaveBeenCalled();
+ // 纯设置读取不受影响。
+ expect(api.androidGetConfig).toHaveBeenCalled();
+ });
+
+ it('keeps prepareAdb → status probing on mount when the android plugin is enabled', async () => {
+ const api = installElectronApi({ androidEnabled: true });
+
+ render(<ComputerUseSection workingDir="/repo" />);
+
+ await waitFor(() => {
+ expect(api.androidStatus).toHaveBeenCalledOnce();
+ });
+ expect(api.androidPrepareAdb).toHaveBeenCalledOnce();
+ // prepareAdb 先于 status:status 必须消费准备完成后的 adb 解析结果。
+ const prepareOrder = api.androidPrepareAdb.mock.invocationCallOrder[0]!;
+ const statusOrder = api.androidStatus.mock.invocationCallOrder[0]!;
+ expect(prepareOrder).toBeLessThan(statusOrder);
+ // 启用态不显示禁用提示。
+ expect(
+ screen.queryByText('settings.computerUse.android.status.disabled'),
+ ).toBeNull();
+ });
+
+ it('clears the stale probe result when the android plugin is toggled off', async () => {
+ const api = installElectronApi({ androidEnabled: true });
+
+ render(<ComputerUseSection workingDir="/repo" />);
+
+ await waitFor(() => {
+ expect(api.androidStatus).toHaveBeenCalledOnce();
+ });
+
+ fireEvent.click(
+ screen.getByRole('switch', { name: 'settings.computerUse.android.toggleAria' }),
+ );
+
+ // 关闭后状态区回到禁用提示,不残留旧的就绪/设备状态。
+ await waitFor(() => {
+ expect(
+ screen.getByText('settings.computerUse.android.status.disabled'),
+ ).toBeTruthy();
+ });
+ // 关闭动作本身不得触发新的 adb 探测。
+ expect(api.androidStatus).toHaveBeenCalledOnce();
+ expect(api.androidPrepareAdb).toHaveBeenCalledOnce();
+ });
+});
diff --git a/apps/desktop/src/renderer/i18n/locales/en/common.json b/apps/desktop/src/renderer/i18n/locales/en/common.json
index a5b5330eed..04e7e17ba5 100644
--- a/apps/desktop/src/renderer/i18n/locales/en/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/en/common.json
@@ -2418,6 +2418,7 @@
"toggleHint": "This machine-wide toggle only applies to agent sessions started afterward; it does not affect a session that is already running.",
"status": {
"checking": "Checking ADB status…",
+ "disabled": "Android automation is off. Turn it on to detect ADB and devices.",
"preparing": "Preparing ADB…",
"ready": "ADB ready. Ready devices: {{count}}. Default: {{device}}",
"adbDetail": "{{version}} at {{path}}",
diff --git a/apps/desktop/src/renderer/i18n/locales/ja/common.json b/apps/desktop/src/renderer/i18n/locales/ja/common.json
index 80a05291e2..9eed1c43e7 100644
--- a/apps/desktop/src/renderer/i18n/locales/ja/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/ja/common.json
@@ -2417,6 +2417,7 @@
"toggleHint": "このマシン全体の切り替えは、以降に開始した Agent セッションにのみ反映されます。実行中のセッションには影響しません。",
"status": {
"checking": "ADB の状態を確認しています…",
+ "disabled": "Android 自動操作はオフです。オンにすると ADB とデバイスを検出します。",
"preparing": "ADB を準備しています…",
"ready": "ADB は準備できています。利用可能なデバイス: {{count}}。デフォルト: {{device}}",
"adbDetail": "{{version}}({{path}})",
diff --git a/apps/desktop/src/renderer/i18n/locales/ko/common.json b/apps/desktop/src/renderer/i18n/locales/ko/common.json
index f72ebb89ad..6a4ea6a5d8 100644
--- a/apps/desktop/src/renderer/i18n/locales/ko/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/ko/common.json
@@ -2417,6 +2417,7 @@
"toggleHint": "이 시스템 전체 토글은 이후 시작하는 Agent 세션에만 적용되며, 진행 중인 세션에는 영향을 주지 않습니다.",
"status": {
"checking": "ADB 상태 확인 중…",
+ "disabled": "Android 자동화가 꺼져 있습니다. 켜면 ADB와 기기를 감지합니다.",
"preparing": "ADB 준비 중…",
"ready": "ADB 준비됨. 사용 가능한 기기: {{count}}. 기본값: {{device}}",
"adbDetail": "{{version}} 위치: {{path}}",
diff --git a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
index 77decfdc58..026a784ac1 100644
--- a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
+++ b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json
@@ -2417,6 +2417,7 @@
"toggleHint": "这个全局开关只对之后新建的 Agent 会话生效;已在运行的 Agent 会话不受影响。",
"status": {
"checking": "正在检查 ADB 状态…",
+ "disabled": "Android 自动化未启用。启用后将检测 ADB 与设备。",
"preparing": "正在准备 ADB…",
"ready": "ADB 已就绪。可用设备:{{count}}。默认设备:{{device}}",
"adbDetail": "{{version}},路径 {{path}}",