"""Build a bounded import closure and run one owned Worker006 qualification. Usage: repository .venv/bin/python evaluation/qualify_topdown.py CONFIG.json Config names a fresh session, pinned worker release/definition and development scene observation paths. Optional symbols path replays saved inference inputs. """ from pathlib import Path import ast import base64 import hashlib import json import subprocess import sys import tarfile import uuid repo = Path(__file__).resolve().parents[3] module = repo/'simulation/urban-plan-research' config = json.loads(Path(sys.argv[1]).read_text()) session = config['session'] if not session.startswith('topdown-') or any(c not in 'abcdefghijklmnopqrstuvwxyz0123456789-' for c in session): raise ValueError('Invalid qualification session') root = repo/'.runtime/simulation-qualifications'/session root.mkdir(exist_ok=False) definition = json.loads((module/'pipeline/definition.json').read_text()) aliases = next(g for g in definition['groups'] if g['group'] == 'uniform_arrows')['files'].copy() aliases.update({p.name: p.relative_to(module).as_posix() for p in (module/'topdown').glob('*.py')}) pending = ['verify_topdown.py','reconstruct_topdown.py','prepare_topdown.py','semantic_symbols.py','test_topdown.py','merge_arrow_artwork.json'] if any(s.get('render_native_geometry') or s.get('render_parallel_observations') for s in config['scenes']): pending.append('render_native_geometry.py') if config.get('audit') or any(s.get('audit') for s in config['scenes']): pending.append('audit_topdown.py') if any(s.get('contract_audit') for s in config['scenes']): pending.append('audit_contract.py') if any(s.get('assemble_from') for s in config['scenes']): pending.append('assemble_reviewed_state.py') if any(s.get('conditional_contract_audit') for s in config['scenes']): pending.append('conditional_contract_audit.py') if any(s.get('pose_domain_audit') for s in config['scenes']): pending.append('pose_domain_audit.py') if any(s.get('endpoint_cause_audit') for s in config['scenes']): pending.append('endpoint_cause_audit.py') if any(s.get('depth_audit') for s in config['scenes']): pending.append('depth_audit.py') if any(s.get('feasibility_audit') for s in config['scenes']): pending.append('feasibility_audit.py') if config.get('camera_tests'): pending.append('test_orthographic_colour.py') if config.get('retire_mapper_builds'): aliases['retire_mapper_builds.py']='evaluation/retire_mapper_builds.py' pending.append('retire_mapper_builds.py') if config.get('queue_canary'): aliases.update({'queue_worker_canary.py':'evaluation/queue_worker_canary.py', 'queue_persistence.py':'../../src/k1link/simulation/ai_polygon/worlds.py', 'infrastructure_jobs.py':'../../src/k1link/simulation/ai_polygon/infrastructure_jobs.py', 'infrastructure_progress.py':'../../src/k1link/simulation/ai_polygon/infrastructure_progress.py'}) pending.extend(['queue_worker_canary.py','queue_persistence.py','infrastructure_jobs.py','infrastructure_progress.py']) files = {} while pending: name = pending.pop() if name in files: continue path = module/aliases[name]; data = path.read_bytes() files[name] = dict(source=path.relative_to(module).as_posix(), sha256=hashlib.sha256(data).hexdigest(), content=base64.b64encode(data).decode()) if path.suffix == '.py': for node in ast.walk(ast.parse(data)): imports = ([node.module] if isinstance(node, ast.ImportFrom) and node.module else [a.name for a in node.names] if isinstance(node, ast.Import) else []) for imported in imports: dependency = imported.split('.')[0]+'.py' if dependency in aliases: pending.append(dependency) for spec in config['scenes']: if spec.get('confirmed_reference_path'): data=Path(spec.pop('confirmed_reference_path')).read_bytes() spec['confirmed_reference']=dict(sha256=hashlib.sha256(data).hexdigest(),content=base64.b64encode(data).decode()) config['files'] = files builder_bytes=Path(__file__).read_bytes() worker_bytes=(module/'evaluation/run_topdown_worker.py').read_bytes() (root/'builder.py').write_bytes(builder_bytes);(root/'worker.py').write_bytes(worker_bytes) config['builder_sha256'] = hashlib.sha256(builder_bytes).hexdigest() config['worker_sha256'] = hashlib.sha256(worker_bytes).hexdigest() (root/'source-manifest.json').write_text(json.dumps({k:{n:v[n] for n in ['source','sha256']} for k,v in files.items()}, indent=2)) (root/'config.json').write_text(json.dumps({k:v for k,v in config.items() if k != 'files'}, indent=2)) sys.path.insert(0, str(repo/'src')) from k1link.observatory.recorded_jobs import ObservatoryRecordedJobQueue queue = ObservatoryRecordedJobQueue(repo/'.runtime/mission-core', definitions=None) owner = 'airun-'+uuid.uuid4().hex queue.reserve_simulation(owner) receipt = dict(owner=owner, state='reserved') (root/'reservation.json').write_text(json.dumps(receipt)) ssh = ['ssh', '-T', '-o', 'BatchMode=yes', '-o', 'ConnectTimeout=15', 'mission-gpu', 'D:/NDC_MISSIONCORE/runtime/simulation/isaac-sim-6.1.0/kit/python/python.exe -'] worker = module/'evaluation/run_topdown_worker.py' code = 'import json,base64\nCONFIG=json.loads(base64.b64decode('+repr(base64.b64encode(json.dumps(config).encode()).decode())+'))\n'+worker_bytes.decode() try: p = subprocess.Popen(ssh, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, errors='replace') p.stdin.write(code); p.stdin.close(); info = None with (root/'execution.log').open('w') as log: for line in p.stdout: log.write(line); log.flush(); print(line, end='', flush=True) if line.startswith('COLLECTION '): info = json.loads(line[11:]) if p.wait() != 0 or not info: raise RuntimeError('Worker qualification failed; inspect execution.log') assert info['bytes'] < 100*1024**2 archive = root/'evidence.tgz' subprocess.run(['scp', '-q', 'mission-gpu:'+info['path'], str(archive)], check=True) assert hashlib.sha256(archive.read_bytes()).hexdigest() == info['sha256'] evidence = root/'evidence'; evidence.mkdir() with tarfile.open(archive) as tar: for member in tar.getmembers(): assert member.isfile() and (evidence/member.name).resolve().is_relative_to(evidence) tar.extractall(evidence, filter='data') for name, digest in json.loads((evidence/'collection.json').read_text()).items(): assert hashlib.sha256((evidence/name).read_bytes()).hexdigest() == digest print('COLLECTED', root, flush=True) finally: check = subprocess.run(ssh, input="from pathlib import Path\nprint(Path('C:/NDC_MISSIONCORE_CACHE/urban-mapping/qualifications/"+session+"/active.json').exists())", text=True, capture_output=True, timeout=30) if check.returncode == 0 and check.stdout.strip() == 'False': queue.release_simulation(owner); receipt['state'] = 'released' else: receipt['state'] = 'cleanup-unconfirmed' (root/'reservation.json').write_text(json.dumps(receipt))