Skip to content

Commit bd11088

Browse files
authored
Merge pull request DeusData#1585 from DeusData/fix/installer-scope
fix(cli): stop relocating a binary a package manager owns (DeusData#1566)
2 parents 75fed22 + 0e89ef8 commit bd11088

3 files changed

Lines changed: 171 additions & 2 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 136 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*

‎src/cli/cli.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ cbm_detected_agents_t cbm_detect_agents(const char *home_dir);
194194
/* Install or refresh every detected agent integration below home. */
195195
int cbm_install_agent_configs(const char *home, const char *binary_path, bool force, bool dry_run);
196196

197+
#ifdef CBM_CLI_ENABLE_TEST_API
198+
/* #1566: name the package manager that owns a binary path, or NULL when it is
199+
* not recognisably foreign. Detection must be positive evidence — see the
200+
* implementation comment. */
201+
const char *cbm_cli_external_manager_name_for_testing(const char *self_path);
202+
#endif
203+
197204
#ifdef CBM_CLI_ENABLE_TEST_API
198205
int cbm_build_qwen_hook_command_for_testing(const char *binary_path, bool windows, char *command,
199206
size_t command_size, char *shell, size_t shell_size);

‎tests/test_cli.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12193,6 +12193,33 @@ TEST(cli_skill_frontmatter_scalars_with_colons_are_quoted_issue1554) {
1219312193
PASS();
1219412194
}
1219512195

12196+
/* #1566: a binary owned by mise/Homebrew/nix is not ours to relocate, and the
12197+
* detection must key on POSITIVE evidence — a recognised manager path — not on
12198+
* "outside our default install dir". The latter misreads an ordinary
12199+
* `install --dir=/opt/cbm` (and every test binary) as foreign, and would then
12200+
* REFUSE to update an installation we own. A false positive costs a working
12201+
* update; a false negative only leaves today's behaviour for an unrecognised
12202+
* manager, which --skip-binary covers explicitly. */
12203+
TEST(cli_external_manager_detection_needs_positive_evidence_issue1566) {
12204+
/* Recognised managers -> named. */
12205+
ASSERT_NOT_NULL(cbm_cli_external_manager_name_for_testing(
12206+
"/Users/x/.local/share/mise/installs/cbm/0.10.3/bin/codebase-memory-mcp"));
12207+
ASSERT_NOT_NULL(cbm_cli_external_manager_name_for_testing(
12208+
"/opt/homebrew/Cellar/codebase-memory-mcp/0.10.3/bin/codebase-memory-mcp"));
12209+
ASSERT_NOT_NULL(
12210+
cbm_cli_external_manager_name_for_testing("/nix/store/abc-cbm/bin/codebase-memory-mcp"));
12211+
12212+
/* Ours, or merely unusual, must NOT be claimed as externally managed. */
12213+
ASSERT_NULL(
12214+
cbm_cli_external_manager_name_for_testing("/Users/x/.local/bin/codebase-memory-mcp"));
12215+
ASSERT_NULL(cbm_cli_external_manager_name_for_testing("/opt/cbm/codebase-memory-mcp"));
12216+
ASSERT_NULL(cbm_cli_external_manager_name_for_testing("/usr/local/bin/codebase-memory-mcp"));
12217+
ASSERT_NULL(cbm_cli_external_manager_name_for_testing("build/c/test-runner"));
12218+
ASSERT_NULL(cbm_cli_external_manager_name_for_testing(""));
12219+
ASSERT_NULL(cbm_cli_external_manager_name_for_testing(NULL));
12220+
PASS();
12221+
}
12222+
1219612223
TEST(cli_update_accepts_retired_variant_flags_issue1544) {
1219712224
char *ui_argv[] = {"--dry-run", "--ui", "--yes"};
1219812225
char *standard_argv[] = {"--dry-run", "--standard", "--yes"};
@@ -12282,6 +12309,7 @@ SUITE(cli) {
1228212309
RUN_TEST(cli_update_download_failure_does_not_quiesce_sessions);
1228312310
RUN_TEST(cli_update_already_current_does_not_quiesce_sessions);
1228412311
RUN_TEST(cli_skill_frontmatter_scalars_with_colons_are_quoted_issue1554);
12312+
RUN_TEST(cli_external_manager_detection_needs_positive_evidence_issue1566);
1228512313
RUN_TEST(cli_update_accepts_retired_variant_flags_issue1544);
1228612314
RUN_TEST(cli_update_agent_configs_finish_before_guard_release);
1228712315
RUN_TEST(cli_uninstall_quiesces_active_cohort_before_removing_binary_and_index);

0 commit comments

Comments
 (0)