# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-halt-when-a-federated-trust-broker-s-policy-translation-silently-widens-a-delegated-scope.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-halt-when-a-federated-trust-broker-s-policy-translation-silently-widens-a-delegated-scope.warrant.json
# Markdown SHA-256:   558982b636df4cf36bcaa6df015c0eed5416e2c0e98d0a9c5ac380a786b1acfc
# Code AST SHA-256:   d760e832f479ed2bb0500b04665fe374bc164ca5061a8160c41c87138a7de491
# Verified Classes:   ScopeWideningDetected, Capability
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet


class ScopeWideningDetected(RuntimeError):
    """Raised when a translated capability grants authority not present in the delegated warrant."""


@dataclass(frozen=True)
class Capability:
    tool: str
    predicates: FrozenSet[str]          # e.g. frozenset({"tenant_id=A-7721"})
    enforced: FrozenSet[str] = field(default_factory=frozenset)  # predicates with attestation
    broker_confidence: str = "unknown"


def assert_scope_monotonic(delegated: Capability, translated: Capability) -> None:
    """Enforce Auth(translated) subset-of Auth(delegated) before the instance executes.

    Halts on any predicate that was delegated but is neither translated nor
    enforcement-attested, and on any tool substitution to a broader surface.
    """
    if translated.tool != delegated.tool:
        raise ScopeWideningDetected(
            f"tool substitution: delegated={delegated.tool!r} translated={translated.tool!r}"
        )

    # Predicates present in the delegated warrant but absent from the translation
    # are dropped constraints -> widening. Confidence is deliberately ignored.
    dropped = delegated.predicates - translated.predicates
    if dropped:
        raise ScopeWideningDetected(
            f"delegated predicates dropped in translation: {sorted(dropped)} "
            f"(broker_confidence={translated.broker_confidence!r})"
        )

    # A predicate that survives translation but has no enforcement attestation
    # is carried-but-unenforced: equivalent to dropped.
    unenforced = translated.predicates - translated.enforced
    if unenforced:
        raise ScopeWideningDetected(
            f"predicates translated without enforcement attestation: {sorted(unenforced)}"
        )

    # Any predicate the translation introduced that was never delegated is
    # also a widening (translation must not invent authority).
    invented = translated.predicates - delegated.predicates
    if invented:
        raise ScopeWideningDetected(
            f"translation introduced undelegated predicates: {sorted(invented)}"
        )


def execute_federated_call(delegated: Capability, translated: Capability) -> str:
    try:
        assert_scope_monotonic(delegated, translated)
    except ScopeWideningDetected as exc:
        # Fail closed: do not execute, emit attributable event upstream.
        return f"HALTED: {exc}"
    return f"EXECUTED under scope {sorted(translated.predicates)}"
