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
|
package detector
import (
"fmt"
"testing"
)
func same() Action {
return Action{ToolName: "bash", Input: "ls", Output: "[tool_call]"}
}
// Reset forgets what the detector saw: a command repeated up to the threshold
// is flagged, and after /new the same command counts from zero again — the
// new session is a new task, and the old repetitions belong to the old one.
func TestResetForgetsTheRecordedActions(t *testing.T) {
d := NewLoopDetector(10, 3)
for i := 0; i < 3; i++ {
if d.Record(same()) {
t.Fatalf("loop flagged after %d identical action(s), threshold is 3", i+1)
}
}
if !d.Record(same()) {
t.Fatal("4th identical action not flagged as a loop")
}
d.Reset()
for i := 0; i < 3; i++ {
if d.Record(same()) {
t.Fatalf("after Reset, loop flagged after %d identical action(s)", i+1)
}
}
}
// Reset keeps the detector's settings: the threshold still applies afterwards.
func TestResetKeepsThreshold(t *testing.T) {
d := NewLoopDetector(10, 2)
d.Reset()
d.Record(same())
d.Record(same())
if !d.Record(same()) {
t.Fatal("after Reset, the threshold of 2 no longer applies")
}
}
func ExampleLoopDetector_Reset() {
d := NewLoopDetector(10, 2)
a := Action{ToolName: "bash", Input: "ls", Output: "[tool_call]"}
d.Record(a)
d.Record(a)
fmt.Println(d.Record(a)) // third time in a row: a loop
d.Reset()
fmt.Println(d.Record(a)) // a new session: counted from zero
// Output:
// true
// false
}
|