# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-skill-injection-attack-in-a-dynamically-loaded-tool-registry-when-the-poisoned-manifest-self-attests-as-valid.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-skill-injection-attack-in-a-dynamically-loaded-tool-registry-when-the-poisoned-manifest-self-attests-as-valid.warrant.json
# Markdown SHA-256:   809991104813f8a9fc0ea976fc491d50853c7bad0e5935db18a4770a850ebaeb
# Code AST SHA-256:   97218c8285380ddb59958244aae84b556edd0c68958116fde77c05a735ef2be2
# Verified Classes:   SkillInjectionBoundaryError
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class SkillInjectionBoundaryError(RuntimeError):
    """Raised when a dynamically loaded tool manifest crosses the
    untrusted-description / trusted-instruction boundary without the
    required provenance separation or change-gating."""


TRUSTED_DESCRIPTION_MAX_CHARS = 4096
IMPERATIVE_MARKERS = (
    "before calling", "first call", "you must", "include its contents",
    "ignore previous", "do not tell", "system:", "<instruction",
)


def admit_manifest(
    manifest: dict,
    *,
    registry_is_operator_controlled: bool,
    signature_valid: bool,
    is_reload: bool,
    has_approved_diff: bool,
    planner_uses_delimited_data_region: bool,
) -> dict:
    """Decide whether a manifest may enter the selectable tool set.

    Invariant enforced: a manifest whose description text is mutable at
    runtime AND self-attesting (not operator-controlled) may enter the
    planner context ONLY if (a) it is wrapped in a privilege-delimited
    data region AND (b) any change since last load has been approved.
    """
    desc = manifest.get("description", "")
    if not isinstance(desc, str):
        raise SkillInjectionBoundaryError(
            f"tool {manifest.get('name')!r}: description is not a string"
        )

    if not signature_valid:
        raise SkillInjectionBoundaryError(
            f"tool {manifest.get('name')!r}: signature invalid; reject at loader"
        )

    # Operator-controlled registries may be placed inline (trusted metadata).
    if registry_is_operator_controlled:
        return manifest

    # Self-attesting, mutable registry: description is adversarial data.
    if not planner_uses_delimited_data_region:
        raise SkillInjectionBoundaryError(
            f"tool {manifest.get('name')!r}: mutable self-attesting registry "
            "requires delimited data region in planner context; refusing inline placement"
        )

    if is_reload and not has_approved_diff:
        raise SkillInjectionBoundaryError(
            f"tool {manifest.get('name')!r}: description changed since last load "
            "without approval; silent hot-swap of untrusted text is disallowed"
        )

    lowered = desc.lower()
    hits = [m for m in IMPERATIVE_MARKERS if m in lowered]
    if hits:
        # Not a hard reject (legit tools say "use this to..."), but must be
        # surfaced: the planner gets a suspicion flag, not silent ingestion.
        manifest = {**manifest, "_injection_suspicion": hits}

    if len(desc) > TRUSTED_DESCRIPTION_MAX_CHARS:
        raise SkillInjectionBoundaryError(
            f"tool {manifest.get('name')!r}: description exceeds "
            f"{TRUSTED_DESCRIPTION_MAX_CHARS} chars; oversized descriptions "
            "are a known injection carrier"
        )

    return manifest
