magpie icon indicating copy to clipboard operation
magpie copied to clipboard

[Bug] v0.1.1099 使用 OpenCode Go 的 DeepSeek 时仍报 reasoning_text 回传错误

Open sperwe opened this issue 1 month ago • 5 comments

使用 Codex,通过 Magpie 路由到 OpenCode Go 的 deepseek-v4.1-flash,思考强度为 max,部分请求报 HTTP 400:

The reasoning_text in the thinking mode must be passed back to the API.

客户端同时提示 stream disconnected before completion。

看到 #388 已说明在 v0.1.559 修复,但我使用 v0.1.1099 仍遇到同类错误。

当前使用混合路由组,模式为 order、stays=turn。尚不确定是否与模型切换有关

期望结果

对需要回传思考记录的上游,连续工具回传、下一轮和恢复会话应保留兼容的思考记录;如历史来自其他模型且无法兼容,应明确提示或安全选择兼容路径。

已核对的代码及待排查点

  • responses.go 的 parseResponses / buildResponses 已有 #388 的修复逻辑;当前模型名含 deepseek,主机为 opencode.ai,并非明显不满足原检查条件。
  • TestReasoningReplayed 验证了预先构造思考项的单次转发,但未完整模拟 OpenCode Go 的实际回复、客户端保存及再次回传,未覆盖本路由组。
  • passthrough 的兼容重试可能调用 withoutBareReasoning。后者会删除没有 encrypted_content 的 reasoning 项。请核查这个分支是否可能影响需要明文思考回传的 DeepSeek。

以上仅是排查线索,未证明失败请求走了这个分支,也未证明缺失发生在 Magpie 而非客户端或上游。

sperwe avatar Oct 07 '26 05:10 sperwe

谢谢报告,排查线索很有用。对着代码看了,原因在 magpie 这边:

OpenCode Go 的 deepseek-v4.1-flash 在 models.dev 里没有标明只走某个 API,所以 Codex 的 Responses 请求被 magpie 原样转给了 OpenCode Go 的 /responses。#388 的修复只在 magpie 翻译请求时生效(responses.go 的 buildResponses 把上一轮思考作为 reasoning_text 写回);原样转发时,上一轮的思考只能靠 Codex 自己回传。而 Codex 回传 reasoning 项时不带 reasoning_text 内容(codex-rs protocol/src/models.rs 的 should_serialize_reasoning_content:content 里有 ReasoningText 就不序列化),只剩 summary 和 encrypted_content。DeepSeek 拿不到思考原文,就报了这个 400。

你提到的 withoutBareReasoning 也会让情况更糟:转发时只要上游有一次以 param=input 拒绝请求,magpie 就会把这个模型记成「不收没有密封内容的 reasoning」,之后发给它的请求都会去掉 reasoning 项(gateway.go 的 passthrough)。

要修的是转发这条路:给 DeepSeek 这类模型补回思考原文。修好后会在这里回复。

yetone avatar Oct 07 '26 05:10 yetone

在 v0.1.1106 里修好了,更新后再试试。

需要更正我上面说的一点:magpie 补不回 DeepSeek 的思考原文。原文在 Codex 回传时已经丢了,magpie 手里也没有。现在的做法是,转发给 OpenCode Go 的 /responses 时,没有 reasoning_text 的 reasoning 项会用它的 summary 填上 reasoning_text。DeepSeek 要的是这个字段在场,所以不会再报 400。

另外,对需要回传思考的模型(DeepSeek 这类),magpie 也不会再因为一次被拒,就在之后所有请求里把 reasoning 项删掉。

验证:用你贴的报错原文模拟 OpenCode Go 写了测试(TestDeepSeekReasoningPassedThrough),改动前失败,改动后通过;go test -race ./internal/gateway 也通过。没有用真实的 OpenCode Go 账号试过,所以先不关 issue,等你确认。

yetone avatar Oct 07 '26 18:10 yetone

#1104 的修复(v0.1.1106 的 withReasoningText)在「Codex 密封了思考、没有 summary」这种 item 上仍然漏,今天在 0.1.1151 上又稳定复现了,补一个 patch。

现场证据(magpie 自己的 usage.jsonl)

t=2026-10-10T22:58:14  agent=codex  provider=opencode-go  host=opencode.ai
model=deepseek-v4.1-flash  req=group/auto  status=400
err="OpenCode Go: The `reasoning_text` in the thinking mode must be passed back to the API. (request_id: 7911a5aa-...)"

同一条形状今天 9 次(01:11 / 16:00 / 16:03 / 16:30 / 22:58 / 23:14 …),全部是 agent=codex + req=group/auto → opencode-go/deepseek-v4.1-flash。

根因:item 在,但没有可填的东西

withReasoningText 只从 summary 取值,summary 为空就 continue:

var b strings.Builder
for _, s := range it.Summary { b.WriteString(s.Text) }
if b.Len() == 0 {
    continue        // ← 密封 item 走到这里
}

而 Codex 回传的 reasoning item 有相当一部分是空 summary + encrypted_content:我数了本机那个活动会话(39MB rollout)里的 9184 行,reasoning item 的形状分布是

summary content encrypted 条数
1 0 – 547
0 0 有 227
1 0 有 81
0 1 有 12
3/2/4 0 有 10

那 227 条就是「另一家 provider 服务的轮次 / OpenAI 自己的轮次,被 Codex 密封回传」——summary 空的,withReasoningText 填不了,原样发给 OpenCode Go,DeepSeek 就拒整条请求。

