Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(plugins): bound remote model catalog cache
PraiseZhuc9342492d815
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/ghostAgentModels.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/ghostAgentModels.test.tsindex e773e22c50..3153a58782 100644--- a/apps/desktop/src/main/cindy-brain/__tests__/ghostAgentModels.test.ts+++ b/apps/desktop/src/main/cindy-brain/__tests__/ghostAgentModels.test.ts@@ -1,6 +1,10 @@import { describe, expect, it, vi } from 'vitest';import type { ProviderView } from '@cindy/model-providers';-import { createGhostAgentModelsCache, projectGhostAgentModels } from '../ghostAgentModels.js';+import {+ createGhostAgentModelsCache,+ GHOST_AGENT_MODELS_CACHE_MAX_CATALOGS,+ projectGhostAgentModels,+} from '../ghostAgentModels.js';const provider = (id: string, efforts: string[]) =>({@@ -177,16 +181,19 @@ describe('plugin model projection', () => {* - 生产入口与可观察结果:插件目录缓存命中仍有效的目录,过期或被容量淘汰的目录重新读取。* - Bug/规则依据:makecindy/cindy#7472 Greptile「旧目录一直占内存」。* - 现有覆盖缺口:现有用例没有验证 TTL 过期回收,也没有跨 owner 的总目录条数上限。- * - 最小失败输入:63 个过期目录、1 个新鲜目录与第 65 个目录;再读新鲜项及最早插入项。+ * - 最小失败输入:达到 64 项后保留 1 个新鲜目录,其余过期;写入超限目录后重读新鲜项与最早项。* - tier:unit。- * - 实测耗时:0.59 s(红测单文件运行,macOS arm64)。+ * - 实测耗时:0.57 s(修复后单文件运行,macOS arm64)。*/it('evicts expired catalogs before enforcing a bounded cache size', async () => {let now = 0;const cache = createGhostAgentModelsCache({ now: () => now, ttlMs: 60_000 });const readFor = (id: string) => vi.fn<() => Promise<ProviderView[]>>().mockResolvedValue([provider(id, ['high'])]);- const expiredReads = Array.from({ length: 63 }, (_, index) => readFor(`expired-${index}`));+ const expiredReads = Array.from(+ { length: GHOST_AGENT_MODELS_CACHE_MAX_CATALOGS - 1 },+ (_, index) => readFor(`expired-${index}`),+ );for (const [index, read] of expiredReads.entries()) {await cache.catalog(`expired-owner-${index}`, `expired-device-${index}`, read);@@ -207,7 +214,10 @@ describe('plugin model projection', () => {expect(freshRead).toHaveBeenCalledTimes(1);const bounded = createGhostAgentModelsCache({ now: () => 0, ttlMs: 60_000 });- const capacityReads = Array.from({ length: 65 }, (_, index) => readFor(`capacity-${index}`));+ const capacityReads = Array.from(+ { length: GHOST_AGENT_MODELS_CACHE_MAX_CATALOGS + 1 },+ (_, index) => readFor(`capacity-${index}`),+ );for (const [index, read] of capacityReads.entries()) {await bounded.catalog(`capacity-owner-${index}`, `capacity-device-${index}`, read);}diff --git a/apps/desktop/src/main/cindy-brain/ghostAgentModels.ts b/apps/desktop/src/main/cindy-brain/ghostAgentModels.tsindex 49af2351ca..63fc362585 100644--- a/apps/desktop/src/main/cindy-brain/ghostAgentModels.ts+++ b/apps/desktop/src/main/cindy-brain/ghostAgentModels.ts@@ -7,6 +7,7 @@ import {import type { GhostAgentModelsResult } from '../../shared/ghost.js';export const GHOST_AGENT_MODELS_CACHE_MS = 60_000;+export const GHOST_AGENT_MODELS_CACHE_MAX_CATALOGS = 64;export const GHOST_REMOTE_AGENT_MODELS = ['codex', 'claude-code', 'pi'] as const;export type GhostAgentLocation = { agentDeviceId: string; name: string };export type GhostAgentLocationsReadResult =@@ -47,6 +48,22 @@ export function createGhostAgentModelsCache(options: { now?: () => number; ttlMsconst locationReads = new Map<string, Promise<GhostAgentLocation[] | undefined>>();const catalogs = new Map<string, { at: number; value: GhostAgentModelsOk }>();const catalogReads = new Map<string, Promise<GhostAgentModelsResult>>();+ const pruneExpiredCatalogs = (at: number) => {+ for (const [key, entry] of catalogs) {+ if (at - entry.at >= ttlMs) catalogs.delete(key);+ }+ };+ const storeCatalog = (key: string, value: GhostAgentModelsOk) => {+ const at = now();+ pruneExpiredCatalogs(at);+ catalogs.delete(key);+ while (catalogs.size >= GHOST_AGENT_MODELS_CACHE_MAX_CATALOGS) {+ const oldestKey = catalogs.keys().next().value;+ if (oldestKey === undefined) break;+ catalogs.delete(oldestKey);+ }+ catalogs.set(key, { at, value });+ };return {locations(@@ -81,8 +98,14 @@ export function createGhostAgentModelsCache(options: { now?: () => number; ttlMsread: () => Promise<ProviderView[]>,): Promise<GhostAgentModelsResult> {const key = `${owner}\0${agentDeviceId}`;+ const requestedAt = now();+ pruneExpiredCatalogs(requestedAt);const hit = catalogs.get(key);- if (hit && now() - hit.at < ttlMs) return Promise.resolve(hit.value);+ if (hit) {+ catalogs.delete(key);+ catalogs.set(key, hit);+ return Promise.resolve(hit.value);+ }const inFlight = catalogReads.get(key);if (inFlight) return inFlight;@@ -93,7 +116,7 @@ export function createGhostAgentModelsCache(options: { now?: () => number; ttlMsconst 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 });+ storeCatalog(key, value);return value;}).catch((): GhostAgentModelsResult => ({