# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-tool-manifest-whose-signature-verifies-but-whose-capability-set-exceeds-its-declared-provenance.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-tool-manifest-whose-signature-verifies-but-whose-capability-set-exceeds-its-declared-provenance.warrant.json
# Markdown SHA-256:   0e12b517ee3d9314e985c7e919010465202494c2928be1f5354c63801dcdf51c
# Code AST SHA-256:   0bd6438c4b0352123db0807fc898cd9ccd9db2771b3d337d0e2ce02c492c9d3d
# Verified Classes:   CapabilityScopeViolation
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class CapabilityScopeViolation(RuntimeError):
    """Raised when a signed manifest's asserted or live capability set
    exceeds what the signer is authorized to assert, or when the runtime
    advertises tools absent from the signed declaration."""
    pass


def admit_session(
    signer_fingerprint: str,
    signed_declaration: set[str],
    live_tools: set[str],
    signer_scope: set[str],
    routing_name: str,
    name_pins: dict[str, str],
) -> set[str]:
    # 1. Identity: name must resolve to exactly this fingerprint, or be unpinned.
    pinned = name_pins.get(routing_name)
    if pinned is not None and pinned != signer_fingerprint:
        raise CapabilityScopeViolation(
            f"agent card name {routing_name!r} pinned to {pinned}, "
            f"but presented by {signer_fingerprint}"
        )

    # 2. Runtime must not exceed the signed declaration.
    extra_live = live_tools - signed_declaration
    if extra_live:
        raise CapabilityScopeViolation(
            f"runtime advertises unsigned tools {sorted(extra_live)}; "
            f"signed declaration was {sorted(signed_declaration)}"
        )

    # 3. Signed declaration must not exceed the signer's delegated scope.
    overreach = signed_declaration - signer_scope
    if overreach:
        raise CapabilityScopeViolation(
            f"signer {signer_fingerprint} asserted capabilities "
            f"{sorted(overreach)} outside delegated scope {sorted(signer_scope)}"
        )

    # Narrower runtime is permitted; return the callable set.
    return live_tools
