Skip to content

Commit c17c025

Browse files
committed
fix(opencode): preserve reasoning providerMetadata across model switches
Anthropic thinking blocks carry a cryptographic signature in providerMetadata that must survive every turn of a multi-turn conversation. The differentModel guard was correctly stripping provider-specific metadata from text and tool parts, but incorrectly doing the same for reasoning parts, causing Anthropic to reject the request with 'thinking blocks cannot be modified'. Fixes anomalyco#22813
1 parent 74b14a2 commit c17c025

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

‎packages/opencode/src/session/message-v2.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ export namespace MessageV2 {
793793
assistantMessage.parts.push({
794794
type: "reasoning",
795795
text: part.text,
796-
...(differentModel ? {} : { providerMetadata: part.metadata }),
796+
providerMetadata: part.metadata,
797797
})
798798
}
799799
}

‎packages/opencode/test/session/message-v2.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,49 @@ describe("session.message-v2.toModelMessage", () => {
434434
])
435435
})
436436

437+
test("preserves reasoning providerMetadata even when assistant model differs", async () => {
438+
const userID = "m-user"
439+
const assistantID = "m-assistant"
440+
441+
const input: MessageV2.WithParts[] = [
442+
{
443+
info: userInfo(userID),
444+
parts: [
445+
{
446+
...basePart(userID, "u1"),
447+
type: "text",
448+
text: "think about this",
449+
},
450+
] as MessageV2.Part[],
451+
},
452+
{
453+
info: assistantInfo(assistantID, userID, undefined, { providerID: "other", modelID: "other" }),
454+
parts: [
455+
{
456+
...basePart(assistantID, "a1"),
457+
type: "reasoning",
458+
text: "reasoning trace",
459+
time: { start: 0 },
460+
metadata: { anthropic: { signature: "sig-abc" } },
461+
},
462+
] as MessageV2.Part[],
463+
} as MessageV2.WithParts,
464+
]
465+
466+
expect(await MessageV2.toModelMessages(input, model)).toStrictEqual([
467+
{
468+
role: "user",
469+
content: [{ type: "text", text: "think about this" }],
470+
},
471+
{
472+
role: "assistant",
473+
content: [
474+
{ type: "reasoning", text: "reasoning trace", providerOptions: { anthropic: { signature: "sig-abc" } } },
475+
],
476+
},
477+
])
478+
})
479+
437480
test("replaces compacted tool output with placeholder", async () => {
438481
const userID = "m-user"
439482
const assistantID = "m-assistant"

0 commit comments

Comments
 (0)