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): 自定义供应商 updated_at 为非数值时不再写出 NaN (#3116)

smonkes553809ac970b
diff --git a/apps/desktop/src/main/maker-host/__tests__/custom-provider-store.test.ts b/apps/desktop/src/main/maker-host/__tests__/custom-provider-store.test.ts
index 53dbfd39d0..b11bb2fcd4 100644
--- a/apps/desktop/src/main/maker-host/__tests__/custom-provider-store.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/custom-provider-store.test.ts
@@ -933,6 +933,65 @@ describe('custom-provider-store CRUD (per-runtime)', () => {
expect(await updateCustomProvider('ghost', valid)).toBeNull();
});
+ // updated_at 列声明为 INTEGER,但表不是 STRICT,所以历史脏数据可能是文本。旧实现直接
+ // 对它 +1 得到字符串拼接,Math.max 变 NaN,写入 NOT NULL 整数列即失败 —— 那个供应商
+ // 此后永久保存不了。
+ it('still saves when updated_at holds a non-numeric value', async () => {
+ mountDb();
+ await createCustomProvider(valid, 1_000);
+ raw!
+ .prepare('UPDATE custom_providers SET updated_at = ? WHERE id = ?')
+ .run('2026-08-19T01:45:07.003Z', 'openrouter');
+ expect(
+ raw!.prepare("SELECT typeof(updated_at) AS t FROM custom_providers WHERE id = 'openrouter'").get(),
+ ).toEqual({ t: 'text' });
+
+ const updated = await updateCustomProvider('openrouter', { ...valid, name: 'Recovered' }, 5_000);
+
+ expect(updated?.name).toBe('Recovered');
+ expect((await getCustomProvider('openrouter'))?.name).toBe('Recovered');
+ const row = raw!
+ .prepare("SELECT updated_at AS updatedAt, typeof(updated_at) AS t FROM custom_providers WHERE id = 'openrouter'")
+ .get() as { updatedAt: number; t: string };
+ expect(row.t).toBe('integer');
+ expect(row.updatedAt).toBe(5_000);
+ });
+
+ it('keeps the optimistic-lock bump strictly increasing when the clock does not advance', async () => {
+ mountDb();
+ await createCustomProvider(valid, 9_000);
+ // now 早于库里的 updatedAt:仍必须写出更大的值,否则乐观锁比较会失效。
+ await updateCustomProvider('openrouter', { ...valid, name: 'Same tick' }, 1_000);
+ const row = raw!
+ .prepare("SELECT updated_at AS updatedAt FROM custom_providers WHERE id = 'openrouter'")
+ .get() as { updatedAt: number };
+ expect(row.updatedAt).toBe(9_001);
+ });
+
+ it('recovers a non-numeric updated_at through the snapshot-guarded write too', async () => {
+ mountDb();
+ await createCustomProvider(valid, 1_000);
+ const snapshot = await getCustomProvider('openrouter');
+ raw!
+ .prepare('UPDATE custom_providers SET updated_at = ? WHERE id = ?')
+ .run('not-a-timestamp', 'openrouter');
+
+ // where 子句用旧值做乐观锁比较,文本值也要能匹配上并写回一个合法整数。
+ expect(
+ await updateCustomProviderIfUnchanged(
+ 'openrouter',
+ snapshot!,
+ { ...snapshot!, name: 'Discovered' },
+ 7_000,
+ ),
+ ).toBe(true);
+ const row = raw!
+ .prepare("SELECT updated_at AS updatedAt, typeof(updated_at) AS t FROM custom_providers WHERE id = 'openrouter'")
+ .get() as { updatedAt: number; t: string };
+ expect(row.t).toBe('integer');
+ expect(row.updatedAt).toBe(7_000);
+ });
+
it('isolates data per db file (account switch = new db)', async () => {
mountDb();
await createCustomProvider(valid);
diff --git a/apps/desktop/src/main/maker-host/custom-provider-store.ts b/apps/desktop/src/main/maker-host/custom-provider-store.ts
index 933e2fdafb..faeb573287 100644
--- a/apps/desktop/src/main/maker-host/custom-provider-store.ts
+++ b/apps/desktop/src/main/maker-host/custom-provider-store.ts
@@ -655,6 +655,21 @@ function parseAuth(raw: string | null): CustomProviderConfig['auth'] {
return undefined;
}
+/**
+ * 计算写回的 `updatedAt`:既要严格大于库里的旧值(`updateCustomProviderIfUnchanged` 依赖它
+ * 做乐观锁),也要不早于当前时刻。
+ *
+ * 必须先转数值再 +1:`updated_at` 列声明为 INTEGER,但 SQLite 非 STRICT 表允许存入文本,
+ * 而 `rowToConfig` 只读 id/name/runtimes/auth、不校验该列。一旦某行存的是字符串,
+ * `existing.updatedAt + 1` 会退化成字符串拼接,`Math.max` 得到 NaN,写入 NOT NULL 整数列
+ * 触发 SQLITE_CONSTRAINT_NOTNULL —— 该供应商此后永久无法保存,且错误对用户不可诉。
+ */
+function nextUpdatedAt(existingUpdatedAt: unknown, now: number): number {
+ const previous = Number(existingUpdatedAt);
+ if (!Number.isFinite(previous)) return now;
+ return Math.max(now, previous + 1);
+}
+
/** 安全解析 runtimes JSON(坏数据兜底为 {},逐字段防御)。 */
function parseRuntimes(raw: string): Partial<Record<AgentKind, CustomProviderRuntimeConfig>> {
let v: unknown;
@@ -812,7 +827,7 @@ export async function updateCustomProvider(
name: c.name,
runtimes: JSON.stringify(c.runtimes),
auth: c.auth ? JSON.stringify(c.auth) : null,
- updatedAt: Math.max(now, existing.updatedAt + 1),
+ updatedAt: nextUpdatedAt(existing.updatedAt, now),
})
.where(eq(customProviders.id, id));
return c;
@@ -850,7 +865,7 @@ export async function updateCustomProviderIfUnchanged(
name: nextConfig.name,
runtimes: JSON.stringify(nextConfig.runtimes),
auth: nextConfig.auth ? JSON.stringify(nextConfig.auth) : null,
- updatedAt: Math.max(now, existing.updatedAt + 1),
+ updatedAt: nextUpdatedAt(existing.updatedAt, now),
})
.where(and(eq(customProviders.id, id), eq(customProviders.updatedAt, existing.updatedAt)));
return result.changes === 1;