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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
"""Canonical, fail-closed scientific protocol checks for E-Clock X0."""

from __future__ import annotations

import hashlib
import json
import math
from collections.abc import Iterable, Mapping
from pathlib import Path
from typing import Any, cast

import jsonschema
import numpy as np
import pandas as pd
from pandas.api.types import is_integer_dtype
from torch import nn

from e_jepa_ttc.artifacts.hashing import compute_file_hash, verify_artifact_hash
from e_jepa_ttc.evaluation.garl_ttc_protocol import BUCKETS, PAPER_MID_WEIGHTS

PRODUCTION_ROW_COUNT = 8192
PRODUCTION_FOLDS = (0, 1, 2)
EXECUTABLE_ARMS = ("X0-A5-REPLAY", "X0-PAIR-U", "X0-BASE-U", "X0-DYN-U")
REFERENCE_FAMILIES = (
    "official_a5_oof",
    "official_c2f_oof",
    "nested_router_retrained_a5_constituent",
    "nested_router_retrained_c2f_constituent",
    "prospective_router_r",
)
IDENTITY_HASH_FIELDS = ("sample_token", "sequence_id", "track_id")
ROW_LEVEL_OOF_COLUMNS = (
    "sample_token",
    "sequence_id",
    "track_id",
    "outer_fold",
    "target_ttc_s",
    "target_benchmark_phase",
    "predicted_benchmark_phase",
    "predicted_inverse_ttc_raw",
    "predicted_ttc_raw",
    "predicted_ttc_clipped",
    "is_clip_saturated",
    "scientific_mid_per_row",
    "scientific_failure",
    "sample_weight",
    "arm_id",
    "seed",
    "checkpoint_sha256",
    "config_sha256",
    "protocol_sha256",
    "cache_manifest_sha256",
    "split_manifest_sha256",
)


def read_official_a5_csv(path: Path) -> pd.DataFrame:
    """Read the historical A5 CSV with its signed parser semantics.

    The frozen ``prediction_sha256`` was produced with pandas' high-precision
    parser.  Runner-produced OOF CSVs instead require ``round_trip`` parsing;
    conflating those two artifact classes changes float64 identity by one ULP.
    """

    return pd.read_csv(path, float_precision="high")


def canonical_records_hash(frame: pd.DataFrame, columns: Iterable[str]) -> str:
    """Hash ordered typed records after stable sample-token sorting."""

    names = tuple(columns)
    missing = sorted(set(names) - set(frame.columns))
    if missing:
        raise ValueError(f"hash columns missing: {missing}")
    ordered = frame.sort_values("sample_token", kind="stable").loc[:, names]
    records = ordered.to_dict(orient="records")
    encoded = json.dumps(
        records,
        ensure_ascii=False,
        allow_nan=False,
        sort_keys=True,
        separators=(",", ":"),
    ).encode("utf-8")
    return hashlib.sha256(encoded).hexdigest()


def tensor_state_sha256(module: nn.Module) -> str:
    """Hash ordered state names, shapes, dtypes and physical tensor bytes."""

    digest = hashlib.sha256()
    for name, tensor in sorted(module.state_dict().items()):
        contiguous = tensor.detach().cpu().contiguous()
        digest.update(name.encode("utf-8"))
        digest.update(str(tuple(contiguous.shape)).encode("ascii"))
        digest.update(str(contiguous.dtype).encode("ascii"))
        digest.update(contiguous.numpy().tobytes())
    return digest.hexdigest()


def module_topology_sha256(module: nn.Module) -> str:
    """Hash learnable parameter names and shapes without their values."""

    payload = [
        {"name": name, "shape": list(parameter.shape), "requires_grad": parameter.requires_grad}
        for name, parameter in module.named_parameters()
    ]
    return hashlib.sha256(
        json.dumps(payload, sort_keys=True, separators=(",", ":")).encode("utf-8")
    ).hexdigest()


def load_signed_json(path: Path, *, schema_path: Path | None = None) -> dict[str, Any]:
    """Load one signed JSON artifact and optionally apply its closed schema."""

    payload = json.loads(path.read_text(encoding="utf-8"))
    if not isinstance(payload, dict) or not verify_artifact_hash(payload):
        raise ValueError(f"artifact signature mismatch: {path}")
    if schema_path is not None:
        schema = json.loads(schema_path.read_text(encoding="utf-8"))
        jsonschema.Draft202012Validator(schema).validate(payload)
    return payload


