Files
adnify/tests/agent/utils/loopDetectorContentCycle.test.ts
adnaan 72ca54c631 fix(agent): make loop detection advisory and stop wasting iterations
Loop detection now only ever warns. It never terminates a turn.

Previously three rules returned `isLoop: true` (checkExactRepeat's hard
threshold, checkContentChange, checkPatternLoop) and loop.ts responded by
breaking out of the turn. checkPatternLoop was the most aggressive: a 2-call
pattern repeated just twice was enough, so the ordinary long-task rhythm of
edit -> lint -> edit -> lint could kill the run. That contradicted the module's
own stated goal ("treat ... as a warning, not a hard stop") and made long tasks
unfinishable.

All three now return `warning`, which loop.ts already handles the right way:
surface an alert, feed a corrective hint back to the model, and keep going. The
break-on-isLoop branch in loop.ts is removed as dead code. `checkLoop` also had
to be updated to return content/pattern results at all — its early-return
conditions still tested only `isLoop`, so warnings from those two rules were
being swallowed. Message formatting is unaffected: the helpers dispatch on
`details.category`, not `reason`.

Two changes to reduce hitting the tool-call limit in the first place:

- Auto-fix now gives up after 2 attempts on an UNCHANGED lint error set. It
  spends an iteration per report, so errors the model cannot fix (generated
  files, vendored code, false positives) previously consumed every remaining
  iteration and pushed the turn into the limit. Keying on the error signature
  means a model making progress keeps its full budget.
- The limit warning now names the actual limit and points at
  Settings -> Agent -> Max Loops (adjustable 5-100), and asks the model to
  report what remains rather than just "adjust the strategy".

Also fixes the limit check itself: `iteration >= maxIterations` sat outside the
while loop, so a turn that merely finished on its final allowed iteration got a
bogus "limit reached" alert, a SECOND `loop:end` event, and an extra LLM
request. That fired even for `reason: 'aborted'` — a fresh request after the
user pressed stop. It is now gated on `shouldContinue` (true only on the
"run another iteration" path) and on the abort signal, and
completeWithSoftLimitFeedback returns early when already aborted.

Tests assert the contract directly: every rule warns, none reports isLoop, and
the classic edit/lint alternation is not treated as a loop.
2026-08-15 23:49:13 +08:00

