#!/usr/bin/env python3
"""Run camera inference, LiDAR fusion and world-state publication in one paced loop."""

from __future__ import annotations

import argparse
import hashlib
import importlib.metadata
import json
import os
import platform
import resource
import shutil
import threading
import time
from collections import Counter
from dataclasses import dataclass
from datetime import UTC, datetime
from pathlib import Path
from typing import Any

import numpy as np
import run_e9_multirate_perception as e9
from e10_fusion_runtime import (
    CuboidCompletionTracker,
    LidarReplayPack,
    WorldStateProjector,
    _completion_profile,
    _object_support_ground_filter_profile,
    canonical_json,
    clearance,
    distance_history,
    fuse_tracks,
    fusion_document,
    project_points,
    sha256,
)
from run_e4_full_session_segmentation import TARGET_CLASS_COUNT, _dependency_manifest, _load_model
from run_e4_full_session_segmentation import _profile as read_semantic_profile
from run_e4_full_session_segmentation import _validate_source as validate_semantic_source
from run_e5_instance_tracking import (
    TwoStageTracker,
    _detections,
    _infer,
    _load_valid_fov,
    _preprocess,
    _read_timeline,
    _track_document,
    _validate_source,
    _verify_model,
)
from run_e8_realtime_tracking import LatestWinsQueue
from run_e8_realtime_tracking import _read_profile as read_detector_profile
from run_recorded_perception_epoch import (
    _artifact,
    _GpuTelemetry,
    _percentiles,
    _valid_sha256,
    _validate_job,
    _write_json,
)

PROFILE_SCHEMA = "missioncore.e10-integrated-perception-profile/v1"
RESULT_SCHEMA = "missioncore.e10-integrated-perception-result/v1"
REPORT_SCHEMA = "missioncore.e10-integrated-perception-report/v1"
IDENTITY_SCHEMA = "missioncore.e10-integrated-perception-identity/v1"
SEMANTIC_SCHEMA = "missioncore.e10-semantic-frame/v1"
FUSION_SCHEMA = "missioncore.e10-fusion-frame/v1"
WORLD_SCHEMA = "missioncore.live-perception-world-state/v1"
PIPELINE_ID = "source-paced-yolox-eomt-kb4-lidar-world-state/v1"


def arguments() -> argparse.Namespace:
    parser = argparse.ArgumentParser()
    commands = parser.add_subparsers(dest="command", required=True)
    for name in ("preflight", "run"):
        command = commands.add_parser(name)
        command.add_argument("--job", type=Path, required=True)
        command.add_argument("--profile", type=Path, required=True)
        command.add_argument("--detector-profile", type=Path, required=True)
        command.add_argument("--semantic-profile", type=Path, required=True)
        command.add_argument("--valid-fov-root", type=Path, required=True)
        command.add_argument("--model-root", type=Path, required=True)
        command.add_argument("--cache", type=Path, required=True)
        command.add_argument("--environment", type=Path, required=True)
        command.add_argument("--lidar-pack", type=Path, required=True)
        if name == "run":
            command.add_argument("--frames", type=Path, required=True)
            command.add_argument("--timeline", type=Path, required=True)
            command.add_argument("--output", type=Path, required=True)
            command.add_argument("--triton-url", required=True)
            command.add_argument("--free-bytes-floor", type=int, default=0)
            command.add_argument("--orchestrator-sha256", required=True)
            command.add_argument("--container-image", required=True)
    return parser.parse_args()


def read_object(path: Path) -> dict[str, Any]:
    value = json.loads(path.read_text(encoding="utf-8-sig"))
    if not isinstance(value, dict):
        raise RuntimeError(f"JSON root is not an object: {path}")
    return value