def validate_protocol_reference_binding(
    protocol: Mapping[str, Any],
    reference: Mapping[str, Any],
    *,
    protocol_path: Path | None = None,
) -> None:
    """Validate the exact two-way identity used by every scientific operation."""

    if protocol.get("artifact_type") != "scientific_recovery_v9_eclock_protocol_v2":
        raise ValueError("unsupported E-Clock protocol artifact type")
    if reference.get("artifact_type") != "eclock_x0_reference_v2":
        raise ValueError("unsupported E-Clock reference artifact type")
    if not verify_artifact_hash(dict(protocol)) or not verify_artifact_hash(dict(reference)):
        raise ValueError("protocol/reference signature mismatch")
    protocol_record = reference.get("protocol")
    if not isinstance(protocol_record, Mapping):
        raise ValueError("reference lacks protocol binding")
    if protocol_record.get("artifact_sha256") != protocol.get("artifact_sha256"):
        raise ValueError("reference is bound to a different protocol artifact")
    if protocol_path is not None:
        if protocol_record.get("file_sha256") != compute_file_hash(str(protocol_path)):
            raise ValueError("reference is bound to a different protocol physical file")
        if protocol_record.get("bytes") != protocol_path.stat().st_size:
            raise ValueError("reference protocol byte count mismatch")
    registry = reference.get("reference_family_registry")
    families = reference.get("families")
    if registry != list(REFERENCE_FAMILIES) or not isinstance(families, Mapping):
        raise ValueError("reference family registry mismatch")
    if set(families) != set(REFERENCE_FAMILIES):
        raise ValueError("reference must contain exactly five families")
    for name in REFERENCE_FAMILIES:
        family = families.get(name)
        if not isinstance(family, Mapping) or family.get("reference_family") != name:
            raise ValueError("reference family key/identity mismatch")


def require_reference_family(
    reference: Mapping[str, Any], reference_family: str
) -> Mapping[str, Any]:
    """Return one exact family, rejecting aliases and family substitution."""

    if reference_family not in REFERENCE_FAMILIES:
        raise ValueError(f"unknown reference_family: {reference_family}")
    families = reference.get("families")
    if not isinstance(families, Mapping):
        raise ValueError("reference families are missing")
    family = families.get(reference_family)
    if not isinstance(family, Mapping) or family.get("reference_family") != reference_family:
        raise ValueError("reference family identity mismatch")
    return family


def _as_finite_float64(frame: pd.DataFrame, column: str) -> np.ndarray:
    values = np.asarray(pd.to_numeric(frame[column], errors="coerce"), dtype=np.float64)
    if not np.isfinite(values).all():
        raise ValueError(f"{column} contains non-finite values")
    return values


def _as_strict_bool(frame: pd.DataFrame, column: str) -> np.ndarray:
    values = frame[column]
    if not all(isinstance(value, (bool, np.bool_)) for value in values.tolist()):
        raise ValueError(f"{column} must contain booleans")
    return values.to_numpy(dtype=bool)


def _target_phase(target_ttc_s: np.ndarray, delta_t_s: float) -> np.ndarray:
    valid = (target_ttc_s < 0.0) | (target_ttc_s > delta_t_s)
    if not valid.all():
        raise ValueError("target TTC is outside the canonical benchmark-phase domain")
    with np.errstate(divide="raise", invalid="raise", over="raise"):
        phase = -np.log1p(-delta_t_s / target_ttc_s)
    if not np.isfinite(phase).all():
        raise ValueError("target benchmark phase is non-finite")
    return phase


def _prediction_coordinates(
    predicted_phase: np.ndarray, delta_t_s: float
) -> tuple[np.ndarray, np.ndarray]:
    with np.errstate(divide="ignore", invalid="ignore", over="ignore"):
        inverse = -np.expm1(-predicted_phase) / delta_t_s
        raw_ttc = np.reciprocal(inverse)
    if not np.isfinite(inverse).all() or not np.isfinite(raw_ttc).all():
        raise ValueError("predicted phase cannot be verified as finite inverse-TTC/raw TTC")
    return inverse, raw_ttc


