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(lsp): py_lsp scaffold + smoke tests
Martin Vogelb1a6285601ab
diff --git a/Makefile.cbm b/Makefile.cbmindex c7270c99e..ad7760ced 100644--- a/Makefile.cbm+++ b/Makefile.cbm@@ -313,6 +313,8 @@ TEST_SCOPE_SRCS = tests/test_scope.cTEST_TYPE_REP_SRCS = tests/test_type_rep.c+TEST_PY_LSP_SRCS = tests/test_py_lsp.c+TEST_INTEGRATION_SRCS = tests/test_integration.c tests/test_incremental.cTEST_TRACES_SRCS = tests/test_traces.c@@ -330,7 +332,7 @@ TEST_YAML_SRCS = tests/test_yaml.cTEST_SIMHASH_SRCS = tests/test_simhash.c-ALL_TEST_SRCS = $(TEST_FOUNDATION_SRCS) $(TEST_EXTRACTION_SRCS) $(TEST_STORE_SRCS) $(TEST_CYPHER_SRCS) $(TEST_MCP_SRCS) $(TEST_DISCOVER_SRCS) $(TEST_GRAPH_BUFFER_SRCS) $(TEST_PIPELINE_SRCS) $(TEST_WATCHER_SRCS) $(TEST_LZ4_SRCS) $(TEST_ZSTD_SRCS) $(TEST_ARTIFACT_SRCS) $(TEST_SQLITE_WRITER_SRCS) $(TEST_GO_LSP_SRCS) $(TEST_C_LSP_SRCS) $(TEST_SCOPE_SRCS) $(TEST_TYPE_REP_SRCS) $(TEST_TRACES_SRCS) $(TEST_CLI_SRCS) $(TEST_MEM_SRCS) $(TEST_UI_SRCS) $(TEST_SECURITY_SRCS) $(TEST_YAML_SRCS) $(TEST_SIMHASH_SRCS) $(TEST_INTEGRATION_SRCS)+ALL_TEST_SRCS = $(TEST_FOUNDATION_SRCS) $(TEST_EXTRACTION_SRCS) $(TEST_STORE_SRCS) $(TEST_CYPHER_SRCS) $(TEST_MCP_SRCS) $(TEST_DISCOVER_SRCS) $(TEST_GRAPH_BUFFER_SRCS) $(TEST_PIPELINE_SRCS) $(TEST_WATCHER_SRCS) $(TEST_LZ4_SRCS) $(TEST_ZSTD_SRCS) $(TEST_ARTIFACT_SRCS) $(TEST_SQLITE_WRITER_SRCS) $(TEST_GO_LSP_SRCS) $(TEST_C_LSP_SRCS) $(TEST_SCOPE_SRCS) $(TEST_TYPE_REP_SRCS) $(TEST_PY_LSP_SRCS) $(TEST_TRACES_SRCS) $(TEST_CLI_SRCS) $(TEST_MEM_SRCS) $(TEST_UI_SRCS) $(TEST_SECURITY_SRCS) $(TEST_YAML_SRCS) $(TEST_SIMHASH_SRCS) $(TEST_INTEGRATION_SRCS)# ── Build directories ────────────────────────────────────────────diff --git a/internal/cbm/cbm.c b/internal/cbm/cbm.cindex 7c34f0b0d..5d7a8b8b8 100644--- a/internal/cbm/cbm.c+++ b/internal/cbm/cbm.c@@ -5,6 +5,7 @@#include "extract_unified.h"#include "lsp/go_lsp.h"#include "lsp/c_lsp.h"+#include "lsp/py_lsp.h"#include "preprocessor.h"#include "foundation/compat.h"#include "tree_sitter/api.h" // TSParser, TSNode, TSTree, TSInput, TSLanguage, TSPoint, TSParseOptions, TSParseState@@ -357,6 +358,9 @@ CBMFileResult *cbm_extract_file(const char *source, int source_len, CBMLanguageif (language == CBM_LANG_C || language == CBM_LANG_CPP || language == CBM_LANG_CUDA) {cbm_run_c_lsp(a, result, source, source_len, root, language != CBM_LANG_C);}+ if (language == CBM_LANG_PYTHON) {+ cbm_run_py_lsp(a, result, source, source_len, root);+ }atomic_fetch_add(&total_lsp_ns, now_ns() - lsp_start);// Second pass: preprocess C/C++/CUDA and extract additional macro-hidden calls.diff --git a/internal/cbm/lsp/py_lsp.c b/internal/cbm/lsp/py_lsp.cnew file mode 100644index 000000000..bcfad37ea--- /dev/null+++ b/internal/cbm/lsp/py_lsp.c@@ -0,0 +1,137 @@+/*+ * py_lsp.c — Python type-aware call resolution.+ *+ * Phase 2 scaffold: PyLSPContext, single-file entry point, cross-file ++ * batch entry points (no-op shells). Subsequent phases fill in:+ * Phase 3 — imports+ * Phase 4 — scope binding+ * Phase 5 — expression typing+ * Phase 6 — attribute resolution + method dispatch+ * Phase 7 — decorators+ * Phase 8 — class hierarchy + generics+ * Phase 9 — cross-file resolution + batch+ * Phase 10 — cbm_python_stdlib_register+ */+#include "py_lsp.h"+#include "../cbm.h"+#include "../helpers.h"+#include "tree_sitter/api.h"+#include <stdlib.h>+#include <string.h>++void py_lsp_init(PyLSPContext* ctx, CBMArena* arena, const char* source, int source_len,+ const CBMTypeRegistry* registry, const char* module_qn, CBMResolvedCallArray* out) {+ if (!ctx) return;+ memset(ctx, 0, sizeof(PyLSPContext));+ ctx->arena = arena;+ ctx->source = source;+ ctx->source_len = source_len;+ ctx->registry = registry;+ ctx->module_qn = module_qn;+ ctx->resolved_calls = out;+ ctx->current_scope = cbm_scope_push(arena, NULL);+ const char* dbg = getenv("CBM_LSP_DEBUG");+ ctx->debug = dbg && dbg[0] && dbg[0] != '0';+}++void py_lsp_add_import(PyLSPContext* ctx, const char* local_name, const char* module_qn) {+ if (!ctx || !local_name || !module_qn) return;++ int new_count = ctx->import_count + 1;+ const char** names = (const char**)cbm_arena_alloc(ctx->arena,+ (size_t)(new_count + 1) * sizeof(const char*));+ const char** qns = (const char**)cbm_arena_alloc(ctx->arena,+ (size_t)(new_count + 1) * sizeof(const char*));+ if (!names || !qns) return;++ for (int i = 0; i < ctx->import_count; i++) {+ names[i] = ctx->import_local_names[i];+ qns[i] = ctx->import_module_qns[i];+ }+ names[ctx->import_count] = cbm_arena_strdup(ctx->arena, local_name);+ qns[ctx->import_count] = cbm_arena_strdup(ctx->arena, module_qn);+ names[new_count] = NULL;+ qns[new_count] = NULL;++ ctx->import_local_names = names;+ ctx->import_module_qns = qns;+ ctx->import_count = new_count;+}++void py_lsp_process_file(PyLSPContext* ctx, TSNode root) {+ /* Phase 2 stub: no-op. Phase 4+ walks the AST and binds/resolves. */+ (void)ctx;+ (void)root;+}++/* ── cbm_run_py_lsp: single-file entry point ──────────────────── */++void cbm_run_py_lsp(CBMArena* arena, CBMFileResult* result,+ const char* source, int source_len, TSNode root) {+ if (!arena || !result) return;++ CBMTypeRegistry reg;+ cbm_registry_init(®, arena);++ // Phase 10 will register typeshed-generated stdlib here.+ cbm_python_stdlib_register(®, arena);++ const char* module_qn = result->module_qn;++ // Phase 4 will register the file's own definitions into reg here.++ PyLSPContext ctx;+ py_lsp_init(&ctx, arena, source, source_len, ®, module_qn, &result->resolved_calls);++ // Wire imports from the unified extractor.+ for (int i = 0; i < result->imports.count; i++) {+ CBMImport* imp = &result->imports.items[i];+ if (imp->local_name && imp->module_path) {+ py_lsp_add_import(&ctx, imp->local_name, imp->module_path);+ }+ }++ py_lsp_process_file(&ctx, root);+}++/* ── Cross-file + batch — Phase 9 fills these in ──────────────── */++void cbm_run_py_lsp_cross(+ CBMArena* arena,+ const char* source, int source_len,+ const char* module_qn,+ CBMLSPDef* defs, int def_count,+ 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;+}++void cbm_batch_py_lsp_cross(+ CBMArena* arena,+ CBMBatchPyLSPFile* files, int file_count,+ CBMResolvedCallArray* out) {+ (void)arena;+ (void)files;+ (void)file_count;+ (void)out;+}++/* ── Stdlib stub — Phase 10 replaces with auto-generated body ─── */++#ifndef CBM_PYTHON_STDLIB_GENERATED+void cbm_python_stdlib_register(CBMTypeRegistry* reg, CBMArena* arena) {+ (void)reg;+ (void)arena;+}+#endifdiff --git a/internal/cbm/lsp/py_lsp.h b/internal/cbm/lsp/py_lsp.hnew file mode 100644index 000000000..d450f349a--- /dev/null+++ b/internal/cbm/lsp/py_lsp.h@@ -0,0 +1,88 @@+#ifndef CBM_LSP_PY_LSP_H+#define CBM_LSP_PY_LSP_H++#include "type_rep.h"+#include "scope.h"+#include "type_registry.h"+#include "../cbm.h"+#include "go_lsp.h" // CBMLSPDef, CBMResolvedCallArray reused across languages++// PyLSPContext holds state for one Python file's type evaluation.+typedef struct {+ CBMArena* arena;+ const char* source;+ int source_len;+ const CBMTypeRegistry* registry;+ CBMScope* current_scope;++ // Import map: local_name -> module_qn (arena-allocated, NULL-terminated).+ // Built from CBMFileResult.imports.+ const char** import_local_names;+ const char** import_module_qns;+ int import_count;++ // Current function/class context for resolving `self`/`cls` and emitting+ // caller QNs.+ const char* enclosing_func_qn;+ const char* enclosing_class_qn;+ const char* module_qn;++ // Output: resolved calls accumulate here.+ CBMResolvedCallArray* resolved_calls;++ // Debug mode (CBM_LSP_DEBUG env, shared across all language LSPs).+ bool debug;+} PyLSPContext;++// Initialize a PyLSPContext for processing one file.+void py_lsp_init(PyLSPContext* ctx, CBMArena* arena, const char* source, int source_len,+ const CBMTypeRegistry* registry, const char* module_qn, CBMResolvedCallArray* out);++// Add an import mapping (call once per CBMImport entry).+void py_lsp_add_import(PyLSPContext* ctx, const char* local_name, const char* module_qn);++// Process all functions/classes in a file's AST, evaluating types and resolving calls.+// root must be the tree-sitter Python `module` node.+void py_lsp_process_file(PyLSPContext* ctx, TSNode root);++// Entry point: build registry from file's own defs + run LSP resolution.+// Mirrors cbm_run_go_lsp / cbm_run_c_lsp.+void cbm_run_py_lsp(CBMArena* arena, CBMFileResult* result,+ const char* source, int source_len, TSNode root);++// Register Python stdlib types and functions into a registry.+// Auto-generated by scripts/gen-py-stdlib.py from typeshed.+// Phase 10 fills the body; Phase 2 ships a no-op so linkage works.+void cbm_python_stdlib_register(CBMTypeRegistry* reg, CBMArena* arena);++// --- Cross-file LSP resolution ---++void cbm_run_py_lsp_cross(+ CBMArena* arena,+ const char* source, int source_len,+ const char* module_qn,+ CBMLSPDef* defs, int def_count,+ const char** import_names, const char** import_qns, int import_count,+ TSTree* cached_tree, // NULL = parse internally+ CBMResolvedCallArray* out);++// --- Batch cross-file LSP ---++typedef struct {+ const char* source;+ int source_len;+ const char* module_qn;+ TSTree* cached_tree; // from TSTree caching (NULL = parse internally)+ CBMLSPDef* defs; // combined file-local + cross-file defs+ int def_count;+ const char** import_names; // parallel arrays, import_count long+ const char** import_qns;+ int import_count;+} CBMBatchPyLSPFile;++void cbm_batch_py_lsp_cross(+ CBMArena* arena,+ CBMBatchPyLSPFile* files, int file_count,+ CBMResolvedCallArray* out);++#endif // CBM_LSP_PY_LSP_Hdiff --git a/internal/cbm/lsp_all.c b/internal/cbm/lsp_all.cindex 178bb46ea..f276d1d76 100644--- a/internal/cbm/lsp_all.c+++ b/internal/cbm/lsp_all.c@@ -10,3 +10,4 @@#include "lsp/c_lsp.c"#include "lsp/generated/c_stdlib_data.c"#include "lsp/generated/cpp_stdlib_data.c"+#include "lsp/py_lsp.c"diff --git a/tests/test_main.c b/tests/test_main.cindex 548648ec6..b3b29cf11 100644--- a/tests/test_main.c+++ b/tests/test_main.c@@ -43,6 +43,7 @@ extern void suite_go_lsp(void);extern void suite_c_lsp(void);extern void suite_scope(void);extern void suite_type_rep(void);+extern void suite_py_lsp(void);extern void suite_store_arch(void);extern void suite_store_bulk(void);extern void suite_traces(void);@@ -118,6 +119,7 @@ int main(void) {RUN_SUITE(type_rep);RUN_SUITE(go_lsp);RUN_SUITE(c_lsp);+ RUN_SUITE(py_lsp);/* Architecture + ADR + Louvain */RUN_SUITE(store_arch);diff --git a/tests/test_py_lsp.c b/tests/test_py_lsp.cnew file mode 100644index 000000000..9eeb5c441--- /dev/null+++ b/tests/test_py_lsp.c@@ -0,0 +1,124 @@+/*+ * test_py_lsp.c — Tests for Python LSP type-aware call resolution.+ *+ * Mirrors tests/test_go_lsp.c shape: helper extract_py(source) calls+ * cbm_extract_file with CBM_LANG_PYTHON, then assertions search the+ * resolved_calls array. Phase 2 ships smoke tests only; subsequent+ * phases add categories matching the Go LSP layout (param types,+ * method dispatch, decorators, generics, cross-file).+ */+#include "test_framework.h"+#include "cbm.h"+#include "lsp/py_lsp.h"++/* ── Helpers — same shape as test_go_lsp.c ──────────────────────── */++static CBMFileResult *extract_py(const char *source) {+ return cbm_extract_file(source, (int)strlen(source), CBM_LANG_PYTHON,+ "test", "main.py", 0, NULL, NULL);+}++static int find_resolved(const CBMFileResult *r, const char *callerSub, const char *calleeSub) {+ for (int i = 0; i < r->resolved_calls.count; i++) {+ const CBMResolvedCall *rc = &r->resolved_calls.items[i];+ if (rc->caller_qn && strstr(rc->caller_qn, callerSub) && rc->callee_qn &&+ strstr(rc->callee_qn, calleeSub))+ return i;+ }+ return -1;+}++/* Avoid unused-static-function warnings: helpers compiled but not yet used+ * outside the smoke tests will be referenced in Phase 3+ tests. */+__attribute__((unused))+static int require_resolved(const CBMFileResult *r, const char *callerSub, const char *calleeSub) {+ int idx = find_resolved(r, callerSub, calleeSub);+ if (idx < 0) {+ printf(" MISSING resolved call: caller~%s -> callee~%s (have %d)\n",+ callerSub, calleeSub, r->resolved_calls.count);+ for (int i = 0; i < r->resolved_calls.count; i++) {+ const CBMResolvedCall *rc = &r->resolved_calls.items[i];+ printf(" %s -> %s [%s %.2f]\n",+ rc->caller_qn ? rc->caller_qn : "(null)",+ rc->callee_qn ? rc->callee_qn : "(null)",+ rc->strategy ? rc->strategy : "(null)",+ rc->confidence);+ }+ }+ return idx;+}++/* ── Phase 2 — smoke ───────────────────────────────────────────── */++TEST(pylsp_smoke_empty) {+ CBMFileResult *r = extract_py("");+ ASSERT_NOT_NULL(r);+ ASSERT_EQ(r->resolved_calls.count, 0);+ cbm_free_result(r);+ PASS();+}++TEST(pylsp_smoke_one_function) {+ CBMFileResult *r = extract_py(+ "def greet(name):\n"+ " return name\n");+ ASSERT_NOT_NULL(r);+ /* Phase 2 stub: no resolutions yet, but extraction must succeed and+ * the result must be addressable without crashes. */+ ASSERT_GTE(r->defs.count, 1);+ cbm_free_result(r);+ PASS();+}++TEST(pylsp_smoke_one_class) {+ CBMFileResult *r = extract_py(+ "class Greeter:\n"+ " def __init__(self, name):\n"+ " self.name = name\n"+ " def greet(self):\n"+ " return self.name\n");+ ASSERT_NOT_NULL(r);+ /* Class + 2 methods at minimum */+ ASSERT_GTE(r->defs.count, 1);+ cbm_free_result(r);+ PASS();+}++TEST(pylsp_no_crash_on_syntax_error) {+ /* Tree-sitter recovers from errors but we must not crash on the+ * recovered tree. */+ CBMFileResult *r = extract_py(+ "def broken(\n"+ " x = 1\n"+ "class\n");+ ASSERT_NOT_NULL(r);+ cbm_free_result(r);+ PASS();+}++TEST(pylsp_smoke_imports_passed_through) {+ /* Imports populate ctx->import_local_names — Phase 2 just verifies+ * the unified extractor still produces them; resolution happens in+ * Phase 3. */+ CBMFileResult *r = extract_py(+ "import os\n"+ "import json as j\n"+ "from pathlib import Path\n"+ "from . import sibling\n"+ "def use():\n"+ " return os.getcwd()\n");+ ASSERT_NOT_NULL(r);+ ASSERT_GTE(r->imports.count, 3);+ cbm_free_result(r);+ PASS();+}++/* ── Suite ─────────────────────────────────────────────────────── */++SUITE(py_lsp) {+ RUN_TEST(pylsp_smoke_empty);+ RUN_TEST(pylsp_smoke_one_function);+ RUN_TEST(pylsp_smoke_one_class);+ RUN_TEST(pylsp_no_crash_on_syntax_error);+ RUN_TEST(pylsp_smoke_imports_passed_through);+}