#!/usr/bin/env python3 """Resolve a clip frame to source timing, scene and optional recorded controls.""" import argparse import json from pathlib import Path import sqlite3 def resolve_source(root, value): if value.startswith('source/'): value = value[len('source/'):] path = (root / value).resolve() path.relative_to(root.resolve()) return path def rows(path): with path.open(encoding='utf-8-sig') as stream: for line in stream: if line.strip(): yield json.loads(line) def inspect(index, source_root, alignment_path, side, position, records=False): alignment = json.loads(alignment_path.read_text()) aligned = alignment[side][position] conn = sqlite3.connect(index.resolve().as_uri() + '?mode=ro', uri=True) conn.row_factory = sqlite3.Row with conn: result = conn.execute('SELECT * FROM frames WHERE frame_id=?', (aligned['frame_id'],)).fetchone() if result is None: raise ValueError(f'Unknown frame_id {aligned["frame_id"]}') frame = dict(result) segment = dict(conn.execute('SELECT * FROM segments WHERE segment_id=?', (frame['segment_id'],)).fetchone()) conn.close() output_dir = resolve_source(source_root, segment['output_directory']) episode_root = output_dir.parents[3] episode_root.relative_to(source_root.resolve()) output = {'side': side, 'clip_frame': position, 'clip_grid_ns': alignment['grid_ns'][position], 'source_alignment': aligned, 'source_frame': frame, 'segment': segment, 'episode_root': str(episode_root), 'scene_path': str(output_dir / frame['scene_index']) if frame.get('scene_index') else None} frame_path = output_dir / 'frames.jsonl' details = next((r for r in rows(frame_path) if r['proxy_frame_index'] == frame['proxy_frame_index']), None) if details is None: raise ValueError('Published per-frame metadata is missing') output['render_frame_metadata'] = details if records: domain = frame['server_clock_domain'] clock_domain = frame['client_clock_domain'] world = next((r for r in rows(episode_root / '.spool/world_state.jsonl') if r.get('sample_seq') == frame['world_capture_tick'] and r.get('server_clock_domain') == domain), None) output['world_record'] = world client_path = episode_root / '.spool/agents' / segment['agent_id'] / 'client.jsonl' matching = [] method = None for record in rows(client_path): if record.get('server_clock_domain') != domain: continue if clock_domain and record.get('client_clock_domain') != clock_domain: continue if details.get('render_frame_id') is not None: matches = record.get('render_frame_id') == details['render_frame_id'] method = 'same_render_frame_id_and_clock_domains' elif frame.get('client_sample_seq') is not None: matches = record.get('client_sample_seq') == frame['client_sample_seq'] method = 'same_client_sample_seq_and_clock_domains' else: matches = record.get('sample_seq_estimate') == details.get('camera_capture_tick') method = 'same_camera_capture_tick_and_clock_domains' if matches: matching.append(record) output['client_match_method'] = method output['client_records'] = matching output['client_match_count'] = len(matching) output['note'] = ('Raw observations only. Interpolated cameras and smoothing may use ' 'adjacent observations; these records alone do not recreate that filter.') return output def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--index', type=Path, required=True) parser.add_argument('--source-root', type=Path, required=True, help='Directory where map and episode metadata archives were extracted') parser.add_argument('--alignment', type=Path, required=True) parser.add_argument('--side', choices=['left', 'right'], default='left') parser.add_argument('--frame', type=int, default=0) parser.add_argument('--records', action='store_true', help='Scan episode raw streams for world state and matching client observations') parser.add_argument('--output', type=Path, help='Write JSON to a new file instead of stdout') args = parser.parse_args() output = inspect(args.index, args.source_root, args.alignment, args.side, args.frame, args.records) text = json.dumps(output, ensure_ascii=False, indent=2) + '\n' if args.output: with args.output.open('x', encoding='utf-8') as stream: stream.write(text) else: print(text, end='') if __name__ == '__main__': main()