Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(cli): stop blaming sessions for a failure that has none (#1537)

Martin Vogelff853ac14b15
diff --git a/src/cli/cli.c b/src/cli/cli.c
index f85a55f25..ea86a3024 100644
--- a/src/cli/cli.c
+++ b/src/cli/cli.c
@@ -152,17 +152,31 @@ int cbm_cli_exit_status_after_maintenance(int exit_status, bool maintenance_canc
return maintenance_cancelled && exit_status == EXIT_SUCCESS ? EXIT_FAILURE : exit_status;
}
-/* #1537: the refusal is correct, but "sessions could not be stopped" gives the
- * reader nothing to act on — which sessions? The daemon already knows every
- * client pid and `daemon status` prints them, so name that remedy here rather
- * than leaving people to guess (one reporter uninstalled first and still hit
- * this, with no way to see what was holding on). */
-static const char CLI_ACTIVATION_REFUSED_MESSAGE[] =
+/* #1537. Two very different failures reached this one message: the cohort was
+ * BUSY (real sessions are running — the reader can close them), or the
+ * reservation failed outright (lock I/O, stale coordination state, permissions
+ * — nothing the reader can close, because nothing is running). Reporting both
+ * as "active sessions" sent someone hunting processes that a reboot proved did
+ * not exist.
+ *
+ * The remedy line must also survive the situation it prints in: an install or
+ * a retry after `uninstall` has no cbm on PATH, so "run codebase-memory-mcp
+ * daemon status" was advice the reader could not follow at exactly the moment
+ * they needed it. Each message now names its own condition and stays runnable. */
+static const char CLI_ACTIVATION_BUSY_MESSAGE[] =
"error: active CBM sessions and operations could not be stopped safely; "
"no activation was committed.\n"
- "error: run 'codebase-memory-mcp daemon status' to list the client "
- "processes still holding the daemon (MCP servers started by your editor or "
- "agent are the usual holders), close them, then retry.";
+ "error: something is still using CBM. If an editor or agent is running an "
+ "MCP server, close it and retry; 'codebase-memory-mcp daemon status' lists "
+ "the holders when a cbm binary is still installed.";
+static const char CLI_ACTIVATION_REFUSED_MESSAGE[] =
+ "error: activation could not reserve exclusive access; no activation was "
+ "committed.\n"
+ "error: this is NOT a running-session problem — the reservation itself "
+ "failed (coordination lock, leftover state, or permissions). Nothing needs "
+ "to be closed. Check the errors above, and report this with the output of "
+ "'ls -la \"${CBM_CACHE_DIR:-$HOME/.cache/codebase-memory-mcp}\"' if it "
+ "persists.";
static const char CLI_ACTIVATION_PARTIAL_MESSAGE[] =
"error: activation stopped after one or more agent configuration or "
"cleanup operations failed; the published/current executable was kept, "
@@ -211,7 +225,11 @@ static void cli_activation_diagnostic(const cbm_cli_activation_ops_t *ops, const
* stop/reservation failures, which record no refusal note. */
char attributed[CBM_SZ_1K];
const char *note = cbm_activation_transaction_refusal_note();
- if (diagnostic == CLI_ACTIVATION_REFUSED_MESSAGE && note && note[0]) {
+ /* A recorded refusal is more specific than EITHER generic message, so it
+ * wins over both the busy and the reservation-failure wording. */
+ if ((diagnostic == CLI_ACTIVATION_REFUSED_MESSAGE ||
+ diagnostic == CLI_ACTIVATION_BUSY_MESSAGE) &&
+ note && note[0]) {
(void)snprintf(attributed, sizeof(attributed),
"error: activation was refused by a filesystem safety check before any "
"change was made: %s\n"
@@ -245,7 +263,12 @@ int cbm_cli_activation_guard_with_ops(const cbm_cli_activation_ops_t *ops,
if (mutation_lease) {
ops->mutation_lease_release(ops->context, mutation_lease);
}
- cli_activation_diagnostic(ops, CLI_ACTIVATION_REFUSED_MESSAGE);
+ /* 0 means the cohort was BUSY: real sessions hold it and closing them
+ * is the remedy. Anything else is the reservation itself failing, and
+ * telling that reader to close sessions sends them after processes
+ * that do not exist (#1537). */
+ cli_activation_diagnostic(ops, reserve_status == 0 ? CLI_ACTIVATION_BUSY_MESSAGE
+ : CLI_ACTIVATION_REFUSED_MESSAGE);
return CLI_TRUE;
}
diff --git a/tests/test_cli.c b/tests/test_cli.c
index a6628f667..57e37ba69 100644
--- a/tests/test_cli.c
+++ b/tests/test_cli.c
@@ -682,6 +682,40 @@ TEST(cli_activation_refusal_note_reaches_diagnostic_issue1416) {
PASS();
}
+/* #1537: a reservation that FAILED (lock I/O, leftover coordination state,
+ * permissions) is not a reservation that was BUSY. Reporting both as "active
+ * CBM sessions could not be stopped" sent a reporter hunting processes that a
+ * reboot proved did not exist — and the remedy we printed told them to run a
+ * cbm binary they had just uninstalled. Each condition now names itself. */
+TEST(cli_activation_distinguishes_busy_from_reservation_failure_issue1537) {
+ cbm_activation_transaction_note_refusal_for_testing(NULL, 0UL);
+
+ /* BUSY (0): real sessions hold the cohort — closing something is the fix. */
+ cli_activation_fake_t busy = {
+ .participants_active = true,
+ .mutation_reserve_result = 0,
+ };
+ cbm_cli_activation_ops_t ops = {
+ .context = &busy,
+ .reserve_for_mutation = cli_activation_fake_reserve_mutation,
+ .mutation_lease_release = cli_activation_fake_release_mutation,
+ .visible_diagnostic = cli_activation_fake_diagnostic,
+ };
+ ASSERT_EQ(cbm_cli_activation_guard_with_ops(&ops, cli_activation_fake_mutation, &busy), 1);
+ ASSERT_NOT_NULL(strstr(busy.diagnostic, "could not be stopped safely"));
+
+ /* FAILURE (-1): nothing is running, so the reader must not be told to close
+ * sessions — and must not be handed a command that may not be installed. */
+ cli_activation_fake_t failed = {
+ .mutation_reserve_result = -1,
+ };
+ ops.context = &failed;
+ ASSERT_EQ(cbm_cli_activation_guard_with_ops(&ops, cli_activation_fake_mutation, &failed), 1);
+ ASSERT_NOT_NULL(strstr(failed.diagnostic, "NOT a running-session problem"));
+ ASSERT_NULL(strstr(failed.diagnostic, "could not be stopped safely"));
+ PASS();
+}
+
TEST(cli_activation_refuses_unsafe_cohort_reservation) {
cli_activation_fake_t fake = {
.mutation_reserve_result = -1,
@@ -12138,6 +12172,7 @@ SUITE(cli) {
RUN_TEST(cli_activation_quiesces_active_cohort_before_mutation);
RUN_TEST(cli_activation_refuses_when_cohort_does_not_drain);
RUN_TEST(cli_activation_refusal_note_reaches_diagnostic_issue1416);
+ RUN_TEST(cli_activation_distinguishes_busy_from_reservation_failure_issue1537);
RUN_TEST(cli_activation_refuses_unsafe_cohort_reservation);
RUN_TEST(cli_activation_releases_maintenance_lease_after_success);
RUN_TEST(cli_activation_releases_maintenance_lease_when_mutation_fails);