# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-quarantine-provenance-taint-at-the-tool-boundary-rather-than-trusting-label-propagation-through-nested-tool-calls.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-quarantine-provenance-taint-at-the-tool-boundary-rather-than-trusting-label-propagation-through-nested-tool-calls.warrant.json
# Markdown SHA-256:   e145e85741d1676a28928425874c6e8732b72502b992bcad1999c6289ee12589
# Code AST SHA-256:   bc30ff4c3f4578b89421aa8d1bfb20f0c3035a90fa19616951a93f6fca6ab16c
# Verified Classes:   Trust, TaintLaunderingError, Fragment
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import IntEnum
from typing import Iterable


class Trust(IntEnum):
    TRUSTED = 0
    UNTRUSTED_WEB = 1
    UNTRUSTED_TOOL = 2  # taint introduced by an unaudited generative tool


class TaintLaunderingError(RuntimeError):
    """Raised when a tool boundary would synthesize a trusted label from a
    tainted derivation history, or when a callee's echoed provenance
    contradicts the caller-computed join."""


@dataclass
class Fragment:
    text: str
    label: Trust
    derived_from: list[Trust] = field(default_factory=list)


def join(labels: Iterable[Trust]) -> Trust:
    return max(labels, default=Trust.TRUSTED)


def call_generative_tool(
    inputs: list[Fragment],
    tool_identity: Trust,
    tool_output_text: str,
    echoed_provenance: Trust | None,
) -> Fragment:
    """Caller-side taint join across a generative tool boundary.

    The callee does NOT get to report its own output label. We compute the
    join over all input taints plus the tool identity, then compare against
    whatever the callee echoed as a consistency check.
    """
    input_join = join(f.label for f in inputs)
    computed = join([input_join, tool_identity])

    if echoed_provenance is not None and echoed_provenance < computed:
        # Callee claims cleaner than its inputs allow -> laundering attempt.
        raise TaintLaunderingError(
            f"tool echoed {echoed_provenance.name} but derivation join is "
            f"{computed.name}; refusing to accept output as trusted"
        )

    if computed >= Trust.UNTRUSTED_WEB:
        # Quarantine: output of a generative tool fed tainted input must not
        # re-enter the gated context. Caller must route to sandbox or
        # independent re-verification before release.
        raise TaintLaunderingError(
            f"generative tool output derived from {computed.name} input; "
            f"quarantine required before gated use"
        )

    return Fragment(
        text=tool_output_text,
        label=computed,
        derived_from=[f.label for f in inputs] + [tool_identity],
    )


def gated_index_delete(fragment: Fragment) -> None:
    if fragment.label != Trust.TRUSTED:
        raise TaintLaunderingError(
            f"gate refused: fragment label {fragment.label.name} is not TRUSTED"
        )
    # ... destructive operation ...
