fix(gateway): always apply full mimicry for OAuth accounts regardless of client identity

Before: isClaudeCodeRequest() checked whether the client looks like a
real Claude Code CLI (UA, system prompt, X-App header, metadata format).
If it looked like Claude Code, all mimicry was skipped — the assumption
being that a real CLI needs no help.

Problem: third-party tools like opencode partially impersonate Claude
Code (sending claude-cli UA + claude-code beta + CC system prompt) but
miss critical details (billing attribution block, tool-name obfuscation,
cache breakpoints, full beta set). Some users' opencode instances pass
the isClaudeCodeRequest check, causing sub2api to skip mimicry entirely,
while Anthropic still detects the request as third-party.

This explains why 'same opencode version, some users work, some don't'
— it depends on which opencode features/config trigger the validator.

Fix: OAuth accounts now unconditionally run the full mimicry pipeline,
matching Parrot's behavior (Parrot never checks client identity).
This is safe because our mimicry is strictly more complete than any
third-party client's partial impersonation.

Changed:
  - /v1/messages path: remove isClaudeCode gate
  - /v1/messages/count_tokens path: same
This commit is contained in:
keh4l
2026-04-25 00:26:37 +08:00
parent f3233db01f
commit 6dc89765fd

View File

@@ -4155,12 +4155,14 @@ func (s *GatewayService) Forward(ctx context.Context, c *gin.Context, account *A
})
}
isClaudeCode := isClaudeCodeRequest(ctx, c, parsed)
shouldMimicClaudeCode := account.IsOAuth() && !isClaudeCode
// OAuth 账号无条件走完整 mimicry与 Parrot 对齐。
// 不再检查 isClaudeCodeRequest —— 即使客户端自称 Claude Codeopencode 等
// 第三方工具会伪装 UA / X-App / system prompt它的伪装往往不完整缺 billing
// block / 工具名混淆 / cache 策略等),被 Anthropic 判为 third-party。
// 无条件覆盖不会对真正的 Claude Code 造成问题,因为我们的伪装更完整。
shouldMimicClaudeCode := account.IsOAuth()
if shouldMimicClaudeCode {
// 非 Claude Code 客户端:将 system 替换为 Claude Code 标识,原始 system 迁移至 messages
// 条件1) OAuth/SetupToken 账号 2) 不是 Claude Code 客户端 3) 不是 Haiku 模型 4) system 中还没有 Claude Code 提示词
systemRewritten := false
if !strings.Contains(strings.ToLower(reqModel), "haiku") &&
!systemIncludesClaudeCodePrompt(parsed.System) {
@@ -8386,8 +8388,7 @@ func (s *GatewayService) ForwardCountTokens(ctx context.Context, c *gin.Context,
// Pre-filter: strip empty text blocks to prevent upstream 400.
body = StripEmptyTextBlocks(body)
isClaudeCode := isClaudeCodeRequest(ctx, c, parsed)
shouldMimicClaudeCode := account.IsOAuth() && !isClaudeCode
shouldMimicClaudeCode := account.IsOAuth()
if shouldMimicClaudeCode {
normalizeOpts := claudeOAuthNormalizeOptions{stripSystemCacheControl: true}