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.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
fix(pipeline): never mint a staging path from a staging path
Martin Vogel4ce6b115956d
diff --git a/src/pipeline/pipeline.c b/src/pipeline/pipeline.cindex 0ffb46c0b..247f72378 100644--- a/src/pipeline/pipeline.c+++ b/src/pipeline/pipeline.c@@ -38,6 +38,7 @@ enum { CBM_DIR_PERMS = 0755, PL_RING = 4, PL_RING_MASK = 3, PL_SEQ_PASSES = 6 };#include "foundation/profile.h"#include "foundation/mem.h"+#include <ctype.h>#include <errno.h>#include <stdint.h>#include <stdio.h>@@ -2415,12 +2416,56 @@ static bool ensure_db_parent(const char *path) {return ok;}+/* Length of the path a stage was minted for: the input itself unless its+ * basename has exactly the minted shape "<name>.stage.<6 alphanumerics>", in+ * which case the root is <name>. The outer run rewrites the pipeline's db_path+ * to its stage, so the inner publication (dump and delta clone) used to mint+ * ITS stage from that stage: <db>.stage.A.stage.B, with -wal/-shm beside it+ * (#1839). Minting from the root keeps every generation's stage a sibling of+ * the live database. Only the exact minted shape is recognised: a database+ * named "x.stage.y.db" is not a stage and keeps its full name. */+enum { CBM_STAGE_SUFFIX_RANDOM_CHARS = 6 };+static const char cbm_stage_marker[] = ".stage.";++static bool stage_suffix_at(const char *tail) {+ if (strncmp(tail, cbm_stage_marker, sizeof(cbm_stage_marker) - 1) != 0) {+ return false;+ }+ const char *random = tail + sizeof(cbm_stage_marker) - 1;+ for (int i = 0; i < CBM_STAGE_SUFFIX_RANDOM_CHARS; i++) {+ if (!isalnum((unsigned char)random[i])) {+ return false;+ }+ }+ return random[CBM_STAGE_SUFFIX_RANDOM_CHARS] == '\0';+}++static size_t stage_root_length(const char *path) {+ size_t len = strlen(path);+ const size_t suffix_len = sizeof(cbm_stage_marker) - 1 + CBM_STAGE_SUFFIX_RANDOM_CHARS;+ if (len <= suffix_len) {+ return len;+ }+ size_t root_len = len - suffix_len;+ /* The marker must sit inside the basename, never span a separator. */+ for (size_t i = root_len; i < len; i++) {+ if (path[i] == '/'+#ifdef _WIN32+ || path[i] == '\\'+#endif+ ) {+ return len;+ }+ }+ return stage_suffix_at(path + root_len) ? root_len : len;+}+static char *create_staging_path(const char *final_path) {if (!final_path) {return NULL;}static const char suffix[] = ".stage.XXXXXX";- size_t final_len = strlen(final_path);+ size_t final_len = stage_root_length(final_path);if (final_len > SIZE_MAX - sizeof(suffix)) {return NULL;}diff --git a/tests/test_pipeline.c b/tests/test_pipeline.cindex 0f6332cb2..e724736e2 100644--- a/tests/test_pipeline.c+++ b/tests/test_pipeline.c@@ -3416,6 +3416,86 @@ TEST(pipeline_publication_never_uses_a_predictable_staging_path) {PASS();}+static int count_substring(const char *haystack, const char *needle) {+ int count = 0;+ size_t needle_len = strlen(needle);+ for (const char *at = strstr(haystack, needle); at; at = strstr(at + needle_len, needle)) {+ count++;+ }+ return count;+}++static int count_nested_stage_entries(const char *dir_path, const char *db_basename) {+ cbm_dir_t *dir = cbm_opendir(dir_path);+ if (!dir) {+ return -1;+ }+ size_t base_len = strlen(db_basename);+ int count = 0;+ cbm_dirent_t *entry;+ while ((entry = cbm_readdir(dir)) != NULL) {+ if (strncmp(entry->name, db_basename, base_len) == 0 &&+ count_substring(entry->name + base_len, ".stage.") >= 2) {+ count++;+ }+ }+ cbm_closedir(dir);+ return count;+}++/* #1839: the outer run rewrites the pipeline's db_path to its stage, so the+ * inner publication (dump or delta clone) minted ITS stage from a stage:+ * <db>.stage.A.stage.B, plus -wal/-shm under WAL. A generation's stage is a+ * sibling of the live database, whichever path it is minted from; a database+ * whose own basename merely contains ".stage." is not a stage and keeps its+ * full name. */+TEST(pipeline_stage_names_never_nest) {+ char tmp[256];+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_stage_nesting_XXXXXX");+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));+ char db_path[512];+ snprintf(db_path, sizeof(db_path), "%s/generation.db", tmp);+ char odd_db[512];+ snprintf(odd_db, sizeof(odd_db), "%s/x.stage.y.db", tmp);++ char *outer = cbm_pipeline_create_staging_path(db_path);+ ASSERT_NOT_NULL(outer);+ char *inner = cbm_pipeline_create_staging_path(outer);+ ASSERT_NOT_NULL(inner);+ char *odd_stage = cbm_pipeline_create_staging_path(odd_db);+ ASSERT_NOT_NULL(odd_stage);++ size_t db_len = strlen(db_path);+ size_t odd_len = strlen(odd_db);+ int outer_tokens = count_substring(outer, ".stage.");+ int inner_tokens = count_substring(inner, ".stage.");+ bool inner_is_sibling =+ strncmp(inner, db_path, db_len) == 0 && strncmp(inner + db_len, ".stage.", 7) == 0;+ bool inner_distinct = strcmp(inner, outer) != 0;+ bool odd_keeps_basename =+ strncmp(odd_stage, odd_db, odd_len) == 0 && strncmp(odd_stage + odd_len, ".stage.", 7) == 0;+ int nested_entries = count_nested_stage_entries(tmp, "generation.db");++ cbm_pipeline_discard_stage(inner);+ cbm_pipeline_discard_stage(outer);+ cbm_pipeline_discard_stage(odd_stage);+ int leftover_entries = count_generation_stage_artifacts(tmp, "generation.db") ++ count_generation_stage_artifacts(tmp, "x.stage.y.db");+ free(inner);+ free(outer);+ free(odd_stage);+ th_rmtree(tmp);++ ASSERT_EQ(outer_tokens, 1);+ ASSERT_EQ(inner_tokens, 1);+ ASSERT_TRUE(inner_is_sibling);+ ASSERT_TRUE(inner_distinct);+ ASSERT_TRUE(odd_keeps_basename);+ ASSERT_EQ(nested_entries, 0);+ ASSERT_EQ(leftover_entries, 0);+ PASS();+}+/* Discovery and extraction must describe the same immutable generation. A* source file created after extraction is not present in the original file* list, so merely re-hashing that list cannot detect the race. Publication@@ -14120,6 +14200,7 @@ SUITE(pipeline_semantic_manifest_repro) {RUN_TEST(pipeline_git_context_change_forces_full_and_refreshes_branch);RUN_TEST(pipeline_global_extension_config_change_forces_full);RUN_TEST(pipeline_publication_never_uses_a_predictable_staging_path);+ RUN_TEST(pipeline_stage_names_never_nest);RUN_TEST(pipeline_source_mutation_before_publication_preserves_previous_generation);RUN_TEST(pipeline_source_addition_before_publication_preserves_previous_generation);RUN_TEST(pipeline_tsconfig_mutation_before_publication_preserves_previous_generation);