# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-treat-a-tool-result-whose-taint-label-is-absent-rather-than-empty-the-unlabeled-result-boundary-in-cross-tool-information-flow.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-treat-a-tool-result-whose-taint-label-is-absent-rather-than-empty-the-unlabeled-result-boundary-in-cross-tool-information-flow.warrant.json
# Markdown SHA-256:   087a1fb96f371fbde782b22e4d69ba0b4e248643dc5a0f9a930babd2ed2f24a9
# Code AST SHA-256:   30c5c0027f4af022d95c9d9f38bd5c191c073acc89ba8347d1bf90cf25154370
# Verified Classes:   UnlabeledResultError, ToolResult
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional, FrozenSet

class UnlabeledResultError(RuntimeError):
    """Raised when a tool result carries no IFC label (absence, not emptiness)."""
    pass

# Lattice: TOP = untrusted/unknown, BOTTOM = clean.
TOP = "TOP"
BOTTOM = "BOTTOM"

@dataclass(frozen=True)
class ToolResult:
    payload: str
    # None means the field was ABSENT. frozenset() means PRESENT and EMPTY (clean).
    ifc_label: Optional[FrozenSet[str]] = None

def resolve_trust(result: ToolResult) -> str:
    if result.ifc_label is None:
        # Absence of a verdict is not a verdict of cleanliness.
        return TOP
    if len(result.ifc_label) == 0:
        return BOTTOM
    return TOP

def admit_to_context(result: ToolResult) -> str:
    trust = resolve_trust(result)
    if trust == TOP:
        # Fail closed: never let an unlabeled result reach the planner as trusted.
        raise UnlabeledResultError(
            f"tool result has no IFC verdict (label={'absent' if result.ifc_label is None else 'tainted'}); "
            "route to quarantine or re-fetch under strict schema"
        )
    return result.payload

def shell_sink(command: str, source: ToolResult) -> None:
    # Sink refuses TOP; must never see an unlabeled argument as clean.
    if resolve_trust(source) != BOTTOM:
        raise UnlabeledResultError(f"refusing shell exec on non-clean provenance: {command!r}")
    # ... execute ...

# Regression: absent label must raise, not silently pass.
try:
    admit_to_context(ToolResult(payload="<injected>", ifc_label=None))
except UnlabeledResultError as e:
    print("correctly failed closed:", e)
