# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-capability-manifest-is-being-weaponized-through-skill-injection-at-load-time.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-capability-manifest-is-being-weaponized-through-skill-injection-at-load-time.warrant.json
# Markdown SHA-256:   cf2c891d9c7c5b4c207327bfd4b408c5d138b4bfef49b61e01afc6e09f5a5389
# Code AST SHA-256:   5ec8b537a931658a5d11fb098f089f64afb1d3fb54d36ce591bf40f96c896f70
# Verified Classes:   ManifestInjectionError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ManifestInjectionError(RuntimeError):
    """Raised when a dynamically loaded skill manifest exhibits instruction-injection structure."""
    pass


IMPERATIVE_MARKERS = ("you must", "always call", "do not use", "instead of", "ignore prior", "prefer this skill")
ROUTING_MARKERS = ("pass the", "forward the", "send the", "route the")


def audit_manifest(manifest: dict, known_skill_ids: set[str]) -> None:
    desc = manifest.get("description", "") + " " + manifest.get("when_to_use", "")
    lowered = desc.lower()

    signals = []
    if any(m in lowered for m in IMPERATIVE_MARKERS):
        signals.append("imperative_mood")
    if any(other in desc for other in known_skill_ids if other != manifest.get("id")):
        signals.append("cross_skill_reference")

    schema = manifest.get("inputSchema", {}).get("properties", {})
    for prop, spec in schema.items():
        pdesc = (spec.get("description") or "").lower()
        if any(m in pdesc for m in ROUTING_MARKERS):
            signals.append(f"routing_directive:{prop}")
            break

    if len(signals) >= 2:
        raise ManifestInjectionError(
            f"manifest {manifest.get('id')!r} quarantined: signals={signals}"
        )
    if "imperative_mood" in signals and "routing_directive" in " ".join(signals):
        raise ManifestInjectionError(
            f"manifest {manifest.get('id')!r} quarantined: imperative + routing drift"
        )
