# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-to-mint-a-delegated-warrant-when-its-own-aaif-scope-chain-contains-an-unverified-attenuation-hop.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-refuse-to-mint-a-delegated-warrant-when-its-own-aaif-scope-chain-contains-an-unverified-attenuation-hop.warrant.json
# Markdown SHA-256:   c449ba0bc692cea2081aaa7cc3c0daf77302b0ddacb38aa13af87853c36b3bf2
# Code AST SHA-256:   3c0f84890020e94e0adbcf3e24227897e2ab981db212bb43567f218dfcb69f3a
# Verified Classes:   UnverifiedAttenuationHop, Warrant
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class UnverifiedAttenuationHop(RuntimeError):
    """Raised when a warrant mint is attempted over a scope chain that the
    minting instance cannot independently re-verify to the root principal."""
    pass


class Warrant:
    def __init__(self, scope: frozenset, issuer: str, parent_hash: str | None,
                 attenuation_proof: dict | None):
        self.scope = scope
        self.issuer = issuer
        self.parent_hash = parent_hash
        self.attenuation_proof = attenuation_proof


def verify_chain_to_root(w: Warrant, known_issuer_keys: set[str],
                         fetch_parent) -> bool:
    """Walk parent_hash links. Return True only if every hop's issuer key is
    known and every attenuation_proof is present and well-formed."""
    cur = w
    seen = set()
    while cur.parent_hash is not None:
        if cur.parent_hash in seen:
            return False  # cycle in chain
        seen.add(cur.parent_hash)
        if cur.issuer not in known_issuer_keys:
            return False  # rotated-out or unknown issuer: unverifiable hop
        if cur.attenuation_proof is None:
            return False  # self-asserted scope, no proof
        parent = fetch_parent(cur.parent_hash)
        if parent is None:
            return False  # parent warrant unavailable -> cannot verify subset
        if not cur.scope.issubset(parent.scope):
            return False  # proof claims a subset that does not hold
        cur = parent
    return True


def mint_child_warrant(parent: Warrant, requested_scope: frozenset,
                       consumer_trusted: bool, known_issuer_keys: set[str],
                       fetch_parent) -> Warrant:
    # Invariant 1: chain must re-verify from the mint, not downstream.
    if not verify_chain_to_root(parent, known_issuer_keys, fetch_parent):
        raise UnverifiedAttenuationHop(
            f"refusing to mint: parent warrant issued by {parent.issuer!r} "
            f"has an unverifiable attenuation hop; downstream scope would be unbacked"
        )

    # Invariant 2: requested scope must be a verified subset of parent scope.
    if not requested_scope.issubset(parent.scope):
        raise UnverifiedAttenuationHop(
            f"refusing to mint: requested scope {sorted(requested_scope)} "
            f"is not a subset of verified parent scope {sorted(parent.scope)}"
        )

    # Invariant 3: untrusted consumers get a strict subset, never full parent scope.
    if not consumer_trusted and requested_scope == parent.scope:
        raise UnverifiedAttenuationHop(
            "refusing to mint: untrusted consumer requested full parent scope; "
            "strict narrowing required for out-of-trust-domain peers"
        )

    return Warrant(
        scope=requested_scope,
        issuer="self",
        parent_hash=hash((parent.issuer, tuple(sorted(parent.scope)))),
        attenuation_proof={"subset_of": sorted(parent.scope),
                           "requested": sorted(requested_scope)},
    )
