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(model-default): Auto 再次挪动后撤销或改写过期的换来源登记

fmfsaisai88eab80d064e
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/cindyAutoRouteFollow.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/cindyAutoRouteFollow.test.ts
index 690ee690f5..b8087a23c5 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/cindyAutoRouteFollow.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/cindyAutoRouteFollow.test.ts
@@ -6,6 +6,7 @@ import {
cindyAutoRouteToFollow,
followMovedCindyAutoRoutes,
movedCindyAutoProviderId,
+ stagedCindyAutoSwitch,
type CindyAutoRoute,
type CindyAutoRouteFollowDeps,
} from '../cindyAutoRouteFollow.js';
@@ -343,3 +344,122 @@ describe('号池 → 平台:目录刷新按旧路由重新加载过的进行
expect(cindyAutoRouteToFollow(runtimeState(true))).toBeNull();
});
});
+
+describe('已登记的 Auto 换来源,在 Auto 又挪动后撤销或改写', () => {
+ // 9333 上 a91bc630 的真实形态:任务库仍是号池(effort 空串),跟随登记过「下一条切到 Cindy AI」,
+ // 之后管理员又把 Auto 挪回号池。
+ const TASK = 'a91bc630-93d5-4685-9bef-7f3980b9e49e';
+ const REAL_AUTO = 'auto/d00e8b7f-fb6f-4045-922f-310da7b839fc';
+ const POOL = 'byok-f83a176f-8e24-494c-8e88-290584b05a76';
+ const OTHER_POOL = 'byok-0b4c9d2e-1f3a-4c5b-8d6e-7f8091a2b3c4';
+ const current: CindyAutoRoute = {
+ agentKind: 'pi',
+ model: REAL_AUTO,
+ providerId: POOL,
+ effort: '',
+ fastMode: false,
+ };
+ const stagedToPlatform = {
+ configStaged: true,
+ sameAgentSelection: true,
+ targetAgentKind: 'pi' as const,
+ model: REAL_AUTO,
+ providerId: 'xd',
+ fastMode: false,
+ };
+
+ function follow(providers: ProviderView[], intent: typeof stagedToPlatform | undefined) {
+ const stage = vi.fn(async () => {});
+ const cancelStagedSwitch = vi.fn(async () => {});
+ const readStagedSwitch = async () => stagedCindyAutoSwitch(intent, current);
+ return {
+ stage,
+ cancelStagedSwitch,
+ run: () =>
+ followMovedCindyAutoRoutes({
+ listCandidates: async () => [{ sessionId: TASK, ...current }],
+ listProviders: async () => providers,
+ withSessionLock: async (_sessionId, run) => run(),
+ // 已有登记时加锁重读一律不给路由(与真实 readRoute 一致)。
+ readRoute: async () => (intent ? null : current),
+ readStagedSwitch,
+ cancelStagedSwitch,
+ stage,
+ assertCurrent: () => {},
+ }),
+ };
+ }
+
+ it('Auto 挪回当前所在的号池:撤销「切到 Cindy AI」的登记,不再登记新切换', async () => {
+ const backInPool = [
+ provider('xd', 'builtin', ['auto', 'z-ai/glm-5.3-flash']),
+ provider(POOL, 'organization', [REAL_AUTO, `${POOL}/e2e-model`]),
+ ];
+ const { stage, cancelStagedSwitch, run } = follow(backInPool, stagedToPlatform);
+ await run();
+ expect(cancelStagedSwitch).toHaveBeenCalledWith(TASK);
+ expect(stage).not.toHaveBeenCalled();
+ });
+
+ it('登记的目标已没有这个 Auto、它去了另一个号池:改登记到那里', async () => {
+ const movedToOtherPool = [
+ provider('xd', 'builtin', ['auto']),
+ provider(POOL, 'organization', [`${POOL}/e2e-model`]),
+ provider(OTHER_POOL, 'organization', [REAL_AUTO]),
+ ];
+ const { stage, cancelStagedSwitch, run } = follow(movedToOtherPool, stagedToPlatform);
+ await run();
+ expect(stage).toHaveBeenCalledWith(TASK, { ...current, providerId: OTHER_POOL, effort: null });
+ expect(cancelStagedSwitch).not.toHaveBeenCalled();
+ });
+
+ it('哪里都没有这个 Auto 了:撤销登记,交给既有的来源失效处理', async () => {
+ const gone = [
+ provider('xd', 'builtin', ['auto']),
+ provider(POOL, 'organization', [`${POOL}/e2e-model`]),
+ ];
+ const { stage, cancelStagedSwitch, run } = follow(gone, stagedToPlatform);
+ await run();
+ expect(cancelStagedSwitch).toHaveBeenCalledWith(TASK);
+ expect(stage).not.toHaveBeenCalled();
+ });
+
+ it('登记的目标仍有这个 Auto:保持不动', async () => {
+ const stillOnPlatform = [
+ provider('xd', 'builtin', ['auto', REAL_AUTO]),
+ provider(POOL, 'organization', [`${POOL}/e2e-model`]),
+ ];
+ const { stage, cancelStagedSwitch, run } = follow(stillOnPlatform, stagedToPlatform);
+ await run();
+ expect(cancelStagedSwitch).not.toHaveBeenCalled();
+ expect(stage).not.toHaveBeenCalled();
+ });
+
+ it('负向:只认系统登记、同引擎、同一个 Auto、只换来源的登记', () => {
+ expect(stagedCindyAutoSwitch(stagedToPlatform, current)).toEqual({
+ current,
+ targetProviderId: 'xd',
+ });
+ // 用户自己点选的不动。
+ expect(
+ stagedCindyAutoSwitch({ ...stagedToPlatform, configStaged: undefined }, current),
+ ).toBeNull();
+ expect(
+ stagedCindyAutoSwitch({ ...stagedToPlatform, sameAgentSelection: undefined }, current),
+ ).toBeNull();
+ expect(
+ stagedCindyAutoSwitch({ ...stagedToPlatform, targetAgentKind: 'claude-code' }, current),
+ ).toBeNull();
+ expect(
+ stagedCindyAutoSwitch({ ...stagedToPlatform, model: 'auto/other-group' }, current),
+ ).toBeNull();
+ expect(
+ stagedCindyAutoSwitch({ ...stagedToPlatform, targetAgentDeviceId: 'device-2' }, current),
+ ).toBeNull();
+ const plain = { ...current, model: 'z-ai/glm-5.3-flash' };
+ expect(
+ stagedCindyAutoSwitch({ ...stagedToPlatform, model: 'z-ai/glm-5.3-flash' }, plain),
+ ).toBeNull();
+ expect(stagedCindyAutoSwitch(undefined, current)).toBeNull();
+ });
+});
diff --git a/apps/desktop/src/main/maker-ipc/cindyAutoRouteFollow.ts b/apps/desktop/src/main/maker-ipc/cindyAutoRouteFollow.ts
index 206bc31061..8a88673e1f 100644
--- a/apps/desktop/src/main/maker-ipc/cindyAutoRouteFollow.ts
+++ b/apps/desktop/src/main/maker-ipc/cindyAutoRouteFollow.ts
@@ -1,6 +1,13 @@
-import { movedCindyAutoSourceId, type AgentKind, type ProviderView } from '@cindy/model-providers';
+import {
+ effectiveSourceIdForModel,
+ isCindyAutoModelId,
+ movedCindyAutoSourceId,
+ type AgentKind,
+ type ProviderView,
+} from '@cindy/model-providers';
import { isSupportedRuntimeEffort } from './runtimeSelectionAxes.js';
+import type { PendingAgentSwitchIntent } from './sessionAgentSwitchHandler.js';
import type { SessionRuntimeControlSnapshot } from './sessionRuntimeControl.js';
/**
@@ -50,6 +57,30 @@ export function cindyAutoRouteToFollow(state: CindyAutoRuntimeState): CindyAutoR
return state.effective;
}
+/** 系统按配置登记、下一条消息生效的 Auto 换来源(同引擎、同一个 Auto,只换来源)。 */
+export interface StagedCindyAutoSwitch {
+ /** 任务当前实际路由(登记还没生效)。 */
+ current: CindyAutoRoute;
+ targetProviderId: string | null;
+}
+
+/** 从切换意图里认出系统登记的 Auto 换来源;用户自己点选、换引擎、换模型或换电脑的都不算。 */
+export function stagedCindyAutoSwitch(
+ intent: PendingAgentSwitchIntent | undefined,
+ current: CindyAutoRoute,
+): StagedCindyAutoSwitch | null {
+ if (!intent?.configStaged || !intent.sameAgentSelection) return null;
+ if (intent.targetAgentDeviceId !== undefined) return null;
+ if (intent.targetAgentKind !== current.agentKind || intent.model !== current.model) return null;
+ if (!isCindyAutoModelId(intent.model)) return null;
+ return { current, targetProviderId: intent.providerId ?? null };
+}
+
+/** Pi 的 providerId=null 就是 Cindy AI;其它引擎的 null 不推断。 */
+function cindySourceId(agentKind: AgentKind, providerId: string | null): string | null {
+ return providerId ?? (agentKind === 'pi' ? 'xd' : null);
+}
+
export interface CindyAutoRouteFollowDeps {
/**
* 本地数据库是否已就绪。启动早期目录先到、数据库还没接管时直接跳过(不算失败);
@@ -62,6 +93,10 @@ export interface CindyAutoRouteFollowDeps {
withSessionLock(sessionId: string, run: () => Promise<void>): Promise<void>;
/** 加锁后重读任务当前实际路由;已有登记的切换或运行时改道时返回 null,不覆盖。 */
readRoute(sessionId: string): Promise<CindyAutoRoute | null>;
+ /** 系统已登记的 Auto 换来源;没有时返回 null。加锁后会重读。 */
+ readStagedSwitch?(sessionId: string): Promise<StagedCindyAutoSwitch | null>;
+ /** 撤销上面那条登记(调用方已持有该任务的 send 锁)。 */
+ cancelStagedSwitch?(sessionId: string): Promise<void>;
/** 登记下一条消息前的切换(调用方已持有该任务的 send 锁)。 */
stage(sessionId: string, route: CindyAutoRoute): Promise<void>;
assertCurrent(): void;
@@ -77,8 +112,22 @@ export async function followMovedCindyAutoRoutes(deps: CindyAutoRouteFollowDeps)
deps.assertCurrent();
let staged = 0;
for (const candidate of candidates) {
- if (!movedCindyAutoProviderId(providers, candidate)) continue;
try {
+ // 已登记过换来源的任务,按 Auto 现在的位置重新核对那条登记(任务库里的路由还是旧的)。
+ if (deps.readStagedSwitch && (await deps.readStagedSwitch(candidate.sessionId))) {
+ await deps.withSessionLock(candidate.sessionId, async () => {
+ deps.assertCurrent();
+ const switchToCheck = await deps.readStagedSwitch?.(candidate.sessionId);
+ if (
+ switchToCheck &&
+ (await recheckStagedSwitch(deps, providers, candidate.sessionId, switchToCheck))
+ ) {
+ staged += 1;
+ }
+ });
+ continue;
+ }
+ if (!movedCindyAutoProviderId(providers, candidate)) continue;
await deps.withSessionLock(candidate.sessionId, async () => {
deps.assertCurrent();
const route = await deps.readRoute(candidate.sessionId);
@@ -105,3 +154,41 @@ export async function followMovedCindyAutoRoutes(deps: CindyAutoRouteFollowDeps)
}
return staged;
}
+
+/**
+ * 以任务当前路由为准,看 Auto 现在该在哪:目标仍对就不动;当前位置又能用、或哪里都没有了就撤销;
+ * 去了别处就改登记到那里。返回是否改登记。调用方已持有该任务的 send 锁。
+ */
+async function recheckStagedSwitch(
+ deps: CindyAutoRouteFollowDeps,
+ providers: readonly ProviderView[],
+ sessionId: string,
+ { current, targetProviderId }: StagedCindyAutoSwitch,
+): Promise<boolean> {
+ const from = cindySourceId(current.agentKind, current.providerId);
+ // 其它引擎的默认来源不一定是 Cindy AI,判断不了就不动。
+ if (!from) return false;
+ const target = cindySourceId(current.agentKind, targetProviderId);
+ const now = effectiveSourceIdForModel([...providers], from, current.model, current.agentKind);
+ if (now === target) return false;
+ if (!now || now === from) {
+ await deps.cancelStagedSwitch?.(sessionId);
+ deps.logger?.info('Cindy Auto staged switch withdrawn', {
+ sessionId,
+ model: current.model,
+ from,
+ to: target,
+ });
+ return false;
+ }
+ const effort = isSupportedRuntimeEffort(current.effort) ? current.effort : null;
+ await deps.stage(sessionId, { ...current, providerId: now, effort });
+ deps.logger?.info('Cindy Auto staged switch retargeted', {
+ sessionId,
+ model: current.model,
+ from,
+ to: now,
+ previousTarget: target,
+ });
+ return true;
+}
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index 184675d064..b8fa811cfc 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -512,7 +512,11 @@ import {
} from '../localDb/schema.js';
import { nextBotModelRoute, normalizeBotModelChain, type BotModelRoute } from '../../shared/botModelChain.js';
import { createBotModelRouteReconciler } from './botModelRouteReconciler.js';
-import { cindyAutoRouteToFollow, followMovedCindyAutoRoutes } from './cindyAutoRouteFollow.js';
+import {
+ cindyAutoRouteToFollow,
+ followMovedCindyAutoRoutes,
+ stagedCindyAutoSwitch,
+} from './cindyAutoRouteFollow.js';
import { readEffectiveBotModelChain, readEffectiveBotModelSelection } from '../maker-host/bot-model-chain-settings-store.js';
import {
isOrcaWorkerPermissionMode,
@@ -15073,6 +15077,13 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
!!profiles.pendingMutation,
});
},
+ readStagedSwitch: async (sessionId) => {
+ const intent = agentSwitchPending.get(sessionId);
+ if (!intent?.configStaged) return null;
+ const profiles = await readSessionRuntimeProfiles(sessionId);
+ return profiles ? stagedCindyAutoSwitch(intent, profiles.effective) : null;
+ },
+ cancelStagedSwitch: async (sessionId) => cancelPendingAgentSwitchHolder?.(sessionId),
stage: (sessionId, route) => stageSessionRouteSelection(sessionId, route, route.agentKind),
assertCurrent: () => {
if (getActiveAppSession().generation !== owner.generation) {