Files
NODEDC_MISSION_CORE/simulation/ai-polygon/gaussian_cache.py
T
DCCONSTRUCTIONS d0e5f90952 Preserve installed operator workspace before consolidation
Local preservation snapshot, not a new deployment or simulation acceptance.
2026-10-08 09:59:51 +03:00

129 lines
5.2 KiB
Python

"""Verified Gaussian conversion cache, independent of Isaac imports.
The caller supplies the real reader/writer and runtime identities. A code
bundle alone is not evidence of which cached USD the renderer will load.
"""
import hashlib
import json
import uuid
from pathlib import Path
SCHEMA = "missioncore.gaussian-usd-cache/v1"
def file_sha256(path):
digest = hashlib.sha256()
with Path(path).open("rb") as stream:
while chunk := stream.read(1024 * 1024):
digest.update(chunk)
return digest.hexdigest()
def _json_bytes(value):
return json.dumps(value, sort_keys=True, separators=(",", ":"), allow_nan=False).encode()
def _identity(source, recipe):
if not isinstance(recipe, dict) or set(recipe) != {"reader", "writer", "runtime", "output_schema"}:
raise ValueError("Gaussian recipe requires reader, writer, runtime and output_schema identities")
if any(not recipe[key] for key in recipe):
raise ValueError("Gaussian conversion identities must not be empty")
# Round-trip creates an immutable JSON snapshot of caller-owned dictionaries.
recipe = json.loads(_json_bytes(recipe))
before = source.stat()
source_sha256 = file_sha256(source)
after = source.stat()
if (before.st_size, before.st_mtime_ns) != (after.st_size, after.st_mtime_ns):
raise RuntimeError("Gaussian source changed while its identity was read")
return {
"source_sha256": source_sha256,
"source_bytes": after.st_size,
"cache_adapter_sha256": file_sha256(__file__),
"recipe": recipe,
}
def _receipt(path, identity, key, output):
try:
value = json.loads(path.read_text(encoding="utf-8"))
if (
not isinstance(value, dict)
or value.get("schema_version") != SCHEMA
or value.get("cache_key") != key
or value.get("identity") != identity
or value.get("usd_file") != output.name
or value.get("usd_bytes", 0) <= 0
or not output.is_file()
or output.stat().st_size != value["usd_bytes"]
or file_sha256(output) != value.get("usd_sha256")
):
return None
return value
except (OSError, ValueError, TypeError):
return None
def prepare_gaussian(source, convert, recipe, *, cache_root=None):
"""Return the verified USD path and the receipt for the loaded artifact.
``convert(source_path, temporary_usd_path)`` owns the real conversion and
format checks. It must finish writing before returning. This helper proves
byte identity/reuse, not renderer or optical equivalence. Worker admission
serializes scene preparation; this is not a multi-writer cache service.
"""
source = Path(source)
identity = _identity(source, recipe)
key = hashlib.sha256(_json_bytes(identity)).hexdigest()
root = Path(cache_root) if cache_root is not None else source.parent / "gaussian-usd-cache"
directory = root / key
output, receipt_path = directory / "gaussians.usd", directory / "receipt.json"
valid = _receipt(receipt_path, identity, key, output)
if valid is not None:
return output, valid
directory.mkdir(parents=True, exist_ok=True)
token = uuid.uuid4().hex
temporary = directory / (token + ".part.usd")
receipt_temporary = directory / (token + ".part.json")
try:
convert(source, temporary)
if not temporary.is_file() or temporary.stat().st_size == 0:
raise ValueError("Gaussian conversion did not produce a nonempty USD")
if source.stat().st_size != identity["source_bytes"] or file_sha256(source) != identity["source_sha256"]:
raise RuntimeError("Gaussian source changed during conversion")
value = {
"schema_version": SCHEMA,
"cache_key": key,
"identity": identity,
"usd_file": output.name,
"usd_bytes": temporary.stat().st_size,
"usd_sha256": file_sha256(temporary),
}
receipt_temporary.write_bytes(_json_bytes(value) + b"\n")
temporary.replace(output)
receipt_temporary.replace(receipt_path)
return output, value
finally:
temporary.unlink(missing_ok=True)
receipt_temporary.unlink(missing_ok=True)
def verify_loaded_gaussian(source, receipt, *, cache_root=None):
"""Verify the episode's loaded artifact, never the legacy sibling USD.
Verification is read-only: an invalid or missing artifact cannot be repaired
after the episode and then presented as the bytes it actually loaded.
"""
source = Path(source)
if not isinstance(receipt, dict) or not isinstance(receipt.get("identity"), dict):
raise ValueError("Missing loaded Gaussian identity")
identity = _identity(source, receipt["identity"].get("recipe"))
key = hashlib.sha256(_json_bytes(identity)).hexdigest()
root = Path(cache_root) if cache_root is not None else source.parent / "gaussian-usd-cache"
output = root / key / "gaussians.usd"
valid = _receipt(output.parent / "receipt.json", identity, key, output)
if valid is None or valid != receipt:
raise ValueError("Loaded Gaussian evidence differs from source, recipe or actual USD bytes")
return output