# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-re-evaluate-taint-labels-at-the-mcp-enforcement-point-rather-than-trusting-the-planner-s-declared-provenance.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-re-evaluate-taint-labels-at-the-mcp-enforcement-point-rather-than-trusting-the-planner-s-declared-provenance.warrant.json
# Markdown SHA-256:   b2679f42f79f146d07cecc3eaa40d3c8146be45263a75de721c953303c65a2f3
# Code AST SHA-256:   4ecc7a89ed85900496d86caf977acfc6e3174042d4e0bf7437dd0408eca6916c
# Verified Classes:   TaintPolicyViolation, Lineage, EnforcementPoint
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet

class TaintPolicyViolation(RuntimeError):
    """Raised when an MCP call is admitted on declared taint but violates derived lineage."""
    pass

@dataclass(frozen=True)
class Lineage:
    taint: FrozenSet[str]
    text: str  # the actual bytes of a prior source, for splice detection

@dataclass
class EnforcementPoint:
    sink_policy: dict            # tool -> allowed taint set
    declassifiers: set           # tools allowed to drop taint
    splice_k: int = 8

    def check(self, tool: str, arg: str, declared: FrozenSet[str],
              sources: list, cited: list) -> None:
        allowed = self.sink_policy.get(tool)
        if allowed is None:
            raise TaintPolicyViolation(f"tool {tool!r} not in allowlist")

        derived = set(declared)  # declared is a monotone lower bound
        for src in sources:
            if len(src.text) >= self.splice_k and src.text[: self.splice_k] in arg:
                derived |= src.taint  # byte splice: inherit source taint

        for src in cited:  # planner cited a source in its rationale
            derived |= src.taint

        # declared taint may never lower suspicion below derived
        if not declared.issuperset(derived):
            raise TaintPolicyViolation(
                f"declared taint {set(declared)} understates derived {derived} "
                f"for tool {tool!r}"
            )
        if not derived.issubset(allowed):
            raise TaintPolicyViolation(
                f"derived taint {derived} violates sink policy {allowed} "
                f"for tool {tool!r}"
            )
