# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-contract-net-award-whose-attestation-verifies-against-a-stale-outcome-digest-rather-than-the-current-task.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-contract-net-award-whose-attestation-verifies-against-a-stale-outcome-digest-rather-than-the-current-task.warrant.json
# Markdown SHA-256:   1905d7dc97167f93d085af0ce1ea622b24baf822f60cf744cb8d24685cdddd49
# Code AST SHA-256:   5edb87e4d34e23925294fcca9e273dd85e7431009b5972cd3cfd2044217c5051
# Verified Classes:   AttestationRejected, AwardVerdict, CFP, Attestation
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum
from typing import Optional


class AttestationRejected(RuntimeError):
    """Raised when an attestation cannot be used to support an award."""
    pass


class AwardVerdict(Enum):
    ACCEPT = "accept"
    STALE = "stale"          # valid proof, wrong epoch/spec
    RETRACTED = "retracted"  # valid proof, dead auditor root


@dataclass(frozen=True)
class CFP:
    task_epoch: int
    spec_digest: str
    live_auditor_roots: frozenset


@dataclass(frozen=True)
class Attestation:
    issuer: str
    task_epoch: int
    spec_digest: str
    outcome_digest: str
    auditor_root: str
    signature_valid: bool
    proof_valid: bool


def evaluate_award(
    cfp: CFP,
    att: Attestation,
    compatibility_edges: dict,  # (from_epoch, to_epoch) -> bool, signed by manager
) -> AwardVerdict:
    # Hard gate: cryptographic validity. A forged attestation is a different
    # failure class and must never be silently downgraded to STALE.
    if not att.signature_valid or not att.proof_valid:
        raise AttestationRejected(
            f"attestation from {att.issuer} failed cryptographic verification; "
            f"this is a forgery-class rejection, not a staleness rejection"
        )

    # Invariant (3): root liveness. A rotated root is revoked for new awards.
    if att.auditor_root not in cfp.live_auditor_roots:
        return AwardVerdict.RETRACTED

    # Invariant (1): spec pinning.
    if att.spec_digest != cfp.spec_digest:
        # Invariant (2): epoch monotonicity requires an explicit signed edge.
        edge = (att.task_epoch, cfp.task_epoch)
        if compatibility_edges.get(edge) is True:
            return AwardVerdict.ACCEPT
        return AwardVerdict.STALE

    # Spec matches; still require epoch match or signed compatibility edge.
    if att.task_epoch != cfp.task_epoch:
        edge = (att.task_epoch, cfp.task_epoch)
        if compatibility_edges.get(edge) is True:
            return AwardVerdict.ACCEPT
        return AwardVerdict.STALE

    return AwardVerdict.ACCEPT


def award_contract(cfp: CFP, att: Attestation, compatibility_edges: dict) -> str:
    verdict = evaluate_award(cfp, att, compatibility_edges)
    if verdict is AwardVerdict.STALE:
        raise AttestationRejected(
            f"attestation from {att.issuer} is valid but inapplicable: "
            f"spec_digest={att.spec_digest} epoch={att.task_epoch} "
            f"does not match CFP spec_digest={cfp.spec_digest} epoch={cfp.task_epoch}; "
            f"require fresh attestation or signed compatibility edge"
        )
    if verdict is AwardVerdict.RETRACTED:
        raise AttestationRejected(
            f"attestation from {att.issuer} roots to auditor root "
            f"{att.auditor_root} which is no longer live; the acceptance it "
            f"proves has been retracted and cannot justify a new award"
        )
    # Only ACCEPT reaches here; write the award record with the attestation
    # digest pinned so downstream memory distillation cannot re-use it
    # against a future epoch without re-evaluation.
    return f"awarded:{att.issuer}:epoch{cfp.task_epoch}:{att.outcome_digest}"
