Skip to content

fix(openai-compat): Responses→Chat 桥接按 reasoning item id 缓存回注 reasoning_content (#5520) - #5729

Merged
Wei-Shaw merged 3 commits into
Wei-Shaw:mainfrom
lbyxiaolizi:fix/responses-chat-reasoning-content-passback
Aug 19, 2026
Merged

Wei-Shaw merged 3 commits into
Wei-Shaw:mainfrom
lbyxiaolizi:fix/responses-chat-reasoning-content-passback

Conversation

@lbyxiaolizi

Copy link
Copy Markdown
Contributor

问题

Closes #5520

Codex(/v1/responses)经 force_chat_completions 桥接到 DeepSeek thinking 上游时,历史中的 encrypted-only reasoning item(summary: [] + 不透明 encrypted_content,远程 compaction / 跨会话恢复后常见)在 buildChatMessagesFromItems 里取不出明文,后续 assistant 消息缺 reasoning_content,DeepSeek 稳定返回:

400 invalid_request_error
The `reasoning_content` in the thinking mode must be passed back to the API.

客户端仅看到通用 502,且同一会话持续失败。

方案

reasoning item 的 id 一定会被客户端回传,以其为 key 做服务端缓存回注:

  • 缓存层:GatewayCache 新增 Set/GetReasoningContent(Redis key reasoning_content:<itemID>,默认 TTL 7 天;未命中返回哨兵错误 ErrReasoningContentNotFound)
  • 响应侧写入:流式扫 response.output_item.done 的 reasoning 项、非流式扫 responsesResp.Output,把 reasoning 全文按 item id 写缓存;客户端断连后 drain 上游期间(计费需要)用 detached ctx 仍会写完
  • 请求侧回查:apicompat 新增 ResponsesToChatCompletionsRequestWithOptions + ReasoningContentByID 钩子(原 ResponsesToChatCompletionsRequest 签名不变,委托 nil options);encrypted-only item 查缓存补回 pendingReasoning。缓存 miss / 出错一律 fail-open,维持桥接原行为
  • 自愈回写:请求历史里带明文 summary 的 reasoning item 顺手刷新缓存,覆盖 Redis flush / 跨实例漂移后同 id 的 encrypted-only 副本无法取明文的情况

范围说明

测试

  • apicompat:缓存命中恢复 / miss 保持原样(含 nil options 与 legacy 路径一致性)/ 明文 summary 优先不查缓存 / ExtractResponsesReasoningItem
  • service 端到端:流式响应 reasoning 写入缓存;encrypted-only 历史经缓存回注后上游 body 带 reasoning_content + 明文 item 自愈回写
  • repository:miniredis 存取 / 未命中哨兵 / 默认 TTL / 空输入 no-op

go build ./...、go vet(无 tag / unit / integration)、gofmt 均通过;internal/pkg/apicompat、internal/repository 全量测试与 internal/service(-tags=unit)全量测试通过。

…ng_content

修复 #5520:Codex 经 force_chat_completions 桥接到 DeepSeek thinking 上游时,
历史中的 encrypted-only reasoning item(summary 为空 + 不透明 encrypted_content,
远程 compaction / 跨会话恢复后常见)取不出明文,后续 assistant 消息缺
reasoning_content,DeepSeek 400 "The reasoning_content in the thinking mode
must be passed back to the API",客户端仅看到通用 502。

reasoning item 的 id 一定会被客户端回传,以其为 key 做服务端缓存:

- GatewayCache 新增 Set/GetReasoningContent(Redis,默认 TTL 7 天)
- 响应侧:流式扫 response.output_item.done、非流式扫 output,把 reasoning
  全文按 item id 写缓存;客户端断连后 drain 期间用 detached ctx 仍会写完
- 请求侧:apicompat 新增 ResponsesToChatCompletionsRequestWithOptions 与
  ReasoningContentByID 钩子,encrypted-only item 查缓存补回 pendingReasoning;
  缓存 miss/出错一律 fail-open 维持原行为
- 自愈:历史里带明文 summary 的 reasoning item 顺手刷新缓存,覆盖 Redis
  flush / 跨实例漂移

测试:apicompat(命中恢复/miss 保持原样/明文优先)、service 端到端(流式
写缓存、请求侧回注+自愈)、repository miniredis 存取。
Copilot AI lite review requested due to automatic review settings August 17, 2026 08:51

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.

Pull request overview

This PR fixes a DeepSeek thinking-mode incompatibility in the /v1/responses → /v1/chat/completions fallback bridge by caching and restoring reasoning_content for “encrypted-only” reasoning items (empty summary + opaque encrypted_content) using the reasoning item id as the lookup key.

Changes:

  • Add a gateway cache API to store/fetch reasoning text by reasoning item id, with a 7-day default TTL and a sentinel “not found” error.
  • Extend the Responses→Chat bridge with optional lookup hooks to restore reasoning_content for encrypted-only reasoning items.
  • Write-through caching on response (streaming + non-streaming) and best-effort recache from request history, with tests across repository/service/apicompat layers.

Reviewed changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
backend/internal/testutil/stubs.go Update GatewayCache stub to satisfy new reasoning cache interface methods.
backend/internal/service/openai_ws_state_store_test.go Update probe cache stub for new GatewayCache interface methods.
backend/internal/service/openai_gateway_service_test.go Update service test cache stub for new GatewayCache interface methods.
backend/internal/service/openai_gateway_responses_chat_fallback.go Add request-side restore hook, response-side cache writes, and recache-from-input logic.
backend/internal/service/openai_gateway_responses_chat_fallback_test.go Add unit tests validating streamed caching + request-side restore and recache behavior.
backend/internal/service/openai_cyber_session_block_test.go Update combo cache/store stub for new GatewayCache interface methods.
backend/internal/service/openai_account_scheduler_test.go Update scheduler test cache stub for new GatewayCache interface methods.
backend/internal/service/gemini_multiplatform_test.go Update Gemini cache stub for new GatewayCache interface methods.
backend/internal/service/gateway_service.go Add ErrReasoningContentNotFound and extend GatewayCache interface with reasoning cache methods.
backend/internal/service/gateway_multiplatform_test.go Update multiplatform cache stub for new GatewayCache interface methods.
backend/internal/service/gateway_hotpath_optimization_test.go Update hotpath cache stub for new GatewayCache interface methods.
backend/internal/repository/gateway_cache.go Implement Redis-backed Set/GetReasoningContent with default TTL and sentinel miss mapping.
backend/internal/repository/gateway_cache_reasoning_test.go Add miniredis tests for set/get, sentinel miss, TTL fallback, and no-op behavior.
backend/internal/pkg/apicompat/chatcompletions_responses_reasoning_cache_test.go Add bridge-level tests for cache hit/miss behavior and plaintext-preferred rule.
backend/internal/pkg/apicompat/chatcompletions_responses_bridge.go Add ResponsesToChatOptions + new API entrypoint, implement encrypted-only reasoning restoration, and add extraction helper for recache.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +150 to +153
if ttl <= 0 {
ttl = reasoningContentDefaultTTL
}
return c.rdb.Set(ctx, reasoningContentPrefix+itemID, content, ttl).Err()
Comment on lines +360 to +366
func (s *OpenAIGatewayService) setReasoningContent(itemID, content string) {
if s == nil || s.cache == nil {
return
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if err := s.cache.SetReasoningContent(ctx, itemID, content, responsesReasoningCacheTTL); err != nil {
codex 0.147.0 真实 resume 历史复现:DeepSeek 每轮只在开头流式产生一次
reasoning,reasoning → call A → output A → call B 的链式调用中 call B 前
没有 reasoning item,其 assistant 消息缺 reasoning_content,DeepSeek
thinking mode 400 整个历史。

buildChatMessagesFromItems 新增 lastTurnReasoning:记录本轮最近一次
reasoning,跨 tool output 存活,仅被 user 侧 item 清除;assistant 消息
(文本或 tool_calls)在 pendingReasoning 为空时回放本轮 reasoning。
…taRaw_MatchesMapVariant 毫秒边界误报,与本 PR 无关)
@Wei-Shaw
Wei-Shaw merged commit e943f81 into Wei-Shaw:main Aug 19, 2026
8 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 19, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(openai compat): Responses tool history can lose reasoning_content and DeepSeek returns generic 502

3 participants