# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-detect-litmus-harness-contamination-when-adversarial-probe-fixtures-leak-into-the-regression-baseline.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-detect-litmus-harness-contamination-when-adversarial-probe-fixtures-leak-into-the-regression-baseline.warrant.json
# Markdown SHA-256:   69a611cc7a385e5f600f129e94ae5ba25d0264f73c14bab2ded57f0466242627
# Code AST SHA-256:   f5b395762d752f2f8bf847c08ebf10f51ffb3157d01ae8f29dc4a1761767dbd5
# Verified Classes:   BaselineContaminationError, TaintedArtifact
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class BaselineContaminationError(RuntimeError):
    """Raised when a would-be baseline artifact carries an adversarial taint label."""


class TaintedArtifact:
    def __init__(self, fixture_id: str, payload: str, taint_labels: list[str]):
        self.fixture_id = fixture_id
        self.payload = payload
        self.taint_labels = taint_labels


def promote_to_baseline(artifact: TaintedArtifact, sanitizer) -> str:
    # Invariant: the persisted bytes must be re-checked against the SAME
    # adversarial predicate the probe enforces, over the FULL artifact.
    leaked = [label for label in artifact.taint_labels if label in artifact.payload]
    if not leaked:
        return artifact.payload

    # A tainted artifact may only be promoted after an explicit sanitization
    # step that strips the tainted span and records a stable placeholder.
    sanitized = sanitizer(artifact.payload, leaked)
    residual = [label for label in leaked if label in sanitized]
    if residual:
        raise BaselineContaminationError(
            f"fixture {artifact.fixture_id}: taint labels {residual} survived "
            f"sanitization; refusing to promote contaminated baseline"
        )
    return sanitized
