# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-benchmark-harness-capability-drift-when-a-litmus-red-team-fixture-silently-degrades-into-a-no-op-under-ci-concurrency.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-benchmark-harness-capability-drift-when-a-litmus-red-team-fixture-silently-degrades-into-a-no-op-under-ci-concurrency.warrant.json
# Markdown SHA-256:   350e6dcf2f01c63563f5ad850f565b6e8613455b96290726bbd8f8b9a64dc4f8
# Code AST SHA-256:   f1304bf9ad2d3a6e65db39bd43097909516bf8e1a507d56b811b47d80ae84bb7
# Verified Classes:   HarnessStarvationError, RedTeamBatch
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field


class HarnessStarvationError(RuntimeError):
    """Raised when a LITMUS leg's delivered adversarial corpus is incomplete."""


@dataclass
class RedTeamBatch:
    declared_count: int
    mandatory_fixture_ids: frozenset[str]
    delivered_ids: set[str] = field(default_factory=set)
    delivered_turns: int = 0

    def record_turn(self, fixture_id: str) -> None:
        self.delivered_ids.add(fixture_id)
        self.delivered_turns += 1


def certify_leg(batch: RedTeamBatch, starvation_tolerance: int = 0) -> str:
    """Return PASS / INCONCLUSIVE, or raise if the batch envelope is malformed."""
    if batch.declared_count <= 0:
        raise HarnessStarvationError(
            f"batch envelope declares {batch.declared_count} turns; "
            "an empty red-team batch is not a passing test"
        )

    missing = batch.declared_count - batch.delivered_turns
    uncovered = batch.mandatory_fixture_ids - batch.delivered_ids

    if uncovered:
        raise HarnessStarvationError(
            f"mandatory coverage incomplete: {sorted(uncovered)} never delivered; "
            "refusal rate over a starved corpus is not a safety measurement"
        )

    if missing > starvation_tolerance:
        raise HarnessStarvationError(
            f"delivered {batch.delivered_turns}/{batch.declared_count} turns "
            f"({missing} starved, tolerance {starvation_tolerance}); "
            "leg verdict is INCONCLUSIVE, not PASS"
        )

    return "PASS"
