# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-provenance-graph-poisoning-detectors-from-laundering-tainted-memory-through-legitimate-looking-derivation-edges.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-provenance-graph-poisoning-detectors-from-laundering-tainted-memory-through-legitimate-looking-derivation-edges.warrant.json
# Markdown SHA-256:   78902c2ff167a30130738f6f2d41d20973e60634e6eded62d5c7ccbb67c29412
# Code AST SHA-256:   5b3be7172ed87ccb709c451e3dfe44b69bc8c43cafa95f62371c5cee66f2d722
# Verified Classes:   TaintMonotonicityViolation, Node, Edge
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Literal

class TaintMonotonicityViolation(RuntimeError):
    """Raised when an edge kind reduces taint, breaking provenance soundness."""
    pass

EdgeKind = Literal["extract", "summarize", "synthesize", "attest"]

# Sound classification: every kind either propagates or sources taint.
# There is deliberately NO "clearing" class.
PROPAGATING: set[EdgeKind] = {"extract", "summarize", "synthesize", "attest"}

@dataclass
class Node:
    id: str
    tainted: bool = False
    ancestors: set[str] = field(default_factory=set)

@dataclass
class Edge:
    src: str
    dst: str
    kind: EdgeKind

def assert_monotone(edges: list[Edge]) -> None:
    for e in edges:
        if e.kind not in PROPAGATING:
            raise TaintMonotonicityViolation(
                f"edge kind {e.kind!r} is not classified as propagating; "
                f"a non-propagating kind can launder taint ({e.src}->{e.dst})"
            )

def propagate_fixed_point(nodes: dict[str, Node], edges: list[Edge]) -> None:
    assert_monotone(edges)  # guardrail: reject laundering kinds up front
    changed = True
    while changed:
        changed = False
        for e in edges:
            u, v = nodes[e.src], nodes[e.dst]
            want = u.tainted or (u.ancestors & {n for n, x in nodes.items() if x.tainted})
            if want and not v.tainted:
                v.tainted = True
                changed = True
            if not u.ancestors <= v.ancestors:
                v.ancestors |= u.ancestors | {u.id}
                changed = True

def detect_laundering(edges: list[Edge]) -> None:
    """Falsification test: any kind that lets a tainted node yield a clean one."""
    for e in edges:
        if e.kind not in PROPAGATING:
            raise TaintMonotonicityViolation(
                f"laundering channel detected via edge kind {e.kind!r} "
                f"on {e.src}->{e.dst}: trusted-process edges must not reset taint"
            )

if __name__ == "__main__":
    nodes = {
        "doc://untrusted/17": Node("doc://untrusted/17", tainted=True),
        "mem:4412": Node("mem:4412"),
        "mem:4501": Node("mem:4501"),
        "mem:4733": Node("mem:4733"),
    }
    edges = [
        Edge("doc://untrusted/17", "mem:4412", "extract"),
        Edge("mem:4412", "mem:4501", "summarize"),
        Edge("mem:4501", "mem:4733", "synthesize"),
    ]
    propagate_fixed_point(nodes, edges)
    assert nodes["mem:4733"].tainted, "synthesize must not launder taint"
    print("invariant holds: mem:4733 correctly quarantined")
