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 remote catalog reads

PraiseZhu0857c7bbae3d
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/ghostAgentModels.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/ghostAgentModels.test.ts
index 6bd1856f55..59d8dcc672 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/ghostAgentModels.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/ghostAgentModels.test.ts
@@ -146,7 +146,7 @@ describe('plugin model projection', () => {
* - 现有覆盖缺口:既有用例只覆盖已完成的目录结果,没有覆盖空缓存下的并发读取或失败清理。
* - 最小失败输入:同一 owner 和 device 两次并发读取,首次拒绝、重试返回一个可见模型。
* - tier:unit。
- * - 实测耗时:0.62 s(红测单文件运行,macOS arm64)。
+ * - 实测耗时:0.59 s(修复后单文件运行,macOS arm64)。
*/
it('shares an in-flight catalog read for the same owner and device, then retries after failure', async () => {
const cache = createGhostAgentModelsCache();
diff --git a/apps/desktop/src/main/cindy-brain/ghostAgentModels.ts b/apps/desktop/src/main/cindy-brain/ghostAgentModels.ts
index f9f0bb321e..49af2351ca 100644
--- a/apps/desktop/src/main/cindy-brain/ghostAgentModels.ts
+++ b/apps/desktop/src/main/cindy-brain/ghostAgentModels.ts
@@ -46,6 +46,7 @@ export function createGhostAgentModelsCache(options: { now?: () => number; ttlMs
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 }>();
+ const catalogReads = new Map<string, Promise<GhostAgentModelsResult>>();
return {
locations(
@@ -74,23 +75,37 @@ export function createGhostAgentModelsCache(options: { now?: () => number; ttlMs
locationReads.set(owner, pending);
return pending;
},
- async catalog(
+ catalog(
owner: string,
agentDeviceId: string,
read: () => Promise<ProviderView[]>,
): Promise<GhostAgentModelsResult> {
const key = `${owner}\0${agentDeviceId}`;
const hit = catalogs.get(key);
- if (hit && now() - hit.at < ttlMs) return hit.value;
- try {
- const projected = projectGhostAgentModels(await read(), GHOST_REMOTE_AGENT_MODELS);
- if (!projected.ok) return { ok: false, errorCode: 'NOT_AVAILABLE', message: 'Model catalog unavailable' };
- const value = { ...projected, agentDeviceId };
- catalogs.set(key, { at: now(), value });
- return value;
- } catch {
- return { ok: false, errorCode: 'NOT_AVAILABLE', message: 'Model catalog unavailable' };
- }
+ if (hit && now() - hit.at < ttlMs) return Promise.resolve(hit.value);
+ const inFlight = catalogReads.get(key);
+ if (inFlight) return inFlight;
+
+ let pending: Promise<GhostAgentModelsResult>;
+ pending = Promise.resolve()
+ .then(read)
+ .then((views): GhostAgentModelsResult => {
+ const projected = projectGhostAgentModels(views, GHOST_REMOTE_AGENT_MODELS);
+ if (!projected.ok) return { ok: false, errorCode: 'NOT_AVAILABLE', message: 'Model catalog unavailable' };
+ const value = { ...projected, agentDeviceId };
+ catalogs.set(key, { at: now(), value });
+ return value;
+ })
+ .catch((): GhostAgentModelsResult => ({
+ ok: false,
+ errorCode: 'NOT_AVAILABLE',
+ message: 'Model catalog unavailable',
+ }))
+ .finally(() => {
+ if (catalogReads.get(key) === pending) catalogReads.delete(key);
+ });
+ catalogReads.set(key, pending);
+ return pending;
},
};
}