Skip to content

Commit cb53676

Browse files
authored
Merge pull request DeusData#2392 from DeusData/fix/issue-2277
fix(daemon): name the conflicting daemon and the way out on a build conflict (DeusData#2277)
2 parents d82e1e6 + 19d1444 commit cb53676

5 files changed

Lines changed: 237 additions & 13 deletions

File tree

‎src/daemon/bootstrap.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ typedef struct {
9393
cbm_daemon_runtime_connect_result_t connect_result;
9494
bool daemon_spawned;
9595
char message[CBM_DAEMON_CONFLICT_MESSAGE_SIZE];
96+
/* #2277: filled by the client on a CONFLICT only — who holds the
97+
* endpoint and how to clear it (cbm_daemon_conflict_remedy_format). */
98+
char remedy[CBM_DAEMON_CONFLICT_REMEDY_SIZE];
9699
} cbm_daemon_bootstrap_result_t;
97100

98101
/* A probe distinguishes an absent endpoint from a reserved endpoint whose

‎src/daemon/runtime.c‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2857,6 +2857,73 @@ bool cbm_daemon_runtime_request_status(const cbm_daemon_ipc_endpoint_t *endpoint
28572857
return true;
28582858
}
28592859

2860+
/* "; CBM sessions using it: pids 1, 2 and 3 more" — empty when none are known. */
2861+
static bool remedy_sessions_format(const cbm_daemon_runtime_status_t *active, char *out,
2862+
size_t out_size) {
2863+
out[0] = '\0';
2864+
size_t listed = active->client_count;
2865+
if (listed > CBM_DAEMON_CONTROL_CLIENT_CAP) {
2866+
listed = CBM_DAEMON_CONTROL_CLIENT_CAP;
2867+
}
2868+
size_t used = 0;
2869+
for (size_t i = 0; i < listed; i++) {
2870+
int written = snprintf(out + used, out_size - used, "%s%lu",
2871+
i == 0 ? "; CBM sessions using it: pids " : ", ",
2872+
(unsigned long)active->client_pids[i]);
2873+
if (written < 0 || (size_t)written >= out_size - used) {
2874+
return false;
2875+
}
2876+
used += (size_t)written;
2877+
}
2878+
if (listed > 0 && active->committed_clients > listed) {
2879+
int written = snprintf(out + used, out_size - used, " and %lu more",
2880+
(unsigned long)(active->committed_clients - listed));
2881+
if (written < 0 || (size_t)written >= out_size - used) {
2882+
return false;
2883+
}
2884+
}
2885+
return true;
2886+
}
2887+
2888+
bool cbm_daemon_conflict_remedy_format(const cbm_daemon_runtime_status_t *active, char *out,
2889+
size_t out_size) {
2890+
if (!out || out_size == 0) {
2891+
return false;
2892+
}
2893+
out[0] = '\0';
2894+
static const char install_hint[] =
2895+
"Usually another editor or agent session (for example Codex or Claude Code) "
2896+
"launched a different codebase-memory-mcp install: close it, or point every client "
2897+
"at one binary (compare `which -a codebase-memory-mcp` with each client's MCP "
2898+
"config).";
2899+
int written;
2900+
if (!active) {
2901+
written = snprintf(out, out_size,
2902+
"Run `codebase-memory-mcp daemon status` to see the active daemon and "
2903+
"the sessions using it. %s `codebase-memory-mcp daemon stop` retires "
2904+
"the daemon once no session uses it.",
2905+
install_hint);
2906+
} else {
2907+
char sessions[160];
2908+
if (!remedy_sessions_format(active, sessions, sizeof(sessions))) {
2909+
return false;
2910+
}
2911+
const char *lifetime =
2912+
active->permanent ? "permanent" : "session-managed, exits when its last session closes";
2913+
written = snprintf(out, out_size,
2914+
"Active daemon: pid %lu, version %s, %s%s%s. %s "
2915+
"`codebase-memory-mcp daemon stop` retires the daemon once no session "
2916+
"uses it.",
2917+
(unsigned long)active->daemon_pid, active->semantic_version, lifetime,
2918+
active->stopping ? ", already stopping" : "", sessions, install_hint);
2919+
}
2920+
if (written < 0 || (size_t)written >= out_size) {
2921+
out[0] = '\0';
2922+
return false;
2923+
}
2924+
return true;
2925+
}
2926+
28602927
bool cbm_daemon_runtime_request_stop(const cbm_daemon_ipc_endpoint_t *endpoint,
28612928
const cbm_daemon_build_identity_t *identity,
28622929
uint32_t timeout_ms,

‎src/daemon/runtime.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,17 @@ bool cbm_daemon_runtime_request_stop(const cbm_daemon_ipc_endpoint_t *endpoint,
301301
uint32_t timeout_ms,
302302
cbm_daemon_runtime_stop_result_t *result_out);
303303

304+
/* #2277: the actionable half of a version/build conflict refusal. The
305+
* conflict text itself (cbm_daemon_conflict_format) is wire-validated
306+
* byte-for-byte across generations and must not change, so the remedy is a
307+
* separate, client-side sentence built from the cross-build STATUS probe:
308+
* which daemon holds the endpoint (pid, version, lifetime), which CBM
309+
* sessions keep it alive, and how to clear it. active may be NULL when the
310+
* status probe did not answer. Returns false (out empty) on truncation. */
311+
#define CBM_DAEMON_CONFLICT_REMEDY_SIZE 640U
312+
bool cbm_daemon_conflict_remedy_format(const cbm_daemon_runtime_status_t *active, char *out,
313+
size_t out_size);
314+
304315
/* Performs the complete guarded first-participant handoff, starts listening
305316
* synchronously, then owns both that participant claim and its
306317
* accept/connection threads. All config scalar/text data is copied. endpoint

‎src/main.c‎

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ enum {
4545
MAIN_MAX_PORT = 65536,
4646
MAIN_PATH_CAP = 4096,
4747
MAIN_CONNECT_TIMEOUT_MS = 1000,
48+
/* #2277: the conflict-remedy STATUS probe runs only after a refusal, so it
49+
* gets the same budget as `daemon status`: the daemon hashes the peer's
50+
* (~300 MB) image before answering, which can exceed a 1 s connect budget. */
51+
MAIN_CONFLICT_STATUS_PROBE_TIMEOUT_MS = 3000,
4852
MAIN_STARTUP_TIMEOUT_MS = 10000,
4953
/* Backstop for waiting out a held startup transition — see
5054
* main_local_transition_acquire. Not a budget for healthy contention: a busy
@@ -1698,6 +1702,10 @@ static bool main_semver_newer(const char *candidate, const char *active) {
16981702
return false;
16991703
}
17001704

1705+
/* A conflict refusal plus its remedy (#2277), joined by one space. */
1706+
#define MAIN_CLIENT_FAILURE_DETAIL_CAP \
1707+
(CBM_DAEMON_CONFLICT_MESSAGE_SIZE + CBM_DAEMON_CONFLICT_REMEDY_SIZE + 1U)
1708+
17011709
/* A client that cannot reach the daemon must SAY SO, in the caller's own
17021710
* protocol. An MCP client speaks JSON-RPC over stdout, and the old path
17031711
* returned EXIT_FAILURE having written nothing at all: agents saw a transport
@@ -1721,7 +1729,7 @@ static bool main_semver_newer(const char *candidate, const char *active) {
17211729
* just the one that happened to be fixed first. */
17221730
static void main_report_client_failure(cbm_daemon_process_role_t role, const char *detail) {
17231731
if (cbm_daemon_process_role_requires_client(role)) {
1724-
char escaped[CBM_DAEMON_CONFLICT_MESSAGE_SIZE * 2];
1732+
char escaped[MAIN_CLIENT_FAILURE_DETAIL_CAP * 2];
17251733
size_t out = 0;
17261734
for (size_t i = 0; detail[i] && out + 2 < sizeof(escaped); i++) {
17271735
unsigned char c = (unsigned char)detail[i];
@@ -1746,10 +1754,35 @@ static void main_report_client_failure(cbm_daemon_process_role_t role, const cha
17461754

17471755
static void main_report_client_bootstrap_failure(cbm_daemon_process_role_t role,
17481756
const cbm_daemon_bootstrap_result_t *result) {
1749-
main_report_client_failure(role, (result && result->message[0])
1750-
? result->message
1751-
: "CBM daemon connection failed before the session was "
1752-
"established");
1757+
const char *message = (result && result->message[0])
1758+
? result->message
1759+
: "CBM daemon connection failed before the session was "
1760+
"established";
1761+
char detail[MAIN_CLIENT_FAILURE_DETAIL_CAP];
1762+
(void)snprintf(detail, sizeof(detail), "%s%s%s", message,
1763+
(result && result->remedy[0]) ? " " : "", result ? result->remedy : "");
1764+
main_report_client_failure(role, detail);
1765+
}
1766+
1767+
/* #2277: a conflict is a legitimate refusal (another build owns the account
1768+
* daemon), but it must name that daemon and the way out. known_active is a
1769+
* STATUS answer the caller already holds; without one this probes STATUS
1770+
* (cross-build by design) itself. When nothing answers, the remedy falls
1771+
* back to pointing at `daemon status`. */
1772+
static void main_conflict_remedy_probe(const cbm_daemon_ipc_endpoint_t *endpoint,
1773+
const cbm_daemon_build_identity_t *identity,
1774+
const cbm_daemon_runtime_status_t *known_active, char *out,
1775+
size_t out_size) {
1776+
cbm_daemon_runtime_status_t probed;
1777+
const cbm_daemon_runtime_status_t *active = known_active;
1778+
if (!active && endpoint && identity &&
1779+
cbm_daemon_runtime_request_status(endpoint, identity, MAIN_CONFLICT_STATUS_PROBE_TIMEOUT_MS,
1780+
&probed)) {
1781+
active = &probed;
1782+
}
1783+
if (!cbm_daemon_conflict_remedy_format(active, out, out_size)) {
1784+
out[0] = '\0';
1785+
}
17531786
}
17541787

17551788
/* Client bootstrap with the upgrade policy: a CONFLICT against a PERMANENT
@@ -1766,10 +1799,13 @@ static cbm_daemon_bootstrap_status_t main_client_bootstrap_with_upgrade(
17661799
return status;
17671800
}
17681801
cbm_daemon_runtime_status_t active;
1769-
if (!cbm_daemon_runtime_request_status(config->endpoint, config->identity,
1770-
MAIN_CONNECT_TIMEOUT_MS, &active) ||
1771-
!active.permanent ||
1802+
bool active_known = cbm_daemon_runtime_request_status(config->endpoint, config->identity,
1803+
MAIN_CONNECT_TIMEOUT_MS, &active);
1804+
if (!active_known || !active.permanent ||
17721805
!main_semver_newer(config->identity->semantic_version, active.semantic_version)) {
1806+
main_conflict_remedy_probe(config->endpoint, config->identity,
1807+
active_known ? &active : NULL, result->remedy,
1808+
sizeof(result->remedy));
17731809
return status;
17741810
}
17751811
if (report_lifecycle) {
@@ -1786,9 +1822,16 @@ static cbm_daemon_bootstrap_status_t main_client_bootstrap_with_upgrade(
17861822
!drain.accepted) {
17871823
(void)fprintf(stderr, "codebase-memory-mcp: the active daemon did not accept the "
17881824
"upgrade drain; run `codebase-memory-mcp daemon stop`\n");
1825+
main_conflict_remedy_probe(config->endpoint, config->identity, &active, result->remedy,
1826+
sizeof(result->remedy));
17891827
return status;
17901828
}
1791-
return cbm_daemon_bootstrap_execute(config, result);
1829+
status = cbm_daemon_bootstrap_execute(config, result);
1830+
if (status == CBM_DAEMON_BOOTSTRAP_CONFLICT) {
1831+
main_conflict_remedy_probe(config->endpoint, config->identity, NULL, result->remedy,
1832+
sizeof(result->remedy));
1833+
}
1834+
return status;
17921835
}
17931836

17941837
/* One-shot CLI commands execute through the shared daemon, exactly like MCP
@@ -1825,6 +1868,9 @@ static char *main_local_cli_daemon_execute(const char *tool_name, const char *ar
18251868
(void)fprintf(stderr, "error: %s\n",
18261869
bootstrap.message[0] ? bootstrap.message
18271870
: "no CBM daemon connection for CLI execution");
1871+
if (bootstrap.remedy[0]) {
1872+
(void)fprintf(stderr, "hint: %s\n", bootstrap.remedy);
1873+
}
18281874
return NULL;
18291875
}
18301876
if (bootstrap.daemon_spawned && cbm_log_get_level() <= CBM_LOG_INFO) {
@@ -2870,13 +2916,17 @@ int main(int argc, char **argv) {
28702916
char message[CBM_DAEMON_CONFLICT_MESSAGE_SIZE];
28712917
bool formatted = cohort_status == CBM_VERSION_COHORT_CONFLICT &&
28722918
cbm_daemon_conflict_format(&cohort_conflict, message, sizeof(message));
2919+
char remedy[CBM_DAEMON_CONFLICT_REMEDY_SIZE] = "";
28732920
if (cohort_status == CBM_VERSION_COHORT_CONFLICT) {
28742921
(void)cbm_version_cohort_log_conflict(&cohort_conflict);
2922+
main_conflict_remedy_probe(local_endpoint, &local_identity, NULL, remedy,
2923+
sizeof(remedy));
28752924
}
2876-
(void)fprintf(stderr, "codebase-memory-mcp: %s\n",
2925+
(void)fprintf(stderr, "codebase-memory-mcp: %s%s%s\n",
28772926
formatted ? message
28782927
: "CLI exact-build admission could not be verified; retry "
2879-
"after active CBM operations exit");
2928+
"after active CBM operations exit",
2929+
remedy[0] ? " " : "", remedy);
28802930
goto local_cli_cleanup;
28812931
}
28822932
main_local_maintenance_context_init(&maintenance_context);
@@ -3198,11 +3248,17 @@ int main(int argc, char **argv) {
31983248
bool formatted =
31993249
client_cohort_status == CBM_VERSION_COHORT_CONFLICT &&
32003250
cbm_daemon_conflict_format(&client_cohort_conflict, message, sizeof(message));
3251+
char remedy[CBM_DAEMON_CONFLICT_REMEDY_SIZE] = "";
32013252
if (client_cohort_status == CBM_VERSION_COHORT_CONFLICT) {
32023253
(void)cbm_version_cohort_log_conflict(&client_cohort_conflict);
3254+
/* Hooks fail open on a tight budget; they skip the extra probe. */
3255+
if (role != CBM_DAEMON_PROCESS_HOOK_CLIENT) {
3256+
main_conflict_remedy_probe(endpoint, &identity, NULL, remedy, sizeof(remedy));
3257+
}
32033258
}
3204-
(void)fprintf(stderr, "codebase-memory-mcp: %s\n",
3205-
formatted ? message : "client exact-build admission failed");
3259+
(void)fprintf(stderr, "codebase-memory-mcp: %s%s%s\n",
3260+
formatted ? message : "client exact-build admission failed",
3261+
remedy[0] ? " " : "", remedy);
32063262
if (role == CBM_DAEMON_PROCESS_HOOK_CLIENT &&
32073263
client_cohort_status == CBM_VERSION_COHORT_CONFLICT) {
32083264
main_hook_report_conflicted_daemon(hook_dialect);

‎tests/test_daemon_version.c‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "test_framework.h"
1010

1111
#include "daemon/daemon.h"
12+
#include "daemon/runtime.h"
1213
#include "daemon/service.h"
1314
#include "daemon/service_internal.h"
1415
#include "foundation/compat.h"
@@ -277,6 +278,87 @@ TEST(daemon_hello_version_conflict_exposes_active_and_requested_builds) {
277278
PASS();
278279
}
279280

281+
/* #2277: a version/build conflict refusal must say WHICH daemon holds the
282+
* endpoint and how to clear it. The reporter saw only "close all CBM sessions
283+
* and commands", with no pid, no hint that a live agent session (Codex) had
284+
* launched an older install, and no mention of `daemon stop`. */
285+
static cbm_daemon_runtime_status_t version_test_active_status(const char *version, bool permanent,
286+
uint32_t pid) {
287+
cbm_daemon_runtime_status_t status;
288+
memset(&status, 0, sizeof(status));
289+
status.permanent = permanent;
290+
status.daemon_pid = pid;
291+
(void)snprintf(status.semantic_version, sizeof(status.semantic_version), "%s", version);
292+
(void)snprintf(status.build_fingerprint, sizeof(status.build_fingerprint), "%s", BUILD_A);
293+
return status;
294+
}
295+
296+
TEST(daemon_conflict_remedy_names_session_managed_daemon_and_its_sessions) {
297+
cbm_daemon_runtime_status_t active = version_test_active_status("0.10.3", false, 4242);
298+
active.committed_clients = 2;
299+
active.client_count = 2;
300+
active.client_pids[0] = 5151;
301+
active.client_pids[1] = 6262;
302+
char remedy[CBM_DAEMON_CONFLICT_REMEDY_SIZE];
303+
304+
ASSERT_TRUE(cbm_daemon_conflict_remedy_format(&active, remedy, sizeof(remedy)));
305+
ASSERT_NOT_NULL(strstr(remedy, "pid 4242"));
306+
ASSERT_NOT_NULL(strstr(remedy, "version 0.10.3"));
307+
ASSERT_NOT_NULL(strstr(remedy, "session-managed"));
308+
ASSERT_NOT_NULL(strstr(remedy, "5151, 6262"));
309+
ASSERT_NOT_NULL(strstr(remedy, "which -a codebase-memory-mcp"));
310+
ASSERT_NOT_NULL(strstr(remedy, "codebase-memory-mcp daemon stop"));
311+
PASS();
312+
}
313+
314+
TEST(daemon_conflict_remedy_names_permanent_daemon_and_truncated_session_list) {
315+
cbm_daemon_runtime_status_t active = version_test_active_status("0.12.0", true, 77);
316+
active.committed_clients = CBM_DAEMON_CONTROL_CLIENT_CAP + 3U;
317+
active.client_count = CBM_DAEMON_CONTROL_CLIENT_CAP;
318+
for (uint32_t i = 0; i < CBM_DAEMON_CONTROL_CLIENT_CAP; i++) {
319+
active.client_pids[i] = 1000U + i;
320+
}
321+
char remedy[CBM_DAEMON_CONFLICT_REMEDY_SIZE];
322+
323+
ASSERT_TRUE(cbm_daemon_conflict_remedy_format(&active, remedy, sizeof(remedy)));
324+
ASSERT_NOT_NULL(strstr(remedy, "pid 77"));
325+
ASSERT_NOT_NULL(strstr(remedy, "permanent"));
326+
ASSERT_NOT_NULL(strstr(remedy, "1007 and 3 more"));
327+
ASSERT_NOT_NULL(strstr(remedy, "codebase-memory-mcp daemon stop"));
328+
PASS();
329+
}
330+
331+
TEST(daemon_conflict_remedy_reports_a_stopping_daemon) {
332+
cbm_daemon_runtime_status_t active = version_test_active_status("0.10.3", false, 99);
333+
active.stopping = true;
334+
char remedy[CBM_DAEMON_CONFLICT_REMEDY_SIZE];
335+
336+
ASSERT_TRUE(cbm_daemon_conflict_remedy_format(&active, remedy, sizeof(remedy)));
337+
ASSERT_NOT_NULL(strstr(remedy, "pid 99"));
338+
ASSERT_NOT_NULL(strstr(remedy, "already stopping"));
339+
PASS();
340+
}
341+
342+
TEST(daemon_conflict_remedy_without_status_points_at_daemon_status) {
343+
char remedy[CBM_DAEMON_CONFLICT_REMEDY_SIZE];
344+
345+
ASSERT_TRUE(cbm_daemon_conflict_remedy_format(NULL, remedy, sizeof(remedy)));
346+
ASSERT_NOT_NULL(strstr(remedy, "codebase-memory-mcp daemon status"));
347+
ASSERT_NOT_NULL(strstr(remedy, "which -a codebase-memory-mcp"));
348+
ASSERT_NOT_NULL(strstr(remedy, "codebase-memory-mcp daemon stop"));
349+
PASS();
350+
}
351+
352+
TEST(daemon_conflict_remedy_fails_closed_when_truncated) {
353+
cbm_daemon_runtime_status_t active = version_test_active_status("0.10.3", false, 4242);
354+
char tiny[16];
355+
356+
ASSERT_FALSE(cbm_daemon_conflict_remedy_format(&active, tiny, sizeof(tiny)));
357+
ASSERT_STR_EQ(tiny, "");
358+
ASSERT_FALSE(cbm_daemon_conflict_remedy_format(&active, NULL, 0));
359+
PASS();
360+
}
361+
280362
TEST(daemon_hello_rejects_each_abi_mismatch) {
281363
cbm_daemon_build_identity_t active = version_test_identity("2.4.0", BUILD_A);
282364
cbm_daemon_build_identity_t requested = active;
@@ -613,6 +695,11 @@ SUITE(daemon_version) {
613695
RUN_TEST(daemon_build_fingerprint_hashes_exact_executable_bytes);
614696
RUN_TEST(daemon_hello_accepts_only_the_exact_active_build_identity);
615697
RUN_TEST(daemon_hello_version_conflict_exposes_active_and_requested_builds);
698+
RUN_TEST(daemon_conflict_remedy_names_session_managed_daemon_and_its_sessions);
699+
RUN_TEST(daemon_conflict_remedy_names_permanent_daemon_and_truncated_session_list);
700+
RUN_TEST(daemon_conflict_remedy_reports_a_stopping_daemon);
701+
RUN_TEST(daemon_conflict_remedy_without_status_points_at_daemon_status);
702+
RUN_TEST(daemon_conflict_remedy_fails_closed_when_truncated);
616703
RUN_TEST(daemon_hello_rejects_each_abi_mismatch);
617704
RUN_TEST(daemon_hello_fails_closed_without_an_exact_build_fingerprint);
618705
RUN_TEST(daemon_conflict_log_is_durable_private_and_rotates);

0 commit comments

Comments
 (0)