Skip to content

Commit 013e1f7

Browse files
DeusDataclaude
andcommitted
fix(cli): scope the activation guard to the install-target HOME (amendment)
The rendezvous directory the CLI's activation guard talks to is per OS account (service.h), not per HOME/CBM_CACHE_DIR: every daemon owned by one user meets at the same endpoint, and only the cache fingerprint carried in the cohort identity separates one HOME/CBM_CACHE_DIR namespace from another. `cli_activation_production_reserve` used `cbm_version_cohort_reserve_for_mutation`, which treats any lifetime- lock holder at that shared endpoint as the daemon this activation must drain, with no cache-fingerprint awareness at all. An `install` run against a second HOME (a sandbox, a second profile, `--skip-binary` into an alternate CBM_CACHE_DIR) therefore reached the LIVE host daemon at the shared endpoint and drained it — disconnecting every one of the host's real MCP clients — even though nothing the sandboxed install touched belonged to the host's cache namespace. The fix adds `cli_activation_resolve_scope`, a non-blocking probe via the already cache-fingerprint-aware `cbm_version_cohort_acquire`, run before the blind drain path. It distinguishes three cases from the resolved target of THIS activation: - nothing to quiesce at all: a `--skip-binary` install that also resets no index publishes nothing, so no session anywhere needs to stop (`quiesce_required` on `cli_activation_guard_scoped`, plumbed from `cbm_cmd_install`'s own binary/index-reset decision); - the active cohort's cache fingerprint doesn't match this activation's target cache: a foreign namespace, left untouched; - same cache fingerprint (or no active cohort): this activation really does replace that daemon, so the existing drain/quiesce path runs unchanged, including the barrier wait when another activation already holds maintenance. Verified against a real forked daemon (a live runtime service with one committed client) standing in for the host: an install into a foreign HOME/CBM_CACHE_DIR (`--skip-binary` and a full binary install) leaves that daemon serving with its client still attached, while an install that targets the host's own namespace still drains it and the activation audit still names the client it disconnected. New/updated tests in tests/test_cli.c (cli suite): - cli_install_skip_binary_into_foreign_home_never_drains_host_cohort - cli_install_binary_into_foreign_home_never_drains_host_cohort - cli_install_into_host_namespace_still_drains_host_cohort (regression) - cli_install_skip_binary_unchanged_in_host_namespace_quiesces_nothing - cli_activation_quiesce_does_not_wait_on_bootstrap_startup updated to carry a real cache fingerprint for its participant, now that scope is fingerprint-gated src/mcp/index_supervisor.{c,h}: CBM_CLI_ENABLE_TEST_API-gated setter so the fixture's forked daemon can carry the same build fingerprint the guard captures from the supervisor. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 161df2b commit 013e1f7

4 files changed

Lines changed: 576 additions & 14 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 150 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,12 @@ typedef struct {
214214
bool cleanup_ok;
215215
bool original_cache_environment_present;
216216
bool cache_environment_overridden;
217+
/* Scope: what THIS activation replaces decides whether any session must
218+
* be quiesced at all, and the target cache namespace decides WHICH
219+
* cohort. A skipped coordination holds no lock and drains nobody. */
220+
bool quiesce_required;
221+
bool coordination_skipped;
222+
char scope_detail[CBM_SZ_1K];
217223
} cli_activation_production_context_t;
218224

219225
static cbm_cli_activation_ops_t g_cli_activation_test_ops;
@@ -528,6 +534,91 @@ static void cli_activation_release_cleanup_lease(cli_activation_production_conte
528534
}
529535
}
530536

