"""Independent validation of append-only live caption events and host receipts.""" import hashlib,struct,math from pathlib import Path from probe_native_stream import token_table,token_oracle from score_stream_latency import score def require(value,message): if not value:raise ValueError(message) def validate(rows,observations,case,tokenizer,*,frozen_segments=None,expected_ids=None): require(len(rows)==len(observations) and len(rows)>=2,'observer event coverage') require(rows[0]['kind']=='caption_stream_ready' and rows[-1]['kind']=='caption_stream_complete','caption envelope') allowed={'caption_stream_ready','caption_segment_start','caption_window','caption_text_delta','caption_segment_end','caption_stream_complete'} require(all(r['kind'] in allowed for r in rows),'unknown caption event') require(sum(r['kind']=='caption_stream_ready' for r in rows)==sum(r['kind']=='caption_stream_complete' for r in rows)==1,'duplicate envelope') final=rows[-1];require(final['status']=='complete' and final['audio_samples']==case['samples'] and final['input_sha256']==case['sha256'],'input identity/completion') table=token_table(tokenizer);allids=[];local=[];segments=[];active=None;previous_end=0;oracle=[];actual=[];deltas=[];last_time=-1 for i,(r,o) in enumerate(zip(rows,observations)): require(o['event_index']==i and o['kind']==r['kind'] and o.get('index')==r.get('index'),'host observation mapping') at=o['received_seconds_since_feeder_start'] if r['kind']=='caption_stream_ready':continue require(type(at) in (int,float) and math.isfinite(at) and at>=max(0,last_time),'receipt clock') last_time=at if r['kind']=='caption_segment_start': require(active is None and r['segment']==len(segments),'segment ordering') active=dict(id=r['segment'],start_sample=r['start_sample'],decision_sample=r['decision_sample']) require(previous_end<=active['start_sample']<=active['decision_sample'] and active['start_sample']==max(previous_end,active['decision_sample']-6400) and active['decision_sample']%512==0,'activation/preroll') local=[] elif r['kind']=='caption_window': require(active is not None and r['segment']==active['id'],'window outside active session') require(r['index']==len(allids) and r['segment_index']==len(local),'global/local clock') token=r['token_id'];require(type(token) is int and 0<=token