Skip to content

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

Rust

fix(hooks): retry transient 4xx in shell and powershell hook spool

Vinícius DS Andradedc6e6714d4c9
diff --git a/crates/ai-memory-hooks/tests/suite/powershell_spool.rs b/crates/ai-memory-hooks/tests/suite/powershell_spool.rs
index f365460d..93f6770e 100644
--- a/crates/ai-memory-hooks/tests/suite/powershell_spool.rs
+++ b/crates/ai-memory-hooks/tests/suite/powershell_spool.rs
@@ -295,3 +295,58 @@ fn powershell_drain_delivers_and_retires_entries() {
"2xx and terminal 4xx both retire their entries"
);
}
+
+#[test]
+fn powershell_drain_keeps_backlog_on_429_saturation() {
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
+ let server = format!("http://{}", listener.local_addr().unwrap());
+ let receiver = thread::spawn(move || {
+ // 429 indicates server queue saturation (backpressure):
+ // the drain must stop and keep the entry queued rather than retiring it.
+ serve_one(
+ &listener,
+ b"HTTP/1.1 429 Too Many Requests\r\nContent-Length: 0\r\nConnection: close\r\n\r\n",
+ )
+ });
+
+ let home = tempfile::tempdir().unwrap();
+ let data_dir = home.path().join("data");
+ let spool = data_dir.join("hook-spool");
+ std::fs::create_dir_all(&spool).unwrap();
+ let body = r#"{"e":"saturated"}"#;
+ std::fs::write(
+ spool.join("0000000000007-1-0000000000000001.json"),
+ serde_json::json!({
+ "url": format!("{server}/hook?event=stop&agent=codex&ingest_key=pscccccccccccccccc"),
+ "body": body,
+ "created_ms": 7_u64,
+ "auth_mode": "none",
+ "attempts": 0,
+ })
+ .to_string(),
+ )
+ .unwrap();
+
+ let program = format!(
+ ". '{lib}'; Invoke-AiMemoryDrainSpool -Max 8",
+ lib = hook_lib()
+ );
+ let output = run_powershell(
+ &program,
+ home.path(),
+ &[("AI_MEMORY_DATA_DIR", data_dir.to_str().unwrap())],
+ );
+ assert!(
+ output.status.success(),
+ "PowerShell drain failed: {}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+
+ let (_headers, received_body) = receiver.join().unwrap();
+ assert_eq!(received_body, body.as_bytes());
+ assert_eq!(
+ spool_files(&data_dir).len(),
+ 1,
+ "a 429 response must keep the entry queued"
+ );
+}
diff --git a/hooks/_lib.sh b/hooks/_lib.sh
index 084e3017..fddc2735 100644
--- a/hooks/_lib.sh
+++ b/hooks/_lib.sh
@@ -945,6 +945,7 @@ ai_memory_post_hook() {
fi
case "$_amcode" in
2*) ai_memory_kick_drain ;;
+ 408|425|429) ai_memory_spool_event "$_amurl" "$_ambody" ;;
4*) ;;
*) ai_memory_spool_event "$_amurl" "$_ambody" ;;
esac
@@ -1166,10 +1167,10 @@ ai_memory_json_field() {
}
# Deliver the queued backlog, oldest first. Bounded by count so a drain never
-# becomes an unbounded upload. A 2xx or 4xx retires the entry (delivered, or
-# permanently rejected); anything else stops the pass and keeps the remainder
-# for the next one. The bearer goes through a 0600 header file rather than
-# curl's argv, for the reason #552 moved it off the command line.
+# becomes an unbounded upload. A 2xx or terminal 4xx retires the entry (delivered,
+# or permanently rejected); transient 408/425/429 or anything else stops the pass
+# and keeps the remainder for the next one. The bearer goes through a 0600 header file
+# rather than curl's argv, for the reason #552 moved it off the command line.
ai_memory_drain_spool() {
_amdmax=${1:-64}
_amddir=$(ai_memory_spool_dir)
@@ -1198,6 +1199,7 @@ ai_memory_drain_spool() {
--data-binary @- 2>/dev/null) || _amdcode=000
fi
case "$_amdcode" in
+ 408|425|429) return 0 ;;
2*|4*) rm -f "$_amdf" 2>/dev/null || true ;;
*) return 0 ;;
esac
diff --git a/hooks/lib/ai-memory-hook.ps1 b/hooks/lib/ai-memory-hook.ps1
index 022c0885..aa1af94f 100644
--- a/hooks/lib/ai-memory-hook.ps1
+++ b/hooks/lib/ai-memory-hook.ps1
@@ -800,7 +800,9 @@ function Invoke-AiMemoryDrainSpool {
try { $code = [int]$response.StatusCode } catch { $code = 0 }
}
}
- if (($code -ge 200 -and $code -lt 300) -or ($code -ge 400 -and $code -lt 500)) {
+ if ($code -eq 408 -or $code -eq 425 -or $code -eq 429) {
+ break
+ } elseif (($code -ge 200 -and $code -lt 300) -or ($code -ge 400 -and $code -lt 500)) {
Remove-Item -Force -LiteralPath $file.FullName -ErrorAction SilentlyContinue
} else {
break
@@ -1047,9 +1049,9 @@ function Invoke-AiMemoryHook {
if ($_.Exception.Response) {
try { $Status = [int]$_.Exception.Response.StatusCode } catch { $Status = 0 }
}
- # 4xx = permanent rejection (not retried); everything else that
- # failed to deliver is spooled.
- if ($Status -lt 400 -or $Status -ge 500) {
+ # 4xx = permanent rejection (not retried); 408/425/429 (transient timeout
+ # / saturation) and everything else that failed to deliver is spooled.
+ if ($Status -lt 400 -or $Status -ge 500 -or $Status -eq 408 -or $Status -eq 425 -or $Status -eq 429) {
Write-AiMemorySpoolEvent -Url $HookUrl -Body $Payload
}
}
diff --git a/tests/hooks/test_lib.sh b/tests/hooks/test_lib.sh
index 6d333462..0aeee979 100755
--- a/tests/hooks/test_lib.sh
+++ b/tests/hooks/test_lib.sh
@@ -638,6 +638,42 @@ printf '%s' '{"e":"unreachable"}' \
assert_eq "post_hook spools an undelivered event" "1" \
"$(ls "$TMP/spool-data/hook-spool/"*.json 2>/dev/null | wc -l | tr -d ' ')"
+# A 429 saturation response must spool the event for retry, while terminal 4xx is dropped.
+rm -f "$TMP/spool-data/hook-spool/"*.json
+curl() {
+ cat >/dev/null
+ printf '429'
+ return 0
+}
+printf '%s' '{"e":"saturated"}' \
+ | ai_memory_post_hook "http://127.0.0.1:49374/hook?event=post-tool-use&agent=cursor" >/dev/null 2>&1
+assert_eq "post_hook spools on 429 saturation" "1" \
+ "$(ls "$TMP/spool-data/hook-spool/"*.json 2>/dev/null | wc -l | tr -d ' ')"
+
+rm -f "$TMP/spool-data/hook-spool/"*.json
+curl() {
+ cat >/dev/null
+ printf '400'
+ return 0
+}
+printf '%s' '{"e":"bad-request"}' \
+ | ai_memory_post_hook "http://127.0.0.1:49374/hook?event=post-tool-use&agent=cursor" >/dev/null 2>&1
+assert_eq "post_hook drops terminal 400 refusal" "0" \
+ "$(ls "$TMP/spool-data/hook-spool/"*.json 2>/dev/null | wc -l | tr -d ' ')"
+
+# A 429 saturation response during drain must keep the entry queued rather than retiring it.
+rm -f "$TMP/spool-data/hook-spool/"*.json
+ai_memory_spool_event "http://127.0.0.1:49374/hook?event=stop&agent=cursor" '{"e":"drain-saturated"}'
+curl() {
+ cat >/dev/null
+ printf '429'
+ return 0
+}
+ai_memory_drain_spool 64 >/dev/null 2>&1
+assert_eq "drain keeps entry queued on 429 saturation" "1" \
+ "$(ls "$TMP/spool-data/hook-spool/"*.json 2>/dev/null | wc -l | tr -d ' ')"
+unset -f curl
+
unset AI_MEMORY_DATA_DIR
# --- PowerShell bundle spool parity (static) ---------------------------
@@ -656,7 +692,8 @@ PS_SPOOL_STATIC=$(grep -q 'function Write-AiMemorySpoolEvent' "$PS_LIB" \
&& grep -Fq '"auth_mode"' "$PS_LIB" \
&& grep -Fq 'Write-AiMemorySpoolEvent -Url' "$PS_LIB" \
&& grep -Fq 'Invoke-AiMemoryKickDrain' "$PS_LIB" \
- && grep -Fq 'if ($Status -lt 400 -or $Status -ge 500) {' "$PS_LIB" \
+ && grep -Fq 'if ($Status -lt 400 -or $Status -ge 500 -or $Status -eq 408 -or $Status -eq 425 -or $Status -eq 429) {' "$PS_LIB" \
+ && grep -Fq 'if ($code -eq 408 -or $code -eq 425 -or $code -eq 429) {' "$PS_LIB" \
&& grep -Fq -- '--ai-memory-drain-spool' "$PS_LIB" \
&& grep -Fq '$script:AiMemoryLibFile = $PSCommandPath' "$PS_LIB" \
&& grep -Fq 'RedirectStandardOutput' "$PS_LIB" \