# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-that-taint-quarantine-has-silently-failed-provenance-marker-erosion-at-the-retrieval-to-action-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-that-taint-quarantine-has-silently-failed-provenance-marker-erosion-at-the-retrieval-to-action-boundary.warrant.json
# Markdown SHA-256:   66e0d862a810170bf428af2a349526d145f3548d4a64898bb35d59c6072139f2
# Code AST SHA-256:   307255609ae03dafafd10b4d5ddc33454a1690e08ef8a9adfa2a6fcf179d0fa7
# Verified Classes:   TaintErosionError, Node
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field


class TaintErosionError(RuntimeError):
    """Raised when an action's args overlap tainted content but carry no taint label."""
    pass


@dataclass
class Node:
    id: str
    content: str
    taint: frozenset = frozenset()
    # derivation edges: ids of nodes this value was built from
    derived_from: list = field(default_factory=list)


def _overlap(a: str, b: str, n: int = 24) -> bool:
    """Crude content-overlap probe: shared n-gram shingle."""
    if len(a) < n or len(b) < n:
        return a.strip() and a.strip() in b
    shingles_a = {a[i:i + n] for i in range(len(a) - n + 1)}
    return any(b[i:i + n] in shingles_a for i in range(len(b) - n + 1))


def effective_taint(node: Node, graph: dict) -> frozenset:
    """Taint is the union of own label and all ancestors' labels."""
    seen, stack, acc = set(), [node.id], set(node.taint)
    while stack:
        nid = stack.pop()
        if nid in seen:
            continue
        seen.add(nid)
        anc = graph.get(nid)
        if anc is None:
            continue
        acc |= anc.taint
        stack.extend(anc.derived_from)
    return frozenset(acc)


def audit_action(action: Node, graph: dict, overlap_sources: list) -> None:
    """
    Fail closed if action args overlap a tainted source but carry no taint.
    This is the marker-erosion signature: label lost across a model boundary.
    """
    declared = effective_taint(action, graph)
    for src in overlap_sources:
        if not src.taint:
            continue
        if _overlap(action.content, src.content):
            if not declared:
                raise TaintErosionError(
                    f"action {action.id!r} overlaps tainted source {src.id!r} "
                    f"({sorted(src.taint)}) but carries empty taint set: "
                    f"label eroded across derivation edge — block and re-quarantine"
                )
            if not (declared & src.taint):
                raise TaintErosionError(
                    f"action {action.id!r} carries taint {sorted(declared)} "
                    f"disjoint from source taint {sorted(src.taint)} on {src.id!r}"
                )
    # clean: either no overlap, or overlap with correctly propagated taint
