Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(model-access): 处理 #6148 review:Key 失效重触发改为 5 分钟窗口,401 先于读正文上报
fmfsaisaibc8b1346d997
diff --git a/apps/desktop/src/main/mcp-integrations/__tests__/cindyProxySearch.test.ts b/apps/desktop/src/main/mcp-integrations/__tests__/cindyProxySearch.test.tsindex ca891f217a..06273b7c2f 100644--- a/apps/desktop/src/main/mcp-integrations/__tests__/cindyProxySearch.test.ts+++ b/apps/desktop/src/main/mcp-integrations/__tests__/cindyProxySearch.test.ts@@ -163,6 +163,31 @@ describe('cindyProxySearch', () => {expect(fetchImpl).not.toHaveBeenCalled();});+ it('网关 401 时即使读正文失败也上报本次请求携带的 Key', async () => {+ const onKeyRejected = vi.fn();+ const brokenBody = {+ ok: false,+ status: 401,+ headers: new Headers(),+ text: async () => {+ throw new TypeError('terminated');+ },+ } as unknown as Response;+ const service = createCindyProxySearchService({+ getBaseUrl: () => 'https://gateway.example.test',+ getApiKey: () => 'search-key-sent',+ onKeyRejected,+ fetchImpl: vi.fn(async () => brokenBody) as unknown as typeof fetch,+ });+ await expect(service.search({ query: 'Cindy', limit: 5 })).resolves.toMatchObject({+ ok: false,+ errorCode: 'UPSTREAM_UNAVAILABLE',+ status: 401,+ });+ expect(onKeyRejected).toHaveBeenCalledTimes(1);+ expect(onKeyRejected).toHaveBeenCalledWith('search-key-sent');+ });+it('网关 401 时上报本次请求携带的 Key,其他状态不上报', async () => {for (const [status, expectedReports] of [[401, 1], [403, 0], [429, 0], [200, 0]] as const) {const onKeyRejected = vi.fn();diff --git a/apps/desktop/src/main/mcp-integrations/cindyProxySearch.ts b/apps/desktop/src/main/mcp-integrations/cindyProxySearch.tsindex 5dd2ff323b..9dbb14ea32 100644--- a/apps/desktop/src/main/mcp-integrations/cindyProxySearch.ts+++ b/apps/desktop/src/main/mcp-integrations/cindyProxySearch.ts@@ -447,6 +447,8 @@ export function createCindyProxySearchService(deps: CindyProxySearchDeps): Cindy}const requestId = requestIdOf(response);+ // 拿到状态码就上报,不依赖后续正文读取成功(读正文中断时也不能漏报)。+ if (response.status === 401) deps.onKeyRejected?.(apiKey);let body: string;try {body = await response.text();@@ -472,7 +474,6 @@ export function createCindyProxySearchService(deps: CindyProxySearchDeps): Cindy}const latencyMs = Date.now() - startedAt;if (!response.ok) {- if (response.status === 401) deps.onKeyRejected?.(apiKey);const failure = classifyHttpFailure(response.status, body);deps.log?.warn('cindy search request rejected', {logicalProvider: 'cindy',diff --git a/apps/desktop/src/main/model-access/__tests__/gatewayKeyRejection.test.ts b/apps/desktop/src/main/model-access/__tests__/gatewayKeyRejection.test.tsindex f0cde02d67..9a1eeddf83 100644--- a/apps/desktop/src/main/model-access/__tests__/gatewayKeyRejection.test.ts+++ b/apps/desktop/src/main/model-access/__tests__/gatewayKeyRejection.test.ts@@ -31,6 +31,7 @@ vi.mock('../credentialsStore.js', () => ({import {bearerKeyFromHeaders,createGatewayKeyRejectionReporter,+ CINDY_KEY_REJECTION_RETRIGGER_WINDOW_MS,installCindyGatewayKeyRejectionReporter,reportCindyGatewayKeyRejected,reportCindyGatewayKeyRejectedForCurrentKey,@@ -43,6 +44,7 @@ const KEY_B = 'sk-test-invalid-b';function harness(overrides: Partial<GatewayKeyRejectionDeps> = {}) {let currentKey: string | null = KEY_A;+ let clock = 1_000_000;const requestSync = vi.fn(async () => undefined);const log = { info: vi.fn(), warn: vi.fn() };const deps: GatewayKeyRejectionDeps = {@@ -52,6 +54,7 @@ function harness(overrides: Partial<GatewayKeyRejectionDeps> = {}) {readCurrentKey: () => currentKey,requestSync,log,+ now: () => clock,...overrides,};const reporter = createGatewayKeyRejectionReporter(deps);@@ -63,9 +66,14 @@ function harness(overrides: Partial<GatewayKeyRejectionDeps> = {}) {setCurrentKey: (key: string | null) => {currentKey = key;},+ advance: (ms: number) => {+ clock += ms;+ },};}+const WINDOW = CINDY_KEY_REJECTION_RETRIGGER_WINDOW_MS;+describe('createGatewayKeyRejectionReporter', () => {it('re-syncs once when the current server-issued key is rejected', () => {const h = harness();@@ -105,6 +113,27 @@ describe('createGatewayKeyRejectionReporter', () => {expect(h.requestSync).not.toHaveBeenCalled();});+ it('triggers at most once per key within the window, and again after it (rule B)', () => {+ const h = harness();+ h.report(KEY_A);+ h.advance(WINDOW - 1);+ h.report(KEY_A);+ expect(h.requestSync).toHaveBeenCalledTimes(1);+ // 同步失败(failed resolve)或服务端暂时原样返回旧 Key 后,窗口过去即可自行恢复。+ h.advance(1);+ h.report(KEY_A);+ h.report(KEY_A);+ expect(h.requestSync).toHaveBeenCalledTimes(2);+ });++ it('treats a clock moved backwards as an expired window', () => {+ const h = harness();+ h.report(KEY_A);+ h.advance(-1);+ h.report(KEY_A);+ expect(h.requestSync).toHaveBeenCalledTimes(2);+ });+it('triggers once per key and again after the key changes (rule B)', () => {const h = harness();h.report(KEY_A);@@ -175,6 +204,29 @@ describe('reportCurrentKey (agent turns that do not reveal the key they used)',expect(h.requestSync).toHaveBeenCalledTimes(1);});+ it('applies the same window to repeated agent failures', () => {+ const h = harness();+ h.reportCurrent();+ h.advance(WINDOW - 1);+ h.reportCurrent();+ expect(h.requestSync).toHaveBeenCalledTimes(1);+ h.advance(1);+ h.reportCurrent();+ h.reportCurrent();+ expect(h.requestSync).toHaveBeenCalledTimes(2);+ });++ it('lets an accurate report re-trigger only after its own window', () => {+ const h = harness();+ h.report(KEY_A);+ h.reportCurrent(); // 准确上报的窗口内:跳过+ expect(h.requestSync).toHaveBeenCalledTimes(1);+ h.advance(WINDOW);+ h.reportCurrent(); // 窗口已过:智能体路径可再触发+ h.report(KEY_A); // 智能体路径没占用准确上报的机会+ expect(h.requestSync).toHaveBeenCalledTimes(3);+ });+it('can trigger again once the current key changes', () => {const h = harness();h.reportCurrent();diff --git a/apps/desktop/src/main/model-access/gatewayKeyRejection.ts b/apps/desktop/src/main/model-access/gatewayKeyRejection.tsindex 542986418b..5b31f2ee89 100644--- a/apps/desktop/src/main/model-access/gatewayKeyRejection.ts+++ b/apps/desktop/src/main/model-access/gatewayKeyRejection.ts@@ -15,11 +15,16 @@ import { getModelAccessCredentialsStore } from './credentialsStore.js';* 调用方(辅助模型、搜索、媒体、向量、额度、语音、智能体终态)在 401 时把**本次请求* 实际携带的 Key** 交给这里,由这里决定是否触发一次凭据同步。*- * 规则刻意只有两条,不加冷却 / 退避 / 计时器:+ * 规则只有两条,不加退避或定时检查:* - 规则 A:上报的 Key 必须等于本机当前保存的 Key——旧请求的迟到 401 不能为已换上的* 新 Key 再触发同步;- * - 规则 B:每把 Key 只触发一次——内存只记上一次触发的 Key 的 SHA-256 指纹(不存明文),- * 同一把 Key 的后续 401 全部忽略,换了 Key 之后才可能再次触发。+ * - 规则 B:同一把 Key 在 `CINDY_KEY_REJECTION_RETRIGGER_WINDOW_MS`(5 分钟)内最多触发+ * 一次——内存只记上一次触发的 Key 的 SHA-256 指纹(不存明文)和触发时间,换了 Key 立即+ * 可再触发。+ * 为什么按时间窗口而不是「每把 Key 永远只触发一次」:`sync()` 在服务端暂时不可用时会在内部+ * 重试后以 failed 状态结束(不 reject),服务端也可能在网关鉴权缓存窗口内原样返回这把稍后+ * 才被撤销的 Key。永久标记会让这把 Key 之后的 401 一律被忽略,直到重启;窗口过后再允许+ * 触发一次即可自行恢复。窗口内不重复,则不会对着挂掉的服务端或误报的 401 反复请求。* 同步用 `sync()` 而不是 `retry()`:它尊重 unsupported / disabled 终态,自带 single-flight* 与内部重试;失败沿用现有同步表现,这里不新增提示。*@@ -45,17 +50,22 @@ export interface GatewayKeyRejectionDeps {info(message: string): void;warn(message: string): void;};+ /** 当前时间(毫秒),默认 Date.now;测试注入假时钟。 */+ now?(): number;}+/** 同一把 Key 两次触发同步之间的最短间隔。 */+export const CINDY_KEY_REJECTION_RETRIGGER_WINDOW_MS = 5 * 60_000;+/*** 上报器,两个入口:- * - `reportUsedKey`:调用方知道请求实际携带的 Key(规则 A + 规则 B,触发即标记- * `lastTriggeredFingerprint`);- * - `reportCurrentKey`:调用方不知道用的是哪把 Key(智能体回合终态)。当前 Key 已被- * 准确上报触发过,或已被本入口触发过,都跳过;本入口只标记自己的- * `lastCurrentKeyTriggeredFingerprint`,**不标记**准确上报的记录——否则旧 Key 进程- * (未重启的 Codex、老任务)的迟到 401 会把新 Key 唯一的触发机会用掉,新 Key 真失效时- * 准确上报反被忽略。单独记录则保证卡在旧 Key 上的任务对同一把当前 Key 只多触发 1 次。+ * - `reportUsedKey`:调用方知道请求实际携带的 Key(规则 A + 规则 B,触发即记入+ * `lastTriggered`);+ * - `reportCurrentKey`:调用方不知道用的是哪把 Key(智能体回合终态)。当前 Key 在窗口内+ * 已被准确上报触发过,或已被本入口触发过,都跳过;本入口只记自己的+ * `lastCurrentKeyTriggered`,**不写**准确上报的记录——否则旧 Key 进程(未重启的 Codex、+ * 老任务)的迟到 401 会占用新 Key 的触发机会,新 Key 真失效时准确上报反被忽略。单独记录+ * 则保证卡在旧 Key 上的任务对同一把当前 Key 每个窗口只多触发 1 次。*/export interface GatewayKeyRejectionReporter {reportUsedKey(usedKey: string | null | undefined): void;@@ -66,13 +76,30 @@ function keyFingerprint(key: string): string {return createHash('sha256').update(key).digest('hex');}-/** 组装一个上报器;内存里只保留上一次已触发 Key 的指纹。 */+interface TriggerRecord {+ fingerprint: string;+ at: number;+}++/** 组装一个上报器;内存里只保留上一次已触发 Key 的指纹与触发时间。 */export function createGatewayKeyRejectionReporter(deps: GatewayKeyRejectionDeps,): GatewayKeyRejectionReporter {- let lastTriggeredFingerprint: string | null = null;+ const now = deps.now ?? Date.now;+ let lastTriggered: TriggerRecord | null = null;/** 只供 reportCurrentKey 读写;reportUsedKey 既不读也不写它。 */- let lastCurrentKeyTriggeredFingerprint: string | null = null;+ let lastCurrentKeyTriggered: TriggerRecord | null = null;++ /** 同一指纹且仍在窗口内(时钟回拨视为窗口已过,避免长时间卡住)。 */+ const triggeredRecently = (+ record: TriggerRecord | null,+ fingerprint: string,+ at: number,+ ): boolean => {+ if (!record || record.fingerprint !== fingerprint) return false;+ const elapsed = at - record.at;+ return elapsed >= 0 && elapsed < CINDY_KEY_REJECTION_RETRIGGER_WINDOW_MS;+ };const warn = (prefix: string, error: unknown): void => {deps.log?.warn(`${prefix}: ${error instanceof Error ? error.message : String(error)}`);@@ -108,21 +135,23 @@ export function createGatewayKeyRejectionReporter(const currentKey = currentServerKey();// 规则 A:只认本机当前这把 Key 的拒绝。if (!currentKey || currentKey !== usedKey) return;- // 规则 B:同一把 Key 只触发一次。+ // 规则 B:同一把 Key 在窗口内最多触发一次。const fingerprint = keyFingerprint(usedKey);- if (fingerprint === lastTriggeredFingerprint) return;- lastTriggeredFingerprint = fingerprint;+ const at = now();+ if (triggeredRecently(lastTriggered, fingerprint, at)) return;+ lastTriggered = { fingerprint, at };triggerSync();}),reportCurrentKey: () => guarded(() => {const currentKey = currentServerKey();if (!currentKey) return;- // 当前 Key 已被准确上报或本入口同步过一次:不再重复请求。旧 Key 进程在当前 Key 有效时- // 会每轮失败,服务端每次都原样返回当前 Key,不单独记录就会每条消息都请求一次。+ // 当前 Key 在窗口内已被准确上报或本入口同步过:不再重复请求。旧 Key 进程在当前 Key+ // 有效时会每轮失败,服务端每次都原样返回当前 Key,不单独记录就会每条消息都请求一次。const fingerprint = keyFingerprint(currentKey);- if (fingerprint === lastTriggeredFingerprint) return;- if (fingerprint === lastCurrentKeyTriggeredFingerprint) return;- lastCurrentKeyTriggeredFingerprint = fingerprint;+ const at = now();+ if (triggeredRecently(lastTriggered, fingerprint, at)) return;+ if (triggeredRecently(lastCurrentKeyTriggered, fingerprint, at)) return;+ lastCurrentKeyTriggered = { fingerprint, at };triggerSync();}),};@@ -166,7 +195,7 @@ export function reportCindyGatewayKeyRejected(usedKey: string | null | undefined/*** 不知道请求实际用哪把 Key 时的上报(智能体回合因 Key 失效终止)。按本机当前 Key 判断,- * 当前 Key 已被准确上报触发过则跳过;触发同步但不占用该 Key 的「只触发一次」机会。+ * 当前 Key 在窗口内已被任一入口触发过则跳过;触发同步但不占用准确上报的触发机会。*/export function reportCindyGatewayKeyRejectedForCurrentKey(): void {installedReporter?.reportCurrentKey();diff --git a/apps/desktop/src/main/voice-input/LiteLlmTranscriptionProvider.ts b/apps/desktop/src/main/voice-input/LiteLlmTranscriptionProvider.tsindex 01d6840f9a..5e76f50a32 100644--- a/apps/desktop/src/main/voice-input/LiteLlmTranscriptionProvider.ts+++ b/apps/desktop/src/main/voice-input/LiteLlmTranscriptionProvider.ts@@ -115,6 +115,9 @@ export async function transcribeLiteLlmAudioFile(input: LiteLlmAudioFileTranscribody: form,});+ // proxyApiKey is the Cindy AI key sent to the Cindy gateway. Report on the status+ // before reading the body so an interrupted body read cannot drop the report.+ if (response.status === 401) reportCindyGatewayKeyRejected(input.proxyApiKey);const text = await response.text();let parsed: unknown;try {@@ -124,8 +127,6 @@ export async function transcribeLiteLlmAudioFile(input: LiteLlmAudioFileTranscri}if (!response.ok) {- // proxyApiKey is the Cindy AI key sent to the Cindy gateway.- if (response.status === 401) reportCindyGatewayKeyRejected(input.proxyApiKey);throw new Error(transcriptionErrorMessage(parsed, text, response.status));}diff --git a/apps/desktop/src/main/voice-input/__tests__/LiteLlmTranscriptionProvider.test.ts b/apps/desktop/src/main/voice-input/__tests__/LiteLlmTranscriptionProvider.test.tsindex 5571aa798b..f2f1e2d29e 100644--- a/apps/desktop/src/main/voice-input/__tests__/LiteLlmTranscriptionProvider.test.ts+++ b/apps/desktop/src/main/voice-input/__tests__/LiteLlmTranscriptionProvider.test.ts@@ -22,6 +22,24 @@ describe('LiteLlmTranscriptionProvider', () => {reportKeyRejectedMock.mockReset();});+ it('reports a 401 even when reading the response body fails', async () => {+ outboundFetchMock.mockResolvedValue({+ ok: false,+ status: 401,+ text: async () => {+ throw new TypeError('terminated');+ },+ });++ await expect(transcribeLiteLlmAudioFile({+ proxyApiKey: 'sk-sent',+ baseUrl: 'https://gateway.example',+ bytes: Buffer.from([1, 2, 3]),+ })).rejects.toThrow('terminated');+ expect(reportKeyRejectedMock).toHaveBeenCalledTimes(1);+ expect(reportKeyRejectedMock).toHaveBeenCalledWith('sk-sent');+ });+it.each([[401, 1],[403, 0],