Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
feat(hooks): Grok script fallback prints PostToolUse context
Giovanni Coutinhoa86a62e93277
diff --git a/hooks/grok/post-tool-use.ps1 b/hooks/grok/post-tool-use.ps1index 31d7ba81..7413ae90 100755--- a/hooks/grok/post-tool-use.ps1+++ b/hooks/grok/post-tool-use.ps1@@ -1,3 +1,5 @@+# First PostToolUse of a Grok session: additionalContext is what the+# model actually sees. SessionStart and UserPromptSubmit must not fetch.. "$PSScriptRoot\..\lib\ai-memory-hook.ps1"-Invoke-AiMemoryHook -Event "post-tool-use" -Agent "grok"+Invoke-AiMemoryHook -Event "post-tool-use" -Agent "grok" -FetchHandoff -GrokPostToolexit 0diff --git a/hooks/grok/post-tool-use.sh b/hooks/grok/post-tool-use.shindex a90d381d..788ec9e2 100755--- a/hooks/grok/post-tool-use.sh+++ b/hooks/grok/post-tool-use.sh@@ -1,5 +1,9 @@#!/bin/sh# Grok Build CLI post-tool-use hook.+# PostToolUse stdout is the channel Grok shows the model+# (hookSpecificOutput.additionalContext, after the tool result).+# SessionStart and UserPromptSubmit stdout are discarded, so they must+# not accept the handoff. This fetch runs once per session._lib_dir="$(dirname "$0")"[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/..". "$_lib_dir/_lib.sh"@@ -8,8 +12,30 @@ SERVER="${AI_MEMORY_HOOK_URL:-http://127.0.0.1:49374}"PAYLOAD=$(cat)CWD=$(ai_memory_extract_cwd "$PAYLOAD")QS=$(ai_memory_marker_qs "$CWD")+SESSION_ID=$(ai_memory_extract_session_id "$PAYLOAD")+SESSION_QS=""+[ -n "$SESSION_ID" ] && SESSION_QS="&session_id=$(ai_memory_url_encode "$SESSION_ID")"printf '%s' "$PAYLOAD" \| ai_memory_post_hook "$SERVER/hook?event=post-tool-use&agent=grok${QS}" >/dev/null 2>&1 || true++SHOWN_KEY="$SESSION_ID"+if [ -z "$SHOWN_KEY" ]; then+ SHOWN_KEY="grok-post-$(printf '%s' "grok:$CWD" | cksum | awk '{print $1}')"+fi+SHOWN=$(ai_memory_briefed_file "post-$SHOWN_KEY")+if [ -f "$SHOWN" ]; 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)+ai_memory_mark_briefed "$SHOWN"+if [ -n "$HANDOFF" ]; then+ CTX=$(printf '%s' "$HANDOFF" | ai_memory_json_string)+ printf '{"hookSpecificOutput":{"hookEventName":"PostToolUse","additionalContext":%s}}\n' "$CTX"+ exit 0+fiprintf '{}\n'exit 0diff --git a/hooks/lib/ai-memory-hook.ps1 b/hooks/lib/ai-memory-hook.ps1index a4fd6848..b35274bc 100644--- a/hooks/lib/ai-memory-hook.ps1+++ b/hooks/lib/ai-memory-hook.ps1@@ -300,7 +300,10 @@ function Invoke-AiMemoryHook {# first prompt — parity with Claude's once-per-SessionStart brief).# Later fetches keep the handoff but drop the briefing params so the# server does not recompose the brief per prompt.- [switch] $BriefingOncePerSession+ [switch] $BriefingOncePerSession,+ # Grok PostToolUse: wrap a fetched handoff as additionalContext and+ # only fetch once per session. Other events must not set this.+ [switch] $GrokPostTool)$Server = if ($env:AI_MEMORY_HOOK_URL) { $env:AI_MEMORY_HOOK_URL } else { "http://127.0.0.1:49374" }@@ -349,6 +352,7 @@ function Invoke-AiMemoryHook {if ($FetchHandoff) {$NativeSessionQS = ""+ $NativeSessionId = $nulltry {$ParsedPayload = $Payload | ConvertFrom-Json$NativeSessionId = @(@@ -363,6 +367,16 @@ function Invoke-AiMemoryHook {}} catch {}+ $Shown = $null+ if ($GrokPostTool) {+ $ShownKey = [string]$NativeSessionId+ if (-not $ShownKey) { $ShownKey = "grok-post-$PID" }+ $Shown = Get-AiMemoryBriefedFile -Key "post-$ShownKey"+ if (Test-Path $Shown -PathType Leaf) {+ [Console]::Out.Write("{}")+ return+ }+ }# Once-per-session briefing gate. Marker files are created only for# repositories that opt in. Prefer the native session id when Kimi# supplies one; otherwise use a stable hash of agent+cwd.@@ -383,6 +397,9 @@ function Invoke-AiMemoryHook {}}}+ if ($GrokPostTool -and -not $BriefQS) {+ $BriefQS = Get-AiMemoryBriefingQuery -Cwd $Cwd+ }try {$Response = Invoke-WebRequest `-UseBasicParsing `@@ -390,7 +407,15 @@ function Invoke-AiMemoryHook {-Uri "$Server/handoff?agent=$Agent$QS$NativeSessionQS$BriefQS" `-Headers $Headersif ($null -ne $Response -and $Response.Content) {- if ($AntigravityPreInvocationOutput) {+ if ($GrokPostTool) {+ $Wrapped = @{+ hookSpecificOutput = @{+ hookEventName = "PostToolUse"+ additionalContext = $Response.Content+ }+ }+ [Console]::Out.Write(($Wrapped | ConvertTo-Json -Depth 5 -Compress))+ } elseif ($AntigravityPreInvocationOutput) {$Payload = @{injectSteps = @(@{ ephemeralMessage = $Response.Content })}@@ -398,14 +423,17 @@ function Invoke-AiMemoryHook {} else {[Console]::Out.Write($Response.Content)}- } elseif ($AntigravityPreInvocationOutput) {+ } elseif ($AntigravityPreInvocationOutput -or $GrokPostTool) {[Console]::Out.Write("{}")}} catch {- if ($AntigravityPreInvocationOutput) {+ if ($AntigravityPreInvocationOutput -or $GrokPostTool) {[Console]::Out.Write("{}")}}+ if ($Shown) {+ Set-AiMemoryBriefed -Path $Shown+ }# Mark the session as briefed only AFTER the GET completed —# success or error (fail-open: with the server down, re-sending the# brief-flagged request on every prompt would deliver nothing