Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(install-hooks): honor project strategy in manual hooks
AkitaOnRails8b8bfed8d532
diff --git a/README.md b/README.mdindex a749ef43..9ce52c88 100644--- a/README.md+++ b/README.md@@ -279,6 +279,10 @@ other server / hook you have configured, and write a timestampedscripts are staged into `~/.local/share/ai-memory/hooks/<agent>/`automatically; re-running overwrites them so future image updates shipupdated hooks. Drop `--apply` to print the snippet instead of mutating.+If your agent often starts inside repository subdirectories or linked+worktrees, add `--project-strategy repo-root` to `install-hooks` so captures+collapse to the main git repo name; see [`docs/install.md`](docs/install.md)+and [`docs/marker-file.md`](docs/marker-file.md) for details.The Docker wrapper also bridges thin-client commands such as`ai-memory status` and `ai-memory bootstrap` back to the host'sdiff --git a/crates/ai-memory-cli/src/cli.rs b/crates/ai-memory-cli/src/cli.rsindex a7f1418d..88759ed8 100644--- a/crates/ai-memory-cli/src/cli.rs+++ b/crates/ai-memory-cli/src/cli.rs@@ -1079,8 +1079,8 @@ pub struct HookArgs {/// Default project strategy baked in by `install-hooks/// --project-strategy`. Applies only when a `.ai-memory.toml`/// marker does not pin its own `project_strategy`.- #[arg(long)]- pub project_strategy: Option<String>,+ #[arg(long, value_enum)]+ pub project_strategy: Option<ProjectStrategyArg>,}/// Arguments for `install-hooks`.@@ -1626,6 +1626,26 @@ mod tests {);}+ #[test]+ fn hook_project_strategy_rejects_invalid_value() {+ let result = Cli::try_parse_from([+ "ai-memory",+ "hook",+ "--event",+ "session-start",+ "--agent",+ "claude-code",+ "--server-url",+ "http://127.0.0.1:49374",+ "--project-strategy",+ "bogus",+ ]);+ assert!(+ result.is_err(),+ "an unknown hook --project-strategy value must be rejected by value_enum"+ );+ }+#[test]fn vscode_copilot_aliases_parse_to_same_variant() {for alias in ["vscode-copilot", "copilot", "github-copilot"] {diff --git a/crates/ai-memory-cli/src/commands/hook.rs b/crates/ai-memory-cli/src/commands/hook.rsindex 4a519596..844ea431 100644--- a/crates/ai-memory-cli/src/commands/hook.rs+++ b/crates/ai-memory-cli/src/commands/hook.rs@@ -142,7 +142,7 @@ pub async fn run(data_dir: Option<PathBuf>, args: HookArgs) -> anyhow::Result<()let json: serde_json::Value = serde_json::from_str(&payload).unwrap_or(serde_json::Value::Null);let qs = extract_cwd(&json)- .map(|cwd| marker_query_suffix(&cwd, args.project_strategy.as_deref()))+ .map(|cwd| marker_query_suffix(&cwd, args.project_strategy.and_then(|s| s.baked()))).unwrap_or_default();let base = args.server_url.trim_end_matches('/');let dd = resolve_data_dir(data_dir.as_deref());diff --git a/crates/ai-memory-cli/src/commands/install_hooks.rs b/crates/ai-memory-cli/src/commands/install_hooks.rsindex e393f3b1..25a89a53 100644--- a/crates/ai-memory-cli/src/commands/install_hooks.rs+++ b/crates/ai-memory-cli/src/commands/install_hooks.rs@@ -183,19 +183,19 @@ pub fn run(config: &Config, args: InstallHooksArgs) -> Result<()> {}AgentChoice::Codex => {let hooks_dir = resolve_hooks_dir(args.hooks_dir.as_deref(), args.agent)?;- render_agent("codex", &hooks_dir, &server_url, auth)+ render_agent("codex", &hooks_dir, &server_url, auth, strategy)}AgentChoice::Cursor => {let hooks_dir = resolve_hooks_dir(args.hooks_dir.as_deref(), args.agent)?;- render_agent("cursor", &hooks_dir, &server_url, auth)+ render_agent("cursor", &hooks_dir, &server_url, auth, strategy)}AgentChoice::GeminiCli => {let hooks_dir = resolve_hooks_dir(args.hooks_dir.as_deref(), args.agent)?;- render_agent("gemini-cli", &hooks_dir, &server_url, auth)+ render_agent("gemini-cli", &hooks_dir, &server_url, auth, strategy)}AgentChoice::AntigravityCli => {let hooks_dir = resolve_hooks_dir(args.hooks_dir.as_deref(), args.agent)?;- render_agent("antigravity-cli", &hooks_dir, &server_url, auth)+ render_agent("antigravity-cli", &hooks_dir, &server_url, auth, strategy)}AgentChoice::Grok => {let hooks_dir = resolve_hooks_dir(args.hooks_dir.as_deref(), args.agent)?;@@ -1634,29 +1634,59 @@ fn render_agent(hooks_dir: &Path,server_url: &str,auth_token: Option<&str>,+ project_strategy: Option<&str>,) -> Result<()> {- println!("# {label} hook scripts (manual install — wire each to the matching event)");- println!("# Hook scripts: {}", hooks_dir.display());- println!("# AI-memory server URL: {server_url}");+ print!(+ "{}",+ render_agent_output(label, hooks_dir, server_url, auth_token, project_strategy)?+ );+ Ok(())+}++fn render_agent_output(+ label: &str,+ hooks_dir: &Path,+ server_url: &str,+ auth_token: Option<&str>,+ project_strategy: Option<&str>,+) -> Result<String> {+ let mut out = String::new();+ out.push_str(&format!(+ "# {label} hook scripts (manual install — wire each to the matching event)\n"+ ));+ out.push_str(&format!("# Hook scripts: {}\n", hooks_dir.display()));+ out.push_str(&format!("# AI-memory server URL: {server_url}\n"));if auth_token.is_some() {- println!("# Auth: set AI_MEMORY_AUTH_TOKEN in each hook's environment to the");- println!("# value passed via --auth-token (omitted from this printout).");+ out.push_str("# Auth: set AI_MEMORY_AUTH_TOKEN in each hook's environment to the\n");+ out.push_str("# value passed via --auth-token (omitted from this printout).\n");} else {- println!("# Auth: server requires no bearer token. To require one, generate a");- println!("# token with `ai-memory generate-auth-token` and pass it via");- println!("# --auth-token here AND set AI_MEMORY_AUTH_TOKEN on the server.");+ out.push_str("# Auth: server requires no bearer token. To require one, generate a\n");+ out.push_str("# token with `ai-memory generate-auth-token` and pass it via\n");+ out.push_str("# --auth-token here AND set AI_MEMORY_AUTH_TOKEN on the server.\n");}- println!();+ out.push('\n');for entry in std::fs::read_dir(hooks_dir)? {let entry = entry?;let p = entry.path();if p.is_file() && p.extension().is_some_and(|e| e == hook_script_extension()) {- println!("- {}", p.display());+ out.push_str(&format!("- {}\n", p.display()));}}- println!();- println!("Set AI_MEMORY_HOOK_URL in each hook's environment to override the default.");- Ok(())+ out.push('\n');+ out.push_str("Set AI_MEMORY_HOOK_URL in each hook's environment to override the default.\n");+ if let Some(instruction) = manual_agent_project_strategy_instruction(project_strategy) {+ out.push_str(&instruction);+ out.push('\n');+ }+ Ok(out)+}++fn manual_agent_project_strategy_instruction(project_strategy: Option<&str>) -> Option<String> {+ project_strategy.map(|strategy| {+ format!(+ "Set AI_MEMORY_PROJECT_STRATEGY={strategy} in each hook's environment to use the requested project strategy."+ )+ })}/// Copy the bundled hook scripts to a stable user-global location@@ -2198,6 +2228,36 @@ mod tests {assert!(validate_as_user(Some("alice"), Some("some-token")).is_ok());}+ #[test]+ fn manual_agent_render_mentions_repo_root_project_strategy() {+ let temp = TempDir::new().unwrap();+ stub_scripts(temp.path(), &["session-start.sh"]);+ for agent in ["codex", "cursor", "gemini-cli", "antigravity-cli"] {+ let output = render_agent_output(+ agent,+ temp.path(),+ "http://127.0.0.1:49374",+ None,+ Some("repo-root"),+ )+ .unwrap_or_else(|err| panic!("failed to render {agent}: {err}"));+ assert!(+ output.contains("AI_MEMORY_PROJECT_STRATEGY=repo-root"),+ "{agent} manual output must tell users to set the strategy env: {output}"+ );+ }+ }++ #[test]+ fn manual_agent_render_omits_project_strategy_by_default() {+ let temp = TempDir::new().unwrap();+ stub_scripts(temp.path(), &["session-start.sh"]);+ let output =+ render_agent_output("codex", temp.path(), "http://127.0.0.1:49374", None, None)+ .unwrap();+ assert!(!output.contains("AI_MEMORY_PROJECT_STRATEGY"));+ }+#[test]fn hook_server_url_defaults_to_configured_server_url() {let config = Config {diff --git a/tests/hooks/test_lib.sh b/tests/hooks/test_lib.shindex edc39071..d98d0aaf 100755--- a/tests/hooks/test_lib.sh+++ b/tests/hooks/test_lib.sh@@ -102,6 +102,24 @@ if command -v git >/dev/null 2>&1; then"&cwd=$REPO/crates/cli&workspace=oss&project=acme-api&project_strategy=repo-root" \"$QSR"+ rm -f "$REPO/.ai-memory.toml"+ AI_MEMORY_PROJECT_STRATEGY=repo-root+ export AI_MEMORY_PROJECT_STRATEGY+ QSE=$(ai_memory_marker_qs "$REPO/crates/cli")+ assert_eq "repo-root env: no marker resolves to repo basename" \+ "&cwd=$REPO/crates/cli&project=acme-api&project_strategy=repo-root" \+ "$QSE"++ printf 'workspace = "oss"\nproject = "pinned"\nproject_strategy = "basename"\n' \+ >"$REPO/.ai-memory.toml"+ QSO=$(ai_memory_marker_qs "$REPO/crates/cli")+ assert_eq "marker project strategy overrides env default" \+ "&cwd=$REPO/crates/cli&workspace=oss&project=pinned&project_strategy=basename" \+ "$QSO"+ unset AI_MEMORY_PROJECT_STRATEGY++ printf 'workspace = "oss"\nproject_strategy = "repo-root"\n' >"$REPO/.ai-memory.toml"+# A linked worktree whose directory lives OUTSIDE the main repo tree# (a common layout: tools that keep worktrees in a separate directory)# has no .ai-memory.toml ancestor of its own, yet still collapses to the