def read_profile(path: Path) -> tuple[dict[str, Any], str]:
    resolved = path.resolve(strict=True)
    profile = read_object(resolved)
    replay = profile.get("replay")
    source = profile.get("source")
    association = profile.get("association")
    world = profile.get("world_state")
    acceptance = profile.get("acceptance")
    if (
        profile.get("schema_version") != PROFILE_SCHEMA
        or profile.get("mode")
        not in {
            "pilot",
            "qualification",
            "full-session-qualification",
            "semantic-loss-negative-control",
        }
        or not all(
            isinstance(value, dict) for value in (replay, source, association, world, acceptance)
        )
        or source.get("source_id") != "sensor.camera.right"
        or source.get("resolution") != [800, 600]
        or set(association.get("semantic_ids", {}))
        != {"person", "bicycle", "motorcycle", "vehicle"}
        or not isinstance(world.get("clearance"), dict)
    ):
        raise RuntimeError("LAB E10 profile contract is invalid")
    if (
        not 0.1 <= float(replay.get("speed", 0)) <= 10
        or not 1 <= int(replay.get("detector_queue_capacity", 0)) <= 8
        or not 1 <= int(replay.get("semantic_queue_capacity", 0)) <= 4
        or not 2 <= int(replay.get("semantic_sample_every_frames", 0)) <= 30
        or not 100 <= float(replay.get("semantic_ttl_ms", 0)) <= 5000
    ):
        raise RuntimeError("LAB E10 scheduling contract is invalid")
    if profile["mode"] == "semantic-loss-negative-control":
        semantic_loss = profile.get("semantic_loss")
        if (
            not isinstance(semantic_loss, dict)
            or not 1 <= int(semantic_loss.get("stop_after_completed_results", 0)) <= 100
            or not 1 <= int(acceptance.get("minimum_stale_detector_frames", 0)) <= 1_000_000
        ):
            raise RuntimeError("LAB E10 semantic-loss contract is invalid")
    if profile["mode"] == "full-session-qualification":
        selection = profile.get("selection")
        if (
            not isinstance(selection, dict)
            or int(selection.get("required_frame_count", 0)) < 2
            or int(selection.get("required_source_start_frame_index", -1)) != 0
            or int(selection.get("required_source_end_frame_index", -1))
            != int(selection["required_frame_count"]) - 1
            or float(selection.get("minimum_source_span_seconds", 0)) <= 0
        ):
            raise RuntimeError("LAB E10 full-session selection contract is invalid")
    completion = profile.get("cuboid_completion")
    if completion is not None:
        if not isinstance(completion, dict):
            raise RuntimeError("LAB E13 cuboid completion contract is invalid")
        _completion_profile(completion)
    ground_filter = association.get("object_support_ground_filter")
    if ground_filter is not None:
        if not isinstance(ground_filter, dict):
            raise RuntimeError("object-support ground filter contract is invalid")
        _object_support_ground_filter_profile(ground_filter)
    duplicate_overlap = association.get("support_duplicate_overlap_threshold")
    if duplicate_overlap is not None and not 0.0 < float(duplicate_overlap) <= 1.0:
        raise RuntimeError("support duplicate overlap contract is invalid")
    return profile, sha256(resolved)


@dataclass(frozen=True, slots=True)
class SemanticResult:
    frame_index: int
    source_frame_index: int
    session_seconds: float
    completion_age_ms: float
    completed_monotonic: float
    mask: np.ndarray
    mask_sha256: str
    class_pixels: dict[str, int]


class LatestSemantic:
    def __init__(self) -> None:
        self.lock = threading.Lock()
        self.value: SemanticResult | None = None

    def publish(self, value: SemanticResult) -> None:
        with self.lock:
            if self.value is not None and value.frame_index <= self.value.frame_index:
                raise RuntimeError("LAB E10 semantic results are not monotonic")
            self.value = value

    def snapshot(self) -> SemanticResult | None:
        with self.lock:
            return self.value


def semantic_worker(
    *,
    queue: LatestWinsQueue,
    latest: LatestSemantic,
    valid_mask: np.ndarray,
    target_lut: np.ndarray,
    target_names: dict[int, str],
    infer: Any,
    latency: dict[str, list[float]],
    completed: list[SemanticResult],
    failures: list[BaseException],
    stop_after_results: int | None,
    transform_target: Any | None = None,
) -> None:
    try:
        while (envelope := queue.take()) is not None:
            started = time.perf_counter()
            latency["queue_wait_ms"].append(max(0.0, (started - envelope.decoded_monotonic) * 1000))
            fill_started = time.perf_counter()
            model_input = np.where(valid_mask[..., None], envelope.image, 0).astype(np.uint8)
            latency["valid_fov_fill_ms"].append((time.perf_counter() - fill_started) * 1000)
            semantic, measured = infer(model_input)
            if int(semantic.max()) >= len(target_lut):
                raise RuntimeError("LAB E10 EoMT emitted an unknown category")
            target = target_lut[semantic].copy()
            target[~valid_mask] = 0
            if transform_target is not None:
                target = transform_target(target)
                if (
                    not isinstance(target, np.ndarray)
                    or target.dtype != np.uint8
                    or target.shape != valid_mask.shape
                ):
                    raise RuntimeError("semantic target transform returned an invalid mask")
            for name, value in measured.items():
                latency[name].append(float(value))
            finished = time.perf_counter()
            age = max(0.0, (finished - envelope.scheduled_monotonic) * 1000)
            latency["completion_age_ms"].append(age)
            latency["processing_ms"].append((finished - started) * 1000)
            counts = np.bincount(target[valid_mask], minlength=TARGET_CLASS_COUNT)
            result = SemanticResult(
                frame_index=envelope.frame_index,
                source_frame_index=int(envelope.timeline["source_frame_index"]),
                session_seconds=float(envelope.timeline["session_seconds"]),
                completion_age_ms=age,
                completed_monotonic=finished,
                mask=target,
                mask_sha256=hashlib.sha256(target.tobytes()).hexdigest(),
                class_pixels={
                    target_names[index]: int(counts[index])
                    for index in range(1, TARGET_CLASS_COUNT)
                    if int(counts[index]) > 0
                },
            )
            completed.append(result)
            latest.publish(result)
            if stop_after_results is not None and len(completed) >= stop_after_results:
                return
    except BaseException as exc:
        failures.append(exc)


