Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
feat(py_lsp): cross-file resolution + batch entry point
Martin Vogelf4f9ed47f6d7
diff --git a/internal/cbm/lsp/py_lsp.c b/internal/cbm/lsp/py_lsp.cindex dcbdb40af..6e375946f 100644--- a/internal/cbm/lsp/py_lsp.c+++ b/internal/cbm/lsp/py_lsp.c@@ -869,7 +869,100 @@ void cbm_run_py_lsp(CBMArena* arena, CBMFileResult* result,py_lsp_process_file(&ctx, root);}-/* ── Cross-file + batch — Phase 9 fills these in ──────────────── */+/* ── Cross-file + batch ───────────────────────────────────────── */++extern const TSLanguage* tree_sitter_python(void);++/* Split a "|"-separated list into a NULL-terminated array of arena copies. */+static const char** py_split_pipe(CBMArena* arena, const char* text) {+ if (!text || !text[0]) return NULL;+ int count = 1;+ for (const char* p = text; *p; p++) if (*p == '|') count++;+ const char** out = (const char**)cbm_arena_alloc(arena,+ (size_t)(count + 1) * sizeof(const char*));+ if (!out) return NULL;+ int idx = 0;+ const char* start = text;+ for (const char* p = text; ; p++) {+ if (*p == '|' || *p == '\0') {+ size_t n = (size_t)(p - start);+ char* s = (char*)cbm_arena_alloc(arena, n + 1);+ if (!s) return NULL;+ memcpy(s, start, n);+ s[n] = '\0';+ out[idx++] = s;+ if (*p == '\0') break;+ start = p + 1;+ }+ }+ out[idx] = NULL;+ return out;+}++/* Build a registry from CBMLSPDef[] supplied by the caller — covers both+ * the source file's own defs and cross-file referenced defs. */+static void py_register_lsp_defs(CBMArena* arena, CBMTypeRegistry* reg,+ CBMLSPDef* defs, int def_count) {+ for (int i = 0; i < def_count; i++) {+ CBMLSPDef* d = &defs[i];+ if (!d->qualified_name || !d->short_name || !d->label) continue;++ if (strcmp(d->label, "Type") == 0 || strcmp(d->label, "Class") == 0 ||+ strcmp(d->label, "Interface") == 0 || strcmp(d->label, "Protocol") == 0) {+ CBMRegisteredType rt;+ memset(&rt, 0, sizeof(rt));+ rt.qualified_name = cbm_arena_strdup(arena, d->qualified_name);+ rt.short_name = cbm_arena_strdup(arena, d->short_name);+ rt.is_interface = d->is_interface ||+ strcmp(d->label, "Interface") == 0 ||+ strcmp(d->label, "Protocol") == 0;+ rt.embedded_types = py_split_pipe(arena, d->embedded_types);+ if (d->method_names_str && d->method_names_str[0]) {+ rt.method_names = py_split_pipe(arena, d->method_names_str);+ }+ cbm_registry_add_type(reg, rt);+ }++ if (strcmp(d->label, "Function") == 0 || strcmp(d->label, "Method") == 0) {+ CBMRegisteredFunc rf;+ memset(&rf, 0, sizeof(rf));+ rf.qualified_name = cbm_arena_strdup(arena, d->qualified_name);+ rf.short_name = cbm_arena_strdup(arena, d->short_name);++ // Build FUNC type from "|"-separated return types.+ const char** ret_strs = py_split_pipe(arena, d->return_types);+ const CBMType** ret_types = NULL;+ if (ret_strs) {+ int n = 0;+ while (ret_strs[n]) n++;+ if (n > 0) {+ ret_types = (const CBMType**)cbm_arena_alloc(arena,+ (size_t)(n + 1) * sizeof(const CBMType*));+ for (int j = 0; j < n; j++) {+ ret_types[j] = cbm_type_named(arena, ret_strs[j]);+ }+ ret_types[n] = NULL;+ }+ }+ rf.signature = cbm_type_func(arena, NULL, NULL, ret_types);++ if (strcmp(d->label, "Method") == 0 && d->receiver_type && d->receiver_type[0]) {+ rf.receiver_type = cbm_arena_strdup(arena, d->receiver_type);+ if (!cbm_registry_lookup_type(reg, rf.receiver_type)) {+ CBMRegisteredType auto_t;+ memset(&auto_t, 0, sizeof(auto_t));+ auto_t.qualified_name = rf.receiver_type;+ const char* dot = strrchr(d->receiver_type, '.');+ auto_t.short_name = dot+ ? cbm_arena_strdup(arena, dot + 1)+ : rf.receiver_type;+ cbm_registry_add_type(reg, auto_t);+ }+ }+ cbm_registry_add_func(reg, rf);+ }+ }+}void cbm_run_py_lsp_cross(CBMArena* arena,@@ -879,27 +972,87 @@ void cbm_run_py_lsp_cross(const char** import_names, const char** import_qns, int import_count,TSTree* cached_tree,CBMResolvedCallArray* out) {- (void)arena;- (void)source;- (void)source_len;- (void)module_qn;- (void)defs;- (void)def_count;- (void)import_names;- (void)import_qns;- (void)import_count;- (void)cached_tree;- (void)out;+ if (!arena || !source || source_len <= 0 || !out) return;++ TSParser* parser = NULL;+ TSTree* tree = cached_tree;+ bool owns_tree = false;+ if (!tree) {+ parser = ts_parser_new();+ if (!parser) return;+ ts_parser_set_language(parser, tree_sitter_python());+ tree = ts_parser_parse_string(parser, NULL, source, (uint32_t)source_len);+ owns_tree = true;+ if (!tree) { ts_parser_delete(parser); return; }+ }+ TSNode root = ts_tree_root_node(tree);++ CBMTypeRegistry reg;+ cbm_registry_init(®, arena);+ cbm_python_stdlib_register(®, arena);+ py_register_lsp_defs(arena, ®, defs, def_count);++ PyLSPContext ctx;+ py_lsp_init(&ctx, arena, source, source_len, ®, module_qn, out);+ for (int i = 0; i < import_count; i++) {+ if (import_names && import_qns && import_names[i] && import_qns[i]) {+ py_lsp_add_import(&ctx, import_names[i], import_qns[i]);+ }+ }+ py_lsp_process_file(&ctx, root);++ if (owns_tree && tree) ts_tree_delete(tree);+ if (parser) ts_parser_delete(parser);}void cbm_batch_py_lsp_cross(CBMArena* arena,CBMBatchPyLSPFile* files, int file_count,CBMResolvedCallArray* out) {- (void)arena;- (void)files;- (void)file_count;- (void)out;+ if (!arena || !files || file_count <= 0 || !out) return;++ for (int f = 0; f < file_count; f++) {+ CBMBatchPyLSPFile* file = &files[f];+ memset(&out[f], 0, sizeof(CBMResolvedCallArray));+ if (!file->source || file->source_len <= 0) continue;++ CBMArena file_arena;+ cbm_arena_init(&file_arena);++ CBMResolvedCallArray file_out;+ memset(&file_out, 0, sizeof(file_out));++ cbm_run_py_lsp_cross(&file_arena,+ file->source, file->source_len, file->module_qn,+ file->defs, file->def_count,+ file->import_names, file->import_qns, file->import_count,+ file->cached_tree, &file_out);++ if (file_out.count > 0) {+ out[f].count = file_out.count;+ out[f].items = (CBMResolvedCall*)cbm_arena_alloc(arena,+ (size_t)file_out.count * sizeof(CBMResolvedCall));+ if (out[f].items) {+ for (int j = 0; j < file_out.count; j++) {+ CBMResolvedCall* src = &file_out.items[j];+ CBMResolvedCall* dst = &out[f].items[j];+ dst->caller_qn = src->caller_qn+ ? cbm_arena_strdup(arena, src->caller_qn) : NULL;+ dst->callee_qn = src->callee_qn+ ? cbm_arena_strdup(arena, src->callee_qn) : NULL;+ dst->strategy = src->strategy+ ? cbm_arena_strdup(arena, src->strategy) : NULL;+ dst->confidence = src->confidence;+ dst->reason = src->reason+ ? cbm_arena_strdup(arena, src->reason) : NULL;+ }+ } else {+ out[f].count = 0;+ }+ }++ cbm_arena_destroy(&file_arena);+ }}/* ── Stdlib stub — Phase 10 replaces with auto-generated body ─── */diff --git a/tests/test_py_lsp.c b/tests/test_py_lsp.cindex b02e7b183..68234988a 100644--- a/tests/test_py_lsp.c+++ b/tests/test_py_lsp.c@@ -506,6 +506,168 @@ TEST(pylsp_pep695_generic_class) {PASS();}+/* ── Phase 9 — cross-file resolution ──────────────────────────── */++static int find_resolved_arr(const CBMResolvedCallArray *arr, const char *callerSub,+ const char *calleeSub) {+ for (int i = 0; i < arr->count; i++) {+ const CBMResolvedCall *rc = &arr->items[i];+ if (rc->caller_qn && strstr(rc->caller_qn, callerSub) &&+ rc->callee_qn && strstr(rc->callee_qn, calleeSub))+ return i;+ }+ return -1;+}++TEST(pylsp_crossfile_method_dispatch) {+ /* file svc.py defines class RedisStore with Get(); file main.py calls+ * the method on a typed parameter. Reuses CBMLSPDef to feed the+ * cross-file definition into the resolver. */+ const char *source =+ "from svc import RedisStore\n"+ "def process(s: RedisStore):\n"+ " return s.Get('k')\n";++ CBMLSPDef defs[2];+ memset(defs, 0, sizeof(defs));+ defs[0].qualified_name = "svc.RedisStore";+ defs[0].short_name = "RedisStore";+ defs[0].label = "Class";+ defs[0].def_module_qn = "svc";++ defs[1].qualified_name = "svc.RedisStore.Get";+ defs[1].short_name = "Get";+ defs[1].label = "Method";+ defs[1].receiver_type = "svc.RedisStore";+ defs[1].def_module_qn = "svc";++ const char *imp_names[] = {"RedisStore"};+ const char *imp_qns[] = {"svc.RedisStore"};++ CBMArena arena;+ cbm_arena_init(&arena);+ CBMResolvedCallArray out = {0};++ cbm_run_py_lsp_cross(&arena, source, (int)strlen(source), "test.main",+ defs, 2, imp_names, imp_qns, 1, NULL, &out);++ ASSERT_GTE(find_resolved_arr(&out, "process", "Get"), 0);+ cbm_arena_destroy(&arena);+ PASS();+}++TEST(pylsp_crossfile_inheritance) {+ /* svc.py defines class Base with shared(); main.py defines class Child(Base)+ * and calls self.shared(). Caller passes ALL relevant defs (cross-file+ * Base/shared + local Child/go) — same convention as test_go_lsp.c. */+ const char *source =+ "from svc import Base\n"+ "class Child(Base):\n"+ " def go(self):\n"+ " return self.shared()\n";++ CBMLSPDef defs[4];+ memset(defs, 0, sizeof(defs));+ defs[0].qualified_name = "svc.Base";+ defs[0].short_name = "Base";+ defs[0].label = "Class";+ defs[0].def_module_qn = "svc";++ defs[1].qualified_name = "svc.Base.shared";+ defs[1].short_name = "shared";+ defs[1].label = "Method";+ defs[1].receiver_type = "svc.Base";+ defs[1].def_module_qn = "svc";++ defs[2].qualified_name = "test.main.Child";+ defs[2].short_name = "Child";+ defs[2].label = "Class";+ defs[2].def_module_qn = "test.main";+ defs[2].embedded_types = "svc.Base";++ defs[3].qualified_name = "test.main.Child.go";+ defs[3].short_name = "go";+ defs[3].label = "Method";+ defs[3].receiver_type = "test.main.Child";+ defs[3].def_module_qn = "test.main";++ const char *imp_names[] = {"Base"};+ const char *imp_qns[] = {"svc.Base"};++ CBMArena arena;+ cbm_arena_init(&arena);+ CBMResolvedCallArray out = {0};++ cbm_run_py_lsp_cross(&arena, source, (int)strlen(source), "test.main",+ defs, 4, imp_names, imp_qns, 1, NULL, &out);++ ASSERT_GTE(find_resolved_arr(&out, "go", "shared"), 0);+ cbm_arena_destroy(&arena);+ PASS();+}++TEST(pylsp_batch_two_files) {+ const char *src_a =+ "def helper():\n"+ " return 1\n";+ const char *src_b =+ "from a import helper\n"+ "def main():\n"+ " return helper()\n";++ CBMLSPDef a_defs[1];+ memset(a_defs, 0, sizeof(a_defs));+ a_defs[0].qualified_name = "a.helper";+ a_defs[0].short_name = "helper";+ a_defs[0].label = "Function";+ a_defs[0].def_module_qn = "a";++ CBMLSPDef b_defs[1];+ memset(b_defs, 0, sizeof(b_defs));+ b_defs[0].qualified_name = "b.main";+ b_defs[0].short_name = "main";+ b_defs[0].label = "Function";+ b_defs[0].def_module_qn = "b";++ const char *b_imp_names[] = {"helper"};+ const char *b_imp_qns[] = {"a.helper"};++ CBMBatchPyLSPFile files[2];+ memset(files, 0, sizeof(files));+ files[0].source = src_a;+ files[0].source_len = (int)strlen(src_a);+ files[0].module_qn = "a";+ files[0].defs = a_defs;+ files[0].def_count = 1;++ files[1].source = src_b;+ files[1].source_len = (int)strlen(src_b);+ files[1].module_qn = "b";+ files[1].defs = b_defs;+ files[1].def_count = 1;+ /* b imports helper from a — also include a's def in b's reachable set. */+ CBMLSPDef b_combined[2];+ memcpy(&b_combined[0], &a_defs[0], sizeof(CBMLSPDef));+ memcpy(&b_combined[1], &b_defs[0], sizeof(CBMLSPDef));+ files[1].defs = b_combined;+ files[1].def_count = 2;+ files[1].import_names = b_imp_names;+ files[1].import_qns = b_imp_qns;+ files[1].import_count = 1;++ CBMArena arena;+ cbm_arena_init(&arena);+ CBMResolvedCallArray outs[2];+ memset(outs, 0, sizeof(outs));++ cbm_batch_py_lsp_cross(&arena, files, 2, outs);++ /* file b's main should have called helper. */+ ASSERT_GTE(find_resolved_arr(&outs[1], "main", "helper"), 0);+ cbm_arena_destroy(&arena);+ PASS();+}+/* ── Suite ─────────────────────────────────────────────────────── */SUITE(py_lsp) {@@ -540,4 +702,8 @@ SUITE(py_lsp) {RUN_TEST(pylsp_super_call);RUN_TEST(pylsp_multi_inheritance_first_base);RUN_TEST(pylsp_pep695_generic_class);+ /* Phase 9 — cross-file + batch */+ RUN_TEST(pylsp_crossfile_method_dispatch);+ RUN_TEST(pylsp_crossfile_inheritance);+ RUN_TEST(pylsp_batch_two_files);}