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(build): make prod objects follow TEST_SEAMS; stop cx tmp leak

Martin Vogel4c466fc4f95f
diff --git a/Makefile.cbm b/Makefile.cbm
index 7a3cd31e1..00600672c 100644
--- a/Makefile.cbm
+++ b/Makefile.cbm
@@ -830,8 +830,14 @@ BUILD_DIR = build/c
# ── Object file compilation (grammars need relaxed warnings) ─────
# Grammar + tree-sitter runtime: compiled without -Werror (upstream code has warnings)
+# $(TEST_SEAM_DEFINE): these prod objects (grammars, ts_runtime, lsp_all) are
+# linked into the same binary as the CFLAGS_PROD sources, so both halves must
+# agree on the seam switch. lsp_all holds seam code of its own (ts_lsp.c's
+# budget probes, any seam counter a resolver defines): without the define a
+# TEST_SEAMS=1 binary silently lacks those seams, and a seam symbol that cbm.c
+# references fails to link. GRAMMAR_CFLAGS_TEST always carries the define.
GRAMMAR_CFLAGS = -std=c11 -D_DEFAULT_SOURCE -O2 -w -I$(CBM_DIR) -I$(TS_INCLUDE) -I$(TS_SRC) \
- $(MEMWASTE_VENDOR_CFLAGS)
+ $(TEST_SEAM_DEFINE) $(MEMWASTE_VENDOR_CFLAGS)
GRAMMAR_CFLAGS_TEST = -std=c11 -D_DEFAULT_SOURCE -g -O1 -w -I$(CBM_DIR) -I$(TS_INCLUDE) -I$(TS_SRC) \
-DCBM_ENABLE_TEST_SEAMS=1 $(SANITIZED_DEFINE) $(SANITIZE)
GRAMMAR_CFLAGS_TSAN = -std=c11 -D_DEFAULT_SOURCE -g -O1 -w -I$(CBM_DIR) -I$(TS_INCLUDE) -I$(TS_SRC) \
@@ -939,7 +945,9 @@ $(BUILD_DIR)/sqlite3.o: $(SQLITE3_SRC) | $(BUILD_DIR)
$(BUILD_DIR)/tsan_sqlite3.o: $(SQLITE3_SRC) | $(BUILD_DIR)
$(CC) $(SQLITE3_CFLAGS_TEST) $(TSAN_SANITIZE) -c -o $@ $<
-$(BUILD_DIR)/prod_sqlite3.o: $(SQLITE3_SRC) Makefile.cbm | $(BUILD_DIR)
+# .build-config: SQLITE3_CFLAGS carries MEMWASTE_VENDOR_CFLAGS (see the stamp
+# under "Production binary").
+$(BUILD_DIR)/prod_sqlite3.o: $(SQLITE3_SRC) Makefile.cbm $(BUILD_DIR)/.build-config | $(BUILD_DIR)
$(CC) $(SQLITE3_CFLAGS) -c -o $@ $<
# TRE regex (only compiled on Windows — POSIX uses system <regex.h>)
@@ -969,7 +977,7 @@ $(BUILD_DIR)/tre.o: $(TRE_SRC) | $(BUILD_DIR)
$(BUILD_DIR)/tsan_tre.o: $(TRE_SRC) | $(BUILD_DIR)
$(CC) $(TRE_CFLAGS) $(TSAN_SANITIZE) -c -o $@ $<
-$(BUILD_DIR)/prod_tre.o: $(TRE_SRC) | $(BUILD_DIR)
+$(BUILD_DIR)/prod_tre.o: $(TRE_SRC) $(BUILD_DIR)/.build-config | $(BUILD_DIR)
$(CC) $(TRE_CFLAGS) -O2 $(MEMWASTE_VENDOR_CFLAGS) -c -o $@ $<
endif
@@ -1123,28 +1131,35 @@ TS_RUNTIME_OBJ_PROD = $(BUILD_DIR)/prod_ts_runtime.o
LSP_OBJ_PROD = $(BUILD_DIR)/prod_lsp_all.o
PP_OBJ_PROD = $(BUILD_DIR)/prod_preprocessor.o
-$(BUILD_DIR)/prod_%.o: $(CBM_DIR)/%.c $(GRAMMAR_UNITY_DEPS) | $(BUILD_DIR)
+# Every prod object whose flags carry a BUILD_CONFIG_SIG variable
+# (TEST_SEAM_DEFINE via GRAMMAR_CFLAGS, MEMWASTE_VENDOR_CFLAGS) depends on the
+# $(BUILD_DIR)/.build-config stamp. These objects are compiled once and linked
+# into every later binary, so without the stamp a TEST_SEAMS or MEMWASTE flip
+# relinks the PREVIOUS flavour: a release binary keeps seam code compiled into
+# lsp_all, or a seams binary misses it. prod_mimalloc.o and the unixcoder blob
+# take no config variable and stay off the stamp.
+$(BUILD_DIR)/prod_%.o: $(CBM_DIR)/%.c $(GRAMMAR_UNITY_DEPS) $(BUILD_DIR)/.build-config | $(BUILD_DIR)
$(CC) $(GRAMMAR_CFLAGS) -c -o $@ $<
-$(BUILD_DIR)/prod_ts_runtime.o: $(TS_RUNTIME_DEPS) | $(BUILD_DIR)
+$(BUILD_DIR)/prod_ts_runtime.o: $(TS_RUNTIME_DEPS) $(BUILD_DIR)/.build-config | $(BUILD_DIR)
$(CC) $(GRAMMAR_CFLAGS) -c -o $@ $<
-$(BUILD_DIR)/prod_lsp_all.o: $(LSP_UNITY_DEPS) | $(BUILD_DIR)
+$(BUILD_DIR)/prod_lsp_all.o: $(LSP_UNITY_DEPS) $(BUILD_DIR)/.build-config | $(BUILD_DIR)
$(CC) $(GRAMMAR_CFLAGS) -c -o $@ $<
-$(BUILD_DIR)/prod_preprocessor.o: $(CBM_DIR)/preprocessor.cpp | $(BUILD_DIR)
+$(BUILD_DIR)/prod_preprocessor.o: $(CBM_DIR)/preprocessor.cpp $(BUILD_DIR)/.build-config | $(BUILD_DIR)
$(CXX) $(CXXFLAGS_PROD) -w -I$(CBM_DIR)/vendored -c -o $@ $<
# Vendored LZ4 (compiled separately, not unity-built via lz4_store.c)
LZ4_OBJ_PROD = $(BUILD_DIR)/prod_lz4.o $(BUILD_DIR)/prod_lz4hc.o
-$(BUILD_DIR)/prod_lz4.o: $(CBM_DIR)/vendored/lz4/lz4.c | $(BUILD_DIR)
+$(BUILD_DIR)/prod_lz4.o: $(CBM_DIR)/vendored/lz4/lz4.c $(BUILD_DIR)/.build-config | $(BUILD_DIR)
$(CC) -std=c11 -D_DEFAULT_SOURCE -O2 -w $(MEMWASTE_VENDOR_CFLAGS) -I$(CBM_DIR) -c -o $@ $<
-$(BUILD_DIR)/prod_lz4hc.o: $(CBM_DIR)/vendored/lz4/lz4hc.c | $(BUILD_DIR)
+$(BUILD_DIR)/prod_lz4hc.o: $(CBM_DIR)/vendored/lz4/lz4hc.c $(BUILD_DIR)/.build-config | $(BUILD_DIR)
$(CC) -std=c11 -D_DEFAULT_SOURCE -O2 -w $(MEMWASTE_VENDOR_CFLAGS) -I$(CBM_DIR)/vendored/lz4 -c -o $@ $<
# Vendored zstd (compiled separately, not unity-built via zstd_store.c)
ZSTD_OBJ_PROD = $(BUILD_DIR)/prod_zstd.o
-$(BUILD_DIR)/prod_zstd.o: $(CBM_DIR)/vendored/zstd/zstd.c | $(BUILD_DIR)
+$(BUILD_DIR)/prod_zstd.o: $(CBM_DIR)/vendored/zstd/zstd.c $(BUILD_DIR)/.build-config | $(BUILD_DIR)
$(CC) -std=c11 -D_DEFAULT_SOURCE -O2 -w $(MEMWASTE_VENDOR_CFLAGS) -I$(CBM_DIR)/vendored/zstd -c -o $@ $<
OBJS_VENDORED_PROD = $(MIMALLOC_OBJ_PROD) $(SQLITE3_OBJ_PROD) $(TRE_OBJ_PROD) $(GRAMMAR_OBJS_PROD) $(TS_RUNTIME_OBJ_PROD) $(LSP_OBJ_PROD) $(PP_OBJ_PROD) $(LZ4_OBJ_PROD) $(ZSTD_OBJ_PROD) $(UNIXCODER_OBJ)
@@ -1160,7 +1175,8 @@ MAIN_SRC = src/main.c
# configuration and it is rewritten only when that content changes, so ordinary
# incremental builds are unaffected. (Single-quoted: the signature may contain
# the escaped quotes of -DCBM_VERSION, which are literal inside '', but must not
-# contain a literal single quote.)
+# contain a literal single quote.) The config-flavoured prod objects above
+# depend on the stamp for the same reason as the binary.
# SANITIZE belongs here: it changes the instrumentation compiled into every
# object, so a build that only changes it MUST rebuild. Its absence made
# `scripts/test.sh SANITIZE= ...` -- the documented way to get a plain build
diff --git a/tests/test_complexity.c b/tests/test_complexity.c
index d9c651ff9..a512622f7 100644
--- a/tests/test_complexity.c
+++ b/tests/test_complexity.c
@@ -433,36 +433,58 @@ static double cx_ratio(double num, double den) {
return den > 0.0 ? num / den : 0.0;
}
+typedef int (*CxCorpusBuilder)(const char *root, int k);
+
+static void cx_remove_corpus(const char *root) {
+ if (th_rmtree(root) != 0) {
+ fprintf(stderr, " [complexity] WARN: could not remove temp corpus %s\n", root);
+ }
+}
+
+/* Build one corpus shape at k and 2k in two fresh temp dirs, run the pipeline
+ * on both, and remove both dirs before returning — on success and on every
+ * failure path. Only the metrics outlive the call; the corpora used to stay
+ * behind in the temp root, four per suite run. Only dirs this call created
+ * are removed (the temp root is shared with concurrent runs). */
+static int cx_measure(CxCorpusBuilder build, const char *prefix_a, const char *prefix_b,
+ CxMetrics *out_a, CxMetrics *out_b) {
+ const char *tmp = cbm_tmpdir();
+ char root_a[512];
+ char root_b[512];
+ snprintf(root_a, sizeof(root_a), "%s/%s_XXXXXX", tmp, prefix_a);
+ snprintf(root_b, sizeof(root_b), "%s/%s_XXXXXX", tmp, prefix_b);
+ bool made_a = cbm_mkdtemp(root_a) != NULL;
+ bool made_b = made_a && cbm_mkdtemp(root_b) != NULL;
+ int rc = -1;
+ if (made_b && build(root_a, CX_K_BASE) == 0 && build(root_b, CX_K_BASE * 2) == 0) {
+ char db_a[600];
+ 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) {
+ rc = 0;
+ }
+ }
+ if (made_b) {
+ cx_remove_corpus(root_b);
+ }
+ if (made_a) {
+ cx_remove_corpus(root_a);
+ }
+ return rc;
+}
+
/* Shared across the suite so the report test reuses the measured pair instead
* of paying two more pipeline runs. */
static CxMetrics g_cx_base;
static CxMetrics g_cx_doubled;
static bool g_cx_measured = false;
-static char g_cx_root_base[512];
-static char g_cx_root_doubled[512];
static int cx_measure_pair(void) {
if (g_cx_measured) {
return 0;
}
- const char *tmp = cbm_tmpdir();
- snprintf(g_cx_root_base, sizeof(g_cx_root_base), "%s/cbm_cx_base_XXXXXX", tmp);
- snprintf(g_cx_root_doubled, sizeof(g_cx_root_doubled), "%s/cbm_cx_dbl_XXXXXX", tmp);
- if (!cbm_mkdtemp(g_cx_root_base) || !cbm_mkdtemp(g_cx_root_doubled)) {
- return -1;
- }
- if (cx_build_corpus(g_cx_root_base, CX_K_BASE) != 0 ||
- cx_build_corpus(g_cx_root_doubled, CX_K_BASE * 2) != 0) {
- return -1;
- }
- char db1[600];
- char db2[600];
- snprintf(db1, sizeof(db1), "%s/cx.db", g_cx_root_base);
- snprintf(db2, sizeof(db2), "%s/cx.db", g_cx_root_doubled);
- if (cx_run(g_cx_root_base, db1, &g_cx_base) != 0) {
- return -1;
- }
- if (cx_run(g_cx_root_doubled, db2, &g_cx_doubled) != 0) {
+ if (cx_measure(cx_build_corpus, "cbm_cx_base", "cbm_cx_dbl", &g_cx_base, &g_cx_doubled) != 0) {
return -1;
}
g_cx_measured = true;
@@ -553,25 +575,8 @@ static int cx_measure_bigpkg_pair(void) {
if (g_cx_big_measured) {
return 0;
}
- const char *tmp = cbm_tmpdir();
- char ra[512];
- char rb[512];
- snprintf(ra, sizeof(ra), "%s/cbm_cxbig_a_XXXXXX", tmp);
- snprintf(rb, sizeof(rb), "%s/cbm_cxbig_b_XXXXXX", tmp);
- if (!cbm_mkdtemp(ra) || !cbm_mkdtemp(rb)) {
- return -1;
- }
- if (cx_build_bigpkg(ra, CX_K_BASE) != 0 || cx_build_bigpkg(rb, CX_K_BASE * 2) != 0) {
- return -1;
- }
- char db1[600];
- char db2[600];
- snprintf(db1, sizeof(db1), "%s/cx.db", ra);
- snprintf(db2, sizeof(db2), "%s/cx.db", rb);
- if (cx_run(ra, db1, &g_cx_big_base) != 0) {
- return -1;
- }
- if (cx_run(rb, db2, &g_cx_big_doubled) != 0) {
+ if (cx_measure(cx_build_bigpkg, "cbm_cxbig_a", "cbm_cxbig_b", &g_cx_big_base,
+ &g_cx_big_doubled) != 0) {
return -1;
}
g_cx_big_measured = true;