# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-federated-task-warrant-whose-delegation-chain-is-cryptographically-valid-but-whose-policy-scope-closur.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-federated-task-warrant-whose-delegation-chain-is-cryptographically-valid-but-whose-policy-scope-closur.warrant.json
# Markdown SHA-256:   7e43411c239a4734182642532fa8d924ee6164ee564948269028135dd006b478
# Code AST SHA-256:   d9b5055229dd1e59418284e70d0cb185f2eb57726cf7ca0cf9238817f89e22f2
# Verified Classes:   ScopeClosureIndeterminate, ScopeViolation
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ScopeClosureIndeterminate(RuntimeError):
    """Raised when a warrant's chain is valid but a scope predicate
    cannot be evaluated to a definite truth value at the leaf."""
    def __init__(self, predicate: str, binding: dict, broker: str):
        self.predicate = predicate
        self.binding = binding
        self.broker = broker
        super().__init__(
            f"scope predicate {predicate!r} from broker {broker!r} "
            f"is un-evaluable against binding {binding!r}"
        )


class ScopeViolation(RuntimeError):
    """Raised when a scope predicate evaluates to FALSE at the leaf."""
    def __init__(self, predicate: str, binding: dict):
        self.predicate = predicate
        self.binding = binding
        super().__init__(f"scope predicate {predicate!r} falsified by {binding!r}")


def evaluate_warrant(warrant, task_binding, local_policy) -> str:
    # Chain integrity is a precondition, not a substitute for closure.
    if not warrant.chain_verifies():
        raise ScopeViolation("chain_integrity", {})

    for predicate in warrant.conjunctive_scope_closure():
        verdict = local_policy.evaluate(predicate, task_binding)
        if verdict is local_policy.TRUE:
            continue
        if verdict is local_policy.FALSE:
            raise ScopeViolation(predicate.name, task_binding)
        # INDETERMINATE: never execute, never terminally reject.
        raise ScopeClosureIndeterminate(
            predicate.name, task_binding, warrant.last_broker()
        )
    return "EXECUTE"
