# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-manifest-declaration-skill-body-divergence-during-dynamic-skill-loading-and-why-does-the-load-time-schema-check-miss-it.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-manifest-declaration-skill-body-divergence-during-dynamic-skill-loading-and-why-does-the-load-time-schema-check-miss-it.warrant.json
# Markdown SHA-256:   1bb29e1da6408e2439af27aac69b452d4accb15e021eab1af8167537493ed6d6
# Code AST SHA-256:   530c469f54325f49f74b7fd3a79cc4aa583feb9f3b13aaadbe0867106f7586ef
# Verified Classes:   CapabilityExceedanceError
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class CapabilityExceedanceError(RuntimeError):
    """Raised when a skill body invokes a capability not present in its signed manifest."""
    def __init__(self, skill_id: str, call_capability: str, declared: frozenset):
        self.skill_id = skill_id
        self.call_capability = call_capability
        self.declared = declared
        super().__init__(
            f"skill {skill_id!r} invoked undeclared capability {call_capability!r}; "
            f"manifest declares {sorted(declared)}"
        )


def dispatch_skill_call(skill_id: str, manifest: dict, call_capability: str, invoke):
    """Gate every side-effecting call from a skill body against its signed manifest.

    The manifest must already have passed signature verification. This gate
    enforces the *ceiling* invariant: body behavior <= declared capabilities.
    """
    declared = frozenset(manifest.get("capabilities", []))
    if call_capability not in declared:
        # Roll back any writes the skill made earlier in this invocation
        # before propagating; an undeclared call invalidates the whole run.
        raise CapabilityExceedanceError(skill_id, call_capability, declared)
    return invoke()
