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(plugins): coalesce agent location reads

PraiseZhu5ffdd3ec755a
diff --git a/apps/desktop/src/main/cindy-brain/ghostAgentModels.ts b/apps/desktop/src/main/cindy-brain/ghostAgentModels.ts
index b2f78fb527..f9f0bb321e 100644
--- a/apps/desktop/src/main/cindy-brain/ghostAgentModels.ts
+++ b/apps/desktop/src/main/cindy-brain/ghostAgentModels.ts
@@ -44,20 +44,35 @@ export function createGhostAgentModelsCache(options: { now?: () => number; ttlMs
const now = options.now ?? Date.now;
const ttlMs = options.ttlMs ?? GHOST_AGENT_MODELS_CACHE_MS;
let locations: { owner: string; at: number; value: GhostAgentLocation[] } | null = null;
+ const locationReads = new Map<string, Promise<GhostAgentLocation[] | undefined>>();
const catalogs = new Map<string, { at: number; value: GhostAgentModelsOk }>();
return {
- async locations(owner: string, read: () => Promise<GhostAgentLocationsReadResult>): Promise<GhostAgentLocation[] | undefined> {
- if (locations && locations.owner === owner && now() - locations.at < ttlMs) return locations.value;
- try {
- const result = await read();
- if (result.status !== 'complete') return undefined;
- const value = result.locations;
- locations = { owner, at: now(), value };
- return value;
- } catch {
- return undefined;
+ locations(
+ owner: string,
+ read: () => Promise<GhostAgentLocationsReadResult>,
+ ): Promise<GhostAgentLocation[] | undefined> {
+ if (locations && locations.owner === owner && now() - locations.at < ttlMs) {
+ return Promise.resolve(locations.value);
}
+ const inFlight = locationReads.get(owner);
+ if (inFlight) return inFlight;
+
+ let pending: Promise<GhostAgentLocation[] | undefined>;
+ pending = Promise.resolve()
+ .then(read)
+ .then((result) => {
+ if (result.status !== 'complete') return undefined;
+ const value = result.locations;
+ locations = { owner, at: now(), value };
+ return value;
+ })
+ .catch(() => undefined)
+ .finally(() => {
+ if (locationReads.get(owner) === pending) locationReads.delete(owner);
+ });
+ locationReads.set(owner, pending);
+ return pending;
},
async catalog(
owner: string,
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/workerAgentLocations.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/workerAgentLocations.test.ts
index 4fcbaaeef9..4a007c1b0c 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/workerAgentLocations.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/workerAgentLocations.test.ts
@@ -5,7 +5,7 @@
* - 现有覆盖缺口:原用例只验证降级列表,没有覆盖插件缓存失败恢复或同账号并发合并
* - 最小失败输入:同账号两个并发读取,handleListDevices 先拒绝、重试再返回一台远程电脑
* - tier:unit
- * - 实测耗时:4.03 s(红测单文件运行,macOS arm64)
+ * - 实测耗时:4.02 s(修复后单文件运行,macOS arm64)
*/
import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { DeviceLinkDeviceView } from '../../../shared/deviceLinkIpc.js';