feat(perception): seal M4.8S reference graph replay

This commit is contained in:
DCCONSTRUCTIONS
2026-08-25 16:44:26 +03:00
parent 33cef2fdea
commit 05590867b7
12 changed files with 2578 additions and 1 deletions
@@ -0,0 +1,581 @@
#!/usr/bin/env python3
"""Run RF-DETR through the complete source-paced reference graph on Worker 006."""
from __future__ import annotations
import argparse
import hashlib
import json
import resource
import subprocess
import threading
import time
from collections import Counter, defaultdict
from collections.abc import Mapping
from dataclasses import asdict, fields
from functools import partial
from pathlib import Path
from typing import Any, Final, TextIO, cast
import numpy as np
from k1link.perception.contracts import MotionState
from k1link.perception.contracts import ObjectProposal2D
from k1link.perception.detector import (
DetectorProviderSnapshot,
RfDetrShadowDetectorProvider,
)
from k1link.perception.geometry import Ravnoves00GeometryAssociationProvider
from k1link.perception.graph_contracts import (
DeliveredFrame,
GraphRunMode,
GraphRunResultV2,
GraphState,
TerminalOutcomeType,
)
from k1link.perception.m48s_advisory import (
AdvisoryFamily,
M48sSemanticAdvisory,
advisory_policy_matrix,
project_m48s_advisories,
)
from k1link.perception.m48s_reference_graph_runtime import (
build_m48s_reference_graph_runtime,
)
from k1link.perception.motion import ClassIndependentMotionEstimator
from k1link.perception.object_understanding import AdvisoryResponse
from k1link.perception.reference_graph_runtime import ReferenceGraphRuntimePaths
from k1link.perception.providers import SourcePacket
from k1link.perception.rolling_map import RollingLocalObstacleMapProvider
from k1link.perception.temporal import BoundedSpatialTemporalProvider
SCHEMA_VERSION: Final = "missioncore.m48s-reference-graph-shadow-load/v0"
FRAME_EVIDENCE_SCHEMA: Final = "missioncore.m48s-reference-graph-frame-evidence/v0"
AUTHORITY: Final = {
"ground_truth": False,
"candidate_accepted": False,
"commands_enabled": False,
"actuation_allowed": False,
"navigation_or_safety_accepted": False,
}
class GpuTelemetry:
def __init__(self, interval_seconds: float) -> None:
self.interval_seconds = interval_seconds
self.samples: list[dict[str, float]] = []
self._stop = threading.Event()
self._thread = threading.Thread(target=self._run, daemon=True)
def __enter__(self) -> GpuTelemetry:
self._thread.start()
return self
def __exit__(self, *_args: object) -> None:
self._stop.set()
self._thread.join(timeout=10.0)
def _run(self) -> None:
while not self._stop.is_set():
try:
completed = subprocess.run(
[
"nvidia-smi",
"--query-gpu=utilization.gpu,memory.used,power.draw,temperature.gpu",
"--format=csv,noheader,nounits",
],
check=True,
capture_output=True,
text=True,
timeout=10.0,
)
values = [float(value.strip()) for value in completed.stdout.split(",")]
if len(values) == 4:
self.samples.append(
{
"gpu_utilization_percent": values[0],
"gpu_memory_used_mib": values[1],
"gpu_power_w": values[2],
"gpu_temperature_c": values[3],
}
)
except (OSError, ValueError, subprocess.SubprocessError):
pass
self._stop.wait(self.interval_seconds)
def main() -> int:
parser = argparse.ArgumentParser()
for name in (
"graph-config",
"baseline-profile",
"detector-profile",
"geometry-profile",
"temporal-motion-profile",
"rolling-map-profile",
"threat-profile",
"camera-index",
"source-pack",
"local-surface",
"video",
"valid-fov-mask",
):
parser.add_argument(f"--{name}", type=Path, required=True)
parser.add_argument("--triton-origin", default="http://127.0.0.1:8000")
parser.add_argument("--loops", type=int, default=1)
parser.add_argument("--maximum-frames", type=int)
parser.add_argument("--telemetry-interval-seconds", type=float, default=1.0)
parser.add_argument("--output", type=Path, required=True)
parser.add_argument("--progress", type=Path, required=True)
parser.add_argument("--frame-ledger", type=Path, required=True)
arguments = parser.parse_args()
if arguments.loops < 1:
raise RuntimeError("loop count must be positive")
if arguments.maximum_frames is not None and arguments.maximum_frames < 1:
raise RuntimeError("maximum frame count must be positive")
if arguments.telemetry_interval_seconds <= 0:
raise RuntimeError("telemetry interval must be positive")
output = arguments.output.absolute()
progress = arguments.progress.absolute()
frame_ledger = arguments.frame_ledger.absolute()
if output.exists() or progress.exists() or frame_ledger.exists():
raise RuntimeError("result, progress, or frame ledger artifact already exists")
output.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
paths = ReferenceGraphRuntimePaths(
graph_config=arguments.graph_config,
baseline_profile=arguments.baseline_profile,
geometry_profile=arguments.geometry_profile,
temporal_motion_profile=arguments.temporal_motion_profile,
rolling_map_profile=arguments.rolling_map_profile,
threat_profile=arguments.threat_profile,
camera_index=arguments.camera_index,
source_pack=arguments.source_pack,
local_surface=arguments.local_surface,
video=arguments.video,
valid_fov_mask=arguments.valid_fov_mask,
)
started_ns = time.monotonic_ns()
started_utc_ns = time.time_ns()
rss_before_kib = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
loop_documents: list[dict[str, object]] = []
completion_ages_ms: list[float] = []
map_output_ages_ms: list[float] = []
all_deliveries: list[DeliveredFrame] = []
with (
progress.open("x", encoding="utf-8") as progress_stream,
frame_ledger.open("x", encoding="utf-8") as frame_ledger_stream,
GpuTelemetry(arguments.telemetry_interval_seconds) as gpu,
):
for loop_index in range(arguments.loops):
loop_completion_ages_ns: list[int] = []
setup_started_ns = time.monotonic_ns()
with build_m48s_reference_graph_runtime(
paths=paths,
detector_profile=arguments.detector_profile,
triton_origin=arguments.triton_origin,
run_mode=GraphRunMode.SOURCE_PACED_LATEST_WINS,
delivery_observer=partial(
_record_completion_age,
loop_completion_ages_ns,
),
delivery_evidence_observer=partial(
_record_delivery_evidence,
frame_ledger_stream,
loop_index,
),
maximum_frames=arguments.maximum_frames,
) as runtime:
loop_started_ns = time.monotonic_ns()
result = runtime.graph.run()
loop_completed_ns = time.monotonic_ns()
detector_snapshot = cast(
RfDetrShadowDetectorProvider,
runtime.graph.detector,
).snapshot()
provider_snapshots = {
"geometry": asdict(
cast(
Ravnoves00GeometryAssociationProvider,
runtime.graph.geometry,
).snapshot()
),
"temporal": asdict(
cast(
BoundedSpatialTemporalProvider,
runtime.graph.temporal,
).snapshot()
),
"motion": asdict(
cast(
ClassIndependentMotionEstimator,
runtime.graph.motion,
).snapshot()
),
"rolling": asdict(
cast(
RollingLocalObstacleMapProvider,
runtime.graph.rolling,
).snapshot()
),
}
if not isinstance(result, GraphRunResultV2):
raise RuntimeError("RF-DETR shadow did not return a V2 graph result")
loop_wall_seconds = (loop_completed_ns - loop_started_ns) / 1_000_000_000.0
setup_seconds = (loop_started_ns - setup_started_ns) / 1_000_000_000.0
if len(loop_completion_ages_ns) != len(result.deliveries):
raise RuntimeError("final delivery timing accounting did not close")
loop_document = _loop_document(
loop_index=loop_index,
result=result,
detector_snapshot=detector_snapshot,
provider_snapshots=provider_snapshots,
completion_ages_ns=loop_completion_ages_ns,
wall_seconds=loop_wall_seconds,
setup_seconds=setup_seconds,
)
loop_documents.append(loop_document)
completion_ages_ms.extend(value / 1_000_000.0 for value in loop_completion_ages_ns)
map_output_ages_ms.extend(
delivery.obstacle_map.output_age_ns / 1_000_000.0
for delivery in result.deliveries
)
all_deliveries.extend(result.deliveries)
frame_ledger_stream.flush()
progress_row = {
"loop": loop_index + 1,
"loops": arguments.loops,
"elapsed_seconds": round(
(time.monotonic_ns() - started_ns) / 1_000_000_000.0,
3,
),
"admitted": result.admitted_count,
"delivered": len(result.deliveries),
"world_state_completion_p95_ms": _distribution(
[value / 1_000_000.0 for value in loop_completion_ages_ns]
)["p95"],
}
progress_stream.write(json.dumps(progress_row, separators=(",", ":")) + "\n")
progress_stream.flush()
print(json.dumps(progress_row, sort_keys=True), flush=True)
completed_ns = time.monotonic_ns()
wall_seconds = (completed_ns - started_ns) / 1_000_000_000.0
rss_after_kib = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
accounting: Counter[str] = Counter()
for loop in loop_documents:
accounting.update(cast(Mapping[str, int], loop["terminal_outcomes"]))
admitted = sum(cast(int, loop["admitted_count"]) for loop in loop_documents)
delivered = len(all_deliveries)
processing_wall_seconds = sum(
cast(float, loop["wall_seconds"]) for loop in loop_documents
)
queue_high_watermarks = {
stage: max(
cast(dict[str, int], loop["queue_high_watermarks"])[stage]
for loop in loop_documents
)
for stage in ("detector", "geometry", "temporal", "rolling", "threat")
}
advisories = tuple(
advisory
for delivery in all_deliveries
for advisory in project_m48s_advisories(delivery)
)
identity = _identity_metrics(all_deliveries)
semantic = _semantic_metrics(all_deliveries, advisories)
world_state_fps = delivered / processing_wall_seconds
checks = {
"loop_count_completed": len(loop_documents) == arguments.loops,
"graph_stopped_cleanly": all(
loop["state"] == GraphState.STOPPED.value for loop in loop_documents
),
"closed_terminal_accounting": admitted == sum(accounting.values()),
"zero_failed_stale_rejected_or_unavailable": all(
accounting[outcome.value] == 0
for outcome in (
TerminalOutcomeType.FAILED,
TerminalOutcomeType.STALE,
TerminalOutcomeType.REJECTED,
TerminalOutcomeType.UNAVAILABLE,
)
),
"minimum_world_state_fps": world_state_fps >= 9.5,
"maximum_world_state_completion_p95_ms": (
_distribution(completion_ages_ms)["p95"] <= 175.0
),
"bounded_latest_wins_queues": all(value <= 2 for value in queue_high_watermarks.values()),
"temporal_identity_reuse_observed": cast(
int,
identity["multi_frame_component_count"],
)
> 0,
"conservative_unknown_motion_advisory": _unknown_motion_is_conservative(advisories),
"distinct_class_family_policy": len(set(advisory_policy_matrix().values()))
== len(AdvisoryFamily),
"authority_remains_false": all(value is False for value in AUTHORITY.values()),
}
integrated_runtime_gate_passed = all(checks.values())
document = {
"schema_version": SCHEMA_VERSION,
"source": {
"source_id": "RAVNOVES00",
"loops": arguments.loops,
"maximum_frames_per_loop": arguments.maximum_frames,
},
"identity": {
"worker_id": "worker-006",
"graph_id": "reference-perception-graph/v2",
"detector_provider_id": "triton-rf-detr-large-coco-risk-fp16-shadow/v0",
"inputs": _input_digests(paths, arguments.detector_profile),
},
"execution": {
"run_mode": GraphRunMode.SOURCE_PACED_LATEST_WINS.value,
"wall_seconds": round(wall_seconds, 6),
"source_processing_wall_seconds": round(processing_wall_seconds, 6),
"admitted_frames": admitted,
"delivered_world_states": delivered,
"effective_world_state_fps": round(world_state_fps, 6),
"terminal_outcomes": dict(sorted(accounting.items())),
"queue_high_watermarks": queue_high_watermarks,
"loops": loop_documents,
"frame_evidence": {
"schema_version": FRAME_EVIDENCE_SCHEMA,
"path": frame_ledger.name,
"row_count": delivered,
"sha256": _sha256(frame_ledger),
},
},
"metrics": {
"world_state_completion_age_ms": _distribution(completion_ages_ms),
"local_obstacle_map_output_age_ms": _distribution(map_output_ages_ms),
"identity_continuity": identity,
"semantic_advisory": semantic,
"gpu": _telemetry_summary(gpu.samples),
"process_peak_rss_before_mib": round(rss_before_kib / 1024.0, 6),
"process_peak_rss_after_mib": round(rss_after_kib / 1024.0, 6),
},
"checks": checks,
"integrated_runtime_gate_passed": integrated_runtime_gate_passed,
"independent_track_identity_quality_evaluated": False,
"independent_risk_policy_quality_evaluated": False,
"production_accepted": False,
"started_utc_ns": started_utc_ns,
"completed_utc_ns": time.time_ns(),
"completed": True,
"authority": AUTHORITY,
}
output.write_bytes(_canonical_json(document) + b"\n")
print(output)
print(json.dumps(checks, indent=2, sort_keys=True))
return 0 if integrated_runtime_gate_passed else 2
def _record_completion_age(
samples: list[int],
_delivery: DeliveredFrame,
completion_age_ns: int,
) -> None:
samples.append(completion_age_ns)
def _record_delivery_evidence(
stream: TextIO,
loop_index: int,
delivery: DeliveredFrame,
packet: SourcePacket,
proposals: tuple[ObjectProposal2D, ...],
associated_proposal_ids: frozenset[str],
completion_age_ns: int,
) -> None:
if delivery.sequence != packet.envelope.sequence:
raise RuntimeError("delivery evidence sequence binding changed")
row = {
"schema_version": FRAME_EVIDENCE_SCHEMA,
"loop_index": loop_index,
"source_envelope": packet.envelope.to_dict(),
"completion_age_ns": completion_age_ns,
"local_obstacle_map_output_age_ns": delivery.obstacle_map.output_age_ns,
"delivery": delivery.canonical_dict(),
"detector_proposals": [proposal.to_dict() for proposal in proposals],
"associated_proposal_ids": sorted(associated_proposal_ids),
"semantic_advisories": [
advisory.to_dict() for advisory in project_m48s_advisories(delivery)
],
"authority": AUTHORITY,
}
stream.write(_canonical_json(row).decode("utf-8") + "\n")
def _loop_document(
*,
loop_index: int,
result: GraphRunResultV2,
detector_snapshot: DetectorProviderSnapshot,
provider_snapshots: dict[str, dict[str, object]],
completion_ages_ns: list[int],
wall_seconds: float,
setup_seconds: float,
) -> dict[str, object]:
outcomes = Counter(item.outcome.value for item in result.terminal_outcomes)
outcome_stages = Counter(
f"{item.outcome.value}:{item.stage_id}:{item.reason}"
for item in result.terminal_outcomes
)
return {
"loop_index": loop_index,
"state": result.state.value,
"wall_seconds": round(wall_seconds, 6),
"setup_seconds": round(setup_seconds, 6),
"admitted_count": result.admitted_count,
"delivered_count": len(result.deliveries),
"effective_world_state_fps": round(len(result.deliveries) / wall_seconds, 6),
"terminal_outcomes": dict(sorted(outcomes.items())),
"terminal_outcome_stages": dict(sorted(outcome_stages.items())),
"terminal_outcome_details": [item.to_dict() for item in result.terminal_outcomes],
"queue_high_watermarks": dict(result.queue_high_watermarks),
"canonical_payload_sha256": result.canonical_payload_sha256,
"world_state_completion_age_ms": _distribution(
[value / 1_000_000.0 for value in completion_ages_ns]
),
"detector": asdict(detector_snapshot),
"providers": provider_snapshots,
}
def _identity_metrics(deliveries: list[DeliveredFrame]) -> dict[str, object]:
appearances: dict[str, list[int]] = defaultdict(list)
duplicate_components = 0
for delivery in deliveries:
components = (
*delivery.obstacle_map.occupied,
*delivery.obstacle_map.unknown,
)
ids = [item.component_id for item in components]
duplicate_components += len(ids) - len(set(ids))
for component_id in ids:
appearances[component_id].append(delivery.sequence)
lengths = [len(values) for values in appearances.values()]
return {
"unique_component_count": len(appearances),
"multi_frame_component_count": sum(value > 1 for value in lengths),
"maximum_component_publications": max(lengths, default=0),
"mean_component_publications": round(float(np.mean(lengths)), 6) if lengths else 0.0,
"duplicate_component_ids_within_frame": duplicate_components,
"identity_scope": "ephemeral",
"independent_truth_available": False,
}
def _semantic_metrics(
deliveries: list[DeliveredFrame],
advisories: tuple[M48sSemanticAdvisory, ...],
) -> dict[str, object]:
hints: Counter[str] = Counter()
motions: Counter[str] = Counter()
for delivery in deliveries:
obstacles = (
*delivery.obstacle_map.occupied,
*delivery.obstacle_map.unknown,
)
for obstacle in obstacles:
hints[obstacle.semantic_hint or "geometry-only"] += 1
motions[obstacle.motion.value] += 1
for proposal in delivery.obstacle_map.camera_uncertainty:
hints[proposal.semantic_hint or "unclassified-camera"] += 1
motions[MotionState.UNKNOWN.value] += 1
families = Counter(item.family.value for item in advisories)
responses = Counter(
response.value
for item in advisories
for response in item.responses
)
return {
"semantic_hint_counts": dict(sorted(hints.items())),
"motion_counts": dict(sorted(motions.items())),
"advisory_family_counts": dict(sorted(families.items())),
"advisory_response_counts": dict(sorted(responses.items())),
"policy_matrix": {
family.value: [response.value for response in policy]
for family, policy in advisory_policy_matrix().items()
},
"additional_inference_passes": 0,
"authority": AUTHORITY,
}
def _unknown_motion_is_conservative(
advisories: tuple[M48sSemanticAdvisory, ...],
) -> bool:
for item in advisories:
if item.motion is not MotionState.UNKNOWN:
continue
responses = set(item.responses)
if not responses.intersection(
{
AdvisoryResponse.REDUCE_SPEED,
AdvisoryResponse.YIELD,
AdvisoryResponse.STOP,
AdvisoryResponse.ROUTE_AROUND,
}
):
return False
return True
def _distribution(values: list[float]) -> dict[str, float]:
if not values:
return {"mean": 0.0, "p50": 0.0, "p95": 0.0, "p99": 0.0, "maximum": 0.0}
array = np.asarray(values, dtype=np.float64)
return {
"mean": round(float(array.mean()), 6),
"p50": round(float(np.percentile(array, 50)), 6),
"p95": round(float(np.percentile(array, 95)), 6),
"p99": round(float(np.percentile(array, 99)), 6),
"maximum": round(float(array.max()), 6),
}
def _telemetry_summary(samples: list[dict[str, float]]) -> dict[str, Any]:
result: dict[str, Any] = {"sample_count": len(samples)}
for key in (
"gpu_utilization_percent",
"gpu_memory_used_mib",
"gpu_power_w",
"gpu_temperature_c",
):
result[key] = _distribution([sample[key] for sample in samples])
return result
def _input_digests(
paths: ReferenceGraphRuntimePaths,
detector_profile: Path,
) -> dict[str, str]:
values = {
field.name: _sha256(getattr(paths, field.name))
for field in fields(ReferenceGraphRuntimePaths)
}
values["detector_profile"] = _sha256(detector_profile)
return dict(sorted(values.items()))
def _sha256(path: Path) -> str:
digest = hashlib.sha256()
with path.resolve(strict=True).open("rb") as stream:
for chunk in iter(lambda: stream.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def _canonical_json(value: object) -> bytes:
return json.dumps(
value,
sort_keys=True,
separators=(",", ":"),
allow_nan=False,
).encode("utf-8")
if __name__ == "__main__":
raise SystemExit(main())
@@ -0,0 +1,251 @@
#!/usr/bin/env python3
"""Seal exact per-frame evidence from the full M4.8S reference-graph replay."""
from __future__ import annotations
import argparse
import hashlib
import json
import shutil
import tempfile
from pathlib import Path
from typing import Any, Final
from k1link.perception.fixed_class_detector_tournament import canonical_json, sha256_path
SCHEMA_VERSION: Final = "missioncore.m48s-reference-graph-replay/v0"
RAW_SCHEMA_VERSION: Final = "missioncore.m48s-reference-graph-shadow-load/v0"
FRAME_SCHEMA_VERSION: Final = "missioncore.m48s-reference-graph-frame-evidence/v0"
RESULT_PREFIX: Final = "m48s-reference-graph-replay-"
AUTHORITY: Final = {
"ground_truth": False,
"candidate_accepted": False,
"commands_enabled": False,
"actuation_allowed": False,
"navigation_or_safety_accepted": False,
}
def main() -> int:
repository = Path(__file__).resolve().parents[2]
parser = argparse.ArgumentParser()
parser.add_argument("--raw-result", type=Path, required=True)
parser.add_argument("--progress", type=Path, required=True)
parser.add_argument("--frames", type=Path, required=True)
parser.add_argument("--wheel-sha256", required=True)
parser.add_argument("--worker-release-id", required=True)
parser.add_argument(
"--output-root",
type=Path,
default=(
repository
/ ".runtime/compute-experiments/m48s-semantic-shadow/"
"reference-graph-replay-results"
),
)
arguments = parser.parse_args()
paths = {
"raw_result": arguments.raw_result.resolve(strict=True),
"progress": arguments.progress.resolve(strict=True),
"frames": arguments.frames.resolve(strict=True),
}
raw = _load_object(paths["raw_result"])
frame_summary = _validate(raw, paths["frames"])
if (
len(arguments.wheel_sha256) != 64
or any(value not in "0123456789abcdef" for value in arguments.wheel_sha256)
):
raise RuntimeError("worker wheel digest is invalid")
if not arguments.worker_release_id.startswith("mission-core-m48s-reference-graph-replay-"):
raise RuntimeError("worker replay release identity is invalid")
evidence = {
"files": {
name: {"sha256": sha256_path(path), "size_bytes": path.stat().st_size}
for name, path in sorted(paths.items())
},
"worker": {
"worker_id": "worker-006",
"release_id": arguments.worker_release_id,
"wheel_sha256": arguments.wheel_sha256,
"historical_triton_action": "none",
"durable_worker_action": "none",
},
"execution": raw["execution"],
"metrics": raw["metrics"],
"frame_summary": frame_summary,
"checks": raw["checks"],
}
identity = {
"schema_version": SCHEMA_VERSION,
"source_session_id": "20260720T065719Z_viewer_live",
"source_id": "RAVNOVES00",
"graph_id": "reference-perception-graph/v2",
"detector_provider_id": "triton-rf-detr-large-coco-risk-fp16-shadow/v0",
"evidence": evidence,
"completed": True,
"accepted": True,
"production_accepted": False,
"authority": AUTHORITY,
}
result_id = RESULT_PREFIX + hashlib.sha256(canonical_json(identity)).hexdigest()
output_root = arguments.output_root.expanduser().absolute()
output_root.mkdir(mode=0o700, parents=True, exist_ok=True)
destination = output_root / result_id
if destination.exists():
existing = _load_object(destination / "manifest.json")
if existing.get("result_id") != result_id or existing.get("identity") != identity:
raise RuntimeError("immutable replay result exists with different identity")
print(result_id)
return 0
temporary = Path(tempfile.mkdtemp(prefix=".m48s-reference-replay-", dir=output_root))
try:
shutil.copyfile(paths["raw_result"], temporary / "worker-result.json")
shutil.copyfile(paths["progress"], temporary / "worker-progress.jsonl")
shutil.copyfile(paths["frames"], temporary / "frames.jsonl")
report = {
"schema_version": SCHEMA_VERSION,
"result_id": result_id,
"completed": True,
"accepted": True,
"production_accepted": False,
"frame_summary": frame_summary,
"limitations": [
"This is source-paced replay evidence, not physical-live authority.",
"Latest-wins superseded source frames retain exact camera/LiDAR source evidence but have no invented world state.",
"LiDAR camera points are generated later from the pinned factory KB4 calibration and are not semantic labels.",
],
"authority": AUTHORITY,
}
(temporary / "report.json").write_bytes(canonical_json(report) + b"\n")
artifacts = {
path.name: {"sha256": sha256_path(path), "size_bytes": path.stat().st_size}
for path in sorted(temporary.iterdir())
if path.is_file()
}
manifest = {
"schema_version": SCHEMA_VERSION,
"result_id": result_id,
"identity": identity,
"artifacts": artifacts,
}
(temporary / "manifest.json").write_bytes(canonical_json(manifest) + b"\n")
temporary.rename(destination)
except Exception:
shutil.rmtree(temporary, ignore_errors=True)
raise
print(result_id)
print(json.dumps(frame_summary, indent=2, sort_keys=True))
return 0
def _validate(raw: dict[str, Any], frames_path: Path) -> dict[str, object]:
if (
raw.get("schema_version") != RAW_SCHEMA_VERSION
or raw.get("completed") is not True
or raw.get("integrated_runtime_gate_passed") is not True
or raw.get("production_accepted") is not False
or raw.get("authority") != AUTHORITY
):
raise RuntimeError("raw graph replay is not accepted shadow evidence")
checks = raw.get("checks")
execution = raw.get("execution")
if (
not isinstance(checks, dict)
or not checks
or not all(value is True for value in checks.values())
or not isinstance(execution, dict)
or execution.get("admitted_frames") != 4489
or execution.get("run_mode") != "source-paced-latest-wins"
or not isinstance(execution.get("loops"), list)
or len(execution["loops"]) != 1
):
raise RuntimeError("raw graph replay execution contract failed")
ledger = execution.get("frame_evidence")
if (
not isinstance(ledger, dict)
or ledger.get("schema_version") != FRAME_SCHEMA_VERSION
or ledger.get("sha256") != sha256_path(frames_path)
or ledger.get("path") != frames_path.name
):
raise RuntimeError("frame ledger binding failed")
loop = execution["loops"][0]
if not isinstance(loop, dict) or not isinstance(loop.get("terminal_outcome_details"), list):
raise RuntimeError("terminal outcome details are unavailable")
outcomes = loop["terminal_outcome_details"]
if len(outcomes) != 4489:
raise RuntimeError("terminal outcome details do not close source accounting")
outcome_by_sequence: dict[int, str] = {}
superseded_sequences: list[int] = []
delivered_sequences: list[int] = []
for item in outcomes:
if not isinstance(item, dict) or item.get("schema_version") != "missioncore.perception-terminal-outcome/v1":
raise RuntimeError("terminal outcome row is invalid")
sequence = item.get("sequence")
outcome = item.get("outcome")
if not isinstance(sequence, int) or isinstance(sequence, bool) or sequence in outcome_by_sequence:
raise RuntimeError("terminal sequence identity is invalid")
if outcome not in {"delivered", "superseded"}:
raise RuntimeError("unexpected terminal outcome in accepted replay")
outcome_by_sequence[sequence] = outcome
(delivered_sequences if outcome == "delivered" else superseded_sequences).append(sequence)
if sorted(outcome_by_sequence) != list(range(4489)):
raise RuntimeError("terminal sequence set is incomplete")
ledger_sequences: list[int] = []
source_times_ns: list[int] = []
with frames_path.open("r", encoding="utf-8") as stream:
for line_number, line in enumerate(stream, 1):
try:
row = json.loads(line)
except json.JSONDecodeError as exc:
raise RuntimeError(f"invalid frame ledger JSON at line {line_number}") from exc
if (
not isinstance(row, dict)
or row.get("schema_version") != FRAME_SCHEMA_VERSION
or row.get("loop_index") != 0
or row.get("authority") != AUTHORITY
):
raise RuntimeError(f"invalid frame ledger contract at line {line_number}")
envelope = row.get("source_envelope")
delivery = row.get("delivery")
if not isinstance(envelope, dict) or not isinstance(delivery, dict):
raise RuntimeError("frame ledger source or delivery is missing")
sequence = envelope.get("sequence")
timestamps = envelope.get("timestamps")
if (
not isinstance(sequence, int)
or isinstance(sequence, bool)
or delivery.get("sequence") != sequence
or outcome_by_sequence.get(sequence) != "delivered"
or not isinstance(timestamps, dict)
or not isinstance(timestamps.get("source_ns"), int)
):
raise RuntimeError(f"frame ledger binding failed at line {line_number}")
ledger_sequences.append(sequence)
source_times_ns.append(timestamps["source_ns"])
if ledger_sequences != sorted(delivered_sequences) or len(ledger_sequences) != ledger.get("row_count"):
raise RuntimeError("frame ledger delivered sequence set does not close")
if any(right <= left for left, right in zip(source_times_ns, source_times_ns[1:])):
raise RuntimeError("frame ledger source clock is not monotonic")
return {
"source_frame_count": 4489,
"world_state_frame_count": len(ledger_sequences),
"superseded_frame_count": len(superseded_sequences),
"superseded_sequences": sorted(superseded_sequences),
"first_source_time_ns": source_times_ns[0],
"last_source_time_ns": source_times_ns[-1],
"frame_schema_version": FRAME_SCHEMA_VERSION,
}
def _load_object(path: Path) -> dict[str, Any]:
document = json.loads(path.read_text("utf-8"))
if not isinstance(document, dict):
raise RuntimeError(f"JSON document must be an object: {path}")
return document
if __name__ == "__main__":
raise SystemExit(main())
@@ -0,0 +1,251 @@
#!/usr/bin/env python3
"""Seal the complete RF-DETR reference-graph runtime gate."""
from __future__ import annotations
import argparse
import hashlib
import json
import shutil
from pathlib import Path
from typing import Any, Final
from k1link.perception.fixed_class_detector_tournament import (
canonical_json,
false_authority,
sha256_path,
)
SCHEMA_VERSION: Final = "missioncore.m48s-reference-graph-shadow-gate/v0"
RAW_SCHEMA_VERSION: Final = "missioncore.m48s-reference-graph-shadow-load/v0"
RESULT_PREFIX: Final = "m48s-reference-graph-shadow-gate-"
EXPECTED_GRAPH_CONFIG_SHA256: Final = (
"e607916c0d2db5a1078bc194fa0e5e1bac1ca336de8daad29359ed0d2791b6cd"
)
EXPECTED_DETECTOR_PROFILE_SHA256: Final = (
"0c307fd2d19cedd2c9267b6be2effdce82161a719a742f315fcd7a76f7061b08"
)
def main() -> int:
repository = Path(__file__).resolve().parents[2]
parser = argparse.ArgumentParser()
parser.add_argument(
"--raw-result",
type=Path,
default=(
repository
/ ".runtime/m48s-reference-graph-shadow/full-1x-a6ee52c9/result.json"
),
)
parser.add_argument(
"--progress",
type=Path,
default=(
repository
/ ".runtime/m48s-reference-graph-shadow/full-1x-a6ee52c9/progress.jsonl"
),
)
parser.add_argument(
"--graph-config",
type=Path,
default=(
repository
/ "config/perception/m48s-rf-detr-reference-graph-shadow-v0.json"
),
)
parser.add_argument(
"--detector-profile",
type=Path,
default=repository / "config/perception/rf-detr-large-risk-shadow-v0.json",
)
parser.add_argument(
"--wheel-sha256",
default="a6ee52c9113bd298f899352b323f9e2b8175d15ca33f342cb8a9b896c41d64ca",
)
parser.add_argument(
"--worker-release-id",
default="mission-core-m48s-reference-graph-shadow-a6ee52c9",
)
parser.add_argument(
"--output-root",
type=Path,
default=(
repository
/ ".runtime/compute-experiments/m48s-semantic-shadow/"
"reference-graph-shadow-results"
),
)
arguments = parser.parse_args()
paths = {
"raw_result": arguments.raw_result.resolve(strict=True),
"progress": arguments.progress.resolve(strict=True),
"graph_config": arguments.graph_config.resolve(strict=True),
"detector_profile": arguments.detector_profile.resolve(strict=True),
}
raw = _load_object(paths["raw_result"])
_validate(
raw,
graph_config_sha256=sha256_path(paths["graph_config"]),
detector_profile_sha256=sha256_path(paths["detector_profile"]),
wheel_sha256=arguments.wheel_sha256,
worker_release_id=arguments.worker_release_id,
)
execution = raw["execution"]
metrics = raw["metrics"]
evidence = {
"files": {
name: {"sha256": sha256_path(path), "size_bytes": path.stat().st_size}
for name, path in sorted(paths.items())
},
"worker": {
"worker_id": "worker-006",
"release_id": arguments.worker_release_id,
"wheel_sha256": arguments.wheel_sha256,
"historical_triton_action": "none",
"durable_worker_action": "none",
},
"execution": execution,
"world_state_completion_age_ms": metrics["world_state_completion_age_ms"],
"local_obstacle_map_output_age_ms": metrics[
"local_obstacle_map_output_age_ms"
],
"identity_continuity": metrics["identity_continuity"],
"semantic_advisory": metrics["semantic_advisory"],
"gpu": metrics["gpu"],
"checks": raw["checks"],
}
decision = {
"complete_reference_graph_shadow_passed": True,
"source_paced_runtime_gate_accepted": True,
"ready_for_visual_lab": True,
"independent_track_identity_quality_evaluated": False,
"independent_risk_policy_quality_evaluated": False,
"detector_replacement_authorized": False,
"production_accepted": False,
"next_gate": (
"publish the complete world-state replay in Mission Core LAB and retain "
"independent object-centric review as a separate acceptance"
),
}
identity = {
"schema_version": SCHEMA_VERSION,
"graph_id": "reference-perception-graph/v2",
"detector_provider_id": "triton-rf-detr-large-coco-risk-fp16-shadow/v0",
"evidence": evidence,
"decision": decision,
"completed": True,
"accepted": True,
"production_accepted": False,
"authority": false_authority(),
}
result_id = RESULT_PREFIX + hashlib.sha256(canonical_json(identity)).hexdigest()
destination = arguments.output_root.absolute() / result_id
if destination.exists():
existing = _load_object(destination / "manifest.json")
if existing.get("result_id") != result_id or existing.get("identity") != identity:
raise RuntimeError("immutable M48S graph result already exists with different bytes")
print(result_id)
return 0
destination.mkdir(mode=0o700, parents=True)
shutil.copyfile(paths["raw_result"], destination / "worker-result.json")
shutil.copyfile(paths["progress"], destination / "worker-progress.jsonl")
report = {
"schema_version": SCHEMA_VERSION,
"result_id": result_id,
"completed": True,
"accepted": True,
"production_accepted": False,
"evidence": evidence,
"decision": decision,
"authority": false_authority(),
}
(destination / "report.json").write_bytes(canonical_json(report) + b"\n")
manifest = {
"schema_version": SCHEMA_VERSION,
"result_id": result_id,
"identity": identity,
"artifacts": {
"worker-result.json": sha256_path(destination / "worker-result.json"),
"worker-progress.jsonl": sha256_path(destination / "worker-progress.jsonl"),
"report.json": sha256_path(destination / "report.json"),
},
}
(destination / "manifest.json").write_bytes(canonical_json(manifest) + b"\n")
print(result_id)
print(json.dumps(decision, indent=2, sort_keys=True))
return 0
def _validate(
raw: dict[str, Any],
*,
graph_config_sha256: str,
detector_profile_sha256: str,
wheel_sha256: str,
worker_release_id: str,
) -> None:
if (
raw.get("schema_version") != RAW_SCHEMA_VERSION
or raw.get("completed") is not True
or raw.get("integrated_runtime_gate_passed") is not True
or raw.get("production_accepted") is not False
or raw.get("authority") != false_authority()
):
raise RuntimeError("M48S complete graph result is not accepted shadow evidence")
if graph_config_sha256 != EXPECTED_GRAPH_CONFIG_SHA256:
raise RuntimeError("M48S complete graph config identity changed")
if detector_profile_sha256 != EXPECTED_DETECTOR_PROFILE_SHA256:
raise RuntimeError("M48S detector profile identity changed")
if len(wheel_sha256) != 64 or any(value not in "0123456789abcdef" for value in wheel_sha256):
raise RuntimeError("M48S worker wheel digest is invalid")
if worker_release_id != "mission-core-m48s-reference-graph-shadow-a6ee52c9":
raise RuntimeError("M48S worker release identity changed")
checks = raw.get("checks")
execution = raw.get("execution")
metrics = raw.get("metrics")
if (
not isinstance(checks, dict)
or not checks
or not all(value is True for value in checks.values())
or not isinstance(execution, dict)
or not isinstance(metrics, dict)
):
raise RuntimeError("M48S complete graph checks are incomplete")
outcomes = execution.get("terminal_outcomes")
queues = execution.get("queue_high_watermarks")
completion = metrics.get("world_state_completion_age_ms")
identity = metrics.get("identity_continuity")
semantic = metrics.get("semantic_advisory")
if (
execution.get("admitted_frames") != 4489
or not isinstance(outcomes, dict)
or sum(outcomes.values()) != 4489
or outcomes.get("delivered") != execution.get("delivered_world_states")
or any(outcomes.get(key, 0) != 0 for key in ("failed", "stale", "rejected", "unavailable"))
or float(execution.get("effective_world_state_fps", 0.0)) < 9.5
or not isinstance(queues, dict)
or set(queues) != {"detector", "geometry", "temporal", "rolling", "threat"}
or any(not isinstance(value, int) or value > 2 for value in queues.values())
or not isinstance(completion, dict)
or float(completion.get("p95", 1_000.0)) > 175.0
or not isinstance(identity, dict)
or identity.get("duplicate_component_ids_within_frame") != 0
or int(identity.get("multi_frame_component_count", 0)) < 1
or identity.get("independent_truth_available") is not False
or not isinstance(semantic, dict)
or semantic.get("additional_inference_passes") != 0
or semantic.get("authority") != false_authority()
):
raise RuntimeError("M48S complete graph runtime contract failed")
def _load_object(path: Path) -> dict[str, Any]:
document = json.loads(path.read_text("utf-8"))
if not isinstance(document, dict):
raise RuntimeError(f"JSON document must be an object: {path}")
return document
if __name__ == "__main__":
raise SystemExit(main())