codebase-memory-mcp icon indicating copy to clipboard operation
codebase-memory-mcp copied to clipboard

query_graph: inbound anti-join unsupported — the README's own dead-code example is rejected, and the OPTIONAL MATCH workaround silently returns 0 rows

Open melihemreguler opened this issue 4 months ago • 6 comments

Summary

There is no way to express "nodes with no incoming edge" in query_graph. The form the README documents is rejected, and the natural workaround returns zero rows without an error.

The documented example does not work

README, Supported Cypher section:

EXISTS { (n)-[:TYPE]->() } (single-hop existence — great for dead-code, e.g. WHERE NOT EXISTS { (f)<-[:CALLS]-() })

That exact expression is rejected:

WHERE NOT EXISTS { MATCH (f)<-[:CALLS]-() }
-> unsupported EXISTS pattern — only the single-hop form '(var)-[:TYPE]->()' is supported

All three inbound syntaxes rejected

Query fragment Error
WHERE NOT (f)<-[:CALLS]-() unexpected operator at pos 31
WHERE NOT EXISTS((f)<-[:CALLS]-()) expected '{' after EXISTS
WHERE NOT EXISTS { MATCH (f)<-[:CALLS]-() } unsupported EXISTS pattern …

The outbound direction works as documented: WHERE NOT EXISTS { (f)-[:CALLS]->() } -> 2,949 leaf functions.

Silent-failure workaround

MATCH (f:Function)
OPTIONAL MATCH (f)<-[:CALLS]-(c)
WITH f, count(c) AS fan_in
WHERE fan_in = 0
RETURN f.name
-> 0 rows, no error

Expected: the ~291 zero-fan-in functions that search_graph(max_degree=0, exclude_entry_points=true) does return.

Impact

Dead-code detection is a headline feature. It is reachable via search_graph(max_degree=0) but not via Cypher, while the README advertises the Cypher form. Three possible fixes, in descending preference: support the inbound single-hop form; or make the OPTIONAL MATCH + WITH count() = 0 path work; or at minimum make that path fail loudly and correct the README example.


Environment

  • Version: codebase-memory-mcp 0.9.0 (release binary, darwin-arm64)
  • OS: macOS 26.5.2, Apple Silicon
  • Repo under test: DeusData/codebase-memory-mcp @ af3ffbd2 (21,143 nodes / 123,103 edges)
  • Verified identically through both the MCP server and cli mode (outputs byte-identical)

melihemreguler avatar Jul 27 '26 07:07 melihemreguler

Thanks for testing both the documented form and the apparent workaround.

On current main the Cypher parser has no executable EXISTS-subquery/anti-join representation for this shape. OPTIONAL MATCH is parsed, but predicate evaluation and row preservation are separate paths; it is therefore not a safe equivalent for an inbound anti-join. That confirms the README example and engine contract are out of sync, with the dangerous behavior being the silent zero-row workaround.

I have routed this to 0.9.1-rc at priority/high under the query correctness work. Thank you for verifying the workaround rather than only reporting the parse error.

DeusData avatar Jul 28 '26 18:07 DeusData

Confirming this is still present on v0.10.8, and I think I found the specific root cause of the "0 rows" symptom: count() over an OPTIONAL MATCH that has zero real matches returns 1, not 0.

MATCH (f:Function {name: 'foo'})     // known, verified 0 real callers via direct MATCH below
OPTIONAL MATCH (caller)-[r:CALLS]->(f)
WITH f, count(r) AS refs
RETURN f.name, refs
-> refs = "1"

But the direct (non-OPTIONAL) form on the identical pattern:

MATCH (caller)-[r:CALLS]->(f:Function {name: 'foo'})
RETURN caller.name
-> 0 rows

confirms there is genuinely no caller. So OPTIONAL MATCH + count(r) is counting the row itself (bound to a null r) as 1, instead of following standard Cypher null-counting semantics where count() skips NULL. That means WITH f, count(c) AS fan_in WHERE fan_in = 0 can never match anything — fan_in is never actually 0, it's always ≥1 — which exactly matches the "0 rows, no error" workaround failure reported above. Swapping to WITH f, caller WHERE caller IS NULL (checking the bound node for null instead of counting the relationship) works correctly and returns the expected zero-fan-in set.

Might be worth folding this into whichever fix lands here, since "support the inbound single-hop NOT EXISTS form" would sidestep it, but the count() semantics bug will keep biting anyone who reaches for the OPTIONAL MATCH pattern for other aggregations too, not just fan-in.

bhaveshpatelapple avatar Sep 14 '26 23:09 bhaveshpatelapple

Thank you for isolating the null-counting case. It needs its own regression alongside inbound anti-join support; fixing one would not prove the other correct. I have not verified the proposed workaround on current code, so cannot endorse it yet.

DeusData avatar Sep 19 '26 20:09 DeusData

Thank you, @melihemreguler, for testing the documented form and the workaround side by side, and @bhaveshpatelapple for pinning the null-counting case!

On current main, the README form MATCH (f:Function) WHERE NOT EXISTS { (f)<-[:CALLS]-() } RETURN f.name runs and returns exactly the functions with no callers. The forms with MATCH inside the braces, and the bare pattern predicate, are still outside the supported subset, but they now fail with a clear error instead of giving a wrong answer.

The count() case is a real bug and is still present. Over an OPTIONAL MATCH with no match, count(r) gives 0 in RETURN but 1 when aggregated in WITH, so WITH f, count(c) AS fan_in WHERE fan_in = 0 can never match. It's the same defect as in #627, and we'll fix it with its own regression test. Your WITH f, caller WHERE caller IS NULL form returns the correct zero-caller set on current main, so it's safe to use until then.

DeusData avatar Sep 26 '26 04:09 DeusData

Thank you again for separating inbound existence support from WITH null-counting. The latest maintainer check left the WITH aggregate defect open and linked it to #627; the supported existence form does not resolve that separate case. I have no newer fix result to share.

DeusData avatar Oct 19 '26 22:10 DeusData

Thank you for separating existence syntax from null-counting. The WITH aggregate defect remains open alongside #627; support for the existence form does not resolve it. Sorry for the delay; this is waiting on maintainer follow-up, and I’ll follow up soon.

DeusData avatar Oct 25 '26 17:10 DeusData