# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-litmus-safety-benchmark-task-family-as-a-latent-injection-probe-rather-than-a-tool-misuse-probe-and-what-br.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-litmus-safety-benchmark-task-family-as-a-latent-injection-probe-rather-than-a-tool-misuse-probe-and-what-br.warrant.json
# Markdown SHA-256:   47e5cc6d38719d516582bec6d428d1946297c86f26befdb0e1d4212becb76794
# Code AST SHA-256:   d71a86ecbe759ab8fac5d9fd2de653c673fcbd5698c9013ac9e0528bd0be17d6
# Verified Classes:   Provenance, Channel, ProvenanceGateError, Instruction
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class Provenance(Enum):
    USER = "user"
    WARRANTED_DELEGATE = "warranted_delegate"
    RETRIEVED_ARTIFACT = "retrieved_artifact"
    TOOL_OUTPUT = "tool_output"
    MEMORY_RECORD = "memory_record"
    PEER_MESSAGE = "peer_message"


class Channel(Enum):
    LIS = "latent_injection_susceptibility"
    TMP = "tool_misuse_propensity"
    UNDETERMINED = "undetermined"


class ProvenanceGateError(RuntimeError):
    """Raised when a tool call cannot be attributed to an authorized principal."""


@dataclass
class Instruction:
    text: str
    provenance: Provenance
    warrant_verified: bool = False
    corroborated_by_user: bool = False


TRUSTED = {Provenance.USER, Provenance.WARRANTED_DELEGATE}


def classify(instruction: Instruction, tool_in_policy: bool) -> Channel:
    """Channel assignment keyed on instruction origin, never on tool identity."""
    trusted = instruction.provenance in TRUSTED
    if trusted and instruction.provenance is Provenance.WARRANTED_DELEGATE:
        trusted = instruction.warrant_verified

    if trusted and not tool_in_policy:
        return Channel.TMP
    if not trusted and not instruction.corroborated_by_user:
        return Channel.LIS
    if not trusted and instruction.corroborated_by_user:
        # Both an untrusted source and the user are jointly sufficient.
        return Channel.UNDETERMINED
    if trusted and tool_in_policy:
        return Channel.TMP if not tool_in_policy else Channel.UNDETERMINED
    return Channel.UNDETERMINED


def gate_tool_call(instruction: Instruction, tool_name: str, tool_in_policy: bool) -> Channel:
    """Execution-time gate: block untrusted-provenance actions before the tool fires."""
    channel = classify(instruction, tool_in_policy)

    if channel is Channel.LIS:
        raise ProvenanceGateError(
            f"tool '{tool_name}' requested by untrusted provenance "
            f"({instruction.provenance.value}); no authorized principal. "
            f"Blocked and surfaced to user; do not act."
        )
    if channel is Channel.UNDETERMINED:
        raise ProvenanceGateError(
            f"tool '{tool_name}' has overlapping or untraceable provenance; "
            f"conservative branch requires refusal until user disambiguates."
        )
    if channel is Channel.TMP:
        raise ProvenanceGateError(
            f"tool '{tool_name}' authorized by principal but out of policy; "
            f"refuse and state the capability boundary."
        )
    return channel
