# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-memory-poisoning-in-write-time-provenance-scoring-despite-cross-session-consistency-agreement.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-memory-poisoning-in-write-time-provenance-scoring-despite-cross-session-consistency-agreement.warrant.json
# Markdown SHA-256:   55b4f1925ea38b7d6d53415a00dbeea207b9b7eadd69050629f6eb4475ea8a00
# Code AST SHA-256:   ebce79f018193b8d5be9503f8f21b8a123da03d4027fb99efbf0fa515df02e88
# Verified Classes:   ProvenanceError, DerivationEdge, WriteRecord
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class ProvenanceError(RuntimeError):
    pass


class DerivationEdge(Enum):
    EXTERNAL_INGEST = "external_ingest"
    MEMORY_READ_WRITE = "memory_read_write"
    SELF_SUMMARY = "self_summary"


@dataclass
class WriteRecord:
    entry_id: str
    provenance_score: float          # [0,1], higher = more trusted
    consistency_score: float         # [0,1], independence-weighted
    edge: DerivationEdge
    session_id: str


def classify_poisoning(
    record: WriteRecord,
    provenance_floor: float = 0.30,
    consistency_ceiling: float = 0.85,
    history: list[WriteRecord] | None = None,
) -> str:
    """Return a disposition. Raises ProvenanceError on malformed input."""
    if not (0.0 <= record.provenance_score <= 1.0):
        raise ProvenanceError(
            f"provenance_score out of range: {record.provenance_score}"
        )
    if not (0.0 <= record.consistency_score <= 1.0):
        raise ProvenanceError(
            f"consistency_score out of range: {record.consistency_score}"
        )

    low_prov = record.provenance_score < provenance_floor
    high_cons = record.consistency_score >= consistency_ceiling

    # The critical signal: provenance rose without any external ingest edge.
    delta_without_ingest = False
    if history:
        prior = [h for h in history if h.entry_id == record.entry_id]
        if prior:
            last = prior[-1]
            rose = record.provenance_score > last.provenance_score
            no_ingest = all(
                h.edge is not DerivationEdge.EXTERNAL_INGEST
                for h in prior + [record]
            )
            delta_without_ingest = rose and no_ingest

    if low_prov and high_cons and delta_without_ingest:
        return "QUARANTINE: poisoning signature (low-prov + high-cons + read/write-only delta)"
    if low_prov and high_cons:
        return "HOLD: ambiguous, likely self-summarization; require external ingest"
    if high_cons and record.edge is DerivationEdge.SELF_SUMMARY:
        return "DERIVED_NOT_CORROBORATED: hold at original provenance"
    if not low_prov and record.edge is DerivationEdge.EXTERNAL_INGEST:
        return "PROMOTE: external corroboration present"
    return "ACCEPT: no boundary condition triggered"
