# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-loads-a-skill-whose-declared-capability-schema-is-a-strict-subset-of-its-actual-runtime-behavior.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-loads-a-skill-whose-declared-capability-schema-is-a-strict-subset-of-its-actual-runtime-behavior.warrant.json
# Markdown SHA-256:   3e47d5af7e814b52097bcaff60291b557962380a3884c6129eafa71a17ab1c82
# Code AST SHA-256:   0e283317159439a971f8f5ac90b1aa1eafc81415be9c1b814ef94367b4cecd38
# Verified Classes:   SkillContractViolation
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class SkillContractViolation(RuntimeError):
    """Raised when a loaded skill's observable surface exceeds its declared schema."""
    pass


def bind_arguments(declared_schema: dict, call_args: dict) -> dict:
    """
    Enforce that the observed-argument set is a subset of the declared schema.
    Permissive binding (**kwargs, unknown-field-ignoring deserializers) is the
    injection channel; this closes it by construction.
    """
    declared = set(declared_schema.get("input", {}).keys())
    observed = set(call_args.keys())
    extra = observed - declared
    if extra:
        raise SkillContractViolation(
            f"skill observed undeclared arguments {sorted(extra)}; "
            f"declared surface was {sorted(declared)}. "
            f"Reject: implementation may reach beyond declared capability."
        )
    return call_args


def load_skill(manifest: dict, implementation_signature: set) -> dict:
    """
    Reject at load time any skill whose implementation can bind arguments
    outside its declared schema, regardless of how benign the extras appear.
    """
    declared = set(manifest.get("input", {}).keys())
    if implementation_signature - declared:
        raise SkillContractViolation(
            f"skill '{manifest.get('name')}' implementation binds "
            f"{sorted(implementation_signature - declared)} not in declared schema; "
            f"refusing to load (declaration is not a bound on behavior)."
        )
    if manifest.get("side_effects") == "none" and not manifest.get("sandboxed"):
        raise SkillContractViolation(
            f"skill '{manifest.get('name')}' declares no side effects but is not "
            f"sandboxed; manifest claim cannot substitute for runtime enforcement."
        )
    return manifest
