Skip to content

Post-merge phantom nodes are never healed: union merge-driver resurrects deleted symbols, and the scoped post-commit rebuild cannot prune them #2418

Closed

Description

@mattwwatson

Post-merge phantom nodes are never healed: union merge-driver resurrects deleted symbols, and the scoped post-commit rebuild cannot prune them

Version: graphifyy 0.9.30 (installed from the v8 checkout) · macOS 15.6 (darwin 24.6) · Python 3.14.6

Summary

The graphify merge-driver union-merges graph.json, which by design discards
deletions made on either side of a merge. That is a reasonable trade to avoid
conflict markers, and a full graphify update . does correctly clean the
resurrected nodes.

The problem is that nothing in the installed hook set ever runs that full
update
. The post-commit hook rebuilds only the files listed in
GRAPHIFY_CHANGED, so a symbol resurrected by a merge inside a file that is not
touched again persists in graph.json indefinitely — long after the code is
gone. The graph reports it with an EXTRACTED confidence tag and a real
file:line, so there is no signal that it is stale.

This is distinct from #2406 (which is about losing real edges on the
incremental path). This one is the opposite direction: retaining nodes that no
longer exist.

Reproduction

Minimal synthetic repo, all commands verbatim:

mkdir proj && cd proj && git init -q .
mkdir -p src
cat > src/util.py <<'EOF'
def helper_alpha():
    return 1

def helper_doomed():
    return helper_alpha()
EOF
cat > src/main.py <<'EOF'
from src.util import helper_alpha, helper_doomed

def run():
    return helper_alpha() + helper_doomed()
EOF
git add -A && git commit -qm base

graphify update .            # baseline: contains src_util_helper_doomed
cp graphify-out/graph.json ../graph_BASE.json

# branch deletes helper_doomed
git checkout -qb branch-a
cat > src/util.py <<'EOF'
def helper_alpha():
    return 1
EOF
cat > src/main.py <<'EOF'
from src.util import helper_alpha

def run():
    return helper_alpha()
EOF
git add -A && git commit -qm "delete helper_doomed"
graphify update .            # correctly pruned -> node is gone
cp graphify-out/graph.json ../graph_A_deleted.json

# simulate the merge git would perform on graph.json
cp ../graph_BASE.json ../graph_CURRENT.json
graphify merge-driver ../graph_BASE.json ../graph_CURRENT.json ../graph_A_deleted.json

Step 1 — the merge resurrects the deleted node:

$ python3 -c "import json;d=json.load(open('../graph_CURRENT.json'));
print([n['id'] for n in d['nodes'] if 'doomed' in n['id'].lower()])"
['src_util_helper_doomed']

Step 2 — the scoped rebuild the post-commit hook performs does not clean it.
Put the merged graph back, commit a change to an unrelated file, and run exactly
what _REBUILD_BODY_COMMIT runs:

from pathlib import Path
from graphify.watch import _rebuild_code
_rebuild_code(Path('.'), changed_paths=[Path('src/unrelated.py')], force=False)
[graphify watch] Rebuilt: 8 nodes, 7 edges, 3 communities
helper_doomed still present: True      # <-- phantom survives

Step 3 — only an unscoped rebuild heals it:

$ graphify update .
[graphify watch] Rebuilt: 7 nodes, 7 edges, 2 communities
helper_doomed present: False           # <-- cleaned

Expected vs actual

Expected: after a merge, the graph converges back to the code within a
commit or two of normal hook-driven activity.

Actual: it never converges. src/util.py is never in GRAPHIFY_CHANGED
again unless someone happens to edit it, so the phantom node is permanent for
practical purposes. It is indistinguishable from a real node in
graphify explain / query / affected output.

Root cause

Three behaviours that are each individually reasonable compose into the bug:

  1. graphify/cli.py:2047 — the merge driver is _nx.compose(G_cur, G_oth), a
    pure union. %O (the merge base) is accepted as an argument but never
    loaded, so there is no way to distinguish "deleted on one side" from "added
    on the other".
  2. graphify/hooks.py:110 and graphify/hooks.py:141
    (_REBUILD_BODY_COMMIT) — the post-commit hook reads GRAPHIFY_CHANGED and
    passes changed_paths=changed into _rebuild_code, scoping re-extraction to
    the commit's files.
  3. graphify/hooks.py:298-300 — the post-commit hook deliberately exits early
    during merge/rebase/cherry-pick states, so the merge itself never triggers a
    rebuild either.

The prune path (_stale_graph_sources / _prune_graph_json_sources) only drops
nodes whose source_file is gone or newly excluded. Here the file still exists;
only a symbol inside it was deleted. That is repaired by re-extraction replacing
the file's nodes — which the scoped rebuild never performs for that file.

Suggested fix

Any one of these would close it:

  1. Install a post-merge hook that runs an unscoped _rebuild_code(root)
    (no changed_paths). This is the smallest change and matches the existing
    post-commit / post-checkout pattern in graphify/hooks.py. post-merge
    fires after a successful merge, which is exactly when the union damage lands.
  2. Have the merge driver mark the graph dirty — write a
    graphify-out/.needs_full_rebuild stamp when it runs, and have the next
    post-commit rebuild honour it by dropping changed_paths for that one run.
  3. Use %O in the merge driver to compute a real three-way merge: a node
    present in base and in one side but absent from the other was deleted, not
    added, and should not be resurrected. Most correct, most work.

(1) and (2) are complementary and both cheap; (3) is the principled fix.

Documentation gap

Independently of the code fix, README.md's Team setup section says
graphify hook install "also sets up a git merge driver so graph.json is never
left with conflict markers — two devs committing in parallel get their graphs
union-merged automatically." That is accurate but reads as though the merge case
is fully handled. It would help to state that a merge inflates the graph and that
an unscoped graphify update . is needed afterwards, until the above is fixed.

Activity

  1. safishamsi commented on Oct 13, 2026

    @safishamsi
    Member

    Fixed in v0.9.90 (graphifyy 0.9.90 on PyPI) via #3556.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions