@@ -9236,6 +9236,71 @@ static bool cbm_detect_self_path(char *buf, size_t buf_sz, const char *home) {
92369236 return exact;
92379237}
92389238
9239+ /* Is the running binary owned by an external package manager rather than by us?
9240+ *
9241+ * #1566: cbm's installer does two separable jobs — place the binary (and put its
9242+ * directory on PATH), and configure the agents. For someone who installed
9243+ * through mise, Homebrew or nix, the first job is not merely redundant: it drops
9244+ * a SECOND copy into ~/.local/bin that shadows the managed one depending on PATH
9245+ * order, and appends to a shell rc file the package manager never asked us to
9246+ * touch. They wanted the agent configs refreshed and nothing else.
9247+ *
9248+ * The signal is simply that the OS says we are running from somewhere other than
9249+ * the directory we install into. We only infer when the OS REPORTED the path
9250+ * (exact): the ~/.local/bin fallback is a guess, and guessing here would refuse
9251+ * to install for someone who has no binary at all. Naming the manager is
9252+ * best-effort — the behaviour does not depend on recognising it, only the
9253+ * wording does. */
9254+ static const char *cli_external_manager_name(const char *self_path) {
9255+ if (!self_path || !self_path[0]) {
9256+ return NULL;
9257+ }
9258+ if (strstr(self_path, "/mise/") || strstr(self_path, "/.mise/")) {
9259+ return "mise";
9260+ }
9261+ if (strstr(self_path, "/Cellar/") || strstr(self_path, "/homebrew/") ||
9262+ strstr(self_path, "/linuxbrew/")) {
9263+ return "Homebrew";
9264+ }
9265+ if (strstr(self_path, "/nix/store/")) {
9266+ return "nix";
9267+ }
9268+ if (strstr(self_path, "/.asdf/")) {
9269+ return "asdf";
9270+ }
9271+ if (strstr(self_path, "/.cargo/bin/")) {
9272+ return "cargo";
9273+ }
9274+ return NULL;
9275+ }
9276+
9277+ #ifdef CBM_CLI_ENABLE_TEST_API
9278+ const char *cbm_cli_external_manager_name_for_testing(const char *self_path) {
9279+ return cli_external_manager_name(self_path);
9280+ }
9281+ #endif
9282+
9283+ static bool cli_binary_is_externally_managed(const char *self_path, bool self_path_exact,
9284+ const char *bin_dir) {
9285+ (void)bin_dir;
9286+ if (!self_path_exact || !self_path || !self_path[0]) {
9287+ return false;
9288+ }
9289+ /* POSITIVE evidence only: the path must look like a known package manager's.
9290+ *
9291+ * The tempting rule — "anything outside the directory we install into" —
9292+ * is wrong, and the test suite proved it immediately: a test binary runs
9293+ * from build/, and a perfectly ordinary `install --dir=/opt/cbm` also lives
9294+ * outside the default. Both would be misread as foreign, and `update` would
9295+ * refuse to update an installation we own.
9296+ *
9297+ * The asymmetry decides it. A false positive REFUSES a legitimate update; a
9298+ * false negative just leaves today's behaviour in place for an unrecognised
9299+ * manager, which --skip-binary covers explicitly. So we only infer when we
9300+ * can name the owner. */
9301+ return cli_external_manager_name(self_path) != NULL;
9302+ }
9303+
92399304/* Build the agent.install.plan.v1 receipt (#388): a machine-readable list of
92409305 * the config / instruction / skill / agent / hook files `install` WOULD write, produced by
92419306 * running the real install dispatch in record-only mode (no mutation, no
@@ -9366,6 +9431,10 @@ typedef struct {
93669431 cli_binary_validator_t binary_validator;
93679432 bool has_binary_validator;
93689433 bool copy_binary;
9434+ /* #1566: true when the binary is not ours to place — suppresses the PATH
9435+ * edit too, since adding a directory to PATH for a binary we did not
9436+ * install is exactly the unasked-for change that was complained about. */
9437+ bool skip_binary;
93699438 bool delete_indexes;
93709439 bool skip_config;
93719440 bool force;
@@ -9440,6 +9509,14 @@ static int cli_install_activate(void *opaque) {
94409509 return CLI_ACTIVATION_PARTIAL;
94419510 }
94429511 int path_rc = CLI_TRUE;
9512+ /* #1566: only touch PATH when WE placed the binary. Appending to a shell rc
9513+ * for a binary installed by someone else is an unasked-for change to a file
9514+ * we do not own, and the directory we would add may hold nothing at all. */
9515+ if (activation->skip_binary) {
9516+ cli_activation_transaction_finalize_committed_or_fail_stop(&activation->binary_transaction,
9517+ "install_transaction_finalize");
9518+ return CLI_OK;
9519+ }
94439520#ifdef _WIN32
94449521 path_rc = cli_ensure_windows_user_path(activation->bin_dir,
94459522 activation->dry_run || g_cli_activation_test_ops_set);
@@ -9489,6 +9566,8 @@ int cbm_cmd_install(int argc, char **argv) {
94899566 bool plan = false;
94909567 bool reset_indexes = false;
94919568 bool skip_config = false;
9569+ bool skip_binary = false;
9570+ bool force_binary = false;
94929571 const char *requested_bin_dir = NULL;
94939572 for (int i = 0; i < argc; i++) {
94949573 if (strcmp(argv[i], "--dry-run") == 0) {
@@ -9501,6 +9580,12 @@ int cbm_cmd_install(int argc, char **argv) {
95019580 reset_indexes = true;
95029581 } else if (strcmp(argv[i], "--skip-config") == 0) {
95039582 skip_config = true;
9583+ } else if (strcmp(argv[i], "--skip-binary") == 0) {
9584+ /* #1566: the mirror of --skip-config. Configure the agents, leave
9585+ * the binary and PATH alone. */
9586+ skip_binary = true;
9587+ } else if (strcmp(argv[i], "--force-binary") == 0) {
9588+ force_binary = true;
95049589 } else if (strncmp(argv[i], "--dir=", SLEN("--dir=")) == 0) {
95059590 requested_bin_dir = argv[i] + SLEN("--dir=");
95069591 if (!requested_bin_dir[0]) {
@@ -9564,15 +9649,32 @@ int cbm_cmd_install(int argc, char **argv) {
95649649 printf("codebase-memory-mcp install %s\n\n", CBM_VERSION);
95659650
95669651 char self_path[CLI_BUF_1K] = {0};
9567- (void)cbm_detect_self_path(self_path, sizeof(self_path), home);
9652+ bool self_path_exact = cbm_detect_self_path(self_path, sizeof(self_path), home);
9653+
9654+ /* #1566: a binary owned by mise/Homebrew/nix is not ours to relocate. Infer
9655+ * it, and let either flag override the inference in either direction. */
9656+ bool externally_managed =
9657+ !force_binary && cli_binary_is_externally_managed(self_path, self_path_exact, bin_dir);
9658+ if (externally_managed || skip_binary) {
9659+ const char *manager = cli_external_manager_name(self_path);
9660+ if (skip_binary) {
9661+ printf("Skipping binary placement (--skip-binary); configuring agents only.\n\n");
9662+ } else {
9663+ printf("Binary is managed elsewhere%s%s:\n %s\n"
9664+ "Leaving it and your PATH untouched; configuring agents only.\n"
9665+ " (use --force-binary to install a copy into %s anyway)\n\n",
9666+ manager ? " by " : "", manager ? manager : "", self_path, bin_dir);
9667+ }
9668+ skip_binary = true;
9669+ }
95689670
95699671 /* NOT stat(): on Windows it goes through the ANSI code page, so an
95709672 * extended-length or non-ASCII target reports "absent" and a non-force
95719673 * install silently overwrites bytes the user asked to keep. */
95729674 cbm_path_info_t target_status;
95739675 bool target_exists = cbm_path_info_utf8(bin_target, &target_status) == 0;
95749676 bool same_binary = cbm_same_file(self_path, bin_target);
9575- bool do_copy = !same_binary && (!target_exists || force);
9677+ bool do_copy = !skip_binary && ! same_binary && (!target_exists || force);
95769678
95779679 /* (#607) Default: preserve existing indexes. `--reset-indexes` opts into
95789680 * the old prompt-and-delete behaviour. The helper returns 0 only when the
@@ -9748,6 +9850,7 @@ int cbm_cmd_install(int argc, char **argv) {
97489850 .copy_binary = do_copy,
97499851 .delete_indexes = delete_indexes,
97509852 .skip_config = skip_config,
9853+ .skip_binary = skip_binary,
97519854 .force = force,
97529855 .dry_run = dry_run,
97539856 };
@@ -11575,6 +11678,37 @@ int cbm_cmd_update(int argc, char **argv) {
1157511678 "graph UI.\n");
1157611679 }
1157711680
11681+ /* #1566: we cannot update a binary a package manager owns, and pretending
11682+ * otherwise is the dishonest-success pattern this project keeps fixing:
11683+ * reporting that an update ran while changing nothing leaves the user
11684+ * convinced they are current. Refuse, and name the command that WILL work. */
11685+ {
11686+ char self_path[CLI_BUF_1K] = {0};
11687+ const char *home = cbm_get_home_dir();
11688+ bool self_exact = home && cbm_detect_self_path(self_path, sizeof(self_path), home);
11689+ char managed_dir[CLI_BUF_1K] = {0};
11690+ if (self_exact && home) {
11691+ snprintf(managed_dir, sizeof(managed_dir), "%s/.local/bin", home);
11692+ }
11693+ if (self_exact && cli_binary_is_externally_managed(self_path, true, managed_dir)) {
11694+ const char *manager = cli_external_manager_name(self_path);
11695+ (void)fprintf(stderr,
11696+ "error: this binary is managed by %s, so `update` cannot replace it:\n"
11697+ " %s\n",
11698+ manager ? manager : "another installer", self_path);
11699+ if (manager && strcmp(manager, "mise") == 0) {
11700+ (void)fprintf(stderr, " update it with: mise upgrade codebase-memory-mcp\n");
11701+ } else if (manager && strcmp(manager, "Homebrew") == 0) {
11702+ (void)fprintf(stderr, " update it with: brew upgrade codebase-memory-mcp\n");
11703+ } else {
11704+ (void)fprintf(stderr, " update it through whichever tool installed it.\n");
11705+ }
11706+ (void)fprintf(stderr, " to refresh only the agent configurations, run: "
11707+ "codebase-memory-mcp install --skip-binary\n");
11708+ return CLI_TRUE;
11709+ }
11710+ }
11711+
1157811712 /* Updates run from the install script, not from this process — on every
1157911713 * platform.
1158011714 *
0 commit comments