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 automation baseline during ordinary bot reconciliation

ylliuuu66f11a9b48257f
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 9201182d27..e415a2d2c1 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/botModelRouteReconciler.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/botModelRouteReconciler.test.ts
@@ -7,7 +7,7 @@ import type { SessionRuntimeProfile } from '../sessionRuntimeControl';
function harness() {
let owner = 'owner-a';
- const state: { chain: BotModelRoute[]; current: SessionRuntimeProfile; hasRuntimeOverride: boolean; next?: SessionRuntimeProfile } = {
+ const state: { chain: BotModelRoute[]; current: SessionRuntimeProfile; hasRuntimeOverride: boolean; followsCindyDefault?: boolean; next?: SessionRuntimeProfile } = {
chain: [{ harness: 'codex' as const, model: 'luna', providerId: 'xd', effort: 'medium', fastMode: false }],
current: { agentKind: 'pi' as const, model: 'glm', providerId: 'xd', effort: 'high', fastMode: false },
hasRuntimeOverride: false,
@@ -181,6 +181,7 @@ it('finishes at the newest settings selection when another save arrives during s
await Promise.all([first, second]);
expect(h.apply).toHaveBeenCalledTimes(2);
expect(h.apply).toHaveBeenLastCalledWith('canonical', expect.objectContaining({ model: 'newest-model' }), h.state.current);
+ expect(h.onSelectionAccepted).toHaveBeenCalledOnce();
});
it('selecting the current route cancels a different pending route instead of leaving it queued', async () => {
const h = harness();
@@ -204,6 +205,7 @@ it('applies the latest saved selection even when the superseded switch fails', a
await Promise.all([first, second]);
expect(h.apply).toHaveBeenCalledTimes(2);
expect(h.apply).toHaveBeenLastCalledWith('canonical', expect.objectContaining({ model: 'working-model' }), h.state.current);
+ expect(h.onSelectionAccepted).toHaveBeenCalledOnce();
});
it('does not retry a superseded failed switch after the account owner changes', async () => {
@@ -219,6 +221,7 @@ it('does not retry a superseded failed switch after the account owner changes',
fail(new Error('Old provider unavailable'));
expect((await results).map(result => result.status)).toEqual(['rejected', 'rejected']);
expect(h.apply).toHaveBeenCalledOnce();
+ expect(h.onSelectionAccepted).not.toHaveBeenCalled();
});
// Regression: changing only the account location must still move the runtime.
@@ -235,34 +238,117 @@ it('reconciles shared-to-local and local-to-shared selections of the same model'
expect(h.apply).toHaveBeenLastCalledWith('canonical', local, h.state.current);
});
-// #6759: an explicit save can accept the automation's temporary model without a switch.
-it.each([true, false])('invalidates the automation baseline after an explicit save (already current: %s)', async alreadyCurrent => {
+// #6759: exercise the production reconciler before scope.restore, as IM sends do.
+// A restart can lose the override marker while retaining a fallback baseline.
+// Existing save tests did not cover ordinary reconciliation that applies a route.
+function automationHarness() {
const h = harness();
- const permanent = { agentKind: 'codex' as const, model: 'luna', providerId: 'xd', effort: 'medium' as const, fastMode: false };
- h.state.current = alreadyCurrent ? permanent : { ...permanent, model: 'temporary-model' };
- h.apply.mockImplementation(async () => { h.state.current = permanent; });
- let baseline: string | null = JSON.stringify({ ...permanent, model: 'old-model', scopeId: 'automation' });
+ const original = { ...h.state.current, model: 'runtime-fallback' };
+ let baseline: string | null = JSON.stringify({ ...original, scopeId: 'automation' });
+ h.apply.mockImplementation(async (_id, route) => {
+ h.state.current = { ...route, effort: route.effort as SessionRuntimeProfile['effort'] };
+ });
+ h.onSelectionAccepted.mockImplementation(async () => { baseline = null; });
const scope = createScheduledModelScope({
read: async () => ({ profile: h.state.current, baseline }),
write: async (_id, value) => { baseline = value ? JSON.stringify(value) : null; },
- apply: async (_id, profile) => { h.state.current = profile; },
+ apply: async (_id, profile) => {
+ const { agentKind, model, providerId, effort, fastMode } = profile;
+ h.state.current = { agentKind, model, providerId, effort, fastMode };
+ },
isBusy: () => false, createId: () => 'unused',
});
- h.onSelectionAccepted.mockImplementation(async () => { baseline = null; });
+ return { ...h, original, scope, baseline: () => baseline };
+}
+
+it.each(['restart', 'changed-chain', 'pending-route', 'follow-default'] as const)(
+ 'restores the saved fallback after ordinary IM preparation: %s', async scenario => {
+ const h = automationHarness();
+ const primary = { agentKind: 'codex' as const, model: 'luna', providerId: 'xd', effort: 'medium' as const, fastMode: false };
+ if (scenario === 'changed-chain') {
+ h.state.current = primary;
+ await h.reconcile('canonical');
+ h.state.chain[0].model = 'changed-default';
+ }
+ h.state.current = { ...primary, model: 'automation-model' };
+ if (scenario === 'pending-route') {
+ h.state.current = primary;
+ h.state.next = { ...primary, model: 'pending-automation' };
+ }
+ if (scenario === 'follow-default') {
+ h.state.hasRuntimeOverride = true;
+ h.state.followsCindyDefault = true;
+ }
+ const baseline = h.baseline();
+ await h.reconcile('canonical');
+ expect(h.apply).toHaveBeenCalledOnce();
+ expect(h.onSelectionAccepted).not.toHaveBeenCalled();
+ expect(h.baseline()).toBe(baseline);
+ await h.scope.restore('canonical', 'automation');
+ expect(h.state.current).toEqual(h.original);
+ expect(h.baseline()).toBeNull();
+ },
+);
+
+it.each([true, false])('invalidates the automation baseline after an explicit save (already current: %s)', async alreadyCurrent => {
+ const h = automationHarness();
+ const permanent = { agentKind: 'codex' as const, model: 'luna', providerId: 'xd', effort: 'medium' as const, fastMode: false };
+ h.state.current = alreadyCurrent ? permanent : { ...permanent, model: 'temporary-model' };
if (alreadyCurrent) {
await h.reconcile('canonical');
- expect(baseline).not.toBeNull(); // Ordinary reconciliation must not cancel an automation scope.
+ expect(h.baseline()).not.toBeNull();
}
await h.reconcile.profileChanged('canonical');
- await scope.restore('canonical', 'automation');
+ await h.scope.restore('canonical', 'automation');
expect(h.state.current).toEqual(permanent);
- expect(baseline).toBeNull();
+ expect(h.baseline()).toBeNull();
+ expect(h.onSelectionAccepted).toHaveBeenCalledOnce();
expect(h.apply).toHaveBeenCalledTimes(alreadyCurrent ? 0 : 1);
});
-it('does not accept a permanent selection whose staging failed', async () => {
- const h = harness();
+it('retains the baseline and retries an explicit save whose staging failed', async () => {
+ const h = automationHarness();
+ const baseline = h.baseline();
h.apply.mockRejectedValueOnce(new Error('provider unavailable'));
await expect(h.reconcile.profileChanged('canonical')).rejects.toThrow('provider unavailable');
expect(h.onSelectionAccepted).not.toHaveBeenCalled();
+ expect(h.baseline()).toBe(baseline);
+ await h.reconcile('canonical');
+ expect(h.onSelectionAccepted).toHaveBeenCalledOnce();
+ await h.scope.restore('canonical', 'automation');
+ expect(h.state.current.model).toBe('luna');
+});
+
+it('retries an accepted save whose baseline cleanup failed, even when the route already matches', async () => {
+ const h = automationHarness();
+ const baseline = h.baseline();
+ h.onSelectionAccepted.mockRejectedValueOnce(new Error('storage unavailable'));
+ await expect(h.reconcile.profileChanged('canonical')).rejects.toThrow('storage unavailable');
+ expect(h.baseline()).toBe(baseline);
+ await h.reconcile('canonical');
+ expect(h.apply).toHaveBeenCalledOnce();
+ await h.scope.restore('canonical', 'automation');
+ expect(h.state.current.model).toBe('luna');
+ expect(h.baseline()).toBeNull();
+});
+
+it('keeps the baseline when a newer explicit save fails after an ordinary reconcile was in flight', async () => {
+ const h = automationHarness();
+ const baseline = h.baseline();
+ let finish!: () => void;
+ h.apply.mockImplementationOnce(() => new Promise<void>(resolve => { finish = resolve; }));
+ const ordinary = h.reconcile('canonical');
+ await vi.waitFor(() => expect(h.apply).toHaveBeenCalledOnce());
+ h.state.chain[0].model = 'new-permanent';
+ h.apply.mockRejectedValueOnce(new Error('new provider unavailable'));
+ const explicit = h.reconcile.profileChanged('canonical');
+ const results = Promise.allSettled([ordinary, explicit]);
+ finish();
+ expect((await results).map(result => result.status)).toEqual(['rejected', 'rejected']);
+ expect(h.onSelectionAccepted).not.toHaveBeenCalled();
+ expect(h.baseline()).toBe(baseline);
+ await h.reconcile('canonical');
+ expect(h.onSelectionAccepted).toHaveBeenCalledOnce();
+ await h.scope.restore('canonical', 'automation');
+ expect(h.state.current.model).toBe('new-permanent');
});
diff --git a/apps/desktop/src/main/maker-ipc/botModelRouteReconciler.ts b/apps/desktop/src/main/maker-ipc/botModelRouteReconciler.ts
index 6aef57f08e..70006a8ffc 100644
--- a/apps/desktop/src/main/maker-ipc/botModelRouteReconciler.ts
+++ b/apps/desktop/src/main/maker-ipc/botModelRouteReconciler.ts
@@ -101,9 +101,9 @@ export function createBotModelRouteReconciler(deps: {
if (needsApply) await deps.apply(sessionId, route, current);
if (deps.ownerEpoch() !== epoch) throw new Error('Bot model route owner changed');
if ((revisions.get(sessionId) ?? 0) !== revision) continue;
- // profileChanged marks previous as ''; ordinary matching reconciliation
- // must leave an in-flight automation scope intact.
- if (needsApply || (previous !== undefined && previous !== key)) {
+ // Only profileChanged's explicit-save marker supersedes the saved scope.
+ // Ordinary IM preparation can switch routes before automation restore.
+ if (previous === '') {
await deps.onSelectionAccepted?.(sessionId);
}
} catch (error) {
diff --git a/docs/product-rules/model-selector-unified.md b/docs/product-rules/model-selector-unified.md
index 7b2829d84b..4838cffc65 100644
--- a/docs/product-rules/model-selector-unified.md
+++ b/docs/product-rules/model-selector-unified.md
@@ -207,7 +207,7 @@
绑定已有任务的自动化在保存时只更新自动化配置,下次触发时才通过现有任务切换流程应用完整选择;目标正忙时顺延,不能抢占当前轮次。新增的 `modelAgentKind` 标识显式 Harness 选择;旧记录不补默认值,继续跟随绑定任务的 Harness。选择“跟随”会清除整套显式覆盖,切换新建/绑定模式往返时保留用户已选配置。
-自动化应用模型以及恢复之前的模型偏好时,都按活动任务当前的 Agent 执行位置校验来源与模型,并遵守现有新路由准入(含停用、退役、付费与对话能力限制);同账号远程电脑和分享来源使用对应电脑的目录,不能拿本机目录判定不可用,也不能静默换成本机来源。重启后遗留的恢复记录沿用同一规则,无需补字段。绑定任务已归档、删除或缺失时,替代任务按 runner 的本机新建位置解析,不沿用旧执行电脑。伙伴永久模型选择成功处理后(包括选择已与当前临时模型一致、无需切换的情况),清除被新选择取代的旧自动化恢复记录,避免下一条普通消息恢复旧来源;登记失败仍保留原记录。
+自动化应用模型以及恢复之前的模型偏好时,都按活动任务当前的 Agent 执行位置校验来源与模型,并遵守现有新路由准入(含停用、退役、付费与对话能力限制);同账号远程电脑和分享来源使用对应电脑的目录,不能拿本机目录判定不可用,也不能静默换成本机来源。重启后遗留的恢复记录沿用同一规则,无需补字段。绑定任务已归档、删除或缺失时,替代任务按 runner 的本机新建位置解析,不沿用旧执行电脑。伙伴永久模型选择成功处理后(包括选择已与当前临时模型一致、无需切换的情况),清除被新选择取代的旧自动化恢复记录,避免下一条普通消息恢复旧来源;登记失败仍保留原记录。普通伙伴协调即使需要切换路由,也不清除自动化恢复记录;只有明确的永久保存成功接受后才使旧记录失效。
项目自动化的 `schedules.json` 同样保存可选的 `modelAgentKind`,导出、加载与配置同步均保留该标记;删除标记时清除数据库中的覆盖,已有绑定任务不丢失。旧版 `version: 1` 文件无需补字段。自动触发前按实际来源、Harness 和模型的当前目录拷贝补齐或收敛 effort,并清除已不受支持的 Fast;仍有效的用户选择保持不变。