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): 固定 effort 模型切换不再把 null 写入 sessions.effort (#3703)

Chris Zhang0721f493e8b4
diff --git a/apps/desktop/src/main/__tests__/sessionRuntimeControlWiring.test.ts b/apps/desktop/src/main/__tests__/sessionRuntimeControlWiring.test.ts
index 5fb006df61..36921b43c4 100644
--- a/apps/desktop/src/main/__tests__/sessionRuntimeControlWiring.test.ts
+++ b/apps/desktop/src/main/__tests__/sessionRuntimeControlWiring.test.ts
@@ -409,6 +409,25 @@ describe('session runtime control wiring', () => {
expect(targetRoute).not.toContain('requiresCodexThreadRelink && atomicSelection');
});
+ it('omits null runtime effort from the Codex relink SQLite commit', () => {
+ const setModel = handlerBody(
+ registerSource,
+ 'const handleSetModel = async (',
+ 'const recoverRemoteRuntimeAxisPersistence',
+ );
+ const relinkCommit = setModel.slice(
+ setModel.indexOf('commit: async ({ sessionId: targetSessionId, source, newSdkSessionId, target })'),
+ setModel.indexOf('if (write.changes === 0) return false;'),
+ );
+ expect(relinkCommit).toContain('persistableSessionEffort(target.effort)');
+ expect(relinkCommit).toContain(
+ '...(persistableEffort !== undefined ? { effort: persistableEffort } : {})',
+ );
+ expect(relinkCommit).not.toContain(
+ 'effort: target.effort as (typeof sessions.$inferInsert)[\'effort\']',
+ );
+ });
+
it('derives the Codex relink boundary from effective credential identities', () => {
const setModel = handlerBody(
registerSource,
diff --git a/apps/desktop/src/main/localDb/__tests__/sessionPatchToRow.effort.test.ts b/apps/desktop/src/main/localDb/__tests__/sessionPatchToRow.effort.test.ts
new file mode 100644
index 0000000000..13d74b6247
--- /dev/null
+++ b/apps/desktop/src/main/localDb/__tests__/sessionPatchToRow.effort.test.ts
@@ -0,0 +1,25 @@
+import { describe, expect, it } from 'vitest';
+
+import { persistableSessionEffort, sessionPatchToRow } from '../mapper';
+
+describe('persistableSessionEffort', () => {
+ it('keeps legal enum values and drops runtime placeholders', () => {
+ expect(persistableSessionEffort('high')).toBe('high');
+ expect(persistableSessionEffort(' xhigh ')).toBe('xhigh');
+ expect(persistableSessionEffort(null)).toBeUndefined();
+ expect(persistableSessionEffort(undefined)).toBeUndefined();
+ expect(persistableSessionEffort('')).toBeUndefined();
+ expect(persistableSessionEffort(' ')).toBeUndefined();
+ });
+});
+
+describe('sessionPatchToRow effort persistence', () => {
+ it('writes a legal effort and omits null or blank placeholders', () => {
+ expect(sessionPatchToRow({ model: 'gpt-5.6', effort: 'low' })).toEqual(
+ expect.objectContaining({ model: 'gpt-5.6', effort: 'low' }),
+ );
+ expect(sessionPatchToRow({ model: 'grok-4.6', effort: null }).effort).toBeUndefined();
+ expect(sessionPatchToRow({ model: 'grok-4.6', effort: '' }).effort).toBeUndefined();
+ expect(sessionPatchToRow({ model: 'grok-4.6' }).effort).toBeUndefined();
+ });
+});
diff --git a/apps/desktop/src/main/localDb/ipc/__tests__/sessionsUpdate.test.ts b/apps/desktop/src/main/localDb/ipc/__tests__/sessionsUpdate.test.ts
index 5e104ddd54..30ce72419b 100644
--- a/apps/desktop/src/main/localDb/ipc/__tests__/sessionsUpdate.test.ts
+++ b/apps/desktop/src/main/localDb/ipc/__tests__/sessionsUpdate.test.ts
@@ -124,6 +124,7 @@ vi.mock('../../../cindy-brain/index.js', () => ({
import {
patchSessionMetaInDb,
+ persistSessionFields,
registerSessionIpc,
resumeDeletedPiSubagentCleanup,
setSessionRuntimeCleanup,
@@ -493,6 +494,23 @@ describe('local-db:sessions:update handler wiring', () => {
expect(h.routeLock).toHaveBeenCalledWith('codex-local', expect.any(Function));
});
+ it('keeps the last legal sessions.effort when a fixed-effort model switch patches null', async () => {
+ await persistSessionFields('pi-local', {
+ model: 'x-ai-grok/grok-4.6',
+ effort: null,
+ fastMode: false,
+ });
+
+ const persisted = h
+ .sqlite!.prepare('SELECT model, effort, fast_mode FROM sessions WHERE id = ?')
+ .get('pi-local') as { model: string; effort: string; fast_mode: number };
+ expect(persisted).toEqual({
+ model: 'x-ai-grok/grok-4.6',
+ effort: 'high',
+ fast_mode: 0,
+ });
+ });
+
it('rejects setting drift for retained Review tasks while preserving metadata edits', async () => {
await expect(invokeUpdate('review-local', { effort: 'low' })).rejects.toThrow(
/Review task settings are fixed/,
diff --git a/apps/desktop/src/main/localDb/ipc/sessions.ts b/apps/desktop/src/main/localDb/ipc/sessions.ts
index 7c8eee203c..3c5cd656e6 100644
--- a/apps/desktop/src/main/localDb/ipc/sessions.ts
+++ b/apps/desktop/src/main/localDb/ipc/sessions.ts
@@ -38,6 +38,7 @@ import {
sessionToCamel,
sessionCreateToRow,
sessionPatchToRow,
+ persistableSessionEffort,
normalizeRemoteHostId,
finalizePlainPreview,
type SessionRowWithCount,
@@ -180,6 +181,7 @@ async function writeSessionPatch(
setObj: ReturnType<typeof sessionPatchToRow>,
status: unknown,
): Promise<void> {
+ if (Object.keys(setObj).length === 0) return;
const deletedIsTerminal = status === 'active' || status === 'archived';
const result = await db
.update(sessions)
@@ -464,8 +466,10 @@ export async function applyAgentSwitchToSessionRow(
if (patch.providerId !== undefined) setObj.providerId = patch.providerId;
// effort 值域由 renderer 按目标引擎 capabilities 解析(schema 列是字面量联合,
// 跨层传输后此处以 string 到达;非法值与直改 DB 同级,运行时由引擎侧收敛)。
- if (patch.effort !== undefined) {
- setObj.effort = patch.effort as (typeof sessions.$inferInsert)['effort'];
+ // 固定 effort 模型运行时为 null;sessions.effort NOT NULL,省略该字段。
+ const persistableEffort = persistableSessionEffort(patch.effort);
+ if (persistableEffort !== undefined) {
+ setObj.effort = persistableEffort;
}
if (patch.fastMode !== undefined) setObj.fastMode = patch.fastMode;
if (typeof patch.contextWindow === 'number' && patch.contextWindow > 0) {
@@ -480,7 +484,7 @@ export async function applyAgentSwitchToSessionRow(
model: patch.model,
sdkSessionId: nextSdkSessionId,
...(patch.providerId !== undefined ? { providerId: patch.providerId } : {}),
- ...(patch.effort !== undefined ? { effort: patch.effort } : {}),
+ ...(persistableEffort !== undefined ? { effort: persistableEffort } : {}),
...(patch.fastMode !== undefined ? { fastMode: patch.fastMode } : {}),
...(typeof patch.contextWindow === 'number' && patch.contextWindow > 0
? { contextWindow: Math.floor(patch.contextWindow) }
@@ -555,11 +559,17 @@ export async function persistSessionFields(
for (const k of Object.keys(patch)) {
if (REMOTE_PERSIST_FIELDS.has(k)) clean[k] = patch[k];
}
+ if (Object.prototype.hasOwnProperty.call(clean, 'effort')) {
+ const persistableEffort = persistableSessionEffort(clean.effort);
+ if (persistableEffort === undefined) delete clean.effort;
+ else clean.effort = persistableEffort;
+ }
if (Object.keys(clean).length === 0) return;
const db = getDbClient().drizzle;
const setObj = sessionPatchToRow(clean as Parameters<typeof sessionPatchToRow>[0], {
bumpUpdatedAt: false,
});
+ if (Object.keys(setObj).length === 0) return;
await db.update(sessions).set(setObj).where(eq(sessions.id, sessionId));
if (isOwnerScopeCurrent(ownerScope)) broadcastSessionPatched(sessionId, clean, ownerScope);
}
diff --git a/apps/desktop/src/main/localDb/mapper.ts b/apps/desktop/src/main/localDb/mapper.ts
index 2412d59168..b4ddc3e099 100644
--- a/apps/desktop/src/main/localDb/mapper.ts
+++ b/apps/desktop/src/main/localDb/mapper.ts
@@ -54,6 +54,19 @@ import {
type SessionRow = typeof sessions.$inferSelect;
type SessionInsert = typeof sessions.$inferInsert;
+
+/**
+ * 运行时固定 effort 模型用 `null`;`sessions.effort` 是 NOT NULL 枚举。
+ * 空白 / null 表示不落库,UPDATE 保留该行已有的合法档位。
+ */
+export function persistableSessionEffort(
+ effort: unknown,
+): SessionInsert['effort'] | undefined {
+ if (typeof effort !== 'string') return undefined;
+ const trimmed = effort.trim();
+ return trimmed ? (trimmed as SessionInsert['effort']) : undefined;
+}
+
type MessageRow = typeof messages.$inferSelect;
type MessageInsert = typeof messages.$inferInsert;
type ScheduleRow = typeof schedules.$inferSelect;
@@ -384,7 +397,7 @@ export function sessionPatchToRow(
workingDir?: string;
workspaceKind?: WorkspaceKind;
model?: string;
- effort?: string;
+ effort?: string | null;
permissionMode?: string;
providerId?: string | null;
fastMode?: boolean;
@@ -409,7 +422,8 @@ export function sessionPatchToRow(
out.workingDir = normalizeWorkingDirForStorage(patch.workingDir);
if (patch.workspaceKind !== undefined) out.workspaceKind = patch.workspaceKind;
if (patch.model !== undefined) out.model = patch.model;
- if (patch.effort !== undefined) out.effort = patch.effort as SessionInsert['effort'];
+ const persistableEffort = persistableSessionEffort(patch.effort);
+ if (persistableEffort !== undefined) out.effort = persistableEffort;
if (patch.permissionMode !== undefined)
out.permissionMode = patch.permissionMode as SessionInsert['permissionMode'];
if (patch.providerId !== undefined) out.providerId = patch.providerId;
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index 6c5fffc5d4..18ae35322b 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -298,6 +298,7 @@ import {
recycleSessionWorktreeForStatusChange,
setSessionRuntimeCleanup,
} from '../localDb/ipc/sessions.js';
+import { persistableSessionEffort } from '../localDb/mapper.js';
// sidebar-card-mode: turn-done 后刷新列表预览,并按需生成置顶卡片摘要
import {
maybeGenerateSessionTaskSummary,
@@ -15547,6 +15548,7 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
return false;
}
const now = Date.now();
+ const persistableEffort = persistableSessionEffort(target.effort);
const sourceRouteConditions = [
eq(sessions.id, targetSessionId),
eq(sessions.sdkSessionId, source.sdkSessionId),
@@ -15568,7 +15570,7 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
sdkSessionId: newSdkSessionId,
model: target.model,
providerId: target.providerId,
- effort: target.effort as (typeof sessions.$inferInsert)['effort'],
+ ...(persistableEffort !== undefined ? { effort: persistableEffort } : {}),
fastMode: target.fastMode,
updatedAt: now,
})
@@ -15581,7 +15583,7 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
sdkSessionId: newSdkSessionId,
model: target.model,
providerId: target.providerId,
- effort: target.effort,
+ ...(persistableEffort !== undefined ? { effort: persistableEffort } : {}),
fastMode: target.fastMode,
updatedAt: new Date(now).toISOString(),
},