Skip to content

armgit / herdr

Public Imported from GitHub
0

the runtime your coding agents live on

fix: preserve hermes plugin list structure (#4984)

Eric Yuea9711f65b374
diff --git a/src/integration/config_edit.rs b/src/integration/config_edit.rs
index 812907e79..e7c72cb69 100644
--- a/src/integration/config_edit.rs
+++ b/src/integration/config_edit.rs
@@ -399,6 +399,9 @@ pub(crate) fn remove_hermes_plugin_enabled(content: &str) -> String {
update_hermes_enabled_plugin(content, false)
}
+/// Edits supported flat `plugins` and mapped `plugins.enabled` sequences.
+/// Unrelated sibling entries and comments are retained, and repeating the same
+/// enable or disable operation leaves the result unchanged.
pub(crate) fn update_hermes_enabled_plugin(content: &str, enabled: bool) -> String {
let trailing_newline = content.ends_with('\n');
let mut lines: Vec<String> = content.lines().map(str::to_string).collect();
@@ -416,126 +419,167 @@ pub(crate) fn update_hermes_enabled_plugin(content: &str, enabled: bool) -> Stri
let plugins_end =
next_top_level_yaml_key_index(&lines, plugins_index + 1).unwrap_or(lines.len());
- let plugins_inline_items = yaml_key_value_at_indent(&lines[plugins_index], 0, "plugins")
+ let first_child =
+ (plugins_index + 1..plugins_end).find(|&index| yaml_indent(&lines[index]).is_some());
+ // A list under disabled (or another mapping member) is not the flat plugins
+ // list. Classify the owner's first child before looking for sequence items.
+ let flat_sequence =
+ first_child.is_some_and(|index| yaml_list_item_value(&lines[index]).is_some());
+ let mapping_indent = first_child
+ .and_then(|index| yaml_indent(&lines[index]))
+ .filter(|&indent| indent > 0)
+ .unwrap_or(2);
+ let enabled_index = (!flat_sequence)
+ .then(|| {
+ (plugins_index + 1..plugins_end)
+ .find(|&index| yaml_key_at_indent(&lines[index], mapping_indent) == Some("enabled"))
+ })
+ .flatten();
+ let plugins_flow = yaml_key_value_at_indent(&lines[plugins_index], 0, "plugins")
.and_then(yaml_flow_sequence_items);
- let enabled_index = lines[plugins_index + 1..plugins_end]
- .iter()
- .position(|line| yaml_key_at_indent(line, 2) == Some("enabled"))
- .map(|offset| plugins_index + 1 + offset);
- let flat_list_start = lines[plugins_index + 1..plugins_end]
- .iter()
- .position(|line| yaml_list_item_value_at_indent(line, 2).is_some())
- .map(|offset| plugins_index + 1 + offset);
-
- if let Some(enabled_index) = enabled_index {
- if let Some(mut items) = yaml_key_value_at_indent(&lines[enabled_index], 2, "enabled")
- .and_then(yaml_flow_sequence_items)
- {
- let existing_item_index = items
- .iter()
- .position(|item| yaml_scalar_value(item) == HERMES_PLUGIN_INSTALL_NAME);
-
- match (enabled, existing_item_index) {
- (true, Some(_)) | (false, None) => return content.to_string(),
- (true, None) => items.insert(0, HERMES_PLUGIN_INSTALL_NAME.to_string()),
- (false, Some(index)) => {
- items.remove(index);
- }
- }
- let comment = yaml_inline_comment(&lines[enabled_index]);
- let replacement = hermes_enabled_plugin_lines(&items, comment);
- lines.splice(enabled_index..enabled_index + 1, replacement);
+ let (key_index, key_indent, key, flow_items) = if let Some(index) = enabled_index {
+ let items = yaml_key_value_at_indent(&lines[index], mapping_indent, "enabled")
+ .and_then(yaml_flow_sequence_items);
+ (index, mapping_indent, "enabled", items)
+ } else if flat_sequence || plugins_flow.is_some() {
+ (plugins_index, 0, "plugins", plugins_flow)
+ } else {
+ if enabled {
+ let indent = " ".repeat(mapping_indent);
+ lines.insert(plugins_index + 1, format!("{indent}enabled:"));
+ lines.insert(
+ plugins_index + 2,
+ format!("{indent} - {HERMES_PLUGIN_INSTALL_NAME}"),
+ );
return join_yaml_lines(lines, trailing_newline);
}
+ return content.to_string();
+ };
- let list_start = enabled_index + 1;
- let list_end = lines[list_start..plugins_end]
- .iter()
- .position(|line| {
- yaml_indent(line).is_some_and(|indent| indent <= 2) && yaml_key_name(line).is_some()
- })
- .map(|offset| list_start + offset)
- .unwrap_or(plugins_end);
- let existing_item_index = lines[list_start..list_end]
- .iter()
- .position(|line| yaml_list_item_matches(line, HERMES_PLUGIN_INSTALL_NAME))
- .map(|offset| list_start + offset);
-
- match (enabled, existing_item_index) {
- (true, Some(_)) | (false, None) => return content.to_string(),
- (true, None) => lines.insert(list_start, " - herdr-agent-state".to_string()),
- (false, Some(index)) => {
- lines.remove(index);
- }
- }
- return join_yaml_lines(lines, trailing_newline);
- }
-
- if let Some(mut items) = plugins_inline_items {
- let existing_item_index = items
+ if let Some(mut items) = flow_items {
+ let contains_plugin = items
.iter()
- .position(|item| yaml_scalar_value(item) == HERMES_PLUGIN_INSTALL_NAME);
-
- match (enabled, existing_item_index) {
- (true, Some(_)) | (false, None) => return content.to_string(),
- (true, None) => items.insert(0, HERMES_PLUGIN_INSTALL_NAME.to_string()),
- (false, Some(index)) => {
- items.remove(index);
+ .any(|item| yaml_scalar_value(item) == HERMES_PLUGIN_INSTALL_NAME);
+ match (enabled, contains_plugin) {
+ (true, true) | (false, false) => return content.to_string(),
+ (true, false) => items.insert(0, HERMES_PLUGIN_INSTALL_NAME.to_string()),
+ (false, true) => {
+ items.retain(|item| yaml_scalar_value(item) != HERMES_PLUGIN_INSTALL_NAME)
}
}
-
- let comment = yaml_inline_comment(&lines[plugins_index]);
- let replacement = hermes_flat_plugin_lines(&items, comment);
- lines.splice(plugins_index..plugins_end, replacement);
- return join_yaml_lines(lines, trailing_newline);
- }
-
- if let Some(flat_list_start) = flat_list_start {
- let existing_item_index = lines[plugins_index + 1..plugins_end]
+ let comment = yaml_inline_comment(&lines[key_index]);
+ let header = strip_yaml_inline_comment(&lines[key_index]).trim_end();
+ let content = yaml_key_value_at_indent(header, key_indent, key)
+ .map(yaml_node_content)
+ .unwrap_or_default();
+ let prefix = header[..header.len() - content.len()].trim_end();
+ let replacement = yaml_sequence_lines(prefix, key_indent, &items, comment);
+ // Only the header belongs to a flow sequence. Keep following comments.
+ lines.splice(key_index..key_index + 1, replacement);
+ } else {
+ let sequence = YamlBlockSequence::new(&lines, key_index, key_indent, plugins_end);
+ let matches: Vec<_> = sequence
+ .items
.iter()
- .position(|line| yaml_list_item_matches_at_indent(line, 2, HERMES_PLUGIN_INSTALL_NAME))
- .map(|offset| plugins_index + 1 + offset);
-
- match (enabled, existing_item_index) {
- (true, Some(_)) | (false, None) => return content.to_string(),
- (true, None) => lines.insert(flat_list_start, " - herdr-agent-state".to_string()),
- (false, Some(index)) => {
- lines.remove(index);
+ .filter(|item| {
+ yaml_list_item_matches_at_indent(
+ &lines[(*item).clone()],
+ sequence.indent,
+ HERMES_PLUGIN_INSTALL_NAME,
+ )
+ })
+ .cloned()
+ .collect();
+ match (enabled, matches.is_empty()) {
+ (true, false) | (false, true) => return content.to_string(),
+ (true, true) => lines.insert(
+ key_index + 1,
+ format!(
+ "{}- {HERMES_PLUGIN_INSTALL_NAME}",
+ " ".repeat(sequence.indent)
+ ),
+ ),
+ (false, false) => {
+ // Preserve both node properties and the sequence type, so aliases
+ // still refer to an empty list and reinstall can reuse the header.
+ if matches.len() == sequence.items.len()
+ && yaml_key_value_at_indent(&lines[key_index], key_indent, key)
+ .is_some_and(|value| yaml_node_content(value).is_empty())
+ {
+ lines[key_index] = with_yaml_inline_comment(
+ format!(
+ "{} []",
+ strip_yaml_inline_comment(&lines[key_index]).trim_end()
+ ),
+ yaml_inline_comment(&lines[key_index]),
+ );
+ }
+ for item in matches.into_iter().rev() {
+ for index in item.rev() {
+ // Remove the indicator and scalar, retaining standalone
+ // comments and blank lines surrounding the matched item.
+ if yaml_indent(&lines[index]).is_some() {
+ lines.remove(index);
+ }
+ }
+ }
}
}
- return join_yaml_lines(lines, trailing_newline);
- }
-
- if enabled {
- lines.insert(plugins_index + 1, " enabled:".to_string());
- lines.insert(plugins_index + 2, " - herdr-agent-state".to_string());
- return join_yaml_lines(lines, trailing_newline);
}
-
- content.to_string()
+ join_yaml_lines(lines, trailing_newline)
}
-pub(crate) fn hermes_flat_plugin_lines(items: &[String], comment: Option<&str>) -> Vec<String> {
- if items.is_empty() {
- return vec![with_yaml_inline_comment("plugins: []".to_string(), comment)];
+/// Direct items of one block sequence, including YAML's indentless form.
+/// Sibling keys bound the range before indentation or membership is inferred.
+struct YamlBlockSequence {
+ indent: usize,
+ items: Vec<std::ops::Range<usize>>,
+}
+
+impl YamlBlockSequence {
+ /// Finds direct items before the next sibling or ancestor key.
+ /// Empty sequences default to two spaces beyond the owner key.
+ fn new(lines: &[String], key_index: usize, key_indent: usize, parent_end: usize) -> Self {
+ let start = key_index + 1;
+ let end = (start..parent_end)
+ .find(|&index| {
+ yaml_indent(&lines[index]).is_some_and(|indent| {
+ indent < key_indent
+ || indent == key_indent && yaml_list_item_value(&lines[index]).is_none()
+ })
+ })
+ .unwrap_or(parent_end);
+ let indent = (start..end)
+ .find(|&index| yaml_indent(&lines[index]).is_some())
+ .and_then(|index| yaml_indent(&lines[index]))
+ .unwrap_or(key_indent + 2);
+ let starts: Vec<_> = (start..end)
+ .filter(|&index| yaml_list_item_value_at_indent(&lines[index], indent).is_some())
+ .collect();
+ let items = starts
+ .iter()
+ .enumerate()
+ .map(|(index, &start)| start..starts.get(index + 1).copied().unwrap_or(end))
+ .collect();
+ Self { indent, items }
}
-
- let mut lines = vec![with_yaml_inline_comment("plugins:".to_string(), comment)];
- lines.extend(items.iter().map(|item| format!(" - {item}")));
- lines
}
-pub(crate) fn hermes_enabled_plugin_lines(items: &[String], comment: Option<&str>) -> Vec<String> {
+/// Renders parsed flow items under the existing key and node properties.
+/// Empty sequences stay inline as `[]`, retaining the header comment.
+fn yaml_sequence_lines(
+ header: &str,
+ indent: usize,
+ items: &[String],
+ comment: Option<&str>,
+) -> Vec<String> {
+ let padding = " ".repeat(indent);
if items.is_empty() {
- return vec![with_yaml_inline_comment(
- " enabled: []".to_string(),
- comment,
- )];
+ return vec![with_yaml_inline_comment(format!("{header} []"), comment)];
}
-
- let mut lines = vec![with_yaml_inline_comment(" enabled:".to_string(), comment)];
- lines.extend(items.iter().map(|item| format!(" - {item}")));
+ let mut lines = vec![with_yaml_inline_comment(header.to_string(), comment)];
+ lines.extend(items.iter().map(|item| format!("{padding} - {item}")));
lines
}
@@ -602,11 +646,9 @@ pub(crate) fn yaml_indent(line: &str) -> Option<usize> {
}
pub(crate) fn yaml_list_item_value(line: &str) -> Option<&str> {
- line.trim().strip_prefix("- ").map(str::trim)
-}
-
-pub(crate) fn yaml_list_item_matches(line: &str, value: &str) -> bool {
- yaml_list_item_value(line).is_some_and(|item| yaml_scalar_value(item) == value)
+ let value = line.trim().strip_prefix('-')?;
+ // A bare indicator is still a list item; its value may be on the next line.
+ (value.is_empty() || value.starts_with(' ')).then(|| value.trim())
}
pub(crate) fn yaml_list_item_value_at_indent(line: &str, indent: usize) -> Option<&str> {
@@ -616,13 +658,44 @@ pub(crate) fn yaml_list_item_value_at_indent(line: &str, indent: usize) -> Optio
yaml_list_item_value(line)
}
-pub(crate) fn yaml_list_item_matches_at_indent(line: &str, indent: usize, value: &str) -> bool {
- yaml_list_item_value_at_indent(line, indent)
- .is_some_and(|item| yaml_scalar_value(item) == value)
+/// Matches a direct scalar, either inline or below a bare indicator. Additional
+/// content makes it a multiline scalar or collection, not the requested plugin.
+fn yaml_list_item_matches_at_indent(lines: &[String], indent: usize, value: &str) -> bool {
+ let Some(item) = lines
+ .first()
+ .and_then(|line| yaml_list_item_value_at_indent(line, indent))
+ else {
+ return false;
+ };
+ let mut continuation = lines[1..].iter().filter(|line| yaml_indent(line).is_some());
+ let scalar = if strip_yaml_inline_comment(item).is_empty() {
+ let Some(line) = continuation.next() else {
+ return false;
+ };
+ if yaml_indent(line).is_none_or(|depth| depth <= indent) {
+ return false;
+ }
+ line.trim()
+ } else {
+ item
+ };
+ continuation.next().is_none() && yaml_scalar_value(scalar) == value
+}
+
+/// Separates leading anchor/tag properties from a node's actual content.
+fn yaml_node_content(value: &str) -> &str {
+ let mut value = strip_yaml_inline_comment(value).trim();
+ while value.starts_with('&') || value.starts_with('!') {
+ value = value
+ .find(char::is_whitespace)
+ .map(|index| value[index..].trim_start())
+ .unwrap_or_default();
+ }
+ value
}
pub(crate) fn yaml_flow_sequence_items(value: &str) -> Option<Vec<String>> {
- let value = strip_yaml_inline_comment(value).trim();
+ let value = yaml_node_content(value);
let inner = value.strip_prefix('[')?.strip_suffix(']')?.trim();
if inner.is_empty() {
return Some(Vec::new());
diff --git a/src/integration/tests.rs b/src/integration/tests.rs
index c8db3790e..026387a4a 100644
--- a/src/integration/tests.rs
+++ b/src/integration/tests.rs
@@ -2926,6 +2926,283 @@ fn install_hermes_converts_inline_enabled_list_to_block_list() {
);
}
+#[test]
+fn install_hermes_preserves_indentless_enabled_list() {
+ let input = "plugins:\n enabled:\n - example-plugin\n disabled:\n - other-plugin\n";
+ let actual = update_hermes_enabled_plugin(input, true);
+ assert_eq!(
+ actual,
+ "plugins:\n enabled:\n - herdr-agent-state\n - example-plugin\n disabled:\n - other-plugin\n",
+ "new plugin must share the existing sequence indentation"
+ );
+}
+
+#[test]
+fn hermes_block_sequences_preserve_indentation_comments_and_siblings() {
+ for key_indent in [2, 4] {
+ for item_indent in [key_indent, key_indent + 2, key_indent + 4] {
+ let key_padding = " ".repeat(key_indent);
+ let item_padding = " ".repeat(item_indent);
+ let header = format!("plugins:\n{key_padding}enabled: # selected plugins\n");
+ let tail = format!(
+ "{item_padding}# existing plugin\n{item_padding}- 'example-plugin' # keep\n\n{key_padding}disabled:\n{key_padding}- herdr-agent-state\nmodel: example"
+ );
+ let input = format!("{header}{tail}");
+ let expected = format!("{header}{item_padding}- herdr-agent-state\n{tail}");
+ let installed = update_hermes_enabled_plugin(&input, true);
+ assert_eq!(installed, expected);
+ assert_eq!(update_hermes_enabled_plugin(&installed, true), installed);
+ assert_eq!(update_hermes_enabled_plugin(&installed, false), input);
+ assert_eq!(update_hermes_enabled_plugin(&input, false), input);
+ }
+ }
+ for indent in [0, 2, 4] {
+ let padding = " ".repeat(indent);
+ let input = format!(
+ "plugins: # selected\n# keep comment\n{padding}- other-plugin\nmodel: example\n"
+ );
+ let installed = update_hermes_enabled_plugin(&input, true);
+ assert_eq!(installed, format!("plugins: # selected\n{padding}- herdr-agent-state\n# keep comment\n{padding}- other-plugin\nmodel: example\n"));
+ assert_eq!(update_hermes_enabled_plugin(&installed, true), installed);
+ assert_eq!(update_hermes_enabled_plugin(&installed, false), input);
+ }
+}
+
+#[test]
+fn hermes_sequence_membership_is_scoped_to_direct_enabled_items() {
+ for input in [
+ "plugins:\n disabled:\n - herdr-agent-state\n enabled:\n - other\n",
+ "plugins:\n enabled:\n - options:\n - herdr-agent-state\n",
+ ] {
+ let expected = input.replacen(
+ " enabled:\n",
+ if input.contains("options:") {
+ " enabled:\n - herdr-agent-state\n"
+ } else {
+ " enabled:\n - herdr-agent-state\n"
+ },
+ 1,
+ );
+ let installed = update_hermes_enabled_plugin(input, true);
+ assert_eq!(installed, expected);
+ assert_eq!(update_hermes_enabled_plugin(&installed, false), input);
+ assert_eq!(update_hermes_enabled_plugin(input, false), input);
+ }
+}
+
+#[test]
+fn install_hermes_does_not_treat_disabled_as_a_flat_plugin_list() {
+ let input = "plugins:\n disabled:\n - herdr-agent-state\nmodel: example\n";
+ let installed = update_hermes_enabled_plugin(input, true);
+ assert_eq!(installed, "plugins:\n enabled:\n - herdr-agent-state\n disabled:\n - herdr-agent-state\nmodel: example\n");
+ assert_eq!(update_hermes_enabled_plugin(&installed, true), installed);
+ let uninstalled = update_hermes_enabled_plugin(&installed, false);
+ assert_eq!(
+ uninstalled,
+ "plugins:\n enabled: []\n disabled:\n - herdr-agent-state\nmodel: example\n"
+ );
+ assert_eq!(
+ update_hermes_enabled_plugin(&uninstalled, false),
+ uninstalled
+ );
+}
+
+#[test]
+fn uninstall_hermes_keeps_empty_sequences_and_removes_duplicate_direct_items() {
+ for (input, expected) in [
+ ("plugins:\n enabled: # keep\n - 'herdr-agent-state'\n - herdr-agent-state # duplicate\n disabled: []\n", "plugins:\n enabled: [] # keep\n disabled: []\n"),
+ ("plugins: # keep\n- herdr-agent-state\n", "plugins: [] # keep\n"),
+ ("plugins:\n enabled: [herdr-agent-state, herdr-agent-state] # keep\n", "plugins:\n enabled: [] # keep\n"),
+ ] {
+ let uninstalled = update_hermes_enabled_plugin(input, false);
+ assert_eq!(uninstalled, expected);
+ assert_eq!(update_hermes_enabled_plugin(&uninstalled, false), uninstalled);
+ }
+}
+
+#[test]
+fn uninstall_hermes_preserves_bare_sequence_items() {
+ for (header, indent) in [
+ ("plugins:\n", ""),
+ ("plugins:\n", " "),
+ ("plugins:\n enabled:\n", " "),
+ ("plugins:\n enabled:\n", " "),
+ ] {
+ for indicator in ["-", "- ", "- # keep"] {
+ let remaining = format!("{indent}{indicator}\n{indent} other-plugin\n");
+ for plugin_first in [true, false] {
+ let plugin = format!("{indent}- herdr-agent-state\n");
+ let input = if plugin_first {
+ format!("{header}{plugin}{remaining}model: example\n")
+ } else {
+ format!("{header}{remaining}{plugin}model: example\n")
+ };
+ let expected = format!("{header}{remaining}model: example\n");
+ let uninstalled = update_hermes_enabled_plugin(&input, false);
+ assert_eq!(uninstalled, expected, "input: {input}");
+ assert_eq!(update_hermes_enabled_plugin(&uninstalled, false), expected);
+ let installed = update_hermes_enabled_plugin(&uninstalled, true);
+ assert_eq!(
+ installed,
+ format!("{header}{plugin}{remaining}model: example\n")
+ );
+ }
+ }
+ }
+}
+
+#[test]
+fn uninstall_hermes_preserves_bare_item_in_config_file() {
+ let _lock = integration_env_lock();
+ let base = unique_base();
+ fs::create_dir_all(&base).unwrap();
+ std::env::set_var(HERMES_HOME_ENV_VAR, &base);
+ let config_path = base.join("config.yaml");
+ let remaining =
+ "plugins:\n enabled:\n -\n other-plugin\n disabled:\n - disabled-plugin\n";
+ fs::write(&config_path, remaining).unwrap();
+ install_hermes().unwrap();
+
+ let result = uninstall_hermes().unwrap();
+ assert!(result.removed_plugin_dir);
+ assert!(result.updated_config);
+ assert_eq!(fs::read_to_string(&config_path).unwrap(), remaining);
+ assert!(!uninstall_hermes().unwrap().updated_config);
+
+ std::env::remove_var(HERMES_HOME_ENV_VAR);
+ fs::remove_dir_all(base).unwrap();
+}
+
+#[test]
+fn uninstall_hermes_preserves_empty_sequence_node_properties() {
+ for properties in [
+ "&selected",
+ "!!seq &selected",
+ "&selected !!seq",
+ "!<tag:yaml.org,2002:seq> &selected",
+ ] {
+ for (header, indent, alias) in [
+ ("plugins:", " ", "other: *selected\n"),
+ ("plugins:\n enabled:", " ", " disabled: *selected\n"),
+ ] {
+ let input =
+ format!("{header} {properties} # keep\n{indent}- herdr-agent-state\n{alias}");
+ let expected = format!("{header} {properties} [] # keep\n{alias}");
+ let uninstalled = update_hermes_enabled_plugin(&input, false);
+ assert_eq!(uninstalled, expected);
+ assert_eq!(update_hermes_enabled_plugin(&uninstalled, false), expected);
+ assert_eq!(update_hermes_enabled_plugin(&uninstalled, true), input);
+
+ let flow =
+ format!("{header} {properties} [herdr-agent-state, other-plugin] # keep\n{alias}");
+ assert_eq!(
+ update_hermes_enabled_plugin(&flow, false),
+ format!("{header} {properties} # keep\n{indent}- other-plugin\n{alias}")
+ );
+ }
+ }
+}
+
+#[test]
+fn hermes_bare_plugin_items_use_the_continued_scalar() {
+ for (header, indent) in [
+ ("plugins:\n", ""),
+ ("plugins:\n", " "),
+ ("plugins:\n enabled:\n", " "),
+ ("plugins:\n enabled:\n", " "),
+ ] {
+ for indicator in ["-", "- ", "- # plugin"] {
+ for value in [
+ "herdr-agent-state",
+ "'herdr-agent-state'",
+ "\"herdr-agent-state\" # installed",
+ ] {
+ let plugin = format!("{indent}{indicator}\n{indent} {value}\n");
+ let other = format!("{indent}-\n{indent} other-plugin\n");
+ for plugin_first in [true, false] {
+ let items = if plugin_first {
+ format!("{plugin}{other}")
+ } else {
+ format!("{other}{plugin}")
+ };
+ let input = format!("{header}{items}model: example\n");
+ assert_eq!(update_hermes_enabled_plugin(&input, true), input);
+ let expected = format!("{header}{other}model: example\n");
+ assert_eq!(update_hermes_enabled_plugin(&input, false), expected);
+ assert_eq!(update_hermes_enabled_plugin(&expected, false), expected);
+ }
+ }
+ }
+ }
+ let input = "plugins:\n enabled:\n - # plugin\n # keep comment\n\n herdr-agent-state\n - herdr-agent-state # duplicate\n # keep trailing comment\n disabled: []\n";
+ assert_eq!(
+ update_hermes_enabled_plugin(input, false),
+ "plugins:\n enabled: []\n # keep comment\n\n # keep trailing comment\n disabled: []\n"
+ );
+}
+
+#[test]
+fn hermes_does_not_match_scalar_prefixes_or_nested_continuations() {
+ for item in [
+ " - herdr-agent-state\n other\n",
+ " -\n herdr-agent-state\n other\n",
+ " -\n - herdr-agent-state\n",
+ ] {
+ let input = format!("plugins:\n enabled:\n{item} disabled: []\n");
+ assert_eq!(update_hermes_enabled_plugin(&input, false), input);
+ let installed = update_hermes_enabled_plugin(&input, true);
+ assert_eq!(
+ installed,
+ format!("plugins:\n enabled:\n - herdr-agent-state\n{item} disabled: []\n")
+ );
+ assert_eq!(update_hermes_enabled_plugin(&installed, false), input);
+ }
+}
+
+#[test]
+fn hermes_flow_sequence_edits_keep_following_comments() {
+ for (input, expected) in [
+ ("plugins: [other] # selected\n# keep following comment\nmodel: example\n", "plugins: # selected\n - herdr-agent-state\n - other\n# keep following comment\nmodel: example\n"),
+ ("plugins:\n enabled: [other] # selected\n # keep following comment\n disabled: []\n", "plugins:\n enabled: # selected\n - herdr-agent-state\n - other\n # keep following comment\n disabled: []\n"),
+ ] {
+ assert_eq!(update_hermes_enabled_plugin(input, true), expected);
+ assert_eq!(update_hermes_enabled_plugin(expected, true), expected);
+ }
+}
+
+#[test]
+fn hermes_install_and_uninstall_are_idempotent_with_indentless_config() {
+ let _lock = integration_env_lock();
+ let base = unique_base();
+ fs::create_dir_all(&base).unwrap();
+ std::env::set_var(HERMES_HOME_ENV_VAR, &base);
+ let input = "plugins:\n enabled:\n - example-plugin\n disabled:\n - other-plugin\n";
+ let config_path = base.join("config.yaml");
+ fs::write(&config_path, input).unwrap();
+
+ let installed = install_hermes().unwrap();
+ let first = fs::read_to_string(&config_path).unwrap();
+ assert_eq!(first, "plugins:\n enabled:\n - herdr-agent-state\n - example-plugin\n disabled:\n - other-plugin\n");
+ assert!(installed
+ .plugin_dir
+ .join(HERMES_PLUGIN_INIT_INSTALL_NAME)
+ .is_file());
+ install_hermes().unwrap();
+ assert_eq!(fs::read_to_string(&config_path).unwrap(), first);
+
+ let uninstalled = uninstall_hermes().unwrap();
+ assert!(uninstalled.removed_plugin_dir);
+ assert!(uninstalled.updated_config);
+ assert_eq!(fs::read_to_string(&config_path).unwrap(), input);
+ let again = uninstall_hermes().unwrap();
+ assert!(!again.removed_plugin_dir);
+ assert!(!again.updated_config);
+ assert_eq!(fs::read_to_string(&config_path).unwrap(), input);
+
+ std::env::remove_var(HERMES_HOME_ENV_VAR);
+ fs::remove_dir_all(base).unwrap();
+}
+
#[test]
fn install_hermes_preserves_quoted_inline_enabled_items() {
let config =