Skip to content

Commit 6746f43

Browse files
authored
Merge pull request DeusData#2343 from DeusData/fix/issue-2133
fix(cross-repo): a pre-DeusData#768 store no longer aborts ["*"] or wipes edges (DeusData#2133)
2 parents 94097f0 + 45f0c43 commit 6746f43

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
@@ -314,6 +314,93 @@ TEST(cross_repo_wildcard_keeps_projects_containing_internal_tokens) {
314314
PASS();
315315
}
316316

317+
/* A project store written before the #768 edges.local_name_gen column: it
318+
* still answers read-only queries (list_projects shows it), but the
319+
* read-write open every cross-repo target needs refuses it until a reindex. */
320+
static bool cross_repo_create_pre768_project(const cross_repo_fixture_t *fixture,
321+
const char *project) {
322+
char path[512];
323+
if (!cross_repo_project_path(fixture, project, path, sizeof(path))) {
324+
return false;
325+
}
326+
sqlite3 *db = NULL;
327+
if (sqlite3_open(path, &db) != SQLITE_OK) {
328+
sqlite3_close(db);
329+
return false;
330+
}
331+
char sql[1024];
332+
snprintf(sql, sizeof(sql),
333+
"CREATE TABLE projects(name TEXT PRIMARY KEY, indexed_at TEXT NOT NULL,"
334+
" root_path TEXT NOT NULL);"
335+
"CREATE TABLE nodes(id INTEGER PRIMARY KEY AUTOINCREMENT, project TEXT NOT NULL,"
336+
" label TEXT NOT NULL, name TEXT NOT NULL, qualified_name TEXT NOT NULL,"
337+
" file_path TEXT DEFAULT '', start_line INTEGER DEFAULT 0,"
338+
" end_line INTEGER DEFAULT 0, properties TEXT DEFAULT '{}',"
339+
" UNIQUE(project, qualified_name));"
340+
"CREATE TABLE edges(id INTEGER PRIMARY KEY AUTOINCREMENT, project TEXT NOT NULL,"
341+
" source_id INTEGER NOT NULL, target_id INTEGER NOT NULL, type TEXT NOT NULL,"
342+
" properties TEXT DEFAULT '{}', UNIQUE(source_id, target_id, type));"
343+
"INSERT INTO projects VALUES('%s', '2026-06-01T00:00:00Z', '/pre768');",
344+
project);
345+
bool ok = sqlite3_exec(db, sql, NULL, NULL, NULL) == SQLITE_OK;
346+
sqlite3_close(db);
347+
return ok;
348+
}
349+
350+
/* #2133: ["*"] enumerated every store a read-only open accepts, then aborted
351+
* the whole run on the first one the matcher's read-write open refused — a
352+
* single pre-#768 index anywhere in the cache made the wildcard fail with
353+
* "missing, invalid, or not indexed" while naming the same live targets
354+
* worked. A store the matcher cannot use is not a wildcard target. */
355+
TEST(cross_repo_wildcard_skips_pre768_store_issue2133) {
356+
cross_repo_fixture_t fixture;
357+
bool setup = cross_repo_fixture_begin(&fixture) &&
358+
cross_repo_seed_http_pair(&fixture, "wild-src", "wild-api", "/orders", "w") &&
359+
cross_repo_create_pre768_project(&fixture, "aa-pre768-store");
360+
if (!setup) {
361+
cross_repo_fixture_end(&fixture);
362+
FAIL("failed to seed pre-#768 wildcard fixture");
363+
}
364+
365+
const char *targets[] = {"*"};
366+
cbm_cross_repo_result_t result = cbm_cross_repo_match("wild-src", targets, 1);
367+
int edges = cross_repo_count_edges(&fixture, "wild-src", "CROSS_HTTP_CALLS");
368+
cross_repo_fixture_end(&fixture);
369+
370+
ASSERT_FALSE(result.failed);
371+
ASSERT_EQ(result.projects_scanned, 1);
372+
ASSERT_EQ(result.http_edges, 1);
373+
ASSERT_EQ(edges, 1);
374+
PASS();
375+
}
376+
377+
/* Naming an unusable store stays an error, but it must be refused during
378+
* validation, before the source's previous CROSS_* generation is deleted. */
379+
TEST(cross_repo_named_pre768_target_fails_before_cleanup_issue2133) {
380+
cross_repo_fixture_t fixture;
381+
bool setup = cross_repo_fixture_begin(&fixture) &&
382+
cross_repo_seed_http_pair(&fixture, "named-src", "named-api", "/orders", "n") &&
383+
cross_repo_create_pre768_project(&fixture, "aa-pre768-store");
384+
if (!setup) {
385+
cross_repo_fixture_end(&fixture);
386+
FAIL("failed to seed pre-#768 named fixture");
387+
}
388+
389+
const char *live[] = {"named-api"};
390+
cbm_cross_repo_result_t initial = cbm_cross_repo_match("named-src", live, 1);
391+
int before = cross_repo_count_edges(&fixture, "named-src", "CROSS_HTTP_CALLS");
392+
const char *with_pre768[] = {"named-api", "aa-pre768-store"};
393+
cbm_cross_repo_result_t result = cbm_cross_repo_match("named-src", with_pre768, 2);
394+
int after = cross_repo_count_edges(&fixture, "named-src", "CROSS_HTTP_CALLS");
395+
cross_repo_fixture_end(&fixture);
396+
397+
ASSERT_FALSE(initial.failed);
398+
ASSERT_EQ(before, 1);
399+
ASSERT_TRUE(result.failed);
400+
ASSERT_EQ(after, before);
401+
PASS();
402+
}
403+
317404
static bool cross_repo_seed_bounded_scan(const cross_repo_fixture_t *fixture,
318405
const char *source_project, const char *target_project) {
319406
enum { TEST_SCAN_ROWS = 4097 };
@@ -747,6 +834,8 @@ SUITE(cross_repo) {
747834
RUN_TEST(cross_repo_accepts_project_with_missed_shadow_row_issue1609);
748835
RUN_TEST(cross_repo_null_target_fails_without_dereference);
749836
RUN_TEST(cross_repo_wildcard_keeps_projects_containing_internal_tokens);
837+
RUN_TEST(cross_repo_wildcard_skips_pre768_store_issue2133);
838+
RUN_TEST(cross_repo_named_pre768_target_fails_before_cleanup_issue2133);
750839
RUN_TEST(cross_repo_scan_bound_counts_examined_rows_not_matches);
751840
RUN_TEST(cross_repo_propagates_delete_failure);
752841
RUN_TEST(cross_repo_failed_bidirectional_insert_is_not_counted);

0 commit comments

Comments
 (0)