# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-treat-a-guardrail-verdict-that-arrives-after-the-action-it-was-meant-to-gate-has-already-been-committed.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-treat-a-guardrail-verdict-that-arrives-after-the-action-it-was-meant-to-gate-has-already-been-committed.warrant.json
# Markdown SHA-256:   f0eecb2d3da3070ed397d21df8545fcfb999fcb729af4ffd2a0f51a2b78b70c6
# Code AST SHA-256:   d096cb8c726c467d804038d3c878c70f3a81203bf8c32babf8c1cb80d08a0c41
# Verified Classes:   UnboundVerdictError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class UnboundVerdictError(RuntimeError):
    """Raised when a guardrail verdict attempts to suppress an agent-visible
    result without an attested effect-state witness proving non-commitment
    or compensation."""
    pass


def apply_verdict(verdict, buffered_result, effect_witness):
    """
    verdict:        {"decision": "allow"|"deny"|"redact", "reason": str}
    buffered_result: the tool's actual return, already reflecting the effect
    effect_witness: {"committed": bool, "compensated": bool} attested by runtime
    """
    decision = verdict["decision"]
    committed = effect_witness["committed"]
    compensated = effect_witness["compensated"]

    if decision == "allow":
        return buffered_result

    # A verdict may only rewrite the agent-visible result if the effect
    # provably did not commit, or was compensated before the agent reads it.
    effect_is_clean = (not committed) or compensated

    if not effect_is_clean:
        # Committed and not compensated: suppression would split the agent's
        # belief from the environment. Refuse to suppress; annotate truthfully.
        raise UnboundVerdictError(
            f"verdict={decision} arrived post-commit (committed={committed}, "
            f"compensated={compensated}); cannot suppress result for reason="
            f"{verdict['reason']}. Forward truthfully with annotation and escalate."
        )

    if decision == "deny":
        return {"blocked": True, "reason": verdict["reason"],
                "committed": committed, "compensated": compensated}
    if decision == "redact":
        return {"redacted": True, "reason": verdict["reason"],
                "committed": committed, "compensated": compensated}

    raise UnboundVerdictError(f"unknown decision {decision!r}")
