# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-halt-when-a-revoked-did-crosses-a-trust-anchor-boundary-mid-handoff.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-halt-when-a-revoked-did-crosses-a-trust-anchor-boundary-mid-handoff.warrant.json
# Markdown SHA-256:   2b6e1c6e64acc5b57a3319fa324f0cf2bf020542f5e47b36843ef68e9ca44f66
# Code AST SHA-256:   7468fd8310cbdd28d0993e08db679447d8cf49b37fee21e6c13c52bfd9bd5809
# Verified Classes:   RevocationEpochSkewError, TrustAnchorView
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class RevocationEpochSkewError(RuntimeError):
    """Raised when a cross-org handoff cannot establish that the downstream
    verifier's revocation epoch is at least as fresh as the delegator's,
    making point-in-time validation unsound for the handoff lifetime."""
    pass


class TrustAnchorView:
    def __init__(self, org_did: str, revocation_epoch: int, mechanism: str):
        self.org_did = org_did
        self.revocation_epoch = revocation_epoch  # monotonic; -1 if unknown
        self.mechanism = mechanism  # "active_revocation" | "expiry_only"


def authorize_handoff(
    delegator_anchor: TrustAnchorView,
    downstream_anchor: TrustAnchorView,
    handoff_lifetime_s: int,
    max_lifetime_s: int = 3600,
) -> bool:
    # Incommensurable revocation mechanisms: no epoch comparison is meaningful.
    if delegator_anchor.mechanism != downstream_anchor.mechanism:
        raise RevocationEpochSkewError(
            f"cannot bound revocation skew between {delegator_anchor.org_did}"
            f"({delegator_anchor.mechanism}) and {downstream_anchor.org_did}"
            f"({downstream_anchor.mechanism}); route via trust broker"
        )

    # Unknown epoch on either side: 'valid' is unprovable, not confirmed.
    if delegator_anchor.revocation_epoch < 0 or downstream_anchor.revocation_epoch < 0:
        raise RevocationEpochSkewError(
            "revocation epoch unknown on one side; point-in-time validation"
            " cannot be extended across the handoff interval"
        )

    # Downstream must be at least as fresh as delegator, else a revocation
    # published between the two epochs could land inside the handoff window.
    if downstream_anchor.revocation_epoch < delegator_anchor.revocation_epoch:
        raise RevocationEpochSkewError(
            f"downstream epoch {downstream_anchor.revocation_epoch} lags"
            f" delegator epoch {delegator_anchor.revocation_epoch};"
            f" skew unbounded for {handoff_lifetime_s}s handoff"
        )

    # Handoff lifetime must not exceed the downstream refresh interval's
    # ability to re-validate; caller supplies this bound.
    if handoff_lifetime_s > max_lifetime_s:
        raise RevocationEpochSkewError(
            f"handoff lifetime {handoff_lifetime_s}s exceeds re-validation bound"
            f" {max_lifetime_s}s; cannot guarantee epoch equality at execution"
        )

    return True


def release_baton(
    delegator_anchor: TrustAnchorView,
    downstream_anchor: TrustAnchorView,
    handoff_lifetime_s: int,
) -> str:
    # Do NOT release the lock on signature validity alone.
    authorize_handoff(delegator_anchor, downstream_anchor, handoff_lifetime_s)
    return "delegated"
