Files
plugins/packages/devin/thinking-signature.test.mjs
T
yetone a064cf15e1 devin 0.1.35: Claude's thinking keeps its signature and goes back with it (#665)
Vigilans (#665): GetChatMessage's reply carries the thinking's signature
in delta_signature (10) and its type in delta_signature_type (21), and
ChatMessagePrompt takes the thinking back in thinking (11), signature
(12) and signature_type (18). The plugin read only delta_thinking (9),
so /v1/messages answered thinking blocks with signature "", and the
next request left the assistant's thinking out. Field numbers checked
against devin2api's all-protos.proto (GetChatMessageResponse,
ExaChatPb_ChatMessagePrompt) at e8547b6d.

- events() yields {signature} for field 10 and {signatureType} for 21.
- Messages, whole: the signature goes on the last thinking block. Stream:
  a signature_delta in the open thinking block, before its stop, as
  Anthropic streams it. Only when the request's model is Claude; another
  model's thinking stays unsigned, as before.
- The type is kept by signature (the last 512) since a Messages block has
  no field for it; one not kept goes back as "anthropic", what Devin says
  for Claude.
- buildMessages, to a Claude model: a signed thinking block goes back
  on the assistant prompt after it (its text, or the calls' prompt);
  a second one before any prompt gets an empty prompt of its own, and
  one after the text with calls following goes on the calls' prompt.
  Unsigned thinking, thinking to a model that isn't Claude, and a turn
  that only thought send nothing, as before.
- The chat completion and Messages streams read past events they send
  nothing for: a pull that enqueues nothing isn't pulled again in Bun,
  and the Messages stream hung on one (seen in the new tests before the
  loop).

Not changed: chat completions (and OpenCode 2's language model, which
goes through them) have no field for a signature, so they carry the
thinking text only, as before. thinking_redacted (reply 11, prompt 13)
isn't read or sent.

Run at this head on mote:
- bun test packages/devin (HOME a temp dir): 199 pass, 0 fail.
- repo root bun test: 5175 pass, 0 fail (15 skip); bun scripts/check.mjs ok.
- thinking-signature.test.mjs on origin/main 311d628: the six tests of
  the signature (read, whole, stream, replay, two thinking blocks, type)
  red; the four "as before" tests pass.
Not tried: a real Devin account (none in a sandbox); frames are built
from the proto's field numbers, not captured bytes.
2026-11-03 17:28:11 +08:00

182 lines
8.8 KiB
JavaScript

// Claude's thinking through Devin keeps its signature (plugins#665,
// Vigilans). GetChatMessageResponse carries it in delta_signature (field
// 10) and its type in delta_signature_type (21), beside delta_thinking (9);
// ChatMessagePrompt takes the thinking back in thinking (11), signature
// (12) and signature_type (18). The field numbers are the ones in Devin's
// proto (devin2api's all-protos.proto: GetChatMessageResponse,
// ExaChatPb_ChatMessagePrompt); the frames below are built from them, not
// captured from Devin.
import { afterEach, expect, test } from "bun:test"
import { _internal } from "./index.mjs"
const { PB, frame, fields, buildMessages, completeMessages, build, events, familiesOf, SNAPSHOT } = _internal
const dec = new TextDecoder()
const enc = new TextEncoder()
const real = globalThis.fetch
afterEach(() => {
globalThis.fetch = real
_internal.forgetSigTypes?.()
})
const SIG = "EqQBCkgIBRABGAIiQL2xYzRvY0RzZWNyZXRzaWduYXR1cmU="
// tail is a Connect stream's end frame, {}
const tail = () => {
const end = enc.encode("{}")
const t = new Uint8Array(5 + end.length)
t[0] = 2
new DataView(t.buffer).setUint32(1, end.length)
t.set(end, 5)
return t
}
// reply is Devin's answer: thinking, its signature split in two deltas
// (the second frame also says the type), then text and the finish
const reply = (type = "anthropic") => {
const usage = new PB().varint(2, 10).varint(3, 5).done()
return new Response(
new Blob([
frame(new PB().str(9, "Let me think.").done()),
frame(new PB().str(10, SIG.slice(0, 20)).done()),
frame(new PB().str(10, SIG.slice(20)).str(21, type).done()),
frame(new PB().str(3, "Hello.").done()),
frame(new PB().varint(5, 1).bytes(7, usage).done()),
tail(),
]),
)
}
const ctx = { key: "k", server: "https://devin.test", families: familiesOf(SNAPSHOT) }
const req = (model, stream) => ({ model, max_tokens: 64, stream, thinking: { type: "adaptive" }, messages: [{ role: "user", content: "hi" }] })
// prompts is each ChatMessagePrompt a request sends: its role, text,
// calls, and the thinking it carries back
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
})
test("the reply's signature and its type are read", async () => {
const got = []
async function* fr() {
yield { data: new PB().str(9, "hm").str(10, SIG).str(21, "anthropic").done() }
yield { data: new PB().str(3, "ok").done() }
}
for await (const e of events(fr())) got.push(e)
expect(got.slice(0, 4)).toEqual([{ reasoning: "hm" }, { signature: SIG }, { signatureType: "anthropic" }, { text: "ok" }])
})
test("a whole Messages reply's thinking carries Claude's signature", async () => {
globalThis.fetch = async () => reply()
const j = await (await completeMessages(ctx, req("claude-opus-5-5", false))).json()
expect(j.content).toEqual([
{ type: "thinking", thinking: "Let me think.", signature: SIG },
{ type: "text", text: "Hello." },
])
})
test("a streamed Messages reply sends the signature before the thinking block stops", async () => {
globalThis.fetch = async () => reply()
const res = await completeMessages(ctx, req("claude-opus-5-5", true))
const ev = (await res.text()).split("\n").filter((l) => l.startsWith("data: ")).map((l) => JSON.parse(l.slice(6)))
expect(ev.map((e) => e.type + (e.delta?.type ? ":" + e.delta.type : ""))).toEqual([
"message_start",
"content_block_start",
"content_block_delta:thinking_delta",
"content_block_delta:signature_delta",
"content_block_delta:signature_delta",
"content_block_stop",
"content_block_start",
"content_block_delta:text_delta",
"content_block_stop",
"message_delta",
"message_stop",
])
expect(ev[1].content_block).toEqual({ type: "thinking", thinking: "", signature: "" })
expect(ev[3].delta.signature + ev[4].delta.signature).toBe(SIG)
expect(ev[3].index).toBe(0)
})
test("another model's thinking stays unsigned, as before", async () => {
globalThis.fetch = async () => reply("openai")
const j = await (await completeMessages(ctx, req("deepseek-v4-flash", false))).json()
expect(j.content[0]).toEqual({ type: "thinking", thinking: "Let me think.", signature: "" })
globalThis.fetch = async () => reply("openai")
const ev = (await (await completeMessages(ctx, req("deepseek-v4-flash", true))).text()).split("\n").filter((l) => l.startsWith("data: ")).map((l) => JSON.parse(l.slice(6)))
expect(ev.some((e) => e.delta?.type === "signature_delta")).toBe(false)
})
const history = (assistant) => ({
model: "claude-opus-5-5",
messages: [
{ role: "user", content: "Read /a." },
{ role: "assistant", content: assistant },
{ role: "user", content: [{ type: "tool_result", tool_use_id: "toolu_01", content: "A" }] },
],
})
const use = { type: "tool_use", id: "toolu_01", name: "read_file", input: { path: "/a" } }
test("the next request sends Claude's signed thinking back on the prompt that follows it", () => {
const ps = prompts(buildMessages(history([{ type: "thinking", thinking: "Read it first.", signature: SIG }, { type: "text", text: "Reading it." }, use]), "claude-opus-5-5", "k"))
const a = ps.find((p) => p.role === 2)
expect(a).toEqual({ role: 2, text: "Reading it.", calls: ["read_file"], thinking: "Read it first.", signature: SIG, type: "anthropic" })
// with no text, the calls' prompt carries it
const only = prompts(buildMessages(history([{ type: "thinking", thinking: "t", signature: SIG }, use]), "claude-opus-5-5", "k")).find((p) => p.role === 2)
expect(only).toEqual({ role: 2, text: "", calls: ["read_file"], thinking: "t", signature: SIG, type: "anthropic" })
})
test("thinking after the text goes on a prompt of its own ahead of the calls", () => {
const ps = prompts(
buildMessages(
history([{ type: "thinking", thinking: "one", signature: "s1" }, { type: "text", text: "Reading it." }, { type: "thinking", thinking: "two", signature: "s2" }, use]),
"claude-opus-5-5",
"k",
),
).filter((p) => p.role === 2)
expect(ps.map((p) => [p.text, p.calls, p.thinking, p.signature])).toEqual([
["Reading it.", [], "one", "s1"],
["", ["read_file"], "two", "s2"],
])
})
test("the signature's type Devin said goes back with it", async () => {
globalThis.fetch = async () => reply("anthropic-bedrock")
const j = await (await completeMessages(ctx, req("claude-opus-5-5", false))).json()
expect(_internal.sigTypeOf?.(SIG)).toBe("anthropic-bedrock")
const a = prompts(buildMessages(history([...j.content, use]), "claude-opus-5-5", "k")).find((p) => p.role === 2)
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", () => {
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])
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])
// 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)
})
test("a chat completion's request is the same as before", () => {
const chat = { messages: [{ role: "user", content: "hi" }, { role: "assistant", content: "yo", reasoning_content: "t" }, { role: "user", content: "go" }] }
const ps = prompts(build(chat, "claude-opus-5-5", "k"))
expect(ps.every((p) => p.thinking === null && p.signature === null && p.type === null)).toBe(true)
})
test("a streamed chat completion passes the signature over and still ends", async () => {
globalThis.fetch = async () => reply()
const res = await _internal.complete(ctx, { model: "claude-opus-5-5", stream: true, messages: [{ role: "user", content: "hi" }] })
const lines = (await res.text()).split("\n").filter((l) => l.startsWith("data: "))
expect(lines[lines.length - 1]).toBe("data: [DONE]")
expect(lines.some((l) => l.includes(SIG.slice(0, 20)))).toBe(false)
})