# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-brokered-capability-grant-whose-warrant-chain-crosses-an-org-boundary-mid-flight.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-brokered-capability-grant-whose-warrant-chain-crosses-an-org-boundary-mid-flight.warrant.json
# Markdown SHA-256:   cd92769bf6faec37be975a595aa3fcd3e25b6d3fc84f0d76ccf66aff95e02bcb
# Code AST SHA-256:   e5e093b522a3f30d3128ddc358350e721690efb886a879d8bbc32c9907328c15
# Verified Classes:   WarrantAttenuationBreach
# Defensive Guards:   6 fail-closed invariants (zero external deps)

class WarrantAttenuationBreach(RuntimeError):
    """Raised when a brokered warrant's leaf constraints exceed the origin's
    constraint set, or when the chain contains an unaccounted lossy projection."""
    pass


def verify_brokered_warrant(chain, leaf_operation, leaf_constraints):
    """
    chain: list of hops, each {issuer, parent_warrant_hash, projection_lossy,
           dropped_fields, constraint_set_hash}
    leaf_operation: the capability the executing instance is about to exercise
    leaf_constraints: dict of constraints on the leaf warrant
    """
    if not chain:
        raise WarrantAttenuationBreach("empty warrant chain")

    origin = chain[0]
    origin_constraints = origin["constraint_set"]

    # 1. Every hop must anchor to a parent *warrant* hash, not a projection.
    for i, hop in enumerate(chain[1:], start=1):
        if hop.get("parent_warrant_hash") is None:
            raise WarrantAttenuationBreach(
                f"hop {i} ({hop['issuer']}) attenuated against a projection, "
                f"not a parent warrant; attenuation is unverifiable"
            )

    # 2. Any lossy projection whose dropped fields touch this operation
    #    invalidates the chain for that operation.
    for i, hop in enumerate(chain[1:], start=1):
        if hop.get("projection_lossy"):
            dropped = set(hop.get("dropped_fields", []))
            if dropped & set(leaf_operation.get("constraint_names", [])):
                raise WarrantAttenuationBreach(
                    f"hop {i} ({hop['issuer']}) dropped constraints "
                    f"{sorted(dropped)} that govern operation "
                    f"{leaf_operation['name']}; leaf is over-broad"
                )

    # 3. Leaf constraints must be a subset of origin constraints.
    for key, val in leaf_constraints.items():
        if key not in origin_constraints:
            raise WarrantAttenuationBreach(
                f"leaf constraint {key!r} absent from origin; "
                f"widening without origin authorization"
            )
        if origin_constraints[key] != val:
            raise WarrantAttenuationBreach(
                f"leaf constraint {key!r}={val!r} conflicts with "
                f"origin {origin_constraints[key]!r}"
            )

    # 4. Revocation reachability: origin must be able to reach the leaf.
    if not chain[-1].get("revocation_reachable_from_origin"):
        raise WarrantAttenuationBreach(
            "origin revocation channel does not reach leaf; "
            "no kill switch over downstream execution"
        )

    return True
