@@ -215,6 +215,12 @@ typedef struct {
215215 bool cleanup_ok;
216216 bool original_cache_environment_present;
217217 bool cache_environment_overridden;
218+ /* Scope: what THIS activation replaces decides whether any session must
219+ * be quiesced at all, and the target cache namespace decides WHICH
220+ * cohort. A skipped coordination holds no lock and drains nobody. */
221+ bool quiesce_required;
222+ bool coordination_skipped;
223+ char scope_detail[CBM_SZ_1K];
218224} cli_activation_production_context_t;
219225
220226static cbm_cli_activation_ops_t g_cli_activation_test_ops;
@@ -531,6 +537,91 @@ static void cli_activation_release_cleanup_lease(cli_activation_production_conte
531537 }
532538}
533539
540+ typedef enum {
541+ CLI_ACTIVATION_SCOPE_ACTIVE = 0,
542+ CLI_ACTIVATION_SCOPE_NOTHING_TO_REPLACE,
543+ CLI_ACTIVATION_SCOPE_FOREIGN_COHORT,
544+ CLI_ACTIVATION_SCOPE_ERROR,
545+ } cli_activation_scope_t;
546+
547+ static void cli_activation_log_guard_decision(const cli_activation_production_context_t *context,
548+ const char *decision, const char *active_cache) {
549+ char clients[32];
550+ (void)snprintf(clients, sizeof(clients), "%llu",
551+ (unsigned long long)context->daemon_result.active_clients);
552+ const char *scope = cbm_daemon_ipc_endpoint_runtime_dir(context->endpoint);
553+ cbm_log_info("activation.guard", "scope", scope ? scope : "", "cache",
554+ context->cache_fingerprint, "clients", clients, "decision", decision,
555+ "active_cache", active_cache ? active_cache : "");
556+ }
557+
558+ /* Whose sessions does this activation have to stop? The rendezvous directory
559+ * is per OS account (service.h), so the host daemon of another HOME /
560+ * CBM_CACHE_DIR meets this activation at the very same endpoint; only the
561+ * cache fingerprint in the cohort identity separates the namespaces. An
562+ * `install` into a sandbox HOME used to drain the host daemon and every MCP
563+ * client behind it although nothing it touched belonged to the host.
564+ *
565+ * Two questions, answered from the activation's own target: does it replace
566+ * anything at all (a --skip-binary install without an index reset publishes
567+ * nothing), and whose cohort is active. Admission with an immediate deadline
568+ * 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. */
572+ static cli_activation_scope_t cli_activation_resolve_scope(
573+ cli_activation_production_context_t *context) {
574+ const char *runtime_dir = cbm_daemon_ipc_endpoint_runtime_dir(context->endpoint);
575+ const char *scope = runtime_dir ? runtime_dir : "";
576+ const char *action = cli_activation_action_text(context->action);
577+ if (!context->quiesce_required) {
578+ (void)snprintf(context->scope_detail, sizeof(context->scope_detail),
579+ "nothing to quiesce: published binary and indexes unchanged; scope=%s",
580+ scope);
581+ cli_activation_log_guard_decision(context, "nothing_to_replace", NULL);
582+ printf("No CBM session needs to stop for %s: the published binary and indexes are "
583+ "unchanged.\n",
584+ action);
585+ (void)fflush(stdout);
586+ return CLI_ACTIVATION_SCOPE_NOTHING_TO_REPLACE;
587+ }
588+ cbm_version_cohort_lease_t *lease = NULL;
589+ cbm_daemon_conflict_t conflict;
590+ memset(&conflict, 0, sizeof(conflict));
591+ cbm_version_cohort_status_t status = cbm_version_cohort_acquire(
592+ context->cohort_manager, &context->identity, cbm_now_ms(), &lease, &conflict);
593+ cli_activation_release_cleanup_lease(context, &lease);
594+ if (lease) {
595+ return CLI_ACTIVATION_SCOPE_ERROR;
596+ }
597+ switch (status) {
598+ case CBM_VERSION_COHORT_OK:
599+ case CBM_VERSION_COHORT_BUSY:
600+ return CLI_ACTIVATION_SCOPE_ACTIVE;
601+ case CBM_VERSION_COHORT_CONFLICT:
602+ break;
603+ default:
604+ return CLI_ACTIVATION_SCOPE_ERROR;
605+ }
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. */
611+ return CLI_ACTIVATION_SCOPE_ACTIVE;
612+ }
613+ (void)snprintf(context->scope_detail, sizeof(context->scope_detail),
614+ "active cohort serves another cache namespace (%.12s), this %s targets "
615+ "%.12s; scope=%s; nothing stopped",
616+ conflict.active_cache_fingerprint, action, context->cache_fingerprint, scope);
617+ cli_activation_log_guard_decision(context, "foreign_cohort", conflict.active_cache_fingerprint);
618+ printf("Leaving active CBM sessions untouched: they serve another cache namespace than "
619+ "this %s targets.\n",
620+ action);
621+ (void)fflush(stdout);
622+ return CLI_ACTIVATION_SCOPE_FOREIGN_COHORT;
623+ }
624+
534625static int cli_activation_production_reserve(void *opaque, cbm_cli_activation_lock_t *lease_out) {
535626 cli_activation_production_context_t *context = opaque;
536627 if (lease_out) {
@@ -539,6 +630,26 @@ static int cli_activation_production_reserve(void *opaque, cbm_cli_activation_lo
539630 if (!context || !context->cohort_manager || !lease_out) {
540631 return CLI_ERR;
541632 }
633+ cli_activation_scope_t scope = cli_activation_resolve_scope(context);
634+ if (scope == CLI_ACTIVATION_SCOPE_ERROR) {
635+ return CLI_ERR;
636+ }
637+ if (scope != CLI_ACTIVATION_SCOPE_ACTIVE) {
638+ /* 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. */
642+ if (!cli_activation_log_event(context, "quiesce_skipped", context->scope_detail)) {
643+ return CLI_ERR;
644+ }
645+ context->coordination_skipped = true;
646+ context->mutation_authorized = true;
647+ *lease_out = context;
648+ return 1;
649+ }
650+ printf("Stopping active CBM sessions and operations for %s...\n",
651+ cli_activation_action_text(context->action));
652+ (void)fflush(stdout);
542653 cbm_version_cohort_quiesce_result_t quiesce = CBM_VERSION_COHORT_QUIESCE_NOT_NEEDED;
543654 cbm_version_cohort_lease_t *lease = NULL;
544655 context->control_deadline_ms = cli_activation_deadline_after(CLI_ACTIVATION_CONTROL_TIMEOUT_MS);
@@ -585,6 +696,8 @@ static int cli_activation_production_reserve(void *opaque, cbm_cli_activation_lo
585696 }
586697
587698 context->cohort_lease = lease;
699+ cli_activation_log_guard_decision(
700+ context, context->shutdown_requested ? "cohort_drained" : "no_active_cohort", NULL);
588701 if (!cli_activation_log_event(context, "daemon_stopped",
589702 context->shutdown_requested ? "cohort drained"
590703 : "no active cohort")) {
@@ -603,6 +716,13 @@ static void cli_activation_production_release(void *opaque, cbm_cli_activation_l
603716 if (!context) {
604717 return;
605718 }
719+ if (context->coordination_skipped) {
720+ /* Nothing was held: the token is the context itself. */
721+ if (lease != (cbm_cli_activation_lock_t)context) {
722+ context->cleanup_ok = false;
723+ }
724+ return;
725+ }
606726 /* Global release order is the inverse of acquisition: startup first,
607727 * then lifetime/admission/maintenance through the cohort lease. */
608728 if (context->startup_lock) {
@@ -629,9 +749,11 @@ static void cli_activation_production_diagnostic(void *opaque, const char *messa
629749static bool cli_activation_production_context_init(cli_activation_production_context_t *context,
630750 cbm_daemon_runtime_activation_action_t action,
631751 const char *target_version,
632- const char *target_build) {
752+ const char *target_build,
753+ bool quiesce_required) {
633754 memset(context, 0, sizeof(*context));
634755 context->action = action;
756+ context->quiesce_required = quiesce_required;
635757 context->target_version = target_version;
636758 context->target_build = target_build;
637759 context->cleanup_ok = true;
@@ -738,16 +860,22 @@ static void cli_activation_production_context_close(cli_activation_production_co
738860 context->original_cache_environment = NULL;
739861}
740862
741- static int cli_activation_guard(cbm_daemon_runtime_activation_action_t action,
742- const char *target_version, const char *target_build,
743- cbm_cli_activation_mutation_fn mutation, void *mutation_context) {
863+ /* quiesce_required: false when the activation publishes no binary and resets
864+ * no index (a config-only install) — nothing running is then replaced, and
865+ * no session is stopped for it. */
866+ static int cli_activation_guard_scoped(cbm_daemon_runtime_activation_action_t action,
867+ const char *target_version, const char *target_build,
868+ bool quiesce_required,
869+ cbm_cli_activation_mutation_fn mutation,
870+ void *mutation_context) {
744871 if (g_cli_activation_test_ops_set) {
745872 return cbm_cli_activation_guard_with_ops(&g_cli_activation_test_ops, mutation,
746873 mutation_context);
747874 }
748875
749876 cli_activation_production_context_t context;
750- if (!cli_activation_production_context_init(&context, action, target_version, target_build)) {
877+ if (!cli_activation_production_context_init(&context, action, target_version, target_build,
878+ quiesce_required)) {
751879 cli_activation_production_context_close(&context);
752880 /* #1856: this refusal used to print the generic text BARE. Every other
753881 * emitter routes through cli_activation_diagnostic, which appends the
@@ -760,9 +888,6 @@ static int cli_activation_guard(cbm_daemon_runtime_activation_action_t action,
760888 cli_activation_diagnostic(NULL, CLI_ACTIVATION_REFUSED_MESSAGE);
761889 return CLI_TRUE;
762890 }
763- printf("Stopping active CBM sessions and operations for %s...\n",
764- cli_activation_action_text(action));
765- (void)fflush(stdout);
766891 if (!cli_activation_log_event(&context, "requested", NULL)) {
767892 cli_activation_production_context_close(&context);
768893 (void)fprintf(stderr, "error: activation request could not be recorded safely; "
@@ -804,6 +929,13 @@ static int cli_activation_guard(cbm_daemon_runtime_activation_action_t action,
804929 return rc;
805930}
806931
932+ static int cli_activation_guard(cbm_daemon_runtime_activation_action_t action,
933+ const char *target_version, const char *target_build,
934+ cbm_cli_activation_mutation_fn mutation, void *mutation_context) {
935+ return cli_activation_guard_scoped(action, target_version, target_build, true, mutation,
936+ mutation_context);
937+ }
938+
807939/* Tar header field offsets */
808940#define TAR_NAME_LEN 101 /* filename field: bytes 0-99 + NUL */
809941#define TAR_SIZE_OFFSET 124 /* octal size field offset */
@@ -11203,11 +11335,16 @@ int cbm_cmd_install(int argc, char **argv) {
1120311335 .force = force,
1120411336 .dry_run = dry_run,
1120511337 };
11206- int activation_rc =
11207- dry_run ? cli_install_activate(&activation)
11208- : cli_activation_guard(CBM_DAEMON_RUNTIME_ACTIVATION_INSTALL, CBM_VERSION,
11209- has_binary_validator ? binary_validator.fingerprint : NULL,
11210- cli_install_activate, &activation);
11338+ /* What this install replaces decides whether any session must stop: with
11339+ * the published binary untouched (--skip-binary, or an externally managed
11340+ * binary) and no index reset, agent configs are refreshed while every
11341+ * session stays up. */
11342+ bool quiesce_required = has_binary_validator || delete_indexes;
11343+ int activation_rc = dry_run ? cli_install_activate(&activation)
11344+ : cli_activation_guard_scoped(
11345+ CBM_DAEMON_RUNTIME_ACTIVATION_INSTALL, CBM_VERSION,
11346+ has_binary_validator ? binary_validator.fingerprint : NULL,
11347+ quiesce_required, cli_install_activate, &activation);
1121111348 if (activation.binary_transaction) {
1121211349 (void)cli_activation_transaction_abort(&activation.binary_transaction);
1121311350 }
0 commit comments