# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-reconcile-speculative-tool-call-prefetching-against-the-commit-barrier-before-mutating-shared-state.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-reconcile-speculative-tool-call-prefetching-against-the-commit-barrier-before-mutating-shared-state.warrant.json
# Markdown SHA-256:   55f514e692a5f68065b9697f071c465061daa9451343b45342f3584a45cf5311
# Code AST SHA-256:   8b90831503cf64ab11578c803dafefc963f38efe42dd5b22184b94abfefe7f84
# Verified Classes:   SpeculativeCommitViolation, BoundArg
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class SpeculativeCommitViolation(RuntimeError):
    """Raised when a non-idempotent tool call is dispatched with uncommitted arguments."""
    pass


class BoundArg:
    def __init__(self, name: str, value, committed: bool, provenance: str):
        self.name = name
        self.value = value
        self.committed = committed
        self.provenance = provenance


def dispatch(tool, args, model_commit_seq, last_commit_seq):
    # Re-check the commit barrier at dispatch time, not only at batch admission.
    if not tool.idempotent:
        uncommitted = [a for a in args if not a.committed]
        if uncommitted:
            raise SpeculativeCommitViolation(
                f"tool={tool.name} non-idempotent but args "
                f"{[a.name + ':' + a.provenance for a in uncommitted]} are uncommitted; "
                f"demote to dry-run and re-issue after model commit"
            )
        if model_commit_seq <= last_commit_seq:
            raise SpeculativeCommitViolation(
                f"tool={tool.name} dispatched before model commit "
                f"(commit_seq={model_commit_seq} <= last={last_commit_seq})"
            )
    return tool.invoke([a.value for a in args])
