Skip to content

Commit 45f0c43

Browse files
committed
fix(cross-repo): a pre-DeusData#768 store no longer aborts ["*"] or wipes edges (DeusData#2133)
cross-repo-intelligence with target_projects ["*"] failed with "cross-repo source or target project is missing, invalid, or not indexed" while naming the same live targets worked. Root cause: the wildcard enumerator and the pre-write validation both used cr_project_exists(), which only does a read-only open plus the exact-project check. A store indexed before the DeusData#768 edges.local_name_gen column passes that (it still serves queries, so list_projects shows it), but the matcher's read-write open (store init_schema) refuses it until a reindex. One such legacy store anywhere in the cache therefore made every ["*"] run fail, and it failed only after delete_cross_edges() had already removed the source's previous CROSS_* generation, so a failed run was also destructive. Fix: the DeusData#768 probe moves into cbm_store_edges_schema_current() (init_schema now calls it too, one definition), and cr_project_exists() additionally requires it. The wildcard skips a legacy store (logged as cross_repo.project_unusable with the project name); naming one still fails, but during validation, before any write. The probe is read-only, so the legacy store is left untouched for its reindex. Refs DeusData#2133 Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 5958f54 commit 45f0c43

4 files changed

Lines changed: 121 additions & 9 deletions

File tree

‎src/pipeline/pass_cross_repo.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,25 @@ static bool cr_store_has_exact_project(cbm_store_t *store, const char *project)
153153
return matches;
154154
}
155155

156+
/* True when `project` is a usable cross-repo input: its store holds exactly
157+
* that project AND carries the schema the read-write open in
158+
* cr_open_existing_project requires. Checking only the first let a pre-#768
159+
* store (still readable, so list_projects shows it) pass validation and the
160+
* ["*"] enumeration, then abort the whole run at its write open — after the
161+
* source's previous CROSS_* generation had already been deleted. The probe is
162+
* read-only, so the legacy store is left untouched for its reindex. (#2133) */
156163
static bool cr_project_exists(const char *project) {
157164
char path[CR_PATH_BUF];
158165
if (!cr_db_path(project, path, sizeof(path))) {
159166
return false;
160167
}
161168
cbm_store_t *store = cbm_store_open_path_query(path);
162169
bool exists = cr_store_has_exact_project(store, project);
170+
if (exists && !cbm_store_edges_schema_current(store)) {
171+
cbm_log_warn("cross_repo.project_unusable", "project", project, "reason",
172+
"pre_768_schema_reindex_required");
173+
exists = false;
174+
}
163175
cbm_store_close(store);
164176
return exists;
165177
}

