Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions internal/agent/toolloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,20 @@ func (s *toolLoopState) detectReadOnlyStreak() (level, message string) {
//
// A successful exec is exempt: commands such as API calls routinely return the
// same static success body for different inputs, which is not a stuck loop.
// Failing exec calls (isError) are still detected.
// Successful write_file/edit/edit_file calls are exempt too: their results only
// echo the path and byte/replacement count, so legitimate iterative rewrites of
// the same file produce identical results. Identical-args loops are still caught
// by detect. Failing calls (isError) are still detected.
func (s *toolLoopState) detectSameResult(toolName, resultHash string, isError bool) (level, message string) {
if resultHash == "" || (toolName == "exec" && !isError) {
if resultHash == "" {
return "", ""
}
switch toolName {
case "exec", "write_file", "edit", "edit_file":
if !isError {
return "", ""
}
}
var count int
for _, rec := range s.history {
if rec.toolName == toolName && rec.resultHash == resultHash {
Expand Down
17 changes: 17 additions & 0 deletions internal/agent/toolloop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,23 @@ func TestSameResult_ExecErrorStillDetected(t *testing.T) {
}
}

func TestSameResult_WriteFileSuccessExempt(t *testing.T) {
for _, tool := range []string{"write_file", "edit", "edit_file"} {
var s toolLoopState
same := "File written: notes.md (42 bytes)"
for i := range sameResultCritical + 2 {
h := s.record(tool, map[string]any{"path": "notes.md", "content": string(rune('a' + i))})
s.recordResult(h, same)
}
if level, _ := s.detectSameResult(tool, hashResult(same), false); level != "" {
t.Fatalf("expected no detection for successful %s, got %q", tool, level)
}
if level, _ := s.detectSameResult(tool, hashResult(same), true); level != "critical" {
t.Fatalf("expected critical for repeated failing %s, got %q", tool, level)
}
}
}

func TestHashToolCall(t *testing.T) {
// Same input → same hash
h1 := hashToolCall("list_files", map[string]any{"path": "."})
Expand Down
Loading