def semantic_binding(
    semantic: SemanticResult | None, frame_seconds: float, ttl_ms: float
) -> tuple[str, float | None]:
    if semantic is None:
        return "unavailable", None
    age = max(0.0, (frame_seconds - semantic.session_seconds) * 1000)
    return ("fresh" if age <= ttl_ms else "stale"), age


def preflight(args: argparse.Namespace) -> int:
    import torch

    job = _validate_job(args.job.resolve(strict=True))
    profile, profile_sha256 = read_profile(args.profile)
    detector, detector_sha256 = read_detector_profile(args.detector_profile)
    semantic, semantic_sha256 = read_semantic_profile(args.semantic_profile)
    _validate_source(job, detector)
    validate_semantic_source(job, semantic)
    if profile["source"] != detector["source"]:
        raise RuntimeError("LAB E10 detector source binding changed")
    if profile["source"] != {
        key: semantic["source"][key]
        for key in ("source_id", "resolution", "calibration_slot", "calibration_sha256")
    }:
        raise RuntimeError("LAB E10 semantic source binding changed")
    lidar = LidarReplayPack(args.lidar_pack, expected_job_id=job["job_id"])
    try:
        if lidar.identity["calibration_sha256"] != profile["source"]["calibration_sha256"]:
            raise RuntimeError("LAB E10 LiDAR calibration binding changed")
        if profile["mode"] == "full-session-qualification":
            selection = profile["selection"]
            if (
                lidar.frame_count != int(selection["required_frame_count"])
                or int(lidar.source_frame_indices[0])
                != int(selection["required_source_start_frame_index"])
                or int(lidar.source_frame_indices[-1])
                != int(selection["required_source_end_frame_index"])
                or float(lidar.session_seconds[-1] - lidar.session_seconds[0])
                < float(selection["minimum_source_span_seconds"])
            ):
                raise RuntimeError("LAB E10 full-session LiDAR selection changed")
        _load_valid_fov(args.valid_fov_root, job, detector)
        detector_files = _verify_model(detector, args.model_root)
        dependency = _dependency_manifest(args.environment.resolve(strict=True))
        if dependency["identity"]["profile_sha256"] != semantic_sha256:
            raise RuntimeError("LAB E10 semantic dependency identity changed")
        if not torch.cuda.is_available():
            raise RuntimeError("CUDA is required for LAB E10")
        device = torch.device("cuda:0")
        processor, model, _labels, _lut, semantic_files = _load_model(
            semantic, args.cache.resolve(strict=True), device
        )
        del processor, model, _labels, _lut
        torch.cuda.empty_cache()
        print(
            json.dumps(
                {
                    "state": "preflight-ready",
                    "job_id": job["job_id"],
                    "profile_sha256": profile_sha256,
                    "detector_profile_sha256": detector_sha256,
                    "semantic_profile_sha256": semantic_sha256,
                    "lidar_pack_id": lidar.pack_id,
                    "detector_files": detector_files,
                    "semantic_files": semantic_files,
                    "cuda_device": torch.cuda.get_device_name(),
                },
                sort_keys=True,
            ),
            flush=True,
        )
    finally:
        lidar.close()
    return 0


