# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-an-mcp-tool-schema-as-a-liveness-claim-rather-than-a-capability-claim-and-refuse-to-bind-when-the-two-diverge.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-an-mcp-tool-schema-as-a-liveness-claim-rather-than-a-capability-claim-and-refuse-to-bind-when-the-two-diverge.warrant.json
# Markdown SHA-256:   171fbde64e524a89926f47c6ce12c46a564f26741cca3367a3ae7dbf6278754f
# Code AST SHA-256:   56254a4947cd084abe524827c1ede03859b430bee151913e4abebef276a90292
# Verified Classes:   LivenessClaimViolation, PeerManifest, ProbeResult
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Any


class LivenessClaimViolation(RuntimeError):
    """Raised when a peer's advertised schema is treated as a liveness claim
    but the peer cannot satisfy it under the caller's actual runtime state."""


@dataclass(frozen=True)
class PeerManifest:
    name: str
    input_schema: dict[str, Any]
    output_schema: dict[str, Any]
    advertised_timeout_ms: int
    manifest_is_live_probed: bool  # True only if schema was generated from a live health probe


@dataclass(frozen=True)
class ProbeResult:
    responded: bool
    returned_expected_nonempty: bool
    latency_ms: int
    status_discriminator: str | None  # None means the schema cannot distinguish null-from-empty


def bind_peer(manifest: PeerManifest, probe: ProbeResult) -> PeerManifest:
    """Bind a peer only when liveness is verified. Refuse otherwise.

    A schema received under partial observability is a capability claim.
    Liveness must be independently measured before any productive call.
    """
    if manifest.manifest_is_live_probed:
        # Schema is a measurement of current state; binding is safe.
        return manifest

    # Static/cached manifest: schema is capability-only. Verify liveness.
    if not probe.responded:
        raise LivenessClaimViolation(
            f"peer {manifest.name!r} advertised capability but did not respond to probe; "
            f"capability claim is live, liveness claim is dead — refusing to bind"
        )

    if not probe.returned_expected_nonempty:
        raise LivenessClaimViolation(
            f"peer {manifest.name!r} responded to probe but returned an empty/incorrect "
            f"result for a known-nonempty query; this is a conformant null, not negative "
            f"evidence — refusing to bind"
        )

    if probe.status_discriminator is None:
        raise LivenessClaimViolation(
            f"peer {manifest.name!r} output schema admits empty results as valid success "
            f"without a status discriminator; caller cannot distinguish 'no data' from "
            f"'no service' — refusing to bind until schema carries a discriminator"
        )

    if probe.latency_ms > manifest.advertised_timeout_ms:
        raise LivenessClaimViolation(
            f"peer {manifest.name!r} advertised timeout {manifest.advertised_timeout_ms}ms "
            f"but probe measured {probe.latency_ms}ms; advertised timeout is a fabricated "
            f"constant — refusing to inherit it into the step budget"
        )

    return manifest
