# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-trust-broker-policy-translation-from-silently-widening-delegated-authority-across-federated-agent-runtimes.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-trust-broker-policy-translation-from-silently-widening-delegated-authority-across-federated-agent-runtimes.warrant.json
# Markdown SHA-256:   fbaa16cae7b03315c536b1a865c45a923e588cebe99dcfb35ff6ac852682f892
# Code AST SHA-256:   461c4e3f0f7d46d74dd284b9f2f0a1dcb0278568a2a67630923da06722497b20
# Verified Classes:   TranslationMonotonicityError, Warrant
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet

class TranslationMonotonicityError(RuntimeError):
    """Raised when a broker would emit a warrant whose authority is not a subset of the issuer's."""
    pass

@dataclass(frozen=True)
class Warrant:
    subject: str
    action: str
    resource: str
    constraints: FrozenSet[str]           # normalized predicate strings, e.g. "purpose=postmortem"
    unrepresentable: FrozenSet[str] = field(default_factory=frozenset)
    deny_on_unrepresentable: bool = False

    def effective_authority(self) -> FrozenSet[str]:
        # Dropped constraints widen authority; the unrepresentable set must be treated as
        # *absent* constraints only if the consumer is contractually bound to deny on them.
        if self.unrepresentable and not self.deny_on_unrepresentable:
            return self.constraints
        return self.constraints

def translate(issuer: Warrant, target_attrs: FrozenSet[str], target_predicates: FrozenSet[str]) -> Warrant:
    kept, dropped = set(), set()
    for c in issuer.constraints:
        name = c.split("=")[0].split("<")[0].split(">")[0]
        if name in target_attrs and name in target_predicates:
            kept.add(c)
        else:
            dropped.add(c)

    out = Warrant(
        subject=issuer.subject,
        action=issuer.action,
        resource=issuer.resource,
        constraints=frozenset(kept),
        unrepresentable=frozenset(dropped),
        deny_on_unrepresentable=bool(dropped),
    )

    # Invariant: translated authority must be a subset of issuer authority.
    # Dropped constraints enlarge the satisfying set unless the consumer denies on them.
    if dropped and not out.deny_on_unrepresentable:
        raise TranslationMonotonicityError(
            f"broker would widen authority: dropped {sorted(dropped)} "
            f"without deny_on_unrepresentable flag; issuer={issuer.subject}"
        )
    if not out.effective_authority().issubset(issuer.effective_authority()):
        raise TranslationMonotonicityError(
            f"translated constraint set not a subset of issuer set: "
            f"kept={sorted(kept)} issuer={sorted(issuer.constraints)}"
        )
    return out

def consumer_admit(w: Warrant, request_attrs: dict) -> bool:
    # Consumer contract: refuse execution when unrepresentable constraints exist.
    if w.unrepresentable and w.deny_on_unrepresentable:
        raise TranslationMonotonicityError(
            f"consumer refuses: warrant carries unrepresentable constraints {sorted(w.unrepresentable)}"
        )
    for c in w.constraints:
        if "=" in c:
            k, v = c.split("=", 1)
            if request_attrs.get(k) != v:
                return False
    return True
