# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-warrant-scope-divergence-during-a2a-v1-0-cross-org-federation-handshakes.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-warrant-scope-divergence-during-a2a-v1-0-cross-org-federation-handshakes.warrant.json
# Markdown SHA-256:   69b263a10df3f0b9d339e3e80a55b853a0b65be8c0b184fb6e6a7b9e075cdb3f
# Code AST SHA-256:   a059231ef2dcb97844b71bfba8f0656d60ad3431acff3496c6a126ab3404465b
# Verified Classes:   WarrantScopeDivergence
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class WarrantScopeDivergence(RuntimeError):
    """Raised when a Target's ontology-resolved action set does not cover the
    Origin's declared warrant actions, indicating cross-org scope divergence."""
    pass


def admit_federation_handshake(
    warrant: dict,
    target_ontology: dict,
    pinned_ontology_hash: str | None,
) -> dict:
    """
    Evaluate an incoming A2A v1.0 warrant against the Target's ontology.
    Returns an admission decision dict. Raises WarrantScopeDivergence when
    the resolved action superset does not cover the declared action set and
    no ontology pin exists to guarantee identical resolution.
    """
    declared_actions = set(warrant["actions"])
    selectors = warrant["resource_selectors"]

    # Pre-resolve selectors against the Target's own ontology.
    matched_classes = []
    for selector in selectors:
        matched_classes.extend(target_ontology.resolve(selector))

    resolved_action_union = set()
    for cls in matched_classes:
        resolved_action_union |= set(cls.granted_actions)

    covers = declared_actions.issubset(resolved_action_union)
    same_universe = (
        pinned_ontology_hash is not None
        and pinned_ontology_hash == target_ontology.version_hash
    )

    if covers:
        return {
            "status": "ADMITTED",
            "enforced_actions": sorted(resolved_action_union & declared_actions),
        }

    # Divergence detected: resolved classes do not grant everything declared.
    if same_universe:
        # Pinned identical ontology => this is a genuine over-broad warrant,
        # not a divergence. Hard refusal is correct.
        raise WarrantScopeDivergence(
            f"Warrant declares {sorted(declared_actions)} but pinned ontology "
            f"{target_ontology.version_hash} grants only "
            f"{sorted(resolved_action_union)}; Origin over-declared scope."
        )

    # No pin => ontology-relative divergence. Counter-offer narrowed scope
    # rather than refusing, so the Origin can re-issue with acknowledged scope.
    admitted = declared_actions & resolved_action_union
    if not admitted:
        raise WarrantScopeDivergence(
            "No action intersection between declared warrant and Target "
            "ontology-resolved classes; cannot narrow to a non-empty scope."
        )

    return {
        "status": "SCOPE_NARROWED",
        "enforced_actions": sorted(admitted),
        "rejected_actions": sorted(declared_actions - admitted),
        "target_ontology_hash": target_ontology.version_hash,
        "requires_origin_ack": True,
    }
