#!/usr/bin/env python3 """实际已到达训练前缀与公开QA输入核验;不把局部就绪说成正式20k冻结。""" import collections import hashlib import json import math import os from pathlib import Path import sys import time ROOT=Path(__file__).resolve().parents[1] sys.path.insert(0,str(ROOT/'scripts')) from download_assets import atomic,sha def main(): started=time.perf_counter() video_path=ROOT/'manifests/training_videos.candidate.json' inventory_path=ROOT/'provenance/training_media_inventory.json' inventory=json.loads(inventory_path.read_text());videos=json.loads(video_path.read_text()) reports=[];available={};errors=[];pending=[] for video,meta in videos.items(): marker=ROOT/'provenance'/('training_media_extracted_'+video.split('/')[0]+'.json') if not marker.exists():pending.append(video);continue extraction=json.loads(marker.read_text()) if extraction.get('missing') or extraction['found_videos']!=extraction['selected_videos']: errors.append(dict(source_video=video,error='source_extraction_incomplete'));continue directory=ROOT/'data/training_frames'/video if video not in inventory or not directory.is_dir(): errors.append(dict(source_video=video,error='completed_source_missing_actual_video'));continue indices=set();count=0;storage=0;extensions=collections.Counter() with os.scandir(directory) as entries: for entry in entries: path=Path(entry.name) if path.suffix.lower() not in ('.jpg','.jpeg','.png'):continue if not path.stem.isdigit(): errors.append(dict(source_video=video,error='non_numeric_frame_name',file=entry.name));continue index=int(path.stem);info=entry.stat();count+=1;storage+=info.st_size extensions[path.suffix]+=1;indices.add(index) if info.st_size==0:errors.append(dict(source_video=video,error='empty_frame',file=entry.name)) if index>meta['max_query_time']+1e-6: errors.append(dict(source_video=video,error='frame_after_selected_prefix',file=entry.name)) if not indices: errors.append(dict(source_video=video,error='empty_actual_prefix'));continue missing=sorted(set(range(max(indices)+1))-indices) if missing:errors.append(dict(source_video=video,error='noncontiguous_zero_based_prefix',missing=missing)) if count!=len(indices):errors.append(dict(source_video=video,error='duplicate_native_frame_index')) if set(extensions)!={'.jpg'}: errors.append(dict(source_video=video,error='extension_not_consumed_by_current_causal_cache',extensions=dict(extensions))) if count!=inventory[video]['files'] or storage!=inventory[video]['storage_bytes']: errors.append(dict(source_video=video,error='actual_inventory_mismatch',actual_count=count, declared_count=inventory[video]['files'],actual_bytes=storage,declared_bytes=inventory[video]['storage_bytes'])) available[video]=indices reports.append(dict(source_video=video,frames=count,bytes=storage,min_index=min(indices),max_index=max(indices), max_query_time=meta['max_query_time'],contiguous_from_zero=not missing, actual_last_frame_before_prefix_clock=max(indices)row['query_time']+1e-6: raw_failures.append(dict(id=row['id'],error='provided_input_after_derived_query_time')) counts['checked']+=1;counts['all_supplied_frames_present']+=not missing retimed[split]+=row['supplied_max_frame_index']>row['annotation_end'] qa_counts[split]=dict(counts) complete=not pending and len(available)==len(videos) result=dict(status='full_media_prefix_audit' if complete else 'partial_available_sources_only', passed_on_available_sources=not(errors or qa_missing or raw_failures),full_required_media_ready=complete, source_videos_checked=len(available),expected_source_videos=len(videos),pending_source_videos=pending, frames=sum(r['frames'] for r in reports),storage_bytes=sum(r['bytes'] for r in reports), source_reports=reports,media_errors=errors,QA_supplied_frame_failures=qa_missing,QA_raw_or_time_failures=raw_failures, QA_counts=qa_counts,checked_QA_retimed_to_supplied_endpoint=dict(retimed), videos_with_end_before_requested_clock=sum(r['actual_last_frame_before_prefix_clock'] for r in reports), candidate_video_manifest_sha256=sha(video_path),inventory_sha256=sha(inventory_path), candidate_QA_manifest_sha256={split:sha(ROOT/'manifests'/name) for split,name in [('train','online_pilot20k.candidate.jsonl'),('dev','online_dev.candidate.jsonl')]}, wall_seconds=time.perf_counter()-started, boundary='index_file_size_and_exact_raw_QA_reference_audit; RGB_decode_content_overlap_frozen_perception_Writer_and_final20k_freeze_separate') target=ROOT/'provenance/training_media_prefix_audit.json' previous=json.loads(target.read_text()) if target.exists() else None stable=lambda row:{key:value for key,value in row.items() if key!='wall_seconds'} if previous is not None and stable(previous)==stable(result):result=previous else:atomic(target,result) with (ROOT/'logs/training_media_prefix_checks.jsonl').open('a') as log: log.write(json.dumps(dict(timestamp=time.time(),wall_seconds=time.perf_counter()-started, sources=len(available),frames=result['frames'],passed_on_available=result['passed_on_available_sources'], full_media_ready=complete,proof_sha256=sha(target)))+'\n') print(json.dumps({key:value for key,value in result.items() if key not in ('source_reports','pending_source_videos','media_errors','QA_supplied_frame_failures','QA_raw_or_time_failures')},indent=2),flush=True) if errors or qa_missing or raw_failures:raise SystemExit(1) if __name__=='__main__':main()