Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(cli): match captured env names case-insensitively on Windows; tidy test docs
AkitaOnRailsa43e4cb91291
diff --git a/crates/ai-memory-cli/src/commands/install_hooks.rs b/crates/ai-memory-cli/src/commands/install_hooks.rsindex 5e2117bc..ff811d26 100644--- a/crates/ai-memory-cli/src/commands/install_hooks.rs+++ b/crates/ai-memory-cli/src/commands/install_hooks.rs@@ -787,6 +787,12 @@ pub(crate) fn run_with_opencode_dialect_and_mcp_path(break cross-agent continuity.");}+ // Codex's default hooks file depends on CODEX_HOME. Resolve it from the+ // captured environment once, into `args` like the project strategy below,+ // so every reader of the existing install and the writer agree on it.+ if args.apply && args.agent == AgentChoice::Codex && args.config_file.is_none() {+ args.config_file = Some(codex_hooks_path(&config.runtime_env)?);+ }// Preserve an existing `--capture-assistant` opt-in on a bare re-apply. There// is no negative flag, so a re-run without `--capture-assistant` (notably the// `ai-memory run` auto-wire, which always passes it off) must NOT silently@@ -794,12 +800,6 @@ pub(crate) fn run_with_opencode_dialect_and_mcp_path(// `--capture-assistant` still forces it on; this only fills in the unset case// from what is already installed. Gated to the agents where assistant capture// is allowed (Claude Code, Codex), whose configs share the nested-hooks shape.- // Codex's default hooks file depends on CODEX_HOME. Resolve it from the- // captured environment once, into `args` like the project strategy below,- // so every reader of the existing install and the writer agree on it.- if args.apply && args.agent == AgentChoice::Codex && args.config_file.is_none() {- args.config_file = Some(codex_hooks_path(&config.runtime_env)?);- }if args.apply&& !args.capture_assistant&& capture_assistant_allowed(args.agent)@@ -9874,13 +9874,6 @@ model = "gpt-5");}- /// #552, the regression guard: `--apply` must persist the bearer under the- /// data dir and leave it out of the agent config entirely.- ///- /// Before this, the token was written into the rendered command — as- /// `--auth-token <token>` for native hooks — so it sat in the agent's- /// config file *and* in `/proc/<pid>/cmdline` for the lifetime of every- /// hook, on every tool call./// `install-hooks --agent codex --apply` writes to the CODEX_HOME that/// `Config::load` captured, not one re-read from the live process/// (invariant #1): an injected value decides the file.@@ -9906,10 +9899,22 @@ model = "gpt-5"run(&config, args).expect("apply should succeed");+ // Assert on the hook command, not the binary path: the test binary's+ // path depends on where the workspace is built.let written = std::fs::read_to_string(codex_home.path().join("hooks.json")).unwrap();- assert!(written.contains("ai-memory"), "{written}");+ assert!(+ written.contains("hook --event session-start --agent codex"),+ "{written}"+ );}+ /// #552, the regression guard: `--apply` must persist the bearer under the+ /// data dir and leave it out of the agent config entirely.+ ///+ /// Before this, the token was written into the rendered command — as+ /// `--auth-token <token>` for native hooks — so it sat in the agent's+ /// config file *and* in `/proc/<pid>/cmdline` for the lifetime of every+ /// hook, on every tool call.#[test]fn apply_persists_the_bearer_and_keeps_it_out_of_the_agent_config() {let home = TempDir::new().unwrap();diff --git a/crates/ai-memory-cli/src/config.rs b/crates/ai-memory-cli/src/config.rsindex 207d7bb3..817fa74e 100644--- a/crates/ai-memory-cli/src/config.rs+++ b/crates/ai-memory-cli/src/config.rs@@ -665,6 +665,17 @@ impl std::fmt::Debug for CapturedProcessEnv {}}+/// Whether a captured environment key names `name`: exactly, or ignoring+/// ASCII case where the platform's environment is case-insensitive (Windows).+fn env_name_matches(key: &std::ffi::OsStr, name: &str, case_insensitive: bool) -> bool {+ if case_insensitive {+ key.to_str()+ .is_some_and(|key| key.eq_ignore_ascii_case(name))+ } else {+ key == name+ }+}+/// Environment-only values captured once by [`Config::load`].#[derive(Debug, Clone, Default)]pub struct RuntimeEnv {@@ -813,13 +824,14 @@ impl RuntimeEnv {/// One variable of the captured process environment. Third-party harness/// homes (`CODEX_HOME`, `COPILOT_HOME`, ...) are read through here so a/// command never re-reads the live environment (invariant #1) and tests- /// can inject them.+ /// can inject them. Names match case-insensitively on Windows, as+ /// `std::env::var_os` does there.#[must_use]pub(crate) fn var_os(&self, name: &str) -> Option<OsString> {self.process_env.0.iter()- .find(|(key, _)| key.as_os_str() == name)+ .find(|(key, _)| env_name_matches(key, name, cfg!(windows))).map(|(_, value)| value.clone())}@@ -3290,6 +3302,39 @@ fn normalize_home_dir(home: &str) -> Option<String> {#[cfg(test)]mod tests {+ /// Windows environment names are case-insensitive (`std::env::var_os`+ /// finds `Codex_Home` for `CODEX_HOME`), so the captured lookup must be+ /// too there, and stay exact elsewhere.+ #[test]+ fn captured_env_names_match_case_insensitively_only_on_windows_rules() {+ use std::ffi::OsStr;+ assert!(super::env_name_matches(+ OsStr::new("Codex_Home"),+ "CODEX_HOME",+ true+ ));+ assert!(super::env_name_matches(+ OsStr::new("CODEX_HOME"),+ "CODEX_HOME",+ true+ ));+ assert!(!super::env_name_matches(+ OsStr::new("CODEX_HOMES"),+ "CODEX_HOME",+ true+ ));+ assert!(!super::env_name_matches(+ OsStr::new("Codex_Home"),+ "CODEX_HOME",+ false+ ));+ assert!(super::env_name_matches(+ OsStr::new("CODEX_HOME"),+ "CODEX_HOME",+ false+ ));+ }+/// #552: the bearer used to travel on every hook's command line, readable/// through `/proc/<pid>/cmdline` by any local user. It now lives in the/// data dir instead — which is only an improvement if the files are not