Skip to content

fix(python): a per-callable graph must contain its self-loops - #353

Merged
rahlk merged 2 commits into
release/2.0from
fix/issue-349-self-loop-edges
Sep 7, 2026
Merged

rahlk merged 2 commits into
release/2.0from
fix/issue-349-self-loop-edges

Conversation

@rahlk

@rahlk rahlk commented Sep 7, 2026

Copy link
Copy Markdown
Collaborator

Closes #349.

get_cfg, get_cdg and get_ddg on the Python Neo4j backend silently dropped every self-loop edge. The query asked for a callable's own edges as two containment patterns:

MATCH (c)-[:PY_HAS_BODY_NODE]->(s)-[r]->(d)<-[:PY_HAS_BODY_NODE]-(c)

When s and d are the same node — a data-dependence self-loop, which is what a statement that reads a variable it also redefines produces — both hops match the same relationship, and Cypher's relationship-uniqueness rule discards the row.

Why it was invisible. total is counted from the same match, so the short page agreed with its own count and reported complete. Nothing inside the result could reveal it; only a comparison against the graph does.

Measured on the odoo reference graph: 64,702 self-loop PY_DDG edges. On AccountAccruedOrdersWizard._compute_move_vals, before and after:

before after
total 28,146 28,394
self-loops returned 0 248
complete True True
median wall clock 18.8 ms 13.1 ms

It is faster because a pattern predicate lets the planner test containment instead of expanding a second pattern.

Tests. Two, both failing on the unfixed query and passing on the fix. One is a shape check needing no graph, asserting the second hop is a predicate — that is the durable guard, since it fails even where no corpus has a self-loop. The other runs against a live graph, picks the callable with the most self-loops, and asserts the page holds every one the graph holds.

Blast radius. Python only. TypeScript anchors these accessors on the callable's id prefix, a spelling chosen for speed that happens to sidestep this, and its leg verified self-loops are returned. Java's equivalent is being written the same way, with the same regression test.

Found while porting the accessors to Java, by asking whether the shape Python uses would be safe there.

The Neo4j query for one callable's own cfg/cdg/ddg edges was written as two
containment patterns:

    MATCH (c)-[:PY_HAS_BODY_NODE]->(s)-[r]->(d)<-[:PY_HAS_BODY_NODE]-(c)

When s and d are the same node — a data-dependence self-loop, i.e. a
statement that reads a variable it also redefines — both hops match the
same PY_HAS_BODY_NODE relationship, and Cypher's relationship-uniqueness
rule drops the row. There are 64,702 such edges on the odoo reference
graph; one callable returned 28,146 of its 28,394.

The defect was invisible from inside the result: total is counted from the
same match, so the short page agreed with its own count and reported
complete. Only a comparison against the graph reveals it.

The second hop becomes a pattern predicate, which does not bind the
relationship. Measured on the same callable, it is also faster: 13.1ms
against 18.8ms, since the planner tests containment rather than expanding
a second pattern.

TypeScript anchors these accessors on the callable's id prefix and was
never affected; Java's equivalent is being written the same way.

Closes #349
@rahlk
rahlk merged commit ade490e into release/2.0 Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant