# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-litmus-derived-adversarial-skill-payload-that-passes-every-capability-probe-but-silently-re-scopes-tool-authority-at-inv.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-litmus-derived-adversarial-skill-payload-that-passes-every-capability-probe-but-silently-re-scopes-tool-authority-at-inv.warrant.json
# Markdown SHA-256:   f3c97075838af73c8613eadbbe0ccc2e26664e9e28f842143fbd88deae4d7f91
# Code AST SHA-256:   47a871bb819d3ab29d984d734a6f671f30124a0ab2c0bebfdd1390f5742cf727
# Verified Classes:   UndeclaredSurfaceError, AuthorityEscalationError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class UndeclaredSurfaceError(RuntimeError):
    """Raised when a live tool call carries keys outside the skill's declared manifest surface."""
    pass


class AuthorityEscalationError(RuntimeError):
    """Raised when a wrapper would derive effective scope from caller-supplied fields."""
    pass


def invoke_skill(manifest, wrapper, live_call_kwargs, ambient_scope):
    declared = set(manifest["params"].keys())
    executed = set(live_call_kwargs.keys())

    # Invariant: executed surface must be a subset of declared surface.
    undeclared = executed - declared
    if undeclared:
        raise UndeclaredSurfaceError(
            f"skill {manifest['name']!r} received undeclared keys {sorted(undeclared)}; "
            f"executed surface {sorted(executed)} exceeds declared {sorted(declared)}"
        )

    # Authority is derived from the manifest, never from the argument surface.
    for key in live_call_kwargs:
        if key in ("scope", "_meta", "context", "authority"):
            raise AuthorityEscalationError(
                f"argument {key!r} attempts to carry authority through the tool surface"
            )

    effective_scope = manifest["required_scopes"]
    if not set(effective_scope).issubset(set(ambient_scope)):
        raise AuthorityEscalationError(
            f"declared scope {effective_scope} not covered by ambient {ambient_scope}"
        )

    return wrapper(**live_call_kwargs)


def admit_skill(manifest, probe_serializer, production_serializer):
    # Trust only if probe and production serializers are identical.
    if probe_serializer.signature != production_serializer.signature:
        return "unrefuted"   # must be enforced at runtime, not admitted as trusted
    return "trusted"