537+
typedef enum {
538+
CLI_ACTIVATION_SCOPE_ACTIVE = 0,
539+
CLI_ACTIVATION_SCOPE_NOTHING_TO_REPLACE,
540+
CLI_ACTIVATION_SCOPE_FOREIGN_COHORT,
541+
CLI_ACTIVATION_SCOPE_ERROR,
542+
} cli_activation_scope_t;
543+
544+
static void cli_activation_log_guard_decision(const cli_activation_production_context_t *context,
545+
const char *decision, const char *active_cache) {
546+
char clients[32];
547+
(void)snprintf(clients, sizeof(clients), "%llu",
548+
(unsigned long long)context->daemon_result.active_clients);
549+
const char *scope = cbm_daemon_ipc_endpoint_runtime_dir(context->endpoint);
550+
cbm_log_info("activation.guard", "scope", scope ? scope : "", "cache",
551+
context->cache_fingerprint, "clients", clients, "decision", decision,
552+
"active_cache", active_cache ? active_cache : "");
553+
}
554+
555+
/* Whose sessions does this activation have to stop? The rendezvous directory
556+
* is per OS account (service.h), so the host daemon of another HOME /
557+
* CBM_CACHE_DIR meets this activation at the very same endpoint; only the
558+
* cache fingerprint in the cohort identity separates the namespaces. An
559+
* `install` into a sandbox HOME used to drain the host daemon and every MCP
560+
* client behind it although nothing it touched belonged to the host.
561+
*
562+
* Two questions, answered from the activation's own target: does it replace
563+
* anything at all (a --skip-binary install without an index reset publishes
564+
* nothing), and whose cohort is active. Admission with an immediate deadline
565+
* is the cheapest authoritative read of the active lifetime record: OK means
566+
* the cohort is ours or empty, CONFLICT names the active identity (its cache
567+
* fingerprint is filled for every conflict kind), BUSY means another
568+
* activation holds maintenance and the barrier waits for it as before. */
569+
static cli_activation_scope_t cli_activation_resolve_scope(
570+
cli_activation_production_context_t *context) {
571+
const char *runtime_dir = cbm_daemon_ipc_endpoint_runtime_dir(context->endpoint);
572+
const char *scope = runtime_dir ? runtime_dir : "";
573+
const char *action = cli_activation_action_text(context->action);
574+
if (!context->quiesce_required) {
575+
(void)snprintf(context->scope_detail, sizeof(context->scope_detail),
576+
"nothing to quiesce: published binary and indexes unchanged; scope=%s",
577+
scope);
578+
cli_activation_log_guard_decision(context, "nothing_to_replace", NULL);
579+
printf("No CBM session needs to stop for %s: the published binary and indexes are "
580+
"unchanged.\n",
581+
action);
582+
(void)fflush(stdout);
583+
return CLI_ACTIVATION_SCOPE_NOTHING_TO_REPLACE;
584+
}
585+
cbm_version_cohort_lease_t *lease = NULL;
586+
cbm_daemon_conflict_t conflict;
587+
memset(&conflict, 0, sizeof(conflict));
588+
cbm_version_cohort_status_t status = cbm_version_cohort_acquire(
589+
context->cohort_manager, &context->identity, cbm_now_ms(), &lease, &conflict);
590+
cli_activation_release_cleanup_lease(context, &lease);
591+
if (lease) {
592+
return CLI_ACTIVATION_SCOPE_ERROR;
593+
}
594+
switch (status) {
595+
case CBM_VERSION_COHORT_OK:
596+
case CBM_VERSION_COHORT_BUSY:
597+
return CLI_ACTIVATION_SCOPE_ACTIVE;
598+
case CBM_VERSION_COHORT_CONFLICT:
599+
break;
600+
default:
601+
return CLI_ACTIVATION_SCOPE_ERROR;
602+
}
603+
if (!conflict.active_cache_fingerprint[0] ||
604+
strcmp(conflict.active_cache_fingerprint, context->cache_fingerprint) == 0) {
605+
/* Same cache namespace, another build or version: that IS the daemon
606+
* this activation replaces. An unreadable active cache stays in scope
607+
* rather than silently exempting a same-namespace daemon. */
608+
return CLI_ACTIVATION_SCOPE_ACTIVE;
609+
}
610+
(void)snprintf(context->scope_detail, sizeof(context->scope_detail),
611+
"active cohort serves another cache namespace (%.12s), this %s targets "
612+
"%.12s; scope=%s; nothing stopped",
613+
conflict.active_cache_fingerprint, action, context->cache_fingerprint, scope);
614+
cli_activation_log_guard_decision(context, "foreign_cohort", conflict.active_cache_fingerprint);
615+
printf("Leaving active CBM sessions untouched: they serve another cache namespace than "
616+
"this %s targets.\n",
617+
action);
618+
(void)fflush(stdout);
619+
return CLI_ACTIVATION_SCOPE_FOREIGN_COHORT;
620+
}
621+
531622
static int cli_activation_production_reserve(void *opaque, cbm_cli_activation_lock_t *lease_out) {
532623
cli_activation_production_context_t *context = opaque;
533624
if (lease_out) {
@@ -536,6 +627,26 @@ static int cli_activation_production_reserve(void *opaque, cbm_cli_activation_lo
536627
if (!context || !context->cohort_manager || !lease_out) {
537628
return CLI_ERR;
538629
}
630+
cli_activation_scope_t scope = cli_activation_resolve_scope(context);
631+
if (scope == CLI_ACTIVATION_SCOPE_ERROR) {
632+
return CLI_ERR;
633+
}
634+
if (scope != CLI_ACTIVATION_SCOPE_ACTIVE) {
635+
/* Nothing in the target namespace is being replaced, or the only
636+
* active cohort serves another namespace: hold no maintenance,
637+
* admission, lifetime or startup lock (each wakes or blocks the other
638+
* namespace's sessions) and send no drain request. */
639+
if (!cli_activation_log_event(context, "quiesce_skipped", context->scope_detail)) {
640+
return CLI_ERR;
641+
}
642+
context->coordination_skipped = true;
643+
context->mutation_authorized = true;
644+
*lease_out = context;
645+
return 1;
646+
}
647+
printf("Stopping active CBM sessions and operations for %s...\n",
648+
cli_activation_action_text(context->action));
649+
(void)fflush(stdout);
539650
cbm_version_cohort_quiesce_result_t quiesce = CBM_VERSION_COHORT_QUIESCE_NOT_NEEDED;
540651
cbm_version_cohort_lease_t *lease = NULL;
541652
context->control_deadline_ms = cli_activation_deadline_after(CLI_ACTIVATION_CONTROL_TIMEOUT_MS);
@@ -582,6 +693,8 @@ static int cli_activation_production_reserve(void *opaque, cbm_cli_activation_lo
582693
}
583694

584695
context->cohort_lease = lease;
696+
cli_activation_log_guard_decision(
697+
context, context->shutdown_requested ? "cohort_drained" : "no_active_cohort", NULL);
585698
if (!cli_activation_log_event(context, "daemon_stopped",
586699
context->shutdown_requested ? "cohort drained"
587700
: "no active cohort")) {
@@ -600,6 +713,13 @@ static void cli_activation_production_release(void *opaque, cbm_cli_activation_l
600713
if (!context) {
601714
return;
602715
}
716+
if (context->coordination_skipped) {
717+
/* Nothing was held: the token is the context itself. */
718+
if (lease != (cbm_cli_activation_lock_t)context) {
719+
context->cleanup_ok = false;
720+
}
721+
return;
722+
}
603723
/* Global release order is the inverse of acquisition: startup first,
604724
* then lifetime/admission/maintenance through the cohort lease. */
605725
if (context->startup_lock) {
@@ -626,9 +746,11 @@ static void cli_activation_production_diagnostic(void *opaque, const char *messa
626746
static bool cli_activation_production_context_init(cli_activation_production_context_t *context,
627747
cbm_daemon_runtime_activation_action_t action,
628748
const char *target_version,
629-
const char *target_build) {
749+
const char *target_build,
750+
bool quiesce_required) {
630751
memset(context, 0, sizeof(*context));
631752
context->action = action;
753+
context->quiesce_required = quiesce_required;
632754
context->target_version = target_version;
633755
context->target_build = target_build;
634756
context->cleanup_ok = true;
@@ -735,23 +857,26 @@ static void cli_activation_production_context_close(cli_activation_production_co
735857
context->original_cache_environment = NULL;
736858
}
737859

738-
static int cli_activation_guard(cbm_daemon_runtime_activation_action_t action,
739-
const char *target_version, const char *target_build,
740-
cbm_cli_activation_mutation_fn mutation, void *mutation_context) {
860+
/* quiesce_required: false when the activation publishes no binary and resets
861+
* no index (a config-only install) — nothing running is then replaced, and
862+
* no session is stopped for it. */
863+
static int cli_activation_guard_scoped(cbm_daemon_runtime_activation_action_t action,
864+
const char *target_version, const char *target_build,
865+
bool quiesce_required,
866+
cbm_cli_activation_mutation_fn mutation,
867+
void *mutation_context) {
741868
if (g_cli_activation_test_ops_set) {
742869
return cbm_cli_activation_guard_with_ops(&g_cli_activation_test_ops, mutation,
743870
mutation_context);
744871
}
745872

746873
cli_activation_production_context_t context;
747-
if (!cli_activation_production_context_init(&context, action, target_version, target_build)) {
874+
if (!cli_activation_production_context_init(&context, action, target_version, target_build,
875+
quiesce_required)) {
748876
cli_activation_production_context_close(&context);
749877
cli_activation_production_diagnostic(NULL, CLI_ACTIVATION_REFUSED_MESSAGE);
750878
return CLI_TRUE;
751879
}
752-
printf("Stopping active CBM sessions and operations for %s...\n",
753-
cli_activation_action_text(action));
754-
(void)fflush(stdout);
755880
if (!cli_activation_log_event(&context, "requested", NULL)) {
756881
cli_activation_production_context_close(&context);
757882
(void)fprintf(stderr, "error: activation request could not be recorded safely; "
@@ -793,6 +918,13 @@ static int cli_activation_guard(cbm_daemon_runtime_activation_action_t action,
793918
return rc;
794919
}
795920

921+
static int cli_activation_guard(cbm_daemon_runtime_activation_action_t action,
922+
const char *target_version, const char *target_build,
923+
cbm_cli_activation_mutation_fn mutation, void *mutation_context) {
924+
return cli_activation_guard_scoped(action, target_version, target_build, true, mutation,
925+
mutation_context);
926+
}
927+
796928
/* Tar header field offsets */
797929
#define TAR_NAME_LEN 101 /* filename field: bytes 0-99 + NUL */
798930
#define TAR_SIZE_OFFSET 124 /* octal size field offset */
@@ -10756,11 +10888,16 @@ int cbm_cmd_install(int argc, char **argv) {
1075610888
.force = force,
1075710889
.dry_run = dry_run,
1075810890
};
10759-
int activation_rc =
10760-
dry_run ? cli_install_activate(&activation)
10761-
: cli_activation_guard(CBM_DAEMON_RUNTIME_ACTIVATION_INSTALL, CBM_VERSION,
10762-
has_binary_validator ? binary_validator.fingerprint : NULL,
10763-
cli_install_activate, &activation);
10891+
/* What this install replaces decides whether any session must stop: with
10892+
* the published binary untouched (--skip-binary, or an externally managed
10893+
* binary) and no index reset, agent configs are refreshed while every
10894+
* session stays up. */
10895+
bool quiesce_required = has_binary_validator || delete_indexes;
10896+
int activation_rc = dry_run ? cli_install_activate(&activation)
10897+
: cli_activation_guard_scoped(
10898+
CBM_DAEMON_RUNTIME_ACTIVATION_INSTALL, CBM_VERSION,
10899+
has_binary_validator ? binary_validator.fingerprint : NULL,
10900+
quiesce_required, cli_install_activate, &activation);
1076410901
if (activation.binary_transaction) {
1076510902
(void)cli_activation_transaction_abort(&activation.binary_transaction);
1076610903
}

‎src/mcp/index_supervisor.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ const char *cbm_index_supervisor_build_fingerprint(void) {
149149
return g_build_fingerprint[0] ? g_build_fingerprint : NULL;
150150
}
151151

152+
#if defined(CBM_CLI_ENABLE_TEST_API)
153+
void cbm_index_supervisor_set_build_fingerprint_for_test(const char *fingerprint) {
154+
if (!fingerprint || !worker_fingerprint_valid(fingerprint)) {
155+
return;
156+
}
157+
g_build_fingerprint_capture_attempted = true;
158+
(void)snprintf(g_build_fingerprint, sizeof(g_build_fingerprint), "%s", fingerprint);
159+
}
160+
#endif
161+
152162
static bool worker_fingerprint_valid(const char *fingerprint) {
153163
if (!fingerprint || strlen(fingerprint) != CBM_INDEX_WORKER_BUILD_FINGERPRINT_LENGTH) {
154164
return false;

‎src/mcp/index_supervisor.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ void cbm_index_worker_log_begin(const char *args_json, const char *repo_path);
7676
bool cbm_index_supervisor_capture_build_fingerprint(void);
7777
const char *cbm_index_supervisor_build_fingerprint(void);
7878

79+
#if defined(CBM_CLI_ENABLE_TEST_API)
80+
/* Test-only: the runner captures a synthetic stub (see the capture seam) so
81+
* spawned workers start instantly, but a daemon runtime service can only carry
82+
* the REAL image hash. A fixture that runs a real daemon and drives the
83+
* production activation guard against it aligns the captured value with that
84+
* hash for its duration and restores the previous value afterwards. Invalid
85+
* input is ignored. Never compiled into production. */
86+
void cbm_index_supervisor_set_build_fingerprint_for_test(const char *fingerprint);
87+
#endif
88+
7989
typedef struct {
8090
const char *expected_build_fingerprint;
8191
const char *args_json;

0 commit comments

Comments
 (0)