# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-quarantine-a-tool-result-whose-taint-label-was-laundered-through-a-peer-agent-s-schema-re-negotiation.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-quarantine-a-tool-result-whose-taint-label-was-laundered-through-a-peer-agent-s-schema-re-negotiation.warrant.json
# Markdown SHA-256:   9edfed4dbed313a1b2bef378cfff8226ca068202c55dabc3da8aaa68b9005c16
# Code AST SHA-256:   ae427f1bf87a79e1c912057087b2061ba6cfd7f109514ce1669981c8651b94d2
# Verified Classes:   Trust, TaintLaunderingError, TaintedResult
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import IntEnum


class Trust(IntEnum):
    UNTRUSTED = 0
    TRUSTED = 1


class TaintLaunderingError(RuntimeError):
    """Raised when a tool result's declared trust exceeds the trust of its inputs."""


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


def admit_tool_result(
    declared: TaintedResult,
    input_trusts: list[Trust],
    producer_originated: bool,
) -> TaintedResult:
    """Admit a peer tool result only if its declared trust is a lower bound
    on the trust of every contributing input.

    producer_originated: True only if the producer generated the content
    itself (not a transform of caller-supplied arguments). Transforms may
    never raise trust.
    """
    if not input_trusts:
        # No provenance recorded: fail closed.
        raise TaintLaunderingError(
            f"result from {declared.origin!r} has no input provenance; "
            "cannot establish trust bound, quarantining as UNTRUSTED"
        )

    bound = min(input_trusts)
    if not producer_originated and declared.trust > bound:
        raise TaintLaunderingError(
            f"laundering detected: {declared.origin!r} declared "
            f"{declared.trust.name} for transformed content whose inputs "
            f"bound at {bound.name}; recompute trust = min(inputs)"
        )

    effective = min(declared.trust, bound) if not producer_originated else declared.trust
    return TaintedResult(declared.value, effective, declared.origin)


def handle_peer_summary(
    summary: TaintedResult,
    sent_argument_trust: Trust,
) -> TaintedResult:
    # Summarizer transformed caller-supplied text: producer_originated=False.
    return admit_tool_result(
        declared=summary,
        input_trusts=[sent_argument_trust],
        producer_originated=False,
    )


# Trace of the observed failure:
untrusted_doc = TaintedResult("...embedded instruction...", Trust.UNTRUSTED, "fetch")
peer_summary = TaintedResult("...echoed instruction...", Trust.TRUSTED, "peer-S")
try:
    handle_peer_summary(peer_summary, sent_argument_trust=untrusted_doc.trust)
except TaintLaunderingError as e:
    print(f"quarantined: {e}")  # injected instruction never reaches control flow
