# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-contract-net-bid-receipts-from-certifying-work-the-bidder-never-actually-committed-to.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-contract-net-bid-receipts-from-certifying-work-the-bidder-never-actually-committed-to.warrant.json
# Markdown SHA-256:   6a3d7063fea2dbead06ff4dae0c47142bda32c79a92b1325f6b6546c34565877
# Code AST SHA-256:   13f6b76d6e24dec83fd945d5acefffba01667ec0c11ccbd48bd78b30b7fab3b3
# Verified Classes:   ReceiptBindingError, Award, Receipt
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass


class ReceiptBindingError(RuntimeError):
    """Raised when an outcome receipt is not bound to the manager's canonical spec."""


@dataclass(frozen=True)
class Award:
    spec_hash: str          # H(canonical S), set by the manager
    worker_id: str
    echoed_restatement: str # bidder-authored; MUST NOT be a verification anchor


@dataclass(frozen=True)
class Receipt:
    worker_id: str
    spec_hash: str          # what the worker claims it evaluated against
    deliverable_hash: str
    signature_valid: bool


def verify_receipt(award: Award, receipt: Receipt, deliverable_bytes: bytes) -> None:
    # Authenticity checks (necessary, not sufficient).
    if not receipt.signature_valid:
        raise ReceiptBindingError("receipt signature invalid")
    if receipt.worker_id != award.worker_id:
        raise ReceiptBindingError(
            f"receipt worker {receipt.worker_id!r} != awarded worker {award.worker_id!r}"
        )

    # Binding check: the receipt must be anchored to the manager's canonical spec,
    # NOT to the bidder-authored restatement echoed in the award.
    if receipt.spec_hash != award.spec_hash:
        raise ReceiptBindingError(
            "receipt bound to a non-canonical spec (likely a bidder restatement); "
            f"receipt.spec_hash={receipt.spec_hash} award.spec_hash={award.spec_hash}"
        )

    # Deliverable integrity check.
    actual = hashlib.sha256(deliverable_bytes).hexdigest()
    if receipt.deliverable_hash != actual:
        raise ReceiptBindingError(
            f"deliverable hash mismatch: receipt={receipt.deliverable_hash} actual={actual}"
        )

    # The echoed_restatement field is deliberately never consulted here.
    return None
