Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(doctor): sanitize Hermes profile diagnostics

AkitaOnRails9ff85b63aa92
diff --git a/crates/ai-memory-cli/src/commands/doctor/hermes.rs b/crates/ai-memory-cli/src/commands/doctor/hermes.rs
index 5b4edaff..e1672982 100644
--- a/crates/ai-memory-cli/src/commands/doctor/hermes.rs
+++ b/crates/ai-memory-cli/src/commands/doctor/hermes.rs
@@ -63,8 +63,8 @@ pub(crate) fn reports(root: &Path) -> Vec<HermesProfileReport> {
.filter(|entry| entry.path().is_dir())
.filter_map(|entry| {
let name = entry.file_name().into_string().ok()?;
- // `.deleted` holds tombstones, not a profile.
- (!name.starts_with('.')).then(|| (name, entry.path()))
+ crate::commands::path_util::is_hermes_profile_id(&name)
+ .then(|| (name, entry.path()))
})
.collect();
profiles.sort();
@@ -268,10 +268,17 @@ fn is_ai_memory_hermes_hook(command: &str) -> bool {
pub(crate) fn render(reports: &[HermesProfileReport]) -> String {
let mut out = String::new();
for report in reports {
+ let profile = crate::commands::show::terminal_text(&report.profile);
+ let home = crate::commands::show::terminal_text(&report.home);
+ let events = report
+ .not_allowlisted
+ .iter()
+ .map(|event| crate::commands::show::terminal_text(event))
+ .collect::<Vec<_>>();
let flag = if report.profile == "default" {
String::new()
} else {
- format!(" -p {}", report.profile)
+ format!(" -p {profile}")
};
let hooks = report.hook_events.len();
let state = if hooks == 0 {
@@ -286,10 +293,7 @@ pub(crate) fn render(reports: &[HermesProfileReport]) -> String {
report.not_allowlisted.len()
)
};
- out.push_str(&format!(
- " hermes ({}): {} ({state})\n",
- report.profile, report.home
- ));
+ out.push_str(&format!(" hermes ({}): {} ({state})\n", profile, home));
if hooks > 0 && !report.auto_accept && !report.not_allowlisted.is_empty() {
out.push_str(&format!(
"⚠ Hermes profile {profile}: {events} not in {home}/{ALLOWLIST_FILENAME}, so \
@@ -297,9 +301,9 @@ pub(crate) fn render(reports: &[HermesProfileReport]) -> String {
logs \"not allowlisted — skipped\"). Approve them once with `hermes{flag} \
--accept-hooks chat`, or run `hermes{flag}` and accept the prompt; a running \
gateway picks the approval up. `hermes{flag} hooks doctor` shows each hook.\n",
- profile = report.profile,
- events = report.not_allowlisted.join(", "),
- home = report.home,
+ profile = profile,
+ events = events.join(", "),
+ home = home,
));
}
}
@@ -494,6 +498,7 @@ mod tests {
)
.unwrap();
std::fs::create_dir_all(bots.join("empty")).unwrap();
+ write_home(&bots.join("bad;profile"), &wired, &[]);
let reports = reports(root.path());
let summary: Vec<(&str, usize, Vec<&str>, bool)> = reports
@@ -539,6 +544,20 @@ mod tests {
assert!(!text.contains("--server-url"), "commands stay out: {text}");
}
+ #[test]
+ fn human_report_strips_terminal_controls_from_paths_and_events() {
+ let report = HermesProfileReport {
+ profile: "default".into(),
+ home: "/tmp/unsafe\u{1b}[31m".into(),
+ hook_events: vec!["event\u{1b}[31m".into()],
+ not_allowlisted: vec!["event\u{1b}[31m".into()],
+ auto_accept: false,
+ };
+
+ let text = render(&[report]);
+ assert!(!text.contains('\u{1b}'), "{text:?}");
+ }
+
/// A missing or unreadable allowlist approves nothing, as in Hermes; no
/// Hermes home means no report at all.
#[test]
diff --git a/crates/ai-memory-cli/src/commands/hook.rs b/crates/ai-memory-cli/src/commands/hook.rs
index 800e6fd7..1cb07201 100644
--- a/crates/ai-memory-cli/src/commands/hook.rs
+++ b/crates/ai-memory-cli/src/commands/hook.rs
@@ -1064,8 +1064,10 @@ fn hermes_profile_marker_cwd(runtime_env: &RuntimeEnv, profile: Option<&str>) ->
if !hermes_in_gateway(runtime_env) || runtime_env.ignore_marker() {
return None;
}
- let profile = profile
- .filter(|name| *name != HERMES_GATEWAY_HOST_PROFILE && is_hermes_profile_id(name))?;
+ let profile = profile.filter(|name| {
+ *name != HERMES_GATEWAY_HOST_PROFILE
+ && crate::commands::path_util::is_hermes_profile_id(name)
+ })?;
let hermes_home = crate::commands::path_util::agent_config_home(
runtime_env.var_os("HERMES_HOME"),
)
@@ -1090,18 +1092,6 @@ fn hermes_profile_marker_cwd(runtime_env: &RuntimeEnv, profile: Option<&str>) ->
dir.to_str().map(str::to_owned)
}
-/// `^[a-z0-9][a-z0-9_-]{0,63}$`, Hermes' `PROFILE_ID_RE`.
-fn is_hermes_profile_id(name: &str) -> bool {
- let bytes = name.as_bytes();
- bytes.len() <= 64
- && bytes
- .first()
- .is_some_and(|first| first.is_ascii_lowercase() || first.is_ascii_digit())
- && bytes
- .iter()
- .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || *b == b'_' || *b == b'-')
-}
-
/// Run a single hook end-to-end. Always returns Ok and always writes a JSON
/// object to stdout — a hook must never fail the agent.
///
@@ -8816,8 +8806,12 @@ mod tests {
"{name}"
);
}
- assert!(is_hermes_profile_id(&"a".repeat(64)));
- assert!(!is_hermes_profile_id(&"a".repeat(65)));
+ assert!(crate::commands::path_util::is_hermes_profile_id(
+ &"a".repeat(64)
+ ));
+ assert!(!crate::commands::path_util::is_hermes_profile_id(
+ &"a".repeat(65)
+ ));
// Control: the valid id still routes.
assert!(hermes_profile_marker_cwd(&gateway, Some("sc-seo")).is_some());
}
diff --git a/crates/ai-memory-cli/src/commands/path_util.rs b/crates/ai-memory-cli/src/commands/path_util.rs
index 85537049..99bfbe6e 100644
--- a/crates/ai-memory-cli/src/commands/path_util.rs
+++ b/crates/ai-memory-cli/src/commands/path_util.rs
@@ -61,6 +61,18 @@ pub(crate) fn agent_config_home(env_override: Option<std::ffi::OsString>) -> Opt
ai_memory_workstream::env_dir_override(env_override)
}
+/// Hermes profile ids follow `^[a-z0-9][a-z0-9_-]{0,63}$`.
+pub(crate) fn is_hermes_profile_id(name: &str) -> bool {
+ let bytes = name.as_bytes();
+ bytes.len() <= 64
+ && bytes
+ .first()
+ .is_some_and(|first| first.is_ascii_lowercase() || first.is_ascii_digit())
+ && bytes
+ .iter()
+ .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || *b == b'_' || *b == b'-')
+}
+
/// Claude Code's relocated config root: `$CLAUDE_CONFIG_DIR` when set, else
/// `None`. Named alias kept so Claude call sites read for what they resolve.
pub(crate) fn claude_config_dir(env_override: Option<std::ffi::OsString>) -> Option<PathBuf> {