# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-speculative-tool-call-prefetching-has-crossed-the-conflict-detection-horizon-in-concurrent-agent-swarms.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-speculative-tool-call-prefetching-has-crossed-the-conflict-detection-horizon-in-concurrent-agent-swarms.warrant.json
# Markdown SHA-256:   e96ba01ba337f94f0e74e4e403acf96b7c07badb1b4ad68329fc165278e52e00
# Code AST SHA-256:   fb1ff9b7e3c6425e02a1af1793879209d1ae4d9fdacb4aec257bc3456580496d
# Verified Classes:   ConflictHorizonBreach
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ConflictHorizonBreach(RuntimeError):
    """Raised when the conflict detector's validation coverage no longer
    covers the speculative read set, invalidating its 'no conflict' verdict."""
    pass


def validate_speculative_commit(prefetch_depth: int,
                                validation_fanout: int,
                                issuance_epoch: int,
                                validation_epoch: int,
                                conflict_rate: float,
                                trailing_median_conflict_rate: float,
                                commit_rate_stable: bool) -> bool:
    # Coverage loss: speculative reads outrun what the detector validates.
    if prefetch_depth > validation_fanout:
        raise ConflictHorizonBreach(
            f"prefetch depth {prefetch_depth} exceeds validation fan-out "
            f"{validation_fanout}; detector verdict is uninformative"
        )

    # Epoch skew: reads validated against a later epoch than issued.
    if validation_epoch != issuance_epoch:
        raise ConflictHorizonBreach(
            f"epoch skew: reads issued in epoch {issuance_epoch} validated "
            f"against epoch {validation_epoch}; read set not epoch-coherent"
        )

    # Tripwire: conflict rate collapse with stable commits = detector fell
    # behind the write frontier.
    if (commit_rate_stable
            and trailing_median_conflict_rate > 0
            and conflict_rate < 0.2 * trailing_median_conflict_rate):
        raise ConflictHorizonBreach(
            f"conflict rate {conflict_rate:.3f} collapsed below 20% of "
            f"trailing median {trailing_median_conflict_rate:.3f} with stable "
            f"commit rate; horizon breach suspected"
        )

    return True
