# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-brokered-capability-warrant-whose-policy-translation-is-semantically-wider-than-the-originating-org-s-gran.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-brokered-capability-warrant-whose-policy-translation-is-semantically-wider-than-the-originating-org-s-gran.warrant.json
# Markdown SHA-256:   888080d7c45a802fcf232229f6f2aeb731b6b7817b1138964fa34520f0251715
# Code AST SHA-256:   a7ff40934d8750931c91b02ce0e3173bad0485dbc86624d4487be7e101f6220e
# Verified Classes:   WidenedTranslationError, AttributeState, TranslatedBundle
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Callable, Iterable


class WidenedTranslationError(RuntimeError):
    """Raised when a brokered policy translation is semantically wider than its source warrant."""


@dataclass(frozen=True)
class AttributeState:
    # None means "attribute absent / unsynced" — must not be treated as out-of-set.
    classification: str | None


@dataclass
class TranslatedBundle:
    # predicate: (resource, attr_state) -> bool ; True means "permitted"
    predicate: Callable[[str, AttributeState], bool]
    clauses: int = 0
    has_catch_all_permit: bool = False
    inverted_negation: bool = False


def source_warrant_permits(resource: str, attr: AttributeState) -> bool:
    # Source semantics: allow ONLY when tagged AND in-set. Absence => deny.
    return attr.classification in {"public", "internal"}


def assert_containment(
    bundle: TranslatedBundle,
    probe_resources: Iterable[str],
    probe_states: Iterable[AttributeState],
) -> None:
    """Exhaustive containment check over (resource, attribute-state) probes.

    Rejects if the translated bundle permits any (resource, state) the source forbids.
    Crucially includes absent-attribute states, where widening hides.
    """
    if bundle.has_catch_all_permit:
        raise WidenedTranslationError(
            "translated bundle contains a catch-all permit; widening by construction"
        )
    if bundle.inverted_negation:
        raise WidenedTranslationError(
            "translator inverted 'when P' into 'unless not P'; "
            "totality of P is unprovable across trust domains"
        )
    for resource in probe_resources:
        for attr in probe_states:
            src = source_warrant_permits(resource, attr)
            tgt = bundle.predicate(resource, attr)
            if tgt and not src:
                raise WidenedTranslationError(
                    f"containment violated: resource={resource!r} "
                    f"classification={attr.classification!r} "
                    f"translated=permit source=deny"
                )


def broker_handoff(
    bundle: TranslatedBundle,
    probe_resources: Iterable[str],
    probe_states: Iterable[AttributeState],
) -> str:
    assert_containment(bundle, probe_resources, probe_states)
    return "warrant_emitted"


if __name__ == "__main__":
    # The observed failing translation: default-allow with explicit deny for known-bad tags.
    def observed_translation(resource: str, attr: AttributeState) -> bool:
        # permit unless (tagged AND out-of-set)
        if attr.classification is None:
            return True  # untagged => guard does not fire => PERMIT (the bug)
        return attr.classification in {"public", "internal"}

    bad = TranslatedBundle(predicate=observed_translation, clauses=2)
    probes_r = ["s3://acme-analytics/restricted/x"]
    probes_s = [AttributeState(None), AttributeState("secret"), AttributeState("internal")]
    try:
        broker_handoff(bad, probes_r, probes_s)
    except WidenedTranslationError as e:
        print(f"rejected: {e}")
