# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-detect-memory-poisoning-when-write-provenance-scores-and-anomaly-weighted-recall-gates-disagree.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-detect-memory-poisoning-when-write-provenance-scores-and-anomaly-weighted-recall-gates-disagree.warrant.json
# Markdown SHA-256:   e4ba0ee626735aeaaa9de3cf320f7161af54196501ead659c2900980b8791aa6
# Code AST SHA-256:   1024c8a7f2bd93c36f031b9a39796c8bd271b1b2a848cb8cfbb1bcf1a7078840
# Verified Classes:   ProvenanceGateDisagreement
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ProvenanceGateDisagreement(RuntimeError):
    """Raised when write-provenance and anomaly-weighted recall gate disagree
    in the high-authority/anomalous quadrant and no re-verification is possible."""
    pass


TAU_HIGH = 0.80
TAU_LOW = 0.30
DELTA = 0.40


def resolve_recall(record, provenance, gate_weight, reverify):
    """
    record: opaque memory record
    provenance: float in [0,1], fixed at write time
    gate_weight: float in [0,1], anomaly-weighted recall gate
    reverify: callable(record) -> bool | None, re-checks the source if reachable
    Returns one of: ('admit', record), ('admit_flagged', record),
                    ('quarantine', record), ('drop', record)
    """
    if not (0.0 <= provenance <= 1.0 and 0.0 <= gate_weight <= 1.0):
        raise ProvenanceGateDisagreement(
            f"scores out of range: provenance={provenance} gate={gate_weight}"
        )

    if provenance >= TAU_HIGH and gate_weight <= TAU_LOW:
        # High authority, anomalous recall: the poison quadrant.
        verdict = reverify(record)
        if verdict is True:
            return ("admit", record)
        if verdict is False:
            return ("drop", record)
        # Source unreachable or inconclusive: must quarantine, never max().
        if (provenance - gate_weight) < DELTA:
            raise ProvenanceGateDisagreement(
                "disagreement below delta but source unverifiable; "
                "refusing to admit at full weight without a state transition"
            )
        return ("quarantine", record)

    if provenance <= TAU_LOW and gate_weight >= TAU_HIGH:
        # Low authority, typical recall: admit but do not promote.
        return ("admit_flagged", record)

    if provenance >= TAU_HIGH and gate_weight >= TAU_HIGH:
        return ("admit", record)

    return ("drop", record)
