# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-reject-a-delegated-aaif-credential-whose-delegation-chain-is-structurally-valid-but-whose-attenuation-proof-was-minted-against-a.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-reject-a-delegated-aaif-credential-whose-delegation-chain-is-structurally-valid-but-whose-attenuation-proof-was-minted-against-a.warrant.json
# Markdown SHA-256:   a4e4327a10fa2431eee004c7fb4f4d10a721a9025a357308826a4c67bd6ee277
# Code AST SHA-256:   6de2f13f719c14cbf643ec9d0e9627d285832310e944a6e09cf6c6f772cc62af
# Verified Classes:   SchemaBindingError, Link
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from __future__ import annotations

from dataclasses import dataclass
from typing import Optional


class SchemaBindingError(RuntimeError):
    """Raised when an AAIF delegation link cannot be soundly evaluated
    because its attenuation proof was minted against a schema version
    that does not match the parent link's bound schema."""


@dataclass(frozen=True)
class Link:
    issuer: str
    subject: str
    schema_version: Optional[str]   # None means the issuer omitted it
    grant_commitment: str           # hash of canonical grant bytes
    attenuation_proof: str


def _schema_pinned(link: Link) -> bool:
    return link.schema_version is not None and link.schema_version != ""


def verify_attenuation(parent: Link, child: Link, local_schemas: set[str]) -> None:
    """Verify that `child`'s attenuation proof is sound relative to `parent`.

    Structural checks (signatures, iss/sub linkage, root anchor) are assumed
    already performed by the caller. This function enforces the schema-binding
    invariant that structural verification does not cover.
    """
    if not _schema_pinned(parent):
        # No safe default: an unpinned parent cannot bound the child's proof.
        raise SchemaBindingError(
            f"parent link {parent.issuer}->{parent.subject} has no bound "
            f"schema_version; attenuation proof from {child.issuer} is "
            f"unverifiable and must be rejected"
        )

    if not _schema_pinned(child):
        raise SchemaBindingError(
            f"child link {child.issuer}->{child.subject} omits schema_version; "
            f"cannot confirm the proof was minted under {parent.schema_version!r}"
        )

    if child.schema_version != parent.schema_version:
        # Schema drift across a hop. Do NOT reinterpret parent under child's
        # schema -- capability subset rules are not monotone across versions.
        if parent.schema_version not in local_schemas:
            raise SchemaBindingError(
                f"schema mismatch at hop {parent.subject}->{child.subject}: "
                f"parent bound to {parent.schema_version!r}, child proof minted "
                f"under {child.schema_version!r}, and parent schema is not "
                f"locally available for re-derivation; refusing"
            )
        raise SchemaBindingError(
            f"schema mismatch at hop {parent.subject}->{child.subject}: "
            f"parent bound to {parent.schema_version!r}, child proof minted "
            f"under {child.schema_version!r}; quarantine and re-derive under "
            f"the parent schema before executing"
        )

    # Same schema on both sides: the subset proof is comparable. The caller's
    # canonical-grant commitment check ensures the parent bytes were not
    # reinterpreted between proof generation and verification.
    return None
