Skip and reset loop checking around code blocks (#5144)

This commit is contained in:
Sandy Tao
2025-07-29 21:05:03 -07:00
committed by GitHub
parent 61107ef19d
commit 85a0ed27f6
2 changed files with 134 additions and 11 deletions

View File

@@ -61,6 +61,7 @@ export class LoopDetectionService {
private contentStats = new Map<string, number[]>();
private lastContentIndex = 0;
private loopDetected = false;
private inCodeBlock = false;
// LLM loop track tracking
private turnsInCurrentPrompt = 0;
@@ -156,8 +157,27 @@ export class LoopDetectionService {
* 2. Truncating history if it exceeds the maximum length
* 3. Analyzing content chunks for repetitive patterns using hashing
* 4. Detecting loops when identical chunks appear frequently within a short distance
* 5. Disabling loop detection within code blocks to prevent false positives,
* as repetitive code structures are common and not necessarily loops.
*/
private checkContentLoop(content: string): boolean {
// Code blocks can often contain repetitive syntax that is not indicative of a loop.
// To avoid false positives, we detect when we are inside a code block and
// temporarily disable loop detection.
const numFences = (content.match(/```/g) ?? []).length;
if (numFences) {
// Reset tracking when a code fence is detected to avoid analyzing content
// that spans across code block boundaries.
this.resetContentTracking();
}
const wasInCodeBlock = this.inCodeBlock;
this.inCodeBlock =
numFences % 2 === 0 ? this.inCodeBlock : !this.inCodeBlock;
if (wasInCodeBlock) {
return false;
}
this.streamContentHistory += content;
this.truncateAndUpdate();