Skip to content

Commit 2acbd3b

Browse files
committed
fix(store): ADR sidecar follow-ups from DeusData#2315 review
Three small items promised in the DeusData#2315 review; behaviour is otherwise unchanged. 1. A failed ADR restore on artifact import is no longer silent (src/pipeline/artifact.c). The import dropped both a failed reopen of the installed DB and the status of cbm_store_adr_restore_from_graph_if_absent, so a team ADR that never reached the sidecar left no trace. The restore moves into artifact_import_restore_adr, which logs level=warn msg=artifact.import err=adr_restore detail=<store error> path=<db> on either failure. The import still returns 0: the graph DB is already installed when the restore runs, and the restore stays best-effort as documented. adr_restore_if_absent now sets the store error for its begin, copy and commit failures, so the logged detail names the failing step. 2. The doc comment of adr_copy_legacy_once sat above adr_copy_rows, stacked on top of the comment that documents adr_copy_rows, which left adr_copy_legacy_once undocumented. It now sits above adr_copy_legacy_once (src/store/store.c). 3. adr_restore_if_absent no longer inserts the migrated marker. That INSERT OR IGNORE could never change state. Its only caller, cbm_store_adr_restore_from_graph_if_absent, takes the sidecar handle from adr_conn_write, which hands out a handle (cached or fresh) only after adr_copy_legacy_once returned OK, and that function returns OK only when the marker row is already committed (fast path, or the re-check under the write lock) or it just inserted and committed it. No code deletes an adr_meta row, so the insert hit the existing primary key every time, and its result was discarded anyway. Tests: artifact_import_logs_failed_adr_restore puts a directory at <db>.adr.db (the hook-free trick from the DeusData#2315 pipeline test), then asserts the import still succeeds, the graph DB is installed, and the warn line carries the store reason. It is red without the artifact.c change. artifact_export_roundtrip_keeps_adr now also asserts that the fresh destination sidecar is marked migrated after the import, which pins the invariant item 3 relies on. Refs DeusData#2315 Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 3b46abe commit 2acbd3b

3 files changed

Lines changed: 89 additions & 21 deletions

File tree

