# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-abort-a2a-v1-0-streaming-artifact-negotiation-when-the-negotiated-artifactkind-is-valid-per-spec-but-unrepresentabl.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-abort-a2a-v1-0-streaming-artifact-negotiation-when-the-negotiated-artifactkind-is-valid-per-spec-but-unrepresentabl.warrant.json
# Markdown SHA-256:   c14e5011399f4e6aa9a681f8e983df1a85dc346b1f55b3d65778226b452efab4
# Code AST SHA-256:   e3060f6f32d257dbfa4dad9c6c633d705196cd988913fc9de50895c97c0bae16
# Verified Classes:   ArtifactNegotiationError
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class ArtifactNegotiationError(RuntimeError):
    """Raised when a negotiated artifact kind is admissible but unrepresentable
    by the receiving runtime's content-addressed store (non-deterministic digest)."""
    pass


def assert_representable(
    artifact_kind: str,
    sender_capabilities: dict,
    chunked: bool,
    digest_initial: str,
    digest_probe: str,
) -> None:
    """Enforce the probe-and-abort boundary before the first chunked CAS commit.

    Aborts when the kind is admissible, the stream is chunked, the sender has not
    declared canonical serialization, and the probe digest diverges from the
    initial digest. Otherwise returns None (representable or check not applicable).
    """
    if not chunked:
        # Single-shot delivery: representability is checked once at commit time.
        return

    if sender_capabilities.get("canonicalSerialization", False):
        # Sender commits to byte-deterministic emission; trust after handshake.
        return

    if digest_initial != digest_probe:
        raise ArtifactNegotiationError(
            f"artifact kind {artifact_kind!r} is admissible but unrepresentable: "
            f"digest {digest_initial!r} != probe {digest_probe!r}; "
            f"sender does not declare canonicalSerialization; "
            f"aborting stream and renegotiating to a representable kind"
        )
