[Bug]: build_merge(dedup=False) refuses a no-op merge: #479 guard counts intentional ghost merges (#1145, #3344) as drops
What happened?
On 0.9.100, build_merge(..., dedup=False) raises the #479 ValueError on existing graphs even when the new chunk is empty, so nothing was re-extracted or pruned.
The nodes it reports as dropped are not lost by accident. They are removed by the intentional ghost-merge passes in build_from_json: #1145 (same source_file and label) and #3344 (a document node whose label names a file). The guard in build_merge only excuses the #1799 doc-twin case, so it counts these merges as unexplained drops.
The result is that a graph containing such twins can't be updated through build_merge(dedup=False) at all. This is a different case from #4019, whose reproducer now passes for me (5 -> 5).
Two things make this confusing:
build_from_json(..., dedup=False)on the first case below keeps both nodes (2 -> 2), whilebuild_merge(..., dedup=False)merges them. From reading the code, I think nodes loaded from disk get classified as AST by shape, which would skip thenot dedupcheck at the top of Pass 2. I haven't confirmed that.- The message suggests passing
prune_sources, but no source was meant to be removed here.
Steps to reproduce
import json
import tempfile
from pathlib import Path
from graphify.build import build_merge
EMPTY = {"nodes": [], "edges": [], "hyperedges": [], "input_tokens": 0, "output_tokens": 0}
def node(id_, label, source_file, loc):
return {"id": id_, "label": label, "file_type": "code", "source_file": source_file, "source_location": loc}
CASES = {
"same file, same label (#1145 path)": [
node("src_client", "client.py", "src/client.py", "L1"),
node("client_py", "client.py", "src/client.py", None),
],
"doc mentions the file by name (#3344 path)": [
node("src_client", "client.py", "src/client.py", "L1"),
node("plan_client", "src/client.py", "docs/plan.md", None),
],
}
for name, nodes in CASES.items():
with tempfile.TemporaryDirectory() as t:
graph = Path(t) / "graph.json"
graph.write_text(json.dumps({"directed": False, "multigraph": False, "graph": {}, "nodes": nodes, "links": []}), encoding="utf-8")
try:
G = build_merge([dict(EMPTY)], graph_path=str(graph), dedup=False)
print(f"{name}: 2 -> {G.number_of_nodes()}")
except ValueError as ex:
print(f"{name}: ValueError: {ex}")
Error output or graph output
0.9.100:
same file, same label (#1145 path): ValueError: graphify: build_merge would drop 1 node(s) from sources that were neither re-extracted nor pruned this run (e.g. 'src_client_py'); graph would go 2 → 1 nodes. Pass prune_sources explicitly if you intend to remove them. (#479)
doc mentions the file by name (#3344 path): ValueError: graphify: build_merge would drop 1 node(s) from sources that were neither re-extracted nor pruned this run (e.g. 'docs_plan_client'); graph would go 2 → 1 nodes. Pass prune_sources explicitly if you intend to remove them. (#479)
0.8.31:
same file, same label (#1145 path): 2 -> 2
doc mentions the file by name (#3344 path): 2 -> 2
Graphify version
0.9.100 (compared with 0.8.31)
Operating System
Windows 11
Python Version
3.14.3
Installation Method
pip, in an isolated venv
Additional context
Impact on real graphs: I ran the same empty-chunk merge on copies of 9 real graphs built with 0.8.31. All 9 pass on 0.8.31 with no IDs lost. On 0.9.100, 6 of the 9 are refused:
| Nodes on disk | Node count in the refusal message |
|---|---|
| 6809 | 6614 |
| 4025 | 3865 |
| 935 | 927 |
| 339 | 330 |
| 54 | 49 |
| 29 | 25 |
Removing the nodes that match these two patterns before merging lets 3 of the 6 pass. The other three still report 1, 21 and 9 nodes. I haven't reduced those, so there may be a third path. My filter only approximates the passes, so it may also just be missing some of them.
Re-keying the graph first with build_from_json + to_json(force=True) doesn't help: build_merge still refuses the result.
The guard itself is doing something useful, since it stops the write and nothing is lost. The problem is that there's no way through. Would you prefer that dedup=False also skips these two passes for nodes loaded from disk, or that the guard excuses them as it does for #1799? Either would unblock us. I can test a patch against these graphs.
Note: I only tested the library calls above. I didn't run a full semantic re-extraction on 0.9.100.
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.
Fixed in 0.9.102 (published to PyPI) via #4957. The build_merge guard no longer counts an intentional ghost fold as a dropped node; a genuine drop is still refused.