Skip to content

Commit 4bbf1e6

Browse files
committed
fix(bedrock): split thinking and progress-update blocks streamed under one index
With thinking.display "updates", Bedrock's ConverseStream sends a thinking block and the progress-update (narration) block after it under the same contentBlockIndex: reasoningContent.text "" (thinking, omitted) reasoningContent.signature "…" (decodes to …thinking…) reasoningContent.text "It's working …" reasoningContent.signature "…" (decodes to …narration…) contentBlockStop 3356ddf collected by index, so it sent the pair back as one block with both texts and both signatures concatenated. Seen in a raw API dump of a real Zoo session on the installed VSIX (2026-09-27). Replaying that request as non-streaming Converse with "error" showed the merged block is rejected: messages.111.content.0: Invalid `signature` in `thinking` block. The block is bound to a different conversation. [...] The `thinking` block that preceded this one when it was created is missing from this request. With drop_block the request succeeds, but the API silently drops the block and, per the AWS docs, every later thinking block too. ConverseStream can't show that (see THINKING_BINDING_CONTROLS_BETA). The same replay with the two blocks sent separately succeeded with input_transformations: []. A block now ends when a new text or redacted delta arrives after its signature, so each signature stays with its own text. See also Zoo-Code-Org#1820.
1 parent 1eaafd5 commit 4bbf1e6

2 files changed

Lines changed: 69 additions & 13 deletions

File tree

‎src/api/providers/__tests__/bedrock.spec.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,6 +1959,52 @@ describe("AwsBedrockHandler", () => {
19591959
])
19601960
})
19611961

1962+
it("splits a thinking block and its progress update that Bedrock streams under one index", async () => {
1963+
const provider = new AwsBedrockHandler({
1964+
apiModelId: "global.anthropic.claude-opus-5-5",
1965+
enableReasoningEffort: false,
1966+
modelMaxTokens: 32_000,
1967+
})
1968+
// Shape seen live on 2026-09-27 with display "updates": empty thinking + signature,
1969+
// then the narration's text + its own signature, all at contentBlockIndex 0.
1970+
provider["client"].send = vi.fn().mockResolvedValue({
1971+
stream: asyncStreamFrom([
1972+
{ contentBlockDelta: { contentBlockIndex: 0, delta: { reasoningContent: { text: "" } } } },
1973+
{
1974+
contentBlockDelta: {
1975+
contentBlockIndex: 0,
1976+
delta: { reasoningContent: { signature: "sig-thinking" } },
1977+
},
1978+
},
1979+
{
1980+
contentBlockDelta: {
1981+
contentBlockIndex: 0,
1982+
delta: { reasoningContent: { text: "Checking the " } },
1983+
},
1984+
},
1985+
{ contentBlockDelta: { contentBlockIndex: 0, delta: { reasoningContent: { text: "log next." } } } },
1986+
{
1987+
contentBlockDelta: {
1988+
contentBlockIndex: 0,
1989+
delta: { reasoningContent: { signature: "sig-narration" } },
1990+
},
1991+
},
1992+
{ contentBlockStop: { contentBlockIndex: 0 } },
1993+
{ messageStop: { stopReason: "end_turn" } },
1994+
]),
1995+
})
1996+
1997+
const chunks = await collectStream(provider.createMessage("System prompt", messages))
1998+
1999+
expect(chunks.filter((chunk) => chunk.type === "thinking_block")).toEqual([
2000+
{ type: "thinking_block", block: { type: "thinking", thinking: "", signature: "sig-thinking" } },
2001+
{
2002+
type: "thinking_block",
2003+
block: { type: "thinking", thinking: "Checking the log next.", signature: "sig-narration" },
2004+
},
2005+
])
2006+
})
2007+
19622008
it("does not collect reasoning blocks for models that don't round-trip them", async () => {
19632009
const provider = new AwsBedrockHandler({
19642010
apiModelId: "anthropic.claude-opus-4-8",

‎src/api/providers/bedrock.ts‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ const THINKING_BINDING_CONTROLS_BETA = "thinking-binding-controls-2026-08-01"
164164

165165
type ThinkingBlockBinding = { prefix_mismatch_behavior: "error" | "drop_block" }
166166

167+
type ThinkingBlockInProgress = { text: string; signature: string; redacted?: Uint8Array }
168+
169+
function toThinkingBlockChunk(block: ThinkingBlockInProgress): ApiStreamChunk {
170+
return block.redacted
171+
? {
172+
type: "thinking_block",
173+
block: { type: "redacted_thinking", data: Buffer.from(block.redacted).toString("base64") },
174+
}
175+
: { type: "thinking_block", block: { type: "thinking", thinking: block.text, signature: block.signature } }
176+
}
177+
167178
// Define interface for Bedrock additional model request fields
168179
// This includes thinking configuration, 1M context beta, and other model-specific parameters
169180
interface BedrockAdditionalModelFields {
@@ -699,8 +710,8 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
699710
thinkingDisplay.block_binding = { prefix_mismatch_behavior: "drop_block" }
700711
anthropicBetas.push(THINKING_BINDING_CONTROLS_BETA)
701712
}
702-
// Reasoning blocks of this response by content-block index, emitted whole at contentBlockStop.
703-
const thinkingBlocks = new Map<number, { text: string; signature: string; redacted?: Uint8Array }>()
713+
// Reasoning blocks of this response by content-block index, emitted whole once complete.
714+
const thinkingBlocks = new Map<number, ThinkingBlockInProgress>()
704715

705716
// Apply anthropic_beta to additionalModelRequestFields if any betas are needed
706717
if (anthropicBetas.length > 0) {
@@ -949,7 +960,14 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
949960
const reasoning = delta.reasoningContent
950961
if (keepThinking && reasoning) {
951962
const index = cbDelta.contentBlockIndex ?? 0
952-
const block = thinkingBlocks.get(index) ?? { text: "", signature: "" }
963+
let block = thinkingBlocks.get(index) ?? { text: "", signature: "" }
964+
// Under display "updates" Bedrock streams a thinking block and its progress-update
965+
// (narration) block under one index; each ends with its own signature, and
966+
// sending them back merged fails the signature check.
967+
if (block.signature && (reasoning.text !== undefined || reasoning.redactedContent)) {
968+
yield toThinkingBlockChunk(block)
969+
block = { text: "", signature: "" }
970+
}
953971
block.text += reasoning.text ?? ""
954972
block.signature += reasoning.signature ?? ""
955973
if (reasoning.redactedContent) {
@@ -992,16 +1010,8 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
9921010
const index = streamEvent.contentBlockStop.contentBlockIndex ?? 0
9931011
const block = thinkingBlocks.get(index)
9941012
thinkingBlocks.delete(index)
995-
if (block?.redacted) {
996-
yield {
997-
type: "thinking_block",
998-
block: { type: "redacted_thinking", data: Buffer.from(block.redacted).toString("base64") },
999-
}
1000-
} else if (block?.signature) {
1001-
yield {
1002-
type: "thinking_block",
1003-
block: { type: "thinking", thinking: block.text, signature: block.signature },
1004-
}
1013+
if (block?.redacted || block?.signature) {
1014+
yield toThinkingBlockChunk(block)
10051015
}
10061016
continue
10071017
}

0 commit comments

Comments
 (0)