Skip to content

Commit 029919c

Browse files
authored
Merge pull request DeusData#2355 from DeusData/fix/issue-2209
fix(install): Hermes hook edit no longer refuses unrelated YAML sections (DeusData#2209)
2 parents 1ff6299 + ae578bc commit 029919c

2 files changed

Lines changed: 86 additions & 12 deletions

File tree

‎src/cli/config_yaml_edit.c‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2698,8 +2698,28 @@ static int yaml_sequence_line_has_unsupported(const yaml_doc_t *doc, const yaml_
26982698
return quote != '\0' ? YAML_MATCH : 0;
26992699
}
27002700

2701-
static int yaml_sequence_validate_document(const yaml_doc_t *doc) {
2702-
for (size_t i = 0U; i < doc->line_count; i++) {
2701+
/* Validates only the top-level section the edit descends into. The root
2702+
* mapping itself was already validated when the document was parsed, so the
2703+
* section boundaries are sound; every OTHER top-level section is opaque user
2704+
* content, the same contract the mapping-entry editor applies. Validating the
2705+
* whole document refused the stock Hermes config for flow sequences such as
2706+
* `cli: [hermes-cli]` under `platform_toolsets:`, which the hook edit never
2707+
* reads or rewrites (#2209). An absent root key means the sequence is appended
2708+
* as a new top-level section, which touches no existing line. */
2709+
static int yaml_sequence_validate_document(const yaml_doc_t *doc, const char *root_key,
2710+
size_t root_key_len) {
2711+
bool found = false;
2712+
size_t section_line = 0U;
2713+
size_t section_colon = 0U;
2714+
if (yaml_find_unique_key(doc, 0U, root_key, root_key_len, &found, &section_line,
2715+
&section_colon) != 0) {
2716+
return YAML_ERROR;
2717+
}
2718+
if (!found) {
2719+
return 0;
2720+
}
2721+
size_t section_end = yaml_top_level_section_end(doc, section_line);
2722+
for (size_t i = section_line; i < section_end; i++) {
27032723
const yaml_line_t *line = &doc->lines[i];
27042724
if (line->blank || line->comment || line->dquote_cont) {
27052725
continue;
@@ -2947,7 +2967,7 @@ static int yaml_sequence_analyze(const yaml_doc_t *doc, const char *const *seque
29472967
const char *identity_key, const char *identity_value,
29482968
yaml_mapping_sequence_target_t *target) {
29492969
memset(target, 0, sizeof(*target));
2950-
if (yaml_sequence_validate_document(doc) != 0) {
2970+
if (yaml_sequence_validate_document(doc, sequence_path[0], path_lengths[0]) != 0) {
29512971
return YAML_ERROR;
29522972
}
29532973
size_t parent_begin = 0U;

‎tests/test_config_yaml_edit.c‎

Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,12 +1228,14 @@ TEST(config_yaml_edit_hermes_accepts_interior_brackets_in_plain_scalar_issue1924
12281228

12291229
TEST(config_yaml_edit_hermes_hook_still_refuses_block_scalar_value_issue1924) {
12301230
/* A `>` or `|` that BEGINS a value is a real block scalar whose
1231-
* indentation contract this editor does not model: the fail-closed
1232-
* refusal stays, and the file stays byte-identical. */
1231+
* indentation contract this editor does not model: inside the `hooks`
1232+
* subtree the edit descends into, the fail-closed refusal stays and the
1233+
* file stays byte-identical. The same construct in an untouched section
1234+
* is opaque user content (#2209). */
12331235
static const char *const initials[] = {
1234-
"agent:\n personalities:\n probe: >\n folded face\n",
1235-
"agent:\n personalities:\n probe: |\n literal face\n",
1236-
"agent:\n personalities:\n probe: >-\n chomped face\n",
1236+
"hooks:\n probe: >\n folded face\n",
1237+
"hooks:\n probe: |\n literal face\n",
1238+
"hooks:\n probe: >-\n chomped face\n",
12371239
};
12381240
for (size_t i = 0U; i < sizeof(initials) / sizeof(initials[0]); i++) {
12391241
yaml_fixture_t fixture;
@@ -1250,10 +1252,10 @@ TEST(config_yaml_edit_hermes_hook_still_refuses_block_scalar_value_issue1924) {
12501252

12511253
TEST(config_yaml_edit_hermes_hook_still_refuses_nonempty_flow_sequence_value_issue1924) {
12521254
/* Pins the #1631 line: exact `[]` is accepted, a non-empty flow sequence
1253-
* value is still unsupported in the sequence-document scan. */
1254-
const char *initial = "agent:\n"
1255-
" personalities:\n"
1256-
" probe: [a, b]\n";
1255+
* value inside the edited `hooks` subtree is still unsupported. Outside
1256+
* it, the value is opaque user content (#2209). */
1257+
const char *initial = "hooks:\n"
1258+
" probe: [a, b]\n";
12571259
yaml_fixture_t fixture;
12581260
ASSERT_EQ(yaml_fixture_init(&fixture, initial), 0);
12591261
ASSERT_EQ(yaml_hermes_hook_upsert(&fixture), CBM_YAML_IDENTITY_EDIT_ERROR);
@@ -2027,6 +2029,57 @@ TEST(config_yaml_edit_remove_on_absent_config_is_a_no_op) {
20272029
PASS();
20282030
}
20292031

2032+
/* #2209: the stock Hermes config carries flow sequences and block scalars in
2033+
* sections the hook edit never touches (`platform_toolsets:` -> `cli:
2034+
* [hermes-cli]`, `plugins:` -> `enabled: [...]`, `|` prompts). They are opaque
2035+
* user content, exactly as for the mapping-entry editor that writes
2036+
* `mcp_servers` into the same file; only the `hooks` subtree is validated. */
2037+
TEST(config_yaml_edit_nested_sequence_ignores_foreign_flow_and_block_scalars_issue2209) {
2038+
const char *initial = "plugins:\n"
2039+
" enabled: [disk-cleanup]\n"
2040+
"platform_toolsets:\n"
2041+
" cli: [hermes-cli]\n"
2042+
" telegram: [hermes-telegram]\n"
2043+
"toolsets: [all]\n"
2044+
"agent:\n"
2045+
" system_prompt: |\n"
2046+
" You are helpful.\n"
2047+
" - keep: [this, text]\n"
2048+
" note: >\n"
2049+
" folded\n"
2050+
"model:\n"
2051+
" default: \"anthropic/claude-opus-4.6\"\n";
2052+
yaml_fixture_t fixture;
2053+
ASSERT_EQ(yaml_fixture_init(&fixture, initial), 0);
2054+
ASSERT_EQ(cbm_yaml_upsert_mapping_sequence_item(fixture.path, yaml_hook_sequence_path, 2U, "id",
2055+
yaml_hook_identity, yaml_hook_canonical_item),
2056+
CBM_YAML_IDENTITY_EDIT_OK);
2057+
char *installed = yaml_read_alloc(fixture.path);
2058+
ASSERT_NOT_NULL(installed);
2059+
ASSERT_EQ(strncmp(installed, initial, strlen(initial)), 0);
2060+
ASSERT_NOT_NULL(strstr(installed, "hooks:\n"
2061+
" pre_llm_call:\n"
2062+
" - id: \"codebase-memory-mcp\"\n"));
2063+
ASSERT_EQ(cbm_yaml_upsert_mapping_sequence_item(fixture.path, yaml_hook_sequence_path, 2U, "id",
2064+
yaml_hook_identity, yaml_hook_canonical_item),
2065+
CBM_YAML_IDENTITY_EDIT_OK);
2066+
char *second = yaml_read_alloc(fixture.path);
2067+
ASSERT_NOT_NULL(second);
2068+
ASSERT_STR_EQ(second, installed);
2069+
free(second);
2070+
free(installed);
2071+
ASSERT_EQ(cbm_yaml_remove_mapping_sequence_item(fixture.path, yaml_hook_sequence_path, 2U, "id",
2072+
yaml_hook_identity, yaml_hook_canonical_item),
2073+
CBM_YAML_IDENTITY_EDIT_OK);
2074+
char *removed = yaml_read_alloc(fixture.path);
2075+
ASSERT_NOT_NULL(removed);
2076+
ASSERT_NULL(strstr(removed, "codebase-memory-mcp"));
2077+
ASSERT_EQ(strncmp(removed, initial, strlen(initial)), 0);
2078+
free(removed);
2079+
th_cleanup(fixture.dir);
2080+
PASS();
2081+
}
2082+
20302083
TEST(config_yaml_edit_nested_sequence_ambiguity_fails_byte_identically) {
20312084
const char *cases[] = {
20322085
"hooks:\n pre_llm_call:\n - id: \"bad-indent\"\n",
@@ -2192,6 +2245,7 @@ SUITE(config_yaml_edit) {
21922245
RUN_TEST(config_yaml_edit_nested_sequence_preserves_crlf);
21932246
RUN_TEST(config_yaml_edit_nested_sequence_foreign_identity_is_preserved);
21942247
RUN_TEST(config_yaml_edit_nested_sequence_removes_only_exact_canonical_item);
2248+
RUN_TEST(config_yaml_edit_nested_sequence_ignores_foreign_flow_and_block_scalars_issue2209);
21952249
RUN_TEST(config_yaml_edit_nested_sequence_ambiguity_fails_byte_identically);
21962250
RUN_TEST(config_yaml_edit_remove_on_absent_config_is_a_no_op);
21972251
#ifndef _WIN32

0 commit comments

Comments
 (0)