# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-propagate-taint-across-a-skill-invocation-boundary-before-granting-tool-privileges.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-propagate-taint-across-a-skill-invocation-boundary-before-granting-tool-privileges.warrant.json
# Markdown SHA-256:   93e3c0ae9b90bdd1218fbfa842b4e4c2b108a94d35b55b264dbcd73562648cb9
# Code AST SHA-256:   bcd53482ffba31b73d8b54862d391e904f8a93e098c283d5d615975ceb85c520
# Verified Classes:   Taint, TaintLaunderingError, Call
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import IntEnum


class Taint(IntEnum):
    CLEAN = 0
    REGISTRY_VERIFIED = 1
    UNTRUSTED_EXTERNAL = 2

    def join(self, other: "Taint") -> "Taint":
        return Taint(max(self.value, other.value))


class TaintLaunderingError(RuntimeError):
    """Raised when a skill boundary would reset taint rather than propagate it."""


@dataclass
class Call:
    skill: str
    tool: str
    privilege: str  # "read" | "write"
    instruction_taint: Taint
    arg_taints: list[Taint]
    skill_source_taint: Taint
    declassified: bool = False


def effective_taint(call: Call) -> Taint:
    t = call.instruction_taint.join(call.skill_source_taint)
    for a in call.arg_taints:
        t = t.join(a)
    return t


def authorize(call: Call) -> bool:
    # Schema-scoped shortcut is the bug: it reads only skill_source_taint.
    # Correct predicate must join instruction + args + source.
    t = effective_taint(call)
    if call.privilege == "write" and t >= Taint.UNTRUSTED_EXTERNAL:
        if not call.declassified:
            raise TaintLaunderingError(
                f"write tool {call.skill}.{call.tool} reached with taint={t.name} "
                f"(instruction={call.instruction_taint.name}, "
                f"source={call.skill_source_taint.name}, args={[a.name for a in call.arg_taints]}); "
                f"no registered declassifier — refusing to launder taint across skill boundary"
            )
    return True


def run_chain():
    # Stage 1: untrusted message selects a registry-verified read skill.
    read_call = Call(
        skill="ticket-summarizer", tool="search_kb", privilege="read",
        instruction_taint=Taint.UNTRUSTED_EXTERNAL,
        arg_taints=[Taint.UNTRUSTED_EXTERNAL],
        skill_source_taint=Taint.REGISTRY_VERIFIED,
    )
    assert authorize(read_call)  # read is allowed, but taint must NOT reset
    propagated = effective_taint(read_call)  # == UNTRUSTED_EXTERNAL

    # Stage 2: the read result feeds a write tool. Taint must carry through.
    write_call = Call(
        skill="reconciliation", tool="execute", privilege="write",
        instruction_taint=propagated,          # inherited, not reset
        arg_taints=[propagated],
        skill_source_taint=Taint.REGISTRY_VERIFIED,
    )
    authorize(write_call)  # raises TaintLaunderingError


if __name__ == "__main__":
    run_chain()
