Skip to content

Commit 444863f

Browse files
authored
Merge branch 'main' into fix/contained-indexing
2 parents 3450263 + b075f05 commit 444863f

16 files changed

Lines changed: 989 additions & 148 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
// Tree traversal limits.
2424
enum {
2525
TEMPLATE_DEPTH_LIMIT = 4,
26-
DECLARATOR_DEPTH_LIMIT = 8,
26+
DECLARATOR_DEPTH_LIMIT = CBM_DECLARATOR_DEPTH_LIMIT, // shared define in helpers.h
27+
2728
EXPORT_ANCESTOR_DEPTH = 4,
2829
DECORATOR_SCAN_LIMIT = 3,
2930
C_RETURN_WALK_DEPTH = 5,
@@ -457,27 +458,6 @@ static TSNode resolve_func_name_fp(TSNode node, CBMLanguage lang, const char *ki
457458
return null_node;
458459
}
459460

460-
// Check if a node type is a terminal C declarator name.
461-
static bool is_c_terminal_name(const char *dk) {
462-
return strcmp(dk, "identifier") == 0 || strcmp(dk, "field_identifier") == 0 ||
463-
strcmp(dk, "operator_name") == 0 || strcmp(dk, "operator_cast") == 0 ||
464-
strcmp(dk, "destructor_name") == 0;
465-
}
466-
467-
// Resolve name from a C++ qualified_identifier/scoped_identifier.
468-
static TSNode resolve_qualified_name(TSNode decl) {
469-
static const char *name_kinds[] = {"operator_name", "operator_cast", "destructor_name",
470-
"identifier", "field_identifier", NULL};
471-
for (const char **k = name_kinds; *k; k++) {
472-
TSNode found = cbm_find_child_by_kind(decl, *k);
473-
if (!ts_node_is_null(found)) {
474-
return found;
475-
}
476-
}
477-
TSNode null_node = {0};
478-
return null_node;
479-
}
480-
481461
// C++/CUDA: out-of-line method definitions name the function with a qualified
482462
// declarator (`Foo::bar`, or `ns::Foo::bar`). Return the immediate enclosing
483463
// class name (the scope segment directly left of the function name, e.g. "Foo"),
@@ -528,30 +508,6 @@ static char *cpp_out_of_line_parent_class(CBMArena *a, TSNode node, const char *
528508
return (text && text[0]) ? text : NULL;
529509
}
530510

531-
// Resolve function name from C/C++/CUDA/GLSL declarator chain.
532-
static TSNode resolve_c_declarator_name(TSNode node) {
533-
TSNode decl = ts_node_child_by_field_name(node, TS_FIELD("declarator"));
534-
for (int depth = 0; depth < DECLARATOR_DEPTH_LIMIT && !ts_node_is_null(decl); depth++) {
535-
const char *dk = ts_node_type(decl);
536-
if (is_c_terminal_name(dk)) {
537-
return decl;
538-
}
539-
if (strcmp(dk, "qualified_identifier") == 0 || strcmp(dk, "scoped_identifier") == 0) {
540-
return resolve_qualified_name(decl);
541-
}
542-
TSNode inner = ts_node_child_by_field_name(decl, TS_FIELD("declarator"));
543-
if (ts_node_is_null(inner) && ts_node_named_child_count(decl) > 0) {
544-
inner = ts_node_named_child(decl, 0);
545-
}
546-
if (ts_node_is_null(inner)) {
547-
break;
548-
}
549-
decl = inner;
550-
}
551-
TSNode null_node = {0};
552-
return null_node;
553-
}
554-
555511
// R: resolve function_definition name from parent binary_operator lhs.
556512
static TSNode resolve_r_func_name(TSNode node) {
557513
TSNode parent = ts_node_parent(node);
@@ -675,7 +631,7 @@ static TSNode resolve_func_name_c_family(TSNode *node_ptr, CBMLanguage lang, con
675631
lang == CBM_LANG_GLSL || lang == CBM_LANG_HLSL || lang == CBM_LANG_ISPC ||
676632
lang == CBM_LANG_SLANG) &&
677633
strcmp(kind, "function_definition") == 0) {
678-
return resolve_c_declarator_name(*node_ptr);
634+
return cbm_resolve_c_declarator_name_node(*node_ptr);
679635
}
680636
TSNode null_node = {0};
681637
return null_node;
@@ -3323,6 +3279,7 @@ static void extract_class_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
33233279
def.file_path = ctx->rel_path;
33243280
def.start_line = ts_node_start_point(node).row + TS_LINE_OFFSET;
33253281
def.end_line = ts_node_end_point(node).row + TS_LINE_OFFSET;
3282+
def.lines = (int)(def.end_line - def.start_line + TS_LINE_OFFSET);
33263283
def.is_exported = cbm_is_exported(name, ctx->language);
33273284
def.base_classes = extract_base_classes(a, node, ctx->source, ctx->language);
33283285
def.decorators = extract_decorators(a, node, ctx->source, ctx->language, spec);

‎internal/cbm/extract_unified.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ static TSNode resolve_func_name_node(TSNode node) {
101101
if (ts_node_is_null(name_node) && strcmp(ts_node_type(node), "function_declaration") == 0) {
102102
name_node = cbm_find_child_by_kind(node, "simple_identifier");
103103
}
104+
/* C/C++/CUDA/GLSL: function_definition name lives in the declarator chain. */
105+
if (ts_node_is_null(name_node) && strcmp(ts_node_type(node), "function_definition") == 0) {
106+
name_node = cbm_resolve_c_declarator_name_node(node);
107+
}
104108
return name_node;
105109
}
106110

‎internal/cbm/helpers.c‎

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,52 @@ TSNode cbm_find_enclosing_func(TSNode node, CBMLanguage lang) {
717717
return null_node;
718718
}
719719

720-
// Get the name of a function node (basic: try "name" field)
720+
// Check if a node type is a terminal C declarator name.
721+
static bool is_c_terminal_name(const char *dk) {
722+
return strcmp(dk, "identifier") == 0 || strcmp(dk, "field_identifier") == 0 ||
723+
strcmp(dk, "operator_name") == 0 || strcmp(dk, "operator_cast") == 0 ||
724+
strcmp(dk, "destructor_name") == 0;
725+
}
726+
727+
// Resolve name from a C++ qualified_identifier/scoped_identifier.
728+
static TSNode resolve_qualified_name(TSNode decl) {
729+
static const char *name_kinds[] = {"operator_name", "operator_cast", "destructor_name",
730+
"identifier", "field_identifier", NULL};
731+
for (const char **k = name_kinds; *k; k++) {
732+
TSNode found = cbm_find_child_by_kind(decl, *k);
733+
if (!ts_node_is_null(found)) {
734+
return found;
735+
}
736+
}
737+
TSNode null_node = {0};
738+
return null_node;
739+
}
740+
741+
// Resolve function name from C/C++/CUDA/GLSL declarator chain. Shared canonical
742+
// implementation — see the header for the full rationale (#438).
743+
TSNode cbm_resolve_c_declarator_name_node(TSNode func_node) {
744+
TSNode decl = ts_node_child_by_field_name(func_node, TS_FIELD("declarator"));
745+
for (int depth = 0; depth < CBM_DECLARATOR_DEPTH_LIMIT && !ts_node_is_null(decl); depth++) {
746+
const char *dk = ts_node_type(decl);
747+
if (is_c_terminal_name(dk)) {
748+
return decl;
749+
}
750+
if (strcmp(dk, "qualified_identifier") == 0 || strcmp(dk, "scoped_identifier") == 0) {
751+
return resolve_qualified_name(decl);
752+
}
753+
TSNode inner = ts_node_child_by_field_name(decl, TS_FIELD("declarator"));
754+
if (ts_node_is_null(inner) && ts_node_named_child_count(decl) > 0) {
755+
inner = ts_node_named_child(decl, 0);
756+
}
757+
if (ts_node_is_null(inner)) {
758+
break;
759+
}
760+
decl = inner;
761+
}
762+
TSNode null_node = {0};
763+
return null_node;
764+
}
765+
721766
static const char *func_node_name(CBMArena *a, TSNode func_node, const char *source,
722767
CBMLanguage lang) {
723768
// Wolfram: set_delayed_top/set_top/set_delayed/set — LHS is apply(user_symbol("f"), ...)
@@ -752,6 +797,13 @@ static const char *func_node_name(CBMArena *a, TSNode func_node, const char *sou
752797
}
753798
}
754799
}
800+
// C/C++/CUDA/GLSL: function_definition carries its name in the declarator chain.
801+
if (strcmp(ts_node_type(func_node), "function_definition") == 0) {
802+
TSNode dn = cbm_resolve_c_declarator_name_node(func_node);
803+
if (!ts_node_is_null(dn)) {
804+
return cbm_node_text(a, dn, source);
805+
}
806+
}
755807
return NULL;
756808
}
757809

