Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(hooks): keep the shell bundle's requests off environment proxies
Rudy Mizrahi Celekli8051819d3d26
diff --git a/hooks/_lib.sh b/hooks/_lib.shindex 4ddd6709..569944f8 100644--- a/hooks/_lib.sh+++ b/hooks/_lib.sh@@ -933,7 +933,7 @@ ai_memory_post_hook() {_ambody=$(cat)_amhdr=$(ai_memory_auth_header_file || printf '')if [ -n "${AI_MEMORY_AUTH_TOKEN:-}" ]; then- _amcode=$(printf '%s' "$_ambody" | curl -s --max-time 0.2 -H "X-AI-Memory-Client: hook-sh" -o /dev/null \+ _amcode=$(printf '%s' "$_ambody" | curl -s --noproxy '*' --max-time 0.2 -H "X-AI-Memory-Client: hook-sh" -o /dev/null \-w '%{http_code}' -X POST "$_amurl" \-H "Content-Type: application/json" \-H "Authorization: Bearer $AI_MEMORY_AUTH_TOKEN" \@@ -941,13 +941,13 @@ ai_memory_post_hook() {elif [ -n "$_amhdr" ]; then# `-H @file`: curl reads the header from disk, so the bearer never# appears in curl's argv the way an inline `-H` would (#552).- _amcode=$(printf '%s' "$_ambody" | curl -s --max-time 0.2 -H "X-AI-Memory-Client: hook-sh" -o /dev/null \+ _amcode=$(printf '%s' "$_ambody" | curl -s --noproxy '*' --max-time 0.2 -H "X-AI-Memory-Client: hook-sh" -o /dev/null \-w '%{http_code}' -X POST "$_amurl" \-H "Content-Type: application/json" \-H @"$_amhdr" \--data-binary @- 2>/dev/null) || _amcode=000else- _amcode=$(printf '%s' "$_ambody" | curl -s --max-time 0.2 -H "X-AI-Memory-Client: hook-sh" -o /dev/null \+ _amcode=$(printf '%s' "$_ambody" | curl -s --noproxy '*' --max-time 0.2 -H "X-AI-Memory-Client: hook-sh" -o /dev/null \-w '%{http_code}' -X POST "$_amurl" \-H "Content-Type: application/json" \--data-binary @- 2>/dev/null) || _amcode=000@@ -985,12 +985,12 @@ ai_memory_get_handoff() {esac_amhdr=$(ai_memory_auth_header_file)if [ -n "${AI_MEMORY_AUTH_TOKEN:-}" ]; then- curl -s --max-time 1.0 -H "X-AI-Memory-Client: hook-sh" "$1" \+ curl -s --noproxy '*' --max-time 1.0 -H "X-AI-Memory-Client: hook-sh" "$1" \-H "Authorization: Bearer $AI_MEMORY_AUTH_TOKEN"elif [ -n "$_amhdr" ]; then- curl -s --max-time 1.0 -H "X-AI-Memory-Client: hook-sh" "$1" -H @"$_amhdr"+ curl -s --noproxy '*' --max-time 1.0 -H "X-AI-Memory-Client: hook-sh" "$1" -H @"$_amhdr"else- curl -s --max-time 1.0 -H "X-AI-Memory-Client: hook-sh" "$1"+ curl -s --noproxy '*' --max-time 1.0 -H "X-AI-Memory-Client: hook-sh" "$1"fi}@@ -1208,13 +1208,13 @@ ai_memory_drain_spool() {if [ -n "$_amdtok" ]; then_amdhdr="$_amddir/.drain-header.$$"(umask 077; printf 'Authorization: Bearer %s\n' "$_amdtok" >"$_amdhdr") 2>/dev/null || continue- _amdcode=$(printf '%s' "$_amdbody" | curl -s --max-time 2.0 -H "X-AI-Memory-Client: hook-sh" -o /dev/null \+ _amdcode=$(printf '%s' "$_amdbody" | curl -s --noproxy '*' --max-time 2.0 -H "X-AI-Memory-Client: hook-sh" -o /dev/null \-w '%{http_code}' -X POST "$_amdurl" \-H "Content-Type: application/json" -H @"$_amdhdr" \--data-binary @- 2>/dev/null) || _amdcode=000rm -f "$_amdhdr" 2>/dev/null || trueelse- _amdcode=$(printf '%s' "$_amdbody" | curl -s --max-time 2.0 -H "X-AI-Memory-Client: hook-sh" -o /dev/null \+ _amdcode=$(printf '%s' "$_amdbody" | curl -s --noproxy '*' --max-time 2.0 -H "X-AI-Memory-Client: hook-sh" -o /dev/null \-w '%{http_code}' -X POST "$_amdurl" \-H "Content-Type: application/json" \--data-binary @- 2>/dev/null) || _amdcode=000diff --git a/tests/hooks/test_lib.sh b/tests/hooks/test_lib.shindex dae1fdea..5050600c 100755--- a/tests/hooks/test_lib.sh+++ b/tests/hooks/test_lib.sh@@ -1290,6 +1290,59 @@ assert_eq "client marker: no request went out without it" "0" \"$(grep -c UNMARKED "$MARKER_LOG")"unset AI_MEMORY_DATA_DIR+# --- no environment proxy on any request --------------------------------+# A sandbox that exports a proxy with an empty NO_PROXY (ai-jail) otherwise+# sends the loopback POST and the handoff GET to that proxy, which answers+# 4xx. `ai_memory_post_hook` treats a 4xx as a permanent rejection, so the+# observation is dropped outright rather than spooled. The native client+# (`hook_capture::build_client`) and the MCP bridge both build with+# `no_proxy()`; every curl branch in the shell bundle must match them.+curl() {+ _tnp=PROXYABLE+ _tdata=""+ while [ "$#" -gt 0 ]; do+ case "$1" in+ --noproxy) shift; [ "$#" -gt 0 ] || break+ [ "$1" = '*' ] && _tnp=noproxy ;;+ -H|-X|-w|-o) shift ;;+ --data-binary) shift; [ "$#" -gt 0 ] || break; _tdata="$1" ;;+ esac+ shift+ done+ [ "$_tdata" = "@-" ] && cat >/dev/null+ printf '%s\n' "$_tnp" >>"$NOPROXY_LOG"+ printf '200'+ return 0+}+AI_MEMORY_DATA_DIR="$TMP/noproxy"+export AI_MEMORY_DATA_DIR+mkdir -p "$AI_MEMORY_DATA_DIR"+NOPROXY_LOG="$AI_MEMORY_DATA_DIR/noproxy.log"+: >"$NOPROXY_LOG"+noproxy_round() {+ ai_memory_get_handoff "http://127.0.0.1:49374/handoff?agent=cursor&cwd=%2Fwork" >/dev/null 2>&1 || true+ printf '%s' '{"e":"noproxy"}' \+ | ai_memory_post_hook "http://127.0.0.1:49374/hook?event=stop&agent=cursor" >/dev/null 2>&1+}+noproxy_round # no auth+(umask 077; printf 'Authorization: Bearer file-token\n' >"$AI_MEMORY_DATA_DIR/auth-header")+noproxy_round # header file+rm -f "$AI_MEMORY_DATA_DIR/auth-header"+ai_memory_spool_event "http://127.0.0.1:49374/hook?event=stop&agent=cursor" '{"e":"n1"}'+ai_memory_drain_spool 64 >/dev/null 2>&1 # drain without a token+AI_MEMORY_AUTH_TOKEN=env-token+export AI_MEMORY_AUTH_TOKEN+noproxy_round # env token+ai_memory_spool_event "http://127.0.0.1:49374/hook?event=stop&agent=cursor" '{"e":"n2"}'+unset AI_MEMORY_AUTH_TOKEN+ai_memory_drain_spool 64 >/dev/null 2>&1 # drain with the spooled token+unset -f curl+assert_eq "no proxy: every curl branch was exercised" "yes" \+ "$([ "$(awk 'END { print NR + 0 }' "$NOPROXY_LOG")" -ge 8 ] && printf 'yes' || printf 'no')"+assert_eq "no proxy: no request could reach an environment proxy" "0" \+ "$(grep -c PROXYABLE "$NOPROXY_LOG")"+unset AI_MEMORY_DATA_DIR+# The PowerShell helper builds its request headers in one hashtable per call# site; each must carry the marker, and every Invoke-WebRequest must pass one.PS_HELPER=hooks/lib/ai-memory-hook.ps1