"""Verify the pinned PR7 diagnostic patch, its counts and its observation-only hook.""" from hashlib import sha256 from pathlib import Path from types import SimpleNamespace from unittest.mock import Mock import ast import json import subprocess import sys import tempfile from prepare_pr7_diagnostics import BASE_SHA, OUT, SOURCE def function(tree, name): return next(x for x in tree.body if isinstance(x, ast.FunctionDef) and x.name == name) def dump(node): return ast.dump(node, include_attributes=False) def main(): pins = json.loads((OUT / "pr7_zoom_block_reason_pins.json").read_bytes()) patch = str(OUT / "pr7_zoom_block_reason.patch") with tempfile.TemporaryDirectory(dir=OUT) as directory: source = Path(directory)/"postprocess.py" original = Path(sys.argv[1]) if len(sys.argv) > 1 else SOURCE base = original.read_bytes() assert sha256(base).hexdigest()==BASE_SHA source.write_bytes(base) git = ["git","-c","core.autocrlf=false","apply"] subprocess.run([*git,"--whitespace=error",patch],cwd=directory,check=True) after=source.read_bytes() assert sha256(after).hexdigest()==pins["after"] subprocess.run([*git,"-R",patch],cwd=directory,check=True) assert source.read_bytes()==base old, new = ast.parse(base), ast.parse(after) proc = function(new,"postprocess_control") control = function(old,"postprocess_control") anchor = next(node for node in ast.walk(proc) if isinstance(node,ast.If) and "ZOOM_BLOCK_DIAG_ENABLE" in dump(node.test)) # Removing the optional hook and its sole helper restores the entire AST. for parent in ast.walk(proc): for field,value in ast.iter_fields(parent): if isinstance(value,list) and anchor in value: value.remove(anchor) new.body.remove(function(new,"_log_zoom_block_tick")) assert dump(proc)==dump(control) and dump(old)==dump(new) helper=function(ast.parse(after),"_log_zoom_block_tick") log=Mock() env={"json":json,"logger":log} exec(compile(ast.Module(body=[helper],type_ignores=[]),"","exec"),env) report=env[helper.name] st={"ptz_generation":4,"track_started_at":100.0} state={ "config":SimpleNamespace(ZOOM_BLOCK_DIAG_ENABLE=False), "cam_idx":8, "st":st, "source_seq":1,"zoom_cmd":0.,"final_guard_active":False,"near_edge":False, "edge_recovery_active":False,"visual_control_stale":False,"visual_control_coast":False, "zoom_rearm_ready":True,"raw_zoom_needed":True,"normal_size_hold":False, "current_track_source":"visual","visual_zoom_validation_age":0.2,"zoom_quality_ok":True, "followup_ready":True,"zoom_safe":True,"zoom_angular_safe":True, "center_locked_for_zoom":False,"track_stable":False,"now":101., "zoom_zero_reason":"size_controller","_log_zoom_block_tick":report, } call=compile(ast.Module(body=[anchor],type_ignores=[]),"","exec") exec(call,state) assert "zoom_block_diag" not in st state["config"].ZOOM_BLOCK_DIAG_ENABLE=True st["visual_tracker_score"]=.8 exec(call,state) assert st["zoom_block_diag"]["before_first"]=={"not_centered":1} state.update(zoom_cmd=.10) exec(call,state) assert st["zoom_block_diag"]["first_positive"] state.update(zoom_cmd=0.,zoom_zero_reason="final_brake",final_guard_active=True) exec(call,state) assert st["zoom_block_diag"]["total"]=={"not_centered":1,"final_brake":1} assert st["zoom_block_diag"]["before_first"]=={"not_centered":1} st.update(ptz_generation=5,track_started_at=103.0) exec(call,state) assert st["zoom_block_diag"]["before_first"]=={"final_brake":1} assert log.info.call_count==3 print("Round-trip exact, control AST unchanged outside the hook, toggle/phase/owner/count checks passed.") if __name__=="__main__": main()