Skip to content

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

Rust

refactor(cli): share private-dir creation and one-value secret reads

Talysson de Oliveira Cassiano4b85ebf2e978
diff --git a/crates/ai-memory-cli/src/commands/hook_spool.rs b/crates/ai-memory-cli/src/commands/hook_spool.rs
index 29b64d81..e70d23dd 100644
--- a/crates/ai-memory-cli/src/commands/hook_spool.rs
+++ b/crates/ai-memory-cli/src/commands/hook_spool.rs
@@ -229,36 +229,9 @@ pub fn enqueue(spool: &Path, entry: &SpoolEntry) -> std::io::Result<()> {
/// Create the spool directory `0700` on Unix so the excerpt bodies that reside
/// there (up to `MAX_AGE_MS`) — and even the timestamp+pid metadata in the file
/// names — are only reachable by the owner (#196). The spool holds private
-/// capture until it drains; a world-readable directory would leak that. On
-/// non-Unix the mode is a no-op (falls back to `create_dir_all`). Idempotent:
-/// an existing directory's mode is left untouched (never widened, never
-/// narrowed) to avoid churning a path an operator may have set deliberately.
+/// capture until it drains; a world-readable directory would leak that.
fn create_spool_dir(spool: &Path) -> std::io::Result<()> {
- #[cfg(unix)]
- {
- use std::os::unix::fs::DirBuilderExt as _;
- if spool.is_dir() {
- return Ok(());
- }
- match std::fs::DirBuilder::new()
- .recursive(true)
- .mode(0o700)
- .create(spool)
- {
- Ok(()) => Ok(()),
- // A concurrent drainer/enqueue may have created it between the check
- // and the call; treat an existing directory as success.
- Err(e) if spool.is_dir() => {
- let _ = e;
- Ok(())
- }
- Err(e) => Err(e),
- }
- }
- #[cfg(not(unix))]
- {
- std::fs::create_dir_all(spool)
- }
+ super::path_util::create_private_dir(spool)
}
fn write_private(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
diff --git a/crates/ai-memory-cli/src/commands/path_util.rs b/crates/ai-memory-cli/src/commands/path_util.rs
index eb572f41..85537049 100644
--- a/crates/ai-memory-cli/src/commands/path_util.rs
+++ b/crates/ai-memory-cli/src/commands/path_util.rs
@@ -3,6 +3,35 @@
use std::borrow::Cow;
use std::path::{Path, PathBuf};
+/// Create `dir` (and its parents) `0700` on Unix. On non-Unix the mode is a
+/// no-op (falls back to `create_dir_all`). Idempotent: an existing
+/// directory's mode is left untouched (never widened, never narrowed) to
+/// avoid churning a path an operator may have set deliberately.
+pub(crate) fn create_private_dir(dir: &Path) -> std::io::Result<()> {
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::DirBuilderExt as _;
+ if dir.is_dir() {
+ return Ok(());
+ }
+ match std::fs::DirBuilder::new()
+ .recursive(true)
+ .mode(0o700)
+ .create(dir)
+ {
+ Ok(()) => Ok(()),
+ // A concurrent caller may have created it between the check and
+ // the call; treat an existing directory as success.
+ Err(_) if dir.is_dir() => Ok(()),
+ Err(e) => Err(e),
+ }
+ }
+ #[cfg(not(unix))]
+ {
+ std::fs::create_dir_all(dir)
+ }
+}
+
/// Resolve the user home used for agent configuration paths.
///
/// Tests and scripted wrappers can set `AI_MEMORY_HOME` to exercise the
diff --git a/crates/ai-memory-cli/src/config.rs b/crates/ai-memory-cli/src/config.rs
index 06dc6876..d9f1cf9b 100644
--- a/crates/ai-memory-cli/src/config.rs
+++ b/crates/ai-memory-cli/src/config.rs
@@ -2649,7 +2649,13 @@ pub fn clear_hook_auth_token(data_dir: &Path) -> std::io::Result<()> {
/// Read the persisted bearer, if one was stored. Trailing newline trimmed.
#[must_use]
pub fn read_hook_auth_token(data_dir: &Path) -> Option<String> {
- let raw = std::fs::read_to_string(hook_auth_token_path_in(data_dir)).ok()?;
+ read_trimmed_secret(&hook_auth_token_path_in(data_dir))
+}
+
+/// Read a one-value secret file. Surrounding whitespace is trimmed, and a
+/// missing, unreadable, or blank file is `None` — never an empty bearer.
+pub(crate) fn read_trimmed_secret(path: &Path) -> Option<String> {
+ let raw = std::fs::read_to_string(path).ok()?;
let trimmed = raw.trim();
(!trimmed.is_empty()).then(|| trimmed.to_owned())
}