# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-delegated-warrant-attenuation-as-a-hard-invariant-across-a-federated-trust-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-delegated-warrant-attenuation-as-a-hard-invariant-across-a-federated-trust-boundary.warrant.json
# Markdown SHA-256:   d64f002653ffd535c6c482fdb012bb36b1430aaf60fe31fd5e40add482ab85d2
# Code AST SHA-256:   c41ef0ae5541f134c44f27bb87226db3b03387b3d8cd65b8ad2d248ca802e361
# Verified Classes:   WarrantAmplificationError, ConstraintDimension
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class WarrantAmplificationError(RuntimeError):
    """Raised when a delegation would amplify authority beyond the inbound warrant."""
    pass


class ConstraintDimension:
    def __init__(self, name, kind, value):
        self.name = name
        self.kind = kind          # "numeric_max" | "set_membership" | "opaque"
        self.value = value

    def subset_of(self, other):
        # Returns True if self is a subset (more restrictive or equal) of other.
        if self.kind != other.kind:
            return None  # incomparable -> caller must deny
        if self.kind == "numeric_max":
            return self.value <= other.value
        if self.kind == "set_membership":
            return set(self.value).issubset(set(other.value))
        # opaque / free-form: cannot mechanically compare
        return None


def attenuate(inbound, outbound_intent, delegator, delegatee):
    """Enforce warrant(outbound) subset-of warrant(inbound) on every dimension.

    inbound:          dict name -> ConstraintDimension held by delegator
    outbound_intent:  dict name -> ConstraintDimension the delegator wants to grant
    """
    for name, out_dim in outbound_intent.items():
        if name not in inbound:
            raise WarrantAmplificationError(
                f"delegation {delegator}->{delegatee}: outbound dimension "
                f"'{name}' has no inbound counterpart; cannot prove subset"
            )
        result = out_dim.subset_of(inbound[name])
        if result is None:
            raise WarrantAmplificationError(
                f"delegation {delegator}->{delegatee}: dimension '{name}' "
                f"is incomparable (kind {out_dim.kind}); deny by default"
            )
        if result is False:
            raise WarrantAmplificationError(
                f"delegation {delegator}->{delegatee}: dimension '{name}' "
                f"amplifies {out_dim.value!r} beyond inbound {inbound[name].value!r}"
            )
    return outbound_intent


def verify_chain(chain, originator_policy):
    """Originator re-derives constraints across full chain; rejects any hop violation."""
    current = originator_policy
    for hop in chain:
        try:
            current = attenuate(current, hop["granted"], hop["from"], hop["to"])
        except WarrantAmplificationError as e:
            raise WarrantAmplificationError(
                f"originator rejected result: chain violation at {hop['from']}->{hop['to']}: {e}"
            )
    return current


# Reproducing the observed failure:
origin_policy = {
    "scope":        ConstraintDimension("scope", "opaque", "summarize"),
    "max_records":  ConstraintDimension("max_records", "numeric_max", 500),
    "jurisdiction": ConstraintDimension("jurisdiction", "set_membership", {"EU"}),
}
b_to_c_grant = {
    "scope":        ConstraintDimension("scope", "opaque", "fetch"),
    "max_records":  ConstraintDimension("max_records", "numeric_max", 10000),
    "jurisdiction": ConstraintDimension("jurisdiction", "set_membership", {"US"}),
}

if __name__ == "__main__":
    # Delegator B attempts to sub-delegate; attenuation must block it.
    attenuate(origin_policy, b_to_c_grant, "B(org-2)", "C(org-3)")
    # -> raises WarrantAmplificationError on 'max_records' (10000 > 500)
    #    and would raise on 'jurisdiction' ({US} not subset of {EU})
    #    and on 'scope' (opaque -> incomparable -> deny)
