# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-delegated-verifiable-credential-whose-issuer-did-resolves-across-a-federation-trust-anchor-rotation-mi.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-delegated-verifiable-credential-whose-issuer-did-resolves-across-a-federation-trust-anchor-rotation-mi.warrant.json
# Markdown SHA-256:   71678a468096c555329c80a40fcb6a36c3441239ec52918ef0441a97ede33152
# Code AST SHA-256:   e707c210569f08bf19b96302354035dd43e9ebc6d779d1243e335e47f1a6582b
# Verified Classes:   DelegationIndeterminate, AnchorState, DelegatedCredential
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass
from typing import Optional


class DelegationIndeterminate(RuntimeError):
    """Raised when a delegated VC's anchor rotated mid-handoff without a continuity proof."""


@dataclass
class AnchorState:
    did: str
    current_keys: set[str]
    rotated_at: Optional[float] = None
    continuity_proof: Optional[dict] = None  # binds old_key -> new_key, signed by controller


@dataclass
class DelegatedCredential:
    issuer_did: str
    signing_key: str
    valid_from: float
    valid_until: float
    delegation_chain: list[str]


def verify_delegated_credential(
    vc: DelegatedCredential,
    anchor: AnchorState,
    resolution_time: float,
    max_handoff_skew: float = 30.0,
) -> str:
    if resolution_time < vc.valid_from or resolution_time > vc.valid_until + max_handoff_skew:
        return "reject: outside validity window"

    in_overlap = (
        anchor.rotated_at is not None
        and anchor.rotated_at <= resolution_time <= anchor.rotated_at + max_handoff_skew
    )

    if not in_overlap:
        if vc.signing_key in anchor.current_keys:
            return "accept"
        return "reject: key not authorized under current anchor"

    # Overlap regime: rotation event observed, credential signed under pre-rotation key.
    if anchor.continuity_proof is None:
        raise DelegationIndeterminate(
            f"anchor {anchor.did} rotated at {anchor.rotated_at} with no continuity proof; "
            f"credential signed by {vc.signing_key} is indeterminate — quarantine handoff"
        )

    bound_old = anchor.continuity_proof.get("old_key")
    bound_new = anchor.continuity_proof.get("new_key")
    if vc.signing_key != bound_old or bound_new not in anchor.current_keys:
        raise DelegationIndeterminate(
            f"continuity proof for {anchor.did} does not bind {vc.signing_key} to current anchor keys"
        )

    return "accept: continuity-preserving rotation verified"


if __name__ == "__main__":
    anchor = AnchorState(
        did="did:web:a.example",
        current_keys={"K_new"},
        rotated_at=1000.0,
        continuity_proof=None,  # revocation-by-rotation, no continuity
    )
    vc = DelegatedCredential(
        issuer_did="did:web:a.example",
        signing_key="K_old",
        valid_from=990.0,
        valid_until=1100.0,
        delegation_chain=["did:web:a.example:orch"],
    )
    verify_delegated_credential(vc, anchor, resolution_time=1005.0)
