| |
| |
| |
| |
| |
|
|
| import type { Content } from '@google/genai'; |
| import { createHash } from 'node:crypto'; |
| import { GeminiEventType, type ServerGeminiStreamEvent } from '../core/turn.js'; |
| import { |
| logLoopDetected, |
| logLoopDetectionDisabled, |
| logLlmLoopCheck, |
| } from '../telemetry/loggers.js'; |
| import { |
| LoopDetectedEvent, |
| LoopDetectionDisabledEvent, |
| LoopType, |
| LlmLoopCheckEvent, |
| LlmRole, |
| } from '../telemetry/types.js'; |
| import { |
| isFunctionCall, |
| isFunctionResponse, |
| } from '../utils/messageInspectors.js'; |
| import { debugLogger } from '../utils/debugLogger.js'; |
| import type { AgentLoopContext } from '../config/agent-loop-context.js'; |
|
|
| const TOOL_CALL_LOOP_THRESHOLD = 5; |
| const CONTENT_LOOP_THRESHOLD = 10; |
| const CONTENT_CHUNK_SIZE = 50; |
| const MAX_HISTORY_LENGTH = 5000; |
|
|
| |
| |
| |
| const LLM_LOOP_CHECK_HISTORY_COUNT = 20; |
|
|
| |
| |
| |
| const LLM_CHECK_AFTER_TURNS = 30; |
|
|
| |
| |
| |
| |
| const DEFAULT_LLM_CHECK_INTERVAL = 10; |
|
|
| |
| |
| |
| |
| const MIN_LLM_CHECK_INTERVAL = 5; |
|
|
| |
| |
| |
| |
| const MAX_LLM_CHECK_INTERVAL = 15; |
|
|
| |
| |
| |
| const LLM_CONFIDENCE_THRESHOLD = 0.9; |
| const DOUBLE_CHECK_MODEL_ALIAS = 'loop-detection-double-check'; |
|
|
| const LOOP_DETECTION_SYSTEM_PROMPT = `You are a diagnostic agent that determines whether a conversational AI assistant is stuck in an unproductive loop. Analyze the conversation history (and, if provided, the original user request) to make this determination. |
| |
| ## What constitutes an unproductive state |
| |
| An unproductive state requires BOTH of the following to be true: |
| 1. The assistant has exhibited a repetitive pattern over at least 5 consecutive model actions (tool calls or text responses, counting only model-role turns). |
| 2. The repetition produces NO net change or forward progress toward the user's goal. |
| |
| Specific patterns to look for: |
| - **Alternating cycles with no net effect:** The assistant cycles between the same actions (e.g., edit_file → run_build → edit_file → run_build) where each iteration applies the same edit and encounters the same error, making zero progress. Note: alternating between actions is only a loop if the arguments and outcomes are substantively identical each cycle. If the assistant is modifying different code or getting different errors, that is debugging progress, not a loop. |
| - **Semantic repetition with identical outcomes:** The assistant calls the same tool with semantically equivalent arguments (same file, same line range, same content) multiple times consecutively, and each call produces the same outcome. This does NOT include build/test commands that are re-run after making code changes between invocations — re-running a build to verify a fix is normal workflow. |
| - **Stuck reasoning:** The assistant produces multiple consecutive text responses that restate the same plan, question, or analysis without taking any new action or making a decision. This does NOT include command output that happens to contain repeated status lines or warnings. |
| |
| ## What is NOT an unproductive state |
| |
| You MUST distinguish repetitive-looking but productive work from true loops. The following are examples of forward progress and must NOT be flagged: |
| |
| - **Cross-file batch operations:** A series of tool calls with the same tool name but targeting different files (different file paths in the arguments). For example, adding license headers to 20 files, or running the same refactoring across multiple modules. |
| - **Incremental same-file edits:** Multiple edits to the same file that target different line ranges, different functions, or different text content (e.g., adding docstrings to functions one by one). |
| - **Sequential processing:** A series of read or search operations on different files/paths to gather information. |
| - **Retry with variation:** Re-attempting a failed operation with modified arguments or a different approach. |
| |
| ## Argument analysis (critical) |
| |
| When evaluating tool calls, you MUST compare the **arguments** of each call, not just the tool name. Pay close attention to: |
| - **File paths:** Different file paths mean different targets — this is distinct work, not repetition. |
| - **Line numbers and text content:** Different line ranges or different old_string/new_string values indicate distinct edits. |
| - **Search queries and patterns:** Different search terms indicate information gathering, not looping. |
| |
| A loop exists only when the same tool is called with semantically equivalent arguments repeatedly, indicating no forward progress. |
| |
| ## Using the original user request |
| |
| If the original user request is provided, use it to contextualize the assistant's behavior. If the request implies a batch or multi-step operation (e.g., "update all files", "refactor every module", "add tests for each function"), then repetitive tool calls with varying arguments are expected and should weigh heavily against flagging a loop.`; |
|
|
| const LOOP_DETECTION_SCHEMA: Record<string, unknown> = { |
| type: 'object', |
| properties: { |
| unproductive_state_analysis: { |
| type: 'string', |
| description: |
| 'Your reasoning on if the conversation is looping without forward progress.', |
| }, |
| unproductive_state_confidence: { |
| type: 'number', |
| description: |
| 'A number between 0.0 and 1.0 representing your confidence that the conversation is in an unproductive state.', |
| }, |
| }, |
| required: ['unproductive_state_analysis', 'unproductive_state_confidence'], |
| }; |
|
|
| |
| |
| |
| export interface LoopDetectionResult { |
| count: number; |
| type?: LoopType; |
| detail?: string; |
| confirmedByModel?: string; |
| } |
| |
| |
| |
| |
| export class LoopDetectionService { |
| private readonly context: AgentLoopContext; |
| private promptId = ''; |
| private userPrompt = ''; |
|
|
| |
| private toolCallHistory: string[] = []; |
|
|
| |
| private streamContentHistory = ''; |
| private contentStats = new Map<string, number[]>(); |
| private lastContentIndex = 0; |
| private loopDetected = false; |
| private detectedCount = 0; |
| private lastLoopDetail?: string; |
| private inCodeBlock = false; |
|
|
| private lastLoopType?: LoopType; |
| |
| private turnsInCurrentPrompt = 0; |
| private llmCheckInterval = DEFAULT_LLM_CHECK_INTERVAL; |
| private lastCheckTurn = 0; |
|
|
| |
| private disabledForSession = false; |
|
|
| constructor(context: AgentLoopContext) { |
| this.context = context; |
| } |
|
|
| |
| |
| |
| disableForSession(): void { |
| this.disabledForSession = true; |
| logLoopDetectionDisabled( |
| this.context.config, |
| new LoopDetectionDisabledEvent(this.promptId), |
| ); |
| } |
|
|
| 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'); |
| } |
|
|
| |
| |
| |
| |
| |
| addAndCheck(event: ServerGeminiStreamEvent): LoopDetectionResult { |
| if ( |
| this.disabledForSession || |
| this.context.config.getDisableLoopDetection() |
| ) { |
| return { count: 0 }; |
| } |
| if (this.loopDetected) { |
| return { |
| count: this.detectedCount, |
| type: this.lastLoopType, |
| detail: this.lastLoopDetail, |
| }; |
| } |
|
|
| let isLoop = false; |
| let detail: string | undefined; |
|
|
| switch (event.type) { |
| case GeminiEventType.ToolCallRequest: |
| |
| |
| this.resetContentTracking(); |
| isLoop = this.checkToolCallLoop(event.value); |
| if (isLoop) { |
| detail = `Repeated tool call: ${event.value.name} with arguments ${JSON.stringify(event.value.args)}`; |
| } |
| break; |
| case GeminiEventType.Content: |
| isLoop = this.checkContentLoop(event.value); |
| if (isLoop) { |
| detail = `Repeating content detected: "${this.streamContentHistory.substring(Math.max(0, this.lastContentIndex - 20), this.lastContentIndex + CONTENT_CHUNK_SIZE).trim()}..."`; |
| } |
| break; |
| default: |
| break; |
| } |
|
|
| if (isLoop) { |
| this.loopDetected = true; |
| this.detectedCount++; |
| this.lastLoopDetail = detail; |
| this.lastLoopType = |
| event.type === GeminiEventType.ToolCallRequest |
| ? LoopType.CONSECUTIVE_IDENTICAL_TOOL_CALLS |
| : LoopType.CONTENT_CHANTING_LOOP; |
|
|
| logLoopDetected( |
| this.context.config, |
| new LoopDetectedEvent( |
| this.lastLoopType, |
| this.promptId, |
| this.detectedCount, |
| ), |
| ); |
| } |
| return isLoop |
| ? { |
| count: this.detectedCount, |
| type: this.lastLoopType, |
| detail: this.lastLoopDetail, |
| } |
| : { count: 0 }; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async turnStarted(signal: AbortSignal): Promise<LoopDetectionResult> { |
| if ( |
| this.disabledForSession || |
| this.context.config.getDisableLoopDetection() |
| ) { |
| return { count: 0 }; |
| } |
| if (this.loopDetected) { |
| return { |
| count: this.detectedCount, |
| type: this.lastLoopType, |
| detail: this.lastLoopDetail, |
| }; |
| } |
|
|
| this.turnsInCurrentPrompt++; |
|
|
| if ( |
| this.turnsInCurrentPrompt >= LLM_CHECK_AFTER_TURNS && |
| this.turnsInCurrentPrompt - this.lastCheckTurn >= this.llmCheckInterval |
| ) { |
| this.lastCheckTurn = this.turnsInCurrentPrompt; |
| const { isLoop, analysis, confirmedByModel } = |
| await this.checkForLoopWithLLM(signal); |
| if (isLoop) { |
| this.loopDetected = true; |
| this.detectedCount++; |
| this.lastLoopDetail = analysis; |
| this.lastLoopType = LoopType.LLM_DETECTED_LOOP; |
|
|
| logLoopDetected( |
| this.context.config, |
| new LoopDetectedEvent( |
| this.lastLoopType, |
| this.promptId, |
| this.detectedCount, |
| confirmedByModel, |
| analysis, |
| LLM_CONFIDENCE_THRESHOLD, |
| ), |
| ); |
|
|
| return { |
| count: this.detectedCount, |
| type: this.lastLoopType, |
| detail: this.lastLoopDetail, |
| confirmedByModel, |
| }; |
| } |
| } |
| return { count: 0 }; |
| } |
|
|
| private checkToolCallLoop(toolCall: { name: string; args: object }): boolean { |
| const key = this.getToolCallKey(toolCall); |
| this.toolCallHistory.push(key); |
|
|
| const maxRequiredLength = 5 * TOOL_CALL_LOOP_THRESHOLD; |
| if (this.toolCallHistory.length > maxRequiredLength) { |
| this.toolCallHistory = this.toolCallHistory.slice(-maxRequiredLength); |
| } |
|
|
| const n = this.toolCallHistory.length; |
| const R = TOOL_CALL_LOOP_THRESHOLD; |
|
|
| |
| for (let k = 1; k <= 5; k++) { |
| const requiredLength = k * R; |
| if (n >= requiredLength) { |
| const cycle = this.toolCallHistory.slice(-k); |
| let isPatternMatch = true; |
|
|
| for (let i = 0; i < requiredLength; i++) { |
| const indexFromEnd = requiredLength - i; |
| const actualKey = this.toolCallHistory[n - indexFromEnd]; |
| const expectedKey = cycle[i % k]; |
| if (actualKey !== expectedKey) { |
| isPatternMatch = false; |
| break; |
| } |
| } |
|
|
| if (isPatternMatch) { |
| return true; |
| } |
| } |
| } |
|
|
| return false; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| private checkContentLoop(content: string): boolean { |
| |
| |
| |
| const numFences = (content.match(/```/g) ?? []).length; |
| const hasTable = /(^|\n)\s*(\|.*\||[|+-]{3,})/.test(content); |
| const hasListItem = |
| /(^|\n)\s*[*-+]\s/.test(content) || /(^|\n)\s*\d+\.\s/.test(content); |
| const hasHeading = /(^|\n)#+\s/.test(content); |
| const hasBlockquote = /(^|\n)>\s/.test(content); |
| const isDivider = /^[+-_=*\u2500-\u257F]+$/.test(content); |
|
|
| if ( |
| numFences || |
| hasTable || |
| hasListItem || |
| hasHeading || |
| hasBlockquote || |
| isDivider |
| ) { |
| |
| |
| this.resetContentTracking(); |
| } |
|
|
| const wasInCodeBlock = this.inCodeBlock; |
| this.inCodeBlock = |
| numFences % 2 === 0 ? this.inCodeBlock : !this.inCodeBlock; |
| if (wasInCodeBlock || this.inCodeBlock || isDivider) { |
| return false; |
| } |
|
|
| this.streamContentHistory += content; |
|
|
| this.truncateAndUpdate(); |
| return this.analyzeContentChunksForLoop(); |
| } |
|
|
| |
| |
| |
| |
| private truncateAndUpdate(): void { |
| if (this.streamContentHistory.length <= MAX_HISTORY_LENGTH) { |
| return; |
| } |
|
|
| |
| const truncationAmount = |
| this.streamContentHistory.length - MAX_HISTORY_LENGTH; |
| this.streamContentHistory = |
| this.streamContentHistory.slice(truncationAmount); |
| this.lastContentIndex = Math.max( |
| 0, |
| this.lastContentIndex - truncationAmount, |
| ); |
|
|
| |
| 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); |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| private analyzeContentChunksForLoop(): boolean { |
| while (this.hasMoreChunksToProcess()) { |
| |
| 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)) { |
| return true; |
| } |
|
|
| |
| this.lastContentIndex++; |
| } |
|
|
| return false; |
| } |
|
|
| private hasMoreChunksToProcess(): boolean { |
| return ( |
| this.lastContentIndex + CONTENT_CHUNK_SIZE <= |
| this.streamContentHistory.length |
| ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 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; |
| } |
|
|
| |
| 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 * 5; |
|
|
| if (averageDistance > maxAllowedDistance) { |
| return false; |
| } |
|
|
| |
| |
| const periods = new Set<string>(); |
| for (let i = 0; i < recentIndices.length - 1; i++) { |
| periods.add( |
| this.streamContentHistory.substring( |
| recentIndices[i], |
| recentIndices[i + 1], |
| ), |
| ); |
| } |
|
|
| |
| |
| |
| if (periods.size > Math.floor(CONTENT_LOOP_THRESHOLD / 2)) { |
| return false; |
| } |
|
|
| return true; |
| } |
|
|
| |
| |
| |
| |
| private isActualContentMatch( |
| currentChunk: string, |
| originalIndex: number, |
| ): boolean { |
| const originalChunk = this.streamContentHistory.substring( |
| originalIndex, |
| originalIndex + CONTENT_CHUNK_SIZE, |
| ); |
| return originalChunk === currentChunk; |
| } |
|
|
| private trimRecentHistory(history: Content[]): Content[] { |
| |
| |
| |
| while (history.length > 0 && isFunctionCall(history[history.length - 1])) { |
| history.pop(); |
| } |
|
|
| |
| |
| |
| while (history.length > 0 && isFunctionResponse(history[0])) { |
| history.shift(); |
| } |
|
|
| return history; |
| } |
|
|
| private async checkForLoopWithLLM(signal: AbortSignal): Promise<{ |
| isLoop: boolean; |
| analysis?: string; |
| confirmedByModel?: string; |
| }> { |
| const recentHistory = this.context.geminiClient |
| .getHistory() |
| .slice(-LLM_LOOP_CHECK_HISTORY_COUNT); |
|
|
| const trimmedHistory = this.trimRecentHistory(recentHistory); |
|
|
| const taskPrompt = `Please analyze the conversation history to determine the possibility that the conversation is stuck in a repetitive, non-productive state. Consider the original user request when evaluating whether repeated tool calls represent legitimate batch work or an actual loop. Provide your response in the requested JSON format.`; |
|
|
| const contents = [ |
| ...(this.userPrompt |
| ? [ |
| { |
| role: 'user' as const, |
| parts: [ |
| { |
| text: `<original_user_request>\n${this.userPrompt}\n</original_user_request>`, |
| }, |
| ], |
| }, |
| ] |
| : []), |
| ...trimmedHistory, |
| { role: 'user', parts: [{ text: taskPrompt }] }, |
| ]; |
| if (contents.length > 0 && isFunctionCall(contents[0])) { |
| contents.unshift({ |
| role: 'user', |
| parts: [{ text: 'Recent conversation history:' }], |
| }); |
| } |
|
|
| const flashResult = await this.queryLoopDetectionModel( |
| 'loop-detection', |
| contents, |
| signal, |
| ); |
|
|
| if (!flashResult) { |
| return { isLoop: false }; |
| } |
|
|
| const flashConfidence = |
| |
| typeof flashResult['unproductive_state_confidence'] === 'number' |
| ? flashResult['unproductive_state_confidence'] |
| : 0; |
| const flashAnalysis = |
| |
| typeof flashResult['unproductive_state_analysis'] === 'string' |
| ? flashResult['unproductive_state_analysis'] |
| : ''; |
|
|
| const doubleCheckModelName = |
| this.context.config.modelConfigService.getResolvedConfig({ |
| model: DOUBLE_CHECK_MODEL_ALIAS, |
| }).model; |
|
|
| if (flashConfidence < LLM_CONFIDENCE_THRESHOLD) { |
| logLlmLoopCheck( |
| this.context.config, |
| new LlmLoopCheckEvent( |
| this.promptId, |
| flashConfidence, |
| doubleCheckModelName, |
| -1, |
| ), |
| ); |
| this.updateCheckInterval(flashConfidence); |
| return { isLoop: false }; |
| } |
|
|
| const availability = this.context.config.getModelAvailabilityService(); |
|
|
| if (!availability.snapshot(doubleCheckModelName).available) { |
| const flashModelName = |
| this.context.config.modelConfigService.getResolvedConfig({ |
| model: 'loop-detection', |
| }).model; |
| return { |
| isLoop: true, |
| analysis: flashAnalysis, |
| confirmedByModel: flashModelName, |
| }; |
| } |
|
|
| |
| const mainModelResult = await this.queryLoopDetectionModel( |
| DOUBLE_CHECK_MODEL_ALIAS, |
| contents, |
| signal, |
| ); |
|
|
| const mainModelConfidence = |
| mainModelResult && |
| |
| typeof mainModelResult['unproductive_state_confidence'] === 'number' |
| ? mainModelResult['unproductive_state_confidence'] |
| : 0; |
| const mainModelAnalysis = |
| mainModelResult && |
| |
| typeof mainModelResult['unproductive_state_analysis'] === 'string' |
| ? mainModelResult['unproductive_state_analysis'] |
| : undefined; |
|
|
| logLlmLoopCheck( |
| this.context.config, |
| new LlmLoopCheckEvent( |
| this.promptId, |
| flashConfidence, |
| doubleCheckModelName, |
| mainModelConfidence, |
| ), |
| ); |
|
|
| if (mainModelResult) { |
| if (mainModelConfidence >= LLM_CONFIDENCE_THRESHOLD) { |
| return { |
| isLoop: true, |
| analysis: mainModelAnalysis, |
| confirmedByModel: doubleCheckModelName, |
| }; |
| } else { |
| this.updateCheckInterval(mainModelConfidence); |
| } |
| } |
|
|
| return { isLoop: false }; |
| } |
|
|
| private async queryLoopDetectionModel( |
| model: string, |
| contents: Content[], |
| signal: AbortSignal, |
| ): Promise<Record<string, unknown> | null> { |
| try { |
| const result = await this.context.config.getBaseLlmClient().generateJson({ |
| modelConfigKey: { model }, |
| contents, |
| schema: LOOP_DETECTION_SCHEMA, |
| systemInstruction: LOOP_DETECTION_SYSTEM_PROMPT, |
| abortSignal: signal, |
| promptId: this.promptId, |
| maxAttempts: 2, |
| role: LlmRole.UTILITY_LOOP_DETECTOR, |
| }); |
|
|
| if ( |
| result && |
| |
| typeof result['unproductive_state_confidence'] === 'number' |
| ) { |
| return result; |
| } |
| return null; |
| } catch (error) { |
| if (this.context.config.getDebugMode()) { |
| debugLogger.warn( |
| `Error querying loop detection model (${model}): ${String(error)}`, |
| ); |
| } |
| return null; |
| } |
| } |
|
|
| private updateCheckInterval(unproductive_state_confidence: number): void { |
| this.llmCheckInterval = Math.round( |
| MIN_LLM_CHECK_INTERVAL + |
| (MAX_LLM_CHECK_INTERVAL - MIN_LLM_CHECK_INTERVAL) * |
| (1 - unproductive_state_confidence), |
| ); |
| } |
|
|
| |
| |
| |
| reset(promptId: string, userPrompt?: string): void { |
| this.promptId = promptId; |
| this.userPrompt = userPrompt ?? ''; |
| this.resetToolCallCount(); |
| this.resetContentTracking(); |
| this.resetLlmCheckTracking(); |
| this.loopDetected = false; |
| this.detectedCount = 0; |
| this.lastLoopDetail = undefined; |
| this.lastLoopType = undefined; |
| } |
|
|
| |
| |
| |
| |
| clearDetection(): void { |
| this.loopDetected = false; |
| } |
|
|
| private resetToolCallCount(): void { |
| this.toolCallHistory = []; |
| } |
|
|
| 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; |
| } |
| } |
|
|