Skip to content

fix(agent): exempt successful write_file and edit calls from same-result loop detection - #1699

Open
mvanhorn wants to merge 1 commit into
nextlevelbuilder:devfrom
mvanhorn:fix/1131-exempt-file-write-tools-same-result-loop
Open

mvanhorn wants to merge 1 commit into
nextlevelbuilder:devfrom
mvanhorn:fix/1131-exempt-file-write-tools-same-result-loop

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

Summary

Iteratively modifying the same file with successful write or edit calls can trigger false-positive duplicate-result loop warnings and eventually stop the agent with a critical loop detection. Even though each modification succeeds with new content, the agent is warned as if it were stuck repeating itself. toolLoopState.detectSameResult counts repeated tool outcomes solely by resultHash and previously exempted only successful exec calls.

Because write_file, edit, and edit_file only echo the path and byte or replacement count, repeated legitimate edits to the same file can yield identical result hashes and trip the loop thresholds.

Updated toolLoopState.detectSameResult in internal/agent/toolloop.go to exempt successful (!isError) write_file, edit, and edit_file calls alongside exec, while continuing to flag repeated failing calls and leaving identical-argument loops to detect. Added TestSameResult_WriteFileSuccessExempt in internal/agent/toolloop_test.go to verify that repeated identical results from those tools are ignored on success and trigger a critical detection on error.

Fixes #1131

Type

  • Feature
  • Bug fix
  • Hotfix (targeting main)
  • Refactor
  • Docs
  • CI/CD

Target Branch

Not applicable to this change.

Checklist

  • go build ./... passes
  • go build -tags sqliteonly ./... passes (if Go changes)
  • go vet ./... passes
  • Tests pass: go test -race ./...
  • Web UI builds: cd ui/web && pnpm build (if UI changes)
  • No hardcoded secrets or credentials
  • SQL queries use parameterized $1, $2 (no string concat)
  • New user-facing strings added to all 3 locales (en/vi/zh)
  • Migration version bumped in internal/upgrade/version.go (if new migration)

Test Plan

go test ./internal/agent/... passed, covering the agent package and the new TestSameResult_WriteFileSuccessExempt test.
go vet ./internal/agent/... completed with no reported issues.
gofmt -l internal/agent/toolloop.go internal/agent/toolloop_test.go reported no formatting discrepancies.
Ran go test -v -tags integration ./tests/integration/ in a Linux container (golang:1.24) for the goclaw change; it passed.
The tests in internal/agent/toolloop_test.go fail without the fix in the goclaw change (checked by reverting the source changes).
The changed test internal/agent/toolloop_test.go did not run here: no test runner for it was available.

…p detection

Updated `toolLoopState.detectSameResult` in `internal/agent/toolloop.go`
to exempt successful (`!isError`) `write_file`, `edit`, and `edit_file`
calls alongside `exec`, while continuing to flag repeated failing calls
and leaving identical-argument loops to `detect`. Added
`TestSameResult_WriteFileSuccessExempt` in
`internal/agent/toolloop_test.go` to verify that repeated identical
results from those tools are ignored on success and trigger a critical
detection on error.

Fixes nextlevelbuilder#1131

@clark-cant clark-cant left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary: Extends the same-result loop exemption (added for successful exec in #1674) to successful write_file, edit, and edit_file in toolLoopState.detectSameResult, so legitimate iterative rewrites of the same file no longer trip a false-positive duplicate-result warning/kill. Fixes #1131.

Risk level: Low

Mandatory gates

  • Duplicate / prior implementation: clear — extends the merged #1674 exec exemption; no other open/merged PR or issue covers the write_file/edit case (issue 1131 is maintain:bug-confirmed with our root-cause comment recommending exactly this option A).
  • Project standards: docs found — CONTRIBUTING.md (base dev); matches the existing switch/table style in toolloop.go.
  • Strategic necessity: clear value — removes a confirmed false-positive that stops agents mid-edit.

Findings

  • Suggestion: option C from the issue write-up (the message text "with different arguments" still counts same-argument repeats) is not addressed; consider gating the count on rec.argsHash != argsHash so the wording matches behaviour.
  • Suggestion: option B (content-addressed write_file result) is not implemented; optional hardening, not required for this fix.
  • Suggestion: the new test covers success-exempt and error-critical; an assertion that a genuine identical-args loop is still caught by detect would document the safety claim.

Verdict: Approve — correct, minimal, and consistent with the merged #1674 precedent; CI green (release-versioning/go/web), MERGEABLE/CLEAN, base dev. Merge-ready pending a maintainer approval.

Posted by /ck:review-pr at 2026-11-24T19:10:00Z

@clark-cant clark-cant added agent:github-maintain Processed by github-maintain automation maintain:triaged Triaged by maintain workflow pr:reviewed Reviewed by maintainer labels Nov 24, 2026
@mvanhorn

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I kept this to option A on purpose so it matches the #1674 exec exemption and stays easy to merge.

On the safety point: detectSameResult is the only path that changed. Identical-argument loops still go through detect, which counts records with the same argsHash and resultHash, so a write_file or edit stuck on the same call is still flagged there.

Options B and C are reasonable as separate follow-ups, outside the scope of this fix.

@clark-cant

Copy link
Copy Markdown
Contributor

Thanks for the clarification - agreed, keeping this to option A matches the #1674 exec exemption, and detect still catches identical-args loops via argsHash + resultHash, so the scoping is sound. B/C as separate follow-ups is fine. Merge-ready pending a maintainer approval.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent:github-maintain Processed by github-maintain automation maintain:triaged Triaged by maintain workflow pr:reviewed Reviewed by maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicate write_file Result — Take Action

2 participants