graphify icon indicating copy to clipboard operation
graphify copied to clipboard

build_from_json collapses same-file, same-label nodes at different locations (even with dedup=False) — regression vs 0.8.31

Open cabgab-dev opened this issue 1 month ago • 2 comments

Summary

build_from_json (and therefore build_merge(..., dedup=False)) collapses nodes that share the same source_file and the same label but sit at different source_locations. In 0.8.31 they are all kept; in 0.9.74 only one survives. This is not the cross-file merge from #3094 — the nodes are in the same file and the control below (same label, different file) is preserved.

Typical victims: a JSON key that appears at several nesting levels (name at L2 and L5 of one plugin.json), and a heading repeated in two sections of one Markdown document. They are distinct nodes with distinct ids and locations, but they get merged even though dedup=False was requested.

Minimal reproduction

from graphify.build import build_from_json

def node(i, label, ft, src, loc):
    return {"id": i, "label": label, "file_type": ft, "source_file": src, "source_location": loc}

nodes = [
    node("pkg_name",        "name",  "code",     "pkg/plugin.json", "L2"),
    node("pkg_author_name", "name",  "code",     "pkg/plugin.json", "L5"),
    node("doc_s1_notes",    "Notas", "document", "docs/plan.md",    "L10"),
    node("doc_s2_notes",    "Notas", "document", "docs/plan.md",    "L80"),
    node("other_name",      "name",  "code",     "pkg/other.json",  "L2"),   # control: other file
]
G = build_from_json({"nodes": nodes, "edges": [], "hyperedges": []})
print(len(nodes), "->", G.number_of_nodes(), sorted({n["id"] for n in nodes} - set(G.nodes)))
version result
0.8.31 5 -> 5, nothing lost
0.9.74 5 -> 3, lost ['doc_s2_notes', 'pkg_name']

Real-world impact

On an existing, hand-curated graph (6,412 nodes / 9,308 edges), re-loading its own graph.json through build_from_json gives 6,119 nodes / 8,562 edges on 0.9.74 (0.8.31: unchanged). 286 nodes and 746 edges disappear; 284 of those 286 have a surviving node with the same source_file + label (210 code, 49 document, 11 rationale). None of them come from deleted files.

Consequence: build_merge(new_chunks, graph_path=..., dedup=False) with an empty chunk raises the #479 guard (build_merge would drop 217 node(s) from sources that were neither re-extracted nor pruned) and refuses to write. The guard does its job, but it means an existing graph can no longer be merged incrementally on 0.9.74 without losing nodes, even with dedup=False.

What I did not verify

I suspect the (source_file, label) key used by the ghost-duplicate merge pass in build_from_json (#1145 / #2068) but I have not confirmed that this is the code path. I also did not check intermediate versions between 0.8.31 and 0.9.74, so I can't say which release introduced it.

Expected

Nodes at different source_locations with different ids should survive dedup=False, or the collapse should be limited to the AST-vs-LLM twin case it was designed for (and be opt-out).

Environment: Windows 11, Python 3.14, graphifyy 0.8.31 vs 0.9.74 (fresh venv).

cabgab-dev avatar Oct 03 '26 00:10 cabgab-dev

Thanks for opening this issue, @cabgab-dev. A maintainer will take a look soon.

If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.

To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.

github-actions[bot] avatar Oct 03 '26 00:10 github-actions[bot]

Confirming this reproduces on current v8 (0.9.75): the repro from the issue gives 5 -> 3, losing pkg_name and doc_s2_notes. The other_name control (same label, different file) is kept, as reported.

It looks like the same-file, same-label collapse comes from the ghost-dedup in build_from_json (#1145 and its follow-ups): non-AST nodes are keyed on (source_file, label), and a later node on the same key is treated as a duplicate of the first. That is what removes the two name nodes at L2 and L5 and the two Notas headings.

The tension is that this key is what catches the LLM/AST ghost twins. A fix that keeps same-file nodes with different source_location would also let some real ghosts through, unless the location is part of the key or the collapse is limited to nodes with no location.

Is the collapse meant to apply to same-label nodes at different locations in one file? If you tell me which direction you want (keep them when locations differ, or leave the collapse and make it opt-out for dedup=False), I'm happy to build it with tests.

deepanshupal avatar Oct 04 '26 04:10 deepanshupal