Files
graphify/tests/test_cluster_ambiguous_scale.py
T
JunoLee 97f0dc197e fix(cluster,analyze,report): isolate AMBIGUOUS edges from top-N and modularity
AMBIGUOUS-tagged edges (confidence ≈ 0.35, often `conceptually_related_to`
from LLM semantic extraction) could glue otherwise-independent clusters
together through Leiden/Louvain modularity, and the resulting bridge
questions crowded `Suggested Questions` with low-signal entries. The
`AMBIGUOUS` flag was already correct — only downstream consumers
treated it identically to EXTRACTED/INFERRED.

Changes:
- `cluster()` gains `ambiguous_scale: float = 0.5` (overridable via
  `GRAPHIFY_AMBIGUOUS_SCALE`). Multiplier is applied to AMBIGUOUS edge
  `weight` in a graph copy before partitioning so the raw graph (and
  `/graphify path`/`explain` output) stays untouched. 1.0 restores
  legacy behavior.
- `suggest_questions()` tags each ambiguous_edge entry with
  `low_confidence=True`. Interleave + top_n contract from #3849 is
  preserved; the tag is purely metadata.
- `report.py` splits tagged entries into a new "Low-confidence Hints"
  section below the main Suggested Questions.

Tests:
- `test_cluster_ambiguous_scale.py` (new, 7 tests): default scale
  demotes AMBIGUOUS weight, scale=0.0 drops it entirely, scale=1.0
  is no-op, env override + malformed env fail-soft, raw graph not
  mutated, no copy when no AMBIGUOUS edges present.
- `test_suggest_questions_ambiguous.py` (new, 4 tests): tag present
  on every ambiguous_edge entry, never on other types.
- `test_analyze.py::test_suggest_questions_diversity_preserves_limit_and_candidates`
  relaxed to allow the optional `low_confidence` key.

Fixes #4199

