Skip to content

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

Rust

fix(hooks): never let the drainer recreate a deleted data dir

AkitaOnRailsfe118877889b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e17b2932..de6cfb8b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Fixed
+- Fixed the detached hook drainer recreating a deleted data directory. A
+ drainer started just before `ai-memory uninstall --purge-data` (or one
+ whose data directory was removed some other way) brought back an empty
+ `hook-spool/` with its lock file. The drainer now exits when the data
+ directory is gone. A spool directory the drainer creates is also private
+ (`0700`) now, as one created by a queued event always was; before, it
+ took the default permissions.
+
## [2.6.4] - 2026-10-12
### Fixed
diff --git a/crates/ai-memory-cli/src/commands/hook.rs b/crates/ai-memory-cli/src/commands/hook.rs
index 41e8d0b8..072c6bf7 100644
--- a/crates/ai-memory-cli/src/commands/hook.rs
+++ b/crates/ai-memory-cli/src/commands/hook.rs
@@ -377,6 +377,11 @@ fn after_background_drain_event_enqueue(
pub async fn run_drain(data_dir: Option<PathBuf>) -> anyhow::Result<()> {
let runtime_env = RuntimeEnv::from_process();
let dd = resolve_data_dir_with(data_dir.as_deref(), &runtime_env);
+ if !dd.is_dir() {
+ // Purged (or never created): nothing was queued, and a warning has
+ // nowhere useful to go since `hook-drain.log` lived in that dir.
+ return Ok(());
+ }
// The hook path never runs a full `Config::load` (see the fast-path
// contract in `lib.rs`): an invalid config section elsewhere must not
// fail the drainer. Only the spool retry policy is needed, loaded
diff --git a/crates/ai-memory-cli/src/commands/hook_spool.rs b/crates/ai-memory-cli/src/commands/hook_spool.rs
index 77843b4e..d0fb5e1f 100644
--- a/crates/ai-memory-cli/src/commands/hook_spool.rs
+++ b/crates/ai-memory-cli/src/commands/hook_spool.rs
@@ -364,7 +364,17 @@ pub enum LockedDrainResult {
/// Try to acquire the single-flight drain lock.
pub fn acquire_drain_lock(spool: &Path, wait: DrainLockWait) -> std::io::Result<Option<DrainLock>> {
- std::fs::create_dir_all(spool)?;
+ // A detached drainer that outlives `uninstall --purge-data` (or a test's
+ // temp dir) must not recreate the data dir it was started for; a missing
+ // data dir has nothing queued to drain. The spool dir is created private,
+ // like `enqueue` creates it.
+ if spool.parent().is_some_and(|data_dir| !data_dir.is_dir()) {
+ return Err(std::io::Error::new(
+ std::io::ErrorKind::NotFound,
+ "the hook spool's data directory no longer exists",
+ ));
+ }
+ create_spool_dir(spool)?;
let path = spool.join(".drain.lock");
let file = std::fs::OpenOptions::new()
.create(true)
@@ -1914,6 +1924,33 @@ mod tests {
assert_eq!(r, DrainResult::default());
}
+ #[test]
+ fn drain_lock_never_recreates_a_removed_data_dir() {
+ let tmp = tempfile::tempdir().unwrap();
+ let data_dir = tmp.path().join("purged");
+ let spool = spool_dir(&data_dir);
+
+ let err = acquire_drain_lock(&spool, DrainLockWait::NoWait)
+ .err()
+ .expect("a drain must not run against a missing data dir");
+ assert_eq!(err.kind(), std::io::ErrorKind::NotFound);
+ assert!(!data_dir.exists(), "the drain recreated a removed data dir");
+
+ std::fs::create_dir(&data_dir).unwrap();
+ assert!(
+ acquire_drain_lock(&spool, DrainLockWait::NoWait)
+ .unwrap()
+ .is_some(),
+ "a present data dir still gets its spool dir created"
+ );
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::PermissionsExt as _;
+ let mode = std::fs::metadata(&spool).unwrap().permissions().mode();
+ assert_eq!(mode & 0o777, 0o700, "a drain-created spool must be private");
+ }
+ }
+
#[test]
fn drain_lock_is_exclusive_and_released_on_drop() {
let tmp = tempfile::tempdir().unwrap();