Commit 3e7b0bf
fix: use correct slice index for Bedrock tool result coalescing (#2237)
**Description**
Fix two bugs in the Anthropic-to-Bedrock translator message merging
logic at `internal/translator/anthropic_awsbedrock.go`.
**Bug 1: Wrong slice index (original report)**
When system messages are present in the request body,
`promoteAnthropicSystemMessagesToParam` filters them out, producing a
shorter `messages` slice. The coalescing loop was using
`body.Messages[i+1]` (the original unfiltered slice) instead of
`messages[i+1]` (the filtered slice), causing index misalignment that
could read wrong messages or panic with out-of-bounds access.
**Bug 2: Coalescing logic was unreachable (root cause)**
Tool result messages have `Role: "user"`, so they match `case
anthropicschema.MessageRoleUser` and go through `convertUserMessage()` —
which handles tool results individually but never coalesces consecutive
ones. The coalescing logic in the `default` branch was dead code for
tool results since they never reach that branch.
**Changes**
- Moved the `hasToolResult` check + coalescing loop into the
`MessageRoleUser` case branch
- Non-tool-result user messages still go through `convertUserMessage()`
- Changed `body.Messages[i+1]` → `messages[i+1]` to use the correct
filtered slice
- Simplified the `default` branch to just return an error for unexpected
roles
- Updated existing test to expect coalesced output (3 messages instead
of 4)
- Added regression test with system messages to verify index alignment
**Testing**
- All existing translator tests pass
- New
`TestAnthropicToAWSBedrockTranslator_RequestBody_ToolResultMessagesWithSystemMessages`
verifies the fix works when system messages are present alongside
consecutive tool results
---------
Signed-off-by: liuhy <liuhongyu@apache.org>
Co-authored-by: Ignasi Barrera <ignasi@tetrate.io>1 parent 3456009 commit 3e7b0bf
3 files changed
Lines changed: 300 additions & 43 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | 81 | | |
98 | 82 | | |
99 | 83 | | |
100 | | - | |
101 | | - | |
| 84 | + | |
| 85 | + | |
102 | 86 | | |
103 | 87 | | |
104 | 88 | | |
105 | 89 | | |
106 | 90 | | |
107 | 91 | | |
108 | | - | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
109 | 97 | | |
110 | 98 | | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
111 | 104 | | |
112 | 105 | | |
113 | 106 | | |
| |||
236 | 229 | | |
237 | 230 | | |
238 | 231 | | |
239 | | - | |
| 232 | + | |
240 | 233 | | |
241 | 234 | | |
242 | 235 | | |
243 | 236 | | |
244 | 237 | | |
245 | | - | |
| 238 | + | |
246 | 239 | | |
247 | 240 | | |
248 | 241 | | |
| |||
277 | 270 | | |
278 | 271 | | |
279 | 272 | | |
280 | | - | |
| 273 | + | |
281 | 274 | | |
282 | 275 | | |
283 | 276 | | |
| |||
0 commit comments