fix(core): Improve loop detection for longer repeating patterns (#12505)

This commit is contained in:
Sandy Tao
2025-11-03 18:06:22 -08:00
committed by GitHub
parent fc42c4613f
commit f0c3c81e94
2 changed files with 79 additions and 3 deletions
@@ -28,7 +28,7 @@ import { debugLogger } from '../utils/debugLogger.js';
const TOOL_CALL_LOOP_THRESHOLD = 5;
const CONTENT_LOOP_THRESHOLD = 10;
const CONTENT_CHUNK_SIZE = 50;
const MAX_HISTORY_LENGTH = 1000;
const MAX_HISTORY_LENGTH = 5000;
/**
* The number of recent conversation turns to include in the history when asking the LLM to check for a loop.
@@ -328,7 +328,7 @@ export class LoopDetectionService {
* 2. Verify actual content matches to prevent hash collisions
* 3. Track all positions where this chunk appears
* 4. A loop is detected when the same chunk appears CONTENT_LOOP_THRESHOLD times
* within a small average distance (≤ 1.5 * chunk size)
* within a small average distance (≤ 5 * chunk size)
*/
private isLoopDetectedForChunk(chunk: string, hash: string): boolean {
const existingIndices = this.contentStats.get(hash);
@@ -353,7 +353,7 @@ export class LoopDetectionService {
const totalDistance =
recentIndices[recentIndices.length - 1] - recentIndices[0];
const averageDistance = totalDistance / (CONTENT_LOOP_THRESHOLD - 1);
const maxAllowedDistance = CONTENT_CHUNK_SIZE * 1.5;
const maxAllowedDistance = CONTENT_CHUNK_SIZE * 5;
return averageDistance <= maxAllowedDistance;
}