# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-accepts-a-federated-capability-attestation-whose-policy-translation-widened-scope-at-the-trust-broker.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-accepts-a-federated-capability-attestation-whose-policy-translation-widened-scope-at-the-trust-broker.warrant.json
# Markdown SHA-256:   42941cbc2b4d14bb5a93110fdc9140e3c0a2f5658724b07bbdfa28d2bba35b6c
# Code AST SHA-256:   c1b9ce2bed73b9a203f05d3c93e9813108cde6444540a5709acb6f00fccca339
# Verified Classes:   ConstraintTranslationError, Attestation
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Any


class ConstraintTranslationError(RuntimeError):
    """Raised when an attested constraint cannot be mapped to an enforceable local predicate."""


@dataclass(frozen=True)
class Attestation:
    capability: str
    constraints: dict[str, Any]
    issuer: str
    audience: str


# Local policy language supports only these constraint dimensions.
SUPPORTED_CONSTRAINTS = {"tenant", "max_rows"}


def translate_fail_closed(att: Attestation) -> dict[str, Any]:
    """Translate a federated attestation into a local predicate.

    Invariant: every attested constraint must be consumed. Unknown constraint
    keys are a hard error, never silently dropped.
    """
    unknown = set(att.constraints) - SUPPORTED_CONSTRAINTS
    if unknown:
        raise ConstraintTranslationError(
            f"attestation from {att.issuer} for {att.capability!r} carries "
            f"untranslatable constraints {sorted(unknown)}; refusing to widen scope"
        )
    predicate: dict[str, Any] = {"capability": att.capability}
    predicate.update(att.constraints)
    return predicate


def authorize(att: Attestation, request: dict[str, Any]) -> bool:
    # Fail-closed translation: a translation error denies, it does not grant.
    try:
        predicate = translate_fail_closed(att)
    except ConstraintTranslationError:
        return False

    # Enforce every consumed constraint against the request.
    for key, expected in predicate.items():
        if key == "capability":
            continue
        if request.get(key) != expected:
            return False
    return request.get("capability") == predicate["capability"]


# Demonstration: an attestation with a constraint the local grammar cannot express.
att = Attestation(
    capability="corpus.query",
    constraints={"tenant": "acme", "region": "eu-west"},  # 'region' unsupported
    issuer="tb.federation",
    audience="org-b",
)

# A naive translator would drop 'region' and grant; fail-closed denies.
granted = authorize(att, {"capability": "corpus.query", "tenant": "acme", "region": "us-east"})
assert granted is False, "fail-closed translation must not widen scope"

try:
    translate_fail_closed(att)
except ConstraintTranslationError as e:
    print(f"bounded: {e}")
