Skip to content

Commit 7be2bec

Browse files
authored
Merge pull request #2315 from 0xb007ab1e/fix/adr-reindex-race
fix(pipeline): preserve concurrent manage_adr write across reindex swap
2 parents df30463 + 8cef96d commit 7be2bec

15 files changed

Lines changed: 1300 additions & 231 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ Tools requiring arguments accept JSON piped on stdin. Tools with only optional a
906906
|------|-------------|
907907
| `index_repository` | Index a repository into the graph. A successful call from an active MCP session registers the target for auto-sync while that session remains connected (under `daemon start`, also after it disconnects), subject to the global `auto_watch` setting; CLI calls do not register watches. Waits for the whole index by default; pass `async: true` to start it in the daemon and return at once, then poll with `status: true` (see below). |
908908
| `list_projects` | List indexed projects with paging; use `detail: "stats"` for node/edge counts and database sizes. |
909-
| `delete_project` | Remove a project and all its graph data. |
909+
| `delete_project` | Remove a project and all its graph data, including its ADR sidecar (`<db>.adr.db`). |
910910
| `index_status` | Check indexing status of a project. |
911911
| `check_index_coverage` | Check whether exact paths or a scope are indexed and fresh. A clean result means no recorded gap, not proof of completeness. |
912912

‎scripts/memory-core-baseline.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ src/pipeline/pass_semantic.c 3
7474
src/pipeline/pass_tests.c 3
7575
src/pipeline/pass_usages.c 4
7676
src/pipeline/path_alias.c 40
77-
src/pipeline/pipeline.c 132
77+
src/pipeline/pipeline.c 129
7878
src/pipeline/pipeline_delta.c 19
79-
src/pipeline/pipeline_incremental.c 140
79+
src/pipeline/pipeline_incremental.c 130
8080
src/pipeline/registry.c 17
8181
src/pipeline/worker_pool.c 2
8282
src/store/store.c 354

‎src/cli/cli.c‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7464,6 +7464,13 @@ static const char *get_cache_dir(const char *home_dir) {
74647464
return cbm_resolve_cache_dir();
74657465
}
74667466

