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(python): join module-level calls to exact LSP resolutions

Martin Vogelbf5ea5a46089
diff --git a/internal/cbm/lsp/py_lsp.c b/internal/cbm/lsp/py_lsp.c
index 61fe89b2d..836e6557c 100644
--- a/internal/cbm/lsp/py_lsp.c
+++ b/internal/cbm/lsp/py_lsp.c
@@ -4933,7 +4933,9 @@ void py_lsp_process_file(PyLSPContext *ctx, TSNode root) {
uint32_t nc = rk.n;
const char *prev_func = ctx->enclosing_func_qn;
- ctx->enclosing_func_qn = cbm_arena_sprintf(ctx->arena, "%s.__module__", ctx->module_qn);
+ /* Extraction uses the file's Module QN at top level. Keep the caller
+ * arena-owned for cross-file APIs whose module_qn input may be temporary. */
+ ctx->enclosing_func_qn = cbm_arena_sprintf(ctx->arena, "%s", ctx->module_qn);
// Pass 1: execute top-level binding effects in source order. Function
// bodies remain deferred until the final module scope has been assembled.
for (uint32_t i = 0; i < nc && !ctx->eval_failure; i++) {
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index 95824f98e..b106c46d6 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -10693,6 +10693,205 @@ TEST(pipeline_issue1527_cross_memo_failure_fallback) {
PASS();
}
+static bool python_module_write_fixture_file(const char *root, const char *name, const char *text) {
+ char path[768];
+ int n = snprintf(path, sizeof(path), "%s/%s", root, name);
+ return n > 0 && (size_t)n < sizeof(path) && th_write_file(path, text) == 0;
+}
+
+/* An absent node, failed edge query, or heuristic edge must not satisfy this
+ * integration check. Exact source/target identities precede strategy checks. */
+static bool python_module_exact_lsp_edge(cbm_store_t *store, const char *project,
+ const char *caller, const char *caller_label,
+ const char *target, const char *target_label) {
+ char source_qn[768], target_qn[768];
+ int sn = snprintf(source_qn, sizeof(source_qn), "%s.%s", project, caller);
+ int tn = snprintf(target_qn, sizeof(target_qn), "%s.%s", project, target);
+ if (sn <= 0 || (size_t)sn >= sizeof(source_qn) || tn <= 0 || (size_t)tn >= sizeof(target_qn))
+ return false;
+ cbm_node_t source = {0}, destination = {0};
+ bool correct =
+ cbm_store_find_node_by_qn(store, project, source_qn, &source) == CBM_STORE_OK &&
+ cbm_store_find_node_by_qn(store, project, target_qn, &destination) == CBM_STORE_OK &&
+ source.id > 0 && destination.id > 0 && source.label && destination.label &&
+ strcmp(source.label, caller_label) == 0 && strcmp(destination.label, target_label) == 0;
+ cbm_edge_t *edges = NULL;
+ int edge_count = 0, hits = 0;
+ if (correct) {
+ correct = cbm_store_find_edges_by_source_type(store, source.id, "CALLS", &edges,
+ &edge_count) == CBM_STORE_OK;
+ for (int i = 0; correct && i < edge_count; i++) {
+ if (edges[i].source_id != source.id || edges[i].target_id != destination.id)
+ continue;
+ hits++;
+ const char *properties = edges[i].properties_json;
+ yyjson_doc *doc = properties ? yyjson_read(properties, strlen(properties), 0) : NULL;
+ const char *strategy =
+ doc ? yyjson_get_str(yyjson_obj_get(yyjson_doc_get_root(doc), "strategy")) : NULL;
+ correct = strategy && strncmp(strategy, "lsp_", 4) == 0;
+ if (doc)
+ yyjson_doc_free(doc);
+ }
+ }
+ cbm_store_free_edges(edges, edge_count);
+ cbm_node_free_fields(&source);
+ cbm_node_free_fields(&destination);
+ return correct && hits == 1;
+}
+
+static int python_module_callers_pipeline_case(bool parallel) {
+ static const char helpers[] = "def helper():\n"
+ " return 1\n"
+ "class Service:\n"
+ " def run(self):\n"
+ " return 2\n";
+ static const char app[] = "from helpers import helper, Service\n"
+ "def local_helper():\n"
+ " return 3\n"
+ "class Local:\n"
+ " def run(self):\n"
+ " return 4\n"
+ "class Controller:\n"
+ " def run(self):\n"
+ " helper()\n"
+ " method_service = Service()\n"
+ " method_service.run()\n"
+ "def control():\n"
+ " helper()\n"
+ " function_service = Service()\n"
+ " function_service.run()\n"
+ " local_helper()\n"
+ " function_local = Local()\n"
+ " function_local.run()\n"
+ "helper()\n"
+ "module_service = Service()\n"
+ "module_service.run()\n"
+ "local_helper()\n"
+ "module_local = Local()\n"
+ "module_local.run()\n";
+ static const struct {
+ const char *caller;
+ const char *caller_label;
+ const char *target;
+ const char *target_label;
+ } expected[] = {
+ {"app", "Module", "helpers.helper", "Function"},
+ {"app", "Module", "helpers.Service.run", "Method"},
+ {"app", "Module", "app.local_helper", "Function"},
+ {"app", "Module", "app.Local.run", "Method"},
+ {"app.control", "Function", "helpers.helper", "Function"},
+ {"app.control", "Function", "helpers.Service.run", "Method"},
+ {"app.control", "Function", "app.local_helper", "Function"},
+ {"app.control", "Function", "app.Local.run", "Method"},
+ {"app.Controller.run", "Method", "helpers.helper", "Function"},
+ {"app.Controller.run", "Method", "helpers.Service.run", "Method"},
+ {"pkg.__init__", "Module", "helpers.helper", "Function"},
+ };
+ char root[512];
+ int n = snprintf(root, sizeof(root), "%s/cbm-python-module-XXXXXX", cbm_tmpdir());
+ if (n <= 0 || (size_t)n >= sizeof(root) || !cbm_mkdtemp(root))
+ FAIL("failed to create Python module fixture");
+ char path[768];
+ n = snprintf(path, sizeof(path), "%s/pkg", root);
+ bool written = n > 0 && (size_t)n < sizeof(path);
+ if (written) {
+ cbm_mkdir_p(path, 0755);
+ written = python_module_write_fixture_file(root, "helpers.py", helpers) &&
+ python_module_write_fixture_file(root, "app.py", app) &&
+ python_module_write_fixture_file(root, "pkg/__init__.py",
+ "from helpers import helper\n"
+ "PACKAGE_VALUE = helper()\n");
+ }
+ for (int i = 0; written && parallel && i < 52; i++) {
+ char name[64];
+ snprintf(name, sizeof(name), "module_pad_%02d.py", i);
+ written = python_module_write_fixture_file(root, name, "padding = 0\n");
+ }
+ if (!written) {
+ th_rmtree(root);
+ FAIL("failed to write Python module fixture");
+ }
+
+ const char *keys[] = {"CBM_WORKERS", "CBM_INDEX_SINGLE_THREAD"};
+ char *saved[2] = {0};
+ bool saved_ok = true;
+ for (int i = 0; i < 2; i++) {
+ const char *old = getenv(keys[i]);
+ saved[i] = old ? strdup(old) : NULL;
+ saved_ok = saved_ok && (!old || saved[i]);
+ }
+ if (!saved_ok) {
+ free(saved[0]);
+ free(saved[1]);
+ th_rmtree(root);
+ FAIL("failed to save Python module fixture environment");
+ }
+ int set_rc = cbm_setenv(keys[0], parallel ? "4" : "1", 1);
+ set_rc |= parallel ? cbm_unsetenv(keys[1]) : cbm_setenv(keys[1], "1", 1);
+ n = snprintf(path, sizeof(path), "%s/module.db", root);
+ cbm_pipeline_t *pipeline = set_rc == 0 && n > 0 && (size_t)n < sizeof(path)
+ ? cbm_pipeline_new(root, path, CBM_MODE_FULL)
+ : NULL;
+ int run_rc = pipeline ? cbm_pipeline_run(pipeline) : -1;
+ cbm_file_error_t *errors = NULL;
+ int error_count = 0;
+ cbm_pipeline_get_file_errors(pipeline, &errors, &error_count);
+ cbm_store_t *store = run_rc == 0 ? cbm_store_open_path(path) : NULL;
+ bool correct = store != NULL;
+ int file_count = 0;
+ if (store) {
+ const char *project = cbm_pipeline_project_name(pipeline);
+ cbm_node_t *files = NULL;
+ correct = project &&
+ cbm_store_find_nodes_by_label(store, project, "File", &files, &file_count) ==
+ CBM_STORE_OK &&
+ file_count == (parallel ? 55 : 3);
+ cbm_store_free_nodes(files, file_count);
+ for (size_t i = 0; i < sizeof(expected) / sizeof(expected[0]); i++) {
+ bool edge = project && python_module_exact_lsp_edge(
+ store, project, expected[i].caller, expected[i].caller_label,
+ expected[i].target, expected[i].target_label);
+ correct = correct && edge;
+ }
+ /* Neither the ordinary nor package Module should acquire a fake
+ * __module__ owner node. A failed lookup query is not absence proof. */
+ const char *phantoms[] = {"app.__module__", "pkg.__init__.__module__"};
+ for (int i = 0; project && i < 2; i++) {
+ char qn[768];
+ n = snprintf(qn, sizeof(qn), "%s.%s", project, phantoms[i]);
+ cbm_node_t node = {0};
+ bool absent =
+ n > 0 && (size_t)n < sizeof(qn) &&
+ cbm_store_find_node_by_qn(store, project, qn, &node) == CBM_STORE_NOT_FOUND;
+ correct = correct && absent;
+ cbm_node_free_fields(&node);
+ }
+ cbm_store_close(store);
+ }
+ cbm_pipeline_free(pipeline);
+ int restore_rc = 0;
+ for (int i = 0; i < 2; i++) {
+ restore_rc |= saved[i] ? cbm_setenv(keys[i], saved[i], 1) : cbm_unsetenv(keys[i]);
+ free(saved[i]);
+ }
+ int cleanup_rc = th_rmtree(root);
+ ASSERT_EQ(set_rc, 0);
+ ASSERT_EQ(restore_rc, 0);
+ ASSERT_EQ(cleanup_rc, 0);
+ ASSERT_EQ(run_rc, 0);
+ ASSERT_EQ(error_count, 0);
+ ASSERT_TRUE(correct);
+ PASS();
+}
+
+TEST(pipeline_python_module_callers_sequential) {
+ return python_module_callers_pipeline_case(false);
+}
+
+TEST(pipeline_python_module_callers_parallel) {
+ return python_module_callers_pipeline_case(true);
+}
+
/* #1277: typed instance fields of a Python class imported from another file.
* Counts CALLS edges caller -> callee (QN suffixes) whose properties name an
* LSP strategy. */
@@ -20381,6 +20580,8 @@ TEST(pipeline_calls_args_overlong_argument_stops_cleanly) {
}
SUITE(pipeline) {
+ RUN_TEST(pipeline_python_module_callers_sequential);
+ RUN_TEST(pipeline_python_module_callers_parallel);
RUN_TEST(pipeline_issue1527_python_raw_memo_failure_sequential);
RUN_TEST(pipeline_issue1527_python_raw_memo_failure_parallel);
RUN_TEST(pipeline_issue1527_python_cross_memo_failure_sequential);
diff --git a/tests/test_py_lsp.c b/tests/test_py_lsp.c
index e06adab72..6a7bfa114 100644
--- a/tests/test_py_lsp.c
+++ b/tests/test_py_lsp.c
@@ -2824,7 +2824,226 @@ TEST(pylsp_issue1527_depth_failure_reports_all_entry_points) {
PASS();
}
+/* Join one known source occurrence, not merely two equal aggregate counts.
+ * Changing the owner or range must not borrow a neighbouring resolution. */
+static bool pylsp_module_site_joins(const CBMCallArray *calls, const CBMResolvedCallArray *resolved,
+ const char *source, const char *site_text, const char *caller,
+ const char *target) {
+ const char *site = strstr(source, site_text);
+ if (!site || strstr(site + 1, site_text))
+ return false;
+ uint32_t start = (uint32_t)(site - source);
+ uint32_t end = start + (uint32_t)strlen(site_text);
+ const CBMCall *call = NULL;
+ int call_count = 0;
+ for (int i = 0; i < calls->count; i++) {
+ const CBMCall *candidate = &calls->items[i];
+ if (candidate->enclosing_func_qn && strcmp(candidate->enclosing_func_qn, caller) == 0 &&
+ candidate->site_start_byte == start && candidate->site_end_byte == end) {
+ call = candidate;
+ call_count++;
+ }
+ }
+ if (call_count != 1)
+ return false;
+ const CBMResolvedCall *hit = cbm_pipeline_find_lsp_resolution(resolved, call, false);
+ if (!hit || !hit->caller_qn || strcmp(hit->caller_qn, caller) != 0 || !hit->callee_qn ||
+ strcmp(hit->callee_qn, target) != 0 || hit->kind != CBM_RESOLVED_INVOCATION ||
+ hit->site_start_byte != start || hit->site_end_byte != end ||
+ hit->source_origin != call->source_origin)
+ return false;
+ int semantic_count = 0;
+ for (int i = 0; i < resolved->count; i++) {
+ const CBMResolvedCall *candidate = &resolved->items[i];
+ if (candidate->kind == CBM_RESOLVED_INVOCATION && candidate->caller_qn &&
+ strcmp(candidate->caller_qn, caller) == 0 && candidate->site_start_byte == start &&
+ candidate->site_end_byte == end)
+ semantic_count++;
+ }
+ CBMCall wrong = *call;
+ wrong.enclosing_func_qn = "unrelated.module.caller";
+ bool rejects_owner = cbm_pipeline_find_lsp_resolution(resolved, &wrong, false) == NULL;
+ wrong = *call;
+ wrong.site_start_byte++;
+ return semantic_count == 1 && rejects_owner &&
+ cbm_pipeline_find_lsp_resolution(resolved, &wrong, false) == NULL;
+}
+
+static bool pylsp_module_has_def(const CBMFileResult *r, const char *qn) {
+ for (int i = 0; i < r->defs.count; i++) {
+ if (r->defs.items[i].qualified_name && strcmp(r->defs.items[i].qualified_name, qn) == 0)
+ return true;
+ }
+ return false;
+}
+
+TEST(pylsp_module_callers_join_exact_raw_occurrences) {
+ static const char source[] = "class Alpha:\n"
+ " def render(self):\n"
+ " return 1\n"
+ "class Beta:\n"
+ " def render(self):\n"
+ " return 2\n"
+ "def helper():\n"
+ " return 3\n"
+ "def control():\n"
+ " function_a = Alpha()\n"
+ " function_a.render()\n"
+ " helper()\n"
+ "class Controller:\n"
+ " def control(self):\n"
+ " method_b = Beta()\n"
+ " method_b.render()\n"
+ "module_a = Alpha()\n"
+ "module_b = Beta()\n"
+ "module_a.render()\n"
+ "module_b.render()\n";
+ CBMFileResult *r = cbm_extract_file(source, (int)strlen(source), CBM_LANG_PYTHON, "test",
+ "app.py", 0, NULL, NULL);
+ bool correct = r && !r->has_error && !r->parse_incomplete && !r->lsp_skipped && r->module_qn &&
+ strcmp(r->module_qn, "test.app") == 0;
+ if (correct) {
+ correct =
+ pylsp_module_has_def(r, "test.app.Alpha.render") &&
+ pylsp_module_has_def(r, "test.app.Beta.render") &&
+ pylsp_module_has_def(r, "test.app.control") &&
+ pylsp_module_has_def(r, "test.app.Controller.control") &&
+ pylsp_module_site_joins(&r->calls, &r->resolved_calls, source, "module_a.render()",
+ "test.app", "test.app.Alpha.render") &&
+ pylsp_module_site_joins(&r->calls, &r->resolved_calls, source, "module_b.render()",
+ "test.app", "test.app.Beta.render") &&
+ pylsp_module_site_joins(&r->calls, &r->resolved_calls, source, "function_a.render()",
+ "test.app.control", "test.app.Alpha.render") &&
+ pylsp_module_site_joins(&r->calls, &r->resolved_calls, source, "method_b.render()",
+ "test.app.Controller.control", "test.app.Beta.render");
+ }
+ if (r)
+ cbm_free_result(r);
+ ASSERT_TRUE(correct);
+
+ /* Package symbol-scope normalization is separate. A builtin isolates
+ * this package Module's raw caller identity from that remaining issue. */
+ static const char init_source[] = "PACKAGE_SIZE = len([1])\n";
+ r = cbm_extract_file(init_source, (int)strlen(init_source), CBM_LANG_PYTHON, "test",
+ "pkg/__init__.py", 0, NULL, NULL);
+ correct = r && !r->has_error && !r->parse_incomplete && !r->lsp_skipped && r->module_qn &&
+ strcmp(r->module_qn, "test.pkg.__init__") == 0 &&
+ pylsp_module_has_def(r, "builtins.len") &&
+ pylsp_module_site_joins(&r->calls, &r->resolved_calls, init_source, "len([1])",
+ "test.pkg.__init__", "builtins.len");
+ if (r)
+ cbm_free_result(r);
+ ASSERT_TRUE(correct);
+ PASS();
+}
+
+TEST(pylsp_module_named_lambda_keeps_exact_dunder_pair) {
+ static const char source[] = "class Number:\n"
+ " def __add__(self, other):\n"
+ " return self\n"
+ "module_number = Number()\n"
+ "add = lambda left, right: left + right\n"
+ "first = add(module_number, module_number)\n"
+ "second = add(module_number, module_number)\n";
+ CBMFileResult *r = cbm_extract_file(source, (int)strlen(source), CBM_LANG_PYTHON, "test",
+ "app.py", 0, NULL, NULL);
+ bool correct = r && !r->has_error && !r->parse_incomplete && !r->lsp_skipped &&
+ pylsp_module_has_def(r, "test.app.Number.__add__") &&
+ pylsp_module_site_joins(&r->calls, &r->resolved_calls, source, "left + right",
+ "test.app.<lambda>", "test.app.Number.__add__");
+ if (r)
+ cbm_free_result(r);
+ ASSERT_TRUE(correct);
+ PASS();
+}
+
+TEST(pylsp_module_cross_callers_own_input_and_join_exact_sites) {
+ static const char source[] = "from helpers import Alpha, Beta\n"
+ "module_a = Alpha()\n"
+ "module_b = Beta()\n"
+ "module_a.render()\n"
+ "module_b.render()\n";
+ static const char *const paths[] = {"app.py", "pkg/__init__.py"};
+ static const char *const modules[] = {"test.app", "test.pkg.__init__"};
+ CBMLSPDef defs[] = {
+ {.qualified_name = "test.helpers.Alpha",
+ .short_name = "Alpha",
+ .label = "Class",
+ .def_module_qn = "test.helpers",
+ .lang = CBM_LANG_PYTHON},
+ {.qualified_name = "test.helpers.Alpha.render",
+ .short_name = "render",
+ .label = "Method",
+ .receiver_type = "test.helpers.Alpha",
+ .def_module_qn = "test.helpers",
+ .return_types = "int",
+ .lang = CBM_LANG_PYTHON},
+ {.qualified_name = "test.helpers.Beta",
+ .short_name = "Beta",
+ .label = "Class",
+ .def_module_qn = "test.helpers",
+ .lang = CBM_LANG_PYTHON},
+ {.qualified_name = "test.helpers.Beta.render",
+ .short_name = "render",
+ .label = "Method",
+ .receiver_type = "test.helpers.Beta",
+ .def_module_qn = "test.helpers",
+ .return_types = "int",
+ .lang = CBM_LANG_PYTHON},
+ };
+ const char *import_names[] = {"Alpha", "Beta"};
+ const char *import_qns[] = {"test.helpers.Alpha", "test.helpers.Beta"};
+ bool correct = true;
+ for (int path = 0; path < 2; path++) {
+ CBMFileResult *r = cbm_extract_file(source, (int)strlen(source), CBM_LANG_PYTHON, "test",
+ paths[path], 0, NULL, NULL);
+ if (!r || r->has_error || r->parse_incomplete || r->lsp_skipped || !r->module_qn ||
+ strcmp(r->module_qn, modules[path]) != 0) {
+ correct = false;
+ if (r)
+ cbm_free_result(r);
+ continue;
+ }
+ for (int prebuilt = 0; prebuilt < 2; prebuilt++) {
+ CBMArena arena, shared;
+ cbm_arena_init(&arena);
+ cbm_arena_init(&shared);
+ CBMTypeRegistry *base = prebuilt ? cbm_py_build_cross_registry(&shared, defs, 4) : NULL;
+ CBMTypeRegistry overlay;
+ cbm_registry_init(&overlay, &arena);
+ overlay.fallback = base;
+ char module_input[64];
+ snprintf(module_input, sizeof(module_input), "%s", modules[path]);
+ CBMResolvedCallArray out = {0};
+ CBMLSPStatus status =
+ prebuilt ? cbm_run_py_lsp_cross_with_registry_status(
+ &arena, source, (int)strlen(source), module_input, &overlay,
+ import_names, import_qns, 2, NULL, &out, NULL)
+ : cbm_run_py_lsp_cross_status(&arena, source, (int)strlen(source),
+ module_input, defs, 4, import_names,
+ import_qns, 2, NULL, &out, NULL);
+ /* Returned caller strings must outlive the caller's input buffer.
+ * Overwrite live storage, avoiding a dangling-pointer test. */
+ memset(module_input, 'x', strlen(module_input));
+ correct = correct && (!prebuilt || (base && base->read_only)) &&
+ status == CBM_LSP_COMPLETE &&
+ pylsp_module_site_joins(&r->calls, &out, source, "module_a.render()",
+ modules[path], "test.helpers.Alpha.render") &&
+ pylsp_module_site_joins(&r->calls, &out, source, "module_b.render()",
+ modules[path], "test.helpers.Beta.render");
+ cbm_arena_destroy(&arena);
+ cbm_arena_destroy(&shared);
+ }
+ cbm_free_result(r);
+ }
+ ASSERT_TRUE(correct);
+ PASS();
+}
+
SUITE(py_lsp) {
+ RUN_TEST(pylsp_module_callers_join_exact_raw_occurrences);
+ RUN_TEST(pylsp_module_named_lambda_keeps_exact_dunder_pair);
+ RUN_TEST(pylsp_module_cross_callers_own_input_and_join_exact_sites);
RUN_TEST(pylsp_issue1527_depth_failure_reports_all_entry_points);
RUN_TEST(pylsp_issue1527_cross_memo_failure_fallback_reports_error);
RUN_TEST(pylsp_issue1527_cross_memo_failure_preserves_completed_rows);