Skip to content

Commit 5830d28

Browse files
authored
Merge pull request DeusData#2896 from DeusData/fix/hermes-remover-empty-hooks
fix(cli): accept the emptied hooks: {} mapping in the Hermes hook editor
2 parents 464bfb5 + d681c37 commit 5830d28

3 files changed

Lines changed: 345 additions & 5 deletions

File tree

‎src/cli/config_yaml_edit.c‎

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2589,6 +2589,10 @@ typedef struct {
25892589
bool identity_found;
25902590
size_t identity_start;
25912591
size_t identity_end;
2592+
/* The missing rest of the path begins under a `parent: {}` header. */
2593+
bool parent_inline_empty;
2594+
size_t parent_line;
2595+
size_t parent_colon;
25922596
} yaml_mapping_sequence_target_t;
25932597

25942598
static void yaml_sequence_key_vec_free(yaml_sequence_key_vec_t *keys) {
@@ -2962,6 +2966,37 @@ static int yaml_sequence_parse_block(const yaml_doc_t *doc, size_t begin_line, s
29622966
return 0;
29632967
}
29642968

2969+
/* `section: {}` is how the mapping-entry editor writes a section it emptied
2970+
* (yaml_build_last_mapping_entry_removal), and its upsert accepts it back. On
2971+
* a PARENT of the sequence path it is that same empty mapping, so the rest of
2972+
* the path is missing beneath it: removal is a no-op and upsert expands the
2973+
* header in place. The Hermes remover leaves `hooks: {}` whenever our hook was
2974+
* the only entry. The sequence key itself holding `{}`, any other flow value,
2975+
* and content nested under the inline mapping stay refused. */
2976+
static int yaml_sequence_target_empty_parent(const yaml_doc_t *doc, size_t key_line, size_t colon,
2977+
size_t child_end, size_t next_depth,
2978+
size_t sequence_path_len,
2979+
yaml_mapping_sequence_target_t *target) {
2980+
bool inline_empty = false;
2981+
if (next_depth >= sequence_path_len ||
2982+
yaml_tail_is_explicit_empty_mapping(doc->data, colon, doc->lines[key_line].text_end,
2983+
&inline_empty) != 0 ||
2984+
!inline_empty) {
2985+
return YAML_ERROR;
2986+
}
2987+
for (size_t i = key_line + YAML_UNIT; i < child_end; i++) {
2988+
if (!doc->lines[i].blank && !doc->lines[i].comment) {
2989+
return YAML_ERROR;
2990+
}
2991+
}
2992+
target->missing_index = next_depth;
2993+
target->insert_offset = yaml_sequence_line_offset(doc, child_end);
2994+
target->parent_inline_empty = true;
2995+
target->parent_line = key_line;
2996+
target->parent_colon = colon;
2997+
return 0;
2998+
}
2999+
29653000
static int yaml_sequence_analyze(const yaml_doc_t *doc, const char *const *sequence_path,
29663001
const size_t *path_lengths, size_t sequence_path_len,
29673002
const char *identity_key, const char *identity_value,
@@ -2993,10 +3028,11 @@ static int yaml_sequence_analyze(const yaml_doc_t *doc, const char *const *seque
29933028
return 0;
29943029
}
29953030
const yaml_line_t *header = &doc->lines[key_line];
3031+
size_t child_end = yaml_sequence_nested_end(doc, key_line, parent_end);
29963032
if (!yaml_tail_is_empty(doc->data, colon, header->text_end)) {
2997-
return YAML_ERROR;
3033+
return yaml_sequence_target_empty_parent(doc, key_line, colon, child_end,
3034+
depth + YAML_UNIT, sequence_path_len, target);
29983035
}
2999-
size_t child_end = yaml_sequence_nested_end(doc, key_line, parent_end);
30003036
if (depth + YAML_UNIT == sequence_path_len) {
30013037
target->sequence_found = true;
30023038
target->missing_index = sequence_path_len;
@@ -3095,13 +3131,52 @@ static int yaml_sequence_validate_canonical(const char *canonical_item, size_t c
30953131
return result;
30963132
}
30973133

3134+
/* Copies the document up to insert_offset with the `parent: {}` header turned
3135+
* into a block header `parent:`, exactly as
3136+
* yaml_build_inline_empty_mapping_section_add does for a mapping section: a
3137+
* trailing comment and the line ending are kept. */
3138+
static int yaml_sequence_append_expanded_parent(yaml_buf_t *out, const yaml_doc_t *doc,
3139+
const yaml_mapping_sequence_target_t *target) {
3140+
if (target->parent_line >= doc->line_count) {
3141+
return YAML_ERROR;
3142+
}
3143+
const yaml_line_t *header = &doc->lines[target->parent_line];
3144+
size_t comment = 0U;
3145+
if (target->insert_offset < header->end ||
3146+
yaml_find_comment(doc->data, target->parent_colon + YAML_UNIT, header->text_end,
3147+
&comment) != 0 ||
3148+
yaml_buf_append(out, doc->data, target->parent_colon + YAML_UNIT) != 0) {
3149+
return YAML_ERROR;
3150+
}
3151+
size_t kept_tail = header->text_end;
3152+
if (comment < header->text_end) {
3153+
if (yaml_buf_append_char(out, ' ') != 0) {
3154+
return YAML_ERROR;
3155+
}
3156+
kept_tail = comment;
3157+
}
3158+
if (yaml_buf_append(out, doc->data + kept_tail, header->end - kept_tail) != 0 ||
3159+
yaml_buf_append(out, doc->data + header->end, target->insert_offset - header->end) != 0 ||
3160+
(out->len > 0U && out->data[out->len - YAML_UNIT] != '\n' &&
3161+
yaml_buf_append(out, doc->eol, doc->eol_len) != 0)) {
3162+
return YAML_ERROR;
3163+
}
3164+
return 0;
3165+
}
3166+
30983167
static int yaml_sequence_build_insert(yaml_buf_t *out, const yaml_doc_t *doc,
30993168
const yaml_mapping_sequence_target_t *target,
31003169
const char *const *sequence_path, const size_t *path_lengths,
31013170
size_t sequence_path_len, const yaml_buf_t *rendered_item) {
3102-
if (target->insert_offset > doc->len ||
3103-
yaml_buf_append(out, doc->data, target->insert_offset) != 0 ||
3104-
yaml_append_separator_if_needed(out, doc, target->insert_offset) != 0) {
3171+
if (target->insert_offset > doc->len) {
3172+
return YAML_ERROR;
3173+
}
3174+
if (target->parent_inline_empty) {
3175+
if (yaml_sequence_append_expanded_parent(out, doc, target) != 0) {
3176+
return YAML_ERROR;
3177+
}
3178+
} else if (yaml_buf_append(out, doc->data, target->insert_offset) != 0 ||
3179+
yaml_append_separator_if_needed(out, doc, target->insert_offset) != 0) {
31053180
return YAML_ERROR;
31063181
}
31073182
if (!target->sequence_found) {

‎tests/test_cli.c‎

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
94549591
TEST(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

Comments
 (0)