# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-that-a-poisoned-memory-survived-episodic-to-semantic-distillation-in-a-long-horizon-agent.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-that-a-poisoned-memory-survived-episodic-to-semantic-distillation-in-a-long-horizon-agent.warrant.json
# Markdown SHA-256:   6aefaaabebbdff4fcba4669fac3ed08b23d40b313cbd43bfcf2899db32550a4e
# Code AST SHA-256:   bd928b4f3a282b04f579588cce48abd9f239cc41da72cb32cace15da91b940dd
# Verified Classes:   ProvenanceLaunderingError, EpisodicRecord, SemanticFact
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import List

class ProvenanceLaunderingError(RuntimeError):
    """Raised when a derived memory's provenance chain fails the abstraction-fidelity invariant."""
    pass

@dataclass
class EpisodicRecord:
    id: str
    trust: float
    contradicts: List[str] = field(default_factory=list)
    derived_from: List[str] = field(default_factory=list)  # ids of facts this record cites

@dataclass
class SemanticFact:
    id: str
    support: List[str]           # episodic record ids that entail this fact
    confidence: float
    provisional: bool = False
    laundering_risk: bool = False

def promote_fact(fact: SemanticFact, episodes: dict, high_trust_docs: List[str], trust_threshold: float = 0.5) -> SemanticFact:
    # Exclude circular corroboration: support records that themselves derive from this fact.
    independent = [e for e in fact.support if fact.id not in episodes[e].derived_from]
    if not independent:
        raise ProvenanceLaunderingError(f"fact {fact.id} has no independent support; all evidence is circular")

    effective_trust = min(episodes[e].trust for e in independent)
    contradictions = set()
    for e in independent:
        contradictions.update(episodes[e].contradicts)

    if contradictions & set(high_trust_docs):
        raise ProvenanceLaunderingError(
            f"fact {fact.id} contradicted by high-trust docs {contradictions & set(high_trust_docs)}; quarantine, do not promote"
        )

    if len(independent) == 1 and effective_trust < trust_threshold:
        fact.provisional = True
        fact.laundering_risk = True
        fact.confidence = effective_trust  # pin, do not let summarizer confidence stand
        # extend retention of the source record to the fact's audit horizon
    else:
        fact.confidence = min(fact.confidence, effective_trust)

    return fact