def run(args: argparse.Namespace) -> int:
    import torch
    import transformers
    from PIL import Image
    from scipy.optimize import linear_sum_assignment

    if not _valid_sha256(args.orchestrator_sha256):
        raise RuntimeError("LAB E10 orchestrator SHA-256 is invalid")
    job = _validate_job(args.job.resolve(strict=True))
    profile, profile_sha256 = read_profile(args.profile)
    detector, detector_sha256 = read_detector_profile(args.detector_profile)
    semantic, semantic_sha256 = read_semantic_profile(args.semantic_profile)
    _validate_source(job, detector)
    validate_semantic_source(job, semantic)
    valid_mask, valid_fov = _load_valid_fov(args.valid_fov_root, job, detector)
    detector_files = _verify_model(detector, args.model_root)
    dependency = _dependency_manifest(args.environment.resolve(strict=True))
    if dependency["identity"]["profile_sha256"] != semantic_sha256:
        raise RuntimeError("LAB E10 semantic dependency identity changed")
    lidar = LidarReplayPack(args.lidar_pack, expected_job_id=job["job_id"])
    frames = sorted(args.frames.resolve(strict=True).glob("frame-*.png"))
    timeline_path = args.timeline.resolve(strict=True)
    timeline = _read_timeline(timeline_path, len(frames))
    if (
        len(frames) != lidar.frame_count
        or [int(row["source_frame_index"]) for row in timeline]
        != lidar.source_frame_indices.tolist()
        or not np.allclose(
            np.asarray([float(row["session_seconds"]) for row in timeline]),
            lidar.session_seconds,
            rtol=0,
            atol=1e-6,
        )
    ):
        lidar.close()
        raise RuntimeError("LAB E10 camera and LiDAR replay selections differ")
    if profile["mode"] == "full-session-qualification":
        selection = profile["selection"]
        source_span_seconds = float(timeline[-1]["session_seconds"]) - float(
            timeline[0]["session_seconds"]
        )
        if (
            len(frames) != int(selection["required_frame_count"])
            or int(timeline[0]["source_frame_index"])
            != int(selection["required_source_start_frame_index"])
            or int(timeline[-1]["source_frame_index"])
            != int(selection["required_source_end_frame_index"])
            or source_span_seconds < float(selection["minimum_source_span_seconds"])
        ):
            lidar.close()
            raise RuntimeError("LAB E10 full-session camera selection changed")
    output = args.output.resolve()
    if output.exists():
        raise RuntimeError("LAB E10 output must be absent")
    output.mkdir(mode=0o700, parents=True, exist_ok=False)
    assert_disk(output, args.free_bytes_floor, 0)

    device = torch.device("cuda:0")
    processor, semantic_model, _semantic_labels, target_lut, semantic_files = _load_model(
        semantic, args.cache.resolve(strict=True), device
    )
    target_names = {int(key): str(value) for key, value in semantic["target_taxonomy"].items()}
    infer_semantic = e9._semantic_infer_factory(processor, semantic_model, device)
    tracker = TwoStageTracker(detector["tracking"])
    linear_sum_assignment(np.zeros((1, 1), dtype=np.float64))
    with Image.open(frames[0]) as opened:
        warm = np.asarray(opened.convert("RGB"), dtype=np.uint8)
    _infer(args.triton_url, detector["model"], _preprocess(warm, valid_mask, detector))
    infer_semantic(np.where(valid_mask[..., None], warm, 0).astype(np.uint8))
    del warm

    replay = profile["replay"]
    detector_queue = LatestWinsQueue(int(replay["detector_queue_capacity"]))
    semantic_queue = LatestWinsQueue(int(replay["semantic_queue_capacity"]))
    latest = LatestSemantic()
    completed_semantics: list[SemanticResult] = []
    producer_errors: list[BaseException] = []
    semantic_errors: list[BaseException] = []
    semantic_latency = {
        name: []
        for name in (
            "queue_wait_ms",
            "valid_fov_fill_ms",
            "processor_ms",
            "host_to_device_ms",
            "forward_ms",
            "model_postprocess_ms",
            "processing_ms",
            "completion_age_ms",
        )
    }
    latency = {
        name: []
        for name in (
            "decode_ms",
            "queue_wait_ms",
            "detector_ms",
            "projection_ms",
            "association_ms",
            "clearance_ms",
            "world_state_ms",
            "world_state_age_ms",
        )
    }
    status_counts: Counter[str] = Counter()
    fusion_state_counts: Counter[str] = Counter()
    rejection_counts: Counter[str] = Counter()
    accepted_cuboids = 0
    fused_frames = 0
    detector_failures = 0
    fusion_freshness_violations = 0
    history = distance_history(int(profile["association"]["distance_history_frames"]))
    projector = WorldStateProjector(float(profile["world_state"]["velocity_history_limit_s"]))
    completion_tracker = (
        CuboidCompletionTracker(profile["cuboid_completion"])
        if "cuboid_completion" in profile
        else None
    )
    semantic_path = output / "semantic-frames.jsonl"
    fusion_path = output / "fusion-frames.jsonl"
    world_path = output / "world-state.jsonl"
    gpu_path = output / "gpu-telemetry.jsonl"
    support_offsets = [0]
    support_points: list[np.ndarray] = []
    support_colors: list[np.ndarray] = []
    box_offsets = [0]
    box_centers: list[tuple[float, float, float]] = []
    box_half_sizes: list[tuple[float, float, float]] = []
    box_quaternions: list[tuple[float, float, float, float]] = []
    box_colors: list[tuple[int, int, int, int]] = []
    frame_times_ns: list[int] = []
    disk_before = shutil.disk_usage(output).free

    with (
        semantic_path.open("x", encoding="utf-8", newline="\n") as semantic_stream,
        fusion_path.open("x", encoding="utf-8", newline="\n") as fusion_stream,
        world_path.open("x", encoding="utf-8", newline="\n") as world_stream,
        gpu_path.open("x", encoding="utf-8", newline="\n") as gpu_stream,
        _GpuTelemetry(gpu_stream, 1.0) as gpu,
    ):
        semantic_thread = threading.Thread(
            target=semantic_worker,
            kwargs={
                "queue": semantic_queue,
                "latest": latest,
                "valid_mask": valid_mask,
                "target_lut": target_lut,
                "target_names": target_names,
                "infer": infer_semantic,
                "latency": semantic_latency,
                "completed": completed_semantics,
                "failures": semantic_errors,
                "stop_after_results": (
                    int(profile["semantic_loss"]["stop_after_completed_results"])
                    if profile["mode"] == "semantic-loss-negative-control"
                    else None
                ),
            },
            name="lab-e10-semantic",
            daemon=True,
        )
        semantic_thread.start()
        replay_started = time.perf_counter() + 0.25
        producer = threading.Thread(
            target=e9._producer,
            kwargs={
                "detector_queue": detector_queue,
                "semantic_queue": semantic_queue,
                "semantic_stride": int(replay["semantic_sample_every_frames"]),
                "frame_paths": frames,
                "timeline_rows": timeline,
                "replay_started": replay_started,
                "speed": float(replay["speed"]),
                "error": producer_errors,
            },
            name="lab-e10-source",
            daemon=True,
        )
        producer.start()

        while (envelope := detector_queue.take()) is not None:
            started = time.perf_counter()
            latency["decode_ms"].append(envelope.decode_ms)
            latency["queue_wait_ms"].append(max(0.0, (started - envelope.decoded_monotonic) * 1000))
            try:
                detector_started = time.perf_counter()
                tensor = _preprocess(envelope.image, valid_mask, detector)
                output_tensor, _request_ms = _infer(args.triton_url, detector["model"], tensor)
                detections, _rejected = _detections(output_tensor, detector, valid_mask)
                tracks = tracker.update(detections, envelope.frame_index)
                latency["detector_ms"].append((time.perf_counter() - detector_started) * 1000)

                frame_seconds = float(envelope.timeline["session_seconds"])
                current_semantic = latest.snapshot()
                semantic_status, semantic_age = semantic_binding(
                    current_semantic,
                    frame_seconds,
                    float(replay["semantic_ttl_ms"]),
                )
                status_counts[semantic_status] += 1
                lidar_frame = lidar.frame(envelope.frame_index)
                fusions = ()
                points_lidar = np.empty((0, 3), dtype=np.float64)
                if lidar_frame is None:
                    fusion_state = "depth-unavailable-sync-gate"
                elif semantic_status != "fresh" or current_semantic is None:
                    fusion_state = f"semantic-{semantic_status}"
                else:
                    points_map, position, quaternion = lidar_frame
                    projection_started = time.perf_counter()
                    pixels, depths, source_indices, points_lidar = project_points(
                        points_map, position, quaternion, lidar.profile
                    )
                    latency["projection_ms"].append(
                        (time.perf_counter() - projection_started) * 1000
                    )
                    association_started = time.perf_counter()
                    fusions = fuse_tracks(
                        tracks=[_track_document(track) for track in tracks],
                        semantic_map=current_semantic.mask,
                        pixels=pixels,
                        depths=depths,
                        source_indices=source_indices,
                        points_map=points_map,
                        points_lidar=points_lidar,
                        association=profile["association"],
                        distance_history=history,
                        completion_tracker=completion_tracker,
                        sensor_position_map=position,
                        session_seconds=frame_seconds,
                    )
                    latency["association_ms"].append(
                        (time.perf_counter() - association_started) * 1000
                    )
                    fusion_state = "fused"
                    fused_frames += 1
                if fusion_state == "fused" and semantic_status != "fresh":
                    fusion_freshness_violations += 1
                fusion_state_counts[fusion_state] += 1
                accepted = [item for item in fusions if item.cuboid is not None]
                accepted_cuboids += len(accepted)
                for item in fusions:
                    rejection_counts[item.status] += 1
                frame_support = []
                frame_support_colors = []
                for item in accepted:
                    color_seed = hashlib.sha256(
                        f"e10:{item.association_group}:{item.track_id}".encode()
                    ).digest()
                    color = tuple(64 + value % 176 for value in color_seed[:3])
                    if lidar_frame is None:
                        continue
                    points_map = lidar_frame[0]
                    values = points_map[item.source_indices].astype(np.float32)
                    frame_support.append(values)
                    frame_support_colors.append(
                        np.tile(np.asarray([color], dtype=np.uint8), (values.shape[0], 1))
                    )
                    box_centers.append(item.cuboid.center_map)
                    box_half_sizes.append(item.cuboid.half_size)
                    box_quaternions.append(item.cuboid.quaternion_xyzw)
                    box_colors.append((*color, 88))
                if frame_support:
                    support = np.concatenate(frame_support)
                    colors = np.concatenate(frame_support_colors)
                    support_points.append(support)
                    support_colors.append(colors)
                    support_offsets.append(support_offsets[-1] + support.shape[0])
                else:
                    support_offsets.append(support_offsets[-1])
                box_offsets.append(box_offsets[-1] + len(accepted))
                clearance_started = time.perf_counter()
                clearance_state = clearance(points_lidar, profile["world_state"]["clearance"])
                latency["clearance_ms"].append((time.perf_counter() - clearance_started) * 1000)
                world_started = time.perf_counter()
                result_age = max(0.0, (world_started - envelope.scheduled_monotonic) * 1000)
                if result_age >= 1000:
                    health = "unavailable"
                elif result_age >= float(profile["acceptance"]["maximum_p95_world_state_age_ms"]):
                    health = "stale"
                elif fusion_state != "fused":
                    health = "degraded"
                else:
                    health = "healthy"
                delivery = {
                    "health": health,
                    "result_age_ms": result_age,
                    "semantic_status": semantic_status,
                    "semantic_source_age_ms": semantic_age,
                }
                world = projector.project(
                    frame_index=envelope.frame_index,
                    source_frame_index=int(envelope.timeline["source_frame_index"]),
                    session_seconds=frame_seconds,
                    fusion_state=fusion_state,
                    fusions=fusions,
                    points_lidar=points_lidar,
                    clearance_state=clearance_state,
                    delivery=delivery,
                )
                latency["world_state_ms"].append((time.perf_counter() - world_started) * 1000)
                result_age = max(0.0, (time.perf_counter() - envelope.scheduled_monotonic) * 1000)
                world["delivery"]["result_age_ms"] = result_age
                latency["world_state_age_ms"].append(result_age)
                frame_times_ns.append(round(frame_seconds * 1e9))
                fusion_stream.write(
                    json.dumps(
                        {
                            "schema_version": FUSION_SCHEMA,
                            "frame_index": envelope.frame_index,
                            "source_frame_index": int(envelope.timeline["source_frame_index"]),
                            "session_seconds": frame_seconds,
                            "fusion_state": fusion_state,
                            "semantic_status": semantic_status,
                            "semantic_source_frame_index": None
                            if current_semantic is None
                            else current_semantic.source_frame_index,
                            "objects": [fusion_document(item) for item in fusions],
                        },
                        sort_keys=True,
                        separators=(",", ":"),
                        allow_nan=False,
                    )
                    + "\n"
                )
                world_stream.write(
                    json.dumps(world, sort_keys=True, separators=(",", ":"), allow_nan=False) + "\n"
                )
            except Exception:
                detector_failures += 1
                raise
            consumed = int(detector_queue.snapshot()["consumed"])
            if consumed % 100 == 0:
                fusion_stream.flush()
                world_stream.flush()
                assert_disk(output, args.free_bytes_floor, consumed)
                print(
                    json.dumps(
                        {
                            "phase": "e10-integrated",
                            "processed": consumed,
                            "detector_dropped": detector_queue.snapshot()["dropped_overflow"],
                            "semantic_processed": semantic_queue.snapshot()["consumed"],
                            "fused_frames": fused_frames,
                            "accepted_cuboids": accepted_cuboids,
                        },
                        sort_keys=True,
                    ),
                    flush=True,
                )
        producer.join(timeout=5)
        semantic_thread.join(timeout=30)
        if producer.is_alive() or producer_errors:
            raise RuntimeError("LAB E10 producer failed")
        if semantic_thread.is_alive() or semantic_errors:
            raise RuntimeError("LAB E10 semantic worker failed")
        for result in completed_semantics:
            semantic_stream.write(
                json.dumps(
                    {
                        "schema_version": SEMANTIC_SCHEMA,
                        "frame_index": result.frame_index,
                        "source_frame_index": result.source_frame_index,
                        "session_seconds": result.session_seconds,
                        "completion_age_ms": result.completion_age_ms,
                        "mask_sha256": result.mask_sha256,
                        "class_pixels": result.class_pixels,
                    },
                    sort_keys=True,
                    separators=(",", ":"),
                    allow_nan=False,
                )
                + "\n"
            )
        for stream in (semantic_stream, fusion_stream, world_stream):
            stream.flush()
            os.fsync(stream.fileno())

    replay_finished = time.perf_counter()
    detector_state = detector_queue.snapshot()
    semantic_state = semantic_queue.snapshot()
    source_span = (
        float(timeline[-1]["session_seconds"]) - float(timeline[0]["session_seconds"])
    ) / float(replay["speed"])
    replay_wall = replay_finished - replay_started
    detector_fps = int(detector_state["consumed"]) / max(source_span, replay_wall, 1e-9)
    semantic_fps = int(semantic_state["consumed"]) / max(source_span, replay_wall, 1e-9)
    scheduled_semantic = ((len(frames) - 1) // int(replay["semantic_sample_every_frames"])) + 1
    fresh_coverage = status_counts["fresh"] / max(1, int(detector_state["consumed"]))
    latency_summary = {name: _percentiles(values) for name, values in latency.items()}
    semantic_summary = {name: _percentiles(values) for name, values in semantic_latency.items()}

    arrays_path = output / "transient-perception.npz"
    np.savez_compressed(
        arrays_path,
        frame_times_ns=np.asarray(frame_times_ns, dtype=np.int64),
        semantic_frame_indices=np.asarray(
            [value.frame_index for value in completed_semantics], dtype=np.int64
        ),
        semantic_masks=np.stack([value.mask for value in completed_semantics]).astype(np.uint8),
        support_offsets=np.asarray(support_offsets, dtype=np.int64),
        support_points=np.concatenate(support_points)
        if support_points
        else np.empty((0, 3), dtype=np.float32),
        support_colors=np.concatenate(support_colors)
        if support_colors
        else np.empty((0, 3), dtype=np.uint8),
        box_offsets=np.asarray(box_offsets, dtype=np.int64),
        box_centers=np.asarray(box_centers, dtype=np.float32).reshape((-1, 3)),
        box_half_sizes=np.asarray(box_half_sizes, dtype=np.float32).reshape((-1, 3)),
        box_quaternions=np.asarray(box_quaternions, dtype=np.float32).reshape((-1, 4)),
        box_colors=np.asarray(box_colors, dtype=np.uint8).reshape((-1, 4)),
    )
    acceptance = profile["acceptance"]
    detector_checks = {
        "detector_accounting": int(detector_state["consumed"])
        + int(detector_state["dropped_overflow"])
        == len(frames),
        "detector_minimum_effective_fps": detector_fps
        >= float(acceptance["detector_minimum_effective_fps"]),
        "detector_maximum_drop_fraction": int(detector_state["dropped_overflow"]) / len(frames)
        <= float(acceptance["detector_maximum_drop_fraction"]),
        "maximum_p95_world_state_age_ms": float(latency_summary["world_state_age_ms"]["p95"])
        <= float(acceptance["maximum_p95_world_state_age_ms"]),
        "zero_detector_failures": detector_failures == 0 and not producer_errors,
    }
    if profile["mode"] == "semantic-loss-negative-control":
        stop_after = int(profile["semantic_loss"]["stop_after_completed_results"])
        checks = {
            **detector_checks,
            "semantic_loss_triggered": len(completed_semantics) == stop_after,
            "semantic_queue_accounting": int(semantic_state["consumed"])
            + int(semantic_state["dropped_overflow"])
            + int(semantic_state["final_depth"])
            == scheduled_semantic,
            "minimum_stale_detector_frames": status_counts["stale"]
            >= int(acceptance["minimum_stale_detector_frames"]),
            "no_fusion_with_nonfresh_semantics": fusion_freshness_violations == 0,
            "semantic_worker_stopped_without_error": not semantic_errors,
        }
    else:
        checks = {
            **detector_checks,
            "semantic_accounting": int(semantic_state["consumed"])
            + int(semantic_state["dropped_overflow"])
            == scheduled_semantic,
            "semantic_minimum_effective_fps": semantic_fps
            >= float(acceptance["semantic_minimum_effective_fps"]),
            "semantic_maximum_drop_fraction": int(semantic_state["dropped_overflow"])
            / scheduled_semantic
            <= float(acceptance["semantic_maximum_drop_fraction"]),
            "semantic_maximum_p95_completion_age_ms": float(
                semantic_summary["completion_age_ms"]["p95"]
            )
            <= float(acceptance["semantic_maximum_p95_completion_age_ms"]),
            "minimum_fresh_semantic_coverage": fresh_coverage
            >= float(acceptance["minimum_fresh_semantic_coverage"]),
            "minimum_lidar_fused_frames": fused_frames
            >= int(acceptance["minimum_lidar_fused_frames"]),
            "minimum_accepted_cuboids": accepted_cuboids
            >= int(acceptance["minimum_accepted_cuboids"]),
            "zero_semantic_failures": not semantic_errors,
        }
    accepted = all(checks.values())
    identity = {
        "schema_version": IDENTITY_SCHEMA,
        "job_id": job["job_id"],
        "input_sha256": job["input_sha256"],
        "session_id": job["input"]["session_id"],
        "source_id": job["input"]["source_id"],
        "lidar_pack_id": lidar.pack_id,
        "selection": {
            "frame_count": len(frames),
            "source_start_frame_index": timeline[0]["source_frame_index"],
            "source_end_frame_index": timeline[-1]["source_frame_index"],
            "timeline_start_seconds": timeline[0]["session_seconds"],
            "timeline_end_seconds": timeline[-1]["session_seconds"],
            "timeline_sha256": sha256(timeline_path),
        },
        "configuration": {
            "pipeline": PIPELINE_ID,
            "profile": profile,
            "profile_sha256": profile_sha256,
            "detector_profile_sha256": detector_sha256,
            "semantic_profile_sha256": semantic_sha256,
            "runner_sha256": sha256(Path(__file__).resolve(strict=True)),
            "fusion_runtime_sha256": sha256(
                Path(__file__).with_name("e10_fusion_runtime.py").resolve(strict=True)
            ),
            "orchestrator_sha256": args.orchestrator_sha256,
            "container_image": args.container_image,
            "valid_fov": valid_fov,
        },
        "models": {
            "detector": detector["model"],
            "detector_files": detector_files,
            "semantic": semantic["model"],
            "semantic_files": semantic_files,
        },
    }
    identity_sha256 = hashlib.sha256(canonical_json(identity)).hexdigest()
    result_id = f"e10-integrated-perception-{identity_sha256}"
    metrics = {
        "source_span_seconds": source_span,
        "replay_wall_seconds": replay_wall,
        "detector": {
            "frames_processed": detector_state["consumed"],
            "frames_dropped": detector_state["dropped_overflow"],
            "effective_fps": detector_fps,
            "queue": detector_state,
        },
        "semantic": {
            "frames_processed": semantic_state["consumed"],
            "frames_dropped": semantic_state["dropped_overflow"],
            "effective_fps": semantic_fps,
            "fresh_coverage": fresh_coverage,
            "status_counts": dict(status_counts),
            "latency_ms": semantic_summary,
        },
        "fusion": {
            "fused_frames": fused_frames,
            "fusion_state_counts": dict(fusion_state_counts),
            "accepted_cuboids": accepted_cuboids,
            "rejection_counts": dict(rejection_counts),
            "freshness_violations": fusion_freshness_violations,
        },
        "semantic_loss": {
            "configured": profile["mode"] == "semantic-loss-negative-control",
            "stop_after_completed_results": (
                int(profile["semantic_loss"]["stop_after_completed_results"])
                if profile["mode"] == "semantic-loss-negative-control"
                else None
            ),
            "triggered": (
                len(completed_semantics)
                == int(profile["semantic_loss"]["stop_after_completed_results"])
                if profile["mode"] == "semantic-loss-negative-control"
                else False
            ),
        },
        "latency_ms": latency_summary,
        "gpu_telemetry": gpu.summary(),
        "process_peak_rss_mib": resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024,
        "cuda_peak_memory_allocated_mib": torch.cuda.max_memory_allocated() / 2**20,
        "cuda_peak_memory_reserved_mib": torch.cuda.max_memory_reserved() / 2**20,
        "disk": {
            "free_bytes_before": disk_before,
            "free_bytes_after": shutil.disk_usage(output).free,
            "free_bytes_floor": args.free_bytes_floor,
        },
    }
    report = {
        "schema_version": REPORT_SCHEMA,
        "result_id": result_id,
        "created_at_utc": datetime.now(UTC)
        .isoformat(timespec="milliseconds")
        .replace("+00:00", "Z"),
        "state": "accepted" if accepted else "rejected",
        "ground_truth": False,
        "identity": identity,
        "runtime": {
            "hostname": platform.node(),
            "python": platform.python_version(),
            "numpy": np.__version__,
            "torch": torch.__version__,
            "transformers": transformers.__version__,
            "scipy": importlib.metadata.version("scipy"),
            "gpu": torch.cuda.get_device_name(),
        },
        "metrics": metrics,
        "acceptance": {
            "accepted": accepted,
            "checks": checks,
            "navigation_or_safety_accepted": False,
        },
        "limitations": [
            "Recorded source-paced replay, not direct live K1 transport.",
            "Host-arrival camera/LiDAR synchronization is not a hardware-clock proof.",
            "COCO and Cityscapes models are not forest-domain or safety validated.",
            (
                "Completed 3D cuboids mix visible LiDAR support with class-size priors; "
                "unobserved volume is inferred, not measured or ground truth."
                if "cuboid_completion" in profile
                else (
                    "3D cuboids describe visible LiDAR-supported surfaces, "
                    "not complete object volume."
                )
            ),
            *(
                ["Semantic inference was intentionally stopped for a negative-control run."]
                if profile["mode"] == "semantic-loss-negative-control"
                else []
            ),
        ],
    }
    report_path = output / "run-report.json"
    _write_json(report_path, report)
    artifacts = [
        _artifact(semantic_path, "e10-semantic-frames", "application/x-ndjson", SEMANTIC_SCHEMA),
        _artifact(fusion_path, "e10-fusion-frames", "application/x-ndjson", FUSION_SCHEMA),
        _artifact(world_path, "e10-world-state", "application/x-ndjson", WORLD_SCHEMA),
        _artifact(arrays_path, "e10-transient-perception", "application/x-npz"),
        _artifact(gpu_path, "worker-gpu-telemetry", "application/x-ndjson"),
        _artifact(report_path, "e10-run-report", "application/json", REPORT_SCHEMA),
    ]
    result = {
        "schema_version": RESULT_SCHEMA,
        "result_id": result_id,
        "identity_sha256": identity_sha256,
        "identity": identity,
        "created_at_utc": report["created_at_utc"],
        "acceptance_state": report["state"],
        "ground_truth": False,
        "publication_scope": (
            "recorded-integrated-semantic-loss-negative-control-only"
            if profile["mode"] == "semantic-loss-negative-control"
            else "recorded-integrated-realtime-qualification-only"
        ),
        "frames_processed": detector_state["consumed"],
        "artifacts": artifacts,
    }
    _write_json(output / "result.json", result)
    lidar.close()
    print(
        json.dumps(
            {
                "result_id": result_id,
                "accepted": accepted,
                "detector_fps": detector_fps,
                "semantic_fps": semantic_fps,
                "world_state_age_p95_ms": latency_summary["world_state_age_ms"]["p95"],
                "fused_frames": fused_frames,
                "accepted_cuboids": accepted_cuboids,
            },
            sort_keys=True,
        ),
        flush=True,
    )
    return 0


def assert_disk(path: Path, floor: int, frame: int) -> None:
    if floor < 0 or (floor and shutil.disk_usage(path).free < floor):
        raise RuntimeError(f"LAB E10 crossed the D-backed free-space floor at frame {frame}")


def main() -> int:
    args = arguments()
    return preflight(args) if args.command == "preflight" else run(args)


if __name__ == "__main__":
    raise SystemExit(main())