‎src/store/store.c‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,18 @@ static void iso_now(char *buf, size_t sz) {
230230

231231
/* ── Schema ─────────────────────────────────────────────────────── */
232232

233+
bool cbm_store_edges_schema_current(cbm_store_t *s) {
234+
sqlite3_stmt *probe = NULL;
235+
if (!s || !s->db ||
236+
sqlite3_prepare_v2(s->db, "SELECT local_name_gen FROM edges LIMIT 0;", CBM_NOT_FOUND,
237+
&probe, NULL) != SQLITE_OK) {
238+
sqlite3_finalize(probe);
239+
return false;
240+
}
241+
sqlite3_finalize(probe);
242+
return true;
243+
}
244+
233245
static int init_schema(cbm_store_t *s) {
234246
const char *ddl =
235247
"CREATE TABLE IF NOT EXISTS projects ("
@@ -357,15 +369,9 @@ static int init_schema(cbm_store_t *s) {
357369
* callers already treat an unopenable DB as incompatible (a full index
358370
* deletes + rebuilds it, artifact import refuses and falls back to a
359371
* reindex). Read-only query opens skip init_schema and keep working. */
360-
{
361-
sqlite3_stmt *probe = NULL;
362-
if (sqlite3_prepare_v2(s->db, "SELECT local_name_gen FROM edges LIMIT 0;", CBM_NOT_FOUND,
363-
&probe, NULL) != SQLITE_OK) {
364-
cbm_log_warn("store.schema", "result", "incompatible", "missing",
365-
"edges.local_name_gen");
366-
return CBM_STORE_ERR;
367-
}
368-
sqlite3_finalize(probe);
372+
if (!cbm_store_edges_schema_current(s)) {
373+
cbm_log_warn("store.schema", "result", "incompatible", "missing", "edges.local_name_gen");
374+
return CBM_STORE_ERR;
369375
}
370376

371377
/* FTS5 contentless virtual table for BM25 full-text search.

‎src/store/store.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,11 @@ const char *cbm_store_db_path(const cbm_store_t *s);
371371
* (projects table has correct types, no corruption indicators).
372372
* Returns false if corruption is detected — caller should delete and re-index. */
373373
bool cbm_store_check_integrity(cbm_store_t *s);
374+
/* True when the edges table carries the #768 local_name_gen discriminator —
375+
* the schema a read-write open (cbm_store_open_path_existing) requires. A
376+
* pre-#768 store still serves read-only queries but refuses every write open
377+
* until it is reindexed. Read-only: safe on a store opened for query. */
378+
bool cbm_store_edges_schema_current(cbm_store_t *s);
374379
/* Shallow check + PRAGMA quick_check — catches page-level corruption.
375380
* O(db size); use on rare paths (artifact import), not hot opens. */
376381
bool cbm_store_check_integrity_deep(cbm_store_t *s);

‎tests/test_cross_repo.c‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,93 @@ TEST(cross_repo_wildcard_keeps_projects_containing_internal_tokens) {
236236
PASS();
237237
}
238238

239+
/* A project store written before the #768 edges.local_name_gen column: it
240+
* still answers read-only queries (list_projects shows it), but the
241+
* read-write open every cross-repo target needs refuses it until a reindex. */
242+
static bool cross_repo_create_pre768_project(const cross_repo_fixture_t *fixture,
243+
const char *project) {
244+
char path[512];
245+
if (!cross_repo_project_path(fixture, project, path, sizeof(path))) {
246+
return false;
247+
}
248+
sqlite3 *db = NULL;
249+
if (sqlite3_open(path, &db) != SQLITE_OK) {
250+
sqlite3_close(db);
251+
return false;
252+
}
253+
char sql[1024];
254+
snprintf(sql, sizeof(sql),
255+
"CREATE TABLE projects(name TEXT PRIMARY KEY, indexed_at TEXT NOT NULL,"
256+
" root_path TEXT NOT NULL);"
257+
"CREATE TABLE nodes(id INTEGER PRIMARY KEY AUTOINCREMENT, project TEXT NOT NULL,"
258+
" label TEXT NOT NULL, name TEXT NOT NULL, qualified_name TEXT NOT NULL,"
259+
" file_path TEXT DEFAULT '', start_line INTEGER DEFAULT 0,"
260+
" end_line INTEGER DEFAULT 0, properties TEXT DEFAULT '{}',"
261+
" UNIQUE(project, qualified_name));"
262+
"CREATE TABLE edges(id INTEGER PRIMARY KEY AUTOINCREMENT, project TEXT NOT NULL,"
263+
" source_id INTEGER NOT NULL, target_id INTEGER NOT NULL, type TEXT NOT NULL,"
264+
" properties TEXT DEFAULT '{}', UNIQUE(source_id, target_id, type));"
265+
"INSERT INTO projects VALUES('%s', '2026-06-01T00:00:00Z', '/pre768');",
266+
project);
267+
bool ok = sqlite3_exec(db, sql, NULL, NULL, NULL) == SQLITE_OK;
268+
sqlite3_close(db);
269+
return ok;
270+
}
271+
272+
/* #2133: ["*"] enumerated every store a read-only open accepts, then aborted
273+
* the whole run on the first one the matcher's read-write open refused — a
274+
* single pre-#768 index anywhere in the cache made the wildcard fail with
275+
* "missing, invalid, or not indexed" while naming the same live targets
276+
* worked. A store the matcher cannot use is not a wildcard target. */
277+
TEST(cross_repo_wildcard_skips_pre768_store_issue2133) {
278+
cross_repo_fixture_t fixture;
279+
bool setup = cross_repo_fixture_begin(&fixture) &&
280+
cross_repo_seed_http_pair(&fixture, "wild-src", "wild-api", "/orders", "w") &&
281+
cross_repo_create_pre768_project(&fixture, "aa-pre768-store");
282+
if (!setup) {
283+
cross_repo_fixture_end(&fixture);
284+
FAIL("failed to seed pre-#768 wildcard fixture");
285+
}
286+
287+
const char *targets[] = {"*"};
288+
cbm_cross_repo_result_t result = cbm_cross_repo_match("wild-src", targets, 1);
289+
int edges = cross_repo_count_edges(&fixture, "wild-src", "CROSS_HTTP_CALLS");
290+
cross_repo_fixture_end(&fixture);
291+
292+
ASSERT_FALSE(result.failed);
293+
ASSERT_EQ(result.projects_scanned, 1);
294+
ASSERT_EQ(result.http_edges, 1);
295+
ASSERT_EQ(edges, 1);
296+
PASS();
297+
}
298+
299+
/* Naming an unusable store stays an error, but it must be refused during
300+
* validation, before the source's previous CROSS_* generation is deleted. */
301+
TEST(cross_repo_named_pre768_target_fails_before_cleanup_issue2133) {
302+
cross_repo_fixture_t fixture;
303+
bool setup = cross_repo_fixture_begin(&fixture) &&
304+
cross_repo_seed_http_pair(&fixture, "named-src", "named-api", "/orders", "n") &&
305+
cross_repo_create_pre768_project(&fixture, "aa-pre768-store");
306+
if (!setup) {
307+
cross_repo_fixture_end(&fixture);
308+
FAIL("failed to seed pre-#768 named fixture");
309+
}
310+
311+
const char *live[] = {"named-api"};
312+
cbm_cross_repo_result_t initial = cbm_cross_repo_match("named-src", live, 1);
313+
int before = cross_repo_count_edges(&fixture, "named-src", "CROSS_HTTP_CALLS");
314+
const char *with_pre768[] = {"named-api", "aa-pre768-store"};
315+
cbm_cross_repo_result_t result = cbm_cross_repo_match("named-src", with_pre768, 2);
316+
int after = cross_repo_count_edges(&fixture, "named-src", "CROSS_HTTP_CALLS");
317+
cross_repo_fixture_end(&fixture);
318+
319+
ASSERT_FALSE(initial.failed);
320+
ASSERT_EQ(before, 1);
321+
ASSERT_TRUE(result.failed);
322+
ASSERT_EQ(after, before);
323+
PASS();
324+
}
325+
239326
static bool cross_repo_seed_bounded_scan(const cross_repo_fixture_t *fixture,
240327
const char *source_project, const char *target_project) {
241328
enum { TEST_SCAN_ROWS = 4097 };
@@ -523,6 +610,8 @@ SUITE(cross_repo) {
523610
RUN_TEST(cross_repo_accepts_project_with_missed_shadow_row_issue1609);
524611
RUN_TEST(cross_repo_null_target_fails_without_dereference);
525612
RUN_TEST(cross_repo_wildcard_keeps_projects_containing_internal_tokens);
613+
RUN_TEST(cross_repo_wildcard_skips_pre768_store_issue2133);
614+
RUN_TEST(cross_repo_named_pre768_target_fails_before_cleanup_issue2133);
526615
RUN_TEST(cross_repo_scan_bound_counts_examined_rows_not_matches);
527616
RUN_TEST(cross_repo_propagates_delete_failure);
528617
RUN_TEST(cross_repo_failed_bidirectional_insert_is_not_counted);

0 commit comments

Comments
 (0)