# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-lose-taint-provenance-when-an-injected-label-decays-across-a-tool-boundary-rather-than-persisting-through-the-call-graph.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-lose-taint-provenance-when-an-injected-label-decays-across-a-tool-boundary-rather-than-persisting-through-the-call-graph.warrant.json
# Markdown SHA-256:   7a06e9d8d17da7f41aa88b44295e8ef64a2b57eec121896827c0faeb8f843b58
# Code AST SHA-256:   8c6eef253799600bf0e4a99f94e52b12a390171871aa691489ae78345c8300df
# Verified Classes:   TaintDecayError, Label, Fragment, FlowPolicy
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Any


class TaintDecayError(RuntimeError):
    """Raised when a transform would drop or fail to join an input taint label."""


@dataclass(frozen=True)
class Label:
    source: str
    injection_suspect: bool = False

    def join(self, other: "Label") -> "Label":
        return Label(
            source="untrusted" if "untrusted" in (self.source, other.source) else self.source,
            injection_suspect=self.injection_suspect or other.injection_suspect,
        )


@dataclass
class Fragment:
    value: Any
    label: Label


@dataclass
class FlowPolicy:
    join_rules: dict[str, bool] = field(default_factory=dict)  # transform -> has explicit join rule

    def require_join_rule(self, transform: str) -> None:
        if not self.join_rules.get(transform, False):
            raise TaintDecayError(
                f"transform {transform!r} has no registered label-join rule; "
                f"output would start clean and launder input taint"
            )


def apply_transform(transform: str, inputs: list[Fragment], policy: FlowPolicy) -> Fragment:
    policy.require_join_rule(transform)
    joined = inputs[0].label
    for frag in inputs[1:]:
        joined = joined.join(frag.label)
    # Output label is a superset of the join of all input labels.
    return Fragment(value=f"<{transform} output>", label=joined)


def check_sink(sink: str, arg: Fragment, blocked_sources: set[str]) -> None:
    if arg.label.source in blocked_sources or arg.label.injection_suspect:
        raise TaintDecayError(
            f"sink {sink!r} rejected: argument carries label {arg.label!r}"
        )


if __name__ == "__main__":
    policy = FlowPolicy(join_rules={"summarize": True})
    fetched = Fragment("...ignore prior instructions...", Label("untrusted", injection_suspect=True))
    summary = apply_transform("summarize", [fetched], policy)
    try:
        check_sink("http_post", summary, blocked_sources={"untrusted"})
    except TaintDecayError as e:
        print("BLOCKED (correct):", e)
    # A transform without a join rule must fail loudly, not launder.
    try:
        apply_transform("format_json", [fetched], policy)
    except TaintDecayError as e:
        print("DECAY PREVENTED:", e)
