# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-dynamically-loaded-tool-manifest-before-schema-binding-and-why-post-load-validation-cannot-recover-a-poiso.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-dynamically-loaded-tool-manifest-before-schema-binding-and-why-post-load-validation-cannot-recover-a-poiso.warrant.json
# Markdown SHA-256:   d45728982bd699cd1fa3ed4b0a38ea1ef667b3d7fafc43aad1a5a871d7c926df
# Code AST SHA-256:   bf2790032380bd6e5bb4556f9339de142fc21642ed57f53dcd456f837fdd7b1a
# Verified Classes:   ManifestAdmissionError, Manifest
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import re
from dataclasses import dataclass

class ManifestAdmissionError(RuntimeError):
    """Raised when a dynamically loaded manifest must be rejected before capability binding."""
    pass

# Imperative patterns that target the assistant rather than describe the tool.
ASSISTANT_DIRECTED = re.compile(
    r"\b(assistant|model|agent)\b.*\b(read|send|pass|include|ignore|before calling)\b",
    re.IGNORECASE,
)

FREE_TEXT_FIELDS = ("description", "examples", "default", "errorMessage")

@dataclass
class Manifest:
    name: str
    description: str
    input_schema: dict
    signature_valid: bool
    publisher_revoked: bool
    free_text: dict

def admit_manifest(m: Manifest) -> None:
    # Gate 1: provenance (necessary, not sufficient).
    if not m.signature_valid:
        raise ManifestAdmissionError(f"{m.name}: signature invalid; reject pre-binding")
    if m.publisher_revoked:
        raise ManifestAdmissionError(f"{m.name}: publisher revoked; reject pre-binding")

    # Gate 2: structural conformance (shape only, no trust inference).
    if not isinstance(m.input_schema, dict) or "type" not in m.input_schema:
        raise ManifestAdmissionError(f"{m.name}: schema malformed; reject pre-binding")

    # Gate 3: content semantics on every free-text field, BEFORE context admission.
    for field in FREE_TEXT_FIELDS:
        value = m.free_text.get(field, "")
        if isinstance(value, str) and ASSISTANT_DIRECTED.search(value):
            raise ManifestAdmissionError(
                f"{m.name}: assistant-directed instruction in '{field}'; "
                f"reject before capability binding (context admission is irreversible)"
            )
    # Only now may the manifest be bound into the tool graph / context.
    return None
