graphify icon indicating copy to clipboard operation
graphify copied to clipboard

Python: `calls` edges are deterministically deleted by `references` on the same node pair (alphabetical last-write-wins in build_from_json)

Open alandooley opened this issue 3 months ago • 1 comments

calls edges are systematically deleted by references on the same node pair

Version: graphifyy 0.9.32 Impact: call-graph queries return silently incomplete results. Not random — deterministic and one-directional.

Summary

When two edges share a node pair but differ in relation, the build keeps whichever relation sorts last alphabetically. Because "calls" < "references", a references edge (often just a return-type annotation) always overwrites the real calls edge.

This is not "an arbitrary edge wins" — it is a consistent bias against the single most semantically valuable relation in a code graph.

Root cause

Two lines interact:

  • build.py:924 sorts edges by (source, target, relation).
  • build.py:1052 does a plain last-write-wins G.add_edge(src, tgt, **attrs) into a simple nx.Graph.

Sorted ascending, calls is inserted first and references overwrites it.

Reproduction / measurement

Corpus: a private ~700-file Python repo (13.8k nodes, ~31k raw edges).

Before any workaround, of the node pairs carrying both calls and references:

collapsed groups with >1 relation: 348
SURVIVING relation counts: {'references': 330, 'indirect_call': 18}
DISCARDED relation counts : {'calls':      330, 'contains':      18}

330 of 330 calls edges were deleted. Zero survived.

Concrete example — a type annotation beating an actual call:

pair: ..._site_findings_compose_report -> ..._site_findings_sitereport
  IN  rel=references  loc=L260  ctx=return_type
  IN  rel=calls       loc=L271  ctx=call      <-- deleted
  OUT rel=references  loc=L260

Total calls in the built graph rose from 7,557 → 11,308 once this (plus single-pass extraction) was corrected locally.

Why the existing escape hatches don't cover it

  • directed=True does not help — DiGraph is still a simple graph; the same ordered pair still collides.
  • multigraph_compat.py ships a full capability probe (require_multigraph_capabilities()), but a package-wide grep finds zero call sites. The string --multigraph appears only in that file's own docstring and error message. There is no way to build or export a MultiGraph.
  • graphify extract --no-cluster does preserve relation-variants (it uses dedupe_edges() keyed on (source, target, relation)), but costs clustering, god nodes, and the whole analysis sidecar. Diagnostic valve, not a fix.
  • The consumer side is already multigraph-aware — cli.py:1202-1207 forces {"directed": True, "multigraph": True} on read for the path command (citing #2074), and build.edge_datas() handles parallel edges. The gap is purely build/persist.

Suggested fixes (in preference order)

  1. Relation priority on collapse. When collapsing a pair, prefer the higher-information relation (calls > references, indirect_call > contains) instead of alphabetical last-write-wins. Smallest change, fixes the bias immediately.
  2. Merge instead of overwrite. Keep the surviving edge but retain the discarded relations in a list attribute (e.g. also_relations: ["calls"], with their source_locations), so nothing is silently lost.
  3. Wire up the multigraph path that multigraph_compat.py was clearly written for, behind --multigraph.

Workaround used locally

Pre-build pass over the extraction JSON: for any (source, target) pair carrying both, drop the weaker relation so the stronger survives the last-write-wins. Re-prioritised 369 pairs / 387 edges; relation_variant_groups fell from 375 to 11.

Minor doc inconsistency spotted nearby

cli.py:3364-3370 justifies deduping so raw output matches "the clustered path (whose DiGraph collapses both)" — but the default clustered path builds nx.Graph, not DiGraph (build.py:750, directed=False). The comment describes a graph type the default pipeline never constructs.

alandooley avatar Aug 02 '26 12:08 alandooley

Fixes #2391.

The sort by (source, target, relation) before G.add_edge was added for deterministic collapse order, but that also meant alphabetical order picked the survivor whenever two different relations landed on the same (src, tgt) pair — references always beat calls since "calls" < "references". G.add_edge just overwrites, no merge.

Added a small _RELATION_PRIORITY table so the higher-information relation wins the collapse instead of whichever sorts last. Unlisted relations default to priority 0, so anything not in the table keeps the old behavior — no regression there.

Left out the multigraph route and the also_relations merge idea from the issue to keep this focused on the actual data-loss bug; can follow up separately if maintainers want either.

Added a test covering both input orderings (since the bug is order-dependent) confirming calls survives a collapse with references either way.

abhiramArise avatar Aug 02 '26 15:08 abhiramArise