Skip to content

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

Rust

fix(windows): harden git bash native paths

zapatal82b1b84f4806
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 74a7d05d..1b44a481 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1 MiB (previous contents move to `hook-drain.log.old`), so an agent
pointed at a chronically unreachable server can no longer grow the log
without bound.
+- Windows hook-spool drain locks now treat native lock-violation responses as
+ expected lock contention, so overlapping background drains skip cleanly
+ instead of failing the single-flight guard.
+- Windows wiki checkpoints now fall back to the Git CLI for native libgit2
+ path-resolution failures when reopening freshly initialised repos, keeping
+ delete and purge operations usable under dot-prefixed temp or wrapper paths.
### Changed
- Post-audit cleanup, no behavior change: the `AI_MEMORY_HOOK_PLATFORM`
diff --git a/crates/ai-memory-cli/src/commands/hook_drain_process.rs b/crates/ai-memory-cli/src/commands/hook_drain_process.rs
index 1c7fa8e0..9607c9ba 100644
--- a/crates/ai-memory-cli/src/commands/hook_drain_process.rs
+++ b/crates/ai-memory-cli/src/commands/hook_drain_process.rs
@@ -193,9 +193,8 @@ fn detach_config(try_breakaway: bool) -> DetachConfig {
#[cfg(windows)]
fn apply_detach_flags(command: &mut Command, detach: &DetachConfig) {
use std::os::windows::process::CommandExt as _;
- if let DetachConfig::WindowsCreationFlags(flags) = detach {
- command.creation_flags(*flags);
- }
+ let DetachConfig::WindowsCreationFlags(flags) = detach;
+ command.creation_flags(*flags);
}
#[cfg(windows)]
@@ -311,15 +310,9 @@ mod tests {
fn windows_spawn_config_uses_expected_flags_and_breakaway_toggle() {
let tmp = tempfile::tempdir().unwrap();
let spec = command_spec(tmp.path()).unwrap();
- let DetachConfig::WindowsCreationFlags(with_breakaway) = spawn_config(&spec, true).detach
- else {
- panic!("windows flags")
- };
+ let DetachConfig::WindowsCreationFlags(with_breakaway) = spawn_config(&spec, true).detach;
let DetachConfig::WindowsCreationFlags(without_breakaway) =
- spawn_config(&spec, false).detach
- else {
- panic!("windows flags")
- };
+ spawn_config(&spec, false).detach;
assert_ne!(with_breakaway, without_breakaway);
}
}
diff --git a/crates/ai-memory-cli/src/commands/hook_spool.rs b/crates/ai-memory-cli/src/commands/hook_spool.rs
index f7af1f87..a3fe82e5 100644
--- a/crates/ai-memory-cli/src/commands/hook_spool.rs
+++ b/crates/ai-memory-cli/src/commands/hook_spool.rs
@@ -201,6 +201,24 @@ pub struct DrainLock {
_file: File,
}
+fn is_drain_lock_busy_error(err: &std::io::Error) -> bool {
+ if err.kind() == std::io::ErrorKind::WouldBlock {
+ return true;
+ }
+
+ #[cfg(windows)]
+ {
+ // Windows can report a contended fs2 byte-range lock as the native
+ // ERROR_LOCK_VIOLATION code instead of mapping it to WouldBlock.
+ const ERROR_LOCK_VIOLATION: i32 = 33;
+ if err.raw_os_error() == Some(ERROR_LOCK_VIOLATION) {
+ return true;
+ }
+ }
+
+ false
+}
+
/// Outcome for lock-aware drain wrappers.
#[derive(Debug, PartialEq, Eq)]
pub enum LockedDrainResult {
@@ -224,7 +242,7 @@ pub fn acquire_drain_lock(spool: &Path, wait: DrainLockWait) -> std::io::Result<
loop {
match file.try_lock_exclusive() {
Ok(()) => return Ok(Some(DrainLock { _file: file })),
- Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => match wait {
+ Err(err) if is_drain_lock_busy_error(&err) => match wait {
DrainLockWait::NoWait => return Ok(None),
DrainLockWait::Bounded(limit) if started.elapsed() >= limit => return Ok(None),
DrainLockWait::Bounded(limit) => {
@@ -890,6 +908,26 @@ mod tests {
);
}
+ #[test]
+ fn would_block_lock_error_is_lock_busy() {
+ let err = std::io::Error::from(std::io::ErrorKind::WouldBlock);
+ assert!(is_drain_lock_busy_error(&err));
+ }
+
+ #[cfg(windows)]
+ #[test]
+ fn windows_lock_violation_error_is_lock_busy() {
+ const ERROR_LOCK_VIOLATION: i32 = 33;
+ let err = std::io::Error::from_raw_os_error(ERROR_LOCK_VIOLATION);
+ assert!(is_drain_lock_busy_error(&err));
+ }
+
+ #[test]
+ fn unrelated_lock_error_is_not_lock_busy() {
+ let err = std::io::Error::from(std::io::ErrorKind::PermissionDenied);
+ assert!(!is_drain_lock_busy_error(&err));
+ }
+
#[tokio::test]
async fn drain_exclusive_skips_when_lock_busy() {
let tmp = tempfile::tempdir().unwrap();
diff --git a/crates/ai-memory-store/src/ops.rs b/crates/ai-memory-store/src/ops.rs
index 68061796..1c3b4afa 100644
--- a/crates/ai-memory-store/src/ops.rs
+++ b/crates/ai-memory-store/src/ops.rs
@@ -3224,12 +3224,12 @@ mod tests {
);
assert_eq!(
read_repo_path(&conn, &git_proj),
- Some(git_path),
+ Some(normalize_repo_path_key(&git_path)),
"real git work-tree root must be preserved"
);
assert_eq!(
read_repo_path(&conn, &gone_proj),
- Some(gone_path),
+ Some(normalize_repo_path_key(&gone_path)),
"path absent on this host must be preserved (multi-user/unmounted safety)"
);
}
diff --git a/crates/ai-memory-wiki/src/git.rs b/crates/ai-memory-wiki/src/git.rs
index 36f236de..046a6c01 100644
--- a/crates/ai-memory-wiki/src/git.rs
+++ b/crates/ai-memory-wiki/src/git.rs
@@ -263,7 +263,25 @@ fn commit_all_fallback(
}
fn should_try_commit_cli_fallback(error: &git2::Error) -> bool {
- matches!(error.code(), ErrorCode::NotFound)
+ if matches!(error.code(), ErrorCode::NotFound) {
+ return true;
+ }
+
+ #[cfg(windows)]
+ {
+ // On native Windows, libgit2 can fail to reopen a freshly initialised
+ // wiki repo under dot-prefixed temp dirs with an OS path-resolution
+ // error. The fallback still runs only for Repository::open failures;
+ // real permission or repo corruption errors must also pass through the
+ // Git CLI before they are treated as recoverable.
+ if matches!(error.class(), git2::ErrorClass::Os)
+ && error.message().contains("failed to resolve path")
+ {
+ return true;
+ }
+ }
+
+ false
}
#[cfg(windows)]
@@ -471,6 +489,18 @@ mod tests {
assert_eq!(adapter.commit_count(), 2);
}
+ #[cfg(windows)]
+ #[test]
+ fn commit_all_handles_windows_dot_prefixed_temp_roots() {
+ let tmp = TempDir::new().unwrap();
+ let root = tmp.path().join("wiki");
+ let adapter = GitAdapter::open_or_init(&root).unwrap();
+
+ std::fs::write(root.join("foo.md"), "hello").unwrap();
+ assert!(adapter.commit_all("add foo").unwrap().is_some());
+ assert_eq!(adapter.commit_count(), 1);
+ }
+
#[test]
fn recent_checkpoints_returns_newest_first() {
let tmp = tempdir();
diff --git a/docs/windows.md b/docs/windows.md
index dcf0f10d..77a3500d 100644
--- a/docs/windows.md
+++ b/docs/windows.md
@@ -167,11 +167,27 @@ target\debug\ai-memory.exe init
target\debug\ai-memory.exe serve --transport http --bind 127.0.0.1:49374
```
+For release validation from Git Bash on native Windows, use the same checkout
+with the Rust MSVC toolchain active:
+
+```bash
+cargo test --workspace
+cargo build --locked --release -p ai-memory-cli
+./target/release/ai-memory.exe --version
+```
+
+The version output should match the package version for the checkout.
+
The Tailwind build step supports the pinned
`tailwindcss-windows-x64.exe` binary and falls back to PowerShell
`Invoke-WebRequest` when `curl`/`wget` are unavailable. You should not
need `TAILWIND_SKIP=1` for normal Windows builds.
+Keep Git for Windows' `git.exe` on `PATH` for native builds and hook runs. When
+libgit2 hits a Windows path-resolution error while opening a newly initialized
+wiki repository, ai-memory falls back to the Git CLI instead of treating that
+specific condition as fatal.
+
From another PowerShell window in the repo:
```powershell
@@ -261,6 +277,12 @@ The session-start budget caps how long the hook may block before handoff fetch;
the background budget caps detached cleanup after session-end and does not keep
the agent waiting.
+On Windows, a contended drain lock can be reported as the native
+`ERROR_LOCK_VIOLATION` code instead of Rust's `WouldBlock` error kind.
+ai-memory treats both as normal lock-busy states, so concurrent drains wait,
+skip, or expire according to the same spool timing rules instead of failing the
+hook.
+
## Current Harness Caveats
Windows hook support is new and needs real-world testing against native