Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
feat(pipeline): closure-repair incremental route
Martin Vogelf07c2e2f3436
diff --git a/src/pipeline/pipeline_incremental.c b/src/pipeline/pipeline_incremental.cindex 66531ebc7..2b82ef7e6 100644--- a/src/pipeline/pipeline_incremental.c+++ b/src/pipeline/pipeline_incremental.c@@ -16,6 +16,9 @@ enum { INCR_RING_BUF = 4, INCR_RING_MASK = 3, INCR_TS_BUF = 24 };#include <stdio.h>#include <time.h>#include "pipeline/artifact.h"+#include "pipeline/lsp_surface.h"+#include "pipeline/pass_lsp_cross.h"+#include "yyjson/yyjson.h"#include "pipeline/pipeline_internal.h"#include "store/store.h"#include "graph_buffer/graph_buffer.h"@@ -946,19 +949,162 @@ static void free_incremental_result_cache(CBMFileResult **cache, int count) {free(cache);}+/* ── Closure repair (reverse-dependency incremental) ─────────────+ *+ * Routing model: the semantic manifest names WHAT changed; the previous+ * generation's graph names WHO consumed it. A body edit leaves a file's+ * persisted LSP surface byte-identical, so only the file itself is+ * recomputed; a surface change pulls in exactly the files with edges into+ * it. Per-file extraction is a pure function of file content, so an+ * unchanged dependent can never be surface-changed in turn — the closure is+ * structurally depth-1 and needs no fixpoint. Every uncertain case declines+ * to a FULL rebuild: new files, added definition names (yesterday's graph+ * cannot know tomorrow's referencers), virtual/config manifest entries,+ * missing or unreadable surface rows, dependents outside discovery, or a+ * closure above the budget. Declining is never wrong — it is exactly+ * today's behaviour. */++enum {+ CLOSURE_BUDGET_PERCENT = 30,+ /* A percentage-only budget starves small repositories (1 changed file in+ * a 3-file project is 33%). Closures at or under this absolute size are+ * always worth attempting; the percentage governs at scale, where it is+ * the honest signal that a rebuild costs less than a repair. */+ CLOSURE_BUDGET_FLOOR_FILES = 8,+};++typedef struct {+ /* Deserialized surfaces of the files NOT being re-parsed, registered+ * into the cross registries exactly as a full build would register+ * their fresh parses. Strings live in `arena`. */+ CBMLSPDef *base_defs;+ int base_def_count;+ CBMArena arena;+ /* Filled by run_extract_resolve: the re-parsed files' new surface rows+ * (heap; ownership passes to the publish merge) and the module strings+ * resolve borrowed (freed after publish). */+ cbm_lsp_surface_row_t *fresh_rows;+ int fresh_count;+ char **def_modules;+ int def_module_count;+} closure_resolve_t;++typedef struct {+ cbm_file_info_t *files; /* heap array; entries borrow strings from the+ * caller's discovery array */+ int count;+ int n_changed;+ int n_dependents;+ cbm_lsp_surface_row_t *stored_rows; /* whole previous generation */+ int stored_count;+} closure_plan_t;++static void closure_plan_free(closure_plan_t *plan) {+ free(plan->files);+ cbm_store_free_lsp_surfaces(plan->stored_rows, plan->stored_count);+ memset(plan, 0, sizeof(*plan));+}++/* Short names a surface JSON defines ("lsp"[].sn plus "reg"[].n), as a+ * heap-keyed set. Returns NULL on parse failure — callers decline to FULL. */+static CBMHashTable *surface_name_set(const char *defs_json) {+ yyjson_doc *doc = yyjson_read(defs_json, strlen(defs_json), 0);+ if (!doc) {+ return NULL;+ }+ yyjson_val *root = yyjson_doc_get_root(doc);+ yyjson_val *lsp = root ? yyjson_obj_get(root, "lsp") : NULL;+ yyjson_val *reg = root ? yyjson_obj_get(root, "reg") : NULL;+ CBMHashTable *set = cbm_ht_create(CBM_SZ_64);+ if (!set) {+ yyjson_doc_free(doc);+ return NULL;+ }+ const char *arr_keys[] = {"sn", "n"};+ yyjson_val *arrs[] = {lsp, reg};+ for (int a = 0; a < 2; a++) {+ if (!arrs[a] || !yyjson_is_arr(arrs[a])) {+ continue;+ }+ size_t idx, max;+ yyjson_val *item;+ yyjson_arr_foreach(arrs[a], idx, max, item) {+ const char *name = yyjson_get_str(yyjson_obj_get(item, arr_keys[a]));+ if (name && name[0] && !cbm_ht_get(set, name)) {+ char *copy = strdup(name);+ if (copy) {+ cbm_ht_set(set, copy, copy);+ }+ }+ }+ }+ yyjson_doc_free(doc);+ return set;+}++static void surface_name_free_visitor(const char *key, void *value, void *userdata) {+ (void)key;+ (void)userdata;+ free(value); /* value IS the heap key copy */+}++static void surface_name_set_free(CBMHashTable *set) {+ if (!set) {+ return;+ }+ cbm_ht_foreach(set, surface_name_free_visitor, NULL);+ cbm_ht_free(set);+}++typedef struct {+ CBMHashTable *stored_set;+ bool added;+} surface_added_check_t;++static void surface_added_visitor(const char *key, void *value, void *userdata) {+ (void)value;+ surface_added_check_t *check = (surface_added_check_t *)userdata;+ if (!check->added && !cbm_ht_get(check->stored_set, key)) {+ check->added = true;+ }+}++/* Does `fresh_json` define any short name absent from `stored_json`? An+ * added name can re-route references in files that have NO edge into this+ * file yet (call-the-helper-you-just-wrote, shadowing), which the+ * edge-derived dependent set cannot see — those cases go to FULL. */+static int surface_added_names(const char *stored_json, const char *fresh_json, bool *out_added) {+ *out_added = true; /* fail closed */+ CBMHashTable *stored_set = surface_name_set(stored_json);+ CBMHashTable *fresh_set = surface_name_set(fresh_json);+ int rc = (stored_set && fresh_set) ? 0 : -1;+ if (rc == 0) {+ surface_added_check_t check = {.stored_set = stored_set, .added = false};+ cbm_ht_foreach(fresh_set, surface_added_visitor, &check);+ *out_added = check.added;+ }+ surface_name_set_free(stored_set);+ surface_name_set_free(fresh_set);+ return rc;+}+/* Run parallel or sequential extract+resolve for changed files. Any failure* aborts before persistence: the caller discards this in-memory graph and* preserves the old on-disk database and its retryable hashes. */-static int run_extract_resolve(cbm_pipeline_ctx_t *ctx, cbm_file_info_t *changed_files, int ci) {+static int run_extract_resolve(cbm_pipeline_ctx_t *ctx, cbm_file_info_t *changed_files, int ci,+ closure_resolve_t *closure) {struct timespec t;- /* Per-file LSP always runs (every mode). Cross-file LSP stays disabled in- * incremental regardless (cbm_parallel_resolve is called with NULL- * cross_registries below). */+ /* Per-file LSP always runs (every mode). Cross-file LSP: the legacy+ * partial route still skips it (NULL cross_registries below); the+ * closure-repair route rebuilds the registries from persisted surfaces ++ * this run's fresh parses and resolves with them — the same Tier-2 path+ * a full build takes, which is what makes its output converge. */#define MIN_FILES_FOR_PARALLEL_INCR 50int worker_count = cbm_default_worker_count(true);- bool use_parallel = (worker_count > SKIP_ONE && ci > MIN_FILES_FOR_PARALLEL_INCR);+ bool use_parallel =+ closure != NULL || (worker_count > SKIP_ONE && ci > MIN_FILES_FOR_PARALLEL_INCR);if (use_parallel) {cbm_log_info("incremental.mode", "mode", "parallel", "workers", itoa_buf(worker_count),@@ -1015,12 +1161,74 @@ static int run_extract_resolve(cbm_pipeline_ctx_t *ctx, cbm_file_info_t *changedatomic_store(&shared_ids, registry_next_id);}- /* Incremental skips cross-file LSP precondition build — it would need- * all_defs from the full project, not just the changed slice. */cbm_clock_gettime(CLOCK_MONOTONIC, &t);- rc = cbm_parallel_resolve(ctx, changed_files, ci, cache, &shared_ids, worker_count, NULL, 0,- NULL, NULL /* module_def_index */,- NULL /* cross_registries — incremental skips Tier 2 prebuild */);+ CBMLSPDef *all_defs = NULL;+ int all_def_count = 0;+ char **def_modules = NULL;+ CBMModuleDefIndex *module_def_index = NULL;+ CBMCrossLspRegistries cross_registries = {0};+ CBMCrossLspRegistries *registries_arg = NULL;+ if (closure) {+ /* Fresh surfaces for the re-parsed files, from the same collect+ * path a full build uses; then registries over stored + fresh. */+ def_modules = (char **)calloc((size_t)ci, sizeof(char *));+ int *def_starts = (int *)calloc((size_t)ci + 1, sizeof(int));+ int fresh_count = 0;+ CBMLSPDef *fresh_defs =+ def_modules && def_starts+ ? cbm_pxc_collect_all_defs(cache, changed_files, ci, ctx->project_name,+ def_modules, &fresh_count, def_starts)+ : NULL;+ if ((fresh_defs || fresh_count == 0) && def_starts &&+ cbm_lsp_surface_build_rows(ctx->project_name, cache, changed_files, ci, fresh_defs,+ def_starts, &closure->fresh_rows,+ &closure->fresh_count) != 0) {+ closure->fresh_rows = NULL;+ closure->fresh_count = 0;+ }+ free(def_starts);+ all_def_count = closure->base_def_count + fresh_count;+ if (all_def_count > 0) {+ all_defs = (CBMLSPDef *)cbm_arena_alloc(&closure->arena,+ (size_t)all_def_count * sizeof(CBMLSPDef));+ }+ if (all_defs) {+ if (closure->base_def_count > 0) {+ memcpy(all_defs, closure->base_defs,+ (size_t)closure->base_def_count * sizeof(CBMLSPDef));+ }+ if (fresh_count > 0) {+ memcpy(all_defs + closure->base_def_count, fresh_defs,+ (size_t)fresh_count * sizeof(CBMLSPDef));+ }+ module_def_index = cbm_pxc_build_module_def_index(all_defs, all_def_count);+ CBMArena *xa = &closure->arena;+ cross_registries.go = cbm_go_build_cross_registry(xa, all_defs, all_def_count);+ cross_registries.python = cbm_py_build_cross_registry(xa, all_defs, all_def_count);+ cross_registries.c = cbm_c_build_cross_registry(xa, all_defs, all_def_count);+ cross_registries.cs = cbm_cs_build_cross_registry(xa, all_defs, all_def_count);+ cross_registries.ts = cbm_ts_build_cross_registry(xa, all_defs, all_def_count);+ registries_arg = &cross_registries;+ } else {+ all_def_count = 0;+ }+ free(fresh_defs);+ /* The resolve workers borrow def_modules strings; ownership moves+ * to the closure ctx so they outlive resolve + calls. */+ closure->def_modules = def_modules;+ closure->def_module_count = ci;+ def_modules = NULL;+ cbm_log_info("incremental.closure_registries", "base",+ itoa_buf(closure->base_def_count), "fresh", itoa_buf(fresh_count),+ "elapsed_ms", itoa_buf((int)elapsed_ms(t)));+ }+ cbm_clock_gettime(CLOCK_MONOTONIC, &t);+ rc = cbm_parallel_resolve(ctx, changed_files, ci, cache, &shared_ids, worker_count,+ all_defs, all_def_count, closure ? closure->def_modules : NULL,+ module_def_index, registries_arg);+ if (module_def_index) {+ cbm_pxc_free_module_def_index(module_def_index);+ }if (rc == 0) {rc = cbm_pipeline_check_cancel(ctx);}@@ -1133,7 +1341,8 @@ static int dump_and_persist(cbm_gbuf_t *gbuf, const char *db_path, const char *patomic_int *cancelled, const cbm_file_hash_t *manifest,int manifest_count, const char *adr_content, const char *repo_path,const cbm_coverage_row_t *cov, int cov_count,- const cbm_coverage_meta_t *meta_template) {+ const cbm_coverage_meta_t *meta_template,+ const cbm_lsp_surface_row_t *surface_rows, int surface_row_count) {struct timespec t;cbm_clock_gettime(CLOCK_MONOTONIC, &t);cbm_pipeline_generation_t generation = {@@ -1147,6 +1356,8 @@ static int dump_and_persist(cbm_gbuf_t *gbuf, const char *db_path, const char *p.coverage = cov,.coverage_count = cov_count,.coverage_meta = meta_template ? *meta_template : (cbm_coverage_meta_t){0},+ .surface_rows = surface_rows,+ .surface_row_count = surface_row_count,};int rc = cbm_pipeline_publish_generation(&generation);cbm_log_info("incremental.dump", "rc", itoa_buf(rc), "elapsed_ms",@@ -1162,6 +1373,311 @@ static int dump_and_persist(cbm_gbuf_t *gbuf, const char *db_path, const char *preturn 0;}+/* Probe-extract a set of files into a throwaway graph to compute their+ * FRESH surfaces without touching the real state. Parses the changed files+ * once more than strictly necessary (the executor re-extracts them into the+ * real graph after the purge); the changed set is small by construction and+ * correctness stays trivially reasoned. Returns heap rows via out. */+static int closure_probe_surfaces(cbm_pipeline_t *p, const char *project,+ cbm_file_info_t *probe_files, int probe_count,+ cbm_lsp_surface_row_t **out_rows, int *out_count) {+ *out_rows = NULL;+ *out_count = 0;+ cbm_gbuf_t *probe_gbuf = cbm_gbuf_new(project, cbm_pipeline_repo_path(p));+ if (!probe_gbuf) {+ return -1;+ }+ char **excluded_dirs = NULL;+ int excluded_count = 0;+ cbm_pipeline_get_excluded(p, &excluded_dirs, &excluded_count);+ cbm_path_alias_collection_t *aliases =+ cbm_load_path_aliases_excluded(cbm_pipeline_repo_path(p), excluded_dirs, excluded_count);+ cbm_pipeline_ctx_t probe_ctx = {+ .project_name = project,+ .repo_path = cbm_pipeline_repo_path(p),+ .gbuf = probe_gbuf,+ .registry = NULL,+ .cancelled = cbm_pipeline_cancelled_ptr(p),+ .pipeline = NULL, /* probe must not record file errors twice */+ .mode = cbm_pipeline_get_mode(p),+ .path_aliases = aliases,+ .excluded_dirs = excluded_dirs,+ .excluded_count = excluded_count,+ };+ CBMFileResult **cache = (CBMFileResult **)calloc((size_t)probe_count, sizeof(CBMFileResult *));+ int rc = cache ? 0 : -1;+ if (rc == 0) {+ _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));+ }+ 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));+ int def_count = 0;+ CBMLSPDef *defs = NULL;+ if (def_modules && def_starts) {+ defs = cbm_pxc_collect_all_defs(cache, probe_files, probe_count, project, def_modules,+ &def_count, def_starts);+ rc = cbm_lsp_surface_build_rows(project, cache, probe_files, probe_count, defs,+ def_starts, out_rows, out_count);+ } else {+ rc = -1;+ }+ free(defs);+ free(def_starts);+ if (def_modules) {+ for (int i = 0; i < probe_count; i++) {+ free(def_modules[i]);+ }+ free(def_modules);+ }+ }+ if (cache) {+ free_incremental_result_cache(cache, probe_count);+ }+ cbm_path_alias_collection_free(aliases);+ cbm_gbuf_free(probe_gbuf);+ return rc;+}++/* Decide whether this manifest delta is closure-repairable, and if so build+ * the plan. Returns 1 with *plan filled, or 0 (decline → FULL) with the+ * reason logged. Every early exit declines; nothing here mutates state. */+static int closure_try_plan(cbm_pipeline_t *p, cbm_store_t *store, const char *project,+ cbm_file_info_t *files, int file_count, const cbm_file_hash_t *stored,+ int stored_count, const cbm_file_hash_t *fresh, int fresh_count,+ closure_plan_t *plan) {+ memset(plan, 0, sizeof(*plan));+ struct timespec t;+ cbm_clock_gettime(CLOCK_MONOTONIC, &t);+ const char *decline = NULL;++ /* Index the fresh manifest and discovery list by rel_path. */+ CBMHashTable *fresh_by_path = cbm_ht_create((size_t)fresh_count * PAIR_LEN + CBM_SZ_64);+ CBMHashTable *files_by_path = cbm_ht_create((size_t)file_count * PAIR_LEN + CBM_SZ_64);+ CBMHashTable *stored_by_path = cbm_ht_create((size_t)stored_count * PAIR_LEN + CBM_SZ_64);+ CBMHashTable *closure_set = cbm_ht_create(CBM_SZ_64);+ char **changed_paths = NULL;+ char **dependents = NULL;+ int dependent_count = 0;+ cbm_lsp_surface_row_t *probe_rows = NULL;+ int probe_count = 0;+ cbm_file_info_t *probe_files = NULL;+ int n_changed = 0;+ int n_deleted = 0;+ if (!fresh_by_path || !files_by_path || !stored_by_path || !closure_set) {+ decline = "alloc";+ goto done;+ }+ for (int i = 0; i < file_count; i++) {+ cbm_ht_set(files_by_path, files[i].rel_path, &files[i]);+ }+ for (int i = 0; i < fresh_count; i++) {+ cbm_ht_set(fresh_by_path, fresh[i].rel_path, (void *)&fresh[i]);+ }+ for (int i = 0; i < stored_count; i++) {+ cbm_ht_set(stored_by_path, stored[i].rel_path, (void *)&stored[i]);+ }++ /* Delta classification straight from the semantic manifests (content+ * hashes, not mtimes). Virtual entries are config/lockfile digests —+ * their effect on resolution is not file-local, so any delta there+ * declines. New files decline: yesterday's graph cannot know who would+ * reference a file that did not exist. */+ changed_paths = (char **)calloc((size_t)fresh_count + (size_t)stored_count, sizeof(char *));+ if (!changed_paths) {+ decline = "alloc";+ goto done;+ }+ for (int i = 0; i < fresh_count; i++) {+ const cbm_file_hash_t *old_row = cbm_ht_get(stored_by_path, fresh[i].rel_path);+ bool is_virtual =+ fresh[i].rel_path[0] == '\x1f' || !cbm_ht_get(files_by_path, fresh[i].rel_path);+ if (!old_row) {+ decline = is_virtual ? "virtual_entry_added" : "new_file";+ goto done;+ }+ if (strcmp(old_row->sha256, fresh[i].sha256) != 0) {+ if (is_virtual) {+ decline = "virtual_or_config_changed";+ goto done;+ }+ changed_paths[n_changed++] = (char *)fresh[i].rel_path;+ }+ }+ for (int i = 0; i < stored_count; i++) {+ if (!cbm_ht_get(fresh_by_path, stored[i].rel_path)) {+ /* Gone from the manifest: deleted or newly ignored. Its nodes+ * will be purged; files with edges into it must re-resolve. */+ changed_paths[n_changed + n_deleted] = (char *)stored[i].rel_path;+ n_deleted++;+ }+ }+ if (n_changed == 0 && n_deleted == 0) {+ decline = "no_file_delta";+ goto done;+ }++ /* Load the previous generation's surfaces and probe the changed files'+ * fresh ones. Missing rows fail closed. */+ if (cbm_store_get_lsp_surfaces(store, project, &plan->stored_rows, &plan->stored_count) !=+ CBM_STORE_OK ||+ plan->stored_count == 0) {+ decline = "no_surface_rows";+ goto done;+ }+ CBMHashTable *rows_by_path = cbm_ht_create((size_t)plan->stored_count * PAIR_LEN);+ if (!rows_by_path) {+ decline = "alloc";+ goto done;+ }+ for (int i = 0; i < plan->stored_count; i++) {+ cbm_ht_set(rows_by_path, plan->stored_rows[i].rel_path, &plan->stored_rows[i]);+ }++ probe_files =+ (cbm_file_info_t *)calloc((size_t)(n_changed ? n_changed : 1), sizeof(cbm_file_info_t));+ if (!probe_files) {+ cbm_ht_free(rows_by_path);+ decline = "alloc";+ goto done;+ }+ for (int i = 0; i < n_changed; i++) {+ cbm_file_info_t *info = cbm_ht_get(files_by_path, changed_paths[i]);+ if (!info) {+ cbm_ht_free(rows_by_path);+ decline = "changed_not_discovered";+ goto done;+ }+ probe_files[i] = *info;+ }+ if (n_changed > 0 && closure_probe_surfaces(p, project, probe_files, n_changed, &probe_rows,+ &probe_count) != 0) {+ cbm_ht_free(rows_by_path);+ decline = "probe_failed";+ goto done;+ }++ /* Early cutoff per changed file; surface-changed files seed the+ * dependent query. Deleted files always seed it. */+ int n_surface_changed = 0;+ const char **dep_targets =+ (const char **)calloc((size_t)(n_changed + n_deleted), sizeof(char *));+ if (!dep_targets) {+ cbm_ht_free(rows_by_path);+ decline = "alloc";+ goto done;+ }+ int dep_target_count = 0;+ for (int i = 0; i < n_changed; i++) {+ const cbm_lsp_surface_row_t *stored_row = cbm_ht_get(rows_by_path, changed_paths[i]);+ const cbm_lsp_surface_row_t *fresh_row = NULL;+ for (int j = 0; j < probe_count; j++) {+ if (strcmp(probe_rows[j].rel_path, changed_paths[i]) == 0) {+ fresh_row = &probe_rows[j];+ break;+ }+ }+ if (!stored_row || !fresh_row) {+ free(dep_targets);+ cbm_ht_free(rows_by_path);+ decline = "missing_surface_row";+ goto done;+ }+ if (strcmp(stored_row->surface_sha, fresh_row->surface_sha) == 0) {+ continue; /* body edit: the file re-resolves, nobody else does */+ }+ bool added = false;+ if (surface_added_names(stored_row->defs_json, fresh_row->defs_json, &added) != 0 ||+ added) {+ free(dep_targets);+ cbm_ht_free(rows_by_path);+ decline = "added_definition_names";+ goto done;+ }+ n_surface_changed++;+ dep_targets[dep_target_count++] = changed_paths[i];+ }+ for (int i = 0; i < n_deleted; i++) {+ dep_targets[dep_target_count++] = changed_paths[n_changed + i];+ }+ cbm_ht_free(rows_by_path);++ if (dep_target_count > 0 &&+ cbm_store_get_dependent_files(store, project, dep_targets, dep_target_count, &dependents,+ &dependent_count) != CBM_STORE_OK) {+ free(dep_targets);+ decline = "dependent_query_failed";+ goto done;+ }+ free(dep_targets);++ /* Closure = changed ∪ dependents. Every member must be in the current+ * discovery (a dependent outside it cannot be re-resolved). */+ for (int i = 0; i < n_changed; i++) {+ if (!cbm_ht_get(closure_set, changed_paths[i])) {+ cbm_ht_set(closure_set, changed_paths[i], changed_paths[i]);+ }+ }+ for (int i = 0; i < dependent_count; i++) {+ if (!cbm_ht_get(files_by_path, dependents[i])) {+ decline = "dependent_not_discovered";+ goto done;+ }+ if (!cbm_ht_get(closure_set, dependents[i])) {+ cbm_ht_set(closure_set, dependents[i], dependents[i]);+ }+ }+ /* closure_count == 0 is legitimate: a deleted-only delta with no+ * dependents has nothing to re-parse, but the purge itself still needs+ * the executor. */+ int closure_count = (int)cbm_ht_count(closure_set);+ if (closure_count > CLOSURE_BUDGET_FLOOR_FILES &&+ closure_count * 100 > file_count * CLOSURE_BUDGET_PERCENT) {+ decline = "budget_exceeded";+ goto done;+ }++ plan->files = (cbm_file_info_t *)calloc((size_t)(closure_count ? closure_count : 1),+ sizeof(cbm_file_info_t));+ if (!plan->files) {+ decline = "alloc";+ goto done;+ }+ plan->count = 0;+ for (int i = 0; i < file_count; i++) {+ if (cbm_ht_get(closure_set, files[i].rel_path)) {+ plan->files[plan->count++] = files[i];+ }+ }+ plan->n_changed = n_changed;+ plan->n_dependents = dependent_count;+ cbm_log_info("incremental.closure_plan", "changed", itoa_buf(n_changed), "surface_changed",+ itoa_buf(n_surface_changed), "deleted", itoa_buf(n_deleted), "dependents",+ itoa_buf(dependent_count), "closure", itoa_buf(plan->count), "elapsed_ms",+ itoa_buf((int)elapsed_ms(t)));++done:+ free(probe_files);+ cbm_store_free_lsp_surfaces(probe_rows, probe_count);+ cbm_store_free_dependent_files(dependents, dependent_count);+ free(changed_paths);+ cbm_ht_free(fresh_by_path);+ cbm_ht_free(files_by_path);+ cbm_ht_free(stored_by_path);+ cbm_ht_free(closure_set);+ if (decline) {+ cbm_log_info("incremental.closure_decline", "reason", decline, "elapsed_ms",+ itoa_buf((int)elapsed_ms(t)));+ cbm_store_free_lsp_surfaces(plan->stored_rows, plan->stored_count);+ memset(plan, 0, sizeof(*plan));+ return 0;+ }+ return 1;+}+/* ── Incremental pipeline entry point ────────────────────────────── */int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_file_info_t *files,@@ -1169,6 +1685,9 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_filint baseline_count) {struct timespec t0;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);@@ -1180,9 +1699,14 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil/* Exact semantic-manifest routing is read-only. Do not reopen a sealed* generation in WAL mode merely to prove that it is already current.- * The legacy partial test route still needs a writable connection. */+ * Only the legacy partial TEST route needs a writable connection, so the+ * writable branch exists only in test-API builds. */+#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_APIcbm_store_t *store =force_legacy_partial ? cbm_store_open_path(db_path) : cbm_store_open_path_query(db_path);+#else+ cbm_store_t *store = cbm_store_open_path_query(db_path);+#endifif (!store) {cbm_log_error("incremental.err", "msg", "open_db_failed", "path", db_path);return CBM_NOT_FOUND;@@ -1213,27 +1737,51 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_filcbm_pipeline_semantic_manifests_equal(stored, stored_count, baseline_manifest,baseline_count);cbm_store_coverage_meta_clear(&meta);- cbm_store_free_file_hashes(stored, stored_count);- cbm_store_close(store);if (exact) {+ cbm_store_free_file_hashes(stored, stored_count);+ cbm_store_close(store);#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_APIincr_test_set_last_route(CBM_INCREMENTAL_ROUTE_NOOP);#endifcbm_log_info("incremental.noop", "reason", "semantic_manifest_equal");return cbm_pipeline_refresh_artifact(p, db_path);}+ /* Manifest delta. Closure repair recomputes exactly the changed+ * files plus the recorded consumers of any changed SURFACE; every+ * uncertain case declines inside the planner, making the pre-plan+ * behaviour (full rebuild) the unconditional fallback. */+ if (metadata_current &&+ closure_try_plan(p, store, project, files, file_count, stored, stored_count,+ baseline_manifest, baseline_count, &closure_plan)) {+ closure_active = true;+#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_API+ incr_test_set_last_route(CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR);+#endif+ cbm_log_info("incremental.route", "route", "closure_repair", "files",+ itoa_buf(closure_plan.count));+ /* Fall through into the shared repair tail below. */+ } else {+ cbm_store_free_file_hashes(stored, stored_count);+ cbm_store_close(store);#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_API- incr_test_set_last_route(CBM_INCREMENTAL_ROUTE_FORCED_FULL);+ incr_test_set_last_route(CBM_INCREMENTAL_ROUTE_FORCED_FULL);#endif- cbm_log_info("incremental.force_full", "reason", "semantic_manifest_changed");- return CBM_PIPELINE_FORCE_FULL_REINDEX;+ cbm_log_info("incremental.force_full", "reason", "semantic_manifest_changed");+ return CBM_PIPELINE_FORCE_FULL_REINDEX;+ }}- /* Classify files */+ /* Classify files. The closure route already knows its re-parse set (the+ * plan); the legacy test route keeps its historical mtime+size classify. */int n_changed = 0;int n_unchanged = 0;- bool *is_changed =- classify_files(files, file_count, stored, stored_count, &n_changed, &n_unchanged);+ bool *is_changed = NULL;+ if (closure_active) {+ n_changed = closure_plan.count;+ } else {+ is_changed =+ classify_files(files, file_count, stored, stored_count, &n_changed, &n_unchanged);+ }/* Classify stored files absent from current discovery: truly-deleted* (purge) vs mode-skipped (preserve nodes AND hash rows). */@@ -1251,7 +1799,7 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil/* Fast path: nothing changed → skip. The on-disk DB is left untouched,* which means existing hash rows (including for any mode-skipped files* that were already preserved by an earlier run) remain intact. */- if (n_changed == 0 && deleted_count == 0) {+ if (!closure_active && n_changed == 0 && deleted_count == 0) {#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_APIincr_test_set_last_route(CBM_INCREMENTAL_ROUTE_NOOP);#endif@@ -1265,7 +1813,9 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil}#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_API- incr_test_set_last_route(CBM_INCREMENTAL_ROUTE_LEGACY_PARTIAL);+ if (!closure_active) {+ incr_test_set_last_route(CBM_INCREMENTAL_ROUTE_LEGACY_PARTIAL);+ }#endifcbm_store_free_file_hashes(stored, stored_count);@@ -1285,6 +1835,7 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_filfree(deleted);free_mode_skipped(mode_skipped, mode_skipped_count);cbm_store_close(store);+ closure_plan_free(&closure_plan);return CBM_PIPELINE_ABORT_PRESERVE_DB;}@@ -1292,9 +1843,15 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_filcbm_file_info_t *changed_files =(n_changed > 0) ? malloc((size_t)n_changed * sizeof(cbm_file_info_t)) : NULL;int ci = 0;- for (int i = 0; i < file_count; i++) {- if (is_changed[i]) {- changed_files[ci++] = files[i];+ if (closure_active) {+ for (int i = 0; i < closure_plan.count; i++) {+ changed_files[ci++] = closure_plan.files[i];+ }+ } else {+ for (int i = 0; i < file_count; i++) {+ if (is_changed[i]) {+ changed_files[ci++] = files[i];+ }}}free(is_changed);@@ -1323,6 +1880,7 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_filfree_mode_skipped(mode_skipped, mode_skipped_count);cbm_store_free_coverage(old_cov, old_cov_count);cbm_store_close(store);+ closure_plan_free(&closure_plan);return CBM_NOT_FOUND;}@@ -1345,6 +1903,7 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_filfree_mode_skipped(mode_skipped, mode_skipped_count);cbm_store_free_coverage(old_cov, old_cov_count);cbm_store_close(store);+ closure_plan_free(&closure_plan);return CBM_PIPELINE_ABORT_PRESERVE_DB;}} else if (adr_rc != CBM_STORE_NOT_FOUND) {@@ -1358,6 +1917,7 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_filfree_mode_skipped(mode_skipped, mode_skipped_count);cbm_store_free_coverage(old_cov, old_cov_count);cbm_store_close(store);+ closure_plan_free(&closure_plan);return CBM_PIPELINE_ABORT_PRESERVE_DB;}@@ -1382,6 +1942,87 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_filcbm_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++) {@@ -1443,7 +2084,8 @@ 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);+ int phase_rc =+ run_extract_resolve(&ctx, changed_files, ci, closure_active ? &closure_cr : NULL);if (phase_rc == 0) {phase_rc = cbm_pipeline_pass_k8s(&ctx, changed_files, ci);}@@ -1482,6 +2124,16 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_filfree_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;}@@ -1574,6 +2226,16 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_filfree_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;}@@ -1595,15 +2257,53 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil.coverage_version = CBM_SEMANTIC_INDEX_VERSION,.hash_records_complete = true,};- 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 surfaces: the surviving previous rows plus this run's fresh+ * ones (closure route). The legacy test route publishes none — its+ * 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);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.hindex 2a9add396..d02e6433b 100644--- a/src/pipeline/pipeline_internal.h+++ b/src/pipeline/pipeline_internal.h@@ -775,6 +775,7 @@ typedef enum {CBM_INCREMENTAL_ROUTE_NOOP,CBM_INCREMENTAL_ROUTE_FORCED_FULL,CBM_INCREMENTAL_ROUTE_LEGACY_PARTIAL,+ CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR,} cbm_incremental_route_t;/* Deterministic one-shot fault injection for the incremental-parallel resultdiff --git a/src/store/store.c b/src/store/store.cindex b3eba79e6..9e50d6df0 100644--- a/src/store/store.c+++ b/src/store/store.c@@ -10,6 +10,7 @@#include <stdint.h>#include "foundation/constants.h"+#include "foundation/hash_table.h"#include <math.h>@@ -3544,6 +3545,124 @@ void cbm_store_free_lsp_surfaces(cbm_lsp_surface_row_t *rows, int count) {free(rows);}+/* One chunk of the dependent-file query. Binding an IN list needs one+ * placeholder per element, so the SQL is assembled per chunk; the chunk cap+ * keeps it bounded. Dedup across chunks happens in the caller's hash set. */+enum { DEPFILE_CHUNK = 200 };++static int dependent_files_chunk(cbm_store_t *s, const char *project,+ const char *const *target_files, int chunk_count,+ CBMHashTable *seen, char ***out, int *out_count, int *out_cap) {+ char sql[CBM_SZ_4K];+ int pos = snprintf(sql, sizeof(sql),+ "SELECT DISTINCT src.file_path 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 (");+ for (int i = 0; i < chunk_count; i++) {+ pos += snprintf(sql + pos, sizeof(sql) - (size_t)pos, "%s?%d", i ? "," : "", i + ST_COL_2);+ if ((size_t)pos >= sizeof(sql)) {+ return CBM_STORE_ERR;+ }+ }+ /* Structural containment is not resolution: a Folder/Project container+ * (whose file_path is a properties placeholder, not a real file) or a+ * CONTAINS_* edge says nothing about who consumed the target's+ * definitions, and the closure planner would otherwise decline on a+ * "dependent" no discovery can ever produce. */+ pos += snprintf(sql + pos, sizeof(sql) - (size_t)pos,+ ") AND src.file_path <> '' AND src.file_path IS NOT NULL"+ " AND src.label NOT IN ('Folder','Project')"+ " AND e.type NOT IN ('CONTAINS_FILE','CONTAINS_FOLDER')");+ if ((size_t)pos >= sizeof(sql)) {+ return CBM_STORE_ERR;+ }+ sqlite3_stmt *stmt = NULL;+ if (sqlite3_prepare_v2(s->db, sql, CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) {+ store_set_error_sqlite(s, "dependent_files prepare");+ return CBM_STORE_ERR;+ }+ bind_text(stmt, ST_COL_1, project);+ for (int i = 0; i < chunk_count; i++) {+ bind_text(stmt, i + ST_COL_2, target_files[i]);+ }+ int step_rc;+ while ((step_rc = sqlite3_step(stmt)) == SQLITE_ROW) {+ const char *path = (const char *)sqlite3_column_text(stmt, 0);+ if (!path || !path[0] || cbm_ht_get(seen, path)) {+ continue;+ }+ if (*out_count >= *out_cap) {+ int ncap = *out_cap ? *out_cap * ST_GROWTH : ST_INIT_CAP_8;+ char **grown = realloc(*out, (size_t)ncap * sizeof(char *));+ if (!grown) {+ sqlite3_finalize(stmt);+ return CBM_STORE_ERR;+ }+ *out = grown;+ *out_cap = ncap;+ }+ char *copy = strdup(path);+ if (!copy) {+ sqlite3_finalize(stmt);+ return CBM_STORE_ERR;+ }+ (*out)[(*out_count)++] = copy;+ cbm_ht_set(seen, copy, copy);+ }+ sqlite3_finalize(stmt);+ return step_rc == SQLITE_DONE ? CBM_STORE_OK : CBM_STORE_ERR;+}++int cbm_store_get_dependent_files(cbm_store_t *s, const char *project,+ const char *const *target_files, int target_count, char ***out,+ int *out_count) {+ *out = NULL;+ *out_count = 0;+ if (!s || !project || target_count <= 0) {+ return target_count == 0 ? CBM_STORE_OK : CBM_STORE_ERR;+ }+ /* Exclude the targets themselves up front: an edge between two files of+ * the closure adds nothing, and the caller wants "who ELSE consumed". */+ CBMHashTable *seen = cbm_ht_create((size_t)target_count * PAIR_LEN);+ if (!seen) {+ return CBM_STORE_ERR;+ }+ for (int i = 0; i < target_count; i++) {+ cbm_ht_set(seen, target_files[i], (void *)target_files[i]);+ }+ char **files = NULL;+ int count = 0;+ int cap = 0;+ int rc = CBM_STORE_OK;+ for (int off = 0; off < target_count && rc == CBM_STORE_OK; off += DEPFILE_CHUNK) {+ int chunk = target_count - off;+ if (chunk > DEPFILE_CHUNK) {+ chunk = DEPFILE_CHUNK;+ }+ rc = dependent_files_chunk(s, project, target_files + off, chunk, seen, &files, &count,+ &cap);+ }+ cbm_ht_free(seen);+ if (rc != CBM_STORE_OK) {+ cbm_store_free_dependent_files(files, count);+ return rc;+ }+ *out = files;+ *out_count = count;+ return CBM_STORE_OK;+}++void cbm_store_free_dependent_files(char **files, int count) {+ if (!files) {+ return;+ }+ for (int i = 0; i < count; i++) {+ free(files[i]);+ }+ free(files);+}+/* ── FindEdgesByURLPath ────────────────────────────────────────── */int cbm_store_find_edges_by_url_path(cbm_store_t *s, const char *project, const char *keyword,diff --git a/src/store/store.h b/src/store/store.hindex 859b5a2ba..ecabd5b72 100644--- a/src/store/store.h+++ b/src/store/store.h@@ -121,6 +121,18 @@ int cbm_store_get_lsp_surfaces(cbm_store_t *s, const char *project, cbm_lsp_surfint cbm_store_delete_lsp_surfaces(cbm_store_t *s, const char *project);void cbm_store_free_lsp_surfaces(cbm_lsp_surface_row_t *rows, int count);+/* Reverse-dependency lookup for closure-repair routing: the DISTINCT+ * file_paths of nodes with at least one edge INTO a node of any file in+ * target_files, excluding the target files themselves. This is "who consumed+ * these files' definitions" as recorded by the previous generation — served+ * by idx_edges_target + idx_nodes_file, so cost tracks the result size, not+ * the graph size. out gets a malloc'd array of malloc'd strings; free with+ * cbm_store_free_dependent_files. */+int cbm_store_get_dependent_files(cbm_store_t *s, const char *project,+ const char *const *target_files, int target_count, char ***out,+ int *out_count);+void cbm_store_free_dependent_files(char **files, int count);+/* Find edges whose properties contain a url_path matching the keyword. */int cbm_store_find_edges_by_url_path(cbm_store_t *s, const char *project, const char *keyword,cbm_edge_t **out, int *count);diff --git a/tests/test_pipeline.c b/tests/test_pipeline.cindex b16cccb2b..bcb32d275 100644--- a/tests/test_pipeline.c+++ b/tests/test_pipeline.c@@ -2177,7 +2177,11 @@ TEST(pipeline_incremental_repoints_call_reference_without_stale_edge) {cbm_pipeline_t *second = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);ASSERT_NOT_NULL(second);ASSERT_EQ(cbm_pipeline_run(second), 0);- ASSERT_EQ(cbm_pipeline_incremental_test_last_route(), CBM_INCREMENTAL_ROUTE_FORCED_FULL);+ /* Since closure repair landed, a single-file content change with no+ * added names routes to CLOSURE_REPAIR instead of a forced full+ * rebuild; the convergence assertions below are unchanged and now pin+ * the repaired graph against the same expectations. */+ ASSERT_EQ(cbm_pipeline_incremental_test_last_route(), CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR);const char *second_project = cbm_pipeline_project_name(second);cbm_store_t *second_store = cbm_store_open_path(db_path);ASSERT_NOT_NULL(second_store);@@ -2305,6 +2309,282 @@ TEST(pipeline_incremental_cross_file_call_reference_matches_fresh_full) {* RED: incremental hashing tracks source inputs but not the semantic influence* of tsconfig.json. Changing only the alias mapping leaves the same-DB graph* pointed at target_a.ts while a fresh full index points at target_b.ts. */+/* ── Closure-repair routing matrix + convergence ─────────────────+ *+ * Every test asserts BOTH the route taken and equality with a fresh full+ * index. Route equality matters because full-rebuild would satisfy any+ * convergence assertion vacuously; graph equality matters because a wrong+ * closure that still routes as CLOSURE_REPAIR is precisely the bug class+ * this feature must never ship. */++static void closure_probe_repo(const char *tmp) {+ write_temp_file(tmp, "lib.ts",+ "export function closureProbeHelper(x: number): string {\n"+ " return String(x);\n"+ "}\n");+ /* The callback-pass shape produces the exact-value CALL_REFERENCE edge+ * (same idiom as the tsconfig alias convergence test). */+ write_temp_file(tmp, "caller.ts",+ "import { closureProbeHelper } from './lib';\n"+ "function acceptClosureProbe(callback: (x: number) => string): void {\n"+ " void callback;\n"+ "}\n"+ "export function closureProbeCaller(): void {\n"+ " acceptClosureProbe(closureProbeHelper);\n"+ "}\n");+ write_temp_file(tmp, "bystander.ts",+ "export function closureBystander(): number {\n"+ " return 7;\n"+ "}\n");+}++/* Fresh full reference build of the same tree into its own DB. */+static void closure_fresh_full(const char *tmp, const char *db_path, int *out_nodes,+ int *out_edges, int *out_ref_edges, const char *project_hint) {+ *out_nodes = -1;+ *out_edges = -2;+ *out_ref_edges = -3;+ cbm_unlink(db_path);+ cbm_remove_db_sidecars(db_path);+ cbm_pipeline_t *full = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL);+ if (!full) {+ return;+ }+ int rc = cbm_pipeline_run(full);+ const char *project = cbm_pipeline_project_name(full);+ if (rc == 0) {+ cbm_store_t *store = cbm_store_open_path(db_path);+ if (store) {+ *out_nodes = cbm_store_count_nodes(store, project);+ *out_edges = cbm_store_count_edges(store, project);+ *out_ref_edges = named_edge_to_file_count(store, project, "CALL_REFERENCE",+ "closureProbeCaller", "closureProbeHelper",+ "lib.ts");+ cbm_store_close(store);+ }+ }+ (void)project_hint;+ cbm_pipeline_free(full);+}++TEST(pipeline_closure_repair_body_edit_converges_with_fresh_full) {+ char tmp[256];+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_closure_body_XXXXXX");+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));+ closure_probe_repo(tmp);+ char db[512];+ snprintf(db, sizeof(db), "%s/closure.db", tmp);++ cbm_pipeline_t *baseline = cbm_pipeline_new(tmp, db, 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);++ /* Body edit only: same exported surface, different body. */+ write_temp_file(tmp, "lib.ts",+ "export function closureProbeHelper(x: number): string {\n"+ " const doubled = x + x;\n"+ " return String(doubled);\n"+ "}\n");++ cbm_pipeline_t *incr = cbm_pipeline_new(tmp, db, CBM_MODE_FULL);+ ASSERT_NOT_NULL(incr);+ ASSERT_EQ(cbm_pipeline_run(incr), 0);+ cbm_incremental_route_t route = cbm_pipeline_incremental_test_last_route();+ cbm_pipeline_free(incr);+ ASSERT_EQ(route, CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR);++ int repaired_nodes = -1;+ int repaired_edges = -1;+ int repaired_refs = -1;+ cbm_store_t *store = cbm_store_open_path(db);+ ASSERT_NOT_NULL(store);+ repaired_nodes = cbm_store_count_nodes(store, project);+ repaired_edges = cbm_store_count_edges(store, project);+ repaired_refs = named_edge_to_file_count(store, project, "CALL_REFERENCE",+ "closureProbeCaller", "closureProbeHelper", "lib.ts");+ cbm_store_close(store);++ char full_db[512];+ snprintf(full_db, sizeof(full_db), "%s/reference-full.db", tmp);+ int full_nodes;+ int full_edges;+ int full_refs;+ closure_fresh_full(tmp, full_db, &full_nodes, &full_edges, &full_refs, project);+ th_rmtree(tmp);++ ASSERT_EQ(repaired_refs, 1);+ ASSERT_EQ(repaired_nodes, full_nodes);+ ASSERT_EQ(repaired_edges, full_edges);+ ASSERT_EQ(repaired_refs, full_refs);+ PASS();+}++/* Removing a definition (no additions) keeps the closure route AND must drop+ * the dependent's stale CALL_REFERENCE. This is the assertion legacy partial+ * could never pass: its QN-keyed re-link resurrected yesterday's edge. */+TEST(pipeline_closure_repair_removed_def_drops_dependent_edge) {+ char tmp[256];+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_closure_removed_XXXXXX");+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));+ closure_probe_repo(tmp);+ char db[512];+ snprintf(db, sizeof(db), "%s/closure.db", tmp);++ cbm_pipeline_t *baseline = cbm_pipeline_new(tmp, db, 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);++ cbm_store_t *pre = cbm_store_open_path(db);+ ASSERT_NOT_NULL(pre);+ ASSERT_EQ(named_edge_to_file_count(pre, project, "CALL_REFERENCE", "closureProbeCaller",+ "closureProbeHelper", "lib.ts"),+ 1);+ cbm_store_close(pre);++ /* The helper vanishes; nothing is added. The caller keeps calling a name+ * that no longer resolves anywhere. */+ write_temp_file(tmp, "lib.ts", "export const closureProbeHelper = undefined;\n");++ cbm_pipeline_t *incr = cbm_pipeline_new(tmp, db, CBM_MODE_FULL);+ ASSERT_NOT_NULL(incr);+ ASSERT_EQ(cbm_pipeline_run(incr), 0);+ cbm_incremental_route_t route = cbm_pipeline_incremental_test_last_route();+ cbm_pipeline_free(incr);++ int repaired_refs = -1;+ cbm_store_t *store = cbm_store_open_path(db);+ ASSERT_NOT_NULL(store);+ repaired_refs = named_edge_to_file_count(store, project, "CALL_REFERENCE",+ "closureProbeCaller", "closureProbeHelper", "lib.ts");+ int repaired_nodes = cbm_store_count_nodes(store, project);+ int repaired_edges = cbm_store_count_edges(store, project);+ cbm_store_close(store);++ char full_db[512];+ snprintf(full_db, sizeof(full_db), "%s/reference-full.db", tmp);+ int full_nodes;+ int full_edges;+ int full_refs;+ closure_fresh_full(tmp, full_db, &full_nodes, &full_edges, &full_refs, project);+ th_rmtree(tmp);++ /* Function def -> const def is a label/type change, not an added NAME, so+ * the closure route must hold — and the stale edge must be gone. */+ ASSERT_EQ(route, CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR);+ ASSERT_EQ(repaired_refs, full_refs);+ ASSERT_EQ(repaired_nodes, full_nodes);+ ASSERT_EQ(repaired_edges, full_edges);+ PASS();+}++TEST(pipeline_closure_repair_added_name_declines_to_full) {+ char tmp[256];+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_closure_added_XXXXXX");+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));+ closure_probe_repo(tmp);+ char db[512];+ snprintf(db, sizeof(db), "%s/closure.db", tmp);+ cbm_pipeline_t *baseline = cbm_pipeline_new(tmp, db, CBM_MODE_FULL);+ ASSERT_NOT_NULL(baseline);+ ASSERT_EQ(cbm_pipeline_run(baseline), 0);+ cbm_pipeline_free(baseline);++ /* A NEW name: yesterday's graph cannot know who would now resolve to+ * it, so the planner must decline. */+ write_temp_file(tmp, "lib.ts",+ "export function closureProbeHelper(x: number): string {\n"+ " return String(x);\n"+ "}\n"+ "export function closureProbeExtra(): number {\n"+ " return 1;\n"+ "}\n");+ cbm_pipeline_t *incr = cbm_pipeline_new(tmp, db, CBM_MODE_FULL);+ ASSERT_NOT_NULL(incr);+ ASSERT_EQ(cbm_pipeline_run(incr), 0);+ cbm_incremental_route_t route = cbm_pipeline_incremental_test_last_route();+ cbm_pipeline_free(incr);+ th_rmtree(tmp);+ ASSERT_EQ(route, CBM_INCREMENTAL_ROUTE_FORCED_FULL);+ PASS();+}++TEST(pipeline_closure_repair_new_file_declines_to_full) {+ char tmp[256];+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_closure_newfile_XXXXXX");+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));+ closure_probe_repo(tmp);+ char db[512];+ snprintf(db, sizeof(db), "%s/closure.db", tmp);+ cbm_pipeline_t *baseline = cbm_pipeline_new(tmp, db, CBM_MODE_FULL);+ ASSERT_NOT_NULL(baseline);+ ASSERT_EQ(cbm_pipeline_run(baseline), 0);+ cbm_pipeline_free(baseline);++ write_temp_file(tmp, "newcomer.ts", "export function closureNewcomer(): void {}\n");+ cbm_pipeline_t *incr = cbm_pipeline_new(tmp, db, CBM_MODE_FULL);+ ASSERT_NOT_NULL(incr);+ ASSERT_EQ(cbm_pipeline_run(incr), 0);+ cbm_incremental_route_t route = cbm_pipeline_incremental_test_last_route();+ cbm_pipeline_free(incr);+ th_rmtree(tmp);+ ASSERT_EQ(route, CBM_INCREMENTAL_ROUTE_FORCED_FULL);+ PASS();+}++TEST(pipeline_closure_repair_budget_declines_to_full) {+ char tmp[256];+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_closure_budget_XXXXXX");+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));+ closure_probe_repo(tmp);+ for (int i = 0; i < 12; i++) {+ char name[64];+ char body[256];+ snprintf(name, sizeof(name), "budget_%02d.ts", i);+ snprintf(body, sizeof(body),+ "export function closureBudget%02d(): number {\n return %d;\n}\n", i, i);+ write_temp_file(tmp, name, body);+ }+ for (int i = 0; i < 18; i++) {+ char name[64];+ char body[128];+ snprintf(name, sizeof(name), "filler_%02d.ts", i);+ snprintf(body, sizeof(body), "export const closureFiller%02d = %d;\n", i, i);+ write_temp_file(tmp, name, body);+ }+ char db[512];+ snprintf(db, sizeof(db), "%s/closure.db", tmp);+ cbm_pipeline_t *baseline = cbm_pipeline_new(tmp, db, CBM_MODE_FULL);+ ASSERT_NOT_NULL(baseline);+ ASSERT_EQ(cbm_pipeline_run(baseline), 0);+ cbm_pipeline_free(baseline);++ /* Body-edit enough files to clear the absolute floor AND the percentage:+ * 12 of 33 files is 36%% with 12 > CLOSURE_BUDGET_FLOOR_FILES, so the+ * planner concedes to a full rebuild. */+ for (int i = 0; i < 12; i++) {+ char name[64];+ char body[256];+ snprintf(name, sizeof(name), "budget_%02d.ts", i);+ snprintf(body, sizeof(body),+ "export function closureBudget%02d(): number {\n return %d + 1;\n}\n", i, i);+ write_temp_file(tmp, name, body);+ }+ cbm_pipeline_t *incr = cbm_pipeline_new(tmp, db, CBM_MODE_FULL);+ ASSERT_NOT_NULL(incr);+ ASSERT_EQ(cbm_pipeline_run(incr), 0);+ cbm_incremental_route_t route = cbm_pipeline_incremental_test_last_route();+ cbm_pipeline_free(incr);+ th_rmtree(tmp);+ ASSERT_EQ(route, CBM_INCREMENTAL_ROUTE_FORCED_FULL);+ PASS();+}+TEST(pipeline_incremental_tsconfig_alias_change_matches_fresh_full) {char tmp[256];snprintf(tmp, sizeof(tmp), "/tmp/cbm_ref_tsconfig_incr_XXXXXX");@@ -11574,6 +11854,11 @@ SUITE(pipeline) {* the default all-suite run still executes it. */SUITE(pipeline_semantic_manifest_repro) {RUN_TEST(pipeline_incremental_repoints_call_reference_without_stale_edge);+ RUN_TEST(pipeline_closure_repair_body_edit_converges_with_fresh_full);+ RUN_TEST(pipeline_closure_repair_removed_def_drops_dependent_edge);+ RUN_TEST(pipeline_closure_repair_added_name_declines_to_full);+ RUN_TEST(pipeline_closure_repair_new_file_declines_to_full);+ RUN_TEST(pipeline_closure_repair_budget_declines_to_full);RUN_TEST(pipeline_incremental_tsconfig_alias_change_matches_fresh_full);#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_APIRUN_TEST(pipeline_git_context_change_forces_full_and_refreshes_branch);diff --git a/tests/test_store_nodes.c b/tests/test_store_nodes.cindex d80808a37..64945d1be 100644--- a/tests/test_store_nodes.c+++ b/tests/test_store_nodes.c@@ -577,6 +577,95 @@ TEST(store_lsp_surface_round_trip) {PASS();}+TEST(store_dependent_files_lookup) {+ cbm_store_t *s = cbm_store_open_memory();+ ASSERT_NOT_NULL(s);+ cbm_store_upsert_project(s, "test", "/tmp/test");++ /* a.py and c.py each call into b.py; b.py also references itself, and+ * d.py sits apart. Dependents of {b.py} must be exactly {a.py, c.py}:+ * the self-reference is not a dependent, d.py is unrelated. */+ const char *files[] = {"a.py", "b.py", "c.py", "d.py"};+ int64_t ids[4] = {0};+ for (int i = 0; i < 4; i++) {+ char qn[64];+ snprintf(qn, sizeof(qn), "test.%s.fn", files[i]);+ cbm_node_t n = {.project = "test",+ .label = "Function",+ .name = "fn",+ .qualified_name = qn,+ .file_path = files[i],+ .start_line = 1,+ .end_line = 2,+ .properties_json = "{}"};+ ids[i] = cbm_store_upsert_node(s, &n);+ ASSERT_TRUE(ids[i] > 0);+ }+ const int64_t edge_pairs[][2] = {{ids[0], ids[1]}, {ids[2], ids[1]}, {ids[1], ids[1]}};+ for (int i = 0; i < 3; i++) {+ cbm_edge_t e = {.project = "test",+ .source_id = edge_pairs[i][0],+ .target_id = edge_pairs[i][1],+ .type = "CALLS",+ .properties_json = "{}"};+ ASSERT_TRUE(cbm_store_insert_edge(s, &e) > 0);+ }++ /* Structural container noise: a Folder node (placeholder file_path)+ * with a CONTAINS_FILE edge into b.py must never surface as a+ * dependent — it is not a source file and cannot be re-resolved. */+ cbm_node_t folder = {.project = "test",+ .label = "Folder",+ .name = "pkg",+ .qualified_name = "test.pkg",+ .file_path = "{}",+ .start_line = 0,+ .end_line = 0,+ .properties_json = "{}"};+ int64_t folder_id = cbm_store_upsert_node(s, &folder);+ ASSERT_TRUE(folder_id > 0);+ cbm_edge_t contains = {.project = "test",+ .source_id = folder_id,+ .target_id = ids[1],+ .type = "CONTAINS_FILE",+ .properties_json = "{}"};+ ASSERT_TRUE(cbm_store_insert_edge(s, &contains) > 0);++ const char *targets[] = {"b.py"};+ char **deps = NULL;+ int dep_count = 0;+ ASSERT_EQ(cbm_store_get_dependent_files(s, "test", targets, 1, &deps, &dep_count),+ CBM_STORE_OK);+ ASSERT_EQ(dep_count, 2);+ bool saw_a = false;+ bool saw_c = false;+ for (int i = 0; i < dep_count; i++) {+ saw_a = saw_a || strcmp(deps[i], "a.py") == 0;+ saw_c = saw_c || strcmp(deps[i], "c.py") == 0;+ }+ ASSERT_TRUE(saw_a && saw_c);+ cbm_store_free_dependent_files(deps, dep_count);++ /* No inbound edges: an isolated target has no dependents. */+ const char *targets_d[] = {"d.py"};+ ASSERT_EQ(cbm_store_get_dependent_files(s, "test", targets_d, 1, &deps, &dep_count),+ CBM_STORE_OK);+ ASSERT_EQ(dep_count, 0);+ cbm_store_free_dependent_files(deps, dep_count);++ /* A target list already containing the dependent excludes it: asking for+ * dependents of {a.py, b.py} returns only c.py. */+ const char *targets_ab[] = {"a.py", "b.py"};+ ASSERT_EQ(cbm_store_get_dependent_files(s, "test", targets_ab, 2, &deps, &dep_count),+ CBM_STORE_OK);+ ASSERT_EQ(dep_count, 1);+ ASSERT_STR_EQ(deps[0], "c.py");+ cbm_store_free_dependent_files(deps, dep_count);++ cbm_store_close(s);+ PASS();+}+TEST(store_file_hash_upsert_rejects_null_required_fields) {/* Pins the API contract that `cbm_store_upsert_file_hash` returns* CBM_STORE_ERR (not silent OK) when a NOT NULL column would receive@@ -2072,6 +2161,7 @@ SUITE(store_nodes) {RUN_TEST(store_cascade_delete);RUN_TEST(store_file_hash_crud);RUN_TEST(store_lsp_surface_round_trip);+ RUN_TEST(store_dependent_files_lookup);RUN_TEST(store_file_hash_upsert_rejects_null_required_fields);RUN_TEST(store_node_properties_json);RUN_TEST(store_node_null_properties);