# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-re-attest-a-delegated-warrant-after-a-mid-chain-identity-refresh-rather-than-silently-re-signing-it-under.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-re-attest-a-delegated-warrant-after-a-mid-chain-identity-refresh-rather-than-silently-re-signing-it-under.warrant.json
# Markdown SHA-256:   a96f6f753e5155695f9809b3482c2cf091cee3254a01d2cb8e4da5e508883a2f
# Code AST SHA-256:   5e4dafcffc26a4243e4834b3326cfa3f554869c49fd49c34438b82e8447d75e4
# Verified Classes:   EpochCrossingAttestationError, Warrant, IdentityEpoch
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class EpochCrossingAttestationError(RuntimeError):
    """Raised when a re-attestation request would cross an identity-epoch
    boundary without a valid rotation proof, producing a warrant whose
    signature and issuer claims disagree across verifier postures."""


@dataclass(frozen=True)
class Warrant:
    warrant_id: str
    iss: str          # issuer identity
    sub: str          # subject identity
    scope: str
    iat: int          # issued-at epoch
    cnf: str          # confirmation key binding
    signature_key_id: str


@dataclass(frozen=True)
class IdentityEpoch:
    key_id: str
    not_before: int
    successor_of: Optional[str] = None  # key_id of predecessor, if rotation


def re_attest(
    warrant: Warrant,
    signing_epoch: IdentityEpoch,
    rotation_proof_present: bool,
    preserve_original_iss: bool,
) -> Warrant:
    same_key = signing_epoch.key_id == warrant.signature_key_id
    is_successor = signing_epoch.successor_of == warrant.signature_key_id

    if same_key:
        # Idempotent re-attestation: claims and identity binding unchanged.
        return Warrant(
            warrant_id=warrant.warrant_id,
            iss=warrant.iss,
            sub=warrant.sub,
            scope=warrant.scope,
            iat=warrant.iat,
            cnf=warrant.cnf,
            signature_key_id=signing_epoch.key_id,
        )

    if not is_successor:
        raise EpochCrossingAttestationError(
            f"signing key {signing_epoch.key_id} is neither the original "
            f"signing key {warrant.signature_key_id} nor a documented successor"
        )

    if not rotation_proof_present:
        raise EpochCrossingAttestationError(
            f"successor key {signing_epoch.key_id} presented without a "
            f"rotation proof linking it to {warrant.signature_key_id}; "
            f"refusing cross-epoch re-attestation"
        )

    if preserve_original_iss:
        raise EpochCrossingAttestationError(
            "re-attestation handler asked to preserve original iss/iat across "
            "an epoch boundary; this is re-delegation and must not be performed "
            "by the re-attestation path"
        )

    # Correct path: re-delegate with rewritten identity-binding fields.
    return Warrant(
        warrant_id=f"{warrant.warrant_id}.reissued",
        iss=signing_epoch.key_id,
        sub=warrant.sub,
        scope=warrant.scope,
        iat=signing_epoch.not_before,
        cnf=warrant.cnf,
        signature_key_id=signing_epoch.key_id,
    )
