Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(cli): uninstall no longer gated by agent-config cleanup failures

Martin Vogelc572ddc4f3b6
diff --git a/src/cli/cli.c b/src/cli/cli.c
index 249b43e6f..29ddd7f4f 100644
--- a/src/cli/cli.c
+++ b/src/cli/cli.c
@@ -7729,6 +7729,75 @@ static cbm_install_plan_t *g_install_plan = NULL;
static int g_agent_install_errors = 0;
static int g_agent_uninstall_errors = 0;
+/* Every agent configuration uninstall could not clean, kept for the closing
+ * summary. A cleanup failure no longer stops executable and index removal
+ * (#1954: one symlinked ~/.cursor/mcp.json left a 300 MB binary plus the whole
+ * cache behind), so the user needs ONE list of what is still theirs to fix,
+ * with the observed reason next to each file. */
+typedef struct {
+ char agent[64];
+ char operation[48];
+ char path[CLI_BUF_1K];
+ char reason[160];
+ char detail[160];
+} cbm_agent_config_failure_t;
+
+static cbm_agent_config_failure_t *g_agent_uninstall_failures = NULL;
+static int g_agent_uninstall_failure_count = 0;
+static int g_agent_uninstall_failure_cap = 0;
+
+static void agent_uninstall_failures_reset(void) {
+ free(g_agent_uninstall_failures);
+ g_agent_uninstall_failures = NULL;
+ g_agent_uninstall_failure_count = 0;
+ g_agent_uninstall_failure_cap = 0;
+}
+
+static void agent_uninstall_failure_record(const char *agent, const char *operation,
+ const char *path, const char *reason,
+ const char *detail) {
+ if (g_agent_uninstall_failure_count >= g_agent_uninstall_failure_cap) {
+ int ncap = g_agent_uninstall_failure_cap ? g_agent_uninstall_failure_cap * 2 : CLI_BUF_16;
+ cbm_agent_config_failure_t *grown =
+ realloc(g_agent_uninstall_failures, (size_t)ncap * sizeof(*grown));
+ if (!grown) {
+ return;
+ }
+ g_agent_uninstall_failures = grown;
+ g_agent_uninstall_failure_cap = ncap;
+ }
+ cbm_agent_config_failure_t *entry =
+ &g_agent_uninstall_failures[g_agent_uninstall_failure_count++];
+ (void)snprintf(entry->agent, sizeof(entry->agent), "%s", agent ? agent : "unknown");
+ (void)snprintf(entry->operation, sizeof(entry->operation), "%s",
+ operation ? operation : "unknown");
+ (void)snprintf(entry->path, sizeof(entry->path), "%s", path ? path : "unknown");
+ (void)snprintf(entry->reason, sizeof(entry->reason), "%s", reason ? reason : "");
+ (void)snprintf(entry->detail, sizeof(entry->detail), "%s", detail ? detail : "");
+}
+
+/* The closing list of what uninstall could not clean. Printed AFTER the
+ * executable and the indexes are gone, so nothing in it is a reason to keep
+ * the installation around — each line is one file the user removes an entry
+ * from by hand. */
+static void agent_uninstall_failures_report(bool dry_run) {
+ if (g_agent_uninstall_failure_count == 0) {
+ return;
+ }
+ (void)fprintf(stderr, "\nerror: uninstall %s with %d agent configuration(s) left uncleaned:\n",
+ dry_run ? "dry-run finished" : "finished", g_agent_uninstall_failure_count);
+ for (int i = 0; i < g_agent_uninstall_failure_count; i++) {
+ const cbm_agent_config_failure_t *entry = &g_agent_uninstall_failures[i];
+ (void)fprintf(stderr, " %s (%s): %s", entry->agent, entry->operation, entry->path);
+ if (entry->reason[0]) {
+ (void)fprintf(stderr, " reason=%s", entry->reason);
+ }
+ (void)fputs(entry->detail, stderr);
+ (void)fputc('\n', stderr);
+ }
+ (void)fputs("Remove the codebase-memory-mcp entries from these files by hand.\n", stderr);
+}
+
static void plan_record(const char *agent, const char *kind, const char *path) {
if (!g_install_plan || !path || !path[0]) {
return;
@@ -7801,6 +7870,9 @@ static void record_agent_config_error_with_reason(bool uninstalling, const char
}
(void)fputs(detail, stderr);
(void)fputc('\n', stderr);
+ if (uninstalling) {
+ agent_uninstall_failure_record(agent, operation, path, reason, detail);
+ }
}
static void record_agent_config_error(bool uninstalling, const char *agent, const char *operation,
@@ -12044,13 +12116,12 @@ static int cli_uninstall_activate(void *opaque) {
uninstall_additional_agents(&activation->agents, activation->home, activation->dry_run);
uninstall_agent_client_registry(activation->home, activation->dry_run);
- if (g_agent_uninstall_errors != 0) {
- cli_activation_transaction_abort_or_fail_stop(&activation->binary_transaction,
- "uninstall_transaction_config_cleanup_abort");
- (void)fprintf(stderr, "error: one or more agent cleanup operations failed; executable "
- "and index removal were not started\n");
- return CLI_ACTIVATION_PARTIAL;
- }
+ /* Agent-config failures are collected, never a gate: an entry the editors
+ * refuse to touch (a symlinked config, a foreign file, a malformed
+ * document) is the user's to fix by hand, and leaving a 300 MB executable
+ * plus every index behind because of it is the data-loss shape of #1954.
+ * The indexes and the executable go now; the failures are listed at the
+ * end and decide the exit code. */
if (activation->delete_indexes && !activation->dry_run) {
int expected = count_db_indexes(activation->home);
@@ -12151,6 +12222,7 @@ int cbm_cmd_uninstall(int argc, char **argv) {
printf("codebase-memory-mcp uninstall\n\n");
g_agent_uninstall_errors = 0;
+ agent_uninstall_failures_reset();
cbm_detected_agents_t agents = cbm_detect_agents(home);
/* Confirm index removal outside the startup lock, but defer the mutation
@@ -12220,6 +12292,19 @@ int cbm_cmd_uninstall(int argc, char **argv) {
(void)cli_activation_transaction_abort(&activation.binary_transaction);
}
if (activation_rc != CLI_OK) {
+ agent_uninstall_failures_reset();
+ return CLI_TRUE;
+ }
+
+ if (g_agent_uninstall_errors != 0) {
+ agent_uninstall_failures_report(dry_run);
+ agent_uninstall_failures_reset();
+ printf("\nUninstall finished with errors; the files listed above still hold "
+ "codebase-memory-mcp entries. Please restart your coding-agent sessions "
+ "to properly take this into account.\n");
+ if (dry_run) {
+ printf("(dry-run — no files were modified)\n");
+ }
return CLI_TRUE;
}
@@ -12228,7 +12313,7 @@ int cbm_cmd_uninstall(int argc, char **argv) {
if (dry_run) {
printf("(dry-run — no files were modified)\n");
}
- return g_agent_uninstall_errors == 0 ? 0 : CLI_TRUE;
+ return 0;
}
/* ── Subcommand: update ───────────────────────────────────────── */
diff --git a/tests/test_cli.c b/tests/test_cli.c
index 10ec1097a..aab397da4 100644
--- a/tests/test_cli.c
+++ b/tests/test_cli.c
@@ -714,6 +714,59 @@ static void test_rmdir_r(const char *path) {
th_rmtree(path);
}
+/* Capture everything a command writes to one fd (stdout or stderr) so a test
+ * can assert on the transcript. Restores the fd on end and returns the text. */
+typedef struct {
+ FILE *file;
+ FILE *stream;
+ int target_fd;
+ int saved_fd;
+ bool redirected;
+} cli_fd_capture_t;
+
+static void cli_fd_capture_begin(cli_fd_capture_t *capture, FILE *stream, int target_fd) {
+ memset(capture, 0, sizeof(*capture));
+ capture->stream = stream;
+ capture->target_fd = target_fd;
+ capture->saved_fd = -1;
+ capture->file = tmpfile();
+ if (!capture->file) {
+ return;
+ }
+ capture->saved_fd = dup(target_fd);
+ if (capture->saved_fd < 0) {
+ return;
+ }
+ fflush(stream);
+ capture->redirected = dup2(fileno(capture->file), target_fd) >= 0;
+}
+
+/* Returns the captured text (heap, "" when nothing was written) or NULL when
+ * the capture never engaged. */
+static char *cli_fd_capture_end(cli_fd_capture_t *capture) {
+ fflush(capture->stream);
+ if (capture->saved_fd >= 0) {
+ (void)dup2(capture->saved_fd, capture->target_fd);
+ close(capture->saved_fd);
+ capture->saved_fd = -1;
+ }
+ char *text = NULL;
+ if (capture->file) {
+ if (capture->redirected) {
+ rewind(capture->file);
+ size_t capacity = 65536U;
+ text = calloc(1U, capacity);
+ if (text) {
+ size_t count = fread(text, 1U, capacity - 1U, capture->file);
+ text[count] = '\0';
+ }
+ }
+ fclose(capture->file);
+ capture->file = NULL;
+ }
+ return text;
+}
+
/* Mandatory-daemon activation guard fixture. The production path uses the
* stable per-account endpoint directly; these callbacks make every race
* ordering deterministic without exposing a CLI flag or environment bypass. */
@@ -2269,6 +2322,88 @@ TEST(cli_uninstall_preserves_binary_and_index_when_cohort_does_not_drain) {
PASS();
}
+#ifndef _WIN32
+/* #1954: an agent config that cannot be cleaned must not hold the executable
+ * and the indexes hostage. The fixture's ~/.cursor/mcp.json is a dangling
+ * symlink (a dotfiles checkout that moved) — a shape every config editor keeps
+ * refusing. Before the fix the cleanup failure aborted the activation BEFORE
+ * index and binary removal: `uninstall -y` exited 1 and left a 300 MB binary
+ * plus the whole cache behind, while the report still read "removed". */
+TEST(cli_uninstall_removes_binary_and_index_when_agent_config_cleanup_fails) {
+ char tmpdir[256];
+ snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-uninstall-hostage-XXXXXX");
+ if (!cbm_mkdtemp(tmpdir)) {
+ FAIL("cbm_mkdtemp failed");
+ }
+ char *old_home = NULL;
+ char *old_cache = NULL;
+ cli_activation_save_env(&old_home, &old_cache);
+ cbm_setenv("HOME", tmpdir, 1);
+ /* PATH moves with HOME so no real agent on the developer's PATH is detected. */
+ char *old_path = save_test_env("PATH");
+ cbm_setenv("PATH", tmpdir, 1);
+
+ char cache_dir[512];
+ char index_path[640];
+ snprintf(cache_dir, sizeof(cache_dir), "%s/cache", tmpdir);
+ cbm_setenv("CBM_CACHE_DIR", cache_dir, 1);
+ test_mkdirp(cache_dir);
+ snprintf(index_path, sizeof(index_path), "%s/project.db", cache_dir);
+ write_test_file(index_path, "index must go even when a config cannot be cleaned");
+
+ char bin_dir[512];
+ char bin_target[640];
+ snprintf(bin_dir, sizeof(bin_dir), "%s/.local/bin", tmpdir);
+ test_mkdirp(bin_dir);
+ snprintf(bin_target, sizeof(bin_target), "%s/codebase-memory-mcp", bin_dir);
+ write_test_file(bin_target, "binary must go even when a config cannot be cleaned");
+
+ char cursor_dir[512];
+ char cursor_config[640];
+ char dangling_target[640];
+ snprintf(cursor_dir, sizeof(cursor_dir), "%s/.cursor", tmpdir);
+ test_mkdirp(cursor_dir);
+ snprintf(cursor_config, sizeof(cursor_config), "%s/mcp.json", cursor_dir);
+ snprintf(dangling_target, sizeof(dangling_target), "%s/.dotfiles/cursor/mcp.json", tmpdir);
+ if (symlink(dangling_target, cursor_config) != 0) {
+ FAIL("symlink failed");
+ }
+
+ cli_activation_fake_t fake = {.mutation_reserve_result = 1};
+ cbm_cli_activation_ops_t ops = cli_activation_fake_ops(&fake);
+ cbm_cli_set_activation_ops_for_test(&ops);
+ cli_fd_capture_t out_capture;
+ cli_fd_capture_t err_capture;
+ cli_fd_capture_begin(&out_capture, stdout, STDOUT_FILENO);
+ cli_fd_capture_begin(&err_capture, stderr, STDERR_FILENO);
+ char *argv[] = {"--yes"};
+ int rc = cli_test_cmd_uninstall(1, argv);
+ char *err_text = cli_fd_capture_end(&err_capture);
+ char *out_text = cli_fd_capture_end(&out_capture);
+ cbm_cli_set_activation_ops_for_test(NULL);
+ cbm_set_auto_answer_for_test(0);
+
+ struct stat state;
+ bool binary_gone = lstat(bin_target, &state) != 0 && errno == ENOENT;
+ bool index_gone = lstat(index_path, &state) != 0 && errno == ENOENT;
+ bool link_untouched = lstat(cursor_config, &state) == 0 && S_ISLNK(state.st_mode);
+ bool failure_named = err_text && strstr(err_text, cursor_config) != NULL;
+
+ cli_activation_restore_env(old_home, old_cache);
+ restore_test_env("PATH", old_path);
+ test_rmdir_r(tmpdir);
+ free(err_text);
+ free(out_text);
+
+ ASSERT(rc != 0);
+ ASSERT_TRUE(binary_gone);
+ ASSERT_TRUE(index_gone);
+ ASSERT_TRUE(link_untouched);
+ ASSERT_TRUE(failure_named);
+ PASS();
+}
+#endif
+
TEST(cli_activation_guard_is_bypassed_for_dry_run_and_plan) {
char tmpdir[256];
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-daemon-stateless-XXXXXX");
@@ -4627,7 +4762,7 @@ TEST(cli_agent_uninstall_reports_safe_editor_refusal) {
#else
snprintf(bin_path, sizeof(bin_path), "%s/codebase-memory-mcp", bin_dir);
#endif
- write_test_file(bin_path, "installed binary must remain live\n");
+ write_test_file(bin_path, "installed binary goes even when a config cannot be cleaned\n");
char *saved_home = save_test_env("HOME");
char *saved_path = save_test_env("PATH");
@@ -4651,9 +4786,12 @@ TEST(cli_agent_uninstall_reports_safe_editor_refusal) {
restore_test_env("HOME", saved_home);
restore_test_env("PATH", saved_path);
test_rmdir_r(tmpdir);
- if (rc == 0 || !preserved || !binary_preserved || fake.mutation_reserve_count != 1 ||
- fake.mutation_lease_release_count != 1 || !strstr(fake.diagnostic, "executable was kept"))
- FAIL("agent uninstall refusal must fail before removing the live binary");
+ /* #1954: a refused config is reported and fails the exit code, but it never
+ * keeps the executable installed — that left a 300 MB binary and every index
+ * behind for one unreadable mcp.json. */
+ if (rc == 0 || !preserved || binary_preserved || fake.mutation_reserve_count != 1 ||
+ fake.mutation_lease_release_count != 1 || fake.diagnostic[0] != '\0')
+ FAIL("agent uninstall refusal must fail the exit code without keeping the binary");
PASS();
}
@@ -10463,7 +10601,7 @@ TEST(cli_codex_migrates_to_single_hook_representation) {
#else
snprintf(binary_path, sizeof(binary_path), "%s/codebase-memory-mcp", binary_dir);
#endif
- write_test_file(binary_path, "installed binary must survive failed cleanup\n");
+ write_test_file(binary_path, "installed binary goes even when a config cannot be cleaned\n");
char *saved_home = save_test_env("HOME");
char *saved_path = save_test_env("PATH");
@@ -10515,7 +10653,8 @@ TEST(cli_codex_migrates_to_single_hook_representation) {
snprintf(agent_path, sizeof(agent_path), "%s/agents/codebase-memory.toml", codex_dir);
struct stat state;
hooks = read_test_file_alloc(hooks_path);
- bool independent_cleanup = uninstall_rc != 0 && stat(binary_path, &state) == 0 &&
+ /* #1954: the ambiguous hook fails the exit code; the binary still goes. */
+ bool independent_cleanup = uninstall_rc != 0 && stat(binary_path, &state) != 0 &&
stat(skill_path, &state) != 0 && stat(agent_path, &state) != 0 &&
hooks && !strstr(hooks, "hook-augment");
free(hooks);
@@ -14539,6 +14678,9 @@ SUITE(cli) {
RUN_TEST(cli_update_agent_configs_finish_before_guard_release);
RUN_TEST(cli_uninstall_quiesces_active_cohort_before_removing_binary_and_index);
RUN_TEST(cli_uninstall_preserves_binary_and_index_when_cohort_does_not_drain);
+#ifndef _WIN32
+ RUN_TEST(cli_uninstall_removes_binary_and_index_when_agent_config_cleanup_fails);
+#endif
RUN_TEST(cli_activation_guard_is_bypassed_for_dry_run_and_plan);
#ifdef _WIN32
RUN_TEST(cli_windows_update_hands_off_to_install_script);