# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-accept-a-delegated-credential-after-its-issuer-s-revocation-epoch.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-accept-a-delegated-credential-after-its-issuer-s-revocation-epoch.warrant.json
# Markdown SHA-256:   df51a9fe24dfe4a9d492bd3630c3dd2956102c5ec28e564042cfee0fb4ea4d69
# Code AST SHA-256:   5bc9746b596bf12336368b1ba3161ae3e28f7dd2d86985624c907497185e728c
# Verified Classes:   RevocationWatermarkError, Credential
# Defensive Guards:   6 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class RevocationWatermarkError(RuntimeError):
    """Raised when a verifier cannot prove its revocation epoch is current
    enough to authorize a delegated chain."""


@dataclass(frozen=True)
class Credential:
    token_id: str
    issuer: str
    parent_id: str | None
    parent_epoch: int          # issuer's epoch at issuance
    scope: str
    delegate_depth: int


def verify_chain(
    chain: list[Credential],
    revocation_epochs: dict[str, int],      # issuer -> epoch of list we hold
    revoked: dict[str, int],                # token_id -> epoch revoked
    presentation_epoch: int,
    max_skew: int,
) -> None:
    """Authorize a delegated chain, checking every link, not just the leaf."""
    for cred in chain:
        held = revocation_epochs.get(cred.issuer)
        if held is None:
            raise RevocationWatermarkError(
                f"no revocation list for issuer {cred.issuer}"
            )
        # Watermark must be provably current relative to presentation.
        if held < presentation_epoch - max_skew:
            raise RevocationWatermarkError(
                f"issuer {cred.issuer} watermark {held} older than "
                f"presentation {presentation_epoch} minus skew {max_skew}"
            )
        # The chain must not carry evidence of an unseen revocation.
        if cred.parent_epoch > held:
            raise RevocationWatermarkError(
                f"credential {cred.token_id} issued at epoch "
                f"{cred.parent_epoch} but verifier only knows epoch {held}"
            )
        # Full-chain revocation check: every link, not the leaf.
        revoked_at = revoked.get(cred.token_id)
        if revoked_at is not None and revoked_at <= held:
            raise RevocationWatermarkError(
                f"credential {cred.token_id} revoked at epoch {revoked_at}"
            )

    # Scope narrowing and depth are structural; checked after temporal soundness.
    for parent, child in zip(chain, chain[1:]):
        if child.scope not in parent.scope and not parent.scope.startswith(
            child.scope.split(":")[0]
        ):
            raise RevocationWatermarkError(
                f"scope {child.scope} not narrowed under {parent.scope}"
            )
        if child.delegate_depth >= parent.delegate_depth:
            raise RevocationWatermarkError(
                f"depth {child.delegate_depth} not below {parent.delegate_depth}"
            )
