Skip to content

Commit 5876d73

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 3356ddf commit 5876d73

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
@@ -1953,6 +1953,52 @@ describe("AwsBedrockHandler", () => {
19531953
])
19541954
})
19551955

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

163163
type ThinkingBlockBinding = { prefix_mismatch_behavior: "error" | "drop_block" }
164164

165+
type ThinkingBlockInProgress = { text: string; signature: string; redacted?: Uint8Array }
166+
167+
function toThinkingBlockChunk(block: ThinkingBlockInProgress): ApiStreamChunk {
168+
return block.redacted
169+
? {
170+
type: "thinking_block",
171+
block: { type: "redacted_thinking", data: Buffer.from(block.redacted).toString("base64") },
172+
}
173+
: { type: "thinking_block", block: { type: "thinking", thinking: block.text, signature: block.signature } }
174+
}
175+
165176
// Define interface for Bedrock additional model request fields
166177
// This includes thinking configuration, 1M context beta, and other model-specific parameters
167178
interface BedrockAdditionalModelFields {
@@ -697,8 +708,8 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
697708
thinkingDisplay.block_binding = { prefix_mismatch_behavior: "drop_block" }
698709
anthropicBetas.push(THINKING_BINDING_CONTROLS_BETA)
699710
}
700-
// Reasoning blocks of this response by content-block index, emitted whole at contentBlockStop.
701-
const thinkingBlocks = new Map<number, { text: string; signature: string; redacted?: Uint8Array }>()
711+
// Reasoning blocks of this response by content-block index, emitted whole once complete.
712+
const thinkingBlocks = new Map<number, ThinkingBlockInProgress>()
702713

703714
// Apply anthropic_beta to additionalModelRequestFields if any betas are needed
704715
if (anthropicBetas.length > 0) {
@@ -947,7 +958,14 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
947958
const reasoning = delta.reasoningContent
948959
if (keepThinking && reasoning) {
949960
const index = cbDelta.contentBlockIndex ?? 0
950-
const block = thinkingBlocks.get(index) ?? { text: "", signature: "" }
961+
let block = thinkingBlocks.get(index) ?? { text: "", signature: "" }
962+
// Under display "updates" Bedrock streams a thinking block and its progress-update
963+
// (narration) block under one index; each ends with its own signature, and
964+
// sending them back merged fails the signature check.
965+
if (block.signature && (reasoning.text !== undefined || reasoning.redactedContent)) {
966+
yield toThinkingBlockChunk(block)
967+
block = { text: "", signature: "" }
968+
}
951969
block.text += reasoning.text ?? ""
952970
block.signature += reasoning.signature ?? ""
953971
if (reasoning.redactedContent) {
@@ -990,16 +1008,8 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
9901008
const index = streamEvent.contentBlockStop.contentBlockIndex ?? 0
9911009
const block = thinkingBlocks.get(index)
9921010
thinkingBlocks.delete(index)
993-
if (block?.redacted) {
994-
yield {
995-
type: "thinking_block",
996-
block: { type: "redacted_thinking", data: Buffer.from(block.redacted).toString("base64") },
997-
}
998-
} else if (block?.signature) {
999-
yield {
1000-
type: "thinking_block",
1001-
block: { type: "thinking", thinking: block.text, signature: block.signature },
1002-
}
1011+
if (block?.redacted || block?.signature) {
1012+
yield toThinkingBlockChunk(block)
10031013
}
10041014
continue
10051015
}

0 commit comments

Comments
 (0)