@@ -1528,6 +1528,7 @@ struct cbm_mcp_server {
15281528 cbm_proc_log_cb index_log_callback ;
15291529 void * index_log_context ;
15301530 cbm_mcp_project_mutation_begin_fn mutation_begin ;
1531+ cbm_mcp_project_mutation_try_begin_fn mutation_try_begin ;
15311532 cbm_mcp_project_mutation_end_fn mutation_end ;
15321533 void * mutation_context ;
15331534 cbm_mcp_quarantine_test_hook_fn quarantine_test_hook ;
@@ -1678,14 +1679,27 @@ void cbm_mcp_server_set_project_mutation_guard(cbm_mcp_server_t *srv,
16781679 return ;
16791680 }
16801681 srv -> mutation_begin = begin ;
1682+ srv -> mutation_try_begin = NULL ;
16811683 srv -> mutation_end = end ;
16821684 srv -> mutation_context = begin ? context : NULL ;
16831685}
16841686
1687+ void cbm_mcp_server_set_project_mutation_try_guard (
1688+ cbm_mcp_server_t * srv , cbm_mcp_project_mutation_try_begin_fn try_begin ) {
1689+ if (srv && srv -> mutation_begin ) {
1690+ srv -> mutation_try_begin = try_begin ;
1691+ }
1692+ }
1693+
16851694static bool mcp_project_mutation_begin (cbm_mcp_server_t * srv , const char * project ) {
16861695 return !srv -> mutation_begin || srv -> mutation_begin (srv -> mutation_context , project );
16871696}
16881697
1698+ static bool mcp_project_mutation_try_begin (cbm_mcp_server_t * srv , const char * project ) {
1699+ return !srv -> mutation_begin ||
1700+ (srv -> mutation_try_begin && srv -> mutation_try_begin (srv -> mutation_context , project ));
1701+ }
1702+
16891703static void mcp_project_mutation_end (cbm_mcp_server_t * srv , const char * project ) {
16901704 if (srv -> mutation_end ) {
16911705 srv -> mutation_end (srv -> mutation_context , project );
@@ -2041,8 +2055,18 @@ static bool quarantine_corrupt_store(cbm_mcp_server_t *srv, const char *project,
20412055/* Open the right project's .db file for query tools.
20422056 * Caches the connection — reopens only when project changes.
20432057 * Tracks last-access time so the event loop can evict idle stores. */
2058+ typedef enum {
2059+ STORE_RECOVERY_NONE ,
2060+ STORE_RECOVERY_BUSY ,
2061+ STORE_RECOVERY_TRY_GUARD_UNAVAILABLE ,
2062+ } store_recovery_status_t ;
2063+
20442064static cbm_store_t * resolve_store_internal (cbm_mcp_server_t * srv , const char * project ,
2045- bool mutation_already_held ) {
2065+ bool mutation_already_held , bool nonblocking_recovery ,
2066+ store_recovery_status_t * recovery_status ) {
2067+ if (recovery_status ) {
2068+ * recovery_status = STORE_RECOVERY_NONE ;
2069+ }
20462070 if (!project ) {
20472071 return NULL ; /* project is required — no implicit fallback */
20482072 }
@@ -2072,9 +2096,16 @@ static cbm_store_t *resolve_store_internal(cbm_mcp_server_t *srv, const char *pr
20722096 srv -> store = NULL ;
20732097 bool mutation_acquired = mutation_already_held ;
20742098 if (!mutation_acquired ) {
2075- mutation_acquired = mcp_project_mutation_begin (srv , project );
2099+ mutation_acquired = nonblocking_recovery
2100+ ? mcp_project_mutation_try_begin (srv , project )
2101+ : mcp_project_mutation_begin (srv , project );
20762102 }
20772103 if (!mutation_acquired ) {
2104+ if (nonblocking_recovery && recovery_status ) {
2105+ * recovery_status = srv -> mutation_try_begin
2106+ ? STORE_RECOVERY_BUSY
2107+ : STORE_RECOVERY_TRY_GUARD_UNAVAILABLE ;
2108+ }
20782109 return NULL ;
20792110 }
20802111
@@ -2138,7 +2169,7 @@ static cbm_store_t *resolve_store_internal(cbm_mcp_server_t *srv, const char *pr
21382169}
21392170
21402171static cbm_store_t * resolve_store (cbm_mcp_server_t * srv , const char * project ) {
2141- return resolve_store_internal (srv , project , false);
2172+ return resolve_store_internal (srv , project , false, false, NULL );
21422173}
21432174
21442175/* Forward decl — definition lives below alongside list_projects. */
@@ -10365,6 +10396,28 @@ static char *adr_read_legacy_file(const char *root_path) {
1036510396 "then draft and store. Sections: PURPOSE, STACK, ARCHITECTURE, " \
1036610397 "PATTERNS, TRADEOFFS, PHILOSOPHY."
1036710398
10399+ /* resolve_store opens file-backed projects query-only. A mutation must release
10400+ * that reader before opening a dedicated writer because atomic publication uses
10401+ * a self-contained DELETE-mode database that switches back to WAL on write. */
10402+ static cbm_store_t * open_adr_store_for_write (cbm_mcp_server_t * srv , cbm_store_t * resolved ,
10403+ cbm_store_t * * owned_rw ) {
10404+ if (!srv || !resolved || !owned_rw ) {
10405+ return NULL ;
10406+ }
10407+ const char * resolved_db_path = cbm_store_db_path (resolved );
10408+ if (!resolved_db_path ) {
10409+ return resolved ;
10410+ }
10411+ char * rw_path = heap_strdup (resolved_db_path );
10412+ if (!rw_path ) {
10413+ return NULL ;
10414+ }
10415+ invalidate_cached_store (srv );
10416+ * owned_rw = cbm_store_open_path (rw_path );
10417+ free (rw_path );
10418+ return * owned_rw ;
10419+ }
10420+
1036810421static char * handle_manage_adr (cbm_mcp_server_t * srv , const char * args ) {
1036910422 char * project = get_project_arg (args );
1037010423 char * mode_str = cbm_mcp_get_string_arg (args , "mode" );
@@ -10396,8 +10449,10 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {
1039610449 true);
1039710450 }
1039810451
10452+ bool write_request =
10453+ content && (strcmp (mode_str , "update" ) == 0 || strcmp (mode_str , "store" ) == 0 );
1039910454 bool mutation_held = false;
10400- if (project ) {
10455+ if (write_request && project ) {
1040110456 mutation_held = mcp_project_mutation_begin (srv , project );
1040210457 if (!mutation_held ) {
1040310458 free (project );
@@ -10418,11 +10473,21 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {
1041810473 /* ADRs are stored in the SQLite store (project_summaries), the SAME
1041910474 * backend the UI /api/adr endpoints use — so writes via the MCP tool and
1042010475 * the UI are visible to each other (#256). */
10421- cbm_store_t * resolved = resolve_store_internal (srv , project , mutation_held );
10476+ store_recovery_status_t recovery_status = STORE_RECOVERY_NONE ;
10477+ cbm_store_t * resolved =
10478+ resolve_store_internal (srv , project , mutation_held , !write_request , & recovery_status );
1042210479 if (!resolved ) {
10423- char * err = build_no_store_error (project );
10424- char * res = cbm_mcp_text_result (err , true);
10425- free (err );
10480+ char * res = NULL ;
10481+ if (recovery_status == STORE_RECOVERY_BUSY ) {
10482+ res = cbm_mcp_text_result ("project is busy; retry after indexing" , true);
10483+ } else if (recovery_status == STORE_RECOVERY_TRY_GUARD_UNAVAILABLE ) {
10484+ res =
10485+ cbm_mcp_text_result ("project recovery requires a nonblocking mutation guard" , true);
10486+ } else {
10487+ char * err = build_no_store_error (project );
10488+ res = cbm_mcp_text_result (err , true);
10489+ free (err );
10490+ }
1042610491 if (mutation_held ) {
1042710492 mcp_project_mutation_end (srv , project );
1042810493 }
@@ -10432,66 +10497,57 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {
1043210497 return res ;
1043310498 }
1043410499
10435- /* resolve_store opens file-backed projects READ-ONLY (query stores must
10436- * not mutate the DB). manage_adr is the only resolve_store caller that
10437- * WRITES, so it needs a writable handle. For a file-backed project open a
10438- * dedicated read-write handle to the same DB file (the project is verified
10439- * to exist via resolve_store, so cbm_store_open_path won't create a ghost
10440- * DB). For an in-memory / embedded store (db_path == NULL) the resolved
10441- * store is already writable — use it directly. */
1044210500 cbm_store_t * store = resolved ;
1044310501 cbm_store_t * owned_rw = NULL ;
10444- const char * resolved_db_path = cbm_store_db_path (resolved );
10445- if (resolved_db_path ) {
10446- /* Atomic publication leaves a self-contained DELETE-mode database.
10447- * Opening the writer switches it back to WAL, which requires an
10448- * exclusive lock and therefore fails while resolve_store's read-only
10449- * handle is still open. Copy the ACTUAL resolved path first (it may be
10450- * a drifted-filename fallback), then release that cached reader before
10451- * opening the writer. Request-scoped queries reopen it when needed. */
10452- char * rw_path = heap_strdup (resolved_db_path );
10453- if (!rw_path ) {
10454- if (mutation_held ) {
10455- mcp_project_mutation_end (srv , project );
10456- }
10457- free (project );
10458- free (mode_str );
10459- free (content );
10460- return cbm_mcp_text_result ("failed to allocate ADR store path" , true);
10461- }
10462- invalidate_cached_store (srv );
10463- owned_rw = cbm_store_open_path (rw_path );
10464- free (rw_path );
10465- if (!owned_rw ) {
10466- char * err = build_no_store_error (project );
10467- char * res = cbm_mcp_text_result (err , true);
10468- free (err );
10502+ if (write_request ) {
10503+ store = open_adr_store_for_write (srv , resolved , & owned_rw );
10504+ if (!store ) {
1046910505 if (mutation_held ) {
1047010506 mcp_project_mutation_end (srv , project );
1047110507 }
1047210508 free (project );
1047310509 free (mode_str );
1047410510 free (content );
10475- return res ;
10511+ return cbm_mcp_text_result ( "failed to open writable ADR store" , true) ;
1047610512 }
10477- store = owned_rw ;
1047810513 }
1047910514
1048010515 /* One-time migration: older versions wrote ADRs to a file at
10481- * <root>/.codebase-memory/adr.md. If the store has no ADR yet but that
10482- * legacy file exists, import it so nothing is lost on upgrade. */
10516+ * <root>/.codebase-memory/adr.md. A read never waits for the project lease:
10517+ * it returns the legacy content immediately and attempts migration only if
10518+ * a nonblocking acquire succeeds. */
1048310519 cbm_adr_t adr ;
1048410520 memset (& adr , 0 , sizeof (adr ));
1048510521 bool have_adr = (cbm_store_adr_get (store , project , & adr ) == CBM_STORE_OK );
10486- if (!have_adr ) {
10522+ char * legacy = NULL ;
10523+ if (!have_adr && !write_request ) {
1048710524 char * root_path = project_root_from_store (store , project );
10488- char * legacy = adr_read_legacy_file (root_path );
10525+ legacy = adr_read_legacy_file (root_path );
1048910526 free (root_path );
10490- if (legacy ) {
10491- if (cbm_store_adr_store (store , project , legacy ) == CBM_STORE_OK ) {
10492- have_adr = (cbm_store_adr_get (store , project , & adr ) == CBM_STORE_OK );
10527+ if (legacy && mcp_project_mutation_try_begin (srv , project )) {
10528+ if (!mcp_request_cancelled (srv )) {
10529+ /* A publisher may have completed before the lease was granted.
10530+ * File-backed stores must reopen after acquisition and trust
10531+ * only that generation. Embedded stores have no publication
10532+ * boundary, so retain their live handle. */
10533+ if (cbm_store_db_path (resolved )) {
10534+ invalidate_cached_store (srv );
10535+ resolved = NULL ;
10536+ store = NULL ;
10537+ resolved = resolve_store_internal (srv , project , true, false, NULL );
10538+ }
10539+ if (resolved ) {
10540+ store = open_adr_store_for_write (srv , resolved , & owned_rw );
10541+ if (store ) {
10542+ have_adr = (cbm_store_adr_get (store , project , & adr ) == CBM_STORE_OK );
10543+ if (!have_adr &&
10544+ cbm_store_adr_store (store , project , legacy ) == CBM_STORE_OK ) {
10545+ have_adr = (cbm_store_adr_get (store , project , & adr ) == CBM_STORE_OK );
10546+ }
10547+ }
10548+ }
1049310549 }
10494- free ( legacy );
10550+ mcp_project_mutation_end ( srv , project );
1049510551 }
1049610552 }
1049710553
@@ -10500,7 +10556,8 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {
1050010556 yyjson_mut_doc_set_root (doc , root_obj );
1050110557
1050210558 bool is_error = false;
10503- if ((strcmp (mode_str , "update" ) == 0 || strcmp (mode_str , "store" ) == 0 ) && content ) {
10559+ const char * adr_content = have_adr ? adr .content : legacy ;
10560+ if (write_request ) {
1050410561 if (cbm_store_adr_store (store , project , content ) == CBM_STORE_OK ) {
1050510562 yyjson_mut_obj_add_str (doc , root_obj , "status" , "updated" );
1050610563 yyjson_mut_obj_add_str (doc , root_obj , "semantics" , "whole_document_replaced" );
@@ -10509,10 +10566,10 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {
1050910566 is_error = true;
1051010567 }
1051110568 } else if (strcmp (mode_str , "sections" ) == 0 ) {
10512- adr_list_sections_from_content (doc , root_obj , have_adr ? adr . content : NULL );
10569+ adr_list_sections_from_content (doc , root_obj , adr_content );
1051310570 } else { /* get */
10514- if (have_adr && adr . content ) {
10515- yyjson_mut_obj_add_strcpy (doc , root_obj , "content" , adr . content );
10571+ if (adr_content ) {
10572+ yyjson_mut_obj_add_strcpy (doc , root_obj , "content" , adr_content );
1051610573 } else {
1051710574 yyjson_mut_obj_add_str (doc , root_obj , "content" , "" );
1051810575 yyjson_mut_obj_add_str (doc , root_obj , "status" , "no_adr" );
@@ -10531,6 +10588,7 @@ static char *handle_manage_adr(cbm_mcp_server_t *srv, const char *args) {
1053110588 if (mutation_held ) {
1053210589 mcp_project_mutation_end (srv , project );
1053310590 }
10591+ free (legacy );
1053410592 free (project );
1053510593 free (mode_str );
1053610594 free (content );
0 commit comments