Skip to content

Commit bedf32e

Browse files
rajarshidattapysafishamsi
authored andcommitted
fix(wiki): count each incident edge once in the audit trail (#2635)
1 parent c838df8 commit bedf32e

2 files changed

Lines changed: 50 additions & 8 deletions

File tree

‎graphify/wiki.py‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,23 @@ def _community_article(
7373
top_nodes = sorted(nodes, key=lambda n: G.degree(n), reverse=True)[:25]
7474
cross = _cross_community_links(G, nodes, cid, labels, node_community or {})
7575

76-
# Edge confidence breakdown
77-
conf_counts: Counter = Counter()
78-
for nid in nodes:
79-
for neighbor in G.neighbors(nid):
80-
ed = edge_data(G, nid, neighbor)
81-
conf_counts[ed.get("confidence", "EXTRACTED")] += 1
76+
# Edge confidence breakdown, over every edge INCIDENT to the community (the
77+
# cross-community ones included — those are disproportionately the uncertain
78+
# edges, and AMBIGUOUS is what ARCHITECTURE.md flags for human review).
79+
#
80+
# ``G.edges(nbunch)`` reports each incident edge exactly once. Walking
81+
# ``nodes x G.neighbors`` instead visited an edge once per endpoint inside
82+
# the community, so an intra-community edge was counted TWICE while a
83+
# crossing one was counted once. Intra-community edges are overwhelmingly
84+
# the high-confidence EXTRACTED ones — that is what makes a community — so
85+
# the split was biased towards confidence and understated the review
86+
# burden. On a MultiGraph this also counts parallel edges individually
87+
# rather than collapsing them to the first (``edge_data``), which is the
88+
# same understatement: an AMBIGUOUS edge parallel to an EXTRACTED one used
89+
# to be invisible here.
90+
conf_counts: Counter = Counter(
91+
d.get("confidence", "EXTRACTED") for *_, d in G.edges(nodes, data=True)
92+
)
8293
total_edges = sum(conf_counts.values()) or 1
8394

8495
sources = sorted({G.nodes[n].get("source_file") or "" for n in nodes} - {""})

‎tests/test_wiki.py‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,42 @@ def test_community_article_shows_cohesion(tmp_path):
9696

9797

9898
def test_community_article_has_audit_trail(tmp_path):
99+
"""Each incident edge is counted exactly ONCE (#2633).
100+
101+
The Parsing Layer (n1, n2) touches two edges: n1-n2 (EXTRACTED, both
102+
endpoints inside) and n1-n3 (INFERRED, crossing out). Counting
103+
``nodes x G.neighbors`` visited the intra-community edge from both ends and
104+
the crossing one from only one, reporting EXTRACTED 2 (67%) / INFERRED 1
105+
(33%) — biased towards confidence, which understates the AMBIGUOUS review
106+
burden the section exists to surface.
107+
"""
99108
G = _make_graph()
100109
to_wiki(G, COMMUNITIES, tmp_path, community_labels=LABELS)
101110
parsing = (tmp_path / "Parsing_Layer.md").read_text()
102-
assert "EXTRACTED" in parsing
103-
assert "INFERRED" in parsing
111+
audit = parsing[parsing.index("## Audit Trail"):]
112+
assert "- EXTRACTED: 1 (50%)" in audit, audit
113+
assert "- INFERRED: 1 (50%)" in audit, audit
114+
assert "- AMBIGUOUS: 0 (0%)" in audit, audit
115+
116+
117+
def test_audit_trail_counts_parallel_edges_individually(tmp_path):
118+
"""On a MultiGraph each parallel edge is its own row in the split (#2633).
119+
120+
Collapsing them to the first (``edge_data``) hid an AMBIGUOUS edge behind an
121+
EXTRACTED one running between the same pair — the same understatement as the
122+
double-count above.
123+
"""
124+
G = nx.MultiGraph()
125+
G.add_node("n1", label="parse", file_type="code", source_file="parser.py")
126+
G.add_node("n2", label="validate", file_type="code", source_file="parser.py")
127+
G.add_edge("n1", "n2", relation="calls", confidence="EXTRACTED", weight=1.0)
128+
G.add_edge("n1", "n2", relation="references", confidence="AMBIGUOUS", weight=1.0)
129+
130+
to_wiki(G, {0: ["n1", "n2"]}, tmp_path, community_labels={0: "Parsing Layer"})
131+
audit = (tmp_path / "Parsing_Layer.md").read_text()
132+
audit = audit[audit.index("## Audit Trail"):]
133+
assert "- EXTRACTED: 1 (50%)" in audit, audit
134+
assert "- AMBIGUOUS: 1 (50%)" in audit, audit
104135

105136

106137
def test_god_node_article_has_connections(tmp_path):

0 commit comments

Comments
 (0)