Skip to content

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

Rust

fix: AI_MEMORY_HOST_CWD + dry-run no project side-effects

AkitaOnRails6578214d7b9e
diff --git a/bin/ai-memory b/bin/ai-memory
index 118bce5b..2171e7a5 100755
--- a/bin/ai-memory
+++ b/bin/ai-memory
@@ -214,11 +214,19 @@ fi
# /work path is fresh inside the container so the bind mount
# resolves cleanly. Commands that don't care about the repo CWD
# (install-mcp, install-hooks, status, search, …) just ignore it.
+#
+# Because the container sees `/work` (not the host's actual path),
+# `basename(cwd)` would resolve to "work" for every invocation if the
+# CLI used it directly. We pass `AI_MEMORY_HOST_CWD=$PWD` so the
+# binary can derive the *real* host-side project name from it. The
+# `commands::resolve_project_name` helper checks this env var first
+# and falls back to the cwd basename only if it's unset.
exec "${DOCKER}" run --rm "${TTY_ARGS[@]}" \
-v "${HOME}:${HOME}" \
-v "${PWD}:/work" \
-w /work \
-e HOME="${HOME}" \
+ -e AI_MEMORY_HOST_CWD="${PWD}" \
-u "$(id -u):$(id -g)" \
"${DATA_ARGS[@]}" \
"${ENV_ARGS[@]}" \
diff --git a/crates/ai-memory-cli/src/commands/mod.rs b/crates/ai-memory-cli/src/commands/mod.rs
index 345ceb7a..38ac4396 100644
--- a/crates/ai-memory-cli/src/commands/mod.rs
+++ b/crates/ai-memory-cli/src/commands/mod.rs
@@ -31,9 +31,17 @@ pub mod write_page;
///
/// Precedence:
/// 1. `explicit` (the user's `--project` flag) when non-empty.
-/// 2. Basename of the git repo root walked up from CWD (handles
+/// 2. `AI_MEMORY_HOST_CWD` env var. The docker wrapper sets this
+/// to the host's `$PWD` because inside the container the workdir
+/// is always `/work` (a bind mount), so the container's own
+/// `current_dir()` returns "work" for every invocation. Without
+/// this env var, every dockerised bootstrap would land in project
+/// `default/work` regardless of which host dir it was actually
+/// run from. Honoured here as a basename, same heuristic as the
+/// other fallbacks.
+/// 3. Basename of the git repo root walked up from CWD (handles
/// running from any subdir of the project).
-/// 3. Basename of the bare CWD (covers non-git directories).
+/// 4. Basename of the bare CWD (covers non-git directories).
///
/// Mirrors the heuristic the hook router uses in
/// `ai-memory-hooks::router::resolve_project_ids`, so commands
@@ -44,6 +52,14 @@ pub(crate) fn resolve_project_name(explicit: Option<&str>) -> Result<String> {
if let Some(p) = explicit.filter(|s| !s.is_empty()) {
return Ok(p.to_string());
}
+ if let Ok(host_cwd) = std::env::var("AI_MEMORY_HOST_CWD")
+ && let Some(name) = std::path::Path::new(&host_cwd)
+ .file_name()
+ .and_then(|s| s.to_str())
+ .filter(|s| !s.is_empty())
+ {
+ return Ok(name.to_string());
+ }
if let Ok(root) = ai_memory_consolidate::discover_repo_root(std::path::Path::new("."))
&& let Some(name) = root
.file_name()
diff --git a/crates/ai-memory-mcp/src/admin.rs b/crates/ai-memory-mcp/src/admin.rs
index b5258464..19f00a9a 100644
--- a/crates/ai-memory-mcp/src/admin.rs
+++ b/crates/ai-memory-mcp/src/admin.rs
@@ -364,9 +364,17 @@ async fn handle_bootstrap(
State(state): State<Arc<AdminState>>,
Json(req): Json<BootstrapRequest>,
) -> Result<impl IntoResponse, (StatusCode, Json<serde_json::Value>)> {
- // LLM is required only for live runs. Dry-runs never call the LLM
- // so we handle them directly here without constructing Bootstrap.
- if !req.dry_run && state.llm.is_none() {
+ // Dry-runs are READ-ONLY: no LLM call, no project creation, no
+ // wiki write, no git commit. Early-return BEFORE any state-touching
+ // call so a smoke-test (e.g. `--dry-run` from a tempdir) cannot
+ // pollute the project list with throwaway names. Dry-runs can also
+ // proceed in parallel with anything else — no mutex needed.
+ if req.dry_run {
+ return dry_run_outcome(req.sources, req.max_input_tokens);
+ }
+
+ // Live runs from here on need LLM + workspace/project resolution.
+ if state.llm.is_none() {
return Err((
StatusCode::SERVICE_UNAVAILABLE,
Json(serde_json::json!({
@@ -374,40 +382,20 @@ async fn handle_bootstrap(
})),
));
}
-
- // Resolve workspace + project — create if absent. This is cheap;
- // do it BEFORE acquiring the mutex so the get-or-create race is
- // serialised by the writer actor (not gated on bootstrap-running).
let (ws, proj) = resolve_ws_proj(&state, &req.workspace, &req.project).await?;
- // Dry-run is read-only (no LLM call, no git commit), so it can
- // proceed in parallel with anything else.
- if req.dry_run && state.llm.is_none() {
- return dry_run_outcome(req.sources, req.max_input_tokens);
- }
-
// Serialise live bootstrap runs. Two parallel `process_sources`
// calls would race the wiki's `commit_all` (libgit2 ops on the
// same repo) and stack LLM cost unnecessarily. The wait is
// operator-visible only in log lines; the request stays open
// until the lock is acquired, then proceeds normally.
- if !req.dry_run {
- if let Ok(guard) = state.bootstrap_lock.try_lock() {
- // Fast path: nobody else was bootstrapping, lock acquired
- // immediately. Drop straight through.
- drop(guard);
- } else {
- info!(
- "another bootstrap is in progress; queueing — \
- this request waits for the active one to finish"
- );
- }
+ if state.bootstrap_lock.try_lock().is_err() {
+ info!(
+ "another bootstrap is in progress; queueing — \
+ this request waits for the active one to finish"
+ );
}
- let _bootstrap_guard = if req.dry_run {
- None
- } else {
- Some(state.bootstrap_lock.lock().await)
- };
+ let _bootstrap_guard = state.bootstrap_lock.lock().await;
let llm = Arc::clone(
state