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.

Merge remote-tracking branch 'origin/main' into fix/1084-background-index-cpu

Martin Vogelf70c6a92b220
diff --cc src/mcp/mcp.c
index 31efa9420,f6e43b317..a1c758cfc
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@@ -11108,9 -11849,11 +11849,12 @@@ static char *index_run_supervised_path(
yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
yyjson_mut_val *root = yyjson_mut_obj(doc);
yyjson_mut_doc_set_root(doc, root);
+ /* A doc_candidates setting the user set travels with the request (the
+ * worker reads no config); unset, the worker's index mode decides. */
if (!yyjson_mut_obj_add_strcpy(doc, root, "repo_path", root_path) ||
!cbm_mcp_index_policy_add_to_args(doc, root, &policy) ||
- !cbm_mcp_index_doc_candidates_add_to_args(doc, root, srv ? srv->config : NULL)) {
++ !cbm_mcp_index_doc_candidates_add_to_args(doc, root, srv ? srv->config : NULL) ||
+ !yyjson_mut_obj_add_bool(doc, root, "_cbm_background", true)) {
yyjson_mut_doc_free(doc);
return NULL;
}
@@@ -11687,12 -12457,9 +12475,13 @@@ static char *handle_index_repository(cb
return cbm_mcp_text_result("invalid project name", true);
}
free(name_override);
+ /* Headroom follows the final project name, decided before artifact
+ * bootstrap can create the first local copy of this index. */
+ cbm_pipeline_set_background(p, background &&
+ project_has_committed_index(cbm_pipeline_project_name(p)));
cbm_pipeline_set_persistence(p, persistence);
cbm_pipeline_set_resource_policy(p, &resource_policy);
+ index_doc_candidates_apply(p, doc_candidates);
char *project_name = heap_strdup(cbm_pipeline_project_name(p));
@@@ -18128,7 -20766,7 +20788,8 @@@ static void *autoindex_thread(void *arg
cbm_log_warn("autoindex.err", "msg", "pipeline_create_failed");
return NULL;
}
+ index_doc_candidates_apply(p, index_doc_candidates_choice(srv, NULL));
+ cbm_pipeline_set_background(p, project_has_committed_index(cbm_pipeline_project_name(p)));
/* Block until any concurrent pipeline finishes */
cbm_pipeline_lock();
diff --cc src/pipeline/pipeline.c
index be881942e,92e23de64..16f56f00f
--- a/src/pipeline/pipeline.c
+++ b/src/pipeline/pipeline.c
@@@ -2715,7 -3852,9 +3877,9 @@@ static int run_structure_and_extraction
return CBM_NOT_FOUND;
}
- int worker_count = cbm_pipeline_worker_count(p);
- int worker_count = p->frozen ? 1 : effective_worker_count(true);
++ int worker_count = p->frozen ? 1 : cbm_pipeline_worker_count(p);
+ if (p->frozen)
+ frozen_observe(p, 0, 1);
CBM_PROF_START(t_extract_total);
int rc = (worker_count > SKIP_ONE && file_count > MIN_FILES_FOR_PARALLEL)
? run_parallel_pipeline(p, ctx, files, file_count, worker_count, &t)
@@@ -2807,10 -3977,13 +4002,13 @@@ static int cbm_pipeline_run_staged(cbm_
/* Snapshot every semantic input once before routing/extraction. The same
* bytes drive exact no-op comparison and are checked against a fresh
* rediscovery immediately before any replacement is published. */
- rc = mode_promoted
+ rc = p->frozen ? cbm_pipeline_build_frozen_manifest(p, files, file_count, p->excluded_dirs,
+ p->excluded_count, &baseline_manifest,
+ &baseline_count)
+ : mode_promoted
? cbm_pipeline_build_fresh_semantic_manifest(p, p->project_name, &baseline_manifest,
&baseline_count)
- : cbm_pipeline_build_semantic_manifest(p->project_name, p->repo_path, files,
+ : cbm_pipeline_build_semantic_manifest(p, p->project_name, p->repo_path, files,
file_count, p->excluded_dirs, p->excluded_count,
&p->git_ctx, p->userconfig, &baseline_manifest,
&baseline_count);
diff --cc src/pipeline/pipeline_incremental.c
index 8c348c3f7,6e6740573..954347138
--- a/src/pipeline/pipeline_incremental.c
+++ b/src/pipeline/pipeline_incremental.c
@@@ -1212,7 -1787,7 +1787,8 @@@ static int run_extract_resolve(cbm_pipe
* a full build takes, which is what makes its output converge. */
#define MIN_FILES_FOR_PARALLEL_INCR 50
- int worker_count = cbm_pipeline_worker_count(ctx->pipeline);
- int worker_count = cbm_pipeline_is_frozen(ctx->pipeline) ? 1 : cbm_default_worker_count(true);
++ int worker_count =
++ cbm_pipeline_is_frozen(ctx->pipeline) ? 1 : cbm_pipeline_worker_count(ctx->pipeline);
bool use_parallel =
closure != NULL || (worker_count > SKIP_ONE && ci > MIN_FILES_FOR_PARALLEL_INCR);
@@@ -1590,11 -2273,13 +2274,13 @@@ static int closure_probe_surfaces(cbm_p
_Atomic int64_t probe_ids;
atomic_init(&probe_ids, cbm_gbuf_next_id(probe_gbuf));
rc = cbm_parallel_extract(&probe_ctx, probe_files, probe_count, cache, &probe_ids,
- cbm_default_worker_count(true));
+ cbm_pipeline_worker_count(p));
}
if (rc == 0) {
- char **def_modules = (char **)calloc((size_t)probe_count, sizeof(char *));
- int *def_starts = (int *)calloc((size_t)probe_count + 1, sizeof(int));
+ char **def_modules =
+ (char **)cbm_calloc(CBM_MEM_CLASS_EXTRACT, (size_t)probe_count * sizeof(char *));
+ int *def_starts =
+ (int *)cbm_calloc(CBM_MEM_CLASS_EXTRACT, ((size_t)probe_count + 1) * sizeof(int));
int def_count = 0;
CBMLSPDef *defs = NULL;
CBMArena probe_arena;
@@@ -1602,12 -2287,10 +2288,14 @@@
if (def_modules && def_starts) {
defs = cbm_pxc_collect_all_defs(NULL, &probe_arena, cache, probe_files, probe_count,
project, def_modules, &def_count, def_starts);
- rc = def_count < 0
- ? CBM_PIPELINE_ABORT_PRESERVE_DB
- : cbm_lsp_surface_build_rows(NULL, project, cache, probe_files, probe_count,
- defs, def_starts, out_rows, out_count);
+ /* Extract left pipeline NULL so file errors are not recorded twice.
+ * Surface rows only borrow it for the background worker policy. */
+ probe_ctx.pipeline = p;
- rc = cbm_lsp_surface_build_rows(&probe_ctx, project, cache, probe_files, probe_count,
- defs, def_starts, out_rows, out_count);
++ rc = def_count < 0 ? CBM_PIPELINE_ABORT_PRESERVE_DB
++ : cbm_lsp_surface_build_rows(&probe_ctx, project, cache, probe_files,
++ probe_count, defs, def_starts, out_rows,
++ out_count);
+ probe_ctx.pipeline = NULL;
} else {
rc = -1;
}
diff --cc tests/test_mcp.c
index f42f0dc6a,1ced07c95..253a6f555
--- a/tests/test_mcp.c
+++ b/tests/test_mcp.c
@@@ -21514,10 -25326,16 +25479,20 @@@ SUITE(mcp)
RUN_TEST(autoindex_limit_guards_non_git_root_issue713);
RUN_TEST(autoindex_limit_admits_non_git_root_under_limit_issue713);
RUN_TEST(autoindex_limit_guards_git_root_issue713);
+ RUN_TEST(mcp_auto_index_in_process_fresh_project_uses_full_width);
+ RUN_TEST(mcp_auto_index_in_process_existing_index_keeps_headroom);
+ RUN_TEST(mcp_background_flag_fresh_project_uses_full_width);
+ RUN_TEST(mcp_background_flag_existing_index_keeps_headroom);
+ RUN_TEST(search_code_context_window_is_bounded);
+ RUN_TEST(search_code_context_extreme_values_answer_normally);
+ RUN_TEST(tool_query_graph_max_rows_above_ceiling_is_capped);
+ RUN_TEST(tool_search_graph_limit_above_ceiling_is_capped);
+ RUN_TEST(tool_trace_cursor_truncated_after_leg_is_rejected);
+ RUN_TEST(search_code_rejects_quote_in_file_pattern);
+
+ /* search_code: every match resolves inside the project root */
+ RUN_TEST(search_code_matches_stay_inside_project_root);
+ RUN_TEST(search_code_drops_walker_hits_that_resolve_outside_root);
}
/* Kept separate so daemon-coordination regressions can be iterated without
diff --cc tests/test_pipeline.c
index c541db5e2,7b868a7c1..7c947fec4
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@@ -5481,262 -7875,831 +7989,831 @@@ TEST(pipeline_exact_inputs_migrate_cove
PASS();
}
- /* An exact semantic manifest must fail closed when its repository root cannot
- * be traversed. A regular file is a deterministic cross-platform opendir
- * failure that does not depend on process permissions. */
- TEST(pipeline_semantic_manifest_rejects_non_directory_root) {
- char tmp[256];
- snprintf(tmp, sizeof(tmp), "/tmp/cbm_manifest_not_dir_XXXXXX");
- ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
- write_temp_file(tmp, "not-a-directory", "manifest root sentinel\n");
- char root_path[512];
- snprintf(root_path, sizeof(root_path), "%s/not-a-directory", tmp);
+ typedef struct {
+ bool published;
+ int rename_calls;
+ int export_count;
+ int exports_before_publish;
+ } artifact_publish_observer_t;
- cbm_file_hash_t *manifest = NULL;
- int manifest_count = -1;
- int rc = cbm_pipeline_build_semantic_manifest(NULL, "manifest-fail-closed", root_path, NULL, 0,
- NULL, 0, NULL, NULL, &manifest, &manifest_count);
- cbm_pipeline_free_semantic_manifest(manifest, manifest_count > 0 ? manifest_count : 0);
- th_rmtree(tmp);
+ static artifact_publish_observer_t *g_artifact_publish_observer;
- ASSERT_TRUE(rc != 0);
- ASSERT_EQ(manifest_count, 0);
- PASS();
+ static void observe_artifact_publish_log(const char *line) {
+ if (g_artifact_publish_observer && line && strstr(line, "msg=artifact.export")) {
+ g_artifact_publish_observer->export_count++;
+ if (!g_artifact_publish_observer->published) {
+ g_artifact_publish_observer->exports_before_publish++;
+ }
+ }
}
- /* A fully validated staged graph must be able to recover from a definitely
- * non-SQLite destination without deleting evidence or overwriting an earlier
- * quarantine. The replacement happens only after the corrupt bytes are moved. */
- TEST(pipeline_full_reindex_quarantines_corrupt_destination_without_overwrite) {
- static const char corrupt_bytes[] = "not a sqlite database\n";
- static const char previous_backup[] = "older quarantine\n";
- char tmp[256];
- snprintf(tmp, sizeof(tmp), "/tmp/cbm_publish_corrupt_XXXXXX");
- ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
- write_temp_file(tmp, "fresh.py", "def FreshAfterCorruption():\n return 1\n");
- write_temp_file(tmp, "generation.db", corrupt_bytes);
- write_temp_file(tmp, "generation.db.corrupt", previous_backup);
-
- char db_path[512];
- char old_backup_path[512];
- char new_backup_path[512];
- snprintf(db_path, sizeof(db_path), "%s/generation.db", tmp);
- snprintf(old_backup_path, sizeof(old_backup_path), "%s.corrupt", db_path);
- snprintf(new_backup_path, sizeof(new_backup_path), "%s.corrupt.1", db_path);
+ static int observe_successful_publish_rename(const char *staging_path, const char *final_path,
+ void *arg) {
+ artifact_publish_observer_t *observer = (artifact_publish_observer_t *)arg;
+ observer->rename_calls++;
+ int rc = cbm_rename_replace(staging_path, final_path);
+ observer->published = rc == 0;
+ return rc;
+ }
- cbm_pipeline_t *pipeline = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
- ASSERT_NOT_NULL(pipeline);
- int run_rc = cbm_pipeline_run(pipeline);
- char project[256];
- snprintf(project, sizeof(project), "%s", cbm_pipeline_project_name(pipeline));
- cbm_pipeline_free(pipeline);
+ static int run_observing_artifact_publish(cbm_pipeline_t *pipeline,
+ artifact_publish_observer_t *observer) {
+ cbm_pipeline_set_rename_hook_for_tests(pipeline, observe_successful_publish_rename, observer);
+ CBMLogLevel previous_level = cbm_log_get_level();
+ CBMLogFormat previous_format = cbm_log_get_format();
+ g_artifact_publish_observer = observer;
+ cbm_log_set_level(CBM_LOG_INFO);
+ cbm_log_set_format(CBM_LOG_FORMAT_TEXT);
+ cbm_log_set_sink_ex(observe_artifact_publish_log, CBM_LOG_SINK_REPLACE);
+ int rc = cbm_pipeline_run(pipeline);
+ cbm_log_set_sink(NULL);
+ cbm_log_set_format(previous_format);
+ cbm_log_set_level(previous_level);
+ g_artifact_publish_observer = NULL;
+ return rc;
+ }
- char old_observed[64] = {0};
- char new_observed[64] = {0};
- FILE *old_backup_file = cbm_fopen(old_backup_path, "rb");
- FILE *new_backup_file = cbm_fopen(new_backup_path, "rb");
- size_t old_size =
- old_backup_file ? fread(old_observed, 1, sizeof(old_observed) - 1, old_backup_file) : 0;
- size_t new_size =
- new_backup_file ? fread(new_observed, 1, sizeof(new_observed) - 1, new_backup_file) : 0;
- if (old_backup_file) {
- fclose(old_backup_file);
+ static bool pipeline_reports_excluded_dir(cbm_pipeline_t *pipeline, const char *rel_path) {
+ char **excluded = NULL;
+ int excluded_count = 0;
+ cbm_pipeline_get_excluded(pipeline, &excluded, &excluded_count);
+ for (int i = 0; i < excluded_count; i++) {
+ if (excluded[i] && strcmp(excluded[i], rel_path) == 0) {
+ return true;
+ }
}
- if (new_backup_file) {
- fclose(new_backup_file);
+ return false;
+ }
+
+ typedef struct {
+ int rc;
+ cbm_incremental_route_t route;
+ bool tools_excluded;
+ artifact_publish_observer_t publish;
+ } observed_fast_run_t;
+
+ static observed_fast_run_t run_observed_fast_pipeline(const char *repo_path, const char *db_path) {
+ observed_fast_run_t result = {.rc = CBM_NOT_FOUND};
+ cbm_pipeline_t *pipeline = cbm_pipeline_new(repo_path, db_path, CBM_MODE_FAST);
+ if (!pipeline) {
+ return result;
}
+ result.rc = run_observing_artifact_publish(pipeline, &result.publish);
+ result.route = cbm_pipeline_incremental_test_last_route();
+ result.tools_excluded = pipeline_reports_excluded_dir(pipeline, "tools");
+ cbm_pipeline_free(pipeline);
+ return result;
+ }
- int fresh_nodes = -1;
- cbm_store_t *store = cbm_store_open_path_query(db_path);
- if (store) {
- fresh_nodes = named_node_count(store, project, "FreshAfterCorruption");
- cbm_store_close(store);
+ typedef struct {
+ int rc;
+ int before_nodes;
+ int after_nodes;
+ } imported_generation_t;
+
+ static imported_generation_t import_artifact_generation(const char *repo_path,
+ const char *import_path,
+ const char *project) {
+ imported_generation_t result = {
+ .rc = cbm_artifact_import(repo_path, import_path),
+ .before_nodes = -1,
+ .after_nodes = -1,
+ };
+ if (result.rc == 0) {
+ observe_named_generation(import_path, project, "StoredBefore", "StoredAfter",
+ &result.before_nodes, &result.after_nodes);
}
- th_rmtree(tmp);
+ return result;
+ }
- ASSERT_EQ(run_rc, 0);
- ASSERT_EQ((int)old_size, (int)strlen(previous_backup));
- ASSERT_STR_EQ(old_observed, previous_backup);
- ASSERT_EQ((int)new_size, (int)strlen(corrupt_bytes));
- ASSERT_STR_EQ(new_observed, corrupt_bytes);
- ASSERT_EQ(fresh_nodes, 1);
- PASS();
+ static bool stored_mode_is_full(const char *db_path, const char *project) {
+ cbm_store_t *store = cbm_store_open_path(db_path);
+ if (!store) {
+ return false;
+ }
+ cbm_coverage_meta_t meta = {0};
+ bool full = cbm_store_coverage_meta_get(store, project, &meta) == CBM_STORE_OK &&
+ meta.index_mode && strcmp(meta.index_mode, "full") == 0;
+ cbm_store_coverage_meta_clear(&meta);
+ cbm_store_close(store);
+ return full;
}
- /* Readable legacy graphs are replaceable generations, not corruption. The
- * publication boundary must neither run current-schema migration against the
- * old edges table nor require an ADR table that did not exist yet. */
- TEST(pipeline_full_reindex_replaces_legacy_schema_without_quarantine) {
+ /* Once a repository contains a shared artifact, every subsequently published
+ * full generation must refresh it, even when persistence was not explicitly
+ * requested on that invocation. Otherwise the live DB advances while a clean
+ * teammate import silently restores the previous graph. */
+ TEST(pipeline_existing_artifact_refreshes_after_default_forced_full_reindex) {
char tmp[256];
- snprintf(tmp, sizeof(tmp), "/tmp/cbm_publish_legacy_XXXXXX");
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_artifact_refresh_XXXXXX");
ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
- write_temp_file(tmp, "fresh.py", "def FreshAfterLegacy():\n return 1\n");
-
+ char source_path[512];
char db_path[512];
+ char imported_path[512];
+ snprintf(source_path, sizeof(source_path), "%s/generation.py", tmp);
snprintf(db_path, sizeof(db_path), "%s/generation.db", tmp);
- sqlite3 *legacy = NULL;
- ASSERT_EQ(sqlite3_open_v2(db_path, &legacy, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL),
- SQLITE_OK);
- const char *legacy_schema =
- "CREATE TABLE projects(name TEXT PRIMARY KEY,indexed_at TEXT NOT NULL,root_path TEXT NOT "
- "NULL);"
- "CREATE TABLE file_hashes(project TEXT NOT NULL,rel_path TEXT NOT NULL,sha256 TEXT NOT "
- "NULL,mtime_ns INTEGER NOT NULL DEFAULT 0,size INTEGER NOT NULL DEFAULT 0,PRIMARY KEY "
- "(project,rel_path));"
- "CREATE TABLE nodes(id INTEGER PRIMARY KEY,project TEXT NOT NULL,label TEXT NOT NULL,name "
- "TEXT NOT NULL,qualified_name TEXT NOT NULL,file_path TEXT,start_line INTEGER,end_line "
- "INTEGER,properties TEXT DEFAULT '{}');"
- "CREATE TABLE edges(id INTEGER PRIMARY KEY,project TEXT NOT NULL,source_id INTEGER NOT "
- "NULL,target_id INTEGER NOT NULL,type TEXT NOT NULL,properties TEXT DEFAULT '{}',UNIQUE "
- "(source_id,target_id,type));";
- ASSERT_EQ(sqlite3_exec(legacy, legacy_schema, NULL, NULL, NULL), SQLITE_OK);
- sqlite3_stmt *insert_project = NULL;
- ASSERT_EQ(sqlite3_prepare_v2(
- legacy, "INSERT INTO projects(name,indexed_at,root_path) VALUES(?1,'legacy',?2);",
- -1, &insert_project, NULL),
- SQLITE_OK);
- const char *project_name = strrchr(tmp, '/');
- project_name = project_name ? project_name + 1 : tmp;
- ASSERT_EQ(sqlite3_bind_text(insert_project, 1, project_name, -1, SQLITE_TRANSIENT), SQLITE_OK);
- ASSERT_EQ(sqlite3_bind_text(insert_project, 2, tmp, -1, SQLITE_TRANSIENT), SQLITE_OK);
- ASSERT_EQ(sqlite3_step(insert_project), SQLITE_DONE);
- sqlite3_finalize(insert_project);
- sqlite3_close(legacy);
+ snprintf(imported_path, sizeof(imported_path), "%s/imported.db", tmp);
+ ASSERT_EQ(th_write_file(source_path, "def ArtifactGenerationBefore():\n return 1\n"), 0);
- cbm_pipeline_t *pipeline = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
- ASSERT_NOT_NULL(pipeline);
- int run_rc = cbm_pipeline_run(pipeline);
+ cbm_pipeline_incremental_test_reset_faults();
+ artifact_publish_observer_t baseline_observer = {0};
+ cbm_pipeline_t *baseline = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(baseline);
+ cbm_pipeline_set_persistence(baseline, true);
+ int baseline_rc = run_observing_artifact_publish(baseline, &baseline_observer);
char project[256];
- snprintf(project, sizeof(project), "%s", cbm_pipeline_project_name(pipeline));
- cbm_pipeline_free(pipeline);
+ snprintf(project, sizeof(project), "%s", cbm_pipeline_project_name(baseline));
+ cbm_pipeline_free(baseline);
+ ASSERT_TRUE(cbm_artifact_exists(tmp));
- int fresh_nodes = -1;
- cbm_store_t *store = cbm_store_open_path_query(db_path);
- if (store) {
- fresh_nodes = named_node_count(store, project, "FreshAfterLegacy");
- cbm_store_close(store);
+ ASSERT_EQ(th_write_file(source_path, "def ArtifactGenerationAfter():\n return 2\n"), 0);
+ cbm_pipeline_incremental_test_reset_faults();
+ artifact_publish_observer_t reindex_observer = {0};
+ cbm_pipeline_t *default_reindex = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(default_reindex);
+ int reindex_rc = run_observing_artifact_publish(default_reindex, &reindex_observer);
+ cbm_incremental_route_t reindex_route = cbm_pipeline_incremental_test_last_route();
+ cbm_pipeline_free(default_reindex);
+
+ /* A derived artifact must not become an input that forces another rebuild,
+ * and refreshing it must not switch the authoritative DB back to WAL. */
+ cbm_pipeline_incremental_test_reset_faults();
+ artifact_publish_observer_t explicit_observer = {0};
+ cbm_pipeline_t *explicit_noop = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(explicit_noop);
+ cbm_pipeline_set_persistence(explicit_noop, true);
+ int explicit_rc = run_observing_artifact_publish(explicit_noop, &explicit_observer);
+ cbm_incremental_route_t explicit_route = cbm_pipeline_incremental_test_last_route();
+ cbm_pipeline_free(explicit_noop);
+
+ cbm_pipeline_incremental_test_reset_faults();
+ artifact_publish_observer_t observer = {0};
+ cbm_pipeline_t *unchanged = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(unchanged);
+ int unchanged_rc = run_observing_artifact_publish(unchanged, &observer);
+ cbm_incremental_route_t unchanged_route = cbm_pipeline_incremental_test_last_route();
+ cbm_pipeline_free(unchanged);
+
+ char journal_mode[16] = {0};
+ sqlite3 *raw = NULL;
+ sqlite3_stmt *journal_stmt = NULL;
+ int journal_ok =
+ sqlite3_open_v2(db_path, &raw, SQLITE_OPEN_READONLY, NULL) == SQLITE_OK &&
+ sqlite3_prepare_v2(raw, "PRAGMA journal_mode;", -1, &journal_stmt, NULL) == SQLITE_OK &&
+ sqlite3_step(journal_stmt) == SQLITE_ROW;
+ if (journal_ok) {
+ const unsigned char *mode = sqlite3_column_text(journal_stmt, 0);
+ if (!mode || snprintf(journal_mode, sizeof(journal_mode), "%s", (const char *)mode) >=
+ (int)sizeof(journal_mode)) {
+ journal_ok = 0;
+ }
}
- int generated_column_count = 0;
- sqlite3 *current = NULL;
- sqlite3_stmt *column_query = NULL;
- if (sqlite3_open_v2(db_path, &current, SQLITE_OPEN_READONLY, NULL) == SQLITE_OK &&
- sqlite3_prepare_v2(current,
- "SELECT count(*) FROM pragma_table_xinfo('edges') "
- "WHERE name='local_name_gen';",
- -1, &column_query, NULL) == SQLITE_OK &&
- sqlite3_step(column_query) == SQLITE_ROW) {
- generated_column_count = sqlite3_column_int(column_query, 0);
+ sqlite3_finalize(journal_stmt);
+ if (raw) {
+ sqlite3_close(raw);
}
- sqlite3_finalize(column_query);
- if (current) {
- sqlite3_close(current);
+
+ int live_before = -1;
+ int live_after = -1;
+ cbm_store_t *live_store = cbm_store_open_path(db_path);
+ if (live_store) {
+ live_before = named_node_count(live_store, project, "ArtifactGenerationBefore");
+ live_after = named_node_count(live_store, project, "ArtifactGenerationAfter");
+ cbm_store_close(live_store);
}
- char quarantine_path[512];
- snprintf(quarantine_path, sizeof(quarantine_path), "%s.corrupt", db_path);
- cbm_path_info_t quarantine_info;
- bool quarantined = cbm_path_info_utf8(quarantine_path, &quarantine_info) == 0;
+
+ int import_rc = cbm_artifact_import(tmp, imported_path);
+ int artifact_before = -1;
+ int artifact_after = -1;
+ cbm_store_t *artifact_store = import_rc == 0 ? cbm_store_open_path(imported_path) : NULL;
+ if (artifact_store) {
+ artifact_before = named_node_count(artifact_store, project, "ArtifactGenerationBefore");
+ artifact_after = named_node_count(artifact_store, project, "ArtifactGenerationAfter");
+ cbm_store_close(artifact_store);
+ }
+ cbm_pipeline_incremental_test_reset_faults();
th_rmtree(tmp);
- ASSERT_EQ(run_rc, 0);
- ASSERT_EQ(fresh_nodes, 1);
- ASSERT_EQ(generated_column_count, 1);
- ASSERT_FALSE(quarantined);
+ ASSERT_EQ(baseline_rc, 0);
+ ASSERT_EQ(baseline_observer.rename_calls, 1);
+ ASSERT_EQ(baseline_observer.exports_before_publish, 0);
+ ASSERT_EQ(baseline_observer.export_count, 1);
+ ASSERT_EQ(reindex_rc, 0);
+ ASSERT_EQ(reindex_route, CBM_INCREMENTAL_ROUTE_FORCED_FULL);
+ ASSERT_EQ(reindex_observer.rename_calls, 1);
+ ASSERT_EQ(reindex_observer.exports_before_publish, 0);
+ ASSERT_EQ(reindex_observer.export_count, 1);
+ /* The no-ops are proven on the live generation and publish nothing; the
+ * artifact is still refreshed, from that already published database. */
+ ASSERT_EQ(explicit_rc, 0);
+ ASSERT_EQ(explicit_route, CBM_INCREMENTAL_ROUTE_NOOP);
+ ASSERT_EQ(explicit_observer.rename_calls, 0);
+ ASSERT_EQ(explicit_observer.export_count, 1);
+ ASSERT_EQ(unchanged_rc, 0);
+ ASSERT_EQ(unchanged_route, CBM_INCREMENTAL_ROUTE_NOOP);
+ ASSERT_EQ(observer.rename_calls, 0);
+ ASSERT_EQ(observer.export_count, 1);
+ ASSERT_TRUE(journal_ok);
+ ASSERT_STR_EQ(journal_mode, "delete");
+ ASSERT_EQ(live_before, 0);
+ ASSERT_EQ(live_after, 1);
+ ASSERT_EQ(import_rc, 0);
+ ASSERT_EQ(artifact_before, 0);
+ ASSERT_EQ(artifact_after, 1);
PASS();
}
- #endif
- /* Re-indexing only a target whose qualified name survives a callable-to-value
- * edit must not restore the unchanged caller's stale inbound CALL_REFERENCE
- * snapshot. A fresh full index of the edited sources is the convergence oracle:
- * the now-non-callable identifier remains a USAGE, never a call relationship. */
- TEST(pipeline_incremental_changed_target_invalidates_stale_inbound_call_reference) {
+ /* Cancellation observed after predump must be an explicit preserved abort,
+ * never a false success. The retry then publishes the edited generation. */
+ TEST(pipeline_full_cancel_after_predump_preserves_previous_generation) {
char tmp[256];
- snprintf(tmp, sizeof(tmp), "/tmp/cbm_ref_stale_inbound_XXXXXX");
- if (!cbm_mkdtemp(tmp)) {
- FAIL("tmpdir");
- }
-
- static const char callable_target_source[] = "export function staleSemanticTarget(): void {}\n";
- static const char value_target_source[] = "export const staleSemanticTarget = 42;\n";
- static const char caller_source[] =
- "import { staleSemanticTarget } from './target';\n"
- "function acceptStaleSemanticReference(callback: () => void): void {}\n"
- "export function staleSemanticCaller(): void {\n"
- " acceptStaleSemanticReference(staleSemanticTarget);\n"
- "}\n";
-
- write_temp_file(tmp, "target.ts", callable_target_source);
- write_temp_file(tmp, "caller.ts", caller_source);
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_publish_cancel_XXXXXX");
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
+ static const char before_source[] = "def BeforeCancel():\n return 1\n";
+ static const char after_source[] = "def AfterCancel():\n return 2\n# changed\n";
+ write_temp_file(tmp, "generation.py", before_source);
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/generation.db", tmp);
- char incremental_db[512];
- snprintf(incremental_db, sizeof(incremental_db), "%s/stale-inbound-incremental.db", tmp);
- cbm_pipeline_t *baseline = cbm_pipeline_new(tmp, incremental_db, CBM_MODE_FULL);
+ cbm_pipeline_incremental_test_reset_faults();
+ cbm_pipeline_t *baseline = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
ASSERT_NOT_NULL(baseline);
ASSERT_EQ(cbm_pipeline_run(baseline), 0);
- const char *baseline_project = cbm_pipeline_project_name(baseline);
- cbm_store_t *baseline_store = cbm_store_open_path(incremental_db);
- ASSERT_NOT_NULL(baseline_store);
- ASSERT_EQ(named_edge_count(baseline_store, baseline_project, "CALL_REFERENCE",
- "staleSemanticCaller", "staleSemanticTarget"),
- 1);
- ASSERT_EQ(named_edge_count(baseline_store, baseline_project, "USAGE", "staleSemanticCaller",
- "staleSemanticTarget"),
- 0);
- ASSERT_EQ(named_edge_count(baseline_store, baseline_project, "CALLS", "staleSemanticCaller",
- "staleSemanticTarget"),
- 0);
- cbm_store_close(baseline_store);
+ char project[256];
+ snprintf(project, sizeof(project), "%s", cbm_pipeline_project_name(baseline));
cbm_pipeline_free(baseline);
- write_temp_file(tmp, "target.ts", value_target_source);
-
- cbm_pipeline_t *incremental = cbm_pipeline_new(tmp, incremental_db, CBM_MODE_FULL);
- ASSERT_NOT_NULL(incremental);
- ASSERT_EQ(cbm_pipeline_run(incremental), 0);
- const char *incremental_project = cbm_pipeline_project_name(incremental);
- cbm_store_t *incremental_store = cbm_store_open_path(incremental_db);
- ASSERT_NOT_NULL(incremental_store);
- int incremental_reference =
- named_edge_count(incremental_store, incremental_project, "CALL_REFERENCE",
- "staleSemanticCaller", "staleSemanticTarget");
- int incremental_usage = named_edge_count(incremental_store, incremental_project, "USAGE",
- "staleSemanticCaller", "staleSemanticTarget");
- int incremental_calls = named_edge_count(incremental_store, incremental_project, "CALLS",
- "staleSemanticCaller", "staleSemanticTarget");
- cbm_store_close(incremental_store);
- cbm_pipeline_free(incremental);
+ write_temp_file(tmp, "generation.py", after_source);
+ cbm_pipeline_incremental_test_cancel_after_predump_once();
+ cbm_pipeline_t *cancelled = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(cancelled);
+ int cancelled_rc = cbm_pipeline_run(cancelled);
+ cbm_incremental_route_t cancelled_route = cbm_pipeline_incremental_test_last_route();
+ cbm_pipeline_free(cancelled);
+ int cancelled_before = -1;
+ int cancelled_after = -1;
+ observe_named_generation(db_path, project, "BeforeCancel", "AfterCancel", &cancelled_before,
+ &cancelled_after);
- char full_db[512];
- snprintf(full_db, sizeof(full_db), "%s/stale-inbound-full.db", tmp);
- cbm_pipeline_t *full = cbm_pipeline_new(tmp, full_db, CBM_MODE_FULL);
- ASSERT_NOT_NULL(full);
- ASSERT_EQ(cbm_pipeline_run(full), 0);
- const char *full_project = cbm_pipeline_project_name(full);
- cbm_store_t *full_store = cbm_store_open_path(full_db);
- ASSERT_NOT_NULL(full_store);
- int full_reference = named_edge_count(full_store, full_project, "CALL_REFERENCE",
- "staleSemanticCaller", "staleSemanticTarget");
- int full_usage = named_edge_count(full_store, full_project, "USAGE", "staleSemanticCaller",
- "staleSemanticTarget");
- int full_calls = named_edge_count(full_store, full_project, "CALLS", "staleSemanticCaller",
- "staleSemanticTarget");
- cbm_store_close(full_store);
- cbm_pipeline_free(full);
+ cbm_pipeline_incremental_test_reset_faults();
+ cbm_pipeline_t *retry = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(retry);
+ int retry_rc = cbm_pipeline_run(retry);
+ cbm_incremental_route_t retry_route = cbm_pipeline_incremental_test_last_route();
+ cbm_pipeline_free(retry);
+ int retry_before = -1;
+ int retry_after = -1;
+ observe_named_generation(db_path, project, "BeforeCancel", "AfterCancel", &retry_before,
+ &retry_after);
+ int stage_count = count_generation_stage_artifacts(tmp, "generation.db");
+ cbm_pipeline_incremental_test_reset_faults();
th_rmtree(tmp);
- ASSERT_EQ(full_reference, 0);
- ASSERT_EQ(full_usage, 1);
- ASSERT_EQ(full_calls, 0);
- ASSERT_EQ(incremental_reference, full_reference);
- ASSERT_EQ(incremental_usage, full_usage);
- ASSERT_EQ(incremental_calls, full_calls);
- PASS();
- }
-
- /* Registry construction in incremental-parallel mode can materialize serial
- * resource nodes after extraction establishes the workers' shared-ID
- * watermark. A subsequent worker-created synthetic node must not reuse that
- * ID: duplicate temporary IDs make dump-time edge remapping point CONFIGURES
+ ASSERT_EQ(cancelled_rc, CBM_PIPELINE_ABORT_PRESERVE_DB);
+ ASSERT_EQ(cancelled_route, CBM_INCREMENTAL_ROUTE_FORCED_FULL);
+ ASSERT_EQ(cancelled_before, 1);
+ ASSERT_EQ(cancelled_after, 0);
+ ASSERT_EQ(retry_rc, 0);
+ ASSERT_EQ(retry_route, CBM_INCREMENTAL_ROUTE_FORCED_FULL);
+ ASSERT_EQ(retry_before, 0);
+ ASSERT_EQ(retry_after, 1);
+ ASSERT_EQ(stage_count, 0);
+ PASS();
+ }
+
+ /* Cancellation can arrive while the destination DB is being quiesced. It must
+ * still be observed at the final commit boundary, before the atomic rename. */
+ TEST(pipeline_full_cancel_after_destination_prepare_preserves_previous_generation) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_publish_late_cancel_XXXXXX");
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
+ write_temp_file(tmp, "generation.py", "def BeforeLateCancel():\n return 1\n");
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/generation.db", tmp);
+
+ cbm_pipeline_incremental_test_reset_faults();
+ cbm_pipeline_t *baseline = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(baseline);
+ ASSERT_EQ(cbm_pipeline_run(baseline), 0);
+ char project[256];
+ snprintf(project, sizeof(project), "%s", cbm_pipeline_project_name(baseline));
+ cbm_pipeline_free(baseline);
+
+ write_temp_file(tmp, "generation.py", "def AfterLateCancel():\n return 2\n# changed\n");
+ cbm_pipeline_incremental_test_cancel_after_destination_prepare_once();
+ cbm_pipeline_t *cancelled = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(cancelled);
+ int cancelled_rc = cbm_pipeline_run(cancelled);
+ cbm_pipeline_free(cancelled);
+
+ int before_count = -1;
+ int after_count = -1;
+ observe_named_generation(db_path, project, "BeforeLateCancel", "AfterLateCancel", &before_count,
+ &after_count);
+ int stage_count = count_generation_stage_artifacts(tmp, "generation.db");
+ cbm_pipeline_incremental_test_reset_faults();
+ th_rmtree(tmp);
+
+ ASSERT_EQ(cancelled_rc, CBM_PIPELINE_ABORT_PRESERVE_DB);
+ ASSERT_EQ(before_count, 1);
+ ASSERT_EQ(after_count, 0);
+ ASSERT_EQ(stage_count, 0);
+ PASS();
+ }
+
+ /* A failure after graph serialization but before metadata/publication must
+ * discard only the staged generation. The final path remains the complete old
+ * graph and a clean retry converges. */
+ TEST(pipeline_full_persist_failure_after_stage_dump_preserves_previous_generation) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_publish_full_fail_XXXXXX");
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
+ write_temp_file(tmp, "generation.py", "def BeforeFullPersist():\n return 1\n");
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/generation.db", tmp);
+
+ cbm_pipeline_incremental_test_reset_faults();
+ cbm_pipeline_t *baseline = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(baseline);
+ ASSERT_EQ(cbm_pipeline_run(baseline), 0);
+ char project[256];
+ snprintf(project, sizeof(project), "%s", cbm_pipeline_project_name(baseline));
+ cbm_pipeline_free(baseline);
+
+ write_temp_file(tmp, "generation.py",
+ "def AfterFullPersist():\n return 2\n# changed generation\n");
+ cbm_pipeline_incremental_test_fail_after_stage_dump_once();
+ cbm_pipeline_t *faulted = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(faulted);
+ int faulted_rc = cbm_pipeline_run(faulted);
+ cbm_pipeline_free(faulted);
+ int faulted_before = -1;
+ int faulted_after = -1;
+ observe_named_generation(db_path, project, "BeforeFullPersist", "AfterFullPersist",
+ &faulted_before, &faulted_after);
+ int faulted_stage_count = count_generation_stage_artifacts(tmp, "generation.db");
+
+ cbm_pipeline_incremental_test_reset_faults();
+ cbm_pipeline_t *retry = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(retry);
+ int retry_rc = cbm_pipeline_run(retry);
+ cbm_pipeline_free(retry);
+ int retry_before = -1;
+ int retry_after = -1;
+ observe_named_generation(db_path, project, "BeforeFullPersist", "AfterFullPersist",
+ &retry_before, &retry_after);
+ int retry_stage_count = count_generation_stage_artifacts(tmp, "generation.db");
+ cbm_pipeline_incremental_test_reset_faults();
+ th_rmtree(tmp);
+
+ ASSERT_EQ(faulted_rc, CBM_PIPELINE_PERSIST_FAILED);
+ ASSERT_EQ(faulted_before, 1);
+ ASSERT_EQ(faulted_after, 0);
+ ASSERT_EQ(faulted_stage_count, 0);
+ ASSERT_EQ(retry_rc, 0);
+ ASSERT_EQ(retry_before, 0);
+ ASSERT_EQ(retry_after, 1);
+ ASSERT_EQ(retry_stage_count, 0);
+ PASS();
+ }
+
+ /* Keep the legacy partial route test-only so its fail-closed behavior remains
+ * covered even though production semantic changes now force a full rebuild. */
+ TEST(pipeline_incremental_persist_failure_preserves_previous_generation_and_retries) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_publish_incr_fail_XXXXXX");
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
+ write_temp_file(tmp, "generation.py", "def BeforeIncrementalPersist():\n return 1\n");
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/generation.db", tmp);
+
+ cbm_pipeline_incremental_test_reset_faults();
+ cbm_pipeline_t *baseline = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(baseline);
+ ASSERT_EQ(cbm_pipeline_run(baseline), 0);
+ char project[256];
+ snprintf(project, sizeof(project), "%s", cbm_pipeline_project_name(baseline));
+ cbm_pipeline_free(baseline);
+
+ write_temp_file(tmp, "generation.py",
+ "def AfterIncrementalPersist():\n return 2\n# changed generation\n");
+ cbm_pipeline_incremental_test_force_legacy_partial_once();
+ cbm_pipeline_incremental_test_fail_after_stage_dump_once();
+ cbm_pipeline_t *faulted = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(faulted);
+ int faulted_rc = cbm_pipeline_run(faulted);
+ cbm_incremental_route_t faulted_route = cbm_pipeline_incremental_test_last_route();
+ cbm_pipeline_free(faulted);
+ int faulted_before = -1;
+ int faulted_after = -1;
+ observe_named_generation(db_path, project, "BeforeIncrementalPersist",
+ "AfterIncrementalPersist", &faulted_before, &faulted_after);
+ int faulted_stage_count = count_generation_stage_artifacts(tmp, "generation.db");
+
+ cbm_pipeline_incremental_test_reset_faults();
+ cbm_pipeline_t *retry = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(retry);
+ int retry_rc = cbm_pipeline_run(retry);
+ cbm_incremental_route_t retry_route = cbm_pipeline_incremental_test_last_route();
+ cbm_pipeline_free(retry);
+ int retry_before = -1;
+ int retry_after = -1;
+ observe_named_generation(db_path, project, "BeforeIncrementalPersist",
+ "AfterIncrementalPersist", &retry_before, &retry_after);
+ int retry_stage_count = count_generation_stage_artifacts(tmp, "generation.db");
+ cbm_pipeline_incremental_test_reset_faults();
+ th_rmtree(tmp);
+
+ ASSERT_EQ(faulted_rc, CBM_PIPELINE_PERSIST_FAILED);
+ ASSERT_EQ(faulted_route, CBM_INCREMENTAL_ROUTE_LEGACY_PARTIAL);
+ ASSERT_EQ(faulted_before, 1);
+ ASSERT_EQ(faulted_after, 0);
+ ASSERT_EQ(faulted_stage_count, 0);
+ ASSERT_EQ(retry_rc, 0);
+ ASSERT_EQ(retry_route, CBM_INCREMENTAL_ROUTE_FORCED_FULL);
+ ASSERT_EQ(retry_before, 0);
+ ASSERT_EQ(retry_after, 1);
+ ASSERT_EQ(retry_stage_count, 0);
+ PASS();
+ }
+
+ TEST(pipeline_incremental_successful_publication_preserves_adr) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_publish_incr_adr_XXXXXX");
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
+ write_temp_file(tmp, "generation.py", "def BeforeAdrIncremental():\n return 1\n");
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/generation.db", tmp);
+
+ cbm_pipeline_incremental_test_reset_faults();
+ cbm_pipeline_t *baseline = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(baseline);
+ ASSERT_EQ(cbm_pipeline_run(baseline), 0);
+ char project[256];
+ snprintf(project, sizeof(project), "%s", cbm_pipeline_project_name(baseline));
+ cbm_pipeline_free(baseline);
+ static const char adr_text[] = "# Decision\nPreserve across publication.";
+ cbm_store_t *adr_store = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(adr_store);
+ ASSERT_EQ(cbm_store_adr_store(adr_store, project, adr_text), CBM_STORE_OK);
+ cbm_store_close(adr_store);
+
+ write_temp_file(tmp, "generation.py",
+ "def AfterAdrIncremental():\n return 2\n# changed generation\n");
+ cbm_pipeline_incremental_test_force_legacy_partial_once();
+ cbm_pipeline_t *incremental = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(incremental);
+ int run_rc = cbm_pipeline_run(incremental);
+ cbm_incremental_route_t route = cbm_pipeline_incremental_test_last_route();
+ cbm_pipeline_free(incremental);
+ cbm_store_t *published = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(published);
+ cbm_adr_t adr = {0};
+ int adr_rc = cbm_store_adr_get(published, project, &adr);
+ bool adr_matches = adr_rc == CBM_STORE_OK && adr.content && strcmp(adr.content, adr_text) == 0;
+ cbm_store_adr_free(&adr);
+ cbm_store_close(published);
+ cbm_pipeline_incremental_test_reset_faults();
+ th_rmtree(tmp);
+
+ ASSERT_EQ(run_rc, 0);
+ ASSERT_EQ(route, CBM_INCREMENTAL_ROUTE_LEGACY_PARTIAL);
+ ASSERT_TRUE(adr_matches);
+ PASS();
+ }
+
+ /* A forced-full rebuild must never erase an ADR merely because the old
+ * generation could not be read completely. The capture is part of the
+ * publication transaction: failure preserves both graph and ADR. */
+ TEST(pipeline_full_adr_capture_failure_preserves_previous_generation) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_publish_adr_capture_XXXXXX");
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
+ write_temp_file(tmp, "generation.py", "def BeforeAdrCapture():\n return 1\n");
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/generation.db", tmp);
+
+ cbm_pipeline_incremental_test_reset_faults();
+ cbm_pipeline_t *baseline = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(baseline);
+ ASSERT_EQ(cbm_pipeline_run(baseline), 0);
+ char project[256];
+ snprintf(project, sizeof(project), "%s", cbm_pipeline_project_name(baseline));
+ cbm_pipeline_free(baseline);
+
+ static const char adr_text[] = "# Decision\nADR capture is fail-closed.";
+ cbm_store_t *adr_store = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(adr_store);
+ ASSERT_EQ(cbm_store_adr_store(adr_store, project, adr_text), CBM_STORE_OK);
+ cbm_store_close(adr_store);
+
+ write_temp_file(tmp, "generation.py", "def AfterAdrCapture():\n return 2\n");
+ cbm_pipeline_incremental_test_fail_adr_capture_once();
+ cbm_pipeline_t *faulted = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(faulted);
+ int faulted_rc = cbm_pipeline_run(faulted);
+ cbm_pipeline_free(faulted);
+
+ int faulted_before = -1;
+ int faulted_after = -1;
+ observe_named_generation(db_path, project, "BeforeAdrCapture", "AfterAdrCapture",
+ &faulted_before, &faulted_after);
+ cbm_store_t *preserved = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(preserved);
+ cbm_adr_t preserved_adr = {0};
+ int preserved_adr_rc = cbm_store_adr_get(preserved, project, &preserved_adr);
+ bool preserved_adr_matches = preserved_adr_rc == CBM_STORE_OK && preserved_adr.content &&
+ strcmp(preserved_adr.content, adr_text) == 0;
+ cbm_store_adr_free(&preserved_adr);
+ cbm_store_close(preserved);
+
+ cbm_pipeline_incremental_test_reset_faults();
+ cbm_pipeline_t *retry = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(retry);
+ int retry_rc = cbm_pipeline_run(retry);
+ cbm_pipeline_free(retry);
+ int retry_before = -1;
+ int retry_after = -1;
+ observe_named_generation(db_path, project, "BeforeAdrCapture", "AfterAdrCapture", &retry_before,
+ &retry_after);
+ cbm_store_t *published = cbm_store_open_path(db_path);
+ ASSERT_NOT_NULL(published);
+ cbm_adr_t published_adr = {0};
+ int published_adr_rc = cbm_store_adr_get(published, project, &published_adr);
+ bool published_adr_matches = published_adr_rc == CBM_STORE_OK && published_adr.content &&
+ strcmp(published_adr.content, adr_text) == 0;
+ cbm_store_adr_free(&published_adr);
+ cbm_store_close(published);
+ cbm_pipeline_incremental_test_reset_faults();
+ th_rmtree(tmp);
+
+ ASSERT_EQ(faulted_rc, CBM_PIPELINE_ABORT_PRESERVE_DB);
+ ASSERT_EQ(faulted_before, 1);
+ ASSERT_EQ(faulted_after, 0);
+ ASSERT_TRUE(preserved_adr_matches);
+ ASSERT_EQ(retry_rc, 0);
+ ASSERT_EQ(retry_before, 0);
+ ASSERT_EQ(retry_after, 1);
+ ASSERT_TRUE(published_adr_matches);
+ PASS();
+ }
+
+ /* An exact semantic manifest must fail closed when its repository root cannot
+ * be traversed. A regular file is a deterministic cross-platform opendir
+ * failure that does not depend on process permissions. */
+ TEST(pipeline_semantic_manifest_rejects_non_directory_root) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_manifest_not_dir_XXXXXX");
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
+ write_temp_file(tmp, "not-a-directory", "manifest root sentinel\n");
+ char root_path[512];
+ snprintf(root_path, sizeof(root_path), "%s/not-a-directory", tmp);
+
+ cbm_file_hash_t *manifest = NULL;
+ int manifest_count = -1;
- int rc = cbm_pipeline_build_semantic_manifest("manifest-fail-closed", root_path, NULL, 0, NULL,
- 0, NULL, NULL, &manifest, &manifest_count);
++ int rc = cbm_pipeline_build_semantic_manifest(NULL, "manifest-fail-closed", root_path, NULL, 0,
++ NULL, 0, NULL, NULL, &manifest, &manifest_count);
+ cbm_pipeline_free_semantic_manifest(manifest, manifest_count > 0 ? manifest_count : 0);
+ th_rmtree(tmp);
+
+ ASSERT_TRUE(rc != 0);
+ ASSERT_EQ(manifest_count, 0);
+ PASS();
+ }
+
+ /* A fully validated staged graph must be able to recover from a definitely
+ * non-SQLite destination without deleting evidence or overwriting an earlier
+ * quarantine. The replacement happens only after the corrupt bytes are moved. */
+ TEST(pipeline_full_reindex_quarantines_corrupt_destination_without_overwrite) {
+ static const char corrupt_bytes[] = "not a sqlite database\n";
+ static const char previous_backup[] = "older quarantine\n";
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_publish_corrupt_XXXXXX");
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
+ write_temp_file(tmp, "fresh.py", "def FreshAfterCorruption():\n return 1\n");
+ write_temp_file(tmp, "generation.db", corrupt_bytes);
+ write_temp_file(tmp, "generation.db.corrupt", previous_backup);
+
+ char db_path[512];
+ char old_backup_path[512];
+ char new_backup_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/generation.db", tmp);
+ snprintf(old_backup_path, sizeof(old_backup_path), "%s.corrupt", db_path);
+ snprintf(new_backup_path, sizeof(new_backup_path), "%s.corrupt.1", db_path);
+
+ cbm_pipeline_t *pipeline = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(pipeline);
+ int run_rc = cbm_pipeline_run(pipeline);
+ char project[256];
+ snprintf(project, sizeof(project), "%s", cbm_pipeline_project_name(pipeline));
+ cbm_pipeline_free(pipeline);
+
+ char old_observed[64] = {0};
+ char new_observed[64] = {0};
+ FILE *old_backup_file = cbm_fopen(old_backup_path, "rb");
+ FILE *new_backup_file = cbm_fopen(new_backup_path, "rb");
+ size_t old_size =
+ old_backup_file ? fread(old_observed, 1, sizeof(old_observed) - 1, old_backup_file) : 0;
+ size_t new_size =
+ new_backup_file ? fread(new_observed, 1, sizeof(new_observed) - 1, new_backup_file) : 0;
+ if (old_backup_file) {
+ fclose(old_backup_file);
+ }
+ if (new_backup_file) {
+ fclose(new_backup_file);
+ }
+
+ int fresh_nodes = -1;
+ cbm_store_t *store = cbm_store_open_path_query(db_path);
+ if (store) {
+ fresh_nodes = named_node_count(store, project, "FreshAfterCorruption");
+ cbm_store_close(store);
+ }
+ th_rmtree(tmp);
+
+ ASSERT_EQ(run_rc, 0);
+ ASSERT_EQ((int)old_size, (int)strlen(previous_backup));
+ ASSERT_STR_EQ(old_observed, previous_backup);
+ ASSERT_EQ((int)new_size, (int)strlen(corrupt_bytes));
+ ASSERT_STR_EQ(new_observed, corrupt_bytes);
+ ASSERT_EQ(fresh_nodes, 1);
+ PASS();
+ }
+
+ /* Readable legacy graphs are replaceable generations, not corruption. The
+ * publication boundary must neither run current-schema migration against the
+ * old edges table nor require an ADR table that did not exist yet. */
+ TEST(pipeline_full_reindex_replaces_legacy_schema_without_quarantine) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_publish_legacy_XXXXXX");
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
+ write_temp_file(tmp, "fresh.py", "def FreshAfterLegacy():\n return 1\n");
+
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/generation.db", tmp);
+ sqlite3 *legacy = NULL;
+ ASSERT_EQ(sqlite3_open_v2(db_path, &legacy, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL),
+ SQLITE_OK);
+ const char *legacy_schema =
+ "CREATE TABLE projects(name TEXT PRIMARY KEY,indexed_at TEXT NOT NULL,root_path TEXT NOT "
+ "NULL);"
+ "CREATE TABLE file_hashes(project TEXT NOT NULL,rel_path TEXT NOT NULL,sha256 TEXT NOT "
+ "NULL,mtime_ns INTEGER NOT NULL DEFAULT 0,size INTEGER NOT NULL DEFAULT 0,PRIMARY KEY "
+ "(project,rel_path));"
+ "CREATE TABLE nodes(id INTEGER PRIMARY KEY,project TEXT NOT NULL,label TEXT NOT NULL,name "
+ "TEXT NOT NULL,qualified_name TEXT NOT NULL,file_path TEXT,start_line INTEGER,end_line "
+ "INTEGER,properties TEXT DEFAULT '{}');"
+ "CREATE TABLE edges(id INTEGER PRIMARY KEY,project TEXT NOT NULL,source_id INTEGER NOT "
+ "NULL,target_id INTEGER NOT NULL,type TEXT NOT NULL,properties TEXT DEFAULT '{}',UNIQUE "
+ "(source_id,target_id,type));";
+ ASSERT_EQ(sqlite3_exec(legacy, legacy_schema, NULL, NULL, NULL), SQLITE_OK);
+ sqlite3_stmt *insert_project = NULL;
+ ASSERT_EQ(sqlite3_prepare_v2(
+ legacy, "INSERT INTO projects(name,indexed_at,root_path) VALUES(?1,'legacy',?2);",
+ -1, &insert_project, NULL),
+ SQLITE_OK);
+ const char *project_name = strrchr(tmp, '/');
+ project_name = project_name ? project_name + 1 : tmp;
+ ASSERT_EQ(sqlite3_bind_text(insert_project, 1, project_name, -1, SQLITE_TRANSIENT), SQLITE_OK);
+ ASSERT_EQ(sqlite3_bind_text(insert_project, 2, tmp, -1, SQLITE_TRANSIENT), SQLITE_OK);
+ ASSERT_EQ(sqlite3_step(insert_project), SQLITE_DONE);
+ sqlite3_finalize(insert_project);
+ sqlite3_close(legacy);
+
+ cbm_pipeline_t *pipeline = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(pipeline);
+ int run_rc = cbm_pipeline_run(pipeline);
+ char project[256];
+ snprintf(project, sizeof(project), "%s", cbm_pipeline_project_name(pipeline));
+ cbm_pipeline_free(pipeline);
+
+ int fresh_nodes = -1;
+ cbm_store_t *store = cbm_store_open_path_query(db_path);
+ if (store) {
+ fresh_nodes = named_node_count(store, project, "FreshAfterLegacy");
+ cbm_store_close(store);
+ }
+ int generated_column_count = 0;
+ sqlite3 *current = NULL;
+ sqlite3_stmt *column_query = NULL;
+ if (sqlite3_open_v2(db_path, &current, SQLITE_OPEN_READONLY, NULL) == SQLITE_OK &&
+ sqlite3_prepare_v2(current,
+ "SELECT count(*) FROM pragma_table_xinfo('edges') "
+ "WHERE name='local_name_gen';",
+ -1, &column_query, NULL) == SQLITE_OK &&
+ sqlite3_step(column_query) == SQLITE_ROW) {
+ generated_column_count = sqlite3_column_int(column_query, 0);
+ }
+ sqlite3_finalize(column_query);
+ if (current) {
+ sqlite3_close(current);
+ }
+ char quarantine_path[512];
+ snprintf(quarantine_path, sizeof(quarantine_path), "%s.corrupt", db_path);
+ cbm_path_info_t quarantine_info;
+ bool quarantined = cbm_path_info_utf8(quarantine_path, &quarantine_info) == 0;
+ th_rmtree(tmp);
+
+ ASSERT_EQ(run_rc, 0);
+ ASSERT_EQ(fresh_nodes, 1);
+ ASSERT_EQ(generated_column_count, 1);
+ ASSERT_FALSE(quarantined);
+ PASS();
+ }
+ #endif
+
+ /* Re-indexing only a target whose qualified name survives a callable-to-value
+ * edit must not restore the unchanged caller's stale inbound CALL_REFERENCE
+ * snapshot. A fresh full index of the edited sources is the convergence oracle:
+ * the now-non-callable identifier remains a USAGE, never a call relationship. */
+ TEST(pipeline_incremental_changed_target_invalidates_stale_inbound_call_reference) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_ref_stale_inbound_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ FAIL("tmpdir");
+ }
+
+ static const char callable_target_source[] = "export function staleSemanticTarget(): void {}\n";
+ static const char value_target_source[] = "export const staleSemanticTarget = 42;\n";
+ static const char caller_source[] =
+ "import { staleSemanticTarget } from './target';\n"
+ "function acceptStaleSemanticReference(callback: () => void): void {}\n"
+ "export function staleSemanticCaller(): void {\n"
+ " acceptStaleSemanticReference(staleSemanticTarget);\n"
+ "}\n";
+
+ write_temp_file(tmp, "target.ts", callable_target_source);
+ write_temp_file(tmp, "caller.ts", caller_source);
+
+ char incremental_db[512];
+ snprintf(incremental_db, sizeof(incremental_db), "%s/stale-inbound-incremental.db", tmp);
+ cbm_pipeline_t *baseline = cbm_pipeline_new(tmp, incremental_db, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(baseline);
+ ASSERT_EQ(cbm_pipeline_run(baseline), 0);
+ const char *baseline_project = cbm_pipeline_project_name(baseline);
+ cbm_store_t *baseline_store = cbm_store_open_path(incremental_db);
+ ASSERT_NOT_NULL(baseline_store);
+ ASSERT_EQ(named_edge_count(baseline_store, baseline_project, "CALL_REFERENCE",
+ "staleSemanticCaller", "staleSemanticTarget"),
+ 1);
+ ASSERT_EQ(named_edge_count(baseline_store, baseline_project, "USAGE", "staleSemanticCaller",
+ "staleSemanticTarget"),
+ 0);
+ ASSERT_EQ(named_edge_count(baseline_store, baseline_project, "CALLS", "staleSemanticCaller",
+ "staleSemanticTarget"),
+ 0);
+ cbm_store_close(baseline_store);
+ cbm_pipeline_free(baseline);
+
+ write_temp_file(tmp, "target.ts", value_target_source);
+
+ cbm_pipeline_t *incremental = cbm_pipeline_new(tmp, incremental_db, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(incremental);
+ ASSERT_EQ(cbm_pipeline_run(incremental), 0);
+ const char *incremental_project = cbm_pipeline_project_name(incremental);
+ cbm_store_t *incremental_store = cbm_store_open_path(incremental_db);
+ ASSERT_NOT_NULL(incremental_store);
+ int incremental_reference =
+ named_edge_count(incremental_store, incremental_project, "CALL_REFERENCE",
+ "staleSemanticCaller", "staleSemanticTarget");
+ int incremental_usage = named_edge_count(incremental_store, incremental_project, "USAGE",
+ "staleSemanticCaller", "staleSemanticTarget");
+ int incremental_calls = named_edge_count(incremental_store, incremental_project, "CALLS",
+ "staleSemanticCaller", "staleSemanticTarget");
+ cbm_store_close(incremental_store);
+ cbm_pipeline_free(incremental);
+
+ char full_db[512];
+ snprintf(full_db, sizeof(full_db), "%s/stale-inbound-full.db", tmp);
+ cbm_pipeline_t *full = cbm_pipeline_new(tmp, full_db, CBM_MODE_FULL);
+ ASSERT_NOT_NULL(full);
+ ASSERT_EQ(cbm_pipeline_run(full), 0);
+ const char *full_project = cbm_pipeline_project_name(full);
+ cbm_store_t *full_store = cbm_store_open_path(full_db);
+ ASSERT_NOT_NULL(full_store);
+ int full_reference = named_edge_count(full_store, full_project, "CALL_REFERENCE",
+ "staleSemanticCaller", "staleSemanticTarget");
+ int full_usage = named_edge_count(full_store, full_project, "USAGE", "staleSemanticCaller",
+ "staleSemanticTarget");
+ int full_calls = named_edge_count(full_store, full_project, "CALLS", "staleSemanticCaller",
+ "staleSemanticTarget");
+ cbm_store_close(full_store);
+ cbm_pipeline_free(full);
+ th_rmtree(tmp);
+
+ ASSERT_EQ(full_reference, 0);
+ ASSERT_EQ(full_usage, 1);
+ ASSERT_EQ(full_calls, 0);
+ ASSERT_EQ(incremental_reference, full_reference);
+ ASSERT_EQ(incremental_usage, full_usage);
+ ASSERT_EQ(incremental_calls, full_calls);
+ PASS();
+ }
+
+ /* Registry construction in incremental-parallel mode can materialize serial
+ * resource nodes after extraction establishes the workers' shared-ID
+ * watermark. A subsequent worker-created synthetic node must not reuse that
+ * ID: duplicate temporary IDs make dump-time edge remapping point CONFIGURES
* or DECORATES at the wrong target. The second run changes all 51 files so the
* production incremental path crosses MIN_FILES_FOR_PARALLEL_INCR. */
TEST(pipeline_incremental_parallel_registry_nodes_advance_shared_ids) {