Skip to content

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

Rust

fix(wrapper): preserve stdin across every output mode

AkitaOnRails0055407e6563
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8db204f5..115feb00 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -45,15 +45,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
ambiguous. Unpackaged installs keep resolving to the plain path.
(#250)
-### 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 (#243).
+ now always pass `-i`, while `-t` is added only when stdin and stdout are
+ both terminals. `AI_MEMORY_NO_TTY=1` disables only TTY allocation and
+ no longer disconnects stdin. (#243)
## [1.18.0] - 2026-07-23
diff --git a/bin/ai-memory b/bin/ai-memory
index fa90ad17..4deecc26 100755
--- a/bin/ai-memory
+++ b/bin/ai-memory
@@ -302,15 +302,12 @@ 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=()
+# Keep stdin attached in every mode. This covers pipes and redirects, plus the
+# easy-to-miss terminal-stdin/redirected-stdout case. `AI_MEMORY_NO_TTY`
+# suppresses only the pseudo-terminal allocation.
+TTY_ARGS=(-i)
if [ -z "${AI_MEMORY_NO_TTY:-}" ] && [ -t 0 ] && [ -t 1 ]; then
- TTY_ARGS=(-it)
-elif [ ! -t 0 ]; then
- TTY_ARGS=(-i)
+ TTY_ARGS+=(-t)
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 840f07bc..3dc5f83f 100644
--- a/bin/ai-memory.ps1
+++ b/bin/ai-memory.ps1
@@ -44,14 +44,11 @@ $HomePath = (Resolve-Path -LiteralPath $HOME).Path
$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.
+$DockerArgs = @("run", "--rm", "-i")
+# Keep stdin attached in every mode. `AI_MEMORY_NO_TTY` suppresses only the
+# pseudo-terminal allocation.
if (-not $env:AI_MEMORY_NO_TTY -and -not [Console]::IsInputRedirected -and -not [Console]::IsOutputRedirected) {
- $DockerArgs += "-it"
-} elseif ([Console]::IsInputRedirected) {
- $DockerArgs += "-i"
+ $DockerArgs += "-t"
}
$DockerArgs += @(
diff --git a/crates/ai-memory-cli/tests/packaging.rs b/crates/ai-memory-cli/tests/packaging.rs
index 19b6d8ca..d0f94359 100644
--- a/crates/ai-memory-cli/tests/packaging.rs
+++ b/crates/ai-memory-cli/tests/packaging.rs
@@ -285,6 +285,37 @@ fn wrapper_keeps_stdin_attached_when_it_is_a_pipe() {
);
}
+#[test]
+fn docker_wrappers_keep_stdin_attached_independently_of_tty_allocation() {
+ let posix = read_repo("bin/ai-memory");
+ assert!(
+ posix.contains("TTY_ARGS=(-i)"),
+ "POSIX wrapper must attach stdin before inspecting terminal state"
+ );
+ assert!(
+ posix.contains("TTY_ARGS+=(-t)"),
+ "POSIX wrapper must add TTY allocation separately"
+ );
+ assert!(
+ !posix.contains("TTY_ARGS=(-it)"),
+ "combined flags can drop stdin when only stdout is redirected"
+ );
+
+ let powershell = read_repo("bin/ai-memory.ps1");
+ assert!(
+ powershell.contains("$DockerArgs = @(\"run\", \"--rm\", \"-i\")"),
+ "PowerShell wrapper must attach stdin before inspecting console state"
+ );
+ assert!(
+ powershell.contains("$DockerArgs += \"-t\""),
+ "PowerShell wrapper must add TTY allocation separately"
+ );
+ assert!(
+ !powershell.contains("$DockerArgs += \"-it\""),
+ "PowerShell must not couple stdin attachment to TTY allocation"
+ );
+}
+
#[cfg(unix)]
#[test]
fn managed_run_wrapper_uses_host_binary_path_and_remote_server_without_docker() {