Skip to content

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

Rust

fix(hooks): check grok subagent markers as payload keys, not raw substrings

Rudy Mizrahi Celeklidff7549e99a1
diff --git a/hooks/_lib.sh b/hooks/_lib.sh
index 569944f8..7fb409e4 100644
--- a/hooks/_lib.sh
+++ b/hooks/_lib.sh
@@ -287,6 +287,45 @@ ai_memory_extract_session_id() {
done
}
+# True when the payload claims to be a subagent/child session: a known marker
+# key at the TOP LEVEL holding a non-empty string. Parity with the native
+# router (`commands/hook.rs::payload_is_subagent`) and the PowerShell
+# `Test-AiMemorySubagentPayload`, both of which read keyed values. The depth
+# walk is what makes it a payload check rather than a text search: tool input
+# and tool output routinely quote these key names (reading a source file that
+# names them is enough), and a session must not lose its handoff over that.
+ai_memory_payload_is_subagent() {
+ printf '%s' "${1:-$(cat)}" | LC_ALL=C awk '
+ { buf = buf $0 "\n" }
+ END {
+ split("subagentType subagent_type agent_type agent_id parentSessionId", names, " ")
+ for (n in names) marker[names[n]] = 1
+ len = length(buf)
+ depth = 0
+ want = "key"
+ for (i = 1; i <= len; i++) {
+ c = substr(buf, i, 1)
+ if (c == "{" || c == "[") { depth++; if (depth == 1) want = "key"; continue }
+ if (c == "}" || c == "]") { depth--; continue }
+ if (c == ",") { if (depth == 1) want = "key"; continue }
+ if (c != "\"") continue
+ tok = ""
+ for (i++; i <= len; i++) {
+ c = substr(buf, i, 1)
+ if (c == "\\") { tok = tok substr(buf, i, 2); i++; continue }
+ if (c == "\"") break
+ tok = tok c
+ }
+ if (depth != 1) continue
+ if (want == "key") { key = tok; want = "value"; continue }
+ gsub(/^[ \t\r\n]+|[ \t\r\n]+$/, "", tok)
+ if (marker[key] && tok != "") exit 0
+ want = "key"
+ }
+ exit 1
+ }'
+}
+
# Antigravity's PreInvocation hook fires before every model call. Only the
# documented invocationNum=0 boundary represents the startup event that
# ai-memory maps to SessionStart. Missing or malformed counters fail closed so
diff --git a/hooks/grok/post-tool-use.sh b/hooks/grok/post-tool-use.sh
index 9d8504c7..64bf51c9 100755
--- a/hooks/grok/post-tool-use.sh
+++ b/hooks/grok/post-tool-use.sh
@@ -31,13 +31,12 @@ fi
# Subagent payloads must not take the parent's handoff — same key set as
# the native router (`commands/hook.rs::payload_is_subagent`): subagentType,
-# subagent_type, agent_type, agent_id, parentSessionId.
-case "$PAYLOAD" in
- *'"subagentType"'*|*'"subagent_type"'*|*'"agent_type"'*|*'"agent_id"'*|*'"parentSessionId"'*|*'\"subagentType\"'*)
- printf '{}\n'
- exit 0
- ;;
-esac
+# subagent_type, agent_type, agent_id, parentSessionId. Keyed, so a tool
+# result that merely quotes one of those names cannot suppress the fetch.
+if ai_memory_payload_is_subagent "$PAYLOAD"; then
+ printf '{}\n'
+ exit 0
+fi
BRIEF_QS=$(ai_memory_briefing_qs "$CWD")
HANDOFF=$(ai_memory_get_handoff "$SERVER/handoff?agent=grok${QS}${SESSION_QS}${BRIEF_QS}" 2>/dev/null || true)
if [ -n "$HANDOFF" ]; then
diff --git a/tests/hooks/test_lib.sh b/tests/hooks/test_lib.sh
index 4b8f1865..611037c5 100755
--- a/tests/hooks/test_lib.sh
+++ b/tests/hooks/test_lib.sh
@@ -187,6 +187,28 @@ assert_eq "antigravity initial invocation after a 256 KB field" "yes" \
assert_eq "extract session id after a 256 KB field" "agy" \
"$(ai_memory_extract_session_id "$PAYLOAD_AGY_BIG")"
+# A subagent marker only counts as a top-level key with a non-empty string
+# value, so a tool result that quotes the key name is still a root payload.
+subagent_payload() {
+ if ai_memory_payload_is_subagent "$1"; then printf 'yes'; else printf 'no'; fi
+}
+assert_eq "top-level agent_id marks a subagent" "yes" \
+ "$(subagent_payload '{"session_id":"s","agent_id":"planner"}')"
+assert_eq "top-level parentSessionId marks a subagent" "yes" \
+ "$(subagent_payload '{"parentSessionId":"p","session_id":"s"}')"
+assert_eq "blank marker value is not a subagent" "no" \
+ "$(subagent_payload '{"session_id":"s","agent_id":" "}')"
+assert_eq "marker key quoted in a tool result is not a subagent" "no" \
+ "$(subagent_payload '{"session_id":"s","tool_response":"keys = [\"subagentType\", \"agent_id\"]"}')"
+assert_eq "marker key and value quoted in a tool result is not a subagent" "no" \
+ "$(subagent_payload '{"session_id":"s","tool_response":"{\"agent_id\": \"x\"}"}')"
+assert_eq "marker nested in tool input is not a subagent" "no" \
+ "$(subagent_payload '{"session_id":"s","tool_input":{"agent_id":"x"}}')"
+assert_eq "marker nested after a non-string field is not a subagent" "no" \
+ "$(subagent_payload '{"session_id":"s","tool_input":{"timeout":30,"agent_id":"x"}}')"
+assert_eq "no marker at all is not a subagent" "no" \
+ "$(subagent_payload '{"session_id":"s","tool_name":"read_file"}')"
+
FAKE_CURL_BIN="$TMP/fake-curl-bin"
FAKE_CURL_LOG="$TMP/fake-curl.log"
mkdir -p "$FAKE_CURL_BIN"
@@ -969,6 +991,36 @@ for key in subagentType subagent_type agent_type agent_id parentSessionId; do
assert_eq "grok: $key payload sent zero GETs" "0" "$(grok_gets)"
done
+# 3b. A marker key nested in a tool's arguments or result is data, not a
+# claim about the session: a root payload like this still fetches.
+grok_case nested-marker
+NESTED='{"session_id":"g-nested","cwd":"'"$GROK_CASE_DIR"'","tool_name":"bash","tool_input":{"timeout":30,"agent_id":"planner-a"}}'
+OUT=$(grok_run "$NESTED")
+case "$OUT" in
+ *'"additionalContext":"HANDOFF BODY"'*)
+ PASS=$((PASS + 1))
+ printf ' ok %s\n' "grok: a marker nested in tool input still fetches" ;;
+ *)
+ FAIL=$((FAIL + 1))
+ printf ' FAIL grok: a marker nested in tool input still fetches\n got =%s\n' "$OUT" ;;
+esac
+assert_eq "grok: nested marker payload GETs the handoff once" "1" "$(grok_gets)"
+
+# 3c. A blank top-level marker is not a child session either — parity with
+# `payload_is_subagent`, which requires a non-empty string.
+grok_case blank-marker
+BLANK='{"session_id":"g-blank","cwd":"'"$GROK_CASE_DIR"'","tool_name":"read_file","agent_id":" "}'
+OUT=$(grok_run "$BLANK")
+case "$OUT" in
+ *'"additionalContext":"HANDOFF BODY"'*)
+ PASS=$((PASS + 1))
+ printf ' ok %s\n' "grok: a blank marker value still fetches" ;;
+ *)
+ FAIL=$((FAIL + 1))
+ printf ' FAIL grok: a blank marker value still fetches\n got =%s\n' "$OUT" ;;
+esac
+assert_eq "grok: blank marker payload GETs the handoff once" "1" "$(grok_gets)"
+
unset AI_MEMORY_DATA_DIR GROK_LOG
PATH=${PATH#"$TMP/bin:"}
export PATH