# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-detect-a-skill-manifest-injection-that-survives-signature-verification-but-rewrites-tool-semantics.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-detect-a-skill-manifest-injection-that-survives-signature-verification-but-rewrites-tool-semantics.warrant.json
# Markdown SHA-256:   14a84f596d9025a8e240e49a9cfc3d59feb4e876a3c13e1a0e1015edff743f8c
# Code AST SHA-256:   a1eaba6b4b193ab409f6874cd5d3e001995b89a2ea578119ede741d78bf72a0c
# Verified Classes:   SkillManifestTrustError
# Defensive Guards:   6 fail-closed invariants (zero external deps)

class SkillManifestTrustError(RuntimeError):
    """Raised when a signed skill manifest fails semantic-fidelity checks."""
    pass


INSTRUCTIONAL_MARKERS = (
    "pass the", "you must", "caller's full", "forward the",
    "include the environment", "send the token",
)
SINK_PARAM_NAMES = {"context", "env", "environment", "metadata", "credentials"}


def admit_skill(manifest: dict, handler_hash_ok: bool,
                signature_ok: bool, handler_static_analysis: dict) -> None:
    if not signature_ok:
        raise SkillManifestTrustError("signature invalid; reject outright")

    # Signature is necessary but NOT sufficient.
    if not handler_hash_ok:
        raise SkillManifestTrustError(
            "handler hash unverified at load time; refusing pre-exposure"
        )

    schema = manifest.get("parameters", {})
    if schema.get("additionalProperties", True):
        raise SkillManifestTrustError("open schema permits undeclared sinks")

    for prop in schema.get("properties", {}):
        if prop.lower() in SINK_PARAM_NAMES:
            raise SkillManifestTrustError(
                f"free-form sink parameter '{prop}' rejected at load"
            )

    desc = manifest.get("description", "").lower()
    for marker in INSTRUCTIONAL_MARKERS:
        if marker in desc:
            raise SkillManifestTrustError(
                f"description contains procedural directive '{marker}'; "
                "quarantine for publisher remediation"
            )

    declared = manifest.get("capability_class")
    observed = handler_static_analysis.get("capability_class")
    if declared != observed:
        raise SkillManifestTrustError(
            f"capability mismatch: declared={declared} observed={observed}"
        )
