88#include "cli/agent_profiles.h"
99#include "cli/cli.h"
1010#include "cli/activation_transaction.h"
11+ #include "cli/config_edit_path.h"
1112#include "cli/config_json_like.h"
1213#include "cli/config_text_edit.h"
1314#include "cli/config_toml_edit.h"
@@ -7983,6 +7984,90 @@ static cbm_install_plan_t *g_install_plan = NULL;
79837984static int g_agent_install_errors = 0;
79847985static int g_agent_uninstall_errors = 0;
79857986
7987+ /* Every agent configuration uninstall could not clean, kept for the closing
7988+ * summary. A cleanup failure no longer stops executable and index removal
7989+ * (#1954: one symlinked ~/.cursor/mcp.json left a 300 MB binary plus the whole
7990+ * cache behind), so the user needs ONE list of what is still theirs to fix,
7991+ * with the observed reason next to each file. */
7992+ typedef struct {
7993+ char agent[64];
7994+ char operation[48];
7995+ char path[CLI_BUF_1K];
7996+ char reason[160];
7997+ char detail[160];
7998+ } cbm_agent_config_failure_t;
7999+
8000+ static cbm_agent_config_failure_t *g_agent_uninstall_failures = NULL;
8001+ static int g_agent_uninstall_failure_count = 0;
8002+ static int g_agent_uninstall_failure_cap = 0;
8003+
8004+ static void agent_uninstall_failures_reset(void) {
8005+ free(g_agent_uninstall_failures);
8006+ g_agent_uninstall_failures = NULL;
8007+ g_agent_uninstall_failure_count = 0;
8008+ g_agent_uninstall_failure_cap = 0;
8009+ }
8010+
8011+ static void agent_uninstall_failure_record(const char *agent, const char *operation,
8012+ const char *path, const char *reason,
8013+ const char *detail) {
8014+ if (g_agent_uninstall_failure_count >= g_agent_uninstall_failure_cap) {
8015+ int ncap = g_agent_uninstall_failure_cap ? g_agent_uninstall_failure_cap * 2 : CLI_BUF_16;
8016+ cbm_agent_config_failure_t *grown =
8017+ realloc(g_agent_uninstall_failures, (size_t)ncap * sizeof(*grown));
8018+ if (!grown) {
8019+ return;
8020+ }
8021+ g_agent_uninstall_failures = grown;
8022+ g_agent_uninstall_failure_cap = ncap;
8023+ }
8024+ cbm_agent_config_failure_t *entry =
8025+ &g_agent_uninstall_failures[g_agent_uninstall_failure_count++];
8026+ (void)snprintf(entry->agent, sizeof(entry->agent), "%s", agent ? agent : "unknown");
8027+ (void)snprintf(entry->operation, sizeof(entry->operation), "%s",
8028+ operation ? operation : "unknown");
8029+ (void)snprintf(entry->path, sizeof(entry->path), "%s", path ? path : "unknown");
8030+ (void)snprintf(entry->reason, sizeof(entry->reason), "%s", reason ? reason : "");
8031+ (void)snprintf(entry->detail, sizeof(entry->detail), "%s", detail ? detail : "");
8032+ }
8033+
8034+ /* The agent-configuration writers opt in to following user-owned symlinked
8035+ * config files under the user's configuration roots (#1954, decision C);
8036+ * every other caller of the config editors keeps refusing links. Cleared by
8037+ * the same command when its configuration work is done. */
8038+ static void cli_config_follow_begin(const char *home) {
8039+ cbm_config_edit_path_follow_clear();
8040+ if (home && home[0]) {
8041+ (void)cbm_config_edit_path_follow_add_root(home);
8042+ }
8043+ const char *xdg_config = getenv("XDG_CONFIG_HOME");
8044+ if (xdg_config && xdg_config[0]) {
8045+ (void)cbm_config_edit_path_follow_add_root(xdg_config);
8046+ }
8047+ }
8048+
8049+ /* The closing list of what uninstall could not clean. Printed AFTER the
8050+ * executable and the indexes are gone, so nothing in it is a reason to keep
8051+ * the installation around — each line is one file the user removes an entry
8052+ * from by hand. */
8053+ static void agent_uninstall_failures_report(bool dry_run) {
8054+ if (g_agent_uninstall_failure_count == 0) {
8055+ return;
8056+ }
8057+ (void)fprintf(stderr, "\nerror: uninstall %s with %d agent configuration(s) left uncleaned:\n",
8058+ dry_run ? "dry-run finished" : "finished", g_agent_uninstall_failure_count);
8059+ for (int i = 0; i < g_agent_uninstall_failure_count; i++) {
8060+ const cbm_agent_config_failure_t *entry = &g_agent_uninstall_failures[i];
8061+ (void)fprintf(stderr, " %s (%s): %s", entry->agent, entry->operation, entry->path);
8062+ if (entry->reason[0]) {
8063+ (void)fprintf(stderr, " reason=%s", entry->reason);
8064+ }
8065+ (void)fputs(entry->detail, stderr);
8066+ (void)fputc('\n', stderr);
8067+ }
8068+ (void)fputs("Remove the codebase-memory-mcp entries from these files by hand.\n", stderr);
8069+ }
8070+
79868071static void plan_record(const char *agent, const char *kind, const char *path) {
79878072 if (!g_install_plan || !path || !path[0]) {
79888073 return;
@@ -8038,6 +8123,14 @@ static void describe_agent_config_target(const char *path, char *out, size_t out
80388123 : info.is_directory ? "directory"
80398124 : info.is_regular ? "regular file"
80408125 : "special file";
8126+ /* A refused symlink names the rule that refused it (#1954): the user
8127+ * then knows whether to fix ownership, the target, or the parent. */
8128+ char refusal[160];
8129+ if (info.is_symlink && cbm_config_edit_path_refusal(path, refusal, sizeof(refusal))) {
8130+ (void)snprintf(out, out_size, " (target: symlink, %lld bytes; not followed: %s)",
8131+ (long long)info.size, refusal);
8132+ return;
8133+ }
80418134 (void)snprintf(out, out_size, " (target: %s, %lld bytes)", kind, (long long)info.size);
80428135}
80438136
@@ -8055,6 +8148,9 @@ static void record_agent_config_error_with_reason(bool uninstalling, const char
80558148 }
80568149 (void)fputs(detail, stderr);
80578150 (void)fputc('\n', stderr);
8151+ if (uninstalling) {
8152+ agent_uninstall_failure_record(agent, operation, path, reason, detail);
8153+ }
80588154}
80598155
80608156static void record_agent_config_error(bool uninstalling, const char *agent, const char *operation,
@@ -10089,12 +10185,24 @@ static void install_additional_agent_configs(const cbm_detected_agents_t *agents
1008910185 }
1009010186}
1009110187
10188+ static int cbm_install_agent_configs_in_scope(const char *home, const char *binary_path, bool force,
10189+ bool dry_run, cbm_detected_agents_t *agents_in);
10190+
1009210191int cbm_install_agent_configs(const char *home, const char *binary_path, bool force, bool dry_run) {
1009310192 g_agent_install_errors = 0;
1009410193 cbm_detected_agents_t agents = cbm_detect_agents(home);
1009510194 if (g_client_selection && !cli_clients_apply_selection(g_client_selection, &agents)) {
1009610195 return CLI_ERR;
1009710196 }
10197+ cli_config_follow_begin(home);
10198+ int result = cbm_install_agent_configs_in_scope(home, binary_path, force, dry_run, &agents);
10199+ cbm_config_edit_path_follow_clear();
10200+ return result;
10201+ }
10202+
10203+ static int cbm_install_agent_configs_in_scope(const char *home, const char *binary_path, bool force,
10204+ bool dry_run, cbm_detected_agents_t *agents_in) {
10205+ cbm_detected_agents_t agents = *agents_in;
1009810206 if (!g_install_plan) {
1009910207 print_detected_agents(&agents, home);
1010010208 }
@@ -12404,21 +12512,22 @@ static int cli_uninstall_activate(void *opaque) {
1240412512 return CLI_TRUE;
1240512513 }
1240612514
12515+ cli_config_follow_begin(activation->home);
1240712516 if (activation->agents.claude_code) {
1240812517 uninstall_claude_code(activation->home, activation->bin_path, activation->dry_run);
1240912518 }
1241012519 uninstall_cli_agents(&activation->agents, activation->home, activation->dry_run);
1241112520 uninstall_editor_agents(&activation->agents, activation->home, activation->dry_run);
1241212521 uninstall_additional_agents(&activation->agents, activation->home, activation->dry_run);
1241312522 uninstall_agent_client_registry(activation->home, activation->dry_run);
12523+ cbm_config_edit_path_follow_clear();
1241412524
12415- if (g_agent_uninstall_errors != 0) {
12416- cli_activation_transaction_abort_or_fail_stop(&activation->binary_transaction,
12417- "uninstall_transaction_config_cleanup_abort");
12418- (void)fprintf(stderr, "error: one or more agent cleanup operations failed; executable "
12419- "and index removal were not started\n");
12420- return CLI_ACTIVATION_PARTIAL;
12421- }
12525+ /* Agent-config failures are collected, never a gate: an entry the editors
12526+ * refuse to touch (a symlinked config, a foreign file, a malformed
12527+ * document) is the user's to fix by hand, and leaving a 300 MB executable
12528+ * plus every index behind because of it is the data-loss shape of #1954.
12529+ * The indexes and the executable go now; the failures are listed at the
12530+ * end and decide the exit code. */
1242212531
1242312532#ifdef _WIN32
1242412533 /* #2117: install registers the install directory in the persistent
@@ -12556,6 +12665,7 @@ int cbm_cmd_uninstall(int argc, char **argv) {
1255612665 printf("codebase-memory-mcp uninstall\n\n");
1255712666
1255812667 g_agent_uninstall_errors = 0;
12668+ agent_uninstall_failures_reset();
1255912669 cbm_detected_agents_t agents = cbm_detect_agents(home);
1256012670
1256112671 /* Confirm index removal outside the startup lock, but defer the mutation
@@ -12625,6 +12735,19 @@ int cbm_cmd_uninstall(int argc, char **argv) {
1262512735 (void)cli_activation_transaction_abort(&activation.binary_transaction);
1262612736 }
1262712737 if (activation_rc != CLI_OK) {
12738+ agent_uninstall_failures_reset();
12739+ return CLI_TRUE;
12740+ }
12741+
12742+ if (g_agent_uninstall_errors != 0) {
12743+ agent_uninstall_failures_report(dry_run);
12744+ agent_uninstall_failures_reset();
12745+ printf("\nUninstall finished with errors; the files listed above still hold "
12746+ "codebase-memory-mcp entries. Please restart your coding-agent sessions "
12747+ "to properly take this into account.\n");
12748+ if (dry_run) {
12749+ printf("(dry-run — no files were modified)\n");
12750+ }
1262812751 return CLI_TRUE;
1262912752 }
1263012753
@@ -12633,7 +12756,7 @@ int cbm_cmd_uninstall(int argc, char **argv) {
1263312756 if (dry_run) {
1263412757 printf("(dry-run — no files were modified)\n");
1263512758 }
12636- return g_agent_uninstall_errors == 0 ? 0 : CLI_TRUE ;
12759+ return 0 ;
1263712760}
1263812761
1263912762/* ── Subcommand: update ───────────────────────────────────────── */
0 commit comments