7467+
/* A per-project ADR sidecar ("<db>.adr.db") sits in the cache beside its index
7468+
* but is not itself an index — exclude it from listing/counting. */
7469+
static bool cli_is_adr_sidecar(const char *name, size_t len) {
7470+
const size_t ext = sizeof(".adr.db") - 1;
7471+
return len >= ext && strcmp(name + len - ext, ".adr.db") == 0;
7472+
}
7473+
74677474
int cbm_list_indexes(const char *home_dir) {
74687475
const char *cache_dir = get_cache_dir(home_dir);
74697476
if (!cache_dir) {
@@ -7478,6 +7485,7 @@ int cbm_list_indexes(const char *home_dir) {
74787485
int count = 0;
74797486
cbm_dirent_t *ent;
74807487
while ((ent = cbm_readdir(d)) != NULL) {
7488+
/* cbm_is_project_index_db already excludes the ".adr.db" sidecar. */
74817489
if (cbm_is_project_index_db(ent->name)) {
74827490
printf(" %s/%s\n", cache_dir, ent->name);
74837491
count++;
@@ -7501,9 +7509,18 @@ int cbm_remove_indexes(const char *home_dir) {
75017509
int count = 0;
75027510
cbm_dirent_t *ent;
75037511
while ((ent = cbm_readdir(d)) != NULL) {
7504-
if (cbm_is_project_index_db(ent->name)) {
7505-
char path[CLI_BUF_1K];
7506-
snprintf(path, sizeof(path), "%s/%s", cache_dir, ent->name);
7512+
bool is_index = cbm_is_project_index_db(ent->name);
7513+
/* A "<db>.adr.db" ADR sidecar is excluded from cbm_is_project_index_db,
7514+
* but a full index wipe must still remove it (and any orphan whose index
7515+
* is already gone) — otherwise the next re-index of that name inherits a
7516+
* stale ADR. It appears as its own dirent, so remove it directly. */
7517+
bool is_adr = cli_is_adr_sidecar(ent->name, strlen(ent->name));
7518+
if (!is_index && !is_adr) {
7519+
continue;
7520+
}
7521+
char path[CLI_BUF_1K];
7522+
snprintf(path, sizeof(path), "%s/%s", cache_dir, ent->name);
7523+
if (is_index) {
75077524
/* Also remove .db.tmp if present */
75087525
char tmp_path[CLI_FIELD_1040];
75097526
snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", path);
@@ -7517,6 +7534,11 @@ int cbm_remove_indexes(const char *home_dir) {
75177534
* its .db. Sidecars are not indexes, so count is unchanged. */
75187535
cbm_remove_db_sidecars(path);
75197536
cbm_remove_db_sidecars(tmp_path);
7537+
} else {
7538+
/* ADR sidecar: remove it and its own -wal/-shm/-journal; not an
7539+
* index, so count is unchanged. */
7540+
cbm_unlink(path);
7541+
cbm_remove_db_sidecars(path);
75207542
}
75217543
}
75227544
cbm_closedir(d);
@@ -10754,7 +10776,8 @@ static int count_db_indexes(const char *home) {
1075410776
int count = 0;
1075510777
cbm_dirent_t *ent;
1075610778
while ((ent = cbm_readdir(d)) != NULL) {
10757-
if (cbm_is_project_index_db(ent->name)) {
10779+
if (cbm_is_project_index_db(ent->name) &&
10780+
!cli_is_adr_sidecar(ent->name, strlen(ent->name))) {
1075810781
count++;
1075910782
}
1076010783
}

‎src/foundation/str_util.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,22 @@ bool cbm_is_internal_cache_db(const char *filename) {
317317

318318
bool cbm_is_project_index_db(const char *filename) {
319319
static const char db_ext[] = ".db";
320+
static const char adr_ext[] = ".adr.db";
320321
const size_t ext_len = sizeof(db_ext) - 1;
322+
const size_t adr_len = sizeof(adr_ext) - 1;
321323
if (!filename) {
322324
return false;
323325
}
324326
size_t len = strlen(filename);
325327
if (len <= ext_len || strcmp(filename + len - ext_len, db_ext) != 0) {
326328
return false;
327329
}
330+
/* A per-project ADR sidecar ("<db>.adr.db") ends in ".db" but is not itself
331+
* an index — exclude it from every index scan/count (MCP loops + the CLI's
332+
* cbm_list_indexes / count_db_indexes all gate on this helper). */
333+
if (len >= adr_len && strcmp(filename + len - adr_len, adr_ext) == 0) {
334+
return false;
335+
}
328336
return !cbm_is_internal_cache_db(filename);
329337
}
330338

‎src/mcp/mcp.c‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ static const tool_def_t TOOLS[] = {
769769
"\"offset\":{\"type\":\"integer\",\"default\":0,\"minimum\":0},"
770770
"\"metadata_only\":{\"type\":\"boolean\",\"default\":false,"
771771
"\"description\":\"Compatibility: omit counts, size, and branch.\"}}}"},
772-
{"delete_project", "Delete a project from the index",
772+
{"delete_project", "Delete a project from the index, including its ADR store",
773773
"{\"type\":\"object\",\"properties\":{\"project\":{\"type\":\"string\"}},\"required\":["
774774
"\"project\"]}"},
775775

@@ -7885,7 +7885,9 @@ static char *handle_index_status(cbm_mcp_server_t *srv, const char *args) {
78857885
return result;
78867886
}
78877887

7888-
/* delete_project: just erase the .db file (and WAL/SHM). */
7888+
/* delete_project: erase the .db file (and WAL/SHM) plus the ADR sidecar
7889+
* "<db>.adr.db" (and its WAL/SHM/journal), so deleting a project removes its
7890+
* ADR store with it. */
78897891
static char *handle_delete_project(cbm_mcp_server_t *srv, const char *args) {
78907892
char *name = get_project_arg(args);
78917893
if (!name) {
@@ -7924,18 +7926,37 @@ static char *handle_delete_project(cbm_mcp_server_t *srv, const char *args) {
79247926
const char *error_detail = NULL;
79257927
bool is_error = false;
79267928

7929+
/* ADR sidecar lives beside the graph DB as "<db>.adr.db" — remove it (and
7930+
* its own WAL/SHM/journal) whenever the project is deleted. Sized as the
7931+
* graph path plus the ".adr.db" suffix. */
7932+
char adr_path[CBM_SZ_1K + sizeof(".adr.db")];
7933+
int adr_n = snprintf(adr_path, sizeof(adr_path), "%s.adr.db", path);
7934+
bool have_adr_path = adr_n > 0 && (size_t)adr_n < sizeof(adr_path);
7935+
79277936
if (exists) {
79287937
int rc = cbm_unlink(path);
79297938
(void)cbm_unlink(wal);
79307939
(void)cbm_unlink(shm);
79317940
if (rc == 0) {
79327941
status = "deleted";
7942+
/* Only drop the ADR store once the graph DB is actually gone, so a
7943+
* failed graph delete leaves the project (and its ADR) intact. */
7944+
if (have_adr_path) {
7945+
(void)cbm_unlink(adr_path);
7946+
(void)cbm_remove_db_sidecars(adr_path);
7947+
}
79337948
} else {
79347949
status = "delete_failed";
79357950
error_detail = strerror(errno);
79367951
is_error = true;
79377952
}
79387953
} else {
7954+
/* No graph DB, but a sidecar could still linger (e.g. ADR written then
7955+
* the index deleted out of band) — best-effort clean it up. */
7956+
if (have_adr_path) {
7957+
(void)cbm_unlink(adr_path);
7958+
(void)cbm_remove_db_sidecars(adr_path);
7959+
}
79397960
is_error = true;
79407961
}
79417962

@@ -20792,9 +20813,10 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {
2079220813
}
2079320814
}
2079420815

20795-
/* ADRs are stored in the SQLite store (project_summaries), the SAME
20796-
* backend the UI /api/adr endpoints use — so writes via the MCP tool and
20797-
* the UI are visible to each other (#256). */
20816+
/* ADRs are stored in the per-project ADR sidecar DB ("<db>.adr.db"), the
20817+
* SAME backend the UI /api/adr endpoints use (via cbm_store_adr_*) — so
20818+
* writes via the MCP tool and the UI are visible to each other (#256), and
20819+
* a reindex of the graph DB never disturbs them. */
2079820820
store_recovery_status_t recovery_status = STORE_RECOVERY_NONE;
2079920821
cbm_store_t *resolved =
2080020822
resolve_store_internal(srv, project, mutation_held, !write_request, &recovery_status, true);

‎src/pipeline/artifact.c‎

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -884,13 +884,54 @@ static void artifact_snapshot_tmp_close(artifact_snapshot_tmp_t *tmp) {
884884
tmp->dir[0] = '\0';
885885
}
886886

887+
/* Make the snapshot's project_summaries carry exactly the current ADR — and
888+
* nothing stale. ADRs now live in the "<db>.adr.db" sidecar, but a legacy
889+
* pre-sidecar row can still linger in the graph DB and gets copied into the
890+
* snapshot by VACUUM INTO; incremental/closure-repair clones keep carrying it.
891+
* So ALWAYS clear project_summaries first, then insert only when an ADR is
892+
* present (`adr` is NULL when the sidecar has none). Otherwise a no-ADR export
893+
* would ship the stale row and a teammate's import would restore a decision
894+
* record that was deleted. Best-effort: on any failure the artifact simply
895+
* omits the ADR, as before the sidecar existed. */
896+
static void snapshot_inject_adr(const char *snapshot_path, const cbm_adr_t *adr) {
897+
sqlite3 *db = NULL;
898+
if (sqlite3_open_v2(snapshot_path, &db, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) {
899+
sqlite3_close(db);
900+
return;
901+
}
902+
sqlite3_exec(db,
903+
"CREATE TABLE IF NOT EXISTS project_summaries ("
904+
" project TEXT PRIMARY KEY, summary TEXT NOT NULL, source_hash TEXT NOT NULL,"
905+
" created_at TEXT NOT NULL, updated_at TEXT NOT NULL);",
906+
NULL, NULL, NULL);
907+
/* Drop any stale row (a deleted ADR must not resurrect through the export). */
908+
sqlite3_exec(db, "DELETE FROM project_summaries;", NULL, NULL, NULL);
909+
if (adr && adr->project && adr->content) {
910+
sqlite3_stmt *st = NULL;
911+
if (sqlite3_prepare_v2(db,
912+
"INSERT INTO project_summaries "
913+
"(project, summary, source_hash, created_at, updated_at) "
914+
"VALUES (?1, ?2, '', ?3, ?4);",
915+
-1, &st, NULL) == SQLITE_OK) {
916+
sqlite3_bind_text(st, 1, adr->project, -1, SQLITE_TRANSIENT);
917+
sqlite3_bind_text(st, 2, adr->content, -1, SQLITE_TRANSIENT);
918+
sqlite3_bind_text(st, 3, adr->created_at ? adr->created_at : "", -1, SQLITE_TRANSIENT);
919+
sqlite3_bind_text(st, 4, adr->updated_at ? adr->updated_at : "", -1, SQLITE_TRANSIENT);
920+
sqlite3_step(st);
921+
sqlite3_finalize(st);
922+
}
923+
}
924+
sqlite3_close(db);
925+
}
926+
887927
/* Prepare a stripped DB copy for best-quality export.
888-
* VACUUM INTO → (optionally) drop indexes → VACUUM. Returns malloc'd buffer
889-
* or NULL. VACUUM INTO runs on BOTH quality levels: it is the consistent
890-
* snapshot — the store runs in WAL mode, so raw main-file bytes miss
891-
* committed transactions still in the -wal and can be mid-checkpoint torn
892-
* (#895). Only the index-stripping is BEST-only. */
893-
static char *prepare_snapshot_db(const char *db_path, size_t *out_size, bool strip_indexes) {
928+
* VACUUM INTO → (optionally) drop indexes → VACUUM → (optionally) inject the
929+
* ADR. Returns malloc'd buffer or NULL. VACUUM INTO runs on BOTH quality
930+
* levels: it is the consistent snapshot — the store runs in WAL mode, so raw
931+
* main-file bytes miss committed transactions still in the -wal and can be
932+
* mid-checkpoint torn (#895). Only the index-stripping is BEST-only. */
933+
static char *prepare_snapshot_db(const char *db_path, size_t *out_size, bool strip_indexes,
934+
const cbm_adr_t *adr) {
894935
artifact_snapshot_tmp_t tmp;
895936
if (!artifact_snapshot_tmp_open(&tmp)) {
896937
artifact_export_fail("prepare_snapshot_dir", cbm_tmpdir(), "private_tmpdir_failed", errno);
@@ -935,6 +976,11 @@ static char *prepare_snapshot_db(const char *db_path, size_t *out_size, bool str
935976
}
936977
}
937978

979+
/* Carry the ADR (from the sidecar) into the snapshot so the exported
980+
* artifact ships the team's decision record. After the strip/VACUUM, so the
981+
* row is not dropped. */
982+
snapshot_inject_adr(tmp_path, adr);
983+
938984
/* Reopened by path rather than held on a descriptor across VACUUM INTO,
939985
* because sqlite owns the create. The private directory is what makes that
940986
* safe: an attacker who cannot enter it cannot swap the file underneath. */
@@ -980,15 +1026,28 @@ int cbm_artifact_export(const char *db_path, const char *repo_path, const char *
9801026
char *db_data = NULL;
9811027
int compression_level = ART_ZSTD_FAST;
9821028

1029+
/* Fetch the ADR from the sidecar (read-only; does not create it) so the
1030+
* snapshot can carry it — the team-shared artifact used to include the ADR
1031+
* via project_summaries, and the sidecar must not silently break that. */
1032+
cbm_adr_t adr = {0};
1033+
bool have_adr = false;
1034+
cbm_store_t *adr_src = cbm_store_open_path_query(db_path);
1035+
if (adr_src) {
1036+
have_adr = cbm_store_adr_get(adr_src, project_name, &adr) == CBM_STORE_OK;
1037+
cbm_store_close(adr_src);
1038+
}
1039+
const cbm_adr_t *adr_arg = have_adr ? &adr : NULL;
1040+
9831041
if (quality == CBM_ARTIFACT_BEST) {
9841042
compression_level = ART_ZSTD_BEST;
985-
db_data = prepare_snapshot_db(db_path, &db_size, true);
1043+
db_data = prepare_snapshot_db(db_path, &db_size, true, adr_arg);
9861044
} else {
9871045
/* FAST keeps zstd-3 and its indexes, but still snapshots via
9881046
* VACUUM INTO: the raw main-file bytes of a live WAL store are a
9891047
* torn copy (#895). */
990-
db_data = prepare_snapshot_db(db_path, &db_size, false);
1048+
db_data = prepare_snapshot_db(db_path, &db_size, false, adr_arg);
9911049
}
1050+
cbm_store_adr_free(&adr);
9921051

9931052
if (!db_data || db_size == 0) {
9941053
free(db_data);
@@ -1198,6 +1257,17 @@ int cbm_artifact_import(const char *repo_path, const char *cache_db_path) {
11981257
cbm_unlink(wal);
11991258
cbm_unlink(shm);
12001259

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+
}
1270+
12011271
cbm_log_info("artifact.import", "db", cache_db_path, "size_mb",
12021272
itoa_buf((int)((size_t)dlen / ART_BYTES_PER_MB)));
12031273

0 commit comments

Comments
 (0)