def stream_td_materialization(instance_path: Path, family_root: Path, selected_arcs=(), *,
atf_cache_root: Path | None = None, block_arcs: int = 8192) -> TDMaterialization:
"""Check the published ATF pin and hash every directed arc before returning sparse route operands.
A cache holds generated Blauth ATFs under the sidecar path from the descriptor. Both a
cached and an in-tree ATF must reproduce the descriptor's full canonical SHA-256.
"""
if isinstance(block_arcs, bool) or not isinstance(block_arcs, int) or block_arcs < 1:
raise ValueError("block_arcs must be a positive integer")
instance = load_td_benchmark_instance(instance_path)
td = instance.td
selected = None if selected_arcs is None else set(selected_arcs)
operands = InstanceATFs(instance.instance_name, instance.benchmark_name.value, tuple(instance.horizon),
instance.num_customers, {})
canonical = sha256()
if td.model == "atf-ndcpwlf":
path = family_root / validate_relative_sidecar_path(td.atf_path)
stored = path.is_file()
if not stored and atf_cache_root is not None:
path = atf_cache_root / validate_relative_sidecar_path(td.atf_path)
try:
with (gzip_open if path.suffix == ".gz" else open)(path, "rb") as stream:
records = _stored_records(stream, canonical, instance, operands)
digest, count, knots = _digest_arcs(operands, records, selected)
raw_sha = canonical.hexdigest()
if raw_sha != td.atf_sha256:
raise ValueError("the input bytes require canonical serialization")
canonical_sha = raw_sha
except (ValueError, KeyError, TypeError):
# Ordinary ATF JSON may reorder object members or use different whitespace.
# Only the published canonical byte layout admits the line-wise fast path.
full = load_instance_atfs(path)
if (full.instance_name != instance.instance_name or full.benchmark_name != instance.benchmark_name.value
or full.num_customers != instance.num_customers or tuple(full.horizon) != tuple(instance.horizon)):
raise ValueError("stored ATF identity or horizon differs from descriptor")
operands.generator = full.generator
operands.arcs.clear()
rows = ((i, j, arc.xs, arc.ys) for (i, j), arc in sorted(full.arcs.items()))
digest, count, knots = _digest_arcs(operands, rows, selected)
canonical_sha = compute_atf_sha256(full)
with (gzip_open if path.suffix == ".gz" else open)(path, "rb") as stream:
raw_sha = file_digest(stream, "sha256").hexdigest()
sidecars = ([{"path": td.atf_path, "sha256": raw_sha,
"scope": "decompressed" if path.suffix == ".gz" else "file"}] if stored else [])
else:
if td.model == "igp-profile":
cats = load_instance_categories(family_root / validate_relative_sidecar_path(td.categories_path))
_match(compute_categories_sha256(cats), td.categories_sha256, td.categories_path)
if cats.benchmark_name != instance.benchmark_name.value or cats.num_categories != td.num_categories():
raise ValueError("category table does not describe this TD instance")
factory = partial(materialize_selected_atfs_igp, instance, cats)
operands.generator = dict(IGP_MATERIALIZER_GENERATOR)
names = [td.categories_path]
elif td.model == "road-graph":
road = load_instance_road_graph(family_root / validate_relative_sidecar_path(td.graph.path))
overlay = load_traffic_overlay(family_root / validate_relative_sidecar_path(td.traffic.path))
_match(compute_road_graph_sha256(road), td.graph.sha256, td.graph.path)
_match(compute_traffic_overlay_sha256(overlay), td.traffic.sha256, td.traffic.path)
validate_overlay_against_road(overlay, road)
if road.benchmark_name != instance.benchmark_name.value:
raise ValueError("road graph does not describe this family")
if (tuple(road.horizon) != tuple(instance.horizon) or road.sample_step != td.sample_step
or road.simplify_tolerance != td.simplify_tolerance):
raise ValueError("graph and descriptor materialization parameters differ")
factory = partial(materialize_selected_atfs_roadgraph, instance, road, overlay)
operands.generator = dict(ROAD_MATERIALIZER_GENERATOR)
names = [td.graph.path, td.traffic.path]
else:
raise ValueError(f"unsupported TD model {td.model}")
sidecars = [_sidecar_record(family_root, name) for name in names]
records = _generated_records(operands, factory, canonical, block_arcs)
digest, count, knots = _digest_arcs(operands, records, selected)
canonical_sha = canonical.hexdigest()
_match(canonical_sha, td.atf_sha256, str(instance_path))
return TDMaterialization(operands, canonical_sha, digest, sidecars, count, knots)