# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-halt-speculative-decomposition-when-optimistic-commit-outruns-conflict-detection.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-halt-speculative-decomposition-when-optimistic-commit-outruns-conflict-detection.warrant.json
# Markdown SHA-256:   3ce32fb0de591b320ff3e6863f62b211edb35701cc10bea6ad3f1fe40296f1cf
# Code AST SHA-256:   8522f15f981b526afaf2ddb1a6ccf1217c49ac3bf426f3b85598fc36c4d21c1e
# Verified Classes:   UnclosedValidationSetError, CommitState
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class UnclosedValidationSetError(RuntimeError):
    """Raised when a worker attempts to publish against a sibling set
    that does not yet include every dispatched sibling's write set."""


class CommitState:
    STAGED = "staged"
    VALIDATED = "validated"
    PUBLISHED = "published"


def publish_commit(
    worker_id: str,
    validated_against: set[str],
    dispatched_siblings: set[str],
    barrier_closed: bool,
    state: str,
) -> str:
    # The closed-validation-set invariant: a worker may only publish
    # against the full dispatched roster, and only after the barrier
    # has observed every sibling's write-set announcement.
    missing = dispatched_siblings - validated_against - {worker_id}
    if missing:
        raise UnclosedValidationSetError(
            f"worker {worker_id} validated against {sorted(validated_against)} "
            f"but siblings {sorted(missing)} have not announced; "
            f"publication would escape the barrier against an open set"
        )
    if not barrier_closed:
        raise UnclosedValidationSetError(
            f"worker {worker_id} attempted PUBLISHED before barrier_closed; "
            f"stage the result and defer publication"
        )
    if state != CommitState.VALIDATED:
        raise UnclosedValidationSetError(
            f"worker {worker_id} in state {state!r}, expected VALIDATED"
        )
    return CommitState.PUBLISHED