我用本地回显上游对比过四种形状(关掉我自己那个 workaround 中间件之后):

客户端发来的 reasoning item magpie 现在发给上游的
有 summary 文本 ✅ content:[{reasoning_text: <summary>}](#1104 的修复生效)
空 summary + encrypted ❌ 原样透传,没有 content → 上游 400
完全没有 reasoning item 原样(没有 item)
空 summary、也没有 encrypted 原样(#1104 测试里那条「nothing to give back」)

复现(用仓库里现成的 deepseekResponses 假上游)

internal/gateway/sealed_reasoning_test.go,在未改动的 main 上失败:

--- FAIL: TestDeepSeekReasoningPassedThroughWhenOnlySealed
    a sealed reasoning item with no summary: 400
    {"error":{"message":"OpenCode Go: The `reasoning_text` in the thinking mode must be passed back to the API.", ...}}

建议修法

密封 item 没有可读文本时,用占位符把 content 补上;完全空的 item 保持现状(#1104 那条测试仍然通过):

+// missingReasoningText is what a reasoning item nothing readable can be
+// filled from goes back with: the upstream asks for the field, not for a
+// particular text, and an item it is left out of refuses the whole request.
+const missingReasoningText = "."
+
 func withReasoningText(body []byte) []byte {
 	...
 		var it struct {
-			Type    string  `json:"type"`
-			Summary []rText `json:"summary"`
-			Content []rText `json:"content"`
+			Type      string          `json:"type"`
+			Summary   []rText         `json:"summary"`
+			Content   []rText         `json:"content"`
+			Encrypted json.RawMessage `json:"encrypted_content"`
 		}
 	...
 		if b.Len() == 0 {
-			continue
+			// nothing readable: a sealed turn is passed back with the
+			// placeholder, one with nothing at all is left alone
+			if len(it.Encrypted) == 0 || string(it.Encrypted) == "null" {
+				continue
+			}
+			b.WriteString(missingReasoningText)
 		}

internal/gateway/responses.go 一处,20 增 5 删;无新增依赖、无网络地址、无凭据读写。

验证

  • 新测试 + #1104 原有的 TestDeepSeekReasoningPassedThrough:go test -tags nogui -run TestDeepSeekReasoningPassedThrough ./internal/gateway → ok
  • go test -tags nogui ./internal/gateway → 只剩两条与本改动无关的环境失败(TestClaudeSubscriptionRunsTheDownloadedClaudeCode、TestClaudeSubscriptionWithoutClaudeCodeSaysHowToGetIt);在未改动的原始 clone 上同样失败,已核对
  • go test -tags nogui -race -run "TestDeepSeek|TestReasoning|TestBareReasoning|TestWithoutReasoning|TestResponses|TestPassthrough" ./internal/gateway → ok
  • gofmt 干净

一个还没证实的相邻形状

翻译路径 buildResponses 里,replay && p.Text != "" 才会写出 reasoning item——某一轮思考文本为空时,请求里一个 reasoning item 都没有。这种「压根没有 item」上游会不会同样 400,我没能证实(上游现在不严格,测不出来)。如果会,那和 #1462(Chat 侧同一根因)是同一类处理。

本机缓解

在网关中间件里给末尾工具轮补 reasoning_content / reasoning_text(plugins.json + 一个 *.middleware.js,不改二进制、更新不覆盖)。如果上面的方向可以接受,我可以把它整理成 PR 或社区中间件包。

Clear8Heart avatar Oct 10 '26 16:10 Clear8Heart

@Clear8Heart 抱歉回复晚了,谢谢这么完整的分析和补丁。

你说的「空 summary + 只有 encrypted_content」这种 reasoning 项,以及你最后提到的「这一步压根没有 reasoning 项」,main 上已经由 691858e06 一起修了,v0.1.1188 起已发布(为 #1592 做的,和你的补丁是各自独立写的,所以没用上你的补丁):

  • withReasoningText(internal/gateway/responses.go)对没有任何可读文本的 reasoning 项,有 summary 就用 summary,没有就填一个占位的 reasoning_text(" "),encrypted_content、id 这些字段原样保留;
  • 一个 function_call / custom_tool_call 前面没有自己那一步的 reasoning 项时,会在它前面补一个;
  • 以上只对需要回传思考的模型(DeepSeek 这类)生效。

这几种形状当时都对着真实的 api.deepseek.com/v1/responses 复现过 400,修复后返回 200。你那边的 OpenCode Go deepseek-v4.1-flash 没用真实账号试过,麻烦升级到 v0.1.1188 或更新版本后,关掉你那个本地中间件看看 usage.jsonl 里还有没有这条 400。确认前 issue 先开着。

yetone avatar Oct 12 '26 20:10 yetone

Result

Confirmed fixed on upstream main at 7d67910f; the fix commit 691858e0 remains an ancestor of current main 9277c256. First release: v0.1.1188+.

Evidence

  • The existing regression coverage for empty-summary / encrypted-only reasoning passed at the audited tip.
  • Removing the fix reproduces the original reasoning_text replay failure.

Boundary

Not tried: one final live OpenCode Go request from this machine. The regression path and the DeepSeek behavior were verified; the remaining check is the reporter's default OpenCode Go setup.

No further code change appears necessary. Please retest with the current release and report back if the same request still fails.

Edward4226 avatar Oct 25 '26 13:10 Edward4226