|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Validation logic for conformance test replay mode.""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +from dataclasses import dataclass |
| 20 | +import difflib |
| 21 | +import json |
| 22 | +from typing import Optional |
| 23 | + |
| 24 | +from ...events.event import Event |
| 25 | +from ...sessions.session import Session |
| 26 | + |
| 27 | + |
| 28 | +@dataclass |
| 29 | +class ComparisonResult: |
| 30 | + """Result of comparing two objects during conformance testing.""" |
| 31 | + |
| 32 | + success: bool |
| 33 | + error_message: Optional[str] = None |
| 34 | + |
| 35 | + |
| 36 | +def _generate_mismatch_message( |
| 37 | + context: str, actual_value: str, recorded_value: str |
| 38 | +) -> str: |
| 39 | + """Generate a generic mismatch error message.""" |
| 40 | + return ( |
| 41 | + f"{context} mismatch - \nActual: \n{actual_value} \nRecorded:" |
| 42 | + f" \n{recorded_value}" |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +def _generate_diff_message( |
| 47 | + context: str, actual_dict: dict, recorded_dict: dict |
| 48 | +) -> str: |
| 49 | + """Generate a diff-based error message for comparison failures.""" |
| 50 | + # Convert to pretty-printed JSON for better readability |
| 51 | + actual_json = json.dumps(actual_dict, indent=2, sort_keys=True) |
| 52 | + recorded_json = json.dumps(recorded_dict, indent=2, sort_keys=True) |
| 53 | + |
| 54 | + # Generate unified diff |
| 55 | + diff_lines = list( |
| 56 | + difflib.unified_diff( |
| 57 | + recorded_json.splitlines(keepends=True), |
| 58 | + actual_json.splitlines(keepends=True), |
| 59 | + fromfile=f"recorded {context}\n", |
| 60 | + tofile=f"actual {context}\n", |
| 61 | + lineterm="", |
| 62 | + ) |
| 63 | + ) |
| 64 | + |
| 65 | + if diff_lines: |
| 66 | + return f"{context} mismatch:\n" + "".join(diff_lines) |
| 67 | + else: |
| 68 | + # Fallback to generic format if diff doesn't work |
| 69 | + return _generate_mismatch_message(context, actual_json, recorded_json) |
| 70 | + |
| 71 | + |
| 72 | +def compare_event( |
| 73 | + actual_event: Event, recorded_event: Event, index: int |
| 74 | +) -> ComparisonResult: |
| 75 | + """Compare a single actual event with a recorded event.""" |
| 76 | + # Comprehensive exclude dict for all fields that can differ between runs |
| 77 | + excluded_fields = { |
| 78 | + # Event-level fields that vary per run |
| 79 | + "id": True, |
| 80 | + "timestamp": True, |
| 81 | + "invocation_id": True, |
| 82 | + "long_running_tool_ids": True, |
| 83 | + # Content fields that vary per run |
| 84 | + "content": { |
| 85 | + "parts": { |
| 86 | + "__all__": { |
| 87 | + "thought_signature": True, |
| 88 | + "function_call": {"id": True}, |
| 89 | + "function_response": {"id": True}, |
| 90 | + } |
| 91 | + } |
| 92 | + }, |
| 93 | + # Action fields that vary per run |
| 94 | + "actions": { |
| 95 | + "state_delta": { |
| 96 | + "_adk_recordings_config": True, |
| 97 | + "_adk_replay_config": True, |
| 98 | + }, |
| 99 | + "requested_auth_configs": True, |
| 100 | + "requested_tool_confirmations": True, |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + # Compare events using model dumps with comprehensive exclude dict |
| 105 | + actual_dict = actual_event.model_dump( |
| 106 | + exclude_none=True, exclude=excluded_fields |
| 107 | + ) |
| 108 | + recorded_dict = recorded_event.model_dump( |
| 109 | + exclude_none=True, exclude=excluded_fields |
| 110 | + ) |
| 111 | + |
| 112 | + if actual_dict != recorded_dict: |
| 113 | + return ComparisonResult( |
| 114 | + success=False, |
| 115 | + error_message=_generate_diff_message( |
| 116 | + f"event {index}", actual_dict, recorded_dict |
| 117 | + ), |
| 118 | + ) |
| 119 | + |
| 120 | + return ComparisonResult(success=True) |
| 121 | + |
| 122 | + |
| 123 | +def compare_events( |
| 124 | + actual_events: list[Event], recorded_events: list[Event] |
| 125 | +) -> ComparisonResult: |
| 126 | + """Compare actual events with recorded events.""" |
| 127 | + if len(actual_events) != len(recorded_events): |
| 128 | + return ComparisonResult( |
| 129 | + success=False, |
| 130 | + error_message=_generate_mismatch_message( |
| 131 | + "Event count", str(len(actual_events)), str(len(recorded_events)) |
| 132 | + ), |
| 133 | + ) |
| 134 | + |
| 135 | + for i, (actual, recorded) in enumerate(zip(actual_events, recorded_events)): |
| 136 | + result = compare_event(actual, recorded, i) |
| 137 | + if not result.success: |
| 138 | + return result |
| 139 | + |
| 140 | + return ComparisonResult(success=True) |
| 141 | + |
| 142 | + |
| 143 | +def compare_session( |
| 144 | + actual_session: Session, recorded_session: Session |
| 145 | +) -> ComparisonResult: |
| 146 | + """Compare actual session with recorded session using comprehensive exclude list. |
| 147 | +
|
| 148 | + Returns: |
| 149 | + ComparisonResult with success status and optional error message |
| 150 | + """ |
| 151 | + # Comprehensive exclude dict for all fields that can differ between runs |
| 152 | + excluded_fields = { |
| 153 | + # Session-level fields that vary per run |
| 154 | + "id": True, |
| 155 | + "last_update_time": True, |
| 156 | + # State fields that contain ADK internal configuration |
| 157 | + "state": { |
| 158 | + "_adk_recordings_config": True, |
| 159 | + "_adk_replay_config": True, |
| 160 | + }, |
| 161 | + # Events comparison handled separately |
| 162 | + "events": True, |
| 163 | + } |
| 164 | + |
| 165 | + # Compare sessions using model dumps with comprehensive exclude dict |
| 166 | + actual_dict = actual_session.model_dump( |
| 167 | + exclude_none=True, exclude=excluded_fields |
| 168 | + ) |
| 169 | + recorded_dict = recorded_session.model_dump( |
| 170 | + exclude_none=True, exclude=excluded_fields |
| 171 | + ) |
| 172 | + |
| 173 | + if actual_dict != recorded_dict: |
| 174 | + return ComparisonResult( |
| 175 | + success=False, |
| 176 | + error_message=_generate_diff_message( |
| 177 | + "session", actual_dict, recorded_dict |
| 178 | + ), |
| 179 | + ) |
| 180 | + |
| 181 | + return ComparisonResult(success=True) |
0 commit comments