# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-verifier-model-non-determinism-as-a-replay-divergence-fault-rather-than-plan-noise.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-verifier-model-non-determinism-as-a-replay-divergence-fault-rather-than-plan-noise.warrant.json
# Markdown SHA-256:   33354644604d46e610c5c2d4b1c24e74eb7d279eb9878516cf363752894ca45c
# Code AST SHA-256:   90f7b97a96628a7e84e40dd38d58cfb9de13447c61dfd18301e01d57047466d2
# Verified Classes:   ReplayDivergenceError, ReconstructionMismatchError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ReplayDivergenceError(RuntimeError):
    """Raised when an identically-parameterized replay of a committed
    branch produces a score vector that deviates beyond the calibrated
    batch-shape tolerance."""
    pass


class ReconstructionMismatchError(RuntimeError):
    """Raised when a replay cannot be parameterized identically to the
    commit record; this is reconstruction, not replay, and must not be
    classified as divergence."""
    pass


def classify_replay(
    commit_record: dict,
    replay_record: dict,
    calibrated_tolerance: float,
) -> str:
    """
    commit_record / replay_record keys:
      batch_membership: frozenset[str]
      precision_mode: str
      reduction_order: str
      scores: dict[str, float]   # branch_id -> verifier score
    """
    params = ("batch_membership", "precision_mode", "reduction_order")
    for p in params:
        if commit_record[p] != replay_record[p]:
            raise ReconstructionMismatchError(
                f"replay parameter {p!r} differs from commit record: "
                f"{commit_record[p]!r} != {replay_record[p]!r}; "
                f"this is a reconstruction, not a replay"
            )

    committed = commit_record["scores"]
    replayed = replay_record["scores"]
    if set(committed) != set(replayed):
        raise ReplayDivergenceError(
            f"replay scored a different branch set: "
            f"committed={sorted(committed)} replayed={sorted(replayed)}"
        )

    worst_branch, worst_delta = None, 0.0
    for branch, score in committed.items():
        delta = abs(replayed[branch] - score)
        if delta > worst_delta:
            worst_branch, worst_delta = branch, delta

    if worst_delta > calibrated_tolerance:
        raise ReplayDivergenceError(
            f"identically-parameterized replay diverged on branch "
            f"{worst_branch!r}: delta={worst_delta:.4f} exceeds "
            f"calibrated tolerance {calibrated_tolerance:.4f}"
        )
    return "replay_consistent"
