@@ -9033,9 +9033,19 @@ static void install_additional_agent_configs(const cbm_detected_agents_t *agents
90339033 }
90349034}
90359035
9036+ /* #1558: set by `install --clients=...` after validation, consumed at the one
9037+ * place detection happens. Validation runs in cbm_cmd_install so an unknown
9038+ * token fails before anything is written, not midway through configuring. */
9039+ static const char *g_client_selection = NULL;
9040+ static bool cli_clients_apply_selection(const char *spec, cbm_detected_agents_t *detected);
9041+ static void cli_clients_print_list(FILE *out);
9042+
90369043int cbm_install_agent_configs(const char *home, const char *binary_path, bool force, bool dry_run) {
90379044 g_agent_install_errors = 0;
90389045 cbm_detected_agents_t agents = cbm_detect_agents(home);
9046+ if (g_client_selection && !cli_clients_apply_selection(g_client_selection, &agents)) {
9047+ return CLI_ERR;
9048+ }
90399049 if (!g_install_plan) {
90409050 print_detected_agents(&agents, home);
90419051 }
@@ -9151,6 +9161,121 @@ int cbm_install_handle_existing_indexes(const char *home, bool reset, bool dry_r
91519161 return prepare_result;
91529162}
91539163
9164+ /* ── Client selection (#1558) ──────────────────────────────────
9165+ *
9166+ * `install` configured EVERY detected client. A user who wanted Claude and
9167+ * Codex had to revert the OpenCode and Cursor integrations by hand, and the
9168+ * next install silently recreated them. `--clients=claude,codex` restricts it.
9169+ *
9170+ * The table is the flag's vocabulary AND its documentation: 26 clients ship
9171+ * here, with tokens nobody would guess (factory-droid, mistral-vibe,
9172+ * copilot-cli), so `--clients=help` prints every token. A selector whose
9173+ * accepted values can only be learned from the source is not a usable
9174+ * selector, and an unrecognised token fails loudly with the list rather than
9175+ * silently configuring nothing. */
9176+ typedef struct {
9177+ const char *token;
9178+ size_t offset;
9179+ const char *display;
9180+ } cli_client_def_t;
9181+
9182+ #define CLI_CLIENT(field, token, display) \
9183+ { token, offsetof(cbm_detected_agents_t, field), display }
9184+
9185+ static const cli_client_def_t CLI_CLIENTS[] = {
9186+ CLI_CLIENT(claude_code, "claude", "Claude Code"),
9187+ CLI_CLIENT(codex, "codex", "Codex"),
9188+ CLI_CLIENT(gemini, "gemini", "Gemini"),
9189+ CLI_CLIENT(zed, "zed", "Zed"),
9190+ CLI_CLIENT(opencode, "opencode", "OpenCode"),
9191+ CLI_CLIENT(antigravity, "antigravity", "Antigravity"),
9192+ CLI_CLIENT(aider, "aider", "Aider"),
9193+ CLI_CLIENT(kilocode, "kilocode", "Kilo Code"),
9194+ CLI_CLIENT(vscode, "vscode", "VS Code"),
9195+ CLI_CLIENT(cursor, "cursor", "Cursor"),
9196+ CLI_CLIENT(windsurf, "windsurf", "Windsurf"),
9197+ CLI_CLIENT(augment, "augment", "Augment"),
9198+ CLI_CLIENT(openclaw, "openclaw", "OpenClaw"),
9199+ CLI_CLIENT(kiro, "kiro", "Kiro"),
9200+ CLI_CLIENT(junie, "junie", "Junie"),
9201+ CLI_CLIENT(hermes, "hermes", "Hermes"),
9202+ CLI_CLIENT(openhands, "openhands", "OpenHands"),
9203+ CLI_CLIENT(cline, "cline", "Cline"),
9204+ CLI_CLIENT(warp, "warp", "Warp"),
9205+ CLI_CLIENT(qwen, "qwen", "Qwen"),
9206+ CLI_CLIENT(copilot_cli, "copilot-cli", "Copilot CLI"),
9207+ CLI_CLIENT(factory_droid, "factory-droid", "Factory Droid"),
9208+ CLI_CLIENT(crush, "crush", "Crush"),
9209+ CLI_CLIENT(goose, "goose", "Goose"),
9210+ CLI_CLIENT(mistral_vibe, "mistral-vibe", "Mistral Vibe"),
9211+ };
9212+
9213+ enum { CLI_CLIENT_COUNT = sizeof(CLI_CLIENTS) / sizeof(CLI_CLIENTS[0]) };
9214+
9215+ static void cli_clients_print_list(FILE *out) {
9216+ (void)fprintf(out, "Available --clients tokens:\n");
9217+ for (size_t i = 0; i < CLI_CLIENT_COUNT; i++) {
9218+ (void)fprintf(out, " %-16s %s\n", CLI_CLIENTS[i].token, CLI_CLIENTS[i].display);
9219+ }
9220+ (void)fprintf(out, "\nExample: --clients=claude,codex\n"
9221+ "Omit --clients to configure every detected client.\n");
9222+ }
9223+
9224+ /* Restrict `detected` to the comma-separated token list. Returns false (after
9225+ * printing the vocabulary) when a token is unknown, so a typo can never be
9226+ * mistaken for "that client was not installed". */
9227+ static bool cli_clients_apply_selection(const char *spec, cbm_detected_agents_t *detected) {
9228+ bool wanted[CLI_CLIENT_COUNT];
9229+ memset(wanted, 0, sizeof(wanted));
9230+ char buf[CLI_BUF_1K];
9231+ snprintf(buf, sizeof(buf), "%s", spec ? spec : "");
9232+ char *save = NULL;
9233+ for (char *tok = strtok_r(buf, ",", &save); tok; tok = strtok_r(NULL, ",", &save)) {
9234+ while (*tok == ' ') {
9235+ tok++;
9236+ }
9237+ size_t len = strlen(tok);
9238+ while (len > 0 && tok[len - 1] == ' ') {
9239+ tok[--len] = '\0';
9240+ }
9241+ if (!tok[0]) {
9242+ continue;
9243+ }
9244+ bool matched = false;
9245+ for (size_t i = 0; i < CLI_CLIENT_COUNT; i++) {
9246+ if (strcmp(tok, CLI_CLIENTS[i].token) == 0) {
9247+ wanted[i] = true;
9248+ matched = true;
9249+ break;
9250+ }
9251+ }
9252+ if (!matched) {
9253+ (void)fprintf(stderr, "error: unknown client: %s\n\n", tok);
9254+ cli_clients_print_list(stderr);
9255+ return false;
9256+ }
9257+ }
9258+ for (size_t i = 0; i < CLI_CLIENT_COUNT; i++) {
9259+ if (!wanted[i]) {
9260+ *(bool *)((char *)detected + CLI_CLIENTS[i].offset) = false;
9261+ }
9262+ }
9263+ return true;
9264+ }
9265+
9266+ #ifdef CBM_CLI_ENABLE_TEST_API
9267+ bool cbm_cli_clients_apply_selection_for_testing(const char *spec,
9268+ cbm_detected_agents_t *detected) {
9269+ return cli_clients_apply_selection(spec, detected);
9270+ }
9271+ size_t cbm_cli_clients_count_for_testing(void) {
9272+ return CLI_CLIENT_COUNT;
9273+ }
9274+ const char *cbm_cli_clients_token_for_testing(size_t index) {
9275+ return index < CLI_CLIENT_COUNT ? CLI_CLIENTS[index].token : NULL;
9276+ }
9277+ #endif
9278+
91549279/* ── Subcommand: install ──────────────────────────────────────── */
91559280
91569281/* The activation flow (self-path detection, simple-copy activate) is the same
@@ -9539,6 +9664,7 @@ int cbm_cmd_install(int argc, char **argv) {
95399664 bool skip_config = false;
95409665 bool skip_binary = false;
95419666 bool force_binary = false;
9667+ const char *requested_clients = NULL;
95429668 const char *requested_bin_dir = NULL;
95439669 for (int i = 0; i < argc; i++) {
95449670 if (strcmp(argv[i], "--dry-run") == 0) {
@@ -9551,6 +9677,18 @@ int cbm_cmd_install(int argc, char **argv) {
95519677 reset_indexes = true;
95529678 } else if (strcmp(argv[i], "--skip-config") == 0) {
95539679 skip_config = true;
9680+ } else if (strncmp(argv[i], "--clients=", SLEN("--clients=")) == 0) {
9681+ requested_clients = argv[i] + SLEN("--clients=");
9682+ if (!requested_clients[0]) {
9683+ (void)fprintf(stderr, "error: --clients requires a value\n\n");
9684+ cli_clients_print_list(stderr);
9685+ return CLI_TRUE;
9686+ }
9687+ } else if (strcmp(argv[i], "--clients") == 0) {
9688+ /* Bare `--clients` (and `--clients=help`/`list`) print the
9689+ * vocabulary. 26 clients ship with tokens nobody would guess. */
9690+ cli_clients_print_list(stdout);
9691+ return 0;
95549692 } else if (strcmp(argv[i], "--skip-binary") == 0) {
95559693 /* #1566: the mirror of --skip-config. Configure the agents, leave
95569694 * the binary and PATH alone. */
@@ -9617,6 +9755,16 @@ int cbm_cmd_install(int argc, char **argv) {
96179755 return 0;
96189756 }
96199757
9758+ /* #1558: validate the client tokens BEFORE anything is written, so a typo
9759+ * fails immediately instead of part-way through configuring. */
9760+ if (requested_clients) {
9761+ cbm_detected_agents_t probe = cbm_detect_agents(home);
9762+ if (!cli_clients_apply_selection(requested_clients, &probe)) {
9763+ return CLI_TRUE;
9764+ }
9765+ g_client_selection = requested_clients;
9766+ }
9767+
96209768 printf("codebase-memory-mcp install %s\n\n", CBM_VERSION);
96219769
96229770 char self_path[CLI_BUF_1K] = {0};
0 commit comments