Skip to content

path: shortest path runs on an undirected view, so results can traverse edges backwards #2487

Closed

Description

@luliaz0601

Version: graphify 0.9.33 (pipx, Python 3.14.5, Windows)

What happens

graphify path A B resolves the route on an undirected view and without edge weights:

# serve.py:1665
path_nodes = nx.shortest_path(_und, src_nid, tgt_nid)

Because _und is undirected and no weight= is passed, the returned route can walk edges
against their direction. On a real Next.js repo (215 nodes, 379 edges, all EXTRACTED):

$ graphify path "apiAuth.ts" "imagem/page.tsx"
Shortest path (5 hops):
  apiAuth.ts --imports_from--> server.ts --imports_from--> ref_supabase_ssr
             <--imports_from-- client.ts <--imports_from-- useFavoritos.ts
             <--imports_from-- imagem/page.tsx

Three of the five hops point backwards. The rendering is honest — it draws <-- — but
the result is presented under the label "shortest path", which reads as a dependency chain.

Why it matters

imports_from is directional and the direction carries the meaning. "A depends on B" and
"B depends on A" are different facts, and a route that mixes both answers a different
question: "are these two files connected by some chain?" rather than "how does A reach B?"

For an agent asking "how does the login flow reach the users table" — the example used in
the project's own docs — a mixed-direction chain can support a wrong conclusion about
which module owns what.

Suggested fix

A --directed flag (or making directed the default, with --undirected to opt out) using
the directed graph that is already in memory:

G_dir = G if G.is_directed() else G.to_directed()
path_nodes = nx.shortest_path(G_dir, src_nid, tgt_nid)

networkx is already a dependency, so this needs no new package.

Related: weighting by confidence

shortest_path is also called without weight=, so every edge costs 1 regardless of how
it was derived. Since edges already carry confidence (EXTRACTED / INFERRED /
AMBIGUOUS), an optional --by-confidence mode could map confidence to cost and run
Dijkstra, returning the most reliable route rather than the one with fewest hops. On a
mixed corpus the two answers differ; on a code-only corpus they coincide, so the flag would
be a no-op there and cost nothing.

Happy to send a PR for the --directed part if the direction is welcome.

Activity

  1. safishamsi commented on Aug 5, 2026

    @safishamsi
    Member

    Fixed in v0.9.34. graphify path and the MCP shortest_path tool now respect edge direction by default, building a digraph from the true _src/_tgt directions, so a returned path no longer traverses edges backwards. Use --undirected (CLI) or undirected=true (MCP) to search ignoring direction; when no directed path exists it says so instead of returning a reversed one. Thanks for the report.

  2. added a commit that references this issue on Aug 17, 2026
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