Skip to content

Commit fb7a635

Browse files
author
SS-42
committed
fix(mcp): keep ADR reads nonblocking
Signed-off-by: SS-42 <noreply@incogni.to>
1 parent 9595822 commit fb7a635

7 files changed

Lines changed: 407 additions & 71 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ JSON arguments can also be piped on stdin. Inline JSON remains accepted for back
616616
| `get_code_snippet` | Read source code for a function by qualified name. |
617617
| `get_architecture` | Codebase overview: languages, packages, routes, hotspots, clusters, ADR. |
618618
| `search_code` | Grep-like text search within indexed project files. |
619-
| `manage_adr` | CRUD for Architecture Decision Records. |
619+
| `manage_adr` | CRUD for Architecture Decision Records. Query modes do not wait behind a same-project reindex; writes remain serialized. |
620620
| `ingest_traces` | Ingest runtime traces to validate HTTP_CALLS edges. |
621621

622622
## Graph Data Model

‎src/daemon/application.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,12 @@ static bool application_session_mutation_begin(void *context, const char *projec
10091009
application_mutation_begin_internal(session->application, session, project, true);
10101010
}
10111011

1012+
static bool application_session_mutation_try_begin(void *context, const char *project) {
1013+
cbm_daemon_application_session_t *session = context;
1014+
return session &&
1015+
application_mutation_begin_internal(session->application, session, project, false);
1016+
}
1017+
10121018
static void application_session_mutation_end(void *context, const char *project) {
10131019
cbm_daemon_application_session_t *session = context;
10141020
if (session) {
@@ -2246,6 +2252,8 @@ static cbm_daemon_runtime_application_session_t *application_session_open(
22462252
cbm_mcp_server_set_index_executor(session->mcp, application_index_execute, session);
22472253
cbm_mcp_server_set_project_mutation_guard(session->mcp, application_session_mutation_begin,
22482254
application_session_mutation_end, session);
2255+
cbm_mcp_server_set_project_mutation_try_guard(session->mcp,
2256+
application_session_mutation_try_begin);
22492257
session->tool_profile = CBM_MCP_TOOL_PROFILE_ALL;
22502258
session->application = application;
22512259
session->client_id = client_id;

‎src/main.c‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,17 +251,21 @@ static bool main_test_worker_project_lock_marker(const main_local_cli_mutation_t
251251
}
252252
#endif
253253

254-
static bool main_local_cli_mutation_begin(void *context, const char *project) {
254+
static bool main_local_cli_mutation_begin_internal(void *context, const char *project, bool wait) {
255255
main_local_cli_mutation_t *mutation = context;
256256
if (!mutation || !mutation->manager || !project || !project[0]) {
257257
return false;
258258
}
259259
for (;;) {
260-
uint64_t now = cbm_now_ms();
261-
uint64_t deadline = now > UINT64_MAX - 100U ? UINT64_MAX : now + 100U;
262260
cbm_project_lock_lease_t *lease = NULL;
263-
cbm_private_file_lock_status_t status =
264-
cbm_project_lock_acquire(mutation->manager, project, deadline, NULL, &lease);
261+
cbm_private_file_lock_status_t status;
262+
if (wait) {
263+
uint64_t now = cbm_now_ms();
264+
uint64_t deadline = now > UINT64_MAX - 100U ? UINT64_MAX : now + 100U;
265+
status = cbm_project_lock_acquire(mutation->manager, project, deadline, NULL, &lease);
266+
} else {
267+
status = cbm_project_lock_try_acquire(mutation->manager, project, &lease);
268+
}
265269
if (status == CBM_PRIVATE_FILE_LOCK_OK && lease) {
266270
main_local_cli_lease_t *held = calloc(1, sizeof(*held));
267271
if (held) {
@@ -292,6 +296,9 @@ static bool main_local_cli_mutation_begin(void *context, const char *project) {
292296
"refuse_mutation");
293297
return false;
294298
}
299+
if (!wait) {
300+
return false;
301+
}
295302
if (mutation->feedback && !mutation->waiting_reported) {
296303
(void)fprintf(mutation->feedback, "Waiting for another CBM mutation of %s...\n",
297304
project);
@@ -301,6 +308,14 @@ static bool main_local_cli_mutation_begin(void *context, const char *project) {
301308
}
302309
}
303310

311+
static bool main_local_cli_mutation_begin(void *context, const char *project) {
312+
return main_local_cli_mutation_begin_internal(context, project, true);
313+
}
314+
315+
static bool main_local_cli_mutation_try_begin(void *context, const char *project) {
316+
return main_local_cli_mutation_begin_internal(context, project, false);
317+
}
318+
304319
static void main_local_cli_mutation_end(void *context, const char *project) {
305320
main_local_cli_mutation_t *mutation = context;
306321
if (!mutation || !project) {
@@ -775,6 +790,8 @@ static int run_cli(int argc, char **argv, cbm_project_lock_manager_t *project_lo
775790
if (project_locks) {
776791
cbm_mcp_server_set_project_mutation_guard(srv, main_local_cli_mutation_begin,
777792
main_local_cli_mutation_end, &mutation);
793+
cbm_mcp_server_set_project_mutation_try_guard(srv,
794+
main_local_cli_mutation_try_begin);
778795
}
779796
}
780797
maintenance_binding_failed = srv && !maintenance_context;

‎src/mcp/mcp.c‎

Lines changed: 112 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
16851694
static 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+
16891703
static 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+
20442064
static 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

21402171
static 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+
1036810421
static 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);

‎src/mcp/mcp.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ typedef char *(*cbm_mcp_index_executor_fn)(void *context, const char *repo_path,
134134
typedef bool (*cbm_mcp_project_mutation_begin_fn)(void *context, const char *project);
135135
typedef void (*cbm_mcp_project_mutation_end_fn)(void *context, const char *project);
136136

137+
/* Nonblocking counterpart for opportunistic writes during a read request. A
138+
* successful call is released through the primary mutation guard's end hook. */
139+
typedef bool (*cbm_mcp_project_mutation_try_begin_fn)(void *context, const char *project);
140+
137141
/* Create an MCP server. store_path is the SQLite database directory. */
138142
cbm_mcp_server_t *cbm_mcp_server_new(const char *store_path);
139143

@@ -180,6 +184,12 @@ void cbm_mcp_server_set_project_mutation_guard(cbm_mcp_server_t *srv,
180184
cbm_mcp_project_mutation_begin_fn begin,
181185
cbm_mcp_project_mutation_end_fn end, void *context);
182186

187+
/* Configure an optional nonblocking acquire for the configured mutation
188+
* guard. If no try hook is set, a read that requires recovery fails with a
189+
* configuration error rather than invoking the blocking begin callback. */
190+
void cbm_mcp_server_set_project_mutation_try_guard(cbm_mcp_server_t *srv,
191+
cbm_mcp_project_mutation_try_begin_fn try_begin);
192+
183193
/* Read one complete MCP message from in. Supports newline-delimited JSON and
184194
* Content-Length framing, including additional headers. Returns 1 on success,
185195
* 0 on clean EOF, and -1 on invalid input or an I/O/allocation error. On

0 commit comments

Comments
 (0)