# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-litmus-derived-skill-injection-probe-as-a-contained-adversarial-artifact-rather-than-an-executable-capa.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-litmus-derived-skill-injection-probe-as-a-contained-adversarial-artifact-rather-than-an-executable-capa.warrant.json
# Markdown SHA-256:   5f6e5c0f2115a6aa7900b3f73db32925c265c73868a7c8cd5f66264690cfaab4
# Code AST SHA-256:   422ed900182f486e6305b06edba3d6706ae327c5254cc012c064b926ef8fc453
# Verified Classes:   ProbeContainmentViolation, DispatchTable
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ProbeContainmentViolation(RuntimeError):
    """Raised when an adversarial LITMUS-derived probe reaches a resolvable dispatch path."""
    pass


class DispatchTable:
    """Resolution table with provenance-aware enforcement."""

    def __init__(self, adversarial_provenance_ids: set[str]):
        self._entries: dict[str, dict] = {}
        self._adversarial = adversarial_provenance_ids
        self._fallback = None  # must stay None for containment

    def register(self, name: str, provenance_id: str, body: str) -> None:
        self._entries[name] = {"provenance": provenance_id, "body": body}

    def set_fallback(self, other: "DispatchTable") -> None:
        # Enabling a fallback is the exact mechanism that leaked in the observed run.
        self._fallback = other

    def resolve(self, name: str) -> str:
        entry = self._entries.get(name)
        if entry is None:
            if self._fallback is not None:
                return self._fallback.resolve(name)
            raise ProbeContainmentViolation(f"unresolvable skill: {name}")

        if entry["provenance"] in self._adversarial:
            # Provenance check fires at dispatch, not at read time.
            raise ProbeContainmentViolation(
                f"skill {name!r} has adversarial provenance "
                f"{entry['provenance']!r}; refusing dispatch"
            )
        return entry["body"]


def enforce_containment(table: DispatchTable) -> None:
    """Validate that the table cannot leak a probe through a fallback path."""
    if table._fallback is not None:
        raise ProbeContainmentViolation(
            "fallback resolution path present; probes may leak across "
            "shadow/live boundary — containment invariant violated"
        )


def dispatch(table: DispatchTable, name: str) -> str:
    enforce_containment(table)
    return table.resolve(name)
