# 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-scope-was-translated-across-an-organizational-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-brokered-capability-grant-whose-scope-was-translated-across-an-organizational-boundary.warrant.json
# Markdown SHA-256:   b05eea5ae0f881faf1af4313a254d14617ac304fa308488650100dc67977f1a1
# Code AST SHA-256:   0387a418ddad3cd3fef6f78f95faa9a2e320293e8107472b5775f311379a4dd5
# Verified Classes:   CapabilityTranslationError, OriginGrant, TranslatedGrant
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Optional


class CapabilityTranslationError(RuntimeError):
    """Raised when a brokered cross-org grant cannot be proven a subset of origin intent."""
    pass


@dataclass
class OriginGrant:
    action: str
    constraints: dict          # attribute predicates in origin dialect
    issuer: str


@dataclass
class TranslatedGrant:
    action: str
    constraints: dict          # target-dialect representation
    broker: str
    subset_proof: Optional[str] = None      # broker attestation of translated ⊑ origin
    origin_cosign: Optional[str] = None     # required for any widening mapping
    unmapped_constraints: list = field(default_factory=list)


def _is_subset(translated: TranslatedGrant, origin: OriginGrant) -> bool:
    # A translated grant is a subset only if every origin constraint is represented
    # and no translated constraint is weaker than its origin counterpart.
    for key, val in origin.constraints.items():
        if key not in translated.constraints:
            return False          # elision == widening, not subset
        if translated.constraints[key] != val:
            return False
    return True


def admit_federated_grant(origin: OriginGrant, translated: TranslatedGrant) -> None:
    if translated.unmapped_constraints:
        # Broker dropped a constraint it could not map. Elision widens scope.
        if not translated.origin_cosign:
            raise CapabilityTranslationError(
                f"Broker {translated.broker} elided constraints "
                f"{translated.unmapped_constraints} from origin grant by "
                f"{origin.issuer}; no origin co-sign. Refusing execution."
            )

    if not translated.subset_proof:
        raise CapabilityTranslationError(
            f"Broker {translated.broker} supplied no subset proof for "
            f"translated grant; cannot verify translated ⊑ origin. Refusing."
        )

    if not _is_subset(translated, origin):
        # Locally valid but wider than origin intent. This is the silent over-read case.
        raise CapabilityTranslationError(
            f"Translated grant from {translated.broker} is not a subset of "
            f"origin grant {origin.issuer}:{origin.action}. Widening detected "
            f"without co-sign. Refusing execution."
        )

    # Only here is the grant admissible for consumption.
    return None