def _bucket_names(target_ttc_s: np.ndarray) -> np.ndarray:
    result = np.full(target_ttc_s.shape, "", dtype=object)
    for name, lower, upper in BUCKETS:
        result[(target_ttc_s > lower) & (target_ttc_s <= upper)] = name
    if np.any(result == ""):
        raise ValueError("target lies outside the frozen signed TTC buckets")
    return result.astype(str)


def _require_single_string(frame: pd.DataFrame, column: str, expected: str) -> None:
    values = set(frame[column].astype(str))
    if values != {expected}:
        raise ValueError(f"{column} mismatch")


def precheck_production_oof(
    frame: pd.DataFrame,
    *,
    protocol: Mapping[str, Any],
    reference: Mapping[str, Any],
    arm_id: str,
    config_sha256: str,
    checkpoint_sha256_by_fold: Mapping[int, str],
) -> pd.DataFrame:
    """Validate one complete OOF frame against frozen, signed canonical identity."""

    validate_protocol_reference_binding(protocol, reference)
    registry = protocol.get("executable_arm_registry")
    if arm_id not in EXECUTABLE_ARMS or registry != list(EXECUTABLE_ARMS):
        raise ValueError("arm_id is not in the closed executable registry")
    if set(frame.columns) != set(ROW_LEVEL_OOF_COLUMNS):
        missing = sorted(set(ROW_LEVEL_OOF_COLUMNS) - set(frame.columns))
        extra = sorted(set(frame.columns) - set(ROW_LEVEL_OOF_COLUMNS))
        raise ValueError(f"row-level OOF schema mismatch; missing={missing}, extra={extra}")
    if len(frame) != int(protocol["production_row_count"]):
        raise ValueError("production OOF requires exactly 8,192 rows")
    tokens = cast(pd.Series, frame["sample_token"])
    if bool(tokens.isna().any()) or bool((tokens.astype(str).str.len() == 0).any()):
        raise ValueError("sample_token is absent")
    if bool(tokens.duplicated().any()):
        raise ValueError("sample_token is duplicated")
    if not is_integer_dtype(frame["outer_fold"].dtype):
        raise ValueError("outer_fold must have an integer dtype")
    if not is_integer_dtype(frame["seed"].dtype):
        raise ValueError("seed must have an integer dtype")
    folds = frame["outer_fold"].to_numpy(dtype=np.int64)
    if set(folds.tolist()) != set(PRODUCTION_FOLDS):
        raise ValueError("outer_fold universe must be exactly {0,1,2}")
    seeds = frame["seed"].to_numpy(dtype=np.int64)
    if set(seeds.tolist()) != {int(protocol["authorized_seed"])}:
        raise ValueError("seed mismatch")

    normalized = frame.copy()
    target_ttc = _as_finite_float64(normalized, "target_ttc_s")
    target_phase_supplied = _as_finite_float64(normalized, "target_benchmark_phase")
    predicted_phase = _as_finite_float64(normalized, "predicted_benchmark_phase")
    inverse_supplied = _as_finite_float64(normalized, "predicted_inverse_ttc_raw")
    raw_ttc_supplied = _as_finite_float64(normalized, "predicted_ttc_raw")
    clipped_supplied = _as_finite_float64(normalized, "predicted_ttc_clipped")
    mid_supplied = _as_finite_float64(normalized, "scientific_mid_per_row")
    sample_weight = _as_finite_float64(normalized, "sample_weight")
    if np.any(sample_weight <= 0.0):
        raise ValueError("sample_weight must be strictly positive")
    clip_flags = _as_strict_bool(normalized, "is_clip_saturated")
    failure_supplied = _as_strict_bool(normalized, "scientific_failure")

    metric = protocol["metric"]
    delta_t_s = float(metric["metric_delta_t_s"])
    clip_seconds = float(metric["deployment_ttc_clip_seconds"])
    minimum_abs_ttc = float(metric["minimum_abs_prediction_ttc_s"])
    target_phase = _target_phase(target_ttc, delta_t_s)
    inverse, raw_ttc = _prediction_coordinates(predicted_phase, delta_t_s)
    clipped = np.clip(raw_ttc, -clip_seconds, clip_seconds)
    saturated = np.abs(raw_ttc) > clip_seconds
    failure = ~np.isfinite(raw_ttc) | (np.abs(raw_ttc) < minimum_abs_ttc)
    scientific_mid = 1.0e4 * np.abs(target_phase - predicted_phase)
    if not np.isfinite(scientific_mid).all():
        raise ValueError("scientific MiD contains non-finite values")
    comparisons = (
        ("target_benchmark_phase", target_phase_supplied, target_phase),
        ("predicted_inverse_ttc_raw", inverse_supplied, inverse),
        ("predicted_ttc_raw", raw_ttc_supplied, raw_ttc),
        ("predicted_ttc_clipped", clipped_supplied, clipped),
        ("scientific_mid_per_row", mid_supplied, scientific_mid),
    )
    for name, supplied, recomputed in comparisons:
        if not np.allclose(supplied, recomputed, rtol=1.0e-12, atol=1.0e-12):
            raise ValueError(f"{name} disagrees with canonical float64 transformation")
    if not np.array_equal(clip_flags, saturated):
        raise ValueError("is_clip_saturated disagrees with raw TTC")
    if not np.array_equal(failure_supplied, failure):
        raise ValueError("scientific_failure disagrees with raw TTC")

    normalized["target_ttc_s"] = target_ttc
    normalized["target_benchmark_phase"] = target_phase
    normalized["predicted_benchmark_phase"] = predicted_phase
    normalized["predicted_inverse_ttc_raw"] = inverse
    normalized["predicted_ttc_raw"] = raw_ttc
    normalized["predicted_ttc_clipped"] = clipped
    normalized["is_clip_saturated"] = saturated
    normalized["scientific_mid_per_row"] = scientific_mid
    normalized["scientific_failure"] = failure
    normalized["sample_weight"] = sample_weight
    normalized["ttc_bucket"] = _bucket_names(target_ttc)

    sequences = normalized["sequence_id"].astype(str)
    expected_sequences = list(protocol["canonical_sequence_ids"])
    if sorted(sequences.unique().tolist()) != expected_sequences:
        raise ValueError("canonical sequence universe mismatch")
    sequence_to_fold = protocol["canonical_sequence_to_fold"]
    for sequence, expected_fold in sequence_to_fold.items():
        observed = set(normalized.loc[sequences == sequence, "outer_fold"].astype(int))
        if observed != {int(expected_fold)}:
            raise ValueError(f"canonical sequence-to-fold mismatch: {sequence}")
    expected_bucket_counts = protocol["canonical_bucket_counts_by_sequence"]
    for sequence in expected_sequences:
        subset = normalized.loc[sequences == sequence]
        observed = subset["ttc_bucket"].value_counts().to_dict()
        if observed != expected_bucket_counts[sequence]:
            raise ValueError(f"canonical sequence/bucket counts mismatch: {sequence}")

    hashes = protocol["canonical_hashes"]
    observed_hashes = {
        "token_identity_sha256": canonical_records_hash(
            normalized, ("sample_token", "sequence_id", "track_id")
        ),
        "target_sha256": canonical_records_hash(normalized, ("sample_token", "target_ttc_s")),
        "fold_assignment_sha256": canonical_records_hash(
            normalized, ("sample_token", "sequence_id", "outer_fold")
        ),
        "sample_weight_sha256": canonical_records_hash(
            normalized, ("sample_token", "sample_weight")
        ),
    }
    if observed_hashes != hashes:
        raise ValueError("self-consistent row hashes do not match the canonical protocol")

    _require_single_string(normalized, "arm_id", arm_id)
    _require_single_string(normalized, "config_sha256", config_sha256)
    _require_single_string(normalized, "protocol_sha256", str(protocol["artifact_sha256"]))
    _require_single_string(
        normalized,
        "cache_manifest_sha256",
        str(protocol["cache_binding"]["file_sha256"]),
    )
    _require_single_string(
        normalized,
        "split_manifest_sha256",
        str(protocol["split_binding"]["file_sha256"]),
    )
    if set(checkpoint_sha256_by_fold) != set(PRODUCTION_FOLDS):
        raise ValueError("exactly three checkpoint SHAs are required")
    for fold, expected_sha in checkpoint_sha256_by_fold.items():
        _require_single_string(
            normalized.loc[normalized["outer_fold"] == fold],
            "checkpoint_sha256",
            expected_sha,
        )
    return normalized


