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 calibrated provider routing (#1311)

0xKingVee9527a5963306f7a9
diff --git a/apps/desktop/src/renderer/__tests__/draftModelCalibration.test.ts b/apps/desktop/src/renderer/__tests__/draftModelCalibration.test.ts
index 165744f584..d8103b05c8 100644
--- a/apps/desktop/src/renderer/__tests__/draftModelCalibration.test.ts
+++ b/apps/desktop/src/renderer/__tests__/draftModelCalibration.test.ts
@@ -12,6 +12,7 @@ import type { CatalogModel, ProviderView } from '@cindy/model-providers';
import {
calibrateDraftModel,
pickConnectedModelForAgent,
+ resolveDraftSessionProviderId,
type DraftModelCalibrationInput,
} from '../lib/draftModelCalibration';
@@ -467,3 +468,109 @@ describe('校准结果要带上供应商', () => {
expect(result).toEqual({ model: 'claude-opus-5', providerId: 'anthropic' });
});
});
+
+describe('resolveDraftSessionProviderId', () => {
+ it('XD 已连接但不提供当前模型时,不能省略校准选中的 Anthropic 来源(issue #1196)', () => {
+ const gateway = provider('xd', true, {
+ 'claude-code': [model('claude-sonnet-5')],
+ });
+ const anthropic = provider(
+ 'anthropic',
+ true,
+ { 'claude-code': [model('claude-opus-5')] },
+ 'subscription',
+ );
+
+ expect(
+ resolveDraftSessionProviderId({
+ providers: [gateway, anthropic],
+ agent: 'claude-code',
+ model: 'claude-opus-5',
+ explicitProviderId: null,
+ effectiveProviderId: 'anthropic',
+ }),
+ ).toBe('anthropic');
+ });
+
+ it('只有 Anthropic 已连接时仍可省略来源,保留默认路由语义', () => {
+ const anthropic = provider(
+ 'anthropic',
+ true,
+ { 'claude-code': [model('claude-opus-5')] },
+ 'subscription',
+ );
+
+ expect(
+ resolveDraftSessionProviderId({
+ providers: [anthropic],
+ agent: 'claude-code',
+ model: 'claude-opus-5',
+ explicitProviderId: null,
+ effectiveProviderId: 'anthropic',
+ }),
+ ).toBeNull();
+ });
+
+ it('XD 被停用时仍按 main 的 spawn 默认语义保留 Anthropic 来源', () => {
+ const suspendedGateway = {
+ ...provider('xd', true, {
+ 'claude-code': [model('claude-opus-5')],
+ }),
+ suspended: true,
+ } as ProviderView;
+ const anthropic = provider(
+ 'anthropic',
+ true,
+ { 'claude-code': [model('claude-opus-5')] },
+ 'subscription',
+ );
+
+ expect(
+ resolveDraftSessionProviderId({
+ providers: [suspendedGateway, anthropic],
+ agent: 'claude-code',
+ model: 'claude-opus-5',
+ explicitProviderId: null,
+ effectiveProviderId: 'anthropic',
+ }),
+ ).toBe('anthropic');
+ });
+
+ it('Codex 折扣模型只由 XD 提供时显式保留 XD,锁定既有实际落点', () => {
+ const openai = provider(
+ 'openai',
+ true,
+ { codex: [model('gpt-5.6-sol')] },
+ 'subscription',
+ );
+ const gateway = provider('xd', true, {
+ codex: [model('codex/gpt-5.6-sol')],
+ });
+
+ expect(
+ resolveDraftSessionProviderId({
+ providers: [openai, gateway],
+ agent: 'codex',
+ model: 'codex/gpt-5.6-sol',
+ explicitProviderId: null,
+ effectiveProviderId: 'xd',
+ }),
+ ).toBe('xd');
+ });
+
+ it('用户显式选择且仍有效时始终保留该来源', () => {
+ const gateway = provider('xd', true, {
+ 'claude-code': [model('claude-opus-5')],
+ });
+
+ expect(
+ resolveDraftSessionProviderId({
+ providers: [gateway],
+ agent: 'claude-code',
+ model: 'claude-opus-5',
+ explicitProviderId: 'xd',
+ effectiveProviderId: 'xd',
+ }),
+ ).toBe('xd');
+ });
+});
diff --git a/apps/desktop/src/renderer/features/cc-agent/NewMakerDraftRoute.tsx b/apps/desktop/src/renderer/features/cc-agent/NewMakerDraftRoute.tsx
index 4f5429570b..28d875bfc1 100644
--- a/apps/desktop/src/renderer/features/cc-agent/NewMakerDraftRoute.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/NewMakerDraftRoute.tsx
@@ -106,7 +106,11 @@ import {
} from '@/lib/composerDraftStore';
import type { JSONContent } from '@tiptap/core';
import { base64ToUint8Array } from '@/lib/fileTypeInference';
-import { calibrateDraftModel, type DraftModelCalibrationResult } from '@/lib/draftModelCalibration';
+import {
+ calibrateDraftModel,
+ resolveDraftSessionProviderId,
+ type DraftModelCalibrationResult,
+} from '@/lib/draftModelCalibration';
import { showWorktreeError } from '@/lib/worktreeToast';
import type { CreateWorktreeResp } from '@/lib/worktree.types';
import * as sessionService from '@/lib/sessionService';
@@ -1393,19 +1397,17 @@ export function NewMakerDraftRoute() {
// **未过滤**的目录,被用户隐藏、被 SSH 订阅直连排除、被 chat-bridge 排除掉的来源在那边
// 依然是候选。此时 UI 高亮 B、main 却路由到 A,会话就从一个用户看不见的来源发出去。所以
// 只在默认路由确实落回 effectiveSourceId 时才省略它,不一致时显式带上(PR #548 review)。
+ // 这里的「默认」还必须覆盖 main 的 spawn-aware 语义:Claude OAuth 会话收到 providerId=null
+ // 且存在 Gateway key 时会按 agent 默认走 XD,即使 XD 的动态目录并不提供当前模型。只比较
+ // effectiveSourceIdForModel 的模型级默认会把这种分叉误判成「可安全省略」(issue #1196)。
const localProviderIdForDraft = useMemo<string | null>(() => {
- if (chatPrefs.providerId && chatPrefs.providerId === effectiveSourceId) {
- return chatPrefs.providerId;
- }
- if (!effectiveSourceId) return null;
- // 用与 main 同源的解析函数 + 未过滤目录复算一次默认路由,比较的是同一口径。
- const defaultRouted = effectiveSourceIdForModel(
- localProviders,
- null,
- calibratedDraftModel,
- capabilityAgentKind,
- );
- return defaultRouted === effectiveSourceId ? null : effectiveSourceId;
+ return resolveDraftSessionProviderId({
+ providers: localProviders,
+ agent: capabilityAgentKind,
+ model: calibratedDraftModel,
+ explicitProviderId: chatPrefs.providerId,
+ effectiveProviderId: effectiveSourceId,
+ });
}, [
chatPrefs.providerId,
effectiveSourceId,
diff --git a/apps/desktop/src/renderer/lib/draftModelCalibration.ts b/apps/desktop/src/renderer/lib/draftModelCalibration.ts
index b6471bef90..bb2cea98e5 100644
--- a/apps/desktop/src/renderer/lib/draftModelCalibration.ts
+++ b/apps/desktop/src/renderer/lib/draftModelCalibration.ts
@@ -13,7 +13,9 @@
import {
connectedProvidersForAgent,
+ effectiveSourceIdForModel,
isModelSelectableForNewRoute,
+ nativeDefaultSourceId,
type AgentKind,
type CatalogModel,
type ProviderView,
@@ -166,3 +168,53 @@ export function calibrateDraftModel({
const picked = pickConnectedModelForAgent(providers, agent, model);
return picked ?? { model, providerId: null };
}
+
+export interface DraftSessionProviderResolutionInput {
+ /** main 进程用于默认路由的完整本机来源目录。 */
+ providers: readonly ProviderView[];
+ agent: AgentKind;
+ model: string;
+ /** 用户在草稿里显式选择的来源。 */
+ explicitProviderId: string | null | undefined;
+ /** renderer 已按当前模型与校准结果解析出的生效来源。 */
+ effectiveProviderId: string | null;
+}
+
+/**
+ * 决定新建会话是否必须把 renderer 的生效来源显式带给 main。
+ *
+ * 只有在省略 providerId 后 main 会解析到同一个来源时才允许返回 null;否则必须把来源
+ * 固化在会话上,避免 UI 所见与首条请求的实际路由分叉。
+ */
+export function resolveDraftSessionProviderId({
+ providers,
+ agent,
+ model,
+ explicitProviderId,
+ effectiveProviderId,
+}: DraftSessionProviderResolutionInput): string | null {
+ if (explicitProviderId && explicitProviderId === effectiveProviderId) {
+ return explicitProviderId;
+ }
+ if (!effectiveProviderId) return null;
+ const modelDefaultProviderId = effectiveSourceIdForModel(
+ [...providers],
+ null,
+ model,
+ agent,
+ );
+ // main 收到 providerId=null 后按 agent 的原生来源选择启动链路,而不是只在“提供当前
+ // 模型”的来源里重算。典型分叉:XD 与 Anthropic 都已连接,只有 Anthropic 目录含
+ // claude-opus-5。modelDefault 是 anthropic,但 agentDefault 仍是 xd;若只比较前者
+ // 就会错误地省略 providerId,main 随后拿 gateway key 启动 XD 路由(issue #1196)。
+ const agentDefaultProviderId = nativeDefaultSourceId(
+ // main 的 spawn 默认不读取用户的 suspended 开关;这里必须保留 suspended 来源,
+ // 否则 UI 停用 XD 后会把 agentDefault 错算成 Anthropic,再次把必需的来源省略掉。
+ connectedProvidersForAgent([...providers], agent, { includeSuspended: true }),
+ agent,
+ );
+ return modelDefaultProviderId === effectiveProviderId &&
+ agentDefaultProviderId === effectiveProviderId
+ ? null
+ : effectiveProviderId;
+}