# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-of-a-revoked-federated-agent-identity-be-rejected-the-trust-mesh-propagation-invariant.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-of-a-revoked-federated-agent-identity-be-rejected-the-trust-mesh-propagation-invariant.warrant.json
# Markdown SHA-256:   3c87e0079634e096bf083cc83793fc8ee890557177c32d610d725f80109d6077
# Code AST SHA-256:   af51e650aaff44a55a6c7fc4c323cbee6b4d08cf5c5fbf4a9e60266890413bb6
# Verified Classes:   RevocationWatermarkError, RevocationRecord, Proof
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class RevocationWatermarkError(RuntimeError):
    """Raised when a verifier cannot establish a valid path watermark."""
    pass


@dataclass(frozen=True)
class RevocationRecord:
    issuer: str
    effective_time: float


@dataclass(frozen=True)
class Proof:
    issuer: str
    issuance_time: float
    path: tuple[str, ...]  # issuers from leaf to anchor


def compute_path_watermark(
    proof: Proof,
    watermarks: dict[str, float],
    strict: bool = True,
) -> float:
    missing = [issuer for issuer in proof.path if issuer not in watermarks]
    if missing:
        if strict:
            raise RevocationWatermarkError(
                f"no watermark for issuers on path: {missing}"
            )
        return 0.0
    return min(watermarks[issuer] for issuer in proof.path)


def must_reject(
    proof: Proof,
    revocations: list[RevocationRecord],
    watermarks: dict[str, float],
    strict: bool = True,
) -> bool:
    path_wm = compute_path_watermark(proof, watermarks, strict=strict)
    for rec in revocations:
        if rec.issuer not in proof.path:
            continue
        if proof.issuance_time <= rec.effective_time:
            continue
        if path_wm >= rec.effective_time:
            return True
        # watermark lags: under-propagated, caller decides
        if strict:
            raise RevocationWatermarkError(
                f"path watermark {path_wm} lags revocation effective "
                f"time {rec.effective_time} for issuer {rec.issuer}"
            )
    return False


def verify_or_raise(
    proof: Proof,
    revocations: list[RevocationRecord],
    watermarks: dict[str, float],
) -> None:
    if must_reject(proof, revocations, watermarks, strict=True):
        raise RevocationWatermarkError(
            f"proof from {proof.issuer} issued at {proof.issuance_time} "
            f"rejected under current path watermark"
        )


if __name__ == "__main__":
    now = time.time()
    revs = [RevocationRecord(issuer="A", effective_time=now - 100)]
    wms = {"A": now - 5, "B": now - 5, "C": now - 5}
    stale = {"A": now - 5, "B": now - 5, "C": now - 6 * 3600}
    proof = Proof(issuer="B", issuance_time=now - 50, path=("B", "A"))
    verify_or_raise(proof, revs, wms)  # ok: post-revocation proof rejected
    try:
        verify_or_raise(proof, revs, stale)
    except RevocationWatermarkError as exc:
        print("expected:", exc)
