Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
fix(cypher): count the unnamed head node against the variable cap
Joshua Richter98899ba5d62c
diff --git a/src/cypher/cypher.c b/src/cypher/cypher.cindex aa175e6b3..9dfe97c76 100644--- a/src/cypher/cypher.c+++ b/src/cypher/cypher.c@@ -36,6 +36,10 @@ enum {/* search miss sentinel */ /* mask for ebuf ring buffer (8 entries) */};#define CYP_DBL_MAX 1e308+/* execute_single binds the first pattern's leading node even when the query+ * leaves it unnamed, so the engine needs a name for it. The binding slot is+ * real, which is why check_pattern_var_capacity counts it. */+#define CYP_ANON_HEAD_VAR "_n0"#include <ctype.h>#include <limits.h> // INT_MAX@@ -4994,7 +4998,7 @@ static int execute_single(cbm_store_t *store, cbm_query_t *q, const char *projecint bind_cap = scan_count > max_rows ? scan_count : (max_rows > 0 ? max_rows : SKIP_ONE);binding_t *bindings = malloc((bind_cap + SKIP_ONE) * sizeof(binding_t));int bind_count = 0;- const char *var_name = pat0->nodes[0].variable ? pat0->nodes[0].variable : "_n0";+ const char *var_name = pat0->nodes[0].variable ? pat0->nodes[0].variable : CYP_ANON_HEAD_VAR;for (int i = 0; i < scan_count && bind_count < bind_cap; i++) {if ((i & CYPHER_DEADLINE_CHECK_MASK) == 0 && cypher_deadline_exceeded()) {@@ -5165,7 +5169,8 @@ static const char *scope_checkable_var(const cbm_return_item_t *item) {}/* Says which limit the query passed and how to get under it. An unnamed node- * takes no slot, so dropping a name the query never uses is the cheap way out.+ * takes no slot — except the head of the first pattern, which the engine binds+ * either way — so dropping a name the query never uses is the cheap way out.* Splitting the MATCH is NOT — every pattern of one query shares one binding,* which is why the caller counts across all of them. Separate queries do work,* because each gets a binding of its own. */@@ -5203,6 +5208,15 @@ static char *check_pattern_var_capacity(const cbm_query_t *q) {const char *edge_vars[CYP_MAX_EDGE_VARS] = {NULL};int node_n = 0;int edge_n = 0;+ /* The head of the first pattern always takes a node slot, named or not:+ * execute_single binds it under CYP_ANON_HEAD_VAR when the query leaves it+ * unnamed. Count it first, or a query with an unnamed head and CYP_MAX_VARS+ * named nodes passes this check and still loses its last name in+ * binding_set — the exact silence this guard exists to remove. */+ if (q->pattern_count > 0 && q->patterns[0].node_count > 0 &&+ !q->patterns[0].nodes[0].variable) {+ node_vars[node_n++] = CYP_ANON_HEAD_VAR;+ }for (int pi = 0; pi < q->pattern_count; pi++) {const cbm_pattern_t *pat = &q->patterns[pi];for (int ni = 0; ni < pat->node_count; ni++) {diff --git a/tests/test_cypher.c b/tests/test_cypher.cindex acebee3cf..fbd9d36bf 100644--- a/tests/test_cypher.c+++ b/tests/test_cypher.c@@ -3262,6 +3262,41 @@ TEST(cypher_wide_edge_pattern_refused) {PASS();}+TEST(cypher_unnamed_head_takes_a_slot) {+ /* The head of the first pattern is bound whether the query names it or not:+ * execute_single falls back to the synthetic name "_n0". So an unnamed head+ * plus CYP_MAX_VARS (16) named nodes needs 17 slots and only 16 exist. Before+ * the fix, the capacity check counted names alone, let this query through,+ * and binding_set dropped the 16th name without a word — a0..a14 answered and+ * a15 came back empty. Refuse it instead. */+ cbm_store_t *s = setup_cypher_store();+ char query[2048];+ int off = snprintf(query, sizeof(query), "MATCH (:NoSuchLabelXYZ)");+ for (int i = 0; i < 16; i++) { /* 16 named + the unnamed head = 17 */+ off += snprintf(query + off, sizeof(query) - (size_t)off, "-[:CALLS]->(a%d)", i);+ }+ snprintf(query + off, sizeof(query) - (size_t)off, " RETURN a0.name");+ cbm_cypher_result_t wide = {0};+ ASSERT_TRUE(cbm_cypher_execute(s, query, "test", 0, &wide) != 0);+ ASSERT_NOT_NULL(wide.error);+ ASSERT_TRUE(strstr(wide.error, "node") != NULL);+ cbm_cypher_result_free(&wide);++ /* One name fewer fits exactly, so the guard still refuses only what a+ * binding genuinely cannot hold. */+ off = snprintf(query, sizeof(query), "MATCH (:NoSuchLabelXYZ)");+ for (int i = 0; i < 15; i++) {+ off += snprintf(query + off, sizeof(query) - (size_t)off, "-[:CALLS]->(a%d)", i);+ }+ snprintf(query + off, sizeof(query) - (size_t)off, " RETURN a0.name");+ cbm_cypher_result_t ok = {0};+ ASSERT_EQ(cbm_cypher_execute(s, query, "test", 0, &ok), 0);+ cbm_cypher_result_free(&ok);++ cbm_store_close(s);+ PASS();+}+TEST(cypher_scope_check_survives_wide_pattern) {/* Regression test for #1995. check_projection_scope models declared names in* a fixed array and used to skip the check entirely when a query declared@@ -4606,6 +4641,7 @@ SUITE(cypher) {RUN_TEST(cypher_wide_with_refused_not_truncated);RUN_TEST(cypher_wide_pattern_refused);RUN_TEST(cypher_wide_edge_pattern_refused);+ RUN_TEST(cypher_unnamed_head_takes_a_slot);RUN_TEST(cypher_scope_check_survives_wide_pattern);RUN_TEST(cypher_parse_neq);RUN_TEST(cypher_parse_in);