# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-an-a2a-agent-card-is-semantically-incompatible-despite-schema-valid-conformance.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-an-a2a-agent-card-is-semantically-incompatible-despite-schema-valid-conformance.warrant.json
# Markdown SHA-256:   f27fbde261a429122ecf8d6a976f201bda336e10973c8e30634e08f096c496bc
# Code AST SHA-256:   946197f37c5d088ab834fb95ee162062b3b7baa850bb2453c46da23454586385
# Verified Classes:   CardSemanticIncompatibility
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class CardSemanticIncompatibility(RuntimeError):
    """Raised when an A2A Agent Card is schema-valid but semantically
    incompatible with the client's state-continuity assumptions."""
    pass


def validate_card_for_stateful_use(card: dict, known_profiles: set[str]) -> None:
    caps = card.get("capabilities", {})
    streaming = caps.get("streaming", False)
    history = caps.get("stateTransitionHistory", False)

    # Signal 1: streaming without transition history correlates with
    # stateless task scoping in the observed vendor set.
    if streaming and not history:
        raise CardSemanticIncompatibility(
            f"agent {card.get('name')!r} advertises streaming without "
            "stateTransitionHistory; task scope is ambiguous, probe required"
        )

    # Signal 2: no agent-level default modes -> mode negotiation diverges.
    if "defaultInputModes" not in card and "defaultOutputModes" not in card:
        raise CardSemanticIncompatibility(
            f"agent {card.get('name')!r} omits agent-level default modes; "
            "per-skill mode semantics are vendor-divergent"
        )

    # Signal 3: provider not resolvable to a known conformance profile.
    provider = card.get("provider", {})
    org = provider.get("organization")
    if not org or org not in known_profiles:
        raise CardSemanticIncompatibility(
            f"provider {org!r} has no resolvable A2A conformance profile; "
            "ambiguous-field interpretation is unverifiable"
        )


def probe_task_scope(client, card: dict) -> bool:
    """Issue a cheap idempotent task, then a contextId-bearing follow-up.
    Returns True iff the remote preserves contextId across turns."""
    first = client.send_task(card, {"text": "ping", "contextId": None})
    ctx = first.get("contextId")
    if ctx is None:
        return False
    second = client.send_task(card, {"text": "ping", "contextId": ctx})
    # If the remote returns a fresh task.id for the same contextId, it is
    # stateless; if it returns the same task.id, it is stateful.
    return second.get("id") == first.get("id")


def promote_peer(client, card: dict, known_profiles: set[str]) -> str:
    try:
        validate_card_for_stateful_use(card, known_profiles)
        return "stateful"
    except CardSemanticIncompatibility:
        if probe_task_scope(client, card):
            return "stateful"
        return "stateless"
