# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-reject-an-a2a-push-notification-callback-whose-jwt-validates-against-the-tenant-that-issued-the-task-but-whose-webhook-endpoint.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-reject-an-a2a-push-notification-callback-whose-jwt-validates-against-the-tenant-that-issued-the-task-but-whose-webhook-endpoint.warrant.json
# Markdown SHA-256:   ffafe8e1501a2acc27d97a1f30c606b757e728ba9c098853cf2f0f2ca96bdbc4
# Code AST SHA-256:   e9fbbf6d9d558909e2e671462abaf0de0d214908e5b8cd10930ce697a41b5714
# Verified Classes:   CrossTenantPushRejected
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class CrossTenantPushRejected(RuntimeError):
    """Raised when a validly-signed A2A push callback would cross an egress
    boundary into a tenant whose allowlist does not permit the destination."""
    pass


def verify_push_callback(
    token: dict,
    issuer_jwks_valid: bool,
    resolved_dest_host: str,
    destination_tenant: str,
    issuer_tenant: str,
    egress_allowlist: dict,  # tenant -> set of permitted hosts
    seen_nonces: set,
) -> None:
    if not issuer_jwks_valid:
        raise CrossTenantPushRejected(
            f"signature invalid for issuer {issuer_tenant}"
        )

    dest_allowed = resolved_dest_host in egress_allowlist.get(destination_tenant, set())
    issuer_allowed = resolved_dest_host in egress_allowlist.get(issuer_tenant, set())

    if not (dest_allowed and issuer_allowed):
        raise CrossTenantPushRejected(
            "egress intersection empty: "
            f"host={resolved_dest_host} "
            f"dest_tenant={destination_tenant} dest_ok={dest_allowed} "
            f"issuer_tenant={issuer_tenant} issuer_ok={issuer_allowed}"
        )

    if token.get("aud") != destination_tenant:
        raise CrossTenantPushRejected(
            f"aud mismatch: token.aud={token.get('aud')} "
            f"expected={destination_tenant}"
        )

    if token.get("webhook") != resolved_dest_host:
        raise CrossTenantPushRejected(
            f"webhook claim {token.get('webhook')} does not bind to "
            f"resolved target {resolved_dest_host}"
        )

    nonce_key = (token.get("task_id"), destination_tenant, token.get("nonce"))
    if nonce_key in seen_nonces:
        raise CrossTenantPushRejected(
            f"replayed callback task_id={token.get('task_id')} "
            f"into tenant={destination_tenant}"
        )
    seen_nonces.add(nonce_key)
