# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-cross-org-attestation-chain-as-broken-rather-than-merely-stale-the-anchor-rotation-gap-in-federated-age.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-cross-org-attestation-chain-as-broken-rather-than-merely-stale-the-anchor-rotation-gap-in-federated-age.warrant.json
# Markdown SHA-256:   71e9e0490758f833e9b3b59ffba5cc86e02b3bcdfab45c4b3a216773baa7551f
# Code AST SHA-256:   5a50f05098f19c2e1e92406584e9d535d30cf9e4234e082fbb8eee742554b772
# Verified Classes:   AnchorGapError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class AnchorGapError(RuntimeError):
    """Raised when a receiver cannot resolve an attestation chain against its
    pinned trust anchor within the declared freshness/overlap bounds."""
    pass


def verify_cross_org_warrant(
    chain,
    pinned_anchor,
    *,
    anchor_staleness_s: float,
    max_staleness_s: float,
    overlap_window_s: float,
    refresh_anchor,
):
    # Invariant: staleness bound must be strictly less than the issuer's
    # declared rotation overlap, or the gap is unbounded and unresolvable.
    if max_staleness_s >= overlap_window_s:
        raise AnchorGapError(
            f"unbounded anchor gap: max_staleness_s={max_staleness_s} "
            f">= overlap_window_s={overlap_window_s}; federation link must not start"
        )

    # Row 1: verifies against current pinned anchor.
    if chain.verifies_against(pinned_anchor):
        return ("ACCEPT", {"anchor_age_s": anchor_staleness_s})

    # Row 2: verifies against a superseded anchor still inside the overlap window.
    if chain.verifies_against(pinned_anchor.superseded) and anchor_staleness_s < overlap_window_s:
        return ("ACCEPT", {"anchor_age_s": anchor_staleness_s, "superseded": True})

    # Row 3: unknown root carrying a continuity proof from the pinned anchor.
    if chain.root_continuity_proof and chain.root_continuity_proof.verify(pinned_anchor):
        return ("ACCEPT_PENDING_REFRESH", {"new_root": chain.root})

    # Rows 4/5: unknown root, no continuity proof.
    if anchor_staleness_s > max_staleness_s:
        # Cannot rule out forgery and we are past our freshness bound.
        raise AnchorGapError(
            f"unverifiable chain with stale anchor "
            f"(staleness={anchor_staleness_s}s > {max_staleness_s}s); treating as hostile"
        )

    # Within freshness bound: suspend, refresh out-of-band against the pinned
    # issuer endpoint (never against the presenter), then re-verify.
    refreshed = refresh_anchor(pinned_anchor.issuer_endpoint)
    if refreshed.matches(chain.root):
        return ("ACCEPT_AFTER_REFRESH", {"new_root": chain.root})
    if refreshed.matches(pinned_anchor):
        raise AnchorGapError(
            "chain root does not match issuer's published anchor after refresh; "
            "forged or mis-issued chain"
        )
    raise AnchorGapError("refresh returned unrecognized anchor; manual review required")
