# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-translate-a-peer-s-authorization-decision-rather-than-re-evaluate-it-locally.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-translate-a-peer-s-authorization-decision-rather-than-re-evaluate-it-locally.warrant.json
# Markdown SHA-256:   bf814a851266323952b52154c227ff608ee46775d3070768b90cc3a4358e7b5e
# Code AST SHA-256:   43cb46ea9cbab3a6c0ef58f875f6d32e7390d456622b03c4cdd2ace2321ac0cd
# Verified Classes:   ObligationKind, TranslationRefused, Obligation, Decision
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import Enum


class ObligationKind(Enum):
    ANNOTATING = "annotating"
    CONSTRAINING = "constraining"


class TranslationRefused(RuntimeError):
    """Raised when a brokered decision cannot be faithfully translated.

    Abstention is the correct outcome; the broker must not substitute a
    local re-evaluation for a foreign decision it cannot represent.
    """


@dataclass
class Obligation:
    key: str
    value: str
    kind: ObligationKind


@dataclass
class Decision:
    verdict: str  # "allow" | "deny"
    obligations: list[Obligation] = field(default_factory=list)


LOCAL_OBLIGATION_MAP = {
    "purpose": "purpose",
    "classification": "data_class",
    "not-after": "not_after",
}


def translate_decision(
    foreign: Decision,
    *,
    resource_owner: str,
    broker_domain: str,
    local_policy_grants: bool,
) -> Decision:
    if resource_owner == broker_domain:
        # Broker is the authority; re-evaluation is legitimate.
        return foreign

    if local_policy_grants and foreign.verdict == "deny":
        raise TranslationRefused(
            "local policy grants a request the owning domain denied; "
            "conflict must be escalated, not resolved by the broker"
        )

    translated: list[Obligation] = []
    for ob in foreign.obligations:
        if ob.kind is ObligationKind.ANNOTATING:
            # Safe to drop; record as annotation only.
            continue
        mapped_key = LOCAL_OBLIGATION_MAP.get(ob.key)
        if mapped_key is None:
            raise TranslationRefused(
                f"constraining obligation {ob.key!r} has no local mapping; "
                "abstain rather than default-allow"
            )
        translated.append(Obligation(mapped_key, ob.value, ob.kind))

    return Decision(verdict=foreign.verdict, obligations=translated)


def broker(request_decision: Decision, **kwargs) -> Decision:
    try:
        return translate_decision(request_decision, **kwargs)
    except TranslationRefused:
        # Surface as explicit abstention for the requesting agent to retry
        # against the origin domain.
        return Decision(verdict="abstain", obligations=[])
