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): preserve selected model during agent switches (#4174)

Chris Zhangf7215b10d089
diff --git a/apps/desktop/src/renderer/__tests__/vendorModelFallback.test.ts b/apps/desktop/src/renderer/__tests__/vendorModelFallback.test.ts
index 2d2cac6ba1..55a0f88f33 100644
--- a/apps/desktop/src/renderer/__tests__/vendorModelFallback.test.ts
+++ b/apps/desktop/src/renderer/__tests__/vendorModelFallback.test.ts
@@ -60,6 +60,45 @@ const PROVIDERS: ProviderView[] = [
];
describe('shouldFallbackVendorModel', () => {
+ const subscriptionRoutes = [
+ provider('openai', 'builtin', {
+ codex: ['gpt-6-astra'],
+ pi: ['chatgpt/gpt-6-astra'],
+ }),
+ provider('xd', 'builtin', { pi: ['glm-5.3'] }),
+ ];
+
+ it('does not repair the old model while a cross-engine intent masks it', () => {
+ // This exact mixed-generation comparison previously selected the Pi seed.
+ expect(shouldFallbackVendorModel(subscriptionRoutes, 'gpt-6-astra', 'pi')).toBe(true);
+ expect(shouldFallbackVendorModel(subscriptionRoutes, 'gpt-6-astra', 'pi', {
+ hasSwitchIntent: true,
+ })).toBe(false);
+ // Persisted fields still belong to Codex until Main commits the switch.
+ expect(shouldFallbackVendorModel(subscriptionRoutes, 'gpt-6-astra', 'codex')).toBe(false);
+ });
+
+ it('preserves a bound route after intent consumption, even with a partial catalog', () => {
+ expect(shouldFallbackVendorModel(subscriptionRoutes, 'chatgpt/gpt-6-astra', 'pi', {
+ providerId: 'openai',
+ })).toBe(false);
+ // A delayed snapshot/other controller must not turn openai + GPT into openai + GLM.
+ expect(shouldFallbackVendorModel(subscriptionRoutes, 'gpt-6-astra', 'pi', {
+ providerId: 'openai',
+ })).toBe(false);
+ });
+
+ it('preserves host-authoritative selection when a phone or remote desktop switches it', () => {
+ // Host Renderer sees an intent registered elsewhere, not a local click flag.
+ expect(shouldFallbackVendorModel(subscriptionRoutes, 'gpt-6-astra', 'pi', {
+ hasSwitchIntent: true,
+ })).toBe(false);
+ // Control-side and SSH catalogs cannot establish that a host route is invalid.
+ expect(shouldFallbackVendorModel(subscriptionRoutes, 'gpt-6-astra', 'pi', {
+ isRemote: true,
+ })).toBe(false);
+ });
+
it('keeps a custom (mimo) codex model in a codex session (the fixed bug)', () => {
expect(shouldFallbackVendorModel(PROVIDERS, 'mimo-codex', 'codex')).toBe(false);
});
diff --git a/apps/desktop/src/renderer/features/cc-agent/CCAgentSessionView.tsx b/apps/desktop/src/renderer/features/cc-agent/CCAgentSessionView.tsx
index ada041d80e..bc3e841fba 100644
--- a/apps/desktop/src/renderer/features/cc-agent/CCAgentSessionView.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/CCAgentSessionView.tsx
@@ -3942,7 +3942,7 @@ export function CCAgentSessionView({
t,
]);
- // M35: Vendor fallback —— 会话的 model 与它(固定不变的)agent vendor 明确错配时,
+ // M35: Vendor fallback —— 仅修复未绑定 provider、且没有切换意图的历史行。
// 回退到该 vendor 的默认模型。守的是「绕过模型选择器写入 session.model」的脏数据路径
// (历史 DB 行 / 上古 model id),选择器主动选择的 happy path 由 provider 逻辑自己保证一致。
//
@@ -3955,15 +3955,19 @@ export function CCAgentSessionView({
// (别名 / 脏数据 / 目录未加载的瞬间)一律不动,避免 load race 误杀。cc / codex 统一这一套。
const sessionAgentKind = session?.agentKind;
const sessionModel = session?.model;
+ const sessionProviderId = session?.providerId;
useEffect(() => {
if (readOnly) return;
if (!sessionAgentKind || !sessionModel || !sessionId) return;
// device-link 远程会话:vendor↔model 一致性由被控端权威保证。控制端不能替它"纠正"——
// 远程模型可能只存在于被控端(本地目录查不到 → 误判跨 vendor),且本地 DB 没有该会话行,
// sessionService.update 会写错 / refreshServerSession 对远程是 no-op。直接跳过(规则:host 为准)。
- if (getSessionDeviceId(sessionId)) return;
- const agent = displayAgentKind;
- if (!shouldFallbackVendorModel(providers, sessionModel, agent)) return;
+ const agent = dbToMakerAgentKind(sessionAgentKind);
+ if (!shouldFallbackVendorModel(providers, sessionModel, agent, {
+ providerId: sessionProviderId,
+ hasSwitchIntent: agentSwitchIntent != null,
+ isRemote: Boolean(isRemoteSession || remoteDeviceId || getSessionDeviceId(sessionId)),
+ })) return;
// 用三值化后的 agent 映射选默认模型:Pi 会话必须回退到 Pi 目录默认,而不是被
// `isCodex ? 'codex' : 'cc'` 误写成 CC 首选(可能是更贵的 Opus)(codex review)。
const defaultModel = getDefaultModelForVendor(
@@ -3974,12 +3978,15 @@ export function CCAgentSessionView({
.then(() => refreshServerSession())
.catch((err) => log.warn('vendor fallback patch failed:', err));
}, [
- displayAgentKind,
+ agentSwitchIntent,
+ isRemoteSession,
providers,
refreshServerSession,
+ remoteDeviceId,
sessionAgentKind,
sessionId,
sessionModel,
+ sessionProviderId,
readOnly,
]);
diff --git a/apps/desktop/src/renderer/features/cc-agent/lib/vendorModelFallback.ts b/apps/desktop/src/renderer/features/cc-agent/lib/vendorModelFallback.ts
index 1970ff5080..affdebb6c0 100644
--- a/apps/desktop/src/renderer/features/cc-agent/lib/vendorModelFallback.ts
+++ b/apps/desktop/src/renderer/features/cc-agent/lib/vendorModelFallback.ts
@@ -26,14 +26,25 @@ import { providerOffersModel, type AgentKind, type ProviderView } from '@cindy/m
*
* @param providers live 供应商视图(useProviders 的结果;含内置 + 自定义,按 agent 挂模型)。
* @param sessionModel 会话当前存的 model id。
- * @param agent 会话所属 agent(建会话即固定,不可变)。
+ * @param agent 与 sessionModel 同一持久化快照中的 agent,不得传展示意图的 agent。
* @returns true = 需要回退(model 明确属于另一个 vendor);false = 保持不动。
*/
export function shouldFallbackVendorModel(
providers: ProviderView[],
sessionModel: string,
agent: AgentKind,
+ context: {
+ providerId?: string | null;
+ hasSwitchIntent?: boolean;
+ isRemote?: boolean;
+ } = {},
): boolean {
+ // Only repair unbound legacy rows. A selected route belongs to Main, including
+ // routes selected by mobile/another desktop while this view is open. Catalog
+ // absence is not permission to replace that route with a new-session seed.
+ // Pending intent and persisted model are different generations (and may use
+ // different wire IDs); never compare one generation's model to the other's agent.
+ if (context.providerId || context.hasSwitchIntent || context.isRemote) return false;
// 本端任一供应商 offer 该模型 → 合法(含自定义供应商、gpt-5.x 等),绝不回退。
if (providers.some((p) => providerOffersModel(p, sessionModel, agent))) return false;
// 本端不 offer:仅当对端 agent 明确 offer 它(确定的跨 vendor 错配)才回退;