graphify icon indicating copy to clipboard operation
graphify copied to clipboard

[Bug]: graphify query prints a different answer on each run (BFS walks a set in hash order)

Open rohit-jsfreaky opened this issue 3 weeks ago • 3 comments

Pre-flight checks

  • [x] I have checked the Troubleshooting section in the README

What happened?

graphify query prints a different answer on every run, on the same graph. _bfs in graphify/serve.py walks each level as a set of node ids (frontier = set(start_nodes), for n in frontier:), so the EDGE lines come out in string-hash order, which Python randomizes per process. query is not in _HASHSEED_PINNED_COMMANDS, and the MCP server runs the same traversal in a long-running process, so nothing pins it. The order also decides which edges survive the token budget.

Distinct outputs over 10 runs (PYTHONHASHSEED 1 to 10):

repo query budget distinct outputs
flask how does url_for work 50k 10
click how does echo work 50k 10
django how does slugify work default 10
django how does the queryset build sql 50k 10

On a FastAPI template repo, graphify query 'how does cn() work' run 6 times gave 5 different outputs.

Steps to reproduce

cd flask && graphify update .
for s in 1 2 3; do PYTHONHASHSEED=$s graphify query "how does url_for work" --budget 50000 | md5sum; done

Three different hashes.

Error output or graph output

Example, first 3 contains EDGE lines on a tiny graph (one seed, 60 neighbours), seeds 1 / 2 / 3:

helper_7, helper_17, helper_6 | helper_1, helper_13, helper_17 | helper_18, helper_17, helper_8

Graphify version

0.9.105 (v8 at 5eedffa8)

Operating System

Windows (also seen on Linux)

Python Version

3.12

Installation Method

uv tool install (recommended)

Additional context

_dfs uses a list stack and is already stable. Related but different: #2817 and #1667 (cluster order), #3743 (hash seed pinned for other commands).

rohit-jsfreaky avatar Nov 09 '26 17:11 rohit-jsfreaky

Thanks for opening this issue, @rohit-jsfreaky. 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 Nov 09 '26 17:11 github-actions[bot]

The skill template has the same problem, and #5112 does not touch it. In graphify/skills/*/references/query.md the NetworkX fallback walks frontier as a set (line 128) and ranks with sorted(subgraph_nodes, key=relevance, reverse=True) (line 155). Most nodes score 0, so their order follows set hash order and the char_budget cut keeps different nodes on each run (reproduced with PYTHONHASHSEED=1..4 on 0.9.93; main still has it).

Breaking ties by node id fixed it for us:

for n in sorted(frontier, key=str):
...
ranked_nodes = sorted(subgraph_nodes,
                      key=lambda nid: (-relevance(nid), str(nid)))

maxseoguy avatar Nov 10 '26 18:11 maxseoguy

Fixed in 0.9.106 (now on PyPI) via #5112: BFS now walks levels in a fixed order so a query's edge output no longer varies with PYTHONHASHSEED.

safishamsi avatar Nov 10 '26 22:11 safishamsi