# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-instruction-taint-that-arrives-already-laundered-through-a-trusted-provenance-summarizer.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-instruction-taint-that-arrives-already-laundered-through-a-trusted-provenance-summarizer.warrant.json
# Markdown SHA-256:   1a4d07d25c47b21e5adc1ac6452ad8a7757632b03eaf380b6d459b95b430172b
# Code AST SHA-256:   ff4a1a8a1ab0b6a1aa0cb1116231d7188e27e633575c465dc3a1a1fbec56b5f1
# Verified Classes:   ProvenanceLaunderingError, IncompleteDerivationError, ContextNode, ProvenanceGraph
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Optional

class ProvenanceLaunderingError(RuntimeError):
    """Raised when a node's trust label exceeds the meet of its derivation set."""
    pass

class IncompleteDerivationError(RuntimeError):
    """Raised when a derived node's derivation set cannot be fully verified."""
    pass

@dataclass
class ContextNode:
    node_id: str
    content: str
    channel_trust: float          # trust of the producer channel
    parents: list[str] = field(default_factory=list)
    derivation_complete: bool = True
    trust: Optional[float] = None

class ProvenanceGraph:
    def __init__(self):
        self.nodes: dict[str, ContextNode] = {}

    def add(self, node: ContextNode) -> ContextNode:
        if node.parents:
            if not node.derivation_complete:
                node.trust = 0.0
                self.nodes[node.node_id] = node
                raise IncompleteDerivationError(
                    f"node {node.node_id} has unverifiable derivation set; "
                    f"forced trust=0.0 and blocked from action-selection"
                )
            parent_meet = min(self.nodes[p].trust for p in node.parents)
            justified = min(node.channel_trust, parent_meet)
            if node.trust is None:
                node.trust = justified
            elif node.trust > justified + 1e-9:
                raise ProvenanceLaunderingError(
                    f"node {node.node_id}: declared trust {node.trust} exceeds "
                    f"meet(derivation_set)={justified}; producer channel_trust="
                    f"{node.channel_trust} appears to have laundered taint from "
                    f"parents {node.parents}"
                )
        else:
            node.trust = node.channel_trust if node.trust is None else node.trust
        self.nodes[node.node_id] = node
        return node

    def admit_to_planner(self, node_id: str) -> bool:
        node = self.nodes[node_id]
        if node.trust is None:
            raise ProvenanceLaunderingError(
                f"node {node_id} has no computed trust; refusing admission"
            )
        return node.trust >= 0.5


def ingest_summary(graph: ProvenanceGraph, summary_id: str,
                   content: str, source_ids: list[str],
                   summarizer_channel_trust: float = 1.0) -> ContextNode:
    """A trusted summarizer relays untrusted content. Its output must inherit taint."""
    node = ContextNode(
        node_id=summary_id,
        content=content,
        channel_trust=summarizer_channel_trust,
        parents=source_ids,
        derivation_complete=True,
        trust=None,
    )
    return graph.add(node)


def attempt_laundered_insert(graph: ProvenanceGraph, summary_id: str,
                             content: str, source_ids: list[str]) -> None:
    """Demonstrates the naive bug: summarizer claims its own channel trust."""
    node = ContextNode(
        node_id=summary_id,
        content=content,
        channel_trust=1.0,
        parents=source_ids,
        derivation_complete=True,
        trust=1.0,  # BUG: producer identity used as trust, not the meet
    )
    graph.add(node)  # raises ProvenanceLaunderingError
