"""Replay repaired acceptance on an existing house, never rewriting its run.""" import argparse import json from pathlib import Path import re import sys import time VERSION = Path(__file__).resolve().parents[1] SOURCE = VERSION / 'source' sys.path.insert(0, str(VERSION)) from effective_contract import verified_effective_request from process_control import execute_command from run_chain import base_identity, contained, digest, load, output_diagnostics, save def bind_saved_blend(scene, scene_id, prior, inputs): """Require the original generation's hash for this exact saved scene.""" if not isinstance(scene_id, str) or not scene_id or Path(scene_id).name != scene_id: raise ValueError('Saved scene requires an explicit scene_id basename') blend = contained(scene, scene_id + '.blend') hashes = [] for field in ('input_sha256', 'output_artifacts_sha256'): recorded = prior.get(field, {}) if not isinstance(recorded, dict): raise ValueError('Original ' + field + ' must be a hash mapping') if str(blend) in recorded: sha = recorded[str(blend)] if not isinstance(sha, str) or not re.fullmatch('[0-9a-f]{64}', sha): raise ValueError('Invalid original saved blend SHA256') hashes.append(sha) if not hashes: raise ValueError('Original generation did not bind this saved blend SHA256') if len(set(hashes)) != 1: raise ValueError('Original saved blend SHA256 bindings conflict') inputs[str(blend)] = hashes[0] if not blend.is_file() or digest(blend) != hashes[0]: raise ValueError('Saved blend differs from the original generation SHA256') return blend, hashes[0] def review_saved_house(run, output, *, export_meshes=False): run, output = Path(run).resolve(), Path(output).resolve() if (VERSION not in run.parents or VERSION not in output.parents or run == output or run in output.parents or SOURCE in output.parents): raise ValueError('Use an existing isolated run and a separate new review directory') output.mkdir(parents=True, exist_ok=False) started = time.monotonic() result = dict(schema='rhg.frontier-saved-house-review.v1', passed=False, fresh_generation_executed=False, output_contract_passed=False, physical_qualified=False, whole_task_acceptance=False, gpu_used=False, original_run=str(run), mesh_export_requested=export_meshes) inputs = {} try: prior = load(run / 'chain/result.json') config = load(run / 'chain_config.json') attempt = run / 'chain/attempt_01' frozen = attempt / 'request.json' runtime = load(attempt / 'runtime.json') manifest_path = (run / config['manifest']).resolve() generated_path = contained(attempt, config['generation']['manifest_output']) old_implementations = {str(VERSION / name) for name in prior['implementation_sha256'] if (VERSION / name).parent == VERSION} inputs = {path: sha for path, sha in prior['input_sha256'].items() if path not in old_implementations} if not all(Path(path).is_file() and digest(path) == sha for path, sha in inputs.items()): raise ValueError('Original run inputs changed; cannot replay acceptance') if digest(generated_path) != prior['output_manifest_sha256']: raise ValueError('Original generated manifest changed') for path in (run / 'chain/result.json', run / 'chain_config.json', generated_path, Path(__file__), *VERSION.glob('*.py')): inputs[str(path)] = digest(path) result['base_identity_before'] = base_identity() if not result['base_identity_before']['passed']: raise ValueError('Baseline source identity failed') manifest, request, generated = load(manifest_path), load(frozen), load(generated_path) blend, blend_sha = bind_saved_blend(generated_path.parent, generated.get('scene_id'), prior, inputs) result.update(saved_blend=str(blend), saved_blend_sha256=blend_sha) pools = [Path(path) if Path(path).is_absolute() else run / path for path in config.get('prepared_pools', [])] for key in ('prepared_asset_pool', 'supplemental_prepared_pool'): if runtime.get(key): path = Path(runtime[key]) pools.append(path if path.is_absolute() else SOURCE / path) effective, candidates, audit = verified_effective_request( manifest, request, frozen, generated_path, pools, inputs, runtime) if audit is None: raise ValueError('Saved house has no effective eligibility evidence') save(output / 'effective_request_review.json', audit) diagnostics = output_diagnostics(manifest, generated, effective, candidates, inputs) save(output / 'output_review.json', dict(passed=not diagnostics, diagnostics=diagnostics)) result['output_contract_passed'] = not diagnostics if diagnostics: raise ValueError('Saved output contract still blocked') report = load(generated_path.parent.parent / 'run_report.json') plan = load(generated_path.parent / 'generation_task_plan.json') if (report.get('requested_scope_passed') is not True or plan.get('passed') is not True or plan.get('scope') != 'no_robot_task_requested'): raise ValueError('Original native generation scope did not pass') result.update(entity_count=len(generated['entities']), room_count=len(generated['floor_plan']['rooms']), native_scope_passed=True) if export_meshes: if digest(blend) != blend_sha: raise ValueError('Saved blend changed before mesh export') meshes = output / 'geometry/physical_meshes.json.gz' command = [runtime['blender'], '--background', '--python-exit-code', '1', '--python', str(SOURCE / 'packages/sim2real/geometry/export_meshes.py'), '--', '--scene', str(generated_path.parent), '--out', str(meshes)] receipt = execute_command(command, SOURCE, output / 'mesh_export.log', 1800) save(output / 'mesh_export_command.json', receipt) if receipt['exit_code'] or receipt['timed_out'] or not receipt['cleanup_passed']: raise ValueError('Saved mesh export failed; inspect retained log') result.update(mesh_export_passed=True, physical_meshes=str(meshes), physical_meshes_sha256=digest(meshes)) result.update(passed=True, acceptance_scope='saved_real_house_native_contract_replay_and_optional_mesh_export_only') except Exception as error: result.update(error_type=type(error).__name__, error=str(error)) finally: result['inputs_unchanged'] = all(Path(path).is_file() and digest(path) == sha for path, sha in inputs.items()) result['base_identity_after'] = base_identity() if not result['inputs_unchanged'] or not result['base_identity_after']['passed']: result['passed'] = False if 'mesh_export_passed' in result: result['mesh_export_passed'] = False result['elapsed_s'] = round(time.monotonic() - started, 3) save(output / 'input_sha256.json', inputs) save(output / 'result.json', result) return result def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--run', type=Path, required=True) parser.add_argument('--output', type=Path, required=True) parser.add_argument('--export-meshes', action='store_true') args = parser.parse_args() result = review_saved_house(args.run, args.output, export_meshes=args.export_meshes) print(json.dumps(result), flush=True) return 0 if result['passed'] else 1 if __name__ == '__main__': raise SystemExit(main())