feat(dispatch): link interface members to every implementer, not just one

resolve_interface_dispatch (shared by the C# and Swift gates) bailed out
entirely whenever an interface/protocol had more than one implementer,
on the reasoning that guessing which one a runtime call reaches would be
wrong. For a static structural graph the question isn't which one is
reached at runtime, it's which ones *can* be reached, and the answer with
N implementers is all N, not none -- so every chain through an injected
dependency with more than one implementation was left permanently cut at
the interface.

Each implementer is still subject to the existing single-owner guard
independently: an implementer that itself declares more than one method
of the same name is skipped on its own, without suppressing its siblings.
Confidence drops as the candidate set grows, using the extraction
rubric's own discrete INFERRED tiers (0.85 / 0.75 / 0.65 floor) instead of
inventing continuous precision.

Fixes #3800 for the dispatches_to half (interface->implementation calls).
The property-access half of that issue (a method edge for `c.Count`
through an interface-typed receiver) is a separate mechanism and is not
addressed here, to keep this PR to one concern.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
(cherry picked from commit f340494e07)
This commit is contained in:
Kamal Pandey authored and safishamsi committed 2026-10-13 11:14:16 +01:00
1 parent 750b564285
commit e7395e66d8
5 files changed
+155 -69

No files matched your search

+5 -4
View File
@@ -8,9 +8,10 @@ walk stops at the interface and every chain through an injected dependency is
cut at that point. On a Scrutor-scanned .NET service where every dependency is
an interface, that is most chains.
The mechanism — single-implementer, single-owner, per-method `dispatches_to`
edges at INFERRED — lives in `graphify.interface_dispatch` so that another
language can add a gate rather than a copy; this module fixes the C# gate.
The mechanism — single-owner, per-method `dispatches_to` edges at INFERRED,
fanning out to every implementer — lives in `graphify.interface_dispatch` so
that another language can add a gate rather than a copy; this module fixes
the C# gate.
Both ends of a pair must be declared in a `.cs` file: `implements` is
resolved by name, so in a mixed corpus a Java class declaring
`implements IReport` binds to a C# `IReport` when that is the only node with
@@ -34,5 +35,5 @@ def resolve_csharp_interface_dispatch(
all_nodes: list[dict],
all_edges: list[dict],
) -> None:
"""Link each single-implementer C# interface method to its implementation."""
"""Link each C# interface method to every implementation that owns it unambiguously."""
resolve_interface_dispatch(per_file, all_nodes, all_edges, suffixes=_CSHARP_SUFFIXES)
+73 -46
View File
@@ -1,4 +1,4 @@
"""Member-level dispatch from an interface requirement to its single implementer.
"""Member-level dispatch from an interface requirement to its implementer(s).
A call through an injected dependency lands on the interface's method node,
because that is what the call site names: `_report.Build()` where `_report` is
@@ -7,20 +7,26 @@ separate node, and nothing joins the two, so a directed walk stops at the
interface and every chain through an injected dependency is cut at that point.
This resolver runs after all files are extracted, with the merged corpus
available, and links the interface's method to the implementing method:
available, and links the interface's method to every implementing method:
ireport_ireport_build --dispatches_to--> report_report_build
ireport_ireport_build --dispatches_to--> alternatereport_alternatereport_build
It only fires when the interface has exactly one implementer and that
implementer owns exactly one method of the same name, the single-owner guard
`resolve_pascal_inherited_calls` and `resolve_ruby_member_calls` already use.
Walking `implements` to the one type that can serve the call mirrors what the
runtime does; guessing among several implementers would not, so anything
ambiguous is left alone.
For a static structural graph the question a call through the interface asks
is "what can this reach", not "which one does it reach at runtime" — with N
implementers the answer is all N, not none. Each implementer is still subject
to the single-owner guard `resolve_pascal_inherited_calls` and
`resolve_ruby_member_calls` already use: an implementer that itself declares
more than one method of the same name is ambiguous and is skipped on its own,
independently of its siblings.
The join is per method rather than per call site, so one edge reconnects every
call that reaches the interface method. Confidence is `INFERRED`: the target is
forced once there is a single implementer, but the source text never names it.
The join is per method rather than per call site, so one edge per implementer
reconnects every call that reaches the interface method. Confidence is
`INFERRED` and falls as the candidate set grows, on the extraction rubric's own
tiers: 0.85 ("strong inference") for a single implementer — the target is
forced, the source text just never names it — 0.75 for two, and a 0.65 floor
("weak inference") from three on, rather than inventing finer-grained
precision the rubric does not have.
The mechanism knows no language. Each language registers a thin wrapper
(`graphify.csharp_dispatch` for C# interfaces, #3003) that fixes the file
@@ -35,6 +41,22 @@ from graphify.rcfile import effective_suffix
DISPATCH_RELATION = "dispatches_to"
# Confidence falls as the implementer count grows, snapped to the extraction
# rubric's discrete INFERRED tiers (0.95/0.85/0.75/0.65/0.55) rather than a
# continuous function of count, which the rubric has no basis for (#2813
# forbids inventing precision a formula does not have).
_DISPATCH_CONFIDENCE_BY_IMPLEMENTER_COUNT = {1: 0.85, 2: 0.75}
_DISPATCH_CONFIDENCE_FLOOR = 0.65
def _dispatch_confidence(implementer_count: int) -> float:
"""INFERRED confidence for a dispatch edge, given how many implementers exist.
One implementer is forced — "strong inference". Three or more floors at
"weak inference" instead of continuing to fall per extra implementer.
"""
return _DISPATCH_CONFIDENCE_BY_IMPLEMENTER_COUNT.get(implementer_count, _DISPATCH_CONFIDENCE_FLOOR)
def declared_in(node: dict | None, suffixes: tuple[str, ...]) -> bool:
"""True when the node is a declaration that lives in a file with one of ``suffixes``.
@@ -74,12 +96,12 @@ def resolve_interface_dispatch(
*,
suffixes: tuple[str, ...],
) -> None:
"""Link each single-implementer interface method to its implementation.
"""Link each interface method to every implementation that owns it unambiguously.
Purely additive: the existing call edge to the interface method is left in
place, since the call site really does name the interface. ``suffixes`` is
the language gate: the interface and the implementer must both be declared
in a file with one of these extensions.
the language gate: the interface and each implementer must both be
declared in a file with one of these extensions.
"""
if not any(
declared_in(result, suffixes)
@@ -122,42 +144,47 @@ def resolve_interface_dispatch(
new_edges: list[dict] = []
for interface_nid, impls in implementers.items():
if len(impls) != 1:
continue
impl_nid = next(iter(impls))
interface_node = node_by_id.get(interface_nid)
impl_node = node_by_id.get(impl_nid)
if interface_node is None or impl_node is None:
if interface_node is None or not declared_in(interface_node, suffixes):
continue
# Both ends must be declarations in this language's files. A sourceless
# stub minted for a dangling reference carries no members worth
# dispatching to, and a cross-language pair is a name collision rather
# than an implementation.
if not declared_in(interface_node, suffixes) or not declared_in(impl_node, suffixes):
interface_methods = methods_of.get(interface_nid, {})
if not interface_methods:
continue
score = _dispatch_confidence(len(impls))
impl_methods = methods_of.get(impl_nid, {})
for name, declared in methods_of.get(interface_nid, {}).items():
if len(declared) != 1:
for impl_nid in impls:
impl_node = node_by_id.get(impl_nid)
# Both ends must be declarations in this language's files. A
# sourceless stub minted for a dangling reference carries no
# members worth dispatching to, and a cross-language pair is a
# name collision rather than an implementation.
if impl_node is None or not declared_in(impl_node, suffixes):
continue
candidates = impl_methods.get(name, set())
if len(candidates) != 1:
continue
source = next(iter(declared))
target = next(iter(candidates))
if source == target or (source, target) in existing_pairs:
continue
existing_pairs.add((source, target))
new_edges.append({
"source": source,
"target": target,
"relation": DISPATCH_RELATION,
"context": "call",
"confidence": "INFERRED",
"confidence_score": 0.85,
"source_file": str(impl_node.get("source_file", "")),
"source_location": impl_node.get("source_location"),
"weight": 1.0,
})
impl_methods = methods_of.get(impl_nid, {})
for name, declared in interface_methods.items():
if len(declared) != 1:
continue
# Ambiguity is per implementer: one implementer overloading a
# name does not disqualify its siblings from a clean match.
candidates = impl_methods.get(name, set())
if len(candidates) != 1:
continue
source = next(iter(declared))
target = next(iter(candidates))
if source == target or (source, target) in existing_pairs:
continue
existing_pairs.add((source, target))
new_edges.append({
"source": source,
"target": target,
"relation": DISPATCH_RELATION,
"context": "call",
"confidence": "INFERRED",
"confidence_score": score,
"source_file": str(impl_node.get("source_file", "")),
"source_location": impl_node.get("source_location"),
"weight": 1.0,
})
all_edges.extend(new_edges)
+4 -4
View File
@@ -6,9 +6,9 @@ requirement (#3673), because that is what the call site names. The conforming
directed walk stops at the protocol and every chain through an injected
dependency is cut at that point — the Swift twin of the C# gap #3003 closed.
The mechanism — single-conformer, single-owner, per-method `dispatches_to`
edges at INFERRED — lives in `graphify.interface_dispatch` and is shared with
the C# pass; this module fixes the Swift gate. Both ends of a pair must be
The mechanism — single-owner, per-method `dispatches_to` edges at INFERRED,
fanning out to every conformer — lives in `graphify.interface_dispatch` and
is shared with the C# pass; this module fixes the Swift gate. Both ends of a pair must be
declared in a `.swift` file: `implements` is resolved by name, so a Kotlin or
Java class declaring the same supertype name binds to the Swift protocol when
that is the only node with the name, and linking a requirement to its method
@@ -42,5 +42,5 @@ def resolve_swift_protocol_dispatch(
all_nodes: list[dict],
all_edges: list[dict],
) -> None:
"""Link each single-conformer Swift protocol requirement to its implementation."""
"""Link each Swift protocol requirement to every conformer that owns it unambiguously."""
resolve_interface_dispatch(per_file, all_nodes, all_edges, suffixes=_SWIFT_SUFFIXES)
+58 -8
View File
@@ -5,11 +5,11 @@ is what the call site names. The implementing `Report.Build()` is a separate
node with nothing joining the two, so a directed walk stops at the interface
and every chain through an injected dependency is cut there.
`resolve_csharp_interface_dispatch` links the interface's method to the
implementing method when the interface has exactly one implementer and that
implementer owns exactly one method of the same name. Ambiguity at either step
leaves the pair alone, the same single-owner guard the Pascal and Ruby
resolvers use.
`resolve_csharp_interface_dispatch` links the interface's method to every
implementing method, as long as that implementer owns exactly one method of
the same name. An implementer's own ambiguity leaves only that implementer's
edge out, the same single-owner guard the Pascal and Ruby resolvers use.
Confidence falls as the implementer count grows (0.85 / 0.75 / 0.65).
"""
from __future__ import annotations
@@ -96,13 +96,63 @@ def test_the_call_to_the_interface_method_is_kept(tmp_path):
assert (_find(r, ".Go()", "runner"), _find(r, ".Build()", "ireport")) in calls
def test_two_implementers_produce_no_edge(tmp_path):
dispatch, _ = _extract(tmp_path, {"S.cs": (
def test_two_implementers_both_get_an_edge_at_lower_confidence(tmp_path):
dispatch, r = _extract(tmp_path, {"S.cs": (
"public interface IR { void M(); }\n"
"public class A : IR { public void M() { } }\n"
"public class B : IR { public void M() { } }\n"
)})
assert not dispatch
iface_m = _find(r, ".M()", "ir")
assert dispatch == {
(iface_m, _find(r, ".M()", "s_a")),
(iface_m, _find(r, ".M()", "s_b")),
}
for edge in r["edges"]:
if edge["relation"] == "dispatches_to":
assert edge["confidence"] == "INFERRED"
assert edge["confidence_score"] == 0.75
def test_three_implementers_floor_at_the_weak_inference_tier(tmp_path):
dispatch, r = _extract(tmp_path, {"S.cs": (
"public interface IR { void M(); }\n"
"public class A : IR { public void M() { } }\n"
"public class B : IR { public void M() { } }\n"
"public class C : IR { public void M() { } }\n"
)})
assert len(dispatch) == 3
for edge in r["edges"]:
if edge["relation"] == "dispatches_to":
assert edge["confidence_score"] == 0.65
def test_one_ambiguous_implementer_does_not_suppress_its_siblings(tmp_path):
# B owns two distinct nodes named `.M()` (synthetic: the real extractor
# collapses same-named overloads into one node, so this models a case
# where it would not, e.g. partial classes merged incorrectly). B's
# ambiguity must stay scoped to B and not suppress A's clean match.
from graphify.csharp_dispatch import resolve_csharp_interface_dispatch
nodes = [
{"id": "iface", "label": "IR", "source_file": "a.cs", "_callable_class": True},
{"id": "a", "label": "A", "source_file": "a.cs", "_callable_class": True},
{"id": "b", "label": "B", "source_file": "a.cs", "_callable_class": True},
{"id": "iface_m", "label": ".M()", "source_file": "a.cs", "_callable": True},
{"id": "a_m", "label": ".M()", "source_file": "a.cs", "_callable": True},
{"id": "b_m1", "label": ".M()", "source_file": "a.cs", "_callable": True},
{"id": "b_m2", "label": ".M()", "source_file": "a.cs", "_callable": True},
]
edges = [
{"source": "a", "target": "iface", "relation": "implements"},
{"source": "b", "target": "iface", "relation": "implements"},
{"source": "iface", "target": "iface_m", "relation": "method"},
{"source": "a", "target": "a_m", "relation": "method"},
{"source": "b", "target": "b_m1", "relation": "method"},
{"source": "b", "target": "b_m2", "relation": "method"},
]
resolve_csharp_interface_dispatch([], nodes, edges)
dispatch = {(e["source"], e["target"]) for e in edges if e["relation"] == "dispatches_to"}
assert dispatch == {("iface_m", "a_m")}
def test_no_matching_member_produces_no_edge(tmp_path):
+15 -7
View File
@@ -6,10 +6,11 @@ conformer's `.fetch()` is a separate node with nothing joining the two, so a
directed walk stops at the protocol and every chain through an injected
dependency is cut there — the Swift twin of the C# gap #3003 closed.
`resolve_swift_protocol_dispatch` links the requirement to the implementing
method when the protocol has exactly one conformer and that conformer owns
exactly one method of the same name. Ambiguity at either step leaves the pair
alone, the same single-owner guard the C#, Pascal and Ruby resolvers use.
`resolve_swift_protocol_dispatch` links the requirement to every conforming
method, as long as that conformer owns exactly one method of the same name.
A conformer's own ambiguity leaves only that conformer's edge out, the same
single-owner guard the C#, Pascal and Ruby resolvers use. Confidence falls as
the conformer count grows (0.85 / 0.75 / 0.65).
"""
from __future__ import annotations
@@ -152,13 +153,20 @@ def test_class_conformer_of_a_protocol_in_another_file_is_a_known_limit(tmp_path
assert not dispatch
def test_two_conformers_produce_no_edge(tmp_path):
dispatch, _ = _extract(tmp_path, {"S.swift": (
def test_two_conformers_both_get_an_edge_at_lower_confidence(tmp_path):
dispatch, r = _extract(tmp_path, {"S.swift": (
"protocol Shape { func area() -> Double }\n"
"struct Square: Shape { func area() -> Double { 1 } }\n"
"struct Circle: Shape { func area() -> Double { 2 } }\n"
)})
assert not dispatch
requirement = _find(r, ".area()", "s_shape")
assert dispatch == {
(requirement, _find(r, ".area()", "square")),
(requirement, _find(r, ".area()", "circle")),
}
for edge in r["edges"]:
if edge["relation"] == "dispatches_to":
assert edge["confidence_score"] == 0.75
def test_no_matching_member_produces_no_edge(tmp_path):