# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-a-signed-skill-manifest-from-silently-admitting-a-revoked-tool-into-an-agent-s-active-toolset-during-transparency-log-equivoc.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-a-signed-skill-manifest-from-silently-admitting-a-revoked-tool-into-an-agent-s-active-toolset-during-transparency-log-equivoc.warrant.json
# Markdown SHA-256:   70cf62b54d9a8df869fd0bb0cb5349f33b585663326813934d4c1b4e081d7221
# Code AST SHA-256:   290fa40239abe7643ea39dcbdb7beeef57dfc23d4bc5d94e6b89f963e9924b57
# Verified Classes:   EquivocationSuspect, TreeHead, ConsistencyProof
# Defensive Guards:   6 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class EquivocationSuspect(RuntimeError):
    """Raised when a served tree head cannot be bound to a corroborated, fresh state."""
    pass


@dataclass
class TreeHead:
    seq: int
    root_hash: bytes
    timestamp: float
    operator_sig: bytes


@dataclass
class ConsistencyProof:
    from_seq: int
    to_seq: int
    hashes: list


DELTA_MAX_SECONDS = 300.0
CLOCK_SKEW = 30.0


def verify_head_binding(
    pinned: TreeHead,
    served: TreeHead,
    proof: ConsistencyProof,
    witness_root_hashes: set,
    now: float | None = None,
) -> str:
    now = now if now is not None else time.time()

    # Freshness: a stale head cannot corroborate revocation state.
    if now - served.timestamp > DELTA_MAX_SECONDS:
        raise EquivocationSuspect(
            f"served head seq={served.seq} age={now - served.timestamp:.1f}s "
            f"exceeds Δ_max={DELTA_MAX_SECONDS}s"
        )

    # Progress: forward sequence demands a non-trivial, matching consistency proof.
    if served.seq > pinned.seq:
        if proof.to_seq != served.seq or proof.from_seq != pinned.seq:
            raise EquivocationSuspect(
                f"consistency proof bounds {proof.from_seq}->{proof.to_seq} "
                f"do not match {pinned.seq}->{served.seq}"
            )
        if not proof.hashes:
            raise EquivocationSuspect(
                f"empty consistency proof for forward seq {pinned.seq}->{served.seq} "
                "(split-view equivocation fingerprint)"
            )
    elif served.seq < pinned.seq:
        raise EquivocationSuspect(
            f"served head seq={served.seq} regressed below pinned seq={pinned.seq}"
        )

    # Witness corroboration: root hash must agree with at least one independent source.
    if witness_root_hashes and served.root_hash not in witness_root_hashes:
        raise EquivocationSuspect(
            f"root hash {served.root_hash.hex()[:16]} not corroborated by any witness"
        )

    return "ok" if served.seq > pinned.seq else "degraded-freshness"


def materialize_toolset(
    manifest_validity_end: float,
    pinned: TreeHead,
    served: TreeHead,
    proof: ConsistencyProof,
    witness_root_hashes: set,
    now: float | None = None,
) -> str:
    now = now if now is not None else time.time()
    status = verify_head_binding(pinned, served, proof, witness_root_hashes, now)

    # Manifest cannot assert validity beyond what the head can corroborate.
    if manifest_validity_end > served.timestamp + CLOCK_SKEW:
        raise EquivocationSuspect(
            f"manifest validity_end={manifest_validity_end:.0f} exceeds "
            f"corroborated head timestamp={served.timestamp:.0f}"
        )

    if status == "degraded-freshness":
        return "read-only"
    return "full"