‎src/pipeline/artifact.c‎

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,28 @@ int cbm_artifact_export(const char *db_path, const char *repo_path, const char *
11301130

11311131
/* ── Import ──────────────────────────────────────────────────────── */
11321132

1133+
/* Restore the imported ADR (carried in the graph DB's project_summaries) into
1134+
* the per-project sidecar so it arrives with the shared artifact — even for a
1135+
* teammate who already indexed the project (a marked sidecar with no ADR),
1136+
* which the marker-gated migrate-once would skip. INSERT OR IGNORE means a
1137+
* local ADR is never overwritten. Best-effort: the graph DB is already
1138+
* installed when this runs, so a failed restore never fails the import, but it
1139+
* is logged with the store's reason so a team ADR that did not arrive is
1140+
* diagnosable instead of silently missing. */
1141+
static void artifact_import_restore_adr(const char *cache_db_path) {
1142+
cbm_store_t *restore = cbm_store_open_path_existing(cache_db_path);
1143+
if (!restore) {
1144+
cbm_log_warn("artifact.import", "err", "adr_restore", "detail", "reopen_imported_db",
1145+
"path", cache_db_path);
1146+
return;
1147+
}
1148+
if (cbm_store_adr_restore_from_graph_if_absent(restore) != CBM_STORE_OK) {
1149+
cbm_log_warn("artifact.import", "err", "adr_restore", "detail", cbm_store_error(restore),
1150+
"path", cache_db_path);
1151+
}
1152+
cbm_store_close(restore);
1153+
}
1154+
11331155
int cbm_artifact_import(const char *repo_path, const char *cache_db_path) {
11341156
if (!repo_path || !cache_db_path) {
11351157
return CBM_NOT_FOUND;
@@ -1257,16 +1279,7 @@ int cbm_artifact_import(const char *repo_path, const char *cache_db_path) {
12571279
cbm_unlink(wal);
12581280
cbm_unlink(shm);
12591281

1260-
/* Restore the imported ADR (carried in the graph DB's project_summaries) into
1261-
* the per-project sidecar so it arrives with the shared artifact — even for a
1262-
* teammate who already indexed the project (a marked sidecar with no ADR),
1263-
* which the marker-gated migrate-once would skip. INSERT OR IGNORE means a
1264-
* local ADR is never overwritten. Best-effort. */
1265-
cbm_store_t *restore = cbm_store_open_path_existing(cache_db_path);
1266-
if (restore) {
1267-
(void)cbm_store_adr_restore_from_graph_if_absent(restore);
1268-
cbm_store_close(restore);
1269-
}
1282+
artifact_import_restore_adr(cache_db_path);
12701283

12711284
cbm_log_info("artifact.import", "db", cache_db_path, "size_mb",
12721285
itoa_buf((int)((size_t)dlen / ART_BYTES_PER_MB)));

‎src/store/store.c‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12010,13 +12010,6 @@ static bool adr_migrated(sqlite3 *adb) {
1201012010
return done;
1201112011
}
1201212012

12013-
/* Copy any legacy ADR rows from the graph DB's project_summaries into the
12014-
* sidecar (INSERT OR IGNORE, so a local row is never clobbered), then set the
12015-
* one-time marker. The marker check, copy and marker insert run in ONE
12016-
* BEGIN IMMEDIATE transaction on the sidecar, so two processes migrating at
12017-
* once cannot re-copy a row the other just deleted. Returns CBM_STORE_ERR on
12018-
* any failure (transaction rolled back) so callers can fail closed and preserve
12019-
* the old generation. No-op once the marker is set. */
1202012013
/* Copy every ADR row from the graph DB's project_summaries into the sidecar with
1202112014
* INSERT OR IGNORE (so a row already present locally is never overwritten). Must
1202212015
* run inside a transaction on the sidecar connection. Returns OK/ERR; does not
@@ -12070,6 +12063,13 @@ static int adr_copy_rows(cbm_store_t *s, sqlite3 *adb) {
1207012063
return (rc == SQLITE_DONE) ? CBM_STORE_OK : CBM_STORE_ERR;
1207112064
}
1207212065

12066+
/* Copy any legacy ADR rows from the graph DB's project_summaries into the
12067+
* sidecar (INSERT OR IGNORE, so a local row is never clobbered), then set the
12068+
* one-time marker. The marker check, copy and marker insert run in ONE
12069+
* BEGIN IMMEDIATE transaction on the sidecar, so two processes migrating at
12070+
* once cannot re-copy a row the other just deleted. Returns CBM_STORE_ERR on
12071+
* any failure (transaction rolled back) so callers can fail closed and preserve
12072+
* the old generation. No-op once the marker is set. */
1207312073
static int adr_copy_legacy_once(cbm_store_t *s, sqlite3 *adb) {
1207412074
if (adr_migrated(adb)) {
1207512075
return CBM_STORE_OK; /* fast path: already migrated, no lock needed */
@@ -12106,19 +12106,22 @@ static int adr_copy_legacy_once(cbm_store_t *s, sqlite3 *adb) {
1210612106
* DB's project_summaries, and a teammate who already indexed the project (so the
1210712107
* sidecar is marked, but has no ADR) must still receive it. INSERT OR IGNORE
1210812108
* guarantees a local ADR is never clobbered; running ungated by the marker is
12109-
* what distinguishes this from the one-time legacy migration. */
12109+
* what distinguishes this from the one-time legacy migration. It never writes
12110+
* the marker: `adb` comes from adr_conn_write, which hands out a sidecar handle
12111+
* only after adr_copy_legacy_once committed it, and nothing deletes it. On
12112+
* failure the store error names the step (the import logs it). */
1211012113
static int adr_restore_if_absent(cbm_store_t *s, sqlite3 *adb) {
1211112114
if (sqlite3_exec(adb, "BEGIN IMMEDIATE;", NULL, NULL, NULL) != SQLITE_OK) {
12115+
store_set_error_sqlite_on(s, adb, "adr restore begin");
1211212116
return CBM_STORE_ERR;
1211312117
}
1211412118
if (adr_copy_rows(s, adb) != CBM_STORE_OK) {
12119+
store_set_error(s, "adr restore copy failed");
1211512120
(void)sqlite3_exec(adb, "ROLLBACK;", NULL, NULL, NULL);
1211612121
return CBM_STORE_ERR;
1211712122
}
12118-
/* Mark migrated too, so a later reindex's before-delete migration is a no-op. */
12119-
(void)sqlite3_exec(adb, "INSERT OR IGNORE INTO adr_meta (key, value) VALUES ('migrated','1');",
12120-
NULL, NULL, NULL);
1212112123
if (sqlite3_exec(adb, "COMMIT;", NULL, NULL, NULL) != SQLITE_OK) {
12124+
store_set_error_sqlite_on(s, adb, "adr restore commit");
1212212125
(void)sqlite3_exec(adb, "ROLLBACK;", NULL, NULL, NULL);
1212312126
return CBM_STORE_ERR;
1212412127
}

‎tests/test_artifact.c‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,21 @@ TEST(artifact_export_roundtrip_keeps_adr) {
252252
cbm_store_adr_free(&adr);
253253
cbm_store_close(dst);
254254

255+
/* The fresh destination sidecar is marked migrated after the import, so a
256+
* later reindex's migrate-before-delete is a no-op. Opening the sidecar for
257+
* the restore already commits the marker; the restore itself adds none. */
258+
char side[1100];
259+
snprintf(side, sizeof(side), "%s.adr.db", import_db);
260+
sqlite3 *raw = NULL;
261+
ASSERT_EQ(sqlite3_open_v2(side, &raw, SQLITE_OPEN_READONLY, NULL), SQLITE_OK);
262+
sqlite3_stmt *st = NULL;
263+
ASSERT_EQ(
264+
sqlite3_prepare_v2(raw, "SELECT value FROM adr_meta WHERE key='migrated';", -1, &st, NULL),
265+
SQLITE_OK);
266+
ASSERT_EQ(sqlite3_step(st), SQLITE_ROW);
267+
sqlite3_finalize(st);
268+
sqlite3_close(raw);
269+
255270
cleanup_dir(g_tmpdir);
256271
PASS();
257272
}
@@ -391,6 +406,42 @@ TEST(artifact_import_restores_adr_when_local_absent) {
391406
PASS();
392407
}
393408

409+
/* A failed ADR restore must not be silent (#2315 follow-up). A DIRECTORY at
410+
* "<db>.adr.db" makes the sidecar open fail deterministically (no hook). The
411+
* graph DB is already installed when the restore runs, so the import still
412+
* succeeds (best-effort, unchanged), but the team ADR that did not arrive is
413+
* logged with the store's reason instead of being dropped without a trace. */
414+
TEST(artifact_import_logs_failed_adr_restore) {
415+
setup_artifact_test();
416+
create_test_db(g_db);
417+
418+
cbm_store_t *src = cbm_store_open_path(g_db);
419+
ASSERT_NOT_NULL(src);
420+
ASSERT_EQ(cbm_store_adr_store(src, "test-proj", "# Decision\nshared A"), CBM_STORE_OK);
421+
cbm_store_close(src);
422+
ASSERT_EQ(cbm_artifact_export(g_db, g_repo, "test-proj", CBM_ARTIFACT_FAST), 0);
423+
424+
char import_db[1024];
425+
snprintf(import_db, sizeof(import_db), "%s/imported.db", g_tmpdir);
426+
char adr_dir[1100];
427+
snprintf(adr_dir, sizeof(adr_dir), "%s.adr.db", import_db);
428+
ASSERT_EQ(cbm_mkdir(adr_dir), 0);
429+
430+
capture_logs_start();
431+
int rc = cbm_artifact_import(g_repo, import_db);
432+
const char *logs = capture_logs_end();
433+
434+
ASSERT_EQ(rc, 0); /* the graph DB is installed; the restore stays best-effort */
435+
ASSERT_TRUE(cbm_file_exists(import_db));
436+
const char *line = strstr(logs, "level=warn msg=artifact.import err=adr_restore");
437+
ASSERT_NOT_NULL(line);
438+
/* The store's reason is carried (the text log format maps spaces to '_'). */
439+
ASSERT(strstr(line, "detail=adr_sidecar_open") != NULL);
440+
441+
cleanup_dir(g_tmpdir);
442+
PASS();
443+
}
444+
394445
TEST(artifact_export_best_roundtrip) {
395446
setup_artifact_test();
396447
create_test_db(g_db);
@@ -1361,6 +1412,7 @@ SUITE(artifact) {
13611412
RUN_TEST(artifact_export_without_adr_does_not_resurrect_legacy_row);
13621413
RUN_TEST(artifact_import_does_not_clobber_local_adr);
13631414
RUN_TEST(artifact_import_restores_adr_when_local_absent);
1415+
RUN_TEST(artifact_import_logs_failed_adr_restore);
13641416
RUN_TEST(artifact_export_best_roundtrip);
13651417
RUN_TEST(artifact_exists_check);
13661418
RUN_TEST(artifact_commit_hash);

0 commit comments

Comments
 (0)