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.

Achieve zero clang-tidy errors at CC<=25 threshold

Martin Vogelb523116ef278
diff --git a/Makefile.cbm b/Makefile.cbm
index 6b2aa1b34..eeb2b9c00 100644
--- a/Makefile.cbm
+++ b/Makefile.cbm
@@ -524,12 +524,21 @@ lint-format:
@echo "=== clang-format ==="
@$(CLANG_FORMAT) --dry-run --Werror $(LINT_SRCS) $(LINT_HDRS)
+# Ban NOLINTNEXTLINE in our code (vendored code exempt)
+lint-no-suppress:
+ @echo "=== NOLINTNEXTLINE check ==="
+ @if grep -rn 'NOLINTNEXTLINE\|NOLINTBEGIN\|NOLINTEND' src/ internal/cbm/*.c 2>/dev/null | grep -v vendored; then \
+ echo "ERROR: NOLINTNEXTLINE/NOLINTBEGIN/NOLINTEND found in source code."; \
+ echo "Fix the underlying issue instead of suppressing the linter."; \
+ exit 1; \
+ fi
+
# All linters (run with make -j3 lint for parallel execution)
-lint: lint-tidy lint-cppcheck lint-format
+lint: lint-tidy lint-cppcheck lint-format lint-no-suppress
@echo "=== All linters passed ==="
# CI linters (no clang-tidy — platform-dependent, enforced locally via pre-commit)
-lint-ci: lint-cppcheck lint-format
+lint-ci: lint-cppcheck lint-format lint-no-suppress
@echo "=== CI linters passed ==="
# ── Security audit (6 layers) ────────────────────────────────────
diff --git a/internal/cbm/extract_imports.c b/internal/cbm/extract_imports.c
index 51a6f353b..9da4d00f0 100644
--- a/internal/cbm/extract_imports.c
+++ b/internal/cbm/extract_imports.c
@@ -186,10 +186,8 @@ static void emit_py_import_from_name(CBMExtractCtx *ctx, TSNode child, const cha
static void process_py_import_from(CBMExtractCtx *ctx, TSNode node) {
CBMArena *a = ctx->arena;
TSNode module_node = resolve_py_module_node(node);
- // NOLINTBEGIN(readability-implicit-bool-conversion)
char *mod_path =
ts_node_is_null(module_node) ? NULL : cbm_node_text(a, module_node, ctx->source);
- // NOLINTEND(readability-implicit-bool-conversion)
uint32_t nc = ts_node_child_count(node);
for (uint32_t j = 0; j < nc; j++) {
diff --git a/src/pipeline/pass_calls.c b/src/pipeline/pass_calls.c
index 819072bc5..7f641caeb 100644
--- a/src/pipeline/pass_calls.c
+++ b/src/pipeline/pass_calls.c
@@ -66,7 +66,23 @@ static const char *itoa_log(int val) {
/* Build per-file import map from cached extraction result or graph buffer edges.
* Returns parallel arrays of (local_name, module_qn) pairs. Caller frees. */
-// NOLINTNEXTLINE(readability-function-cognitive-complexity)
+/* Parse "local_name":"value" from JSON properties string. Returns strdup'd key or NULL. */
+static char *extract_local_name_from_json(const char *props_json) {
+ if (!props_json) {
+ return NULL;
+ }
+ const char *start = strstr(props_json, "\"local_name\":\"");
+ if (!start) {
+ return NULL;
+ }
+ start += strlen("\"local_name\":\"");
+ const char *end = strchr(start, '"');
+ if (!end || end <= start) {
+ return NULL;
+ }
+ return cbm_strndup(start, end - start);
+}
+
static int build_import_map(cbm_pipeline_ctx_t *ctx, const char *rel_path,
const CBMFileResult *result, const char ***out_keys,
const char ***out_vals, int *out_count) {
@@ -128,19 +144,11 @@ static int build_import_map(cbm_pipeline_ctx_t *ctx, const char *rel_path,
if (!target) {
continue;
}
-
- if (e->properties_json) {
- const char *start = strstr(e->properties_json, "\"local_name\":\"");
- if (start) {
- start += strlen("\"local_name\":\"");
- const char *end = strchr(start, '"');
- if (end && end > start) {
- char *key = cbm_strndup(start, end - start);
- keys[count] = key;
- vals[count] = target->qualified_name;
- count++;
- }
- }
+ char *key = extract_local_name_from_json(e->properties_json);
+ if (key) {
+ keys[count] = key;
+ vals[count] = target->qualified_name;
+ count++;
}
}
@@ -162,7 +170,172 @@ static void free_import_map(const char **keys, const char **vals, int count) {
}
}
-// NOLINTNEXTLINE(readability-function-cognitive-complexity)
+/* Handle a route registration call: create Route node + HANDLES edge. */
+static void handle_route_registration(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
+ const cbm_gbuf_node_t *source_node, const char *module_qn,
+ const char **imp_keys, const char **imp_vals, int imp_count) {
+ const char *method = cbm_service_pattern_route_method(call->callee_name);
+ char route_qn[CBM_ROUTE_QN_SIZE];
+ snprintf(route_qn, sizeof(route_qn), "__route__%s__%s", method ? method : "ANY",
+ call->first_string_arg);
+ char route_props[256];
+ snprintf(route_props, sizeof(route_props), "{\"method\":\"%s\"}", method ? method : "ANY");
+ int64_t route_id = cbm_gbuf_upsert_node(ctx->gbuf, "Route", call->first_string_arg, route_qn,
+ "", 0, 0, route_props);
+ char props[512];
+ snprintf(props, sizeof(props),
+ "{\"callee\":\"%s\",\"url_path\":\"%s\",\"via\":\"route_registration\"}",
+ call->callee_name, call->first_string_arg);
+ cbm_gbuf_insert_edge(ctx->gbuf, source_node->id, route_id, "CALLS", props);
+ if (call->second_arg_name != NULL && call->second_arg_name[0] != '\0') {
+ cbm_resolution_t hres = cbm_registry_resolve(ctx->registry, call->second_arg_name,
+ module_qn, imp_keys, imp_vals, imp_count);
+ if (hres.qualified_name != NULL && hres.qualified_name[0] != '\0') {
+ const cbm_gbuf_node_t *handler = cbm_gbuf_find_by_qn(ctx->gbuf, hres.qualified_name);
+ if (handler != NULL) {
+ char hprops[256];
+ snprintf(hprops, sizeof(hprops), "{\"handler\":\"%s\"}", hres.qualified_name);
+ cbm_gbuf_insert_edge(ctx->gbuf, handler->id, route_id, "HANDLES", hprops);
+ }
+ }
+ }
+}
+
+/* Emit an HTTP/async route edge for a service call. */
+/* Build route QN and upsert Route node for HTTP/async edge. */
+static int64_t create_svc_route_node(cbm_pipeline_ctx_t *ctx, const char *url, cbm_svc_kind_t svc,
+ const char *method, const char *broker) {
+ char route_qn[CBM_ROUTE_QN_SIZE];
+ const char *prefix =
+ (svc == CBM_SVC_HTTP) ? (method ? method : "ANY") : (broker ? broker : "async");
+ snprintf(route_qn, sizeof(route_qn), "__route__%s__%s", prefix, url);
+ const char *rp = (svc == CBM_SVC_HTTP) ? (method ? method : "{}") : (broker ? broker : "{}");
+ return cbm_gbuf_upsert_node(ctx->gbuf, "Route", url, route_qn, "", 0, 0, rp);
+}
+
+static void emit_http_async_edge(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
+ const cbm_gbuf_node_t *source, const cbm_gbuf_node_t *target,
+ const cbm_resolution_t *res, cbm_svc_kind_t svc) {
+ const char *url_or_topic = call->first_string_arg;
+ bool is_url = (url_or_topic && url_or_topic[0] != '\0' &&
+ (url_or_topic[0] == '/' || strstr(url_or_topic, "://") != NULL));
+ bool is_topic = (url_or_topic && url_or_topic[0] != '\0' && svc == CBM_SVC_ASYNC &&
+ strlen(url_or_topic) > 2);
+ if (!is_url && !is_topic) {
+ char props[512];
+ snprintf(props, sizeof(props),
+ "{\"callee\":\"%s\",\"confidence\":%.2f,\"strategy\":\"%s\",\"candidates\":%d}",
+ call->callee_name, res->confidence, res->strategy ? res->strategy : "unknown",
+ res->candidate_count);
+ cbm_gbuf_insert_edge(ctx->gbuf, source->id, target->id, "CALLS", props);
+ return;
+ }
+ const char *edge_type = (svc == CBM_SVC_HTTP) ? "HTTP_CALLS" : "ASYNC_CALLS";
+ const char *method =
+ (svc == CBM_SVC_HTTP) ? cbm_service_pattern_http_method(call->callee_name) : NULL;
+ const char *broker =
+ (svc == CBM_SVC_ASYNC) ? cbm_service_pattern_broker(res->qualified_name) : NULL;
+ int64_t route_id = create_svc_route_node(ctx, url_or_topic, svc, method, broker);
+ char props[512];
+ snprintf(props, sizeof(props), "{\"callee\":\"%s\",\"url_path\":\"%s\"%s%s%s%s%s}",
+ call->callee_name, url_or_topic, method ? ",\"method\":\"" : "", method ? method : "",
+ method ? "\"" : "", broker ? ",\"broker\":\"" : "", broker ? broker : "");
+ if (broker) {
+ size_t plen = strlen(props);
+ if (plen > 0 && props[plen - 1] != '}') {
+ snprintf(props + plen - 1, sizeof(props) - plen + 1, "\"}");
+ }
+ }
+ cbm_gbuf_insert_edge(ctx->gbuf, source->id, route_id, edge_type, props);
+}
+
+/* Classify a resolved call and emit the appropriate edge. */
+static void emit_classified_edge(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
+ const cbm_gbuf_node_t *source, const cbm_gbuf_node_t *target,
+ const cbm_resolution_t *res, const char *module_qn,
+ const char **imp_keys, const char **imp_vals, int imp_count) {
+ cbm_svc_kind_t svc = cbm_service_pattern_match(res->qualified_name);
+ if (svc == CBM_SVC_ROUTE_REG && call->first_string_arg && call->first_string_arg[0] == '/') {
+ handle_route_registration(ctx, call, source, module_qn, imp_keys, imp_vals, imp_count);
+ return;
+ }
+ if (svc == CBM_SVC_HTTP || svc == CBM_SVC_ASYNC) {
+ emit_http_async_edge(ctx, call, source, target, res, svc);
+ return;
+ }
+ if (svc == CBM_SVC_CONFIG) {
+ char props[512];
+ snprintf(props, sizeof(props), "{\"callee\":\"%s\",\"key\":\"%s\",\"confidence\":%.2f}",
+ call->callee_name, call->first_string_arg ? call->first_string_arg : "",
+ res->confidence);
+ cbm_gbuf_insert_edge(ctx->gbuf, source->id, target->id, "CONFIGURES", props);
+ return;
+ }
+ char props[512];
+ snprintf(props, sizeof(props),
+ "{\"callee\":\"%s\",\"confidence\":%.2f,\"strategy\":\"%s\",\"candidates\":%d}",
+ call->callee_name, res->confidence, res->strategy ? res->strategy : "unknown",
+ res->candidate_count);
+ cbm_gbuf_insert_edge(ctx->gbuf, source->id, target->id, "CALLS", props);
+}
+
+/* Find source node for a call: enclosing function or file node. */
+static const cbm_gbuf_node_t *calls_find_source(cbm_pipeline_ctx_t *ctx, const char *rel,
+ const char *enclosing_qn) {
+ const cbm_gbuf_node_t *src = NULL;
+ if (enclosing_qn) {
+ src = cbm_gbuf_find_by_qn(ctx->gbuf, enclosing_qn);
+ }
+ if (!src) {
+ char *fqn = cbm_pipeline_fqn_compute(ctx->project_name, rel, "__file__");
+ src = cbm_gbuf_find_by_qn(ctx->gbuf, fqn);
+ free(fqn);
+ }
+ return src;
+}
+
+/* Resolve one call and emit the appropriate edge. Returns 1 if resolved, 0 if not. */
+static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call, const char *rel,
+ const char *module_qn, const char **imp_keys, const char **imp_vals,
+ int imp_count) {
+ const cbm_gbuf_node_t *source_node = calls_find_source(ctx, rel, call->enclosing_func_qn);
+ if (!source_node) {
+ return 0;
+ }
+ cbm_resolution_t res = cbm_registry_resolve(ctx->registry, call->callee_name, module_qn,
+ imp_keys, imp_vals, imp_count);
+ if (!res.qualified_name || res.qualified_name[0] == '\0') {
+ return 0;
+ }
+ const cbm_gbuf_node_t *target_node = cbm_gbuf_find_by_qn(ctx->gbuf, res.qualified_name);
+ if (!target_node || source_node->id == target_node->id) {
+ return 0;
+ }
+ emit_classified_edge(ctx, call, source_node, target_node, &res, module_qn, imp_keys, imp_vals,
+ imp_count);
+ return 1;
+}
+
+static CBMFileResult *calls_get_or_extract(cbm_pipeline_ctx_t *ctx, int idx,
+ const cbm_file_info_t *fi, bool *owned) {
+ *owned = false;
+ if (ctx->result_cache && ctx->result_cache[idx]) {
+ return ctx->result_cache[idx];
+ }
+ int slen = 0;
+ char *src = read_file(fi->path, &slen);
+ if (!src) {
+ return NULL;
+ }
+ CBMFileResult *r = cbm_extract_file(src, slen, fi->language, ctx->project_name, fi->rel_path,
+ CBM_EXTRACT_BUDGET, NULL, NULL);
+ free(src);
+ if (r) {
+ *owned = true;
+ }
+ return r;
+}
+
int cbm_pipeline_pass_calls(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files, int file_count) {
cbm_log_info("pass.start", "pass", "calls", "files", itoa_log(file_count));
@@ -176,31 +349,12 @@ int cbm_pipeline_pass_calls(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *file
return -1;
}
- const char *path = files[i].path;
const char *rel = files[i].rel_path;
-
- /* Use cached extraction result or re-extract */
- CBMFileResult *result = NULL;
bool result_owned = false;
- if (ctx->result_cache) {
- result = ctx->result_cache[i];
- }
+ CBMFileResult *result = calls_get_or_extract(ctx, i, &files[i], &result_owned);
if (!result) {
- CBMLanguage lang = files[i].language;
- int source_len = 0;
- char *source = read_file(path, &source_len);
- if (!source) {
- errors++;
- continue;
- }
- result = cbm_extract_file(source, source_len, lang, ctx->project_name, rel,
- CBM_EXTRACT_BUDGET, NULL, NULL);
- free(source);
- if (!result) {
- errors++;
- continue;
- }
- result_owned = true;
+ errors++;
+ continue;
}
if (result->calls.count == 0) {
@@ -225,168 +379,12 @@ int cbm_pipeline_pass_calls(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *file
if (!call->callee_name) {
continue;
}
-
total_calls++;
-
- /* Find enclosing function node (source of CALLS edge) */
- const cbm_gbuf_node_t *source_node = NULL;
- if (call->enclosing_func_qn) {
- source_node = cbm_gbuf_find_by_qn(ctx->gbuf, call->enclosing_func_qn);
- }
- if (!source_node) {
- /* Try module-level: file node as source */
- char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel, "__file__");
- source_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn);
- free(file_qn);
- }
- if (!source_node) {
- unresolved++;
- continue;
- }
-
- /* Resolve callee through registry */
- cbm_resolution_t res = cbm_registry_resolve(ctx->registry, call->callee_name, module_qn,
- imp_keys, imp_vals, imp_count);
-
- if (!res.qualified_name || res.qualified_name[0] == '\0') {
- unresolved++;
- continue;
- }
-
- /* Find target node in graph buffer */
- const cbm_gbuf_node_t *target_node = cbm_gbuf_find_by_qn(ctx->gbuf, res.qualified_name);
- if (!target_node) {
- unresolved++;
- continue;
- }
-
- /* Skip self-calls */
- if (source_node->id == target_node->id) {
- continue;
- }
-
- /* Classify edge type by library in resolved QN */
- cbm_svc_kind_t svc = cbm_service_pattern_match(res.qualified_name);
-
- if (svc == CBM_SVC_ROUTE_REG && call->first_string_arg != NULL &&
- call->first_string_arg[0] == '/') {
- /* Route registration: router.GET("/path", handler) → Route + HANDLES */
- const char *method = cbm_service_pattern_route_method(call->callee_name);
- char route_qn[CBM_ROUTE_QN_SIZE];
- snprintf(route_qn, sizeof(route_qn), "__route__%s__%s", method ? method : "ANY",
- call->first_string_arg);
- char route_props[256];
- snprintf(route_props, sizeof(route_props), "{\"method\":\"%s\"}",
- method ? method : "ANY");
- int64_t route_id = cbm_gbuf_upsert_node(ctx->gbuf, "Route", call->first_string_arg,
- route_qn, "", 0, 0, route_props);
-
- char props[512];
- snprintf(props, sizeof(props),
- "{\"callee\":\"%s\",\"url_path\":\"%s\",\"via\":\"route_registration\"}",
- call->callee_name, call->first_string_arg);
- cbm_gbuf_insert_edge(ctx->gbuf, source_node->id, route_id, "CALLS", props);
-
- /* Resolve handler and create HANDLES edge */
- if (call->second_arg_name != NULL && call->second_arg_name[0] != '\0') {
- cbm_resolution_t hres =
- cbm_registry_resolve(ctx->registry, call->second_arg_name, module_qn,
- imp_keys, imp_vals, imp_count);
- if (hres.qualified_name != NULL && hres.qualified_name[0] != '\0') {
- const cbm_gbuf_node_t *handler =
- cbm_gbuf_find_by_qn(ctx->gbuf, hres.qualified_name);
- if (handler != NULL) {
- char hprops[256];
- snprintf(hprops, sizeof(hprops), "{\"handler\":\"%s\"}",
- hres.qualified_name);
- cbm_gbuf_insert_edge(ctx->gbuf, handler->id, route_id, "HANDLES",
- hprops);
- }
- }
- }
+ if (resolve_single_call(ctx, call, rel, module_qn, imp_keys, imp_vals, imp_count)) {
resolved++;
- continue;
- }
-
- if (svc == CBM_SVC_HTTP || svc == CBM_SVC_ASYNC) {
- /* HTTP/async call — route through Route node for cross-service traversal.
- * Only create Route if string looks like a URL (HTTP) or topic name (async). */
- const char *url_or_topic = call->first_string_arg;
- int is_valid_url =
- (url_or_topic != NULL && url_or_topic[0] != '\0' &&
- (url_or_topic[0] == '/' || strstr(url_or_topic, "://") != NULL));
- int is_valid_topic = (url_or_topic != NULL && url_or_topic[0] != '\0' &&
- svc == CBM_SVC_ASYNC && strlen(url_or_topic) > 2);
- if (is_valid_url || is_valid_topic) {
- const char *edge_type = (svc == CBM_SVC_HTTP) ? "HTTP_CALLS" : "ASYNC_CALLS";
- const char *http_method =
- (svc == CBM_SVC_HTTP) ? cbm_service_pattern_http_method(call->callee_name)
- : NULL;
- const char *broker = (svc == CBM_SVC_ASYNC)
- ? cbm_service_pattern_broker(res.qualified_name)
- : NULL;
-
- /* Build Route QN: __route__METHOD__/path or __route__broker__topic */
- char route_qn[CBM_ROUTE_QN_SIZE];
- if (svc == CBM_SVC_HTTP) {
- snprintf(route_qn, sizeof(route_qn), "__route__%s__%s",
- http_method ? http_method : "ANY", url_or_topic);
- } else {
- snprintf(route_qn, sizeof(route_qn), "__route__%s__%s",
- broker ? broker : "async", url_or_topic);
- }
-
- /* Create or find Route node */
- int64_t route_id = cbm_gbuf_upsert_node(
- ctx->gbuf, "Route", url_or_topic, route_qn, "", 0, 0,
- svc == CBM_SVC_HTTP ? (http_method ? http_method : "{}")
- : (broker ? broker : "{}"));
-
- /* Edge: caller → Route */
- char props[512];
- snprintf(props, sizeof(props),
- "{\"callee\":\"%s\",\"url_path\":\"%s\"%s%s%s%s%s}", call->callee_name,
- url_or_topic, http_method ? ",\"method\":\"" : "",
- http_method ? http_method : "", http_method ? "\"" : "",
- broker ? ",\"broker\":\"" : "", broker ? broker : "");
- if (broker) {
- /* Close the broker value quote */
- size_t plen = strlen(props);
- if (plen > 0 && props[plen - 1] != '}') {
- snprintf(props + plen - 1, sizeof(props) - plen + 1, "\"}");
- }
- }
- cbm_gbuf_insert_edge(ctx->gbuf, source_node->id, route_id, edge_type, props);
- } else {
- /* No URL/topic extracted — fall through to normal CALLS edge */
- char props[512];
- snprintf(props, sizeof(props),
- "{\"callee\":\"%s\",\"confidence\":%.2f,\"strategy\":\"%s\","
- "\"candidates\":%d}",
- call->callee_name, res.confidence,
- res.strategy ? res.strategy : "unknown", res.candidate_count);
- cbm_gbuf_insert_edge(ctx->gbuf, source_node->id, target_node->id, "CALLS",
- props);
- }
- } else if (svc == CBM_SVC_CONFIG) {
- char props[512];
- snprintf(props, sizeof(props),
- "{\"callee\":\"%s\",\"key\":\"%s\",\"confidence\":%.2f}",
- call->callee_name, call->first_string_arg ? call->first_string_arg : "",
- res.confidence);
- cbm_gbuf_insert_edge(ctx->gbuf, source_node->id, target_node->id, "CONFIGURES",
- props);
} else {
- /* Normal CALLS edge */
- char props[512];
- snprintf(props, sizeof(props),
- "{\"callee\":\"%s\",\"confidence\":%.2f,\"strategy\":\"%s\","
- "\"candidates\":%d}",
- call->callee_name, res.confidence, res.strategy ? res.strategy : "unknown",
- res.candidate_count);
- cbm_gbuf_insert_edge(ctx->gbuf, source_node->id, target_node->id, "CALLS", props);
+ unresolved++;
}
- resolved++;
}
free(module_qn);
@@ -411,7 +409,85 @@ int cbm_pipeline_pass_calls(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *file
* creates CALLS edges from the endpoint to the dependency function.
* Without this, FastAPI auth/DI functions appear as dead code (in_degree=0). */
-// NOLINTNEXTLINE(readability-function-cognitive-complexity)
+/* Extract Python function signature text from source starting at given line. Caller frees. */
+static char *extract_py_signature(const char *source, int start_line, int end_line) {
+ int sig_end = start_line + 15;
+ if (end_line > 0 && sig_end > end_line) {
+ sig_end = end_line;
+ }
+ const char *p = source;
+ int line = 1;
+ while (*p && line < start_line) {
+ if (*p == '\n') {
+ line++;
+ }
+ p++;
+ }
+ const char *sig_start = p;
+ while (*p && line < sig_end) {
+ if (*p == '\n') {
+ line++;
+ }
+ p++;
+ if (p > sig_start + 1 && p[-1] == ':' && p[-2] == ')') {
+ break;
+ }
+ }
+ size_t sig_len = (size_t)(p - sig_start);
+ char *sig = malloc(sig_len + 1);
+ if (!sig) {
+ return NULL;
+ }
+ memcpy(sig, sig_start, sig_len);
+ sig[sig_len] = '\0';
+ return sig;
+}
+
+/* Scan one function's signature for Depends(func_ref) and create CALLS edges. */
+static int scan_depends_in_sig(cbm_pipeline_ctx_t *ctx, const cbm_regex_t *re, const char *sig,
+ const CBMDefinition *def, const char *module_qn, const char **ik,
+ const char **iv, int ic) {
+ int count = 0;
+ cbm_regmatch_t match[2];
+ const char *scan = sig;
+ while (cbm_regexec(re, scan, 2, match, 0) == 0) {
+ int ref_len = match[1].rm_eo - match[1].rm_so;
+ char func_ref[256];
+ if (ref_len >= (int)sizeof(func_ref)) {
+ ref_len = (int)sizeof(func_ref) - 1;
+ }
+ memcpy(func_ref, scan + match[1].rm_so, (size_t)ref_len);
+ func_ref[ref_len] = '\0';
+ cbm_resolution_t res = cbm_registry_resolve(ctx->registry, func_ref, module_qn, ik, iv, ic);
+ if (res.qualified_name && res.qualified_name[0] != '\0') {
+ const cbm_gbuf_node_t *sn = cbm_gbuf_find_by_qn(ctx->gbuf, def->qualified_name);
+ const cbm_gbuf_node_t *tn = cbm_gbuf_find_by_qn(ctx->gbuf, res.qualified_name);
+ if (sn && tn && sn->id != tn->id) {
+ cbm_gbuf_insert_edge(ctx->gbuf, sn->id, tn->id, "CALLS",
+ "{\"confidence\":0.95,\"strategy\":\"fastapi_depends\"}");
+ count++;
+ }
+ }
+ scan += match[0].rm_eo;
+ }
+ return count;
+}
+
+static bool is_callable_def(const CBMDefinition *def) {
+ return def->qualified_name && def->start_line > 0 && def->label &&
+ (strcmp(def->label, "Function") == 0 || strcmp(def->label, "Method") == 0);
+}
+
+static bool file_has_depends_call(const CBMFileResult *result) {
+ for (int c = 0; c < result->calls.count; c++) {
+ if (result->calls.items[c].callee_name &&
+ strcmp(result->calls.items[c].callee_name, "Depends") == 0) {
+ return true;
+ }
+ }
+ return false;
+}
+
void cbm_pipeline_pass_fastapi_depends(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
int file_count) {
cbm_regex_t depends_re;
@@ -428,20 +504,8 @@ void cbm_pipeline_pass_fastapi_depends(cbm_pipeline_ctx_t *ctx, const cbm_file_i
break;
}
- /* Check if file has Depends call in cached extraction */
CBMFileResult *result = ctx->result_cache ? ctx->result_cache[i] : NULL;
- if (!result) {
- continue;
- }
- bool has_depends = false;
- for (int c = 0; c < result->calls.count; c++) {
- if (result->calls.items[c].callee_name &&
- strcmp(result->calls.items[c].callee_name, "Depends") == 0) {
- has_depends = true;
- break;
- }
- }
- if (!has_depends) {
+ if (!result || !file_has_depends_call(result)) {
continue;
}
@@ -462,76 +526,17 @@ void cbm_pipeline_pass_fastapi_depends(cbm_pipeline_ctx_t *ctx, const cbm_file_i
for (int d = 0; d < result->defs.count; d++) {
CBMDefinition *def = &result->defs.items[d];
- if (!def->qualified_name || def->start_line == 0) {
- continue;
- }
- if (strcmp(def->label, "Function") != 0 && strcmp(def->label, "Method") != 0) {
+ if (!is_callable_def(def)) {
continue;
}
- /* Extract function signature (def line through ~15 lines for multi-line sigs) */
- int sig_end_line = (int)def->start_line + 15;
- if (def->end_line > 0 && sig_end_line > (int)def->end_line) {
- sig_end_line = (int)def->end_line;
- }
-
- /* Find signature region in source */
- const char *p = source;
- int line = 1;
- while (*p && line < def->start_line) {
- if (*p == '\n') {
- line++;
- }
- p++;
- }
- const char *sig_start = p;
- while (*p && line < sig_end_line) {
- if (*p == '\n') {
- line++;
- }
- p++;
- /* Stop at closing paren + colon (end of Python signature) */
- if (p > sig_start + 1 && p[-1] == ':' && p[-2] == ')') {
- break;
- }
- }
- size_t sig_len = (size_t)(p - sig_start);
- char *sig = malloc(sig_len + 1);
+ char *sig = extract_py_signature(source, (int)def->start_line, (int)def->end_line);
if (!sig) {
continue;
}
- memcpy(sig, sig_start, sig_len);
- sig[sig_len] = '\0';
-
- /* Match Depends(func_ref) patterns */
- cbm_regmatch_t match[2];
- const char *scan = sig;
- while (cbm_regexec(&depends_re, scan, 2, match, 0) == 0) {
- int ref_len = match[1].rm_eo - match[1].rm_so;
- char func_ref[256];
- if (ref_len >= (int)sizeof(func_ref)) {
- ref_len = (int)sizeof(func_ref) - 1;
- }
- memcpy(func_ref, scan + match[1].rm_so, (size_t)ref_len);
- func_ref[ref_len] = '\0';
-
- /* Resolve through registry */
- cbm_resolution_t res = cbm_registry_resolve(ctx->registry, func_ref, module_qn,
- imp_keys, imp_vals, imp_count);
- if (res.qualified_name && res.qualified_name[0] != '\0') {
- const cbm_gbuf_node_t *src_node =
- cbm_gbuf_find_by_qn(ctx->gbuf, def->qualified_name);
- const cbm_gbuf_node_t *tgt_node =
- cbm_gbuf_find_by_qn(ctx->gbuf, res.qualified_name);
- if (src_node && tgt_node && src_node->id != tgt_node->id) {
- cbm_gbuf_insert_edge(ctx->gbuf, src_node->id, tgt_node->id, "CALLS",
- "{\"confidence\":0.95,\"strategy\":\"fastapi_depends\""
- "}");
- edge_count++;
- }
- }
- scan += match[0].rm_eo;
- }
+
+ edge_count += scan_depends_in_sig(ctx, &depends_re, sig, def, module_qn, imp_keys,
+ imp_vals, imp_count);
free(sig);
}
diff --git a/src/pipeline/pass_definitions.c b/src/pipeline/pass_definitions.c
index ae3de2f37..a5fafa2d9 100644
--- a/src/pipeline/pass_definitions.c
+++ b/src/pipeline/pass_definitions.c
@@ -65,60 +65,53 @@ static const char *itoa_log(int val) {
/* Append a JSON-escaped string value to buf at position *pos.
* Writes: ,"key":"escaped_value"
* Handles: \, ", \n, \r, \t */
-// NOLINTNEXTLINE(readability-function-cognitive-complexity)
+static int def_json_escape_char(char *buf, size_t avail, char ch) {
+ char esc = 0;
+ switch (ch) {
+ case '"':
+ esc = '"';
+ break;
+ case '\\':
+ esc = '\\';
+ break;
+ case '\n':
+ esc = 'n';
+ break;
+ case '\r':
+ esc = 'r';
+ break;
+ case '\t':
+ esc = 't';
+ break;
+ default:
+ if (avail >= 1) {
+ buf[0] = ch;
+ }
+ return 1;
+ }
+ if (avail >= 2) {
+ buf[0] = '\\';
+ buf[1] = esc;
+ }
+ return 2;
+}
+
static void append_json_string(char *buf, size_t bufsize, size_t *pos, const char *key,
const char *val) {
- if (!val || !val[0]) {
+ if (!val || val[0] == '\0') {
return;
}
if (*pos >= bufsize - 10) {
return;
}
-
size_t p = *pos;
- /* ,\"key\":\" */
int w = snprintf(buf + p, bufsize - p, ",\"%s\":\"", key);
if (w <= 0 || (size_t)w >= bufsize - p) {
return;
}
p += (size_t)w;
-
for (const char *s = val; *s && p < bufsize - 3; s++) {
- switch (*s) {
- case '"':
- buf[p++] = '\\';
- if (p < bufsize - 2) {
- buf[p++] = '"';
- }
- break;
- case '\\':
- buf[p++] = '\\';
- if (p < bufsize - 2) {
- buf[p++] = '\\';
- }
- break;
- case '\n':
- buf[p++] = '\\';
- if (p < bufsize - 2) {
- buf[p++] = 'n';
- }
- break;
- case '\r':
- buf[p++] = '\\';
- if (p < bufsize - 2) {
- buf[p++] = 'r';
- }
- break;
- case '\t':
- buf[p++] = '\\';
- if (p < bufsize - 2) {
- buf[p++] = 't';
- }
- break;
- default:
- buf[p++] = *s;
- break;
- }
+ p += (size_t)def_json_escape_char(buf + p, bufsize - p - 2, *s);
}
if (p < bufsize - 1) {
buf[p++] = '"';
@@ -196,7 +189,61 @@ static void build_def_props(char *buf, size_t bufsize, const CBMDefinition *def)
}
}
-// NOLINTNEXTLINE(readability-function-cognitive-complexity)
+/* Process one definition: create node, register, DEFINES + DEFINES_METHOD edges. */
+static void process_def(cbm_pipeline_ctx_t *ctx, const CBMDefinition *def, const char *rel) {
+ if (!def->qualified_name || !def->name) {
+ return;
+ }
+ char props[2048];
+ build_def_props(props, sizeof(props), def);
+ int64_t node_id = cbm_gbuf_upsert_node(
+ ctx->gbuf, def->label ? def->label : "Function", def->name, def->qualified_name,
+ def->file_path ? def->file_path : rel, (int)def->start_line, (int)def->end_line, props);
+ if (node_id > 0 && def->label &&
+ (strcmp(def->label, "Function") == 0 || strcmp(def->label, "Method") == 0 ||
+ strcmp(def->label, "Class") == 0)) {
+ cbm_registry_add(ctx->registry, def->name, def->qualified_name, def->label);
+ }
+ char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel, "__file__");
+ const cbm_gbuf_node_t *file_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn);
+ if (file_node && node_id > 0) {
+ cbm_gbuf_insert_edge(ctx->gbuf, file_node->id, node_id, "DEFINES", "{}");
+ }
+ free(file_qn);
+ if (def->parent_class && def->label && strcmp(def->label, "Method") == 0) {
+ const cbm_gbuf_node_t *parent = cbm_gbuf_find_by_qn(ctx->gbuf, def->parent_class);
+ if (parent && node_id > 0) {
+ cbm_gbuf_insert_edge(ctx->gbuf, parent->id, node_id, "DEFINES_METHOD", "{}");
+ }
+ }
+}
+
+/* Create IMPORTS edges for one file's imports. */
+static int create_import_edges_for_file(cbm_pipeline_ctx_t *ctx, const CBMFileResult *result,
+ const char *rel) {
+ int count = 0;
+ for (int j = 0; j < result->imports.count; j++) {
+ const CBMImport *imp = &result->imports.items[j];
+ if (!imp->module_path) {
+ continue;
+ }
+ char *target_qn = cbm_pipeline_fqn_module(ctx->project_name, imp->module_path);
+ const cbm_gbuf_node_t *target = cbm_gbuf_find_by_qn(ctx->gbuf, target_qn);
+ char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel, "__file__");
+ const cbm_gbuf_node_t *source_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn);
+ if (source_node && target) {
+ char imp_props[256];
+ snprintf(imp_props, sizeof(imp_props), "{\"local_name\":\"%s\"}",
+ imp->local_name ? imp->local_name : "");
+ cbm_gbuf_insert_edge(ctx->gbuf, source_node->id, target->id, "IMPORTS", imp_props);
+ count++;
+ }
+ free(target_qn);
+ free(file_qn);
+ }
+ return count;
+}
+
int cbm_pipeline_pass_definitions(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
int file_count) {
cbm_log_info("pass.start", "pass", "definitions", "files", itoa_log(file_count));
@@ -240,75 +287,14 @@ int cbm_pipeline_pass_definitions(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
/* Create nodes for each definition */
for (int d = 0; d < result->defs.count; d++) {
- CBMDefinition *def = &result->defs.items[d];
- if (!def->qualified_name || !def->name) {
- continue;
- }
-
- char props[2048];
- build_def_props(props, sizeof(props), def);
-
- int64_t node_id =
- cbm_gbuf_upsert_node(ctx->gbuf, def->label ? def->label : "Function", def->name,
- def->qualified_name, def->file_path ? def->file_path : rel,
- (int)def->start_line, (int)def->end_line, props);
-
- /* Register callable symbols in the registry */
- if (node_id > 0 && def->label &&
- (strcmp(def->label, "Function") == 0 || strcmp(def->label, "Method") == 0 ||
- strcmp(def->label, "Class") == 0)) {
- cbm_registry_add(ctx->registry, def->name, def->qualified_name, def->label);
- }
-
- /* DEFINES edge: File node → Definition node */
- const char *file_qn_name = "__file__";
- char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel, file_qn_name);
- const cbm_gbuf_node_t *file_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn);
- if (file_node && node_id > 0) {
- cbm_gbuf_insert_edge(ctx->gbuf, file_node->id, node_id, "DEFINES", "{}");
- }
- free(file_qn);
-
- /* DEFINES_METHOD edge: Class → Method */
- if (def->parent_class && def->label && strcmp(def->label, "Method") == 0) {
- const cbm_gbuf_node_t *parent = cbm_gbuf_find_by_qn(ctx->gbuf, def->parent_class);
- if (parent && node_id > 0) {
- cbm_gbuf_insert_edge(ctx->gbuf, parent->id, node_id, "DEFINES_METHOD", "{}");
- }
- }
-
+ process_def(ctx, &result->defs.items[d], rel);
total_defs++;
}
/* Store calls for pass_calls (we save them in the extraction results
* for now — a future optimization would batch these) */
total_calls += result->calls.count;
- total_imports += result->imports.count;
-
- /* Store per-file import map for later use by pass_calls.
- * For each import, create an IMPORTS edge: File → imported module. */
- for (int j = 0; j < result->imports.count; j++) {
- CBMImport *imp = &result->imports.items[j];
- if (!imp->module_path) {
- continue;
- }
-
- /* Find or create the target module node */
- char *target_qn = cbm_pipeline_fqn_module(ctx->project_name, imp->module_path);
- const cbm_gbuf_node_t *target = cbm_gbuf_find_by_qn(ctx->gbuf, target_qn);
-
- char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel, "__file__");
- const cbm_gbuf_node_t *source_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn);
-
- if (source_node && target) {
- char imp_props[256];
- snprintf(imp_props, sizeof(imp_props), "{\"local_name\":\"%s\"}",
- imp->local_name ? imp->local_name : "");
- cbm_gbuf_insert_edge(ctx->gbuf, source_node->id, target->id, "IMPORTS", imp_props);
- }
- free(target_qn);
- free(file_qn);
- }
+ total_imports += create_import_edges_for_file(ctx, result, rel);
/* Cache or free the extraction result */
if (ctx->result_cache) {
diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.c
index 8fcf9706b..722549ce2 100644
--- a/src/pipeline/pass_parallel.c
+++ b/src/pipeline/pass_parallel.c
@@ -80,10 +80,41 @@ static const char *itoa_log(int val) {
}
/* Append a JSON-escaped string value to buf at position *pos. */
-// NOLINTNEXTLINE(readability-function-cognitive-complexity)
+/* Escape one character for JSON. Returns bytes written (1 or 2). */
+static int json_escape_char(char *buf, size_t avail, char ch) {
+ char esc = 0;
+ switch (ch) {
+ case '"':
+ esc = '"';
+ break;
+ case '\\':
+ esc = '\\';
+ break;
+ case '\n':
+ esc = 'n';
+ break;
+ case '\r':
+ esc = 'r';
+ break;
+ case '\t':
+ esc = 't';
+ break;
+ default:
+ if (avail >= 1) {
+ buf[0] = ch;
+ }
+ return 1;
+ }
+ if (avail >= 2) {
+ buf[0] = '\\';
+ buf[1] = esc;
+ }
+ return 2;
+}
+
static void append_json_string(char *buf, size_t bufsize, size_t *pos, const char *key,
const char *val) {
- if (!val || !val[0]) {
+ if (!val || val[0] == '\0') {
return;
}
if (*pos >= bufsize - 10) {
@@ -96,41 +127,8 @@ static void append_json_string(char *buf, size_t bufsize, size_t *pos, const cha
}
p += (size_t)w;
for (const char *s = val; *s && p < bufsize - 3; s++) {
- switch (*s) {
- case '"':
- buf[p++] = '\\';
- if (p < bufsize - 2) {
- buf[p++] = '"';
- }
- break;
- case '\\':
- buf[p++] = '\\';
- if (p < bufsize - 2) {
- buf[p++] = '\\';
- }
- break;
- case '\n':
- buf[p++] = '\\';
- if (p < bufsize - 2) {
- buf[p++] = 'n';
- }
- break;
- case '\r':
- buf[p++] = '\\';
- if (p < bufsize - 2) {
- buf[p++] = 'r';
- }
- break;
- case '\t':
- buf[p++] = '\\';
- if (p < bufsize - 2) {
- buf[p++] = 't';
- }
- break;
- default:
- buf[p++] = *s;
- break;
- }
+ int n = json_escape_char(buf + p, bufsize - p - 2, *s);
+ p += (size_t)n;
}
if (p < bufsize - 1) {
buf[p++] = '"';
@@ -362,7 +360,45 @@ typedef struct {
_Atomic int next_file_idx;
} extract_ctx_t;
-// NOLINTNEXTLINE(readability-function-cognitive-complexity)
+/* Insert one definition node (and its route if present) into the local gbuf. */
+static void insert_def_into_gbuf(extract_worker_state_t *ws, const cbm_file_info_t *fi,
+ CBMDefinition *def) {
+ char props[2048];
+ build_def_props(props, sizeof(props), def);
+ int64_t func_id =
+ cbm_gbuf_upsert_node(ws->local_gbuf, def->label ? def->label : "Function", def->name,
+ def->qualified_name, def->file_path ? def->file_path : fi->rel_path,
+ (int)def->start_line, (int)def->end_line, props);
+ ws->nodes_created++;
+ if (def->route_path && def->route_path[0] != '\0') {
+ const char *rm = def->route_method ? def->route_method : "ANY";
+ char route_qn[CBM_ROUTE_QN_SIZE];
+ snprintf(route_qn, sizeof(route_qn), "__route__%s__%s", rm, def->route_path);
+ char rprops[256];
+ snprintf(rprops, sizeof(rprops), "{\"method\":\"%s\",\"source\":\"decorator\"}", rm);
+ int64_t route_id =
+ cbm_gbuf_upsert_node(ws->local_gbuf, "Route", def->route_path, route_qn,
+ def->file_path ? def->file_path : fi->rel_path, 0, 0, rprops);
+ char hprops[512];
+ snprintf(hprops, sizeof(hprops), "{\"handler\":\"%s\"}", def->qualified_name);
+ cbm_gbuf_insert_edge(ws->local_gbuf, func_id, route_id, "HANDLES", hprops);
+ }
+}
+
+static void log_extract_fail(int pos, uint64_t ms, const char *path) {
+ if (pos < 24) {
+ cbm_log_warn("parallel.extract.file.fail", "pos", itoa_log(pos), "elapsed_ms",
+ itoa_log((int)ms), "path", path);
+ }
+}
+
+static void log_extract_done(int pos, uint64_t ms, int defs, const char *path) {
+ if (pos < 24 || ms > 1000) {
+ cbm_log_info("parallel.extract.file.done", "pos", itoa_log(pos), "elapsed_ms",
+ itoa_log((int)ms), "defs", itoa_log(defs), "path", path);
+ }
+}
+
static void extract_worker(int worker_id, void *ctx_ptr) {
extract_ctx_t *ec = ctx_ptr;
extract_worker_state_t *ws = &ec->workers[worker_id];
@@ -408,54 +444,18 @@ static void extract_worker(int worker_id, void *ctx_ptr) {
uint64_t file_elapsed_ms = (extract_now_ns() - file_t0) / 1000000ULL;
if (!result) {
- if (sort_pos < 24) {
- cbm_log_warn("parallel.extract.file.fail", "pos", itoa_log(sort_pos), "elapsed_ms",
- itoa_log((int)file_elapsed_ms), "path", fi->rel_path);
- }
+ log_extract_fail(sort_pos, file_elapsed_ms, fi->rel_path);
free_source(source);
ws->errors++;
continue;
}
-
- /* Per-file completion log for large/slow files */
- if (sort_pos < 24 || file_elapsed_ms > 1000) {
- cbm_log_info("parallel.extract.file.done", "pos", itoa_log(sort_pos), "elapsed_ms",
- itoa_log((int)file_elapsed_ms), "defs", itoa_log(result->defs.count),
- "path", fi->rel_path);
- }
+ log_extract_done(sort_pos, file_elapsed_ms, result->defs.count, fi->rel_path);
/* Create definition nodes in local gbuf */
for (int d = 0; d < result->defs.count; d++) {
CBMDefinition *def = &result->defs.items[d];
- if (!def->qualified_name || !def->name) {
- continue;
- }
-
- char props[2048];
- build_def_props(props, sizeof(props), def);
-
- int64_t func_id = cbm_gbuf_upsert_node(
- ws->local_gbuf, def->label ? def->label : "Function", def->name,
- def->qualified_name, def->file_path ? def->file_path : fi->rel_path,
- (int)def->start_line, (int)def->end_line, props);
- ws->nodes_created++;
-
- /* AST-extracted route: create Route node + HANDLES edge directly.
- * Pure AST approach: route_path/route_method extracted from decorator
- * tree-sitter nodes during extraction, no regex needed. */
- if (def->route_path && def->route_path[0]) {
- const char *rm = def->route_method ? def->route_method : "ANY";
- char route_qn[CBM_ROUTE_QN_SIZE];
- snprintf(route_qn, sizeof(route_qn), "__route__%s__%s", rm, def->route_path);
- char rprops[256];
- snprintf(rprops, sizeof(rprops), "{\"method\":\"%s\",\"source\":\"decorator\"}",
- rm);
- int64_t route_id = cbm_gbuf_upsert_node(
- ws->local_gbuf, "Route", def->route_path, route_qn,
- def->file_path ? def->file_path : fi->rel_path, 0, 0, rprops);
- char hprops[512];
- snprintf(hprops, sizeof(hprops), "{\"handler\":\"%s\"}", def->qualified_name);
- cbm_gbuf_insert_edge(ws->local_gbuf, func_id, route_id, "HANDLES", hprops);
+ if (def->qualified_name && def->name) {
+ insert_def_into_gbuf(ws, fi, def);
}
}
@@ -596,7 +596,35 @@ int cbm_parallel_extract(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
/* ── Phase 3B: Serial Registry Build ─────────────────────────────── */
-// NOLINTNEXTLINE(readability-function-cognitive-complexity)
+/* Register one definition and create DEFINES + DEFINES_METHOD edges. Returns edge count. */
+static int register_and_link_def(cbm_pipeline_ctx_t *ctx, const CBMDefinition *def, const char *rel,
+ int *reg_entries) {
+ int edges = 0;
+ if (!def->name || !def->qualified_name || !def->label) {
+ return 0;
+ }
+ if (strcmp(def->label, "Function") == 0 || strcmp(def->label, "Method") == 0 ||
+ strcmp(def->label, "Class") == 0) {
+ cbm_registry_add(ctx->registry, def->name, def->qualified_name, def->label);
+ (*reg_entries)++;
+ }
+ char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel, "__file__");
+ const cbm_gbuf_node_t *file_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn);
+ const cbm_gbuf_node_t *def_node = cbm_gbuf_find_by_qn(ctx->gbuf, def->qualified_name);
+ if (file_node && def_node) {
+ cbm_gbuf_insert_edge(ctx->gbuf, file_node->id, def_node->id, "DEFINES", "{}");
+ edges++;
+ }
+ free(file_qn);
+ if (def->parent_class && strcmp(def->label, "Method") == 0) {
+ const cbm_gbuf_node_t *parent = cbm_gbuf_find_by_qn(ctx->gbuf, def->parent_class);
+ if (parent && def_node) {
+ cbm_gbuf_insert_edge(ctx->gbuf, parent->id, def_node->id, "DEFINES_METHOD", "{}");
+ }
+ }
+ return edges;
+}
+
int cbm_build_registry_from_cache(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
int file_count, CBMFileResult **result_cache) {
cbm_log_info("parallel.registry.start", "files", itoa_log(file_count));
@@ -617,37 +645,9 @@ int cbm_build_registry_from_cache(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
const char *rel = files[i].rel_path;
- /* Register callable symbols */
+ /* Register callable symbols + DEFINES/DEFINES_METHOD edges */
for (int d = 0; d < result->defs.count; d++) {
- CBMDefinition *def = &result->defs.items[d];
- if (!def->name || !def->qualified_name || !def->label) {
- continue;
- }
-
- if (strcmp(def->label, "Function") == 0 || strcmp(def->label, "Method") == 0 ||
- strcmp(def->label, "Class") == 0) {
- cbm_registry_add(ctx->registry, def->name, def->qualified_name, def->label);
- reg_entries++;
- }
-
- /* DEFINES edge: File → Definition */
- char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel, "__file__");
- const cbm_gbuf_node_t *file_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn);
- const cbm_gbuf_node_t *def_node = cbm_gbuf_find_by_qn(ctx->gbuf, def->qualified_name);
- if (file_node && def_node) {
- cbm_gbuf_insert_edge(ctx->gbuf, file_node->id, def_node->id, "DEFINES", "{}");
- defines_edges++;
- }
- free(file_qn);
-
- /* DEFINES_METHOD edge: Class → Method */
- if (def->parent_class && strcmp(def->label, "Method") == 0) {
- const cbm_gbuf_node_t *parent = cbm_gbuf_find_by_qn(ctx->gbuf, def->parent_class);
- if (parent && def_node) {
- cbm_gbuf_insert_edge(ctx->gbuf, parent->id, def_node->id, "DEFINES_METHOD",
- "{}");
- }
- }
+ defines_edges += register_and_link_def(ctx, &result->defs.items[d], rel, &reg_entries);
}
/* IMPORTS edges */
@@ -713,7 +713,40 @@ typedef struct {
/* Append arg data as JSON to edge properties: ,"args":[{"i":0,"e":"x","v":"val"},...]
* Returns new position in buffer. */
-// NOLINTNEXTLINE(readability-function-cognitive-complexity)
+/* Sanitize expression string for JSON (in-place). */
+static void sanitize_expr(char *expr_buf, const char *expr) {
+ if (expr) {
+ snprintf(expr_buf, 128, "%.*s", 120, expr);
+ for (char *p = expr_buf; *p; p++) {
+ if (*p == '"') {
+ *p = '\'';
+ }
+ if (*p == '\n' || *p == '\r') {
+ *p = ' ';
+ }
+ }
+ } else {
+ expr_buf[0] = '\0';
+ }
+}
+
+/* Format one call arg as JSON. Returns snprintf result. */
+static int format_call_arg(char *buf, size_t bufsize, const CBMCallArg *a, const char *expr) {
+ if (a->keyword && a->value) {
+ return snprintf(buf, bufsize, "{\"i\":%d,\"k\":\"%s\",\"e\":\"%s\",\"v\":\"%s\"}", a->index,
+ a->keyword, expr, a->value);
+ }
+ if (a->keyword) {
+ return snprintf(buf, bufsize, "{\"i\":%d,\"k\":\"%s\",\"e\":\"%s\"}", a->index, a->keyword,
+ expr);
+ }
+ if (a->value) {
+ return snprintf(buf, bufsize, "{\"i\":%d,\"e\":\"%s\",\"v\":\"%s\"}", a->index, expr,
+ a->value);
+ }
+ return snprintf(buf, bufsize, "{\"i\":%d,\"e\":\"%s\"}", a->index, expr);
+}
+
static size_t append_args_json(char *buf, size_t bufsize, size_t pos, const CBMCall *call) {
if (call->arg_count == 0 || pos >= bufsize - 20) {
return pos;
@@ -728,35 +761,9 @@ static size_t append_args_json(char *buf, size_t bufsize, size_t pos, const CBMC
if (i > 0 && pos < bufsize - 1) {
buf[pos++] = ',';
}
- /* Truncate long expressions to keep edge properties compact */
char expr_buf[128];
- if (a->expr) {
- snprintf(expr_buf, sizeof(expr_buf), "%.*s", 120, a->expr);
- /* Escape quotes for JSON safety */
- for (char *p = expr_buf; *p; p++) {
- if (*p == '"') {
- *p = '\'';
- }
- if (*p == '\n' || *p == '\r') {
- *p = ' ';
- }
- }
- } else {
- expr_buf[0] = '\0';
- }
- if (a->keyword && a->value) {
- n = snprintf(buf + pos, bufsize - pos,
- "{\"i\":%d,\"k\":\"%s\",\"e\":\"%s\",\"v\":\"%s\"}", a->index, a->keyword,
- expr_buf, a->value);
- } else if (a->keyword) {
- n = snprintf(buf + pos, bufsize - pos, "{\"i\":%d,\"k\":\"%s\",\"e\":\"%s\"}", a->index,
- a->keyword, expr_buf);
- } else if (a->value) {
- n = snprintf(buf + pos, bufsize - pos, "{\"i\":%d,\"e\":\"%s\",\"v\":\"%s\"}", a->index,
- expr_buf, a->value);
- } else {
- n = snprintf(buf + pos, bufsize - pos, "{\"i\":%d,\"e\":\"%s\"}", a->index, expr_buf);
- }
+ sanitize_expr(expr_buf, a->expr);
+ n = format_call_arg(buf + pos, bufsize - pos, a, expr_buf);
if (n > 0) {
pos += (size_t)n;
}
@@ -768,9 +775,153 @@ static size_t append_args_json(char *buf, size_t bufsize, size_t pos, const CBMC
return pos;
}
-/* Classify a resolved call by library identity and emit the appropriate edge.
- * Extracted from resolve_worker to keep cognitive complexity under threshold. */
-// NOLINTNEXTLINE(readability-function-cognitive-complexity)
+/* Scan call args for a URL-like route path and handler reference. */
+static bool is_path_keyword(const char *keyword) {
+ static const char *path_keywords[] = {"prefix", "path", "route", "pattern",
+ "url", "endpoint", "rule", "mount_path",
+ "route_path", "url_path", NULL};
+ for (const char **kw = path_keywords; *kw; kw++) {
+ if (strcmp(keyword, *kw) == 0) {
+ return true;
+ }
+ }
+ return false;
+}
+
+static const char *find_route_path_in_args(const CBMCall *call, const char **out_handler) {
+ *out_handler = NULL;
+ /* 1. First string arg starting with / */
+ if (call->first_string_arg && call->first_string_arg[0] == '/') {
+ *out_handler = call->second_arg_name;
+ return call->first_string_arg;
+ }
+ /* 2. Keyword args (prefix=, path=, route=, etc.) */
+ const char *found = NULL;
+ for (int ai = 0; ai < call->arg_count && !found; ai++) {
+ const CBMCallArg *ca = &call->args[ai];
+ const char *val = ca->value ? ca->value : ca->expr;
+ if (!val || val[0] != '/') {
+ continue;
+ }
+ if (ca->keyword && is_path_keyword(ca->keyword)) {
+ found = val;
+ } else if (!ca->keyword && ca->index == 0) {
+ found = val;
+ }
+ }
+ if (!found) {
+ return NULL;
+ }
+ /* 3. Handler: first identifier arg that's not a path/keyword */
+ for (int ai = 0; ai < call->arg_count; ai++) {
+ const CBMCallArg *ca = &call->args[ai];
+ if (!ca->expr || ca->expr[0] == '/' || ca->expr[0] == '"' || ca->expr[0] == '\'') {
+ continue;
+ }
+ if (ca->keyword && (strcmp(ca->keyword, "prefix") == 0 ||
+ strcmp(ca->keyword, "name") == 0 || strcmp(ca->keyword, "tags") == 0)) {
+ continue;
+ }
+ *out_handler = ca->expr;
+ break;
+ }
+ return found;
+}
+
+/* Build props JSON, append args, close brace, emit edge. */
+static void finalize_and_emit(cbm_gbuf_t *gbuf, int64_t src_id, int64_t tgt_id,
+ const char *edge_type, char *props, int n, const CBMCall *call) {
+ if (n > 0 && (size_t)n < sizeof(props) - 2) {
+ size_t pos = append_args_json(props, 2048, (size_t)n, call);
+ if (pos < sizeof(props) - 1) {
+ props[pos] = '}';
+ props[pos + 1] = '\0';
+ }
+ }
+ cbm_gbuf_insert_edge(gbuf, src_id, tgt_id, edge_type, props);
+}
+
+/* Emit HTTP_CALLS or ASYNC_CALLS edge via Route node. */
+static void emit_http_async_service_edge(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *source,
+ const CBMCall *call, const cbm_resolution_t *res,
+ cbm_svc_kind_t svc, const char *arg) {
+ const char *edge_type = (svc == CBM_SVC_HTTP) ? "HTTP_CALLS" : "ASYNC_CALLS";
+ const char *method =
+ (svc == CBM_SVC_HTTP) ? cbm_service_pattern_http_method(call->callee_name) : NULL;
+ const char *broker =
+ (svc == CBM_SVC_ASYNC) ? cbm_service_pattern_broker(res->qualified_name) : NULL;
+
+ char route_qn[CBM_ROUTE_QN_SIZE];
+ const char *prefix =
+ (svc == CBM_SVC_HTTP) ? (method ? method : "ANY") : (broker ? broker : "async");
+ snprintf(route_qn, sizeof(route_qn), "__route__%s__%s", prefix, arg);
+ char route_props[256];
+ snprintf(route_props, sizeof(route_props), "%s%s%s",
+ method ? "{\"method\":\"" : (broker ? "{\"broker\":\"" : "{}"),
+ method ? method : (broker ? broker : ""), (method || broker) ? "\"}" : "");
+ int64_t route_id = cbm_gbuf_upsert_node(gbuf, "Route", arg, route_qn, "", 0, 0, route_props);
+
+ char props[2048];
+ int n = snprintf(props, sizeof(props), "{\"callee\":\"%s\",\"url_path\":\"%s\"%s%s%s%s%s%s",
+ call->callee_name, arg, method ? ",\"method\":\"" : "", method ? method : "",
+ method ? "\"" : "", broker ? ",\"broker\":\"" : "", broker ? broker : "",
+ broker ? "\"" : "");
+ finalize_and_emit(gbuf, source->id, route_id, edge_type, props, n, call);
+}
+
+/* Emit CONFIGURES edge. */
+static void emit_config_edge(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *source,
+ const cbm_gbuf_node_t *target, const CBMCall *call,
+ const cbm_resolution_t *res, const char *arg) {
+ char props[2048];
+ int n = snprintf(props, sizeof(props), "{\"callee\":\"%s\",\"key\":\"%s\",\"confidence\":%.2f",
+ call->callee_name, arg ? arg : "", res->confidence);
+ finalize_and_emit(gbuf, source->id, target->id, "CONFIGURES", props, n, call);
+}
+
+/* Emit normal CALLS edge. */
+static void emit_normal_calls_edge(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *source,
+ const cbm_gbuf_node_t *target, const CBMCall *call,
+ const cbm_resolution_t *res) {
+ char props[2048];
+ int n = snprintf(props, sizeof(props),
+ "{\"callee\":\"%s\",\"confidence\":%.2f,\"strategy\":\"%s\",\"candidates\":%d",
+ call->callee_name, res->confidence, res->strategy ? res->strategy : "unknown",
+ res->candidate_count);
+ finalize_and_emit(gbuf, source->id, target->id, "CALLS", props, n, call);
+}
+
+/* Classify a resolved call by library identity and emit the appropriate edge. */
+/* Create Route node + CALLS + HANDLES edges for a route registration call. */
+static void emit_route_registration(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *source,
+ const CBMCall *call, const char *route_path,
+ const char *handler_ref, const char *module_qn,
+ const cbm_registry_t *registry, const cbm_gbuf_t *main_gbuf,
+ const char **ik, const char **iv, int ic) {
+ const char *method = cbm_service_pattern_route_method(call->callee_name);
+ char rqn[CBM_ROUTE_QN_SIZE];
+ snprintf(rqn, sizeof(rqn), "__route__%s__%s", method ? method : "ANY", route_path);
+ char rp[256];
+ snprintf(rp, sizeof(rp), "{\"method\":\"%s\"}", method ? method : "ANY");
+ int64_t rid = cbm_gbuf_upsert_node(gbuf, "Route", route_path, rqn, "", 0, 0, rp);
+ char props[512];
+ snprintf(props, sizeof(props),
+ "{\"callee\":\"%s\",\"url_path\":\"%s\",\"via\":\"route_registration\"}",
+ call->callee_name, route_path);
+ cbm_gbuf_insert_edge(gbuf, source->id, rid, "CALLS", props);
+ if (handler_ref && handler_ref[0] != '\0') {
+ cbm_resolution_t hres = cbm_registry_resolve(registry, handler_ref, module_qn, ik, iv, ic);
+ if (hres.qualified_name && hres.qualified_name[0] != '\0') {
+ const cbm_gbuf_node_t *h = cbm_gbuf_find_by_qn(main_gbuf, hres.qualified_name);
+ if (h) {
+ char hp[256];
+ snprintf(hp, sizeof(hp), "{\"handler\":\"%s\"}", hres.qualified_name);
+ cbm_gbuf_insert_edge(gbuf, h->id, rid, "HANDLES", hp);
+ }
+ }
+ }
+}
+
static void emit_service_edge(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *source,
const cbm_gbuf_node_t *target, const CBMCall *call,
const cbm_resolution_t *res, const char *module_qn,
@@ -786,170 +937,250 @@ static void emit_service_edge(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *source,
}
if (svc == CBM_SVC_ROUTE_REG) {
- /* Generalized route registration: find URL path from ANY argument.
- * Handles: router.GET("/path", handler), app.include_router(r, prefix="/path"),
- * app.add_url_rule("/path", view_func=h), app.mount("/path", sub_app), etc.
- * Language-agnostic: any call to a router framework with a path arg. */
- const char *route_path = NULL;
const char *handler_ref = NULL;
+ const char *route_path = find_route_path_in_args(call, &handler_ref);
+ if (route_path) {
+ emit_route_registration(gbuf, source, call, route_path, handler_ref, module_qn,
+ registry, main_gbuf, imp_keys, imp_vals, imp_count);
+ return;
+ }
+ /* No path found — fall through to normal CALLS edge */
+ }
+
+ bool has_url = (arg && arg[0] != '\0' && (arg[0] == '/' || strstr(arg, "://") != NULL));
+ bool has_topic = (arg && arg[0] != '\0' && svc == CBM_SVC_ASYNC && strlen(arg) > 2);
+
+ if ((svc == CBM_SVC_HTTP || svc == CBM_SVC_ASYNC) && (has_url || has_topic)) {
+ emit_http_async_service_edge(gbuf, source, call, res, svc, arg);
+ } else if (svc == CBM_SVC_CONFIG) {
+ emit_config_edge(gbuf, source, target, call, res, arg);
+ } else {
+ emit_normal_calls_edge(gbuf, source, target, call, res);
+ }
+}
- /* 1. Check first_string_arg (covers router.GET("/path", handler)) */
- if (arg != NULL && arg[0] == '/') {
- route_path = arg;
- handler_ref = call->second_arg_name;
+/* Find the source node for an edge: enclosing function or file node. */
+static const cbm_gbuf_node_t *find_source_node(const cbm_gbuf_t *gbuf, const char *project,
+ const char *rel, const char *enclosing_qn) {
+ const cbm_gbuf_node_t *src = NULL;
+ if (enclosing_qn) {
+ src = cbm_gbuf_find_by_qn(gbuf, enclosing_qn);
+ }
+ if (!src) {
+ char *file_qn = cbm_pipeline_fqn_compute(project, rel, "__file__");
+ src = cbm_gbuf_find_by_qn(gbuf, file_qn);
+ free(file_qn);
+ }
+ return src;
+}
+
+/* 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,
+ const char **imp_vals, int imp_count) {
+ for (int c = 0; c < result->calls.count; c++) {
+ CBMCall *call = &result->calls.items[c];
+ if (!call->callee_name) {
+ continue;
+ }
+ const cbm_gbuf_node_t *source_node =
+ find_source_node(rc->main_gbuf, rc->project_name, rel, call->enclosing_func_qn);
+ if (!source_node) {
+ continue;
}
- /* 2. Scan keyword args for path-like values (prefix=, path=, route=, pattern=) */
- if (!route_path) {
- static const char *path_keywords[] = {"prefix", "path", "route", "pattern",
- "url", "endpoint", "rule", "mount_path",
- "route_path", "url_path", NULL};
- for (int ai = 0; ai < call->arg_count && !route_path; ai++) {
- const CBMCallArg *ca = &call->args[ai];
- const char *val = ca->value ? ca->value : ca->expr;
- if (!val || val[0] != '/') {
- continue;
- }
- if (ca->keyword) {
- for (const char **kw = path_keywords; *kw; kw++) {
- if (strcmp(ca->keyword, *kw) == 0) {
- route_path = val;
- break;
- }
- }
- } else if (ca->index == 0) {
- /* First positional arg starting with / is likely a path */
- route_path = val;
- }
+ cbm_resolution_t res = cbm_registry_resolve(rc->registry, call->callee_name, module_qn,
+ imp_keys, imp_vals, imp_count);
+ 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,
+ .confidence = 0.5,
+ .strategy = "callee_suffix"};
+ emit_service_edge(ws->local_edge_buf, source_node, source_node, call, &fake_res,
+ module_qn, rc->registry, rc->main_gbuf, imp_keys, imp_vals,
+ imp_count);
}
+ continue;
}
+ const cbm_gbuf_node_t *target_node = cbm_gbuf_find_by_qn(rc->main_gbuf, res.qualified_name);
+ if (!target_node || source_node->id == target_node->id) {
+ continue;
+ }
+ emit_service_edge(ws->local_edge_buf, source_node, target_node, call, &res, module_qn,
+ rc->registry, rc->main_gbuf, imp_keys, imp_vals, imp_count);
+ ws->calls_resolved++;
+ }
+}
- /* 3. Find handler reference from args (first identifier/attribute arg) */
- if (!handler_ref) {
- for (int ai = 0; ai < call->arg_count && !handler_ref; ai++) {
- const CBMCallArg *ca = &call->args[ai];
- if (!ca->expr || ca->expr[0] == '/' || ca->expr[0] == '"' || ca->expr[0] == '\'') {
- continue;
- }
- /* Skip known non-handler keywords */
- if (ca->keyword &&
- (strcmp(ca->keyword, "prefix") == 0 || strcmp(ca->keyword, "name") == 0 ||
- strcmp(ca->keyword, "tags") == 0)) {
- continue;
- }
- handler_ref = ca->expr;
- }
+/* Resolve usages for one file. */
+static void resolve_file_usages(resolve_ctx_t *rc, resolve_worker_state_t *ws,
+ CBMFileResult *result, const char *rel, const char *module_qn,
+ const char **imp_keys, const char **imp_vals, int imp_count) {
+ for (int u = 0; u < result->usages.count; u++) {
+ CBMUsage *usage = &result->usages.items[u];
+ if (!usage->ref_name) {
+ continue;
+ }
+ const cbm_gbuf_node_t *src =
+ find_source_node(rc->main_gbuf, rc->project_name, rel, usage->enclosing_func_qn);
+ if (!src) {
+ continue;
}
+ cbm_resolution_t res = cbm_registry_resolve(rc->registry, usage->ref_name, module_qn,
+ imp_keys, imp_vals, imp_count);
+ if (!res.qualified_name || res.qualified_name[0] == '\0') {
+ continue;
+ }
+ const cbm_gbuf_node_t *tgt = cbm_gbuf_find_by_qn(rc->main_gbuf, res.qualified_name);
+ if (!tgt || src->id == tgt->id) {
+ continue;
+ }
+ char uprops[256];
+ snprintf(uprops, sizeof(uprops), "{\"callee\":\"%s\"}", usage->ref_name);
+ cbm_gbuf_insert_edge(ws->local_edge_buf, src->id, tgt->id, "USAGE", uprops);
+ ws->usages_resolved++;
+ }
+}
- if (route_path) {
- const char *method = cbm_service_pattern_route_method(call->callee_name);
- char route_qn[CBM_ROUTE_QN_SIZE];
- snprintf(route_qn, sizeof(route_qn), "__route__%s__%s", method ? method : "ANY",
- route_path);
- char route_props[256];
- snprintf(route_props, sizeof(route_props), "{\"method\":\"%s\"}",
- method ? method : "ANY");
- int64_t route_id =
- cbm_gbuf_upsert_node(gbuf, "Route", route_path, route_qn, "", 0, 0, route_props);
- char props[512];
- snprintf(props, sizeof(props),
- "{\"callee\":\"%s\",\"url_path\":\"%s\",\"via\":\"route_registration\"}",
- call->callee_name, route_path);
- cbm_gbuf_insert_edge(gbuf, source->id, route_id, "CALLS", props);
-
- /* Resolve handler and create HANDLES edge */
- if (handler_ref && handler_ref[0]) {
- cbm_resolution_t hres = cbm_registry_resolve(registry, handler_ref, module_qn,
- imp_keys, imp_vals, imp_count);
- if (hres.qualified_name != NULL && hres.qualified_name[0] != '\0') {
- const cbm_gbuf_node_t *handler =
- cbm_gbuf_find_by_qn(main_gbuf, hres.qualified_name);
- if (handler != NULL) {
- char hprops[256];
- snprintf(hprops, sizeof(hprops), "{\"handler\":\"%s\"}",
- hres.qualified_name);
- cbm_gbuf_insert_edge(gbuf, handler->id, route_id, "HANDLES", hprops);
- }
- }
- }
- return;
+/* Resolve throws/raises for one file. */
+static void resolve_file_throws(resolve_ctx_t *rc, resolve_worker_state_t *ws,
+ CBMFileResult *result, const char *module_qn, const char **imp_keys,
+ const char **imp_vals, int imp_count) {
+ for (int t = 0; t < result->throws.count; t++) {
+ CBMThrow *thr = &result->throws.items[t];
+ if (!thr->exception_name || !thr->enclosing_func_qn) {
+ continue;
}
- /* No path found — fall through to normal CALLS edge */
+ const cbm_gbuf_node_t *src = cbm_gbuf_find_by_qn(rc->main_gbuf, thr->enclosing_func_qn);
+ if (!src) {
+ continue;
+ }
+ const char *edge_type = is_checked_exception(thr->exception_name) ? "THROWS" : "RAISES";
+ cbm_resolution_t res = cbm_registry_resolve(rc->registry, thr->exception_name, module_qn,
+ imp_keys, imp_vals, imp_count);
+ if (!res.qualified_name || res.qualified_name[0] == '\0') {
+ continue;
+ }
+ const cbm_gbuf_node_t *tgt = cbm_gbuf_find_by_qn(rc->main_gbuf, res.qualified_name);
+ if (!tgt || src->id == tgt->id) {
+ continue;
+ }
+ cbm_gbuf_insert_edge(ws->local_edge_buf, src->id, tgt->id, edge_type, "{}");
}
+}
- int has_url = (arg != NULL && arg[0] != '\0' && (arg[0] == '/' || strstr(arg, "://") != NULL));
- int has_topic = (arg != NULL && arg[0] != '\0' && svc == CBM_SVC_ASYNC && strlen(arg) > 2);
+/* Resolve reads/writes for one file. */
+static void resolve_file_rw(resolve_ctx_t *rc, resolve_worker_state_t *ws, CBMFileResult *result,
+ const char *rel, const char *module_qn, const char **imp_keys,
+ const char **imp_vals, int imp_count) {
+ for (int r = 0; r < result->rw.count; r++) {
+ CBMReadWrite *rw = &result->rw.items[r];
+ if (!rw->var_name) {
+ continue;
+ }
+ const cbm_gbuf_node_t *src =
+ find_source_node(rc->main_gbuf, rc->project_name, rel, rw->enclosing_func_qn);
+ if (!src) {
+ continue;
+ }
+ cbm_resolution_t res = cbm_registry_resolve(rc->registry, rw->var_name, module_qn, imp_keys,
+ imp_vals, imp_count);
+ if (!res.qualified_name || res.qualified_name[0] == '\0') {
+ continue;
+ }
+ const cbm_gbuf_node_t *tgt = cbm_gbuf_find_by_qn(rc->main_gbuf, res.qualified_name);
+ if (!tgt || src->id == tgt->id) {
+ continue;
+ }
+ const char *etype = rw->is_write ? "WRITES" : "READS";
+ cbm_gbuf_insert_edge(ws->local_edge_buf, src->id, tgt->id, etype, "{}");
+ }
+}
- if ((svc == CBM_SVC_HTTP || svc == CBM_SVC_ASYNC) && (has_url || has_topic)) {
- const char *edge_type = (svc == CBM_SVC_HTTP) ? "HTTP_CALLS" : "ASYNC_CALLS";
- const char *method =
- (svc == CBM_SVC_HTTP) ? cbm_service_pattern_http_method(call->callee_name) : NULL;
- const char *broker =
- (svc == CBM_SVC_ASYNC) ? cbm_service_pattern_broker(res->qualified_name) : NULL;
+/* Resolve base_classes → INHERITS edges for one definition. */
+static void resolve_def_inherits(resolve_ctx_t *rc, resolve_worker_state_t *ws,
+ const CBMDefinition *def, const cbm_gbuf_node_t *node,
+ const char *mq, const char **ik, const char **iv, int ic) {
+ if (!def->base_classes) {
+ return;
+ }
+ for (int b = 0; def->base_classes[b]; b++) {
+ const char *bqn = resolve_as_class(rc->registry, def->base_classes[b], mq, ik, iv, ic);
+ if (!bqn) {
+ continue;
+ }
+ const cbm_gbuf_node_t *bn = cbm_gbuf_find_by_qn(rc->main_gbuf, bqn);
+ if (bn && node->id != bn->id) {
+ cbm_gbuf_insert_edge(ws->local_edge_buf, node->id, bn->id, "INHERITS", "{}");
+ ws->semantic_resolved++;
+ }
+ }
+}
- char route_qn[CBM_ROUTE_QN_SIZE];
- if (svc == CBM_SVC_HTTP) {
- snprintf(route_qn, sizeof(route_qn), "__route__%s__%s", method ? method : "ANY", arg);
- } else {
- snprintf(route_qn, sizeof(route_qn), "__route__%s__%s", broker ? broker : "async", arg);
+/* Resolve decorators → DECORATES edges for one definition. */
+static void resolve_def_decorators(resolve_ctx_t *rc, resolve_worker_state_t *ws,
+ const CBMDefinition *def, const cbm_gbuf_node_t *node,
+ const char *mq, const char **ik, const char **iv, int ic) {
+ if (!def->decorators) {
+ return;
+ }
+ for (int dc = 0; def->decorators[dc]; dc++) {
+ char fn[256];
+ extract_decorator_func(def->decorators[dc], fn, sizeof(fn));
+ if (fn[0] == '\0') {
+ continue;
+ }
+ cbm_resolution_t res = cbm_registry_resolve(rc->registry, fn, mq, ik, iv, ic);
+ if (!res.qualified_name || res.qualified_name[0] == '\0') {
+ continue;
+ }
+ const cbm_gbuf_node_t *dn = cbm_gbuf_find_by_qn(rc->main_gbuf, res.qualified_name);
+ if (dn && node->id != dn->id) {
+ char dp[256];
+ snprintf(dp, sizeof(dp), "{\"decorator\":\"%s\"}", def->decorators[dc]);
+ cbm_gbuf_insert_edge(ws->local_edge_buf, node->id, dn->id, "DECORATES", dp);
+ ws->semantic_resolved++;
}
+ }
+}
- char route_props[256];
- if (method != NULL) {
- snprintf(route_props, sizeof(route_props), "{\"method\":\"%s\"}", method);
- } else if (broker != NULL) {
- snprintf(route_props, sizeof(route_props), "{\"broker\":\"%s\"}", broker);
- } else {
- snprintf(route_props, sizeof(route_props), "{}");
+/* Resolve INHERITS + DECORATES + IMPLEMENTS for one file. */
+static void resolve_file_semantic(resolve_ctx_t *rc, resolve_worker_state_t *ws,
+ CBMFileResult *result, const char *module_qn,
+ const char **imp_keys, const char **imp_vals, int imp_count) {
+ for (int d = 0; d < result->defs.count; d++) {
+ CBMDefinition *def = &result->defs.items[d];
+ if (!def->qualified_name) {
+ continue;
}
- int64_t route_id =
- cbm_gbuf_upsert_node(gbuf, "Route", arg, route_qn, "", 0, 0, route_props);
-
- char props[2048];
- int n = snprintf(props, sizeof(props), "{\"callee\":\"%s\",\"url_path\":\"%s\"%s%s%s%s%s%s",
- call->callee_name, arg, method ? ",\"method\":\"" : "",
- method ? method : "", method ? "\"" : "", broker ? ",\"broker\":\"" : "",
- broker ? broker : "", broker ? "\"" : "");
- if (n > 0 && (size_t)n < sizeof(props) - 2) {
- size_t pos = append_args_json(props, sizeof(props), (size_t)n, call);
- if (pos < sizeof(props) - 1) {
- props[pos] = '}';
- props[pos + 1] = '\0';
- }
+ const cbm_gbuf_node_t *node = cbm_gbuf_find_by_qn(rc->main_gbuf, def->qualified_name);
+ if (!node) {
+ continue;
}
- cbm_gbuf_insert_edge(gbuf, source->id, route_id, edge_type, props);
- } else if (svc == CBM_SVC_CONFIG) {
- char props[2048];
- int n =
- snprintf(props, sizeof(props), "{\"callee\":\"%s\",\"key\":\"%s\",\"confidence\":%.2f",
- call->callee_name, arg != NULL ? arg : "", res->confidence);
- if (n > 0 && (size_t)n < sizeof(props) - 2) {
- size_t pos = append_args_json(props, sizeof(props), (size_t)n, call);
- if (pos < sizeof(props) - 1) {
- props[pos] = '}';
- props[pos + 1] = '\0';
- }
+ resolve_def_inherits(rc, ws, def, node, module_qn, imp_keys, imp_vals, imp_count);
+ resolve_def_decorators(rc, ws, def, node, module_qn, imp_keys, imp_vals, imp_count);
+ }
+ for (int t = 0; t < result->impl_traits.count; t++) {
+ CBMImplTrait *it = &result->impl_traits.items[t];
+ if (!it->trait_name || !it->struct_name) {
+ continue;
}
- cbm_gbuf_insert_edge(gbuf, source->id, target->id, "CONFIGURES", props);
- } else {
- char props[2048];
- int n = snprintf(props, sizeof(props),
- "{\"callee\":\"%s\",\"confidence\":%.2f,\"strategy\":\"%s\","
- "\"candidates\":%d",
- call->callee_name, res->confidence,
- res->strategy ? res->strategy : "unknown", res->candidate_count);
- if (n > 0 && (size_t)n < sizeof(props) - 2) {
- size_t pos = append_args_json(props, sizeof(props), (size_t)n, call);
- if (pos < sizeof(props) - 1) {
- props[pos] = '}';
- props[pos + 1] = '\0';
- }
+ const char *tqn = resolve_as_class(rc->registry, it->trait_name, module_qn, imp_keys,
+ imp_vals, imp_count);
+ const char *sqn = resolve_as_class(rc->registry, it->struct_name, module_qn, imp_keys,
+ imp_vals, imp_count);
+ if (!tqn || !sqn) {
+ continue;
+ }
+ const cbm_gbuf_node_t *tn = cbm_gbuf_find_by_qn(rc->main_gbuf, tqn);
+ const cbm_gbuf_node_t *sn = cbm_gbuf_find_by_qn(rc->main_gbuf, sqn);
+ if (tn && sn && tn->id != sn->id) {
+ cbm_gbuf_insert_edge(ws->local_edge_buf, sn->id, tn->id, "IMPLEMENTS", "{}");
+ ws->semantic_resolved++;
}
- cbm_gbuf_insert_edge(gbuf, source->id, target->id, "CALLS", props);
}
}
-// NOLINTNEXTLINE(readability-function-cognitive-complexity,readability-function-size)
static void resolve_worker(int worker_id, void *ctx_ptr) {
resolve_ctx_t *rc = ctx_ptr;
resolve_worker_state_t *ws = &rc->workers[worker_id];
@@ -990,244 +1221,19 @@ static void resolve_worker(int worker_id, void *ctx_ptr) {
char *module_qn = cbm_pipeline_fqn_module(rc->project_name, rel);
/* ── CALLS resolution ──────────────────────────────────── */
- for (int c = 0; c < result->calls.count; c++) {
- CBMCall *call = &result->calls.items[c];
- if (!call->callee_name) {
- continue;
- }
-
- const cbm_gbuf_node_t *source_node = NULL;
- if (call->enclosing_func_qn) {
- source_node = cbm_gbuf_find_by_qn(rc->main_gbuf, call->enclosing_func_qn);
- }
- if (!source_node) {
- char *file_qn = cbm_pipeline_fqn_compute(rc->project_name, rel, "__file__");
- source_node = cbm_gbuf_find_by_qn(rc->main_gbuf, file_qn);
- free(file_qn);
- }
- if (!source_node) {
- continue;
- }
-
- cbm_resolution_t res = cbm_registry_resolve(rc->registry, call->callee_name, module_qn,
- imp_keys, imp_vals, imp_count);
- if (!res.qualified_name || res.qualified_name[0] == '\0') {
- /* Unresolved call — still check for route registration by callee suffix.
- * Handles patterns like app.include_router(r, prefix="/path") where
- * "app" is a local variable that the resolver can't follow. */
- if (cbm_service_pattern_route_method(call->callee_name) != NULL) {
- cbm_resolution_t fake_res = {.qualified_name = call->callee_name,
- .confidence = 0.5,
- .strategy = "callee_suffix"};
- emit_service_edge(ws->local_edge_buf, source_node, source_node, call, &fake_res,
- module_qn, rc->registry, rc->main_gbuf, imp_keys, imp_vals,
- imp_count);
- }
- continue;
- }
-
- const cbm_gbuf_node_t *target_node =
- cbm_gbuf_find_by_qn(rc->main_gbuf, res.qualified_name);
- if (!target_node || source_node->id == target_node->id) {
- continue;
- }
-
- /* Classify and emit edge via helper (keeps resolve_worker complexity down) */
- emit_service_edge(ws->local_edge_buf, source_node, target_node, call, &res, module_qn,
- rc->registry, rc->main_gbuf, imp_keys, imp_vals, imp_count);
- ws->calls_resolved++;
- }
+ resolve_file_calls(rc, ws, result, rel, module_qn, imp_keys, imp_vals, imp_count);
/* ── USAGE resolution ──────────────────────────────────── */
- for (int u = 0; u < result->usages.count; u++) {
- CBMUsage *usage = &result->usages.items[u];
- if (!usage->ref_name) {
- continue;
- }
-
- const cbm_gbuf_node_t *src = NULL;
- if (usage->enclosing_func_qn) {
- src = cbm_gbuf_find_by_qn(rc->main_gbuf, usage->enclosing_func_qn);
- }
- if (!src) {
- char *file_qn = cbm_pipeline_fqn_compute(rc->project_name, rel, "__file__");
- src = cbm_gbuf_find_by_qn(rc->main_gbuf, file_qn);
- free(file_qn);
- }
- if (!src) {
- continue;
- }
-
- cbm_resolution_t res = cbm_registry_resolve(rc->registry, usage->ref_name, module_qn,
- imp_keys, imp_vals, imp_count);
- if (!res.qualified_name || res.qualified_name[0] == '\0') {
- continue;
- }
-
- const cbm_gbuf_node_t *tgt = cbm_gbuf_find_by_qn(rc->main_gbuf, res.qualified_name);
- if (!tgt || src->id == tgt->id) {
- continue;
- }
-
- {
- char uprops[256];
- snprintf(uprops, sizeof(uprops), "{\"callee\":\"%s\"}", usage->ref_name);
- cbm_gbuf_insert_edge(ws->local_edge_buf, src->id, tgt->id, "USAGE", uprops);
- }
- ws->usages_resolved++;
- }
+ resolve_file_usages(rc, ws, result, rel, module_qn, imp_keys, imp_vals, imp_count);
/* ── THROWS / RAISES ───────────────────────────────────── */
- for (int t = 0; t < result->throws.count; t++) {
- CBMThrow *thr = &result->throws.items[t];
- if (!thr->exception_name || !thr->enclosing_func_qn) {
- continue;
- }
-
- const cbm_gbuf_node_t *src = cbm_gbuf_find_by_qn(rc->main_gbuf, thr->enclosing_func_qn);
- if (!src) {
- continue;
- }
-
- const char *edge_type = is_checked_exception(thr->exception_name) ? "THROWS" : "RAISES";
- cbm_resolution_t res = cbm_registry_resolve(rc->registry, thr->exception_name,
- module_qn, imp_keys, imp_vals, imp_count);
- if (!res.qualified_name || res.qualified_name[0] == '\0') {
- continue;
- }
-
- const cbm_gbuf_node_t *tgt = cbm_gbuf_find_by_qn(rc->main_gbuf, res.qualified_name);
- if (!tgt || src->id == tgt->id) {
- continue;
- }
-
- cbm_gbuf_insert_edge(ws->local_edge_buf, src->id, tgt->id, edge_type, "{}");
- }
+ resolve_file_throws(rc, ws, result, module_qn, imp_keys, imp_vals, imp_count);
/* ── READS / WRITES ────────────────────────────────────── */
- for (int r = 0; r < result->rw.count; r++) {
- CBMReadWrite *rw = &result->rw.items[r];
- if (!rw->var_name) {
- continue;
- }
+ resolve_file_rw(rc, ws, result, rel, module_qn, imp_keys, imp_vals, imp_count);
- const cbm_gbuf_node_t *src = NULL;
- if (rw->enclosing_func_qn) {
- src = cbm_gbuf_find_by_qn(rc->main_gbuf, rw->enclosing_func_qn);
- }
- if (!src) {
- char *file_qn = cbm_pipeline_fqn_compute(rc->project_name, rel, "__file__");
- src = cbm_gbuf_find_by_qn(rc->main_gbuf, file_qn);
- free(file_qn);
- }
- if (!src) {
- continue;
- }
-
- cbm_resolution_t res = cbm_registry_resolve(rc->registry, rw->var_name, module_qn,
- imp_keys, imp_vals, imp_count);
- if (!res.qualified_name || res.qualified_name[0] == '\0') {
- continue;
- }
-
- const cbm_gbuf_node_t *tgt = cbm_gbuf_find_by_qn(rc->main_gbuf, res.qualified_name);
- if (!tgt || src->id == tgt->id) {
- continue;
- }
-
- const char *etype = rw->is_write ? "WRITES" : "READS";
- cbm_gbuf_insert_edge(ws->local_edge_buf, src->id, tgt->id, etype, "{}");
- }
-
- /* ── INHERITS + DECORATES ──────────────────────────────── */
- for (int d = 0; d < result->defs.count; d++) {
- CBMDefinition *def = &result->defs.items[d];
- if (!def->qualified_name) {
- continue;
- }
-
- const cbm_gbuf_node_t *node = cbm_gbuf_find_by_qn(rc->main_gbuf, def->qualified_name);
- if (!node) {
- continue;
- }
-
- /* INHERITS */
- if (def->base_classes) {
- for (int b = 0; def->base_classes[b]; b++) {
- const char *base_qn =
- resolve_as_class(rc->registry, def->base_classes[b], module_qn, imp_keys,
- imp_vals, imp_count);
- if (!base_qn) {
- continue;
- }
- const cbm_gbuf_node_t *base_node = cbm_gbuf_find_by_qn(rc->main_gbuf, base_qn);
- if (!base_node || node->id == base_node->id) {
- continue;
- }
- cbm_gbuf_insert_edge(ws->local_edge_buf, node->id, base_node->id, "INHERITS",
- "{}");
- ws->semantic_resolved++;
- }
- }
-
- /* DECORATES */
- if (def->decorators) {
- for (int dc = 0; def->decorators[dc]; dc++) {
- char func_name[256];
- extract_decorator_func(def->decorators[dc], func_name, sizeof(func_name));
- if (func_name[0] == '\0') {
- continue;
- }
-
- cbm_resolution_t res = cbm_registry_resolve(rc->registry, func_name, module_qn,
- imp_keys, imp_vals, imp_count);
- if (!res.qualified_name || res.qualified_name[0] == '\0') {
- continue;
- }
-
- const cbm_gbuf_node_t *dec_node =
- cbm_gbuf_find_by_qn(rc->main_gbuf, res.qualified_name);
- if (!dec_node || node->id == dec_node->id) {
- continue;
- }
-
- char dprops[256];
- snprintf(dprops, sizeof(dprops), "{\"decorator\":\"%s\"}", def->decorators[dc]);
- cbm_gbuf_insert_edge(ws->local_edge_buf, node->id, dec_node->id, "DECORATES",
- dprops);
- ws->semantic_resolved++;
- }
- }
- }
-
- /* ── IMPLEMENTS (Rust impl) ────────────────────────────── */
- for (int t = 0; t < result->impl_traits.count; t++) {
- CBMImplTrait *it = &result->impl_traits.items[t];
- if (!it->trait_name || !it->struct_name) {
- continue;
- }
-
- const char *trait_qn = resolve_as_class(rc->registry, it->trait_name, module_qn,
- imp_keys, imp_vals, imp_count);
- if (!trait_qn) {
- continue;
- }
- const char *struct_qn = resolve_as_class(rc->registry, it->struct_name, module_qn,
- imp_keys, imp_vals, imp_count);
- if (!struct_qn) {
- continue;
- }
-
- const cbm_gbuf_node_t *trait_node = cbm_gbuf_find_by_qn(rc->main_gbuf, trait_qn);
- const cbm_gbuf_node_t *struct_node = cbm_gbuf_find_by_qn(rc->main_gbuf, struct_qn);
- if (!trait_node || !struct_node || trait_node->id == struct_node->id) {
- continue;
- }
-
- cbm_gbuf_insert_edge(ws->local_edge_buf, struct_node->id, trait_node->id, "IMPLEMENTS",
- "{}");
- ws->semantic_resolved++;
- }
+ /* ── INHERITS + DECORATES + IMPLEMENTS ──────────────────── */
+ resolve_file_semantic(rc, ws, result, module_qn, imp_keys, imp_vals, imp_count);
free(module_qn);
free_import_map(imp_keys, imp_vals, imp_count);
diff --git a/src/pipeline/pass_semantic.c b/src/pipeline/pass_semantic.c
index 5630d8908..fa9788a11 100644
--- a/src/pipeline/pass_semantic.c
+++ b/src/pipeline/pass_semantic.c
@@ -202,7 +202,45 @@ static bool fp_ends_with(const char *fp, const char *suffix) {
return fplen >= sflen && strcmp(fp + fplen - sflen, suffix) == 0;
}
-// NOLINTNEXTLINE(readability-function-cognitive-complexity)
+/* Info about one interface method (name + node ID). */
+typedef struct {
+ const char *name;
+ int64_t id;
+} go_imethod_t;
+
+/* Check if class has all interface methods and create IMPLEMENTS + OVERRIDE edges. */
+static int check_go_class_implements(cbm_pipeline_ctx_t *ctx, const cbm_gbuf_node_t *cls,
+ const cbm_gbuf_node_t *iface, const go_imethod_t *imethods,
+ int im_count) {
+ if (!cls->file_path || !cls->qualified_name) {
+ return 0;
+ }
+ if (!fp_ends_with(cls->file_path, ".go")) {
+ return 0;
+ }
+ char prefix[512];
+ snprintf(prefix, sizeof(prefix), "%s.", cls->qualified_name);
+ for (int m = 0; m < im_count; m++) {
+ char method_qn[512];
+ snprintf(method_qn, sizeof(method_qn), "%s%s", prefix, imethods[m].name);
+ if (!cbm_gbuf_find_by_qn(ctx->gbuf, method_qn)) {
+ return 0;
+ }
+ }
+ cbm_gbuf_insert_edge(ctx->gbuf, cls->id, iface->id, "IMPLEMENTS", "{}");
+ int edges = 1;
+ for (int m = 0; m < im_count; m++) {
+ char method_qn[512];
+ snprintf(method_qn, sizeof(method_qn), "%s%s", prefix, imethods[m].name);
+ const cbm_gbuf_node_t *cm = cbm_gbuf_find_by_qn(ctx->gbuf, method_qn);
+ if (cm) {
+ cbm_gbuf_insert_edge(ctx->gbuf, cm->id, imethods[m].id, "OVERRIDE", "{}");
+ edges++;
+ }
+ }
+ return edges;
+}
+
int cbm_pipeline_implements_go(cbm_pipeline_ctx_t *ctx) {
int edge_count = 0;
@@ -237,16 +275,12 @@ int cbm_pipeline_implements_go(cbm_pipeline_ctx_t *ctx) {
}
/* Collect interface method info */
- typedef struct {
- const char *name;
- int64_t id;
- } imethod_t;
- imethod_t imethods[128];
+ go_imethod_t imethods[128];
int im_count = 0;
for (int j = 0; j < dm_count && im_count < 128; j++) {
const cbm_gbuf_node_t *m = cbm_gbuf_find_by_id(ctx->gbuf, dm_edges[j]->target_id);
if (m && m->name) {
- imethods[im_count++] = (imethod_t){m->name, m->id};
+ imethods[im_count++] = (go_imethod_t){m->name, m->id};
}
}
if (im_count == 0) {
@@ -255,52 +289,120 @@ int cbm_pipeline_implements_go(cbm_pipeline_ctx_t *ctx) {
/* Check each Class node for method-set satisfaction */
for (int c = 0; c < class_count; c++) {
- const cbm_gbuf_node_t *cls = classes[c];
- if (!cls->file_path || !cls->qualified_name) {
+ edge_count += check_go_class_implements(ctx, classes[c], iface, imethods, im_count);
+ }
+ }
+ return edge_count;
+}
+
+/* Process INHERITS + DECORATES edges for one definition. */
+/* Resolve one decorator and create DECORATES edge. */
+static void resolve_decorator(cbm_pipeline_ctx_t *ctx, const cbm_gbuf_node_t *node,
+ const char *decorator, const char *module_qn, const char **imp_keys,
+ const char **imp_vals, int imp_count, int *count) {
+ char func_name[256];
+ extract_decorator_func(decorator, func_name, sizeof(func_name));
+ if (func_name[0] == '\0') {
+ return;
+ }
+ cbm_resolution_t res =
+ cbm_registry_resolve(ctx->registry, func_name, module_qn, imp_keys, imp_vals, imp_count);
+ if (!res.qualified_name || res.qualified_name[0] == '\0') {
+ return;
+ }
+ const cbm_gbuf_node_t *dec = cbm_gbuf_find_by_qn(ctx->gbuf, res.qualified_name);
+ if (dec && node->id != dec->id) {
+ char props[256];
+ snprintf(props, sizeof(props), "{\"decorator\":\"%s\"}", decorator);
+ cbm_gbuf_insert_edge(ctx->gbuf, node->id, dec->id, "DECORATES", props);
+ (*count)++;
+ }
+}
+
+static void sem_process_def_edges(cbm_pipeline_ctx_t *ctx, const CBMDefinition *def,
+ const char *module_qn, const char **imp_keys,
+ const char **imp_vals, int imp_count, int *inherits_count,
+ int *decorates_count) {
+ if (!def->qualified_name) {
+ return;
+ }
+ const cbm_gbuf_node_t *node = cbm_gbuf_find_by_qn(ctx->gbuf, def->qualified_name);
+ if (!node) {
+ return;
+ }
+ if (def->base_classes) {
+ for (int b = 0; def->base_classes[b]; b++) {
+ const char *base_qn = resolve_as_class(ctx->registry, def->base_classes[b], module_qn,
+ imp_keys, imp_vals, imp_count);
+ if (!base_qn) {
continue;
}
- if (!fp_ends_with(cls->file_path, ".go")) {
- continue;
+ const cbm_gbuf_node_t *base_node = cbm_gbuf_find_by_qn(ctx->gbuf, base_qn);
+ if (base_node && node->id != base_node->id) {
+ cbm_gbuf_insert_edge(ctx->gbuf, node->id, base_node->id, "INHERITS", "{}");
+ (*inherits_count)++;
}
+ }
+ }
+ if (def->decorators) {
+ for (int dc = 0; def->decorators[dc]; dc++) {
+ resolve_decorator(ctx, node, def->decorators[dc], module_qn, imp_keys, imp_vals,
+ imp_count, decorates_count);
+ }
+ }
+}
- /* Build QN prefix: "pkg.FileReader." */
- char prefix[512];
- snprintf(prefix, sizeof(prefix), "%s.", cls->qualified_name);
-
- /* Check if ALL interface methods are present */
- bool all_match = true;
- for (int m = 0; m < im_count; m++) {
- char method_qn[512];
- snprintf(method_qn, sizeof(method_qn), "%s%s", prefix, imethods[m].name);
- if (!cbm_gbuf_find_by_qn(ctx->gbuf, method_qn)) {
- all_match = false;
- break;
- }
- }
- if (!all_match) {
- continue;
- }
+/* Get extraction result from cache or re-extract. Sets *owned=true if caller must free. */
+static CBMFileResult *sem_get_or_extract(cbm_pipeline_ctx_t *ctx, int file_idx,
+ const cbm_file_info_t *fi, bool *owned) {
+ *owned = false;
+ if (ctx->result_cache && ctx->result_cache[file_idx]) {
+ return ctx->result_cache[file_idx];
+ }
+ int source_len = 0;
+ char *source = read_file(fi->path, &source_len);
+ if (!source) {
+ return NULL;
+ }
+ CBMFileResult *r = cbm_extract_file(source, source_len, fi->language, ctx->project_name,
+ fi->rel_path, CBM_EXTRACT_BUDGET, NULL, NULL);
+ free(source);
+ if (r) {
+ *owned = true;
+ }
+ return r;
+}
- /* IMPLEMENTS edge: Class → Interface */
- cbm_gbuf_insert_edge(ctx->gbuf, cls->id, iface->id, "IMPLEMENTS", "{}");
- edge_count++;
-
- /* OVERRIDE edges: ClassMethod → InterfaceMethod */
- for (int m = 0; m < im_count; m++) {
- char method_qn[512];
- snprintf(method_qn, sizeof(method_qn), "%s%s", prefix, imethods[m].name);
- const cbm_gbuf_node_t *cm = cbm_gbuf_find_by_qn(ctx->gbuf, method_qn);
- if (cm) {
- cbm_gbuf_insert_edge(ctx->gbuf, cm->id, imethods[m].id, "OVERRIDE", "{}");
- edge_count++;
- }
- }
+/* Resolve Rust impl traits for one file's extraction results. */
+static int resolve_impl_traits(cbm_pipeline_ctx_t *ctx, const CBMFileResult *result,
+ const char *module_qn, const char **imp_keys, const char **imp_vals,
+ int imp_count) {
+ int count = 0;
+ for (int t = 0; t < result->impl_traits.count; t++) {
+ CBMImplTrait *it = &result->impl_traits.items[t];
+ if (!it->trait_name || !it->struct_name) {
+ continue;
+ }
+ const char *trait_qn = resolve_as_class(ctx->registry, it->trait_name, module_qn, imp_keys,
+ imp_vals, imp_count);
+ if (!trait_qn) {
+ continue;
+ }
+ const char *struct_qn = resolve_as_class(ctx->registry, it->struct_name, module_qn,
+ imp_keys, imp_vals, imp_count);
+ if (!struct_qn) {
+ continue;
+ }
+ const cbm_gbuf_node_t *tn = cbm_gbuf_find_by_qn(ctx->gbuf, trait_qn);
+ const cbm_gbuf_node_t *sn = cbm_gbuf_find_by_qn(ctx->gbuf, struct_qn);
+ if (tn && sn && tn->id != sn->id) {
+ cbm_gbuf_insert_edge(ctx->gbuf, sn->id, tn->id, "IMPLEMENTS", "{}");
+ count++;
}
}
- return edge_count;
+ return count;
}
-// NOLINTNEXTLINE(readability-function-cognitive-complexity)
int cbm_pipeline_pass_semantic(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
int file_count) {
cbm_log_info("pass.start", "pass", "semantic", "files", itoa_log(file_count));
@@ -315,31 +417,13 @@ int cbm_pipeline_pass_semantic(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *f
return -1;
}
- const char *path = files[i].path;
const char *rel = files[i].rel_path;
- /* Use cached extraction result or re-extract */
- CBMFileResult *result = NULL;
bool result_owned = false;
- if (ctx->result_cache) {
- result = ctx->result_cache[i];
- }
+ CBMFileResult *result = sem_get_or_extract(ctx, i, &files[i], &result_owned);
if (!result) {
- CBMLanguage lang = files[i].language;
- int source_len = 0;
- char *source = read_file(path, &source_len);
- if (!source) {
- errors++;
- continue;
- }
- result = cbm_extract_file(source, source_len, lang, ctx->project_name, rel,
- CBM_EXTRACT_BUDGET, NULL, NULL);
- free(source);
- if (!result) {
- errors++;
- continue;
- }
- result_owned = true;
+ errors++;
+ continue;
}
/* Build import map for resolution */
@@ -352,96 +436,13 @@ int cbm_pipeline_pass_semantic(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *f
/* ── INHERITS + DECORATES from definitions ──────────────── */
for (int d = 0; d < result->defs.count; d++) {
- CBMDefinition *def = &result->defs.items[d];
- if (!def->qualified_name) {
- continue;
- }
-
- const cbm_gbuf_node_t *node = cbm_gbuf_find_by_qn(ctx->gbuf, def->qualified_name);
- if (!node) {
- continue;
- }
-
- /* INHERITS: base_classes */
- if (def->base_classes) {
- for (int b = 0; def->base_classes[b]; b++) {
- const char *base_qn =
- resolve_as_class(ctx->registry, def->base_classes[b], module_qn, imp_keys,
- imp_vals, imp_count);
- if (!base_qn) {
- continue;
- }
-
- const cbm_gbuf_node_t *base_node = cbm_gbuf_find_by_qn(ctx->gbuf, base_qn);
- if (!base_node || node->id == base_node->id) {
- continue;
- }
-
- cbm_gbuf_insert_edge(ctx->gbuf, node->id, base_node->id, "INHERITS", "{}");
- inherits_count++;
- }
- }
-
- /* DECORATES: decorator → the decorated function/class */
- if (def->decorators) {
- for (int dc = 0; def->decorators[dc]; dc++) {
- char func_name[256];
- extract_decorator_func(def->decorators[dc], func_name, sizeof(func_name));
- if (func_name[0] == '\0') {
- continue;
- }
-
- cbm_resolution_t res = cbm_registry_resolve(ctx->registry, func_name, module_qn,
- imp_keys, imp_vals, imp_count);
- if (!res.qualified_name || res.qualified_name[0] == '\0') {
- continue;
- }
-
- const cbm_gbuf_node_t *dec_node =
- cbm_gbuf_find_by_qn(ctx->gbuf, res.qualified_name);
- if (!dec_node || node->id == dec_node->id) {
- continue;
- }
-
- char props[256];
- snprintf(props, sizeof(props), "{\"decorator\":\"%s\"}", def->decorators[dc]);
- cbm_gbuf_insert_edge(ctx->gbuf, node->id, dec_node->id, "DECORATES", props);
- decorates_count++;
- }
- }
+ sem_process_def_edges(ctx, &result->defs.items[d], module_qn, imp_keys, imp_vals,
+ imp_count, &inherits_count, &decorates_count);
}
/* ── IMPLEMENTS from impl_traits (Rust) ─────────────────── */
- for (int t = 0; t < result->impl_traits.count; t++) {
- CBMImplTrait *it = &result->impl_traits.items[t];
- if (!it->trait_name || !it->struct_name) {
- continue;
- }
-
- const char *trait_qn = resolve_as_class(ctx->registry, it->trait_name, module_qn,
- imp_keys, imp_vals, imp_count);
- if (!trait_qn) {
- continue;
- }
-
- const char *struct_qn = resolve_as_class(ctx->registry, it->struct_name, module_qn,
- imp_keys, imp_vals, imp_count);
- if (!struct_qn) {
- continue;
- }
-
- const cbm_gbuf_node_t *trait_node = cbm_gbuf_find_by_qn(ctx->gbuf, trait_qn);
- const cbm_gbuf_node_t *struct_node = cbm_gbuf_find_by_qn(ctx->gbuf, struct_qn);
- if (!trait_node || !struct_node) {
- continue;
- }
- if (trait_node->id == struct_node->id) {
- continue;
- }
-
- cbm_gbuf_insert_edge(ctx->gbuf, struct_node->id, trait_node->id, "IMPLEMENTS", "{}");
- implements_count++;
- }
+ implements_count +=
+ resolve_impl_traits(ctx, result, module_qn, imp_keys, imp_vals, imp_count);
free(module_qn);
free_import_map(imp_keys, imp_vals, imp_count);
diff --git a/src/pipeline/pipeline.c b/src/pipeline/pipeline.c
index 69da7d41f..15680514a 100644
--- a/src/pipeline/pipeline.c
+++ b/src/pipeline/pipeline.c
@@ -180,7 +180,50 @@ static void free_seen_dir_key(const char *key, void *val, void *ud) {
/* ── Pass 1: Structure ──────────────────────────────────────────── */
/* Create Project, Folder/Package, and File nodes in the graph buffer. */
-// NOLINTNEXTLINE(readability-function-cognitive-complexity)
+/* Walk directory chain upward, creating Folder nodes and CONTAINS_FOLDER edges. */
+static void create_folder_chain(cbm_pipeline_t *p, const char *dir, CBMHashTable *seen_dirs) {
+ char *walk = strdup(dir);
+ while (walk[0] != '\0' && !cbm_ht_get(seen_dirs, walk)) {
+ cbm_ht_set(seen_dirs, strdup(walk), (void *)1);
+ char *folder_qn = cbm_pipeline_fqn_folder(p->project_name, walk);
+ const char *dir_base = strrchr(walk, '/');
+ dir_base = dir_base ? dir_base + 1 : walk;
+ cbm_gbuf_upsert_node(p->gbuf, "Folder", dir_base, folder_qn, walk, 0, 0, "{}");
+
+ char *pdir = strdup(walk);
+ char *ps = strrchr(pdir, '/');
+ if (ps) {
+ *ps = '\0';
+ } else {
+ free(pdir);
+ pdir = strdup("");
+ }
+ const char *pqn;
+ char *pqn_heap = NULL;
+ if (pdir[0] == '\0') {
+ pqn = p->project_name;
+ } else {
+ pqn_heap = cbm_pipeline_fqn_folder(p->project_name, pdir);
+ pqn = pqn_heap;
+ }
+ const cbm_gbuf_node_t *fn = cbm_gbuf_find_by_qn(p->gbuf, folder_qn);
+ const cbm_gbuf_node_t *pn = cbm_gbuf_find_by_qn(p->gbuf, pqn);
+ if (fn && pn) {
+ cbm_gbuf_insert_edge(p->gbuf, pn->id, fn->id, "CONTAINS_FOLDER", "{}");
+ }
+ free(folder_qn);
+ free(pqn_heap);
+ char *up = strrchr(walk, '/');
+ if (up) {
+ *up = '\0';
+ } else {
+ walk[0] = '\0';
+ }
+ free(pdir);
+ }
+ free(walk);
+}
+
static int pass_structure(cbm_pipeline_t *p, const cbm_file_info_t *files, int file_count) {
cbm_log_info("pass.start", "pass", "structure", "files", itoa_buf(file_count));
@@ -232,55 +275,7 @@ static int pass_structure(cbm_pipeline_t *p, const cbm_file_info_t *files, int f
}
/* Walk up directory chain, creating Folder nodes */
- char *walk_dir = strdup(dir);
- while (walk_dir[0] != '\0' && !cbm_ht_get(seen_dirs, walk_dir)) {
- cbm_ht_set(seen_dirs, strdup(walk_dir), (void *)1);
-
- char *folder_qn = cbm_pipeline_fqn_folder(p->project_name, walk_dir);
- const char *dir_base = strrchr(walk_dir, '/');
- dir_base = dir_base ? dir_base + 1 : walk_dir;
-
- cbm_gbuf_upsert_node(p->gbuf, "Folder", dir_base, folder_qn, walk_dir, 0, 0, "{}");
-
- /* CONTAINS_FOLDER edge: parent -> this dir */
- char *pdir = strdup(walk_dir);
- char *ps = strrchr(pdir, '/');
- if (ps) {
- {
- *ps = '\0';
- }
- } else {
- free(pdir);
- pdir = strdup("");
- }
-
- const char *pqn;
- char *pqn_heap = NULL;
- if (pdir[0] == '\0') {
- pqn = p->project_name;
- } else {
- pqn_heap = cbm_pipeline_fqn_folder(p->project_name, pdir);
- pqn = pqn_heap;
- }
-
- const cbm_gbuf_node_t *fn = cbm_gbuf_find_by_qn(p->gbuf, folder_qn);
- const cbm_gbuf_node_t *pn = cbm_gbuf_find_by_qn(p->gbuf, pqn);
- if (fn && pn) {
- cbm_gbuf_insert_edge(p->gbuf, pn->id, fn->id, "CONTAINS_FOLDER", "{}");
- }
-
- free(folder_qn);
- free(pqn_heap);
-
- /* Move up one level */
- char *up = strrchr(walk_dir, '/');
- if (up) {
- *up = '\0';
- } else {
- walk_dir[0] = '\0';
- }
- free(pdir);
- }
+ create_folder_chain(p, dir, seen_dirs);
/* Now create the CONTAINS_FILE edge */
const cbm_gbuf_node_t *fnode = cbm_gbuf_find_by_qn(p->gbuf, file_qn);
@@ -291,7 +286,6 @@ static int pass_structure(cbm_pipeline_t *p, const cbm_file_info_t *files, int f
free(file_qn);
free(dir);
- free(walk_dir);
free(parent_qn_heap);
}
@@ -326,7 +320,27 @@ static void *gh_compute_thread_fn(void *arg) {
/* Process infra bindings: topic→URL pairs from IaC configs.
* Creates Route nodes for endpoints and HANDLES edges linking
* topic Routes to endpoint Routes (bridging the gap). */
-// NOLINTNEXTLINE(readability-function-cognitive-complexity)
+/* Process one infra binding: create Route node + INFRA_MAPS edge. */
+static int process_one_infra_binding(cbm_gbuf_t *gbuf, const CBMInfraBinding *ib,
+ const char *rel_path) {
+ char url_route_qn[CBM_ROUTE_QN_SIZE];
+ snprintf(url_route_qn, sizeof(url_route_qn), "__route__infra__%s", ib->target_url);
+ int64_t url_route_id = cbm_gbuf_upsert_node(gbuf, "Route", ib->target_url, url_route_qn,
+ rel_path, 0, 0, "{\"source\":\"infra\"}");
+ char topic_route_qn[CBM_ROUTE_QN_SIZE];
+ snprintf(topic_route_qn, sizeof(topic_route_qn), "__route__%s__%s",
+ ib->broker ? ib->broker : "async", ib->source_name);
+ const cbm_gbuf_node_t *topic_route = cbm_gbuf_find_by_qn(gbuf, topic_route_qn);
+ if (!topic_route) {
+ return 0;
+ }
+ char props[512];
+ snprintf(props, sizeof(props), "{\"broker\":\"%s\",\"topic\":\"%s\",\"endpoint\":\"%s\"}",
+ ib->broker ? ib->broker : "async", ib->source_name, ib->target_url);
+ cbm_gbuf_insert_edge(gbuf, topic_route->id, url_route_id, "INFRA_MAPS", props);
+ return 1;
+}
+
static void cbm_pipeline_process_infra_bindings(cbm_gbuf_t *gbuf, const cbm_file_info_t *files,
CBMFileResult **result_cache, int file_count) {
int bindings = 0;
@@ -336,31 +350,8 @@ static void cbm_pipeline_process_infra_bindings(cbm_gbuf_t *gbuf, const cbm_file
}
for (int bi = 0; bi < result_cache[i]->infra_bindings.count; bi++) {
const CBMInfraBinding *ib = &result_cache[i]->infra_bindings.items[bi];
- if (!ib->source_name || !ib->target_url) {
- continue;
- }
-
- /* Create or find the endpoint Route node */
- char url_route_qn[CBM_ROUTE_QN_SIZE];
- snprintf(url_route_qn, sizeof(url_route_qn), "__route__infra__%s", ib->target_url);
- int64_t url_route_id =
- cbm_gbuf_upsert_node(gbuf, "Route", ib->target_url, url_route_qn, files[i].rel_path,
- 0, 0, "{\"source\":\"infra\"}");
-
- /* Find the topic/queue Route node created by code-side ASYNC_CALLS */
- char topic_route_qn[CBM_ROUTE_QN_SIZE];
- snprintf(topic_route_qn, sizeof(topic_route_qn), "__route__%s__%s",
- ib->broker ? ib->broker : "async", ib->source_name);
- const cbm_gbuf_node_t *topic_route = cbm_gbuf_find_by_qn(gbuf, topic_route_qn);
-
- if (topic_route) {
- /* Bridge: topic Route → endpoint Route via INFRA_MAPS edge */
- char props[512];
- snprintf(props, sizeof(props),
- "{\"broker\":\"%s\",\"topic\":\"%s\",\"endpoint\":\"%s\"}",
- ib->broker ? ib->broker : "async", ib->source_name, ib->target_url);
- cbm_gbuf_insert_edge(gbuf, topic_route->id, url_route_id, "INFRA_MAPS", props);
- bindings++;
+ if (ib->source_name && ib->target_url) {
+ bindings += process_one_infra_binding(gbuf, ib, files[i].rel_path);
}
}
}
@@ -371,41 +362,321 @@ static void cbm_pipeline_process_infra_bindings(cbm_gbuf_t *gbuf, const cbm_file
}
}
-// NOLINTNEXTLINE(readability-function-cognitive-complexity)
+static bool is_infra_file(const char *fp) {
+ return fp != NULL &&
+ (strstr(fp, ".yaml") != NULL || strstr(fp, ".yml") != NULL ||
+ strstr(fp, ".tf") != NULL || strstr(fp, ".hcl") != NULL || strstr(fp, ".toml") != NULL);
+}
+
+/* Try to create an infra Route node from one string_ref. */
+static void try_upsert_infra_route(cbm_gbuf_t *gbuf, const CBMStringRef *sr, const char *fp) {
+ if (sr->kind != CBM_STRREF_URL || !sr->value || !strstr(sr->value, "://")) {
+ return;
+ }
+ char route_qn[CBM_ROUTE_QN_SIZE];
+ snprintf(route_qn, sizeof(route_qn), "__route__infra__%s", sr->value);
+ char route_props[512];
+ if (sr->key_path) {
+ snprintf(route_props, sizeof(route_props), "{\"source\":\"infra\",\"key_path\":\"%s\"}",
+ sr->key_path);
+ } else {
+ snprintf(route_props, sizeof(route_props), "{\"source\":\"infra\"}");
+ }
+ cbm_gbuf_upsert_node(gbuf, "Route", sr->value, route_qn, fp, 0, 0, route_props);
+}
+
static void cbm_pipeline_extract_infra_routes(cbm_gbuf_t *gbuf, const cbm_file_info_t *files,
CBMFileResult **result_cache, int file_count) {
for (int i = 0; i < file_count; i++) {
- if (!result_cache[i]) {
- continue;
- }
- const char *fp = files[i].rel_path;
- if (fp == NULL || (strstr(fp, ".yaml") == NULL && strstr(fp, ".yml") == NULL &&
- strstr(fp, ".tf") == NULL && strstr(fp, ".hcl") == NULL &&
- strstr(fp, ".toml") == NULL)) {
+ if (!result_cache[i] || !is_infra_file(files[i].rel_path)) {
continue;
}
for (int si = 0; si < result_cache[i]->string_refs.count; si++) {
- const CBMStringRef *sr = &result_cache[i]->string_refs.items[si];
- if (sr->kind == CBM_STRREF_URL && sr->value != NULL &&
- strstr(sr->value, "://") != NULL) {
- char route_qn[CBM_ROUTE_QN_SIZE];
- snprintf(route_qn, sizeof(route_qn), "__route__infra__%s", sr->value);
- char route_props[512];
- if (sr->key_path != NULL) {
- snprintf(route_props, sizeof(route_props),
- "{\"source\":\"infra\",\"key_path\":\"%s\"}", sr->key_path);
- } else {
- snprintf(route_props, sizeof(route_props), "{\"source\":\"infra\"}");
- }
- cbm_gbuf_upsert_node(gbuf, "Route", sr->value, route_qn, fp, 0, 0, route_props);
+ try_upsert_infra_route(gbuf, &result_cache[i]->string_refs.items[si],
+ files[i].rel_path);
+ }
+ }
+}
+
+/* Run decorator_tags, configlink, and route matching passes. */
+static void run_predump_passes(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx) {
+ struct timespec t;
+ if (!check_cancel(p)) {
+ cbm_clock_gettime(CLOCK_MONOTONIC, &t);
+ cbm_pipeline_pass_decorator_tags(p->gbuf, p->project_name);
+ cbm_log_info("pass.timing", "pass", "decorator_tags", "elapsed_ms",
+ itoa_buf((int)elapsed_ms(t)));
+ }
+ if (!check_cancel(p)) {
+ cbm_clock_gettime(CLOCK_MONOTONIC, &t);
+ cbm_pipeline_pass_configlink(ctx);
+ cbm_log_info("pass.timing", "pass", "configlink", "elapsed_ms",
+ itoa_buf((int)elapsed_ms(t)));
+ }
+ if (!check_cancel(p)) {
+ cbm_clock_gettime(CLOCK_MONOTONIC, &t);
+ cbm_pipeline_create_route_nodes(p->gbuf);
+ cbm_log_info("pass.timing", "pass", "route_match", "elapsed_ms",
+ itoa_buf((int)elapsed_ms(t)));
+ }
+}
+
+/* Run the sequential pipeline path: definitions, k8s, calls, usages, semantic. */
+static int run_sequential_pipeline(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx,
+ const cbm_file_info_t *files, int file_count,
+ struct timespec *t) {
+ cbm_log_info("pipeline.mode", "mode", "sequential", "files", itoa_buf(file_count));
+ CBMFileResult **seq_cache = (CBMFileResult **)calloc(file_count, sizeof(CBMFileResult *));
+ if (seq_cache) {
+ ctx->result_cache = seq_cache;
+ }
+ typedef int (*seq_pass_fn)(cbm_pipeline_ctx_t *, const cbm_file_info_t *, int);
+ static const struct {
+ seq_pass_fn fn;
+ const char *name;
+ bool ignore_err;
+ } seq_passes[] = {
+ {cbm_pipeline_pass_definitions, "definitions", false},
+ {cbm_pipeline_pass_k8s, "k8s", true},
+ {cbm_pipeline_pass_calls, "calls", false},
+ {cbm_pipeline_pass_usages, "usages", false},
+ {cbm_pipeline_pass_semantic, "semantic", false},
+ };
+ int rc = 0;
+ for (int si = 0; si < 5 && rc == 0; si++) {
+ cbm_clock_gettime(CLOCK_MONOTONIC, t);
+ int pr = seq_passes[si].fn(ctx, files, file_count);
+ if (pr != 0 && !seq_passes[si].ignore_err) {
+ rc = pr;
+ }
+ cbm_log_info("pass.timing", "pass", seq_passes[si].name, "elapsed_ms",
+ itoa_buf((int)elapsed_ms(*t)));
+ if (check_cancel(p)) {
+ rc = -1;
+ }
+ }
+ if (seq_cache) {
+ for (int i = 0; i < file_count; i++) {
+ if (seq_cache[i]) {
+ cbm_free_result(seq_cache[i]);
}
}
+ free(seq_cache);
+ ctx->result_cache = NULL;
}
+ return rc;
+}
+
+/* Run the parallel pipeline path: extract, registry, resolve, infra, k8s. */
+static int run_parallel_pipeline(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx,
+ const cbm_file_info_t *files, int file_count, int worker_count,
+ struct timespec *t) {
+ cbm_log_info("pipeline.mode", "mode", "parallel", "workers", itoa_buf(worker_count), "files",
+ itoa_buf(file_count));
+ _Atomic int64_t shared_ids;
+ atomic_init(&shared_ids, cbm_gbuf_next_id(p->gbuf));
+ CBMFileResult **cache = (CBMFileResult **)calloc(file_count, sizeof(CBMFileResult *));
+ if (!cache) {
+ cbm_log_error("pipeline.err", "phase", "cache_alloc");
+ return -1;
+ }
+ cbm_clock_gettime(CLOCK_MONOTONIC, t);
+ int rc = cbm_parallel_extract(ctx, files, file_count, cache, &shared_ids, worker_count);
+ cbm_log_info("pass.timing", "pass", "parallel_extract", "elapsed_ms",
+ itoa_buf((int)elapsed_ms(*t)));
+ if (rc != 0 || check_cancel(p)) {
+ free(cache);
+ return rc != 0 ? rc : -1;
+ }
+ cbm_gbuf_set_next_id(p->gbuf, atomic_load(&shared_ids));
+ cbm_clock_gettime(CLOCK_MONOTONIC, t);
+ rc = cbm_build_registry_from_cache(ctx, files, file_count, cache);
+ cbm_log_info("pass.timing", "pass", "registry_build", "elapsed_ms",
+ itoa_buf((int)elapsed_ms(*t)));
+ if (rc != 0 || check_cancel(p)) {
+ for (int i = 0; i < file_count; i++) {
+ if (cache[i]) {
+ cbm_free_result(cache[i]);
+ }
+ }
+ free(cache);
+ return rc != 0 ? rc : -1;
+ }
+ cbm_clock_gettime(CLOCK_MONOTONIC, t);
+ rc = cbm_parallel_resolve(ctx, files, file_count, cache, &shared_ids, worker_count);
+ cbm_log_info("pass.timing", "pass", "parallel_resolve", "elapsed_ms",
+ itoa_buf((int)elapsed_ms(*t)));
+ cbm_gbuf_set_next_id(p->gbuf, atomic_load(&shared_ids));
+ cbm_pipeline_extract_infra_routes(p->gbuf, files, cache, file_count);
+ cbm_pipeline_process_infra_bindings(p->gbuf, files, cache, file_count);
+ for (int i = 0; i < file_count; i++) {
+ if (cache[i]) {
+ cbm_free_result(cache[i]);
+ }
+ }
+ free(cache);
+ if (rc != 0) {
+ return rc;
+ }
+ cbm_clock_gettime(CLOCK_MONOTONIC, t);
+ cbm_pipeline_pass_k8s(ctx, files, file_count);
+ cbm_log_info("pass.timing", "pass", "k8s", "elapsed_ms", itoa_buf((int)elapsed_ms(*t)));
+ return check_cancel(p) ? -1 : 0;
+}
+
+/* Try incremental pipeline or delete old DB for reindex.
+ * Returns >= 0 if incremental was used (the return code), or -1 to proceed with full. */
+static int try_incremental_or_delete_db(cbm_pipeline_t *p, cbm_file_info_t *files, int file_count) {
+ char *db_path = resolve_db_path(p);
+ if (!db_path) {
+ return -1;
+ }
+ struct stat db_st;
+ if (stat(db_path, &db_st) != 0) {
+ free(db_path);
+ return -1;
+ }
+ cbm_store_t *check_store = cbm_store_open_path(db_path);
+ if (check_store && cbm_store_check_integrity(check_store)) {
+ cbm_file_hash_t *hashes = NULL;
+ int hash_count = 0;
+ cbm_store_get_file_hashes(check_store, p->project_name, &hashes, &hash_count);
+ cbm_store_free_file_hashes(hashes, hash_count);
+ cbm_store_close(check_store);
+ if (hash_count > 0 && file_count <= hash_count + (hash_count / 2)) {
+ cbm_log_info("pipeline.route", "path", "incremental", "stored_hashes",
+ itoa_buf(hash_count));
+ int rc = cbm_pipeline_run_incremental(p, db_path, files, file_count);
+ free(db_path);
+ return rc;
+ }
+ if (hash_count > 0) {
+ cbm_log_info("pipeline.route", "path", "mode_change_reindex", "stored_hashes",
+ itoa_buf(hash_count), "discovered", itoa_buf(file_count));
+ }
+ } else if (check_store) {
+ cbm_store_close(check_store);
+ }
+ cbm_log_info("pipeline.route", "path", "reindex", "action", "deleting old db");
+ cbm_unlink(db_path);
+ char wal[1040];
+ char shm[1040];
+ snprintf(wal, sizeof(wal), "%s-wal", db_path);
+ snprintf(shm, sizeof(shm), "%s-shm", db_path);
+ cbm_unlink(wal);
+ cbm_unlink(shm);
+ free(db_path);
+ return -1;
+}
+
+/* Get platform-specific mtime in nanoseconds. */
+static int64_t stat_mtime_ns(const struct stat *fst) {
+#ifdef __APPLE__
+ return ((int64_t)fst->st_mtimespec.tv_sec * 1000000000LL) + (int64_t)fst->st_mtimespec.tv_nsec;
+#elif defined(_WIN32)
+ return (int64_t)fst->st_mtime * 1000000000LL;
+#else
+ return ((int64_t)fst->st_mtim.tv_sec * 1000000000LL) + (int64_t)fst->st_mtim.tv_nsec;
+#endif
+}
+
+/* Dump graph to SQLite and persist file hashes for incremental indexing. */
+static int dump_and_persist_hashes(cbm_pipeline_t *p, const cbm_file_info_t *files, int file_count,
+ struct timespec *t) {
+ cbm_clock_gettime(CLOCK_MONOTONIC, t);
+ const char *home = cbm_get_home_dir();
+ char db_path[1024];
+ if (p->db_path) {
+ snprintf(db_path, sizeof(db_path), "%s", p->db_path);
+ } else {
+ if (!home) {
+ home = cbm_tmpdir();
+ }
+ snprintf(db_path, sizeof(db_path), "%s/.cache/codebase-memory-mcp/%s.db", home,
+ p->project_name);
+ }
+ char db_dir[1024];
+ snprintf(db_dir, sizeof(db_dir), "%s", db_path);
+ char *last_slash = strrchr(db_dir, '/');
+ if (last_slash) {
+ *last_slash = '\0';
+ cbm_mkdir_p(db_dir, 0755);
+ }
+ int rc = cbm_gbuf_dump_to_sqlite(p->gbuf, db_path);
+ if (rc != 0) {
+ cbm_log_error("pipeline.err", "phase", "dump");
+ return rc;
+ }
+ cbm_log_info("pass.timing", "pass", "dump", "elapsed_ms", itoa_buf((int)elapsed_ms(*t)));
+ cbm_store_t *hash_store = cbm_store_open_path(db_path);
+ if (hash_store) {
+ cbm_store_delete_file_hashes(hash_store, p->project_name);
+ for (int i = 0; i < file_count; i++) {
+ struct stat fst;
+ if (stat(files[i].path, &fst) == 0) {
+ cbm_store_upsert_file_hash(hash_store, p->project_name, files[i].rel_path, "",
+ stat_mtime_ns(&fst), fst.st_size);
+ }
+ }
+ cbm_store_close(hash_store);
+ cbm_log_info("pass.timing", "pass", "persist_hashes", "files", itoa_buf(file_count));
+ }
+ return 0;
+}
+
+/* Run githistory (I/O) + httplinks (CPU) in parallel, then apply edges. */
+static int run_githistory_and_httplinks(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx) {
+ struct timespec t_gh;
+ struct timespec t_hl;
+ cbm_clock_gettime(CLOCK_MONOTONIC, &t_gh);
+ cbm_clock_gettime(CLOCK_MONOTONIC, &t_hl);
+
+ cbm_githistory_result_t gh_result = {0};
+ cbm_thread_t gh_thread;
+ bool gh_threaded = false;
+ gh_compute_arg_t gh_arg = {.repo_path = ctx->repo_path, .result = &gh_result};
+
+ if (p->mode != CBM_MODE_FAST) {
+ if (cbm_default_worker_count(true) > 1) {
+ if (cbm_thread_create(&gh_thread, 0, gh_compute_thread_fn, &gh_arg) == 0) {
+ gh_threaded = true;
+ }
+ }
+ if (!gh_threaded) {
+ cbm_pipeline_githistory_compute(ctx->repo_path, &gh_result);
+ cbm_log_info("pass.timing", "pass", "githistory_compute", "elapsed_ms",
+ itoa_buf((int)elapsed_ms(t_gh)));
+ }
+ } else {
+ cbm_log_info("pass.skip", "pass", "githistory", "reason", "fast_mode");
+ }
+
+ int rc = cbm_pipeline_pass_httplinks(ctx);
+ cbm_log_info("pass.timing", "pass", "httplinks", "elapsed_ms", itoa_buf((int)elapsed_ms(t_hl)));
+
+ if (gh_threaded) {
+ cbm_thread_join(&gh_thread);
+ cbm_log_info("pass.timing", "pass", "githistory_compute", "elapsed_ms",
+ itoa_buf((int)elapsed_ms(t_gh)));
+ }
+
+ if (rc != 0) {
+ free(gh_result.couplings);
+ return rc;
+ }
+
+ int gh_edges = 0;
+ if (gh_result.count > 0) {
+ gh_edges = cbm_pipeline_githistory_apply(ctx, &gh_result);
+ }
+ cbm_log_info("pass.done", "pass", "githistory", "commits", itoa_buf(gh_result.commit_count),
+ "edges", itoa_buf(gh_edges));
+ free(gh_result.couplings);
+ return 0;
}
/* ── Pipeline run ────────────────────────────────────────────────── */
-// NOLINTNEXTLINE(readability-function-cognitive-complexity,readability-function-size)
int cbm_pipeline_run(cbm_pipeline_t *p) {
if (!p) {
return -1;
@@ -429,75 +700,19 @@ int cbm_pipeline_run(cbm_pipeline_t *p) {
int rc = cbm_discover(p->repo_path, &opts, &files, &file_count);
if (rc != 0) {
cbm_log_error("pipeline.err", "phase", "discover", "rc", itoa_buf(rc));
- cbm_discover_free(files, file_count);
- cbm_set_user_lang_config(NULL);
- cbm_userconfig_free(p->userconfig);
- p->userconfig = NULL;
- return -1;
}
cbm_log_info("pipeline.discover", "files", itoa_buf(file_count), "elapsed_ms",
itoa_buf((int)elapsed_ms(t0)));
-
- if (check_cancel(p)) {
- cbm_discover_free(files, file_count);
- cbm_set_user_lang_config(NULL);
- cbm_userconfig_free(p->userconfig);
- p->userconfig = NULL;
- return -1;
+ if (rc != 0 || check_cancel(p)) {
+ rc = -1;
+ goto cleanup;
}
- /* Check for existing DB → route to incremental or delete for reindex */
- {
- char *db_path = resolve_db_path(p);
- if (db_path) {
- struct stat db_st;
- if (stat(db_path, &db_st) == 0) {
- /* DB exists — try incremental path first */
- cbm_store_t *check_store = cbm_store_open_path(db_path);
- if (check_store && cbm_store_check_integrity(check_store)) {
- cbm_file_hash_t *hashes = NULL;
- int hash_count = 0;
- cbm_store_get_file_hashes(check_store, p->project_name, &hashes, &hash_count);
- cbm_store_free_file_hashes(hashes, hash_count);
- cbm_store_close(check_store);
-
- if (hash_count > 0) {
- /* Detect mode change (e.g. fast→full): if discovered files
- * outnumber stored hashes by >50%, the DB was built with a
- * narrower file set. Delete and reindex fresh instead of
- * single-threading ~thousands of "new" files incrementally. */
- if (file_count > hash_count + (hash_count / 2)) {
- cbm_log_info("pipeline.route", "path", "mode_change_reindex",
- "stored_hashes", itoa_buf(hash_count), "discovered",
- itoa_buf(file_count));
- /* Falls through to delete-and-reindex below */
- } else {
- cbm_log_info("pipeline.route", "path", "incremental", "stored_hashes",
- itoa_buf(hash_count));
- rc = cbm_pipeline_run_incremental(p, db_path, files, file_count);
- cbm_discover_free(files, file_count);
- free(db_path);
- return rc;
- }
- }
- } else if (check_store) {
- cbm_store_close(check_store);
- }
-
- /* Not eligible for incremental → reindex: delete old DB first.
- * cbm_write_db writes directly to the final path (no rename),
- * so the old file must be gone before the pipeline dumps. */
- cbm_log_info("pipeline.route", "path", "reindex", "action", "deleting old db");
- cbm_unlink(db_path);
- char wal[1040];
- char shm[1040];
- snprintf(wal, sizeof(wal), "%s-wal", db_path);
- snprintf(shm, sizeof(shm), "%s-shm", db_path);
- cbm_unlink(wal);
- cbm_unlink(shm);
- }
- free(db_path);
- }
+ /* Check for existing DB → try incremental or delete for reindex */
+ rc = try_incremental_or_delete_db(p, files, file_count);
+ if (rc >= 0) {
+ cbm_discover_free(files, file_count);
+ return rc;
}
cbm_log_info("pipeline.route", "path", "full");
@@ -518,361 +733,49 @@ int cbm_pipeline_run(cbm_pipeline_t *p) {
};
cbm_clock_gettime(CLOCK_MONOTONIC, &t);
- rc = pass_structure(p, files, file_count);
- if (rc != 0) { // cppcheck-suppress knownConditionTrueFalse
- goto cleanup;
- }
+ pass_structure(p, files, file_count);
cbm_log_info("pass.timing", "pass", "structure", "elapsed_ms", itoa_buf((int)elapsed_ms(t)));
if (check_cancel(p)) {
rc = -1;
goto cleanup;
}
- /* Decide: parallel or sequential pipeline */
+ /* Run extraction passes (parallel or sequential) */
int worker_count = cbm_default_worker_count(true);
#define MIN_FILES_FOR_PARALLEL 50
- bool use_parallel = (worker_count > 1 && file_count > MIN_FILES_FOR_PARALLEL);
-
- if (use_parallel) {
- cbm_log_info("pipeline.mode", "mode", "parallel", "workers", itoa_buf(worker_count),
- "files", itoa_buf(file_count));
-
- /* Shared atomic ID source — workers allocate globally unique IDs */
- _Atomic int64_t shared_ids;
- int64_t gbuf_next = cbm_gbuf_next_id(p->gbuf);
- atomic_init(&shared_ids, gbuf_next);
-
- /* Allocate result cache: one CBMFileResult* per file */
- CBMFileResult **result_cache =
- (CBMFileResult **)calloc(file_count, sizeof(CBMFileResult *));
- if (!result_cache) {
- cbm_log_error("pipeline.err", "phase", "cache_alloc");
- rc = -1;
- goto cleanup;
- }
-
- /* Phase 3A: Parallel extract + definition nodes */
- cbm_clock_gettime(CLOCK_MONOTONIC, &t);
- rc = cbm_parallel_extract(&ctx, files, file_count, result_cache, &shared_ids, worker_count);
- cbm_log_info("pass.timing", "pass", "parallel_extract", "elapsed_ms",
- itoa_buf((int)elapsed_ms(t)));
- if (rc != 0) {
- free(result_cache);
- goto cleanup;
- }
- if (check_cancel(p)) {
- free(result_cache);
- rc = -1;
- goto cleanup;
- }
-
- /* Sync gbuf ID counter after merge */
- cbm_gbuf_set_next_id(p->gbuf, atomic_load(&shared_ids));
-
- /* Phase 3B: Serial registry build + DEFINES/IMPORTS edges */
- cbm_clock_gettime(CLOCK_MONOTONIC, &t);
- rc = cbm_build_registry_from_cache(&ctx, files, file_count, result_cache);
- cbm_log_info("pass.timing", "pass", "registry_build", "elapsed_ms",
- itoa_buf((int)elapsed_ms(t)));
- if (rc != 0) {
- for (int i = 0; i < file_count; i++) {
- if (result_cache[i]) {
- cbm_free_result(result_cache[i]);
- }
- }
- free(result_cache);
- goto cleanup;
- }
- if (check_cancel(p)) {
- for (int i = 0; i < file_count; i++) {
- if (result_cache[i]) {
- cbm_free_result(result_cache[i]);
- }
- }
- free(result_cache);
- rc = -1;
- goto cleanup;
- }
-
- /* Phase 4: Parallel resolution (calls + usages + semantic, fused) */
- cbm_clock_gettime(CLOCK_MONOTONIC, &t);
- rc = cbm_parallel_resolve(&ctx, files, file_count, result_cache, &shared_ids, worker_count);
- cbm_log_info("pass.timing", "pass", "parallel_resolve", "elapsed_ms",
- itoa_buf((int)elapsed_ms(t)));
-
- /* Sync gbuf ID counter after resolve merge */
- cbm_gbuf_set_next_id(p->gbuf, atomic_load(&shared_ids));
-
- /* Create Route nodes from infrastructure config URLs */
- cbm_pipeline_extract_infra_routes(p->gbuf, files, result_cache, file_count);
-
- /* Process infra bindings: link topic Routes to endpoint URLs */
- cbm_pipeline_process_infra_bindings(p->gbuf, files, result_cache, file_count);
-
- /* Free cached extraction results */
- for (int i = 0; i < file_count; i++) {
- if (result_cache[i]) {
- cbm_free_result(result_cache[i]);
- }
- }
- free(result_cache);
-
- if (rc != 0) {
- goto cleanup;
- }
- if (check_cancel(p)) {
- rc = -1;
- goto cleanup;
- }
-
- cbm_clock_gettime(CLOCK_MONOTONIC, &t);
- rc = cbm_pipeline_pass_k8s(&ctx, files, file_count);
- if (rc != 0) { /* log warning, continue */
- }
- cbm_log_info("pass.timing", "pass", "k8s", "elapsed_ms", itoa_buf((int)elapsed_ms(t)));
- if (check_cancel(p)) {
- rc = -1;
- goto cleanup;
- }
- } else {
- cbm_log_info("pipeline.mode", "mode", "sequential", "files", itoa_buf(file_count));
-
- /* Allocate result cache: pass_definitions stores results for reuse
- * by pass_calls/usages/semantic, avoiding 3x redundant file I/O + parsing */
- CBMFileResult **seq_cache = (CBMFileResult **)calloc(file_count, sizeof(CBMFileResult *));
- if (seq_cache) {
- ctx.result_cache = seq_cache;
- }
-
- /* Sequential fallback: original 4-pass chain */
- cbm_clock_gettime(CLOCK_MONOTONIC, &t);
- rc = cbm_pipeline_pass_definitions(&ctx, files, file_count);
- if (rc != 0) {
- goto seq_cleanup;
- }
- cbm_log_info("pass.timing", "pass", "definitions", "elapsed_ms",
- itoa_buf((int)elapsed_ms(t)));
- if (check_cancel(p)) {
- rc = -1;
- goto seq_cleanup;
- }
-
- cbm_clock_gettime(CLOCK_MONOTONIC, &t);
- rc = cbm_pipeline_pass_k8s(&ctx, files, file_count);
- if (rc != 0) { /* log warning, continue */
- }
- cbm_log_info("pass.timing", "pass", "k8s", "elapsed_ms", itoa_buf((int)elapsed_ms(t)));
- if (check_cancel(p)) {
- rc = -1;
- goto seq_cleanup;
- }
-
- cbm_clock_gettime(CLOCK_MONOTONIC, &t);
- rc = cbm_pipeline_pass_calls(&ctx, files, file_count);
- if (rc != 0) {
- goto seq_cleanup;
- }
- cbm_log_info("pass.timing", "pass", "calls", "elapsed_ms", itoa_buf((int)elapsed_ms(t)));
- if (check_cancel(p)) {
- rc = -1;
- goto seq_cleanup;
- }
-
- cbm_clock_gettime(CLOCK_MONOTONIC, &t);
- rc = cbm_pipeline_pass_usages(&ctx, files, file_count);
- if (rc != 0) {
- goto seq_cleanup;
- }
- cbm_log_info("pass.timing", "pass", "usages", "elapsed_ms", itoa_buf((int)elapsed_ms(t)));
- if (check_cancel(p)) {
- rc = -1;
- goto seq_cleanup;
- }
-
- cbm_clock_gettime(CLOCK_MONOTONIC, &t);
- rc = cbm_pipeline_pass_semantic(&ctx, files, file_count);
- if (rc != 0) {
- goto seq_cleanup;
- }
- cbm_log_info("pass.timing", "pass", "semantic", "elapsed_ms", itoa_buf((int)elapsed_ms(t)));
- if (check_cancel(p)) {
- rc = -1;
- goto seq_cleanup;
- }
-
- seq_cleanup:
- /* Free cached extraction results */
- if (seq_cache) {
- for (int i = 0; i < file_count; i++) {
- if (seq_cache[i]) {
- cbm_free_result(seq_cache[i]);
- }
- }
- free(seq_cache);
- ctx.result_cache = NULL;
- }
- if (rc != 0) {
- goto cleanup;
- }
+ rc = (worker_count > 1 && file_count > MIN_FILES_FOR_PARALLEL)
+ ? run_parallel_pipeline(p, &ctx, files, file_count, worker_count, &t)
+ : run_sequential_pipeline(p, &ctx, files, file_count, &t);
+ if (check_cancel(p)) {
+ rc = -1;
}
-
- /* Post-extraction passes (shared by both parallel and sequential) */
- cbm_clock_gettime(CLOCK_MONOTONIC, &t);
- rc = cbm_pipeline_pass_tests(&ctx, files, file_count);
if (rc != 0) {
goto cleanup;
}
+
+ /* Post-extraction passes */
+ cbm_clock_gettime(CLOCK_MONOTONIC, &t);
+ rc = cbm_pipeline_pass_tests(&ctx, files, file_count);
cbm_log_info("pass.timing", "pass", "tests", "elapsed_ms", itoa_buf((int)elapsed_ms(t)));
+ if (rc == 0 && !check_cancel(p)) {
+ rc = run_githistory_and_httplinks(p, &ctx);
+ }
if (check_cancel(p)) {
rc = -1;
- goto cleanup;
}
-
- /* ── Fused post-passes: githistory (I/O) + httplinks (CPU) in parallel ── */
- {
- struct timespec t_gh;
- struct timespec t_hl;
- cbm_clock_gettime(CLOCK_MONOTONIC, &t_gh);
- cbm_clock_gettime(CLOCK_MONOTONIC, &t_hl);
-
- cbm_githistory_result_t gh_result = {0};
- cbm_thread_t gh_thread;
- bool gh_threaded = false;
- gh_compute_arg_t gh_arg = {.repo_path = ctx.repo_path, .result = &gh_result};
-
- /* Skip githistory entirely in fast mode */
- if (p->mode != CBM_MODE_FAST) {
-
- /* Only parallelize if we have multiple cores */
- if (cbm_default_worker_count(true) > 1) {
- if (cbm_thread_create(&gh_thread, 0, gh_compute_thread_fn, &gh_arg) == 0) {
- gh_threaded = true;
- }
- }
-
- /* If threading failed or single-core, run githistory serially first */
- if (!gh_threaded) {
- cbm_pipeline_githistory_compute(ctx.repo_path, &gh_result);
- cbm_log_info("pass.timing", "pass", "githistory_compute", "elapsed_ms",
- itoa_buf((int)elapsed_ms(t_gh)));
- }
- } else {
- cbm_log_info("pass.skip", "pass", "githistory", "reason", "fast_mode");
- }
-
- /* Run httplinks on main thread (CPU-bound) */
- rc = cbm_pipeline_pass_httplinks(&ctx);
- cbm_log_info("pass.timing", "pass", "httplinks", "elapsed_ms",
- itoa_buf((int)elapsed_ms(t_hl)));
-
- /* Wait for githistory thread to complete */
- if (gh_threaded) {
- cbm_thread_join(&gh_thread);
- cbm_log_info("pass.timing", "pass", "githistory_compute", "elapsed_ms",
- itoa_buf((int)elapsed_ms(t_gh)));
- }
-
- if (rc != 0) {
- free(gh_result.couplings);
- goto cleanup;
- }
- if (check_cancel(p)) {
- free(gh_result.couplings);
- rc = -1;
- goto cleanup;
- }
-
- /* Apply githistory edges (serial, writes to gbuf) */
- int gh_edges = 0;
- if (gh_result.count > 0) {
- gh_edges = cbm_pipeline_githistory_apply(&ctx, &gh_result);
- }
- cbm_log_info("pass.done", "pass", "githistory", "commits", itoa_buf(gh_result.commit_count),
- "edges", itoa_buf(gh_edges));
- free(gh_result.couplings);
+ if (rc != 0) {
+ goto cleanup;
}
/* Pre-dump passes (operate on graph buffer, not store) */
- if (!check_cancel(p)) {
- cbm_clock_gettime(CLOCK_MONOTONIC, &t);
- cbm_pipeline_pass_decorator_tags(p->gbuf, p->project_name);
- cbm_log_info("pass.timing", "pass", "decorator_tags", "elapsed_ms",
- itoa_buf((int)elapsed_ms(t)));
- }
+ run_predump_passes(p, &ctx);
+ /* Dump + persist */
if (!check_cancel(p)) {
- cbm_clock_gettime(CLOCK_MONOTONIC, &t);
- cbm_pipeline_pass_configlink(&ctx);
- cbm_log_info("pass.timing", "pass", "configlink", "elapsed_ms",
- itoa_buf((int)elapsed_ms(t)));
+ rc = dump_and_persist_hashes(p, files, file_count, &t);
}
-
- /* Match infra Route URLs (from YAML) to handler Routes (from code) */
- if (!check_cancel(p)) {
- cbm_clock_gettime(CLOCK_MONOTONIC, &t);
- cbm_pipeline_create_route_nodes(p->gbuf);
- cbm_log_info("pass.timing", "pass", "route_match", "elapsed_ms",
- itoa_buf((int)elapsed_ms(t)));
- }
-
- /* Direct dump: construct B-tree pages in C, fwrite() to .db file.
- * Zero SQLite library involvement — cbm_write_db() builds the binary
- * format directly from flat arrays. Atomic: writes .tmp then renames. */
- if (!check_cancel(p)) {
- cbm_clock_gettime(CLOCK_MONOTONIC, &t);
-
- const char *home = cbm_get_home_dir();
- char db_path[1024];
- if (p->db_path) {
- snprintf(db_path, sizeof(db_path), "%s", p->db_path);
- } else {
- if (!home) {
- home = cbm_tmpdir();
- }
- snprintf(db_path, sizeof(db_path), "%s/.cache/codebase-memory-mcp/%s.db", home,
- p->project_name);
- }
-
- /* Ensure parent directory exists (e.g. ~/.cache/codebase-memory-mcp/) */
- char db_dir[1024];
- snprintf(db_dir, sizeof(db_dir), "%s", db_path);
- char *last_slash = strrchr(db_dir, '/');
- if (last_slash) {
- *last_slash = '\0';
- cbm_mkdir_p(db_dir, 0755);
- }
-
- rc = cbm_gbuf_dump_to_sqlite(p->gbuf, db_path);
- if (rc != 0) {
- cbm_log_error("pipeline.err", "phase", "dump");
- goto cleanup;
- }
- cbm_log_info("pass.timing", "pass", "dump", "elapsed_ms", itoa_buf((int)elapsed_ms(t)));
-
- /* Persist file hashes so next run can use incremental path */
- cbm_store_t *hash_store = cbm_store_open_path(db_path);
- if (hash_store) {
- cbm_store_delete_file_hashes(hash_store, p->project_name);
- for (int i = 0; i < file_count; i++) {
- struct stat fst;
- if (stat(files[i].path, &fst) == 0) {
- int64_t mtime_ns;
-#ifdef __APPLE__
- mtime_ns = ((int64_t)fst.st_mtimespec.tv_sec * 1000000000LL) +
- (int64_t)fst.st_mtimespec.tv_nsec;
-#elif defined(_WIN32)
- mtime_ns = (int64_t)fst.st_mtime * 1000000000LL;
-#else
- mtime_ns =
- ((int64_t)fst.st_mtim.tv_sec * 1000000000LL) + (int64_t)fst.st_mtim.tv_nsec;
-#endif
- cbm_store_upsert_file_hash(hash_store, p->project_name, files[i].rel_path, "",
- mtime_ns, fst.st_size);
- }
- }
- cbm_store_close(hash_store);
- cbm_log_info("pass.timing", "pass", "persist_hashes", "files", itoa_buf(file_count));
- }
+ if (rc != 0) {
+ goto cleanup;
}
cbm_log_info("pipeline.done", "nodes", itoa_buf(cbm_gbuf_node_count(p->gbuf)), "edges",
diff --git a/src/pipeline/registry.c b/src/pipeline/registry.c
index 38b251d82..8ba603d91 100644
--- a/src/pipeline/registry.c
+++ b/src/pipeline/registry.c
@@ -457,11 +457,9 @@ static int filter_import_reachable(const char **candidates, int count, const cha
return n;
}
-// NOLINTBEGIN(bugprone-easily-swappable-parameters) — public API, parameter order is intentional
cbm_fuzzy_result_t cbm_registry_fuzzy_resolve(const cbm_registry_t *r, const char *callee_name,
const char *module_qn, const char **import_map_keys,
const char **import_map_vals, int import_map_count) {
- // NOLINTEND(bugprone-easily-swappable-parameters)
(void)import_map_keys;
cbm_fuzzy_result_t no_match = {{0}, false};
if (!r || !callee_name) {