Repository navigation
fix(openai-compat): Responses→Chat 桥接按 reasoning item id 缓存回注 reasoning_content (#5520) - #5729
Merged
Wei-Shaw merged 3 commits intoAug 19, 2026
Conversation
…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 存取。
Contributor
There was a problem hiding this comment.
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_contentfor 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 无关)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
Closes #5520
Codex(
/v1/responses)经force_chat_completions桥接到 DeepSeek thinking 上游时,历史中的 encrypted-only reasoning item(summary: []+ 不透明encrypted_content,远程 compaction / 跨会话恢复后常见)在buildChatMessagesFromItems里取不出明文,后续 assistant 消息缺reasoning_content,DeepSeek 稳定返回:客户端仅看到通用 502,且同一会话持续失败。
方案
reasoning item 的
id一定会被客户端回传,以其为 key 做服务端缓存回注:GatewayCache新增Set/GetReasoningContent(Redis keyreasoning_content:<itemID>,默认 TTL 7 天;未命中返回哨兵错误ErrReasoningContentNotFound)response.output_item.done的 reasoning 项、非流式扫responsesResp.Output,把 reasoning 全文按 item id 写缓存;客户端断连后 drain 上游期间(计费需要)用 detached ctx 仍会写完ResponsesToChatCompletionsRequestWithOptions+ReasoningContentByID钩子(原ResponsesToChatCompletionsRequest签名不变,委托 nil options);encrypted-only item 查缓存补回pendingReasoning。缓存 miss / 出错一律 fail-open,维持桥接原行为范围说明
/v1/responses→ Chat Completions 回退桥接(本 issue 场景);Anthropic→CC 直桥在 [Bug] Anthropic→Chat Completions 直接桥接丢弃 assistant thinking 块,DeepSeek 等 reasoning 模型多轮对话报 400 (reasoning_content must be passed back) #5528 已修测试
ExtractResponsesReasoningItemreasoning_content+ 明文 item 自愈回写go build ./...、go vet(无 tag /unit/integration)、gofmt均通过;internal/pkg/apicompat、internal/repository全量测试与internal/service(-tags=unit)全量测试通过。