Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(install-hooks): warn about Cursor hook wrappers that mention ai-memory
Gaalbu5bf4b32ee0ba
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f82367a3..219b4d87 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0## [Unreleased]+### Fixed+- `install-hooks --agent cursor --apply` now warns about existing+ `~/.cursor/hooks.json` entries that mention ai-memory but are not ai-memory+ hook entries, such as a pre-2.1.0 shim that injected `cwd`. Those entries+ are kept beside the native ones, so every event would otherwise be captured+ twice without any sign (#721).+## [2.2.1] - 2026-09-12### Fixeddiff --git a/crates/ai-memory-cli/src/commands/install_hooks.rs b/crates/ai-memory-cli/src/commands/install_hooks.rsindex 81e0ba81..416d490f 100644--- a/crates/ai-memory-cli/src/commands/install_hooks.rs+++ b/crates/ai-memory-cli/src/commands/install_hooks.rs@@ -2058,9 +2058,60 @@ fn apply_to_cursor_settings(ApplyOutcome::NoOp => "already up to date",});+ warn_unrecognized_ai_memory_hooks(&path);Ok(())}+/// Warn about hook entries that mention ai-memory but are not recognised as+/// ours, e.g. a wrapper shim that injects `cwd` before calling the binary.+/// `install-hooks` keeps those beside the native entries it adds, so every+/// event would then be captured twice (#721). Best effort: never fails apply.+fn warn_unrecognized_ai_memory_hooks(path: &Path) {+ let Ok(raw) = fs::read_to_string(path) else {+ return;+ };+ let Ok(root) = serde_json::from_str::<serde_json::Value>(&raw) else {+ return;+ };+ for (event, command) in unrecognized_ai_memory_hook_entries(&root) {+ eprintln!(+ "# warning: {} `{event}` hook `{command}` mentions ai-memory but is not an \+ ai-memory hook entry, so it was kept beside the native one and the event \+ may be captured twice. Remove it if it wraps ai-memory.",+ path.display()+ );+ }+}++fn unrecognized_ai_memory_hook_entries(root: &serde_json::Value) -> Vec<(String, String)> {+ let Some(hooks) = root.get("hooks").and_then(serde_json::Value::as_object) else {+ return Vec::new();+ };+ let mut found = Vec::new();+ for (event, entries) in hooks {+ let Some(entries) = entries.as_array() else {+ continue;+ };+ for entry in entries.iter().filter(|e| !is_ai_memory_hook_entry(e)) {+ let handlers = entry+ .get("hooks")+ .and_then(serde_json::Value::as_array)+ .map_or_else(|| vec![entry], |inner| inner.iter().collect());+ for handler in handlers {+ let Some(command) = handler.get("command").and_then(serde_json::Value::as_str)+ else {+ continue;+ };+ let lower = command.to_ascii_lowercase();+ if lower.contains("ai-memory") || lower.contains("ai_memory") {+ found.push((event.clone(), command.to_string()));+ }+ }+ }+ }+ found+}+fn merge_cursor_hooks(staged: &Path,server_url: &str,@@ -8966,6 +9017,37 @@ model = "gpt-5");}+ #[test]+ fn unrecognized_ai_memory_hook_entries_flags_wrappers_only() {+ let root = serde_json::json!({+ "version": 1,+ "hooks": {+ "sessionStart": [+ {"command": "/usr/bin/ai-memory", "args": ["hook", "--event", "session-start", "--agent", "cursor", "--server-url", "http://h"]},+ {"command": "~/bin/ai-memory-cwd-shim.sh session-start"},+ {"command": "/opt/other-tool --flag"}+ ],+ "stop": [+ {"matcher": "", "hooks": [{"command": "/home/u/.local/ai_memory_wrap stop"}]}+ ]+ }+ });+ let found = unrecognized_ai_memory_hook_entries(&root);+ assert_eq!(+ found,+ vec![+ (+ "sessionStart".to_string(),+ "~/bin/ai-memory-cwd-shim.sh session-start".to_string()+ ),+ (+ "stop".to_string(),+ "/home/u/.local/ai_memory_wrap stop".to_string()+ ),+ ]+ );+ }+#[test]fn cursor_apply_is_idempotent() {let hooks_tmp = TempDir::new().unwrap();