130 lines
5.5 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { LoopDetector } from '@/renderer/agent/utils/LoopDetector'
/**
* Regression tests for content-cycle detection.
*
* The bug: `loop.ts` records content hashes using `meta.filePath` — an ABSOLUTE
* path from `resolvePath()` in the tool executors — while `checkContentChange`
* looked them up by `record.target`, the RELATIVE path from the model's
* `args.path`. The two keys never matched, so the hash map always looked empty
* to the reader and the "agent rewrites the same file back and forth" case was
* never detected.
*
* Detection is ADVISORY: every result is reported as `warning` with
* `isLoop: false`, because terminating a turn on a repeated tool pattern made
* long-running tasks unfinishable. These tests assert the warning fires (and
* carries the right category), never that the turn is aborted.
*
* Threshold note: `maxNoChangeEdits` = `maxSameTargetRepeats` = 8 (defaults.ts),
* so a cycle needs 8 recorded hashes before it trips. Args are varied per
* iteration so the exact-repeat rule doesn't pre-empt the content-cycle rule.
*/
const WORKSPACE = 'E:/Project/adnify'
const RELATIVE = 'src/a.ts'
function driveCycle(hashKey: string, iterations = 8) {
const detector = new LoopDetector()
for (let i = 0; i < iterations; i++) {
detector.recordExecutedTool(
{ name: 'edit_file', arguments: { path: RELATIVE, old_string: `v${i}`, new_string: `v${i + 1}` } },
true
)
// Alternate between two states: the file is being rewritten back and forth.
detector.updateContentHashBySignature(hashKey, i % 2 === 0 ? 'AAAA' : 'BBBB')
}
return detector.checkLoop([
{ name: 'edit_file', arguments: { path: RELATIVE, old_string: 'vZ', new_string: 'vW' } } as never,
])
}
describe('LoopDetector content-cycle detection', () => {
it('detects a cycle when hashes are keyed by the absolute path executors report', () => {
const result = driveCycle(`${WORKSPACE}/${RELATIVE}`)
expect(result.warning).toBeTruthy()
expect(result.details?.category).toBe('content_cycle')
// Advisory only: detection must never terminate the turn.
expect(result.isLoop).toBe(false)
})
it('detects a cycle when hashes are keyed by the relative path the model sent', () => {
const result = driveCycle(RELATIVE)
expect(result.warning).toBeTruthy()
expect(result.details?.category).toBe('content_cycle')
expect(result.isLoop).toBe(false)
})
it('treats backslash and forward-slash paths as the same file', () => {
const result = driveCycle(`${WORKSPACE}\\src\\a.ts`)
expect(result.warning).toBeTruthy()
expect(result.details?.category).toBe('content_cycle')
expect(result.isLoop).toBe(false)
})
it('does not flag a file whose content keeps changing', () => {
const detector = new LoopDetector()
for (let i = 0; i < 8; i++) {
detector.recordExecutedTool(
{ name: 'edit_file', arguments: { path: RELATIVE, old_string: `v${i}`, new_string: `v${i + 1}` } },
true
)
detector.updateContentHashBySignature(`${WORKSPACE}/${RELATIVE}`, `unique-${i}`)
}
const result = detector.checkLoop([
{ name: 'edit_file', arguments: { path: RELATIVE, old_string: 'vZ', new_string: 'vW' } } as never,
])
expect(result.details?.category).not.toBe('content_cycle')
})
it('does not conflate same-named files in different directories', () => {
const detector = new LoopDetector()
// Alternating hashes, but split across two distinct files: neither file
// individually cycles, so this must not trip the detector.
for (let i = 0; i < 8; i++) {
detector.recordExecutedTool(
{ name: 'edit_file', arguments: { path: 'src/a.ts', old_string: `v${i}`, new_string: `v${i + 1}` } },
true
)
const target = i % 2 === 0 ? `${WORKSPACE}/src/a.ts` : `${WORKSPACE}/tests/a.ts`
detector.updateContentHashBySignature(target, i % 2 === 0 ? 'AAAA' : 'BBBB')
}
const result = detector.checkLoop([
{ name: 'edit_file', arguments: { path: 'src/a.ts', old_string: 'vZ', new_string: 'vW' } } as never,
])
expect(result.details?.category).not.toBe('content_cycle')
})
})
describe('LoopDetector never terminates a turn', () => {
// The loop only breaks out of the turn on `isLoop`. Long tasks legitimately
// repeat tool patterns, so every detector rule must stay advisory. If a future
// change reintroduces `isLoop: true`, this fails loudly.
it('reports exact argument repetition as a warning, not a loop', () => {
const detector = new LoopDetector()
const call = { name: 'write_file', arguments: { path: RELATIVE, content: 'same' } }
for (let i = 0; i < 30; i++) detector.recordExecutedTool(call, true)
const result = detector.checkLoop([call as never])
expect(result.isLoop).toBe(false)
expect(result.warning).toBeTruthy()
})
it('reports a repeating tool pattern as a warning, not a loop', () => {
const detector = new LoopDetector()
// The classic long-task rhythm: edit, lint, edit, lint...
for (let i = 0; i < 12; i++) {
detector.recordExecutedTool({ name: 'edit_file', arguments: { path: RELATIVE } }, true)
detector.recordExecutedTool({ name: 'get_lint_errors', arguments: { path: RELATIVE } }, true)
}
const result = detector.checkLoop([
{ name: 'edit_file', arguments: { path: RELATIVE } } as never,
])
expect(result.isLoop).toBe(false)
})
it('never returns isLoop for a cycling file', () => {
expect(driveCycle(`${WORKSPACE}/${RELATIVE}`, 20).isLoop).toBe(false)
})
})