# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-lose-policy-authority-when-a-federated-trust-broker-translates-scopes-across-org-boundaries.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-lose-policy-authority-when-a-federated-trust-broker-translates-scopes-across-org-boundaries.warrant.json
# Markdown SHA-256:   794279d3aaef1fa1888f663ca36d2154cfed6014bed924c04b09c6e47459ce04
# Code AST SHA-256:   451db55d910bc4667d796f05695ed2094d525260eec14db9de6675f3b16800c0
# Verified Classes:   ScopeWideningError, Scope
# Defensive Guards:   6 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Any


class ScopeWideningError(RuntimeError):
    """Raised when a broker's translation would grant authority broader than the parent warrant."""
    pass


@dataclass(frozen=True)
class Scope:
    action: str
    constraints: dict[str, Any] = field(default_factory=dict)


def translate_scope(parent: Scope, mapping: dict[str, str]) -> Scope:
    """Translate a foreign scope to local semantics, preserving all constraint dimensions.

    Raises ScopeWideningError if any parent constraint dimension cannot be
    represented locally. Unknown constraints are never dropped.
    """
    local_action = mapping.get(parent.action)
    if local_action is None:
        # Unmapped action: deny rather than pass through unqualified.
        raise ScopeWideningError(
            f"no translation for action {parent.action!r}; refusing to widen"
        )

    local_constraints: dict[str, Any] = {}
    for key, value in parent.constraints.items():
        if key not in _LOCAL_CONSTRAINT_SCHEMA:
            # The parent carries a constraint the local schema cannot express.
            # Mapping to the unqualified local scope would widen authority.
            raise ScopeWideningError(
                f"constraint dimension {key!r} from parent scope {parent.action!r} "
                f"has no local representation; translation would widen authority"
            )
        local_constraints[key] = value

    derived = Scope(action=local_action, constraints=local_constraints)
    _assert_containment(parent, derived)
    return derived


_LOCAL_CONSTRAINT_SCHEMA = {"redaction", "tenant", "ttl_seconds"}


def _assert_containment(parent: Scope, derived: Scope) -> None:
    """Hard invariant: derived authority must be a subset of parent authority."""
    for key, parent_val in parent.constraints.items():
        if key not in derived.constraints:
            raise ScopeWideningError(
                f"derived scope dropped constraint {key!r} present in parent"
            )
        if derived.constraints[key] != parent_val:
            raise ScopeWideningError(
                f"derived constraint {key!r}={derived.constraints[key]!r} "
                f"does not match parent {parent_val!r}"
            )


# Reproduction of the observed failure:
parent = Scope(action="doc.summarize", constraints={"redaction": "required"})
mapping = {"doc.summarize": "summarize"}
try:
    translate_scope(parent, mapping)  # redaction is representable -> ok
except ScopeWideningError as e:
    raise AssertionError(f"unexpected rejection: {e}")

# The dangerous case: parent carries a dimension the local schema lacks.
poisoned = Scope(action="doc.summarize", constraints={"pii_egress": "deny"})
try:
    translate_scope(poisoned, mapping)
except ScopeWideningError:
    pass  # correct: broker denies instead of widening to unqualified "summarize"
else:
    raise ScopeWideningError("broker widened authority by dropping pii_egress")
