| 💾 Saved. d722711 k33g 4h ago | 1 | package detector |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func same() Action { |
| 9 | return Action{ToolName: "bash", Input: "ls", Output: "[tool_call]"} |
| 10 | } |
| 11 | |
| 12 | // Reset forgets what the detector saw: a command repeated up to the threshold |
| 13 | // is flagged, and after /new the same command counts from zero again — the |
| 14 | // new session is a new task, and the old repetitions belong to the old one. |
| 15 | func TestResetForgetsTheRecordedActions(t *testing.T) { |
| 16 | d := NewLoopDetector(10, 3) |
| 17 | for i := 0; i < 3; i++ { |
| 18 | if d.Record(same()) { |
| 19 | t.Fatalf("loop flagged after %d identical action(s), threshold is 3", i+1) |
| 20 | } |
| 21 | } |
| 22 | if !d.Record(same()) { |
| 23 | t.Fatal("4th identical action not flagged as a loop") |
| 24 | } |
| 25 | |
| 26 | d.Reset() |
| 27 | |
| 28 | for i := 0; i < 3; i++ { |
| 29 | if d.Record(same()) { |
| 30 | t.Fatalf("after Reset, loop flagged after %d identical action(s)", i+1) |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // Reset keeps the detector's settings: the threshold still applies afterwards. |
| 36 | func TestResetKeepsThreshold(t *testing.T) { |
| 37 | d := NewLoopDetector(10, 2) |
| 38 | d.Reset() |
| 39 | d.Record(same()) |
| 40 | d.Record(same()) |
| 41 | if !d.Record(same()) { |
| 42 | t.Fatal("after Reset, the threshold of 2 no longer applies") |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func ExampleLoopDetector_Reset() { |
| 47 | d := NewLoopDetector(10, 2) |
| 48 | a := Action{ToolName: "bash", Input: "ls", Output: "[tool_call]"} |
| 49 | d.Record(a) |
| 50 | d.Record(a) |
| 51 | fmt.Println(d.Record(a)) // third time in a row: a loop |
| 52 | d.Reset() |
| 53 | fmt.Println(d.Record(a)) // a new session: counted from zero |
| 54 | // Output: |
| 55 | // true |
| 56 | // false |
| 57 | } |