Skip to content

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

Rust

fix(wrapper): keep stdin attached when it is a pipe

Bruno Miiller39531f9b5c88
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3183aca0..706b6268 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,6 +15,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
client of the running server; the command now requires a reachable
server and no longer works against an offline data directory.
+### Fixed
+- The Docker wrappers (`bin/ai-memory`, `bin/ai-memory.ps1`) kept stdin
+ attached only on a real terminal, so every piped or redirected
+ invocation reached the container with a closed stdin. `ai-memory
+ write-page --body -` therefore stored a page with frontmatter and an
+ empty body while still reporting a successful write, and the same
+ applied to any other stdin reader (hooks fed by a pipe). The wrappers
+ now pass `-i` whenever stdin is not a terminal, and keep `-t` for real
+ terminals only.
+
## [1.18.0] - 2026-07-23
### Added
diff --git a/bin/ai-memory b/bin/ai-memory
index c960824d..fa90ad17 100755
--- a/bin/ai-memory
+++ b/bin/ai-memory
@@ -302,9 +302,15 @@ esac
maybe_warn_outdated || true
+# `-t` only makes sense on a real terminal, but `-i` must be passed whenever
+# stdin is a pipe/redirect too: subcommands that read stdin (`write-page
+# --body -`, `hook`) otherwise see an immediately-closed stdin inside the
+# container and silently persist an empty body.
TTY_ARGS=()
if [ -z "${AI_MEMORY_NO_TTY:-}" ] && [ -t 0 ] && [ -t 1 ]; then
TTY_ARGS=(-it)
+elif [ ! -t 0 ]; then
+ TTY_ARGS=(-i)
fi
# The CLI runs INSIDE the container but renders hook config for the HOST, which
diff --git a/bin/ai-memory.ps1 b/bin/ai-memory.ps1
index 45398a01..840f07bc 100644
--- a/bin/ai-memory.ps1
+++ b/bin/ai-memory.ps1
@@ -45,8 +45,13 @@ $WorkPath = (Get-Location).Path
$HookHostRoot = ($HomePath -replace '\\', '/') + "/.local/share/ai-memory/hooks"
$DockerArgs = @("run", "--rm")
+# `-t` needs a real console, but `-i` must also be passed when stdin is
+# redirected: `write-page --body -` (and other stdin readers) would otherwise
+# see empty stdin inside the container and silently persist an empty body.
if (-not $env:AI_MEMORY_NO_TTY -and -not [Console]::IsInputRedirected -and -not [Console]::IsOutputRedirected) {
$DockerArgs += "-it"
+} elseif ([Console]::IsInputRedirected) {
+ $DockerArgs += "-i"
}
$DockerArgs += @(
diff --git a/crates/ai-memory-cli/tests/packaging.rs b/crates/ai-memory-cli/tests/packaging.rs
index f977ab60..19b6d8ca 100644
--- a/crates/ai-memory-cli/tests/packaging.rs
+++ b/crates/ai-memory-cli/tests/packaging.rs
@@ -209,6 +209,82 @@ fn macos_wrapper_routes_urls_by_real_subcommand() {
);
}
+// Like run_wrapper_on_fake_macos's fake docker, but the wrapper is spawned with
+// stdin on a pipe: the shape every `cat page.md | ai-memory write-page --body -`
+// (and every CI/cron) invocation actually has.
+#[cfg(unix)]
+fn run_wrapper_with_piped_stdin(args: &[&str], stdin_payload: &str) -> String {
+ use std::io::Write as _;
+
+ let tmp = tempfile::tempdir().unwrap();
+ let docker_args = tmp.path().join("docker-args.txt");
+ let docker = tmp.path().join("docker");
+ std::fs::write(
+ &docker,
+ format!(
+ "#!/usr/bin/env bash\nprintf '%s\\n' \"$@\" > {}\n",
+ shell_path(&docker_args)
+ ),
+ )
+ .unwrap();
+ {
+ use std::os::unix::fs::PermissionsExt;
+ std::fs::set_permissions(&docker, std::fs::Permissions::from_mode(0o755)).unwrap();
+ }
+
+ let mut child = shell_script_command(&repo_root().join("bin/ai-memory"))
+ .args(args)
+ .env("AI_MEMORY_DOCKER", shell_path(&docker))
+ .env("AI_MEMORY_NO_VERSION_CHECK", "1")
+ .env("AI_MEMORY_DATA_VOLUME", "test-ai-memory-data")
+ .env("HOME", shell_path(tmp.path()))
+ .env_remove("AI_MEMORY_SERVER_URL")
+ .env_remove("CLAUDE_CONFIG_DIR")
+ .stdin(Stdio::piped())
+ .stdout(Stdio::piped())
+ .stderr(Stdio::piped())
+ .spawn()
+ .unwrap();
+ child
+ .stdin
+ .take()
+ .expect("stdin was piped")
+ .write_all(stdin_payload.as_bytes())
+ .unwrap();
+ let output = child.wait_with_output().unwrap();
+ assert!(
+ output.status.success(),
+ "wrapper failed: stdout={} stderr={}",
+ String::from_utf8_lossy(&output.stdout),
+ String::from_utf8_lossy(&output.stderr)
+ );
+ std::fs::read_to_string(docker_args).unwrap()
+}
+
+#[cfg(unix)]
+#[test]
+fn wrapper_keeps_stdin_attached_when_it_is_a_pipe() {
+ let args = run_wrapper_with_piped_stdin(
+ &["write-page", "--path", "notes/x.md", "--body", "-"],
+ "# body that must survive the container boundary\n",
+ );
+ let flags: Vec<&str> = args.lines().collect();
+
+ // Without `-i` docker gives the container a closed stdin, so `--body -`
+ // reads an empty string and the page is persisted with frontmatter only —
+ // silently, because the CLI still reports a successful write.
+ assert!(
+ flags.contains(&"-i"),
+ "piped stdin must stay attached for `--body -`; got {args}"
+ );
+ // A pipe is not a terminal: asking for a TTY here makes docker fail with
+ // "the input device is not a TTY".
+ assert!(
+ !flags.contains(&"-t") && !flags.contains(&"-it"),
+ "no TTY may be requested when stdin is a pipe; got {args}"
+ );
+}
+
#[cfg(unix)]
#[test]
fn managed_run_wrapper_uses_host_binary_path_and_remote_server_without_docker() {