# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-draw-the-write-audit-threshold-when-provenance-weighted-recall-silently-launders-a-poisoned-memory-through-partial-credit-prove.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-draw-the-write-audit-threshold-when-provenance-weighted-recall-silently-launders-a-poisoned-memory-through-partial-credit-prove.warrant.json
# Markdown SHA-256:   711864d57de162186ad2c5cdc125e03cfdcfbd35e1891916d7e10bdd64b1fe30
# Code AST SHA-256:   c83f03daf1a9b3e2340455d6225108b5c63634e51f182c08240178efe7ea7a81
# Verified Classes:   ProvenanceLaunderingError
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class ProvenanceLaunderingError(RuntimeError):
    """Raised when a write would be promoted on provenance while its
    anomaly score exceeds the veto ceiling, or when the promotion
    decision violates monotonicity in anomaly_score."""
    pass


ANOMALY_THRESHOLD = 0.65
ANOMALY_VETO_CEILING = 0.78
PROVENANCE_FLOOR = 0.70


def should_promote(anomaly_score: float, provenance_weight: float) -> bool:
    """Conjunctive promotion with hard anomaly veto.

    Rejects the disjunctive rule that promotes on provenance alone.
    Raises ProvenanceLaunderingError if the write is in the laundering
    zone: provenance above floor but anomaly above veto ceiling.
    """
    if not (0.0 <= anomaly_score <= 1.0):
        raise ValueError(f"anomaly_score out of range: {anomaly_score}")
    if not (0.0 <= provenance_weight <= 1.0):
        raise ValueError(f"provenance_weight out of range: {provenance_weight}")

    above_veto = anomaly_score > ANOMALY_VETO_CEILING
    above_floor = provenance_weight >= PROVENANCE_FLOOR

    if above_veto and above_floor:
        raise ProvenanceLaunderingError(
            f"write in laundering zone: anomaly_score={anomaly_score:.3f} "
            f"> veto_ceiling={ANOMALY_VETO_CEILING}, "
            f"provenance_weight={provenance_weight:.3f} "
            f">= floor={PROVENANCE_FLOOR}; quarantine for review"
        )

    return (anomaly_score < ANOMALY_THRESHOLD) and above_floor


def assert_monotonicity(samples: list[tuple[float, float]]) -> None:
    """Verify P(promote) is non-increasing in anomaly_score per provenance tier.

    samples: list of (anomaly_score, provenance_weight) sorted by anomaly_score.
    Raises ProvenanceLaunderingError if a higher anomaly score promotes
    while a lower one with equal-or-higher provenance does not.
    """
    for i in range(len(samples) - 1):
        a_lo, p_lo = samples[i]
        a_hi, p_hi = samples[i + 1]
        if a_hi < a_lo:
            raise ValueError("samples must be sorted by anomaly_score ascending")
        try:
            promote_lo = should_promote(a_lo, p_lo)
        except ProvenanceLaunderingError:
            promote_lo = False
        try:
            promote_hi = should_promote(a_hi, p_hi)
        except ProvenanceLaunderingError:
            promote_hi = False
        if promote_hi and not promote_lo and p_hi >= p_lo:
            raise ProvenanceLaunderingError(
                f"monotonicity violated: ({a_lo:.3f},{p_lo:.3f}) not promoted "
                f"but higher-anomaly ({a_hi:.3f},{p_hi:.3f}) promoted"
            )


# Observed trace replay: the poisoned write that the disjunctive rule promoted.
if __name__ == "__main__":
    try:
        promoted = should_promote(anomaly_score=0.81, provenance_weight=0.92)
        print(f"poison promoted (BUG): {promoted}")
    except ProvenanceLaunderingError as e:
        print(f"quarantined (CORRECT): {e}")
