# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-taint-propagating-sandbox-isolation-silently-fail-when-an-mcp-tool-returns-a-structured-error-that-the-model-re-emits-as-a-safe-p.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-taint-propagating-sandbox-isolation-silently-fail-when-an-mcp-tool-returns-a-structured-error-that-the-model-re-emits-as-a-safe-p.warrant.json
# Markdown SHA-256:   31609e5196810ade78aebcb1322ca57e92c87648792eca44769f39676529113b
# Code AST SHA-256:   dbdd3cae98b8992c3b6160f087f880078d9ed6f5925c902ecfc72ff0628ff2bb
# Verified Classes:   TaintBoundaryViolation, Provenance, Tainted, Literal, PrivilegedSink
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Any


class TaintBoundaryViolation(RuntimeError):
    """Raised when an untainted (agent-authored) literal reaches a privileged sink
    parameter that is declared tainted-only."""
    pass


@dataclass(frozen=True)
class Provenance:
    pid: str
    tainted: bool


@dataclass(frozen=True)
class Tainted:
    value: str
    provenance: Provenance


@dataclass(frozen=True)
class Literal:
    value: str


@dataclass
class PrivilegedSink:
    # Parameters declared tainted-only: the sink refuses Literal values.
    tainted_only_params: frozenset[str] = field(
        default_factory=lambda: frozenset({"env", "path", "header"})
    )

    def admit(self, param: str, value: Any) -> Tainted:
        if param in self.tainted_only_params:
            if isinstance(value, Literal):
                # The model paraphrased a tainted hint into a clean literal.
                # The sink rejects structurally; it does not ask the model
                # whether the literal was "derived from" tainted context.
                raise TaintBoundaryViolation(
                    f"param {param!r} received agent-authored Literal "
                    f"{value.value!r}; tainted-only sink requires provenance token"
                )
            if not isinstance(value, Tainted):
                raise TaintBoundaryViolation(
                    f"param {param!r} missing provenance token"
                )
            if value.provenance.tainted:
                raise TaintBoundaryViolation(
                    f"param {param!r} carries tainted provenance "
                    f"{value.provenance.pid!r}; declassify via trusted source first"
                )
            return value
        # Unprivileged params accept literals freely.
        return value if isinstance(value, Tainted) else Tainted(
            value=str(value), provenance=Provenance(pid="agent", tainted=False)
        )

    def exec(self, env: Any) -> None:
        admitted = self.admit("env", env)
        # ... actual execution using admitted.value ...


def handle_untrusted_error_response(sink: PrivilegedSink, response: dict) -> None:
    """Model has read the error and paraphrased the hint into a retry param."""
    hint = response["error"]["hint"]  # tainted, provenance p_88f1
    # The model's re-emission is a new literal with no ancestry link:
    retry_env = Literal("/tmp/.cache_override")
    # Sink enforces provenance structurally, not by model judgment:
    sink.exec(env=retry_env)  # raises TaintBoundaryViolation
