# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-brokered-capability-delegation-has-silently-widened-its-scope-during-cross-org-policy-translation.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-brokered-capability-delegation-has-silently-widened-its-scope-during-cross-org-policy-translation.warrant.json
# Markdown SHA-256:   7bde708a1587feb923fd51193fcdddeeaa11a673a70cc6d5770a8ee555e22053
# Code AST SHA-256:   4ffed850412922ae4382bccf4c30f1520c65fa40d039334e8071ef690ce9c03e
# Verified Classes:   FederationScopeViolation, TranslationFidelityError, Warrant
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Callable, FrozenSet

class FederationScopeViolation(RuntimeError):
    """Raised when a brokered delegation's effective scope exceeds intended scope."""
    pass

class TranslationFidelityError(FederationScopeViolation):
    """Raised when a broker emits a warrant without verifiable translation provenance."""
    pass

@dataclass(frozen=True)
class Warrant:
    issuer: str
    subject: str
    issued_predicate: str          # e.g. 'owner_org=="A" and tier=="internal"'
    translated_scope: FrozenSet[str]
    schema_version_pair: tuple[str, str]
    translation_fidelity: float
    ttl_hours: int

def enforce_delegation(
    warrant: Warrant,
    resource_id: str,
    predicate_evaluator: Callable[[str], bool],
    enforcement_paths: FrozenSet[str],
    covered_paths: FrozenSet[str],
) -> bool:
    # C3: provenance must be present and high-fidelity, or the class must be closed.
    if warrant.translation_fidelity < 0.95:
        raise TranslationFidelityError(
            f"warrant {warrant.subject} fidelity={warrant.translation_fidelity} "
            f"below threshold; schema {warrant.schema_version_pair}"
        )

    # C2: every invocation path must consult the allowlist.
    uncovered = enforcement_paths - covered_paths
    if uncovered:
        raise FederationScopeViolation(
            f"enforcement path(s) {sorted(uncovered)} bypass translated allowlist"
        )

    # C1: predicate extension must be a subset of translated scope.
    if predicate_evaluator(resource_id) and resource_id not in warrant.translated_scope:
        raise FederationScopeViolation(
            f"resource {resource_id} satisfies issued predicate "
            f"'{warrant.issued_predicate}' but is outside translated scope; "
            f"monotone-widening invariant violated"
        )

    return resource_id in warrant.translated_scope
