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): route instance-field discovery through a per-file overlay
Martin Vogelbd005b4b09ec
diff --git a/internal/cbm/lsp/py_lsp.c b/internal/cbm/lsp/py_lsp.cindex 6dc5f1137..4fc087ab2 100644--- a/internal/cbm/lsp/py_lsp.c+++ b/internal/cbm/lsp/py_lsp.c@@ -447,6 +447,50 @@ static const CBMRegisteredFunc *py_lookup_attribute(PyLSPContext *ctx, const chareturn py_lookup_attribute_depth(ctx, type_qn, member_name, 0);}+/* Per-file field overlay: look up a (class_qn, field_name) recorded during resolve+ * against the sealed shared registry. Linear scan — the overlay only holds fields+ * discovered in THIS file's own classes, so it is small. */+static const CBMType *py_overlay_lookup_field(const PyLSPContext *ctx, const char *class_qn,+ const char *field_name) {+ for (int i = 0; i < ctx->field_overlay_count; i++) {+ if (strcmp(ctx->field_overlay[i].class_qn, class_qn) == 0 &&+ strcmp(ctx->field_overlay[i].field_name, field_name) == 0)+ return ctx->field_overlay[i].field_type;+ }+ return NULL;+}++/* Record a (class_qn, field_name) -> field_type in the per-file overlay. Overwrites+ * an existing entry (mirrors the direct-mutation overwrite semantics). Arena-backed;+ * grows by copy. class_qn/field_name are arena-dup'd so the entry never borrows a+ * transient node-text buffer. */+static void py_overlay_register_field(PyLSPContext *ctx, const char *class_qn,+ const char *field_name, const CBMType *field_type) {+ for (int i = 0; i < ctx->field_overlay_count; i++) {+ if (strcmp(ctx->field_overlay[i].class_qn, class_qn) == 0 &&+ strcmp(ctx->field_overlay[i].field_name, field_name) == 0) {+ ctx->field_overlay[i].field_type = field_type;+ return;+ }+ }+ if (ctx->field_overlay_count >= ctx->field_overlay_cap) {+ int new_cap = ctx->field_overlay_cap == 0 ? 16 : ctx->field_overlay_cap * 2;+ void *na = cbm_arena_alloc(ctx->arena, (size_t)new_cap * sizeof(*ctx->field_overlay));+ if (!na)+ return; /* OOM: drop this field (resolution degrades, never corrupts) */+ if (ctx->field_overlay && ctx->field_overlay_count > 0)+ memcpy(na, ctx->field_overlay,+ (size_t)ctx->field_overlay_count * sizeof(*ctx->field_overlay));+ ctx->field_overlay = na;+ ctx->field_overlay_cap = new_cap;+ }+ ctx->field_overlay[ctx->field_overlay_count].class_qn = cbm_arena_strdup(ctx->arena, class_qn);+ ctx->field_overlay[ctx->field_overlay_count].field_name =+ cbm_arena_strdup(ctx->arena, field_name);+ ctx->field_overlay[ctx->field_overlay_count].field_type = field_type;+ ctx->field_overlay_count++;+}+/* Look up a field on a registered type. Walks alias / embedded chain* with the same depth cap as method lookup. Returns the field's type* or NULL if no match. */@@ -467,6 +511,15 @@ static const CBMType *py_lookup_field_depth(PyLSPContext *ctx, const char *type_}}}+ /* Per-file overlay: `self.x = ...` fields discovered during resolve against the+ * sealed shared registry live here (not on rt). Checked at the same point the+ * direct field scan would have found them in the mutable path — preserving the+ * derived-shadows-base precedence before recursing into alias/base classes. */+ {+ const CBMType *ov = py_overlay_lookup_field(ctx, type_qn, field_name);+ if (ov)+ return ov;+ }if (rt->alias_of) {const CBMType *a = py_lookup_field_depth(ctx, rt->alias_of, field_name, depth + 1);if (a)@@ -497,6 +550,19 @@ static void py_register_instance_field(PyLSPContext *ctx, const char *class_qn,if (!ctx || !ctx->registry || !class_qn || !field_name || !field_type)return;+ /* Shared Tier-2 registry is finalized + sealed (read_only) and read concurrently+ * by all resolve workers. Writing its type entries directly below would bypass the+ * add_* seal, race the other workers, and leave the shared entry pointing into+ * this file's resolve arena (freed when the file completes). Route the discovery+ * to the per-file overlay instead; py_lookup_field consults it alongside the+ * shared base, so same-file `self.x`/PEP-526 resolution is preserved with zero+ * shared mutation. Mutable per-file registries (read_only == false) keep the+ * byte-identical direct write below. */+ if (ctx->registry->read_only) {+ py_overlay_register_field(ctx, class_qn, field_name, field_type);+ return;+ }+// Find the type entry. cbm_registry_lookup_type returns a const pointer;// we need a mutable pointer into the registry's array.CBMRegisteredType *rt = NULL;diff --git a/internal/cbm/lsp/py_lsp.h b/internal/cbm/lsp/py_lsp.hindex b8f5816dd..1d99a7a39 100644--- a/internal/cbm/lsp/py_lsp.h+++ b/internal/cbm/lsp/py_lsp.h@@ -98,6 +98,23 @@ typedef struct {int eval_steps; // per-file work budget used (PY_EVAL_MAX_STEPS_PER_FILE)uint32_t eval_truncations; // depth/budget cutoff count — gates memo inserts+ // Per-file instance-field OVERLAY. When the Tier-2 registry is shared + sealed+ // (registry->read_only), `self.x = ...` / PEP-526 field discoveries made during+ // resolve are recorded HERE instead of mutating the shared registry (which would+ // bypass the add_* seal, race the other resolve workers, and leave the shared+ // entry pointing into this file's arena once it is freed). py_lookup_field+ // consults this overlay alongside the shared base, so same-file attribute-chain+ // resolution is preserved with zero shared mutation. Arena-allocated (per-file+ // lifetime); holds no pointer into the shared registry, and the shared registry+ // holds none into it. Mutable per-file registries keep the direct write.+ struct {+ const char *class_qn;+ const char *field_name;+ const CBMType *field_type;+ } *field_overlay;+ int field_overlay_count;+ int field_overlay_cap;+// Debug mode (CBM_LSP_DEBUG env, shared across all language LSPs).bool debug;} PyLSPContext;diff --git a/tests/test_c_lsp.c b/tests/test_c_lsp.cindex 0202e3c18..72fd1cd55 100644--- a/tests/test_c_lsp.c+++ b/tests/test_c_lsp.c@@ -15284,6 +15284,80 @@ TEST(seal_py_shared_registry_readonly) {PASS();}+/* Sibling of seal_py_shared_registry_readonly at the FIELD-ARRAY granularity.+ *+ * The count-based seal tests only catch mutations routed through+ * cbm_registry_add_func/_type (which the read_only seal no-ops). But+ * py_register_instance_field (py_lsp.c:495), invoked from the `self.x = ...`+ * assignment handler (py_lsp.c:1616), writes the registered type's+ * field_names/field_types arrays DIRECTLY (py_lsp.c:517 in-place; :543-544 pointer+ * reassign) with NO read_only guard — bypassing the seal. In the fused parallel+ * resolve path pass_parallel.c:2726 hands EVERY worker the one shared, finalized,+ * read_only registry (pipeline.c:901 → cbm_py_build_cross_registry), so this is a+ * cross-thread data race AND a cross-file dangling pointer (the appended arrays are+ * allocated in the per-file resolve arena, freed when that file completes) into a+ * sealed registry. func_count/type_count stay put, which is exactly why the sibling+ * test above stays green while the bug persists.+ *+ * INVARIANT (green <=> fixed): resolving a `self.x = ...` method body against the+ * sealed shared registry leaves the class's field arrays byte-identical. RED today+ * (new field "x" is appended → field_names pointer + count both change); GREEN once+ * py_register_instance_field honors ctx->registry->read_only. */+TEST(seal_py_shared_registry_readonly_fields) {+ CBMArena arena;+ cbm_arena_init(&arena);++ /* One Python class Foo (+ method m) so the shared registry holds a type+ * "test.mod.Foo" that resolve's self.x= handler will target. */+ CBMLSPDef defs[2];+ memset(defs, 0, sizeof(defs));+ defs[0].qualified_name = "test.mod.Foo";+ defs[0].short_name = "Foo";+ defs[0].label = "Class";+ defs[0].def_module_qn = "test.mod";+ defs[0].lang = CBM_LANG_PYTHON;+ defs[1].qualified_name = "test.mod.Foo.m";+ defs[1].short_name = "m";+ defs[1].label = "Method";+ defs[1].receiver_type = "test.mod.Foo";+ defs[1].def_module_qn = "test.mod";+ defs[1].lang = CBM_LANG_PYTHON;++ CBMTypeRegistry *reg = cbm_py_build_cross_registry(&arena, defs, 2);+ ASSERT_NOT_NULL(reg);+ ASSERT_TRUE(reg->read_only);++ /* Snapshot Foo's field arrays before resolve. The entry is stable across+ * resolve: read_only blocks add_type, so reg->types is never reallocated. */+ const CBMRegisteredType *rt = cbm_registry_lookup_type(reg, "test.mod.Foo");+ ASSERT_NOT_NULL(rt);+ const char **names_before = rt->field_names;+ int count_before = 0;+ if (rt->field_names)+ while (rt->field_names[count_before])+ count_before++;++ /* A method assigning a NEW instance field on self → the buggy code appends+ * "x" directly into the sealed registry entry. */+ const char *src = "class Foo:\n"+ " def m(self):\n"+ " self.x = 1\n";+ CBMResolvedCallArray out = {0};+ cbm_run_py_lsp_cross_with_registry(&arena, src, (int)strlen(src), "test.mod", reg, NULL, NULL, 0,+ NULL, &out);++ /* The sealed registry entry must be untouched by resolve. */+ int count_after = 0;+ if (rt->field_names)+ while (rt->field_names[count_after])+ count_after++;+ ASSERT_EQ(count_after, count_before);+ ASSERT_TRUE(rt->field_names == names_before);++ cbm_arena_destroy(&arena);+ PASS();+}+TEST(seal_cs_shared_registry_readonly) {CBMArena arena;cbm_arena_init(&arena);@@ -15339,6 +15413,7 @@ SUITE(c_lsp) {RUN_TEST(clsp_tier2_shared_registry_readonly_c);RUN_TEST(clsp_tier2_shared_registry_readonly_cpp);RUN_TEST(seal_py_shared_registry_readonly);+ RUN_TEST(seal_py_shared_registry_readonly_fields);RUN_TEST(seal_cs_shared_registry_readonly);RUN_TEST(seal_ts_shared_registry_readonly);RUN_TEST(seal_go_shared_registry_readonly);diff --git a/tests/test_py_lsp.c b/tests/test_py_lsp.cindex b13af74bd..1dbdb7459 100644--- a/tests/test_py_lsp.c+++ b/tests/test_py_lsp.c@@ -556,6 +556,63 @@ TEST(pylsp_crossfile_method_dispatch) {PASS();}+/* Graph-quality guard for the Python seal fix (per-file field overlay). In the+ * FUSED path the shared Tier-2 registry is read_only, so `self.b = Bar()` can no+ * longer be recorded by mutating the shared registry; it is recorded in the per-file+ * overlay instead. This test proves the overlay preserves same-file attribute-chain+ * resolution: self.b.work() must still resolve to Bar.work. It RED-fails under a+ * plain read_only *gate* (option A: the field is dropped, self.b is untyped, the+ * work() call goes unresolved) and GREENs with the overlay. It ALSO asserts the+ * shared registry entry stays unmutated (Foo.field_names == NULL) — the seal half. */+TEST(pylsp_fused_self_attr_chain_via_overlay) {+ CBMArena arena;+ cbm_arena_init(&arena);++ /* Sealed shared registry: class Bar with method work(), class Foo. */+ CBMLSPDef defs[3];+ memset(defs, 0, sizeof(defs));+ defs[0].qualified_name = "test.mod.Bar";+ defs[0].short_name = "Bar";+ defs[0].label = "Class";+ defs[0].def_module_qn = "test.mod";+ defs[0].lang = CBM_LANG_PYTHON;+ defs[1].qualified_name = "test.mod.Bar.work";+ defs[1].short_name = "work";+ defs[1].label = "Method";+ defs[1].receiver_type = "test.mod.Bar";+ defs[1].def_module_qn = "test.mod";+ defs[1].lang = CBM_LANG_PYTHON;+ defs[2].qualified_name = "test.mod.Foo";+ defs[2].short_name = "Foo";+ defs[2].label = "Class";+ defs[2].def_module_qn = "test.mod";+ defs[2].lang = CBM_LANG_PYTHON;++ CBMTypeRegistry *reg = cbm_py_build_cross_registry(&arena, defs, 3);+ ASSERT_NOT_NULL(reg);+ ASSERT_TRUE(reg->read_only);++ /* Foo.__init__ records self.b = Bar(); Foo.run dispatches self.b.work(). */+ const char *src = "class Foo:\n"+ " def __init__(self):\n"+ " self.b = Bar()\n"+ " def run(self):\n"+ " return self.b.work()\n";+ CBMResolvedCallArray out = {0};+ cbm_run_py_lsp_cross_with_registry(&arena, src, (int)strlen(src), "test.mod", reg, NULL, NULL, 0,+ NULL, &out);++ /* Overlay preserves the attribute-chain edge. */+ ASSERT_GTE(find_resolved_arr(&out, "run", "work"), 0);+ /* Seal preserved: the shared registry entry was NOT mutated. */+ const CBMRegisteredType *foo = cbm_registry_lookup_type(reg, "test.mod.Foo");+ ASSERT_NOT_NULL(foo);+ ASSERT_TRUE(foo->field_names == NULL);++ cbm_arena_destroy(&arena);+ PASS();+}+/* Issue #228: a class/static method invoked directly on a CROSS-FILE imported* class name — ActionRecordX.build_from_text(...) — produced no CALLS edge, so* the method showed in/out degree 0 and was flagged as dead code. Distinct from@@ -1395,6 +1452,7 @@ SUITE(py_lsp) {RUN_TEST(pylsp_pep695_generic_class);/* Phase 9 — cross-file + batch */RUN_TEST(pylsp_crossfile_method_dispatch);+ RUN_TEST(pylsp_fused_self_attr_chain_via_overlay);RUN_TEST(pylsp_crossfile_classmethod_on_class_issue228);RUN_TEST(pylsp_crossfile_inheritance);RUN_TEST(pylsp_batch_two_files);