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
fix(py-lsp): walk each lambda body at most once per call chain
Nelson Spenceffa0b320047f
diff --git a/internal/cbm/lsp/py_lsp.c b/internal/cbm/lsp/py_lsp.cindex 8a0396115..039dec586 100644--- a/internal/cbm/lsp/py_lsp.c+++ b/internal/cbm/lsp/py_lsp.c@@ -165,7 +165,7 @@ static void py_register_instance_field(PyLSPContext *ctx, const char *class_qn,static void py_bind_for_target(PyLSPContext *ctx, TSNode left, const CBMType *elem_type);static void py_register_lambda(PyLSPContext *ctx, const char *name, TSNode lambda_node);static void py_register_dict_literal(PyLSPContext *ctx, const char *name, TSNode dict_node);-static TSNode py_lookup_lambda(PyLSPContext *ctx, const char *name);+static int py_lookup_lambda(PyLSPContext *ctx, const char *name);static const char *py_lookup_dict_dispatch(PyLSPContext *ctx, const char *var, const char *key);/* ── Scope mutation wrappers ─────────────────────────────────────────────@@ -690,19 +690,21 @@ static void py_register_lambda(PyLSPContext *ctx, const char *name, TSNode lambd}ctx->lambdas[ctx->lambda_count].name = cbm_arena_strdup(ctx->arena, name);ctx->lambdas[ctx->lambda_count].lambda_node = lambda_node;+ ctx->lambdas[ctx->lambda_count].expanding = false;ctx->lambda_count++;}-static TSNode py_lookup_lambda(PyLSPContext *ctx, const char *name) {- TSNode null_node = {0};+/* Registry index of the lambda bound to `name`, or -1. An index, not a+ * pointer: the registry array is replaced when it grows. */+static int py_lookup_lambda(PyLSPContext *ctx, const char *name) {if (!ctx || !name)- return null_node;+ return -1;for (int i = 0; i < ctx->lambda_count; i++) {if (ctx->lambdas[i].name && strcmp(ctx->lambdas[i].name, name) == 0) {- return ctx->lambdas[i].lambda_node;+ return i;}}- return null_node;+ return -1;}/* Helper for stripping quotes from a tree-sitter `string` node text. */@@ -2519,11 +2521,18 @@ static void py_emit_call_for(PyLSPContext *ctx, TSNode call_node) {// Walk the lambda body for any nested call sites with the params// bound to the call's arg types. Resolved calls get caller_qn// set to a synthetic <lambda> child of the enclosing function.- TSNode lambda_node = py_lookup_lambda(ctx, fname);- if (!ts_node_is_null(lambda_node)) {+ int lambda_idx = py_lookup_lambda(ctx, fname);+ if (lambda_idx >= 0) {+ TSNode lambda_node = ctx->lambdas[lambda_idx].lambda_node;TSNode params = ts_node_child_by_field_name(lambda_node, "parameters", 10);TSNode body = ts_node_child_by_field_name(lambda_node, "body", 4);TSNode args = ts_node_child_by_field_name(call_node, "arguments", 9);+ // A lambda that reaches itself again (directly or through another+ // lambda) would re-walk its body once per nesting level up to the+ // walk-depth cap, and two such calls in one body double the work+ // per level. Walk each body at most once per chain.+ if (ctx->lambdas[lambda_idx].expanding)+ return;CBMScope *saved = ctx->current_scope;ctx->current_scope = py_scope_push_checked(ctx);// Bind each lambda param to the call-site arg's type.@@ -2556,7 +2565,9 @@ static void py_emit_call_for(PyLSPContext *ctx, TSNode call_node) {ctx->enclosing_func_qn = cbm_arena_sprintf(ctx->arena, "%s.<lambda>",prev_func ? prev_func : ctx->module_qn);if (!ts_node_is_null(body)) {+ ctx->lambdas[lambda_idx].expanding = true;py_resolve_calls_in(ctx, body);+ ctx->lambdas[lambda_idx].expanding = false;}ctx->enclosing_func_qn = prev_func;py_scope_restore(ctx, saved);diff --git a/internal/cbm/lsp/py_lsp.h b/internal/cbm/lsp/py_lsp.hindex 939025100..6d1a3fb27 100644--- a/internal/cbm/lsp/py_lsp.h+++ b/internal/cbm/lsp/py_lsp.h@@ -13,6 +13,7 @@typedef struct {const char *name; // bound name, e.g. "fn"TSNode lambda_node; // the lambda AST node+ bool expanding; // body is being re-walked for a call site right now} CBMLambdaEntry;// Function-as-dict-value entry. When `funcs = {"a": foo, "b": bar}` isdiff --git a/tests/test_py_lsp.c b/tests/test_py_lsp.cindex 3aaadee51..14ce8adb3 100644--- a/tests/test_py_lsp.c+++ b/tests/test_py_lsp.c@@ -1931,6 +1931,56 @@ TEST(pylsp_lambda_rewalk_dedupes_dunder_occurrence) {PASS();}+/* A lambda body is also re-walked for calls made inside another lambda's+ * re-walk, so a lambda that reaches itself again (directly or through another+ * lambda) was re-walked once per nesting level until the walk-depth cap. With+ * two such calls in one body the work doubled per level, and a two-line+ * `fib = lambda n: fib(n - 1) + fib(n - 2)` drove extraction to many GB. A+ * lambda already being re-walked must not be re-entered, so each helper()+ * site below resolves once. The recursion here is linear, so without the+ * guard this stays small and fails the count instead of exhausting memory. */+TEST(pylsp_recursive_lambda_rewalk_is_bounded) {+ CBMFileResult *r = extract_py("def helper():\n"+ " return 0\n"+ "count = lambda n: helper() if n == 0 else count(n - 1)\n"+ "even = lambda n: helper() if n == 0 else odd(n - 1)\n"+ "odd = lambda n: even(n - 1)\n"+ "count(3)\n"+ "even(4)\n");+ ASSERT_NOT_NULL(r);+ ASSERT_FALSE(r->has_error || r->parse_incomplete);++ int helper_resolutions = 0;+ for (int i = 0; i < r->resolved_calls.count; i++) {+ const CBMResolvedCall *rc = &r->resolved_calls.items[i];+ if (rc->callee_qn && strstr(rc->callee_qn, ".helper"))+ helper_resolutions++;+ }+ ASSERT_EQ(helper_resolutions, 2);++ cbm_free_result(r);+ PASS();+}++/* The re-walk guard is released after each walk, so a later call to the same+ * lambda still binds its own argument type: both A.m and B.m resolve. */+TEST(pylsp_lambda_rewalk_guard_released_between_calls) {+ CBMFileResult *r = extract_py("class A:\n"+ " def m(self):\n"+ " return 1\n"+ "class B:\n"+ " def m(self):\n"+ " return 2\n"+ "call_m = lambda x: x.m()\n"+ "call_m(A())\n"+ "call_m(B())\n");+ ASSERT_NOT_NULL(r);+ ASSERT_GTE(require_resolved(r, "<lambda>", ".A.m"), 0);+ ASSERT_GTE(require_resolved(r, "<lambda>", ".B.m"), 0);+ cbm_free_result(r);+ PASS();+}+/* Parser-backed Python calls have the same occurrence-identity requirement as* synthetic dunders. Distinct typed receivers can expose the same method leaf* in one caller; each CBMCall must join the semantic record for its own site. */@@ -2243,6 +2293,8 @@ SUITE(py_lsp) {RUN_TEST(pylsp_round5_super_init);RUN_TEST(pylsp_dunder_same_leaf_occurrences_join_by_exact_site);RUN_TEST(pylsp_lambda_rewalk_dedupes_dunder_occurrence);+ RUN_TEST(pylsp_recursive_lambda_rewalk_is_bounded);+ RUN_TEST(pylsp_lambda_rewalk_guard_released_between_calls);RUN_TEST(pylsp_ordinary_same_leaf_calls_join_by_exact_site);/* Round 6 — generators, dataclasses, properties */RUN_TEST(pylsp_round6_generator_yields_iterable);