Skip to content

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

Rust

fix(hooks): emit JSON from Claude Code hooks to stop "plain text" debug spam

Gustavo Cateim9c3cf572b097
diff --git a/hooks/_lib.sh b/hooks/_lib.sh
index e4dad52b..0064510c 100644
--- a/hooks/_lib.sh
+++ b/hooks/_lib.sh
@@ -117,14 +117,17 @@ ai_memory_get_handoff() {
fi
}
-# Encode stdin as a JSON string. Used only by Antigravity's PreInvocation
-# hook, whose stdout contract is JSON rather than raw context text.
+# Encode stdin as a JSON string (with surrounding quotes). Used by hooks
+# whose stdout contract is JSON rather than raw context text: Antigravity's
+# PreInvocation hook and Claude Code's session-start hook (which wraps the
+# handoff in hookSpecificOutput.additionalContext).
ai_memory_json_string() {
awk '
BEGIN { printf "\"" }
{
gsub(/\\/, "\\\\")
gsub(/"/, "\\\"")
+ gsub(/\t/, "\\t")
gsub(/\r/, "\\r")
printf "%s%s", sep, $0
sep = "\\n"
diff --git a/hooks/claude-code/post-tool-use.sh b/hooks/claude-code/post-tool-use.sh
index 2342eaa0..7c8709a1 100755
--- a/hooks/claude-code/post-tool-use.sh
+++ b/hooks/claude-code/post-tool-use.sh
@@ -18,4 +18,10 @@ QS=$(ai_memory_marker_qs "$CWD")
printf '%s' "$PAYLOAD" \
| ai_memory_post_hook "$SERVER/hook?event=post-tool-use&agent=claude-code${QS}" >/dev/null 2>&1 || true
+# Acknowledge to Claude Code with an empty JSON object. This hook only
+# POSTs (fire-and-forget) and injects no context; without "{" on stdout
+# Claude Code treats the (empty) output as plain text and logs
+# "Hook output does not start with {, treating as plain text" on every
+# tool call. Emitting {} keeps the debug log clean.
+printf '{}\n'
exit 0
diff --git a/hooks/claude-code/pre-compact.sh b/hooks/claude-code/pre-compact.sh
index a73d46d6..49fdcca4 100755
--- a/hooks/claude-code/pre-compact.sh
+++ b/hooks/claude-code/pre-compact.sh
@@ -18,4 +18,10 @@ QS=$(ai_memory_marker_qs "$CWD")
printf '%s' "$PAYLOAD" \
| ai_memory_post_hook "$SERVER/hook?event=pre-compact&agent=claude-code${QS}" >/dev/null 2>&1 || true
+# Acknowledge to Claude Code with an empty JSON object. This hook only
+# POSTs (fire-and-forget) and injects no context; without "{" on stdout
+# Claude Code treats the (empty) output as plain text and logs
+# "Hook output does not start with {, treating as plain text".
+# Emitting {} keeps the debug log clean.
+printf '{}\n'
exit 0
diff --git a/hooks/claude-code/pre-tool-use.sh b/hooks/claude-code/pre-tool-use.sh
index b0ec2594..a10980f1 100755
--- a/hooks/claude-code/pre-tool-use.sh
+++ b/hooks/claude-code/pre-tool-use.sh
@@ -18,4 +18,10 @@ QS=$(ai_memory_marker_qs "$CWD")
printf '%s' "$PAYLOAD" \
| ai_memory_post_hook "$SERVER/hook?event=pre-tool-use&agent=claude-code${QS}" >/dev/null 2>&1 || true
+# Acknowledge to Claude Code with an empty JSON object. This hook only
+# POSTs (fire-and-forget) and injects no context; without "{" on stdout
+# Claude Code treats the (empty) output as plain text and logs
+# "Hook output does not start with {, treating as plain text" on every
+# tool call. Emitting {} keeps the debug log clean.
+printf '{}\n'
exit 0
diff --git a/hooks/claude-code/session-end.sh b/hooks/claude-code/session-end.sh
index 868ee98e..95b4b3d6 100755
--- a/hooks/claude-code/session-end.sh
+++ b/hooks/claude-code/session-end.sh
@@ -18,4 +18,10 @@ QS=$(ai_memory_marker_qs "$CWD")
printf '%s' "$PAYLOAD" \
| ai_memory_post_hook "$SERVER/hook?event=session-end&agent=claude-code${QS}" >/dev/null 2>&1 || true
+# Acknowledge to Claude Code with an empty JSON object. This hook only
+# POSTs (fire-and-forget) and injects no context; without "{" on stdout
+# Claude Code treats the (empty) output as plain text and logs
+# "Hook output does not start with {, treating as plain text".
+# Emitting {} keeps the debug log clean.
+printf '{}\n'
exit 0
diff --git a/hooks/claude-code/session-start.sh b/hooks/claude-code/session-start.sh
index 1c905f0c..65de120a 100755
--- a/hooks/claude-code/session-start.sh
+++ b/hooks/claude-code/session-start.sh
@@ -24,5 +24,17 @@ QS=$(ai_memory_marker_qs "$CWD")
printf '%s' "$PAYLOAD" \
| ai_memory_post_hook "$SERVER/hook?event=session-start&agent=claude-code${QS}" >/dev/null 2>&1 || true
-ai_memory_get_handoff "$SERVER/handoff?agent=claude-code${QS}" 2>/dev/null || true
+# Claude Code prepends a SessionStart hook's stdout to the resuming
+# session as context. Emit it as structured JSON
+# (hookSpecificOutput.additionalContext) instead of raw text: bare text
+# does not start with "{", so Claude Code logs every session start as
+# "Hook output does not start with {, treating as plain text". JSON
+# injects the same handoff with a clean debug log; no handoff -> "{}".
+HANDOFF=$(ai_memory_get_handoff "$SERVER/handoff?agent=claude-code${QS}" 2>/dev/null || true)
+if [ -n "$HANDOFF" ]; then
+ printf '{"hookSpecificOutput":{"hookEventName":"SessionStart","additionalContext":%s}}\n' \
+ "$(printf '%s' "$HANDOFF" | ai_memory_json_string)"
+else
+ printf '{}\n'
+fi
exit 0
diff --git a/hooks/claude-code/stop.sh b/hooks/claude-code/stop.sh
index e6c67b12..57e3b5c2 100755
--- a/hooks/claude-code/stop.sh
+++ b/hooks/claude-code/stop.sh
@@ -18,4 +18,10 @@ QS=$(ai_memory_marker_qs "$CWD")
printf '%s' "$PAYLOAD" \
| ai_memory_post_hook "$SERVER/hook?event=stop&agent=claude-code${QS}" >/dev/null 2>&1 || true
+# Acknowledge to Claude Code with an empty JSON object. This hook only
+# POSTs (fire-and-forget) and injects no context; without "{" on stdout
+# Claude Code treats the (empty) output as plain text and logs
+# "Hook output does not start with {, treating as plain text".
+# Emitting {} keeps the debug log clean.
+printf '{}\n'
exit 0
diff --git a/hooks/claude-code/user-prompt-submit.sh b/hooks/claude-code/user-prompt-submit.sh
index c2de7196..3dcd8855 100755
--- a/hooks/claude-code/user-prompt-submit.sh
+++ b/hooks/claude-code/user-prompt-submit.sh
@@ -18,4 +18,10 @@ QS=$(ai_memory_marker_qs "$CWD")
printf '%s' "$PAYLOAD" \
| ai_memory_post_hook "$SERVER/hook?event=user-prompt&agent=claude-code${QS}" >/dev/null 2>&1 || true
+# Acknowledge to Claude Code with an empty JSON object. This hook only
+# POSTs (fire-and-forget) and injects no context; without "{" on stdout
+# Claude Code treats the (empty) output as plain text and logs
+# "Hook output does not start with {, treating as plain text" on every
+# prompt. Emitting {} keeps the debug log clean.
+printf '{}\n'
exit 0