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): spilling must not change the graph

Martin Vogel9a2bdc8c1dfe
diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.c
index 7e9dbbdea..9c251d3f1 100644
--- a/src/pipeline/pass_parallel.c
+++ b/src/pipeline/pass_parallel.c
@@ -1020,6 +1020,20 @@ static void extract_worker(int worker_id, void *ctx_ptr) {
pp_spill_enter(ec, "near_budget");
}
}
+ /* The MACHINE can be out of memory while our own charge sits well
+ * under the budget: measured 2026-09-18 on a 48 GB host, 22 GB
+ * charged against a 24 GB budget, and the OS killed the worker
+ * anyway because a VM held the rest. Spill on real scarcity too.
+ * RELIEF ONLY — `over` is deliberately NOT set from this, so the
+ * futility/abort path below stays keyed to OUR budget. Another
+ * process's allocation spike must never fail this run.
+ * The cheap charge comparison guards the syscall, so the pressure
+ * query costs nothing until we are already in the danger zone. */
+ if (!pp_spill_active(ec) && cbm_mem_charged() > cbm_mem_budget() / 2 &&
+ cbm_mem_system_under_pressure()) {
+ pp_spill_enter(ec, "system_pressure");
+ (void)pp_spill_sweep(ec, worker_id);
+ }
bool settling = false;
if (over) {
/* Admission control, first response: park what can be parked.
@@ -1652,8 +1666,23 @@ int cbm_build_registry_from_cache(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t
rels[i] = files[i].rel_path;
}
}
- CBMHashTable *namespace_map =
- cbm_pipeline_namespace_map_build(ctx->project_name, result_cache, rels, file_count);
+ /* Built from every file, including the ones already parked on disk: their
+ * results are NULL in result_cache, and a file absent from this map does
+ * not fail to resolve, it resolves through the looser fallback. Spilling
+ * therefore used to CHANGE the graph rather than merely delay it -- php
+ * measured 57,182 edges in memory against 59,379 while spilling, the same
+ * binary and corpus (2026-09-18), differing in both directions. */
+ const char **namespaces = cbm_calloc(CBM_MEM_CLASS_OTHER, (size_t)file_count * sizeof(char *));
+ CBMHashTable *namespace_map = NULL;
+ if (namespaces) {
+ for (int i = 0; i < file_count; i++) {
+ namespaces[i] = result_cache[i] ? result_cache[i]->namespace_name
+ : cbm_result_spill_namespace(ctx->spill, i);
+ }
+ namespace_map =
+ cbm_pipeline_namespace_map_build_names(ctx->project_name, namespaces, rels, file_count);
+ cbm_free(CBM_MEM_CLASS_OTHER, namespaces);
+ }
free(rels);
for (int i = 0; i < file_count; i++) {
diff --git a/src/pipeline/pass_pkgmap.c b/src/pipeline/pass_pkgmap.c
index 40e4c6c3f..4715a9275 100644
--- a/src/pipeline/pass_pkgmap.c
+++ b/src/pipeline/pass_pkgmap.c
@@ -2100,13 +2100,19 @@ const cbm_gbuf_node_t *cbm_pipeline_resolve_import_node(const cbm_pipeline_ctx_t
/* ── Namespace map ───────────────────────────────────────────────── */
-CBMHashTable *cbm_pipeline_namespace_map_build(const char *project_name,
- CBMFileResult *const *results,
- const char *const *rels, int count) {
+/* The namespace names themselves, so a caller that has parked some results on
+ * disk can still contribute their namespaces (see
+ * cbm_result_spill_namespace). A file missing from this map does not fail to
+ * resolve -- it resolves DIFFERENTLY, through the looser fallback, which is why
+ * an incomplete map changed edge counts in both directions rather than only
+ * losing edges. */
+CBMHashTable *cbm_pipeline_namespace_map_build_names(const char *project_name,
+ const char *const *namespaces,
+ const char *const *rels, int count) {
CBMHashTable *map = NULL;
for (int i = 0; i < count; i++) {
- const CBMFileResult *r = results[i];
- if (!r || !r->namespace_name || !r->namespace_name[0] || !rels[i]) {
+ const char *namespace_name = namespaces[i];
+ if (!namespace_name || !namespace_name[0] || !rels[i]) {
continue;
}
if (!map) {
@@ -2122,7 +2128,7 @@ CBMHashTable *cbm_pipeline_namespace_map_build(const char *project_name,
/* Normalize the namespace key to dot-separated form so it matches the
* dot-normalized lookups in cbm_pipeline_resolve_import_node (PHP uses
* '\\', some grammars '::' or '/'). */
- char *key = strdup(r->namespace_name);
+ char *key = strdup(namespace_name);
if (!key) {
free(file_qn);
continue;
@@ -2162,6 +2168,25 @@ CBMHashTable *cbm_pipeline_namespace_map_build(const char *project_name,
return map;
}
+/* Convenience for callers whose results are all in memory (the sequential
+ * definitions pass). A caller that can SPILL must use the _names variant and
+ * fill the parked slots from cbm_result_spill_namespace, or its map silently
+ * loses those files. */
+CBMHashTable *cbm_pipeline_namespace_map_build(const char *project_name,
+ CBMFileResult *const *results,
+ const char *const *rels, int count) {
+ const char **names = cbm_calloc(CBM_MEM_CLASS_OTHER, (size_t)count * sizeof(char *));
+ if (!names) {
+ return NULL;
+ }
+ for (int i = 0; i < count; i++) {
+ names[i] = results[i] ? results[i]->namespace_name : NULL;
+ }
+ CBMHashTable *map = cbm_pipeline_namespace_map_build_names(project_name, names, rels, count);
+ cbm_free(CBM_MEM_CLASS_OTHER, names);
+ return map;
+}
+
static void ns_map_free_entry(const char *key, void *value, void *ud) {
(void)ud;
free((void *)key); /* strdup'd in cbm_pipeline_namespace_map_build */
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index 0a3820142..aa675603d 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -238,6 +238,13 @@ const cbm_gbuf_node_t *cbm_pipeline_resolve_import_node(const cbm_pipeline_ctx_t
CBMHashTable *cbm_pipeline_namespace_map_build(const char *project_name,
CBMFileResult *const *results,
const char *const *rels, int count);
+/* The same map built from the namespace names directly. The parallel pass needs
+ * this: results it has spilled are NULL in its cache, and a file missing from
+ * the map does not fail to resolve -- it resolves through the looser fallback,
+ * so an incomplete map CHANGES the graph instead of shrinking it. */
+CBMHashTable *cbm_pipeline_namespace_map_build_names(const char *project_name,
+ const char *const *namespaces,
+ const char *const *rels, int count);
void cbm_pipeline_namespace_map_free(CBMHashTable *map);
/* Parse a manifest file and collect pkg entries. Returns true if basename matched. */
diff --git a/tests/test_parallel.c b/tests/test_parallel.c
index 13477b67c..85df7d55d 100644
--- a/tests/test_parallel.c
+++ b/tests/test_parallel.c
@@ -119,6 +119,33 @@ static int setup_parallel_repo(void) {
" @Override\n public double area() { return 0.0; }\n}\n");
fclose(f);
+ /* NAMESPACE-declaring files whose import resolves through the namespace
+ * map (PHP `use`; C# `using` and Java package imports take the same path).
+ * Without these the spill-parity test compared a Go+Java fixture that never
+ * touches that map, so it passed while spilling silently changed import
+ * resolution for every namespaced repository: the map is built from the
+ * in-memory result cache, where a PARKED result is NULL, so a spilled file
+ * contributed no namespace at all and its imports fell through to the
+ * looser fallback (php corpus, 2026-09-18: 57,182 edges in memory against
+ * 59,379 while spilling, same binary, reproducible 3/3 each way). */
+ snprintf(path, sizeof(path), "%s/app", g_par_tmpdir);
+ cbm_mkdir(path);
+ snprintf(path, sizeof(path), "%s/app/Models.php", g_par_tmpdir);
+ f = fopen(path, "w");
+ if (!f)
+ return -1;
+ fprintf(f, "<?php\nnamespace App\\Models;\n\n"
+ "class User {\n public function name() { return \"u\"; }\n}\n");
+ fclose(f);
+ snprintf(path, sizeof(path), "%s/app/Services.php", g_par_tmpdir);
+ f = fopen(path, "w");
+ if (!f)
+ return -1;
+ fprintf(f, "<?php\nnamespace App\\Services;\n\nuse App\\Models\\User;\n\n"
+ "class UserService {\n"
+ " public function make() { $u = new User(); return $u->name(); }\n}\n");
+ fclose(f);
+
return 0;
}