# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-distinguish-manifest-provenance-from-manifest-content-in-mcp-tool-injection-defenses.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-distinguish-manifest-provenance-from-manifest-content-in-mcp-tool-injection-defenses.warrant.json
# Markdown SHA-256:   57862f6fc51db6db014d9728ced1b12c2fd26637b9f0e7d2883fc5f8d790989a
# Code AST SHA-256:   b3b531142167b743335141296cfc9f3019b25ab185ff4b466e1fe46706e46b75
# Verified Classes:   ManifestProvenanceError, Manifest
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class ManifestProvenanceError(RuntimeError):
    """Raised when a tool manifest's provenance cannot be bound to its claimed identity."""
    pass


class Manifest:
    def __init__(self, name, publisher_key, signature_valid, canonical_key,
                 in_current_index, revoked, description):
        self.name = name
        self.publisher_key = publisher_key
        self.signature_valid = signature_valid
        self.canonical_key = canonical_key
        self.in_current_index = in_current_index
        self.revoked = revoked
        self.description = description


def admit_manifest(manifest: Manifest) -> bool:
    # Content checks are orthogonal and insufficient alone.
    if not manifest.signature_valid:
        raise ManifestProvenanceError(
            f"manifest {manifest.name!r}: signature does not verify"
        )
    # Provenance: the signing key must be the canonical key bound to this name.
    if manifest.publisher_key != manifest.canonical_key:
        raise ManifestProvenanceError(
            f"manifest {manifest.name!r}: signed by {manifest.publisher_key!r} "
            f"but canonical publisher is {manifest.canonical_key!r} (impersonation)"
        )
    # Freshness: stale-mirror replay of a validly signed but delisted manifest.
    if not manifest.in_current_index:
        raise ManifestProvenanceError(
            f"manifest {manifest.name!r}: absent from current registry index "
            f"(stale-mirror replay)"
        )
    if manifest.revoked:
        raise ManifestProvenanceError(
            f"manifest {manifest.name!r}: signature valid but manifest revoked"
        )
    return True


def admit_manifest_text(manifest: Manifest) -> bool:
    admit_manifest(manifest)
    lowered = manifest.description.lower()
    for imperative in ("ignore previous", "disregard", "you must", "system:"):
        if imperative in lowered:
            raise ManifestProvenanceError(
                f"manifest {manifest.name!r}: description contains model-directed "
                f"imperative {imperative!r}; refusing to inject into context"
            )
    return True