def production_sequence_macro_metrics(checked: pd.DataFrame) -> dict[str, Any]:
    """Aggregate canonical phase MiD directly, never through clipped TTC."""

    missing = {"sequence_id", "ttc_bucket", "scientific_mid_per_row"} - set(checked.columns)
    if missing:
        raise ValueError(f"checked OOF columns missing: {sorted(missing)}")
    per_sequence: dict[str, Any] = {}
    sequence_values: list[float] = []
    for sequence, subset in checked.groupby("sequence_id", sort=True):
        bins: dict[str, Any] = {}
        weighted = 0.0
        for bucket, _lower, _upper in BUCKETS:
            values = subset.loc[subset["ttc_bucket"] == bucket, "scientific_mid_per_row"].to_numpy(
                dtype=np.float64
            )
            if values.size == 0 or not np.isfinite(values).all():
                raise ValueError(f"invalid sequence/bucket MiD: {sequence}/{bucket}")
            mean = float(np.mean(values, dtype=np.float64))
            bins[bucket] = {"count": int(values.size), "mid": mean}
            weighted += PAPER_MID_WEIGHTS[bucket] * mean
        if not math.isfinite(weighted):
            raise ValueError(f"non-finite sequence MiD: {sequence}")
        per_sequence[str(sequence)] = {"paper_MiD_overall": weighted, "bins": bins}
        sequence_values.append(weighted)
    overall = float(np.mean(np.asarray(sequence_values, dtype=np.float64), dtype=np.float64))
    if not math.isfinite(overall):
        raise ValueError("sequence macro MiD is non-finite")
    return {
        "protocol": "garl_signed_v1_phase_direct_float64",
        "sequence_macro_paper_MiD_overall": overall,
        "per_sequence": per_sequence,
    }


