# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-cross-agent-timing-correlation-as-evidence-of-a-covert-channel-rather-than-benign-scheduling-jitter-and-w.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-cross-agent-timing-correlation-as-evidence-of-a-covert-channel-rather-than-benign-scheduling-jitter-and-w.warrant.json
# Markdown SHA-256:   ba65c3bd7eda66cb9f0939fd9f8483c1cb261037bbc4ff6ed9fce1d5016bf92b
# Code AST SHA-256:   3c2f01b0214701e41017ac806b4d343f1078ac3bca0bdcd0be6750187f2c14ee
# Verified Classes:   InadmissibleChannelVerdict, ChannelEvidence
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import math
from dataclasses import dataclass, field
from typing import Sequence


class InadmissibleChannelVerdict(RuntimeError):
    """Raised when a covert-channel verdict lacks the evidence required to be falsifiable."""


@dataclass
class ChannelEvidence:
    pair: tuple[str, str]
    raw_mutual_info_bits: float
    residual_mutual_info_bits: float
    null_mutual_info_bits: float          # permutation-null, sample-size corrected
    conditioning_set: Sequence[str] = field(default_factory=list)
    encoding_dimension: str | None = None  # "delay" | "ordering" | "equivalence" | "padding"
    cross_trust_boundary: bool = False
    sample_count: int = 0

    FREE_DIMENSIONS = {"delay", "ordering", "equivalence", "padding"}
    MIN_SAMPLES = 200


def adjudicate(ev: ChannelEvidence) -> str:
    """Return 'benign' | 'out_of_scope' | 'channel_suspected'. Raise if the verdict is inadmissible."""
    if not ev.conditioning_set:
        raise InadmissibleChannelVerdict(
            f"pair {ev.pair}: no conditioning set declared; residual correlation is unidentifiable"
        )
    if ev.sample_count < ChannelEvidence.MIN_SAMPLES:
        raise InadmissibleChannelVerdict(
            f"pair {ev.pair}: {ev.sample_count} samples below bias-correction floor "
            f"{ChannelEvidence.MIN_SAMPLES}; mutual-info estimate unreliable"
        )
    if ev.residual_mutual_info_bits <= ev.null_mutual_info_bits:
        return "benign"
    if not ev.cross_trust_boundary:
        return "out_of_scope"
    if ev.encoding_dimension not in ChannelEvidence.FREE_DIMENSIONS:
        raise InadmissibleChannelVerdict(
            f"pair {ev.pair}: residual {ev.residual_mutual_info_bits:.3f} bits exceeds null "
            f"{ev.null_mutual_info_bits:.3f} but no free encoding dimension named; "
            f"verdict would be unfalsifiable"
        )
    return "channel_suspected"