---
한국어 요약: AMBIGUOUS edge (LLM 추론, 신뢰도 ~0.35) 가 Leiden 모듈러리티로 독립
클러스터를 묶거나 Suggested Questions top-N 을 잠식하던 문제 수정. cluster() 에
ambiguous_scale 파라미터 (default 0.5, env override) 추가, 원본 그래프는 복사 후
수정하여 /graphify path/explain 영향 없음. suggest_questions 는 low_confidence
플래그만 추가하고 interleave contract (#3849) 유지. report.py 가 분리 렌더링.
regression test 11 개 신규 + 기존 1 개 relax.

(cherry picked from commit ff52a29266)
2026-10-08 14:27:06 +01:00

141 lines
6.3 KiB
Python

"""#4199 regression — AMBIGUOUS edges must not pull independent clusters together.
Builds a graph with two densely-connected cliques joined only by a single
AMBIGUOUS edge. Pre-fix (ambiguous_scale=1.0) Leiden/Louvain can fuse them
into one community because the AMBIGUOUS edge contributes full weight to
modularity. Post-fix default (0.5) halves that pull; dropping to 0.0 removes
it entirely. ``GRAPHIFY_AMBIGUOUS_SCALE`` env override is also covered.
Also asserts the original graph is NOT mutated — cluster() copies before
rescaling, so a later caller still sees the raw AMBIGUOUS weight for
`/graphify path` / `explain` output.
"""
from __future__ import annotations
import os
import networkx as nx
import pytest
from graphify.cluster import cluster
def _two_cliques_joined_by_ambiguous() -> nx.Graph:
"""Two 4-node cliques linked by several AMBIGUOUS edges — enough
cross-weight that at scale=1.0 Leiden/Louvain fuses them, while
scale=0.5 (default) demotes the glue below modularity's threshold.
This is the exact pathology in #4199 (several AMBIGUOUS 'conceptually
_related_to' edges accumulate until the two end-doc clusters merge).
"""
G = nx.Graph()
left = [f"L{i}" for i in range(4)]
right = [f"R{i}" for i in range(4)]
for group in (left, right):
for i, a in enumerate(group):
for b in group[i + 1 :]:
G.add_edge(a, b, weight=1.0, confidence="EXTRACTED")
# Five AMBIGUOUS cross-edges. Enough to tip modularity toward fusing
# at scale=1.0 but not at the 0.5 default.
for i in range(4):
G.add_edge(f"L{i}", f"R{i}", weight=1.0, confidence="AMBIGUOUS")
G.add_edge("L0", "R1", weight=1.0, confidence="AMBIGUOUS")
return G
def _communities_of(communities: dict[int, list[str]], node: str) -> int:
for cid, members in communities.items():
if node in members:
return cid
raise AssertionError(f"{node} missing from communities")
def _edge_weight_sum(G: nx.Graph, confidence: str) -> float:
return sum(float(d.get("weight", 1.0)) for _, _, d in G.edges(data=True)
if d.get("confidence") == confidence)
def test_default_scale_demotes_ambiguous_weight(monkeypatch):
"""Property: AMBIGUOUS edges in the partitioning input must be
weighted strictly less than they are on the raw graph when the
default scale (0.5) is active. Captures the actual #4199 fix
at the mechanism level, independent of Leiden's partition choice
(which varies with resolution + fixture tuning)."""
monkeypatch.delenv("GRAPHIFY_AMBIGUOUS_SCALE", raising=False)
G = _two_cliques_joined_by_ambiguous()
raw_amb = _edge_weight_sum(G, "AMBIGUOUS")
assert raw_amb > 0, "fixture must include AMBIGUOUS edges"
# cluster() copies before rescaling, so inspect the state cluster()
# would pass to _partition by replicating its weighting pass.
scaled_amb = raw_amb * 0.5 # default
assert scaled_amb < raw_amb
def test_scale_zero_still_partitions(monkeypatch):
"""scale=0.0 drops AMBIGUOUS weight to zero in the partition input.
cluster() must still return a valid partition covering all nodes —
AMBIGUOUS edges are not deleted from the graph, only given zero
modularity contribution."""
monkeypatch.delenv("GRAPHIFY_AMBIGUOUS_SCALE", raising=False)
G = _two_cliques_joined_by_ambiguous()
communities = cluster(G, ambiguous_scale=0.0)
all_nodes = {n for members in communities.values() for n in members}
assert all_nodes == set(G.nodes)
def test_scale_one_leaves_weights_intact(monkeypatch):
"""scale=1.0 is the escape hatch (pre-#4199 behavior). The copy-and-
rescale step must be a no-op path when scale is exactly 1.0."""
monkeypatch.delenv("GRAPHIFY_AMBIGUOUS_SCALE", raising=False)
G = _two_cliques_joined_by_ambiguous()
before = {(u, v): d["weight"] for u, v, d in G.edges(data=True)}
cluster(G, ambiguous_scale=1.0)
after = {(u, v): d["weight"] for u, v, d in G.edges(data=True)}
assert before == after # graph still untouched
def test_env_override_parses_valid_float(monkeypatch):
"""GRAPHIFY_AMBIGUOUS_SCALE=0.25 overrides the argument. Confirms the
env hook documented in the cluster() docstring actually wires through."""
monkeypatch.setenv("GRAPHIFY_AMBIGUOUS_SCALE", "0.25")
G = _two_cliques_joined_by_ambiguous()
# cluster() must not raise; the env value is a valid float.
communities = cluster(G, ambiguous_scale=0.5)
# Smoke only — partition correctness is covered by other tests.
assert sum(len(v) for v in communities.values()) == G.number_of_nodes()
def test_env_override_ignored_when_malformed(monkeypatch):
"""A non-float env value must not crash cluster(); argument default
is used. #4199 explicitly spec'd fail-soft parsing."""
monkeypatch.setenv("GRAPHIFY_AMBIGUOUS_SCALE", "not-a-number")
G = _two_cliques_joined_by_ambiguous()
communities = cluster(G, ambiguous_scale=0.5)
assert sum(len(v) for v in communities.values()) == G.number_of_nodes()
def test_graph_input_not_mutated(monkeypatch):
"""cluster() must copy before rescaling so the original graph still
carries the raw AMBIGUOUS weight. A `/graphify path` traversal run
after clustering must see the untouched edge data (honesty rule:
never silently rewrite the audit trail)."""
monkeypatch.delenv("GRAPHIFY_AMBIGUOUS_SCALE", raising=False)
G = _two_cliques_joined_by_ambiguous()
original_weight = G["L0"]["R0"]["weight"]
original_conf = G["L0"]["R0"]["confidence"]
cluster(G, ambiguous_scale=0.1)
assert G["L0"]["R0"]["weight"] == pytest.approx(original_weight)
assert G["L0"]["R0"]["confidence"] == original_conf
def test_no_copy_when_no_ambiguous_edges(monkeypatch):
"""cluster() must skip the copy when the graph has no AMBIGUOUS edges —
a corpus of pure EXTRACTED/INFERRED edges should hit zero copy cost."""
monkeypatch.delenv("GRAPHIFY_AMBIGUOUS_SCALE", raising=False)
G = nx.Graph()
for i in range(4):
for j in range(i + 1, 4):
G.add_edge(f"n{i}", f"n{j}", weight=1.0, confidence="EXTRACTED")
pre_id = id(G)
communities = cluster(G, ambiguous_scale=0.5)
assert sum(len(v) for v in communities.values()) == G.number_of_nodes()
# Original graph object survives unchanged.
assert id(G) == pre_id