diff --git a/internal/channels/telegram/live_test.go b/internal/channels/telegram/live_test.go index 756a4193a..4ce5f9697 100644 --- a/internal/channels/telegram/live_test.go +++ b/internal/channels/telegram/live_test.go @@ -185,6 +185,42 @@ func TestLiveReplySkipsUnchangedRenders(t *testing.T) { } } +func TestLiveReplyCollapsesRepeatedSuccessfulEquivalentToolCalls(t *testing.T) { + live, calls := newTestLiveReply(t, true) + + for range 14 { + live.ToolCall(toolEvent("grep", "/src/a.go:12:match\n/src/b.go:40:match", "")) + } + live.finalize(context.Background(), "done") + + got := (*calls)[len(*calls)-1].body() + want := "🔧 grep — done ×14\n\n2 matches\n\ndone" + if got != want { + t.Fatalf("repeated successful grep calls rendered as:\n%s\nwant:\n%s", got, want) + } + if strings.Count(got, "🔧 grep") != 1 { + t.Fatalf("repeated successful grep calls should occupy one entry, got %d:\n%s", strings.Count(got, "🔧 grep"), got) + } +} + +func TestLiveReplyDoesNotCollapseSuccessfulCallsWithDifferentResults(t *testing.T) { + live, calls := newTestLiveReply(t, true) + + live.ToolCall(toolEvent("grep", "/src/a.go:12:match\n/src/b.go:40:match", "")) + live.ToolCall(toolEvent("grep", "/src/a.go:12:match\n/src/b.go:40:match", "")) + live.ToolCall(toolEvent("grep", "/src/a.go:12:match", "")) + live.ToolCall(toolEvent("rg", "/src/a.go:12:match\n/src/b.go:40:match", "")) + live.finalize(context.Background(), "done") + + got := (*calls)[len(*calls)-1].body() + if strings.Count(got, "🔧 ") != 3 { + t.Fatalf("successful calls with different tools or summaries were collapsed:\n%s", got) + } + if !strings.Contains(got, "🔧 grep — done ×2") || strings.Contains(got, "×3") { + t.Fatalf("only equivalent successful calls should be counted together:\n%s", got) + } +} + func TestLiveReplySnapshotsToolCallVisibility(t *testing.T) { live, calls := newTestLiveReply(t, true) live.g.SetShowToolCalls(false) diff --git a/internal/gateway/tool_render_test.go b/internal/gateway/tool_render_test.go index 0cdc89362..241eff91e 100644 --- a/internal/gateway/tool_render_test.go +++ b/internal/gateway/tool_render_test.go @@ -49,6 +49,25 @@ func TestToolCallEventRenderToolCallBoundsOutputAndPreservesFailure(t *testing.T } } +func TestToolCallEventSuccessKeysCollapseEquivalentResultsWithoutColliding(t *testing.T) { + equivalent := ToolCallEvent{Name: "grep", Output: "/src/a.go:12:match\n/src/b.go:40:match"} + equivalentCopy := ToolCallEvent{Name: "grep", Output: "/src/a.go:99:different details\n/src/b.go:7:another detail"} + differentSummary := ToolCallEvent{Name: "grep", Output: "/src/a.go:12:match"} + differentTool := ToolCallEvent{Name: "rg", Output: equivalent.Output} + + if key := FailureKey(equivalent); key == "" { + t.Fatal("successful tool calls need a non-empty aggregation key") + } else if key != FailureKey(equivalentCopy) { + t.Fatalf("equivalent successful results should share a key: %q vs %q", key, FailureKey(equivalentCopy)) + } + if FailureKey(equivalent) == FailureKey(differentSummary) { + t.Fatalf("different successful summaries must not share a key: %q", FailureKey(equivalent)) + } + if FailureKey(equivalent) == FailureKey(differentTool) { + t.Fatalf("different successful tools must not share a key: %q", FailureKey(equivalent)) + } +} + func TestToolCallEventFailureKeyCollapsesLegacyTurnBudget(t *testing.T) { first := ToolCallEvent{Name: "read", Err: "tool read: turn budget exceeded (200000 chars)"} second := ToolCallEvent{Name: "shell", Err: "tool shell: turn budget exceeded (200000 chars)"}