bots-garden/mini-mepublic Fork 0
d72271127802973540c648bfb372176cdaaa8e4f
Commits
Clone
git clone https://git.rickub.com/bots-garden/mini-me.git
git clone ssh://git@rickub.com/bots-garden/mini-me.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

💾 Saved. d722711 · on d72271127802973540c648bfb372176cdaaa8e4f · k33g · 6h ago
detector_test.go · 57 lines · 1.4 KBGo Blame HistoryRaw
 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
}