# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-when-a-trust-broker-s-policy-translation-silently-widens-its-mandate-across-an-org-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-when-a-trust-broker-s-policy-translation-silently-widens-its-mandate-across-an-org-boundary.warrant.json
# Markdown SHA-256:   afed0808da710515dc6f23c9905dfbf34d56789ef6d2e89d56edec00a4769caa
# Code AST SHA-256:   34c900e5161c762e585319c6cffa16d6abc83e7566206f31e856b44d33e05cac
# Verified Classes:   TranslationContainmentError
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class TranslationContainmentError(RuntimeError):
    """Raised when a broker grant cannot be shown to be a subset of the origin policy."""
    pass


def authorize_cross_org_action(grant: dict, origin_policy_hash: str) -> bool:
    """
    Gate a cross-org action on a broker grant.

    A grant is only authoritative if it carries a signed translation attestation
    asserting containment (translated_policy subset-of origin_policy) and the
    attestation references the origin policy hash we expect.
    """
    attestation = grant.get("translation_attestation")
    if attestation is None:
        raise TranslationContainmentError(
            "grant carries no translation_attestation; cannot prove "
            "translated policy is a subset of origin policy — refusing "
            "cross-org execution (possible silent mandate widening)"
        )

    if not attestation.get("signature_verified", False):
        raise TranslationContainmentError(
            "translation_attestation signature did not verify; broker "
            "provenance is untrusted"
        )

    if attestation.get("origin_policy_hash") != origin_policy_hash:
        raise TranslationContainmentError(
            f"attestation references origin policy "
            f"{attestation.get('origin_policy_hash')!r} but expected "
            f"{origin_policy_hash!r}; translation may target a different "
            f"policy than the one in force"
        )

    if not attestation.get("containment_holds", False):
        raise TranslationContainmentError(
            "attestation explicitly reports containment failure: "
            "translated policy is not a subset of origin policy"
        )

    if grant.get("translation_warnings"):
        raise TranslationContainmentError(
            f"lossy translation detected: {grant['translation_warnings']!r}; "
            f"dropped predicates widen the mandate — escalating instead of "
            f"executing"
        )

    return True
