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): keep background refreshes on the parallel route

Martin Vogeldb74648c22b9
diff --git a/src/pipeline/pipeline.c b/src/pipeline/pipeline.c
index 5474e8f5e..99bf23f6e 100644
--- a/src/pipeline/pipeline.c
+++ b/src/pipeline/pipeline.c
@@ -1437,6 +1437,22 @@ void cbm_pipeline_set_committed_counts(cbm_pipeline_t *p, int nodes, int edges)
}
}
+/* Two workers is the smallest width that takes the parallel route. */
+enum { PL_PARALLEL_MIN_WORKERS = 2 };
+
+/* Background refreshes leave CPU headroom, but never drop below two workers
+ * while the foreground width is two or more. WHY: one worker sends the indexer
+ * down the sequential route, which still builds a different graph than the
+ * parallel route (fewer Route and HANDLES edges, churned CALLS). Until the two
+ * routes agree, an automatic refresh must not change the graph only because
+ * the host has two or fewer performance cores. */
+int cbm_pipeline_background_worker_width(int foreground, int headroom) {
+ if (foreground >= PL_PARALLEL_MIN_WORKERS && headroom < PL_PARALLEL_MIN_WORKERS) {
+ return PL_PARALLEL_MIN_WORKERS;
+ }
+ return headroom;
+}
+
/* Effective worker count. The crash supervisor re-runs its worker single-
* threaded (CBM_INDEX_SINGLE_THREAD=1) so a per-file marker can pin the EXACT
* crasher; a parallel re-run would race the marker. Honour that override
@@ -1447,8 +1463,11 @@ int cbm_pipeline_worker_count(const cbm_pipeline_t *p) {
int workers;
if (st && st[0] == '1') {
workers = 1;
+ } else if (p && p->background) {
+ workers = cbm_pipeline_background_worker_width(cbm_default_worker_count(true),
+ cbm_default_worker_count(false));
} else {
- workers = cbm_default_worker_count(!p || !p->background);
+ workers = cbm_default_worker_count(true);
}
#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_API
atomic_store(&g_last_worker_count, workers);
diff --git a/src/pipeline/pipeline.h b/src/pipeline/pipeline.h
index a9ac97935..e7222e4dd 100644
--- a/src/pipeline/pipeline.h
+++ b/src/pipeline/pipeline.h
@@ -79,6 +79,11 @@ void cbm_pipeline_set_background(cbm_pipeline_t *p, bool background);
* crash-recovery overrides. */
int cbm_pipeline_worker_count(const cbm_pipeline_t *p);
+/* Background width from the foreground width and the headroom width: the
+ * headroom, floored at two whenever the foreground width is two or more so an
+ * automatic refresh keeps the parallel route. */
+int cbm_pipeline_background_worker_width(int foreground, int headroom);
+
#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_API
void cbm_pipeline_worker_count_test_reset(void);
int cbm_pipeline_worker_count_test_last(void);
diff --git a/tests/test_mcp.c b/tests/test_mcp.c
index 2d6ba3ea3..e20de2034 100644
--- a/tests/test_mcp.c
+++ b/tests/test_mcp.c
@@ -23303,7 +23303,9 @@ static int mcp_assert_inprocess_worker_policy(bool committed, bool automatic) {
}
/* Capture while CBM_WORKERS is still unset. Restoring first makes the
* expected count follow the lane override instead of this policy. */
- int expected = cbm_default_worker_count(!committed);
+ int expected = committed ? cbm_pipeline_background_worker_width(cbm_default_worker_count(true),
+ cbm_default_worker_count(false))
+ : cbm_default_worker_count(true);
mcp_test_restore_env(environment, sizeof(environment) / sizeof(environment[0]));
bool cleaned = !cache_ready || th_rmtree(cache) == 0;
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index 96825bcca..e9c636d83 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -140,12 +140,19 @@ TEST(pipeline_background_worker_policy_preserves_overrides) {
int foreground_default = cbm_pipeline_worker_count(foreground);
int background_default = cbm_pipeline_worker_count(background);
bool defaults_match = foreground_default == cbm_default_worker_count(true) &&
- background_default == cbm_default_worker_count(false) &&
- foreground_default >= 1 && background_default >= 1;
-
+ background_default == cbm_pipeline_background_worker_width(
+ cbm_default_worker_count(true),
+ cbm_default_worker_count(false)) &&
+ foreground_default >= 1 && background_default >= 1 &&
+ (foreground_default < 2 || background_default >= 2);
+
+ /* A pinned single worker leaves no parallel route to preserve. */
+ cbm_setenv("CBM_WORKERS", "1", 1);
+ bool env_override =
+ cbm_pipeline_worker_count(foreground) == 1 && cbm_pipeline_worker_count(background) == 1;
cbm_setenv("CBM_WORKERS", "3", 1);
- bool env_override = cbm_pipeline_worker_count(foreground) == 3 &&
- cbm_pipeline_worker_count(background) == 3;
+ env_override = env_override && cbm_pipeline_worker_count(foreground) == 3 &&
+ cbm_pipeline_worker_count(background) == 3;
cbm_setenv("CBM_INDEX_SINGLE_THREAD", "1", 1);
bool recovery_override = cbm_pipeline_worker_count(foreground) == 1 &&
@@ -165,6 +172,25 @@ TEST(pipeline_background_worker_policy_preserves_overrides) {
PASS();
}
+/* An automatic refresh keeps CPU headroom but never falls to one worker, the
+ * sequential route, while the foreground width could run in parallel. Each
+ * host shape is given as the two widths cbm_default_worker_count() derives
+ * from it (foreground: every core; headroom: performance cores minus one, at
+ * least one), so the verdict does not depend on the machine running it. */
+TEST(pipeline_background_worker_width_keeps_parallel_route) {
+ /* One core: foreground 1, headroom 1. Nothing to preserve. */
+ ASSERT_EQ(cbm_pipeline_background_worker_width(1, 1), 1);
+ /* Two performance cores: headroom alone would be 1. */
+ ASSERT_EQ(cbm_pipeline_background_worker_width(2, 1), 2);
+ /* Two performance cores beside efficiency cores: same floor. */
+ ASSERT_EQ(cbm_pipeline_background_worker_width(8, 1), 2);
+ /* Larger hosts keep their headroom unchanged. */
+ ASSERT_EQ(cbm_pipeline_background_worker_width(3, 2), 2);
+ ASSERT_EQ(cbm_pipeline_background_worker_width(4, 3), 3);
+ ASSERT_EQ(cbm_pipeline_background_worker_width(18, 5), 5);
+ PASS();
+}
+
#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS
enum { SURFACE_BUDGET_FILES = 5 };
@@ -28325,6 +28351,7 @@ SUITE(pipeline) {
/* Lifecycle */
RUN_TEST(pipeline_create_free);
RUN_TEST(pipeline_background_worker_policy_preserves_overrides);
+ RUN_TEST(pipeline_background_worker_width_keeps_parallel_route);
#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS
RUN_TEST(pipeline_surface_respects_total_worker_budget);
#endif