@@ -9450,6 +9450,143 @@ TEST(cli_hermes_stable_shell_context_contract) {
94509450 PASS();
94519451}
94529452
9453+ /* A fake HOME holding only ~/.hermes/config.yaml, so install and uninstall
9454+ * reach exactly the Hermes editors. */
9455+ typedef struct {
9456+ char tmpdir[256];
9457+ char config_path[640];
9458+ char binary_path[640];
9459+ char *saved_home;
9460+ char *saved_path;
9461+ char *saved_hermes;
9462+ } cli_hermes_cycle_t;
9463+
9464+ static bool cli_hermes_cycle_open(cli_hermes_cycle_t *cycle, const char *initial_config) {
9465+ memset(cycle, 0, sizeof(*cycle));
9466+ snprintf(cycle->tmpdir, sizeof(cycle->tmpdir), "/tmp/cli-hermes-cycle-XXXXXX");
9467+ if (!cbm_mkdtemp(cycle->tmpdir)) {
9468+ cycle->tmpdir[0] = '\0';
9469+ return false;
9470+ }
9471+ cycle->saved_home = save_test_env("HOME");
9472+ cycle->saved_path = save_test_env("PATH");
9473+ cycle->saved_hermes = save_test_env("HERMES_HOME");
9474+ cbm_setenv("HOME", cycle->tmpdir, 1);
9475+ cbm_setenv("PATH", cycle->tmpdir, 1);
9476+ cbm_unsetenv("HERMES_HOME");
9477+
9478+ char hermes_dir[512];
9479+ snprintf(hermes_dir, sizeof(hermes_dir), "%s/.hermes", cycle->tmpdir);
9480+ snprintf(cycle->config_path, sizeof(cycle->config_path), "%s/config.yaml", hermes_dir);
9481+ #ifdef _WIN32
9482+ snprintf(cycle->binary_path, sizeof(cycle->binary_path),
9483+ "%s/.local/bin/codebase-memory-mcp.exe", cycle->tmpdir);
9484+ #else
9485+ snprintf(cycle->binary_path, sizeof(cycle->binary_path), "%s/.local/bin/codebase-memory-mcp",
9486+ cycle->tmpdir);
9487+ #endif
9488+ test_mkdirp(hermes_dir);
9489+ return write_test_file(cycle->config_path, initial_config) == 0;
9490+ }
9491+
9492+ static void cli_hermes_cycle_close(cli_hermes_cycle_t *cycle) {
9493+ if (!cycle->tmpdir[0]) {
9494+ return;
9495+ }
9496+ restore_test_env("HOME", cycle->saved_home);
9497+ restore_test_env("PATH", cycle->saved_path);
9498+ restore_test_env("HERMES_HOME", cycle->saved_hermes);
9499+ test_rmdir_r(cycle->tmpdir);
9500+ cycle->tmpdir[0] = '\0';
9501+ }
9502+
9503+ /* Found while finishing #1862: when our hook was the only entry under
9504+ * `hooks:`, uninstall empties that mapping and the mapping editor writes it as
9505+ * `hooks: {}` (the same convention as `mcp_servers: {}`). The hook sequence
9506+ * editor then refused the file, so install -> uninstall -> install exited 1,
9507+ * and so did a repeated uninstall. The explicit empty mapping must be accepted:
9508+ * a repeated uninstall changes nothing and the reinstall restores the first
9509+ * install byte for byte, with exactly one owned hook. */
9510+ TEST(cli_hermes_hook_reinstalls_after_uninstall_empties_hooks) {
9511+ cli_hermes_cycle_t cycle;
9512+ if (!cli_hermes_cycle_open(&cycle, "theme: solarized\n")) {
9513+ cli_hermes_cycle_close(&cycle);
9514+ FAIL("Hermes fake-HOME setup failed");
9515+ }
9516+ char *argv[] = {"uninstall", "--yes"};
9517+ int install_rc = cbm_install_agent_configs(cycle.tmpdir, cycle.binary_path, false, false);
9518+ char *installed = read_test_file_alloc(cycle.config_path);
9519+ int uninstall_rc = cli_test_cmd_uninstall(2, argv);
9520+ char *emptied = read_test_file_alloc(cycle.config_path);
9521+ int repeat_rc = cli_test_cmd_uninstall(2, argv);
9522+ char *repeated = read_test_file_alloc(cycle.config_path);
9523+ int reinstall_rc = cbm_install_agent_configs(cycle.tmpdir, cycle.binary_path, false, false);
9524+ char *reinstalled = read_test_file_alloc(cycle.config_path);
9525+ cli_hermes_cycle_close(&cycle);
9526+
9527+ bool installed_once = install_rc == 0 && installed &&
9528+ test_count_substring(installed, "pre_llm_call:") == 1U &&
9529+ test_count_substring(installed, "hook-augment") == 1U;
9530+ bool emptied_clean = uninstall_rc == 0 && emptied && strstr(emptied, "theme: solarized\n") &&
9531+ test_count_substring(emptied, "hooks: {}\n") == 1U &&
9532+ !strstr(emptied, "pre_llm_call") && !strstr(emptied, "hook-augment");
9533+ bool repeat_unchanged = repeat_rc == 0 && emptied && repeated && strcmp(repeated, emptied) == 0;
9534+ bool reinstalled_once =
9535+ reinstall_rc == 0 && installed && reinstalled && strcmp(reinstalled, installed) == 0;
9536+ free(installed);
9537+ free(emptied);
9538+ free(repeated);
9539+ free(reinstalled);
9540+ if (!installed_once)
9541+ FAIL("Hermes install must write exactly one owned pre_llm_call hook");
9542+ if (!emptied_clean)
9543+ FAIL("uninstall must remove the owned hook and leave only the empty hooks: {} mapping");
9544+ if (!repeat_unchanged)
9545+ FAIL("a repeated uninstall must accept hooks: {} and leave the file unchanged");
9546+ if (!reinstalled_once)
9547+ FAIL("reinstall must accept hooks: {} and restore exactly one owned hook");
9548+ PASS();
9549+ }
9550+
9551+ /* The same cycle with a user's own hook in the shared pre_llm_call sequence:
9552+ * the foreign hook survives untouched and uninstall restores the original
9553+ * file byte for byte. */
9554+ TEST(cli_hermes_hook_cycle_keeps_foreign_pre_llm_hook) {
9555+ static const char original[] = "theme: solarized\n"
9556+ "mcp_servers: {}\n"
9557+ "hooks:\n"
9558+ " pre_llm_call:\n"
9559+ " - command: \"/usr/bin/user-hermes-hook\"\n";
9560+ cli_hermes_cycle_t cycle;
9561+ if (!cli_hermes_cycle_open(&cycle, original)) {
9562+ cli_hermes_cycle_close(&cycle);
9563+ FAIL("Hermes fake-HOME setup failed");
9564+ }
9565+ char *argv[] = {"uninstall", "--yes"};
9566+ int install_rc = cbm_install_agent_configs(cycle.tmpdir, cycle.binary_path, false, false);
9567+ char *installed = read_test_file_alloc(cycle.config_path);
9568+ int uninstall_rc = cli_test_cmd_uninstall(2, argv);
9569+ char *restored = read_test_file_alloc(cycle.config_path);
9570+ int reinstall_rc = cbm_install_agent_configs(cycle.tmpdir, cycle.binary_path, false, false);
9571+ char *reinstalled = read_test_file_alloc(cycle.config_path);
9572+ cli_hermes_cycle_close(&cycle);
9573+
9574+ bool merged = install_rc == 0 && installed &&
9575+ test_count_substring(installed, "/usr/bin/user-hermes-hook") == 1U &&
9576+ test_count_substring(installed, "pre_llm_call:") == 1U &&
9577+ test_count_substring(installed, "hook-augment") == 1U;
9578+ bool restored_exactly = uninstall_rc == 0 && restored && strcmp(restored, original) == 0;
9579+ bool reinstalled_once =
9580+ reinstall_rc == 0 && installed && reinstalled && strcmp(reinstalled, installed) == 0;
9581+ free(installed);
9582+ free(restored);
9583+ free(reinstalled);
9584+ if (!merged || !restored_exactly || !reinstalled_once)
9585+ FAIL("a user's own pre_llm_call hook must survive install, uninstall and reinstall, and "
9586+ "uninstall must restore the original file byte for byte");
9587+ PASS();
9588+ }
9589+
94539590#ifndef _WIN32
94549591TEST(cli_detected_agent_summary_includes_registry_clients) {
94559592 char tmpdir[256];
@@ -20553,6 +20690,8 @@ SUITE(cli) {
2055320690 RUN_TEST(cli_junie_current_durable_context_contract);
2055420691 RUN_TEST(cli_rovo_installs_documented_global_memory);
2055520692 RUN_TEST(cli_hermes_stable_shell_context_contract);
20693+ RUN_TEST(cli_hermes_hook_reinstalls_after_uninstall_empties_hooks);
20694+ RUN_TEST(cli_hermes_hook_cycle_keeps_foreign_pre_llm_hook);
2055620695#ifndef _WIN32
2055720696 RUN_TEST(cli_detected_agent_summary_includes_registry_clients);
2055820697#endif
0 commit comments