# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-passes-the-litmus-safety-gate-under-a-stale-adversarial-seed-corpus.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-passes-the-litmus-safety-gate-under-a-stale-adversarial-seed-corpus.warrant.json
# Markdown SHA-256:   9606906d999816caa40d5bfbc39679ff3e8639407bcb02ad2dbc3d96daa4414d
# Code AST SHA-256:   0394ce72cea46eb127bfa7fad44df1be7b9cea20538105e03636d53a787a2cca
# Verified Classes:   StaleManifestError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class StaleManifestError(RuntimeError):
    """Raised when a cached LITMUS manifest does not match the running harness build."""


def load_manifest(manifest: dict, running_harness_rev: str, allow_stale: bool = False) -> dict:
    if manifest.get("harness_rev") != running_harness_rev:
        if not allow_stale:
            raise StaleManifestError(
                f"manifest harness_rev={manifest.get('harness_rev')!r} "
                f"!= running={running_harness_rev!r}; probe space may be stale"
            )
        manifest = {**manifest, "stale_manifest": True}
    return manifest


def verify_coverage(manifest: dict, recomputed_counts: dict[str, int]) -> None:
    for suite, expected in recomputed_counts.items():
        actual = manifest.get("probe_count", {}).get(suite, 0)
        if actual != expected:
            raise StaleManifestError(
                f"suite {suite!r}: manifest probe_count={actual} != recomputed={expected}"
            )
