Skip to content

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

Rust

fix(macos): drain command output while the child runs

Samir Hanna Verza35c22bd0a00b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d7569b73..6cc46c88 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,10 @@ version in [`docs/changelog/`](docs/changelog/):
## [Unreleased]
+### Fixed
+
+- Fixed false command timeouts in the macOS menu app when CLI output or diagnostics exceeded a pipe buffer. (#PR)
+
## [3.2.10] - 2026-10-29
### Changed
diff --git a/companions/ai-memory-macos/README.md b/companions/ai-memory-macos/README.md
index 1dddf60f..bd8d071b 100644
--- a/companions/ai-memory-macos/README.md
+++ b/companions/ai-memory-macos/README.md
@@ -42,7 +42,7 @@ Drag the `.app` to `/Applications` for a stable LaunchAgent path. Notarization,
1. Open the app (menu bar extra; no Dock icon).
2. **Install & Start Server** runs the bundled `ai-memory init` if `config.toml` is missing, renders the existing launchd template, and `launchctl bootstrap`s `com.github.akitaonrails.ai-memory`.
3. The status item turns green when `GET /admin/status` succeeds.
-4. **Open Web UI**, **Show Status…** (bundled `ai-memory status`), **Open Config**, **Open Data Directory**, **Open Logs**.
+4. **Open Web UI**, **Show Status…** (bundled `ai-memory status`), **Open Config**, **Open Data Directory**, **Open Logs**. CLI output and diagnostics are collected while the command runs, so a large status report does not fill an unread pipe.
Updates: replace `/Applications/AI Memory.app`. The data dir is untouched. If the helper path inside the bundle changed, **Restart Server** re-renders the plist.
diff --git a/companions/ai-memory-macos/Sources/AIMemoryMenuCore/ProcessRunner.swift b/companions/ai-memory-macos/Sources/AIMemoryMenuCore/ProcessRunner.swift
index ab551177..054bcc69 100644
--- a/companions/ai-memory-macos/Sources/AIMemoryMenuCore/ProcessRunner.swift
+++ b/companions/ai-memory-macos/Sources/AIMemoryMenuCore/ProcessRunner.swift
@@ -52,6 +52,10 @@ public struct ProcessRunner: CommandRunning {
try process.run()
+ // A child can fill either pipe before exiting, so both must be drained
+ // while the timeout loop waits for it.
+ let outReader = ProcessOutput(stdout.fileHandleForReading)
+ let errReader = ProcessOutput(stderr.fileHandleForReading)
let deadline = Date().addingTimeInterval(timeout)
while process.isRunning, Date() < deadline {
Thread.sleep(forTimeInterval: 0.05)
@@ -61,16 +65,34 @@ public struct ProcessRunner: CommandRunning {
throw CliError.timeout
}
- let outData = stdout.fileHandleForReading.readDataToEndOfFile()
- let errData = stderr.fileHandleForReading.readDataToEndOfFile()
return ProcessResult(
exitCode: process.terminationStatus,
- stdout: String(data: outData, encoding: .utf8) ?? "",
- stderr: String(data: errData, encoding: .utf8) ?? ""
+ stdout: String(data: outReader.read(), encoding: .utf8) ?? "",
+ stderr: String(data: errReader.read(), encoding: .utf8) ?? ""
)
}
}
+private final class ProcessOutput: @unchecked Sendable {
+ private let completed = DispatchGroup()
+ private let lock = NSLock()
+ private var data = Data()
+
+ init(_ handle: FileHandle) {
+ completed.enter()
+ DispatchQueue.global().async {
+ let output = handle.readDataToEndOfFile()
+ self.lock.withLock { self.data = output }
+ self.completed.leave()
+ }
+ }
+
+ func read() -> Data {
+ completed.wait()
+ return lock.withLock { data }
+ }
+}
+
public enum CliError: Error, Equatable, Sendable {
case timeout
case missingRuntime
diff --git a/companions/ai-memory-macos/Tests/AIMemoryMenuTests/ProcessRunnerTests.swift b/companions/ai-memory-macos/Tests/AIMemoryMenuTests/ProcessRunnerTests.swift
new file mode 100644
index 00000000..dd012c63
--- /dev/null
+++ b/companions/ai-memory-macos/Tests/AIMemoryMenuTests/ProcessRunnerTests.swift
@@ -0,0 +1,35 @@
+import Foundation
+import Testing
+@testable import AIMemoryMenuCore
+
+struct ProcessRunnerTests {
+ @Test func drainsBothPipesWhileTheChildRuns() throws {
+ let result = try ProcessRunner(timeout: 5).run(
+ binary: URL(fileURLWithPath: "/bin/sh"),
+ arguments: ["-c", "head -c 1048576 /dev/zero; head -c 1048576 /dev/zero >&2"],
+ extraEnv: [:]
+ )
+ #expect(result.exitCode == 0)
+ #expect(result.stdout.utf8.count == 1048576)
+ #expect(result.stderr.utf8.count == 1048576)
+ }
+
+ @Test func preservesTextEnvironmentAndExitStatus() throws {
+ let result = try ProcessRunner().run(
+ binary: URL(fileURLWithPath: "/bin/sh"),
+ arguments: ["-c", "printf '%s' \"$AI_MEMORY_RUNNER_TEST\"; printf 'diagnostic\\n' >&2; exit 7"],
+ extraEnv: ["AI_MEMORY_RUNNER_TEST": "memória 支付\n"]
+ )
+ #expect(result == ProcessResult(exitCode: 7, stdout: "memória 支付\n", stderr: "diagnostic\n"))
+ }
+
+ @Test func stillTimesOutForAChildThatDoesNotExit() {
+ #expect(throws: CliError.timeout) {
+ try ProcessRunner(timeout: 0.1).run(
+ binary: URL(fileURLWithPath: "/bin/sleep"),
+ arguments: ["5"],
+ extraEnv: [:]
+ )
+ }
+ }
+}