Skip to content

graphify query omits edges between seed nodes and other visited nodes #2323

Closed

Description

@cagdasyurekli

Summary

graphify query returns the relevant nodes but can omit real edges between them. This occurs when both endpoints are selected as seed nodes, and more generally for cross-edges between nodes that have already been visited.

Tested against Graphify 0.9.30 and the current v8 branch at ecfcd16.

Minimal reproduction

# app.py
from pricing import discounted_total

def checkout(subtotal: float, discount_rate: float) -> float:
    return discounted_total(subtotal, discount_rate)
# pricing.py
def discounted_total(subtotal: float, discount_rate: float) -> float:
    return subtotal * (1 - discount_rate)
graphify extract . --code-only
graphify query "How does checkout calculate the final total?"

graphify-out/graph.json correctly contains a checkout() --calls--> discounted_total() edge. Both functions are selected as query seeds and both nodes appear in the output, but the calls edge is missing.

Root cause

_bfs() and _dfs() in graphify/serve.py append an edge to edges_seen only when that edge discovers an unvisited neighbor:

if neighbor not in visited:
    next_frontier.add(neighbor)
    edges_seen.append((n, neighbor))

Because all seed nodes are marked visited before traversal begins, an edge between two seeds is never recorded. The same behavior omits non-tree/cross-edges between other already-visited nodes. The rendered result is therefore a traversal tree rather than the induced subgraph described by the returned node set.

Expected behavior

Every graph edge whose endpoints are both in the visited node set should be eligible for rendering, while preserving traversal-edge order and avoiding duplicates.

With that behavior, the reproduction includes:

EDGE checkout() --calls [EXTRACTED context=call]--> discounted_total() at=app.py:L5

Proposed fix

After BFS/DFS determines the visited node set, retain the traversal edges and append missing edges from G.edges() whose endpoints are both visited. Deduplicate using ordered endpoint pairs for directed graphs and unordered endpoint pairs for undirected graphs.

Validated locally with regression coverage for:

  • an edge directly between two seed nodes;
  • a cross-edge in a triangle where all three nodes are visited;
  • the original CLI reproduction.

Relevant upstream tests passed:

tests/test_serve.py + tests/test_query_cli.py
133 passed

Disclosure

This report was prepared with OpenAI Codex at the user's request after reproducing the issue on a synthetic fixture. No private repository data is included.

Activity

  1. Rishet11 commented on Jul 31, 2026

    @Rishet11
    Contributor

    Picking this up — thanks for the precise root cause and the repro.

    Confirmed the mechanism in v8 @ 4fe1109: _bfs sets visited = set(start_nodes) (serve.py:833) before the loop, so the if neighbor not in visited guard at :844 can never record a seed-to-seed edge. _dfs marks on pop (:868) and drops cross-edges the same way.

    Planned approach, following your suggestion with two additions:

    • complete the induced subgraph inside _bfs/_dfs rather than at the call site, so the completion pass is structurally bound to traversal_graph (serve.py:1054-1055) and cannot resurrect edges that _filter_graph_by_context deliberately removed;
    • iterate edges incident to the visited set rather than all of G.edges(), so cost scales with the subgraph rather than the whole graph;
    • dedup against the existing traversal edges, ordered pairs when G.is_directed() and unordered otherwise, since u->v and v->u are genuinely distinct on a DiGraph (mutual recursion, circular imports).

    Note the MCP query_graph tool (serve.py:1449) shares this traversal, so its output gains edges too. I'll cover that path with a test.

    Will open a PR against v8 with before/after CLI output on your repro.

  2. added a commit that references this issue on Aug 1, 2026
    875c7df
  3. added 2 commits that reference this issue on Aug 13, 2026
    86a497d
    9925a5f
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