# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-of-a-bft-consensus-protocol-commits-a-task-to-an-agent-whose-equivocation-is-only-observable-after-the-commit-fence.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-of-a-bft-consensus-protocol-commits-a-task-to-an-agent-whose-equivocation-is-only-observable-after-the-commit-fence.warrant.json
# Markdown SHA-256:   b26074b45906b67592d6af2d9b33ac1c01afd474c84ec3af7a8b6423bdacf032
# Code AST SHA-256:   383f8109f268f557c06abf706c9307952343141808a9dc54191f1e6c6a45c611
# Verified Classes:   PostCommitEquivocationError, TraceAttestationError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class PostCommitEquivocationError(RuntimeError):
    """Raised when an agent's execution trace diverges from its committed proposal."""
    pass


class TraceAttestationError(RuntimeError):
    """Raised when the trace hash does not match the commit certificate binding."""
    pass


def verify_post_commit_trace(commit_cert: dict, observed_trace: dict) -> None:
    if commit_cert.get("trace_hash") is None:
        raise TraceAttestationError(
            "Commit certificate does not bind a trace hash; "
            "post-commit equivocation is undetectable for this task class."
        )
    if commit_cert["trace_hash"] != observed_trace.get("hash"):
        raise PostCommitEquivocationError(
            f"Agent {observed_trace.get('agent_id')} emitted trace "
            f"{observed_trace.get('hash')} but committed to "
            f"{commit_cert['trace_hash']} for task {commit_cert.get('task_id')}."
        )


def commit_task(commit_cert: dict, observed_trace: dict, irreversible: bool) -> str:
    if irreversible:
        try:
            verify_post_commit_trace(commit_cert, observed_trace)
        except (PostCommitEquivocationError, TraceAttestationError) as exc:
            raise PostCommitEquivocationError(
                f"Refusing to apply irreversible effect: {exc}"
            ) from exc
    return "applied"
