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(ruby-lsp): fail closed on unresolved ancestry
Ben Fairless60801bc53fd1
diff --git a/internal/cbm/lsp/ruby_lsp.c b/internal/cbm/lsp/ruby_lsp.cindex f7f41e10f..1411c3649 100644--- a/internal/cbm/lsp/ruby_lsp.c+++ b/internal/cbm/lsp/ruby_lsp.c@@ -437,23 +437,44 @@ static const CBMRegisteredFunc *ruby_own_singleton_method(RubyLSPContext *ctx, creturn cbm_registry_lookup_method(ctx->registry, key, method_name);}+/* Fail-closed ancestry. Every lookup below carries a `blocked` flag that is+ * set the moment the walk meets an ancestor it cannot see: a mixin whose+ * module did not resolve, or a superclass reference that did not resolve.+ * An unseen ancestor may define (or, for a prepend, shadow) the method, so+ * from that point on no hit is trustworthy and callers must emit nothing —+ * including NOT falling through to a same-named top-level def or to the+ * Object typing fallback. Hits found BEFORE the unseen ancestor in Ruby's+ * lookup order are still returned. */+static const CBMRegisteredFunc *ruby_lookup_instance_method_depth(RubyLSPContext *ctx,const char *class_qn,const char *method_name,- int depth);+ int depth, bool *blocked);++/* True when the class declares a superclass the finalize step could not+ * resolve (stdlib-seeded conventions like ApplicationRecord DO resolve). */+static bool ruby_superclass_unresolved(RubyLSPContext *ctx, const char *class_qn) {+ RubyClassInfo *ci = ruby_class_by_qn(ctx, class_qn);+ return ci && ci->superclass_ref && !ci->superclass_qn;+}/* Search a mixin kind on `owner_qn` (most recent first — later mixins win),- * recursing into each module's own include chain. */+ * recursing into each module's own include chain. An UNRESOLVED mixin met+ * before any hit blocks the walk. */static const CBMRegisteredFunc *ruby_lookup_in_mixins(RubyLSPContext *ctx, const char *owner_qn,RubyMixinKind kind, const char *method_name,- int depth) {+ int depth, bool *blocked) {for (int i = ctx->mixin_count - 1; i >= 0; i--) {RubyMixinInfo *mi = &ctx->mixins[i];- if (mi->kind != kind || strcmp(mi->owner_qn, owner_qn) != 0 || !mi->module_qn)+ if (mi->kind != kind || strcmp(mi->owner_qn, owner_qn) != 0)continue;+ if (!mi->module_qn) {+ *blocked = true; /* unseen module — it may define/shadow the method */+ return NULL;+ }const CBMRegisteredFunc *f =- ruby_lookup_instance_method_depth(ctx, mi->module_qn, method_name, depth + 1);- if (f)+ ruby_lookup_instance_method_depth(ctx, mi->module_qn, method_name, depth + 1, blocked);+ if (f || *blocked)return f;}return NULL;@@ -462,30 +483,41 @@ static const CBMRegisteredFunc *ruby_lookup_in_mixins(RubyLSPContext *ctx, conststatic const CBMRegisteredFunc *ruby_lookup_instance_method_depth(RubyLSPContext *ctx,const char *class_qn,const char *method_name,- int depth) {+ int depth, bool *blocked) {if (!ctx || !class_qn || !method_name || depth > RUBY_LOOKUP_MAX_DEPTH)return NULL;- /* Prepended modules shadow the class's own methods. */+ /* Prepended modules shadow the class's own methods — an unresolved+ * prepend therefore blocks even an own-method hit. */const CBMRegisteredFunc *f =- ruby_lookup_in_mixins(ctx, class_qn, RUBY_MIXIN_PREPEND, method_name, depth);- if (f)+ ruby_lookup_in_mixins(ctx, class_qn, RUBY_MIXIN_PREPEND, method_name, depth, blocked);+ if (f || *blocked)return f;f = ruby_own_instance_method(ctx, class_qn, method_name);if (f)return f;- f = ruby_lookup_in_mixins(ctx, class_qn, RUBY_MIXIN_INCLUDE, method_name, depth);- if (f)+ /* An unresolved include stops the walk before the superclass chain. */+ f = ruby_lookup_in_mixins(ctx, class_qn, RUBY_MIXIN_INCLUDE, method_name, depth, blocked);+ if (f || *blocked)return f;+ if (ruby_superclass_unresolved(ctx, class_qn)) {+ *blocked = true;+ return NULL;+ }const char *sup = ruby_superclass_qn(ctx, class_qn);if (sup && strcmp(sup, class_qn) != 0)- return ruby_lookup_instance_method_depth(ctx, sup, method_name, depth + 1);+ return ruby_lookup_instance_method_depth(ctx, sup, method_name, depth + 1, blocked);return NULL;}-const CBMRegisteredFunc *ruby_lookup_instance_method(RubyLSPContext *ctx, const char *class_qn,- const char *method_name) {- const CBMRegisteredFunc *f = ruby_lookup_instance_method_depth(ctx, class_qn, method_name, 0);- if (f)+/* Instance lookup with the universal-receiver typing fallback. Skipped when+ * the ancestry is blocked: nothing below the unseen ancestor is trustworthy. */+static const CBMRegisteredFunc *ruby_lookup_instance_method_b(RubyLSPContext *ctx,+ const char *class_qn,+ const char *method_name,+ bool *blocked) {+ const CBMRegisteredFunc *f =+ ruby_lookup_instance_method_depth(ctx, class_qn, method_name, 0, blocked);+ if (f || *blocked)return f;/* Universal receiver fallback (Object / ActiveSupport predicates) —* typing only; Object methods are stdlib entries, never project defs. */@@ -494,28 +526,39 @@ const CBMRegisteredFunc *ruby_lookup_instance_method(RubyLSPContext *ctx, constreturn NULL;}+const CBMRegisteredFunc *ruby_lookup_instance_method(RubyLSPContext *ctx, const char *class_qn,+ const char *method_name) {+ bool blocked = false;+ return ruby_lookup_instance_method_b(ctx, class_qn, method_name, &blocked);+}+static const CBMRegisteredFunc *ruby_lookup_singleton_method_depth(RubyLSPContext *ctx,const char *class_qn,const char *method_name,- int depth) {+ int depth, bool *blocked) {if (!ctx || !class_qn || !method_name || depth > RUBY_LOOKUP_MAX_DEPTH)return NULL;const CBMRegisteredFunc *f = ruby_own_singleton_method(ctx, class_qn, method_name);if (f)return f;/* `extend Mod` adds Mod's instance methods class-side. */- f = ruby_lookup_in_mixins(ctx, class_qn, RUBY_MIXIN_EXTEND, method_name, depth);- if (f)+ f = ruby_lookup_in_mixins(ctx, class_qn, RUBY_MIXIN_EXTEND, method_name, depth, blocked);+ if (f || *blocked)return f;+ if (ruby_superclass_unresolved(ctx, class_qn)) {+ *blocked = true;+ return NULL;+ }const char *sup = ruby_superclass_qn(ctx, class_qn);if (sup && strcmp(sup, class_qn) != 0)- return ruby_lookup_singleton_method_depth(ctx, sup, method_name, depth + 1);+ return ruby_lookup_singleton_method_depth(ctx, sup, method_name, depth + 1, blocked);return NULL;}const CBMRegisteredFunc *ruby_lookup_singleton_method(RubyLSPContext *ctx, const char *class_qn,const char *method_name) {- return ruby_lookup_singleton_method_depth(ctx, class_qn, method_name, 0);+ bool blocked = false;+ return ruby_lookup_singleton_method_depth(ctx, class_qn, method_name, 0, &blocked);}/* ── ActiveRecord model detection (typing special case) ─────────── */@@ -809,16 +852,23 @@ static const CBMType *ruby_eval_call_type(RubyLSPContext *ctx, TSNode call, boolif (ctx->self_unknown)return cbm_type_unknown(); /* receiver is a rebound self — zero-edge */if (ctx->enclosing_class_qn) {+ bool blocked = false;const CBMRegisteredFunc *f =ctx->in_singleton_method- ? ruby_lookup_singleton_method(ctx, ctx->enclosing_class_qn, mname)- : ruby_lookup_instance_method_depth(ctx, ctx->enclosing_class_qn, mname, 0);+ ? ruby_lookup_singleton_method_depth(ctx, ctx->enclosing_class_qn, mname, 0,+ &blocked)+ : ruby_lookup_instance_method_depth(ctx, ctx->enclosing_class_qn, mname, 0,+ &blocked);if (f) {if (emit)ruby_emit_resolved(ctx, f->qualified_name, "ruby_self_dispatch", RUBY_CONF_HIGH,call);return ruby_func_return_type(f);}+ /* An unseen ancestor may define this method: do NOT fall through+ * to a same-named top-level def. */+ if (blocked)+ return cbm_type_unknown();}const CBMRegisteredFunc *f =cbm_registry_lookup_symbol(ctx->registry, ctx->module_qn, mname);@@ -839,10 +889,12 @@ static const CBMType *ruby_eval_call_type(RubyLSPContext *ctx, TSNode call, boolif (strcmp(rk, "self") == 0) {if (!ctx->enclosing_class_qn || ctx->self_unknown)return cbm_type_unknown();- const CBMRegisteredFunc *f =- ctx->in_singleton_method- ? ruby_lookup_singleton_method(ctx, ctx->enclosing_class_qn, mname)- : ruby_lookup_instance_method_depth(ctx, ctx->enclosing_class_qn, mname, 0);+ bool blocked = false;+ const CBMRegisteredFunc *f = ctx->in_singleton_method+ ? ruby_lookup_singleton_method_depth(+ ctx, ctx->enclosing_class_qn, mname, 0, &blocked)+ : ruby_lookup_instance_method_depth(+ ctx, ctx->enclosing_class_qn, mname, 0, &blocked);if (f) {if (emit)ruby_emit_resolved(ctx, f->qualified_name, "ruby_self_dispatch", RUBY_CONF_HIGH,@@ -889,19 +941,22 @@ static void ruby_resolve_super(RubyLSPContext *ctx, TSNode site) {const char *mname = strrchr(fq, '.');mname = mname ? mname + 1 : fq;const CBMRegisteredFunc *f = NULL;+ bool blocked = false;/* Search order mirrors Ruby: includes of the class, then superclass- * chain — the class's OWN def is the caller and must be skipped. */+ * chain — the class's OWN def is the caller and must be skipped. An+ * unresolved mixin or superclass on the way blocks the walk. */f = ruby_lookup_in_mixins(ctx, ctx->enclosing_class_qn,ctx->in_singleton_method ? RUBY_MIXIN_EXTEND : RUBY_MIXIN_INCLUDE,- mname, 0);- if (!f) {+ mname, 0, &blocked);+ if (!f && !blocked && !ruby_superclass_unresolved(ctx, ctx->enclosing_class_qn)) {const char *sup = ruby_superclass_qn(ctx, ctx->enclosing_class_qn);if (sup) {- f = ctx->in_singleton_method ? ruby_lookup_singleton_method_depth(ctx, sup, mname, 0)- : ruby_lookup_instance_method_depth(ctx, sup, mname, 0);+ f = ctx->in_singleton_method+ ? ruby_lookup_singleton_method_depth(ctx, sup, mname, 0, &blocked)+ : ruby_lookup_instance_method_depth(ctx, sup, mname, 0, &blocked);}}- if (f)+ if (f && !blocked)ruby_emit_resolved(ctx, f->qualified_name, "ruby_method_super", RUBY_CONF_HIGH, site);/* No known parent or unresolved — zero-edge guarantee. */}diff --git a/tests/test_ruby_lsp.c b/tests/test_ruby_lsp.cindex a3d4aca6e..1ad45e314 100644--- a/tests/test_ruby_lsp.c+++ b/tests/test_ruby_lsp.c@@ -379,6 +379,125 @@ TEST(rubylsp_prepend_mixin) {PASS();}+/* ── 7c. Unresolved ancestry fails closed ───────────────────────── */++/* Three cases where an ancestor the resolver cannot see could define (or+ * shadow) the method being called. Each one has a tempting local answer —+ * the class's own method, a superclass method, a same-named top-level def —+ * and each answer may be wrong, so none may be emitted. */+TEST(rubylsp_unresolved_ancestry_fails_closed) {+ const char *src = /* (a) unresolved PREPEND blocks an own-method hit: the+ * prepended module may shadow `save`. */+ "class Record\n"+ " prepend UnseenAudit\n"+ " def save\n"+ " 1\n"+ " end\n"+ " def persist\n"+ " self.save\n"+ " end\n"+ "end\n"+ "class RecordUser\n"+ " def run\n"+ " r = Record.new\n"+ " r.save\n"+ " end\n"+ "end\n"+ /* (b) unresolved INCLUDE stops the walk before the superclass chain:+ * the included module may define `helper` ahead of Base. */+ "class Base\n"+ " def helper\n"+ " 1\n"+ " end\n"+ "end\n"+ "class Child < Base\n"+ " include UnseenConcern\n"+ " def work\n"+ " self.helper\n"+ " end\n"+ "end\n"+ "class ChildUser\n"+ " def run\n"+ " c = Child.new\n"+ " c.helper\n"+ " end\n"+ "end\n"+ /* (c) a bare call in a class with an unresolved SUPERCLASS must not+ * fall through to a same-named top-level def. */+ "def util\n"+ " 1\n"+ "end\n"+ "class Orphan < UnseenBase\n"+ " def go\n"+ " util()\n"+ " end\n"+ " def again\n"+ " super\n"+ " end\n"+ "end\n";+ CBMFileResult *r = extract_ruby(src);+ ASSERT(r);+ /* (a) */+ ASSERT(find_resolved(r, "Record.persist", "save") < 0);+ ASSERT(find_resolved(r, "RecordUser.run", "save") < 0);+ /* (b) */+ ASSERT(find_resolved(r, "Child.work", "helper") < 0);+ ASSERT(find_resolved(r, "ChildUser.run", "helper") < 0);+ /* (c) */+ ASSERT(find_resolved(r, "Orphan.go", "util") < 0);+ ASSERT(find_resolved(r, "Orphan.again", "again") < 0);+ cbm_free_result(r);+ PASS();+}++/* Positive controls for the rule above: resolved ancestry still reaches the+ * superclass and still falls through to a top-level def, and a hit found+ * BEFORE the unseen ancestor in Ruby's lookup order is still emitted. */+TEST(rubylsp_resolved_ancestry_still_resolves) {+ const char *src = "module Seen\n"+ " def greet\n"+ " 1\n"+ " end\n"+ "end\n"+ "def util\n"+ " 1\n"+ "end\n"+ "class Base\n"+ " def helper\n"+ " 1\n"+ " end\n"+ "end\n"+ "class Child < Base\n"+ " include Seen\n"+ " def work\n"+ " self.helper\n"+ " util()\n"+ " end\n"+ "end\n"+ "class Mixed < UnseenBase\n"+ " include UnseenConcern\n"+ " include Seen\n"+ " def own\n"+ " 2\n"+ " end\n"+ " def work\n"+ " self.own\n"+ " self.greet\n"+ " end\n"+ "end\n";+ CBMFileResult *r = extract_ruby(src);+ ASSERT(r);+ ASSERT(require_resolved_exact(r, "test.main.Child.work", "test.main.Base.helper") >= 0);+ ASSERT(require_resolved_exact(r, "test.main.Child.work", "test.main.util") >= 0);+ /* Own method precedes includes and the superclass in lookup order. */+ ASSERT(require_resolved_exact(r, "test.main.Mixed.work", "test.main.Mixed.own") >= 0);+ /* `include Seen` comes AFTER `include UnseenConcern`, so Seen is searched+ * first and its hit is trustworthy. */+ ASSERT(require_resolved_exact(r, "test.main.Mixed.work", "test.main.Seen.greet") >= 0);+ cbm_free_result(r);+ PASS();+}+/* ── 8. extend mixin (class-side) ───────────────────────────────── */TEST(rubylsp_extend_mixin) {@@ -1119,6 +1238,8 @@ void suite_ruby_lsp(void) {RUN_TEST(rubylsp_inheritance);RUN_TEST(rubylsp_include_mixin);RUN_TEST(rubylsp_prepend_mixin);+ RUN_TEST(rubylsp_unresolved_ancestry_fails_closed);+ RUN_TEST(rubylsp_resolved_ancestry_still_resolves);RUN_TEST(rubylsp_extend_mixin);RUN_TEST(rubylsp_super_dispatch);RUN_TEST(rubylsp_ivar_typing);