# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-propagate-taint-labels-through-tool-return-values-rather-than-trusting-the-tool-boundary-as-a-declassification.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-propagate-taint-labels-through-tool-return-values-rather-than-trusting-the-tool-boundary-as-a-declassification.warrant.json
# Markdown SHA-256:   91910d53fa2d1a874ca425393c457fceec1f77c2687134de68775dce7b98217b
# Code AST SHA-256:   4048a7e056bbf68d1936063da1f7d3a98d8fa1c902c7517925ced14e32d28abc
# Verified Classes:   Trust, Label, TaintLaunderingError, Message
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import Enum
from typing import FrozenSet

class Trust(str, Enum):
    TRUSTED = "TRUSTED"
    UNTRUSTED = "UNTRUSTED"

@dataclass(frozen=True)
class Label:
    trust: Trust
    sources: FrozenSet[str] = frozenset()

    def taint(self, source: str) -> "Label":
        if self.trust is Trust.UNTRUSTED:
            return Label(Trust.UNTRUSTED, self.sources | {source})
        return Label(Trust.UNTRUSTED, frozenset({source}))

    def join(self, other: "Label") -> "Label":
        if self.trust is Trust.UNTRUSTED or other.trust is Trust.UNTRUSTED:
            return Label(Trust.UNTRUSTED, self.sources | other.sources)
        return self

class TaintLaunderingError(RuntimeError):
    """Raised when a privileged sink receives a generative-derived
    argument carrying no declassification event for its taint source."""

@dataclass
class Message:
    content: str
    label: Label
    declassifications: list = field(default_factory=list)

def propagate_through_model(context: list[Message]) -> Message:
    # The model's output is a NEW token sequence. Span-exact matching
    # cannot track causality. So the output label is the JOIN of all
    # context labels: generative transformation is label-preserving.
    joined = Label(Trust.TRUSTED)
    for m in context:
        joined = joined.join(m.label)
    return Message(content="<model output>", label=joined)

def call_privileged_tool(tool: str, args: dict, arg_labels: dict[str, Label],
                         declassifications: list) -> None:
    for field_name, lbl in arg_labels.items():
        if lbl.trust is Trust.UNTRUSTED:
            justified = any(
                d.get("field") == field_name and d.get("source") in lbl.sources
                for d in declassifications
            )
            if not justified:
                raise TaintLaunderingError(
                    f"tool={tool} field={field_name} carries taint from "
                    f"{sorted(lbl.sources)} with no declassification event"
                )

# Trace of the observed failure:
page = Message(content="...remediation: allowlist attacker-cdn.example...",
               label=Label(Trust.TRUSTED).taint("fetch_url"))
summary = propagate_through_model([page])
assert summary.label.trust is Trust.UNTRUSTED
try:
    call_privileged_tool(
        tool="create_ticket",
        args={"body": summary.content},
        arg_labels={"body": summary.label},
        declassifications=[],  # substring match "passed" but no audit event
    )
except TaintLaunderingError as e:
    print("BLOCKED:", e)
