Skip to content

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

Rust

install-hooks: honour CLAUDE_CONFIG_DIR for claude-code settings

klebervirgilio47603111f2a7
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 02a5a0f6..b310b0dc 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
`$CLAUDE_CONFIG_DIR/.claude.json` when the variable is set (non-empty),
falling back to `~/.claude.json` otherwise. The dry-run output prints the
resolved config path instead of a hardcoded `~/.claude.json`.
+- `install-hooks --agent claude-code` and `setup-agent` follow the same
+ resolution for the hooks settings file: `$CLAUDE_CONFIG_DIR/settings.json`
+ when set, else `~/.claude/settings.json`. Rendered output and the
+ chmod-600 warning show the resolved path.
## [1.17.1] - 2026-07-20
diff --git a/crates/ai-memory-cli/src/commands/install_hooks.rs b/crates/ai-memory-cli/src/commands/install_hooks.rs
index 509082c2..291581e6 100644
--- a/crates/ai-memory-cli/src/commands/install_hooks.rs
+++ b/crates/ai-memory-cli/src/commands/install_hooks.rs
@@ -34,8 +34,22 @@ use crate::commands::render_shared::{
};
use crate::config::{Config, DEFAULT_SERVER_URL};
-/// `~/.claude/settings.json` — Claude Code hooks live under `hooks`.
+/// Claude Code's settings file — hooks live under `hooks`.
+/// `$CLAUDE_CONFIG_DIR/settings.json` when the var is set, else
+/// `~/.claude/settings.json`.
pub(crate) fn claude_settings_path() -> anyhow::Result<std::path::PathBuf> {
+ claude_settings_path_in(std::env::var_os("CLAUDE_CONFIG_DIR"))
+}
+
+/// The env value comes in as a parameter so tests can exercise both
+/// branches without mutating process env (mirrors
+/// `install_mcp::kimi_code_home`).
+fn claude_settings_path_in(
+ env_override: Option<std::ffi::OsString>,
+) -> anyhow::Result<std::path::PathBuf> {
+ if let Some(dir) = crate::commands::path_util::claude_config_dir(env_override) {
+ return Ok(dir.join("settings.json"));
+ }
Ok(home_dir()
.context("could not locate $HOME for ~/.claude/settings.json")?
.join(".claude")
@@ -265,7 +279,18 @@ pub fn run(config: &Config, args: InstallHooksArgs) -> Result<()> {
AgentChoice::Omp => render_omp_extension(&server_url, auth, strategy),
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, strategy)
+ let settings_path = match &args.config_file {
+ Some(p) => p.clone(),
+ None => claude_settings_path()?,
+ };
+ render_claude_code(
+ &hooks_dir,
+ &server_url,
+ auth,
+ &config.data_dir,
+ strategy,
+ &settings_path,
+ )
}
AgentChoice::Codex => {
let hooks_dir = resolve_hooks_dir(args.hooks_dir.as_deref(), args.agent)?;
@@ -2886,6 +2911,7 @@ fn render_claude_code(
auth_token: Option<&str>,
data_dir: &Path,
project_strategy: Option<&str>,
+ settings_path: &Path,
) -> 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
@@ -2913,12 +2939,18 @@ fn render_claude_code(
);
let serialized =
serde_json::to_string_pretty(&payload).context("serializing claude code hook config")?;
- println!("# Claude Code hook config — merge into ~/.claude/settings.json");
+ println!(
+ "# Claude Code hook config — merge into {}",
+ settings_path.display()
+ );
println!("# Hook scripts: {}", hooks_dir.display());
println!("# AI-memory server URL: {server_url}");
if auth_token.is_some() {
println!("# Auth: AI_MEMORY_AUTH_TOKEN embedded in each hook command below.");
- println!("# Treat ~/.claude/settings.json as sensitive (chmod 600).");
+ println!(
+ "# Treat {} as sensitive (chmod 600).",
+ settings_path.display()
+ );
}
println!();
println!("{serialized}");
@@ -5198,6 +5230,27 @@ command = "AI_MEMORY_HOOK_URL=http://old:1 /old/ai-memory/hooks/kimi-code/sessio
assert_eq!(second, ApplyOutcome::NoOp, "second apply must be a no-op");
}
+ #[test]
+ fn claude_settings_path_honours_claude_config_dir() {
+ let custom = if cfg!(windows) {
+ r"C:\custom\claude"
+ } else {
+ "/custom/claude"
+ };
+ let path = claude_settings_path_in(Some(std::ffi::OsString::from(custom))).unwrap();
+ assert_eq!(path, Path::new(custom).join("settings.json"));
+
+ // Empty override and unset var both fall back to ~/.claude/settings.json.
+ for env in [None, Some(std::ffi::OsString::new())] {
+ let path = claude_settings_path_in(env).unwrap();
+ assert!(
+ path.ends_with(Path::new(".claude").join("settings.json")),
+ "default must be ~/.claude/settings.json, got {}",
+ path.display()
+ );
+ }
+ }
+
#[test]
fn kimi_code_config_path_honours_kimi_code_home() {
let custom = if cfg!(windows) {
diff --git a/crates/ai-memory-cli/src/commands/setup_agent.rs b/crates/ai-memory-cli/src/commands/setup_agent.rs
index 94dbd21f..c05d238b 100644
--- a/crates/ai-memory-cli/src/commands/setup_agent.rs
+++ b/crates/ai-memory-cli/src/commands/setup_agent.rs
@@ -256,13 +256,17 @@ fn emit_claude_code(emit_root: &Path, args: &SetupAgentArgs) -> Result<()> {
build_claude_code_payload(emit_root, &args.server_url, args.auth_token.as_deref());
let serialized =
serde_json::to_string_pretty(&payload).context("serializing Claude Code hook config")?;
- println!("# Claude Code — merge into ~/.claude/settings.json");
+ let settings_path = crate::commands::install_hooks::claude_settings_path()?;
+ println!("# Claude Code — merge into {}", settings_path.display());
println!("# Hook scripts (must be reachable from the host that runs Claude Code):");
println!("# {}", emit_root.display());
println!("# AI-memory server: {}", args.server_url);
if args.auth_token.is_some() {
println!("# Auth: AI_MEMORY_AUTH_TOKEN embedded in each hook's env block.");
- println!("# Treat ~/.claude/settings.json as sensitive (chmod 600).");
+ println!(
+ "# Treat {} as sensitive (chmod 600).",
+ settings_path.display()
+ );
}
println!("# Tip: also run `ai-memory install-mcp --client claude-code --auth-token <…>`");
println!("# to register the MCP endpoint (separate from hooks).");
diff --git a/docs/install.md b/docs/install.md
index 7b1b7606..2e77b476 100644
--- a/docs/install.md
+++ b/docs/install.md
@@ -68,10 +68,11 @@ ai-memory install-mcp --client claude-code --apply
ai-memory install-hooks --agent claude-code --apply
```
-If `CLAUDE_CONFIG_DIR` is set, `install-mcp --client claude-code` writes the
-MCP registration to `$CLAUDE_CONFIG_DIR/.claude.json` instead of
-`~/.claude.json`, matching Claude Code's own config resolution. `uninstall`
-sweeps the same path.
+If `CLAUDE_CONFIG_DIR` is set, the claude-code installers match Claude Code's
+own config resolution: `install-mcp` writes the MCP registration to
+`$CLAUDE_CONFIG_DIR/.claude.json` (instead of `~/.claude.json`) and
+`install-hooks` / `setup-agent` target `$CLAUDE_CONFIG_DIR/settings.json`
+(instead of `~/.claude/settings.json`). `uninstall` sweeps the same paths.
The CLI commands (`bootstrap`, `status`, `search`, `lint`, `auto-improve`,
`curator`, `pending-writes`, etc.) inherit the two env vars automatically. So do