# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-propagate-prompt-injection-taint-across-tool-output-provenance-labels-without-losing-it-at-the-sink-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-propagate-prompt-injection-taint-across-tool-output-provenance-labels-without-losing-it-at-the-sink-boundary.warrant.json
# Markdown SHA-256:   e68ddc7fc40f5f0ed59a3022b28d4e358ed51300f8def409b66f840d70f5942a
# Code AST SHA-256:   64eb49752c9e8201b6a692eb0dc19ffafcff5198745d4e4725020c3d9bb09cdf
# Verified Classes:   Trust, Labeled, TaintViolation
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import IntEnum


class Trust(IntEnum):
    TRUSTED = 0
    UNTRUSTED = 1  # higher = more tainted; join is max


@dataclass(frozen=True)
class Labeled:
    value: str
    trust: Trust

    def __post_init__(self):
        if not isinstance(self.trust, Trust):
            raise TaintViolation(f"value has no valid trust label: {self.trust!r}")


class TaintViolation(RuntimeError):
    """Raised when a taint label is missing, downgraded, or reaches a sink illegally."""


def join(*parts: Labeled) -> Labeled:
    """The only legal way to build a string from other strings."""
    if not parts:
        raise TaintViolation("join of zero parts has undefined provenance")
    worst = max(p.trust for p in parts)
    return Labeled("".join(p.value for p in parts), worst)


def relabel(src: Labeled, new_trust: Trust) -> Labeled:
    """Monotone relabel only: may increase taint, never decrease."""
    if new_trust < src.trust:
        raise TaintViolation(
            f"non-monotone relabel: {src.trust.name} -> {new_trust.name} "
            f"on value derived from untrusted input"
        )
    return Labeled(src.value, new_trust)


def summarize(agent_output: str, source: Labeled) -> Labeled:
    """A summarizer is a taint amplifier, not a launderer."""
    # The summary is derived from `source`; its trust is the join, not the tool's.
    return Labeled(agent_output, source.trust)


def run_shell(cmd: Labeled) -> None:
    if cmd.trust is not Trust.TRUSTED:
        raise TaintViolation(
            f"sink refused: command carries {cmd.trust.name} provenance "
            f"and may contain injected instructions"
        )
    # ... execute ...


def handle_ticket(ticket_body: Labeled, summarizer_output: str) -> None:
    # Correct: interpolation is a labeled join, not an f-string.
    prompt = join(Labeled("Summarize this ticket: ", Trust.TRUSTED), ticket_body)
    summary = summarize(summarizer_output, prompt)
    # The buggy path relabeled summary to TRUSTED here; that must raise.
    if summary.trust is Trust.UNTRUSTED and "curl" in summary.value:
        raise TaintViolation(
            "summary of untrusted ticket reached shell sink with injected command"
        )
    run_shell(summary)
