# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-tainted-context-provenance-as-non-transitive-across-tool-call-boundaries-and-what-breaks-when-it-does-not.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-tainted-context-provenance-as-non-transitive-across-tool-call-boundaries-and-what-breaks-when-it-does-not.warrant.json
# Markdown SHA-256:   768859ecc3b5d0e2021f858249c4f282cc5c0d5f992238c08f538a32b9b44bf2
# Code AST SHA-256:   d5d3020889c68dfd384dddc339bf90cd1876f69992325ee8c0128ed86f15b317
# Verified Classes:   TaintBoundaryError, Trust, ContextSegment, ToolCall
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import Enum


class TaintBoundaryError(RuntimeError):
    """Raised when an outbound call's sink surface intersects live untrusted origins."""


class Trust(Enum):
    TRUSTED = "trusted"
    UNTRUSTED = "untrusted"


@dataclass(frozen=True)
class ContextSegment:
    origin: str
    trust: Trust
    text: str


@dataclass
class ToolCall:
    sink: str                 # "email", "http", "file_write", "shell"
    surface: str              # recipient, URL host, path prefix, command target
    args: dict = field(default_factory=dict)


# Sinks whose surface is an exfiltration channel.
EXFILTRATION_SINKS = {"email", "http", "file_write", "shell"}


def live_untrusted_origins(context: list[ContextSegment]) -> set[str]:
    return {seg.origin for seg in context if seg.trust is Trust.UNTRUSTED}


def check_taint_boundary(
    call: ToolCall,
    context: list[ContextSegment],
    trusted_allowlist: dict[str, set[str]],
) -> None:
    """
    Enforce intensional taint: block if any untrusted origin was live in the
    generation context AND the call targets an exfiltration sink whose surface
    is not resolvable against a trusted allowlist.

    Substring matching against retrieved spans is deliberately NOT used --
    it is defeated by paraphrase (the t=3 laundering step).
    """
    if call.sink not in EXFILTRATION_SINKS:
        return  # disjoint sink: no exfiltration path, allow with attenuation

    tainted = live_untrusted_origins(context)
    if not tainted:
        return  # no untrusted content live: clean allow

    allowed = trusted_allowlist.get(call.sink, set())
    if call.surface in allowed:
        return  # surface resolves to a trusted source, not model generation

    raise TaintBoundaryError(
        f"sink={call.sink!r} surface={call.surface!r} would exfiltrate from "
        f"untrusted origins={sorted(tainted)!r}; surface not in trusted "
        f"allowlist and cannot be proven disjoint from tainted context"
    )


def guarded_tool_dispatch(
    call: ToolCall,
    context: list[ContextSegment],
    trusted_allowlist: dict[str, set[str]],
) -> str:
    check_taint_boundary(call, context, trusted_allowlist)
    return f"dispatched {call.sink} -> {call.surface}"
