# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-detect-scope-narrowing-collapse-in-delegated-aaif-credentials-when-a-cross-issuer-trust-chain-re-signs-a-warrant-under-a-wider.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-detect-scope-narrowing-collapse-in-delegated-aaif-credentials-when-a-cross-issuer-trust-chain-re-signs-a-warrant-under-a-wider.warrant.json
# Markdown SHA-256:   51506acfc4bee523cd43cf541e98be2509500629ffe6e93782a3e12d02f084eb
# Code AST SHA-256:   1086188f8db6c73c63531a07a7d36b694bb002d11eb6b76d03072a120237454b
# Verified Classes:   ScopeContainmentError, Predicate, Warrant
# Defensive Guards:   7 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional

class ScopeContainmentError(RuntimeError):
    """Raised when a re-signed delegated warrant's lowered predicate is not
    a subset of its parent's lowered predicate, or when containment is
    undecidable for the issuer pair."""
    pass

@dataclass(frozen=True)
class Predicate:
    action: str
    resource_set: frozenset[str]

@dataclass(frozen=True)
class Warrant:
    issuer: str
    parent_hash: Optional[str]
    predicate: Predicate
    lowering_fn_id: str
    signature_valid: bool

def _lower(pred: Predicate, ontology: dict[str, frozenset[str]]) -> frozenset[tuple[str, str]]:
    # Lower a predicate to (action, resource) pairs under a shared lattice.
    if pred.action not in ontology:
        raise ScopeContainmentError(
            f"action {pred.action!r} absent from shared lattice; containment undecidable"
        )
    resources = ontology[pred.action]
    resolved = pred.resource_set & resources
    if pred.resource_set - resources:
        raise ScopeContainmentError(
            f"resources {pred.resource_set - resources} unresolvable under lattice"
        )
    return frozenset((pred.action, r) for r in resolved)

def verify_delegation_hop(
    child: Warrant,
    parent: Warrant,
    ontology: dict[str, frozenset[str]],
) -> None:
    if not child.signature_valid or not parent.signature_valid:
        raise ScopeContainmentError("signature chain invalid")
    if child.parent_hash is None:
        raise ScopeContainmentError("child warrant missing parent binding")
    if child.lowering_fn_id != parent.lowering_fn_id:
        raise ScopeContainmentError(
            f"lowering function mismatch: {child.lowering_fn_id} vs {parent.lowering_fn_id}"
        )
    child_set = _lower(child.predicate, ontology)
    parent_set = _lower(parent.predicate, ontology)
    if not child_set.issubset(parent_set):
        widened = child_set - parent_set
        raise ScopeContainmentError(
            f"scope-narrowing collapse: child grants {sorted(widened)} not present in parent"
        )
    # containment proven; hop is safe to propagate

def admit_warrant_chain(
    chain: list[Warrant],
    ontology: dict[str, frozenset[str]],
) -> Warrant:
    if not chain:
        raise ScopeContainmentError("empty warrant chain")
    for parent, child in zip(chain, chain[1:]):
        verify_delegation_hop(child, parent, ontology)
    return chain[-1]
