# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-distrust-its-own-long-horizon-recall-provenance-decay-and-the-silent-write-boundary-in-agent-memory.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-distrust-its-own-long-horizon-recall-provenance-decay-and-the-silent-write-boundary-in-agent-memory.warrant.json
# Markdown SHA-256:   3ca138bed0e64baa6ef95cf734d2db9c30070076126d130a8c491a089fa661d4
# Code AST SHA-256:   2386a99783473ba626b035c3893770e279c1fa536ad0f6755a9d718c56851189
# Verified Classes:   ProvenanceLaunderingError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ProvenanceLaunderingError(RuntimeError):
    """Raised when a consistency check attempts to re-attest an item
    whose provenance has decayed below the verifiability threshold."""


PROVENANCE_THRESHOLD = 0.6          # fraction of derivation hops intact
MAX_BATCH_PROMOTION_RATE = 0.25     # expected honest correlation ceiling


def re_attest(item: dict, batch_promotions: dict, batch_id: str) -> bool:
    completeness = item.get("provenance_completeness", 0.0)
    agrees = item.get("agrees_with_state", False)

    if completeness < PROVENANCE_THRESHOLD:
        # Agreement is NOT sufficient under decayed provenance.
        # Promoting here is the laundering path.
        raise ProvenanceLaunderingError(
            f"item {item.get('id')} has provenance completeness "
            f"{completeness:.2f} < {PROVENANCE_THRESHOLD}; "
            f"agrees_with_state={agrees} is weak negative evidence, "
            f"quarantine as UNVERIFIABLE, do not promote"
        )

    if not agrees:
        return False

    promoted = batch_promotions.get(batch_id, 0)
    total = batch_promotions.get(f"{batch_id}:total", 1)
    if total and promoted / total > MAX_BATCH_PROMOTION_RATE:
        raise ProvenanceLaunderingError(
            f"batch {batch_id} promotion rate {promoted}/{total} exceeds "
            f"ceiling {MAX_BATCH_PROMOTION_RATE}; correlated promotion "
            f"across an ingest batch indicates re-synchronization after injection"
        )

    return True
