# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-validate-a-skill-s-declared-capability-set-against-its-actual-tool-call-surface-before-granting-load-time-trust.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-validate-a-skill-s-declared-capability-set-against-its-actual-tool-call-surface-before-granting-load-time-trust.warrant.json
# Markdown SHA-256:   21b2878ba86b7b6086a85aac61b600fd011c4ede38ec31a2127477dbddff20de
# Code AST SHA-256:   1976a942160895b00d70c6bc690f233fb8ef4c13f92fc270fe77bce7ce738c0c
# Verified Classes:   CapabilityScopeViolation
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class CapabilityScopeViolation(RuntimeError):
    """Raised when a skill frame invokes a tool outside its declared capability set."""
    pass


DEICTIC_MARKERS = (
    "whatever tool",
    "any available",
    "most direct access",
    "your environment",
    "available capabilities",
)


def validate_skill_load(manifest: dict, body: str, host_enforces_frame_scope: bool) -> None:
    declared = set(manifest.get("tools", []))
    if not declared:
        raise CapabilityScopeViolation(
            f"skill {manifest.get('name')!r} declares no tools; cannot bound execution"
        )
    deictic = [m for m in DEICTIC_MARKERS if m in body.lower()]
    if deictic and not host_enforces_frame_scope:
        raise CapabilityScopeViolation(
            f"skill {manifest.get('name')!r} contains capability-deictic references "
            f"{deictic} but host does not enforce per-frame tool allowlisting; "
            f"undeclared escalation is reachable at dispatch"
        )


def dispatch_tool(skill_frame: dict, tool_name: str, args: dict) -> dict:
    declared = set(skill_frame["declared_tools"])
    if tool_name not in declared:
        raise CapabilityScopeViolation(
            f"frame {skill_frame['id']!r} invoked undeclared tool {tool_name!r}; "
            f"declared={sorted(declared)}"
        )
    return {"tool": tool_name, "provenance": skill_frame["id"], "args": args}
