"""The existing agent may explain measurements, never replace values or invent grades.""" from copy import deepcopy import json import pytest pytest.importorskip('ai_coach') from ai_coach.config import Settings from ai_coach.service import CoachService from baseball_batting.sam_batting_contract import MeasurementGroup,SamMeasurement,SwingMeasurements from baseball_batting.swing_clips import Clip def groups(): return [MeasurementGroup(key='timing',label='Movement timing',annotated_video=Clip(status='unavailable',reason_code='fixture'), swings=[SwingMeasurements(rep=1,window_source='reviewed',start_frame=0,end_frame=30,baseline_frame=0, forward_rotation_sign=1,lead_foot='left',quality_flags=[],measurements=[ SamMeasurement(key='hip_to_shoulder_ms',label='Hip-to-shoulder peak delay',value=-200,unit='ms', reference='signed_capture_time_difference',statistic='second_peak_minus_first_peak',status='estimated', formula='1000 × (shoulder_peak_time - hip_peak_time)'), SamMeasurement(key='hand_speed_relative_m_s',label='Hand speed relative to hips',value=2,unit='m/s', reference='wrist_midpoint_relative_to_hip_midpoint',statistic='maximum',status='estimated',formula='length(dW/dt)'), SamMeasurement(key='center_of_mass_displacement_cm',label='Center of mass',value=None,unit='cm', reference='ground',statistic='maximum',status='unavailable',reason_code='missing_mass_model',formula='mass-weighted segments')])])] def test_rules_review_each_metric_and_preserve_negative_peak_meaning(tmp_path): service=CoachService(Settings(_env_file=None,google_api_key=None,cache_path=tmp_path/'cache.sqlite',log_dir=tmp_path/'logs')) data=groups();service.review_measurements(data) rows=data[0].swings[0].measurements assert all(row.coaching for row in rows) assert rows[0].value==-200 and 'before' in rows[0].coaching.review assert rows[2].coaching.assessment=='unavailable' and 'mass model' in rows[2].coaching.review assert rows[0].coaching.source=='rules' def test_agent_numeric_claim_is_removed_per_metric_and_cache_prevents_repeat_call(tmp_path,monkeypatch): from google import genai from google.genai import types from pydantic import SecretStr calls=[] class Client: def __init__(self,**kwargs):self.models=self def __enter__(self):return self def __exit__(self,*a):pass def generate_content(self,**kwargs): calls.append(kwargs) return types.GenerateContentResponse(candidates=[types.Candidate(content=types.Content(parts=[types.Part.from_text(text=json.dumps({'reviews':[ {'id':'timing:1:hip_to_shoulder_ms','review':'The shoulders reach their rotation speed peak before the hips.'}, {'id':'timing:1:hand_speed_relative_m_s','review':'Your bat speed was 90 mph and is excellent.'}]}))]))], usage_metadata=types.GenerateContentResponseUsageMetadata(prompt_token_count=50,candidates_token_count=30)) monkeypatch.setattr(genai,'Client',Client) settings=Settings(_env_file=None,google_api_key=SecretStr('fixture-key-not-a-credential'),cache_path=tmp_path/'cache.sqlite',log_dir=tmp_path/'logs') service=CoachService(settings) data=groups();service.review_measurements(data) rows=data[0].swings[0].measurements assert rows[0].coaching.source=='coach_agent' and rows[0].value==-200 assert rows[1].coaching.source=='rules' and rows[1].value==2 assert '90' not in rows[1].coaching.review service.review_measurements(groups()) assert len(calls)==1 assert 'image/jpeg' not in str(calls[0])