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(desktop): title IPC 补 sender 守卫,分页游标缺失归 upstream,省掉重复 Keychain 读

Dasha34531cbfefd
diff --git a/apps/desktop/src/main/maker-host/__tests__/anthropicModelDiscovery.test.ts b/apps/desktop/src/main/maker-host/__tests__/anthropicModelDiscovery.test.ts
index 8f12303f09..118ed4b5dc 100644
--- a/apps/desktop/src/main/maker-host/__tests__/anthropicModelDiscovery.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/anthropicModelDiscovery.test.ts
@@ -1033,6 +1033,41 @@ describe('HTTP 发现失败的归因与选择性重试', () => {
}
});
+ it('has_more=true 但没有可用游标归 upstream,不把半截清单当完整清单', async () => {
+ // 静默收尾会把「只翻了一页的前缀」当成完整清单交出去,而它完全可能小到刚好落进 shrink
+ // 守卫的放行区间 —— 真清单被截断替换、失败态被清、也不排重试(PR #548 review)。
+ const model = (id: string) => ({ id, display_name: id, type: 'model' });
+ for (const bad of [{ has_more: true }, { has_more: true, last_id: 42 }, { has_more: true, last_id: '' }]) {
+ resetAnthropicDiscoveryForTest();
+ setAnthropicDiscoveredModels([]);
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn().mockResolvedValue({
+ ok: true,
+ json: async () => ({ data: [model('claude-opus-4-8')], ...bad }),
+ }),
+ );
+ await refreshAnthropicModelsFromHttp();
+ expect(getAnthropicModelDiscoveryFailure()?.kind).toBe('upstream');
+ // 半截结果一律不生效。
+ expect(anthropicIds()).toEqual([]);
+ }
+ });
+
+ it('连接态由调用方给出时不再重读凭证库(macOS 上那是一次同步 Keychain 子进程)', async () => {
+ vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new TypeError('fetch failed')));
+ await refreshAnthropicModelsFromHttp();
+
+ // 显式传 false = 调用方已知未连接:不暴露失败态,且不去问凭证库。
+ authState.loggedIn = true;
+ expect(getAnthropicModelDiscoveryFailure(false)).toBeNull();
+ // 显式传 true 同理:即便凭证库此刻说未登录,也按调用方给的连接态走。
+ authState.loggedIn = false;
+ expect(getAnthropicModelDiscoveryFailure(true)?.kind).toBe('network');
+ // 不传时保持原有的自查语义。
+ expect(getAnthropicModelDiscoveryFailure()).toBeNull();
+ });
+
it('答复正常但没有可用模型归 empty', async () => {
vi.stubGlobal(
'fetch',
diff --git a/apps/desktop/src/main/maker-host/createDesktopProviderService.ts b/apps/desktop/src/main/maker-host/createDesktopProviderService.ts
index 749f958ece..2eed4050a1 100644
--- a/apps/desktop/src/main/maker-host/createDesktopProviderService.ts
+++ b/apps/desktop/src/main/maker-host/createDesktopProviderService.ts
@@ -400,8 +400,11 @@ export function getDesktopProviderService(): ProviderService {
genericOAuthConnected: (providerId) => hasGenericOAuthLogin(providerId),
// 动态清单发现的失败归因:目前只有 anthropic 是「清单唯一来源是动态发现」的供应商,
// 拉不到就是零模型 —— UI 要据此讲明失败理由,而不是一直说「正在发现」。
- modelDiscoveryFailure: (providerId) =>
- providerId === 'anthropic' ? getAnthropicModelDiscoveryFailure() : null,
+ //
+ // 连接态直接沿用本次快照已经算好的那个:它内部要读凭证库,macOS 上每读一次就是一个
+ // 同步的 `security` 子进程,同一次 listProviders 不该为此阻塞主线程两回(PR #548 review)。
+ modelDiscoveryFailure: (providerId, connected) =>
+ providerId === 'anthropic' ? getAnthropicModelDiscoveryFailure(connected) : null,
});
return singleton;
}
diff --git a/apps/desktop/src/main/maker-host/model-discovery/anthropic.ts b/apps/desktop/src/main/maker-host/model-discovery/anthropic.ts
index 477dd7ca03..bb25636067 100644
--- a/apps/desktop/src/main/maker-host/model-discovery/anthropic.ts
+++ b/apps/desktop/src/main/maker-host/model-discovery/anthropic.ts
@@ -884,9 +884,17 @@ function notifyFailureChanged(): void {
*
* 未登录时一律返回 null:没有授权就谈不上「发现失败」,那种状态下 UI 该讲的是「去连接」,
* 而不是把上一个账号留下的失败理由摆给新用户看。
+ *
+ * `knownConnected` 让调用方交出同一次快照里**已经算好**的连接态。macOS 上 hasClaudeAiOAuth()
+ * 会同步 `execFileSync('security', ...)` 读一次 Keychain —— listProviders 先在 connection 回调
+ * 里读过一遍,这里再读一遍就是白白让 Electron 主线程多阻塞一个子进程,而供应商列表还会随
+ * PROVIDER_CHANGED 反复重取(PR #548 review)。
*/
-export function getAnthropicModelDiscoveryFailure(): ProviderModelDiscoveryFailure | null {
- if (!hasClaudeAiOAuth()) return null;
+export function getAnthropicModelDiscoveryFailure(
+ knownConnected?: boolean,
+): ProviderModelDiscoveryFailure | null {
+ const connected = knownConnected ?? hasClaudeAiOAuth();
+ if (!connected) return null;
return lastFailure;
}
@@ -961,10 +969,22 @@ export function refreshAnthropicModelsFromHttp(options?: {
);
}
entries.push(...body.data);
- url =
- body.has_more && typeof body.last_id === 'string'
- ? `${upstream.replace(/\/+$/, '')}/v1/models?limit=1000&after_id=${encodeURIComponent(body.last_id)}`
- : null;
+ // has_more=true 却没给可用游标 —— 静默收尾会把「只翻了一页的前缀」当成完整清单交出去,
+ // 而它完全可能小到刚好落进 shrink 守卫的放行区间,于是真清单被截断替换、失败态被清、
+ // 也不排重试,用户从此少一半模型且毫无提示(PR #548 review)。这是响应本身不完整,归
+ // upstream 让它重试。
+ if (body.has_more === true) {
+ if (typeof body.last_id !== 'string' || body.last_id.length === 0) {
+ throw new DiscoveryResponseError(
+ `has_more=true without a usable last_id cursor (got ${
+ body.last_id === undefined ? 'missing' : typeof body.last_id
+ })`,
+ );
+ }
+ url = `${upstream.replace(/\/+$/, '')}/v1/models?limit=1000&after_id=${encodeURIComponent(body.last_id)}`;
+ } else {
+ url = null;
+ }
}
} catch (err) {
// 失败不清列表:现值(含磁盘缓存)是上次成功的真数据;SDK 通道随后仍会精化。
diff --git a/apps/desktop/src/main/maker-host/provider-service.ts b/apps/desktop/src/main/maker-host/provider-service.ts
index cef8b07cde..01a2352b0c 100644
--- a/apps/desktop/src/main/maker-host/provider-service.ts
+++ b/apps/desktop/src/main/maker-host/provider-service.ts
@@ -57,8 +57,14 @@ export interface ProviderServiceDeps {
/**
* 动态清单发现的最近一次失败(生产 = anthropic 的 getAnthropicModelDiscoveryFailure)。
* 只有「清单唯一来源是动态发现」的供应商需要,缺席 = 该供应商没有这种失败态。
+ *
+ * `connected` 是本次快照刚算出的连接态,直接传下去供实现复用:这些判定往往要读凭证库
+ * (macOS 上是一次同步的 Keychain 子进程),同一次 listProviders 里不该读第二遍。
*/
- modelDiscoveryFailure?: (providerId: string) => ProviderModelDiscoveryFailure | null;
+ modelDiscoveryFailure?: (
+ providerId: string,
+ connected: boolean,
+ ) => ProviderModelDiscoveryFailure | null;
}
export interface ProviderService {
@@ -113,7 +119,7 @@ export function createProviderService(deps: ProviderServiceDeps): ProviderServic
const discoveryFailures: ModelDiscoveryFailureState = {};
if (deps.modelDiscoveryFailure) {
for (const p of catalog.providers) {
- const failure = deps.modelDiscoveryFailure(p.id);
+ const failure = deps.modelDiscoveryFailure(p.id, connected[p.id] === true);
if (failure) discoveryFailures[p.id] = failure;
}
}
diff --git a/apps/desktop/src/main/maker-ipc/title.ts b/apps/desktop/src/main/maker-ipc/title.ts
index c1c80df050..9cfbd26f3f 100644
--- a/apps/desktop/src/main/maker-ipc/title.ts
+++ b/apps/desktop/src/main/maker-ipc/title.ts
@@ -235,21 +235,27 @@ function parseAutoTitleRequest(raw: unknown): SessionAutoTitleRequest {
}
export function registerMakerTitleIpc(): void {
+ // 这两条通道读供应商快照时会放行本机绑定自愈(写绑定文件、并为 Anthropic 起一次带凭证的
+ // 清单发现),与下面的 AUTO_TITLE 同属特权入口,守卫口径也应当一致 —— 原先只有 AUTO_TITLE
+ // 做了 sender 断言(PR #548 review)。两者都不在 device-link allowlist 里,可以直接用会抛的
+ // 守卫。
ipcMain.handle(
MAKER_INVOKE.GENERATE_TITLE,
async (
- _event: Electron.IpcMainInvokeEvent,
+ event: Electron.IpcMainInvokeEvent,
{ message, agentKind, sessionId }: { message: string; agentKind: AgentKind; sessionId?: string },
): Promise<{ title: string | null }> => {
+ assertTrustedAppRendererEvent(event);
return { title: await generateMakerSessionTitle(message, agentKind, sessionId) };
},
);
ipcMain.handle(
MAKER_INVOKE.REGENERATE_TITLE,
async (
- _event: Electron.IpcMainInvokeEvent,
+ event: Electron.IpcMainInvokeEvent,
{ sessionId }: { sessionId: string },
): Promise<{ title: string | null }> => {
+ assertTrustedAppRendererEvent(event);
return { title: await regenerateMakerSessionTitle(sessionId) };
},
);