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.

perf(python): index unresolved submodule prefix probes

Martin Vogel23e291058c2d
diff --git a/internal/cbm/lsp/py_lsp.c b/internal/cbm/lsp/py_lsp.c
index 004b4e2c1..82a782d64 100644
--- a/internal/cbm/lsp/py_lsp.c
+++ b/internal/cbm/lsp/py_lsp.c
@@ -40,6 +40,11 @@
#define PY_LSP_MAX_EVAL_DEPTH 256
#include "lsp_work.h"
+#ifdef CBM_ENABLE_TEST_SEAMS
+_Atomic uint64_t cbm_py_submodule_probes = 0;
+_Atomic uint64_t cbm_py_submodule_probe_visits = 0;
+#endif
+
#ifdef CBM_ENABLE_TEST_SEAMS
static _Thread_local int py_test_memo_allocations_left = -1;
static _Thread_local bool py_test_depth_failure;
@@ -167,51 +172,32 @@ static bool py_func_is_exact_callable_value(const CBMRegisteredFunc *func) {
(!func->decorator_qns || !func->decorator_qns[0]);
}
-static int py_func_qn_pointer_cmp(const void *left, const void *right) {
- const CBMRegisteredFunc *a = *(CBMRegisteredFunc *const *)left;
- const CBMRegisteredFunc *b = *(CBMRegisteredFunc *const *)right;
- const char *aqn = a ? a->qualified_name : NULL;
- const char *bqn = b ? b->qualified_name : NULL;
- if (!aqn || !bqn)
- return aqn ? 1 : bqn ? -1 : 0;
- return strcmp(aqn, bqn);
-}
-
-/* The graph identifies functions by QN. When Python source redefines the
- * same QN, the runtime's last binding is knowable but the graph no longer has
- * one occurrence-exact definition target. Mark the whole duplicate group so
- * callable values fail closed without changing ordinary call overload/rebind
- * behavior. */
-static void py_mark_ambiguous_callable_bindings(CBMTypeRegistry *registry) {
- if (!registry || registry->func_count <= 1)
+/* Reuse the QN snapshot for duplicate marks and later submodule probes.
+ * The graph cannot identify one exact callable value for a duplicate QN;
+ * ordinary exact lookups retain their original registration-order policy. */
+static void py_mark_ambiguous_callable_bindings(CBMTypeRegistry *registry, CBMArena *idx_arena) {
+ if (!registry)
+ return;
+ cbm_registry_build_func_qn_order(registry, idx_arena);
+ if (registry->func_count <= 1)
return;
- size_t count = (size_t)registry->func_count;
- CBMRegisteredFunc **sorted = count <= SIZE_MAX / sizeof(*sorted)
- ? (CBMRegisteredFunc **)malloc(count * sizeof(*sorted))
- : NULL;
- if (!sorted) {
- /* Allocation failure must reduce precision, never fabricate it. */
+ const CBMRegistryQNEntry *sorted = registry->func_qn_sorted;
+ if (!sorted || registry->func_qn_sorted_upto != registry->func_count) {
for (int i = 0; i < registry->func_count; i++)
registry->funcs[i].flags |= CBM_FUNC_FLAG_AMBIGUOUS_BINDING;
return;
}
- for (int i = 0; i < registry->func_count; i++)
- sorted[i] = &registry->funcs[i];
- qsort(sorted, count, sizeof(*sorted), py_func_qn_pointer_cmp);
- for (size_t first = 0; first < count;) {
- size_t end = first + 1;
- const char *qn = sorted[first]->qualified_name;
- while (qn && end < count && sorted[end]->qualified_name &&
- strcmp(qn, sorted[end]->qualified_name) == 0) {
+ int count = registry->func_qn_sorted_count;
+ for (int first = 0; first < count;) {
+ int end = first + 1;
+ while (end < count && strcmp(sorted[first].qualified_name, sorted[end].qualified_name) == 0)
end++;
- }
- if (qn && end - first > 1) {
- for (size_t i = first; i < end; i++)
- sorted[i]->flags |= CBM_FUNC_FLAG_AMBIGUOUS_BINDING;
+ if (end - first > 1) {
+ for (int i = first; i < end; i++)
+ registry->funcs[sorted[i].func_index].flags |= CBM_FUNC_FLAG_AMBIGUOUS_BINDING;
}
first = end;
}
- free(sorted);
}
/* Depth-guarded entry for the AST call-resolution walk. The walk recurses once
@@ -1588,6 +1574,22 @@ static const CBMType *py_iterable_element_type(PyLSPContext *ctx, const CBMType
/* The real recursive-descent evaluator. Never call directly — go through
* the memoizing, depth-guarded py_eval_expr_type wrapper below
* (every recursive call inside this body already does). */
+/* Existing function-only submodule predicate, after exact symbol/type misses. */
+static bool py_names_submodule(PyLSPContext *ctx, const char *qn) {
+ const char *prefix = qn ? cbm_arena_sprintf(ctx->arena, "%s.", qn) : NULL;
+ if (!prefix)
+ return false;
+#ifdef CBM_ENABLE_TEST_SEAMS
+ uint64_t visits = 0;
+ bool found = cbm_registry_has_func_qn_prefix(ctx->registry, prefix, &visits);
+ atomic_fetch_add_explicit(&cbm_py_submodule_probes, 1, memory_order_relaxed);
+ atomic_fetch_add_explicit(&cbm_py_submodule_probe_visits, visits, memory_order_relaxed);
+ return found;
+#else
+ return cbm_registry_has_func_qn_prefix(ctx->registry, prefix, NULL);
+#endif
+}
+
static const CBMType *py_eval_expr_type_uncached(PyLSPContext *ctx, TSNode node) {
if (!ctx || ts_node_is_null(node))
return cbm_type_unknown();
@@ -1708,25 +1710,7 @@ static const CBMType *py_eval_expr_type_uncached(PyLSPContext *ctx, TSNode node)
const CBMRegisteredType *rt = cbm_registry_lookup_type(ctx->registry, qn);
if (rt)
return cbm_type_named(ctx->arena, qn);
- // Submodule: if any registered function/type has qn starting
- // with "<mod>.<attr>." then mod.attr is itself a module.
- // Linear scan over registry funcs is O(R) per access; we
- // skip it for the common case where mod.attr is already
- // matched as a function/type above. With ~900 stdlib funcs
- // and many module-attr accesses per file, this can dominate
- // — keeping the loop tight and bailing early on first match.
- const char *prefix = cbm_arena_sprintf(ctx->arena, "%s.", qn);
- size_t prefix_len = strlen(prefix);
- bool is_submodule = false;
- CBMFreeFuncIter all_funcs;
- cbm_registry_all_funcs_chain(ctx->registry, &all_funcs);
- for (int i = -1; !is_submodule && (i = cbm_free_func_iter_next(&all_funcs)) >= 0;) {
- const char *fqn = all_funcs.reg->funcs[i].qualified_name;
- if (fqn && strncmp(fqn, prefix, prefix_len) == 0) {
- is_submodule = true;
- }
- }
- if (is_submodule)
+ if (py_names_submodule(ctx, qn))
return cbm_type_module(ctx->arena, qn);
return cbm_type_unknown();
}
@@ -5459,7 +5443,6 @@ void cbm_run_py_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
for (int i = 0; i < result->defs.count; i++) {
py_register_def(arena, &reg, &result->defs.items[i], module_qn);
}
- py_mark_ambiguous_callable_bindings(&reg);
/* Hash-index the registry before the walk (#1527). Unfinalized, every
* lookup scanned the stdlib plus all of this file's definitions, which
@@ -5470,6 +5453,7 @@ void cbm_run_py_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
* scratch arena that dies with this call, not in the result arena. */
CBMArena idx_arena;
cbm_arena_init(&idx_arena);
+ py_mark_ambiguous_callable_bindings(&reg, &idx_arena);
reg.index_first_registered = true;
cbm_registry_finalize_into(&reg, &idx_arena);
@@ -5720,7 +5704,7 @@ CBMLSPStatus cbm_run_py_lsp_cross_status(CBMArena *arena, const char *source, in
ctx.eval_failure = CBM_LSP_SCOPE_FAILED;
goto cross_cleanup;
}
- py_mark_ambiguous_callable_bindings(&reg);
+ py_mark_ambiguous_callable_bindings(&reg, &idx_arena);
/* Finalize registry — O(1) lookups. See go_lsp.c "3c. Finalize"
* comment for the rationale. */
@@ -5764,7 +5748,7 @@ CBMTypeRegistry *cbm_py_build_cross_registry(CBMArena *arena, CBMLSPDef *defs, i
return NULL; /* caller owns the shared arena and other registries */
}
- py_mark_ambiguous_callable_bindings(reg);
+ py_mark_ambiguous_callable_bindings(reg, arena);
cbm_registry_finalize(reg);
reg->read_only = true; /* seal: shared Tier-2 registry is read-only during resolve */
return reg;
diff --git a/internal/cbm/lsp/py_lsp.h b/internal/cbm/lsp/py_lsp.h
index 02e1ab159..016beccdf 100644
--- a/internal/cbm/lsp/py_lsp.h
+++ b/internal/cbm/lsp/py_lsp.h
@@ -244,4 +244,12 @@ typedef struct {
bool cbm_batch_py_lsp_cross(CBMArena *arena, CBMBatchPyLSPFile *files, int file_count,
CBMResolvedCallArray *out);
+#ifdef CBM_ENABLE_TEST_SEAMS
+#include <stdatomic.h>
+/* Process-wide comparison counts for deterministic resolver operation tests.
+ * Only missing exact symbol/type attributes reach this submodule predicate. */
+extern _Atomic uint64_t cbm_py_submodule_probes;
+extern _Atomic uint64_t cbm_py_submodule_probe_visits;
+#endif
+
#endif // CBM_LSP_PY_LSP_H
diff --git a/internal/cbm/lsp/type_registry.c b/internal/cbm/lsp/type_registry.c
index 216c34779..d2b1f5cb1 100644
--- a/internal/cbm/lsp/type_registry.c
+++ b/internal/cbm/lsp/type_registry.c
@@ -985,6 +985,99 @@ const CBMRegisteredFunc *cbm_registry_lookup_symbol(const CBMTypeRegistry *reg,
return cbm_registry_lookup_func(reg, buf);
}
+/* A stable total order, independent of qsort's treatment of equal keys. */
+static int registry_qn_entry_cmp(const void *left, const void *right) {
+ const CBMRegistryQNEntry *a = left;
+ const CBMRegistryQNEntry *b = right;
+ int cmp = strcmp(a->qualified_name, b->qualified_name);
+ return cmp ? cmp : (a->func_index > b->func_index) - (a->func_index < b->func_index);
+}
+
+#ifdef CBM_ENABLE_TEST_SEAMS
+static _Thread_local int registry_qn_order_allocations_left = -1;
+void cbm_registry_test_qn_order_fail_after(int successful_allocations) {
+ registry_qn_order_allocations_left = successful_allocations;
+}
+#endif
+
+void cbm_registry_build_func_qn_order(CBMTypeRegistry *reg, CBMArena *arena) {
+ if (!reg || !arena || reg->read_only)
+ return;
+ /* A failed rebuild must not leave an old snapshot authoritative. */
+ reg->func_qn_sorted = NULL;
+ reg->func_qn_sorted_count = 0;
+ reg->func_qn_sorted_upto = 0;
+ if (reg->func_count <= 0 || (size_t)reg->func_count > SIZE_MAX / sizeof(CBMRegistryQNEntry))
+ return;
+#ifdef CBM_ENABLE_TEST_SEAMS
+ if (registry_qn_order_allocations_left == 0)
+ return;
+ if (registry_qn_order_allocations_left > 0)
+ registry_qn_order_allocations_left--;
+#endif
+ CBMRegistryQNEntry *sorted = cbm_arena_alloc(arena, (size_t)reg->func_count * sizeof(*sorted));
+ if (!sorted)
+ return;
+ int count = 0;
+ for (int i = 0; i < reg->func_count; i++) {
+ if (reg->funcs[i].qualified_name) {
+ sorted[count].qualified_name = reg->funcs[i].qualified_name;
+ sorted[count].func_index = i;
+ count++;
+ }
+ }
+ qsort(sorted, (size_t)count, sizeof(*sorted), registry_qn_entry_cmp);
+ reg->func_qn_sorted = sorted;
+ reg->func_qn_sorted_count = count;
+ reg->func_qn_sorted_upto = reg->func_count;
+}
+
+static bool registry_link_has_func_qn_prefix(const CBMTypeRegistry *reg, const char *prefix,
+ size_t length, uint64_t *visits) {
+ int scan_from = 0;
+ if (reg->func_qn_sorted && reg->func_qn_sorted_upto <= reg->func_count) {
+ int lo = 0, hi = reg->func_qn_sorted_count;
+ while (lo < hi) {
+ int mid = lo + (hi - lo) / 2;
+ if (visits)
+ (*visits)++;
+ if (strcmp(reg->func_qn_sorted[mid].qualified_name, prefix) < 0)
+ lo = mid + 1;
+ else
+ hi = mid;
+ }
+ if (lo < reg->func_qn_sorted_count) {
+ if (visits)
+ (*visits)++;
+ if (strncmp(reg->func_qn_sorted[lo].qualified_name, prefix, length) == 0)
+ return true;
+ }
+ scan_from = reg->func_qn_sorted_upto;
+ }
+ for (int i = scan_from; i < reg->func_count; i++) {
+ if (visits)
+ (*visits)++;
+ const char *qn = reg->funcs[i].qualified_name;
+ if (qn && strncmp(qn, prefix, length) == 0)
+ return true;
+ }
+ return false;
+}
+
+bool cbm_registry_has_func_qn_prefix(const CBMTypeRegistry *reg, const char *prefix,
+ uint64_t *visits) {
+ if (!prefix)
+ return false;
+ size_t length = strlen(prefix);
+ /* The old iterator omits shadowed fallback QNs, whose equal head QN has
+ * the same prefix answer. This boolean union needs no winner selection. */
+ for (const CBMTypeRegistry *link = reg; link; link = link->fallback) {
+ if (registry_link_has_func_qn_prefix(link, prefix, length, visits))
+ return true;
+ }
+ return false;
+}
+
// Count parameters in a FUNC signature.
static int count_func_params(const CBMRegisteredFunc *f) {
if (!f || !f->signature || f->signature->kind != CBM_TYPE_FUNC)
diff --git a/internal/cbm/lsp/type_registry.h b/internal/cbm/lsp/type_registry.h
index 66b128283..0d1ef564b 100644
--- a/internal/cbm/lsp/type_registry.h
+++ b/internal/cbm/lsp/type_registry.h
@@ -83,6 +83,12 @@ typedef struct {
int slot; // bucket slot this entry sits in (for resize)
} CBMRegistryHashEntry;
+/* A borrowed immutable QN and stable array index, never a movable func pointer. */
+typedef struct {
+ const char *qualified_name;
+ int func_index;
+} CBMRegistryQNEntry;
+
// Cross-file type/function registry.
typedef struct CBMTypeRegistry {
CBMRegisteredFunc *funcs;
@@ -159,6 +165,13 @@ typedef struct CBMTypeRegistry {
// hashed chains answer with the LAST registration (the Tier-2 behavior,
// left unchanged).
bool index_first_registered;
+
+ /* Additional prefix/duplicate index; exact lookup order is unchanged.
+ * Covers non-NULL QNs in funcs[0..func_qn_sorted_upto), ordered by (QN,index).
+ * Appended functions remain visible through a tail scan. */
+ CBMRegistryQNEntry *func_qn_sorted;
+ int func_qn_sorted_count;
+ int func_qn_sorted_upto;
} CBMTypeRegistry;
// Initialize a registry.
@@ -207,6 +220,25 @@ const CBMRegisteredFunc *cbm_registry_lookup_func(const CBMTypeRegistry *reg,
const CBMRegisteredFunc *cbm_registry_lookup_symbol(const CBMTypeRegistry *reg,
const char *package_qn, const char *name);
+/* Build in an arena that outlives every use (owner arena for shared registries,
+ * walk scratch for per-file registries). QN strings and registered identities
+ * must remain immutable while a snapshot is used; end its use before changing
+ * identities, then rebuild before querying again. Appends do not invalidate it.
+ * Never frees caller arenas. Sealed registries are unchanged. A failed rebuild
+ * discards the old order and queries scan the complete registry instead. */
+void cbm_registry_build_func_qn_order(CBMTypeRegistry *reg, CBMArena *arena);
+
+/* Boolean starts-with over all function/method QNs in the fallback chain.
+ * Does not allocate or mutate. NULL prefix is false; empty prefix matches any
+ * non-NULL QN. Optional visits accumulates compared entries, including tail
+ * scans; fully indexed links use a lower bound plus at most one candidate. */
+bool cbm_registry_has_func_qn_prefix(const CBMTypeRegistry *reg, const char *prefix,
+ uint64_t *visits);
+#ifdef CBM_ENABLE_TEST_SEAMS
+/* -1 disables; zero fails this allocation until reset; positive counts down. */
+void cbm_registry_test_qn_order_fail_after(int successful_allocations);
+#endif
+
// Resolve type alias chain: follow alias_of until concrete type found (max 16 levels).
const CBMRegisteredType *cbm_registry_resolve_alias(const CBMTypeRegistry *reg,
const char *type_qn);
diff --git a/scripts/memory-core-baseline.txt b/scripts/memory-core-baseline.txt
index 79fa00270..fe3a7c883 100644
--- a/scripts/memory-core-baseline.txt
+++ b/scripts/memory-core-baseline.txt
@@ -10,7 +10,6 @@ internal/cbm/extract_defs.c 4
internal/cbm/helpers.c 3
internal/cbm/lsp/c_lsp.c 4
internal/cbm/lsp/perl_lsp.c 21
-internal/cbm/lsp/py_lsp.c 2
internal/cbm/macro_table.c 1
internal/cbm/service_patterns.c 2
internal/cbm/sqlite_writer.c 74
diff --git a/tests/test_complexity.c b/tests/test_complexity.c
index 775141cfc..76acfd09c 100644
--- a/tests/test_complexity.c
+++ b/tests/test_complexity.c
@@ -50,11 +50,13 @@
#include "cbm.h"
#include "discover/discover.h"
#include "lang_specs.h"
+#include "lsp/py_lsp.h"
#include "tree_sitter/api.h"
#include "pipeline/pass_lsp_cross.h"
#include "pipeline/pipeline.h"
#include "pipeline/pipeline_internal.h"
#include "store/store.h"
+#include "yyjson/yyjson.h"
#include <stdatomic.h>
#include <stdint.h>
@@ -93,6 +95,9 @@ enum {
* code path. The sequential path's own per-file registry work is bounded
* by the 50-file ceiling and is deliberately out of scope here. */
CX_BIGPKG_FILES_PER_K = 20,
+ CX_PYEXT_FILES_PER_MOD = 20,
+ CX_PYEXT_DEFS_PER_FILE = 120,
+ CX_PYEXT_PROBES_PER_FILE = 3,
};
/* Linear growth bounds for a 2x input doubling. Fixed structural overhead
@@ -337,6 +342,44 @@ static int cx_build_bigpkg(const char *root, int k) {
return 0;
}
+/* Every file probes absent external submodules and retains a real local-call
+ * control. Independent module copies grow the shared registry without adding
+ * cross-copy dependencies. Both sizes exceed the parallel-driver threshold. */
+static int cx_build_pyext(const char *root, int k) {
+ char dir[1024];
+ char path[1200];
+ for (int m = 0; m < k; m++) {
+ int n = snprintf(dir, sizeof(dir), "%s/pyext/mod%d", root, m);
+ if (n < 0 || (size_t)n >= sizeof(dir) || th_mkdir_p(dir) != 0)
+ return -1;
+ for (int j = 0; j < CX_PYEXT_FILES_PER_MOD; j++) {
+ n = snprintf(path, sizeof(path), "%s/p%d.py", dir, j);
+ if (n < 0 || (size_t)n >= sizeof(path))
+ return -1;
+ FILE *f = fopen(path, "w");
+ if (!f)
+ return -1;
+ bool written = fprintf(f, "import extlib\n\n") >= 0;
+ for (int d = 0; written && d < CX_PYEXT_DEFS_PER_FILE; d++) {
+ written =
+ fprintf(f, "def cx_pyext_definition_%d(x):\n return x + %d\n\n", d, d) >= 0;
+ }
+ if (written)
+ written =
+ fprintf(f, "def cx_pyext_use():\n return (cx_pyext_definition_0(0)") >= 0;
+ for (int probe = 0; written && probe < CX_PYEXT_PROBES_PER_FILE; probe++) {
+ written = fprintf(f, " + extlib.sub%d.helper(%d)", probe, probe) >= 0;
+ }
+ if (written)
+ written = fprintf(f, ")\n") >= 0;
+ int closed = fclose(f);
+ if (!written || closed != 0)
+ return -1;
+ }
+ }
+ return 0;
+}
+
/* ── Metrics ─────────────────────────────────────────────────────────── */
/* Per-pass timing capture: a TEE log sink parses `pass.timing` lines during a
@@ -414,16 +457,87 @@ typedef struct {
uint64_t dk_hub_edges; /* Image edges the component split examined */
int infra_nodes[CX_INFRA_COUNT];
int infra_edges[CX_INFRA_COUNT];
+ uint64_t py_probes;
+ uint64_t py_probe_visits;
+ int pyext_files;
+ int pyext_defs;
+ int pyext_controls;
+ int errors;
double wall_s;
} CxMetrics;
+/* Count the intended corpus, then prove each use() kept its exact local LSP
+ * call. Query errors are failures, never indistinguishable from zero rows. */
+static bool cx_pyext_control(cbm_store_t *store, const char *project, const cbm_node_t *caller) {
+ if (!caller->qualified_name || !caller->file_path)
+ return false;
+ const char *dot = strrchr(caller->qualified_name, '.');
+ if (!dot || dot - caller->qualified_name > 1000)
+ return false;
+ char target_qn[1200];
+ int n = snprintf(target_qn, sizeof(target_qn), "%.*s.cx_pyext_definition_0",
+ (int)(dot - caller->qualified_name), caller->qualified_name);
+ if (n < 0 || (size_t)n >= sizeof(target_qn))
+ return false;
+ cbm_node_t target = {0};
+ cbm_edge_t *edges = NULL;
+ int count = 0;
+ bool found = false;
+ if (cbm_store_find_node_by_qn(store, project, target_qn, &target) == CBM_STORE_OK &&
+ target.label && strcmp(target.label, "Function") == 0 && target.file_path &&
+ strcmp(target.file_path, caller->file_path) == 0 &&
+ cbm_store_find_edges_by_source_type(store, caller->id, "CALLS", &edges, &count) ==
+ CBM_STORE_OK) {
+ for (int i = 0; i < count; i++) {
+ if (edges[i].target_id != target.id || !edges[i].properties_json)
+ continue;
+ yyjson_doc *doc =
+ yyjson_read(edges[i].properties_json, strlen(edges[i].properties_json), 0);
+ const char *source =
+ doc ? yyjson_get_str(yyjson_obj_get(yyjson_doc_get_root(doc), "source")) : NULL;
+ found = found || (source && strcmp(source, "lsp") == 0);
+ yyjson_doc_free(doc);
+ }
+ }
+ cbm_store_free_edges(edges, count);
+ cbm_node_free_fields(&target);
+ return found;
+}
+
+static bool cx_pyext_population(cbm_store_t *store, const char *project, CxMetrics *out) {
+ cbm_node_t *nodes = NULL;
+ int count = 0;
+ bool ok = cbm_store_find_nodes_by_label(store, project, "File", &nodes, &count) == CBM_STORE_OK;
+ for (int i = 0; ok && i < count; i++) {
+ if (nodes[i].file_path && strncmp(nodes[i].file_path, "pyext/", 6) == 0)
+ out->pyext_files++;
+ }
+ cbm_store_free_nodes(nodes, count);
+ nodes = NULL;
+ count = 0;
+ ok = ok &&
+ cbm_store_find_nodes_by_label(store, project, "Function", &nodes, &count) == CBM_STORE_OK;
+ for (int i = 0; ok && i < count; i++) {
+ const cbm_node_t *node = &nodes[i];
+ if (!node->file_path || strncmp(node->file_path, "pyext/", 6) != 0 || !node->name)
+ continue;
+ static const char definition_prefix[] = "cx_pyext_definition_";
+ if (strncmp(node->name, definition_prefix, sizeof(definition_prefix) - 1) == 0)
+ out->pyext_defs++;
+ if (strcmp(node->name, "cx_pyext_use") == 0 && cx_pyext_control(store, project, node))
+ out->pyext_controls++;
+ }
+ cbm_store_free_nodes(nodes, count);
+ return ok;
+}
+
static double cx_now_s(void) {
struct timespec ts;
cbm_profile_now(&ts);
return (double)ts.tv_sec + (double)ts.tv_nsec / 1e9;
}
-static int cx_run(const char *root, const char *db_path, CxMetrics *out) {
+static int cx_run(const char *root, const char *db_path, CxMetrics *out, bool pyext) {
memset(out, 0, sizeof(*out));
uint64_t d0;
@@ -441,6 +555,8 @@ static int cx_run(const char *root, const char *db_path, CxMetrics *out) {
uint64_t sc0 = atomic_load_explicit(&g_sh_candidates, memory_order_relaxed);
uint64_t fc0 = atomic_load_explicit(&g_tf_candidates, memory_order_relaxed);
uint64_t dh0 = atomic_load_explicit(&g_dk_hub_edges, memory_order_relaxed);
+ uint64_t pp0 = atomic_load_explicit(&cbm_py_submodule_probes, memory_order_relaxed);
+ uint64_t pv0 = atomic_load_explicit(&cbm_py_submodule_probe_visits, memory_order_relaxed);
atomic_store_explicit(&g_cx_pass_count, 0, memory_order_relaxed);
cbm_log_set_sink_ex(cx_pass_sink, CBM_LOG_SINK_TEE);
@@ -452,6 +568,7 @@ static int cx_run(const char *root, const char *db_path, CxMetrics *out) {
}
int rc = cbm_pipeline_run(p);
out->wall_s = cx_now_s() - t0;
+ cbm_pipeline_get_file_errors(p, NULL, &out->errors);
cbm_log_set_sink(NULL);
int captured = atomic_load_explicit(&g_cx_pass_count, memory_order_relaxed);
out->pass_count = captured < CX_MAX_PASSES ? captured : CX_MAX_PASSES;
@@ -484,6 +601,10 @@ static int cx_run(const char *root, const char *db_path, CxMetrics *out) {
out->tf_candidates = atomic_load_explicit(&g_tf_candidates, memory_order_relaxed) - fc0;
out->dk_hub_edges = atomic_load_explicit(&g_dk_hub_edges, memory_order_relaxed) - dh0;
+ out->py_probes = atomic_load_explicit(&cbm_py_submodule_probes, memory_order_relaxed) - pp0;
+ out->py_probe_visits =
+ atomic_load_explicit(&cbm_py_submodule_probe_visits, memory_order_relaxed) - pv0;
+
cbm_store_t *s = cbm_store_open_path(db_path);
if (!s) {
return -1;
@@ -498,8 +619,9 @@ static int cx_run(const char *root, const char *db_path, CxMetrics *out) {
out->infra_nodes[i] = cbm_store_count_nodes_scoped(s, project, CX_INFRA_DIRS[i]);
out->infra_edges[i] = cbm_store_count_edges_scoped(s, project, CX_INFRA_DIRS[i]);
}
+ bool extra_ok = !pyext || cx_pyext_population(s, project, out);
cbm_store_close(s);
- return 0;
+ return extra_ok ? 0 : -1;
}
static double cx_ratio(double num, double den) {
@@ -534,7 +656,8 @@ static int cx_measure(CxCorpusBuilder build, const char *prefix_a, const char *p
char db_b[600];
snprintf(db_a, sizeof(db_a), "%s/cx.db", root_a);
snprintf(db_b, sizeof(db_b), "%s/cx.db", root_b);
- if (cx_run(root_a, db_a, out_a) == 0 && cx_run(root_b, db_b, out_b) == 0) {
+ bool pyext = build == cx_build_pyext;
+ if (cx_run(root_a, db_a, out_a, pyext) == 0 && cx_run(root_b, db_b, out_b, pyext) == 0) {
rc = 0;
}
}
@@ -740,6 +863,58 @@ TEST(complexity_shared_package_growth_stays_linear) {
PASS();
}
+TEST(complexity_py_submodule_probe_avoids_registry_scan) {
+ static const char *const keys[] = {"CBM_WORKERS", "CBM_INDEX_SINGLE_THREAD"};
+ char *saved[2] = {NULL, NULL};
+ bool ready = true;
+ for (int i = 0; i < 2; i++) {
+ const char *old = getenv(keys[i]);
+ saved[i] = old ? strdup(old) : NULL;
+ if (old && !saved[i])
+ ready = false;
+ }
+ CxMetrics a = {0}, b = {0};
+ int rc = -1, restored = 0;
+ if (ready) {
+ int configured = cbm_setenv(keys[0], "2", 1);
+ configured |= cbm_unsetenv(keys[1]);
+ if (configured == 0)
+ rc = cx_measure(cx_build_pyext, "cbm_cxpy_a", "cbm_cxpy_b", &a, &b);
+ for (int i = 0; i < 2; i++)
+ restored |= saved[i] ? cbm_setenv(keys[i], saved[i], 1) : cbm_unsetenv(keys[i]);
+ }
+ for (int i = 0; i < 2; i++)
+ free(saved[i]);
+ /* cx_measure removes both owned corpora before any assertion below. */
+ ASSERT_TRUE(ready);
+ ASSERT_EQ(restored, 0);
+ ASSERT_EQ(rc, 0);
+ int files = CX_K_BASE * CX_PYEXT_FILES_PER_MOD;
+ ASSERT_EQ(a.errors, 0);
+ ASSERT_EQ(b.errors, 0);
+ ASSERT_EQ(a.pyext_files, files);
+ ASSERT_EQ(b.pyext_files, files * 2);
+ ASSERT_EQ(a.pyext_defs, files * CX_PYEXT_DEFS_PER_FILE);
+ ASSERT_EQ(b.pyext_defs, files * 2 * CX_PYEXT_DEFS_PER_FILE);
+ ASSERT_EQ(a.pyext_controls, files);
+ ASSERT_EQ(b.pyext_controls, files * 2);
+ ASSERT_GT((long long)a.py_probes, (long long)CX_MIN_BASE_WORK);
+ ASSERT_GT((long long)b.py_probes, (long long)CX_MIN_BASE_WORK);
+ ASSERT_GT((long long)a.py_probe_visits, 0);
+ ASSERT_GT((long long)b.py_probe_visits, 0);
+ double probes_ratio = cx_ratio((double)b.py_probes, (double)a.py_probes);
+ double visits_ratio = cx_ratio((double)b.py_probe_visits, (double)a.py_probe_visits);
+ printf(" py_probes %llu -> %llu (%.2f), visits %llu -> %llu (%.2f)\n",
+ (unsigned long long)a.py_probes, (unsigned long long)b.py_probes, probes_ratio,
+ (unsigned long long)a.py_probe_visits, (unsigned long long)b.py_probe_visits,
+ visits_ratio);
+ ASSERT_TRUE(probes_ratio >= CX_RATIO_LO && probes_ratio <= CX_RATIO_HI);
+ ASSERT_TRUE(visits_ratio <= CX_RATIO_HI);
+ /* The direct registry test also enforces a logarithmic comparison bound,
+ * independently of fixed stdlib size and this corpus-growth ratio. */
+ PASS();
+}
+
/* Throughput report — information only, never a gate (CI-determinism rule:
* rates depend on the machine and scheduler, so a threshold would be a
* lottery). Written locally under private/ (gitignored); CBM_COMPLEXITY_
@@ -972,5 +1147,6 @@ SUITE(complexity) {
RUN_TEST(complexity_infra_resolution_is_linear);
RUN_TEST(complexity_importance_scoring_is_linear);
RUN_TEST(complexity_shared_package_growth_stays_linear);
+ RUN_TEST(complexity_py_submodule_probe_avoids_registry_scan);
RUN_TEST(complexity_throughput_report_written);
}
diff --git a/tests/test_py_lsp.c b/tests/test_py_lsp.c
index 347e9e0f7..cb2542d45 100644
--- a/tests/test_py_lsp.c
+++ b/tests/test_py_lsp.c
@@ -3901,7 +3901,412 @@ TEST(pylsp_class_frame_failure_rolls_back_raw_and_cross_outputs) {
PASS();
}
+/* The prefix index is an existence oracle, never an exact-symbol selector. */
+static bool pylsp_prefix_scan(const CBMTypeRegistry *reg, const char *prefix) {
+ if (!reg || !prefix)
+ return false;
+ CBMFreeFuncIter iter;
+ cbm_registry_all_funcs_chain(reg, &iter);
+ size_t length = strlen(prefix);
+ for (int i; (i = cbm_free_func_iter_next(&iter)) >= 0;) {
+ const char *qn = iter.reg->funcs[i].qualified_name;
+ if (qn && strncmp(qn, prefix, length) == 0)
+ return true;
+ }
+ return false;
+}
+
+static bool pylsp_prefix_add(CBMTypeRegistry *reg, const char *qn, const char *receiver) {
+ int before = reg->func_count;
+ cbm_registry_add_func(
+ reg,
+ (CBMRegisteredFunc){.qualified_name = qn, .short_name = "call", .receiver_type = receiver});
+ return reg->func_count == before + 1;
+}
+
+static bool pylsp_prefix_oracles(const CBMTypeRegistry *reg) {
+ const char *prefixes[] = {NULL, "", "pkg.", "pkg.sub.",
+ "pkg.sub.call", "pkg.su.", "pkg.submarine.", "pkg.Type.",
+ "only.tail.", "missing.", "zzzz."};
+ for (size_t i = 0; i < sizeof(prefixes) / sizeof(prefixes[0]); i++) {
+ uint64_t visits = 17;
+ bool indexed = cbm_registry_has_func_qn_prefix(reg, prefixes[i], &visits);
+ if (indexed != pylsp_prefix_scan(reg, prefixes[i]) || visits < 17 ||
+ cbm_registry_has_func_qn_prefix(reg, prefixes[i], NULL) != indexed ||
+ (!prefixes[i] && visits != 17))
+ return false;
+ }
+ return true;
+}
+
+TEST(pylsp_prefix_order_matches_iterator_and_chain_shadowing) {
+ CBMArena arena, scratch;
+ cbm_arena_init(&arena);
+ cbm_arena_init(&scratch);
+ CBMTypeRegistry head, middle, tail, empty;
+ cbm_registry_init(&head, &arena);
+ cbm_registry_init(&middle, &arena);
+ cbm_registry_init(&tail, &arena);
+ cbm_registry_init(&empty, &arena);
+ bool ok = pylsp_prefix_add(&head, NULL, NULL) && pylsp_prefix_add(&head, "", NULL) &&
+ pylsp_prefix_add(&head, "pkg.sub.call", NULL) &&
+ pylsp_prefix_add(&head, "pkg.sub.call", NULL) &&
+ pylsp_prefix_add(&head, "pkg.Type.call", "pkg.Type") &&
+ pylsp_prefix_add(&middle, "pkg.sub.call", "different.receiver") &&
+ pylsp_prefix_add(&middle, "pkg.submarine.call", NULL) &&
+ pylsp_prefix_add(&tail, "only.tail.call", NULL);
+ head.fallback = &middle;
+ middle.fallback = &tail;
+ ok = pylsp_prefix_oracles(NULL) && pylsp_prefix_oracles(&empty) &&
+ pylsp_prefix_oracles(&head) && ok;
+ cbm_registry_build_func_qn_order(&head, &scratch);
+ ok = head.func_qn_sorted && head.func_qn_sorted_count == 4 && head.func_qn_sorted_upto == 5 &&
+ pylsp_prefix_oracles(&head) && ok;
+ cbm_registry_build_func_qn_order(&tail, &arena);
+ ok = tail.func_qn_sorted && pylsp_prefix_oracles(&head) && ok;
+ cbm_registry_build_func_qn_order(&middle, &scratch);
+ ok = middle.func_qn_sorted && pylsp_prefix_oracles(&head) && ok;
+ if (head.func_qn_sorted) {
+ int duplicate_count = 0, previous = -1;
+ for (int i = 0; i < head.func_qn_sorted_count; i++) {
+ const CBMRegistryQNEntry *entry = &head.func_qn_sorted[i];
+ ok = entry->func_index >= 0 && entry->func_index < head.func_count &&
+ entry->qualified_name == head.funcs[entry->func_index].qualified_name && ok;
+ if (strcmp(entry->qualified_name, "pkg.sub.call") == 0) {
+ ok = entry->func_index > previous && ok;
+ previous = entry->func_index;
+ duplicate_count++;
+ }
+ }
+ ok = duplicate_count == 2 && ok;
+ }
+ size_t owned_before = cbm_arena_total(&arena), scratch_before = cbm_arena_total(&scratch);
+ ok = pylsp_prefix_oracles(&head) && cbm_arena_total(&arena) == owned_before &&
+ cbm_arena_total(&scratch) == scratch_before && ok;
+ /* An indexed fallback also works behind an unindexed empty head. */
+ empty.fallback = &head;
+ ok = pylsp_prefix_oracles(&empty) && ok;
+ cbm_arena_destroy(&scratch);
+ cbm_arena_destroy(&arena);
+ ASSERT_TRUE(ok);
+ PASS();
+}
+
+TEST(pylsp_prefix_order_survives_array_growth_rebuild_and_seal) {
+ CBMArena arena, scratch;
+ cbm_arena_init(&arena);
+ cbm_arena_init(&scratch);
+ CBMTypeRegistry reg, unindexed;
+ cbm_registry_init(&reg, &arena);
+ cbm_registry_init(&unindexed, &arena);
+ bool ok = pylsp_prefix_add(&reg, "pkg.old.call", NULL);
+ cbm_registry_build_func_qn_order(&reg, &scratch);
+ CBMRegisteredFunc *original_array = reg.funcs;
+ const CBMRegistryQNEntry *order = reg.func_qn_sorted;
+ for (int i = 0; i < 140; i++) {
+ const char *qn = cbm_arena_sprintf(&arena, "tail.mod%03d.call", i);
+ ok = qn && pylsp_prefix_add(&reg, qn, NULL) && ok;
+ }
+ ok = reg.func_count == 141 && reg.funcs != original_array && reg.func_qn_sorted == order &&
+ reg.func_qn_sorted_upto == 1 &&
+ cbm_registry_has_func_qn_prefix(&reg, "tail.mod139.", NULL) &&
+ !cbm_registry_has_func_qn_prefix(&reg, "tail.mod140.", NULL) && ok;
+ cbm_registry_build_func_qn_order(&reg, &scratch);
+ ok = reg.func_qn_sorted && reg.func_qn_sorted_upto == reg.func_count &&
+ cbm_registry_has_func_qn_prefix(&reg, "tail.mod139.", NULL) && ok;
+ /* End use of the old snapshot before replacing an identity. No lookup is
+ * permitted until the complete order has been rebuilt. */
+ if (reg.func_count == 141) {
+ reg.funcs[0].qualified_name = "pkg.renamed.call";
+ cbm_registry_build_func_qn_order(&reg, &scratch);
+ ok = !cbm_registry_has_func_qn_prefix(&reg, "pkg.old.", NULL) &&
+ cbm_registry_has_func_qn_prefix(&reg, "pkg.renamed.", NULL) && ok;
+ }
+ order = reg.func_qn_sorted;
+ reg.read_only = true;
+ cbm_registry_test_qn_order_fail_after(0);
+ cbm_registry_build_func_qn_order(&reg, &scratch);
+ cbm_registry_test_qn_order_fail_after(-1);
+ cbm_registry_add_func(&reg, (CBMRegisteredFunc){.qualified_name = "forbidden.call"});
+ ok = reg.func_qn_sorted == order && reg.func_count == 141 &&
+ cbm_registry_has_func_qn_prefix(&reg, "pkg.renamed.", NULL) &&
+ !cbm_registry_has_func_qn_prefix(&reg, "forbidden.", NULL) && ok;
+ ok = pylsp_prefix_add(&unindexed, "sealed.call", NULL) && ok;
+ unindexed.read_only = true;
+ cbm_registry_build_func_qn_order(&unindexed, &scratch);
+ uint64_t visits = 0;
+ ok = !unindexed.func_qn_sorted &&
+ cbm_registry_has_func_qn_prefix(&unindexed, "sealed.", &visits) && visits == 1 && ok;
+ cbm_arena_destroy(&scratch);
+ cbm_arena_destroy(&arena);
+ ASSERT_TRUE(ok);
+ PASS();
+}
+
+TEST(pylsp_prefix_order_allocation_failure_preserves_oracle) {
+ CBMArena arena;
+ cbm_arena_init(&arena);
+ CBMTypeRegistry reg;
+ cbm_registry_init(&reg, &arena);
+ bool ok = pylsp_prefix_add(&reg, "pkg.sub.call", NULL) && pylsp_prefix_add(&reg, NULL, NULL) &&
+ pylsp_prefix_add(&reg, "only.tail.call", NULL);
+ cbm_registry_test_qn_order_fail_after(0);
+ cbm_registry_build_func_qn_order(&reg, &arena);
+ cbm_registry_test_qn_order_fail_after(-1);
+ uint64_t visits = 0;
+ ok = !reg.func_qn_sorted && reg.func_qn_sorted_count == 0 && reg.func_qn_sorted_upto == 0 &&
+ pylsp_prefix_oracles(&reg) && !cbm_registry_has_func_qn_prefix(&reg, "zzzz.", &visits) &&
+ visits == 3 && ok;
+ cbm_registry_test_qn_order_fail_after(1);
+ cbm_registry_build_func_qn_order(&reg, &arena);
+ bool first_built = reg.func_qn_sorted && reg.func_qn_sorted_upto == reg.func_count;
+ cbm_registry_build_func_qn_order(&reg, &arena);
+ cbm_registry_test_qn_order_fail_after(-1);
+ ok = first_built && !reg.func_qn_sorted && reg.func_qn_sorted_count == 0 &&
+ reg.func_qn_sorted_upto == 0 && pylsp_prefix_oracles(&reg) && ok;
+ cbm_registry_build_func_qn_order(&reg, &arena);
+ ok = reg.func_qn_sorted && reg.func_qn_sorted_count == 2 && reg.func_qn_sorted_upto == 3 &&
+ pylsp_prefix_oracles(&reg) && ok;
+ cbm_arena_destroy(&arena);
+ ASSERT_TRUE(ok);
+ PASS();
+}
+
+TEST(pylsp_prefix_order_has_absolute_comparison_bound) {
+ enum { FUNCTIONS = 1024, PROBES = 32, BOUND = 12 };
+ CBMArena arena;
+ cbm_arena_init(&arena);
+ CBMTypeRegistry reg;
+ cbm_registry_init(&reg, &arena);
+ bool ok = true;
+ for (int i = FUNCTIONS - 1; i >= 0; i--) {
+ const char *qn = cbm_arena_sprintf(&arena, "project.mod%04d.call", i);
+ ok = qn && pylsp_prefix_add(&reg, qn, NULL) && ok;
+ }
+ cbm_registry_build_func_qn_order(&reg, &arena);
+ ok = reg.func_count == FUNCTIONS && reg.func_qn_sorted_count == FUNCTIONS &&
+ reg.func_qn_sorted_upto == FUNCTIONS && reg.func_qn_sorted && ok;
+ for (int i = 0; i < PROBES; i++) {
+ char hit[64], miss[64];
+ int a = snprintf(hit, sizeof(hit), "project.mod%04d.", i * 31);
+ int b = snprintf(miss, sizeof(miss), "project.mod%04dx.", i * 31);
+ uint64_t positive = 0, negative = 0;
+ bool ready = a > 0 && (size_t)a < sizeof(hit) && b > 0 && (size_t)b < sizeof(miss);
+ ok = ready && cbm_registry_has_func_qn_prefix(&reg, hit, &positive) &&
+ !cbm_registry_has_func_qn_prefix(&reg, miss, &negative) && positive > 0 &&
+ positive <= BOUND && negative > 0 && negative <= BOUND &&
+ pylsp_prefix_scan(&reg, hit) && !pylsp_prefix_scan(&reg, miss) && ok;
+ }
+ /* Failure restores the reference scan, which must inspect every entry on
+ * this absent suffix. This is a deterministic contrast, not a timing ratio. */
+ cbm_registry_test_qn_order_fail_after(0);
+ cbm_registry_build_func_qn_order(&reg, &arena);
+ cbm_registry_test_qn_order_fail_after(-1);
+ uint64_t visits = 0;
+ ok = !reg.func_qn_sorted && !cbm_registry_has_func_qn_prefix(&reg, "zzzz.", &visits) &&
+ visits == FUNCTIONS && visits > BOUND && ok;
+ cbm_arena_destroy(&arena);
+ ASSERT_TRUE(ok);
+ PASS();
+}
+
+TEST(pylsp_prefix_order_preserves_exact_selection_and_ambiguity) {
+ CBMArena arena;
+ cbm_arena_init(&arena);
+ bool ok = true;
+ for (int first = 0; first < 2; first++) {
+ CBMTypeRegistry reg;
+ cbm_registry_init(&reg, &arena);
+ ok = pylsp_prefix_add(&reg, "pkg.duplicate", NULL) &&
+ pylsp_prefix_add(&reg, "pkg.duplicate", NULL) && ok;
+ const CBMRegisteredFunc *linear = cbm_registry_lookup_func(&reg, "pkg.duplicate");
+ ok = reg.func_count == 2 && linear == &reg.funcs[0] && ok;
+ cbm_registry_build_func_qn_order(&reg, &arena);
+ ok = cbm_registry_lookup_func(&reg, "pkg.duplicate") == linear && ok;
+ reg.index_first_registered = first != 0;
+ cbm_registry_finalize(&reg);
+ const CBMRegisteredFunc *selected = cbm_registry_lookup_func(&reg, "pkg.duplicate");
+ ok = reg.func_count == 2 && selected == &reg.funcs[first ? 0 : 1] && ok;
+ cbm_registry_build_func_qn_order(&reg, &arena);
+ ok = cbm_registry_lookup_func(&reg, "pkg.duplicate") == selected && ok;
+ }
+ CBMLSPDef defs[] = {
+ {.qualified_name = "pkg.duplicate",
+ .short_name = "duplicate",
+ .label = "Function",
+ .def_module_qn = "pkg",
+ .return_types = "int",
+ .lang = CBM_LANG_PYTHON},
+ {.qualified_name = "pkg.duplicate",
+ .short_name = "duplicate",
+ .label = "Function",
+ .def_module_qn = "pkg",
+ .return_types = "str",
+ .lang = CBM_LANG_PYTHON},
+ {.qualified_name = "pkg.unique",
+ .short_name = "unique",
+ .label = "Function",
+ .def_module_qn = "pkg",
+ .lang = CBM_LANG_PYTHON},
+ };
+ for (int fail = 0; fail < 2; fail++) {
+ cbm_registry_test_qn_order_fail_after(fail ? 0 : -1);
+ CBMTypeRegistry *reg = cbm_py_build_cross_registry(&arena, defs, 3);
+ cbm_registry_test_qn_order_fail_after(-1);
+ ok = reg && reg->read_only && reg->func_count > 3 && ok;
+ if (reg) {
+ int duplicate_count = 0, unique_count = 0;
+ for (int i = 0; i < reg->func_count; i++) {
+ const CBMRegisteredFunc *f = &reg->funcs[i];
+ bool ambiguous = (f->flags & CBM_FUNC_FLAG_AMBIGUOUS_BINDING) != 0;
+ if (fail)
+ ok = ambiguous && ok;
+ if (f->qualified_name && strcmp(f->qualified_name, "pkg.duplicate") == 0) {
+ duplicate_count++;
+ ok = ambiguous && ok;
+ }
+ if (f->qualified_name && strcmp(f->qualified_name, "pkg.unique") == 0) {
+ unique_count++;
+ ok = ambiguous == (fail != 0) && ok;
+ }
+ }
+ ok = duplicate_count == 2 && unique_count == 1 &&
+ (fail ? reg->func_qn_sorted == NULL : reg->func_qn_sorted != NULL) &&
+ cbm_registry_has_func_qn_prefix(reg, "pkg.", NULL) &&
+ !cbm_registry_has_func_qn_prefix(reg, "pkg.missing.", NULL) && ok;
+ }
+ }
+ cbm_arena_destroy(&arena);
+ ASSERT_TRUE(ok);
+ PASS();
+}
+
+/* A negative must have one real extracted source occurrence and no accepted
+ * resolution; a missing carrier cannot make it pass. */
+static bool pylsp_prefix_site_unresolved(const CBMCallArray *calls,
+ const CBMResolvedCallArray *resolved, const char *source,
+ const char *site_text, const char *caller) {
+ const char *site = strstr(source, site_text);
+ if (!site || strstr(site + 1, site_text))
+ return false;
+ uint32_t start = (uint32_t)(site - source), end = start + (uint32_t)strlen(site_text);
+ const CBMCall *found = NULL;
+ int count = 0;
+ for (int i = 0; i < calls->count; i++) {
+ const CBMCall *call = &calls->items[i];
+ if (call->enclosing_func_qn && strcmp(call->enclosing_func_qn, caller) == 0 &&
+ call->site_start_byte == start && call->site_end_byte == end) {
+ found = call;
+ count++;
+ }
+ }
+ return count == 1 && cbm_pipeline_find_lsp_resolution(resolved, found, false) == NULL;
+}
+
+TEST(pylsp_prefix_raw_per_file_and_shared_keep_exact_calls) {
+ static const char source[] = "import pkg\nimport vendor\n"
+ "def ping():\n return 1\n"
+ "def run():\n"
+ " pkg.sub.ping()\n"
+ " pkg.su.ping()\n"
+ " vendor.external.ping()\n";
+ CBMLSPDef defs[] = {
+ {.qualified_name = "pkg.sub.ping",
+ .short_name = "ping",
+ .label = "Function",
+ .def_module_qn = "pkg.sub",
+ .return_types = "int",
+ .lang = CBM_LANG_PYTHON},
+ {.qualified_name = "pkg.submarine.ping",
+ .short_name = "ping",
+ .label = "Function",
+ .def_module_qn = "pkg.submarine",
+ .return_types = "str",
+ .lang = CBM_LANG_PYTHON},
+ };
+ const char *names[] = {"pkg", "vendor"};
+ const char *qns[] = {"pkg", "vendor"};
+ bool ok = true;
+ for (int fail = 0; fail < 2; fail++) {
+ uint64_t probes_before =
+ atomic_load_explicit(&cbm_py_submodule_probes, memory_order_relaxed);
+ uint64_t visits_before =
+ atomic_load_explicit(&cbm_py_submodule_probe_visits, memory_order_relaxed);
+ cbm_registry_test_qn_order_fail_after(fail ? 0 : -1);
+ CBMFileResult *r = cbm_extract_file(source, (int)strlen(source), CBM_LANG_PYTHON, "pkg",
+ "sub.py", 0, NULL, NULL);
+ cbm_registry_test_qn_order_fail_after(-1);
+ bool healthy = r && !r->has_error && !r->parse_incomplete && !r->lsp_skipped &&
+ pylsp_module_has_def(r, "pkg.sub.ping") &&
+ pylsp_module_has_def(r, "pkg.sub.run");
+ ok = healthy &&
+ atomic_load_explicit(&cbm_py_submodule_probes, memory_order_relaxed) > probes_before &&
+ atomic_load_explicit(&cbm_py_submodule_probe_visits, memory_order_relaxed) >
+ visits_before &&
+ ok;
+ if (healthy) {
+ ok = pylsp_module_site_joins(&r->calls, &r->resolved_calls, source, "pkg.sub.ping()",
+ "pkg.sub.run", "pkg.sub.ping") &&
+ pylsp_prefix_site_unresolved(&r->calls, &r->resolved_calls, source,
+ "pkg.su.ping()", "pkg.sub.run") &&
+ pylsp_prefix_site_unresolved(&r->calls, &r->resolved_calls, source,
+ "vendor.external.ping()", "pkg.sub.run") &&
+ ok;
+ for (int shared_mode = 0; shared_mode < 2; shared_mode++) {
+ CBMArena arena, shared;
+ cbm_arena_init(&arena);
+ cbm_arena_init(&shared);
+ CBMTypeRegistry *reg = NULL;
+ cbm_registry_test_qn_order_fail_after(fail ? 0 : -1);
+ if (shared_mode)
+ reg = cbm_py_build_cross_registry(&shared, defs, 2);
+ CBMResolvedCallArray out = {0};
+ CBMLSPStatus status = CBM_LSP_SCOPE_FAILED;
+ probes_before =
+ atomic_load_explicit(&cbm_py_submodule_probes, memory_order_relaxed);
+ visits_before =
+ atomic_load_explicit(&cbm_py_submodule_probe_visits, memory_order_relaxed);
+ if (!shared_mode || reg) {
+ status = shared_mode
+ ? cbm_run_py_lsp_cross_with_registry_status(
+ &arena, source, (int)strlen(source), "pkg.sub", reg, names,
+ qns, 2, r->cached_tree, &out, NULL)
+ : cbm_run_py_lsp_cross_status(&arena, source, (int)strlen(source),
+ "pkg.sub", defs, 2, names, qns, 2,
+ r->cached_tree, &out, NULL);
+ }
+ cbm_registry_test_qn_order_fail_after(-1);
+ ok = status == CBM_LSP_COMPLETE &&
+ atomic_load_explicit(&cbm_py_submodule_probes, memory_order_relaxed) >
+ probes_before &&
+ atomic_load_explicit(&cbm_py_submodule_probe_visits, memory_order_relaxed) >
+ visits_before &&
+ pylsp_module_site_joins(&r->calls, &out, source, "pkg.sub.ping()",
+ "pkg.sub.run", "pkg.sub.ping") &&
+ pylsp_prefix_site_unresolved(&r->calls, &out, source, "pkg.su.ping()",
+ "pkg.sub.run") &&
+ pylsp_prefix_site_unresolved(&r->calls, &out, source, "vendor.external.ping()",
+ "pkg.sub.run") &&
+ ok;
+ cbm_arena_destroy(&arena);
+ cbm_arena_destroy(&shared);
+ }
+ }
+ if (r)
+ cbm_free_result(r);
+ }
+ ASSERT_TRUE(ok);
+ PASS();
+}
+
SUITE(py_lsp) {
+ RUN_TEST(pylsp_prefix_order_matches_iterator_and_chain_shadowing);
+ RUN_TEST(pylsp_prefix_order_survives_array_growth_rebuild_and_seal);
+ RUN_TEST(pylsp_prefix_order_allocation_failure_preserves_oracle);
+ RUN_TEST(pylsp_prefix_order_has_absolute_comparison_bound);
+ RUN_TEST(pylsp_prefix_order_preserves_exact_selection_and_ambiguity);
+ RUN_TEST(pylsp_prefix_raw_per_file_and_shared_keep_exact_calls);
+
RUN_TEST(pylsp_classbody_calls_join_extracted_caller);
RUN_TEST(pylsp_classbody_names_visible_to_later_statements_only);
RUN_TEST(pylsp_decorator_and_base_calls_join_extracted_caller);