Skip to content

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.

Cherry-pick extraction and Cypher improvements from PR #162

Martin Vogelf71fded895c6
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.c
index 525b58445..5da9013b7 100644
--- a/internal/cbm/extract_defs.c
+++ b/internal/cbm/extract_defs.c
@@ -180,6 +180,8 @@ static void extract_class_methods(CBMExtractCtx *ctx, TSNode class_node, const c
const CBMLangSpec *spec);
static void extract_class_fields(CBMExtractCtx *ctx, TSNode class_node, const char *class_qn,
const CBMLangSpec *spec);
+static TSNode find_class_body(TSNode class_node, CBMLanguage lang);
+static void extract_enum_members(CBMExtractCtx *ctx, TSNode node, const char *class_qn);
static void extract_elixir_call(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec);
// --- Helpers ---
@@ -1864,6 +1866,10 @@ static void extract_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
cbm_defs_push(&ctx->result->defs, a, def);
+ if (strcmp(label, "Enum") == 0) {
+ extract_enum_members(ctx, node, class_qn);
+ }
+
// Extract methods inside the class
extract_class_methods(ctx, node, class_qn, spec);
@@ -2426,6 +2432,80 @@ static void extract_csharp_vars(CBMExtractCtx *ctx, TSNode node, CBMArena *a) {
}
}
+/* Check if a tree-sitter node type is an enum member declaration. */
+static bool is_enum_member_kind(const char *kind) {
+ return strcmp(kind, "enum_member_declaration") == 0 || strcmp(kind, "enum_constant") == 0 ||
+ strcmp(kind, "enum_member") == 0 || strcmp(kind, "enum_assignment") == 0 ||
+ strcmp(kind, "enumerator") == 0;
+}
+
+/* Extract enum members as Variable nodes (C#, Java, TypeScript, C++). */
+static void extract_enum_members(CBMExtractCtx *ctx, TSNode node, const char *class_qn) {
+ CBMArena *a = ctx->arena;
+ TSNode body = find_class_body(node, ctx->language);
+ if (ts_node_is_null(body)) {
+ return;
+ }
+ uint32_t mc = ts_node_named_child_count(body);
+ for (uint32_t mi = 0; mi < mc; mi++) {
+ TSNode member = ts_node_named_child(body, mi);
+ if (!is_enum_member_kind(ts_node_type(member))) {
+ continue;
+ }
+ TSNode mname = ts_node_child_by_field_name(member, TS_FIELD("name"));
+ if (ts_node_is_null(mname)) {
+ mname = cbm_find_child_by_kind(member, "identifier");
+ }
+ if (ts_node_is_null(mname)) {
+ continue;
+ }
+ char *member_name = cbm_node_text(a, mname, ctx->source);
+ if (!member_name || !member_name[0]) {
+ continue;
+ }
+ CBMDefinition mdef;
+ memset(&mdef, 0, sizeof(mdef));
+ mdef.name = member_name;
+ mdef.qualified_name = cbm_arena_sprintf(a, "%s.%s", class_qn, member_name);
+ mdef.label = "Variable";
+ mdef.file_path = ctx->rel_path;
+ mdef.start_line = ts_node_start_point(member).row + TS_LINE_OFFSET;
+ mdef.end_line = ts_node_end_point(member).row + TS_LINE_OFFSET;
+ cbm_defs_push(&ctx->result->defs, a, mdef);
+ }
+}
+
+/* Resolve the identifier node from a destructure pattern child.
+ * pair_pattern → value field; shorthand/identifier → itself; others → first named child. */
+static TSNode destructure_ident(TSNode pat_child) {
+ const char *pk = ts_node_type(pat_child);
+ if (strcmp(pk, "shorthand_property_identifier_pattern") == 0 || strcmp(pk, "identifier") == 0) {
+ return pat_child;
+ }
+ if (strcmp(pk, "pair_pattern") == 0) {
+ return ts_node_child_by_field_name(pat_child, TS_FIELD("value"));
+ }
+ /* rest_pattern, assignment_pattern, etc. — first named child. */
+ return ts_node_named_child(pat_child, 0);
+}
+
+/* Emit individual Variable nodes for each destructured binding. */
+static void extract_destructured_vars(CBMExtractCtx *ctx, TSNode pattern, TSNode decl,
+ CBMArena *a) {
+ uint32_t pc = ts_node_named_child_count(pattern);
+ for (uint32_t pi = 0; pi < pc; pi++) {
+ TSNode pat_child = ts_node_named_child(pattern, pi);
+ TSNode ident = destructure_ident(pat_child);
+ if (ts_node_is_null(ident)) {
+ continue;
+ }
+ char *id_text = cbm_node_text(a, ident, ctx->source);
+ if (id_text && id_text[0]) {
+ push_var_def(ctx, id_text, decl);
+ }
+ }
+}
+
// JS/TS variable extraction: skip function-assigned declarators.
static void extract_js_vars(CBMExtractCtx *ctx, TSNode node, CBMArena *a) {
uint32_t n = ts_node_named_child_count(node);
@@ -2444,7 +2524,14 @@ static void extract_js_vars(CBMExtractCtx *ctx, TSNode node, CBMArena *a) {
}
TSNode vname = ts_node_child_by_field_name(child, TS_FIELD("name"));
if (!ts_node_is_null(vname)) {
- push_var_def(ctx, cbm_node_text(a, vname, ctx->source), child);
+ const char *nk = ts_node_type(vname);
+ /* Destructured patterns: emit individual identifiers instead of
+ * the raw "{A, B, C}" text as a single Variable node. */
+ if (strcmp(nk, "object_pattern") == 0 || strcmp(nk, "array_pattern") == 0) {
+ extract_destructured_vars(ctx, vname, child, a);
+ } else {
+ push_var_def(ctx, cbm_node_text(a, vname, ctx->source), child);
+ }
}
}
}
diff --git a/src/cypher/cypher.c b/src/cypher/cypher.c
index 6aedeb923..9ebd8d0b7 100644
--- a/src/cypher/cypher.c
+++ b/src/cypher/cypher.c
@@ -35,6 +35,7 @@ enum {
#include <ctype.h>
#include "foundation/compat_regex.h"
+#include <stddef.h>
#include <stdint.h> // int64_t
#include <stdio.h>
#include <stdlib.h>
@@ -326,6 +327,13 @@ static bool lex_skip_whitespace_comments(const char *input, int len, int *i) {
}
return true;
}
+ /* SQL-style -- single-line comment */
+ if (*i + SKIP_ONE < len && input[*i] == '-' && input[*i + SKIP_ONE] == '-') {
+ while (*i < len && input[*i] != '\n') {
+ (*i)++;
+ }
+ return true;
+ }
if (*i + SKIP_ONE < len && input[*i] == '/' && input[*i + SKIP_ONE] == '*') {
*i += PAIR_LEN;
while (*i + SKIP_ONE < len && !(input[*i] == '*' && input[*i + SKIP_ONE] == '/')) {
@@ -1639,35 +1647,58 @@ typedef struct {
const char *edge_var_names[CYP_MAX_EDGE_VARS]; /* variable names (edges) */
cbm_edge_t edge_vars[CYP_MAX_EDGE_VARS]; /* edge data */
int edge_var_count;
+ cbm_store_t *store; /* for computing in_degree/out_degree on demand */
} binding_t;
-/* Get node property by name */
-static const char *node_prop(const cbm_node_t *n, const char *prop) {
+/* Return a string field from a node by property name. NULL-safe. */
+static const char *node_string_field(const cbm_node_t *n, const char *prop) {
+ static const struct {
+ const char *key;
+ size_t offset;
+ } fields[] = {
+ {"name", offsetof(cbm_node_t, name)},
+ {"qualified_name", offsetof(cbm_node_t, qualified_name)},
+ {"label", offsetof(cbm_node_t, label)},
+ {"file_path", offsetof(cbm_node_t, file_path)},
+ };
+ for (size_t i = 0; i < sizeof(fields) / sizeof(fields[0]); i++) {
+ if (strcmp(prop, fields[i].key) == 0) {
+ const char *val = *(const char **)((const char *)n + fields[i].offset);
+ return val ? val : "";
+ }
+ }
+ return NULL;
+}
+
+/* Get node property by name.
+ * store may be NULL; only needed for virtual degree properties. */
+static const char *node_prop(const cbm_node_t *n, const char *prop, cbm_store_t *store) {
if (!n || !prop) {
return "";
}
- if (strcmp(prop, "name") == 0) {
- return n->name ? n->name : "";
- }
- if (strcmp(prop, "qualified_name") == 0) {
- return n->qualified_name ? n->qualified_name : "";
- }
- if (strcmp(prop, "label") == 0) {
- return n->label ? n->label : "";
- }
- if (strcmp(prop, "file_path") == 0) {
- return n->file_path ? n->file_path : "";
+ const char *str = node_string_field(n, prop);
+ if (str) {
+ return str;
}
+ /* Integer properties returned as strings. */
+ static _Thread_local char int_buf[CBM_SZ_32];
if (strcmp(prop, "start_line") == 0) {
- /* Return as string */
- static char buf[CBM_SZ_32];
- snprintf(buf, sizeof(buf), "%d", n->start_line);
- return buf;
+ snprintf(int_buf, sizeof(int_buf), "%d", n->start_line);
+ return int_buf;
}
if (strcmp(prop, "end_line") == 0) {
- static char buf[CBM_SZ_32];
- snprintf(buf, sizeof(buf), "%d", n->end_line);
- return buf;
+ snprintf(int_buf, sizeof(int_buf), "%d", n->end_line);
+ return int_buf;
+ }
+ /* Virtual computed properties: in_degree/out_degree via CALLS edges.
+ * Enables Cypher dead-code detection: WHERE n.in_degree = '0'. */
+ if (store && (strcmp(prop, "in_degree") == 0 || strcmp(prop, "out_degree") == 0)) {
+ int in_deg = 0;
+ int out_deg = 0;
+ cbm_store_node_degree(store, n->id, &in_deg, &out_deg);
+ int val = (strcmp(prop, "in_degree") == 0) ? in_deg : out_deg;
+ snprintf(int_buf, sizeof(int_buf), "%d", val);
+ return int_buf;
}
return "";
}
@@ -1827,6 +1858,7 @@ static void binding_copy(binding_t *dst, const binding_t *src) {
dst->edge_var_names[i] = src->edge_var_names[i]; /* AST-owned */
edge_deep_copy(&dst->edge_vars[i], &src->edge_vars[i]);
}
+ dst->store = src->store;
}
/* Deep-copy a node into a binding (binding owns the strings) */
@@ -1858,7 +1890,7 @@ static const char *resolve_condition_value(const cbm_condition_t *c, binding_t *
return NULL; /* unbound variable */
}
if (c->property) {
- return node_prop(n, c->property);
+ return node_prop(n, c->property, b->store);
}
/* Bare alias (e.g. post-WITH virtual var) — use node name directly */
return n->name ? n->name : "";
@@ -1991,11 +2023,34 @@ static bool eval_where(const cbm_where_clause_t *w, binding_t *b) {
return is_and;
}
-/* Check inline property filters */
-static bool check_inline_props(const cbm_node_t *n, const cbm_prop_filter_t *props, int count) {
+/* Check if a string value looks like a regex pattern. */
+static bool looks_like_regex(const char *s) {
+ if (!s) {
+ return false;
+ }
+ return strstr(s, ".*") || strstr(s, ".+") || strchr(s, '[') || strchr(s, '(') ||
+ strchr(s, '|') || strchr(s, '^') || strchr(s, '$');
+}
+
+/* Check inline property filters.
+ * Values that look like regex patterns are matched with POSIX ERE;
+ * plain values use exact strcmp. */
+static bool check_inline_props(const cbm_node_t *n, const cbm_prop_filter_t *props, int count,
+ cbm_store_t *store) {
for (int i = 0; i < count; i++) {
- const char *actual = node_prop(n, props[i].key);
- if (strcmp(actual, props[i].value) != 0) {
+ const char *actual = node_prop(n, props[i].key, store);
+ if (looks_like_regex(props[i].value)) {
+ cbm_regex_t re;
+ if (cbm_regcomp(&re, props[i].value, CBM_REG_EXTENDED | CBM_REG_NOSUB) == 0) {
+ bool matched = cbm_regexec(&re, actual, 0, NULL, 0) == 0;
+ cbm_regfree(&re);
+ if (!matched) {
+ return false;
+ }
+ } else if (strcmp(actual, props[i].value) != 0) {
+ return false;
+ }
+ } else if (strcmp(actual, props[i].value) != 0) {
return false;
}
}
@@ -2068,7 +2123,7 @@ static const char *binding_get_virtual(binding_t *b, const char *var, const char
cbm_node_t *n = binding_get(b, var);
if (n) {
if (prop) {
- return node_prop(n, prop);
+ return node_prop(n, prop, b->store);
}
return n->name ? n->name : "";
}
@@ -2147,7 +2202,7 @@ static void scan_pattern_nodes(cbm_store_t *store, const char *project, int max_
if (first->prop_count > 0) {
int kept = 0;
for (int i = 0; i < *out_count; i++) {
- if (check_inline_props(&(*out_nodes)[i], first->props, first->prop_count)) {
+ if (check_inline_props(&(*out_nodes)[i], first->props, first->prop_count, store)) {
if (kept != i) {
(*out_nodes)[kept] = (*out_nodes)[i];
}
@@ -2178,7 +2233,7 @@ static void process_edges(cbm_store_t *store, cbm_edge_t *edges, int edge_count,
node_fields_free(&found);
continue;
}
- if (!check_inline_props(&found, target_node->props, target_node->prop_count)) {
+ if (!check_inline_props(&found, target_node->props, target_node->prop_count, store)) {
node_fields_free(&found);
continue;
}
@@ -2211,7 +2266,7 @@ static void expand_var_length(cbm_store_t *store, cbm_rel_pattern_t *rel,
if (target_node->label && strcmp(hop->node.label, target_node->label) != 0) {
continue;
}
- if (!check_inline_props(&hop->node, target_node->props, target_node->prop_count)) {
+ if (!check_inline_props(&hop->node, target_node->props, target_node->prop_count, store)) {
continue;
}
binding_t nb = {0};
@@ -3287,6 +3342,7 @@ static int execute_single(cbm_store_t *store, cbm_query_t *q, const char *projec
for (int i = 0; i < scan_count && bind_count < bind_cap; i++) {
binding_t b = {0};
+ b.store = store;
binding_set(&b, var_name, &scanned[i]);
bool pass = !q->where || eval_where(q->where, &b);
if (pass) {
diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.c
index 619757b28..76a2c0e65 100644
--- a/src/pipeline/pass_parallel.c
+++ b/src/pipeline/pass_parallel.c
@@ -26,6 +26,8 @@ enum {
#define PP_NSEC_PER_SEC 1000000000ULL
#define PP_USEC_PER_MS 1000000ULL
#define PP_HALF_CONF 0.5
+#define PP_FIELD_HINT_CONF 0.85
+enum { PP_CSHARP_M_PREFIX_LEN = 2 };
#include "pipeline/pipeline.h"
#include "pipeline/pipeline_internal.h"
#include "pipeline/worker_pool.h"
@@ -1193,6 +1195,60 @@ static const cbm_gbuf_node_t *find_source_node(const cbm_gbuf_t *gbuf, const cha
return src;
}
+/* Field type hint resolution for obj.Method() with multiple candidates.
+ * Strips C# field prefixes (_ / m_), capitalizes to get type name, and
+ * checks if TypeName.Method or ITypeName.Method exists among candidates. */
+static void try_field_type_hint(resolve_ctx_t *rc, cbm_resolution_t *res, const char *callee_name,
+ int64_t source_id) {
+ if (!res->qualified_name || res->candidate_count <= SKIP_ONE) {
+ return;
+ }
+ const char *dot = strchr(callee_name, '.');
+ if (!dot) {
+ return;
+ }
+ size_t plen = (size_t)(dot - callee_name);
+ char obj_name[CBM_SZ_256];
+ if (plen >= sizeof(obj_name)) {
+ return;
+ }
+ memcpy(obj_name, callee_name, plen);
+ obj_name[plen] = '\0';
+
+ const char *type_hint = obj_name;
+ if (type_hint[0] == '_') {
+ type_hint++;
+ }
+ if (type_hint[0] == 'm' && type_hint[SKIP_ONE] == '_') {
+ type_hint += PP_CSHARP_M_PREFIX_LEN;
+ }
+
+ char type_name[CBM_SZ_256];
+ snprintf(type_name, sizeof(type_name), "%s", type_hint);
+ if (type_name[0] >= 'a' && type_name[0] <= 'z') {
+ type_name[0] -= ('a' - 'A');
+ }
+
+ char iface_name[CBM_SZ_256];
+ snprintf(iface_name, sizeof(iface_name), "I%s", type_name);
+
+ const char *method = dot + SKIP_ONE;
+ const char **cands = NULL;
+ int cand_count = 0;
+ cbm_registry_find_by_name(rc->registry, method, &cands, &cand_count);
+ for (int ci = 0; ci < cand_count; ci++) {
+ if (strstr(cands[ci], type_name) || strstr(cands[ci], iface_name)) {
+ const cbm_gbuf_node_t *better = cbm_gbuf_find_by_qn(rc->main_gbuf, cands[ci]);
+ if (better && better->id != source_id) {
+ res->qualified_name = cands[ci];
+ res->confidence = PP_FIELD_HINT_CONF;
+ res->strategy = "field_type_hint";
+ return;
+ }
+ }
+ }
+}
+
/* Resolve calls for one file and emit CALLS/HTTP_CALLS/ASYNC_CALLS edges. */
static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CBMFileResult *result,
const char *rel, const char *module_qn, const char **imp_keys,
@@ -1210,6 +1266,9 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
cbm_resolution_t res = cbm_registry_resolve(rc->registry, call->callee_name, module_qn,
imp_keys, imp_vals, imp_count);
+
+ try_field_type_hint(rc, &res, call->callee_name, source_node->id);
+
if (!res.qualified_name || res.qualified_name[0] == '\0') {
if (cbm_service_pattern_route_method(call->callee_name) != NULL) {
cbm_resolution_t fake_res = {.qualified_name = call->callee_name,
diff --git a/src/pipeline/registry.c b/src/pipeline/registry.c
index e0b94194e..d9408950d 100644
--- a/src/pipeline/registry.c
+++ b/src/pipeline/registry.c
@@ -108,15 +108,41 @@ static int common_prefix_len(const char *a, const char *b) {
return count;
}
-/* Pick candidate with longest common prefix with caller module. */
+enum { REG_TEST_PENALTY = 1000 };
+
+/* Check if a qualified name looks like a test/mock path. */
+static bool is_test_qn(const char *qn) {
+ if (!qn) {
+ return false;
+ }
+ return (strstr(qn, "Test") != NULL || strstr(qn, "test") != NULL ||
+ strstr(qn, "Mock") != NULL || strstr(qn, "mock") != NULL ||
+ strstr(qn, "Stub") != NULL || strstr(qn, "stub") != NULL ||
+ strstr(qn, "Fake") != NULL || strstr(qn, "fake") != NULL ||
+ strstr(qn, "Fixture") != NULL || strstr(qn, "spec") != NULL);
+}
+
+/* Score a candidate for tiebreaking. Higher = better.
+ * Layer 1: Non-test code preferred over test code (+1000)
+ * Layer 2: Namespace proximity via common prefix length (+plen) */
+static int candidate_score(const char *candidate_qn, const char *module_qn) {
+ int score = 0;
+ if (!is_test_qn(candidate_qn)) {
+ score += REG_TEST_PENALTY;
+ }
+ score += common_prefix_len(candidate_qn, module_qn);
+ return score;
+}
+
+/* Pick candidate with highest composite score (test-deprioritization + namespace proximity). */
static const char *best_by_import_distance(const char **candidates, int count,
const char *module_qn) {
const char *best = NULL;
- int best_len = CBM_NOT_FOUND;
+ int best_score = CBM_NOT_FOUND;
for (int i = 0; i < count; i++) {
- int plen = common_prefix_len(candidates[i], module_qn);
- if (plen > best_len) {
- best_len = plen;
+ int score = candidate_score(candidates[i], module_qn);
+ if (score > best_score) {
+ best_score = score;
best = candidates[i];
}
}