# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-quarantine-a-tool-result-after-the-planner-has-already-committed-to-a-branch-the-taint-propagation-race-between-inj.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-quarantine-a-tool-result-after-the-planner-has-already-committed-to-a-branch-the-taint-propagation-race-between-inj.warrant.json
# Markdown SHA-256:   356b8016bae73ac10a935c8e568e147518cf02b807976299db46cf39c69a5d02
# Code AST SHA-256:   e80b73d4fafaae53ba9f182df218c38674743cc5b96daaecc516ba566a7fad42
# Verified Classes:   TaintError, Label, PlanStep
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import IntEnum

class TaintError(RuntimeError):
    pass

class Label(IntEnum):
    CLEAN = 0
    DERIVED_UNTRUSTED = 1
    UNTRUSTED_EXTERNAL = 2

def subsumes_untrusted(label: Label) -> bool:
    return label >= Label.DERIVED_UNTRUSTED

@dataclass
class PlanStep:
    action: str
    derivation: set[Label] = field(default_factory=set)

SIDE_EFFECT_PREFIXES = ("transfer_", "delete_", "send_", "exec_", "write_")

def enforce_quarantine(step: PlanStep, quarantined_actions: set[str]) -> None:
    if step.action in quarantined_actions:
        raise TaintError(
            f"re-derivation of quarantined action {step.action!r} after eviction; hard halt"
        )
    if not step.action.startswith(SIDE_EFFECT_PREFIXES):
        return
    worst = max(step.derivation, default=Label.CLEAN)
    if subsumes_untrusted(worst):
        raise TaintError(
            f"side-effecting action {step.action!r} has derivation set {step.derivation} "
            f"intersecting untrusted lattice (worst={worst.name}); quarantine required"
        )

# Observed failure: transfer_funds planned while poisoned doc in context.
poisoned = PlanStep(action="transfer_funds", derivation={Label.UNTRUSTED_EXTERNAL})
try:
    enforce_quarantine(poisoned, quarantined_actions=set())
except TaintError as e:
    print(f"BLOCKED: {e}")
    # Correct response: evict tainted source, re-plan, record action as quarantined.
    quarantined_actions = {"transfer_funds"}
    retry = PlanStep(action="transfer_funds", derivation={Label.CLEAN})
    try:
        enforce_quarantine(retry, quarantined_actions)
    except TaintError as e2:
        print(f"HARD HALT: {e2}")
