# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-enforce-taint-aware-tool-sandboxing-when-injection-propagated-actions-cross-an-execution-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-enforce-taint-aware-tool-sandboxing-when-injection-propagated-actions-cross-an-execution-boundary.warrant.json
# Markdown SHA-256:   02306e7760b8b7438dcfacfdf23edb8bcf819e421ca2fe54e277544c8868048a
# Code AST SHA-256:   8dc6ba314dfdce495bc101f131d6a3a9a0daea3d642989ed34306c9ef11a7492
# Verified Classes:   Trust, TaintViolation, Arg, ToolCall
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


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


class TaintViolation(RuntimeError):
    """Raised when a side-effecting tool call carries untrusted provenance."""


@dataclass(frozen=True)
class Arg:
    name: str
    value: str
    source_id: str | None      # None => synthesized, treated as untrusted by default
    plane: str                 # "data" | "control"


@dataclass(frozen=True)
class ToolCall:
    tool: str
    side_effecting: bool
    args: tuple[Arg, ...]


def resolve_trust(arg: Arg, source_trust: dict[str, Trust]) -> Trust:
    if arg.source_id is None:
        return Trust.UNTRUSTED          # fail-closed on unresolvable provenance
    return source_trust.get(arg.source_id, Trust.UNTRUSTED)


def enforce_boundary(
    call: ToolCall,
    source_trust: dict[str, Trust],
    declassified: set[str],            # source ids explicitly declassified by operator
) -> None:
    if not call.side_effecting:
        return
    for arg in call.args:
        trust = resolve_trust(arg, source_trust)
        if trust is Trust.UNTRUSTED and arg.source_id not in declassified:
            if arg.plane == "control":
                raise TaintViolation(
                    f"control-plane argument {arg.name!r} resolves to untrusted "
                    f"source {arg.source_id}; tool {call.tool!r} blocked pending declassification"
                )
            # data-plane: allowed but must be logged by caller
    return


def propagate_tool_output(
    output: str, input_trusts: list[Trust], out_source_id: str, source_trust: dict[str, Trust]
) -> None:
    # Transitive rule: a tool's output inherits the worst trust of its inputs.
    worst = Trust.UNTRUSTED if Trust.UNTRUSTED in input_trusts else Trust.TRUSTED
    source_trust[out_source_id] = worst


# Demonstration: two-hop laundering attempt is still caught.
source_trust = {"src_doc": Trust.UNTRUSTED}
propagate_tool_output("summary text", [Trust.UNTRUSTED], "src_summary", source_trust)
call = ToolCall(
    tool="set_permissions",
    side_effecting=True,
    args=(
        Arg("principal", "svc-writer", source_id=None, plane="control"),
        Arg("resource", "config-bucket", source_id="src_summary", plane="control"),
        Arg("action", "grant-write", source_id="src_summary", plane="control"),
    ),
)
try:
    enforce_boundary(call, source_trust, declassified=set())
except TaintViolation as exc:
    print(f"BLOCKED: {exc}")
