Skip to content

Commit e8f0f4a

Browse files
committed
fix(cli): accept the emptied hooks: {} mapping in the Hermes hook editor
Removing the only Hermes pre_llm_call hook empties the hooks mapping, and the mapping editor writes an emptied section as hooks: {} - the same convention it uses for mcp_servers: {}. The hook sequence editor then refused that file for both upsert and removal, because it required every parent on the sequence path to be a block mapping header. On a config where our hook was the only entry under hooks, install -> uninstall -> install exited 1, and a repeated uninstall did too. An exact empty flow mapping on a parent of the sequence path is now read as that empty mapping, mirroring how the mapping-entry editor already treats section: {}. Removal is a byte-identical no-op, and upsert expands the header in place, keeping its comment, the line endings and every other byte, so a reinstall lands back on the first install byte for byte. The sequence key itself holding a mapping, any other flow value, and content nested under the inline mapping stay refused. Hooks written by the user and every foreign key are left untouched. Found while finishing DeusData#1862: its persisted hook opt-out runs this remover on every later install, so every Hermes user whose only hook was ours would have hit the refusal on each install after the first. Signed-off-by: Martin Vogel <[email protected]>
1 parent a5b248f commit e8f0f4a

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
@@ -9449,6 +9449,143 @@ TEST(cli_hermes_stable_shell_context_contract) {
94499449
PASS();
94509450
}
94519451

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

0 commit comments

Comments
 (0)