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 }