Repository navigation
fix: make graph output deterministic (stop graphify-out churn) - #1010
Conversation
graphify-out regenerates differently on every `graphify update` even when no source changed, so the committed graph is perpetually dirty and the post-commit/post-checkout hooks fight every commit. Two independent nondeterminism sources, each fixed here: 1. Edge direction flips. build.py builds an undirected graph and stores direction in _src/_tgt; collapsing two edges onto the same node pair is last-write-wins, and unstable edge iteration order flips them run-to-run. Fixed by sorting edges by (source, target, relation) before the add loop. 2. Clustering churn. The networkx Louvain fallback iterates string-keyed sets whose order is randomized per-process by PYTHONHASHSEED, so community assignments differ run-to-run even with seed=42. Fixed by exporting PYTHONHASHSEED=0 in the generated post-commit and post-checkout hook scripts. With both fixes, `graphify update` is idempotent: rebuilding an already-converged graphify-out reproduces graph.json and GRAPH_REPORT.md byte-for-byte. Co-Authored-By: Claude Opus 4.7 <[email protected]>
safishamsi
left a comment
There was a problem hiding this comment.
Both fixes are correct — edge sort is consistent with the same pattern already in cluster.py, and PYTHONHASHSEED=0 in hooks is the right lever for Louvain hash-order sensitivity.
Two requests before merge:
-
Non-hook gap:
PYTHONHASHSEED=0only helps runs triggered by the post-commit/post-checkout hooks. Manualgraphify updateinvocations still churn. You mentioned adding a re-exec guard in__main__.py— that would complete the fix. Either add it here or open a follow-up issue so it doesn't get lost. -
Regression tests: two trivial additions would lock this in — an idempotency test in
test_build.pyassertingbuild_from_jsonoutput is invariant to input-edge ordering, and anassert 'PYTHONHASHSEED=0' in _HOOK_SCRIPTcheck intest_hooks.py.
Also worth clarifying in the PR description whether graph.html is now stable — it's listed as a churning file in the problem statement but isn't mentioned in the verification claim.
…ify-Labs#1010) graphify-out regenerates differently on every `graphify update` even when no source changed, so the committed graph is perpetually dirty and the post-commit/post-checkout hooks fight every commit. Two independent nondeterminism sources, each fixed here: 1. Edge direction flips. build.py builds an undirected graph and stores direction in _src/_tgt; collapsing two edges onto the same node pair is last-write-wins, and unstable edge iteration order flips them run-to-run. Fixed by sorting edges by (source, target, relation) before the add loop. 2. Clustering churn. The networkx Louvain fallback iterates string-keyed sets whose order is randomized per-process by PYTHONHASHSEED, so community assignments differ run-to-run even with seed=42. Fixed by exporting PYTHONHASHSEED=0 in the generated post-commit and post-checkout hook scripts. With both fixes, `graphify update` is idempotent: rebuilding an already-converged graphify-out reproduces graph.json and GRAPH_REPORT.md byte-for-byte. Co-authored-by: Claude Opus 4.7 <[email protected]>
Problem
graphify-out/(graph.json,graph.html,GRAPH_REPORT.md) regenerates differently on everygraphify updateeven when no source file changed. The committed graph is therefore perpetually dirty, and the post-commit / post-checkout hooks fight every commit and branch switch.There are two independent nondeterminism sources. This PR fixes both.
1. Edge direction flips (
build.py)build_from_jsonbuilds an undirectednx.Graphand stores the original direction in_src/_tgtedge attributes. When two edges collapse onto the same undirected node pair, last-write-wins — and because the edge iteration order isn't stable run-to-run, the serialized_src/_tgtflips (same edge appearsA→Bone run,B→Athe next). In a real graph this accounted for roughly half the churn.Fix: iterate edges in a deterministic order (
sortedbysource, target, relation) before the add loop, so the last-write outcome is stable.2. Clustering depends on
PYTHONHASHSEED(hooks.py)The
networkxLouvain fallback (used whengraspologicisn't installed) iterates string-keyed node/neighbour sets internally, whose order is randomized per-process by Python's hash randomization. Even withseed=42, community assignments differ run-to-run, so every node's community id churns.Empirically, pinning
PYTHONHASHSEED=0makes clustering fully deterministic. The git hooks invoke Python directly, so the env var has to be set in the generated hook scripts.Fix: export
PYTHONHASHSEED=0in both_HOOK_SCRIPTand_CHECKOUT_SCRIPT.Verification
With both fixes applied,
graphify updateis idempotent: rebuilding an already-convergedgraphify-outreproducesgraph.jsonandGRAPH_REPORT.mdbyte-for-byte across repeated runs. Verified on a ~3,770-node / ~12,900-edge real repository graph (stable at 49 communities across runs) where the working tree previously never stayed clean.Notes
graphify updatemanually outside the hooks, consider also settingPYTHONHASHSEED=0at CLI entry (e.g. a re-exec guard in__main__.py) — happy to add that here if you'd prefer.🤖 Generated with Claude Code