# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-resolves-a-bridged-did-across-two-aaif-trust-roots-that-disagree-on-key-rotation-epoch.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-resolves-a-bridged-did-across-two-aaif-trust-roots-that-disagree-on-key-rotation-epoch.warrant.json
# Markdown SHA-256:   1f804db450507c6878296c7ba92366ab8555a5e7476d543c508b11c9d12c5e88
# Code AST SHA-256:   6b545ca4d7fb9c8408edc4a7c90be0b05a4dcc46afae7810ec6bb6943a35371d
# Verified Classes:   BridgeEpochIncoherence
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class BridgeEpochIncoherence(RuntimeError):
    """Raised when a bridge assertion resolves to a non-maximal epoch across trust roots."""
    pass


def verify_bridged_warrant(
    warrant,
    bridge_assertion,
    root_views,          # dict: root_name -> {"epoch": int, "history": list[dict]}
    skew_tolerance_epochs: int = 0,
):
    """
    Enforce epoch-maximal bridging for a delegated warrant.

    root_views[root]["history"] entries: {"epoch": int, "reason": str|None}
    bridge_assertion: {"epoch": int, "root": str}
    """
    max_epoch = max(v["epoch"] for v in root_views.values())
    bridge_epoch = bridge_assertion["epoch"]
    bridge_root = bridge_assertion["root"]

    # Invariant 1: bridge must reference an epoch within skew tolerance of the max.
    if max_epoch - bridge_epoch > skew_tolerance_epochs:
        raise BridgeEpochIncoherence(
            f"bridge epoch {bridge_epoch} (root={bridge_root}) lags max epoch "
            f"{max_epoch} by {max_epoch - bridge_epoch} > tolerance "
            f"{skew_tolerance_epochs}; refusing historical-key acceptance"
        )

    # Invariant 2: no compromise-reasoned rotation between bridge epoch and max.
    for root, view in root_views.items():
        for entry in view.get("history", []):
            if bridge_epoch < entry["epoch"] <= max_epoch:
                if entry.get("reason") == "keyCompromise":
                    raise BridgeEpochIncoherence(
                        f"key used by warrant (epoch {bridge_epoch}) was revoked "
                        f"for compromise at epoch {entry['epoch']} on root {root}; "
                        f"warrant must be rejected"
                    )

    return {"accepted": True, "epoch": bridge_epoch, "max_epoch": max_epoch}
