# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-trust-brokered-a2a-delegation-because-the-policy-translation-is-lossy.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-trust-brokered-a2a-delegation-because-the-policy-translation-is-lossy.warrant.json
# Markdown SHA-256:   ab72a593f0268e83ed6a6c0d0830a4f9e7e78d436cfe688ef23cfa7740d03a55
# Code AST SHA-256:   2f36cf4afb6a15947c7ac7e6914fc11e496048c52f17668b90a127ed643e64e0
# Verified Classes:   UnsoundTranslationError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class UnsoundTranslationError(RuntimeError):
    """Raised when a brokered A2A delegation carries a policy translation
    whose dropped conjuncts cannot be proven implied by retained conjuncts."""
    pass


def authorize_brokered_delegation(delegation, receiver_policy_engine):
    manifest = delegation.translation_manifest

    # Step 1: absence of manifest is indistinguishable from silent widening.
    if manifest is None:
        raise UnsoundTranslationError(
            f"delegation {delegation.id} from {delegation.origin_domain} "
            f"carries no translation manifest; soundness unverifiable"
        )

    # Step 5: advisory delegations cannot widen the admit set.
    if delegation.mode == "advisory":
        return receiver_policy_engine.evaluate(
            delegation.translated_policy, delegation.warrant
        )

    retained = manifest.retained_conjuncts
    dropped = manifest.dropped_conjuncts

    # Step 2: full mapping.
    if not dropped:
        return receiver_policy_engine.evaluate(
            delegation.translated_policy, delegation.warrant
        )

    # Step 3/4: every dropped conjunct must be provably implied by retained ones.
    # The broker's attestation flag is deliberately ignored: the broker cannot
    # discharge this proof, so its assertion carries no weight here.
    unproven = []
    for conjunct in dropped:
        if not receiver_policy_engine.proves_implied(conjunct, retained):
            unproven.append(conjunct)

    if unproven:
        raise UnsoundTranslationError(
            f"delegation {delegation.id} from {delegation.origin_domain}: "
            f"translation dropped {len(unproven)} conjunct(s) not implied by "
            f"retained policy: {[c.name for c in unproven]}; "
            f"refusing to evaluate translated policy"
        )

    return receiver_policy_engine.evaluate(
        delegation.translated_policy, delegation.warrant
    )
