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(provider-group): discard expired cooldowns during atomic merge

Lizi1695e9e1cb5d
diff --git a/apps/desktop/src/main/provider-group/__tests__/coolingStore.test.ts b/apps/desktop/src/main/provider-group/__tests__/coolingStore.test.ts
index 0ef900e53e..910069c61d 100644
--- a/apps/desktop/src/main/provider-group/__tests__/coolingStore.test.ts
+++ b/apps/desktop/src/main/provider-group/__tests__/coolingStore.test.ts
@@ -42,6 +42,7 @@ describe('mergeCooling', () => {
expect(mergeCooling(
{ anthropic: { 'device:mini:a': 1_000 } },
{ anthropic: { 'device:studio:a': 2_000 } },
+ 0,
)).toEqual({
anthropic: { 'device:mini:a': 1_000, 'device:studio:a': 2_000 },
});
@@ -53,12 +54,14 @@ describe('mergeCooling', () => {
expect(mergeCooling(
{ anthropic: { 'device:mini:a': 5_000 } },
{ anthropic: { 'device:mini:a': 3_000 } },
+ 0,
)).toEqual({
anthropic: { 'device:mini:a': 5_000 },
});
expect(mergeCooling(
{ anthropic: { 'device:mini:a': 3_000 } },
{ anthropic: { 'device:mini:a': 5_000 } },
+ 0,
)).toEqual({
anthropic: { 'device:mini:a': 5_000 },
});
@@ -68,8 +71,17 @@ describe('mergeCooling', () => {
expect(mergeCooling(
{ anthropic: { a: 1 } },
{ openai: { b: 2 } },
+ 0,
)).toEqual({ anthropic: { a: 1 }, openai: { b: 2 } });
});
+
+ it('到期条目从两份快照移除,未到期的并发冷却保留较晚结束时间', () => {
+ expect(mergeCooling(
+ { anthropic: { old: 99, boundary: 100, active: 200 }, openai: { old: 1 } },
+ { anthropic: { stale: 50, active: 150, fresh: 300 } },
+ 100,
+ )).toEqual({ anthropic: { active: 200, fresh: 300 } });
+ });
});
describe('writeProviderGroupCooling (real file)', () => {
@@ -93,10 +105,31 @@ describe('writeProviderGroupCooling (real file)', () => {
it('keeps an earlier cooldown instead of discarding it on the next write (greptile-apps P1)', async () => {
// 模拟 PR 场景:两台电脑先后撞上限额。修复前的实现每次 updateAtomic 都是整份覆盖,
// 谁最后拿到锁谁就是最终结果;修复后每次写入在锁内和当前文件内容合并,谁先谁后都不会挤丢对方。
- coolingStore.writeProviderGroupCooling({ anthropic: { 'device:studio:a': 2_000 } });
- await new Promise((resolve) => setTimeout(resolve, 50));
- coolingStore.writeProviderGroupCooling({ anthropic: { 'device:mini:a': 1_000 } });
- await new Promise((resolve) => setTimeout(resolve, 50));
- expect(coolingStore.readProviderGroupCooling().anthropic).toEqual({ 'device:studio:a': 2_000, 'device:mini:a': 1_000 });
+ const now = Date.now();
+ const studioUntil = now + 120_000;
+ const miniUntil = now + 60_000;
+ coolingStore.writeProviderGroupCooling({ anthropic: { 'device:studio:a': studioUntil } });
+ coolingStore.writeProviderGroupCooling({ anthropic: { 'device:mini:a': miniUntil } });
+ await vi.waitFor(() => {
+ expect(coolingStore.readProviderGroupCooling().anthropic).toEqual({ 'device:studio:a': studioUntil, 'device:mini:a': miniUntil });
+ });
+ });
+
+ it('旧文件的 512 个已过期成员释放容量,新成员冷却写盘并在重启后保留', async () => {
+ const now = Date.now();
+ const expired = Object.fromEntries(Array.from({ length: PROVIDER_GROUP_STORAGE_MEMBER_CAP }, (_, i) => [`device:old${i}:openai`, now - 1000]));
+ const file = path.join(tmpDir, 'provider-group-cooling.json');
+ // 模拟旧版已经写满的文件;当前新版写入不能再把过期条目并回来。
+ const current = coolingStore.readProviderGroupCooling();
+ fs.writeFileSync(file, JSON.stringify({ cooling: { ...current, openai: expired } }));
+ const next = { 'device:new:openai': now + 60_000 };
+ coolingStore.writeProviderGroupCooling({ openai: next });
+ await vi.waitFor(() => {
+ expect(JSON.parse(fs.readFileSync(file, 'utf8')).cooling.openai).toEqual(next);
+ });
+ vi.resetModules();
+ const restarted = await import('../coolingStore.js');
+ expect(restarted.readProviderGroupCooling().openai).toEqual(next);
+ expect(restarted.readProviderGroupCooling().anthropic).toEqual(current.anthropic);
});
});
diff --git a/apps/desktop/src/main/provider-group/coolingStore.ts b/apps/desktop/src/main/provider-group/coolingStore.ts
index 724a51d911..2b30704f5e 100644
--- a/apps/desktop/src/main/provider-group/coolingStore.ts
+++ b/apps/desktop/src/main/provider-group/coolingStore.ts
@@ -54,11 +54,12 @@ export function readProviderGroupCooling(): ProviderGroupPersistedCooling {
/**
* 在锁内和所在文件当前内容合并(按 provider + 成员取较晚的结束时间),不直接用调用方
* 快照覆盖:连续冷却两台电脑时,早开始、后步拿到文件锁的写入不应该把已落地的那一台挤丢
- * (greptile-apps P1, PR #5792 review)。
+ * (greptile-apps P1, PR #5792 review)。合并时移除到期记录,避免旧成员永久占满存储边界。
*/
function mergeCooling(
current: ProviderGroupPersistedCooling,
next: ProviderGroupPersistedCooling,
+ now = Date.now(),
): ProviderGroupPersistedCooling {
const merged: ProviderGroupPersistedCooling = {};
for (const providerId of new Set([...Object.keys(current), ...Object.keys(next)])) {
@@ -67,7 +68,7 @@ function mergeCooling(
const entries: Record<string, number> = {};
for (const memberKey of new Set([...Object.keys(a), ...Object.keys(b)])) {
const until = Math.max(a[memberKey] ?? 0, b[memberKey] ?? 0);
- if (until > 0) entries[memberKey] = until;
+ if (until > now) entries[memberKey] = until;
}
if (Object.keys(entries).length > 0) merged[providerId] = entries;
}