122 lines
5.6 KiB
Python
122 lines
5.6 KiB
Python
"""Bind mounted lane definitions to the rebuilt native road graph.
|
|
|
|
The report is an offline artifact. It records the exact graph and mounted
|
|
definition hashes so the runtime can fail closed when either source changes.
|
|
It does not infer a lane change from an empty lane or from geometric order
|
|
alone: asymmetric definitions stay unavailable until the explicit source
|
|
ordinal contract is enabled by the caller.
|
|
"""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
STAGE = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(STAGE))
|
|
from telemetry.native_lane_permissions import LanePermissionResolver
|
|
from telemetry.native_runtime_work import cooperative_json_loads, cooperative_json_write
|
|
|
|
|
|
def _road_groups(graph):
|
|
groups = {}
|
|
for lane in graph.get('lanes', []):
|
|
if lane.get('kind') != 'road':
|
|
continue
|
|
uid = str(lane.get('road_uid'))
|
|
groups.setdefault(uid, []).append(lane)
|
|
return groups
|
|
|
|
|
|
def _road_record(uid, lanes):
|
|
if not lanes:
|
|
raise ValueError('Empty road lane group: ' + uid)
|
|
forward = next((lane for lane in lanes if lane.get('travel_direction') == 1), None)
|
|
sample = forward or next(iter(lanes))
|
|
if forward is not None:
|
|
start, end = forward.get('start_node_uid'), forward.get('end_node_uid')
|
|
else:
|
|
start, end = sample.get('end_node_uid'), sample.get('start_node_uid')
|
|
fields = {
|
|
'uid': uid,
|
|
'roadLookToken': sample.get('road_look_token'),
|
|
'rightTemplateVariant': sample.get('rightTemplateVariant', ''),
|
|
'leftTemplateVariant': sample.get('leftTemplateVariant', ''),
|
|
'rightLaneVariant': sample.get('rightLaneVariant', ''),
|
|
'leftLaneVariant': sample.get('leftLaneVariant', ''),
|
|
'startNodeUid': start,
|
|
'endNodeUid': end,
|
|
'hidden': False,
|
|
}
|
|
if not isinstance(fields['roadLookToken'], str) or not fields['roadLookToken']:
|
|
raise ValueError('Missing road look token: ' + uid)
|
|
if not all(isinstance(fields[key], str) for key in (
|
|
'rightTemplateVariant', 'leftTemplateVariant',
|
|
'rightLaneVariant', 'leftLaneVariant')):
|
|
raise ValueError('Missing explicit variant metadata: ' + uid)
|
|
return fields
|
|
|
|
|
|
def build(graph_path, catalog_path, output, *, allow_source_ordinal_binding=False):
|
|
graph_path, catalog_path, output = map(Path, (graph_path, catalog_path, output))
|
|
graph_bytes = graph_path.read_bytes()
|
|
graph = cooperative_json_loads(graph_bytes)
|
|
catalog_bytes = catalog_path.read_bytes()
|
|
catalog = cooperative_json_loads(catalog_bytes)
|
|
# A matching graph hash alone cannot rescue a catalog extracted from an
|
|
# older mod stack. Bind only definitions from the graph's mounted sources.
|
|
source = graph.get('provenance') or {}
|
|
mounted = catalog.get('provenance') or {}
|
|
archives, definitions = source.get('archives', []), mounted.get('archives', [])
|
|
# The graph may carry an additional full-file digest from an earlier
|
|
# extraction. The catalog uses the same header/index fingerprint; absence
|
|
# of that redundant digest is not a change to the mounted archive.
|
|
same_archives = len(archives) == len(definitions) and all(
|
|
{k:v for k,v in a.items() if k != 'verified_full_sha256'} ==
|
|
{k:v for k,v in b.items() if k != 'verified_full_sha256'}
|
|
and (not a.get('verified_full_sha256') or not b.get('verified_full_sha256')
|
|
or a['verified_full_sha256'] == b['verified_full_sha256'])
|
|
for a,b in zip(archives, definitions))
|
|
if (source.get('loaded_archive_order') != mounted.get('loaded_archive_order') or not same_archives):
|
|
raise ValueError('Lane definition catalog does not match mounted graph sources')
|
|
resolver = LanePermissionResolver(catalog)
|
|
records, failures = {}, {}
|
|
for uid, lanes in _road_groups(graph).items():
|
|
try:
|
|
road = _road_record(uid, lanes)
|
|
records[uid] = resolver.road_permissions(
|
|
road, lanes,
|
|
allow_source_ordinal_binding=allow_source_ordinal_binding)
|
|
except (ValueError, KeyError, TypeError, OverflowError) as exc:
|
|
failures[uid] = str(exc)
|
|
available = [uid for uid, report in records.items() if report.get('available')]
|
|
report = dict(
|
|
schema_version=1,
|
|
available=bool(available),
|
|
source='Mounted ATS lane definitions bound to PPD-anchored native road slots',
|
|
graph_sha256=hashlib.sha256(graph_bytes).hexdigest(),
|
|
catalog_sha256=hashlib.sha256(catalog_bytes).hexdigest(),
|
|
game_build_sha256=graph.get('game_build_sha256'),
|
|
road_permissions=records,
|
|
failures=failures,
|
|
coverage=dict(road_groups=len(records), available_road_groups=len(available),
|
|
lane_groups_failed=len(failures),
|
|
source_ordinal_binding=bool(allow_source_ordinal_binding)),
|
|
)
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
pending = output.with_suffix('.pending.json')
|
|
cooperative_json_write(pending, report)
|
|
pending.replace(output)
|
|
return dict(path=str(output.resolve()), **report['coverage'], available=report['available'])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--graph', type=Path, required=True)
|
|
parser.add_argument('--catalog', type=Path, required=True)
|
|
parser.add_argument('--output', type=Path, required=True)
|
|
parser.add_argument('--allow-source-ordinal-binding', action='store_true')
|
|
args = parser.parse_args()
|
|
print(json.dumps(build(args.graph, args.catalog, args.output,
|
|
allow_source_ordinal_binding=args.allow_source_ordinal_binding), indent=2))
|