Skip to content

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.

fix(pipeline): unify LSP-override resolver across sequential and parallel paths

Martin Vogel085b4a718c55
diff --git a/src/pipeline/lsp_resolve.h b/src/pipeline/lsp_resolve.h
new file mode 100644
index 000000000..576b2306d
--- /dev/null
+++ b/src/pipeline/lsp_resolve.h
@@ -0,0 +1,78 @@
+/*
+ * lsp_resolve.h — Shared LSP-override resolver for the call-edge pipeline.
+ *
+ * Both pipeline paths (sequential cbm_pipeline_pass_calls and parallel
+ * cbm_parallel_extract → resolve_file_calls) need to look up an
+ * LSP-resolved call for a given (caller, callee) pair before falling back
+ * to the registry's name-based resolver. Before this header existed, each
+ * pipeline carried its own copy of that lookup with divergent confidence
+ * floors and slightly different match semantics — most production
+ * indexing went through the parallel path with a 0.5 floor while the
+ * sequential path used 0.6, so the same project produced different
+ * CALLS-edge attributions depending on which pipeline mode kicked in.
+ *
+ * Centralising the lookup here means both pipelines admit exactly the
+ * same set of LSP overrides. Each pipeline still owns its own edge
+ * emission (sequential uses emit_classified_edge, parallel uses
+ * emit_service_edge) — this header only does the matching.
+ *
+ * Inline-only: no .c file needed.
+ */
+#ifndef CBM_PIPELINE_LSP_RESOLVE_H
+#define CBM_PIPELINE_LSP_RESOLVE_H
+
+#include "cbm.h"
+#include "foundation/constants.h"
+
+#include <string.h>
+
+/* Confidence floor below which LSP-resolved calls are ignored and the
+ * registry resolver is consulted instead. Locked at 0.6 per the v1
+ * Python-LSP integration plan; revisit when telemetry justifies a knob.
+ * Applies to every language whose LSP populates result->resolved_calls
+ * (Go, C/C++, Python, PHP). */
+#define CBM_LSP_CONFIDENCE_FLOOR 0.6f
+
+/* Look up the highest-confidence LSP-resolved call entry whose caller QN
+ * matches the textual call's enclosing function and whose callee QN
+ * short-name matches the textual callee. Returns a pointer into `arr`
+ * or NULL if no qualifying entry exists.
+ *
+ * Match rule: the LSP emits CBMResolvedCall entries whose caller_qn
+ * matches the call's enclosing function and whose callee_qn ends with
+ * the textual callee_name as the last dot-separated segment. The
+ * pointer returned aliases into `arr` and stays valid as long as the
+ * underlying CBMFileResult is alive. */
+static inline const CBMResolvedCall *
+cbm_pipeline_find_lsp_resolution(const CBMResolvedCallArray *arr, const CBMCall *call) {
+ if (!arr || arr->count == 0 || !call) {
+ return NULL;
+ }
+ if (!call->enclosing_func_qn || !call->callee_name) {
+ return NULL;
+ }
+ const CBMResolvedCall *best = NULL;
+ for (int i = 0; i < arr->count; i++) {
+ const CBMResolvedCall *rc = &arr->items[i];
+ if (!rc->caller_qn || !rc->callee_qn) {
+ continue;
+ }
+ if (rc->confidence < CBM_LSP_CONFIDENCE_FLOOR) {
+ continue;
+ }
+ if (strcmp(rc->caller_qn, call->enclosing_func_qn) != 0) {
+ continue;
+ }
+ const char *short_name = strrchr(rc->callee_qn, '.');
+ short_name = short_name ? short_name + SKIP_ONE : rc->callee_qn;
+ if (strcmp(short_name, call->callee_name) != 0) {
+ continue;
+ }
+ if (!best || rc->confidence > best->confidence) {
+ best = rc;
+ }
+ }
+ return best;
+}
+
+#endif /* CBM_PIPELINE_LSP_RESOLVE_H */
diff --git a/src/pipeline/pass_calls.c b/src/pipeline/pass_calls.c
index 8d564aba2..f15ab024d 100644
--- a/src/pipeline/pass_calls.c
+++ b/src/pipeline/pass_calls.c
@@ -15,6 +15,7 @@ enum { PC_RING = 4, PC_RING_MASK = 3, PC_SIG_SCAN = 15, PC_REGEX_GRP = 2 };
#include "pipeline/pipeline.h"
#include <stdint.h>
#include "pipeline/pipeline_internal.h"
+#include "pipeline/lsp_resolve.h"
#include "graph_buffer/graph_buffer.h"
#include "foundation/log.h"
#include "foundation/compat.h"
@@ -322,49 +323,6 @@ static const cbm_gbuf_node_t *calls_find_source(cbm_pipeline_ctx_t *ctx, const c
return src;
}
-/* Confidence floor below which LSP-resolved calls are ignored and the
- * registry resolver is consulted instead. Locked at 0.6 per the v1
- * Python-LSP integration plan; revisit when telemetry justifies a knob.
- * Applies to every language whose LSP populates result->resolved_calls
- * (Go, C, Python, PHP). */
-#define CBM_LSP_CONFIDENCE_FLOOR 0.6f
-
-/* Look up the highest-confidence LSP-resolved call entry whose caller QN
- * matches the textual call's enclosing function and whose callee QN
- * short-name matches the textual callee. Returns a pointer into
- * result->resolved_calls or NULL if no qualifying entry exists. */
-static const CBMResolvedCall *find_lsp_resolution(const CBMResolvedCallArray *arr,
- const CBMCall *call) {
- if (!arr || arr->count == 0 || !call) {
- return NULL;
- }
- if (!call->enclosing_func_qn || !call->callee_name) {
- return NULL;
- }
- const CBMResolvedCall *best = NULL;
- for (int i = 0; i < arr->count; i++) {
- const CBMResolvedCall *rc = &arr->items[i];
- if (!rc->caller_qn || !rc->callee_qn) {
- continue;
- }
- if (rc->confidence < CBM_LSP_CONFIDENCE_FLOOR) {
- continue;
- }
- if (strcmp(rc->caller_qn, call->enclosing_func_qn) != 0) {
- continue;
- }
- const char *short_name = strrchr(rc->callee_qn, '.');
- short_name = short_name ? short_name + SKIP_ONE : rc->callee_qn;
- if (strcmp(short_name, call->callee_name) != 0) {
- continue;
- }
- if (!best || rc->confidence > best->confidence) {
- best = rc;
- }
- }
- return best;
-}
-
/* Resolve one call and emit the appropriate edge. Returns 1 if resolved, 0 if not. */
static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call,
const CBMResolvedCallArray *lsp_calls, const char *rel,
@@ -376,7 +334,7 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call,
}
/* LSP-resolved calls take precedence over registry-textual matching. */
- const CBMResolvedCall *lsp = find_lsp_resolution(lsp_calls, call);
+ const CBMResolvedCall *lsp = cbm_pipeline_find_lsp_resolution(lsp_calls, call);
if (lsp) {
const cbm_gbuf_node_t *target_node = cbm_gbuf_find_by_qn(ctx->gbuf, lsp->callee_qn);
if (target_node && source_node->id != target_node->id) {
diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.c
index 90480ac90..ab3e589f0 100644
--- a/src/pipeline/pass_parallel.c
+++ b/src/pipeline/pass_parallel.c
@@ -30,6 +30,7 @@ enum {
enum { PP_CSHARP_M_PREFIX_LEN = 2 };
#include "pipeline/pipeline.h"
#include "pipeline/pipeline_internal.h"
+#include "pipeline/lsp_resolve.h"
#include "pipeline/worker_pool.h"
#include "foundation/compat.h"
#include "foundation/compat_thread.h"
@@ -816,7 +817,12 @@ typedef struct __attribute__((aligned(CBM_CACHE_LINE))) {
int usages_resolved;
int semantic_resolved;
int errors;
- char _pad[CBM_CACHE_LINE - sizeof(cbm_gbuf_t *) - (PP_RING * sizeof(int))];
+ /* Subset of calls_resolved that were attributed via the LSP-override
+ * path (cbm_pipeline_find_lsp_resolution hit) rather than the
+ * registry's textual matcher. Surfaced in the parallel.resolve.done
+ * log line so divergence between pipelines becomes observable. */
+ int lsp_overrides;
+ char _pad[CBM_CACHE_LINE - sizeof(cbm_gbuf_t *) - ((PP_RING + 1) * sizeof(int))];
} resolve_worker_state_t;
typedef struct {
@@ -1447,43 +1453,6 @@ static void try_field_type_hint(resolve_ctx_t *rc, cbm_resolution_t *res, const
}
}
-/* Look up an LSP-resolved override for this call site.
- * Returns a cbm_resolution_t with non-empty qualified_name on hit, empty on
- * miss. See src/pipeline/pass_calls.c for the rationale. */
-static cbm_resolution_t lsp_override_resolution_pp(const CBMFileResult *result,
- const CBMCall *call) {
- cbm_resolution_t empty = {0};
- if (!result || !call || !call->callee_name || !call->enclosing_func_qn) return empty;
- if (result->resolved_calls.count == 0) return empty;
- const char *short_name = call->callee_name;
- const char *p = call->callee_name;
- for (; *p; p++) {
- if (*p == '.') short_name = p + 1;
- }
- if (!short_name || !*short_name) return empty;
- size_t snlen = strlen(short_name);
-
- const CBMResolvedCall *best = NULL;
- for (int i = 0; i < result->resolved_calls.count; i++) {
- const CBMResolvedCall *rc2 = &result->resolved_calls.items[i];
- if (!rc2->caller_qn || !rc2->callee_qn) continue;
- if (strcmp(rc2->caller_qn, call->enclosing_func_qn) != 0) continue;
- size_t qlen = strlen(rc2->callee_qn);
- if (qlen <= snlen) continue;
- if (rc2->callee_qn[qlen - snlen - 1] != '.') continue;
- if (strcmp(rc2->callee_qn + qlen - snlen, short_name) != 0) continue;
- if (rc2->confidence < 0.5f) continue;
- if (!best || rc2->confidence > best->confidence) best = rc2;
- }
- if (!best) return empty;
- cbm_resolution_t r = {0};
- r.qualified_name = best->callee_qn;
- r.strategy = best->strategy ? best->strategy : "lsp_override";
- r.confidence = (double)best->confidence;
- r.candidate_count = 1;
- return r;
-}
-
/* Resolve calls for one file and emit CALLS/HTTP_CALLS/ASYNC_CALLS edges. */
static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CBMFileResult *result,
const char *rel, const char *module_qn, const char **imp_keys,
@@ -1499,8 +1468,21 @@ static void resolve_file_calls(resolve_ctx_t *rc, resolve_worker_state_t *ws, CB
continue;
}
- cbm_resolution_t res = lsp_override_resolution_pp(result, call);
- if (!res.qualified_name || res.qualified_name[0] == '\0') {
+ /* LSP-resolved calls take precedence over registry textual matching.
+ * Same helper + same CBM_LSP_CONFIDENCE_FLOOR as the sequential
+ * pipeline (pass_calls.c) — both paths must admit the same set of
+ * LSP overrides so a project doesn't get different attributions
+ * depending on whether parallel mode kicked in. */
+ cbm_resolution_t res = {0};
+ const CBMResolvedCall *lsp =
+ cbm_pipeline_find_lsp_resolution(&result->resolved_calls, call);
+ if (lsp) {
+ res.qualified_name = lsp->callee_qn;
+ res.strategy = lsp->strategy ? lsp->strategy : "lsp_override";
+ res.confidence = (double)lsp->confidence;
+ res.candidate_count = 1;
+ ws->lsp_overrides++;
+ } else {
res = cbm_registry_resolve(rc->registry, call->callee_name, module_qn,
imp_keys, imp_vals, imp_count);
}
@@ -1804,12 +1786,14 @@ int cbm_parallel_resolve(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
int total_calls = 0;
int total_usages = 0;
int total_semantic = 0;
+ int total_lsp_overrides = 0;
for (int i = 0; i < worker_count; i++) {
if (workers[i].local_edge_buf) {
cbm_gbuf_merge(ctx->gbuf, workers[i].local_edge_buf);
total_calls += workers[i].calls_resolved;
total_usages += workers[i].usages_resolved;
total_semantic += workers[i].semantic_resolved;
+ total_lsp_overrides += workers[i].lsp_overrides;
cbm_gbuf_free(workers[i].local_edge_buf);
}
}
@@ -1826,6 +1810,7 @@ int cbm_parallel_resolve(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
}
cbm_log_info("parallel.resolve.done", "calls", itoa_log(total_calls), "usages",
- itoa_log(total_usages), "semantic", itoa_log(total_semantic + go_impl));
+ itoa_log(total_usages), "semantic", itoa_log(total_semantic + go_impl),
+ "lsp_overrides", itoa_log(total_lsp_overrides));
return 0;
}
diff --git a/tests/test_parallel.c b/tests/test_parallel.c
index aaf5e2ec0..ae5171f68 100644
--- a/tests/test_parallel.c
+++ b/tests/test_parallel.c
@@ -428,6 +428,98 @@ TEST(gbuf_next_id_accessors) {
PASS();
}
+/* ── Parallel-pipeline LSP-override regression ────────────────────── */
+/* Pin the wiring fix that unified pass_calls.c (sequential) and
+ * pass_parallel.c (parallel) on cbm_pipeline_find_lsp_resolution +
+ * CBM_LSP_CONFIDENCE_FLOOR (lsp_resolve.h). Before the unification, the
+ * parallel path carried its own lsp_override_resolution_pp at floor 0.5
+ * while the sequential path used find_lsp_resolution at floor 0.6, so a
+ * project produced different CALLS edge attributions depending on which
+ * pipeline mode kicked in. This test indexes a small Python repo via
+ * the parallel pipeline and asserts at least one resulting CALLS edge
+ * carries an "lsp_*" strategy — proof the parallel path consults
+ * result->resolved_calls and emits LSP-attributed edges. */
+
+typedef struct {
+ int lsp_strategy_count;
+ int total_calls;
+} lsp_edge_count_ctx_t;
+
+static void count_lsp_call_edges(const cbm_gbuf_edge_t *edge, void *ud) {
+ lsp_edge_count_ctx_t *c = ud;
+ if (!edge || !edge->type || strcmp(edge->type, "CALLS") != 0) {
+ return;
+ }
+ c->total_calls++;
+ if (edge->properties_json && strstr(edge->properties_json, "\"strategy\":\"lsp")) {
+ c->lsp_strategy_count++;
+ }
+}
+
+TEST(parallel_python_lsp_override_emits_lsp_strategy_edges) {
+ char tmpdir[256];
+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_par_pylsp_XXXXXX");
+ if (!cbm_mkdtemp(tmpdir)) {
+ SKIP("mkdtemp failed");
+ }
+
+ /* Single-file scenario on purpose. cbm_run_py_lsp runs per-file in
+ * the parallel pipeline, so cross-file type inference doesn't fire
+ * during cbm_parallel_extract; the type registry only sees the
+ * current file's own defs. With Greeter and main() in the same file,
+ * py_lsp can register Greeter from the file's own defs, type the
+ * `g = Greeter()` constructor as NAMED("…Greeter"), and resolve
+ * `g.hello()` to Greeter.hello via attribute lookup — yielding a
+ * resolved_calls entry whose callee_qn matches the gbuf node QN
+ * exactly. The cross-file scenario lights up the same wiring code
+ * (lsp_overrides counter increments, confirmed in earlier runs) but
+ * the resulting edge gets dropped at cbm_gbuf_find_by_qn because the
+ * per-file py_lsp emits an unprefixed module path that doesn't match
+ * the project-qualified gbuf QN — that's a separate cross-file bug
+ * tracked elsewhere, not what this test is pinning. */
+ char fpath0[512];
+ snprintf(fpath0, sizeof(fpath0), "%s/app.py", tmpdir);
+ FILE *f = fopen(fpath0, "w");
+ if (!f) {
+ SKIP("fopen app.py failed");
+ }
+ fprintf(f, "class Greeter:\n"
+ " def hello(self):\n"
+ " return 'hi'\n"
+ "\n"
+ "def main():\n"
+ " g = Greeter()\n"
+ " g.hello()\n");
+ fclose(f);
+
+ cbm_file_info_t files[1] = {0};
+ files[0].path = fpath0;
+ files[0].rel_path = (char *)"app.py";
+ files[0].language = CBM_LANG_PYTHON;
+
+ cbm_gbuf_t *gbuf = run_parallel("cbm_par_pylsp", tmpdir, files, 1, 1);
+ ASSERT_NOT_NULL(gbuf);
+
+ lsp_edge_count_ctx_t c = {0};
+ cbm_gbuf_foreach_edge(gbuf, count_lsp_call_edges, &c);
+
+ /* Sanity: extraction produced at least one call edge. */
+ ASSERT_GT(c.total_calls, 0);
+ /* The parallel pipeline must surface at least one LSP-attributed
+ * CALLS edge. This proves the unified cbm_pipeline_find_lsp_resolution
+ * (shared with pass_calls.c at floor 0.6) is actually consulted in
+ * the parallel pipeline, and that the resulting edge is emitted with
+ * the LSP strategy intact rather than overwritten by the registry
+ * fallback. */
+ ASSERT_GT(c.lsp_strategy_count, 0);
+
+ cbm_gbuf_free(gbuf);
+
+ unlink(fpath0);
+ rmdir(tmpdir);
+ PASS();
+}
+
/* ── Suite Registration ──────────────────────────────────────────── */
SUITE(parallel) {
@@ -441,6 +533,7 @@ SUITE(parallel) {
/* Parallel pipeline parity tests */
RUN_TEST(parallel_node_count);
+ RUN_TEST(parallel_python_lsp_override_emits_lsp_strategy_edges);
RUN_TEST(parallel_calls_parity);
RUN_TEST(parallel_defines_parity);
RUN_TEST(parallel_defines_method_parity);