diff --git a/internal/app/archied/state_store_client_test.go b/internal/app/archied/state_store_client_test.go index 9c6442ee6..780859524 100644 --- a/internal/app/archied/state_store_client_test.go +++ b/internal/app/archied/state_store_client_test.go @@ -19,6 +19,8 @@ func TestComposeStateStoreClientRequiresTarget(t *testing.T) { } func TestComposeStateStoreClientNonLoopbackFailsClosed(t *testing.T) { + // Keep this test independent of credentials inherited from the test runner. + t.Setenv("STATE_STORE_TOKEN", "") for _, tt := range []struct { name string target string @@ -73,6 +75,8 @@ func TestOpenStateStoreAdapterRequiresTarget(t *testing.T) { } func TestOpenStateStoreAdapterRemoteFailsClosed(t *testing.T) { + // Keep this test independent of credentials inherited from the test runner. + t.Setenv("STATE_STORE_TOKEN", "") b := newBootstrap() b.secrets = &secret.Registry{} b.cfg.Services.State = config.ServiceConnection{Target: "0.0.0.0:9090"} 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/stream.go b/internal/gateway/stream.go index 4a49dad1f..ede870062 100644 --- a/internal/gateway/stream.go +++ b/internal/gateway/stream.go @@ -37,13 +37,11 @@ func RenderToolCall(e ToolCallEvent) string { return toolProgressBlock(e.Name, "done", toolPreview(e.Name, e.Output)) } -// FailureKey identifies equivalent failures for channel adapters that collapse -// retries. Legacy aggregate-output-limit errors deliberately share one key -// across tool names: they are one obsolete turn-level condition, not five -// independently useful failures. -// FailureKey identifies equivalent failures for channel adapters that -// collapse retries. (A function rather than a method because ToolCallEvent -// is a messaging alias: see chat_contract_aliases.go.) +// FailureKey identifies equivalent tool calls for channel adapters that +// collapse repeated activity. Legacy aggregate-output-limit errors deliberately +// share one key across tool names: they are one obsolete turn-level condition, +// not five independently useful failures. (A function rather than a method +// because ToolCallEvent is a messaging alias: see chat_contract_aliases.go.) func FailureKey(e ToolCallEvent) string { if _, ok := legacyTurnBudgetLimit(e.Err); ok { return "legacy-turn-output-limit" @@ -51,7 +49,10 @@ func FailureKey(e ToolCallEvent) string { if line := cleanToolError(e.Name, e.Err); line != "" { return strings.TrimSpace(e.Name) + "\x00" + line } - return "" + // Successful calls are also eligible for aggregation. Use the same + // compact summary that is shown to the user so equivalent results share a + // key while different result summaries remain distinct. + return "success\x00" + strings.TrimSpace(e.Name) + "\x00" + toolPreview(e.Name, e.Output) } func toolProgressBlock(name, status, preview string) string { 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)"}