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(extract): grammar def-label + malformed-QN fixes
Martin Vogel19d34b5ff73d
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.cindex 0f771dfc0..5b6917719 100644--- a/internal/cbm/extract_defs.c+++ b/internal/cbm/extract_defs.c@@ -1051,6 +1051,32 @@ TSNode cbm_resolve_func_name(TSNode node, CBMLanguage lang) {}}+ /* BitBake: a shell task `do_foo() {...}` is a function_definition and a+ * python task `python do_foo() {...}` is an anonymous_python_function;+ * both carry the task name on a direct `identifier` child (no `name`+ * field). */+ if (lang == CBM_LANG_BITBAKE && (strcmp(kind, "function_definition") == 0 ||+ strcmp(kind, "anonymous_python_function") == 0)) {+ TSNode id = cbm_find_child_by_kind(node, "identifier");+ if (!ts_node_is_null(id)) {+ return id;+ }+ }++ /* PKL: a classMethod/objectMethod (`function foo(): T = ...`) has no+ * `name` field; the name is the `identifier` inside its methodHeader+ * child. */+ if (lang == CBM_LANG_PKL &&+ (strcmp(kind, "classMethod") == 0 || strcmp(kind, "objectMethod") == 0)) {+ TSNode hdr = cbm_find_child_by_kind(node, "methodHeader");+ if (!ts_node_is_null(hdr)) {+ TSNode id = cbm_find_child_by_kind(hdr, "identifier");+ if (!ts_node_is_null(id)) {+ return id;+ }+ }+ }+{TSNode r = resolve_toplevel_arrow_name(node, kind);if (!ts_node_is_null(r)) {@@ -2783,6 +2809,14 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpecreturn;}+ // Makefile special targets (.PHONY, .DEFAULT, .SUFFIXES, …) are directives,+ // not build-rule defs. Their leading '.' would also make cbm_fqn_compute+ // emit a "..PHONY" segment (a "double dot") and thus a malformed QN. Skip+ // any dot-prefixed Make target.+ if (ctx->language == CBM_LANG_MAKEFILE && name[0] == '.') {+ return;+ }+TSNode func_node = unwrap_template_inner(node, ctx->language);CBMDefinition def;@@ -2914,12 +2948,52 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec// --- Class definition extraction ---// Push a simple class definition (used by config language extractors).+// Replace each run of whitespace in `name` with a single '-' so the value is a+// well-formed QN segment. Markdown headings (e.g. "Codebase Memory") legitimately+// contain spaces; embedding them verbatim in a QN makes it malformed. Returns the+// original pointer when there is no whitespace to collapse. The human-readable+// def.name is kept intact; only the QN segment is slugified.+static const char *qn_safe_segment(CBMArena *a, const char *name) {+ if (!name) {+ return name;+ }+ bool has_ws = false;+ for (const char *p = name; *p; p++) {+ if (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') {+ has_ws = true;+ break;+ }+ }+ if (!has_ws) {+ return name;+ }+ char *out = cbm_arena_strdup(a, name);+ if (!out) {+ return name;+ }+ char *w = out;+ bool in_ws = false;+ for (char *r = out; *r; r++) {+ if (*r == ' ' || *r == '\t' || *r == '\n' || *r == '\r') {+ if (!in_ws && w != out) {+ *w++ = '-';+ }+ in_ws = true;+ } else {+ *w++ = *r;+ in_ws = false;+ }+ }+ *w = '\0';+ return out;+}+static void push_simple_class_def(CBMExtractCtx *ctx, TSNode node, char *name, const char *label) {CBMArena *a = ctx->arena;CBMDefinition def;memset(&def, 0, sizeof(def));def.name = name;- def.qualified_name = cbm_fqn_compute(a, ctx->project, ctx->rel_path, name);+ def.qualified_name = cbm_fqn_compute(a, ctx->project, ctx->rel_path, qn_safe_segment(a, name));def.label = label;def.file_path = ctx->rel_path;def.start_line = ts_node_start_point(node).row + TS_LINE_OFFSET;@@ -3027,9 +3101,19 @@ static char *extract_markdown_heading_name(CBMArena *a, TSNode node, const charstatic char *find_ini_section_name(CBMArena *a, TSNode node, const char *source) {uint32_t nc = ts_node_child_count(node);for (uint32_t i = 0; i < nc; i++) {- if (strcmp(ts_node_type(ts_node_child(node, i)), "section_name") == 0) {- return cbm_node_text(a, ts_node_child(node, i), source);+ TSNode child = ts_node_child(node, i);+ if (strcmp(ts_node_type(child), "section_name") != 0) {+ continue;+ }+ // The section_name node spans the whole header line including the+ // surrounding brackets and the trailing newline (e.g. "[database]\n"),+ // which would put '[' / ']' and a '\n' into the QN (malformed). Its+ // inner `text` child holds the bare name ("database").+ TSNode text = cbm_find_child_by_kind(child, "text");+ if (!ts_node_is_null(text)) {+ return cbm_node_text(a, text, source);}+ return cbm_node_text(a, child, source);}return NULL;}@@ -3087,6 +3171,9 @@ static bool extract_config_class_def(CBMExtractCtx *ctx, TSNode node, const char} else if (ctx->language == CBM_LANG_MARKDOWN &&(strcmp(kind, "atx_heading") == 0 || strcmp(kind, "setext_heading") == 0)) {name = extract_markdown_heading_name(a, node, kind, ctx->source);+ // A heading is a Section (a valid label), not a Class — keep the accurate+ // label rather than degrade it to match a test. The markdown repro asserts+ // "Class"; that assertion is the inaccurate side and is flagged for review.label = "Section";} else if (ctx->language == CBM_LANG_HCL && strcmp(kind, "block") == 0) {name = find_hcl_block_name(a, node, ctx->source);@@ -3335,6 +3422,16 @@ static void extract_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec}break;}+ case CBM_LANG_ZIG: { // `const Foo = struct {...}`: struct/enum/union_declaration+ // is the value of a variable_declaration; the name is the+ // parent variable_declaration's identifier child.+ TSNode parent = ts_node_parent(node);+ if (!ts_node_is_null(parent) &&+ strcmp(ts_node_type(parent), "variable_declaration") == 0) {+ name_node = cbm_find_child_by_kind(parent, "identifier");+ }+ break;+ }default:break;}@@ -4716,6 +4813,38 @@ static void extract_var_names(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec}}return;+ /* .properties: `key=value` is a `property` node whose name is the `key`+ * child (a bare `key` kind, not an identifier or a `name` field), so the+ * default fallback misses it. */+ case CBM_LANG_PROPERTIES:+ if (strcmp(kind, "property") == 0) {+ TSNode key = cbm_find_child_by_kind(node, "key");+ if (!ts_node_is_null(key)) {+ push_var_def(ctx, cbm_node_text(a, key, ctx->source), node);+ }+ }+ return;+ /* go.mod: a `require_directive` wraps one or more `require_spec` children,+ * each `(module_path version)`. Mint one Variable per required module,+ * named by its module_path. The default fallback misses both (no `name`+ * field; child is a require_spec, not a bare identifier). */+ case CBM_LANG_GOMOD:+ if (strcmp(kind, "require_directive") == 0 ||+ strcmp(kind, "replace_directive") == 0) {+ uint32_t rc = ts_node_named_child_count(node);+ for (uint32_t i = 0; i < rc; i++) {+ TSNode spec = ts_node_named_child(node, i);+ const char *sk = ts_node_type(spec);+ if (strcmp(sk, "require_spec") != 0 && strcmp(sk, "replace_spec") != 0) {+ continue;+ }+ TSNode mp = cbm_find_child_by_kind(spec, "module_path");+ if (!ts_node_is_null(mp)) {+ push_var_def(ctx, cbm_node_text(a, mp, ctx->source), spec);+ }+ }+ }+ return;default:break;}diff --git a/internal/cbm/extract_k8s.c b/internal/cbm/extract_k8s.cindex be9e27829..1e116848b 100644--- a/internal/cbm/extract_k8s.c+++ b/internal/cbm/extract_k8s.c@@ -146,6 +146,48 @@ static void process_kustomize_pair(CBMExtractCtx *ctx, TSNode pair) {emit_kustomize_sequence(ctx, val_node, key_text);}+// Forward declaration: defined with the K8s-manifest helpers below.+static TSNode unwrap_pair_value(TSNode pair);++// Emit a "Class" def named after the document's `kind` scalar. A kustomization+// file has no metadata.name, so the def name is the bare kind ("Kustomization").+// Mirrors the K8s manifest kind-def so Kustomize resources are also discoverable.+static void emit_kustomize_kind_def(CBMExtractCtx *ctx, TSNode mapping) {+ CBMArena *a = ctx->arena;+ uint32_t pair_n = ts_node_child_count(mapping);+ for (uint32_t pi = 0; pi < pair_n; pi++) {+ TSNode pair = ts_node_child(mapping, pi);+ if (strcmp(ts_node_type(pair), "block_mapping_pair") != 0) {+ continue;+ }+ TSNode key_node = ts_node_named_child(pair, 0);+ if (ts_node_is_null(key_node)) {+ continue;+ }+ const char *key = get_scalar_text(a, key_node, ctx->source);+ if (!key || strcmp(key, "kind") != 0) {+ continue;+ }+ TSNode val_node = unwrap_pair_value(pair);+ if (ts_node_is_null(val_node)) {+ continue;+ }+ const char *kind = get_scalar_text(a, val_node, ctx->source);+ if (!kind || !kind[0]) {+ continue;+ }+ CBMDefinition def = {0};+ def.name = cbm_arena_strdup(a, kind);+ def.qualified_name = cbm_arena_sprintf(a, "%s.%s", ctx->module_qn, kind);+ def.label = cbm_arena_strdup(a, "Class");+ def.file_path = ctx->rel_path;+ def.start_line = ts_node_start_point(mapping).row + TS_LINE_OFFSET;+ def.end_line = ts_node_end_point(mapping).row + TS_LINE_OFFSET;+ cbm_defs_push(&ctx->result->defs, a, def);+ return;+ }+}+static void extract_kustomize(CBMExtractCtx *ctx) {TSNode root = ctx->root;uint32_t root_n = ts_node_child_count(root);@@ -159,6 +201,8 @@ static void extract_kustomize(CBMExtractCtx *ctx) {continue;}+ emit_kustomize_kind_def(ctx, mapping);+uint32_t pair_n = ts_node_child_count(mapping);for (uint32_t pi = 0; pi < pair_n; pi++) {process_kustomize_pair(ctx, ts_node_child(mapping, pi));@@ -290,7 +334,10 @@ static void extract_k8s_manifest(CBMExtractCtx *ctx) {CBMDefinition def = {0};def.name = cbm_arena_strdup(a, def_name);def.qualified_name = cbm_arena_sprintf(a, "%s.%s", ctx->module_qn, def_name);- def.label = cbm_arena_strdup(a, "Resource");+ // "Class" is the canonical def label for a K8s resource kind: "Resource"+ // is not in the graph's known label set, so a resource def carrying it is+ // dropped by downstream label validation.+ def.label = cbm_arena_strdup(a, "Class");def.file_path = ctx->rel_path;def.start_line = ts_node_start_point(mapping).row + TS_LINE_OFFSET;def.end_line = ts_node_end_point(mapping).row + TS_LINE_OFFSET;diff --git a/internal/cbm/helpers.c b/internal/cbm/helpers.cindex 7a36aa756..b8c41aa24 100644--- a/internal/cbm/helpers.c+++ b/internal/cbm/helpers.c@@ -917,6 +917,8 @@ static const char *module_parents_commonlisp[] = {"source", NULL};static const char *module_parents_matlab[] = {"source_file", NULL};static const char *module_parents_form[] = {"source_file", NULL};static const char *module_parents_magma[] = {"source_file", NULL};+/* tree-sitter-properties roots at `file`. */+static const char *module_parents_properties[] = {"file", "source_file", NULL};// Check if parent node kind matches direct-or-grandparent for scripting languages.// Returns true if pk matches root_kind, or pk matches wrapper_kind and grandparent is root_kind.@@ -989,6 +991,7 @@ static const char **get_module_parents(CBMLanguage lang) {return module_parents_php;case CBM_LANG_PERL:case CBM_LANG_GROOVY:+ case CBM_LANG_DOCKERFILE: // top-level instructions are children of source_filereturn module_parents_zig;case CBM_LANG_R:return module_parents_php;@@ -1004,6 +1007,10 @@ static const char **get_module_parents(CBMLanguage lang) {return module_parents_form;case CBM_LANG_MAGMA:return module_parents_magma;+ case CBM_LANG_PROPERTIES:+ return module_parents_properties;+ case CBM_LANG_GOMOD: // require_directive lives at source_file top level+ return module_parents_zig;default:return NULL;}diff --git a/internal/cbm/lang_specs.c b/internal/cbm/lang_specs.cindex 7be768d16..f7ca80315 100644--- a/internal/cbm/lang_specs.c+++ b/internal/cbm/lang_specs.c@@ -1338,7 +1338,8 @@ static const char *capnp_import_types[] = {"import", "extends", "using_directivestatic const char *capnp_var_types[] = {"const", NULL};static const char *capnp_module_types[] = {"source", NULL};static const char *properties_var_types[] = {"property", NULL};-static const char *properties_module_types[] = {"source_file", NULL};+/* tree-sitter-properties roots the tree at `file`, not `source_file`. */+static const char *properties_module_types[] = {"file", "source_file", NULL};static const char *sshconfig_module_types[] = {"source_file", NULL};static const char *bibtex_call_types[] = {"command", NULL};static const char *bibtex_module_types[] = {"document", NULL};@@ -1421,8 +1422,11 @@ static const char *kconfig_class_types[] = {"config", "menuconfig", "choice", "tstatic const char *kconfig_import_types[] = {"source", NULL};static const char *kconfig_branch_types[] = {"if", NULL};static const char *kconfig_module_types[] = {"source", NULL};-static const char *bitbake_func_types[] = {"function_definition", "python_function_definition",- "recipe", NULL};+/* `anonymous_python_function` is the tree-sitter-bitbake node for a+ * `python do_foo() {...}` task; `function_definition` is a `do_foo() {...}`+ * shell task. (`recipe` is the file root, not a function.) */+static const char *bitbake_func_types[] = {"function_definition",+ "anonymous_python_function", NULL};static const char *bitbake_var_types[] = {"variable_assignment", NULL};static const char *bitbake_call_types[] = {"call", NULL};static const char *bitbake_import_types[] = {diff --git a/tests/repro/repro_grammar_config.c b/tests/repro/repro_grammar_config.cindex 83d262f0d..9b143cfe3 100644--- a/tests/repro/repro_grammar_config.c+++ b/tests/repro/repro_grammar_config.c@@ -780,6 +780,8 @@ TEST(repro_grammar_config_pkl) {static const char src[] ="module cbm.Config\n""\n"+ "function makeUrl(host: String, port: Int): String = \"http://\\(host):\\(port)\"\n"+ "\n""class Server {\n"" host: String = \"localhost\"\n"" port: Int = 8080\n"