# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-silent-memory-poisoning-when-recall-time-anomaly-scoring-and-write-provenance-attestation-disagree.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-silent-memory-poisoning-when-recall-time-anomaly-scoring-and-write-provenance-attestation-disagree.warrant.json
# Markdown SHA-256:   b103a36f752c423d47ce4ccb1f428186dc6bf010b38d15ba9e275a99b7d387bc
# Code AST SHA-256:   8089631a9a1ae01d1baf5e7bf858ba55dad0d3ec26df7b0f292a51dc680bc229
# Verified Classes:   MemoryConflictUnresolvable, QuarantineEntry
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class MemoryConflictUnresolvable(RuntimeError):
    """Raised when provenance attestation and recall anomaly scoring disagree
    and no valid adjudication path exists (e.g., attestation record evicted)."""
    pass


class QuarantineEntry:
    def __init__(self, entry_id, attestation, recall_score, disposition=None):
        self.entry_id = entry_id
        self.attestation = attestation  # dict: origin, tool, session, parent_hash
        self.recall_score = recall_score
        self.disposition = disposition  # None until adjudicated


def resolve_write_recall_conflict(entry, anomaly_threshold, attestation_store):
    """
    Invariant: provenance-valid + recall-anomalous => quarantine, never trust.
    provenance-dirty + recall-benign => quarantine, never evict silently.
    """
    attestation_valid = entry.attestation is not None and entry.attestation.get("parent_hash")
    recall_anomalous = entry.recall_score >= anomaly_threshold

    if attestation_valid and not recall_anomalous:
        return "accept"

    if attestation_valid and recall_anomalous:
        # Slow-drip poisoning signature: clean origin, drifted content.
        return QuarantineEntry(
            entry.entry_id, entry.attestation, entry.recall_score,
            disposition="suspect_poisoning"
        )

    if not attestation_valid and recall_anomalous:
        return QuarantineEntry(
            entry.entry_id, entry.attestation, entry.recall_score,
            disposition="suspect_poisoning_and_bad_origin"
        )

    # attestation-dirty, recall-benign: possible tool-version bug, recoverable.
    if entry.attestation is None:
        # Cannot adjudicate without origin record; must escalate, not guess.
        if entry.entry_id not in attestation_store:
            raise MemoryConflictUnresolvable(
                f"Entry {entry.entry_id}: recall-benign but attestation record "
                f"evicted; cannot distinguish tool bug from silent injection."
            )
    return QuarantineEntry(
        entry.entry_id, entry.attestation, entry.recall_score,
        disposition="suspect_tool_mismatch"
    )
