Files
coder/coderd/x/chatd/chatloop/tooltruncate_internal_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

106 lines
2.9 KiB
Go

package chatloop
import (
"strings"
"testing"
"unicode/utf8"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestToolResultByteBudget(t *testing.T) {
t.Parallel()
tests := []struct {
name string
contextLimit int64
want int
}{
{name: "Unknown", contextLimit: 0, want: defaultToolResultBytes},
{name: "Negative", contextLimit: -1, want: defaultToolResultBytes},
{name: "BelowFloor", contextLimit: 1000, want: minToolResultBytes},
{
name: "LargeWindow",
contextLimit: 200_000,
want: 200_000 / toolResultContextDivisor * bytesPerTokenEstimate,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tt.want, ToolResultByteBudget(tt.contextLimit))
})
}
t.Run("NeverBelowFloor", func(t *testing.T) {
t.Parallel()
for limit := int64(1); limit <= 200_000; limit += 137 {
assert.GreaterOrEqual(t, ToolResultByteBudget(limit), minToolResultBytes)
}
})
}
func TestTruncateToolResultText(t *testing.T) {
t.Parallel()
t.Run("UnderLimitUnchanged", func(t *testing.T) {
t.Parallel()
in := "small output"
out, truncated := TruncateToolResultText(in, 1024)
assert.False(t, truncated)
assert.Equal(t, in, out)
})
t.Run("ExactlyAtLimitUnchanged", func(t *testing.T) {
t.Parallel()
in := strings.Repeat("a", 1024)
out, truncated := TruncateToolResultText(in, 1024)
assert.False(t, truncated)
assert.Equal(t, in, out)
})
t.Run("ZeroBudgetUnchanged", func(t *testing.T) {
t.Parallel()
in := strings.Repeat("a", 1024)
out, truncated := TruncateToolResultText(in, 0)
assert.False(t, truncated)
assert.Equal(t, in, out)
})
t.Run("PreservesHeadAndTail", func(t *testing.T) {
t.Parallel()
in := strings.Repeat("A", 1000) + "MIDDLE" + strings.Repeat("B", 1000)
const maxBytes = 600
out, truncated := TruncateToolResultText(in, maxBytes)
require.True(t, truncated)
assert.LessOrEqual(t, len(out), maxBytes)
assert.True(t, utf8.ValidString(out))
assert.True(t, strings.HasPrefix(out, strings.Repeat("A", 100)))
assert.True(t, strings.HasSuffix(out, strings.Repeat("B", 100)))
assert.NotContains(t, out, "MIDDLE")
assert.Contains(t, out, "truncated")
})
t.Run("MultibyteStaysValid", func(t *testing.T) {
t.Parallel()
// Each rune is 3 bytes, so cuts routinely land mid-rune.
in := strings.Repeat("界", 1000)
const maxBytes = 600
out, truncated := TruncateToolResultText(in, maxBytes)
require.True(t, truncated)
assert.LessOrEqual(t, len(out), maxBytes)
assert.True(t, utf8.ValidString(out), "truncated output must be valid UTF-8")
})
t.Run("TinyBudgetHardCut", func(t *testing.T) {
t.Parallel()
in := strings.Repeat("界", 10) // 30 bytes
const maxBytes = 10
out, truncated := TruncateToolResultText(in, maxBytes)
require.True(t, truncated)
assert.LessOrEqual(t, len(out), maxBytes)
assert.True(t, utf8.ValidString(out))
})
}