File size: 6,876 Bytes
3d7a2d5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | package agent
import (
"fmt"
"testing"
"charm.land/fantasy"
)
// makeStep creates a StepResult with the given tool calls and results in its Content.
func makeStep(calls []fantasy.ToolCallContent, results []fantasy.ToolResultContent) fantasy.StepResult {
var content fantasy.ResponseContent
for _, c := range calls {
content = append(content, c)
}
for _, r := range results {
content = append(content, r)
}
return fantasy.StepResult{
Response: fantasy.Response{
Content: content,
},
}
}
// makeToolStep creates a step with a single tool call and matching text result.
func makeToolStep(name, input, output string) fantasy.StepResult {
callID := fmt.Sprintf("call_%s_%s", name, input)
return makeStep(
[]fantasy.ToolCallContent{
{ToolCallID: callID, ToolName: name, Input: input},
},
[]fantasy.ToolResultContent{
{ToolCallID: callID, ToolName: name, Result: fantasy.ToolResultOutputContentText{Text: output}},
},
)
}
// makeEmptyStep creates a step with no tool calls (e.g. a text-only response).
func makeEmptyStep() fantasy.StepResult {
return fantasy.StepResult{
Response: fantasy.Response{
Content: fantasy.ResponseContent{
fantasy.TextContent{Text: "thinking..."},
},
},
}
}
func TestHasRepeatedToolCalls(t *testing.T) {
t.Run("no steps", func(t *testing.T) {
result := hasRepeatedToolCalls(nil, 10, 5)
if result {
t.Error("expected false for empty steps")
}
})
t.Run("fewer steps than window", func(t *testing.T) {
steps := make([]fantasy.StepResult, 5)
for i := range steps {
steps[i] = makeToolStep("read", `{"file":"a.go"}`, "content")
}
result := hasRepeatedToolCalls(steps, 10, 5)
if result {
t.Error("expected false when fewer steps than window size")
}
})
t.Run("all different signatures", func(t *testing.T) {
steps := make([]fantasy.StepResult, 10)
for i := range steps {
steps[i] = makeToolStep("tool", fmt.Sprintf(`{"i":%d}`, i), fmt.Sprintf("result-%d", i))
}
result := hasRepeatedToolCalls(steps, 10, 5)
if result {
t.Error("expected false when all signatures are different")
}
})
t.Run("exact repeat at threshold not detected", func(t *testing.T) {
// maxRepeats=5 means > 5 is needed, so exactly 5 should return false
steps := make([]fantasy.StepResult, 10)
for i := range 5 {
steps[i] = makeToolStep("read", `{"file":"a.go"}`, "content")
}
for i := 5; i < 10; i++ {
steps[i] = makeToolStep("tool", fmt.Sprintf(`{"i":%d}`, i), fmt.Sprintf("result-%d", i))
}
result := hasRepeatedToolCalls(steps, 10, 5)
if result {
t.Error("expected false when count equals maxRepeats (threshold is >)")
}
})
t.Run("loop detected", func(t *testing.T) {
// 6 identical steps in a window of 10 with maxRepeats=5 → detected
steps := make([]fantasy.StepResult, 10)
for i := range 6 {
steps[i] = makeToolStep("read", `{"file":"a.go"}`, "content")
}
for i := 6; i < 10; i++ {
steps[i] = makeToolStep("tool", fmt.Sprintf(`{"i":%d}`, i), fmt.Sprintf("result-%d", i))
}
result := hasRepeatedToolCalls(steps, 10, 5)
if !result {
t.Error("expected true when same signature appears more than maxRepeats times")
}
})
t.Run("steps without tool calls are skipped", func(t *testing.T) {
// Mix of tool steps and empty steps — empty ones should not affect counts
steps := make([]fantasy.StepResult, 10)
for i := range 4 {
steps[i] = makeToolStep("read", `{"file":"a.go"}`, "content")
}
for i := 4; i < 8; i++ {
steps[i] = makeEmptyStep()
}
for i := 8; i < 10; i++ {
steps[i] = makeToolStep("write", `{"file":"b.go"}`, "ok")
}
result := hasRepeatedToolCalls(steps, 10, 5)
if result {
t.Error("expected false: only 4 repeated tool calls, empty steps should be skipped")
}
})
t.Run("multiple different patterns alternating", func(t *testing.T) {
// Two patterns alternating: each appears 5 times — not above threshold
steps := make([]fantasy.StepResult, 10)
for i := range steps {
if i%2 == 0 {
steps[i] = makeToolStep("read", `{"file":"a.go"}`, "content-a")
} else {
steps[i] = makeToolStep("write", `{"file":"b.go"}`, "content-b")
}
}
result := hasRepeatedToolCalls(steps, 10, 5)
if result {
t.Error("expected false: two patterns each appearing 5 times (not > 5)")
}
})
}
func TestGetToolInteractionSignature(t *testing.T) {
t.Run("empty content returns empty string", func(t *testing.T) {
sig := getToolInteractionSignature(fantasy.ResponseContent{})
if sig != "" {
t.Errorf("expected empty string, got %q", sig)
}
})
t.Run("text only content returns empty string", func(t *testing.T) {
content := fantasy.ResponseContent{
fantasy.TextContent{Text: "hello"},
}
sig := getToolInteractionSignature(content)
if sig != "" {
t.Errorf("expected empty string, got %q", sig)
}
})
t.Run("tool call with result produces signature", func(t *testing.T) {
content := fantasy.ResponseContent{
fantasy.ToolCallContent{ToolCallID: "1", ToolName: "read", Input: `{"file":"a.go"}`},
fantasy.ToolResultContent{ToolCallID: "1", ToolName: "read", Result: fantasy.ToolResultOutputContentText{Text: "content"}},
}
sig := getToolInteractionSignature(content)
if sig == "" {
t.Error("expected non-empty signature")
}
})
t.Run("same interactions produce same signature", func(t *testing.T) {
content1 := fantasy.ResponseContent{
fantasy.ToolCallContent{ToolCallID: "1", ToolName: "read", Input: `{"file":"a.go"}`},
fantasy.ToolResultContent{ToolCallID: "1", ToolName: "read", Result: fantasy.ToolResultOutputContentText{Text: "content"}},
}
content2 := fantasy.ResponseContent{
fantasy.ToolCallContent{ToolCallID: "2", ToolName: "read", Input: `{"file":"a.go"}`},
fantasy.ToolResultContent{ToolCallID: "2", ToolName: "read", Result: fantasy.ToolResultOutputContentText{Text: "content"}},
}
sig1 := getToolInteractionSignature(content1)
sig2 := getToolInteractionSignature(content2)
if sig1 != sig2 {
t.Errorf("expected same signature for same interactions, got %q and %q", sig1, sig2)
}
})
t.Run("different inputs produce different signatures", func(t *testing.T) {
content1 := fantasy.ResponseContent{
fantasy.ToolCallContent{ToolCallID: "1", ToolName: "read", Input: `{"file":"a.go"}`},
fantasy.ToolResultContent{ToolCallID: "1", ToolName: "read", Result: fantasy.ToolResultOutputContentText{Text: "content"}},
}
content2 := fantasy.ResponseContent{
fantasy.ToolCallContent{ToolCallID: "1", ToolName: "read", Input: `{"file":"b.go"}`},
fantasy.ToolResultContent{ToolCallID: "1", ToolName: "read", Result: fantasy.ToolResultOutputContentText{Text: "content"}},
}
sig1 := getToolInteractionSignature(content1)
sig2 := getToolInteractionSignature(content2)
if sig1 == sig2 {
t.Error("expected different signatures for different inputs")
}
})
}
|