Skip to content

Commit 42173bc

Browse files
authored
fix(opencode): preserve signed thinking during anthropic reorder (#30182)
1 parent 0294342 commit 42173bc

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

‎packages/opencode/src/provider/transform.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,18 @@ function normalizeMessages(
230230
const parts = msg.content
231231
const first = parts.findIndex((part) => part.type === "tool-call")
232232
if (first === -1) return [msg]
233+
// Anthropic signs thinking blocks, so moving them during replay invalidates the request.
234+
if (
235+
parts
236+
.slice(first)
237+
.some(
238+
(part) =>
239+
part.type === "reasoning" &&
240+
(part.providerOptions?.anthropic?.signature != null ||
241+
part.providerOptions?.anthropic?.redactedData != null),
242+
)
243+
)
244+
return [msg]
233245
if (!parts.slice(first).some((part) => part.type !== "tool-call")) return [msg]
234246
return [
235247
{ ...msg, content: parts.filter((part) => part.type !== "tool-call") },

‎packages/opencode/test/provider/transform.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,6 +1680,25 @@ describe("ProviderTransform.message - anthropic empty content filtering", () =>
16801680
])
16811681
})
16821682

1683+
test("leaves signed anthropic reasoning after tool calls unchanged", () => {
1684+
const msgs = [
1685+
{
1686+
role: "assistant",
1687+
content: [
1688+
{ type: "reasoning", text: "First thought", providerOptions: { anthropic: { signature: "sig-1" } } },
1689+
{ type: "tool-call", toolCallId: "toolu_1", toolName: "read", input: { filePath: "/root" } },
1690+
{ type: "reasoning", text: "Second thought", providerOptions: { anthropic: { signature: "sig-2" } } },
1691+
{ type: "tool-call", toolCallId: "toolu_2", toolName: "glob", input: { pattern: "**/*.pdf" } },
1692+
],
1693+
},
1694+
] as any[]
1695+
1696+
const result = ProviderTransform.message(msgs, anthropicModel, {}) as any[]
1697+
1698+
expect(result).toHaveLength(1)
1699+
expect(result[0].content).toMatchObject(msgs[0].content)
1700+
})
1701+
16831702
test("splits vertex anthropic assistant messages when text trails tool calls", () => {
16841703
const model = {
16851704
...anthropicModel,
@@ -1717,6 +1736,34 @@ describe("ProviderTransform.message - anthropic empty content filtering", () =>
17171736
],
17181737
})
17191738
})
1739+
1740+
test("leaves redacted vertex anthropic reasoning after tool calls unchanged", () => {
1741+
const model = {
1742+
...anthropicModel,
1743+
providerID: "google-vertex-anthropic",
1744+
api: {
1745+
id: "claude-sonnet-4@20250514",
1746+
url: "https://us-central1-aiplatform.googleapis.com",
1747+
npm: "@ai-sdk/google-vertex/anthropic",
1748+
},
1749+
}
1750+
1751+
const msgs = [
1752+
{
1753+
role: "assistant",
1754+
content: [
1755+
{ type: "tool-call", toolCallId: "toolu_1", toolName: "read", input: { filePath: "/root" } },
1756+
{ type: "reasoning", text: "", providerOptions: { anthropic: { redactedData: "redacted-1" } } },
1757+
{ type: "tool-call", toolCallId: "toolu_2", toolName: "glob", input: { pattern: "**/*.pdf" } },
1758+
],
1759+
},
1760+
] as any[]
1761+
1762+
const result = ProviderTransform.message(msgs, model, {}) as any[]
1763+
1764+
expect(result).toHaveLength(1)
1765+
expect(result[0].content).toMatchObject(msgs[0].content)
1766+
})
17201767
})
17211768

17221769
describe("ProviderTransform.message - strip openai metadata when store=false", () => {

0 commit comments

Comments
 (0)