| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- import { createHash } from 'node:crypto';
- import type { LocalRuntimeLogger } from '../common/logger.js';
- import { logger as defaultLogger } from '../common/logger.js';
- import type { MetricsClient } from '../common/metrics.js';
- import type { PreparedReview } from './preparation.js';
- export type ReviewValidationStage = 'candidate' | 'document' | 'finding' | 'correction';
- export type ReviewValidationOutcome = 'first_pass' | 'recovered' | 'partial' | 'failed';
- type ReviewValidationCategory = 'format' | 'projection';
- export interface ReviewValidationObservabilityOptions {
- metricsClient?: MetricsClient;
- logger?: Pick<LocalRuntimeLogger, 'warn' | 'error'>;
- model?: string;
- }
- interface ReviewValidationIdentity {
- sessionId: string;
- turnId: string;
- prepared: PreparedReview;
- }
- interface ReviewValidationFailureInput extends ReviewValidationIdentity {
- stage: ReviewValidationStage;
- error: unknown;
- candidateText: string;
- reasonCode?: string;
- details?: unknown;
- assistant?: ReviewAssistantEventContext;
- }
- export interface ReviewAssistantEventContext {
- stopReason: string;
- errorMessage?: string;
- responseId?: string;
- responseModel?: string;
- contentTypes: string[];
- diagnostics?: unknown;
- signalAborted: boolean;
- }
- interface RecordedFailure {
- fields: Record<string, unknown>;
- }
- export class ReviewValidationObserver {
- private readonly metricsClient: MetricsClient | undefined;
- private readonly validationLogger: Pick<LocalRuntimeLogger, 'warn' | 'error'>;
- private readonly model: string | undefined;
- private sawFailure = false;
- private sawFormatFailure = false;
- private resultRecorded = false;
- private lastFailure: RecordedFailure | undefined;
- constructor(options: ReviewValidationObservabilityOptions = {}) {
- this.metricsClient = options.metricsClient;
- this.validationLogger = options.logger ?? defaultLogger;
- this.model = options.model;
- }
- recordFailure(input: ReviewValidationFailureInput): void {
- const attempt = this.sawFailure ? 'retry' : 'initial';
- const message = errorMessage(input.error);
- const invalidEntityLocation =
- input.stage === 'document' ? findUnescapedXmlEntity(input.candidateText) : undefined;
- const reasonCode =
- input.reasonCode ??
- (invalidEntityLocation
- ? 'xml_invalid_entity'
- : classifyReviewValidationError(input.stage, message));
- const category = validationCategory(input.stage, reasonCode);
- const location = invalidEntityLocation ?? parseXmlErrorLocation(message);
- this.sawFailure = true;
- if (category === 'format') this.sawFormatFailure = true;
- const fields = {
- event: 'code_review_candidate_validation_failed',
- session_id: input.sessionId,
- turn_id: input.turnId,
- review_run_id: input.prepared.context.reviewRunId,
- trigger: input.prepared.trigger,
- mode: input.prepared.mode,
- attempt,
- stage: input.stage,
- category,
- reason_code: reasonCode,
- error: message,
- ...(location ?? {}),
- candidate_length: input.candidateText.length,
- candidate_sha256: createHash('sha256').update(input.candidateText).digest('hex'),
- // Review candidates are capped at 1 MB by the XML parser. Keep the full
- // rejected document in the local engineering log so field reports retain
- // enough evidence to reproduce model-format failures.
- candidate_xml: input.candidateText,
- ...(input.assistant ? assistantEventFields(input.assistant, attempt) : {}),
- ...(this.model ? { model: this.model } : {}),
- ...(input.details !== undefined ? { details: input.details } : {}),
- };
- this.lastFailure = { fields };
- this.validationLogger.warn(fields, 'Code review candidate validation failed');
- this.metricsClient?.counter('code_review_candidate_validation_failure_total', 1, {
- attempt,
- category,
- mode: input.prepared.mode,
- reasonCode,
- stage: input.stage,
- trigger: input.prepared.trigger,
- });
- }
- recordIgnoredTerminalEmpty(
- input: ReviewValidationIdentity & {
- candidateText: string;
- assistant: ReviewAssistantEventContext;
- },
- ): void {
- const attempt = this.sawFailure ? 'retry' : 'initial';
- const fields = {
- event: 'code_review_assistant_empty_ignored',
- session_id: input.sessionId,
- turn_id: input.turnId,
- review_run_id: input.prepared.context.reviewRunId,
- trigger: input.prepared.trigger,
- mode: input.prepared.mode,
- attempt,
- reason_code: `assistant_empty_${input.assistant.stopReason}`,
- candidate_length: input.candidateText.length,
- candidate_sha256: createHash('sha256').update(input.candidateText).digest('hex'),
- candidate_xml: input.candidateText,
- ...assistantEventFields(input.assistant, attempt),
- ...(this.model ? { model: this.model } : {}),
- };
- this.validationLogger.warn(fields, 'Code review ignored an empty terminal assistant event');
- }
- recordResult(identity: ReviewValidationIdentity, outcome?: ReviewValidationOutcome): void {
- if (this.resultRecorded) return;
- this.resultRecorded = true;
- this.metricsClient?.counter('code_review_candidate_result_total', 1, {
- mode: identity.prepared.mode,
- formatStatus: this.sawFormatFailure ? 'invalid' : 'valid',
- outcome: outcome ?? (this.sawFailure ? 'recovered' : 'first_pass'),
- trigger: identity.prepared.trigger,
- });
- }
- recordExhausted(identity: ReviewValidationIdentity): void {
- this.recordResult(identity, 'failed');
- const fields = {
- ...(this.lastFailure?.fields ?? {
- session_id: identity.sessionId,
- turn_id: identity.turnId,
- review_run_id: identity.prepared.context.reviewRunId,
- trigger: identity.prepared.trigger,
- mode: identity.prepared.mode,
- }),
- event: 'code_review_candidate_validation_exhausted',
- };
- this.validationLogger.error(fields, 'Code review candidate validation exhausted');
- }
- }
- function assistantEventFields(
- assistant: ReviewAssistantEventContext,
- attempt: 'initial' | 'retry',
- ): Record<string, unknown> {
- return {
- assistant_stop_reason: assistant.stopReason,
- assistant_error_message: assistant.errorMessage,
- assistant_response_id: assistant.responseId,
- assistant_response_model: assistant.responseModel,
- assistant_content_types: assistant.contentTypes,
- assistant_diagnostics: assistant.diagnostics,
- candidate_retry_pending: attempt === 'retry',
- signal_aborted: assistant.signalAborted,
- };
- }
- function findUnescapedXmlEntity(text: string): { line: number; column: number } | undefined {
- const match = /&(?!amp;|lt;|gt;|quot;|apos;|#\d+;|#x[\da-f]+;)/iu.exec(text);
- if (!match || match.index < 0) return undefined;
- const before = text.slice(0, match.index);
- const lines = before.split('\n');
- return {
- line: lines.length,
- column: (lines.at(-1)?.length ?? 0) + 1,
- };
- }
- function parseXmlErrorLocation(message: string): { line: number; column: number } | undefined {
- const match = /(?:^|\s)(\d+):(\d+):\s/u.exec(message);
- if (!match) return undefined;
- const line = Number(match[1]);
- const column = Number(match[2]);
- if (!Number.isSafeInteger(line) || !Number.isSafeInteger(column)) return undefined;
- return { line, column };
- }
- function validationCategory(
- stage: ReviewValidationStage,
- reasonCode: string,
- ): ReviewValidationCategory {
- if (stage === 'finding') {
- return reasonCode === 'finding_schema_invalid' ? 'format' : 'projection';
- }
- return reasonCode === 'correction_application_failed' ? 'projection' : 'format';
- }
- function errorMessage(error: unknown): string {
- return error instanceof Error ? error.message : String(error);
- }
- export function classifyReviewValidationError(
- stage: ReviewValidationStage,
- message: string,
- ): string {
- if (stage === 'candidate') return 'candidate_missing';
- if (/entity/iu.test(message)) return 'xml_invalid_entity';
- if (/too large/iu.test(message)) return 'document_too_large';
- if (/too many nodes/iu.test(message)) return 'document_node_limit';
- if (/deeply nested/iu.test(message)) return 'document_depth_limit';
- if (/doctype/iu.test(message)) return 'xml_doctype_not_allowed';
- if (/processing instruction/iu.test(message)) return 'xml_processing_instruction_not_allowed';
- if (/comment/iu.test(message) && /not allowed/iu.test(message)) return 'xml_comment_not_allowed';
- if (/exactly one root/iu.test(message)) return 'xml_multiple_roots';
- if (/outside review candidate xml root/iu.test(message)) return 'xml_outside_text';
- if (/not closed|unexpected close|unclosed|text data outside of root/iu.test(message)) {
- return 'xml_not_well_formed';
- }
- if (stage === 'finding') {
- if (/outside the local changes/iu.test(message)) return 'target_outside_changes';
- if (/does not overlap a deleted line/iu.test(message)) return 'old_target_not_deleted';
- if (/invalid finding schema/iu.test(message)) return 'finding_schema_invalid';
- return 'finding_validation_failed';
- }
- if (stage === 'correction') return 'correction_invalid';
- return 'document_schema_invalid';
- }
|