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): cc 路由识别 provider-oauth 占位 key,权限 Auto 分类器不再吃确定性 401(部分修复 #831) (#916)

fico-huba4c8e4cbce00
diff --git a/apps/desktop/src/main/maker-host/__tests__/claudeProxyScopeGate.test.ts b/apps/desktop/src/main/maker-host/__tests__/claudeProxyScopeGate.test.ts
index f879cf11c3..56816c1fd8 100644
--- a/apps/desktop/src/main/maker-host/__tests__/claudeProxyScopeGate.test.ts
+++ b/apps/desktop/src/main/maker-host/__tests__/claudeProxyScopeGate.test.ts
@@ -96,6 +96,30 @@ describe('cc routingTransform — xAI 会话的辅助请求回落默认路由 (i
expect(decision).toBeNull();
});
+ it('claude-haiku 分类器请求(provider-oauth spawn 带占位 x-api-key)→ 换网关 key,不 passthrough (#831)', () => {
+ // codex→cc 切换后的 openai/xai 来源会话:cc 子进程 env 里是占位 key,分类器请求带着它
+ // 落到 ② 段。占位 key 不是可用凭证,按「无凭证」处理换网关 key;此前被误判成
+ // gateway-spawn passthrough → 网关确定性 401 → 首次权限请求即 auto→ask 降级。
+ const transform = createModelRoutingTransform();
+ const decision = transform(
+ { model: 'claude-haiku-4-5-20251001' },
+ ctxWith({ ...SESSION_HEADER, 'x-api-key': 'xdt-provider-auth-placeholder-key' }),
+ );
+ expect(decision).toEqual({
+ headerOverride: { 'x-api-key': 'sk-gw', authorization: 'Bearer sk-gw' },
+ });
+ });
+
+ it('占位 x-api-key 且无网关 key → 维持 passthrough(与改动前行为一致,上游 401)', () => {
+ gatewayKey = null;
+ const transform = createModelRoutingTransform();
+ const decision = transform(
+ { model: 'claude-haiku-4-5-20251001' },
+ ctxWith({ ...SESSION_HEADER, 'x-api-key': 'xdt-provider-auth-placeholder-key' }),
+ );
+ expect(decision).toBeNull();
+ });
+
it('claude-haiku 分类器请求(无网关 key 的 oauth-spawn)→ 直连 Anthropic 订阅', () => {
gatewayKey = null;
const transform = createModelRoutingTransform();
diff --git a/apps/desktop/src/main/maker-host/anthropic-compat-proxy-host.ts b/apps/desktop/src/main/maker-host/anthropic-compat-proxy-host.ts
index b588cf8994..eec98aebea 100644
--- a/apps/desktop/src/main/maker-host/anthropic-compat-proxy-host.ts
+++ b/apps/desktop/src/main/maker-host/anthropic-compat-proxy-host.ts
@@ -34,7 +34,7 @@ import {
type RoutingTransform,
} from '@cindy/anthropic-compat-proxy';
-import { ANTHROPIC_DIRECT_UPSTREAM, anthropicCatalogModelIds, isAnthropicWireModel } from './claude-gateway-config.js';
+import { ANTHROPIC_DIRECT_UPSTREAM, CLAUDE_PROVIDER_AUTH_PLACEHOLDER_KEY, anthropicCatalogModelIds, isAnthropicWireModel } from './claude-gateway-config.js';
import { getActiveCatalog } from './active-catalog.js';
import { getResponsesBridgeHandler } from './anthropic-responses-bridge-host.js';
import { getSessionEffort, getSessionFastMode } from './session-effort-store.js';
@@ -118,13 +118,13 @@ function isPlainObject(v: unknown): v is Record<string, unknown> {
return typeof v === 'object' && v !== null && !Array.isArray(v);
}
-/** 大小写不敏感地判断请求是否带某 header(且非空)。 */
-function hasHeader(headers: Readonly<Record<string, string>>, name: string): boolean {
+/** 大小写不敏感取 header 值;缺失或空串 → null。 */
+function headerValue(headers: Readonly<Record<string, string>>, name: string): string | null {
const lower = name.toLowerCase();
for (const [k, v] of Object.entries(headers)) {
- if (k.toLowerCase() === lower && typeof v === 'string' && v.length > 0) return true;
+ if (k.toLowerCase() === lower && typeof v === 'string' && v.length > 0) return v;
}
- return false;
+ return null;
}
/**
@@ -207,7 +207,15 @@ export function createModelRoutingTransform(): RoutingTransform {
// providerId=null 时读)——显式选了供应商但被 scope 门放下来的辅助请求(如 xai 会话
// 的 claude-* 分类器调用)不该往表里写死记录。
const recordDefaultRoute = sessionId !== null && getSessionProvider(sessionId) == null;
- if (hasHeader(ctx.headers, 'x-api-key')) {
+ // provider-oauth spawn 的占位 key **不是可用凭证**:scope 门放下来的辅助请求
+ // (如 openai / xai 来源 cc 会话里 CLI 自发的 claude-* 权限分类器调用)带着它
+ // passthrough 会在网关吃确定性 401 → 误触发 auto→ask 降级(#831)。与 codex
+ // ①.5 段 #890 修复同语义:占位 key 按「无可用凭证」处理,走下方换网关 key 的
+ // 默认路由;真实 key(gateway-spawn)照旧 passthrough。
+ const apiKeyHeader = headerValue(ctx.headers, 'x-api-key');
+ const hasUsableApiKey =
+ apiKeyHeader !== null && apiKeyHeader !== CLAUDE_PROVIDER_AUTH_PLACEHOLDER_KEY;
+ if (hasUsableApiKey) {
// gateway-spawn:自带网关 key,passthrough。
if (recordDefaultRoute) recordClaudeSessionRoute(sessionId, 'gateway');
return null;
@@ -218,6 +226,12 @@ export function createModelRoutingTransform(): RoutingTransform {
if (recordDefaultRoute) recordClaudeSessionRoute(sessionId, 'gateway');
return decision;
}
+ if (apiKeyHeader !== null) {
+ // 占位 key 且无网关 key:保持改动前行为(passthrough,上游 401)——下方的
+ // Anthropic 直连支路是给 oauth-spawn 订阅 token 准备的,占位 key 不适用。
+ log.warn('provider-oauth cc spawn 占位 key 且无网关 key; passthrough (预期 401)', { wireModel });
+ return null;
+ }
// 没网关 key:Anthropic 模型只能直连(唯一出路),其余 passthrough + 记一条诊断。
// 判据 = 前缀地板 ∪ 目录 anthropic 供应商模型集合(新家族改 OSS 目录即可,无需发版)。
if (isAnthropicWireModel(wireModel, anthropicCatalogModelIds(getActiveCatalog()))) {
diff --git a/apps/desktop/src/main/maker-host/auth-adapters.ts b/apps/desktop/src/main/maker-host/auth-adapters.ts
index 923191cfd6..bcbb01946e 100644
--- a/apps/desktop/src/main/maker-host/auth-adapters.ts
+++ b/apps/desktop/src/main/maker-host/auth-adapters.ts
@@ -53,6 +53,7 @@ import {
terminateCodexLoginProcess,
} from './codex-auth-state.js';
import { CODEX_GATEWAY_ENV_KEY, CODEX_PROVIDER_OAUTH_PLACEHOLDER_KEY } from './codex-gateway-config.js';
+import { CLAUDE_PROVIDER_AUTH_PLACEHOLDER_KEY } from './claude-gateway-config.js';
import {
clearClaudeAiOAuth,
hasClaudeAiOAuth,
@@ -89,8 +90,10 @@ const log = createLogger('auth-adapters');
* Host-injected provider sessions only need a non-empty credential to pass Claude Code's
* local auth preflight. The loopback proxy replaces it with the selected provider's real
* API key / OAuth token before forwarding the request.
+ * 定义已下沉到 claude-gateway-config.ts(proxy-host 路由识别占位 key 需要它,而本
+ * 文件 import 了 proxy-host,反向 import 会成环);这里 re-export 保持既有消费点。
*/
-export const CLAUDE_PROVIDER_AUTH_PLACEHOLDER_KEY = 'xdt-provider-auth-placeholder-key';
+export { CLAUDE_PROVIDER_AUTH_PLACEHOLDER_KEY } from './claude-gateway-config.js';
/** Codex CLI 的 HOME 目录, auth.json 放在根, sessions 子目录放会话 jsonl。 */
function getCodexHome(): string {
diff --git a/apps/desktop/src/main/maker-host/claude-gateway-config.ts b/apps/desktop/src/main/maker-host/claude-gateway-config.ts
index 982080729d..2a580e6aa4 100644
--- a/apps/desktop/src/main/maker-host/claude-gateway-config.ts
+++ b/apps/desktop/src/main/maker-host/claude-gateway-config.ts
@@ -23,6 +23,14 @@ import type { Catalog } from '@cindy/model-providers';
/** Anthropic 模型在 oauth-spawn 下直连的上游。 */
export const ANTHROPIC_DIRECT_UPSTREAM = 'https://api.anthropic.com';
+/**
+ * provider-oauth 形态 cc spawn 的占位 API key(真实凭证由 proxy 在路由时注入)。
+ * 定义放本 leaf 模块:auth-adapters(写入 env)与 anthropic-compat-proxy-host
+ * (路由时识别「这不是可用凭证」,#831)都要消费,而 auth-adapters 已 import
+ * proxy-host,反向 import 会成环。
+ */
+export const CLAUDE_PROVIDER_AUTH_PLACEHOLDER_KEY = 'xdt-provider-auth-placeholder-key';
+
/**
* 前缀兼容地板(见文件头)。新增 Anthropic 家族名**不需要**改这里——
* 加进 OSS 目录 anthropic 供应商名下即可;此列表只为历史裸别名与目录失效兜底而存在。