Skip to content

fix: make graph output deterministic (stop graphify-out churn) - #1010

Merged
safishamsi merged 1 commit into
Graphify-Labs:v8from
atarutin:fix/deterministic-graph-output
May 27, 2026
Merged

safishamsi merged 1 commit into
Graphify-Labs:v8from
atarutin:fix/deterministic-graph-output

Conversation

@atarutin

Copy link
Copy Markdown
Contributor

Problem

graphify-out/ (graph.json, graph.html, GRAPH_REPORT.md) regenerates differently on every graphify update even 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_json builds an undirected nx.Graph and stores the original direction in _src / _tgt edge 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/_tgt flips (same edge appears A→B one run, B→A the next). In a real graph this accounted for roughly half the churn.

Fix: iterate edges in a deterministic order (sorted by source, target, relation) before the add loop, so the last-write outcome is stable.

2. Clustering depends on PYTHONHASHSEED (hooks.py)

The networkx Louvain fallback (used when graspologic isn't installed) iterates string-keyed node/neighbour sets internally, whose order is randomized per-process by Python's hash randomization. Even with seed=42, community assignments differ run-to-run, so every node's community id churns.

Empirically, pinning PYTHONHASHSEED=0 makes 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=0 in both _HOOK_SCRIPT and _CHECKOUT_SCRIPT.

Verification

With both fixes applied, graphify update is idempotent: rebuilding an already-converged graphify-out reproduces graph.json and GRAPH_REPORT.md byte-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

  • Fix 1 is a clean root-cause fix with no behavior change beyond stable ordering.
  • Fix 2 is scoped to the generated hook scripts. For users who run graphify update manually outside the hooks, consider also setting PYTHONHASHSEED=0 at CLI entry (e.g. a re-exec guard in __main__.py) — happy to add that here if you'd prefer.

🤖 Generated with Claude Code

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 safishamsi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Non-hook gap: PYTHONHASHSEED=0 only helps runs triggered by the post-commit/post-checkout hooks. Manual graphify update invocations 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.

  2. Regression tests: two trivial additions would lock this in — an idempotency test in test_build.py asserting build_from_json output is invariant to input-edge ordering, and an assert 'PYTHONHASHSEED=0' in _HOOK_SCRIPT check in test_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.

@safishamsi
safishamsi merged commit a54a542 into Graphify-Labs:v8 May 27, 2026
PerryLin-0420 pushed a commit to PerryLin-0420/graphify-decouple that referenced this pull request Aug 13, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants