From b864bf55b60e36eb00545d9dc528a3110c0957c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=A4=A7=E7=8C=AB?= <16399091+binaricat@users.noreply.github.com> Date: Sat, 21 Nov 2026 17:38:41 +0800 Subject: [PATCH] fix(ai): retain configured default agent during failed discovery --- application/i18n/locales/en/ai.ts | 1 + application/i18n/locales/es/ai.ts | 1 + application/i18n/locales/ru/ai.ts | 1 + application/i18n/locales/zh-CN/ai.ts | 1 + application/i18n/locales/zh-TW/ai.ts | 1 + components/settings/settings-ui.tsx | 3 +- .../tabs/SettingsAITab.lifecycle.test.ts | 63 ++++++++++++++++++- components/settings/tabs/SettingsAITab.tsx | 24 ++++--- .../settings/tabs/ai/managedAgentState.ts | 14 +++++ 9 files changed, 100 insertions(+), 9 deletions(-) diff --git a/application/i18n/locales/en/ai.ts b/application/i18n/locales/en/ai.ts index 458894fad..a351aceb8 100644 --- a/application/i18n/locales/en/ai.ts +++ b/application/i18n/locales/en/ai.ts @@ -333,6 +333,7 @@ export const enAiMessages: Messages = { 'ai.defaultAgent': 'Default Agent', 'ai.defaultAgent.description': 'Agent to use when starting a new AI session', 'ai.defaultAgent.catty': 'Catty (Built-in)', + 'ai.defaultAgent.unavailable': 'Unavailable', 'ai.toolAccess.title': 'Tool Access', 'ai.toolAccess.mode': 'Netcatty Access Mode', 'ai.toolAccess.description': 'Choose how external agents access Netcatty sessions. MCP exposes the built-in server, while Skills + CLI points agents to the local Netcatty skill and CLI commands.', diff --git a/application/i18n/locales/es/ai.ts b/application/i18n/locales/es/ai.ts index 135452601..136375bde 100644 --- a/application/i18n/locales/es/ai.ts +++ b/application/i18n/locales/es/ai.ts @@ -333,6 +333,7 @@ export const esAiMessages: Messages = { 'ai.defaultAgent': 'Agente predeterminado', 'ai.defaultAgent.description': 'Agente que se usará al iniciar una nueva sesión de IA', 'ai.defaultAgent.catty': 'Catty (Integrado)', + 'ai.defaultAgent.unavailable': 'No disponible', 'ai.toolAccess.title': 'Acceso a herramientas', 'ai.toolAccess.mode': 'Modo de acceso de Netcatty', 'ai.toolAccess.description': 'Elige cómo acceden los agentes externos a las sesiones de Netcatty. MCP expone el servidor integrado, mientras que Habilidades + CLI apunta a los agentes hacia las habilidades locales de Netcatty y los comandos de CLI.', diff --git a/application/i18n/locales/ru/ai.ts b/application/i18n/locales/ru/ai.ts index 611f2cfb5..394427c1b 100644 --- a/application/i18n/locales/ru/ai.ts +++ b/application/i18n/locales/ru/ai.ts @@ -273,6 +273,7 @@ export const ruAiMessages: Messages = { 'ai.defaultAgent': 'Агент по умолчанию', 'ai.defaultAgent.description': 'Агент, который будет использоваться при запуске новой AI-сессии', 'ai.defaultAgent.catty': 'Catty (встроенный)', + 'ai.defaultAgent.unavailable': 'Недоступен', 'ai.toolAccess.title': 'Доступ к инструментам', 'ai.toolAccess.mode': 'Режим доступа Netcatty', 'ai.toolAccess.description': 'Выберите, как внешние агенты получают доступ к сессиям Netcatty. MCP предоставляет встроенный сервер, а Skills + CLI указывает агентам на локальный skill Netcatty и команды CLI.', diff --git a/application/i18n/locales/zh-CN/ai.ts b/application/i18n/locales/zh-CN/ai.ts index 2151cd86d..82e6fdb84 100644 --- a/application/i18n/locales/zh-CN/ai.ts +++ b/application/i18n/locales/zh-CN/ai.ts @@ -333,6 +333,7 @@ export const zhCNAiMessages: Messages = { 'ai.defaultAgent': '默认 Agent', 'ai.defaultAgent.description': '创建新 AI 会话时使用的 Agent', 'ai.defaultAgent.catty': 'Catty(内置)', + 'ai.defaultAgent.unavailable': '暂不可用', 'ai.toolAccess.title': '工具接入', 'ai.toolAccess.mode': 'Netcatty 接入模式', 'ai.toolAccess.description': '选择外部 Agent 访问 Netcatty 会话的方式。MCP 会暴露内置服务器,Skills + CLI 会引导 Agent 读取本地 Skill 并调用 Netcatty CLI。', diff --git a/application/i18n/locales/zh-TW/ai.ts b/application/i18n/locales/zh-TW/ai.ts index e74449b6b..f8c25e0d4 100644 --- a/application/i18n/locales/zh-TW/ai.ts +++ b/application/i18n/locales/zh-TW/ai.ts @@ -333,6 +333,7 @@ export const zhTWAiMessages: Messages = { 'ai.defaultAgent': '預設 Agent', 'ai.defaultAgent.description': '建立新 AI 工作階段時使用的 Agent', 'ai.defaultAgent.catty': 'Catty(內建)', + 'ai.defaultAgent.unavailable': '暫時無法使用', 'ai.toolAccess.title': '工具介接', 'ai.toolAccess.mode': 'Netcatty 介接模式', 'ai.toolAccess.description': '選擇外部 Agent 存取 Netcatty 工作階段的方式。MCP 會公開內建伺服器,Skills + CLI 會引導 Agent 讀取本機 Skill 並呼叫 Netcatty CLI。', diff --git a/components/settings/settings-ui.tsx b/components/settings/settings-ui.tsx index cc7149f92..362a8ea36 100644 --- a/components/settings/settings-ui.tsx +++ b/components/settings/settings-ui.tsx @@ -36,7 +36,7 @@ export const Toggle: React.FC = ({ checked, onChange, disabled, ari interface SelectProps { value: string; - options: { value: string; label: string; icon?: React.ReactNode }[]; + options: { value: string; label: string; icon?: React.ReactNode; disabled?: boolean }[]; onChange: (value: string) => void; className?: string; disabled?: boolean; @@ -87,6 +87,7 @@ export const Select: React.FC = ({ diff --git a/components/settings/tabs/SettingsAITab.lifecycle.test.ts b/components/settings/tabs/SettingsAITab.lifecycle.test.ts index f63821f22..19728138f 100644 --- a/components/settings/tabs/SettingsAITab.lifecycle.test.ts +++ b/components/settings/tabs/SettingsAITab.lifecycle.test.ts @@ -2,9 +2,12 @@ import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import test from "node:test"; import vm from "node:vm"; -import React, { useCallback, useEffect, useRef, useState } from "react"; +import React, { useCallback, useEffect, useMemo, useRef, useState } from "react"; import TestRenderer, { act } from "react-test-renderer"; import ts from "typescript"; +import { canSendWithAgent } from "../../ai/agentSendEligibility"; +import { buildManagedAgentState, getUnavailablePreferredHarness } from "./ai/managedAgentState"; +import type { ExternalAgentConfig } from "../../../infrastructure/ai/types"; (globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; @@ -94,3 +97,61 @@ test("an older AI detection result cannot replace or finish the newer request", assert.equal(h.state.busy, false); } finally { await act(async () => h.root.unmount()); } }); + + +// Mount the same option computation and normalization effect that the +// Settings tab runs after a probe has updated the actual saved agent state. +const pickerStart = source.indexOf(" // Agent options for default agent"); +const pickerEnd = source.indexOf("\n useEffect(() => {\n const bridge", pickerStart); +assert.ok(pickerStart >= 0 && pickerEnd > pickerStart); +const runDefaultPicker = vm.runInNewContext(ts.transpile(`(function(ctx) { + const { useMemo, useEffect, externalAgents, defaultAgentId, setDefaultAgentId, t, + canSendWithAgent, getUnavailablePreferredHarness, React, AgentIconBadge } = ctx; + ${source.slice(pickerStart, pickerEnd)} + return agentOptions; +})`, { target: ts.ScriptTarget.ES2022, jsx: ts.JsxEmit.React }), { console }) as + (ctx: Record) => { value: string; disabled?: boolean }[]; + +test("Settings retains the unavailable saved default, blocks sends and restores the same choice", async () => { + const agent: ExternalAgentConfig = { + id: "discovered_copilot", + name: "GitHub Copilot CLI", + command: "/fixture/copilot", + commandSource: "manual", + sdkBackend: "copilot", + enabled: true, + available: true, + }; + let options: ReturnType = []; + let choice = ""; + function Harness({ agents }: { agents: ExternalAgentConfig[] }) { + const [defaultAgentId, setDefaultAgentId] = useState(agent.id); + options = runDefaultPicker({ + useMemo, useEffect, externalAgents: agents, defaultAgentId, setDefaultAgentId, + t: (key: string) => key, canSendWithAgent, getUnavailablePreferredHarness, React, + AgentIconBadge: () => null, + }); + choice = defaultAgentId; + return null; + } + const failed = buildManagedAgentState([agent], agent.id, "copilot", null, "manual"); + const recovered = buildManagedAgentState(failed.agents, agent.id, "copilot", { + path: agent.command, available: true, version: "1.0.106", + }, "manual"); + let root!: TestRenderer.ReactTestRenderer; + const render = (agents: ExternalAgentConfig[]) => React.createElement(Harness, { agents }); + try { + await act(async () => { root = TestRenderer.create(render([agent])); }); + await act(async () => { root.update(render(failed.agents)); }); + assert.equal(choice, agent.id); + assert.equal(options.find(option => option.value === agent.id)?.disabled, true); + assert.equal(canSendWithAgent(choice, failed.agents), false); + await act(async () => { root.update(render(recovered.agents)); }); + assert.equal(choice, agent.id); + assert.equal(options.find(option => option.value === agent.id)?.disabled, undefined); + assert.equal(canSendWithAgent(choice, recovered.agents), true); + const reset = buildManagedAgentState(recovered.agents, agent.id, "copilot", null, "auto"); + await act(async () => { root.update(render(reset.agents)); }); + assert.equal(choice, "catty"); + } finally { await act(async () => root.unmount()); } +}); diff --git a/components/settings/tabs/SettingsAITab.tsx b/components/settings/tabs/SettingsAITab.tsx index b0c8cc911..4942edc15 100644 --- a/components/settings/tabs/SettingsAITab.tsx +++ b/components/settings/tabs/SettingsAITab.tsx @@ -68,6 +68,7 @@ import { areExternalAgentListsEqual, buildManagedAgentState, getInitialManagedAgentPaths, + getUnavailablePreferredHarness, updateCodebuddyManagedEnv, updateCodebuddyManagedOptions, } from "./ai/managedAgentState"; @@ -655,13 +656,22 @@ const SettingsAITab: React.FC = ({ [removeProvider, editingProviderId, removeProviderConfirm], ); - // Agent options for default agent - const agentOptions = useMemo(() => [ - { value: "catty", label: t('ai.defaultAgent.catty'), icon: }, - ...externalAgents - .filter((a) => canSendWithAgent(a.id, externalAgents)) - .map((a) => ({ value: a.id, label: a.name, icon: })), - ], [externalAgents, t]); + // Agent options for default agent. Include a saved default that is temporarily offline as a disabled option. + // This preserves the setting without offering that harness for new selections. + const agentOptions = useMemo(() => { + const unavailable = getUnavailablePreferredHarness(externalAgents, defaultAgentId); + return [ + { value: "catty", label: t('ai.defaultAgent.catty'), icon: }, + ...externalAgents + .filter((a) => a === unavailable || canSendWithAgent(a.id, externalAgents)) + .map((a) => ({ + value: a.id, + label: a === unavailable ? `${a.name} (${t('ai.defaultAgent.unavailable')})` : a.name, + icon: , + disabled: a === unavailable ? true : undefined, + })), + ]; + }, [externalAgents, defaultAgentId, t]); useEffect(() => { if (!agentOptions.some((option) => option.value === defaultAgentId)) { diff --git a/components/settings/tabs/ai/managedAgentState.ts b/components/settings/tabs/ai/managedAgentState.ts index 2595862fa..4d710a81d 100644 --- a/components/settings/tabs/ai/managedAgentState.ts +++ b/components/settings/tabs/ai/managedAgentState.ts @@ -1,12 +1,26 @@ import type { ExternalAgentConfig } from "../../../../infrastructure/ai/types"; import { type ManagedAgentKey, + getExternalAgentSdkBackend, isPathLikeCommand, } from "../../../../infrastructure/ai/managedAgents"; import type { AgentPathInfo } from "./types"; import { AGENT_DEFAULTS, isCursorAvailableForMode } from "./types"; import { buildCodebuddyEnv } from "./codebuddyConfigEnv"; +/** A saved, enabled harness can remain preferred while its version probe fails. */ +export function getUnavailablePreferredHarness( + agents: ExternalAgentConfig[], + defaultAgentId: string, +): ExternalAgentConfig | undefined { + return agents.find((agent) => + agent.id === defaultAgentId && + Object.keys(AGENT_DEFAULTS).some((key) => agent.id === `discovered_${key}`) && + agent.enabled && agent.available === false && + agent.commandSource !== "auto" && isPathLikeCommand(agent.command) && + Boolean(getExternalAgentSdkBackend(agent))); +} + function getAutoManagedAgentStoredPath( agents: ExternalAgentConfig[], agentKey: ManagedAgentKey,