# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-presents-a-delegated-warrant-whose-issuer-chain-crosses-a-federation-trust-anchor-rotation-mid-handoff.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-presents-a-delegated-warrant-whose-issuer-chain-crosses-a-federation-trust-anchor-rotation-mid-handoff.warrant.json
# Markdown SHA-256:   d0eef21c3bb2f1321edf8e55ff401fd3caa0a10721a4f074fae79ae6f02c9397
# Code AST SHA-256:   fc85976273900eda96f057a78f46c0028f877fe52d48c0dd8a6cccc1df14e4d3
# Verified Classes:   TrustAnchorRotationError, WarrantSupersededError, PolicyNarrowedError
# Defensive Guards:   6 fail-closed invariants (zero external deps)

class TrustAnchorRotationError(RuntimeError):
    """Raised when a verifiable presentation's authority chain cannot be
    reconciled with the relying party's current trust-anchor state."""
    pass


class WarrantSupersededError(TrustAnchorRotationError):
    """Retroactive rotation invalidated the warrant; presenting agent must re-mint."""
    pass


class PolicyNarrowedError(TrustAnchorRotationError):
    """Non-retroactive rotation: warrant valid, but asserted scope exceeds current policy."""
    pass


def verify_presentation(vp, relying_party_trust_store, now):
    chain = vp["credentialSubject"]["delegationChain"]
    asserted_scope = set(vp["credentialSubject"]["scope"])
    issuance_instant = vp["proof"]["created"]

    for link in chain:
        anchor_did = link["issuer"]
        signing_key = link["signingKeyId"]

        history = relying_party_trust_store.anchor_history(anchor_did)
        if history is None:
            raise TrustAnchorRotationError(
                f"no trust-anchor history for {anchor_did}; cannot reconcile "
                f"issuance {issuance_instant} against verifier-time {now}"
            )

        rotation = history.rotation_between(issuance_instant, now)
        if rotation is None:
            # Same-anchor regime: signing key must be current.
            if not history.is_current_key(signing_key):
                raise TrustAnchorRotationError(
                    f"key {signing_key} not current under {anchor_did} and no "
                    f"rotation recorded between {issuance_instant} and {now}"
                )
            continue

        if rotation.retroactive is None:
            # Absent metadata: fail closed as retroactive.
            raise WarrantSupersededError(
                f"anchor {anchor_did} rotated at {rotation.instant} with no "
                f"retroactivity metadata; treating as retroactive, warrant superseded"
            )

        if rotation.retroactive:
            raise WarrantSupersededError(
                f"anchor {anchor_did} rotated retroactively at {rotation.instant}; "
                f"warrant minted at {issuance_instant} is superseded, re-mint required"
            )

        # Non-retroactive: warrant stands, but must be re-scoped to current policy.
        if not rotation.was_key_valid_at(signing_key, issuance_instant):
            raise TrustAnchorRotationError(
                f"key {signing_key} was not valid at issuance {issuance_instant} "
                f"under {anchor_did}"
            )
        current_policy = rotation.current_policy_scope()
        granted = asserted_scope & current_policy
        if not granted:
            raise PolicyNarrowedError(
                f"asserted scope {asserted_scope} has empty intersection with "
                f"current policy {current_policy} under {anchor_did}"
            )
        asserted_scope = granted  # grant only the intersection

    return {"granted_scope": asserted_scope, "status": "accepted"}
