Skip to content

build_merge() defaults to directed=False, so graphify update silently downgrades a directed graph to undirected — betweenness becomes meaningless with no warning #2342

Closed

Description

@lafetaylor2

Summary

graphify update (incremental mode) calls build_merge() without passing directed=, which defaults to False. Since the merged graph is then written back with to_json(..., force=True), a graph built with --directed becomes undirected after any routine refresh, and nothing in stdout or the report says so.

The saved directed flag round-trips correctly everywhere else — cluster-only inherits it (cli.py:1681, build_from_json(_raw, directed=_directed)), and #801 already went to the trouble of preserving per-edge source/target through the merge. It's specifically the graph type that is dropped.

Verified against v1.0.0 (current default branch) and reproduced on 0.8.27.

Why it matters

Betweenness centrality is only meaningful on a directed graph. Undirected, a leaf utility that everything imports becomes a fake bridge: every pair of its importers appears two hops apart "through" it, though no control flow passes through it at all.

On a real 4,607-node / 10,760-edge polyglot graph, a logging helper with in_degree=176, out_degree=0 — a pure sink — ranked as the #1 god node at 0.090 betweenness. Rebuilt directed, it scores exactly 0.0000, along with three other high-degree sinks that had been occupying most of the top five. The genuine bridges were invisible until those four dropped out.

So the failure mode isn't "the graph looks a bit different" — it's that ## God Nodes and the betweenness-derived ## Suggested Questions both silently start reporting ubiquity instead of architecture, and there is no signal that the graph changed type.

Reproduction

Minimal, no corpus required:

import json
from pathlib import Path
from graphify.build import build_from_json, build_merge
from graphify.cluster import cluster
from graphify.export import to_json

Path('graphify-out').mkdir(exist_ok=True)
extraction = {
    'nodes': [
        {'id': 'a_caller', 'label': 'caller()', 'file_type': 'code', 'source_file': 'a.py'},
        {'id': 'a_callee', 'label': 'callee()', 'file_type': 'code', 'source_file': 'a.py'},
        {'id': 'a_leaf',   'label': 'leaf()',   'file_type': 'code', 'source_file': 'a.py'},
    ],
    'edges': [
        {'source': 'a_caller', 'target': 'a_callee', 'relation': 'calls',
         'confidence': 'EXTRACTED', 'confidence_score': 1.0, 'source_file': 'a.py'},
        {'source': 'a_callee', 'target': 'a_leaf', 'relation': 'calls',
         'confidence': 'EXTRACTED', 'confidence_score': 1.0, 'source_file': 'a.py'},
    ],
    'hyperedges': [],
}

G = build_from_json(extraction, directed=True)
to_json(G, cluster(G), 'graphify-out/graph.json')
print('saved directed:', json.loads(Path('graphify-out/graph.json').read_text())['directed'])

# what `graphify update` does
G2 = build_merge([{'nodes': [], 'edges': [], 'hyperedges': []}],
                 graph_path='graphify-out/graph.json', prune_sources=None)
to_json(G2, cluster(G2), 'graphify-out/graph.json', force=True)
print('after update  :', json.loads(Path('graphify-out/graph.json').read_text())['directed'])
saved directed: True
after update  : False

Passing directed=True to that same build_merge call returns is_directed()=True, confirming the plumbing works and only the default is at fault.

Location

graphify/cli.py:3547 (v1.0.0) — directed= is absent:

G = _build_merge(
    [merged],
    graph_path=existing_graph_path,
    prune_sources=_prune_sources or None,
    dedup=True,
    dedup_llm_backend=dedup_backend,
    root=target,
)

graphify/build.py — def build_merge(..., *, directed: bool = False, ...).

Same path is reached by graphify hook install (post-commit) and graphify --watch, so on a directed graph those revert it on every commit / file change.

Suggested fix

The robust version is to make build_merge inherit by default, since it already reads graph_path and so is the only place that knows the answer:

def build_merge(..., *, directed: bool | None = None, ...):
    # directed=None -> inherit from the existing graph; explicit True/False still wins.
    if directed is None:
        directed = bool(data.get("directed", False)) if graph_path.exists() else False

That fixes every caller at once and makes "an existing directed graph stays directed" the default everywhere, which seems like the least surprising behaviour.

The minimal alternative is to thread it at the call site in cli.py, reading the flag off existing_graph_path before the merge.

Either way it would help to print the resolved mode ([graphify update] directed=True) so a future downgrade is visible in the log rather than silent.

Related

Activity

  1. Rishet11 commented on Jul 31, 2026

    @Rishet11
    Contributor

    Reproduced. The keyword default is the whole bug, and build_merge never asks the loaded graph what it was.

    build_merge(..., *, directed: bool = False, ...) (graphify/build.py:1263) takes direction from the caller only, and build.py:1327 rebuilds with that stale default (G = build(all_chunks, directed=directed, ...)) no matter what the on-disk graph said:

    built directed:        True
    on-disk directed flag: True
    after build_merge:     False   # <- returned graph is undirected
    

    One detail that corrects the report slightly: build_merge itself did not rewrite the flag in my run (the file still said directed: true afterwards), so the downgrade lands when the caller serializes the returned graph, not inside build_merge. That matters for the fix — the returned object is what has to be right.

    cli.py:3547 is the only call site in the tree (grepped; there is no second copy of the merge logic), and it passes no directed=, so every graphify update that goes through the merge path inherits False.

    No existing test pins the False default: the calls in tests/test_build_merge_hyperedges_and_prune.py never pass or assert directed, and test_build_merge_preserves_call_edge_direction (tests/test_build.py:501) only checks edge tuple order.

    Plan: make the parameter directed: bool | None = None and, when it is None, honor the directed flag already parsed out of the existing graph.json, falling back to the current False when there is no existing graph (that fresh-graph branch is the part I will test hardest — getting it wrong would flip behavior for brand-new graphs). Claiming this; PR against v8.

  2. safishamsi commented on Aug 1, 2026

    @safishamsi
    Member

    Fixed in v0.9.32. graphify update now inherits the on-disk directed flag on both the clustered and --no-cluster rebuild paths, so God-node and path ranking keep their direction. Thanks for the report.

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