aws.internal._merge_adjacent_roles

(internal) Converse requires strict user/assistant alternation, and several paths here can put two same-role messages side by side: a dropped assistant turn (a reasoning-only reply lowers to no content) leaves the user turns that bracketed it adjacent, the rendered prompt can end on the same role the journal opens with, and consecutive toolResults arrive as separate `user` messages when a journal-only event splits their group.

Reference version

Signature

function aws.internal._merge_adjacent_roles(messages: aws.internal.Message[]) -> aws.internal.Message[]

(internal) Converse requires strict user/assistant alternation, and several paths here can put two same-role messages side by side: a dropped assistant turn (a reasoning-only reply lowers to no content) leaves the user turns that bracketed it adjacent, the rendered prompt can end on the same role the journal opens with, and consecutive toolResults arrive as separate user messages when a journal-only event splits their group.

Merging by concatenating content lists repairs every one of those, not just the shape each guard was written for. Block order inside each message is preserved and messages are joined in order, so a cachePoint still sits after exactly the blocks it was marking. _push_tool_result's grouping becomes a special case of this.

Source:<builtin>/aws/ns_internal/bedrock.bamlbytes 3809138595