# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-write-audit-lineage-as-insufficient-to-detect-memory-poisoning.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-write-audit-lineage-as-insufficient-to-detect-memory-poisoning.warrant.json
# Markdown SHA-256:   627a1c78ee2e3ae4732c5dd046e0297b3d51be35d9fd0a09282468bbfb9a36c6
# Code AST SHA-256:   578ef25edae22eb4bb0c38b79c67505ff1070941242be0da4f5e4076dd8ad970
# Verified Classes:   TrustProvenanceViolation
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class TrustProvenanceViolation(RuntimeError):
    """Raised when a recall weight depends on a trust score whose
    contributing records cannot be re-validated at update time."""
    pass


def audit_trust_update(writer_id: str, new_score: float,
                       contributing_records: list[dict],
                       validator) -> float:
    """Re-validate the records that justified a trust-score change.

    Lineage audit alone is insufficient here: the records may have clean
    provenance but poisoned content that inflated the score. We must
    re-check content against the validator before accepting the score.
    """
    if not contributing_records:
        raise TrustProvenanceViolation(
            f"writer {writer_id}: trust update with no contributing records; "
            "score cannot be traced and must not be applied"
        )

    for rec in contributing_records:
        if not validator(rec):
            raise TrustProvenanceViolation(
                f"writer {writer_id}: contributing record {rec.get('id')} "
                f"fails content re-validation; refusing trust update to "
                f"{new_score} (lineage was clean, content was not)"
            )

    # Only now is the score safe to persist with its provenance snapshot.
    return new_score


def recall_weight(record: dict, trust_snapshot: dict, validator) -> float:
    """Compute retrieval weight; refuse if trust provenance is unverifiable."""
    writer = record["writer_id"]
    snap = trust_snapshot.get(writer)
    if snap is None:
        raise TrustProvenanceViolation(
            f"record {record['id']}: no trust snapshot for writer {writer}; "
            "cannot distinguish assigned trust from poisoned trust"
        )
    if not validator(record):
        raise TrustProvenanceViolation(
            f"record {record['id']}: content fails validation despite "
            "clean write-audit lineage; recall blocked"
        )
    return snap["score"] * record.get("base_weight", 1.0)
