mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-20 08:47:44 +00:00
Skip and reset loop checking around code blocks (#5144)
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user