# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-stale-capability-shadow-in-a2a-v1-0-agent-card-discovery.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-stale-capability-shadow-in-a2a-v1-0-agent-card-discovery.warrant.json
# Markdown SHA-256:   0b22c8537e96a5e400b0412575013b9ccaf7b6dd00e1e7893c95c936287ecf19
# Code AST SHA-256:   236443269aa4837d3c9b76101073d02d50e85f7b06ad06c3845d76b4985b3efe
# Verified Classes:   StaleCapabilityShadowError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class StaleCapabilityShadowError(RuntimeError):
    """Raised when a negotiated capability is not live on the remote runtime."""
    pass


def commit_task_with_liveness_check(
    card: dict,
    negotiated_skill: str,
    probe_fn,
    submit_fn,
    cache_bypass_fetch,
):
    advertised = {s["id"] for s in card.get("skills", [])}
    if negotiated_skill not in advertised:
        raise StaleCapabilityShadowError(
            f"skill {negotiated_skill!r} absent from card; negotiation invalid"
        )

    fresh_card = cache_bypass_fetch()
    fresh_skills = {s["id"] for s in fresh_card.get("skills", [])}
    if negotiated_skill not in fresh_skills:
        raise StaleCapabilityShadowError(
            f"skill {negotiated_skill!r} present in cached card "
            f"(version={card.get('version')}) but absent in cache-bypassed "
            f"fetch (version={fresh_card.get('version')}); stale-capability shadow"
        )

    live = probe_fn(negotiated_skill)
    if not live:
        raise StaleCapabilityShadowError(
            f"skill {negotiated_skill!r} advertised and freshly listed but "
            f"runtime probe returned not-live; refusing commitment"
        )

    return submit_fn(negotiated_skill)
