Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
cli: auto-derive --project from cwd basename (drop "scratch" defaults)
AkitaOnRailsae944638dded
diff --git a/crates/ai-memory-cli/src/cli.rs b/crates/ai-memory-cli/src/cli.rsindex 8d021ace..262d924f 100644--- a/crates/ai-memory-cli/src/cli.rs+++ b/crates/ai-memory-cli/src/cli.rs@@ -122,13 +122,20 @@ pub struct BootstrapArgs {/// any subdir of the project works).#[arg(long)]pub repo_path: Option<PathBuf>,- /// Workspace + project labels stamped on the generated pages.- /// Match what your `ai-memory serve` uses so the bootstrap pages- /// live in the same wiki silo as future captured sessions.+ /// Workspace name. Defaults to `default` (the single workspace+ /// all hook-captured sessions land in today).#[arg(long, default_value = "default")]pub workspace: String,- #[arg(long, default_value = "scratch")]- pub project: String,+ /// Project name. When omitted, auto-derived from the basename of+ /// the resolved repo path — same heuristic the hook router uses+ /// to bucket per-cwd observations, so the bootstrap pages land+ /// in the same project as future session captures.+ ///+ /// Dot-prefixed dirs are preserved verbatim — `~/.config` becomes+ /// project `.config`, matching what the router does for sessions+ /// launched from there. Pass `--project` explicitly to override.+ #[arg(long)]+ pub project: Option<String>,/// Maximum total tokens of source text sent to the LLM in one/// run. When the collected sources exceed this, lower-priority/// inputs (older git commits, then code module headers, then@@ -364,9 +371,12 @@ pub struct EmbedArgs {/// Workspace name (auto-created if absent).#[arg(long, default_value = "default")]pub workspace: String,- /// Project name within the workspace (auto-created if absent).- #[arg(long, default_value = "scratch")]- pub project: String,+ /// Project name. When omitted, auto-derived from the basename of+ /// the current git repo root (or CWD if no git repo). Matches the+ /// hook router's per-cwd convention so this command targets the+ /// same project sessions write into.+ #[arg(long)]+ pub project: Option<String>,}/// Arguments for `forget-sweep`.@@ -378,9 +388,10 @@ pub struct ForgetSweepArgs {/// Workspace name (auto-created if absent).#[arg(long, default_value = "default")]pub workspace: String,- /// Project name within the workspace (auto-created if absent).- #[arg(long, default_value = "scratch")]- pub project: String,+ /// Project name. When omitted, auto-derived from the basename of+ /// the current git repo root (or CWD if no git repo).+ #[arg(long)]+ pub project: Option<String>,}/// Arguments for `lint`.@@ -392,9 +403,10 @@ pub struct LintArgs {/// Workspace name (auto-created if absent).#[arg(long, default_value = "default")]pub workspace: String,- /// Project name within the workspace (auto-created if absent).- #[arg(long, default_value = "scratch")]- pub project: String,+ /// Project name. When omitted, auto-derived from the basename of+ /// the current git repo root (or CWD if no git repo).+ #[arg(long)]+ pub project: Option<String>,}/// Arguments for `llm-test`.diff --git a/crates/ai-memory-cli/src/commands/bootstrap.rs b/crates/ai-memory-cli/src/commands/bootstrap.rsindex 62e1b0e0..efaa4543 100644--- a/crates/ai-memory-cli/src/commands/bootstrap.rs+++ b/crates/ai-memory-cli/src/commands/bootstrap.rs@@ -41,6 +41,10 @@ pub async fn run(_config: &Config, args: BootstrapArgs) -> Result<()> {.context("auto-detecting --repo-path (no .git found at or above CWD)")?,};+ // ---- project — auto-derive from repo basename if absent -------+ let project = super::resolve_project_name(args.project.as_deref())?;+ info!(workspace = %args.workspace, project = %project, repo_path = %repo_path.display(), "bootstrap target");+// ---- collect sources locally ----------------------------------let sources = collect_sources(&repo_path,@@ -55,7 +59,7 @@ pub async fn run(_config: &Config, args: BootstrapArgs) -> Result<()> {// ---- POST to server -------------------------------------------let body = serde_json::json!({"workspace": args.workspace,- "project": args.project,+ "project": project,"sources": sources,"max_input_tokens": args.max_input_tokens,"dry_run": args.dry_run,@@ -63,7 +67,7 @@ pub async fn run(_config: &Config, args: BootstrapArgs) -> Result<()> {});let outcome: BootstrapOutcome = post_json(&ep, "/admin/bootstrap", &body).await?;- print_human_report(&outcome, &args.workspace, &args.project);+ print_human_report(&outcome, &args.workspace, &project);let report = serde_json::to_string_pretty(&outcome)?;println!("\n--- machine-readable ---\n{report}");Ok(())diff --git a/crates/ai-memory-cli/src/commands/embed.rs b/crates/ai-memory-cli/src/commands/embed.rsindex eb141bb2..1cdb567d 100644--- a/crates/ai-memory-cli/src/commands/embed.rs+++ b/crates/ai-memory-cli/src/commands/embed.rs@@ -27,12 +27,13 @@ struct EmbedRequest {/// response.pub async fn run(_config: &Config, args: EmbedArgs) -> Result<()> {let endpoint = ServerEndpoint::from_env();+ let project = super::resolve_project_name(args.project.as_deref())?;let report: serde_json::Value = post_json(&endpoint,"/admin/embed",&EmbedRequest {workspace: args.workspace,- project: args.project,+ project,// The CLI flag was historically named `force`; the server// field is `reembed` — map them here.reembed: args.force,diff --git a/crates/ai-memory-cli/src/commands/forget_sweep.rs b/crates/ai-memory-cli/src/commands/forget_sweep.rsindex afa42e3e..d10b782a 100644--- a/crates/ai-memory-cli/src/commands/forget_sweep.rs+++ b/crates/ai-memory-cli/src/commands/forget_sweep.rs@@ -25,12 +25,13 @@ struct ForgetSweepRequest {/// response.pub async fn run(_config: &Config, args: ForgetSweepArgs) -> Result<()> {let endpoint = ServerEndpoint::from_env();+ let project = super::resolve_project_name(args.project.as_deref())?;let report: serde_json::Value = post_json(&endpoint,"/admin/forget-sweep",&ForgetSweepRequest {workspace: args.workspace,- project: args.project,+ project,dry_run: args.dry_run,},)diff --git a/crates/ai-memory-cli/src/commands/lint.rs b/crates/ai-memory-cli/src/commands/lint.rsindex fd09d329..87b2073b 100644--- a/crates/ai-memory-cli/src/commands/lint.rs+++ b/crates/ai-memory-cli/src/commands/lint.rs@@ -22,12 +22,13 @@ struct LintRequest {/// response.pub async fn run(_config: &Config, args: LintArgs) -> Result<()> {let endpoint = ServerEndpoint::from_env();+ let project = super::resolve_project_name(args.project.as_deref())?;let report: serde_json::Value = post_json(&endpoint,"/admin/lint",&LintRequest {workspace: args.workspace,- project: args.project,+ project,dry_run: args.dry_run,},)diff --git a/crates/ai-memory-cli/src/commands/mod.rs b/crates/ai-memory-cli/src/commands/mod.rsindex ff0728a9..98c3ac7b 100644--- a/crates/ai-memory-cli/src/commands/mod.rs+++ b/crates/ai-memory-cli/src/commands/mod.rs@@ -1,5 +1,7 @@//! Subcommand implementations.+use anyhow::{Context, Result, anyhow};+pub mod apply_shared;pub mod backup;pub mod bootstrap;@@ -23,3 +25,42 @@ pub mod setup_agent;pub mod status;pub mod watch;pub mod write_page;++/// Resolve the effective project name for a client command.+///+/// Precedence:+/// 1. `explicit` (the user's `--project` flag) when non-empty.+/// 2. 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).+///+/// Mirrors the heuristic the hook router uses in+/// `ai-memory-hooks::router::resolve_project_ids`, so commands+/// auto-target the same project the user's interactive sessions+/// have been writing into. Dot-prefixed dirs are preserved+/// verbatim (`~/.config` → project `.config`).+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(root) = ai_memory_consolidate::discover_repo_root(std::path::Path::new("."))+ && let Some(name) = root+ .file_name()+ .and_then(|s| s.to_str())+ .filter(|s| !s.is_empty())+ {+ return Ok(name.to_string());+ }+ let cwd = std::env::current_dir().context("getting CWD for project auto-detect")?;+ cwd.file_name()+ .and_then(|s| s.to_str())+ .map(str::to_string)+ .filter(|s| !s.is_empty())+ .ok_or_else(|| {+ anyhow!(+ "could not derive project name from CWD ({}); \+ pass --project explicitly",+ cwd.display()+ )+ })+}