Files
coder/coderd/x/chatd/chattool/toolcall_test.go
T
Mathias Fredriksson fa5bac6e77 feat: send tool call IDs and cancel unresolved calls on interrupt (#29954)
Retries and replica takeovers ran tool calls again, and an interrupt
recorded the same generic result whether a call had finished, was
still running, or never started.

chatd now derives a tool call ID from the chat, the committed
assistant message, and the provider tool call ID, and sends it with
execute starts, edit_files, and write_file. Every replica and retry
computes the same ID, so the agent returns the first result instead
of running the tool again, and a retried foreground execute waits
only for the rest of its timeout.

On interrupt, chatd cancels the unresolved calls of those tools on
the agent before committing, within 30 seconds, and records what
happened: "tool call was canceled before it ran; no changes were
made", the output of a command canceled while running, or the real
result. The UI shows a canceled command with the existing killed
marker. If the cancel fails, including an old agent's 404, the result
stays the generic interrupted text.

Calls whose provider tool call ID is empty or repeats in the message
get no ID and behave as before.

Fixes CODAGT-757
2026-09-30 14:31:52 +03:00

49 lines
1.3 KiB
Go

package chattool_test
import (
"testing"
"charm.land/fantasy"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/coderd/x/chatd/chattool"
)
func TestToolCallID(t *testing.T) {
t.Parallel()
// A changed value would make the agent run retried calls again.
chatID := uuid.MustParse("11111111-1111-1111-1111-111111111111")
require.Equal(t, uuid.MustParse("ca92e3e7-ee42-5889-b1df-75869f9117ee"), chattool.ToolCallID(chatID, 42, "call_1"))
}
func TestToolCallIDs(t *testing.T) {
t.Parallel()
chatID := uuid.New()
tests := []struct {
name string
providerIDs []string
want []string
}{
{name: "Unique", providerIDs: []string{"a", "b"}, want: []string{"a", "b"}},
{name: "Empty", providerIDs: []string{"", "b"}, want: []string{"b"}},
{name: "Repeated", providerIDs: []string{"a", "a", "b"}, want: []string{"b"}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
calls := make([]fantasy.ToolCallContent, 0, len(tc.providerIDs))
for _, id := range tc.providerIDs {
calls = append(calls, fantasy.ToolCallContent{ToolCallID: id})
}
want := make(map[string]uuid.UUID, len(tc.want))
for _, id := range tc.want {
want[id] = chattool.ToolCallID(chatID, 7, id)
}
require.Equal(t, want, chattool.ToolCallIDs(chatID, 7, calls))
})
}
}