Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
test(cli): harden shell completion output
AkitaOnRailsf4218a3f9a4c
diff --git a/crates/ai-memory-cli/src/commands/completions.rs b/crates/ai-memory-cli/src/commands/completions.rsindex a107d661..64916857 100644--- a/crates/ai-memory-cli/src/commands/completions.rs+++ b/crates/ai-memory-cli/src/commands/completions.rs@@ -36,8 +36,38 @@ pub fn run(args: CompletionsArgs) -> Result<()> {let mut script = Vec::new();generate(args.shell, &mut cmd, bin_name, &mut script);- match std::io::stdout().write_all(&script) {+ let stdout = std::io::stdout();+ write_script(stdout.lock(), &script)+}++fn write_script(mut output: impl Write, script: &[u8]) -> Result<()> {+ match output.write_all(script) {Err(e) if e.kind() == std::io::ErrorKind::BrokenPipe => Ok(()),other => other.context("writing completion script to stdout"),}}++#[cfg(test)]+mod tests {+ use super::*;++ struct ClosedPipe;++ impl Write for ClosedPipe {+ fn write(&mut self, _buf: &[u8]) -> std::io::Result<usize> {+ Err(std::io::Error::new(+ std::io::ErrorKind::BrokenPipe,+ "reader closed",+ ))+ }++ fn flush(&mut self) -> std::io::Result<()> {+ Ok(())+ }+ }++ #[test]+ fn closed_output_pipe_is_a_clean_exit() {+ write_script(ClosedPipe, b"completion script").unwrap();+ }+}diff --git a/crates/ai-memory-cli/tests/completions.rs b/crates/ai-memory-cli/tests/completions.rsindex 4601354c..430f6889 100644--- a/crates/ai-memory-cli/tests/completions.rs+++ b/crates/ai-memory-cli/tests/completions.rs@@ -13,10 +13,12 @@ fn ai_memory_bin() -> std::path::PathBuf {}fn completions_for(shell: &str) -> String {+ let temp = tempfile::tempdir().unwrap();+ let missing_data_dir = temp.path().join("missing-data");let out = Command::new(ai_memory_bin())// Point config + data dir at a path that does not exist: generating a// completion script must not read, create, or require either.- .env("AI_MEMORY_DATA_DIR", "/nonexistent/ai-memory-completions")+ .env("AI_MEMORY_DATA_DIR", &missing_data_dir).args(["completions", shell]).output().unwrap_or_else(|e| panic!("failed to run `completions {shell}`: {e}"));@@ -27,6 +29,15 @@ fn completions_for(shell: &str) -> String {out.status.code(),String::from_utf8_lossy(&out.stderr),);+ assert!(+ out.stderr.is_empty(),+ "`completions {shell}` polluted stderr: {}",+ String::from_utf8_lossy(&out.stderr),+ );+ assert!(+ !missing_data_dir.exists(),+ "`completions {shell}` created its configured data directory",+ );String::from_utf8(out.stdout).expect("completion script should be UTF-8")}@@ -51,7 +62,16 @@ fn scripts_cover_subcommands_from_across_the_tree() {// regression in how the command tree is handed to the generator shows up.for shell in ["bash", "fish", "zsh"] {let script = completions_for(shell);- for subcommand in ["init", "serve", "write-page", "purge-project", "user"] {+ for subcommand in [+ "init",+ "serve",+ "write-page",+ "purge-project",+ "user",+ "rotate-token",+ "auth",+ "login",+ ] {assert!(script.contains(subcommand),"{shell} completion script is missing `{subcommand}`",diff --git a/docs/install.md b/docs/install.mdindex d990bc9e..9c6ce1e4 100644--- a/docs/install.md+++ b/docs/install.md@@ -1121,10 +1121,12 @@ docker exec ai-memory ai-memory search "karpathy"docker exec ai-memory ai-memory backup --to /data/snapshot.tar.gz# B) One-shot, no running container needed for pure-stdout helpers-# (generate-auth-token, install-mcp, install-hooks, setup-agent, llm-test).+# (generate-auth-token, completions, install-mcp, install-hooks, setup-agent,+# llm-test).# Auth login is stateful: use docker exec against the running container or# the wrapper so it writes into the same data volume as the server.docker run --rm akitaonrails/ai-memory:latest generate-auth-token+docker run --rm akitaonrails/ai-memory:latest completions zshdocker run --rm akitaonrails/ai-memory:latest install-mcp --client cursordocker run --rm akitaonrails/ai-memory:latest --help # full subcommand tree```diff --git a/docs/shell-completions.md b/docs/shell-completions.mdindex fdf7dc64..ae168c47 100644--- a/docs/shell-completions.md+++ b/docs/shell-completions.md@@ -6,6 +6,10 @@ the binary's own command tree, so it covers every subcommand and flag of theversion that produced it — including nested commands like `user rotate-token`and `auth login`.+The Docker wrapper's `upgrade` command is wrapper-owned rather than part of the+native clap command tree, so it is the one subcommand not present in generated+completions.+The command reads no config and does not need a data directory, so it can berun before `ai-memory init` or inside a packaging step.@@ -14,6 +18,7 @@ run before `ai-memory init` or inside a packaging step.### fish```fish+mkdir -p ~/.config/fish/completionsai-memory completions fish > ~/.config/fish/completions/ai-memory.fish```@@ -59,15 +64,24 @@ source <(ai-memory completions bash)ai-memory completions powershell | Out-String | Invoke-Expression```-To persist it, append that line to the file at `$PROFILE`:+To persist the generated script, keep it beside your PowerShell profile and+dot-source it from `$PROFILE` once:```powershell-ai-memory completions powershell >> $PROFILE+$completionDir = Join-Path (Split-Path -Parent $PROFILE) "completions"+$completionPath = Join-Path $completionDir "ai-memory.ps1"+New-Item -ItemType Directory -Force $completionDir | Out-Null+ai-memory completions powershell | Set-Content -Encoding utf8 $completionPath+Add-Content -Path $PROFILE -Value ". '$completionPath'"```+After upgrades, regenerate `$completionPath`; the profile line does not need to+be added again.+### elvish```elvish+mkdir -p ~/.config/elvish/libai-memory completions elvish > ~/.config/elvish/lib/ai-memory.elv```