1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { createHash } from 'crypto';
import { GeminiEventType, ServerGeminiStreamEvent } from '../core/turn.js';
import { logLoopDetected } from '../telemetry/loggers.js';
import { LoopDetectedEvent, LoopType } from '../telemetry/types.js';
import { Config, DEFAULT_GEMINI_FLASH_MODEL } from '../config/config.js';
import { SchemaUnion, Type } from '@google/genai';
const TOOL_CALL_LOOP_THRESHOLD = 5;
const CONTENT_LOOP_THRESHOLD = 10;
const CONTENT_CHUNK_SIZE = 50;
const MAX_HISTORY_LENGTH = 1000;
/**
* The number of recent conversation turns to include in the history when asking the LLM to check for a loop.
*/
const LLM_LOOP_CHECK_HISTORY_COUNT = 20;
/**
* The number of turns that must pass in a single prompt before the LLM-based loop check is activated.
*/
const LLM_CHECK_AFTER_TURNS = 30;
/**
* The default interval, in number of turns, at which the LLM-based loop check is performed.
* This value is adjusted dynamically based on the LLM's confidence.
*/
const DEFAULT_LLM_CHECK_INTERVAL = 3;
/**
* The minimum interval for LLM-based loop checks.
* This is used when the confidence of a loop is high, to check more frequently.
*/
const MIN_LLM_CHECK_INTERVAL = 5;
/**
* The maximum interval for LLM-based loop checks.
* This is used when the confidence of a loop is low, to check less frequently.
*/
const MAX_LLM_CHECK_INTERVAL = 15;
/**
* Service for detecting and preventing infinite loops in AI responses.
* Monitors tool call repetitions and content sentence repetitions.
*/
export class LoopDetectionService {
private readonly config: Config;
private promptId = '';
// Tool call tracking
private lastToolCallKey: string | null = null;
private toolCallRepetitionCount: number = 0;
// Content streaming tracking
private streamContentHistory = '';
private contentStats = new Map<string, number[]>();
private lastContentIndex = 0;
private loopDetected = false;
// LLM loop track tracking
private turnsInCurrentPrompt = 0;
private llmCheckInterval = DEFAULT_LLM_CHECK_INTERVAL;
private lastCheckTurn = 0;
constructor(config: Config) {
this.config = config;
}
private getToolCallKey(toolCall: { name: string; args: object }): string {
const argsString = JSON.stringify(toolCall.args);
const keyString = `${toolCall.name}:${argsString}`;
return createHash('sha256').update(keyString).digest('hex');
}
/**
* Processes a stream event and checks for loop conditions.
* @param event - The stream event to process
* @returns true if a loop is detected, false otherwise
*/
addAndCheck(event: ServerGeminiStreamEvent): boolean {
if (this.loopDetected) {
return true;
}
switch (event.type) {
case GeminiEventType.ToolCallRequest:
// content chanting only happens in one single stream, reset if there
// is a tool call in between
this.resetContentTracking();
this.loopDetected = this.checkToolCallLoop(event.value);
break;
case GeminiEventType.Content:
this.loopDetected = this.checkContentLoop(event.value);
break;
default:
break;
}
return this.loopDetected;
}
/**
* Signals the start of a new turn in the conversation.
*
* This method increments the turn counter and, if specific conditions are met,
* triggers an LLM-based check to detect potential conversation loops. The check
* is performed periodically based on the `llmCheckInterval`.
*
* @param signal - An AbortSignal to allow for cancellation of the asynchronous LLM check.
* @returns A promise that resolves to `true` if a loop is detected, and `false` otherwise.
*/
async turnStarted(signal: AbortSignal) {
this.turnsInCurrentPrompt++;
if (
this.turnsInCurrentPrompt >= LLM_CHECK_AFTER_TURNS &&
this.turnsInCurrentPrompt - this.lastCheckTurn >= this.llmCheckInterval
) {
this.lastCheckTurn = this.turnsInCurrentPrompt;
return await this.checkForLoopWithLLM(signal);
}
return false;
}
private checkToolCallLoop(toolCall: { name: string; args: object }): boolean {
const key = this.getToolCallKey(toolCall);
if (this.lastToolCallKey === key) {
this.toolCallRepetitionCount++;
} else {
this.lastToolCallKey = key;
this.toolCallRepetitionCount = 1;
}
if (this.toolCallRepetitionCount >= TOOL_CALL_LOOP_THRESHOLD) {
logLoopDetected(
this.config,
new LoopDetectedEvent(
LoopType.CONSECUTIVE_IDENTICAL_TOOL_CALLS,
this.promptId,
),
);
return true;
}
return false;
}
/**
* Detects content loops by analyzing streaming text for repetitive patterns.
*
* The algorithm works by:
* 1. Appending new content to the streaming history
* 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
*/
private checkContentLoop(content: string): boolean {
this.streamContentHistory += content;
this.truncateAndUpdate();
return this.analyzeContentChunksForLoop();
}
/**
* Truncates the content history to prevent unbounded memory growth.
* When truncating, adjusts all stored indices to maintain their relative positions.
*/
private truncateAndUpdate(): void {
if (this.streamContentHistory.length <= MAX_HISTORY_LENGTH) {
return;
}
// Calculate how much content to remove from the beginning
const truncationAmount =
this.streamContentHistory.length - MAX_HISTORY_LENGTH;
this.streamContentHistory =
this.streamContentHistory.slice(truncationAmount);
this.lastContentIndex = Math.max(
0,
this.lastContentIndex - truncationAmount,
);
// Update all stored chunk indices to account for the truncation
for (const [hash, oldIndices] of this.contentStats.entries()) {
const adjustedIndices = oldIndices
.map((index) => index - truncationAmount)
.filter((index) => index >= 0);
if (adjustedIndices.length > 0) {
this.contentStats.set(hash, adjustedIndices);
} else {
this.contentStats.delete(hash);
}
}
}
/**
* Analyzes content in fixed-size chunks to detect repetitive patterns.
*
* Uses a sliding window approach:
* 1. Extract chunks of fixed size (CONTENT_CHUNK_SIZE)
* 2. Hash each chunk for efficient comparison
* 3. Track positions where identical chunks appear
* 4. Detect loops when chunks repeat frequently within a short distance
*/
private analyzeContentChunksForLoop(): boolean {
while (this.hasMoreChunksToProcess()) {
// Extract current chunk of text
const currentChunk = this.streamContentHistory.substring(
this.lastContentIndex,
this.lastContentIndex + CONTENT_CHUNK_SIZE,
);
const chunkHash = createHash('sha256').update(currentChunk).digest('hex');
if (this.isLoopDetectedForChunk(currentChunk, chunkHash)) {
logLoopDetected(
this.config,
new LoopDetectedEvent(
LoopType.CHANTING_IDENTICAL_SENTENCES,
this.promptId,
),
);
return true;
}
// Move to next position in the sliding window
this.lastContentIndex++;
}
return false;
}
private hasMoreChunksToProcess(): boolean {
return (
this.lastContentIndex + CONTENT_CHUNK_SIZE <=
this.streamContentHistory.length
);
}
/**
* Determines if a content chunk indicates a loop pattern.
*
* Loop detection logic:
* 1. Check if we've seen this hash before (new chunks are stored for future comparison)
* 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)
*/
private isLoopDetectedForChunk(chunk: string, hash: string): boolean {
const existingIndices = this.contentStats.get(hash);
if (!existingIndices) {
this.contentStats.set(hash, [this.lastContentIndex]);
return false;
}
if (!this.isActualContentMatch(chunk, existingIndices[0])) {
return false;
}
existingIndices.push(this.lastContentIndex);
if (existingIndices.length < CONTENT_LOOP_THRESHOLD) {
return false;
}
// Analyze the most recent occurrences to see if they're clustered closely together
const recentIndices = existingIndices.slice(-CONTENT_LOOP_THRESHOLD);
const totalDistance =
recentIndices[recentIndices.length - 1] - recentIndices[0];
const averageDistance = totalDistance / (CONTENT_LOOP_THRESHOLD - 1);
const maxAllowedDistance = CONTENT_CHUNK_SIZE * 1.5;
return averageDistance <= maxAllowedDistance;
}
/**
* Verifies that two chunks with the same hash actually contain identical content.
* This prevents false positives from hash collisions.
*/
private isActualContentMatch(
currentChunk: string,
originalIndex: number,
): boolean {
const originalChunk = this.streamContentHistory.substring(
originalIndex,
originalIndex + CONTENT_CHUNK_SIZE,
);
return originalChunk === currentChunk;
}
private async checkForLoopWithLLM(signal: AbortSignal) {
const recentHistory = this.config
.getGeminiClient()
.getHistory()
.slice(-LLM_LOOP_CHECK_HISTORY_COUNT);
const prompt = `You are a sophisticated AI diagnostic agent specializing in identifying when a conversational AI is stuck in an unproductive state. Your task is to analyze the provided conversation history and determine if the assistant has ceased to make meaningful progress.
An unproductive state is characterized by one or more of the following patterns over the last 5 or more assistant turns:
Repetitive Actions: The assistant repeats the same tool calls or conversational responses a decent number of times. This includes simple loops (e.g., tool_A, tool_A, tool_A) and alternating patterns (e.g., tool_A, tool_B, tool_A, tool_B, ...).
Cognitive Loop: The assistant seems unable to determine the next logical step. It might express confusion, repeatedly ask the same questions, or generate responses that don't logically follow from the previous turns, indicating it's stuck and not advancing the task.
Crucially, differentiate between a true unproductive state and legitimate, incremental progress.
For example, a series of 'tool_A' or 'tool_B' tool calls that make small, distinct changes to the same file (like adding docstrings to functions one by one) is considered forward progress and is NOT a loop. A loop would be repeatedly replacing the same text with the same content, or cycling between a small set of files with no net change.
Please analyze the conversation history to determine the possibility that the conversation is stuck in a repetitive, non-productive state.`;
const contents = [
...recentHistory,
{ role: 'user', parts: [{ text: prompt }] },
];
const schema: SchemaUnion = {
type: Type.OBJECT,
properties: {
reasoning: {
type: Type.STRING,
description:
'Your reasoning on if the conversation is looping without forward progress.',
},
confidence: {
type: Type.NUMBER,
description:
'A number between 0.0 and 1.0 representing your confidence that the conversation is in an unproductive state.',
},
},
required: ['reasoning', 'confidence'],
};
let result;
try {
result = await this.config
.getGeminiClient()
.generateJson(contents, schema, signal, DEFAULT_GEMINI_FLASH_MODEL);
} catch (e) {
// Do nothing, treat it as a non-loop.
this.config.getDebugMode() ? console.error(e) : console.debug(e);
return false;
}
if (typeof result.confidence === 'number') {
if (result.confidence > 0.9) {
if (typeof result.reasoning === 'string' && result.reasoning) {
console.warn(result.reasoning);
}
logLoopDetected(
this.config,
new LoopDetectedEvent(LoopType.LLM_DETECTED_LOOP, this.promptId),
);
return true;
} else {
this.llmCheckInterval = Math.round(
MIN_LLM_CHECK_INTERVAL +
(MAX_LLM_CHECK_INTERVAL - MIN_LLM_CHECK_INTERVAL) *
(1 - result.confidence),
);
}
}
return false;
}
/**
* Resets all loop detection state.
*/
reset(promptId: string): void {
this.promptId = promptId;
this.resetToolCallCount();
this.resetContentTracking();
this.resetLlmCheckTracking();
this.loopDetected = false;
}
private resetToolCallCount(): void {
this.lastToolCallKey = null;
this.toolCallRepetitionCount = 0;
}
private resetContentTracking(resetHistory = true): void {
if (resetHistory) {
this.streamContentHistory = '';
}
this.contentStats.clear();
this.lastContentIndex = 0;
}
private resetLlmCheckTracking(): void {
this.turnsInCurrentPrompt = 0;
this.llmCheckInterval = DEFAULT_LLM_CHECK_INTERVAL;
this.lastCheckTurn = 0;
}
}
|