Skip to content

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

Rust

fix(hooks): de-verbatim Windows data dir so native capture isn't dropped

cristianodwese64379c7769d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a8e1230b..c0b859e1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Fixed
+- Native Windows Claude Code hooks no longer silently drop every captured event.
+ On Windows, `install-hooks` canonicalizes the data dir, which yields a verbatim
+ extended-length path (`\\?\C:\…`), and that prefix was baked verbatim into the
+ generated `--data-dir` for each native hook command. At capture time the
+ hook-spool write under a `\\?\`-prefixed data dir never lands, and the failure
+ was swallowed (`let _ = enqueue(...)`), so every `UserPromptSubmit` /
+ `PreToolUse` / `PostToolUse` / `PreCompact` / `Stop` / `SessionEnd` event was
+ lost while each hook still exited 0 — only `SessionStart` (handoff retrieval)
+ kept working, masking the loss. The data dir is now de-verbatim'd both when
+ rendering the hook command (new installs emit a plain `--data-dir`) and when
+ the hook resolves its data dir at capture time (an already-installed hook
+ recovers on the next session without re-running `install-hooks`). (issue #116)
+
## [1.1.2] - 2026-06-19
### Added
diff --git a/crates/ai-memory-cli/src/commands/hook.rs b/crates/ai-memory-cli/src/commands/hook.rs
index 9095cec6..4b666519 100644
--- a/crates/ai-memory-cli/src/commands/hook.rs
+++ b/crates/ai-memory-cli/src/commands/hook.rs
@@ -21,6 +21,7 @@ use crate::cli::HookArgs;
use super::hook_capture::{build_client, extract_cwd, get_handoff, marker_query_suffix};
use super::hook_spool;
+use super::render_shared::strip_windows_verbatim_prefix;
// All drain/handoff timings default to the current short values and can be
// overridden by whole-minute env vars for very high-latency or large-backlog
@@ -226,20 +227,49 @@ pub async fn run(data_dir: Option<PathBuf>, args: HookArgs) -> anyhow::Result<()
/// fast-path skips config for latency). Mirrors `config.rs`: explicit
/// `--data-dir`, else `AI_MEMORY_DATA_DIR`, else the platform local-data dir.
fn resolve_data_dir(data_dir: Option<&Path>) -> PathBuf {
- data_dir
+ let dir = data_dir
.map(Path::to_path_buf)
.or_else(|| std::env::var_os("AI_MEMORY_DATA_DIR").map(PathBuf::from))
.unwrap_or_else(|| {
dirs::data_local_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join("ai-memory")
- })
+ });
+ // A verbatim `\\?\…` data dir — what `install-hooks` baked into
+ // settings.json before #116 was fixed — silently breaks the hook-spool
+ // write, dropping every event. Normalise it so an already-installed hook
+ // recovers on the next session without re-running `install-hooks`.
+ match dir.to_str() {
+ Some(s) if s.starts_with(r"\\?\") => {
+ PathBuf::from(strip_windows_verbatim_prefix(s).into_owned())
+ }
+ _ => dir,
+ }
}
#[cfg(test)]
mod tests {
use super::*;
+ #[test]
+ fn resolve_data_dir_strips_verbatim_prefix_from_baked_arg() {
+ // A pre-#116 install baked a verbatim `--data-dir` into settings.json;
+ // the hook must resolve it to the plain path so the spool write lands
+ // and enqueue/drain agree on the directory.
+ let resolved =
+ resolve_data_dir(Some(Path::new(r"\\?\C:\Users\me\AppData\Local\ai-memory")));
+ assert_eq!(
+ resolved,
+ PathBuf::from(r"C:\Users\me\AppData\Local\ai-memory")
+ );
+ }
+
+ #[test]
+ fn resolve_data_dir_leaves_plain_path_untouched() {
+ let resolved = resolve_data_dir(Some(Path::new(r"C:\Users\me\ai-memory")));
+ assert_eq!(resolved, PathBuf::from(r"C:\Users\me\ai-memory"));
+ }
+
#[test]
fn should_incremental_drain_only_post_tool_use_over_threshold() {
assert!(should_incremental_drain("post-tool-use", 32, 32));
diff --git a/crates/ai-memory-cli/src/commands/render_shared.rs b/crates/ai-memory-cli/src/commands/render_shared.rs
index 9fa6376a..1180d58c 100644
--- a/crates/ai-memory-cli/src/commands/render_shared.rs
+++ b/crates/ai-memory-cli/src/commands/render_shared.rs
@@ -685,11 +685,38 @@ enum NativeQuote {
Windows,
}
+/// Strip the Windows extended-length (verbatim) prefix from a path string so it
+/// can be safely embedded in a hook command line.
+///
+/// `install-hooks` canonicalizes the data dir (see `config.rs`), which on
+/// Windows yields a verbatim path — `\\?\C:\Users\…\ai-memory`, or
+/// `\\?\UNC\server\share` for a network location. Emitting that verbatim form
+/// into the native hook command silently breaks the hook-spool write at capture
+/// time, so every lifecycle event is dropped while each hook still exits 0
+/// (issue #116). Rendering the plain path avoids that. A no-op for any path
+/// without the prefix — including every POSIX path — so it is safe to apply
+/// before either quote style.
+pub(crate) fn strip_windows_verbatim_prefix(path: &str) -> Cow<'_, str> {
+ if let Some(rest) = path.strip_prefix(r"\\?\UNC\") {
+ // `\\?\UNC\server\share` → `\\server\share`
+ Cow::Owned(format!(r"\\{rest}"))
+ } else if let Some(rest) = path.strip_prefix(r"\\?\") {
+ // `\\?\C:\…` → `C:\…`
+ Cow::Borrowed(rest)
+ } else {
+ Cow::Borrowed(path)
+ }
+}
+
fn native_data_dir_arg(data_dir: Option<&Path>, quote: NativeQuote) -> String {
let Some(data_dir) = data_dir else {
return String::new();
};
- let path = data_dir.to_string_lossy();
+ // A canonicalized data dir is verbatim on Windows (`\\?\C:\…`); emitting
+ // that into the hook command silently breaks the native hook-spool write
+ // (#116), so render the plain path. No-op for non-verbatim / POSIX paths.
+ let lossy = data_dir.to_string_lossy();
+ let path = strip_windows_verbatim_prefix(&lossy);
match quote {
NativeQuote::Posix => format!(" --data-dir {}", shell_quote(&path)),
NativeQuote::Windows => format!(" --data-dir {}", win_double_quote(&path)),
@@ -1333,6 +1360,52 @@ mod tests {
assert!(!cmd.contains("--auth-token"), "no token expected: {cmd}");
}
+ #[test]
+ fn strip_windows_verbatim_prefix_handles_disk_unc_and_plain() {
+ let strip = strip_windows_verbatim_prefix;
+ // Disk verbatim → plain drive path.
+ assert_eq!(
+ &*strip(r"\\?\C:\Users\me\AppData\Local\ai-memory"),
+ r"C:\Users\me\AppData\Local\ai-memory"
+ );
+ // UNC verbatim → plain UNC path.
+ assert_eq!(&*strip(r"\\?\UNC\server\share\m"), r"\\server\share\m");
+ // Already-plain Windows path is left untouched.
+ assert_eq!(&*strip(r"C:\Users\me\ai-memory"), r"C:\Users\me\ai-memory");
+ // POSIX path is left untouched.
+ assert_eq!(
+ &*strip("/home/alice/.local/share/ai-memory"),
+ "/home/alice/.local/share/ai-memory"
+ );
+ }
+
+ #[test]
+ fn windows_native_command_strips_verbatim_data_dir() {
+ // Regression for #116: install-hooks canonicalizes the data dir to a
+ // verbatim `\\?\C:\…` path; the baked `--data-dir` must be the plain
+ // form, or the native hook-spool write silently drops every event.
+ let cmd = hook_command(
+ &PathBuf::from(
+ r"C:\Users\me\AppData\Local\ai-memory\hooks\claude-code\post-tool-use.sh",
+ ),
+ "https://srv.example.com",
+ None,
+ HookCommandContext::new(
+ HookCommandPlatform::WindowsNative,
+ "claude-code",
+ Some(Path::new(r"\\?\C:\Users\me\AppData\Local\ai-memory")),
+ ),
+ );
+ assert!(
+ cmd.contains(r#"--data-dir "C:\Users\me\AppData\Local\ai-memory""#),
+ "plain data dir expected: {cmd}"
+ );
+ assert!(
+ !cmd.contains(r"\\?\"),
+ "verbatim prefix must not leak into the hook command: {cmd}"
+ );
+ }
+
#[test]
fn windows_bash_payload_uses_bash_c_and_sh_hooks() {
let root = PathBuf::from(r"C:\Users\alice\.local\share\ai-memory\hooks\claude-code");