Skip to content

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

Rust

fix(hooks): non-blocking stdin read in PowerShell hook helper (#48)

Mauro Couto de Paiva8c3f6a8bed68
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 73be45d9..e5e643a4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Fixed
+- **Windows PowerShell hooks no longer hang or stall the agent.** The shared
+ `hooks/lib/ai-memory-hook.ps1` read stdin via `[Console]::In.ReadToEnd()`,
+ which blocks indefinitely when the agent does not close the stdin pipe
+ (observed on Claude Code `PreCompact`); because the `Invoke-WebRequest`
+ timeout only starts after the read returns, a stuck read meant the hook
+ never POSTed anything. Stdin is now read asynchronously, guarded by
+ `[Console]::IsInputRedirected` with a 2s cap, so the hook can never freeze.
+ HTTP timeouts were also raised from 1s to 3s (POST) / 2s (handoff GET) to
+ tolerate remote servers over higher-latency links. The full raw payload is
+ still forwarded (parity with `_lib.sh`), so observation title/body stay
+ intact. Affects every agent still on the PowerShell hook runner
+ (Codex, Cursor, Gemini CLI, Antigravity, OpenCode on Windows).
+
## [0.6.1] - 2026-05-28
### Added
diff --git a/hooks/lib/ai-memory-hook.ps1 b/hooks/lib/ai-memory-hook.ps1
index 92a93afa..79e30ff1 100644
--- a/hooks/lib/ai-memory-hook.ps1
+++ b/hooks/lib/ai-memory-hook.ps1
@@ -61,6 +61,25 @@ function Get-AiMemoryMarkerQuery {
return $qs
}
+function Read-AiMemoryStdin {
+ try {
+ if (-not [Console]::IsInputRedirected) { return "" }
+ $StdinStream = [Console]::OpenStandardInput()
+ $StdinReader = [System.IO.StreamReader]::new($StdinStream, [System.Text.Encoding]::UTF8, $false, 4096)
+ $ReadTask = $StdinReader.ReadToEndAsync()
+ if ($ReadTask.Wait(2000)) {
+ $result = $ReadTask.Result
+ $StdinReader.Dispose()
+ $StdinStream.Dispose()
+ return $result
+ }
+ $StdinReader.Dispose()
+ $StdinStream.Dispose()
+ } catch {
+ }
+ return ""
+}
+
function Invoke-AiMemoryHook {
param(
[Parameter(Mandatory = $true)] [string] $Event,
@@ -70,7 +89,7 @@ function Invoke-AiMemoryHook {
)
$Server = if ($env:AI_MEMORY_HOOK_URL) { $env:AI_MEMORY_HOOK_URL } else { "http://127.0.0.1:49374" }
- $Payload = [Console]::In.ReadToEnd()
+ $Payload = Read-AiMemoryStdin
$Cwd = Get-AiMemoryCwd -Payload $Payload
$QS = Get-AiMemoryMarkerQuery -Cwd $Cwd
$Headers = @{}
@@ -82,7 +101,7 @@ function Invoke-AiMemoryHook {
try {
Invoke-WebRequest `
-UseBasicParsing `
- -TimeoutSec 1 `
+ -TimeoutSec 3 `
-Method Post `
-Uri "$Server/hook?event=$Event&agent=$Agent$QS" `
-Headers $Headers `
@@ -95,7 +114,7 @@ function Invoke-AiMemoryHook {
try {
$Response = Invoke-WebRequest `
-UseBasicParsing `
- -TimeoutSec 1 `
+ -TimeoutSec 2 `
-Uri "$Server/handoff?agent=$Agent$QS" `
-Headers $Headers
if ($null -ne $Response -and $Response.Content) {