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.

feat(pipeline): delta-merge executor for the closure route

Martin Vogelbbd13335f524
diff --git a/Makefile.cbm b/Makefile.cbm
index e9c86b5f6..f3ca31eb7 100644
--- a/Makefile.cbm
+++ b/Makefile.cbm
@@ -303,6 +303,7 @@ GRAPH_BUFFER_SRCS = src/graph_buffer/graph_buffer.c
PIPELINE_SRCS = \
src/pipeline/fqn.c \
src/pipeline/lsp_surface.c \
+ src/pipeline/pipeline_delta.c \
src/pipeline/path_alias.c \
src/pipeline/registry.c \
src/pipeline/pipeline.c \
diff --git a/src/pipeline/pipeline.c b/src/pipeline/pipeline.c
index 82766dcd6..8116c0012 100644
--- a/src/pipeline/pipeline.c
+++ b/src/pipeline/pipeline.c
@@ -1675,6 +1675,17 @@ int cbm_pipeline_publish_generation(const cbm_pipeline_generation_t *generation)
return CBM_PIPELINE_ABORT_PRESERVE_DB;
}
+ return cbm_pipeline_publish_staged(stage_path, generation, true);
+}
+
+/* Complete and publish an already-materialized staging database: metadata
+ * writes, FTS policy, integrity, seal, then the shared finalize leg. Takes
+ * ownership of stage_path (frees it on every path). fts_wholesale selects
+ * the dump path's delete-all-and-rebuild; the delta path passes false
+ * because its patch step already wrote row-level FTS inserts for exactly
+ * the nodes it created. */
+int cbm_pipeline_publish_staged(char *stage_path, const cbm_pipeline_generation_t *generation,
+ bool fts_wholesale) {
cbm_store_t *store = cbm_store_open_path(stage_path);
if (!store) {
discard_generation_stage(stage_path);
@@ -1714,7 +1725,10 @@ int cbm_pipeline_publish_generation(const cbm_pipeline_generation_t *generation)
if (have_project_info) {
cbm_project_free_fields(&project_info);
}
- if (generation_rebuild_fts(store) != CBM_STORE_OK || !cbm_store_check_integrity(store)) {
+ if (fts_wholesale && generation_rebuild_fts(store) != CBM_STORE_OK) {
+ ok = false;
+ }
+ if (ok && !cbm_store_check_integrity(store)) {
ok = false;
}
if (ok && cbm_store_seal_for_atomic_publish(store) != CBM_STORE_OK) {
@@ -1732,6 +1746,10 @@ int cbm_pipeline_publish_generation(const cbm_pipeline_generation_t *generation)
return fin_rc;
}
+char *cbm_pipeline_create_staging_path(const char *final_path) {
+ return create_staging_path(final_path);
+}
+
void cbm_pipeline_discard_stage(const char *stage_path) {
discard_generation_stage(stage_path);
}
diff --git a/src/pipeline/pipeline_delta.c b/src/pipeline/pipeline_delta.c
new file mode 100644
index 000000000..6f8271f9b
--- /dev/null
+++ b/src/pipeline/pipeline_delta.c
@@ -0,0 +1,462 @@
+/*
+ * pipeline_delta.c — delta-repair staging primitives (closure route only).
+ *
+ * The dedicated incremental subsystem: instead of loading the whole previous
+ * graph into RAM and dumping a complete new generation, the closure route
+ * CLONES the live database (copy-on-write where the filesystem offers it),
+ * PATCHES exactly the repaired node/edge set into the clone inside one
+ * transaction, and publishes through the same sealed-staging finalize leg as
+ * the dump path. The general indexing pipeline is untouched; these
+ * primitives exist only for the closure orchestration in
+ * pipeline_incremental.c.
+ *
+ * Id discipline carries the design: node ids are AUTOINCREMENT and never
+ * reused, the small in-RAM gbuf is pre-seeded with PROXY nodes carrying
+ * their real database ids, and fresh nodes are numbered above the previous
+ * generation's MAX(id) — so "id > max_db_id" is the complete, marker-free
+ * definition of what the patch inserts, and every edge endpoint id is
+ * database-valid by construction.
+ *
+ * FTS policy: the nodes_fts table is contentless, so purged rows cannot be
+ * deleted individually on existing databases; their rowids can never alias
+ * a live node again (AUTOINCREMENT), so dead entries simply drop out of the
+ * rowid join at query time. The patch inserts rows for exactly the new
+ * nodes, via the same cbm_camel_split SQL function the wholesale rebuild
+ * uses.
+ */
+#include "foundation/constants.h"
+#include "pipeline/pipeline_internal.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "foundation/compat_fs.h"
+#include "foundation/log.h"
+#include "sqlite3.h"
+#include "store/store.h"
+
+enum { DELTA_IN_CHUNK = 200 };
+
+/* Assemble "?,?,...,?" for a chunked IN list. buf must hold 2*count. */
+static void delta_placeholders(char *buf, int count) {
+ int pos = 0;
+ for (int i = 0; i < count; i++) {
+ buf[pos++] = '?';
+ if (i + 1 < count) {
+ buf[pos++] = ',';
+ }
+ }
+ buf[pos] = '\0';
+}
+
+int cbm_delta_stage_clone(const char *final_db_path, char **out_stage_path) {
+ *out_stage_path = NULL;
+ char *stage = cbm_pipeline_create_staging_path(final_db_path);
+ if (!stage) {
+ return CBM_NOT_FOUND;
+ }
+ if (cbm_clone_or_copy_file(final_db_path, stage) != 0) {
+ cbm_pipeline_discard_stage(stage);
+ free(stage);
+ return CBM_NOT_FOUND;
+ }
+ *out_stage_path = stage;
+ return 0;
+}
+
+/* Snapshot inbound cross-file edges into the given files from OUTSIDE them,
+ * keyed by endpoint qualified names — the same semantics as the gbuf-based
+ * capture, expressed as one indexed query per chunk. Edge types a full
+ * reindex recomputes wholesale are excluded for the same reasons recorded
+ * there (restoring a stale copy could produce edges a full build would not). */
+static bool delta_edge_type_is_recomputed(const char *type) {
+ return type && (strcmp(type, "SIMILAR_TO") == 0 || strcmp(type, "SEMANTICALLY_RELATED") == 0 ||
+ strcmp(type, "FILE_CHANGES_WITH") == 0 || strcmp(type, "DATA_FLOWS") == 0);
+}
+
+int cbm_delta_snapshot_inbound(cbm_store_t *store, const char *project, const char *const *paths,
+ int path_count, cbm_delta_saved_edge_t **out, int *out_count) {
+ *out = NULL;
+ *out_count = 0;
+ if (path_count <= 0) {
+ return 0;
+ }
+ sqlite3 *db = cbm_store_get_db(store);
+ if (!db) {
+ return CBM_NOT_FOUND;
+ }
+ cbm_delta_saved_edge_t *items = NULL;
+ int count = 0;
+ int cap = 0;
+ for (int off = 0; off < path_count; off += DELTA_IN_CHUNK) {
+ int chunk = path_count - off;
+ if (chunk > DELTA_IN_CHUNK) {
+ chunk = DELTA_IN_CHUNK;
+ }
+ char ph[2 * DELTA_IN_CHUNK + 1];
+ delta_placeholders(ph, chunk);
+ char sql[CBM_SZ_4K];
+ int n = snprintf(sql, sizeof(sql),
+ "SELECT src.qualified_name, tgt.qualified_name, e.type, e.properties"
+ " FROM edges e"
+ " JOIN nodes tgt ON e.target_id = tgt.id"
+ " JOIN nodes src ON e.source_id = src.id"
+ " WHERE e.project = ?1 AND tgt.file_path IN (%s)"
+ " AND src.file_path NOT IN (%s)"
+ " AND src.file_path <> '' AND src.file_path IS NOT NULL",
+ ph, ph);
+ if (n < 0 || (size_t)n >= sizeof(sql)) {
+ cbm_delta_free_snapshot(items, count);
+ return CBM_NOT_FOUND;
+ }
+ sqlite3_stmt *stmt = NULL;
+ if (sqlite3_prepare_v2(db, sql, CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) {
+ cbm_delta_free_snapshot(items, count);
+ return CBM_NOT_FOUND;
+ }
+ sqlite3_bind_text(stmt, 1, project, CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ for (int i = 0; i < chunk; i++) {
+ sqlite3_bind_text(stmt, 2 + i, paths[off + i], CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ sqlite3_bind_text(stmt, 2 + chunk + i, paths[off + i], CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ }
+ int step_rc;
+ while ((step_rc = sqlite3_step(stmt)) == SQLITE_ROW) {
+ const char *type = (const char *)sqlite3_column_text(stmt, 2);
+ if (delta_edge_type_is_recomputed(type)) {
+ continue;
+ }
+ if (count >= cap) {
+ int ncap = cap ? cap * 2 : 64;
+ cbm_delta_saved_edge_t *grown = realloc(items, (size_t)ncap * sizeof(*items));
+ if (!grown) {
+ sqlite3_finalize(stmt);
+ cbm_delta_free_snapshot(items, count);
+ return CBM_NOT_FOUND;
+ }
+ items = grown;
+ cap = ncap;
+ }
+ const char *sq = (const char *)sqlite3_column_text(stmt, 0);
+ const char *tq = (const char *)sqlite3_column_text(stmt, 1);
+ const char *props = (const char *)sqlite3_column_text(stmt, 3);
+ cbm_delta_saved_edge_t *e = &items[count];
+ e->source_qn = strdup(sq ? sq : "");
+ e->target_qn = strdup(tq ? tq : "");
+ e->type = strdup(type ? type : "");
+ e->props = strdup(props ? props : "{}");
+ if (!e->source_qn || !e->target_qn || !e->type || !e->props) {
+ free(e->source_qn);
+ free(e->target_qn);
+ free(e->type);
+ free(e->props);
+ sqlite3_finalize(stmt);
+ cbm_delta_free_snapshot(items, count);
+ return CBM_NOT_FOUND;
+ }
+ count++;
+ }
+ sqlite3_finalize(stmt);
+ if (step_rc != SQLITE_DONE) {
+ cbm_delta_free_snapshot(items, count);
+ return CBM_NOT_FOUND;
+ }
+ }
+ *out = items;
+ *out_count = count;
+ return 0;
+}
+
+void cbm_delta_free_snapshot(cbm_delta_saved_edge_t *items, int count) {
+ if (!items) {
+ return;
+ }
+ for (int i = 0; i < count; i++) {
+ free(items[i].source_qn);
+ free(items[i].target_qn);
+ free(items[i].type);
+ free(items[i].props);
+ }
+ free(items);
+}
+
+int cbm_delta_purge(cbm_store_t *store, const char *project, const char *const *paths,
+ int path_count) {
+ if (path_count <= 0) {
+ return 0;
+ }
+ sqlite3 *db = cbm_store_get_db(store);
+ if (!db) {
+ return CBM_NOT_FOUND;
+ }
+ for (int off = 0; off < path_count; off += DELTA_IN_CHUNK) {
+ int chunk = path_count - off;
+ if (chunk > DELTA_IN_CHUNK) {
+ chunk = DELTA_IN_CHUNK;
+ }
+ char ph[2 * DELTA_IN_CHUNK + 1];
+ delta_placeholders(ph, chunk);
+ char sql[CBM_SZ_1K];
+ int n = snprintf(sql, sizeof(sql),
+ "DELETE FROM nodes WHERE project = ?1 AND file_path IN (%s)", ph);
+ if (n < 0 || (size_t)n >= sizeof(sql)) {
+ return CBM_NOT_FOUND;
+ }
+ sqlite3_stmt *stmt = NULL;
+ if (sqlite3_prepare_v2(db, sql, CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) {
+ return CBM_NOT_FOUND;
+ }
+ sqlite3_bind_text(stmt, 1, project, CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ for (int i = 0; i < chunk; i++) {
+ sqlite3_bind_text(stmt, 2 + i, paths[off + i], CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ }
+ int rc = sqlite3_step(stmt);
+ sqlite3_finalize(stmt);
+ if (rc != SQLITE_DONE) {
+ return CBM_NOT_FOUND;
+ }
+ }
+ return 0;
+}
+
+/* Labels whose nodes resolution or the restricted post-passes may reference
+ * as edge endpoints. Everything else stays on disk; an unexpected reference
+ * to an unseeded QN surfaces as a fresh node whose INSERT then violates the
+ * UNIQUE(project, qualified_name) constraint, failing the patch — and the
+ * orchestration's fallback to a full rebuild self-heals. Fail-closed, never
+ * silently wrong. */
+static const char *const DELTA_PRESEED_LABELS[] = {
+ "Function", "Method", "Class", "Struct", "Interface", "Enum", "Type",
+ "Trait", "Protocol", "Variable", "Field", "File", "Module", "Package",
+ "Folder", "Project", "EnvVar", "Dependency", "Route",
+};
+
+int64_t cbm_delta_preseed(cbm_store_t *store, const char *project, cbm_gbuf_t *gbuf) {
+ sqlite3 *db = cbm_store_get_db(store);
+ if (!db) {
+ return -1;
+ }
+ int64_t max_id = 0;
+ {
+ sqlite3_stmt *stmt = NULL;
+ if (sqlite3_prepare_v2(db, "SELECT COALESCE(MAX(id), 0) FROM nodes WHERE project = ?1",
+ CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) {
+ return -1;
+ }
+ sqlite3_bind_text(stmt, 1, project, CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ if (sqlite3_step(stmt) == SQLITE_ROW) {
+ max_id = sqlite3_column_int64(stmt, 0);
+ }
+ sqlite3_finalize(stmt);
+ }
+
+ enum { PRESEED_LABEL_COUNT = sizeof(DELTA_PRESEED_LABELS) / sizeof(DELTA_PRESEED_LABELS[0]) };
+ char ph[2 * PRESEED_LABEL_COUNT + 1];
+ delta_placeholders(ph, PRESEED_LABEL_COUNT);
+ char sql[CBM_SZ_1K];
+ int n = snprintf(sql, sizeof(sql),
+ "SELECT id, label, name, qualified_name, file_path FROM nodes"
+ " WHERE project = ?1 AND label IN (%s) ORDER BY id",
+ ph);
+ if (n < 0 || (size_t)n >= sizeof(sql)) {
+ return -1;
+ }
+ sqlite3_stmt *stmt = NULL;
+ if (sqlite3_prepare_v2(db, sql, CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) {
+ return -1;
+ }
+ sqlite3_bind_text(stmt, 1, project, CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ for (int i = 0; i < PRESEED_LABEL_COUNT; i++) {
+ sqlite3_bind_text(stmt, 2 + i, DELTA_PRESEED_LABELS[i], CBM_NOT_FOUND, SQLITE_STATIC);
+ }
+ int step_rc;
+ int64_t seeded = 0;
+ while ((step_rc = sqlite3_step(stmt)) == SQLITE_ROW) {
+ int64_t id = sqlite3_column_int64(stmt, 0);
+ const char *label = (const char *)sqlite3_column_text(stmt, 1);
+ const char *name = (const char *)sqlite3_column_text(stmt, 2);
+ const char *qn = (const char *)sqlite3_column_text(stmt, 3);
+ const char *fp = (const char *)sqlite3_column_text(stmt, 4);
+ /* Pin the gbuf id to the database id: proxies ARE their rows. */
+ cbm_gbuf_set_next_id(gbuf, id);
+ int64_t got = cbm_gbuf_upsert_node(gbuf, label, name, qn, fp ? fp : "", 0, 0, "{}");
+ if (got != id) {
+ /* A QN collision inside the preseed set would silently split
+ * identity between RAM and disk; the run cannot be trusted. */
+ sqlite3_finalize(stmt);
+ cbm_log_error("delta.preseed_id_mismatch", "qn", qn ? qn : "");
+ return -1;
+ }
+ seeded++;
+ }
+ sqlite3_finalize(stmt);
+ if (step_rc != SQLITE_DONE) {
+ return -1;
+ }
+ cbm_gbuf_set_next_id(gbuf, max_id + 1);
+ char seeded_buf[32];
+ snprintf(seeded_buf, sizeof(seeded_buf), "%lld", (long long)seeded);
+ cbm_log_info("delta.preseed", "proxies", seeded_buf);
+ return max_id;
+}
+
+/* Patch visitor state: inserts everything above the id watermark. */
+typedef struct {
+ sqlite3 *db;
+ const char *project;
+ int64_t max_db_id;
+ sqlite3_stmt *node_stmt;
+ sqlite3_stmt *edge_stmt;
+ bool failed;
+ int64_t nodes;
+ int64_t edges;
+} delta_patch_ctx_t;
+
+static void delta_patch_node(const cbm_gbuf_node_t *node, void *userdata) {
+ delta_patch_ctx_t *ctx = (delta_patch_ctx_t *)userdata;
+ if (ctx->failed || node->id <= ctx->max_db_id) {
+ return;
+ }
+ sqlite3_reset(ctx->node_stmt);
+ sqlite3_bind_int64(ctx->node_stmt, 1, node->id);
+ sqlite3_bind_text(ctx->node_stmt, 2, ctx->project, CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ sqlite3_bind_text(ctx->node_stmt, 3, node->label, CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ sqlite3_bind_text(ctx->node_stmt, 4, node->name, CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ sqlite3_bind_text(ctx->node_stmt, 5, node->qualified_name, CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ sqlite3_bind_text(ctx->node_stmt, 6, node->file_path ? node->file_path : "", CBM_NOT_FOUND,
+ SQLITE_TRANSIENT);
+ sqlite3_bind_int(ctx->node_stmt, 7, node->start_line);
+ sqlite3_bind_int(ctx->node_stmt, 8, node->end_line);
+ sqlite3_bind_text(ctx->node_stmt, 9, node->properties_json ? node->properties_json : "{}",
+ CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ if (sqlite3_step(ctx->node_stmt) != SQLITE_DONE) {
+ ctx->failed = true;
+ cbm_log_error("delta.patch_node_failed", "qn",
+ node->qualified_name ? node->qualified_name : "");
+ return;
+ }
+ ctx->nodes++;
+}
+
+static void delta_patch_edge(const cbm_gbuf_edge_t *edge, void *userdata) {
+ delta_patch_ctx_t *ctx = (delta_patch_ctx_t *)userdata;
+ if (ctx->failed) {
+ return;
+ }
+ if (edge->source_id <= ctx->max_db_id && edge->target_id <= ctx->max_db_id) {
+ return; /* both endpoints pre-existed: nothing new to record */
+ }
+ sqlite3_reset(ctx->edge_stmt);
+ sqlite3_bind_text(ctx->edge_stmt, 1, ctx->project, CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ sqlite3_bind_int64(ctx->edge_stmt, 2, edge->source_id);
+ sqlite3_bind_int64(ctx->edge_stmt, 3, edge->target_id);
+ sqlite3_bind_text(ctx->edge_stmt, 4, edge->type, CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ sqlite3_bind_text(ctx->edge_stmt, 5, edge->properties_json ? edge->properties_json : "{}",
+ CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ if (sqlite3_step(ctx->edge_stmt) != SQLITE_DONE) {
+ ctx->failed = true;
+ cbm_log_error("delta.patch_edge_failed", "type", edge->type ? edge->type : "");
+ return;
+ }
+ ctx->edges++;
+}
+
+int cbm_delta_patch(cbm_store_t *store, const char *project, cbm_gbuf_t *gbuf, int64_t max_db_id,
+ const cbm_delta_saved_edge_t *snapshot, int snapshot_count) {
+ sqlite3 *db = cbm_store_get_db(store);
+ if (!db) {
+ return CBM_NOT_FOUND;
+ }
+ if (cbm_store_begin(store) != CBM_STORE_OK) {
+ return CBM_NOT_FOUND;
+ }
+ delta_patch_ctx_t ctx = {.db = db, .project = project, .max_db_id = max_db_id};
+ if (sqlite3_prepare_v2(db,
+ "INSERT INTO nodes (id, project, label, name, qualified_name,"
+ " file_path, start_line, end_line, properties)"
+ " VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9)",
+ CBM_NOT_FOUND, &ctx.node_stmt, NULL) != SQLITE_OK ||
+ sqlite3_prepare_v2(db,
+ "INSERT OR IGNORE INTO edges (project, source_id, target_id, type,"
+ " properties) VALUES (?1,?2,?3,?4,?5)",
+ CBM_NOT_FOUND, &ctx.edge_stmt, NULL) != SQLITE_OK) {
+ sqlite3_finalize(ctx.node_stmt);
+ sqlite3_finalize(ctx.edge_stmt);
+ cbm_store_rollback(store);
+ return CBM_NOT_FOUND;
+ }
+ cbm_gbuf_foreach_node(gbuf, delta_patch_node, &ctx);
+ if (!ctx.failed) {
+ cbm_gbuf_foreach_edge(gbuf, delta_patch_edge, &ctx);
+ }
+ sqlite3_finalize(ctx.node_stmt);
+ sqlite3_finalize(ctx.edge_stmt);
+
+ /* Re-link the snapshotted inbound edges by qualified name. A target
+ * whose QN no longer exists simply matches no row — full-reindex
+ * semantics for deleted symbols, dedup by the UNIQUE edge constraint. */
+ if (!ctx.failed && snapshot_count > 0) {
+ sqlite3_stmt *relink = NULL;
+ if (sqlite3_prepare_v2(db,
+ "INSERT OR IGNORE INTO edges (project, source_id, target_id,"
+ " type, properties)"
+ " SELECT ?1, s.id, t.id, ?2, ?3 FROM nodes s, nodes t"
+ " WHERE s.project = ?1 AND s.qualified_name = ?4"
+ " AND t.project = ?1 AND t.qualified_name = ?5",
+ CBM_NOT_FOUND, &relink, NULL) != SQLITE_OK) {
+ ctx.failed = true;
+ } else {
+ for (int i = 0; i < snapshot_count && !ctx.failed; i++) {
+ sqlite3_reset(relink);
+ sqlite3_bind_text(relink, 1, project, CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ sqlite3_bind_text(relink, 2, snapshot[i].type, CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ sqlite3_bind_text(relink, 3, snapshot[i].props, CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ sqlite3_bind_text(relink, 4, snapshot[i].source_qn, CBM_NOT_FOUND,
+ SQLITE_TRANSIENT);
+ sqlite3_bind_text(relink, 5, snapshot[i].target_qn, CBM_NOT_FOUND,
+ SQLITE_TRANSIENT);
+ if (sqlite3_step(relink) != SQLITE_DONE) {
+ ctx.failed = true;
+ }
+ }
+ sqlite3_finalize(relink);
+ }
+ }
+
+ /* Row-level FTS for exactly the new nodes, through the same tokenizer
+ * function the wholesale rebuild uses. */
+ if (!ctx.failed) {
+ sqlite3_stmt *fts = NULL;
+ if (sqlite3_prepare_v2(db,
+ "INSERT INTO nodes_fts (rowid, name, qualified_name, label,"
+ " file_path)"
+ " SELECT id, cbm_camel_split(name), qualified_name, label,"
+ " file_path FROM nodes WHERE project = ?1 AND id > ?2",
+ CBM_NOT_FOUND, &fts, NULL) == SQLITE_OK) {
+ sqlite3_bind_text(fts, 1, project, CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ sqlite3_bind_int64(fts, 2, max_db_id);
+ if (sqlite3_step(fts) != SQLITE_DONE) {
+ ctx.failed = true;
+ }
+ sqlite3_finalize(fts);
+ } else {
+ /* FTS5 may be compiled out; the table then never existed and
+ * search runs without it — matching the dump path's behavior. */
+ cbm_log_warn("delta.fts_insert_unavailable", "project", project);
+ }
+ }
+
+ if (ctx.failed) {
+ cbm_store_rollback(store);
+ return CBM_NOT_FOUND;
+ }
+ if (cbm_store_commit(store) != CBM_STORE_OK) {
+ return CBM_NOT_FOUND;
+ }
+ char nodes_buf[32];
+ char edges_buf[32];
+ snprintf(nodes_buf, sizeof(nodes_buf), "%lld", (long long)ctx.nodes);
+ snprintf(edges_buf, sizeof(edges_buf), "%lld", (long long)ctx.edges);
+ cbm_log_info("delta.patch", "nodes", nodes_buf, "edges", edges_buf);
+ return 0;
+}
diff --git a/src/pipeline/pipeline_incremental.c b/src/pipeline/pipeline_incremental.c
index 12bea4d76..36ba7b9b5 100644
--- a/src/pipeline/pipeline_incremental.c
+++ b/src/pipeline/pipeline_incremental.c
@@ -1873,6 +1873,379 @@ done:
return 1;
}
+/* ── Delta-repair orchestration (closure route) ──────────────────
+ *
+ * The closure route's executor: clone the live generation, repair the
+ * closure against proxy nodes carrying real database ids, patch the clone
+ * transactionally, publish through the shared staged tail. No full graph
+ * load, no full dump. EVERY failure discards the stage and returns
+ * CBM_PIPELINE_FORCE_FULL_REINDEX: the live database is never touched, so
+ * a full rebuild always self-heals whatever the delta could not do.
+ *
+ * Takes ownership of changed_files/deleted/mode_skipped/old_cov/plan and
+ * closes `store` (the routing read connection). */
+static int run_closure_delta(cbm_pipeline_t *p, const char *db_path, const char *project,
+ const cbm_file_hash_t *baseline_manifest, int baseline_count,
+ closure_plan_t *plan, cbm_file_info_t *changed_files, int ci,
+ char **deleted, int deleted_count, cbm_file_hash_t *mode_skipped,
+ int mode_skipped_count, cbm_coverage_row_t *old_cov, int old_cov_count,
+ cbm_store_t *route_store, struct timespec t0) {
+ struct timespec t;
+ cbm_store_close(route_store);
+
+ int result = CBM_PIPELINE_FORCE_FULL_REINDEX;
+ char *stage = NULL;
+ cbm_store_t *staging = NULL;
+ cbm_delta_saved_edge_t *snapshot = NULL;
+ int snapshot_count = 0;
+ cbm_gbuf_t *gbuf = NULL;
+ cbm_registry_t *registry = NULL;
+ cbm_path_alias_collection_t *path_aliases = NULL;
+ CBMHashTable *stale_surface_set = NULL;
+ closure_resolve_t cr = {0};
+ bool cr_arena_live = false;
+ const char **purge_paths = NULL;
+ cbm_file_hash_t *manifest = NULL;
+ int manifest_count = 0;
+ cbm_lsp_surface_row_t *publish_rows = NULL;
+ int publish_row_count = 0;
+ cbm_coverage_row_t *cov = NULL;
+ int cov_n = 0;
+
+ cbm_clock_gettime(CLOCK_MONOTONIC, &t);
+ if (cbm_delta_stage_clone(db_path, &stage) != 0) {
+ goto out;
+ }
+ cbm_log_info("delta.clone", "elapsed_ms", itoa_buf((int)elapsed_ms(t)));
+ staging = cbm_store_open_path(stage);
+ if (!staging) {
+ goto out;
+ }
+
+ /* Purge set = closure files + deleted files. */
+ int purge_count = ci + deleted_count;
+ purge_paths = (const char **)calloc((size_t)(purge_count ? purge_count : 1), sizeof(char *));
+ if (!purge_paths) {
+ goto out;
+ }
+ for (int i = 0; i < ci; i++) {
+ purge_paths[i] = changed_files[i].rel_path;
+ }
+ for (int i = 0; i < deleted_count; i++) {
+ purge_paths[ci + i] = deleted[i];
+ }
+ cbm_clock_gettime(CLOCK_MONOTONIC, &t);
+ if (cbm_delta_snapshot_inbound(staging, project, purge_paths, purge_count, &snapshot,
+ &snapshot_count) != 0) {
+ goto out;
+ }
+ if (cbm_delta_purge(staging, project, purge_paths, purge_count) != 0) {
+ goto out;
+ }
+ cbm_log_info("delta.snapshot_purge", "captured", itoa_buf(snapshot_count), "purged_files",
+ itoa_buf(purge_count), "elapsed_ms", itoa_buf((int)elapsed_ms(t)));
+
+ /* Small graph: proxies with real ids, then the name registry from them. */
+ cbm_clock_gettime(CLOCK_MONOTONIC, &t);
+ gbuf = cbm_gbuf_new(project, cbm_pipeline_repo_path(p));
+ if (!gbuf) {
+ goto out;
+ }
+ int64_t max_db_id = cbm_delta_preseed(staging, project, gbuf);
+ if (max_db_id < 0) {
+ goto out;
+ }
+ registry = cbm_registry_new();
+ if (!registry) {
+ goto out;
+ }
+ cbm_gbuf_foreach_node(gbuf, registry_visitor, registry);
+ cbm_log_info("delta.preseed_done", "registry", itoa_buf(cbm_registry_size(registry)),
+ "elapsed_ms", itoa_buf((int)elapsed_ms(t)));
+
+ /* Stale-surface set + rehydrated base defs, exactly as the plan computed
+ * them. Owned keys: deleted[] strings die with this function's frees. */
+ stale_surface_set = cbm_ht_create((size_t)(purge_count)*PAIR_LEN + CBM_SZ_64);
+ if (!stale_surface_set) {
+ goto out;
+ }
+ for (int i = 0; i < purge_count; i++) {
+ char *key = strdup(purge_paths[i]);
+ if (!key) {
+ goto out;
+ }
+ cbm_ht_set(stale_surface_set, key, key);
+ }
+ cbm_arena_init(&cr.arena);
+ cr_arena_live = true;
+ {
+ int cap = 0;
+ for (int i = 0; i < plan->stored_count; i++) {
+ const cbm_lsp_surface_row_t *row = &plan->stored_rows[i];
+ if (cbm_ht_get(stale_surface_set, row->rel_path)) {
+ continue;
+ }
+ CBMLSPDef *row_defs = NULL;
+ int row_count = cbm_lsp_surface_defs_from_json(&cr.arena, row->defs_json, &row_defs);
+ if (row_count < 0) {
+ cbm_log_error("delta.rehydrate_failed", "path", row->rel_path);
+ goto out;
+ }
+ if (row_count > 0) {
+ if (cr.base_def_count + row_count > cap) {
+ int ncap = cap ? cap * PAIR_LEN : CBM_SZ_256;
+ while (ncap < cr.base_def_count + row_count) {
+ ncap *= PAIR_LEN;
+ }
+ CBMLSPDef *grown =
+ (CBMLSPDef *)cbm_arena_alloc(&cr.arena, (size_t)ncap * sizeof(CBMLSPDef));
+ if (!grown) {
+ goto out;
+ }
+ if (cr.base_def_count > 0) {
+ memcpy(grown, cr.base_defs, (size_t)cr.base_def_count * sizeof(CBMLSPDef));
+ }
+ cr.base_defs = grown;
+ cap = ncap;
+ }
+ memcpy(cr.base_defs + cr.base_def_count, row_defs,
+ (size_t)row_count * sizeof(CBMLSPDef));
+ cr.base_def_count += row_count;
+ }
+ }
+ }
+
+ char **excluded_dirs = NULL;
+ int excluded_count = 0;
+ cbm_pipeline_get_excluded(p, &excluded_dirs, &excluded_count);
+ path_aliases =
+ cbm_load_path_aliases_excluded(cbm_pipeline_repo_path(p), excluded_dirs, excluded_count);
+ cbm_pipeline_ctx_t ctx = {
+ .project_name = project,
+ .repo_path = cbm_pipeline_repo_path(p),
+ .gbuf = gbuf,
+ .registry = registry,
+ .cancelled = cbm_pipeline_cancelled_ptr(p),
+ .pipeline = p,
+ .mode = cbm_pipeline_get_mode(p),
+ .path_aliases = path_aliases,
+ .excluded_dirs = excluded_dirs,
+ .excluded_count = excluded_count,
+ };
+ for (int i = 0; i < ci; i++) {
+ char *file_qn = cbm_pipeline_fqn_compute(project, changed_files[i].rel_path, "__file__");
+ if (file_qn) {
+ const char *rel = changed_files[i].rel_path;
+ const char *slash = strrchr(rel, '/');
+ const char *basename = slash ? slash + SKIP_ONE : rel;
+ char props[CBM_SZ_256];
+ const char *ext = strrchr(basename, '.');
+ snprintf(props, sizeof(props), "{\"extension\":\"%s\"}", ext ? ext : "");
+ cbm_gbuf_upsert_node(gbuf, "File", basename, file_qn, rel, 0, 0, props);
+ free(file_qn);
+ }
+ }
+
+ cbm_clock_gettime(CLOCK_MONOTONIC, &t);
+ int phase_rc = run_extract_resolve(&ctx, changed_files, ci, &cr);
+ if (phase_rc == 0) {
+ phase_rc = cbm_pipeline_pass_k8s(&ctx, changed_files, ci);
+ }
+ if (phase_rc == 0) {
+ phase_rc = cbm_pipeline_check_cancel(&ctx);
+ }
+ if (phase_rc == 0) {
+ phase_rc = run_postpasses(&ctx, changed_files, ci, project);
+ }
+ if (ctx.return_type_table) {
+ for (int i = 0; i < ctx.return_type_table->count; i++) {
+ free((void *)ctx.return_type_table->entries[i].return_type);
+ }
+ free((void *)ctx.return_type_table);
+ }
+ if (ctx.macro_table) {
+ free((void *)ctx.macro_table);
+ }
+ cbm_pkgmap_free(cbm_pipeline_get_pkgmap());
+ cbm_pipeline_set_pkgmap(NULL);
+ if (phase_rc != 0) {
+ cbm_log_error("delta.err", "phase", "extract_resolve", "rc", itoa_buf(phase_rc));
+ goto out;
+ }
+ cbm_log_info("delta.repair", "files", itoa_buf(ci), "elapsed_ms", itoa_buf((int)elapsed_ms(t)));
+
+ cbm_clock_gettime(CLOCK_MONOTONIC, &t);
+ if (cbm_delta_patch(staging, project, gbuf, max_db_id, snapshot, snapshot_count) != 0) {
+ goto out;
+ }
+ cbm_log_info("delta.patch_done", "elapsed_ms", itoa_buf((int)elapsed_ms(t)));
+
+ /* Coverage: previous failure rows for files not re-extracted + this
+ * run's fresh entries — same merge the legacy tail performs. */
+ cbm_file_error_t *run_errs = NULL;
+ int run_err_count = 0;
+ cbm_pipeline_get_file_errors(p, &run_errs, &run_err_count);
+ char **run_excluded = NULL;
+ int run_excluded_count = 0;
+ cbm_pipeline_get_excluded(p, &run_excluded, &run_excluded_count);
+ cbm_ignored_file_t *run_ignored = NULL;
+ int run_ignored_count = 0;
+ int run_ignored_total = 0;
+ cbm_pipeline_get_ignored(p, &run_ignored, &run_ignored_count, &run_ignored_total);
+ int cov_cap = old_cov_count + run_err_count + run_excluded_count + run_ignored_count;
+ bool coverage_rows_available = cov_cap == 0;
+ if (cov_cap > 0) {
+ cov = (cbm_coverage_row_t *)malloc((size_t)cov_cap * sizeof(*cov));
+ coverage_rows_available = cov != NULL;
+ }
+ if (cov) {
+ for (int i = 0; i < old_cov_count; i++) {
+ bool by_design = old_cov[i].kind && strncmp(old_cov[i].kind, "not_indexed", 11) == 0;
+ if (!by_design && old_cov[i].rel_path &&
+ !cbm_ht_get(stale_surface_set, old_cov[i].rel_path)) {
+ cov[cov_n++] = old_cov[i];
+ }
+ }
+ for (int i = 0; i < run_err_count; i++) {
+ cov[cov_n].rel_path = run_errs[i].path;
+ cov[cov_n].kind = run_errs[i].phase;
+ cov[cov_n].detail = run_errs[i].reason;
+ cov_n++;
+ }
+ for (int i = 0; i < run_excluded_count; i++) {
+ cov[cov_n].rel_path = run_excluded[i];
+ cov[cov_n].kind = "not_indexed_dir";
+ cov[cov_n].detail = "excluded subtree";
+ cov_n++;
+ }
+ for (int i = 0; i < run_ignored_count; i++) {
+ cov[cov_n].rel_path = run_ignored[i].rel_path;
+ cov[cov_n].kind = "not_indexed_file";
+ cov[cov_n].detail = run_ignored[i].reason;
+ cov_n++;
+ }
+ }
+
+ /* Publication race gate: identical to every other route. */
+#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_API
+ cbm_pipeline_persist_test_run_before_final_manifest();
+#endif
+ if (cbm_pipeline_build_fresh_semantic_manifest(project, cbm_pipeline_repo_path(p),
+ cbm_pipeline_get_mode(p), &manifest,
+ &manifest_count) != 0 ||
+ !cbm_pipeline_semantic_manifests_equal(baseline_manifest, baseline_count, manifest,
+ manifest_count)) {
+ cbm_log_warn("delta.abort", "reason", "semantic_inputs_changed");
+ result = CBM_PIPELINE_ABORT_PRESERVE_DB;
+ goto out;
+ }
+
+ /* Surviving previous surfaces + this run's fresh ones. */
+ {
+ int cap = plan->stored_count + cr.fresh_count;
+ publish_rows =
+ (cbm_lsp_surface_row_t *)calloc(cap ? (size_t)cap : 1, sizeof(*publish_rows));
+ if (!publish_rows) {
+ goto out;
+ }
+ for (int i = 0; i < plan->stored_count; i++) {
+ cbm_lsp_surface_row_t *row = &plan->stored_rows[i];
+ if (!row->rel_path || cbm_ht_get(stale_surface_set, row->rel_path)) {
+ continue;
+ }
+ publish_rows[publish_row_count++] = *row;
+ memset(row, 0, sizeof(*row));
+ }
+ for (int i = 0; i < cr.fresh_count; i++) {
+ publish_rows[publish_row_count++] = cr.fresh_rows[i];
+ memset(&cr.fresh_rows[i], 0, sizeof(cr.fresh_rows[i]));
+ }
+ }
+
+ /* Committed counts for the #334 plausibility gate. */
+ cbm_pipeline_set_committed_counts(p, cbm_store_count_nodes(staging, project),
+ cbm_store_count_edges(staging, project));
+
+ {
+ int index_mode = cbm_pipeline_get_mode(p);
+ cbm_pipeline_generation_t generation = {
+ .gbuf = NULL,
+ .final_db_path = db_path,
+ .project = project,
+ .cancelled = cbm_pipeline_cancelled_ptr(p),
+ .manifest = manifest,
+ .manifest_count = manifest_count,
+ .adr_content = NULL, /* the clone already carries the ADR rows */
+ .coverage = cov,
+ .coverage_count = cov_n,
+ .coverage_meta =
+ {
+ .index_mode = incr_mode_name(index_mode),
+ .recording_status =
+ !coverage_rows_available
+ ? "unavailable"
+ : (run_ignored_total > run_ignored_count ? "truncated" : "complete"),
+ .ignored_files_stored = run_ignored_count,
+ .ignored_files_total = run_ignored_total,
+ .coverage_version = CBM_SEMANTIC_INDEX_VERSION,
+ .hash_records_complete = true,
+ },
+ .surface_rows = publish_rows,
+ .surface_row_count = publish_row_count,
+ };
+ cbm_store_close(staging);
+ staging = NULL;
+ result = cbm_pipeline_publish_staged(stage, &generation, false);
+ stage = NULL; /* publish_staged owns and frees it on every path */
+ }
+ cbm_log_info("incremental.done_delta", "elapsed_ms", itoa_buf((int)elapsed_ms(t0)));
+
+out:
+ if (staging) {
+ cbm_store_close(staging);
+ }
+ if (stage) {
+ cbm_pipeline_discard_stage(stage);
+ free(stage);
+ }
+ if (result == CBM_PIPELINE_FORCE_FULL_REINDEX) {
+ cbm_log_warn("delta.fallback", "reason", "delta_failed_full_rebuild");
+#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_API
+ incr_test_set_last_route(CBM_INCREMENTAL_ROUTE_FORCED_FULL);
+#endif
+ }
+ cbm_delta_free_snapshot(snapshot, snapshot_count);
+ surface_name_set_free(stale_surface_set);
+ if (cr_arena_live) {
+ cbm_store_free_lsp_surfaces(cr.fresh_rows, cr.fresh_count);
+ for (int i = 0; i < cr.def_module_count; i++) {
+ free(cr.def_modules[i]);
+ }
+ free(cr.def_modules);
+ cbm_arena_destroy(&cr.arena);
+ }
+ cbm_store_free_lsp_surfaces(publish_rows, publish_row_count);
+ cbm_pipeline_free_semantic_manifest(manifest, manifest_count);
+ free(cov);
+ free(purge_paths);
+ if (registry) {
+ cbm_registry_free(registry);
+ }
+ cbm_path_alias_collection_free(path_aliases);
+ if (gbuf) {
+ cbm_gbuf_free(gbuf);
+ }
+ closure_plan_free(plan);
+ free(changed_files);
+ for (int i = 0; i < deleted_count; i++) {
+ free(deleted[i]);
+ }
+ free(deleted);
+ free_mode_skipped(mode_skipped, mode_skipped_count);
+ cbm_store_free_coverage(old_cov, old_cov_count);
+ return result;
+}
+
/* ── Incremental pipeline entry point ────────────────────────────── */
int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_file_info_t *files,
@@ -1882,7 +2255,6 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
cbm_clock_gettime(CLOCK_MONOTONIC, &t0);
closure_plan_t closure_plan = {0};
bool closure_active = false;
- closure_resolve_t closure_cr = {0};
const char *project = cbm_pipeline_project_name(p);
@@ -2053,6 +2425,15 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
cbm_log_info("incremental.reparse", "files", itoa_buf(ci));
+ if (closure_active) {
+ /* The delta executor owns everything passed and never touches the
+ * live database; its every failure falls back to a full rebuild. */
+ return run_closure_delta(p, db_path, project, baseline_manifest, baseline_count,
+ &closure_plan, changed_files, ci, deleted, deleted_count,
+ mode_skipped, mode_skipped_count, old_cov, old_cov_count, store,
+ t0);
+ }
+
struct timespec t;
/* Step 1: Load existing graph into RAM */
@@ -2137,87 +2518,6 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
cbm_log_info("incremental.edge_snapshot", "captured", itoa_buf(edge_cap.count), "elapsed_ms",
itoa_buf((int)elapsed_ms(t)));
- /* Closure prep: the files whose stored surfaces must NOT be registered
- * or republished — everything being re-parsed plus everything deleted.
- * Keys are OWNED copies: the deleted[] strings die inside the purge
- * loop below, and this set outlives it into the publish merge. */
- CBMHashTable *stale_surface_set = NULL;
- if (closure_active) {
- stale_surface_set = cbm_ht_create((size_t)(ci + deleted_count) * PAIR_LEN + CBM_SZ_64);
- bool prep_ok = stale_surface_set != NULL;
- for (int i = 0; prep_ok && i < ci; i++) {
- char *key = strdup(changed_files[i].rel_path);
- prep_ok = key && (cbm_ht_set(stale_surface_set, key, key), true);
- }
- for (int i = 0; prep_ok && i < deleted_count; i++) {
- char *key = strdup(deleted[i]);
- prep_ok = key && (cbm_ht_set(stale_surface_set, key, key), true);
- }
- /* Rehydrate the surviving surfaces into registration input. A row
- * that fails to decode means the previous generation cannot be
- * trusted as registry input — abort and preserve the old DB; the
- * orchestrator's next full run rewrites every row. */
- if (prep_ok) {
- cbm_arena_init(&closure_cr.arena);
- int cap = 0;
- for (int i = 0; i < closure_plan.stored_count && prep_ok; i++) {
- const cbm_lsp_surface_row_t *row = &closure_plan.stored_rows[i];
- if (cbm_ht_get(stale_surface_set, row->rel_path)) {
- continue;
- }
- CBMLSPDef *row_defs = NULL;
- int row_count =
- cbm_lsp_surface_defs_from_json(&closure_cr.arena, row->defs_json, &row_defs);
- if (row_count < 0) {
- prep_ok = false;
- break;
- }
- if (row_count > 0) {
- if (closure_cr.base_def_count + row_count > cap) {
- int ncap = cap ? cap * PAIR_LEN : CBM_SZ_256;
- while (ncap < closure_cr.base_def_count + row_count) {
- ncap *= PAIR_LEN;
- }
- CBMLSPDef *grown = (CBMLSPDef *)cbm_arena_alloc(
- &closure_cr.arena, (size_t)ncap * sizeof(CBMLSPDef));
- if (!grown) {
- prep_ok = false;
- break;
- }
- if (closure_cr.base_def_count > 0) {
- memcpy(grown, closure_cr.base_defs,
- (size_t)closure_cr.base_def_count * sizeof(CBMLSPDef));
- }
- closure_cr.base_defs = grown;
- cap = ncap;
- }
- memcpy(closure_cr.base_defs + closure_cr.base_def_count, row_defs,
- (size_t)row_count * sizeof(CBMLSPDef));
- closure_cr.base_def_count += row_count;
- }
- }
- }
- if (!prep_ok) {
- cbm_log_error("incremental.err", "phase", "closure_rehydrate");
- surface_name_set_free(stale_surface_set);
- cbm_arena_destroy(&closure_cr.arena);
- closure_plan_free(&closure_plan);
- incr_free_edge_capture(&edge_cap);
- free(changed_files);
- for (int i = 0; i < deleted_count; i++) {
- free(deleted[i]);
- }
- free(deleted);
- free_mode_skipped(mode_skipped, mode_skipped_count);
- cbm_store_free_coverage(old_cov, old_cov_count);
- free(saved_adr);
- cbm_gbuf_free(existing);
- return CBM_PIPELINE_ABORT_PRESERVE_DB;
- }
- cbm_log_info("incremental.closure_base", "defs", itoa_buf(closure_cr.base_def_count),
- "rows", itoa_buf(closure_plan.stored_count));
- }
-
/* Step 2: Purge stale nodes */
cbm_clock_gettime(CLOCK_MONOTONIC, &t);
for (int i = 0; i < ci; i++) {
@@ -2279,8 +2579,7 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
}
}
- int phase_rc =
- run_extract_resolve(&ctx, changed_files, ci, closure_active ? &closure_cr : NULL);
+ int phase_rc = run_extract_resolve(&ctx, changed_files, ci, NULL);
if (phase_rc == 0) {
phase_rc = cbm_pipeline_pass_k8s(&ctx, changed_files, ci);
}
@@ -2319,16 +2618,6 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
free_mode_skipped(mode_skipped, mode_skipped_count);
free(saved_adr);
cbm_gbuf_free(existing);
- if (closure_active) {
- surface_name_set_free(stale_surface_set);
- cbm_store_free_lsp_surfaces(closure_cr.fresh_rows, closure_cr.fresh_count);
- for (int i = 0; i < closure_cr.def_module_count; i++) {
- free(closure_cr.def_modules[i]);
- }
- free(closure_cr.def_modules);
- cbm_arena_destroy(&closure_cr.arena);
- closure_plan_free(&closure_plan);
- }
return CBM_PIPELINE_ABORT_PRESERVE_DB;
}
@@ -2421,16 +2710,6 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
free_mode_skipped(mode_skipped, mode_skipped_count);
free(saved_adr);
cbm_gbuf_free(existing);
- if (closure_active) {
- surface_name_set_free(stale_surface_set);
- cbm_store_free_lsp_surfaces(closure_cr.fresh_rows, closure_cr.fresh_count);
- for (int i = 0; i < closure_cr.def_module_count; i++) {
- free(closure_cr.def_modules[i]);
- }
- free(closure_cr.def_modules);
- cbm_arena_destroy(&closure_cr.arena);
- closure_plan_free(&closure_plan);
- }
return CBM_PIPELINE_ABORT_PRESERVE_DB;
}
@@ -2457,48 +2736,15 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
* re-parsed files have no codec output, and publishing a stale row
* would satisfy a future closure plan with yesterday's surface; an
* empty table just routes the next incremental to a full rebuild. */
- cbm_lsp_surface_row_t *publish_rows = NULL;
- int publish_row_count = 0;
- if (closure_active) {
- int cap = closure_plan.stored_count + closure_cr.fresh_count;
- publish_rows =
- (cbm_lsp_surface_row_t *)calloc(cap ? (size_t)cap : 1, sizeof(*publish_rows));
- if (publish_rows) {
- for (int i = 0; i < closure_plan.stored_count; i++) {
- cbm_lsp_surface_row_t *row = &closure_plan.stored_rows[i];
- if (!row->rel_path || cbm_ht_get(stale_surface_set, row->rel_path)) {
- continue;
- }
- publish_rows[publish_row_count++] = *row; /* move ownership */
- memset(row, 0, sizeof(*row));
- }
- for (int i = 0; i < closure_cr.fresh_count; i++) {
- publish_rows[publish_row_count++] = closure_cr.fresh_rows[i];
- memset(&closure_cr.fresh_rows[i], 0, sizeof(closure_cr.fresh_rows[i]));
- }
- }
- }
- int persist_rc =
- dump_and_persist(existing, db_path, project, cbm_pipeline_cancelled_ptr(p), manifest,
- manifest_count, saved_adr, cbm_pipeline_repo_path(p), cov, cov_n,
- &coverage_meta, publish_rows, publish_row_count);
- cbm_store_free_lsp_surfaces(publish_rows, publish_row_count);
+ int persist_rc = dump_and_persist(
+ existing, db_path, project, cbm_pipeline_cancelled_ptr(p), manifest, manifest_count,
+ saved_adr, cbm_pipeline_repo_path(p), cov, cov_n, &coverage_meta, NULL, 0);
cbm_pipeline_free_semantic_manifest(manifest, manifest_count);
free(saved_adr);
free(cov);
cbm_store_free_coverage(old_cov, old_cov_count);
free_mode_skipped(mode_skipped, mode_skipped_count);
cbm_gbuf_free(existing);
- if (closure_active) {
- surface_name_set_free(stale_surface_set);
- free(closure_cr.fresh_rows); /* strings moved into publish_rows */
- for (int i = 0; i < closure_cr.def_module_count; i++) {
- free(closure_cr.def_modules[i]);
- }
- free(closure_cr.def_modules);
- cbm_arena_destroy(&closure_cr.arena);
- closure_plan_free(&closure_plan);
- }
cbm_log_info("incremental.done", "elapsed_ms", itoa_buf((int)elapsed_ms(t0)));
return persist_rc;
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index 2c6c958bf..fdda368eb 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -714,6 +714,38 @@ int cbm_pipeline_publish_generation(const cbm_pipeline_generation_t *generation)
* stage on every failure. Does NOT free stage_path. */
int cbm_pipeline_finalize_staged_generation(char *stage_path, const char *final_db_path,
atomic_int *cancelled);
+/* Metadata writes + FTS policy + integrity + seal + finalize for an
+ * already-materialized staging DB. Takes ownership of stage_path. The dump
+ * path passes fts_wholesale=true; the delta path passes false (its patch
+ * wrote row-level FTS inserts). generation->gbuf is not read here. */
+int cbm_pipeline_publish_staged(char *stage_path, const cbm_pipeline_generation_t *generation,
+ bool fts_wholesale);
+
+/* mkstemp-minted staging sibling of final_path (exported for the delta
+ * executor; the dump path uses it internally). malloc'd, caller frees. */
+char *cbm_pipeline_create_staging_path(const char *final_path);
+
+/* ── Delta-repair staging primitives (pipeline_delta.c) ──────────
+ * Closure-route-only subsystem: clone the live generation, patch exactly
+ * the repaired node/edge set, publish through the shared finalize leg. */
+typedef struct {
+ char *source_qn;
+ char *target_qn;
+ char *type;
+ char *props;
+} cbm_delta_saved_edge_t;
+
+int cbm_delta_stage_clone(const char *final_db_path, char **out_stage_path);
+int cbm_delta_snapshot_inbound(cbm_store_t *store, const char *project, const char *const *paths,
+ int path_count, cbm_delta_saved_edge_t **out, int *out_count);
+void cbm_delta_free_snapshot(cbm_delta_saved_edge_t *items, int count);
+int cbm_delta_purge(cbm_store_t *store, const char *project, const char *const *paths,
+ int path_count);
+/* Pre-seed proxy nodes with their REAL database ids and move the gbuf id
+ * watermark above MAX(id); returns that max id, or -1 on failure. */
+int64_t cbm_delta_preseed(cbm_store_t *store, const char *project, cbm_gbuf_t *gbuf);
+int cbm_delta_patch(cbm_store_t *store, const char *project, cbm_gbuf_t *gbuf, int64_t max_db_id,
+ const cbm_delta_saved_edge_t *snapshot, int snapshot_count);
/* discard helper shared with the delta executor (unlink stage + sidecars). */
void cbm_pipeline_discard_stage(const char *stage_path);
/* The SQLite generation is authoritative. An explicitly requested artifact is