Skip to content

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

Rust

fix(plugins): retry an old binary against AI_MEMORY_SERVER_URL before loopback

AkitaOnRailsad145789e87c
diff --git a/crates/ai-memory-cli/templates/plugin/ai-memory-hook.sh b/crates/ai-memory-cli/templates/plugin/ai-memory-hook.sh
index 5e699e8c..f4826031 100644
--- a/crates/ai-memory-cli/templates/plugin/ai-memory-hook.sh
+++ b/crates/ai-memory-cli/templates/plugin/ai-memory-hook.sh
@@ -95,11 +95,13 @@ fi
# config when --server-url is omitted. An older binary requires the flag
# (and would post an empty one to a bare /hook), so it rejects the call with
# a usage error, exit 2, before reading stdin. Retry it once against the
-# default local server, which still has the whole payload, and say why.
+# server AI_MEMORY_SERVER_URL names, else the default local server; stdin
+# still has the whole payload. Say why.
status=0
"$bin" "$@" || status=$?
if [ "$status" -ne 2 ]; then
exit "$status"
fi
-printf '%s\n' "ai-memory: this ai-memory binary predates the plugin (it needs 3.0 or later); sending to http://127.0.0.1:49374. Upgrade ai-memory, or set the plugin's server_url option." >&2
-exec "$bin" "$@" --server-url "http://127.0.0.1:49374"
+fallback="${AI_MEMORY_SERVER_URL:-http://127.0.0.1:49374}"
+printf '%s\n' "ai-memory: this ai-memory binary predates the plugin (it needs 3.0 or later); sending to $fallback. Upgrade ai-memory, or set the plugin's server_url option." >&2
+exec "$bin" "$@" --server-url "$fallback"
diff --git a/plugins/claude-code/scripts/ai-memory-hook b/plugins/claude-code/scripts/ai-memory-hook
index 5e699e8c..f4826031 100755
--- a/plugins/claude-code/scripts/ai-memory-hook
+++ b/plugins/claude-code/scripts/ai-memory-hook
@@ -95,11 +95,13 @@ fi
# config when --server-url is omitted. An older binary requires the flag
# (and would post an empty one to a bare /hook), so it rejects the call with
# a usage error, exit 2, before reading stdin. Retry it once against the
-# default local server, which still has the whole payload, and say why.
+# server AI_MEMORY_SERVER_URL names, else the default local server; stdin
+# still has the whole payload. Say why.
status=0
"$bin" "$@" || status=$?
if [ "$status" -ne 2 ]; then
exit "$status"
fi
-printf '%s\n' "ai-memory: this ai-memory binary predates the plugin (it needs 3.0 or later); sending to http://127.0.0.1:49374. Upgrade ai-memory, or set the plugin's server_url option." >&2
-exec "$bin" "$@" --server-url "http://127.0.0.1:49374"
+fallback="${AI_MEMORY_SERVER_URL:-http://127.0.0.1:49374}"
+printf '%s\n' "ai-memory: this ai-memory binary predates the plugin (it needs 3.0 or later); sending to $fallback. Upgrade ai-memory, or set the plugin's server_url option." >&2
+exec "$bin" "$@" --server-url "$fallback"
diff --git a/plugins/codex/scripts/ai-memory-hook b/plugins/codex/scripts/ai-memory-hook
index 5e699e8c..f4826031 100755
--- a/plugins/codex/scripts/ai-memory-hook
+++ b/plugins/codex/scripts/ai-memory-hook
@@ -95,11 +95,13 @@ fi
# config when --server-url is omitted. An older binary requires the flag
# (and would post an empty one to a bare /hook), so it rejects the call with
# a usage error, exit 2, before reading stdin. Retry it once against the
-# default local server, which still has the whole payload, and say why.
+# server AI_MEMORY_SERVER_URL names, else the default local server; stdin
+# still has the whole payload. Say why.
status=0
"$bin" "$@" || status=$?
if [ "$status" -ne 2 ]; then
exit "$status"
fi
-printf '%s\n' "ai-memory: this ai-memory binary predates the plugin (it needs 3.0 or later); sending to http://127.0.0.1:49374. Upgrade ai-memory, or set the plugin's server_url option." >&2
-exec "$bin" "$@" --server-url "http://127.0.0.1:49374"
+fallback="${AI_MEMORY_SERVER_URL:-http://127.0.0.1:49374}"
+printf '%s\n' "ai-memory: this ai-memory binary predates the plugin (it needs 3.0 or later); sending to $fallback. Upgrade ai-memory, or set the plugin's server_url option." >&2
+exec "$bin" "$@" --server-url "$fallback"
diff --git a/tests/hooks/test_plugin_launcher.sh b/tests/hooks/test_plugin_launcher.sh
index a86645d0..44867915 100755
--- a/tests/hooks/test_plugin_launcher.sh
+++ b/tests/hooks/test_plugin_launcher.sh
@@ -129,6 +129,15 @@ case "$(cat "$TMP/stderr")" in
*"needs 3.0 or later"*) PASS=$((PASS+1)); printf ' ok %s\n' "an old binary gets a version-skew hint" ;;
*) FAIL=$((FAIL+1)); printf ' FAIL version-skew hint: %s\n' "$(cat "$TMP/stderr")" ;;
esac
+# The retry honours AI_MEMORY_SERVER_URL, so an old binary pointed at a
+# remote server never sends to whatever listens on local loopback.
+printf '{"prompt":"remote"}' | run_launcher CLAUDE_PLUGIN_ROOT="$PLUGIN_ROOT_DIR" \
+ CLAUDE_PLUGIN_OPTION_AI_MEMORY_BIN="$OLD_DIR/ai-memory" \
+ AI_MEMORY_SERVER_URL="https://memory.example:8443" \
+ sh "$LAUNCHER" claude-code user-prompt-submit 2>/dev/null >/dev/null || true
+assert_eq "an old binary's retry uses AI_MEMORY_SERVER_URL" \
+ "hook|--event|user-prompt-submit|--agent|claude-code|--server-url|https://memory.example:8443" \
+ "$(paste -sd '|' "$TMP/argv")"
printf '{}' | TEST_PATH="$STUB_DIR:/usr/bin:/bin" run_launcher CLAUDE_PLUGIN_ROOT="$PLUGIN_ROOT_DIR" \
CLAUDE_PLUGIN_OPTION_AI_MEMORY_BIN="ai-memory" sh "$LAUNCHER" claude-code stop >/dev/null