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): normalize legacy profiles before detecting model saves

ylliuuu660e1448ca2ce4
diff --git a/apps/desktop/src/main/localDb/ipc/botProfileVersioning.ts b/apps/desktop/src/main/localDb/ipc/botProfileVersioning.ts
index d3aa2ba53f..347c9c5598 100644
--- a/apps/desktop/src/main/localDb/ipc/botProfileVersioning.ts
+++ b/apps/desktop/src/main/localDb/ipc/botProfileVersioning.ts
@@ -1,4 +1,3 @@
-import { normalizeBotModelChain, readBotTaskModelOverride } from '../../../shared/botModelChain.js';
import { normalizeBotToolCapabilities, reconcileBotCapabilityList } from '../../../shared/botCapabilitySelection.js';
import { throwIpcError } from '../../utils/ipcValidate.js';
@@ -14,7 +13,7 @@ export function botProfileContentChanged(input: {
);
}
-export { botProfileModelSelectionChanged } from '../../../shared/botModelChain.js';
+export { botProfileModelSelectionChanged, normalizeBotProfileModelChain } from '../../../shared/botModelChain.js';
export function mergeBotProfileCapabilities(input: {
previous: Record<string, unknown>;
@@ -58,38 +57,3 @@ export function mergeBotProfileCapabilities(input: {
}
return normalizeBotToolCapabilities(next);
}
-
-/** Main-owned persistence boundary for the ordered Bot runtime routes. */
-export function normalizeBotProfileModelChain(
- value: Record<string, unknown>,
-): Record<string, unknown> {
- const next = { ...value };
- if (Object.prototype.hasOwnProperty.call(next, 'taskModelOverride')) {
- next.taskModelOverride = readBotTaskModelOverride(next.taskModelOverride);
- }
- if (Object.prototype.hasOwnProperty.call(next, 'modelChain')) {
- const chain = normalizeBotModelChain(next.modelChain);
- if (chain.length === 0 && next.modelChainOverride !== null && next.modelOverride !== null) {
- throw new Error('modelChain must contain at least one valid route');
- }
- const primary = chain[0];
- next.modelChain = chain;
- if (primary) {
- next.harness = primary.harness;
- next.model = primary.model;
- next.providerId = primary.providerId;
- next.effort = primary.effort;
- next.fastMode = primary.fastMode;
- }
- }
- if (Array.isArray(next.modelChainOverride)) {
- const override = normalizeBotModelChain(next.modelChainOverride);
- if (override.length === 0) {
- throw new Error('modelChainOverride must contain at least one valid route');
- }
- next.modelChainOverride = override;
- } else if (next.modelChainOverride !== null && next.modelChainOverride !== undefined) {
- throw new Error('modelChainOverride must be an ordered route list or null');
- }
- return next;
-}
diff --git a/apps/desktop/src/main/localDb/worker/opHandlers/__tests__/botTx.test.ts b/apps/desktop/src/main/localDb/worker/opHandlers/__tests__/botTx.test.ts
index c1461be8dc..cb87bbed77 100644
--- a/apps/desktop/src/main/localDb/worker/opHandlers/__tests__/botTx.test.ts
+++ b/apps/desktop/src/main/localDb/worker/opHandlers/__tests__/botTx.test.ts
@@ -2,6 +2,7 @@ import Database from 'better-sqlite3';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { tx } from '../tx.js';
+import { normalizeBotProfileModelChain } from '../../../ipc/botProfileVersioning';
import { createBotModelRouteReconciler } from '../../../../maker-ipc/botModelRouteReconciler';
import { createScheduledModelScope } from '../../../../maker-ipc/scheduledModelSelection';
import { normalizeBotModelChain } from '../../../../../shared/botModelChain';
@@ -207,9 +208,11 @@ describe('Bot named worker transactions', () => {
expect(h.current().model).toBe('permanent');
});
- it.each(['save-before-begin', 'save-during-begin', 'save-after-begin'])('serializes the real scope writer and permanent save: %s', async order => {
+ it.each(['save-before-begin', 'save-during-begin', 'save-after-begin'].flatMap(order =>
+ [null, '{broken'].map(initialBaseline => ({ order, initialBaseline })),
+ ))('serializes the real scope writer and permanent save: $order / $initialBaseline', async ({ order, initialBaseline }) => {
const h = savedModelFixture();
- h.writeBaseline(null);
+ h.writeBaseline(initialBaseline);
const scope = createScheduledModelScope({ ...h.scopeDeps,
write: async (...args) => {
if (order === 'save-during-begin') h.save('permanent');
@@ -239,6 +242,20 @@ describe('Bot named worker transactions', () => {
expect(JSON.parse(h.readBaseline()!).pendingBotProfileVersion).toBeUndefined();
});
+ it('preserves runtime fallback when a non-model edit normalizes a legacy chain-only profile', async () => {
+ const h = savedModelFixture();
+ const fallback = { ...h.original, model: 'runtime-fallback', scopeId: 'automation' };
+ h.writeBaseline(JSON.stringify(fallback));
+ const previous = JSON.parse(db.prepare('SELECT capabilities_json FROM bot_profile_versions').pluck().get() as string);
+ expect(previous.model).toBeUndefined();
+ const next = normalizeBotProfileModelChain({ ...previous, skills: ['new-skill'] });
+ h.save('old', 1, { capabilitiesJson: JSON.stringify(next), identitySource: 'edited identity' });
+ expect(h.readBaseline()).toBe(JSON.stringify(fallback));
+ await h.reconcile()('saved-session');
+ await h.scope.restore('saved-session');
+ expect(h.current().model).toBe('runtime-fallback');
+ });
+
it('atomically rejects duplicate names and a second Cindy without touching the first profile', () => {
const args = { id: 'first', displayName: 'Renamed', description: '', avatar: '🤖', avatarColor: 'blue', identitySource: 'Custom identity', capabilitiesJson: '{"templateId":"cindy"}', now: 1 };
tx(db, { name: 'bots.createProfile', args });
diff --git a/apps/desktop/src/main/localDb/worker/opHandlers/tx.ts b/apps/desktop/src/main/localDb/worker/opHandlers/tx.ts
index ff808130b7..6553e0c3fd 100644
--- a/apps/desktop/src/main/localDb/worker/opHandlers/tx.ts
+++ b/apps/desktop/src/main/localDb/worker/opHandlers/tx.ts
@@ -472,7 +472,7 @@ function botsUpdateProfile(db: Database.Database, args: unknown): { currentVersi
// A save must also fence a begin that read a null baseline, or begins
// before the async profile refresh. This record is never restored:
// only acceptance of this permanent choice may consume it.
- const baseline = parseScheduledModelBaseline(target.baseline ?? JSON.stringify({
+ const baseline = parseScheduledModelBaseline(target.baseline) ?? parseScheduledModelBaseline(JSON.stringify({
scopeId: `${target.id}:profile:${nextVersion}`, agentKind: dbToMakerAgentKind(target.agentKind),
model: target.model, providerId: target.providerId, effort: target.effort,
fastMode: fastModeFromStorage(target.fastMode),
diff --git a/apps/desktop/src/shared/botModelChain.ts b/apps/desktop/src/shared/botModelChain.ts
index 218db833a6..98e2a79c39 100644
--- a/apps/desktop/src/shared/botModelChain.ts
+++ b/apps/desktop/src/shared/botModelChain.ts
@@ -117,6 +117,45 @@ export function nextBotModelRoute(
/** A saved model selection supersedes an earlier runtime-only fallback. */
export function botProfileModelSelectionChanged(previous: Record<string, unknown>, next: Record<string, unknown>): boolean {
+ // Legacy profiles may contain only modelChain. Compare the same canonical
+ // shape the settings save uses, so adding mirror fields is not a model edit.
+ previous = normalizeBotProfileModelChain(previous);
+ next = normalizeBotProfileModelChain(next);
return ['modelChainOverride', 'modelOverride', 'modelChain', 'harness', 'model', 'providerId', 'effort', 'fastMode']
.some((key) => JSON.stringify(previous[key] ?? null) !== JSON.stringify(next[key] ?? null));
}
+
+/** Main-owned persistence boundary for the ordered Bot runtime routes. */
+export function normalizeBotProfileModelChain(
+ value: Record<string, unknown>,
+): Record<string, unknown> {
+ const next = { ...value };
+ if (Object.prototype.hasOwnProperty.call(next, 'taskModelOverride')) {
+ next.taskModelOverride = readBotTaskModelOverride(next.taskModelOverride);
+ }
+ if (Object.prototype.hasOwnProperty.call(next, 'modelChain')) {
+ const chain = normalizeBotModelChain(next.modelChain);
+ if (chain.length === 0 && next.modelChainOverride !== null && next.modelOverride !== null) {
+ throw new Error('modelChain must contain at least one valid route');
+ }
+ const primary = chain[0];
+ next.modelChain = chain;
+ if (primary) {
+ next.harness = primary.harness;
+ next.model = primary.model;
+ next.providerId = primary.providerId;
+ next.effort = primary.effort;
+ next.fastMode = primary.fastMode;
+ }
+ }
+ if (Array.isArray(next.modelChainOverride)) {
+ const override = normalizeBotModelChain(next.modelChainOverride);
+ if (override.length === 0) {
+ throw new Error('modelChainOverride must contain at least one valid route');
+ }
+ next.modelChainOverride = override;
+ } else if (next.modelChainOverride !== null && next.modelChainOverride !== undefined) {
+ throw new Error('modelChainOverride must be an ordered route list or null');
+ }
+ return next;
+}