# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-inherit-a-litmus-safety-benchmark-verdict-across-a-harness-re-run-when-the-adversarial-seed-corpus-has-be.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-inherit-a-litmus-safety-benchmark-verdict-across-a-harness-re-run-when-the-adversarial-seed-corpus-has-be.warrant.json
# Markdown SHA-256:   20840fb7af234e36d5a52bd82c94553981eb1774331a09be5f5ee21c7396f307
# Code AST SHA-256:   0c6fb99c5e2b91d6a685c77dccda881ceb286dc9cbd6dab77730528cfa19e3fc
# Verified Classes:   VerdictInheritanceError, CorpusManifest
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass


class VerdictInheritanceError(RuntimeError):
    """Raised when a LITMUS baseline verdict cannot be validly inherited."""
    pass


@dataclass(frozen=True)
class CorpusManifest:
    # ordered tuple of (seed_id, prompt_content_hash, template_version)
    ordered_entries: tuple
    shuffle_seed: int

    def sequence_hash(self) -> str:
        h = hashlib.sha256()
        for seed_id, content_hash, tmpl in self.ordered_entries:
            h.update(f"{seed_id}|{content_hash}|{tmpl}".encode())
            h.update(b"\x00")
        return h.hexdigest()

    def content_set_hash(self) -> str:
        h = hashlib.sha256()
        for _, content_hash, tmpl in sorted(self.ordered_entries):
            h.update(f"{content_hash}|{tmpl}".encode())
            h.update(b"\x00")
        return h.hexdigest()


def inherit_verdict(baseline: CorpusManifest, candidate: CorpusManifest) -> bool:
    """Return True iff the candidate run may inherit the baseline verdict.

    Refuses inheritance (raises) when the corpus is not sequence-identical,
    because order-sensitive refusal behavior makes set-equality insufficient.
    """
    if baseline.shuffle_seed != candidate.shuffle_seed:
        # order may differ; content might still match
        if baseline.content_set_hash() != candidate.content_set_hash():
            raise VerdictInheritanceError(
                "content drift: prompt content or template version changed; "
                "re-baseline required before any score comparison"
            )
        raise VerdictInheritanceError(
            "order drift: shuffle_seed differs while content matches; "
            "re-score baseline under candidate permutation, do not compare raw scores"
        )

    if baseline.sequence_hash() != candidate.sequence_hash():
        raise VerdictInheritanceError(
            "sequence hash mismatch with equal shuffle_seed: corpus was mutated "
            "in place; baseline verdict is not reproducible"
        )

    if baseline.content_set_hash() != candidate.content_set_hash():
        # defensive: sequence hash should already catch this
        raise VerdictInheritanceError(
            "content set mismatch despite matching sequence hash; manifest corrupt"
        )

    return True
