Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(upgrade): skip the user-level Claude re-apply when no install is there
Talysson de Oliveira Cassiano9ea81bb9cb83
diff --git a/crates/ai-memory-cli/src/commands/upgrade.rs b/crates/ai-memory-cli/src/commands/upgrade.rsindex 5dade4ed..e71075bf 100644--- a/crates/ai-memory-cli/src/commands/upgrade.rs+++ b/crates/ai-memory-cli/src/commands/upgrade.rs@@ -827,6 +827,24 @@ fn refresh_agent_list(config: &Config, agents: &StagedAgentList) {}fn apply_staged_agent_hooks(config: &Config, name: &str, agent: AgentChoice) {+ if agent == AgentChoice::ClaudeCode+ && let Some(settings) = claude_user_level_install_absent()+ {+ println!(+ " (no ai-memory hooks in {} — skipping the user-level re-apply; a \+ `--scope project` install keeps pointing at the staged scripts, so those are \+ refreshed here)",+ settings.display()+ );+ match install_hooks::restage_claude_code_scripts(&config.data_dir, None) {+ Ok(staged) => println!(" restaged {}", staged.display()),+ Err(err) => println!(+ " (skipped — {err:#}; re-run `ai-memory install-hooks \+ --agent {name} --scope project --apply` in each checkout)"+ ),+ }+ return;+ }println!(" ai-memory install-hooks --agent {name} --apply");if let Err(err) = install_hooks::run(config, apply_hooks_args(agent)) {println!(@@ -835,6 +853,28 @@ fn apply_staged_agent_hooks(config: &Config, name: &str, agent: AgentChoice) {}}+/// The user-level Claude Code settings path when that file provably holds no+/// ai-memory install: it is missing, or parses without one of our hooks. A+/// `--scope project` install stages the same scripts without touching that+/// file, so a staged `claude-code` dir alone no longer proves a user-level+/// install exists — and a bare re-apply would create one the operator never+/// asked for. `None` when the file carries our hooks, and also when it cannot+/// be read or parsed: the re-apply then runs and reports that error, as it+/// did before project scope existed.+fn claude_user_level_install_absent() -> Option<PathBuf> {+ claude_user_level_install_absent_at(install_hooks::claude_settings_path().ok()?)+}++fn claude_user_level_install_absent_at(path: PathBuf) -> Option<PathBuf> {+ let content = match fs::read_to_string(&path) {+ Ok(content) => content,+ Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Some(path),+ Err(_) => return None,+ };+ let document: serde_json::Value = serde_json::from_str(&content).ok()?;+ (!install_hooks::document_carries_ai_memory_hooks(&document)).then_some(path)+}+fn apply_hooks_args(agent: AgentChoice) -> InstallHooksArgs {InstallHooksArgs {agent,@@ -1616,6 +1656,62 @@ mod tests {assert!(!content_length_exceeds_limit(0, 1));}+ /// The claude-code refresh guard: a missing or third-party-only user-level+ /// settings file means the hooks live in a `--scope project` file, and the+ /// refresh must not create a user-level install. A file that exists but+ /// does not parse is not "absent": the re-apply must run and report it.+ #[test]+ fn claude_refresh_skips_only_a_provably_absent_user_level_install() {+ let tmp = tempfile::TempDir::new().unwrap();+ let settings = tmp.path().join("settings.json");+ let absent = |why: &str| {+ assert_eq!(+ claude_user_level_install_absent_at(settings.clone()).as_deref(),+ Some(settings.as_path()),+ "{why}"+ );+ };+ let present = |why: &str| {+ assert_eq!(+ claude_user_level_install_absent_at(settings.clone()),+ None,+ "{why}"+ );+ };+ absent("a missing file holds no install");+ fs::write(+ &settings,+ r#"{"hooks":{"Notification":[{"matcher":"","hooks":[{"type":"command","command":"/usr/bin/n.sh"}]}]}}"#,+ )+ .unwrap();+ absent("a third-party hook is not an ai-memory install");+ fs::write(&settings, "{ not json").unwrap();+ present("an unparsable file must reach install_hooks::run so the error surfaces");+ fs::write(+ &settings,+ r#"{"hooks":{"Stop":[{"matcher":"","hooks":[{"type":"command","command":"AI_MEMORY_HOOK_URL=http://127.0.0.1:49374 /h/.local/share/ai-memory/hooks/claude-code/stop.sh"}]}]}}"#,+ )+ .unwrap();+ present("our hook means a user-level install to refresh");+ }++ /// With only project-scoped installs, the refresh must still re-copy the+ /// scripts those installs point at.+ #[test]+ fn restage_refreshes_the_claude_scripts_without_a_settings_file() {+ let data = tempfile::TempDir::new().unwrap();+ let hooks_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../hooks");+ let staged =+ install_hooks::restage_claude_code_scripts(data.path(), Some(&hooks_dir)).unwrap();+ assert!(staged.starts_with(data.path()), "{staged:?}");+ let scripts = fs::read_dir(&staged)+ .unwrap()+ .filter_map(Result::ok)+ .filter(|entry| entry.path().extension().is_some_and(|ext| ext == "sh"))+ .count();+ assert!(scripts > 0, "no hook scripts staged under {staged:?}");+ }+#[test]fn is_loopback_detects_common_forms() {assert!(is_loopback_url("http://127.0.0.1:49374"));