Repository navigation
Description
Summary
build_from_json() is non-deterministic across separate process runs when given the exact same extraction JSON: the ghost-node merge picks a different "canonical" survivor depending on Python's per-process hash-randomization seed, because it iterates a set of node ids instead of a sorted/ordered sequence.
This is the same class of bug fixed in #1257 ("ghost-merge skips ambiguous (basename, label) collisions where two AST nodes share the same key"), but for the sibling case #1257 didn't cover: two non-AST (semantic/LLM) nodes sharing a (basename, label) key with no AST node present at all. That case still resolves via "first encountered while iterating a Python set" - i.e. still arbitrary, just not yet flagged as ambiguous the way the AST/AST case now is.
Environment
graphify (graphifyy) 0.9.12. Python 3.12.3. Reproduces regardless of platform since hash randomization is a CPython default, not OS-specific.
Root cause
graphify/build.py:
node_set = set(G.nodes()) # line 433 - a set, no ordering guarantee
...
for nid in node_set: # line 452 (Pass 1) and line 472 (Pass 2)
...
elif key not in _loc_nodes:
_loc_nodes[key] = nid # first-seen-in-set-iteration winsPython randomizes string hashing per process by default, so set iteration order for string node ids is stable within one process but differs across separate process invocations. When two non-AST nodes share a (basename, label) key (e.g. two docs with the same filename produce a concept node with the same label), whichever one Pass 1 happens to visit first becomes the "canonical" node the other gets merged/removed into - and that choice flips run to run on identical input.
Notably, the code is already aware set iteration is unsafe for this exact purpose - see the comment a few lines further down, around the alias-candidate loop: "...a Python set, so 'first' is hash-order, not anything meaningful." That awareness just wasn't extended to the Pass 1/Pass 2 ghost-merge loops above it.
Minimal repro
# repro.py
from graphify.build import build_from_json
extraction = {
"nodes": [
{
"id": "dir_a_update_build_merge",
"label": "build_merge() function",
"file_type": "concept",
"source_file": "dir_a/update.md",
"source_location": "L10",
},
{
"id": "dir_b_update_build_merge",
"label": "build_merge() function",
"file_type": "concept",
"source_file": "dir_b/update.md",
"source_location": "L12",
},
],
"edges": [],
}
G = build_from_json(extraction, directed=False)
print("surviving node ids:", sorted(G.nodes()))Running python3 repro.py six times back to back:
surviving node ids: ['dir_b_update_build_merge']
surviving node ids: ['dir_b_update_build_merge']
surviving node ids: ['dir_a_update_build_merge']
surviving node ids: ['dir_a_update_build_merge']
surviving node ids: ['dir_b_update_build_merge']
surviving node ids: ['dir_b_update_build_merge']
Same input, same code, different survivor - flips between dir_a_... and dir_b_... depending on the process's hash seed.
Real-world impact
Hit this on a real run (/graphify on a corpus with several per-tool-variant reference docs that share filenames like update.md, hooks.md, query.md across many directories - a very natural shape for any monorepo/multi-variant docs setup, not a contrived edge case).
First: graphify-out/graph.json content isn't reproducible from graphify-out/.graphify_extract.json alone - rebuilding "the same" graph in a fresh process can silently change which node id represents a given concept.
Second, and concretely what I hit: this breaks the documented Step 4 to Step 5 workflow (cluster in one process, relabel communities and regenerate the report in a second process). Community membership computed against the first build's node ids referenced a node id that the second build's rebuild KeyError'd on, in report.py's generate() calling analyze.py's _is_file_node(), because that id no longer existed in the second build's graph:
File "graphify/analyze.py", line 63, in _is_file_node
attrs = G.nodes[node_id]
KeyError: 'skills_trae_references_update_build_merge_function'
Third, more generally: any workflow that persists node ids across a rebuild (external references, --update, diffing two graph.json snapshots, etc.) can silently drift for reasons unrelated to any real change in the source corpus.
Suggested fix
Iterate sorted(node_set) (or any other deterministic order) in the Pass 1 and Pass 2 loops at build.py lines 452 and 472, the same way the edge-iteration loop just below already does on purpose ("Iterate edges in a deterministic order... Sorting fixes the last-write outcome"). Alternatively, extend the existing _loc_collisions ambiguous-key tracking from #1257 to also cover the non-AST/non-AST tie case, so an ambiguous key is skipped rather than arbitrarily resolved.
Happy to test a fix against the corpus that triggered this if useful.
Related (different root cause, same underlying pattern of PYTHONHASHSEED-driven set/dict iteration order): #1667 (non-determinism in cluster()'s post-Louvain split/reindex) and #1695 (non-determinism in LLM extraction sampling). Both explicitly note everything else in the pipeline looked deterministic in their testing - this issue pins down a third, distinct source in build_from_json() itself, upstream of clustering, that they didn't hit.
Activity
fix(build): make ghost-node merge deterministic across process runs (#…
safishamsi commentedon Jul 9, 2026
Fixed on
v8in 29a6954 (0.9.12). Thanks @erasmust-dotcom — the root-cause, minimal repro, and the pointer to the nearby "'first' is hash-order, not anything meaningful" comment made this a clean fix.Did both of the things you suggested, layered:
sorted(node_set)instead ofset(node_set), matching the deterministic-order fix the edge loop below already uses. That alone makes the survivor stable across process runs.(basename, label)from different files (yourdir_a/update.mdvsdir_b/update.mdcase) are distinct concepts with no AST canonical twin, so merging either way was also data loss, not just nondeterminism. They now both survive, mirroring the AST/AST guard. A genuine same-file duplicate (identicalsource_file) is still a real ghost and still collapses to one node, so this doesn't leave same-file LLM duplicates behind.Verified: your repro now prints
['dir_a_update_build_merge', 'dir_b_update_build_merge']on every run underPYTHONHASHSEED=random(6/6), the same-file-duplicate case still merges to one node deterministically (4/4 fresh procs), and the full suite passes (3118). Regression tests added for both the different-file (both survive) and same-file (still merges) cases.That's the third distinct hashseed-driven nondeterminism source pinned down (after #1667 clustering and #1695 sampling); this one was upstream of clustering in
build_from_jsonitself, exactly as you noted.fix(build): ghost-merge keys on full source_file, not bare basename (#…
fix(build): make ghost-node merge deterministic across process runs (G…
fix(build): ghost-merge keys on full source_file, not bare basename (G…
fix(build): make norm_to_id fallback resolution deterministic