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): skip caching incomplete agent locations

PraiseZhu1e388bd014e4
diff --git a/apps/desktop/src/main/cindy-brain/ghostAgentModels.ts b/apps/desktop/src/main/cindy-brain/ghostAgentModels.ts
index 8c7adbd673..b2f78fb527 100644
--- a/apps/desktop/src/main/cindy-brain/ghostAgentModels.ts
+++ b/apps/desktop/src/main/cindy-brain/ghostAgentModels.ts
@@ -9,6 +9,9 @@ import type { GhostAgentModelsResult } from '../../shared/ghost.js';
export const GHOST_AGENT_MODELS_CACHE_MS = 60_000;
export const GHOST_REMOTE_AGENT_MODELS = ['codex', 'claude-code', 'pi'] as const;
export type GhostAgentLocation = { agentDeviceId: string; name: string };
+export type GhostAgentLocationsReadResult =
+ | { status: 'complete'; locations: GhostAgentLocation[] }
+ | { status: 'degraded'; locations: GhostAgentLocation[] };
type GhostAgentModelsOk = Extract<GhostAgentModelsResult, { ok: true }>;
/** Project each provider separately: the same model can support different efforts per route. */
@@ -44,10 +47,12 @@ export function createGhostAgentModelsCache(options: { now?: () => number; ttlMs
const catalogs = new Map<string, { at: number; value: GhostAgentModelsOk }>();
return {
- async locations(owner: string, read: () => Promise<GhostAgentLocation[]>): Promise<GhostAgentLocation[] | undefined> {
+ async locations(owner: string, read: () => Promise<GhostAgentLocationsReadResult>): Promise<GhostAgentLocation[] | undefined> {
if (locations && locations.owner === owner && now() - locations.at < ttlMs) return locations.value;
try {
- const value = await read();
+ const result = await read();
+ if (result.status !== 'complete') return undefined;
+ const value = result.locations;
locations = { owner, at: now(), value };
return value;
} catch {
diff --git a/apps/desktop/src/main/cindy-brain/index.ts b/apps/desktop/src/main/cindy-brain/index.ts
index 17ccdcc5ad..1eb9f8daf2 100644
--- a/apps/desktop/src/main/cindy-brain/index.ts
+++ b/apps/desktop/src/main/cindy-brain/index.ts
@@ -77,7 +77,11 @@ async function resolveMobilePluginMedia(instanceId: string, url: string, current
return file && /^[a-f0-9]{64}\.[a-z0-9]+$/.test(file) ? { path: `/media/${file}`, mediaKind: result.kind } : null;
}
import { getModelVisibilityOverride, waitForModelVisibilityMirror } from '../maker-host/model-visibility-mirror.js';
-import { createGhostAgentModelsCache, projectGhostAgentModels } from './ghostAgentModels.js';
+import {
+ createGhostAgentModelsCache,
+ projectGhostAgentModels,
+ type GhostAgentLocationsReadResult,
+} from './ghostAgentModels.js';
import { getDesktopProviderService } from '../maker-host/createDesktopProviderService.js';
import { PluginDownloadSlot } from './downloadSlot.js';
import { createDownloader } from '../downloader/index.js';
@@ -2166,12 +2170,12 @@ export function setGhostAgentTurnRunner(runner: GhostAgentTurnRunner | null): vo
}
const ghostAgentModelsCache = createGhostAgentModelsCache();
-let listGhostAgentLocations: (() => Promise<Array<{ agentDeviceId: string; name: string }>>) | null = null;
+let listGhostAgentLocations: (() => Promise<GhostAgentLocationsReadResult>) | null = null;
let readGhostAgentRemoteViews: ((agentDeviceId: string) => Promise<ProviderView[]>) | null = null;
/** maker-ipc 注入设备列表与跨设备目录读取;静态 import device-link 会成环。 */
export function setGhostAgentModelLocationSources(sources: {
- listLocations: () => Promise<Array<{ agentDeviceId: string; name: string }>>;
+ listLocations: () => Promise<GhostAgentLocationsReadResult>;
readRemoteViews: (agentDeviceId: string) => Promise<ProviderView[]>;
} | null): void {
listGhostAgentLocations = sources?.listLocations ?? null;
diff --git a/apps/desktop/src/main/cindy-brain/runtime/__tests__/electronSandboxAdapter.ownerPartition.test.ts b/apps/desktop/src/main/cindy-brain/runtime/__tests__/electronSandboxAdapter.ownerPartition.test.ts
index 910fd188c4..462553984d 100644
--- a/apps/desktop/src/main/cindy-brain/runtime/__tests__/electronSandboxAdapter.ownerPartition.test.ts
+++ b/apps/desktop/src/main/cindy-brain/runtime/__tests__/electronSandboxAdapter.ownerPartition.test.ts
@@ -936,7 +936,7 @@ describe('read-only agent model directory', () => {
let now = 1_000;
const listLocations = vi.fn()
.mockRejectedValueOnce(new Error('cloud down'))
- .mockResolvedValue([{ agentDeviceId: 'claude', name: 'Claude' }]);
+ .mockResolvedValue({ status: 'complete', locations: [{ agentDeviceId: 'claude', name: 'Claude' }] });
const readRemote = vi.fn()
.mockRejectedValueOnce(new Error('offline'))
.mockResolvedValue([] as ProviderView[]);
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 efe7bba393..38187a24db 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/workerAgentLocations.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/workerAgentLocations.test.ts
@@ -1,11 +1,11 @@
/**
* 测试价值
- * - 生产入口与可观察结果:位置读取和插件模型缓存使用真实 listWorkerAgentLocations;完整读取后显示远程电脑,失败时省略 locations,恢复后可重读
+ * - 生产入口与可观察结果:插件模型缓存使用真实 readWorkerAgentLocations;完整读取后显示远程电脑,失败时省略 locations,恢复后可重读
* - Bug/规则依据:makecindy/cindy#7452 SC1;makecindy/cindy#7472 Greptile 失败位置缓存;plugin-agent-models.md §4.0.3a
* - 现有覆盖缺口:现有用例只验证读取函数的降级列表,没有验证插件缓存不会保存该降级结果
* - 最小失败输入:handleListDevices 首次拒绝、随后返回一台远程电脑;同时有一个 active 分享
* - tier:unit
- * - 实测耗时:4.54 s(单文件红测,macOS arm64)
+ * - 实测耗时:4.70 s(修复后单文件运行,macOS arm64)
*/
import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { DeviceLinkDeviceView } from '../../../shared/deviceLinkIpc.js';
@@ -30,7 +30,7 @@ vi.mock('../../device-link/providerShareGuest.js', () => ({
providerShareAgentDeviceId: io.providerShareAgentDeviceId,
}));
-import { listWorkerAgentLocations } from '../workerAgentLocations.js';
+import { listWorkerAgentLocations, readWorkerAgentLocations } from '../workerAgentLocations.js';
import { createGhostAgentModelsCache } from '../../cindy-brain/ghostAgentModels.js';
function device(overrides: Partial<DeviceLinkDeviceView>): DeviceLinkDeviceView {
@@ -96,8 +96,8 @@ describe('listWorkerAgentLocations', () => {
{ shareId: 's1', status: 'active', providerLabel: 'Claude API', owner: { displayName: 'Ann' } },
]);
- await expect(cache.locations('owner', listWorkerAgentLocations)).resolves.toBeUndefined();
- await expect(cache.locations('owner', listWorkerAgentLocations)).resolves.toEqual([
+ await expect(cache.locations('owner', readWorkerAgentLocations)).resolves.toBeUndefined();
+ await expect(cache.locations('owner', readWorkerAgentLocations)).resolves.toEqual([
{ agentDeviceId: 'mac-mini', name: 'Mac mini' },
{ agentDeviceId: 'share:s1', name: 'Claude API · Ann' },
]);
diff --git a/apps/desktop/src/main/maker-ipc/register.ts b/apps/desktop/src/main/maker-ipc/register.ts
index 56de74bbd7..03ad0bdfe1 100644
--- a/apps/desktop/src/main/maker-ipc/register.ts
+++ b/apps/desktop/src/main/maker-ipc/register.ts
@@ -1066,7 +1066,7 @@ import {
registerOrcaRemoteWorkerHandlers,
} from './orcaRemoteWorkerHost.js';
import { createOrcaRemoteWorkers, invokeDeviceValue, type OrcaRemoteWorkers } from './orcaRemoteWorkers.js';
-import { listWorkerAgentLocations } from './workerAgentLocations.js';
+import { listWorkerAgentLocations, readWorkerAgentLocations } from './workerAgentLocations.js';
import {
ORCA_EXECUTION_DEVICES_CHANNEL,
ORCA_REMOTE_WORKER_BRIDGE_CHANNEL,
@@ -14636,7 +14636,7 @@ export function registerMakerIpc(maker: Maker, options: RegisterMakerIpcOptions)
// cindy-brain 反向依赖 maker-ipc / localDb 形成模块环。
setGhostLibraryExtraDirSync(syncLibraryReadonlyExtraDir);
setGhostAgentModelLocationSources({
- listLocations: listWorkerAgentLocations,
+ listLocations: readWorkerAgentLocations,
readRemoteViews: (agentDeviceId) => readDeviceProviderViews(remoteBackgroundInvoke, agentDeviceId),
});
getGhostFsSlot().setSessionSnapshotResolver(async (sessionId, instanceId) =>
diff --git a/apps/desktop/src/main/maker-ipc/workerAgentLocations.ts b/apps/desktop/src/main/maker-ipc/workerAgentLocations.ts
index 54a137daf9..b8a6c78e4c 100644
--- a/apps/desktop/src/main/maker-ipc/workerAgentLocations.ts
+++ b/apps/desktop/src/main/maker-ipc/workerAgentLocations.ts
@@ -3,6 +3,9 @@ import { getReceivedShares, providerShareAgentDeviceId } from '../device-link/pr
import { isExecutionDeviceCandidate } from './orcaRemoteWorkers.js';
export type WorkerAgentLocation = { agentDeviceId: string; name: string };
+export type WorkerAgentLocationsReadResult =
+ | { status: 'complete'; locations: WorkerAgentLocation[] }
+ | { status: 'degraded'; locations: WorkerAgentLocation[] };
/**
* Lead 的 list_available_models 与插件 GET /agent-models 共用的 Worker Agent 位置:
@@ -10,10 +13,16 @@ export type WorkerAgentLocation = { agentDeviceId: string; name: string };
* 那台没开放供应商时按位置再列模型会是空的。分享不写分享者的电脑名
* (provider-sharing.md §6),写「供应商 · 分享者」。不含本机。
*/
-export async function listWorkerAgentLocations(): Promise<WorkerAgentLocation[]> {
- const { devices } = await handleListDevices(deviceDirectoryDeps()).catch(() => ({ devices: [] }));
+export async function readWorkerAgentLocations(): Promise<WorkerAgentLocationsReadResult> {
+ let devices: Awaited<ReturnType<typeof handleListDevices>>['devices'] = [];
+ let complete = true;
+ try {
+ ({ devices } = await handleListDevices(deviceDirectoryDeps()));
+ } catch {
+ complete = false;
+ }
const shares = getReceivedShares().filter((share) => share.status === 'active');
- return [
+ const locations = [
...devices
.filter(isExecutionDeviceCandidate)
.map((device) => ({ agentDeviceId: device.deviceId, name: device.name || device.deviceId })),
@@ -28,4 +37,10 @@ export async function listWorkerAgentLocations(): Promise<WorkerAgentLocation[]>
}
}),
];
+ return complete ? { status: 'complete', locations } : { status: 'degraded', locations };
+}
+
+/** Keeps the existing best-effort array contract for non-plugin callers. */
+export async function listWorkerAgentLocations(): Promise<WorkerAgentLocation[]> {
+ return (await readWorkerAgentLocations()).locations;
}