# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-draw-the-line-between-policy-evaluation-and-policy-enforcement-in-cross-org-multi-agent-federation-with-policy-bounded-capabili.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-draw-the-line-between-policy-evaluation-and-policy-enforcement-in-cross-org-multi-agent-federation-with-policy-bounded-capabili.warrant.json
# Markdown SHA-256:   744600e02bde41940a7be639942b0578a01eb71a07815bc547ea142e0826de2a
# Code AST SHA-256:   6214230fd7e26994b95ebb9d607ad897d701867fd348908dd138562d389b8de3
# Verified Classes:   FederationAdmissibilityError, CommitmentOrderViolation
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class FederationAdmissibilityError(RuntimeError):
    """Raised when a capability is validly delegated but inadmissible at the
    enforcement point. Distinct from AuthorizationError: re-delegation cannot
    fix this; only the enforcing org's policy can."""


class CommitmentOrderViolation(RuntimeError):
    """Raised when an agent attempts to commit side effects before a preflight
    admission probe has returned, or after it returned 'unknown'."""


def preflight_then_commit(
    capability,
    enforcement_point,
    commit_fn,
    *,
    preflight_available: bool,
):
    """
    Enforce the evaluation/enforcement boundary for a single federated call.

    Invariant: no commitment (quota charge, session, mutation) may occur until
    admission is *known*, and admission-unknown is treated as admission-denied
    for commitment purposes (fail closed on commitment, fail open on probe).
    """
    if not preflight_available:
        # Missing preflight is not consent. We refuse to commit blind.
        raise CommitmentOrderViolation(
            f"enforcement_point={enforcement_point!r} exposes no preflight; "
            f"refusing to commit capability={capability.id!r} without admission signal"
        )

    verdict = enforcement_point.preflight(capability)

    if verdict.status == "denied":
        # Capability is cryptographically valid but locally inadmissible.
        # Do NOT retry with a new token; surface as admissibility failure.
        raise FederationAdmissibilityError(
            f"capability={capability.id!r} admissible=false at "
            f"enforcement_point={enforcement_point!r}: {verdict.reason!r}; "
            f"origin={capability.origin_org!r} cannot remediate this"
        )

    if verdict.status != "admitted":
        # 'unknown' or any non-terminal state: fail closed on commitment.
        raise CommitmentOrderViolation(
            f"capability={capability.id!r} admission status={verdict.status!r} "
            f"is not terminal; commitment deferred"
        )

    # Only now, with admission known, do we allow side effects. commit_fn MUST
    # be idempotent under a subsequent enforcement-time rejection, because the
    # probe is advisory and the enforcer may still reject at commit.
    return commit_fn(capability)
