# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-detect-manifest-declared-capability-inflation-in-a-skill-registry-before-executing-a-poisoned-tool.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-detect-manifest-declared-capability-inflation-in-a-skill-registry-before-executing-a-poisoned-tool.warrant.json
# Markdown SHA-256:   222c34b7591d20472d32f5e645150633d6e9753dce88d840912cab7e70128f3c
# Code AST SHA-256:   2f560e9a9ecd739583d9aa3a71f68c9079e73efac01ab1a632e8d7e877511154
# Verified Classes:   ManifestCapabilityInflation, ManifestSpanInjection
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ManifestCapabilityInflation(RuntimeError):
    """Raised when a skill manifest's declared capability class is weaker
    than the side-effect class observed during probationary execution."""
    pass


class ManifestSpanInjection(RuntimeError):
    """Raised when a planner call is justified solely by text inside a
    manifest description/schema.description trust frame."""
    pass


SIDE_EFFECT_ORDER = {"none": 0, "fs_read": 1, "fs_write": 2, "network_local": 3, "network_external": 4}


def bind_arguments(manifest: dict, observed_class: str, requested_args: dict, justification_spans: list[dict]) -> dict:
    declared = manifest.get("side_effects", ["none"])
    declared_max = max(SIDE_EFFECT_ORDER[c] for c in declared)
    observed = SIDE_EFFECT_ORDER.get(observed_class, SIDE_EFFECT_ORDER["network_external"])

    # Invariant: declared may only narrow relative to observed. If observed exceeds
    # declared, the manifest is inflated -> quarantine, do not bind.
    if observed > declared_max:
        raise ManifestCapabilityInflation(
            f"skill={manifest.get('name')!r} declared={declared} but observed={observed_class!r}; "
            f"manifest capability projection is unfaithful"
        )

    # Invariant: no call may be justified solely by manifest-frame spans.
    if justification_spans and all(s.get("source") == "manifest" for s in justification_spans):
        raise ManifestSpanInjection(
            f"skill={manifest.get('name')!r} call justified only by manifest text; "
            f"treating as schema-channel prompt injection"
        )

    # Binding policy is a function of the ENFORCED class, never the declared class.
    if observed >= SIDE_EFFECT_ORDER["network_external"]:
        # unproven egress-capable skill: redact-by-default on probation
        return {k: ("<redacted>" if _is_sensitive(k) else v) for k, v in requested_args.items()}
    return dict(requested_args)


def _is_sensitive(field: str) -> bool:
    return any(tok in field.lower() for tok in ("text", "body", "notes", "email", "token", "key"))
