Skip to content

fix(composite): apply reasoning effort policies to CN targets - #8309

Open
wucm667 wants to merge 15 commits into
Wei-Shaw:mainfrom
wucm667:fix/issue-8308-composite-reasoning-mapping
Open

wucm667 wants to merge 15 commits into
Wei-Shaw:mainfrom
wucm667:fix/issue-8308-composite-reasoning-mapping

Conversation

@wucm667

@wucm667 wucm667 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

问题

聚合分组路由到 DeepSeek 等国产多协议平台时,已配置的推理强度映射不会绑定到请求;原生 Anthropic 直通路径也没有应用策略,导致显式的 low → max 配置静默失效。

根因

策略绑定只允许 OpenAI / Anthropic 目标,原生 /v1/messages 直通路径在转发前未执行绑定的策略。

改动

  • 将聚合分组策略扩展到现有国产平台清单(DeepSeek、Kimi、智谱、MiniMax),其他目标维持原行为。
  • 原生 Anthropic 直通在模型改写前应用映射、上限及拒绝规则,拒绝时返回 Anthropic 格式 403,不发送上游请求。
  • 分别记录映射前请求强度和实际转发强度,覆盖流式与非流式用量信息;缺省值及未配置策略不注入字段。
  • 补充平台边界、模型精确匹配、上限、拒绝、流式与非流式回归。
  • 更正依赖说明:此前依赖差异为 follow-redirects 1.16.1 → 1.16.2,现已与 main 相同;本 PR 不包含 DOMPurify 升级。

验证

  • 已验证 Go 1.27.3 下完整 unit / integration 测试通过。
  • 已验证 golangci-lint v2.14.0、无 CGO 服务端构建及 govulncheck 通过(可达漏洞为 0)。
  • 已验证新增 handler / service 聚焦回归,映射、拒绝及用量元数据符合预期。
  • 已验证前端锁定安装、lint、类型检查、关键 30 个测试文件 / 575 项测试及生产构建通过。
  • 已验证生产依赖审计及仓库例外检查通过;剩余 XLSX 高危条目属于既有有效例外,未新增例外。
  • 已验证部署脚本、发布辅助测试及差异格式检查通过;Apple 生命周期测试在 Linux 使用现有 BSD stat / plutil 兼容适配执行,未代替原生 macOS 验证。

重复检查与边界

已核对 #5602、#8161、#5171 和 #5919:它们未覆盖本次国产目标原生直通的策略绑定与应用缺口。不新增平台能力、不改变未配置流量;实际供应商端点未进行在线请求验证。

Fixes #8308

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Model rewrites can bypass exact-model CN mappings and deny rules, while Anthropic conversion can send effort above the configured ceiling without rechecking enforcement.

2 open issues
1 file not reviewed
  • frontend/pnpm-lock.yaml: Generated file
What changed in this PR

Extends composite-group reasoning-effort policies to DeepSeek, Kimi, Zhipu, and MiniMax, addressing the policy gap described in #8308.

Changes:

  • Applies mappings, ceilings, and deny rules across native and converted Messages paths, while preserving requested and forwarded effort metadata.
  • Adds final-effort checks and regression coverage for provider normalization, model matching, retries, and streaming.
  • Raises the frontend follow-redirects override and locked version to 1.16.2.
File Description
frontend/​pnpm-lock.yaml Locks follow-redirects to 1.16.2.
frontend/​package.json Raises the follow-redirects override minimum.
backend/​internal/​service/​openai_reasoning_effort_policy.go Adds final ceiling and converted-request validation helpers.
backend/​internal/​service/​openai_gateway_messages.go Applies CN policies and preserves original bodies for retries.
backend/​internal/​service/​openai_gateway_messages_cn_policy_test.go Tests converted CN policies, capabilities, and retry behavior.
backend/​internal/​service/​openai_gateway_messages_chat_fallback.go Applies policies around Chat Completions conversion.
backend/​internal/​service/​openai_gateway_messages_anthropic_native.go Enforces native Messages policies and records original effort.
backend/​internal/​service/​openai_gateway_messages_anthropic_native_test.go Tests native mapping, denial, metadata, and GLM normalization.
backend/​internal/​service/​openai_gateway_chat_completions_raw.go Rechecks ceilings before raw upstream forwarding.
backend/​internal/​handler/​openai_chat_completions.go Uses the ceiling-binding policy helper.
backend/​internal/​handler/​openai_chat_completions_reasoning_policy_test.go Tests forwarded effort and provider-normalization boundaries.
backend/​internal/​handler/​composite_platform.go Enables CN policy binding and Anthropic-compatible values.
backend/​internal/​handler/​composite_platform_test.go Tests supported platforms, binding, and omitted effort.

