detached 8e8b1da

Deduplicate tool results in provider requests

commit
8e8b1daa959d84447c9b7a9bbacb093fbb749a44
author
patriceckhart · · 2026-08-27 09:25 UTC
parent
82191b3
signature
unsigned
refs
tag: v0.3.51
2 files · +45 -7
M packages/provider/provider.go +9 -7
@@ -77,12 +77,12 @@ type ReasoningBlock struct {
7777
7878 func (ReasoningBlock) isContent() {}
7979
80-// RepairOrphanedToolResults removes tool_result content blocks (and
81-// entire messages that become empty) when the matching tool_use ID
82-// does not appear anywhere in the given messages. Resume tails,
83-// compaction repair, and provider request builders all need this so
84-// the upstream API never sees a tool_call_id with no corresponding
85-// assistant tool_call earlier in the same request.
80+// RepairOrphanedToolResults removes invalid tool_result content blocks
81+// and entire messages that become empty. A result is invalid when its
82+// matching tool_use ID does not appear anywhere in the messages or when
83+// an earlier result already covers the same ID. Resume tails, compaction
84+// repair, and provider request builders all need this so the upstream API
85+// sees exactly one result for each referenced tool call.
8686 func RepairOrphanedToolResults(msgs []Message) []Message {
8787 useIDs := map[string]bool{}
8888 for _, m := range msgs {
@@ -92,14 +92,16 @@ func RepairOrphanedToolResults(msgs []Message) []Message {
9292 }
9393 }
9494 }
95+ resultIDs := map[string]bool{}
9596 out := make([]Message, 0, len(msgs))
9697 for _, m := range msgs {
9798 var filtered []Content
9899 for _, c := range m.Content {
99100 if tr, ok := c.(ToolResultBlock); ok {
100- if !useIDs[tr.CallID] {
101+ if !useIDs[tr.CallID] || resultIDs[tr.CallID] {
101102 continue
102103 }
104+ resultIDs[tr.CallID] = true
103105 }
104106 filtered = append(filtered, c)
105107 }
@@ -77,12 +77,12 @@ type ReasoningBlock struct {
77 77
78 func (ReasoningBlock) isContent() {}78 func (ReasoningBlock) isContent() {}
79 79
80-// RepairOrphanedToolResults removes tool_result content blocks (and80+// RepairOrphanedToolResults removes invalid tool_result content blocks
81-// entire messages that become empty) when the matching tool_use ID81+// and entire messages that become empty. A result is invalid when its
82-// does not appear anywhere in the given messages. Resume tails,82+// matching tool_use ID does not appear anywhere in the messages or when
83-// compaction repair, and provider request builders all need this so83+// an earlier result already covers the same ID. Resume tails, compaction
84-// the upstream API never sees a tool_call_id with no corresponding84+// repair, and provider request builders all need this so the upstream API
85-// assistant tool_call earlier in the same request.85+// sees exactly one result for each referenced tool call.
86 func RepairOrphanedToolResults(msgs []Message) []Message {86 func RepairOrphanedToolResults(msgs []Message) []Message {
87 useIDs := map[string]bool{}87 useIDs := map[string]bool{}
88 for _, m := range msgs {88 for _, m := range msgs {
@@ -92,14 +92,16 @@ func RepairOrphanedToolResults(msgs []Message) []Message {
92 }92 }
93 }93 }
94 }94 }
95+ resultIDs := map[string]bool{}
95 out := make([]Message, 0, len(msgs))96 out := make([]Message, 0, len(msgs))
96 for _, m := range msgs {97 for _, m := range msgs {
97 var filtered []Content98 var filtered []Content
98 for _, c := range m.Content {99 for _, c := range m.Content {
99 if tr, ok := c.(ToolResultBlock); ok {100 if tr, ok := c.(ToolResultBlock); ok {
100- if !useIDs[tr.CallID] {101+ if !useIDs[tr.CallID] || resultIDs[tr.CallID] {
101 continue102 continue
102 }103 }
104+ resultIDs[tr.CallID] = true
103 }105 }
104 filtered = append(filtered, c)106 filtered = append(filtered, c)
105 }107 }
M packages/provider/provider_test.go +36 -0
@@ -277,6 +277,42 @@ func TestAnthropicBuildRequestStripsAssistantImages(t *testing.T) {
277277 }
278278 }
279279
280+func TestAnthropicBuildRequestDeduplicatesToolResults(t *testing.T) {
281+ c := NewAnthropic("x", "").(*anthropicClient)
282+ wire, err := c.buildRequest(Request{
283+ Model: "claude-sonnet-4-5",
284+ Messages: []Message{
285+ {Role: RoleUser, Content: []Content{TextBlock{Text: "read the file"}}},
286+ {Role: RoleAssistant, Content: []Content{
287+ ToolCallBlock{ID: "tool-1", Name: "read"},
288+ }},
289+ {Role: RoleTool, Content: []Content{
290+ ToolResultBlock{CallID: "tool-1", Content: []Content{TextBlock{Text: "first result"}}},
291+ }},
292+ {Role: RoleTool, Content: []Content{
293+ ToolResultBlock{CallID: "tool-1", Content: []Content{TextBlock{Text: "duplicate result"}}},
294+ }},
295+ },
296+ })
297+ if err != nil {
298+ t.Fatal(err)
299+ }
300+ if len(wire.Messages) != 3 {
301+ t.Fatalf("messages=%d, want 3 after dropping duplicate result message", len(wire.Messages))
302+ }
303+ blocks := wire.Messages[2].Content
304+ if len(blocks) != 1 {
305+ t.Fatalf("tool result blocks=%d, want 1", len(blocks))
306+ }
307+ result, ok := blocks[0].(anthToolResultBlock)
308+ if !ok {
309+ t.Fatalf("tool result block type=%T", blocks[0])
310+ }
311+ if string(result.Content) != `"first result"` {
312+ t.Fatalf("tool result content=%s", result.Content)
313+ }
314+}
315+
280316 func TestAnthropicStreamHappyPath(t *testing.T) {
281317 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
282318 w.Header().Set("content-type", "text/event-stream")
@@ -277,6 +277,42 @@ func TestAnthropicBuildRequestStripsAssistantImages(t *testing.T) {
277 }277 }
278 }278 }
279 279
280+func TestAnthropicBuildRequestDeduplicatesToolResults(t *testing.T) {
281+ c := NewAnthropic("x", "").(*anthropicClient)
282+ wire, err := c.buildRequest(Request{
283+ Model: "claude-sonnet-4-5",
284+ Messages: []Message{
285+ {Role: RoleUser, Content: []Content{TextBlock{Text: "read the file"}}},
286+ {Role: RoleAssistant, Content: []Content{
287+ ToolCallBlock{ID: "tool-1", Name: "read"},
288+ }},
289+ {Role: RoleTool, Content: []Content{
290+ ToolResultBlock{CallID: "tool-1", Content: []Content{TextBlock{Text: "first result"}}},
291+ }},
292+ {Role: RoleTool, Content: []Content{
293+ ToolResultBlock{CallID: "tool-1", Content: []Content{TextBlock{Text: "duplicate result"}}},
294+ }},
295+ },
296+ })
297+ if err != nil {
298+ t.Fatal(err)
299+ }
300+ if len(wire.Messages) != 3 {
301+ t.Fatalf("messages=%d, want 3 after dropping duplicate result message", len(wire.Messages))
302+ }
303+ blocks := wire.Messages[2].Content
304+ if len(blocks) != 1 {
305+ t.Fatalf("tool result blocks=%d, want 1", len(blocks))
306+ }
307+ result, ok := blocks[0].(anthToolResultBlock)
308+ if !ok {
309+ t.Fatalf("tool result block type=%T", blocks[0])
310+ }
311+ if string(result.Content) != `"first result"` {
312+ t.Fatalf("tool result content=%s", result.Content)
313+ }
314+}
315+
280 func TestAnthropicStreamHappyPath(t *testing.T) {316 func TestAnthropicStreamHappyPath(t *testing.T) {
281 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {317 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
282 w.Header().Set("content-type", "text/event-stream")318 w.Header().Set("content-type", "text/event-stream")

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close