# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-to-attenuate-a-delegated-scope-when-the-parent-warrant-is-a-superset-of-the-child-s-capability-set.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-refuse-to-attenuate-a-delegated-scope-when-the-parent-warrant-is-a-superset-of-the-child-s-capability-set.warrant.json
# Markdown SHA-256:   741ccb103783a90625b7d493ba66185309719432660c1caac370f41657893223
# Code AST SHA-256:   5c49b0c3a953c58bc10b35381f524747ffbd75181c0981226b2a2f3d76b55e89
# Verified Classes:   DelegationIntegrityError, Warrant
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass
from typing import Optional


class DelegationIntegrityError(RuntimeError):
    """Raised when a child warrant's derivation cannot be uniquely reconstructed."""
    pass


@dataclass(frozen=True)
class Warrant:
    warrant_id: str
    parent_warrant_id: Optional[str]
    scope: frozenset
    requested_set_hash: Optional[str]
    minted_at: int
    valid_from: int
    valid_until: int


def attenuation_witness(scope: frozenset, requested: frozenset) -> str:
    payload = "|".join(sorted(scope)) + "::" + "|".join(sorted(requested))
    return hashlib.sha256(payload.encode()).hexdigest()


def verify_attenuation(child: Warrant, parent: Warrant) -> None:
    # Subset check is necessary but not the invariant.
    if not child.scope.issubset(parent.scope):
        raise DelegationIntegrityError(
            f"scope escape: {child.scope} not subset of {parent.scope}"
        )

    # The invariant: unique parent-instance binding + intersection witness.
    if child.parent_warrant_id != parent.warrant_id:
        raise DelegationIntegrityError(
            f"child {child.warrant_id} names parent {child.parent_warrant_id}, "
            f"not {parent.warrant_id}"
        )

    if child.requested_set_hash is None:
        raise DelegationIntegrityError(
            f"child {child.warrant_id} lacks requested_set_hash; "
            f"derivation is unauditable and not uniquely reconstructible"
        )

    if not (parent.valid_from <= child.minted_at <= parent.valid_until):
        raise DelegationIntegrityError(
            f"child {child.warrant_id} minted at {child.minted_at} outside "
            f"parent {parent.warrant_id} window "
            f"[{parent.valid_from},{parent.valid_until}]"
        )

    # Equal-scope hops are re-delegation, not attenuation; require re-auth.
    if child.scope == parent.scope:
        raise DelegationIntegrityError(
            f"child {child.warrant_id} does not attenuate parent "
            f"{parent.warrant_id}; equal-scope hop is privilege laundering"
        )


def audit_dedup_key(w: Warrant) -> str:
    # Key on the witness, not the scope, so distinct derivations survive.
    return f"{w.parent_warrant_id}:{w.requested_set_hash}:{sorted(w.scope)}"