‎internal/cbm/helpers.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ const char *cbm_enclosing_func_qn(CBMArena *a, TSNode node, CBMLanguage lang, co
3636
// Cached version: uses ctx->ef_cache to avoid repeated parent-chain walks.
3737
const char *cbm_enclosing_func_qn_cached(CBMExtractCtx *ctx, TSNode node);
3838

39+
// Max declarator-chain descent depth for C/C++/CUDA/GLSL function-name
40+
// resolution. Single source of truth — extract_defs.c's DECLARATOR_DEPTH_LIMIT
41+
// is derived from this so the three extractors cannot drift.
42+
#define CBM_DECLARATOR_DEPTH_LIMIT 8
43+
44+
// Resolve the function-name node for a C/C++/CUDA/GLSL `function_definition`.
45+
// Such nodes have no `name` field — the name is nested in the declarator chain
46+
// (pointer/function/parenthesized/array declarators wrap it; out-of-line method
47+
// definitions name it with a qualified_identifier). Descends the `declarator`
48+
// field to the innermost name node and returns it, or a null node if none is
49+
// found. Shared by the defs, calls, and unified extractors so all three agree on
50+
// enclosing-function attribution — drift between private copies caused #438.
51+
TSNode cbm_resolve_c_declarator_name_node(TSNode func_node);
52+
3953
// Find a child node by kind string.
4054
TSNode cbm_find_child_by_kind(TSNode parent, const char *kind);
4155

‎src/foundation/str_util.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "foundation/constants.h"
77
#include <string.h>
88
#include <ctype.h>
9+
#include <stdio.h>
910

1011
enum {
1112
JSON_ESC_LEN = 2, /* escaped char takes 2 bytes (backslash + char) */
@@ -328,8 +329,11 @@ int cbm_json_escape(char *buf, int bufsize, const char *src) {
328329
buf[pos++] = '\\';
329330
buf[pos++] = 't';
330331
} else if (c < JSON_CTRL_LIMIT) {
331-
/* Other control chars: skip */
332-
continue;
332+
/* Other control chars: escape as \u00XX */
333+
if (pos + 6 > bufsize - JSON_NUL_RESERVE) {
334+
break;
335+
}
336+
pos += snprintf(buf + pos, 7, "\\u%04x", c);
333337
} else {
334338
buf[pos++] = (char)c;
335339
}

‎src/git/git_context.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ static int json_escaped_len(const char *src) {
316316
if (c == '"' || c == '\\' || c == '\n' || c == '\r' || c == '\t') {
317317
len += 2;
318318
} else if (c < 0x20) {
319-
continue;
319+
len += 6; /* \u00XX */
320320
} else {
321321
len++;
322322
}

‎src/mcp/mcp.c‎

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,13 @@ static const tool_def_t TOOLS[] = {
392392
"structure at a glance. Includes 'clusters': Leiden community detection over the call/import "
393393
"graph, surfacing the de-facto modules (each with a label, member count, cohesion score, "
394394
"representative top_nodes, and the packages/edge_types that bind it) — use these to grasp "
395-
"the real architectural seams, which often cut across the folder layout.",
396-
"{\"type\":\"object\",\"properties\":{\"project\":{\"type\":\"string\"},\"aspects\":{\"type\":"
397-
"\"array\",\"items\":{\"type\":\"string\"}}},\"required\":[\"project\"]}"},
395+
"the real architectural seams, which often cut across the folder layout. Optional path scopes "
396+
"analysis to nodes under that directory prefix (file_path).",
397+
"{\"type\":\"object\",\"properties\":{\"project\":{\"type\":\"string\"},\"path\":{\"type\":"
398+
"\"string\",\"description\":\"Optional directory prefix to scope architecture (e.g. "
399+
"apps/hoa)\"},"
400+
"\"aspects\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}}},\"required\":[\"project\"]"
401+
"}"},
398402

399403
{"search_code",
400404
"Graph-augmented code search. Finds text patterns via grep, then enriches results with "
@@ -1918,12 +1922,14 @@ static void append_cross_repo_summary(yyjson_mut_doc *doc, yyjson_mut_val *root,
19181922

19191923
static char *handle_get_architecture(cbm_mcp_server_t *srv, const char *args) {
19201924
char *project = cbm_mcp_get_string_arg(args, "project");
1925+
char *scope_path = cbm_mcp_get_string_arg(args, "path");
19211926
cbm_store_t *store = resolve_store(srv, project);
19221927
REQUIRE_STORE(store, project);
19231928

19241929
char *not_indexed = verify_project_indexed(store, project);
19251930
if (not_indexed) {
19261931
free(project);
1932+
free(scope_path);
19271933
return not_indexed;
19281934
}
19291935

@@ -1963,14 +1969,17 @@ static char *handle_get_architecture(cbm_mcp_server_t *srv, const char *args) {
19631969
/* Counts-only: this handler renders label/type counts but never property
19641970
* keys, and full key discovery json_each-scans every row (seconds-to-
19651971
* minutes on multi-million-node graphs). */
1966-
cbm_store_get_schema_counts(store, project, &schema);
1972+
cbm_store_get_schema_counts_scoped(store, project, scope_path, &schema);
19671973

19681974
cbm_architecture_info_t arch = {0};
1969-
cbm_store_get_architecture(store, project, aspects_strs_count > 0 ? aspects_strs : NULL,
1970-
aspects_strs_count, &arch);
1975+
cbm_store_get_architecture(store, project, scope_path,
1976+
aspects_strs_count > 0 ? aspects_strs : NULL, aspects_strs_count,
1977+
&arch);
19711978

1972-
int node_count = cbm_store_count_nodes(store, project);
1973-
int edge_count = cbm_store_count_edges(store, project);
1979+
int node_count = cbm_store_count_nodes_scoped(store, project, scope_path);
1980+
int edge_count = cbm_store_count_edges_scoped(store, project, scope_path);
1981+
char norm_path[CBM_SZ_512];
1982+
bool path_scoped = cbm_store_normalize_arch_path(scope_path, norm_path, sizeof(norm_path));
19741983

19751984
yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
19761985
yyjson_mut_val *root = yyjson_mut_obj(doc);
@@ -1979,6 +1988,15 @@ static char *handle_get_architecture(cbm_mcp_server_t *srv, const char *args) {
19791988
if (project) {
19801989
yyjson_mut_obj_add_str(doc, root, "project", project);
19811990
}
1991+
if (path_scoped) {
1992+
yyjson_mut_obj_add_str(doc, root, "path", norm_path);
1993+
int root_nodes = cbm_store_count_nodes(store, project);
1994+
int root_edges = cbm_store_count_edges(store, project);
1995+
yyjson_mut_obj_add_int(doc, root, "root_total_nodes", root_nodes);
1996+
yyjson_mut_obj_add_int(doc, root, "root_total_edges", root_edges);
1997+
yyjson_mut_obj_add_int(doc, root, "scoped_total_nodes", node_count);
1998+
yyjson_mut_obj_add_int(doc, root, "scoped_total_edges", edge_count);
1999+
}
19822000
yyjson_mut_obj_add_int(doc, root, "total_nodes", node_count);
19832001
yyjson_mut_obj_add_int(doc, root, "total_edges", edge_count);
19842002

@@ -2194,6 +2212,7 @@ static char *handle_get_architecture(cbm_mcp_server_t *srv, const char *args) {
21942212
yyjson_doc_free(aspects_doc);
21952213
}
21962214
free(project);
2215+
free(scope_path);
21972216

21982217
char *result = cbm_mcp_text_result(json, false);
21992218
free(json);

‎src/pipeline/pipeline.c‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ struct cbm_pipeline {
9898
/* Committed graph size at dump time (-1 = dump did not run). #334 gate axis. */
9999
int committed_nodes;
100100
int committed_edges;
101+
102+
/* ADR (project_summaries) captured before a full-reindex DB delete, so it
103+
* can be restored after the rebuild. NULL when no ADR existed. Issue #516. */
104+
char *saved_adr;
101105
};
102106

103107
/* ── Global pkgmap (one active pipeline at a time) ─────────────── */
@@ -184,6 +188,9 @@ void cbm_pipeline_free(cbm_pipeline_t *p) {
184188
p->excluded_dirs = NULL;
185189
p->excluded_count = 0;
186190
free(p->branch_qn);
191+
free(p->saved_adr); /* freed here too: error paths can exit before the
192+
* restore in dump_and_persist_hashes runs. Issue #516. */
193+
p->saved_adr = NULL;
187194
cbm_git_context_free(&p->git_ctx);
188195
/* gbuf, store, registry freed during/after run */
189196
/* Defensively free userconfig in case run() was never called or panicked */
@@ -812,6 +819,22 @@ static int try_incremental_or_delete_db(cbm_pipeline_t *p, cbm_file_info_t *file
812819
cbm_store_close(check_store);
813820
}
814821
cbm_log_info("pipeline.route", "path", "reindex", "action", "deleting old db");
822+
/* Capture any ADR before deleting the DB so the full-reindex rebuild can
823+
* restore it (project_summaries is otherwise lost). Issue #516. */
824+
{
825+
cbm_store_t *adr_store = cbm_store_open_path(db_path);
826+
if (adr_store) {
827+
cbm_adr_t existing;
828+
if (cbm_store_adr_get(adr_store, p->project_name, &existing) == CBM_STORE_OK) {
829+
if (existing.content) {
830+
free(p->saved_adr);
831+
p->saved_adr = strdup(existing.content);
832+
}
833+
cbm_store_adr_free(&existing);
834+
}
835+
cbm_store_close(adr_store);
836+
}
837+
}
815838
cbm_unlink(db_path);
816839
char wal[PL_WAL_BUF];
817840
char shm[PL_WAL_BUF];
@@ -871,6 +894,14 @@ static int dump_and_persist_hashes(cbm_pipeline_t *p, const cbm_file_info_t *fil
871894
cbm_store_t *hash_store = cbm_store_open_path(db_path);
872895
if (hash_store) {
873896
cbm_store_delete_file_hashes(hash_store, p->project_name);
897+
898+
/* Restore the ADR captured before the dump. Surface a failed restore
899+
* rather than silently dropping the ADR (the original #516 symptom). */
900+
if (p->saved_adr) {
901+
if (cbm_store_adr_store(hash_store, p->project_name, p->saved_adr) != CBM_STORE_OK) {
902+
cbm_log_error("pipeline.err", "phase", "adr_restore", "project", p->project_name);
903+
}
904+
}
874905
for (int i = 0; i < file_count; i++) {
875906
struct stat fst;
876907
if (stat(files[i].path, &fst) == 0) {
@@ -897,6 +928,8 @@ static int dump_and_persist_hashes(cbm_pipeline_t *p, const cbm_file_info_t *fil
897928
cbm_store_close(hash_store);
898929
cbm_log_info("pass.timing", "pass", "persist_hashes", "files", itoa_buf(file_count));
899930
}
931+
free(p->saved_adr);
932+
p->saved_adr = NULL;
900933

901934
/* Export persistent artifact if enabled */
902935
if (p->persistence) {

0 commit comments

Comments
 (0)