# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-safety-gated-commit-is-about-to-fire-on-a-speculative-tool-call-whose-pre-execution-has-already-mutated-shared-stat.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-safety-gated-commit-is-about-to-fire-on-a-speculative-tool-call-whose-pre-execution-has-already-mutated-shared-stat.warrant.json
# Markdown SHA-256:   31099453cbb247e2fa875156423ff6fd58f46812a1ecfa9c0ed65988cbdf9bc3
# Code AST SHA-256:   3738008b4d02990dd652f7a549277bed7c4b18d953a0630f2b9529a4cf73c29b
# Verified Classes:   StaleLITMUSCommitError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class StaleLITMUSCommitError(RuntimeError):
    """Raised when a safety-gated commit fires on a score computed before
    speculative pre-execution mutated a feature the scorer reads."""
    pass


def commit_guard(candidate, pre_exec_score, commit_score, feature_witness, threshold):
    # feature_witness: set of scorer features whose state changed between
    # pre-execution snapshot and commit-time snapshot.
    if commit_score < threshold:
        raise StaleLITMUSCommitError(
            f"candidate {candidate!r}: commit-time score {commit_score:.3f} "
            f"below gate {threshold:.3f} (pre-exec score was {pre_exec_score:.3f})"
        )
    if feature_witness:
        raise StaleLITMUSCommitError(
            f"candidate {candidate!r}: pre-execution mutated scorer features "
            f"{sorted(feature_witness)}; cached score {pre_exec_score:.3f} "
            f"is stale relative to commit-time state"
        )
    return True
