# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-skill-manifest-as-untrusted-input-rather-than-configuration.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-skill-manifest-as-untrusted-input-rather-than-configuration.warrant.json
# Markdown SHA-256:   fe236dd4a4fde883582e837852c63bf3e666a41fd2ffeff99772ca09127b244f
# Code AST SHA-256:   fb6b9178dec4492aea57fcac8ec016d007da66a48c629b1eab86b4c1cfacd356
# Verified Classes:   ManifestAdmissionError, Manifest, ProbeCoverage
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Optional


class ManifestAdmissionError(RuntimeError):
    """Raised when a skill manifest cannot be safely admitted under the veto-only invariant."""
    pass


@dataclass
class Manifest:
    name: str
    side_effects: str                      # "none" | "read" | "write" | "network"
    parameters: dict                       # name -> {"type":..., "enum":[...]?, "pattern":...?}
    description: str
    signed: bool


@dataclass
class ProbeCoverage:
    exercised_args: set = field(default_factory=set)   # frozenset of parameter names actually exercised
    closed: bool = False                                # True only if every param is enum/pattern bounded


def static_veto(m: Manifest) -> Optional[str]:
    # Veto-only: return a reason to reject, or None. Never returns "safe".
    if not m.signed:
        return "unsigned manifest: analysis is theater"
    if m.side_effects == "none":
        for pname, spec in m.parameters.items():
            if spec.get("type") == "string" and spec.get("format") == "uri":
                return f"side_effects=none contradicts uri parameter '{pname}'"
    return None


def admit(m: Manifest, coverage: ProbeCoverage) -> None:
    reason = static_veto(m)
    if reason is not None:
        # Static flag is a hard stop regardless of dynamic results.
        raise ManifestAdmissionError(f"static veto: {reason}")

    declared = set(m.parameters.keys())
    if not coverage.closed:
        raise ManifestAdmissionError(
            f"open parameter space for {m.name!r}; dynamic clean result cannot grant"
        )
    if not declared.issubset(coverage.exercised_args):
        missing = declared - coverage.exercised_args
        raise ManifestAdmissionError(
            f"probe coverage incomplete for {m.name!r}; unexercised: {sorted(missing)}"
        )
    # Reaching here means: static clean AND closed AND fully probed.
    # Grant is scoped to the probed closure and must expire on re-admission.
    return None


# Example: a manifest that passes every static screen but must still be quarantined.
m = Manifest(
    name="summarize_document",
    side_effects="none",
    parameters={"path": {"type": "string"}},   # unbounded string, no enum/pattern
    description="Summarize the given document.",
    signed=True,
)
try:
    admit(m, ProbeCoverage(exercised_args={"path"}, closed=False))
except ManifestAdmissionError as e:
    # Correct outcome: dynamic probes were clean, but coverage is open.
    print(f"quarantined: {e}")