Review effort: Balanced

Comment on lines 188 to 191
if service.IsCNProvider(effectiveAPIKeyPlatform(c, apiKey)) {
maxEffort, mappings = anthropicCompatibleReasoningEffortPolicy(maxEffort, mappings)
}
c.Request = c.Request.WithContext(service.WithOpenAIReasoningEffortPolicy(c.Request.Context(), maxEffort, mappings, overLimit))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已在 caaaae7 修复并补充出站边界回归:原始 public model 保持绑定,映射仅执行一跳;CN 的 Chat/Responses→原生 Anthropic 转换后重新执行 cap-only 检查,最终强度及计费元数据一致。本地完整检查和当前 head 的远端 CI、安全检查均通过。

writeChatCompletionsError(c, http.StatusBadRequest, "invalid_request_error", err.Error())
return nil, err
}
if cappedBody, changed, policyErr := applyFinalOpenAIReasoningEffortCeiling(ctx, upstreamBody, upstreamModel); policyErr != nil {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已在 caaaae7 修复并补充出站边界回归:原始 public model 保持绑定,映射仅执行一跳;CN 的 Chat/Responses→原生 Anthropic 转换后重新执行 cap-only 检查,最终强度及计费元数据一致。本地完整检查和当前 head 的远端 CI、安全检查均通过。

@Wei-Shaw

Wei-Shaw commented Nov 4, 2026

Copy link
Copy Markdown
Owner

感谢修复 CN 目标的推理强度策略缺口。审计中复核确认了两个回归,需要修复后再合并:

1. Chat Completions 原始转发的上限复检没有限定 CN 平台,非 CN 目标出现 403

  • handler/composite_platform.go:145-151 只要 openAIReasoningEffortPolicyForRequest 返回 ok(包括 OpenAI 分组)就把上限策略绑到 ctx。
  • service/openai_gateway_chat_completions_raw.go:207 的 applyFinalOpenAIReasoningEffortCeiling 对所有账号平台执行。
  • openai_reasoning_effort_policy.go:476 的 GLM 检查只看上游模型名前缀 glm-,不看账号平台。

OpenAI APIKey 账号接 GLM 等第三方 OpenAI 兼容上游、走 raw chat 是 main 上正式支持的组合(见 openai_gateway_chat_completions_raw_test.go 的 TestForwardAsRawChatCompletions_NormalizesGLMReasoningEffortForUpstream,Platform: PlatformOpenAI + glm-5.2)。

复现:OpenAI 分组 + OpenAI APIKey 账号,上游模型 glm-4.6,分组上限 medium(downgrade):

客户端 reasoning_effort main 本 PR
low / medium / high 归一为 high 后正常转发 归一 high → 截到 medium → 403
none / minimal 原样转发 403

上限为 xhigh 时,客户端发 xhigh/max 同样 403。这与描述中"其他目标维持原行为"不符;现有测试只覆盖了 CN 平台账号。

建议:复检加 IsCNProvider(account.Platform) 门控,并补一条 OpenAI 账号 + glm 上游的用例。

2. CN GLM 目标上,关闭 thinking 的请求被误判为超限 403

Messages → Chat 转换时 thinking.type=disabled 会合成 reasoning_effort:"none"(apicompat/anthropic_to_responses.go、chatcompletions_anthropic_bridge.go:109)。在 applyMessagesConvertedReasoningEffortPolicy 中,GLM 归一不处理 none,上限阶梯也不认它,最终在 :476 返回 ReasoningEffortOverLimitError。

复现:composite 分组 → Zhipu(chat_completions 协议),上游 glm-4.6,分组配任意上限或仅配映射,请求 output_config.effort:"high" + thinking:{type:"disabled"} → 403 glm-4.6 does not support capped reasoning effort "none"。main 原样转发。

建议:GLM 检查跳过 none/minimal(不参与上限比较),或在检查前按 thinking 关闭的语义处理。

3. 需要确认的设计点

  • GLM 上限不落在原生档位时,配置为 downgrade 实际表现为 deny:客户端 effort 恰好等于上限(如 medium 对 medium)也被拒,而省略 effort 的请求反而不受限。测试把这个行为固化成了预期,请确认是有意为之;更合理的可能是降到不超过上限的最高原生档。
  • ReasoningEffortOverLimitError.Requested 取的是 GLM 归一后的值,错误信息与客户端实际发送值不一致。

4. 与 #8161 的冲突

两者改同一函数 bindOpenAIReasoningEffortPolicyForMessagesRequest,以及 openai_gateway_messages_chat_fallback.go 的同一段,任意合并顺序都会在这两处冲突。另外本 PR 的 applyMessagesConvertedReasoningEffortPolicy 内部已调用 validateGPT61SolCompatRequest,与 #8161 合并后会重复执行(无害但冗余)。建议本 PR 先修复合入,#8161 再 rebase。

另:PR 描述写"升级 DOMPurify 至 3.4.17",实际 diff 是 follow-redirects 1.16.1→1.16.2(已与 main 相同),描述请更正。

hjkl950217 added a commit to hjkl950217/sub2api that referenced this pull request Nov 6, 2026
冲突处理:本地续写模型用 turn state,不引入 previous_response_id;按会话绑定是否存在触发一次重试,并补 ContinuationDisabled 状态。测试按二开完整历史重放校正。

Co-Authored-By: Claude Code <noreply@anthropic.com>
@wucm667

wucm667 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

已在 caaaae7 落实本轮意见:最终复检仅限 CN 账号,保留 OpenAI 兼容 GLM 的既有行为;关闭 thinking 的 none/minimal 不误拒。GLM downgrade 选择不超过上限的最高原生档,无合法档位才拒绝;错误保留客户端原始 effort。补齐原始模型匹配、单跳映射、Chat/Responses/native 路径及出站边界测试,并移除重复兼容校验。正文已更正依赖说明,标题未变。本地完整检查及当前 head 远端 CI、安全检查均通过。保留 #8309 先合、#8161 后整合的顺序。

@wucm667

wucm667 commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

已在原分支整合最新 main 并解决冲突,保留已合入的推理强度合成行为;补齐显式 none 映射后拒绝诊断保留原始请求值的出站回归。此前平台边界、关闭 thinking、GLM 降档和单跳映射修复均保留。另以独立安全提交升级 Go 至 1.27.4,修复新公布的 GO-2026-6859,未添加豁免。本地完整检查、前端 3110 项测试及当前 head 4eda963 的远端 CI、安全检查均通过;当前无冲突,等待复核。

xiaoguai1818 pushed a commit to xiaoguai1818/sub2api that referenced this pull request Nov 21, 2026
Restore Antigravity Responses custom-tool roundtrips and correct Embeddings platform scheduling. Harden OAuth probe fencing, request-scoped Gemini 403 behavior, OpenAI stream failover and WebSocket current-turn safety.

Preserve Claude/GPT-6.2/Grok protocol details and model observations; correct Fable/Kimi quota refreshes and user-vs-account cost reporting. Bound scheduler fallback, request memory retention and shutdown/stream lifecycles without unapproved pricing or schema changes.

Local checks: complete Go unit packages, PostgreSQL 18.1/Redis 8.4 repository and audit integration, focused -race, frontend typecheck/lint/1858 Vitest/build, backend lint 0 issues and embedded server build. Remote CI/Security Scan remain pending.

Upstream references: Wei-Shaw/sub2api Wei-Shaw#8833 Wei-Shaw#8875 Wei-Shaw#8856 Wei-Shaw#8730 Wei-Shaw#8990 Wei-Shaw#8806 Wei-Shaw#8832 Wei-Shaw#8809 Wei-Shaw#8164 Wei-Shaw#8804 Wei-Shaw#8773 Wei-Shaw#8309.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 聚合分组路由到 DeepSeek 等多协议平台时,推理强度映射被静默跳过

3 participants