# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-acceptance-rate-collapse-when-heterogeneous-drafters-share-a-speculative-verifier-under-parallel-plan-execution-with-consensu.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-acceptance-rate-collapse-when-heterogeneous-drafters-share-a-speculative-verifier-under-parallel-plan-execution-with-consensu.warrant.json
# Markdown SHA-256:   66a461d80ee99229640b0de034d7ea069efb7c32f0191d57bf2e8ab1cf1eb852
# Code AST SHA-256:   d146a725c55e36d1b801651f58f401659c5c8dcd181b637bd947e5a2e26aeb14
# Verified Classes:   SpeculativePrefixCollapseError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class SpeculativePrefixCollapseError(RuntimeError):
    """Raised when joint acceptance on a shared prefix collapses below the
    truncate-and-repropose decision boundary, indicating worst-drafter dominance."""
    pass


def should_truncate_and_repropose(
    shared_prefix_acceptance: float,
    mean_truncation_depth: float,
    independent_branch_acceptance: float,
    truncation_overhead_ratio: float = 0.15,
    acceptance_floor: float = 0.45,
    depth_ceiling: float = 2.5,
) -> bool:
    if not (0.0 <= shared_prefix_acceptance <= 1.0):
        raise SpeculativePrefixCollapseError(
            f"acceptance out of range: {shared_prefix_acceptance}"
        )
    if shared_prefix_acceptance < acceptance_floor:
        raise SpeculativePrefixCollapseError(
            "shared-prefix acceptance below floor; prefix is doing negative work"
        )
    if mean_truncation_depth > depth_ceiling:
        raise SpeculativePrefixCollapseError(
            "truncation thrash exceeds cost of independent branches"
        )
    marginal_gain = shared_prefix_acceptance - independent_branch_acceptance
    return marginal_gain > truncation_overhead_ratio


def execute_parallel_plan(branches, verifier, consensus):
    decisions = []
    for branch in branches:
        try:
            decisions.append(
                should_truncate_and_repropose(
                    shared_prefix_acceptance=branch.shared_acceptance,
                    mean_truncation_depth=branch.truncation_depth,
                    independent_branch_acceptance=branch.independent_acceptance,
                )
            )
        except SpeculativePrefixCollapseError as exc:
            branch.record_collapse(exc)
            decisions.append(False)
    if not any(decisions):
        return consensus.fallback(branches)
    return consensus.commit([b for b, d in zip(branches, decisions) if d])
