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.

feat(pipeline): wire cross-file LSP across all five hybrid resolvers

Martin Vogelb2a71b80b991
diff --git a/Makefile.cbm b/Makefile.cbm
index 30a8ec055..ece9d7c86 100644
--- a/Makefile.cbm
+++ b/Makefile.cbm
@@ -179,6 +179,7 @@ PIPELINE_SRCS = \
src/pipeline/pass_parallel.c \
src/pipeline/pass_definitions.c \
src/pipeline/pass_calls.c \
+ src/pipeline/pass_lsp_cross.c \
src/pipeline/pass_usages.c \
src/pipeline/pass_semantic.c \
src/pipeline/pass_tests.c \
diff --git a/internal/cbm/lsp/php_lsp.c b/internal/cbm/lsp/php_lsp.c
index fce7172dc..84436e413 100644
--- a/internal/cbm/lsp/php_lsp.c
+++ b/internal/cbm/lsp/php_lsp.c
@@ -3716,3 +3716,241 @@ void cbm_run_php_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
}
}
}
+
+/* ── Cross-file + batch ───────────────────────────────────────── */
+
+/* Split a "|"-separated list into a NULL-terminated array of arena copies. */
+static const char **php_split_pipe(CBMArena *arena, const char *text) {
+ if (!text || !text[0]) return NULL;
+ int count = 1;
+ for (const char *p = text; *p; p++) if (*p == '|') count++;
+ const char **out = (const char **)cbm_arena_alloc(arena,
+ (size_t)(count + 1) * sizeof(const char *));
+ if (!out) return NULL;
+ int idx = 0;
+ const char *start = text;
+ for (const char *p = text;; p++) {
+ if (*p == '|' || *p == '\0') {
+ size_t n = (size_t)(p - start);
+ char *s = (char *)cbm_arena_alloc(arena, n + 1);
+ if (!s) return NULL;
+ memcpy(s, start, n);
+ s[n] = '\0';
+ out[idx++] = s;
+ if (*p == '\0') break;
+ start = p + 1;
+ }
+ }
+ out[idx] = NULL;
+ return out;
+}
+
+/* Build a registry from caller-supplied CBMLSPDef[] — covers both the source
+ * file's own defs and cross-file referenced defs. PHP labels recognised:
+ * Class / Interface / Trait / Enum / Type for types; Function / Method for
+ * functions. Return type strings are parsed via parse_return_type_text using
+ * each def's def_module_qn so bare type names qualify against the module
+ * where the def lives, not the importer's module. */
+static void php_register_lsp_defs(CBMArena *arena, CBMTypeRegistry *reg,
+ CBMLSPDef *defs, int def_count) {
+ for (int i = 0; i < def_count; i++) {
+ CBMLSPDef *d = &defs[i];
+ if (!d->qualified_name || !d->short_name || !d->label) continue;
+
+ if (strcmp(d->label, "Class") == 0 || strcmp(d->label, "Interface") == 0 ||
+ strcmp(d->label, "Trait") == 0 || strcmp(d->label, "Enum") == 0 ||
+ strcmp(d->label, "Type") == 0) {
+ CBMRegisteredType rt;
+ memset(&rt, 0, sizeof(rt));
+ rt.qualified_name = cbm_arena_strdup(arena, d->qualified_name);
+ rt.short_name = cbm_arena_strdup(arena, d->short_name);
+ rt.is_interface = d->is_interface ||
+ strcmp(d->label, "Interface") == 0;
+ rt.embedded_types = php_split_pipe(arena, d->embedded_types);
+ if (d->method_names_str && d->method_names_str[0]) {
+ rt.method_names = php_split_pipe(arena, d->method_names_str);
+ }
+ cbm_registry_add_type(reg, rt);
+ }
+
+ if (strcmp(d->label, "Function") == 0 || strcmp(d->label, "Method") == 0) {
+ CBMRegisteredFunc rf;
+ memset(&rf, 0, sizeof(rf));
+ rf.qualified_name = cbm_arena_strdup(arena, d->qualified_name);
+ rf.short_name = cbm_arena_strdup(arena, d->short_name);
+
+ /* Build FUNC type from "|"-separated return-type texts. Each piece
+ * is the unqualified return-type expression as it appears in the
+ * def-defining module's source. parse_return_type_text qualifies
+ * bare type names against the def's own module_qn. */
+ const char **ret_strs = php_split_pipe(arena, d->return_types);
+ const CBMType **ret_types = NULL;
+ if (ret_strs) {
+ int n = 0;
+ while (ret_strs[n]) n++;
+ if (n > 0) {
+ ret_types = (const CBMType **)cbm_arena_alloc(arena,
+ (size_t)(n + 1) * sizeof(const CBMType *));
+ if (ret_types) {
+ const char *def_mod = d->def_module_qn ? d->def_module_qn : "";
+ for (int j = 0; j < n; j++) {
+ ret_types[j] = parse_return_type_text(
+ arena, ret_strs[j], def_mod, NULL, NULL, NULL, 0);
+ }
+ ret_types[n] = NULL;
+ }
+ }
+ }
+ rf.signature = cbm_type_func(arena, NULL, NULL, ret_types);
+
+ if (strcmp(d->label, "Method") == 0 && d->receiver_type && d->receiver_type[0]) {
+ rf.receiver_type = cbm_arena_strdup(arena, d->receiver_type);
+ /* Auto-register receiver type if cross-file def chain didn't
+ * include it explicitly, so php_lookup_method's chain walk
+ * has somewhere to land. */
+ if (!cbm_registry_lookup_type(reg, rf.receiver_type)) {
+ CBMRegisteredType auto_t;
+ memset(&auto_t, 0, sizeof(auto_t));
+ auto_t.qualified_name = rf.receiver_type;
+ const char *dot = strrchr(d->receiver_type, '.');
+ auto_t.short_name = dot
+ ? cbm_arena_strdup(arena, dot + 1)
+ : rf.receiver_type;
+ cbm_registry_add_type(reg, auto_t);
+ }
+ }
+ cbm_registry_add_func(reg, rf);
+ }
+ }
+}
+
+void cbm_run_php_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) {
+ if (!arena || !source || source_len <= 0 || !out) return;
+
+ TSParser *parser = NULL;
+ TSTree *tree = cached_tree;
+ bool owns_tree = false;
+ if (!tree) {
+ parser = ts_parser_new();
+ if (!parser) return;
+ ts_parser_set_language(parser, tree_sitter_php_only());
+ tree = ts_parser_parse_string(parser, NULL, source, (uint32_t)source_len);
+ owns_tree = true;
+ if (!tree) {
+ ts_parser_delete(parser);
+ return;
+ }
+ }
+ TSNode root = ts_tree_root_node(tree);
+
+ CBMTypeRegistry reg;
+ cbm_registry_init(&reg, arena);
+ cbm_php_stdlib_register(&reg, arena);
+ php_register_lsp_defs(arena, &reg, defs, def_count);
+
+ PHPLSPContext ctx;
+ php_lsp_init(&ctx, arena, source, source_len, &reg, module_qn, out);
+
+ /* Caller-supplied imports register first. process_file's own AST walk
+ * adds file-internal `use` declarations on top of these. */
+ for (int i = 0; i < import_count; i++) {
+ if (import_names && import_qns && import_names[i] && import_qns[i]) {
+ php_lsp_add_use(&ctx, import_names[i], import_qns[i], CBM_PHP_USE_CLASS);
+ }
+ }
+
+ /* Class field collection — same flow as cbm_run_php_lsp. Walks the AST
+ * to populate typed-property field maps so $this->prop and $obj->prop
+ * resolve to the right type during the call walk. */
+ {
+ php_class_field_table_t tab = {0};
+ uint32_t nc = ts_node_child_count(root);
+ for (uint32_t i = 0; i < nc; i++) {
+ TSNode c = ts_node_child(root, i);
+ if (ts_node_is_null(c)) continue;
+ const char *k = ts_node_type(c);
+ if (strcmp(k, "namespace_definition") == 0) {
+ set_namespace_from_decl(&ctx, c);
+ } else if (strcmp(k, "namespace_use_declaration") == 0) {
+ collect_use_declaration(&ctx, c);
+ }
+ }
+ php_lsp_collect_class_fields(&ctx, &reg, root, &tab);
+ ctx.current_namespace_qn = "";
+ /* Reset to caller-supplied uses only — process_file re-adds AST uses. */
+ ctx.use_count = import_count;
+ for (int i = 0; i < tab.count; i++) {
+ php_class_fields_t *f = &tab.items[i];
+ if (f->count == 0) continue;
+ for (int t = 0; t < reg.type_count; t++) {
+ if (strcmp(reg.types[t].qualified_name, f->class_qn) == 0) {
+ reg.types[t].field_names = f->field_names;
+ reg.types[t].field_types = f->field_types;
+ break;
+ }
+ }
+ }
+ }
+
+ php_lsp_process_file(&ctx, root);
+
+ if (owns_tree && tree) ts_tree_delete(tree);
+ if (parser) ts_parser_delete(parser);
+}
+
+void cbm_batch_php_lsp_cross(
+ CBMArena *arena,
+ CBMBatchPHPLSPFile *files, int file_count,
+ CBMResolvedCallArray *out) {
+ if (!arena || !files || file_count <= 0 || !out) return;
+
+ for (int f = 0; f < file_count; f++) {
+ CBMBatchPHPLSPFile *file = &files[f];
+ memset(&out[f], 0, sizeof(CBMResolvedCallArray));
+ if (!file->source || file->source_len <= 0) continue;
+
+ CBMArena file_arena;
+ cbm_arena_init(&file_arena);
+
+ CBMResolvedCallArray file_out;
+ memset(&file_out, 0, sizeof(file_out));
+
+ cbm_run_php_lsp_cross(&file_arena,
+ file->source, file->source_len, file->module_qn,
+ file->defs, file->def_count,
+ file->import_names, file->import_qns, file->import_count,
+ file->cached_tree, &file_out);
+
+ if (file_out.count > 0) {
+ out[f].count = file_out.count;
+ out[f].items = (CBMResolvedCall *)cbm_arena_alloc(arena,
+ (size_t)file_out.count * sizeof(CBMResolvedCall));
+ if (out[f].items) {
+ for (int j = 0; j < file_out.count; j++) {
+ CBMResolvedCall *src = &file_out.items[j];
+ CBMResolvedCall *dst = &out[f].items[j];
+ dst->caller_qn = src->caller_qn
+ ? cbm_arena_strdup(arena, src->caller_qn) : NULL;
+ dst->callee_qn = src->callee_qn
+ ? cbm_arena_strdup(arena, src->callee_qn) : NULL;
+ dst->strategy = src->strategy
+ ? cbm_arena_strdup(arena, src->strategy) : NULL;
+ dst->confidence = src->confidence;
+ dst->reason = src->reason
+ ? cbm_arena_strdup(arena, src->reason) : NULL;
+ }
+ } else {
+ out[f].count = 0;
+ }
+ }
+
+ cbm_arena_destroy(&file_arena);
+ }
+}
diff --git a/internal/cbm/lsp/php_lsp.h b/internal/cbm/lsp/php_lsp.h
index 4fdd2611b..354c98565 100644
--- a/internal/cbm/lsp/php_lsp.h
+++ b/internal/cbm/lsp/php_lsp.h
@@ -5,6 +5,7 @@
#include "scope.h"
#include "type_registry.h"
#include "../cbm.h"
+#include "go_lsp.h" /* CBMLSPDef reused across languages */
/* PHPLSPContext — per-file state for PHP type-aware call resolution.
* Mirrors GoLSPContext / CLSPContext structure. */
@@ -86,4 +87,43 @@ void cbm_run_php_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
/* Register PHP stdlib + curated framework types into a registry. */
void cbm_php_stdlib_register(CBMTypeRegistry *reg, CBMArena *arena);
+/* --- Cross-file LSP resolution ---
+ *
+ * Mirrors cbm_run_py_lsp_cross / cbm_run_ts_lsp_cross. Caller supplies the
+ * combined CBMLSPDef[] (file-local + cross-file) and a resolved import map
+ * (use → target QN). Imports are added as CLASS-kind uses; file-internal
+ * `use` declarations from the AST are layered on top by process_file.
+ *
+ * Reuses go_lsp.h's CBMLSPDef so cross-language registration is uniform. */
+void cbm_run_php_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 --- */
+
+/* Per-file input for batch PHP LSP processing. */
+typedef struct {
+ const char *source;
+ int source_len;
+ const char *module_qn;
+ TSTree *cached_tree; /* 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;
+} CBMBatchPHPLSPFile;
+
+/* Process multiple PHP files' cross-file LSP in one call. out must point to
+ * file_count pre-zeroed CBMResolvedCallArray structs. */
+void cbm_batch_php_lsp_cross(
+ CBMArena *arena,
+ CBMBatchPHPLSPFile *files, int file_count,
+ CBMResolvedCallArray *out);
+
#endif /* CBM_LSP_PHP_LSP_H */
diff --git a/src/pipeline/pass_lsp_cross.c b/src/pipeline/pass_lsp_cross.c
new file mode 100644
index 000000000..20809b716
--- /dev/null
+++ b/src/pipeline/pass_lsp_cross.c
@@ -0,0 +1,482 @@
+/*
+ * pass_lsp_cross.c — Cross-file LSP type-aware call resolution pass.
+ *
+ * See pass_lsp_cross.h for the high-level contract. This file is the
+ * pipeline glue that converts the existing per-file extraction state
+ * (CBMDefinition / CBMImport / IMPORTS-edge gbuf state) into the input
+ * shape each language LSP's cbm_run_X_lsp_cross expects, then merges
+ * the resulting CBMResolvedCall entries back into per-file results.
+ *
+ * The pass is a no-op for any file whose CBMFileResult is missing or
+ * whose language has no cross-file LSP entry registered (e.g. Rust /
+ * Java today). Per-LSP emit functions dedup against entries already in
+ * resolved_calls, so this pass is also idempotent — safe to invoke
+ * multiple times if the pipeline gains a re-run path later.
+ */
+#include "pipeline/pass_lsp_cross.h"
+#include "pipeline/pipeline_internal.h"
+#include "lsp/go_lsp.h"
+#include "lsp/c_lsp.h"
+#include "lsp/py_lsp.h"
+#include "lsp/ts_lsp.h"
+#include "lsp/php_lsp.h"
+#include "graph_buffer/graph_buffer.h"
+#include "foundation/constants.h"
+#include "foundation/log.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* ── Constants ─────────────────────────────────────────────────── */
+
+enum {
+ PXC_MAX_FILE_BYTES_FACTOR = 100, /* same cap pass_calls.c uses for source size */
+ PXC_ITOA_BUF = 16,
+};
+
+/* Format an int into a thread-local rotating buffer for log key=value emission.
+ * Mirrors the itoa_log helper in pass_calls.c — kept local so passes don't
+ * grow a foundation-wide formatting API just for log output. */
+static const char *itoa_buf(int val) {
+ static _Thread_local char bufs[PXC_ITOA_BUF][PXC_ITOA_BUF];
+ static _Thread_local int slot = 0;
+ char *out = bufs[slot];
+ slot = (slot + 1) & (PXC_ITOA_BUF - 1);
+ snprintf(out, PXC_ITOA_BUF, "%d", val);
+ return out;
+}
+
+/* ── Local helpers ─────────────────────────────────────────────── */
+
+/* Slurp a file into a malloc'd, NUL-terminated buffer. Mirrors the
+ * read_file helper in pass_calls.c / pass_parallel.c (kept local so the
+ * pipeline doesn't grow a public read-file API just for this pass). */
+static char *pxc_read_file(const char *path, int *out_len) {
+ FILE *f = fopen(path, "rb");
+ if (!f) return NULL;
+ (void)fseek(f, 0, SEEK_END);
+ long size = ftell(f);
+ (void)fseek(f, 0, SEEK_SET);
+ if (size <= 0 ||
+ size > (long)PXC_MAX_FILE_BYTES_FACTOR * (long)CBM_SZ_1K * (long)CBM_SZ_1K) {
+ (void)fclose(f);
+ return NULL;
+ }
+ char *buf = (char *)malloc((size_t)size + 1);
+ if (!buf) {
+ (void)fclose(f);
+ return NULL;
+ }
+ size_t nread = fread(buf, 1, (size_t)size, f);
+ (void)fclose(f);
+ if (nread > (size_t)size) nread = (size_t)size;
+ buf[nread] = '\0';
+ *out_len = (int)nread;
+ return buf;
+}
+
+/* Map a CBMDefinition.label to a CBMLSPDef.label. Per-language LSP
+ * registrars only care about Class/Interface/Trait/Enum/Type/Protocol/
+ * Function/Method — variables, modules, decorators, etc. are skipped. */
+static const char *pxc_map_label(const char *label) {
+ if (!label) return NULL;
+ if (strcmp(label, "Class") == 0 ||
+ strcmp(label, "Interface") == 0 ||
+ strcmp(label, "Trait") == 0 ||
+ strcmp(label, "Enum") == 0 ||
+ strcmp(label, "Type") == 0 ||
+ strcmp(label, "Protocol") == 0 ||
+ strcmp(label, "Function") == 0 ||
+ strcmp(label, "Method") == 0) {
+ return label;
+ }
+ return NULL;
+}
+
+/* Build the embedded_types "|"-separated string from base_classes[].
+ * Returns NULL when there are no bases. Allocated in the supplied arena. */
+static const char *pxc_join_pipe(CBMArena *arena, const char *const *items) {
+ if (!items || !items[0]) return NULL;
+ int count = 0;
+ size_t total = 0;
+ for (int i = 0; items[i]; i++) {
+ count++;
+ total += strlen(items[i]);
+ }
+ if (count == 0) return NULL;
+ /* count - 1 separators + NUL. */
+ size_t bufsz = total + (size_t)(count - 1) + 1;
+ char *buf = (char *)cbm_arena_alloc(arena, bufsz);
+ if (!buf) return NULL;
+ char *p = buf;
+ for (int i = 0; i < count; i++) {
+ size_t n = strlen(items[i]);
+ memcpy(p, items[i], n);
+ p += n;
+ if (i + 1 < count) *p++ = '|';
+ }
+ *p = '\0';
+ return buf;
+}
+
+/* Convert one CBMDefinition into a CBMLSPDef. Returns 0 on success, -1
+ * to skip (unsupported label or missing required field). dst gets borrowed
+ * pointers into src and into `arena` for synthesised composites. */
+static int pxc_build_lsp_def(CBMArena *arena, const CBMDefinition *src,
+ const char *module_qn, CBMLSPDef *dst) {
+ const char *label = pxc_map_label(src->label);
+ if (!label || !src->qualified_name || !src->name) return -1;
+ memset(dst, 0, sizeof(*dst));
+ dst->qualified_name = src->qualified_name;
+ dst->short_name = src->name;
+ dst->label = label;
+ dst->receiver_type = src->parent_class;
+ dst->def_module_qn = module_qn;
+ dst->is_interface = (strcmp(label, "Interface") == 0 ||
+ strcmp(label, "Protocol") == 0);
+ /* Single return-type string. The per-language registrars split on '|'
+ * for multi-return languages (Go); single-return languages just see one
+ * piece, which is what's already stored. */
+ dst->return_types = src->return_type;
+ dst->embedded_types = pxc_join_pipe(arena, src->base_classes);
+ return 0;
+}
+
+/* Collect a project-wide CBMLSPDef[] from all cached results. Returns a
+ * malloc'd array (caller frees) of length *out_count. String fields are
+ * borrowed from cache[i]->arena and from def_modules[i] (also borrowed). */
+static CBMLSPDef *pxc_collect_all_defs(CBMFileResult **cache,
+ const cbm_file_info_t *files, int file_count,
+ const char *project_name,
+ char **def_modules, int *out_count) {
+ int total = 0;
+ for (int i = 0; i < file_count; i++) {
+ if (cache[i]) total += cache[i]->defs.count;
+ }
+ if (total == 0) {
+ *out_count = 0;
+ return NULL;
+ }
+ CBMLSPDef *defs = (CBMLSPDef *)calloc((size_t)total, sizeof(CBMLSPDef));
+ if (!defs) {
+ *out_count = 0;
+ return NULL;
+ }
+ int idx = 0;
+ for (int fi = 0; fi < file_count; fi++) {
+ if (!cache[fi]) continue;
+ if (!def_modules[fi]) {
+ def_modules[fi] = cbm_pipeline_fqn_module(project_name, files[fi].rel_path);
+ }
+ for (int di = 0; di < cache[fi]->defs.count; di++) {
+ if (pxc_build_lsp_def(&cache[fi]->arena,
+ &cache[fi]->defs.items[di],
+ def_modules[fi],
+ &defs[idx]) == 0) {
+ idx++;
+ }
+ }
+ }
+ *out_count = idx;
+ return defs;
+}
+
+/* Build per-file import map (local_name -> resolved module QN) from gbuf
+ * IMPORTS edges. Mirrors build_import_map() in pass_parallel.c. Returns 0
+ * with *out_count = 0 when the file has no IMPORTS edges. Caller frees keys
+ * with pxc_free_import_map. */
+static int pxc_build_import_map(const cbm_gbuf_t *gbuf, const char *project_name,
+ const char *rel_path, const char ***out_keys,
+ const char ***out_vals, int *out_count) {
+ *out_keys = NULL;
+ *out_vals = NULL;
+ *out_count = 0;
+
+ char *file_qn = cbm_pipeline_fqn_compute(project_name, rel_path, "__file__");
+ if (!file_qn) return 0;
+ const cbm_gbuf_node_t *file_node = cbm_gbuf_find_by_qn(gbuf, file_qn);
+ free(file_qn);
+ if (!file_node) return 0;
+
+ const cbm_gbuf_edge_t **edges = NULL;
+ int edge_count = 0;
+ int rc = cbm_gbuf_find_edges_by_source_type(gbuf, file_node->id, "IMPORTS",
+ &edges, &edge_count);
+ if (rc != 0 || edge_count == 0) return 0;
+
+ const char **keys = (const char **)calloc((size_t)edge_count, sizeof(const char *));
+ const char **vals = (const char **)calloc((size_t)edge_count, sizeof(const char *));
+ if (!keys || !vals) {
+ free(keys);
+ free(vals);
+ return 0;
+ }
+ int count = 0;
+ for (int i = 0; i < edge_count; i++) {
+ const cbm_gbuf_edge_t *e = edges[i];
+ const cbm_gbuf_node_t *target = cbm_gbuf_find_by_id(gbuf, e->target_id);
+ if (!target || !e->properties_json) continue;
+ const char *start = strstr(e->properties_json, "\"local_name\":\"");
+ if (!start) continue;
+ start += strlen("\"local_name\":\"");
+ const char *end = strchr(start, '"');
+ if (!end || end <= start) continue;
+ size_t n = (size_t)(end - start);
+ char *local = (char *)malloc(n + 1);
+ if (!local) continue;
+ memcpy(local, start, n);
+ local[n] = '\0';
+ keys[count] = local;
+ vals[count] = target->qualified_name; /* borrowed from gbuf */
+ count++;
+ }
+ *out_keys = keys;
+ *out_vals = vals;
+ *out_count = count;
+ return 0;
+}
+
+static void pxc_free_import_map(const char **keys, const char **vals, int count) {
+ if (keys) {
+ for (int i = 0; i < count; i++) free((void *)keys[i]);
+ free((void *)keys);
+ }
+ free((void *)vals); /* vals strings borrowed from gbuf — don't free elements */
+}
+
+/* Detect TS dialect flags from a relative path. */
+static void pxc_ts_modes(CBMLanguage lang, const char *rel_path,
+ bool *out_js, bool *out_jsx, bool *out_dts) {
+ *out_js = (lang == CBM_LANG_JAVASCRIPT);
+ *out_jsx = (lang == CBM_LANG_TSX);
+ *out_dts = false;
+ if (!rel_path) return;
+ size_t rl = strlen(rel_path);
+ if (lang == CBM_LANG_JAVASCRIPT && rl >= 4 &&
+ strcmp(rel_path + rl - 4, ".jsx") == 0) {
+ *out_jsx = true;
+ }
+ if (lang == CBM_LANG_TYPESCRIPT && rl >= 5 &&
+ strcmp(rel_path + rl - 5, ".d.ts") == 0) {
+ *out_dts = true;
+ }
+}
+
+/* Returns true when this language has a cross-file LSP wired up. */
+static bool pxc_has_cross_lsp(CBMLanguage lang) {
+ switch (lang) {
+ case CBM_LANG_GO:
+ case CBM_LANG_C:
+ case CBM_LANG_CPP:
+ case CBM_LANG_CUDA:
+ case CBM_LANG_PYTHON:
+ case CBM_LANG_JAVASCRIPT:
+ case CBM_LANG_TYPESCRIPT:
+ case CBM_LANG_TSX:
+ case CBM_LANG_PHP:
+ return true;
+ default:
+ return false;
+ }
+}
+
+/* True when (caller_qn, callee_qn) is already present in arr. Mirrors the
+ * intra-LSP dedup in py_emit_resolved_call / ts_emit_resolved_call so that
+ * cross-file entries duplicating per-file output get dropped at append. */
+static bool pxc_already_resolved(const CBMResolvedCallArray *arr,
+ const char *caller_qn, const char *callee_qn) {
+ if (!arr || !caller_qn || !callee_qn) return false;
+ for (int i = 0; i < arr->count; i++) {
+ const CBMResolvedCall *rc = &arr->items[i];
+ if (rc->caller_qn && rc->callee_qn &&
+ strcmp(rc->caller_qn, caller_qn) == 0 &&
+ strcmp(rc->callee_qn, callee_qn) == 0) {
+ return true;
+ }
+ }
+ return false;
+}
+
+/* Append cross-file results from `src_out` (allocated in a scratch arena
+ * about to be destroyed) into `dst_calls` (lives in cache_entry->arena),
+ * copying every string field into dst_arena. Skips entries whose
+ * (caller_qn, callee_qn) is already in dst_calls — avoids inflating the
+ * array with cross-file duplicates of per-file LSP output. */
+static void pxc_append_results(CBMArena *dst_arena, CBMResolvedCallArray *dst_calls,
+ const CBMResolvedCallArray *src_out) {
+ if (!dst_calls || !src_out) return;
+ for (int j = 0; j < src_out->count; j++) {
+ const CBMResolvedCall *src = &src_out->items[j];
+ if (!src->caller_qn || !src->callee_qn) continue;
+ if (pxc_already_resolved(dst_calls, src->caller_qn, src->callee_qn)) continue;
+ CBMResolvedCall dst;
+ memset(&dst, 0, sizeof(dst));
+ dst.caller_qn = cbm_arena_strdup(dst_arena, src->caller_qn);
+ dst.callee_qn = cbm_arena_strdup(dst_arena, src->callee_qn);
+ dst.strategy = src->strategy ? cbm_arena_strdup(dst_arena, src->strategy) : NULL;
+ dst.confidence = src->confidence;
+ dst.reason = src->reason ? cbm_arena_strdup(dst_arena, src->reason) : NULL;
+ cbm_resolvedcall_push(dst_calls, dst_arena, dst);
+ }
+}
+
+/* Run cross-file LSP for a single file inside a scratch arena that gets
+ * freed when the call returns. The LSP would otherwise allocate a fresh
+ * type registry + stdlib + all project defs into the supplied arena, and
+ * that adds up to O(N×project_size) memory if we used cache[i]->arena
+ * directly across N files (test_incremental.c saw 3.5 GB peak on a
+ * 1100-file repo before this fix). Output gets copied into the file's own
+ * arena and merged into result->resolved_calls. */
+static void pxc_run_one(CBMLanguage lang, CBMFileResult *r, const char *source,
+ int source_len, const char *module_qn,
+ CBMLSPDef *defs, int def_count,
+ const char **imp_names, const char **imp_qns, int imp_count) {
+ TSTree *tree = r->cached_tree; /* may be NULL — LSP re-parses then */
+
+ CBMArena scratch;
+ cbm_arena_init(&scratch);
+ CBMResolvedCallArray out;
+ memset(&out, 0, sizeof(out));
+
+ switch (lang) {
+ case CBM_LANG_GO:
+ cbm_run_go_lsp_cross(&scratch, source, source_len, module_qn,
+ defs, def_count, imp_names, imp_qns, imp_count, tree,
+ &out);
+ break;
+ case CBM_LANG_C:
+ case CBM_LANG_CPP:
+ case CBM_LANG_CUDA: {
+ bool cpp_mode = (lang != CBM_LANG_C);
+ /* C/C++ cross LSP takes include_paths/include_ns_qns instead of
+ * imports — the existing pipeline doesn't carry C-style include
+ * resolution as a separate map, so pass NULL/0 and let the LSP
+ * fall back to its own #include scan. */
+ cbm_run_c_lsp_cross(&scratch, source, source_len, module_qn, cpp_mode,
+ defs, def_count, NULL, NULL, 0, tree,
+ &out);
+ break;
+ }
+ case CBM_LANG_PYTHON:
+ cbm_run_py_lsp_cross(&scratch, source, source_len, module_qn,
+ defs, def_count, imp_names, imp_qns, imp_count, tree,
+ &out);
+ break;
+ case CBM_LANG_PHP:
+ cbm_run_php_lsp_cross(&scratch, source, source_len, module_qn,
+ defs, def_count, imp_names, imp_qns, imp_count, tree,
+ &out);
+ break;
+ default:
+ break;
+ }
+
+ pxc_append_results(&r->arena, &r->resolved_calls, &out);
+ cbm_arena_destroy(&scratch);
+}
+
+/* Variant of pxc_run_one for TS/JS/JSX/TSX with explicit dialect flags.
+ * Same scratch-arena lifecycle as pxc_run_one. */
+static void pxc_run_one_ts(CBMFileResult *r, const char *source, int source_len,
+ const char *module_qn,
+ CBMLSPDef *defs, int def_count,
+ const char **imp_names, const char **imp_qns, int imp_count,
+ bool js_mode, bool jsx_mode, bool dts_mode) {
+ CBMArena scratch;
+ cbm_arena_init(&scratch);
+ CBMResolvedCallArray out;
+ memset(&out, 0, sizeof(out));
+
+ cbm_run_ts_lsp_cross(&scratch, source, source_len, module_qn,
+ js_mode, jsx_mode, dts_mode,
+ defs, def_count, imp_names, imp_qns, imp_count,
+ r->cached_tree, &out);
+
+ pxc_append_results(&r->arena, &r->resolved_calls, &out);
+ cbm_arena_destroy(&scratch);
+}
+
+int cbm_pipeline_pass_lsp_cross(cbm_pipeline_ctx_t *ctx,
+ const cbm_file_info_t *files,
+ int file_count,
+ CBMFileResult **cache) {
+ if (!ctx || !files || file_count <= 0 || !cache) return 0;
+
+ cbm_log_info("pass.start", "pass", "lsp_cross", "files",
+ itoa_buf(file_count));
+
+ /* Per-file module QN cache so we don't recompute it once per def + once
+ * per call. cbm_pipeline_fqn_module mallocs; freed at end. */
+ char **def_modules = (char **)calloc((size_t)file_count, sizeof(char *));
+ if (!def_modules) {
+ cbm_log_error("pass.err", "pass", "lsp_cross", "phase", "alloc");
+ return 0;
+ }
+
+ int def_count = 0;
+ CBMLSPDef *all_defs = pxc_collect_all_defs(cache, files, file_count,
+ ctx->project_name, def_modules,
+ &def_count);
+
+ int processed = 0;
+ int skipped_no_lsp = 0;
+ int skipped_no_source = 0;
+ int per_lang_calls = 0;
+
+ for (int i = 0; i < file_count; i++) {
+ if (!cache[i]) continue;
+ CBMLanguage lang = files[i].language;
+ if (!pxc_has_cross_lsp(lang)) {
+ skipped_no_lsp++;
+ continue;
+ }
+
+ int source_len = 0;
+ char *source = pxc_read_file(files[i].path, &source_len);
+ if (!source || source_len <= 0) {
+ free(source);
+ skipped_no_source++;
+ continue;
+ }
+
+ if (!def_modules[i]) {
+ def_modules[i] = cbm_pipeline_fqn_module(ctx->project_name, files[i].rel_path);
+ }
+
+ const char **imp_keys = NULL;
+ const char **imp_vals = NULL;
+ int imp_count = 0;
+ pxc_build_import_map(ctx->gbuf, ctx->project_name, files[i].rel_path,
+ &imp_keys, &imp_vals, &imp_count);
+
+ if (lang == CBM_LANG_JAVASCRIPT || lang == CBM_LANG_TYPESCRIPT ||
+ lang == CBM_LANG_TSX) {
+ bool js, jsx, dts;
+ pxc_ts_modes(lang, files[i].rel_path, &js, &jsx, &dts);
+ pxc_run_one_ts(cache[i], source, source_len, def_modules[i],
+ all_defs, def_count, imp_keys, imp_vals, imp_count,
+ js, jsx, dts);
+ } else {
+ pxc_run_one(lang, cache[i], source, source_len, def_modules[i],
+ all_defs, def_count, imp_keys, imp_vals, imp_count);
+ }
+ per_lang_calls++;
+ processed++;
+
+ pxc_free_import_map(imp_keys, imp_vals, imp_count);
+ free(source);
+ }
+
+ free(all_defs);
+ for (int i = 0; i < file_count; i++) free(def_modules[i]);
+ free(def_modules);
+
+ cbm_log_info("pass.done", "pass", "lsp_cross",
+ "files_processed", itoa_buf(processed),
+ "files_skipped_no_lsp", itoa_buf(skipped_no_lsp),
+ "files_skipped_no_source", itoa_buf(skipped_no_source),
+ "defs_total", itoa_buf(def_count),
+ "lsp_calls", itoa_buf(per_lang_calls));
+ return 0;
+}
diff --git a/src/pipeline/pass_lsp_cross.h b/src/pipeline/pass_lsp_cross.h
new file mode 100644
index 000000000..b9857e0a5
--- /dev/null
+++ b/src/pipeline/pass_lsp_cross.h
@@ -0,0 +1,27 @@
+/*
+ * pass_lsp_cross.h — Cross-file LSP type-aware call resolution pass.
+ *
+ * Per-file LSP (cbm_run_X_lsp inside cbm_extract_file) only sees a single
+ * file's defs in its registry, so callees whose receiver type comes from
+ * an imported module stay unresolved. This pass closes that gap: for each
+ * indexed file, it builds a project-wide CBMLSPDef[] (covering ALL files'
+ * defs) plus the file's resolved import map (from gbuf IMPORTS edges),
+ * then calls the language-specific cbm_run_X_lsp_cross. The resulting
+ * CBMResolvedCall entries are appended to each file's resolved_calls so
+ * the call-resolution pass picks them up via the same shared helper
+ * (cbm_pipeline_find_lsp_resolution) that already handles per-file LSP.
+ *
+ * Languages covered: Go, C/C++/CUDA, Python, TypeScript/JavaScript/JSX/TSX,
+ * PHP. PHP cross-file landed alongside this pass — see cbm_run_php_lsp_cross.
+ *
+ * Public declaration of cbm_pipeline_pass_lsp_cross lives in
+ * pipeline_internal.h alongside the other pipeline passes; this header is
+ * a stub kept for source-file readability (mirrors the dedicated header
+ * pattern of pass_cross_repo.h, but no public API surface here).
+ */
+#ifndef CBM_PIPELINE_PASS_LSP_CROSS_H
+#define CBM_PIPELINE_PASS_LSP_CROSS_H
+
+#include "pipeline/pipeline_internal.h"
+
+#endif /* CBM_PIPELINE_PASS_LSP_CROSS_H */
diff --git a/src/pipeline/pipeline.c b/src/pipeline/pipeline.c
index 98da5bf74..396e59bfe 100644
--- a/src/pipeline/pipeline.c
+++ b/src/pipeline/pipeline.c
@@ -12,11 +12,12 @@
*/
#include "foundation/constants.h"
-enum { CBM_DIR_PERMS = 0755, PL_RING = 4, PL_RING_MASK = 3, PL_SEQ_PASSES = 5, PL_WAL_BUF = 1040 };
+enum { CBM_DIR_PERMS = 0755, PL_RING = 4, PL_RING_MASK = 3, PL_SEQ_PASSES = 6, PL_WAL_BUF = 1040 };
#define PL_NSEC_PER_SEC 1000000000LL
#include "pipeline/pipeline.h"
#include "pipeline/artifact.h"
#include "pipeline/pipeline_internal.h"
+#include "pipeline/pass_lsp_cross.h"
#include "pipeline/worker_pool.h"
#include "graph_buffer/graph_buffer.h"
#include "store/store.h"
@@ -471,7 +472,20 @@ static void run_predump_passes(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx) {
}
}
-/* Run the sequential pipeline path: definitions, k8s, calls, usages, semantic. */
+/* Adapter that lets cbm_pipeline_pass_lsp_cross slot into the seq_passes
+ * dispatch table. The cross-file LSP needs the per-file CBMFileResult cache
+ * to read defs/imports without re-extracting; in the sequential path that
+ * cache is ctx->result_cache (set up by run_sequential_pipeline before
+ * launching the dispatch loop). When the cache is unavailable (e.g. if the
+ * pipeline opted out of caching), the pass becomes a no-op since there are
+ * no extracted results to feed cross-file resolution. */
+static int seq_pass_lsp_cross_dispatch(cbm_pipeline_ctx_t *ctx,
+ const cbm_file_info_t *files, int file_count) {
+ if (!ctx || !ctx->result_cache) return 0;
+ return cbm_pipeline_pass_lsp_cross(ctx, files, file_count, ctx->result_cache);
+}
+
+/* Run the sequential pipeline path: definitions, k8s, lsp_cross, calls, usages, semantic. */
static int run_sequential_pipeline(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx,
const cbm_file_info_t *files, int file_count,
struct timespec *t) {
@@ -493,6 +507,7 @@ static int run_sequential_pipeline(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx,
} seq_passes[] = {
{cbm_pipeline_pass_definitions, "definitions", false},
{cbm_pipeline_pass_k8s, "k8s", true},
+ {seq_pass_lsp_cross_dispatch, "lsp_cross", true},
{cbm_pipeline_pass_calls, "calls", false},
{cbm_pipeline_pass_usages, "usages", false},
{cbm_pipeline_pass_semantic, "semantic", false},
@@ -557,6 +572,13 @@ static int run_parallel_pipeline(cbm_pipeline_t *p, cbm_pipeline_ctx_t *ctx,
free(cache);
return rc != 0 ? rc : CBM_NOT_FOUND;
}
+ /* Cross-file LSP: augments per-file resolved_calls with cross-file
+ * type-aware resolutions before parallel_resolve emits CALLS edges.
+ * Soft-failures only — log and continue. */
+ cbm_clock_gettime(CLOCK_MONOTONIC, t);
+ (void)cbm_pipeline_pass_lsp_cross(ctx, files, file_count, cache);
+ cbm_log_info("pass.timing", "pass", "lsp_cross", "elapsed_ms",
+ itoa_buf((int)elapsed_ms(*t)));
cbm_clock_gettime(CLOCK_MONOTONIC, t);
rc = cbm_parallel_resolve(ctx, files, file_count, cache, &shared_ids, worker_count);
cbm_log_info("pass.timing", "pass", "parallel_resolve", "elapsed_ms",
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index b586f17d2..85ef942b3 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -381,6 +381,14 @@ int cbm_pipeline_pass_k8s(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
int cbm_pipeline_pass_calls(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files, int file_count);
+/* Cross-file LSP type-aware call resolution pass. Augments per-file
+ * resolved_calls with cross-file resolutions before call edges are emitted.
+ * Implementation: src/pipeline/pass_lsp_cross.c. */
+int cbm_pipeline_pass_lsp_cross(cbm_pipeline_ctx_t *ctx,
+ const cbm_file_info_t *files,
+ int file_count,
+ CBMFileResult **cache);
+
/* Sub-passes called from pass_calls: pattern-based edge extraction */
void cbm_pipeline_pass_fastapi_depends(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
int file_count);
diff --git a/tests/test_parallel.c b/tests/test_parallel.c
index c1b7fb480..84177540a 100644
--- a/tests/test_parallel.c
+++ b/tests/test_parallel.c
@@ -11,6 +11,7 @@
#include "test_helpers.h"
#include "pipeline/pipeline.h"
#include "pipeline/pipeline_internal.h"
+#include "pipeline/pass_lsp_cross.h"
#include "pipeline/worker_pool.h"
#include "graph_buffer/graph_buffer.h"
#include "discover/discover.h"
@@ -137,6 +138,12 @@ static cbm_gbuf_t *run_parallel(const char *project, const char *repo_path, cbm_
cbm_build_registry_from_cache(&ctx, files, file_count, result_cache);
+ /* Cross-file LSP — mirrors run_parallel_pipeline ordering in pipeline.c.
+ * Augments per-file resolved_calls with cross-file type-aware resolutions
+ * (e.g. method calls on imported classes) before parallel_resolve emits
+ * the call edges. */
+ cbm_pipeline_pass_lsp_cross(&ctx, files, file_count, result_cache);
+
cbm_parallel_resolve(&ctx, files, file_count, result_cache, &shared_ids, worker_count);
cbm_gbuf_set_next_id(gbuf, atomic_load(&shared_ids));