From aa8b2abe3dac884ba7333f101a0a0f6dc0ee3108 Mon Sep 17 00:00:00 2001 From: anthony bushong Date: Wed, 1 Oct 2025 18:35:45 -0700 Subject: [PATCH] fix(core): add telemetry support for smart edit correction events (#10378) Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- .../clearcut-logger/clearcut-logger.ts | 16 +++++++++++++ .../clearcut-logger/event-metadata-key.ts | 13 ++++++++--- packages/core/src/telemetry/constants.ts | 1 + packages/core/src/telemetry/loggers.ts | 23 +++++++++++++++++++ packages/core/src/telemetry/types.ts | 12 ++++++++++ packages/core/src/tools/smart-edit.ts | 10 +++++++- 6 files changed, 71 insertions(+), 4 deletions(-) diff --git a/packages/core/src/telemetry/clearcut-logger/clearcut-logger.ts b/packages/core/src/telemetry/clearcut-logger/clearcut-logger.ts index faf2358ed4..c726730a41 100644 --- a/packages/core/src/telemetry/clearcut-logger/clearcut-logger.ts +++ b/packages/core/src/telemetry/clearcut-logger/clearcut-logger.ts @@ -32,6 +32,7 @@ import type { ModelSlashCommandEvent, ExtensionDisableEvent, SmartEditStrategyEvent, + SmartEditCorrectionEvent, } from '../types.js'; import { EventMetadataKey } from './event-metadata-key.js'; import type { Config } from '../../config/config.js'; @@ -77,6 +78,7 @@ export enum EventNames { MODEL_ROUTING = 'model_routing', MODEL_SLASH_COMMAND = 'model_slash_command', SMART_EDIT_STRATEGY = 'smart_edit_strategy', + SMART_EDIT_CORRECTION = 'smart_edit_correction', } export interface LogResponse { @@ -1055,6 +1057,20 @@ export class ClearcutLogger { this.flushIfNeeded(); } + logSmartEditCorrectionEvent(event: SmartEditCorrectionEvent): void { + const data: EventValue[] = [ + { + gemini_cli_key: EventMetadataKey.GEMINI_CLI_SMART_EDIT_CORRECTION, + value: event.correction, + }, + ]; + + this.enqueueLogEvent( + this.createLogEvent(EventNames.SMART_EDIT_CORRECTION, data), + ); + this.flushIfNeeded(); + } + /** * Adds default fields to data, and returns a new data array. This fields * should exist on all log events. diff --git a/packages/core/src/telemetry/clearcut-logger/event-metadata-key.ts b/packages/core/src/telemetry/clearcut-logger/event-metadata-key.ts index 6cf9f6cab0..d826c4370b 100644 --- a/packages/core/src/telemetry/clearcut-logger/event-metadata-key.ts +++ b/packages/core/src/telemetry/clearcut-logger/event-metadata-key.ts @@ -88,6 +88,16 @@ export enum EventMetadataKey { // Logs the length of tool output GEMINI_CLI_TOOL_CALL_CONTENT_LENGTH = 93, + // ========================================================================== + // Replace Tool Call Event Keys + // =========================================================================== + + // Logs a smart edit tool strategy choice. + GEMINI_CLI_SMART_EDIT_STRATEGY = 109, + + // Logs a smart edit correction event. + GEMINI_CLI_SMART_EDIT_CORRECTION = 110, + // ========================================================================== // GenAI API Request Event Keys // =========================================================================== @@ -416,7 +426,4 @@ export enum EventMetadataKey { // Logs an event when the user uses the /model command. GEMINI_CLI_MODEL_SLASH_COMMAND = 108, - - // Logs a smart edit tool strategy choice. - GEMINI_CLI_SMART_EDIT_STRATEGY = 109, } diff --git a/packages/core/src/telemetry/constants.ts b/packages/core/src/telemetry/constants.ts index bc5f68ef11..f87148cea2 100644 --- a/packages/core/src/telemetry/constants.ts +++ b/packages/core/src/telemetry/constants.ts @@ -33,6 +33,7 @@ export const EVENT_FILE_OPERATION = 'gemini_cli.file_operation'; export const EVENT_MODEL_SLASH_COMMAND = 'gemini_cli.slash_command.model'; export const EVENT_SMART_EDIT_STRATEGY = 'gemini_cli.smart_edit.strategy'; export const EVENT_MODEL_ROUTING = 'gemini_cli.model_routing'; +export const EVENT_SMART_EDIT_CORRECTION = 'gemini_cli.smart_edit.correction'; // Performance Events export const EVENT_STARTUP_PERFORMANCE = 'gemini_cli.startup.performance'; diff --git a/packages/core/src/telemetry/loggers.ts b/packages/core/src/telemetry/loggers.ts index 0efc394ced..dde56a7136 100644 --- a/packages/core/src/telemetry/loggers.ts +++ b/packages/core/src/telemetry/loggers.ts @@ -35,6 +35,7 @@ import { EVENT_MODEL_SLASH_COMMAND, EVENT_EXTENSION_DISABLE, EVENT_SMART_EDIT_STRATEGY, + EVENT_SMART_EDIT_CORRECTION, } from './constants.js'; import type { ApiErrorEvent, @@ -66,6 +67,7 @@ import type { ExtensionInstallEvent, ModelSlashCommandEvent, SmartEditStrategyEvent, + SmartEditCorrectionEvent, } from './types.js'; import { recordApiErrorMetrics, @@ -839,3 +841,24 @@ export function logSmartEditStrategy( }; logger.emit(logRecord); } + +export function logSmartEditCorrectionEvent( + config: Config, + event: SmartEditCorrectionEvent, +): void { + ClearcutLogger.getInstance(config)?.logSmartEditCorrectionEvent(event); + if (!isTelemetrySdkInitialized()) return; + + const attributes: LogAttributes = { + ...getCommonAttributes(config), + ...event, + 'event.name': EVENT_SMART_EDIT_CORRECTION, + }; + + const logger = logs.getLogger(SERVICE_NAME); + const logRecord: LogRecord = { + body: `Smart Edit Tool Correction: ${event.correction}`, + attributes, + }; + logger.emit(logRecord); +} diff --git a/packages/core/src/telemetry/types.ts b/packages/core/src/telemetry/types.ts index b83a218b4d..6481847ca6 100644 --- a/packages/core/src/telemetry/types.ts +++ b/packages/core/src/telemetry/types.ts @@ -714,3 +714,15 @@ export class SmartEditStrategyEvent implements BaseTelemetryEvent { this.strategy = strategy; } } + +export class SmartEditCorrectionEvent implements BaseTelemetryEvent { + 'event.name': 'smart_edit_correction'; + 'event.timestamp': string; + correction: 'success' | 'failure'; + + constructor(correction: 'success' | 'failure') { + this['event.name'] = 'smart_edit_correction'; + this['event.timestamp'] = new Date().toISOString(); + this.correction = correction; + } +} diff --git a/packages/core/src/tools/smart-edit.ts b/packages/core/src/tools/smart-edit.ts index 5efdec620e..81a0ef609d 100644 --- a/packages/core/src/tools/smart-edit.ts +++ b/packages/core/src/tools/smart-edit.ts @@ -34,6 +34,8 @@ import { applyReplacement } from './edit.js'; import { safeLiteralReplace } from '../utils/textUtils.js'; import { SmartEditStrategyEvent } from '../telemetry/types.js'; import { logSmartEditStrategy } from '../telemetry/loggers.js'; +import { SmartEditCorrectionEvent } from '../telemetry/types.js'; +import { logSmartEditCorrectionEvent } from '../telemetry/loggers.js'; interface ReplacementContext { params: EditToolParams; @@ -416,7 +418,10 @@ class EditToolInvocation implements ToolInvocation { ); if (secondError) { - // The fix failed, return the original error + // The fix failed, log failure and return the original error + const event = new SmartEditCorrectionEvent('failure'); + logSmartEditCorrectionEvent(this.config, event); + return { currentContent, newContent: currentContent, @@ -427,6 +432,9 @@ class EditToolInvocation implements ToolInvocation { }; } + const event = new SmartEditCorrectionEvent('success'); + logSmartEditCorrectionEvent(this.config, event); + return { currentContent, newContent: secondAttemptResult.newContent,