Skip to content

Commit 0e89ef8

Browse files
committed
fix(cli): stop relocating a binary a package manager owns (DeusData#1566)
`install`/`update` bundle two separable jobs: place the binary (and put its directory on PATH), and configure the agents. For someone who installed through mise, Homebrew or nix, the first job is not redundant — it is harmful. It drops a SECOND copy into ~/.local/bin that shadows the managed one depending on PATH order, and appends to a shell rc file no package manager asked us to touch: Would install binary -> /Users/user/.local/bin/codebase-memory-mcp Added /Users/user/.local/bin to PATH in /Users/user/.zshrc They wanted the agent configs refreshed and nothing else. Reported by @SC-CTS. `--skip-config` (binary, no configs) has existed for a long time; this adds its missing mirror. Per the user's decision: - **Infer by default.** A recognised manager path means the binary is not ours; we configure the agents and leave the binary and PATH alone. - **`--skip-binary` / `--force-binary`** override the inference either way. - **`update` REFUSES** on a foreign binary and names the command that will work (`mise upgrade`, `brew upgrade`), rather than reporting success while changing nothing — the dishonest-success pattern this project keeps having to fix. - **PATH is only touched when WE placed the binary**, which is a bug in its own right regardless of the rest. Detection is POSITIVE evidence — the path must look like a known manager's — and the first implementation got this wrong in the tempting way: "anything outside our install dir". The test suite failed immediately and was right to. A test binary runs from build/, and an ordinary `install --dir=/opt/cbm` also lives outside the default; both would be misread as foreign, and `update` would then refuse to update an installation we own. The asymmetry settles it: a false positive costs a working update, while a false negative merely leaves today's behaviour for an unrecognised manager, which --skip-binary covers explicitly. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 1649fa8 commit 0e89ef8

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
@@ -9225,6 +9225,71 @@ static bool cbm_detect_self_path(char *buf, size_t buf_sz, const char *home) {
92259225
return exact;
92269226
}
92279227

9228+
/* Is the running binary owned by an external package manager rather than by us?
9229+
*
9230+
* #1566: cbm's installer does two separable jobs — place the binary (and put its
9231+
* directory on PATH), and configure the agents. For someone who installed
9232+
* through mise, Homebrew or nix, the first job is not merely redundant: it drops
9233+
* a SECOND copy into ~/.local/bin that shadows the managed one depending on PATH
9234+
* order, and appends to a shell rc file the package manager never asked us to
9235+
* touch. They wanted the agent configs refreshed and nothing else.
9236+
*
9237+
* The signal is simply that the OS says we are running from somewhere other than
9238+
* the directory we install into. We only infer when the OS REPORTED the path
9239+
* (exact): the ~/.local/bin fallback is a guess, and guessing here would refuse
9240+
* to install for someone who has no binary at all. Naming the manager is
9241+
* best-effort — the behaviour does not depend on recognising it, only the
9242+
* wording does. */
9243+
static const char *cli_external_manager_name(const char *self_path) {
9244+
if (!self_path || !self_path[0]) {
9245+
return NULL;
9246+
}
9247+
if (strstr(self_path, "/mise/") || strstr(self_path, "/.mise/")) {
9248+
return "mise";
9249+
}
9250+
if (strstr(self_path, "/Cellar/") || strstr(self_path, "/homebrew/") ||
9251+
strstr(self_path, "/linuxbrew/")) {
9252+
return "Homebrew";
9253+
}
9254+
if (strstr(self_path, "/nix/store/")) {
9255+
return "nix";
9256+
}
9257+
if (strstr(self_path, "/.asdf/")) {
9258+
return "asdf";
9259+
}
9260+
if (strstr(self_path, "/.cargo/bin/")) {
9261+
return "cargo";
9262+
}
9263+
return NULL;
9264+
}
9265+
9266+
#ifdef CBM_CLI_ENABLE_TEST_API
9267+
const char *cbm_cli_external_manager_name_for_testing(const char *self_path) {
9268+
return cli_external_manager_name(self_path);
9269+
}
9270+
#endif
9271+
9272+
static bool cli_binary_is_externally_managed(const char *self_path, bool self_path_exact,
9273+
const char *bin_dir) {
9274+
(void)bin_dir;
9275+
if (!self_path_exact || !self_path || !self_path[0]) {
9276+
return false;
9277+
}
9278+
/* POSITIVE evidence only: the path must look like a known package manager's.
9279+
*
9280+
* The tempting rule — "anything outside the directory we install into" —
9281+
* is wrong, and the test suite proved it immediately: a test binary runs
9282+
* from build/, and a perfectly ordinary `install --dir=/opt/cbm` also lives
9283+
* outside the default. Both would be misread as foreign, and `update` would
9284+
* refuse to update an installation we own.
9285+
*
9286+
* The asymmetry decides it. A false positive REFUSES a legitimate update; a
9287+
* false negative just leaves today's behaviour in place for an unrecognised
9288+
* manager, which --skip-binary covers explicitly. So we only infer when we
9289+
* can name the owner. */
9290+
return cli_external_manager_name(self_path) != NULL;
9291+
}
9292+
92289293
/* Build the agent.install.plan.v1 receipt (#388): a machine-readable list of
92299294
* the config / instruction / skill / agent / hook files `install` WOULD write, produced by
92309295
* running the real install dispatch in record-only mode (no mutation, no
@@ -9355,6 +9420,10 @@ typedef struct {
93559420
cli_binary_validator_t binary_validator;
93569421
bool has_binary_validator;
93579422
bool copy_binary;
9423+
/* #1566: true when the binary is not ours to place — suppresses the PATH
9424+
* edit too, since adding a directory to PATH for a binary we did not
9425+
* install is exactly the unasked-for change that was complained about. */
9426+
bool skip_binary;
93589427
bool delete_indexes;
93599428
bool skip_config;
93609429
bool force;
@@ -9429,6 +9498,14 @@ static int cli_install_activate(void *opaque) {
94299498
return CLI_ACTIVATION_PARTIAL;
94309499
}
94319500
int path_rc = CLI_TRUE;
9501+
/* #1566: only touch PATH when WE placed the binary. Appending to a shell rc
9502+
* for a binary installed by someone else is an unasked-for change to a file
9503+
* we do not own, and the directory we would add may hold nothing at all. */
9504+
if (activation->skip_binary) {
9505+
cli_activation_transaction_finalize_committed_or_fail_stop(&activation->binary_transaction,
9506+
"install_transaction_finalize");
9507+
return CLI_OK;
9508+
}
94329509
#ifdef _WIN32
94339510
path_rc = cli_ensure_windows_user_path(activation->bin_dir,
94349511
activation->dry_run || g_cli_activation_test_ops_set);
@@ -9478,6 +9555,8 @@ int cbm_cmd_install(int argc, char **argv) {
94789555
bool plan = false;
94799556
bool reset_indexes = false;
94809557
bool skip_config = false;
9558+
bool skip_binary = false;
9559+
bool force_binary = false;
94819560
const char *requested_bin_dir = NULL;
94829561
for (int i = 0; i < argc; i++) {
94839562
if (strcmp(argv[i], "--dry-run") == 0) {
@@ -9490,6 +9569,12 @@ int cbm_cmd_install(int argc, char **argv) {
94909569
reset_indexes = true;
94919570
} else if (strcmp(argv[i], "--skip-config") == 0) {
94929571
skip_config = true;
9572+
} else if (strcmp(argv[i], "--skip-binary") == 0) {
9573+
/* #1566: the mirror of --skip-config. Configure the agents, leave
9574+
* the binary and PATH alone. */
9575+
skip_binary = true;
9576+
} else if (strcmp(argv[i], "--force-binary") == 0) {
9577+
force_binary = true;
94939578
} else if (strncmp(argv[i], "--dir=", SLEN("--dir=")) == 0) {
94949579
requested_bin_dir = argv[i] + SLEN("--dir=");
94959580
if (!requested_bin_dir[0]) {
@@ -9553,15 +9638,32 @@ int cbm_cmd_install(int argc, char **argv) {
95539638
printf("codebase-memory-mcp install %s\n\n", CBM_VERSION);
95549639

95559640
char self_path[CLI_BUF_1K] = {0};
9556-
(void)cbm_detect_self_path(self_path, sizeof(self_path), home);
9641+
bool self_path_exact = cbm_detect_self_path(self_path, sizeof(self_path), home);
9642+
9643+
/* #1566: a binary owned by mise/Homebrew/nix is not ours to relocate. Infer
9644+
* it, and let either flag override the inference in either direction. */
9645+
bool externally_managed =
9646+
!force_binary && cli_binary_is_externally_managed(self_path, self_path_exact, bin_dir);
9647+
if (externally_managed || skip_binary) {
9648+
const char *manager = cli_external_manager_name(self_path);
9649+
if (skip_binary) {
9650+
printf("Skipping binary placement (--skip-binary); configuring agents only.\n\n");
9651+
} else {
9652+
printf("Binary is managed elsewhere%s%s:\n %s\n"
9653+
"Leaving it and your PATH untouched; configuring agents only.\n"
9654+
" (use --force-binary to install a copy into %s anyway)\n\n",
9655+
manager ? " by " : "", manager ? manager : "", self_path, bin_dir);
9656+
}
9657+
skip_binary = true;
9658+
}
95579659

95589660
/* NOT stat(): on Windows it goes through the ANSI code page, so an
95599661
* extended-length or non-ASCII target reports "absent" and a non-force
95609662
* install silently overwrites bytes the user asked to keep. */
95619663
cbm_path_info_t target_status;
95629664
bool target_exists = cbm_path_info_utf8(bin_target, &target_status) == 0;
95639665
bool same_binary = cbm_same_file(self_path, bin_target);
9564-
bool do_copy = !same_binary && (!target_exists || force);
9666+
bool do_copy = !skip_binary && !same_binary && (!target_exists || force);
95659667

95669668
/* (#607) Default: preserve existing indexes. `--reset-indexes` opts into
95679669
* the old prompt-and-delete behaviour. The helper returns 0 only when the
@@ -9737,6 +9839,7 @@ int cbm_cmd_install(int argc, char **argv) {
97379839
.copy_binary = do_copy,
97389840
.delete_indexes = delete_indexes,
97399841
.skip_config = skip_config,
9842+
.skip_binary = skip_binary,
97409843
.force = force,
97419844
.dry_run = dry_run,
97429845
};
@@ -11564,6 +11667,37 @@ int cbm_cmd_update(int argc, char **argv) {
1156411667
"graph UI.\n");
1156511668
}
1156611669

11670+
/* #1566: we cannot update a binary a package manager owns, and pretending
11671+
* otherwise is the dishonest-success pattern this project keeps fixing:
11672+
* reporting that an update ran while changing nothing leaves the user
11673+
* convinced they are current. Refuse, and name the command that WILL work. */
11674+
{
11675+
char self_path[CLI_BUF_1K] = {0};
11676+
const char *home = cbm_get_home_dir();
11677+
bool self_exact = home && cbm_detect_self_path(self_path, sizeof(self_path), home);
11678+
char managed_dir[CLI_BUF_1K] = {0};
11679+
if (self_exact && home) {
11680+
snprintf(managed_dir, sizeof(managed_dir), "%s/.local/bin", home);
11681+
}
11682+
if (self_exact && cli_binary_is_externally_managed(self_path, true, managed_dir)) {
11683+
const char *manager = cli_external_manager_name(self_path);
11684+
(void)fprintf(stderr,
11685+
"error: this binary is managed by %s, so `update` cannot replace it:\n"
11686+
" %s\n",
11687+
manager ? manager : "another installer", self_path);
11688+
if (manager && strcmp(manager, "mise") == 0) {
11689+
(void)fprintf(stderr, " update it with: mise upgrade codebase-memory-mcp\n");
11690+
} else if (manager && strcmp(manager, "Homebrew") == 0) {
11691+
(void)fprintf(stderr, " update it with: brew upgrade codebase-memory-mcp\n");
11692+
} else {
11693+
(void)fprintf(stderr, " update it through whichever tool installed it.\n");
11694+
}
11695+
(void)fprintf(stderr, " to refresh only the agent configurations, run: "
11696+
"codebase-memory-mcp install --skip-binary\n");
11697+
return CLI_TRUE;
11698+
}
11699+
}
11700+
1156711701
/* Updates run from the install script, not from this process — on every
1156811702
* platform.
1156911703
*

‎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)