def clipping_diagnostics(checked: pd.DataFrame) -> dict[str, Any]:
    """Report deployment clipping effects without influencing scientific MiD."""

    target = checked["target_benchmark_phase"].to_numpy(dtype=np.float64)
    raw_phase = checked["predicted_benchmark_phase"].to_numpy(dtype=np.float64)
    clipped_ttc = checked["predicted_ttc_clipped"].to_numpy(dtype=np.float64)
    delta_t_s = 0.1
    clipped_phase = _target_phase(clipped_ttc, delta_t_s)
    raw = 1.0e4 * np.abs(target - raw_phase)
    diagnostic = 1.0e4 * np.abs(target - clipped_phase)
    difference = diagnostic - raw
    return {
        "clip_fraction": float(
            np.mean(
                checked["is_clip_saturated"].to_numpy(dtype=np.float64),
                dtype=np.float64,
            )
        ),
        "mid_raw_mean_per_row": float(np.mean(raw, dtype=np.float64)),
        "mid_clipped_mean_per_row_diagnostic_only": float(np.mean(diagnostic, dtype=np.float64)),
        "delta_clipped_minus_raw_diagnostic_only": float(np.mean(difference, dtype=np.float64)),
        "rows_improved_by_clipping": int(np.count_nonzero(difference < 0.0)),
        "rows_worsened_by_clipping": int(np.count_nonzero(difference > 0.0)),
        "deployment_clipping_not_used_for_scientific_metric": True,
    }


__all__ = [
    "EXECUTABLE_ARMS",
    "IDENTITY_HASH_FIELDS",
    "PRODUCTION_FOLDS",
    "PRODUCTION_ROW_COUNT",
    "REFERENCE_FAMILIES",
    "ROW_LEVEL_OOF_COLUMNS",
    "canonical_records_hash",
    "clipping_diagnostics",
    "load_signed_json",
    "module_topology_sha256",
    "precheck_production_oof",
    "production_sequence_macro_metrics",
    "require_reference_family",
    "tensor_state_sha256",
    "validate_protocol_reference_binding",
]