# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-tool-call-argument-provenance-as-a-hard-exfiltration-boundary-rather-than-a-soft-trust-hint.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-tool-call-argument-provenance-as-a-hard-exfiltration-boundary-rather-than-a-soft-trust-hint.warrant.json
# Markdown SHA-256:   67b600ff43797f41bef0859239a79674c6096b97b5a597925c10b99f40d7ec77
# Code AST SHA-256:   b65b0387633b2de9ab96f8bbfb36c8a538aa3c1d4509c6a7260d1087eec6079e
# Verified Classes:   Clearance, TaintViolation, UnlabeledArgument, Labeled, Sink
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import IntEnum
from typing import Any


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


class TaintViolation(RuntimeError):
    """Untrusted-derived bytes reached an egress-relevant argument position."""


class UnlabeledArgument(RuntimeError):
    """Argument carried no provenance label; transformation closure is broken."""


@dataclass(frozen=True)
class Labeled:
    value: str
    label: Clearance

    def __add__(self, other: "Labeled") -> "Labeled":
        # lattice-meet: any untrusted input taints the result
        return Labeled(self.value + other.value, Clearance(min(self.label, other.label)))


@dataclass(frozen=True)
class Sink:
    name: str
    clearance: Clearance
    egress_positions: frozenset[str]


def invoke(sink: Sink, args: dict[str, Any]) -> str:
    for pos, arg in args.items():
        if pos not in sink.egress_positions:
            continue
        if not isinstance(arg, Labeled):
            # missing label is a platform bug, not a policy violation
            raise UnlabeledArgument(
                f"sink={sink.name} pos={pos!r} has no provenance label; "
                "transformation closure violated upstream"
            )
        if arg.label < sink.clearance:
            raise TaintViolation(
                f"sink={sink.name} pos={pos!r} label={arg.label.name} "
                f"clearance={sink.clearance.name}; refusing egress of untrusted-derived bytes"
            )
    return f"executed {sink.name}"


def file_contract(summary_title: Labeled, account_no: Labeled) -> str:
    # naive interpolation would launder the label; labeled __add__ preserves meet
    filename = (
        Labeled("contract_", Clearance.TRUSTED)
        + summary_title
        + Labeled("_", Clearance.TRUSTED)
        + account_no
        + Labeled(".pdf", Clearance.TRUSTED)
    )
    sink = Sink(
        name="write_shared_volume",
        clearance=Clearance.TRUSTED,
        egress_positions=frozenset({"path"}),
    )
    return invoke(sink, {"path": filename})


if __name__ == "__main__":
    # untrusted PDF-derived title taints the filename; sink rejects it
    file_contract(
        summary_title=Labeled("vendor_contract", Clearance.UNTRUSTED),
        account_no=Labeled("ACCT-4417", Clearance.TRUSTED),
    )
