Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(search): Cindy AI 搜索把余额耗尽(429 + ExceededBudget)误报为限流,改用共享额度判定并给充值引导 (#4024) (#4026)
fico-hub25d08fcd6fe9
diff --git a/apps/desktop/src/main/mcp-integrations/__tests__/cindyProxySearch.test.ts b/apps/desktop/src/main/mcp-integrations/__tests__/cindyProxySearch.test.tsindex 23c59a4747..f8d1b501e1 100644--- a/apps/desktop/src/main/mcp-integrations/__tests__/cindyProxySearch.test.ts+++ b/apps/desktop/src/main/mcp-integrations/__tests__/cindyProxySearch.test.ts@@ -168,7 +168,22 @@ describe('cindyProxySearch', () => {{ status: 401, body: { error: 'insufficient scope' }, code: 'AUTH_REJECTED' },{ status: 403, body: { error: 'insufficient permissions' }, code: 'AUTH_REJECTED' },{ status: 402, body: { error: 'insufficient balance' }, code: 'QUOTA_EXHAUSTED' },+ // 网关预算闸的真实形态:HTTP 429 + ExceededBudget(#4024)—— 是余额耗尽,不是限流。+ {+ status: 429,+ body: { error: 'ExceededBudget', principal: 'aigw:user-1', spend: 12.34, budget: 10 },+ code: 'QUOTA_EXHAUSTED',+ },+ { status: 429, body: { error: 'budget_exceeded' }, code: 'QUOTA_EXHAUSTED' },+ // 瞬时限流照旧,即便正文带 credit 之类宽松措辞也不得升级成「去充值」。{ status: 429, body: { error: 'rate limit' }, code: 'RATE_LIMITED' },+ { status: 429, body: { error: 'Too Many Requests' }, code: 'RATE_LIMITED' },+ {+ status: 429,+ body: { error: 'rate limit exceeded, credits refill in 60s' },+ code: 'RATE_LIMITED',+ },+ { status: 404, body: { error: 'not found' }, code: 'NOT_CONFIGURED' },{ status: 503, body: { error: 'unavailable' }, code: 'UPSTREAM_UNAVAILABLE' },] as const;@@ -271,15 +286,16 @@ describe('cindyProxySearch', () => {const service = createCindyProxySearchService({getBaseUrl: () => 'https://gateway.example.test',getApiKey: () => 'test-key',- fetchImpl: vi.fn(async () =>- ({- ok: true,- status: 200,- headers: new Headers({ 'x-request-id': 'request-body-failed' }),- text: vi.fn(async () => {- throw new Error('stream interrupted');- }),- }) as unknown as Response,+ fetchImpl: vi.fn(+ async () =>+ ({+ ok: true,+ status: 200,+ headers: new Headers({ 'x-request-id': 'request-body-failed' }),+ text: vi.fn(async () => {+ throw new Error('stream interrupted');+ }),+ }) as unknown as Response,) as unknown as typeof fetch,log: { info: vi.fn(), warn },});@@ -332,4 +348,90 @@ describe('cindyProxySearch', () => {expect(logged).not.toContain('sensitive user query');expect(logged).not.toContain('super-secret-test-key');});++ it('余额耗尽(429 + ExceededBudget)给出充值引导,日志只留允许名单内的结构化摘要', async () => {+ const warn = vi.fn();+ const service = createCindyProxySearchService({+ getBaseUrl: () => 'https://gateway.example.test',+ getApiKey: () => 'super-secret-test-key',+ fetchImpl: vi.fn(async () =>+ response(+ {+ error: 'ExceededBudget',+ principal: 'aigw:internal-user-42',+ spend: 12.34,+ budget: 10,+ token: 'placeholder-credential-must-not-be-logged',+ echo: 'sensitive user query',+ padding: 'x'.repeat(600),+ },+ { status: 429, headers: { 'x-request-id': 'req-quota-1' } },+ ),+ ) as unknown as typeof fetch,+ log: { info: vi.fn(), warn },+ });++ const outcome = await service.search({ query: 'sensitive user query', limit: 5 });+ expect(outcome).toMatchObject({+ ok: false,+ errorCode: 'QUOTA_EXHAUSTED',+ status: 429,+ requestId: 'req-quota-1',+ });+ expect(outcome.ok === false && outcome.message).toContain('余额不足');+ expect(outcome.ok === false && outcome.message).toContain('充值');++ expect(warn).toHaveBeenCalledTimes(1);+ const [, meta] = warn.mock.calls[0] as [string, Record<string, unknown>];+ expect(meta.errorCode).toBe('QUOTA_EXHAUSTED');+ // 摘要是允许名单式的结构化字段,不是任意正文:principal / 凭证 / 回显字段一律不进日志。+ expect(meta.bodyDigest).toEqual({+ json: true,+ length: expect.any(Number),+ error: 'ExceededBudget',+ spend: 12.34,+ budget: 10,+ });+ const logged = JSON.stringify(warn.mock.calls);+ expect(logged).not.toContain('internal-user-42');+ expect(logged).not.toContain('aigw:');+ expect(logged).not.toContain('placeholder-credential-must-not-be-logged');+ expect(logged).not.toContain('sensitive user query');+ expect(logged).not.toContain('super-secret-test-key');+ expect(logged).not.toContain('xxxx');+ });++ it('非标识形态的 error 文本与非 JSON 正文只记长度,不落盘内容', async () => {+ const cases: Array<{ body: BodyInit; expected: Record<string, unknown> }> = [+ {+ body: JSON.stringify({+ error: {+ type: 'rate_limit_error',+ message: 'slow down, user asked: sensitive user query',+ },+ }),+ expected: { json: true, error: 'rate_limit_error', type: 'rate_limit_error' },+ },+ {+ body: JSON.stringify({ error: 'insufficient balance for sensitive user query' }),+ expected: { json: true },+ },+ { body: '<html>Bad Gateway sensitive user query</html>', expected: { json: false } },+ ];+ for (const testCase of cases) {+ const warn = vi.fn();+ const service = createCindyProxySearchService({+ getBaseUrl: () => 'https://gateway.example.test',+ getApiKey: () => 'test-key',+ fetchImpl: vi.fn(+ async () => new Response(testCase.body, { status: 502 }),+ ) as unknown as typeof fetch,+ log: { info: vi.fn(), warn },+ });+ await service.search({ query: 'sensitive user query', limit: 5 });+ const [, meta] = warn.mock.calls[0] as [string, Record<string, unknown>];+ expect(meta.bodyDigest).toMatchObject(testCase.expected);+ expect(JSON.stringify(warn.mock.calls)).not.toContain('sensitive user query');+ }+ });});diff --git a/apps/desktop/src/main/mcp-integrations/cindyProxySearch.ts b/apps/desktop/src/main/mcp-integrations/cindyProxySearch.tsindex 3da37d70c2..7bcccfe96d 100644--- a/apps/desktop/src/main/mcp-integrations/cindyProxySearch.ts+++ b/apps/desktop/src/main/mcp-integrations/cindyProxySearch.ts@@ -3,6 +3,10 @@ import { createLogger } from '../logger.js';import { effectiveXdGatewayBaseUrl } from '../model-access/effectiveEndpoint.js';import { getProviderSecretStore } from '../secrets/providerSecretStore.js';import { outboundFetch } from '../maker-host/outbound-fetch.js';+import {+ matchesDeterministicUsageExhaustionText,+ redactSensitiveText,+} from '@cindy/maker-shared/error-redaction';export const CINDY_SEARCH_MODEL_NAME = 'cindy/web-search';const CINDY_SEARCH_WEB_TOOL_TYPE = 'web_search_20250305';@@ -101,13 +105,22 @@ function classifyHttpFailure(message: 'Cindy AI 搜索鉴权失败,请重新登录或稍后再试',};}+ // 余额耗尽(#4024):网关的预算闸用 HTTP 429 + `ExceededBudget` 正文拒绝,与瞬时限流+ // 共用状态码。先按仓库共享的严格判定(与对话 Error Banner / 终端限流重试同一 SSoT)+ // 识别明确的额度耗尽,再落普通 429 —— 否则用户会被引导「稍后再试」而永远不会恢复。+ // 429 只认严格信号:宽松措辞(quota/credit/balance…)留给非 429 状态,避免把+ // 「rate limit exceeded, credits refill soon」这类瞬时限流误判成需要充值。+ const deterministicExhaustion = matchesDeterministicUsageExhaustionText(body.slice(0, 1024));const looksLikeQuota =- status === 402 ||- /(?:quota|credit|balance|insufficient|exhausted|spend limit)/.test(normalized);+ status === 429+ ? deterministicExhaustion+ : status === 402 ||+ deterministicExhaustion ||+ /(?:quota|credit|balance|insufficient|exhausted|spend limit)/.test(normalized);if (looksLikeQuota) {return {errorCode: 'QUOTA_EXHAUSTED',- message: 'Cindy AI 搜索额度不足,请稍后再试或在插件设置中改用自己的搜索渠道',+ message: 'Cindy AI 余额不足,请充值后再试,或在插件设置中改用自己的搜索渠道',};}if (status === 404) {@@ -151,6 +164,68 @@ function classifyHttpFailure(};}+const DIGEST_TOKEN_MAX_CHARS = 64;+const DIGEST_TOKEN_PATTERN = /^[A-Za-z0-9_.:-]{1,64}$/;++/** 结构化诊断摘要:只收允许名单里的字段,绝不把任意上游正文落盘。 */+export interface SearchResponseBodyDigest {+ /** 正文是否为 JSON 对象;非 JSON 只记长度,不记内容。 */+ json: boolean;+ /** 原始正文字符数(用于判断是否被网关截断/是否为空)。 */+ length: number;+ /** 顶层 `error`(字符串或对象的 `code`/`type`)——仅当形如 `ExceededBudget` 的短标识才记录。 */+ error?: string;+ /** 顶层 / `error.` 下的 `code`、`type`,同样只收短标识。 */+ code?: string;+ type?: string;+ /** 网关预算闸附带的数值,用于复盘「预算闸拒绝」而非「真限流」。 */+ spend?: number;+ budget?: number;+}++function digestToken(value: unknown): string | undefined {+ if (typeof value !== 'string') return undefined;+ const trimmed = value.trim();+ if (!DIGEST_TOKEN_PATTERN.test(trimmed)) return undefined;+ // 标识形态本不该含凭证,再过一遍共享脱敏兜底(例如 `key:...` 形态)。+ return redactSensitiveText(trimmed).slice(0, DIGEST_TOKEN_MAX_CHARS);+}++function digestNumber(value: unknown): number | undefined {+ return typeof value === 'number' && Number.isFinite(value) ? value : undefined;+}++/**+ * 非 2xx 响应体的诊断摘要(#4024):区分「预算闸拒绝」与「真限流」靠的是正文,但上游+ * 正文可能回显 query、以非标准字段携带凭证或含其他敏感内容,通用脱敏器不保证识别 ——+ * 因此不记任意文本,只按允许名单抽取短标识与数值,其余只记长度。+ */+function responseBodyDigest(body: string): SearchResponseBodyDigest {+ const digest: SearchResponseBodyDigest = { json: false, length: body.length };+ let decoded: unknown;+ try {+ decoded = JSON.parse(body);+ } catch {+ return digest;+ }+ if (!isRecord(decoded)) return digest;+ digest.json = true;+ const errorField = decoded.error;+ const errorRecord = isRecord(errorField) ? errorField : null;+ const error =+ digestToken(errorField) ?? digestToken(errorRecord?.code) ?? digestToken(errorRecord?.type);+ const code = digestToken(decoded.code) ?? digestToken(errorRecord?.code);+ const type = digestToken(decoded.type) ?? digestToken(errorRecord?.type);+ const spend = digestNumber(decoded.spend) ?? digestNumber(errorRecord?.spend);+ const budget = digestNumber(decoded.budget) ?? digestNumber(errorRecord?.budget);+ if (error !== undefined) digest.error = error;+ if (code !== undefined) digest.code = code;+ if (type !== undefined) digest.type = type;+ if (spend !== undefined) digest.spend = spend;+ if (budget !== undefined) digest.budget = budget;+ return digest;+}+function isRecord(value: unknown): value is Record<string, unknown> {return typeof value === 'object' && value !== null;}@@ -403,6 +478,9 @@ export function createCindyProxySearchService(deps: CindyProxySearchDeps): CindylatencyMs,...(requestId ? { requestId } : {}),errorCode: failure.errorCode,+ // 允许名单式的结构化正文摘要,只进本机诊断日志(不回传插件 / 用户):+ // 区分「预算闸拒绝」与「真限流」靠的正是正文,此前日志只有状态码无从复盘(#4024)。+ bodyDigest: responseBodyDigest(body),});return {ok: false,