Skip to content

Commit 0f52d30

Browse files
authored
Merge pull request #2412 from DeusData/fix/activation-unknown-daemon-is-foreign
fix(cli): never stop a daemon whose cache is not confirmed as ours
2 parents 878379a + ed76cf8 commit 0f52d30

3 files changed

Lines changed: 203 additions & 12 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ typedef struct {
226226
static cbm_cli_activation_ops_t g_cli_activation_test_ops;
227227
static bool g_cli_activation_test_ops_set = false;
228228
static const char *g_cli_activation_runtime_parent_for_test = NULL;
229+
static bool g_cli_activation_scope_cache_unreadable_for_test = false;
229230

230231
static void cli_activation_diagnostic(const cbm_cli_activation_ops_t *ops, const char *message) {
231232
const char *diagnostic = message ? message : CLI_ACTIVATION_REFUSED_MESSAGE;
@@ -335,6 +336,10 @@ const char *cbm_cli_activation_runtime_parent_for_test(void) {
335336
return g_cli_activation_runtime_parent_for_test;
336337
}
337338

339+
void cbm_cli_set_activation_scope_cache_unreadable_for_test(bool unreadable) {
340+
g_cli_activation_scope_cache_unreadable_for_test = unreadable;
341+
}
342+
338343
static const char *cli_activation_action_text(cbm_daemon_runtime_activation_action_t action) {
339344
switch (action) {
340345
case CBM_DAEMON_RUNTIME_ACTIVATION_INSTALL:
@@ -541,6 +546,7 @@ typedef enum {
541546
CLI_ACTIVATION_SCOPE_ACTIVE = 0,
542547
CLI_ACTIVATION_SCOPE_NOTHING_TO_REPLACE,
543548
CLI_ACTIVATION_SCOPE_FOREIGN_COHORT,
549+
CLI_ACTIVATION_SCOPE_UNCONFIRMED,
544550
CLI_ACTIVATION_SCOPE_ERROR,
545551
} cli_activation_scope_t;
546552

@@ -566,9 +572,43 @@ static void cli_activation_log_guard_decision(const cli_activation_production_co
566572
* anything at all (a --skip-binary install without an index reset publishes
567573
* nothing), and whose cohort is active. Admission with an immediate deadline
568574
* is the cheapest authoritative read of the active lifetime record: OK means
569-
* the cohort is ours or empty, CONFLICT names the active identity (its cache
570-
* fingerprint is filled for every conflict kind), BUSY means another
571-
* activation holds maintenance and the barrier waits for it as before. */
575+
* the cohort shares this exact identity (cache included) or is empty,
576+
* CONFLICT names the active identity (its cache fingerprint is filled for
577+
* every conflict kind), BUSY means another activation or a session start held
578+
* the group, so the active identity was never read.
579+
*
580+
* Only a POSITIVE match may drain (user decision 2026-09-28, "unknown =
581+
* foreign, keep it"): the daemon's activation-shutdown handler verifies the
582+
* requester's build, never its cache, so this decision is the only thing
583+
* that keeps another HOME's install/update from stopping the account's live
584+
* daemon. A busy group or an unreadable active cache used to fall through to
585+
* "ours" and stop every session behind that daemon. */
586+
static cli_activation_scope_t cli_activation_scope_unconfirmed(
587+
cli_activation_production_context_t *context, const char *scope, const char *reason) {
588+
const char *action = cli_activation_action_text(context->action);
589+
(void)snprintf(context->scope_detail, sizeof(context->scope_detail),
590+
"active cohort ownership unconfirmed (%s); scope=%s; nothing stopped", reason,
591+
scope);
592+
cli_activation_log_guard_decision(context, "unconfirmed_cohort", NULL);
593+
/* Name the daemon being left: a read-only status probe (no cohort, no
594+
* HELLO, no admission) that works across build skew. */
595+
cbm_daemon_runtime_status_t daemon;
596+
if (cbm_daemon_runtime_request_status(context->endpoint, &context->identity,
597+
CLI_ACTIVATION_CONTROL_TIMEOUT_MS, &daemon)) {
598+
printf("Leaving the running CBM daemon untouched (pid %lu, version %s): this %s could "
599+
"not confirm that it serves the cache this %s targets (%s).\n",
600+
(unsigned long)daemon.daemon_pid, daemon.semantic_version, action, action, reason);
601+
} else {
602+
printf("Leaving active CBM sessions untouched: this %s could not confirm that they "
603+
"serve the cache it targets (%s).\n",
604+
action, reason);
605+
}
606+
printf("If it is yours, close its CBM sessions and run `codebase-memory-mcp daemon stop` "
607+
"to stop it yourself.\n");
608+
(void)fflush(stdout);
609+
return CLI_ACTIVATION_SCOPE_UNCONFIRMED;
610+
}
611+
572612
static cli_activation_scope_t cli_activation_resolve_scope(
573613
cli_activation_production_context_t *context) {
574614
const char *runtime_dir = cbm_daemon_ipc_endpoint_runtime_dir(context->endpoint);
@@ -594,20 +634,27 @@ static cli_activation_scope_t cli_activation_resolve_scope(
594634
if (lease) {
595635
return CLI_ACTIVATION_SCOPE_ERROR;
596636
}
637+
if (g_cli_activation_scope_cache_unreadable_for_test) {
638+
conflict.active_cache_fingerprint[0] = '\0';
639+
}
597640
switch (status) {
598641
case CBM_VERSION_COHORT_OK:
599-
case CBM_VERSION_COHORT_BUSY:
600642
return CLI_ACTIVATION_SCOPE_ACTIVE;
643+
case CBM_VERSION_COHORT_BUSY:
644+
return cli_activation_scope_unconfirmed(
645+
context, scope, "the session group was busy, so its cache identity could not be read");
601646
case CBM_VERSION_COHORT_CONFLICT:
602647
break;
603648
default:
604649
return CLI_ACTIVATION_SCOPE_ERROR;
605650
}
606-
if (!conflict.active_cache_fingerprint[0] ||
607-
strcmp(conflict.active_cache_fingerprint, context->cache_fingerprint) == 0) {
608-
/* Same cache namespace, another build or version: that IS the daemon
609-
* this activation replaces. An unreadable active cache stays in scope
610-
* rather than silently exempting a same-namespace daemon. */
651+
if (!conflict.active_cache_fingerprint[0]) {
652+
return cli_activation_scope_unconfirmed(context, scope,
653+
"its cache identity could not be read");
654+
}
655+
if (strcmp(conflict.active_cache_fingerprint, context->cache_fingerprint) == 0) {
656+
/* Positively confirmed: same cache namespace, another build or
657+
* version. That IS the daemon this activation replaces. */
611658
return CLI_ACTIVATION_SCOPE_ACTIVE;
612659
}
613660
(void)snprintf(context->scope_detail, sizeof(context->scope_detail),
@@ -636,9 +683,10 @@ static int cli_activation_production_reserve(void *opaque, cbm_cli_activation_lo
636683
}
637684
if (scope != CLI_ACTIVATION_SCOPE_ACTIVE) {
638685
/* Nothing in the target namespace is being replaced, or the only
639-
* active cohort serves another namespace: hold no maintenance,
640-
* admission, lifetime or startup lock (each wakes or blocks the other
641-
* namespace's sessions) and send no drain request. */
686+
* active cohort serves another namespace, or its ownership could not
687+
* be confirmed: hold no maintenance, admission, lifetime or startup
688+
* lock (each wakes or blocks the other namespace's sessions) and
689+
* send no drain request. */
642690
if (!cli_activation_log_event(context, "quiesce_skipped", context->scope_detail)) {
643691
return CLI_ERR;
644692
}

‎src/cli/cli.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,12 @@ void cbm_cli_set_activation_ops_for_test(const cbm_cli_activation_ops_t *ops);
505505
void cbm_cli_set_activation_runtime_parent_for_test(const char *runtime_parent);
506506
const char *cbm_cli_activation_runtime_parent_for_test(void);
507507

508+
/* Internal integration-test seam: the activation scope read reports the active
509+
* cohort's cache fingerprint as unreadable (blank), exactly what the scope
510+
* decision sees when that field cannot be recovered. false restores the real
511+
* read. Not a command-line or environment override. */
512+
void cbm_cli_set_activation_scope_cache_unreadable_for_test(bool unreadable);
513+
508514
/* ── Subcommands (wired from main.c) ─────────────────────────── */
509515

510516
/* install: copy binary, install skills, install editor MCP configs, ensure PATH.

‎tests/test_cli.c‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
#include <cli/cli.h>
1919
#include <cli/progress_sink.h>
2020
#include <daemon/bootstrap.h>
21+
#include <daemon/ipc.h>
2122
#include <daemon/runtime.h>
2223
#include <daemon/version_cohort.h>
2324
#include <foundation/constants.h>
2425
#include <foundation/log.h>
2526
#include <foundation/platform.h>
27+
#include <foundation/private_file_lock.h>
2628
#include <foundation/sha256.h>
2729
#include <mcp/mcp.h>
2830
#include <mcp/index_supervisor.h>
@@ -2037,6 +2039,139 @@ TEST(cli_install_skip_binary_unchanged_in_host_namespace_quiesces_nothing) {
20372039
ASSERT_TRUE(nothing_drained);
20382040
PASS();
20392041
}
2042+
2043+
/* cli_scope_install with the activation's stdout captured into out. */
2044+
static int cli_scope_install_captured(cli_scope_fixture_t *fixture, const char *home,
2045+
const char *cache, const char *bin_dir, bool skip_binary,
2046+
char *out, size_t out_size) {
2047+
out[0] = '\0';
2048+
FILE *capture = tmpfile();
2049+
int saved_stdout = capture ? dup(STDOUT_FILENO) : -1;
2050+
bool redirected = false;
2051+
if (capture && saved_stdout >= 0) {
2052+
fflush(stdout);
2053+
redirected = dup2(fileno(capture), STDOUT_FILENO) >= 0;
2054+
}
2055+
int rc = redirected ? cli_scope_install(fixture, home, cache, bin_dir, skip_binary) : -1;
2056+
if (redirected) {
2057+
fflush(stdout);
2058+
(void)dup2(saved_stdout, STDOUT_FILENO);
2059+
}
2060+
if (saved_stdout >= 0) {
2061+
close(saved_stdout);
2062+
}
2063+
if (capture) {
2064+
rewind(capture);
2065+
size_t count = fread(out, 1, out_size - 1U, capture);
2066+
out[count] = '\0';
2067+
fclose(capture);
2068+
}
2069+
return rc;
2070+
}
2071+
2072+
/* The notice an activation prints when it leaves a daemon it could not
2073+
* confirm as its own: names the daemon by pid and tells the user how to stop
2074+
* it themselves. Never the "Stopping ..." banner of a drain. */
2075+
static bool cli_scope_kept_notice_names_host(const char *output, pid_t host) {
2076+
char pid_text[48];
2077+
snprintf(pid_text, sizeof(pid_text), "(pid %ld, version %s)", (long)host, CBM_VERSION);
2078+
return strstr(output, "Leaving the running CBM daemon untouched") != NULL &&
2079+
strstr(output, pid_text) != NULL &&
2080+
strstr(output, "codebase-memory-mcp daemon stop") != NULL &&
2081+
strstr(output, "Stopping active CBM sessions") == NULL;
2082+
}
2083+
2084+
/* (e) USER DECISION 2026-09-28, "unknown = foreign, keep it": the scope read
2085+
* reaches the active cohort but cannot read its cache identity. Ownership is
2086+
* unconfirmed, so the sandbox install must leave the host daemon serving,
2087+
* stop no session, and say which daemon it left and how to stop it. The
2088+
* install publishes a binary (no --skip-binary): an activation that replaces
2089+
* nothing never consults the cohort at all (test d). */
2090+
TEST(cli_install_foreign_home_unreadable_cohort_cache_keeps_host_daemon) {
2091+
cli_scope_fixture_t fixture;
2092+
bool ready = cli_scope_fixture_start(&fixture, "unread");
2093+
char foreign_home[512];
2094+
char foreign_cache[576];
2095+
char foreign_bin[640];
2096+
char activation_log[704];
2097+
cli_scope_foreign_paths(&fixture, foreign_home, foreign_cache, foreign_bin, activation_log);
2098+
bool prepared = ready && cbm_mkdir_p(foreign_home, 0700);
2099+
char output[16384];
2100+
output[0] = '\0';
2101+
cbm_cli_set_activation_scope_cache_unreadable_for_test(true);
2102+
int install_rc = prepared
2103+
? cli_scope_install_captured(&fixture, foreign_home, foreign_cache,
2104+
foreign_bin, false, output, sizeof(output))
2105+
: -1;
2106+
cbm_cli_set_activation_scope_cache_unreadable_for_test(false);
2107+
bool host_serving = prepared && cli_scope_host_serving(&fixture);
2108+
bool notice = cli_scope_kept_notice_names_host(output, fixture.host);
2109+
const char *events = read_test_file(activation_log);
2110+
bool completed = events && strstr(events, "\"phase\":\"completed\"") != NULL;
2111+
bool nothing_drained = events && strstr(events, "cohort drained") == NULL;
2112+
int host_exit = cli_scope_fixture_finish(&fixture);
2113+
2114+
ASSERT_TRUE(ready);
2115+
ASSERT_TRUE(host_serving);
2116+
ASSERT_EQ(host_exit, 0);
2117+
ASSERT_TRUE(nothing_drained);
2118+
ASSERT_TRUE(notice);
2119+
ASSERT_EQ(install_rc, 0);
2120+
ASSERT_TRUE(completed);
2121+
PASS();
2122+
}
2123+
2124+
/* (f) Same decision, busy group: another activation holds the cohort
2125+
* maintenance gate, so the scope read cannot reach the active identity at
2126+
* all. Unconfirmed ownership keeps the host daemon; the sandbox install
2127+
* completes without draining anyone. The gate is held through the product's
2128+
* own lock-directory handle on the name version_cohort.c uses. */
2129+
TEST(cli_install_foreign_home_busy_cohort_keeps_host_daemon) {
2130+
cli_scope_fixture_t fixture;
2131+
bool ready = cli_scope_fixture_start(&fixture, "busy");
2132+
char foreign_home[512];
2133+
char foreign_cache[576];
2134+
char foreign_bin[640];
2135+
char activation_log[704];
2136+
cli_scope_foreign_paths(&fixture, foreign_home, foreign_cache, foreign_bin, activation_log);
2137+
cbm_private_lock_directory_t *lock_directory = NULL;
2138+
cbm_private_file_lock_t *maintenance = NULL;
2139+
bool held = ready &&
2140+
cbm_daemon_ipc_private_lock_directory_new(fixture.endpoint, &lock_directory) ==
2141+
CBM_PRIVATE_FILE_LOCK_OK &&
2142+
cbm_private_file_lock_try_acquire(
2143+
lock_directory, "cbm-version-cohort-maintenance-v1.lock",
2144+
CBM_PRIVATE_FILE_LOCK_EX, &maintenance) == CBM_PRIVATE_FILE_LOCK_OK;
2145+
bool prepared = held && cbm_mkdir_p(foreign_home, 0700);
2146+
char output[16384];
2147+
output[0] = '\0';
2148+
int install_rc = prepared
2149+
? cli_scope_install_captured(&fixture, foreign_home, foreign_cache,
2150+
foreign_bin, false, output, sizeof(output))
2151+
: -1;
2152+
bool host_serving = prepared && cli_scope_host_serving(&fixture);
2153+
bool released =
2154+
maintenance && cbm_private_file_lock_release(&maintenance) == CBM_PRIVATE_FILE_LOCK_OK;
2155+
if (lock_directory) {
2156+
cbm_private_lock_directory_close(lock_directory);
2157+
}
2158+
bool notice = cli_scope_kept_notice_names_host(output, fixture.host);
2159+
const char *events = read_test_file(activation_log);
2160+
bool completed = events && strstr(events, "\"phase\":\"completed\"") != NULL;
2161+
bool nothing_drained = events && strstr(events, "cohort drained") == NULL;
2162+
int host_exit = cli_scope_fixture_finish(&fixture);
2163+
2164+
ASSERT_TRUE(ready);
2165+
ASSERT_TRUE(held);
2166+
ASSERT_TRUE(host_serving);
2167+
ASSERT_EQ(host_exit, 0);
2168+
ASSERT_TRUE(nothing_drained);
2169+
ASSERT_TRUE(notice);
2170+
ASSERT_EQ(install_rc, 0);
2171+
ASSERT_TRUE(completed);
2172+
ASSERT_TRUE(released);
2173+
PASS();
2174+
}
20402175
#endif
20412176

20422177
TEST(cli_install_force_quiesces_active_cohort_before_replacing_binary) {
@@ -16504,6 +16639,8 @@ SUITE(cli) {
1650416639
RUN_TEST(cli_install_binary_into_foreign_home_never_drains_host_cohort);
1650516640
RUN_TEST(cli_install_into_host_namespace_still_drains_host_cohort);
1650616641
RUN_TEST(cli_install_skip_binary_unchanged_in_host_namespace_quiesces_nothing);
16642+
RUN_TEST(cli_install_foreign_home_unreadable_cohort_cache_keeps_host_daemon);
16643+
RUN_TEST(cli_install_foreign_home_busy_cohort_keeps_host_daemon);
1650716644
#endif
1650816645
RUN_TEST(cli_install_force_quiesces_active_cohort_before_replacing_binary);
1650916646
RUN_TEST(cli_install_dir_and_skip_config_stage_first_install_safely);

0 commit comments

Comments
 (0)