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): 冷 Pi 存量路由死掉时切模降级继续,不再与发送互相锁死 (#5747)

Yunjin Zhangd5bb1af6fb3b
diff --git a/apps/desktop/src/main/__tests__/sessionRuntimeControlWiring.test.ts b/apps/desktop/src/main/__tests__/sessionRuntimeControlWiring.test.ts
index 336b3816e5..b51871cb6e 100644
--- a/apps/desktop/src/main/__tests__/sessionRuntimeControlWiring.test.ts
+++ b/apps/desktop/src/main/__tests__/sessionRuntimeControlWiring.test.ts
@@ -1041,21 +1041,28 @@ describe('session runtime control wiring', () => {
const catchBlock = setModel.slice(rehydrateCall, setModel.indexOf('rehydratedColdPiRuntime = liveSessionBeforeRouteChange;'));
expect(catchBlock).toContain('} catch (error) {');
expect(catchBlock).not.toContain('} catch {');
- // Both the thrown failure and the "not live after bootstrap" case go through the one
+ // Only the "not live after bootstrap" case still goes through the fail-closed
// reporter, which logs the full reason and throws the IPC error with a safe detail.
- expect(catchBlock.match(/reportColdPiRehydrationFailure\(/g)).toHaveLength(2);
+ // The rehydration catch itself degrades and continues (dead stored route must not
+ // dead-lock the session); the full reason stays in the main log.
+ expect(catchBlock.match(/reportColdPiRehydrationFailure\(/g)).toHaveLength(1);
expect(catchBlock).toContain("new ColdPiRehydrationError('runtime-not-live'");
expect(catchBlock).toContain('log,');
expect(catchBlock).toContain('throwIpcError,');
- // The error code and fail-closed outcome are unchanged: still no route change on failure.
- expect(catchBlock.match(/localModelWindowSwitchErrorCode\('MODEL_WINDOW_CURRENT_CONTEXT_UNKNOWN'\)/g)).toHaveLength(2);
+ expect(catchBlock).toContain("coldPiRehydrationOutcome === 'fail-closed'");
+ // The error code and fail-closed outcome remain only for the non-degrade branch.
+ expect(catchBlock.match(/localModelWindowSwitchErrorCode\('MODEL_WINDOW_CURRENT_CONTEXT_UNKNOWN'\)/g)).toHaveLength(1);
// Raw reasons (which may carry local paths or stderr) stay in the main log; the IPC
// message only carries the category and the name/code-based detail.
+ const fieldsStart = coldPiRehydrationFailureSource.indexOf('function logColdPiRehydrationFields(');
+ expect(fieldsStart).toBeGreaterThan(-1);
+ const fields = coldPiRehydrationFailureSource.slice(fieldsStart);
+ expect(fields).toContain('reason: failure.reason');
+ expect(fields).toContain('COLD_PI_REHYDRATION_DEGRADED_LOG_MESSAGE');
const reporterStart = coldPiRehydrationFailureSource.indexOf('export function reportColdPiRehydrationFailure(');
expect(reporterStart).toBeGreaterThan(-1);
const reporter = coldPiRehydrationFailureSource.slice(reporterStart);
- expect(reporter).toContain('reason: failure.reason');
expect(reporter).toContain('coldPiRehydrationFailureMessage(failure)');
const messageBuilder = handlerBody(
coldPiRehydrationFailureSource,
@@ -1066,6 +1073,31 @@ describe('session runtime control wiring', () => {
expect(messageBuilder).not.toContain('failure.reason');
});
+ it('keeps the model switch alive when cold Pi rehydration fails (dead stored route)', () => {
+ const setModel = handlerBody(
+ registerSource,
+ 'const handleSetModel = async (',
+ 'const recoverRemoteRuntimeAxisPersistence',
+ );
+ // 存量 BYOM 路由已死(provider 被删/不再提供该模型)时冷启动 bootstrap 必然
+ // fail-closed 失败;恢复失败若拒绝切模,发送与切模互相锁死(会话永久卡死)。
+ // catch 必须降级:只记日志,并与「跳过核实」同款语义置位,让目标路由由下一次
+ // 发送懒创建;闸门对未知当前窗口 fail-open 放行热切。
+ const rehydrateCall = setModel.indexOf('await rehydrateColdPiRuntimeForWindowVerification(sessionId)');
+ const postRehydrate = setModel.indexOf(
+ 'liveSessionBeforeRouteChange = maker.getSession(sessionId);',
+ rehydrateCall,
+ );
+ expect(rehydrateCall).toBeGreaterThan(-1);
+ expect(postRehydrate).toBeGreaterThan(rehydrateCall);
+ const catchOnly = setModel.slice(rehydrateCall, postRehydrate);
+ expect(catchOnly).toContain('} catch (error) {');
+ expect(catchOnly).toContain('logColdPiRehydrationFailure({ log }, coldPiFailureContext, error);');
+ expect(catchOnly).toContain('coldPiRouteWithoutLiveWindowCheck = true;');
+ expect(catchOnly).not.toContain('throwIpcError');
+ expect(catchOnly).not.toContain('reportColdPiRehydrationFailure');
+ });
+
it('skips the cold Pi window rehydration when the live usage leaves the target headroom', () => {
const setModel = handlerBody(
registerSource,
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/coldPiRehydration.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/coldPiRehydration.test.ts
index 01edb02fb8..74340d8794 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/coldPiRehydration.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/coldPiRehydration.test.ts
@@ -1,6 +1,7 @@
import { describe, expect, it, vi } from 'vitest';
import { createIpcError } from '../../../shared/ipc-errors';
import {
+ classifyColdPiRehydrationOutcome,
createColdPiRehydrationForWindowVerification,
type ColdPiRehydrationDeps,
} from '../coldPiRehydration';
@@ -186,3 +187,26 @@ describe('cold Pi rehydration for window verification (#5508)', () => {
expect(fields.detail).toBe('Error EACCES');
});
});
+
+describe('classifyColdPiRehydrationOutcome', () => {
+ it('降级继续:恢复失败且没有活会话(存量路由死掉,切模不能被拒绝)', () => {
+ expect(
+ classifyColdPiRehydrationOutcome({ rehydrationFailed: true, liveAfterBootstrap: false }),
+ ).toBe('degraded');
+ });
+
+ it('保留 fail-closed:bootstrap 声称成功却没有活会话,且没有降级过', () => {
+ expect(
+ classifyColdPiRehydrationOutcome({ rehydrationFailed: false, liveAfterBootstrap: false }),
+ ).toBe('fail-closed');
+ });
+
+ it('有活会话就核实:恢复成功(或竞态中别处已拉起)时拿 live 读数刷新窗口', () => {
+ expect(
+ classifyColdPiRehydrationOutcome({ rehydrationFailed: false, liveAfterBootstrap: true }),
+ ).toBe('verified');
+ expect(
+ classifyColdPiRehydrationOutcome({ rehydrationFailed: true, liveAfterBootstrap: true }),
+ ).toBe('verified');
+ });
+});
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/coldPiRehydrationFailure.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/coldPiRehydrationFailure.test.ts
index 84a2b0cb5a..00c4e84421 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/coldPiRehydrationFailure.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/coldPiRehydrationFailure.test.ts
@@ -1,12 +1,14 @@
import { describe, expect, it, vi } from 'vitest';
import { createIpcError } from '../../../shared/ipc-errors';
import {
+ COLD_PI_REHYDRATION_DEGRADED_LOG_MESSAGE,
COLD_PI_REHYDRATION_FAILURE_LOG_MESSAGE,
ColdPiRehydrationError,
coldPiRehydrationFailureMessage,
describeCauseForIpc,
describeColdPiRehydrationFailure,
redactLocalPaths,
+ logColdPiRehydrationFailure,
reportColdPiRehydrationFailure,
sanitizeColdPiRehydrationReason,
} from '../coldPiRehydrationFailure';
@@ -168,4 +170,38 @@ describe('cold Pi rehydration failure diagnostics (#5508)', () => {
'[PRECONDITION_FAILED] Pi current runtime could not be verified (runtime-not-live: rehydrated Pi runtime is not live after bootstrap); runtime selection was not changed',
);
});
+
+ it('logs the full reason without throwing when the switch degrades past a failed rehydration', () => {
+ const warn = vi.fn();
+ const context = {
+ sessionId: 's-3',
+ fromModel: 'glm-5.3-flash',
+ toModel: 'glm-5.3-flash',
+ currentProviderId: 'glm',
+ nextProviderId: 'glm-coding-plan',
+ };
+
+ expect(() =>
+ logColdPiRehydrationFailure(
+ { log: { warn } },
+ context,
+ new ColdPiRehydrationError(
+ 'bootstrap-failed',
+ `session s-3 bootstrap failed: dead provider at ${LOCAL_PATH}`,
+ { cause: new Error('BYOM provider absent') },
+ ),
+ ),
+ ).not.toThrow();
+
+ expect(warn).toHaveBeenCalledTimes(1);
+ const [message, fields] = warn.mock.calls[0] as [string, Record<string, unknown>];
+ expect(message).toBe(COLD_PI_REHYDRATION_DEGRADED_LOG_MESSAGE);
+ // 降级继续时不能复用“runtime selection unchanged”文案,否则会被读成切模被拒。
+ expect(message).not.toContain('runtime selection unchanged');
+ expect(fields).toMatchObject({
+ detail: 'Error',
+ });
+ // 完整原因(含本机路径)只进 Main 日志;没有 IPC 错误出口。
+ expect(fields.reason).toContain(LOCAL_PATH);
+ });
});
diff --git a/apps/desktop/src/main/maker-ipc/coldPiRehydration.ts b/apps/desktop/src/main/maker-ipc/coldPiRehydration.ts
index 582053e300..8973c1e8b8 100644
--- a/apps/desktop/src/main/maker-ipc/coldPiRehydration.ts
+++ b/apps/desktop/src/main/maker-ipc/coldPiRehydration.ts
@@ -39,6 +39,22 @@ export interface ColdPiRehydrationDeps<
bootstrapSession(createOpts: Opts): Promise<unknown>;
}
+/**
+ * 冷 Pi 恢复失败后的落点(分支表抽成可执行函数,供接线层与单测共用):
+ * - `verified`:恢复后有活会话,拿 live 读数刷新当前窗口;
+ * - `degraded`:恢复失败(最常见是存量 BYOM 路由已死,bootstrap 按 fail-closed
+ * 语义必然失败)——只记日志继续切,闸门对未知当前窗口 fail-open 放行热切,
+ * 目标路由由下一次发送懒创建。失败即拒绝切模会让会话永久卡死;
+ * - `fail-closed`:bootstrap 声称成功却没有活会话,且没降级过(原 #5508 出口)。
+ */
+export function classifyColdPiRehydrationOutcome(input: {
+ rehydrationFailed: boolean;
+ liveAfterBootstrap: boolean;
+}): 'verified' | 'degraded' | 'fail-closed' {
+ if (input.liveAfterBootstrap) return 'verified';
+ return input.rehydrationFailed ? 'degraded' : 'fail-closed';
+}
+
async function stage<T>(
category: ColdPiRehydrationFailureCategory,
describe: string,
diff --git a/apps/desktop/src/main/maker-ipc/coldPiRehydrationFailure.ts b/apps/desktop/src/main/maker-ipc/coldPiRehydrationFailure.ts
index 2a41afa451..4f0bca3bbe 100644
--- a/apps/desktop/src/main/maker-ipc/coldPiRehydrationFailure.ts
+++ b/apps/desktop/src/main/maker-ipc/coldPiRehydrationFailure.ts
@@ -133,20 +133,26 @@ export interface ColdPiRehydrationFailureReporterDeps {
errorCode: IpcErrorCode;
}
+/** 只记日志的调用方:恢复失败降级继续的路径(切模不死锁),不向渲染层抛错。 */
+export interface ColdPiRehydrationFailureLoggerDeps {
+ log: { warn(...args: unknown[]): void };
+}
+
export const COLD_PI_REHYDRATION_FAILURE_LOG_MESSAGE =
'set-model: cold Pi runtime rehydration failed; runtime selection unchanged';
-/**
- * 冷 Pi 恢复失败的唯一出口:完整原因写 Main 日志,IPC 错误只带类别与脱敏概述。
- * 错误码由调用方给出(设备链路下仍映射为 PRECONDITION_FAILED),fail-closed 不变。
- */
-export function reportColdPiRehydrationFailure(
- deps: ColdPiRehydrationFailureReporterDeps,
+// 降级继续的日志文案不能复用上面的“runtime selection unchanged”:切模没有失败,
+// 草率写 unchanged 会让排障者以为选择被拒,指向错误的方向。
+export const COLD_PI_REHYDRATION_DEGRADED_LOG_MESSAGE =
+ 'set-model: cold Pi window verification degraded; continuing with the selected route';
+
+function logColdPiRehydrationFields(
+ log: { warn(...args: unknown[]): void },
+ message: string,
context: ColdPiRehydrationFailureContext,
- error: unknown,
-): never {
- const failure = describeColdPiRehydrationFailure(error);
- deps.log.warn(COLD_PI_REHYDRATION_FAILURE_LOG_MESSAGE, {
+ failure: ColdPiRehydrationFailure,
+): void {
+ log.warn(message, {
sessionId: context.sessionId,
category: failure.category,
reason: failure.reason,
@@ -156,5 +162,37 @@ export function reportColdPiRehydrationFailure(
currentProviderId: context.currentProviderId,
nextProviderId: context.nextProviderId,
});
+}
+
+/**
+ * 冷 Pi 恢复失败但切模降级继续时的出口:完整原因写 Main 日志,不抛 IPC 错误。
+ * 核实是护栏不是闸门——存量路由死掉时 bootstrap 必然失败,若据此拒绝切模,
+ * 用户既发不出去也切不走(会话永久卡死);目标路由由下一次发送懒创建。
+ */
+export function logColdPiRehydrationFailure(
+ deps: ColdPiRehydrationFailureLoggerDeps,
+ context: ColdPiRehydrationFailureContext,
+ error: unknown,
+): void {
+ logColdPiRehydrationFields(
+ deps.log,
+ COLD_PI_REHYDRATION_DEGRADED_LOG_MESSAGE,
+ context,
+ describeColdPiRehydrationFailure(error),
+ );
+}
+
+/**
+ * 冷 Pi 恢复失败仍 fail-closed 的出口(runtime-not-live 等非降级分支):完整原因写
+ * Main 日志,IPC 错误只带类别与脱敏概述。错误码由调用方给出(设备链路下仍映射为
+ * PRECONDITION_FAILED)。
+ */
+export function reportColdPiRehydrationFailure(
+ deps: ColdPiRehydrationFailureReporterDeps,
+ context: ColdPiRehydrationFailureContext,
+ error: unknown,
+): never {
+ const failure = describeColdPiRehydrationFailure(error);
+ logColdPiRehydrationFields(deps.log, COLD_PI_REHYDRATION_FAILURE_LOG_MESSAGE, context, failure);
return deps.throwIpcError(deps.errorCode, coldPiRehydrationFailureMessage(failure));
}
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index fd9f46d392..52b8c9b749 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -474,8 +474,8 @@ import {
} from '../../shared/orca-worker-permission-mode.js';
import { t } from '../i18n.js';
import { createLogger } from '../logger.js';
-import { createColdPiRehydrationForWindowVerification } from './coldPiRehydration.js';
-import { ColdPiRehydrationError, reportColdPiRehydrationFailure } from './coldPiRehydrationFailure.js';
+import { createColdPiRehydrationForWindowVerification, classifyColdPiRehydrationOutcome } from './coldPiRehydration.js';
+import { ColdPiRehydrationError, logColdPiRehydrationFailure, reportColdPiRehydrationFailure } from './coldPiRehydrationFailure.js';
import {
desktopClaudeAuthAdapter,
desktopCodexAuthAdapter,
@@ -19513,21 +19513,28 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
currentProviderId,
nextProviderId: targetRouteProviderId,
};
+ let coldPiRehydrationFailed = false;
try {
await rehydrateColdPiRuntimeForWindowVerification(sessionId);
} catch (error) {
- reportColdPiRehydrationFailure(
- {
- log,
- throwIpcError,
- errorCode: localModelWindowSwitchErrorCode('MODEL_WINDOW_CURRENT_CONTEXT_UNKNOWN'),
- },
- coldPiFailureContext,
- error,
- );
+ // 恢复失败不再阻断切模(存量 BYOM 路由死锁修复,2026-10-10):核实是护栏
+ // 不是闸门。存量路由已死(provider 被删/不再提供该模型)时 bootstrap 按
+ // fail-closed 语义必然失败;若据此拒绝切模,用户既发不出去也切不走,
+ // 会话永久卡死。这里降级为「无当前窗口读数」继续:闸门矩阵对未知窗口
+ // fail-open 放行热切,目标路由由下一次发送懒创建;届时 bootstrap 失败
+ // 会带真实原因浮现。完整原因仍进 Main 日志(#5508)。
+ logColdPiRehydrationFailure({ log }, coldPiFailureContext, error);
+ coldPiRehydrationFailed = true;
+ coldPiRouteWithoutLiveWindowCheck = true;
}
liveSessionBeforeRouteChange = maker.getSession(sessionId);
- if (!liveSessionBeforeRouteChange) {
+ // 落点分支表见 classifyColdPiRehydrationOutcome:只有「bootstrap 声称成功
+ // 却没有活会话」这种非降级形状保留原 fail-closed 出口。
+ const coldPiRehydrationOutcome = classifyColdPiRehydrationOutcome({
+ rehydrationFailed: coldPiRehydrationFailed,
+ liveAfterBootstrap: liveSessionBeforeRouteChange !== undefined,
+ });
+ if (coldPiRehydrationOutcome === 'fail-closed') {
reportColdPiRehydrationFailure(
{
log,
@@ -19538,10 +19545,12 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
new ColdPiRehydrationError('runtime-not-live', 'rehydrated Pi runtime is not live after bootstrap'),
);
}
- rehydratedColdPiRuntime = liveSessionBeforeRouteChange;
- currentRuntimeModel = liveSessionBeforeRouteChange.model;
- runtimeRouteChanged =
- currentRuntimeModel !== model || currentProviderId !== targetRouteProviderId;
+ if (coldPiRehydrationOutcome === 'verified' && liveSessionBeforeRouteChange) {
+ rehydratedColdPiRuntime = liveSessionBeforeRouteChange;
+ currentRuntimeModel = liveSessionBeforeRouteChange.model;
+ runtimeRouteChanged =
+ currentRuntimeModel !== model || currentProviderId !== targetRouteProviderId;
+ }
}
}
}