"""Read-only admission of previously prepared scenes for bounded qualification. Raw PLY identity and a Gaussian Pipeline package identity are different contracts. Reuse the product adapter's LOD0 receipt; never reinterpret a package SHA as a PLY SHA or reconstruct a different scene for a test. """ import hashlib import json import math import re from pathlib import Path def sha256(path): with Path(path).open("rb") as stream: return hashlib.file_digest(stream, "sha256").hexdigest() def cached_scene(runtime_root, run): from k1link.simulation.ai_polygon.terrain_contract import terrain_matches world = run["world"] identity = world["sha256"] if not re.fullmatch(r"[a-f0-9]{64}", identity): raise ValueError("Invalid world identity") terrain_path = Path(run["terrain_manifest"]) terrain = json.loads(terrain_path.read_text()) if ( not terrain_matches(terrain, world) or sha256(terrain["collider"]) != terrain["collider_sha256"] ): raise ValueError("Cached terrain identity or calibration changed") proof = {"world_sha256": identity, "terrain_manifest_sha256": sha256(terrain_path)} if world.get("storage", {}).get("kind") != "project": if world.get("collider_sha256") != terrain["collider_sha256"]: raise ValueError("Cached collider differs from the admitted world") source = Path(runtime_root) / "state/worlds" / (identity + ".ply") if sha256(source) != identity: raise ValueError("Cached Gaussian identity changed") proof["source_contract"] = "original-gaussian-ply" return source, terrain, proof root = Path(runtime_root) / "state/project-packages" / identity settings = world["settings"] # This is the existing project's SOG->PLY convention, not a inferred map # transform. A different adaptation convention needs its own receipt. if settings["meters_per_unit"] != 1 or settings["rotation_degrees"] != [90, 0, 0]: raise ValueError("Unsupported prepared-project coordinate convention") source_collider = world["collider_sha256"] if terrain.get("source_collider_sha256") != source_collider: raise ValueError("Project collider source differs from the admitted world") decoded_receipt = Path(terrain["collider"]).with_suffix(".json") decoded = json.loads(decoded_receipt.read_text()) if ( decoded.get("source_sha256") != source_collider or decoded.get("sha256") != terrain["collider_sha256"] or decoded.get("decoder_sha256") != sha256(Path(__file__).with_name("decode_collision.cjs")) ): raise ValueError("Unverified project collider adaptation") # Eight metres is the mounted rig's maximum laboratory range. The full # route plus that context must already exist in the prepared LOD0 region. xy = [settings["spawn_xy"], *settings["route_xy"]] points = [[x, settings["ground_z"], -y] for x, y in xy] required = [min(p[i] for p in points) - 8 for i in range(3)] + [ max(p[i] for p in points) + 8 for i in range(3) ] candidates = [] for receipt in sorted(root.glob("*.json")): if not re.fullmatch(r"[a-f0-9]{64}", receipt.stem): continue value = json.loads(receipt.read_text()) bounds = value.get("bounds_y_up", []) if ( value.get("source_sha256") != identity or value.get("lod") != 0 or value.get("decimated") is not False or value.get("converter") != "splat-transform-3.4.0" or type(value.get("gaussian_count")) is not int or value["gaussian_count"] < 1 or len(bounds) != 6 or not all(type(v) in (int, float) and math.isfinite(v) for v in bounds) or not all( bounds[i] <= required[i] and bounds[i + 3] >= required[i + 3] for i in range(3) ) ): continue source = receipt.with_suffix(".ply") if source.is_file() and sha256(source) == value.get("sha256"): candidates.append((value["gaussian_count"], str(source), receipt)) if not candidates: raise ValueError("No verified prepared LOD0 scene covers this route and sensor range") _, source, receipt = min(candidates) proof.update( source_contract="prepared-project-lod0", adaptation_receipt_sha256=sha256(receipt), collider_receipt_sha256=sha256(decoded_receipt), required_bounds_y_up=required, ) return Path(source), terrain, proof