Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(hooks): find payload keys in linear time (#870)
mchlcs27616217b1cc
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 00f83a7e..addfc644 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -80,6 +80,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0checksum block, which concatenates every platform's file. The zip's smoketest now requires LF rather than tolerating either, so the format therelease claims is the format it ships. (#838)+- Shell hooks no longer pin a CPU core for minutes on a large payload. The+ `hooks/_lib.sh` extractors for `cwd`/`workspacePaths`/`workspace_roots`,+ the session id, and Antigravity's `invocationNum` located each key with+ `${payload#*"key"}`, which is quadratic in the payload size under dash and+ bash: a 200 KB Cursor `postToolUse` event spent minutes in+ `ai_memory_extract_cwd`, and concurrent hooks stayed at 100% CPU before+ ever reaching the POST. A shared `ai_memory_after_key` helper now finds the+ first occurrence with one linear `awk` pass (about 50 ms at 200 KB) and+ feeds the unchanged `sed` parsing, so the extracted values are the same as+ before. (#870)## [2.4.0] - 2026-09-21diff --git a/hooks/_lib.sh b/hooks/_lib.shindex fa01573e..5b2492d3 100644--- a/hooks/_lib.sh+++ b/hooks/_lib.sh@@ -148,10 +148,20 @@ ai_memory_json_unescape_path() {printf '%s' "$1" | sed 's/\\\\/\\/g; s/\\\//\//g'}+# Print the payload text after the first `"$1"` (a JSON key, quotes added+# here) and succeed, or fail when the key is absent. Same result as+# `${payload#*"key"}`, which is quadratic in the payload size under dash and+# bash and pinned a CPU for minutes on a large tool result (#870).+ai_memory_after_key() {+ LC_ALL=C awk -v k="\"$1\"" '+ found { print; next }+ { i = index($0, k); if (i) { found = 1; print substr($0, i + length(k)) } }+ END { exit !found }'+}+ai_memory_extract_cwd() {payload="${1:-$(cat)}"- rest=${payload#*\"cwd\"}- if [ "$rest" != "$payload" ]; then+ if rest=$(printf '%s' "$payload" | ai_memory_after_key cwd); thenraw=$(printf '%s' "$rest" \| sed -n -E 's/^[[:space:]]*:[[:space:]]*"([^"]*)".*/\1/p' \| head -n 1)@@ -165,8 +175,7 @@ ai_memory_extract_cwd() {# and its tool events send `cwd: ""`, so an empty match above must fall# through to here rather than returning the empty string.for key in workspacePaths workspace_roots; do- rest=${payload#*\"$key\"}- [ "$rest" = "$payload" ] && continue+ rest=$(printf '%s' "$payload" | ai_memory_after_key "$key") || continueraw=$(printf '%s' "$rest" \| sed -n -E 's/^[[:space:]]*:[[:space:]]*\[[[:space:]]*"([^"]*)".*/\1/p' \| head -n 1)@@ -183,8 +192,7 @@ ai_memory_extract_cwd() {ai_memory_extract_session_id() {payload="${1:-$(cat)}"for key in session_id sessionId sessionID session conversationId; do- rest=${payload#*\"$key\"}- if [ "$rest" != "$payload" ]; then+ if rest=$(printf '%s' "$payload" | ai_memory_after_key "$key"); thenprintf '%s' "$rest" \| sed -n -E 's/^[[:space:]]*:[[:space:]]*"([^"]*)".*/\1/p' \| head -n 1@@ -199,8 +207,7 @@ ai_memory_extract_session_id() {# a repeated invocation cannot consume a next-session handoff.ai_memory_antigravity_is_initial_invocation() {payload="${1:-$(cat)}"- rest=${payload#*\"invocationNum\"}- [ "$rest" != "$payload" ] || return 1+ rest=$(printf '%s' "$payload" | ai_memory_after_key invocationNum) || return 1value=$(printf '%s' "$rest" \| sed -n -E 's/^[[:space:]]*:[[:space:]]*([0-9]+)[[:space:]]*([,}]).*/\1/p' \| head -n 1)diff --git a/tests/hooks/test_lib.sh b/tests/hooks/test_lib.shindex 1786411f..40b4562b 100755--- a/tests/hooks/test_lib.sh+++ b/tests/hooks/test_lib.sh@@ -108,6 +108,20 @@ assert_eq "extract cwd from cursor workspace_roots" "/home/u/cur" \PAYLOAD_CURSOR_TOOL='{"session_id":"x","cwd":"","hook_event_name":"postToolUse","workspace_roots":["/home/u/cur"]}'assert_eq "extract cwd falls through cursor empty cwd" "/home/u/cur" \"$(ai_memory_extract_cwd "$PAYLOAD_CURSOR_TOOL")"+# A large tool result must not make extraction quadratic in the payload+# size: `${payload#*"key"}` pinned a CPU for minutes at a few hundred KB (#870).+BIG=$(awk 'BEGIN { for (i = 0; i < 262144; i++) printf "x" }')+PAYLOAD_CURSOR_BIG="{\"tool_output\":\"$BIG\",\"cwd\":\"\",\"workspace_roots\":[\"/home/u/big\"]}"+assert_eq "extract cwd from a 256 KB cursor tool payload" "/home/u/big" \+ "$(ai_memory_extract_cwd "$PAYLOAD_CURSOR_BIG")"+# Only the first occurrence of a key is read, so a non-string top-level value+# never lets a nested field of the same name take its place.+PAYLOAD_NULL_CWD='{"cwd":null,"tool_input":{"cwd":"/tmp/nested"},"workspace_roots":["/home/u/top"]}'+assert_eq "extract cwd ignores nested cwd after a null one" "/home/u/top" \+ "$(ai_memory_extract_cwd "$PAYLOAD_NULL_CWD")"+PAYLOAD_EMPTY_ROOTS='{"cwd":"","workspace_roots":[],"tool_input":{"workspace_roots":["/tmp/nested"]}}'+assert_eq "extract cwd ignores nested roots after an empty array" "" \+ "$(ai_memory_extract_cwd "$PAYLOAD_EMPTY_ROOTS")"antigravity_initial() {if ai_memory_antigravity_is_initial_invocation "$1"; then@@ -128,6 +142,11 @@ assert_eq "antigravity fractional invocation fails closed" "no" \"$(antigravity_initial '{"invocationNum":0.5,"conversationId":"agy"}')"assert_eq "extract antigravity conversation id" "agy" \"$(ai_memory_extract_session_id '{"conversationId":"agy"}')"+PAYLOAD_AGY_BIG="{\"prompt\":\"$BIG\",\"invocationNum\":0,\"conversationId\":\"agy\"}"+assert_eq "antigravity initial invocation after a 256 KB field" "yes" \+ "$(antigravity_initial "$PAYLOAD_AGY_BIG")"+assert_eq "extract session id after a 256 KB field" "agy" \+ "$(ai_memory_extract_session_id "$PAYLOAD_AGY_BIG")"FAKE_CURL_BIN="$TMP/fake-curl-bin"FAKE_CURL_LOG="$TMP/fake-curl.log"