# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-to-inherit-a-delegated-warrant-when-its-provenance-chain-is-cryptographically-valid-but-semantically-unreconstructable.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-refuse-to-inherit-a-delegated-warrant-when-its-provenance-chain-is-cryptographically-valid-but-semantically-unreconstructable.warrant.json
# Markdown SHA-256:   561d05a8ba276b617a4217cd7789409f3dd3d15ebcb18f50e40894052c9346d3
# Code AST SHA-256:   1334a34153346e74a403c4c13ed7bd93a8304eab2ac827df2906e5f97d629d4e
# Verified Classes:   DelegationWarrantError, Warrant
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class DelegationWarrantError(RuntimeError):
    """Raised when a delegated warrant is cryptographically valid but semantically uninheritable."""
    pass


class Warrant:
    def __init__(self, root_intent: str, capability: str, scope: str,
                 nl_intent: str, parent: "Warrant | None" = None,
                 summarized_from: str | None = None):
        self.root_intent = root_intent
        self.capability = capability
        self.scope = scope
        self.nl_intent = nl_intent
        self.parent = parent
        self.summarized_from = summarized_from  # pre-image if this hop compressed the intent


CAPABILITY_LATTICE = {
    "read:contracts": {"read:contracts"},
    "read:documents": {"read:documents", "read:contracts"},
}


def capability_is_subset(leaf_cap: str, ancestor_cap: str) -> bool:
    return leaf_cap in CAPABILITY_LATTICE.get(ancestor_cap, {ancestor_cap})


def verify_inheritable(warrant: Warrant) -> None:
    """Fail closed on any hop where signature validity does not imply semantic safety."""
    node = warrant
    while node.parent is not None:
        # 1. Capability monotonicity: leaf must be a subset of ancestor.
        if not capability_is_subset(node.capability, node.parent.capability):
            raise DelegationWarrantError(
                f"capability widened across hop: {node.parent.capability!r} -> "
                f"{node.capability!r}; signature valid but authorization exceeds root grant"
            )
        # 2. Lossy hop: an intermediate rewrote the intent without preserving the pre-image.
        if node.summarized_from is None and node.nl_intent != node.parent.nl_intent:
            raise DelegationWarrantError(
                "intent rewritten without summarization pre-image; "
                "root intent not reconstructable from chain"
            )
        # 3. Representation divergence: structured scope absent from NL intent.
        if node.scope not in node.nl_intent and node.scope not in node.root_intent:
            raise DelegationWarrantError(
                f"scope {node.scope!r} present in structured field but absent from "
                "natural-language intent; downstream filters will not see the constraint"
            )
        node = node.parent


def execute_delegated(warrant: Warrant) -> str:
    verify_inheritable(warrant)
    return f"executed {warrant.capability} under root intent: {warrant.root_intent!r}"


# Reproduces the observed failure: root grants read:contracts, coordinator widens to
# read:documents and paraphrases intent. Signature chain is intact; inheritance must refuse.
root = Warrant(
    root_intent="summarize all customer contracts in tenant 4471 and extract renewal dates",
    capability="read:contracts", scope="tenant:4471",
    nl_intent="summarize all customer contracts in tenant 4471 and extract renewal dates",
)
coordinator = Warrant(
    root_intent=root.root_intent,
    capability="read:documents", scope="tenant:4471",
    nl_intent="retrieve contract documents for the customer and pull date fields",
    parent=root,
)
execute_delegated(coordinator)  # raises DelegationWarrantError before any read occurs
