Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
feat(render): thread project_strategy through hook_command arms
João Matheuscc9f4fab97ee
diff --git a/crates/ai-memory-cli/src/commands/install_hooks.rs b/crates/ai-memory-cli/src/commands/install_hooks.rsindex eb20d47c..5d82296f 100644--- a/crates/ai-memory-cli/src/commands/install_hooks.rs+++ b/crates/ai-memory-cli/src/commands/install_hooks.rs@@ -173,12 +173,13 @@ pub fn run(config: &Config, args: InstallHooksArgs) -> Result<()> {AgentChoice::Openclaw => openclaw_plugin::apply(&server_url, auth, &args),};}+ let strategy = args.project_strategy.baked();match args.agent {AgentChoice::OpenCode => render_opencode_plugin(&server_url, auth),AgentChoice::Omp => render_omp_extension(&server_url, auth),AgentChoice::ClaudeCode => {let hooks_dir = resolve_hooks_dir(args.hooks_dir.as_deref(), args.agent)?;- render_claude_code(&hooks_dir, &server_url, auth, &config.data_dir)+ render_claude_code(&hooks_dir, &server_url, auth, &config.data_dir, strategy)}AgentChoice::Codex => {let hooks_dir = resolve_hooks_dir(args.hooks_dir.as_deref(), args.agent)?;@@ -198,7 +199,7 @@ pub fn run(config: &Config, args: InstallHooksArgs) -> Result<()> {}AgentChoice::Grok => {let hooks_dir = resolve_hooks_dir(args.hooks_dir.as_deref(), args.agent)?;- render_grok(&hooks_dir, &server_url, auth, &config.data_dir)+ render_grok(&hooks_dir, &server_url, auth, &config.data_dir, strategy)}AgentChoice::Openclaw => {openclaw_plugin::render(&server_url, auth);@@ -467,11 +468,13 @@ fn apply_to_claude_code_settings(};let staged = stage_hook_scripts(hooks_dir, "claude-code")?;let command_dir = staged_command_dir(&staged, "claude-code");+ let strategy = args.project_strategy.baked();let payload = build_claude_code_payload_with_data_dir(&command_dir,server_url,auth_token,Some(data_dir),+ strategy,);let our_hooks = payload.get("hooks")@@ -529,8 +532,14 @@ fn apply_to_grok_settings(};let staged = stage_hook_scripts(hooks_dir, "grok")?;let command_dir = staged_command_dir(&staged, "grok");- let payload =- build_grok_payload_with_data_dir(&command_dir, server_url, auth_token, Some(data_dir));+ let strategy = args.project_strategy.baked();+ let payload = build_grok_payload_with_data_dir(+ &command_dir,+ server_url,+ auth_token,+ Some(data_dir),+ strategy,+ );let our_hooks = payload.get("hooks").and_then(|v| v.as_object())@@ -598,7 +607,15 @@ fn apply_to_codex_settings(};let staged = stage_hook_scripts(hooks_dir, "codex")?;let command_dir = staged_command_dir(&staged, "codex");- let outcome = merge_codex_hooks(&command_dir, server_url, auth_token, data_dir, &path)?;+ let strategy = args.project_strategy.baked();+ let outcome = merge_codex_hooks(+ &command_dir,+ server_url,+ auth_token,+ data_dir,+ strategy,+ &path,+ )?;println!("✓ {} {} ({})",outcome.verb(),@@ -628,6 +645,7 @@ fn merge_codex_hooks(server_url: &str,auth_token: Option<&str>,data_dir: &Path,+ project_strategy: Option<&str>,config_path: &Path,) -> Result<ApplyOutcome> {// Build the Codex-flavoured payload. The JSON shape is identical@@ -640,6 +658,7 @@ fn merge_codex_hooks(auth_token,"codex",Some(data_dir),+ project_strategy,);let our_hooks = payload.get("hooks")@@ -699,7 +718,15 @@ fn apply_to_cursor_settings(};let staged = stage_hook_scripts(hooks_dir, "cursor")?;let command_dir = staged_command_dir(&staged, "cursor");- let outcome = merge_cursor_hooks(&command_dir, server_url, auth_token, data_dir, &path)?;+ let strategy = args.project_strategy.baked();+ let outcome = merge_cursor_hooks(+ &command_dir,+ server_url,+ auth_token,+ data_dir,+ strategy,+ &path,+ )?;println!("✓ {} {} ({})",outcome.verb(),@@ -718,6 +745,7 @@ fn merge_cursor_hooks(server_url: &str,auth_token: Option<&str>,data_dir: &Path,+ project_strategy: Option<&str>,config_path: &Path,) -> Result<ApplyOutcome> {let payload = build_profile_payload_for_agent(@@ -727,6 +755,7 @@ fn merge_cursor_hooks(auth_token,"cursor",Some(data_dir),+ project_strategy,);let our_hooks = payload.get("hooks")@@ -781,7 +810,15 @@ fn apply_to_gemini_settings(};let staged = stage_hook_scripts(hooks_dir, "gemini-cli")?;let command_dir = staged_command_dir(&staged, "gemini-cli");- let outcome = merge_gemini_hooks(&command_dir, server_url, auth_token, data_dir, &path)?;+ let strategy = args.project_strategy.baked();+ let outcome = merge_gemini_hooks(+ &command_dir,+ server_url,+ auth_token,+ data_dir,+ strategy,+ &path,+ )?;println!("✓ {} {} ({})",outcome.verb(),@@ -800,6 +837,7 @@ fn merge_gemini_hooks(server_url: &str,auth_token: Option<&str>,data_dir: &Path,+ project_strategy: Option<&str>,config_path: &Path,) -> Result<ApplyOutcome> {let payload = build_profile_payload_for_agent(@@ -809,6 +847,7 @@ fn merge_gemini_hooks(auth_token,"gemini-cli",Some(data_dir),+ project_strategy,);let our_hooks = payload.get("hooks")@@ -855,7 +894,15 @@ fn apply_to_antigravity_settings(};let staged = stage_hook_scripts(hooks_dir, "antigravity-cli")?;let command_dir = staged_command_dir(&staged, "antigravity-cli");- let outcome = merge_antigravity_hooks(&command_dir, server_url, auth_token, data_dir, &path)?;+ let strategy = args.project_strategy.baked();+ let outcome = merge_antigravity_hooks(+ &command_dir,+ server_url,+ auth_token,+ data_dir,+ strategy,+ &path,+ )?;println!("✓ {} {} ({})",outcome.verb(),@@ -874,10 +921,16 @@ fn merge_antigravity_hooks(server_url: &str,auth_token: Option<&str>,data_dir: &Path,+ project_strategy: Option<&str>,config_path: &Path,) -> Result<ApplyOutcome> {- let payload =- build_antigravity_payload_with_data_dir(staged, server_url, auth_token, Some(data_dir));+ let payload = build_antigravity_payload_with_data_dir(+ staged,+ server_url,+ auth_token,+ Some(data_dir),+ project_strategy,+ );let our_group = payload.get("ai-memory").and_then(|v| v.as_object())@@ -1846,6 +1899,7 @@ fn render_claude_code(server_url: &str,auth_token: Option<&str>,data_dir: &Path,+ project_strategy: Option<&str>,) -> Result<()> {// Soft check: warn (don't bail) if a script is missing. The user// may be running this command inside docker against a host path@@ -1864,8 +1918,13 @@ fn render_claude_code();}}- let payload =- build_claude_code_payload_with_data_dir(hooks_dir, server_url, auth_token, Some(data_dir));+ let payload = build_claude_code_payload_with_data_dir(+ hooks_dir,+ server_url,+ auth_token,+ Some(data_dir),+ project_strategy,+ );let serialized =serde_json::to_string_pretty(&payload).context("serializing claude code hook config")?;println!("# Claude Code hook config — merge into ~/.claude/settings.json");@@ -1885,6 +1944,7 @@ fn render_grok(server_url: &str,auth_token: Option<&str>,data_dir: &Path,+ project_strategy: Option<&str>,) -> Result<()> {// Soft check (same rationale as render_claude_code): warn, don't bail,// so the docker host-path flow still works.@@ -1901,8 +1961,13 @@ fn render_grok();}}- let payload =- build_grok_payload_with_data_dir(hooks_dir, server_url, auth_token, Some(data_dir));+ let payload = build_grok_payload_with_data_dir(+ hooks_dir,+ server_url,+ auth_token,+ Some(data_dir),+ project_strategy,+ );let serialized =serde_json::to_string_pretty(&payload).context("serializing grok hook config")?;println!("# Grok Build CLI hook config — write to ~/.grok/hooks/ai-memory.json");@@ -1923,6 +1988,7 @@ fn render_grok(#[cfg(test)]mod tests {use super::*;+ use crate::cli::ProjectStrategyArg;use std::collections::BTreeMap;use std::fs;#[cfg(unix)]@@ -2034,6 +2100,7 @@ mod tests {as_user: None,apply: true,config_file: None,+ project_strategy: ProjectStrategyArg::Basename,}}@@ -2681,6 +2748,7 @@ model = "gpt-5"as_user: None,apply: true,config_file: Some(tmp.path().join("extensions").join("ai-memory.ts")),+ project_strategy: ProjectStrategyArg::Basename,};let path = resolve_omp_extension_path(&args).unwrap();@@ -2769,6 +2837,7 @@ model = "gpt-5""http://127.0.0.1:49374",None,config_tmp.path(),+ None,&config_path,).unwrap();@@ -2816,6 +2885,7 @@ model = "gpt-5""http://127.0.0.1:49374",None,config_tmp.path(),+ None,&config_path,).unwrap();@@ -2830,6 +2900,7 @@ model = "gpt-5""http://127.0.0.1:49374",None,config_tmp.path(),+ None,&config_path,).unwrap();@@ -2865,6 +2936,7 @@ model = "gpt-5""http://127.0.0.1:49374",None,config_tmp.path(),+ None,&config_path,).unwrap();@@ -2910,6 +2982,7 @@ model = "gpt-5""http://127.0.0.1:49374",None,config_tmp.path(),+ None,&config_path,).unwrap();@@ -2954,6 +3027,7 @@ model = "gpt-5""http://127.0.0.1:49374",None,config_tmp.path(),+ None,&config_path,).unwrap();@@ -3012,6 +3086,7 @@ model = "gpt-5""http://127.0.0.1:49374",None,config_tmp.path(),+ None,&config_path,).unwrap();@@ -3063,6 +3138,7 @@ model = "gpt-5""http://127.0.0.1:49374",None,config_tmp.path(),+ None,&config_path,).unwrap();@@ -3077,6 +3153,7 @@ model = "gpt-5""http://127.0.0.1:49374",None,config_tmp.path(),+ None,&config_path,).unwrap();diff --git a/crates/ai-memory-cli/src/commands/render_shared.rs b/crates/ai-memory-cli/src/commands/render_shared.rsindex 58231754..c16f021f 100644--- a/crates/ai-memory-cli/src/commands/render_shared.rs+++ b/crates/ai-memory-cli/src/commands/render_shared.rs@@ -109,6 +109,7 @@ pub(crate) fn build_claude_code_payload(HookCommandPlatform::for_bash_script_runner(),"claude-code",None,+ None,),)}@@ -118,6 +119,7 @@ pub(crate) fn build_claude_code_payload_with_data_dir(server_url: &str,auth_token: Option<&str>,data_dir: Option<&Path>,+ project_strategy: Option<&str>,) -> serde_json::Value {build_hook_payload_for_platform(&CLAUDE_CODE_EVENTS,@@ -129,6 +131,7 @@ pub(crate) fn build_claude_code_payload_with_data_dir(HookCommandPlatform::for_bash_runner(),"claude-code",data_dir,+ project_strategy,),)}@@ -149,7 +152,12 @@ pub(crate) fn build_grok_payload(server_url,auth_token,HookShape::Nested,- HookCommandContext::new(HookCommandPlatform::for_bash_script_runner(), "grok", None),+ HookCommandContext::new(+ HookCommandPlatform::for_bash_script_runner(),+ "grok",+ None,+ None,+ ),)}@@ -160,6 +168,7 @@ pub(crate) fn build_grok_payload_with_data_dir(server_url: &str,auth_token: Option<&str>,data_dir: Option<&Path>,+ project_strategy: Option<&str>,) -> serde_json::Value {build_hook_payload_for_platform(&CLAUDE_CODE_EVENTS,@@ -167,7 +176,12 @@ pub(crate) fn build_grok_payload_with_data_dir(server_url,auth_token,HookShape::Nested,- HookCommandContext::new(HookCommandPlatform::for_bash_runner(), "grok", data_dir),+ HookCommandContext::new(+ HookCommandPlatform::for_bash_runner(),+ "grok",+ data_dir,+ project_strategy,+ ),)}@@ -292,6 +306,7 @@ pub(crate) fn build_antigravity_payload_with_data_dir(server_url: &str,auth_token: Option<&str>,data_dir: Option<&Path>,+ project_strategy: Option<&str>,) -> serde_json::Value {build_antigravity_payload_for_platform(emit_root,@@ -300,6 +315,7 @@ pub(crate) fn build_antigravity_payload_with_data_dir(HookCommandPlatform::current(),"antigravity-cli",data_dir,+ project_strategy,)}@@ -310,6 +326,7 @@ fn build_antigravity_payload_for_platform(platform: HookCommandPlatform,agent: &str,data_dir: Option<&Path>,+ project_strategy: Option<&str>,) -> serde_json::Value {let mut group = serde_json::Map::new();@@ -321,7 +338,7 @@ fn build_antigravity_payload_for_platform(&abs,server_url,auth_token,- HookCommandContext::new(platform, agent, data_dir),+ HookCommandContext::new(platform, agent, data_dir, project_strategy),);group.insert((*event).to_string(),@@ -343,7 +360,7 @@ fn build_antigravity_payload_for_platform(&abs,server_url,auth_token,- HookCommandContext::new(platform, agent, data_dir),+ HookCommandContext::new(platform, agent, data_dir, project_strategy),);group.insert((*event).to_string(),@@ -375,6 +392,7 @@ pub(crate) fn build_profile_payload(auth_token,"claude-code",None,+ None,)}@@ -385,6 +403,7 @@ pub(crate) fn build_profile_payload_for_agent(auth_token: Option<&str>,agent: &str,data_dir: Option<&Path>,+ project_strategy: Option<&str>,) -> serde_json::Value {build_hook_payload(profile.events,@@ -392,7 +411,12 @@ pub(crate) fn build_profile_payload_for_agent(server_url,auth_token,profile.shape,- HookCommandContext::new(HookCommandPlatform::current(), agent, data_dir),+ HookCommandContext::new(+ HookCommandPlatform::current(),+ agent,+ data_dir,+ project_strategy,+ ),)}@@ -437,6 +461,9 @@ struct HookCommandContext<'a> {platform: HookCommandPlatform,agent: &'a str,data_dir: Option<&'a Path>,+ /// Install-time default project strategy baked into the command+ /// (`install-hooks --project-strategy`). `None` bakes nothing.+ project_strategy: Option<&'a str>,}impl<'a> HookCommandContext<'a> {@@ -444,11 +471,13 @@ impl<'a> HookCommandContext<'a> {platform: HookCommandPlatform,agent: &'a str,data_dir: Option<&'a Path>,+ project_strategy: Option<&'a str>,) -> Self {Self {platform,agent,data_dir,+ project_strategy,}}}@@ -599,6 +628,9 @@ fn hook_command(if let Some(t) = auth_token {prefix.push_str(&format!("AI_MEMORY_AUTH_TOKEN={} ", shell_quote(t)));}+ if let Some(s) = context.project_strategy {+ prefix.push_str(&format!("AI_MEMORY_PROJECT_STRATEGY={} ", shell_quote(s)));+ }format!("{prefix}{}", shell_quote(&script.to_string_lossy()))}HookCommandPlatform::Windows => {@@ -609,6 +641,12 @@ fn hook_command(powershell_quote(t)));}+ if let Some(s) = context.project_strategy {+ setup.push_str(&format!(+ "; $env:AI_MEMORY_PROJECT_STRATEGY={}",+ powershell_quote(s)+ ));+ }format!("powershell.exe -NoProfile -ExecutionPolicy Bypass -Command \"{setup}; & {}\"",powershell_quote(&script.to_string_lossy())@@ -620,6 +658,9 @@ fn hook_command(if let Some(t) = auth_token {inner.push_str(&format!("AI_MEMORY_AUTH_TOKEN={} ", shell_quote(t)));}+ if let Some(s) = context.project_strategy {+ inner.push_str(&format!("AI_MEMORY_PROJECT_STRATEGY={} ", shell_quote(s)));+ }inner.push_str(&shell_quote(&bash_path));format!("bash -c {}", shell_quote(&inner))}@@ -652,6 +693,10 @@ fn hook_command(if let Some(t) = auth_token {cmd.push_str(&format!(" --auth-token {}", win_double_quote(t)));}+ cmd.push_str(&native_project_strategy_arg(+ context.project_strategy,+ NativeQuote::Windows,+ ));cmd}HookCommandPlatform::PosixNative => {@@ -676,6 +721,10 @@ fn hook_command(if let Some(t) = auth_token {cmd.push_str(&format!(" --auth-token {}", shell_quote(t)));}+ cmd.push_str(&native_project_strategy_arg(+ context.project_strategy,+ NativeQuote::Posix,+ ));cmd}}@@ -700,6 +749,19 @@ fn native_data_dir_arg(data_dir: Option<&Path>, quote: NativeQuote) -> String {}}+/// Append ` --project-strategy <value>` to a native hook command when an+/// install-time default was baked in (`install-hooks --project-strategy`).+/// `None` appends nothing, keeping the command byte-identical to before.+fn native_project_strategy_arg(strategy: Option<&str>, quote: NativeQuote) -> String {+ let Some(strategy) = strategy else {+ return String::new();+ };+ match quote {+ NativeQuote::Posix => format!(" --project-strategy {}", shell_quote(strategy)),+ NativeQuote::Windows => format!(" --project-strategy {}", win_double_quote(strategy)),+ }+}+/// Convert a Windows path to Git Bash (MSYS2) format./// `C:\Users\alice\hooks\x.sh` → `/c/Users/alice/hooks/x.sh`fn to_git_bash_path(path: &str) -> String {@@ -763,7 +825,7 @@ mod tests {server_url,auth_token,shape,- HookCommandContext::new(HookCommandPlatform::Posix, "claude-code", None),+ HookCommandContext::new(HookCommandPlatform::Posix, "claude-code", None, None),)}@@ -798,7 +860,7 @@ mod tests {"http://localhost:49374",None,HookShape::Nested,- HookCommandContext::new(HookCommandPlatform::PosixNative, "grok", None),+ HookCommandContext::new(HookCommandPlatform::PosixNative, "grok", None, None),);let command = v.pointer("/hooks/SessionStart/0/hooks/0/command")@@ -817,7 +879,7 @@ mod tests {"http://localhost:49374",None,HookShape::Nested,- HookCommandContext::new(HookCommandPlatform::Posix, "grok", None),+ HookCommandContext::new(HookCommandPlatform::Posix, "grok", None, None),);let command = v.pointer("/hooks/SessionStart/0/hooks/0/command")@@ -1032,7 +1094,12 @@ mod tests {"http://h:49374",Some("tok"),HookShape::Nested,- HookCommandContext::new(HookCommandPlatform::WindowsNative, "claude-code", None),+ HookCommandContext::new(+ HookCommandPlatform::WindowsNative,+ "claude-code",+ None,+ None,+ ),);// Each native command must carry `hook --event <stem>` where <stem>// matches the .sh script the other platforms invoke — so the server@@ -1089,7 +1156,7 @@ mod tests {&PathBuf::from("/tmp/hooks dir/session-start.sh"),"http://localhost:49374/mcp?x=1&y=2",Some("tok;rm -rf /"),- HookCommandContext::new(HookCommandPlatform::Posix, "claude-code", None),+ HookCommandContext::new(HookCommandPlatform::Posix, "claude-code", None, None),);assert!(@@ -1115,7 +1182,7 @@ mod tests {"http://localhost:49374",Some("tok'en"),HookShape::Nested,- HookCommandContext::new(HookCommandPlatform::Windows, "claude-code", None),+ HookCommandContext::new(HookCommandPlatform::Windows, "claude-code", None, None),);let cmd = v.pointer("/hooks/SessionStart/0/hooks/0/command")@@ -1144,6 +1211,7 @@ mod tests {HookCommandPlatform::Posix,"antigravity-cli",None,+ None,);// Top-level key is the named group "ai-memory", not "hooks"@@ -1246,7 +1314,7 @@ mod tests {),"https://my-server.example.com",Some("tok123"),- HookCommandContext::new(HookCommandPlatform::WindowsBash, "claude-code", None),+ HookCommandContext::new(HookCommandPlatform::WindowsBash, "claude-code", None, None),);assert!(cmd.starts_with("bash -c "),@@ -1276,7 +1344,7 @@ mod tests {&PathBuf::from(r"C:\Users\alice\hooks\session-start.sh"),"http://localhost:49374",None,- HookCommandContext::new(HookCommandPlatform::WindowsBash, "claude-code", None),+ HookCommandContext::new(HookCommandPlatform::WindowsBash, "claude-code", None, None),);assert!(cmd.starts_with("bash -c "));assert!(@@ -1297,7 +1365,7 @@ mod tests {&PathBuf::from("/home/alice/.local/share/ai-memory/hooks/claude-code/session-start.sh"),"https://my-server.example.com",Some("tok123"),- HookCommandContext::new(HookCommandPlatform::PosixNative, "claude-code", None),+ HookCommandContext::new(HookCommandPlatform::PosixNative, "claude-code", None, None),);assert!(cmd.contains("hook --event session-start"),@@ -1326,6 +1394,7 @@ mod tests {HookCommandPlatform::PosixNative,"codex",Some(Path::new("/home/alice/.local/share/custom memory")),+ None,),);assert!(cmd.contains("hook --event pre-tool-use"), "{cmd}");@@ -1350,6 +1419,7 @@ mod tests {HookCommandPlatform::WindowsNative,"claude-code",Some(Path::new(r"\\?\C:\Users\me\AppData\Local\ai-memory")),+ None,),);assert!(@@ -1372,7 +1442,7 @@ mod tests {"https://my-server.example.com",Some("tok123"),HookShape::Nested,- HookCommandContext::new(HookCommandPlatform::WindowsBash, "claude-code", None),+ HookCommandContext::new(HookCommandPlatform::WindowsBash, "claude-code", None, None),);let cmd = v.pointer("/hooks/SessionStart/0/hooks/0/command")