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.

fix(pipeline): link methods to their type after every file is registered

Martin Vogeld2ea417ec2fc
diff --git a/src/pipeline/pass_definitions.c b/src/pipeline/pass_definitions.c
index aecd91d99..0f95c7989 100644
--- a/src/pipeline/pass_definitions.c
+++ b/src/pipeline/pass_definitions.c
@@ -412,11 +412,27 @@ static void pd_add_file_doc(const cbm_gbuf_node_t *file_node, const char *doc) {
cbm_free(CBM_MEM_CLASS_GBUF_STRING, neu);
}
-/* Process one definition: create node, register, DEFINES + DEFINES_METHOD edges.
- * `file_doc`, the file's own doc (passed with its first def only), goes on the
- * File node. */
+/* DEFINES_METHOD from a method's type, and the parent edge of a diagram
+ * element. Both look the parent up by QN, so the parent's node must exist. */
+static void link_def_parents(cbm_pipeline_ctx_t *ctx, const CBMDefinition *def, int64_t node_id) {
+ if (node_id <= 0) {
+ return;
+ }
+ 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) {
+ cbm_gbuf_insert_edge(ctx->gbuf, parent->id, node_id, "DEFINES_METHOD", "{}");
+ }
+ }
+ cbm_pipeline_diagram_parent_edge(ctx, def, node_id);
+}
+
+/* Process one definition: create node, register, DEFINES edge. `file_doc`,
+ * the file's own doc (passed with its first def only), goes on the File node.
+ * `link_parents` links its DEFINES_METHOD / diagram parent right away; with
+ * the result cache the pass links them once every file is registered. */
static void process_def(cbm_pipeline_ctx_t *ctx, const CBMDefinition *def, const char *rel,
- const char *file_doc, CBMLanguage lang) {
+ const char *file_doc, CBMLanguage lang, bool link_parents) {
if (!def->qualified_name || !def->name) {
return;
}
@@ -451,13 +467,39 @@ static void process_def(cbm_pipeline_ctx_t *ctx, const CBMDefinition *def, const
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", "{}");
+ if (link_parents) {
+ link_def_parents(ctx, def, node_id);
+ }
+}
+
+/* Parent links of the definitions cached in Phase 1, made once EVERY file's
+ * nodes exist. A method is often defined in a file the directory walk returns
+ * before its type's file (a Go method in another file of its package, an
+ * out-of-line C++ method in foo.cpp before foo.h); linking it while files were
+ * still being registered found no parent, so the sequential pipeline lost the
+ * edge -- and the IMPLEMENTS / OVERRIDE edges derived from it -- that the
+ * parallel registry build (pass_parallel.c register_and_link_def) keeps. */
+static void link_cached_def_parents(cbm_pipeline_ctx_t *ctx, CBMFileResult **cache,
+ int file_count) {
+ for (int i = 0; i < file_count; i++) {
+ if (cbm_pipeline_check_cancel(ctx)) {
+ return;
+ }
+ const CBMFileResult *result = cache[i];
+ if (!result) {
+ continue;
+ }
+ for (int d = 0; d < result->defs.count; d++) {
+ const CBMDefinition *def = &result->defs.items[d];
+ if (!def->parent_class || !def->qualified_name) {
+ continue;
+ }
+ const cbm_gbuf_node_t *node = cbm_gbuf_find_by_qn(ctx->gbuf, def->qualified_name);
+ if (node) {
+ link_def_parents(ctx, def, node->id);
+ }
}
}
- cbm_pipeline_diagram_parent_edge(ctx, def, node_id);
}
/* Create Channel nodes + EMITS / LISTENS_ON edges for one file's channels.
@@ -1022,7 +1064,7 @@ int cbm_pipeline_pass_definitions(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
/* Create nodes for each definition */
const char *file_doc = result->module_doc; /* goes with the first def */
for (int d = 0; d < result->defs.count; d++) {
- process_def(ctx, &result->defs.items[d], rel, file_doc, lang);
+ process_def(ctx, &result->defs.items[d], rel, file_doc, lang, !local_cache);
file_doc = NULL;
total_defs++;
}
@@ -1046,6 +1088,12 @@ int cbm_pipeline_pass_definitions(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
}
}
+ /* Every file's nodes exist now: link methods and diagram elements to
+ * their parents (without the cache each file linked its own above). */
+ if (local_cache) {
+ link_cached_def_parents(ctx, local_cache, file_count);
+ }
+
/* Phase 2: now that all extraction results are cached and Module
* nodes for every file are in the graph, walk the cache again to
* create IMPORTS / channel edges. Imports resolve against the full
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index b52513ace..78bd9c97e 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -2536,6 +2536,117 @@ TEST(pipeline_js_route_not_bound_to_python_builtin_incremental) {
PASS();
}
+/* FD1: a method defined in another file than its type must get its
+ * DEFINES_METHOD edge on every venue. The sequential definitions pass linked
+ * the edge while it was still registering files, so a method whose type lived
+ * in a file the directory walk returns LATER found no parent: on helm and
+ * prometheus the sequential venue (CBM_WORKERS=1) lost 236 DEFINES_METHOD
+ * edges, and the IMPLEMENTS / OVERRIDE edges derived from them, that the
+ * parallel venue keeps. Each of the two files holds one type and the method of
+ * the OTHER file's type, so whichever order the walk returns them in, one
+ * method precedes its type: the case fails without depending on directory
+ * order. */
+typedef struct {
+ int run_rc;
+ bool store_opened;
+ int alpha_method; /* FdmAlpha -[DEFINES_METHOD]-> FdmAlphaGet */
+ int beta_method; /* FdmBeta -[DEFINES_METHOD]-> FdmBetaGet */
+} CrossFileMethodObservation;
+
+static const char FDM_X_GO[] = "package fdm\n"
+ "\n"
+ "type FdmBeta struct{ n int }\n"
+ "\n"
+ "func (a *FdmAlpha) FdmAlphaGet() int { return a.n }\n";
+
+static const char FDM_Y_GO[] = "package fdm\n"
+ "\n"
+ "type FdmAlpha struct{ n int }\n"
+ "\n"
+ "func (b *FdmBeta) FdmBetaGet() int { return b.n }\n";
+
+static int write_cross_file_method_fixture(const char *dir, int pad) {
+ char path[512];
+ snprintf(path, sizeof(path), "%s/x.go", dir);
+ if (th_write_file(path, FDM_X_GO) != 0) {
+ return -1;
+ }
+ snprintf(path, sizeof(path), "%s/y.go", dir);
+ if (th_write_file(path, FDM_Y_GO) != 0) {
+ return -1;
+ }
+ for (int i = 0; i < pad; i++) {
+ char source[128];
+ snprintf(path, sizeof(path), "%s/fdm_pad_%02d.go", dir, i);
+ snprintf(source, sizeof(source), "package fdm\n\nfunc fdmPad%02d() int { return %d }\n", i,
+ i);
+ if (th_write_file(path, source) != 0) {
+ return -1;
+ }
+ }
+ return 0;
+}
+
+static CrossFileMethodObservation run_cross_file_method_case(bool parallel, int pad) {
+ CrossFileMethodObservation o = {.run_rc = -1, .alpha_method = -1, .beta_method = -1};
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_fd1_method_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ return o;
+ }
+ if (write_cross_file_method_fixture(tmp, pad) == 0) {
+ XlangEnvSave save = xlang_env_select(parallel);
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/fdm.db", tmp);
+ cbm_pipeline_t *pipeline = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ if (pipeline) {
+ o.run_rc = cbm_pipeline_run(pipeline);
+ const char *project = cbm_pipeline_project_name(pipeline);
+ cbm_store_t *store = cbm_store_open_path(db_path);
+ o.store_opened = store != NULL;
+ if (store && project) {
+ o.alpha_method =
+ named_edge_count(store, project, "DEFINES_METHOD", "FdmAlpha", "FdmAlphaGet");
+ o.beta_method =
+ named_edge_count(store, project, "DEFINES_METHOD", "FdmBeta", "FdmBetaGet");
+ }
+ if (store) {
+ cbm_store_close(store);
+ }
+ cbm_pipeline_free(pipeline);
+ }
+ xlang_env_restore(&save);
+ }
+ th_rmtree(tmp);
+ return o;
+}
+
+TEST(pipeline_cross_file_method_defines_method_sequential) {
+ CrossFileMethodObservation o = run_cross_file_method_case(false, 0);
+ ASSERT_EQ(o.run_rc, 0);
+ ASSERT_TRUE(o.store_opened);
+ ASSERT_EQ(o.alpha_method, 1);
+ ASSERT_EQ(o.beta_method, 1);
+ PASS();
+}
+
+/* The same input through both resolvers: 55 fillers exceed
+ * MIN_FILES_FOR_PARALLEL, CBM_INDEX_SINGLE_THREAD keeps the first run
+ * sequential and CBM_WORKERS sends the second through pass_parallel.c. */
+TEST(pipeline_cross_file_method_defines_method_venue_parity) {
+ CrossFileMethodObservation seq = run_cross_file_method_case(false, 55);
+ CrossFileMethodObservation par = run_cross_file_method_case(true, 55);
+ ASSERT_EQ(seq.run_rc, 0);
+ ASSERT_EQ(par.run_rc, 0);
+ ASSERT_TRUE(seq.store_opened);
+ ASSERT_TRUE(par.store_opened);
+ ASSERT_EQ(par.alpha_method, 1);
+ ASSERT_EQ(par.beta_method, 1);
+ ASSERT_EQ(seq.alpha_method, par.alpha_method);
+ ASSERT_EQ(seq.beta_method, par.beta_method);
+ PASS();
+}
+
static bool test_buffer_appendf(char *buffer, size_t capacity, size_t *used, const char *format,
...) {
if (!buffer || !used || *used >= capacity) {
@@ -23593,6 +23704,8 @@ SUITE(pipeline) {
RUN_TEST(pipeline_js_route_not_bound_to_python_builtin_sequential);
RUN_TEST(pipeline_js_route_not_bound_to_python_builtin_parallel);
RUN_TEST(pipeline_js_route_not_bound_to_python_builtin_incremental);
+ RUN_TEST(pipeline_cross_file_method_defines_method_sequential);
+ RUN_TEST(pipeline_cross_file_method_defines_method_venue_parity);
RUN_TEST(pipeline_objectscript_export_aggregate_exceeds_arena_block_table);
#if defined(CBM_COVERAGE_MARKER_TEST_API) && CBM_COVERAGE_MARKER_TEST_API
RUN_TEST(pipeline_objectscript_export_range_join_keeps_one_trailing_marker);