# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-quarantine-tainted-sandbox-capabilities-before-warrant-propagation-crosses-an-mcp-handoff-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-quarantine-tainted-sandbox-capabilities-before-warrant-propagation-crosses-an-mcp-handoff-boundary.warrant.json
# Markdown SHA-256:   accec52412e8b256c6c8c0455e3ea75a24c98962b31e09d8bb1cc0c2859d8fc8
# Code AST SHA-256:   d79cb1e1af6156ca86f4ad2ef6c269bd0c3dbfb5083bd28019fa971314a95c55
# Verified Classes:   Taint, TaintClosureError, Warrant
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import IntEnum
from typing import FrozenSet


class Taint(IntEnum):
    CLEAN = 0
    TRUSTED_INTERNAL = 1
    UNTRUSTED_EXTERNAL = 2
    UNKNOWN = 3  # maximal; never silently downgraded


class TaintClosureError(RuntimeError):
    """Raised when a handoff warrant cannot be soundly honored because the
    taint closure of its argument set is not covered by a transferred manifest."""


@dataclass(frozen=True)
class Warrant:
    capability: str
    artifact_refs: FrozenSet[str]
    taint_manifest: dict  # artifact_ref -> Taint, signed upstream
    manifest_signed: bool


QUARANTINE_SAFE = frozenset({"read_only", "no_egress", "no_mutation"})


def effective_taint(ref: str, manifest: dict, channel_trust: Taint) -> Taint:
    # Content taint joins channel trust; channel alone never clears content.
    content = manifest.get(ref, Taint.UNKNOWN)
    return Taint(max(content, channel_trust))


def honor_warrant(w: Warrant, channel_trust: Taint) -> None:
    if not w.artifact_refs:
        return  # disjoint from any tainted closure; safe to execute

    if not w.taint_manifest:
        # No manifest: empty lattice is UNKNOWN, not CLEAN.
        if w.capability not in QUARANTINE_SAFE:
            raise TaintClosureError(
                f"warrant for {w.capability!r} carries no taint manifest and "
                f"references {len(w.artifact_refs)} artifact(s); "
                f"capability is not quarantine-safe, refusing handoff"
            )
        return

    if not w.manifest_signed:
        raise TaintClosureError(
            f"taint manifest for {w.capability!r} is unsigned; "
            f"cannot verify closure, refusing handoff"
        )

    uncovered = w.artifact_refs - set(w.taint_manifest.keys())
    if uncovered:
        raise TaintClosureError(
            f"taint manifest does not cover {sorted(uncovered)}; "
            f"closure incomplete, refusing handoff"
        )

    for ref in w.artifact_refs:
        t = effective_taint(ref, w.taint_manifest, channel_trust)
        if t >= Taint.UNTRUSTED_EXTERNAL and w.capability not in QUARANTINE_SAFE:
            raise TaintClosureError(
                f"artifact {ref!r} has effective taint {t.name} but capability "
                f"{w.capability!r} is not quarantine-safe; refusing handoff"
            )
