"""Deterministic OCR support for actual GUI observations, never agent narrative.""" import json,re,subprocess,tempfile,hashlib from pathlib import Path from PIL import Image def gui_queue(trajectory,assets,expected): events=[];cache_path=assets/'gui-ocr-cache.json';cache=json.loads(cache_path.read_text()) if cache_path.exists() else {} for step in trajectory.get('steps',[]): if step.get('source')!='agent':continue for result in step.get('observation',{}).get('results',[]): for part in result.get('content',[]): if part.get('type')!='image':continue name=Path(part.get('source',{}).get('path','')).name;p=assets/'operator-screenshots'/name if not p.is_file():continue digest=hashlib.sha256(p.read_bytes()).hexdigest() key=name+':'+digest if key in cache:text=cache[key] else: with tempfile.TemporaryDirectory() as temp: image=Image.open(p);image.resize((image.width*2,image.height*2)).save(Path(temp)/'screen.png') r=subprocess.run(['tesseract',str(Path(temp)/'screen.png'),'stdout','--psm','11'],capture_output=True,text=True,timeout=20);text=' '.join(r.stdout.split()).lower() cache[key]=text;cache_path.write_text(json.dumps(cache)) if 'queue' not in text and 'export' not in text:continue names=[n for n in expected if n.lower().replace('.mp4','') in text or n.lower() in text] if len(names)==len(expected):events.append({'step':step['step_id'],'names':names,'ready':any(x in text for x in ['ready','pending','queued']),'done':any(x in text for x in ['completed','done','finished']),'text':text}) return {'batch':any(a['ready'] and b['done'] and a['step']=10 and int(inliers.sum())/len(good)>=.5: found[name]=True;evidence[name]={'screenshot':shot,'source_second':second,'inliers':int(inliers.sum())};break if found[name]:break capture.release() return found,evidence