From 1a342edfae311e87c7e9f4af5a261ac7a60eb610 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 4 Nov 2026 13:49:14 +0800 Subject: [PATCH] devin 0.1.36: another model's reasoning goes back as its thinking (#703) kamikakushi (#703): swe-2 is conditioned on the assistant turns before it having thinking (ChatMessagePrompt field 11). The plugin sent field 11 only beside a signature (#665), and no builder handed it a non-Claude model's reasoning, so swe-2 stopped emitting delta_thinking on a tool loop's later turns (the reporter's A/B against swe-2-high: no field 11, 0 bytes of thinking; field 11 without 12, thinking on 5 of 5 runs). Each claim checked against index.mjs at 0.1.35: build() read only content and tool_calls, v2Chat() dropped reasoning parts, encodeMsg() gated 11 on the signature. - encodeMsg: field 11 whenever the thinking has text; 12 and 18 only with a signature, as before. - build (chat completions): reasoning_content (or reasoning, as some clients name it) goes on the assistant prompt as unsigned thinking, to a model that isn't Claude. joinSplitCalls keeps both halves'. - v2Chat (OpenCode 2): reasoning parts become reasoning_content, which build then handles. - buildMessages (/v1/messages, the sibling the issue doesn't name): a non-Claude model's thinking block goes back as its text alone, never with its signature. - Claude: unchanged. A chat completion still sends Claude no thinking (no field for a signature there), and Messages sends only signed thinking with its signature and type, as #665 did: Anthropic refuses a thinking block without a valid signature, so unsigned text never goes to Claude on any path. Not done: the issue's optional made-up thought for a history turn whose client sent no reasoning (Pi's openai-completions default). Nothing is written into the model's history that it didn't say. Run (mote, bun 1.4.2, HOME a temp dir): packages/devin 208 pass, 0 fail; repo root bun test 5411 pass, 0 fail, 19 skip; bun scripts/check.mjs ok. Broke it by hand: - index.mjs from origin/main: 7 red (the 6 new non-Claude tests and the updated "another model's goes without a signature"); the Claude tests pass both ways. - only encodeMsg's old gate put back: the same 7 red. - build's Claude gate removed: "Claude gets no unsigned thinking from a chat completion", "a chat completion's request is the same as before" and the v2 Claude check red. Not tried: a real Devin account; frames are built from the proto's field numbers, and swe-2's behaviour rests on the reporter's A/B. --- packages/devin/index.mjs | 46 +++++-- packages/devin/package.json | 2 +- packages/devin/reasoning-history.test.mjs | 147 +++++++++++++++++++++ packages/devin/thinking-signature.test.mjs | 5 +- 4 files changed, 187 insertions(+), 13 deletions(-) create mode 100644 packages/devin/reasoning-history.test.mjs diff --git a/packages/devin/index.mjs b/packages/devin/index.mjs index 8ffca6c..08edaf7 100644 --- a/packages/devin/index.mjs +++ b/packages/devin/index.mjs @@ -1200,7 +1200,8 @@ function joinSplitCalls(msgs) { const prev = out.at(-1) if (m?.role === "assistant" && prev?.role === "assistant" && prev.tool_calls?.length) { const content = [textOf(prev.content), textOf(m.content)].filter(Boolean).join("\n\n") - out[out.length - 1] = { ...prev, content, tool_calls: [...prev.tool_calls, ...(m.tool_calls ?? [])] } + const reasoning = [reasoningOf(prev), reasoningOf(m)].filter(Boolean).join("\n\n") + out[out.length - 1] = { ...prev, content, tool_calls: [...prev.tool_calls, ...(m.tool_calls ?? [])], ...(reasoning ? { reasoning_content: reasoning } : {}) } continue } out.push(m) @@ -1208,6 +1209,13 @@ function joinSplitCalls(msgs) { return out } +// reasoningOf is an assistant message's reasoning as a chat completion +// carries it: reasoning_content, or reasoning as some clients name it. +function reasoningOf(m) { + for (const v of [m?.reasoning_content, m?.reasoning]) if (typeof v === "string" && v) return v + return "" +} + // effortOf is the effort a request names, as magpie reads it. function effortOf(s) { const e = String(s ?? "").trim().toLowerCase() @@ -1461,6 +1469,7 @@ function noted(text) { // build is the GetChatMessage request for a chat completion, to the model // uid. function build(chat, uid, key) { + const claude = isClaude(uid) const msgs = [] let pending = [] // calls the last reply made, not yet answered const answer = () => { @@ -1497,7 +1506,13 @@ function build(chat, uid, key) { const text = typeof m.content === "string" ? m.content : Array.isArray(m.content) ? m.content.filter((p) => p?.type === "text" && p.text).map((p) => p.text).join("\n\n") : "" if (!text && !calls.length) continue // a turn that only thought, or failed answer() - msgs.push({ role: ASSISTANT, text, calls }) + // the reply's reasoning goes back as its thinking (11), unsigned, to + // a model that isn't Claude: swe-2 stops thinking on a tool loop's + // later turns when the turns before carry none (plugins#703). A chat + // completion has no field for a signature, and Claude's thinking + // goes back only signed (plugins#665), so Claude gets none, as before. + const reasoning = claude ? "" : reasoningOf(m) + msgs.push({ role: ASSISTANT, text, calls, ...(reasoning ? { thinking: { text: reasoning } } : {}) }) // a copy: answering a call takes it off pending, never off the // reply that made it, which goes to Devin with every call it made pending = [...calls] @@ -1630,10 +1645,13 @@ function encodeMsg(m) { // own: where the client put cache_control (#566) if (m.cache) b.bytes(8, new PB().varint(1, 1)) for (const p of m.images ?? []) b.bytes(10, new PB().str(1, p.data).str(2, p.type)) - // the signed thinking the reply had, sent back as Devin wrote it: - // thinking (11), signature (12) and signature_type (18) (plugins#665) + // the thinking the reply had, sent back as Devin wrote it: thinking + // (11), and Claude's signature (12) and signature_type (18) when it was + // signed (plugins#665). Another model's thinking has no signature and + // goes back as its text alone (plugins#703); which thinking reaches + // here is the builders' to say. + if (m.thinking?.text) b.str(11, m.thinking.text) if (m.thinking?.signature) { - if (m.thinking.text) b.str(11, m.thinking.text) b.str(12, m.thinking.signature) if (m.thinking.type) b.str(18, m.thinking.type) } @@ -1972,10 +1990,11 @@ function buildMessages(req, uid, key) { const own = [] const calls = [] let mark = false // a tool_use the client marked: the prompt with the calls ends there - // a signed thinking block, for the prompt that follows it: Claude's - // thinking goes back with its signature, which Anthropic wants on a - // tool loop's next turn (plugins#665). Unsigned thinking, and any to - // a model that isn't Claude, goes no further, as before. + // a thinking block, for the prompt that follows it: Claude's thinking + // goes back with its signature, which Anthropic wants on a tool + // loop's next turn (plugins#665), and unsigned thinking to Claude + // goes no further, as before. Another model's thinking goes back as + // its text alone, never with a signature (plugins#703). let thought = null const take = (p) => { if (thought) [p.thinking, thought] = [thought, null] @@ -1992,6 +2011,9 @@ function buildMessages(req, uid, key) { // two in a row: the first has a prompt of its own if (thought) own.push(take({ role: ASSISTANT, text: "", calls: [] })) thought = { text: typeof b.thinking === "string" ? b.thinking : "", signature: b.signature, type: sigTypeOf(b.signature) } + } else if (b?.type === "thinking" && !claude && typeof b.thinking === "string" && b.thinking) { + if (thought) own.push(take({ role: ASSISTANT, text: "", calls: [] })) + thought = { text: b.thinking } } } if (calls.length) { @@ -2524,14 +2546,18 @@ function v2Chat(options, modelID) { } if (m.role === "assistant") { let text = "" + let reasoning = "" const calls = [] for (const p of m.content ?? []) { if (p.type === "text") text += (text ? "\n\n" : "") + p.text + // the reply's reasoning, which build sends back to a model that + // isn't Claude (plugins#703) + else if (p.type === "reasoning" && typeof p.text === "string" && p.text) reasoning += (reasoning ? "\n\n" : "") + p.text else if (p.type === "tool-call") calls.push({ id: p.toolCallId, type: "function", function: { name: p.toolName, arguments: JSON.stringify(p.input ?? {}) } }) } if (!text && !calls.length) continue - messages.push({ role: "assistant", content: text, ...(calls.length ? { tool_calls: calls } : {}) }) + messages.push({ role: "assistant", content: text, ...(reasoning ? { reasoning_content: reasoning } : {}), ...(calls.length ? { tool_calls: calls } : {}) }) continue } if (m.role === "tool") { diff --git a/packages/devin/package.json b/packages/devin/package.json index fa62111..f079a06 100644 --- a/packages/devin/package.json +++ b/packages/devin/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-devin-auth", - "version": "0.1.35", + "version": "0.1.36", "description": "Devin subscriptions (the devin CLI's account) in OpenCode and magpie", "type": "module", "main": "./index.mjs", diff --git a/packages/devin/reasoning-history.test.mjs b/packages/devin/reasoning-history.test.mjs new file mode 100644 index 0000000..9bc5b97 --- /dev/null +++ b/packages/devin/reasoning-history.test.mjs @@ -0,0 +1,147 @@ +// A model that isn't Claude gets its earlier turns' reasoning back +// (plugins#703, kamikakushi). swe-2 is conditioned on the assistant turns +// before having thinking (ChatMessagePrompt field 11): with none there it +// stops emitting delta_thinking on a tool loop's later turns. The thinking +// goes back as its text alone, never with a signature (12) or its type +// (18), which only Claude's signed thinking carries (plugins#665); Claude +// gets no unsigned thinking on any path. Field numbers are Devin's proto's +// (devin2api's all-protos.proto, ExaChatPb_ChatMessagePrompt); the +// reporter's A/B against swe-2-high is what the swe-2 behaviour rests on. +import { expect, test } from "bun:test" +import { _internal } from "./index.mjs" + +const { fields, build, buildMessages, v2Chat } = _internal +const dec = new TextDecoder() +const SIG = "EqQBCkgIBRABGAIiQL2xYzRvY0RzZWNyZXRzaWduYXR1cmU=" + +// prompts is each ChatMessagePrompt a request sends, with the thinking, +// signature and signature type it carries +const prompts = (body) => + fields(body) + .filter((f) => f.num === 3 && f.wire === 2) + .map((f) => { + const p = { role: 0, text: "", calls: [], thinking: null, signature: null, type: null } + for (const g of fields(f.data)) { + if (g.num === 2) p.role = g.n + else if (g.num === 3) p.text = dec.decode(g.data) + else if (g.num === 6) p.calls.push(dec.decode(fields(g.data).find((h) => h.num === 2).data)) + else if (g.num === 11) p.thinking = dec.decode(g.data) + else if (g.num === 12) p.signature = dec.decode(g.data) + else if (g.num === 18) p.type = dec.decode(g.data) + } + return p + }) +const assistant = (body) => prompts(body).filter((p) => p.role === 2) + +// a two-step tool loop as a chat completion client sends it back +const chatLoop = (extra = {}) => ({ + messages: [ + { role: "user", content: "Read /a, then /b." }, + { role: "assistant", content: null, reasoning_content: "Start with /a.", tool_calls: [{ id: "call_1", type: "function", function: { name: "read_file", arguments: '{"path":"/a"}' } }], ...extra }, + { role: "tool", tool_call_id: "call_1", content: "A" }, + { role: "assistant", content: "Now /b.", reasoning_content: "A is read; /b next.", tool_calls: [{ id: "call_2", type: "function", function: { name: "read_file", arguments: '{"path":"/b"}' } }] }, + { role: "tool", tool_call_id: "call_2", content: "B" }, + ], + tools: [{ type: "function", function: { name: "read_file", parameters: { type: "object", properties: { path: { type: "string" } } } } }], +}) + +test("a chat completion's reasoning goes back as unsigned thinking to swe-2", () => { + expect(assistant(build(chatLoop(), "swe-2", "k")).map((p) => [p.text, p.calls, p.thinking, p.signature, p.type])).toEqual([ + ["", ["read_file"], "Start with /a.", null, null], + ["Now /b.", ["read_file"], "A is read; /b next.", null, null], + ]) +}) + +test("reasoning named reasoning, as some clients send it, goes back too", () => { + const chat = chatLoop() + for (const m of chat.messages) if (m.reasoning_content) [m.reasoning, m.reasoning_content] = [m.reasoning_content, undefined] + expect(assistant(build(chat, "swe-2", "k")).map((p) => p.thinking)).toEqual(["Start with /a.", "A is read; /b next."]) +}) + +test("a turn without reasoning sends no thinking, and a turn that only thought still sends nothing", () => { + const chat = chatLoop() + delete chat.messages[1].reasoning_content + chat.messages.push({ role: "assistant", content: "", reasoning_content: "nothing to say" }, { role: "user", content: "go" }) + const ps = assistant(build(chat, "swe-2", "k")) + expect(ps.map((p) => p.thinking)).toEqual([null, "A is read; /b next."]) +}) + +test("a call split over two messages keeps both halves' reasoning", () => { + const chat = { + messages: [ + { role: "user", content: "Read both." }, + { role: "assistant", content: null, reasoning_content: "First /a.", tool_calls: [{ id: "call_1", type: "function", function: { name: "read_file", arguments: "{}" } }] }, + { role: "assistant", content: null, reasoning_content: "And /b.", tool_calls: [{ id: "call_2", type: "function", function: { name: "read_file", arguments: "{}" } }] }, + { role: "tool", tool_call_id: "call_1", content: "A" }, + { role: "tool", tool_call_id: "call_2", content: "B" }, + ], + } + const ps = assistant(build(chat, "swe-2", "k")) + expect(ps.map((p) => [p.calls, p.thinking])).toEqual([[["read_file", "read_file"], "First /a.\n\nAnd /b."]]) +}) + +test("Claude gets no unsigned thinking from a chat completion", () => { + for (const uid of ["claude-opus-5-5", "claude-sonnet-4-6-thinking"]) { + const ps = prompts(build(chatLoop(), uid, "k")) + expect(ps.every((p) => p.thinking === null && p.signature === null && p.type === null)).toBe(true) + } +}) + +// OpenCode 2's language model: a reply's reasoning parts +const v2Prompt = [ + { role: "user", content: [{ type: "text", text: "Read /a." }] }, + { + role: "assistant", + content: [ + { type: "reasoning", text: "Start with /a." }, + { type: "tool-call", toolCallId: "call_1", toolName: "read_file", input: { path: "/a" } }, + ], + }, + { role: "tool", content: [{ type: "tool-result", toolCallId: "call_1", toolName: "read_file", output: { type: "text", value: "A" } }] }, +] + +test("OpenCode 2's reasoning parts go back as reasoning_content, and on to swe-2 as thinking", () => { + const chat = v2Chat({ prompt: v2Prompt }, "swe-2") + const a = chat.messages.find((m) => m.role === "assistant") + expect(a.reasoning_content).toBe("Start with /a.") + expect(assistant(build(chat, "swe-2", "k")).map((p) => [p.calls, p.thinking, p.signature])).toEqual([[["read_file"], "Start with /a.", null]]) + // and not on to Claude + const claude = v2Chat({ prompt: v2Prompt }, "claude-opus-5-5") + expect(prompts(build(claude, "claude-opus-5-5", "k")).every((p) => p.thinking === null)).toBe(true) +}) + +test("an assistant turn with no reasoning part has no reasoning_content", () => { + const prompt = [v2Prompt[0], { role: "assistant", content: [{ type: "text", text: "ok" }] }] + const a = v2Chat({ prompt }, "swe-2").messages.find((m) => m.role === "assistant") + expect("reasoning_content" in a).toBe(false) +}) + +// Messages to a model that isn't Claude: a thinking block goes back as its +// text, whatever signature it has; to Claude only signed thinking, with it +const messagesLoop = (thinking) => ({ + messages: [ + { role: "user", content: "Read /a." }, + { role: "assistant", content: [thinking, { type: "tool_use", id: "toolu_01", name: "read_file", input: { path: "/a" } }] }, + { role: "user", content: [{ type: "tool_result", tool_use_id: "toolu_01", content: "A" }] }, + ], +}) + +test("Messages: another model's thinking goes back without a signature", () => { + for (const signature of ["", SIG, undefined]) { + const ps = assistant(buildMessages(messagesLoop({ type: "thinking", thinking: "Start with /a.", signature }), "swe-2", "k")) + expect(ps.map((p) => [p.calls, p.thinking, p.signature, p.type])).toEqual([[["read_file"], "Start with /a.", null, null]]) + } + // an empty one sends nothing + const empty = assistant(buildMessages(messagesLoop({ type: "thinking", thinking: "", signature: "" }), "swe-2", "k")) + expect(empty.map((p) => p.thinking)).toEqual([null]) +}) + +test("Messages: Claude's signed thinking is as #665 left it, and its unsigned none", () => { + const signed = assistant(buildMessages(messagesLoop({ type: "thinking", thinking: "Start with /a.", signature: SIG }), "claude-opus-5-5", "k")) + expect(signed.map((p) => [p.calls, p.thinking, p.signature, p.type])).toEqual([[["read_file"], "Start with /a.", SIG, "anthropic"]]) + // a signed block whose text is empty sends its signature without text + const bare = assistant(buildMessages(messagesLoop({ type: "thinking", thinking: "", signature: SIG }), "claude-opus-5-5", "k")) + expect(bare.map((p) => [p.thinking, p.signature])).toEqual([[null, SIG]]) + const unsigned = assistant(buildMessages(messagesLoop({ type: "thinking", thinking: "Start with /a.", signature: "" }), "claude-opus-5-5", "k")) + expect(unsigned.map((p) => [p.thinking, p.signature, p.type])).toEqual([[null, null, null]]) +}) diff --git a/packages/devin/thinking-signature.test.mjs b/packages/devin/thinking-signature.test.mjs index d9b298d..2a80fc6 100644 --- a/packages/devin/thinking-signature.test.mjs +++ b/packages/devin/thinking-signature.test.mjs @@ -156,11 +156,12 @@ test("the signature's type Devin said goes back with it", async () => { expect([a.thinking, a.signature, a.type]).toEqual(["Let me think.", SIG, "anthropic-bedrock"]) }) -test("unsigned thinking, and thinking to another model, goes no further", () => { +test("unsigned thinking to Claude goes no further, and another model's goes without a signature", () => { const unsigned = prompts(buildMessages(history([{ type: "thinking", thinking: "t", signature: "" }, use]), "claude-opus-5-5", "k")).find((p) => p.role === 2) expect([unsigned.thinking, unsigned.signature, unsigned.type]).toEqual([null, null, null]) + // another model's thinking goes back as its text alone (plugins#703) const other = prompts(buildMessages(history([{ type: "thinking", thinking: "t", signature: SIG }, use]), "deepseek-v4-flash-high", "k")).find((p) => p.role === 2) - expect([other.thinking, other.signature, other.type]).toEqual([null, null, null]) + expect([other.thinking, other.signature, other.type]).toEqual(["t", null, null]) // a turn that only thought still sends nothing of its own const ps = prompts(buildMessages({ messages: [{ role: "user", content: "hi" }, { role: "assistant", content: [{ type: "thinking", thinking: "t", signature: SIG }] }] }, "claude-opus-5-5", "k")) expect(ps.some((p) => p.role === 2)).toBe(false)