Log the 2 types of loop detection (#4193)

This commit is contained in:
Sandy Tao
2025-07-14 21:44:07 -07:00
committed by GitHub
parent 734da8b9d2
commit 886faa2990
7 changed files with 102 additions and 5 deletions

View File

@@ -15,6 +15,7 @@ import {
ApiResponseEvent,
ApiErrorEvent,
FlashFallbackEvent,
LoopDetectedEvent,
} from '../types.js';
import { EventMetadataKey } from './event-metadata-key.js';
import { Config } from '../../config/config.js';
@@ -33,6 +34,7 @@ const api_response_event_name = 'api_response';
const api_error_event_name = 'api_error';
const end_session_event_name = 'end_session';
const flash_fallback_event_name = 'flash_fallback';
const loop_detected_event_name = 'loop_detected';
export interface LogResponse {
nextRequestWaitMs?: number;
@@ -448,6 +450,18 @@ export class ClearcutLogger {
});
}
logLoopDetectedEvent(event: LoopDetectedEvent): void {
const data = [
{
gemini_cli_key: EventMetadataKey.GEMINI_CLI_LOOP_DETECTED_TYPE,
value: JSON.stringify(event.loop_type),
},
];
this.enqueueLogEvent(this.createLogEvent(loop_detected_event_name, data));
this.flushIfNeeded();
}
logEndSessionEvent(event: EndSessionEvent): void {
const data = [
{

View File

@@ -150,6 +150,13 @@ export enum EventMetadataKey {
// Logs the total number of Google accounts ever used.
GEMINI_CLI_GOOGLE_ACCOUNTS_COUNT = 37,
// ==========================================================================
// Loop Detected Event Keys
// ===========================================================================
// Logs the type of loop detected.
GEMINI_CLI_LOOP_DETECTED_TYPE = 38,
}
export function getEventMetadataKey(

View File

@@ -25,6 +25,7 @@ import {
ToolCallEvent,
UserPromptEvent,
FlashFallbackEvent,
LoopDetectedEvent,
} from './types.js';
import {
recordApiErrorMetrics,
@@ -288,3 +289,23 @@ export function logApiResponse(config: Config, event: ApiResponseEvent): void {
);
recordTokenUsageMetrics(config, event.model, event.tool_token_count, 'tool');
}
export function logLoopDetected(
config: Config,
event: LoopDetectedEvent,
): void {
ClearcutLogger.getInstance(config)?.logLoopDetectedEvent(event);
if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
};
const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = {
body: `Loop detected. Type: ${event.loop_type}.`,
attributes,
};
logger.emit(logRecord);
}

View File

@@ -246,6 +246,23 @@ export class FlashFallbackEvent {
}
}
export enum LoopType {
CONSECUTIVE_IDENTICAL_TOOL_CALLS = 'consecutive_identical_tool_calls',
CHANTING_IDENTICAL_SENTENCES = 'chanting_identical_sentences',
}
export class LoopDetectedEvent {
'event.name': 'loop_detected';
'event.timestamp': string; // ISO 8601
loop_type: LoopType;
constructor(loop_type: LoopType) {
this['event.name'] = 'loop_detected';
this['event.timestamp'] = new Date().toISOString();
this.loop_type = loop_type;
}
}
export type TelemetryEvent =
| StartSessionEvent
| EndSessionEvent
@@ -254,4 +271,5 @@ export type TelemetryEvent =
| ApiRequestEvent
| ApiErrorEvent
| ApiResponseEvent
| FlashFallbackEvent;
| FlashFallbackEvent
| LoopDetectedEvent;