# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-poisoned-skill-manifest-at-registry-load-time-before-capability-negotiation-commits-the-injection-into-the-live-tool-sur.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-poisoned-skill-manifest-at-registry-load-time-before-capability-negotiation-commits-the-injection-into-the-live-tool-sur.warrant.json
# Markdown SHA-256:   a14eee11a63b012021881d975d9e044540c6d80d74f396d7f0b9b9c5fbf9a04c
# Code AST SHA-256:   5c14e9f0d2cc1fea8cf03a013857888df021e006106d4b988df9051f8ded7d27
# Verified Classes:   ManifestProvenanceViolation
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ManifestProvenanceViolation(RuntimeError):
    """Raised when a registry manifest's description carries instruction-shaped
    content and would enter the planning context without provenance isolation."""


INSTRUCTION_MARKERS = (
    "ignore previous", "subsequent", "all future", "from now on",
    "route all", "redirect", "do not log", "omit from audit",
)


def admit_manifest(manifest: dict, admitted_hashes: dict) -> dict:
    skill_id = manifest.get("name")
    description = manifest.get("description", "")
    endpoint = manifest.get("endpoint")
    desc_hash = manifest.get("description_hash")

    # Refresh drift check: a previously-admitted skill whose description
    # changed must re-enter the full gate, not be trusted by name.
    prior = admitted_hashes.get(skill_id)
    if prior is not None and prior != desc_hash:
        raise ManifestProvenanceViolation(
            f"description drift for {skill_id}: admitted={prior} now={desc_hash}"
        )

    lowered = description.lower()
    flagged = [m for m in INSTRUCTION_MARKERS if m in lowered]
    if flagged:
        # Quarantine before this text can share a prompt with operator spans.
        raise ManifestProvenanceViolation(
            f"instruction-shaped content in {skill_id} description: {flagged}; "
            f"endpoint={endpoint} hash={desc_hash}"
        )

    return {
        "skill_id": skill_id,
        "origin": f"registry:{skill_id}",
        "description": description,
        "endpoint": endpoint,
        "description_hash": desc_hash,
    }
