fix(cli): recover edge direction from _src/_tgt in path and explain (#2309)
graphify path decided hop direction from the persisted source/target order, so a link stored in flipped order (pre-#563 graphs, raw dumps, merge-driver output) printed backwards; explain had the same defect, and the query/merge load shims clobbered in-file _src/_tgt markers. All now honor _src/_tgt (the build-side direction truth), falling back to arc order for markerless files. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
c8163d8338
commit
50be9dcd36
+41
-10
@@ -914,10 +914,17 @@ def dispatch_command(cmd: str) -> None:
|
||||
# a seed with no outgoing edges. Direction is instead preserved
|
||||
# per-edge below (mirrors graphify/build.py's _src/_tgt pattern)
|
||||
# so the *rendering* stays correct without narrowing traversal.
|
||||
# Keep in-file markers when present (#2309): unconditionally
|
||||
# overwriting them with source/target would clobber the true
|
||||
# direction of a link persisted in flipped endpoint order.
|
||||
_raw = dict(
|
||||
_raw,
|
||||
links=[
|
||||
{**link, "_src": link.get("source"), "_tgt": link.get("target")}
|
||||
{
|
||||
**link,
|
||||
"_src": link.get("_src", link.get("source")),
|
||||
"_tgt": link.get("_tgt", link.get("target")),
|
||||
}
|
||||
for link in _raw.get("links", [])
|
||||
],
|
||||
)
|
||||
@@ -1259,12 +1266,19 @@ def dispatch_command(cmd: str) -> None:
|
||||
# direction — never a fabricated `calls` (#2074). A pair may carry
|
||||
# several parallel relations; show all, and fall back to an honest
|
||||
# "related" when the stored edge has no relation.
|
||||
if G.has_edge(u, v):
|
||||
datas = edge_datas(G, u, v)
|
||||
forward = True
|
||||
else:
|
||||
datas = edge_datas(G, v, u)
|
||||
forward = False
|
||||
# Direction truth lives in the per-link _src/_tgt markers (#2309):
|
||||
# undirected NetworkX storage canonicalizes endpoint order, so the
|
||||
# persisted source/target arc can be flipped relative to the real
|
||||
# caller→callee direction. Recover it from _src when present, else
|
||||
# fall back to the loaded arc tail (markerless canonical files keep
|
||||
# today's behavior).
|
||||
fwd, bwd = [], []
|
||||
for a, b in ((u, v), (v, u)):
|
||||
if G.has_edge(a, b):
|
||||
for d in edge_datas(G, a, b):
|
||||
(fwd if d.get("_src", a) == u else bwd).append(d)
|
||||
datas = fwd or bwd
|
||||
forward = bool(fwd)
|
||||
rels = sorted({d.get("relation") for d in datas if d.get("relation")})
|
||||
rel = "/".join(rels) if rels else "related"
|
||||
confs = sorted({d.get("confidence") for d in datas if d.get("confidence")})
|
||||
@@ -1360,10 +1374,20 @@ def dispatch_command(cmd: str) -> None:
|
||||
print(f" Degree: {G.degree(nid)}")
|
||||
from graphify.build import edge_data
|
||||
connections: list[tuple[str, str, dict]] = [] # (direction, neighbor_id, edge_data)
|
||||
# Classify by the edge's TRUE direction, not the loaded arc order:
|
||||
# a link persisted in flipped endpoint order carries its truth in the
|
||||
# per-edge _src marker (#2309). Markerless edges fall back to the arc
|
||||
# tail (today's behavior).
|
||||
for nb in G.successors(nid):
|
||||
connections.append(("out", nb, edge_data(G, nid, nb)))
|
||||
_ed = edge_data(G, nid, nb)
|
||||
connections.append(
|
||||
("out" if _ed.get("_src", nid) == nid else "in", nb, _ed)
|
||||
)
|
||||
for nb in G.predecessors(nid):
|
||||
connections.append(("in", nb, edge_data(G, nb, nid)))
|
||||
_ed = edge_data(G, nb, nid)
|
||||
connections.append(
|
||||
("in" if _ed.get("_src", nb) == nb else "out", nb, _ed)
|
||||
)
|
||||
if connections:
|
||||
print(f"\nConnections ({len(connections)}):")
|
||||
connections.sort(key=lambda c: G.degree(c[1]), reverse=True)
|
||||
@@ -2103,10 +2127,17 @@ def dispatch_command(cmd: str) -> None:
|
||||
data = dict(data, links=data["edges"])
|
||||
# Preserve stored edge direction across undirected node_link_graph (#2261).
|
||||
# Mirrors cli.py's query pattern and export.py's _src/_tgt restoration.
|
||||
# Keep in-file markers when present (#2309): unconditionally
|
||||
# overwriting them with source/target would clobber the true
|
||||
# direction of a link persisted in flipped endpoint order.
|
||||
data = dict(
|
||||
data,
|
||||
links=[
|
||||
{**link, "_src": link.get("source"), "_tgt": link.get("target")}
|
||||
{
|
||||
**link,
|
||||
"_src": link.get("_src", link.get("source")),
|
||||
"_tgt": link.get("_tgt", link.get("target")),
|
||||
}
|
||||
for link in data.get("links", [])
|
||||
],
|
||||
)
|
||||
|
||||
@@ -192,3 +192,96 @@ def test_path_relation_fallback_related_when_missing(monkeypatch, tmp_path, caps
|
||||
out = _run(monkeypatch, gp, "Alpha", "Beta", capsys)
|
||||
assert "--related-->" in out
|
||||
assert "---->" not in out.replace("--related-->", "")
|
||||
|
||||
|
||||
# ── #2309: hop direction must honor _src/_tgt markers, not stored arc order ──
|
||||
|
||||
def _flipped_marker_graph(tmp_path):
|
||||
"""3-node chain where the middle link is PERSISTED in flipped endpoint
|
||||
order (source/target swapped) but carries its direction truth in the
|
||||
per-link _src/_tgt markers — the shape produced by pre-#563 graphs, raw
|
||||
node_link_data dumps, and undirected-storage canonicalization."""
|
||||
data = {
|
||||
"directed": False, "multigraph": False, "graph": {},
|
||||
"nodes": [
|
||||
{"id": "ingest", "label": "ingest.ts", "source_file": "src/ingest.ts"},
|
||||
{"id": "logger", "label": "logger.ts", "source_file": "src/logger.ts"},
|
||||
{"id": "draft", "label": "draft-generator.ts",
|
||||
"source_file": "src/draft-generator.ts"},
|
||||
],
|
||||
"links": [
|
||||
# Canonical order + matching markers.
|
||||
{"source": "ingest", "target": "logger",
|
||||
"_src": "ingest", "_tgt": "logger",
|
||||
"relation": "calls", "confidence": "EXTRACTED"},
|
||||
# FLIPPED persisted order; truth is draft --imports_from--> logger.
|
||||
{"source": "logger", "target": "draft",
|
||||
"_src": "draft", "_tgt": "logger",
|
||||
"relation": "imports_from", "confidence": "EXTRACTED"},
|
||||
],
|
||||
}
|
||||
p = tmp_path / "graph.json"
|
||||
p.write_text(json.dumps(data))
|
||||
return p
|
||||
|
||||
|
||||
def test_path_direction_recovered_from_src_tgt_markers(monkeypatch, tmp_path, capsys):
|
||||
"""#2309: a hop over a link stored in flipped order must render the TRUE
|
||||
direction from its _src/_tgt markers, not the persisted arc order."""
|
||||
p = _flipped_marker_graph(tmp_path)
|
||||
out = _run(monkeypatch, p, "ingest", "draft-generator", capsys)
|
||||
assert "Shortest path (2 hops):" in out
|
||||
assert "ingest.ts --calls [EXTRACTED]--> logger.ts" in out
|
||||
# True direction is draft -> logger, so the logger->draft hop is reversed.
|
||||
assert "logger.ts <--imports_from [EXTRACTED]-- draft-generator.ts" in out
|
||||
assert "--imports_from [EXTRACTED]-->" not in out
|
||||
|
||||
|
||||
def test_path_canonical_marker_graph_still_forward(monkeypatch, tmp_path, capsys):
|
||||
"""#2309 control: a to_json-shaped graph whose markers AGREE with the
|
||||
persisted source/target order keeps rendering forward (no regression)."""
|
||||
data = {
|
||||
"directed": False, "multigraph": False, "graph": {},
|
||||
"nodes": [
|
||||
{"id": "a", "label": "Alpha", "source_file": "a.py"},
|
||||
{"id": "b", "label": "Beta", "source_file": "b.py"},
|
||||
],
|
||||
"links": [
|
||||
{"source": "a", "target": "b", "_src": "a", "_tgt": "b",
|
||||
"relation": "calls", "confidence": "EXTRACTED"},
|
||||
],
|
||||
}
|
||||
gp = tmp_path / "graph.json"
|
||||
gp.write_text(json.dumps(data))
|
||||
out = _run(monkeypatch, gp, "Alpha", "Beta", capsys)
|
||||
assert "Alpha --calls [EXTRACTED]--> Beta" in out
|
||||
# And walking the same edge backwards still reverses the arrow.
|
||||
out = _run(monkeypatch, gp, "Beta", "Alpha", capsys)
|
||||
assert "Beta <--calls [EXTRACTED]-- Alpha" in out
|
||||
|
||||
|
||||
def test_explain_direction_recovered_from_src_tgt_markers(monkeypatch, tmp_path, capsys):
|
||||
"""#2309: explain's in/out classification must honor _src markers — an
|
||||
edge persisted as hub->spoke but truly spoke->hub is an IN edge of hub."""
|
||||
data = {
|
||||
"directed": False, "multigraph": False, "graph": {},
|
||||
"nodes": [
|
||||
{"id": "hub", "label": "hub.ts", "source_file": "src/hub.ts"},
|
||||
{"id": "spoke", "label": "spoke.ts", "source_file": "src/spoke.ts"},
|
||||
],
|
||||
"links": [
|
||||
# Persisted arc hub->spoke, but the markers say spoke calls hub.
|
||||
{"source": "hub", "target": "spoke",
|
||||
"_src": "spoke", "_tgt": "hub",
|
||||
"relation": "calls", "confidence": "EXTRACTED"},
|
||||
],
|
||||
}
|
||||
gp = tmp_path / "graph.json"
|
||||
gp.write_text(json.dumps(data))
|
||||
monkeypatch.setattr(mainmod, "_check_skill_version", lambda _: None)
|
||||
monkeypatch.setattr(mainmod.sys, "argv",
|
||||
["graphify", "explain", "hub", "--graph", str(gp)])
|
||||
mainmod.main()
|
||||
out = capsys.readouterr().out
|
||||
assert "<-- spoke.ts [calls]" in out
|
||||
assert "--> spoke.ts" not in out
|
||||
|
||||
Reference in New Issue
Block a user