diff --git a/internal/agent/toolloop.go b/internal/agent/toolloop.go index f88199890f..da5d9b9fb8 100644 --- a/internal/agent/toolloop.go +++ b/internal/agent/toolloop.go @@ -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 { diff --git a/internal/agent/toolloop_test.go b/internal/agent/toolloop_test.go index b620c5de11..a16c2ed3b3 100644 --- a/internal/agent/toolloop_test.go +++ b/internal/agent/toolloop_test.go @@ -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": "."})