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(bots): retain recovery while same-engine selection is pending

ylliuuu66a77c0936e859
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/botModelRouteReconciler.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/botModelRouteReconciler.test.ts
index 71a0d583ba..fa83a4b4cb 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/botModelRouteReconciler.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/botModelRouteReconciler.test.ts
@@ -1,9 +1,10 @@
import { describe, expect, it, vi } from 'vitest';
import { withSendToSessionLock } from '../sendToSessionLock';
import { createScheduledModelScope } from '../scheduledModelSelection';
-import { createBotModelRouteReconciler } from '../botModelRouteReconciler';
+import { createBotModelRouteApplier, createBotModelRouteReconciler } from '../botModelRouteReconciler';
import type { BotModelRoute } from '../../../shared/botModelChain';
-import type { SessionRuntimeProfile } from '../sessionRuntimeControl';
+import { acceptSessionRuntimeMutation, cancelPendingSessionRuntimeMutation, clearSessionRuntimeControlState,
+ getSessionRuntimeControlSnapshot, type SessionRuntimeProfile } from '../sessionRuntimeControl';
function harness() {
let owner = 'owner-a';
@@ -465,3 +466,51 @@ it('rereads a newer durable save when acceptance loses its database compare-and-
expect(accepted).toHaveBeenCalledTimes(2);
expect(baseline).toBe('');
});
+
+// #7241 / #7177, unit tier: the real runtime control queues a same-Harness
+// selection outside the Harness-switch registry. Earlier tests only rejected
+// staging itself; they did not exercise successful staging that remains pending.
+it.each(['same-engine', 'cross-engine'] as const)('keeps accepted recovery until a deferred %s choice lands', async kind => {
+ const sessionId = `pending-bot-route-${kind}`;
+ const current: SessionRuntimeProfile = { agentKind: 'codex', model: 'old', providerId: 'xd', effort: 'medium', fastMode: false };
+ let baseline: string | null = JSON.stringify({ ...current, scopeId: 'legacy', pendingBotProfileVersion: 2 });
+ const original = baseline;
+ let deferred = true;
+ let harnessPending = false;
+ let effective = current;
+ const apply = createBotModelRouteApplier({
+ stage: async (_id, route) => {
+ const profile: SessionRuntimeProfile = { ...route, effort: 'medium' };
+ if (kind === 'same-engine') acceptSessionRuntimeMutation({ sessionId, source: 'agent', profile, deferred });
+ else harnessPending = deferred;
+ if (!deferred) effective = profile;
+ },
+ isIdle: () => !deferred,
+ applyPendingSwitch: async () => {},
+ hasPendingSwitch: () => harnessPending,
+ onApplied: async () => { baseline = null; },
+ });
+ const reconcile = createBotModelRouteReconciler({ ownerEpoch: () => 'owner',
+ read: async () => ({ chain: [{ harness: kind === 'same-engine' ? 'codex' : 'pi',
+ model: 'saved', providerId: 'xd', effort: 'medium', fastMode: false }],
+ current: effective, hasRuntimeOverride: true, scheduledModelBaseline: baseline }),
+ apply, onSelectionAccepted: async () => { baseline = null; },
+ });
+ try {
+ await expect(reconcile(sessionId)).rejects.toThrow('Saved companion route is still pending');
+ expect(baseline).toBe(original);
+ // A failed safe-boundary apply cancels its pending mutation, not the saved
+ // permanent choice. The next reconciliation must still be able to retry.
+ const pending = getSessionRuntimeControlSnapshot(sessionId).pending;
+ if (pending) cancelPendingSessionRuntimeMutation(sessionId, pending.generation);
+ harnessPending = false;
+ expect(effective.model).toBe('old');
+ expect(baseline).toBe(original);
+ deferred = false;
+ await reconcile(sessionId);
+ expect(effective.model).toBe('saved');
+ expect(baseline).toBeNull();
+ } finally {
+ clearSessionRuntimeControlState(sessionId);
+ }
+});
diff --git a/apps/desktop/src/main/maker-ipc/botModelRouteReconciler.ts b/apps/desktop/src/main/maker-ipc/botModelRouteReconciler.ts
index f4aefe0a31..1f95213aeb 100644
--- a/apps/desktop/src/main/maker-ipc/botModelRouteReconciler.ts
+++ b/apps/desktop/src/main/maker-ipc/botModelRouteReconciler.ts
@@ -1,4 +1,6 @@
import { parseScheduledModelBaseline } from '../../shared/scheduledModelBaseline';
+import { ScheduledModelSelectionBusyError } from './scheduledModelSelection';
+import { getSessionRuntimeControlSnapshot } from './sessionRuntimeControl';
import { throwIpcError } from '../utils/ipcValidate.js';
import type { AgentKind } from '@cindy/maker-core';
import { normalizeBotModelChain, type BotModelRoute } from '../../shared/botModelChain.js';
@@ -58,6 +60,26 @@ function sameRoute(a: RuntimeRoute, b: RuntimeRoute): boolean {
&& a.effort === b.effort && a.fastMode === b.fastMode && (a.agentDeviceId ?? null) === (b.agentDeviceId ?? null);
}
+/** Apply through both runtime switch paths before retiring recovery state. */
+export function createBotModelRouteApplier(deps: {
+ stage(sessionId: string, route: RuntimeRoute, current: RuntimeRoute): Promise<void>;
+ isIdle(sessionId: string): boolean;
+ applyPendingSwitch(sessionId: string): Promise<void>;
+ hasPendingSwitch(sessionId: string): boolean;
+ onApplied(sessionId: string): Promise<void>;
+}) {
+ return async (sessionId: string, route: RuntimeRoute, current: RuntimeRoute): Promise<void> => {
+ await deps.stage(sessionId, route, current);
+ if (deps.isIdle(sessionId)) await deps.applyPendingSwitch(sessionId);
+ // Same-Harness choices wait in runtime control, not the Harness registry.
+ // Staging alone must not retire the durable recovery marker.
+ if (deps.hasPendingSwitch(sessionId) || getSessionRuntimeControlSnapshot(sessionId).pending) {
+ throw new ScheduledModelSelectionBusyError('Saved companion route is still pending');
+ }
+ await deps.onApplied(sessionId);
+ };
+}
+
/** Apply the permanent profile through ordinary Session model/switch controls.
* Fallback and Agent choices remain effective until the configured chain changes.
* Background tasks keep their frozen route; the reader selects canonical tasks only.
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index 4b3046139b..5620f662d6 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -571,7 +571,7 @@ import {
schedules,
} from '../localDb/schema.js';
import { nextBotModelRoute, normalizeBotModelChain, type BotModelRoute } from '../../shared/botModelChain.js';
-import { createBotModelRouteReconciler } from './botModelRouteReconciler.js';
+import { createBotModelRouteApplier, createBotModelRouteReconciler } from './botModelRouteReconciler.js';
import {
cindyAutoRouteToFollow,
followMovedCindyAutoRoutes,
@@ -16636,22 +16636,15 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
: control.pending?.profile ?? control.effectiveOverride ?? undefined,
};
},
- apply: async (sessionId, route, current) => {
- await stageSessionRouteSelection(sessionId, { ...route, agentDeviceId: route.agentDeviceId ?? null }, current.agentKind);
- // Consume the same model/Harness intent as the normal send path. Its
- // verified window protection and history handoff also apply here; a Bot
- // settings save need not wait for a second user message. Busy runtimes
- // retain their intent for the existing safe-boundary coordinator.
- if (isSessionIdleForRouteApply(sessionId)) {
- await applyPendingAgentSwitchIfIdle(agentSwitchDeps, sessionId, { bootstrapAfterSwitch: true });
- }
- // Keep the durable acceptance marker if the switch failed or still waits
- // for a safe boundary. Staging alone must not lose recovery after restart.
- if (agentSwitchPending.get(sessionId)) {
- throw new ScheduledModelSelectionBusyError('Saved companion route is still pending');
- }
- await updateScheduledPreference(sessionId, null);
- },
+ apply: createBotModelRouteApplier({
+ stage: (sessionId, route, current) => stageSessionRouteSelection(
+ sessionId, { ...route, agentDeviceId: route.agentDeviceId ?? null }, current.agentKind),
+ isIdle: isSessionIdleForRouteApply,
+ applyPendingSwitch: (sessionId) => applyPendingAgentSwitchIfIdle(
+ agentSwitchDeps, sessionId, { bootstrapAfterSwitch: true }),
+ hasPendingSwitch: (sessionId) => !!agentSwitchPending.get(sessionId),
+ onApplied: async (sessionId) => { await updateScheduledPreference(sessionId, null); },
+ }),
});
configureBotModelSelectionCoordinator(createBotModelSelectionCoordinator({
diff --git a/docs/e2e-evidence/issue-7177-legacy-model-recovery/README.md b/docs/e2e-evidence/issue-7177-legacy-model-recovery/README.md
index a5fa2c5d08..a21e4fddc6 100644
--- a/docs/e2e-evidence/issue-7177-legacy-model-recovery/README.md
+++ b/docs/e2e-evidence/issue-7177-legacy-model-recovery/README.md
@@ -93,3 +93,21 @@ NODE_OPTIONS=--max-old-space-size=12288 node node_modules/typescript/bin/tsc --n
回归驱动 exit 0。记录提交仅增加证据,生产代码仍对应上述源码提交。
推送目的地为已配置个人 fork 的 `cindy/serene-euclid`;上游 origin 写入返回 403,未修改 main。
查询该源码 SHA 的 fork check runs 为 0;未将本地验收写成远端 CI 或正式包验收通过。
+
+## PR #7241:安全边界延期切换补充
+
+Greptile 指出同引擎选择会进入 runtime control 的 pending,而非 Harness 切换登记表。
+清理前只检查后者,会在同引擎选择尚未应用时丢失恢复记录。已将生产应用步骤提取到同一模块的
+`createBotModelRouteApplier`,同时检查两处 pending;register 复用该步骤,沿用原切换和清理入口。
+
+在既有 `botModelRouteReconciler.test.ts` 增加 unit tier 表驱动回归:调用生产应用步骤及真实
+runtime control,覆盖同引擎/跨引擎延期、取消失败的待执行选择后保留记录,以及再次应用成功后清理。
+已有测试仅模拟 staging 抛错,没有覆盖 staging 成功但尚未生效的情况。
+
+- 先红:保留原来的单登记表检查,定向用例 1 失败、1 通过(1.11 秒);同引擎分支错误地成功。
+- 后绿:`botModelRouteReconciler`、`sessionRuntimeControl`、`scheduledModelSelection` 三文件 141 项通过,exit 0(1.69 秒)。
+- 单独运行修改文件:40 项通过,exit 0(1.24 秒,测试体 272ms)。
+- 同一 Desktop 直接编译器类型检查命令:exit 0(180.16 秒)。
+
+上述是评审补修的自动化验证;前述真实模型状态副本结果仍对应明确记录的 `d6517ea6bd`,
+不将它冒称为该补修的新构建实测。