Skip to content

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

Rust

feat(upgrade): refactor agent directory listing into a separate function

Ruben Licio Reis5f472456a6d9
diff --git a/crates/ai-memory-cli/src/commands/upgrade.rs b/crates/ai-memory-cli/src/commands/upgrade.rs
index 206c9d99..8392745c 100644
--- a/crates/ai-memory-cli/src/commands/upgrade.rs
+++ b/crates/ai-memory-cli/src/commands/upgrade.rs
@@ -423,20 +423,14 @@ fn copy_dir_recursive(src: &Path, dest: &Path) -> Result<()> {
Ok(())
}
-fn refresh_staged_hooks(config: &Config) -> Result<()> {
- let staging = install_hooks::hook_staging_root(
- &config.data_dir,
- ai_memory_wiki::backup::running_in_container(),
- );
- let hooks_root = staging.join("hooks");
- if !hooks_root.is_dir() {
- println!("→ no staged hook scripts found at {}", hooks_root.display());
- println!(" (nothing to refresh — install-hooks hasn't been run with --apply yet)");
- return Ok(());
- }
-
+/// List agent dirs under a staged hooks root, skipping shared helper dirs.
+///
+/// `lib` and `_*-prefixed` directories hold shared helpers, not agents (#38).
+/// Unknown directory names are omitted (callers may log them).
+fn list_staged_agents(hooks_root: &Path) -> Result<(Vec<(String, AgentChoice)>, Vec<String>)> {
let mut agents = Vec::new();
- for entry in fs::read_dir(&hooks_root)
+ let mut unknown = Vec::new();
+ for entry in fs::read_dir(hooks_root)
.with_context(|| format!("reading staged hooks at {}", hooks_root.display()))?
{
let entry = entry?;
@@ -447,17 +441,35 @@ fn refresh_staged_hooks(config: &Config) -> Result<()> {
let Some(name) = name.to_str() else {
continue;
};
- // `lib` and `_*-prefixed` dirs hold shared helpers, not agents (#38).
if name == "lib" || name.starts_with('_') {
continue;
}
match AgentChoice::from_str(name, true) {
Ok(agent) => agents.push((name.to_string(), agent)),
- Err(_) => {
- println!(" (skipping unknown staged agent dir `{name}`)");
- }
+ Err(_) => unknown.push(name.to_string()),
}
}
+ agents.sort_by(|a, b| a.0.cmp(&b.0));
+ unknown.sort();
+ Ok((agents, unknown))
+}
+
+fn refresh_staged_hooks(config: &Config) -> Result<()> {
+ let staging = install_hooks::hook_staging_root(
+ &config.data_dir,
+ ai_memory_wiki::backup::running_in_container(),
+ );
+ let hooks_root = staging.join("hooks");
+ if !hooks_root.is_dir() {
+ println!("→ no staged hook scripts found at {}", hooks_root.display());
+ println!(" (nothing to refresh — install-hooks hasn't been run with --apply yet)");
+ return Ok(());
+ }
+
+ let (agents, unknown) = list_staged_agents(&hooks_root)?;
+ for name in &unknown {
+ println!(" (skipping unknown staged agent dir `{name}`)");
+ }
if agents.is_empty() {
println!(
@@ -907,4 +919,33 @@ mod tests {
assert!(is_loopback_url("http://localhost:49374"));
assert!(!is_loopback_url("http://192.168.1.10:49374"));
}
+
+ #[test]
+ fn list_staged_agents_keeps_known_and_skips_helpers() -> Result<()> {
+ let dir = tempfile::tempdir()?;
+ let hooks = dir.path();
+ for name in ["claude-code", "cursor", "lib", "_shared", "not-an-agent"] {
+ fs::create_dir(hooks.join(name))?;
+ }
+ fs::write(hooks.join("README.md"), b"ignore files")?;
+
+ let (agents, unknown) = list_staged_agents(hooks)?;
+ let names: Vec<_> = agents.iter().map(|(n, _)| n.as_str()).collect();
+ assert_eq!(names, ["claude-code", "cursor"]);
+ assert_eq!(unknown, ["not-an-agent"]);
+ assert!(agents.iter().all(|(_, a)| {
+ matches!(a, AgentChoice::ClaudeCode | AgentChoice::Cursor)
+ }));
+ Ok(())
+ }
+
+ #[test]
+ fn list_staged_agents_empty_root_returns_empty() -> Result<()> {
+ let dir = tempfile::tempdir()?;
+ fs::create_dir(dir.path().join("lib"))?;
+ let (agents, unknown) = list_staged_agents(dir.path())?;
+ assert!(agents.is_empty());
+ assert!(unknown.is_empty());
+ Ok(())
+ }
}