Skip to content

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

Rust

fix(hooks): drop dead bin/regen-hooks, enumerate bundles in parity test

omartelo17f8deba5f3d
diff --git a/bin/regen-hooks b/bin/regen-hooks
deleted file mode 100755
index 18dd23b3..00000000
--- a/bin/regen-hooks
+++ /dev/null
@@ -1,148 +0,0 @@
-#!/usr/bin/env bash
-# Regenerate every hook script under hooks/*/*.sh.
-#
-# The 21 hook scripts (3 agents × 7 events) are nearly identical —
-# only the `event=` query string and the agent name in comments
-# differ. Maintaining them by hand drifts; this script is the single
-# source of truth.
-#
-# Adding a new lifecycle event:
-# 1. Add a row to the EVENTS array below.
-# 2. Add the matching variant to CLAUDE_CODE_EVENTS in
-# crates/ai-memory-cli/src/commands/render_shared.rs
-# (keep them in sync — the Rust constant drives the install-hooks
-# JSON config, this script drives the on-disk scripts).
-# 3. Run `bin/regen-hooks`.
-# 4. Commit both changes together.
-#
-# Run from anywhere; resolves the repo root from $0.
-
-set -euo pipefail
-
-SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
-ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
-HOOKS_DIR="$ROOT/hooks"
-
-# Map: agent dir → agent= query string the server expects.
-declare -A AGENT_QUERY=(
- [claude-code]="claude-code"
- [codex]="codex"
- [opencode]="open-code"
-)
-
-# Map: agent dir → human-readable name used in script comments.
-declare -A AGENT_LABEL=(
- [claude-code]="Claude Code"
- [codex]="codex"
- [opencode]="opencode"
-)
-
-# Each row: (filename-no-extension, event-query-value). Keep in sync
-# with crates/ai-memory-cli/src/commands/render_shared.rs.
-EVENTS=(
- "session-start:session-start"
- "user-prompt-submit:user-prompt"
- "pre-tool-use:pre-tool-use"
- "post-tool-use:post-tool-use"
- "pre-compact:pre-compact"
- "stop:stop"
- "session-end:session-end"
-)
-
-write_simple_hook() {
- local dir=$1 event_file=$2 event_q=$3
- local agent_q="${AGENT_QUERY[$dir]}"
- local agent_label="${AGENT_LABEL[$dir]}"
- cat > "$HOOKS_DIR/$dir/$event_file.sh" <<EOF
-#!/bin/sh
-# $agent_label $event_q hook.
-# Forwards the event JSON to the ai-memory server. Adds an
-# Authorization: Bearer header when AI_MEMORY_AUTH_TOKEN is set in
-# this hook's environment (set it via install-hooks --auth-token).
-SERVER="\${AI_MEMORY_HOOK_URL:-http://127.0.0.1:49374}"
-post_hook() {
- if [ -n "\${AI_MEMORY_AUTH_TOKEN:-}" ]; then
- curl -s --max-time 0.5 -X POST "\$1" \\
- -H "Content-Type: application/json" \\
- -H "Authorization: Bearer \$AI_MEMORY_AUTH_TOKEN" \\
- --data-binary @-
- else
- curl -s --max-time 0.5 -X POST "\$1" \\
- -H "Content-Type: application/json" \\
- --data-binary @-
- fi
-}
-post_hook "\$SERVER/hook?event=$event_q&agent=$agent_q" >/dev/null 2>&1 || true
-exit 0
-EOF
-}
-
-write_session_start_hook() {
- local dir=$1
- local agent_q="${AGENT_QUERY[$dir]}"
- local agent_label="${AGENT_LABEL[$dir]}"
- cat > "$HOOKS_DIR/$dir/session-start.sh" <<EOF
-#!/bin/sh
-# $agent_label SessionStart hook.
-#
-# 1. Forwards the event JSON to the ai-memory server (fire-and-forget).
-# 2. Synchronously fetches any pending cross-agent handoff and prints
-# it to stdout — agent CLIs prepend session-start hook stdout to
-# the next session, so the resuming agent sees prior context with
-# no human in the loop.
-#
-# Both calls carry an Authorization: Bearer header when
-# AI_MEMORY_AUTH_TOKEN is set in this hook's environment.
-SERVER="\${AI_MEMORY_HOOK_URL:-http://127.0.0.1:49374}"
-PAYLOAD=\$(cat)
-
-post_hook() {
- if [ -n "\${AI_MEMORY_AUTH_TOKEN:-}" ]; then
- curl -s --max-time 0.5 -X POST "\$1" \\
- -H "Content-Type: application/json" \\
- -H "Authorization: Bearer \$AI_MEMORY_AUTH_TOKEN" \\
- --data-binary @-
- else
- curl -s --max-time 0.5 -X POST "\$1" \\
- -H "Content-Type: application/json" \\
- --data-binary @-
- fi
-}
-
-get_handoff() {
- if [ -n "\${AI_MEMORY_AUTH_TOKEN:-}" ]; then
- curl -s --max-time 1.0 "\$1" \\
- -H "Authorization: Bearer \$AI_MEMORY_AUTH_TOKEN"
- else
- curl -s --max-time 1.0 "\$1"
- fi
-}
-
-echo "\$PAYLOAD" | post_hook "\$SERVER/hook?event=session-start&agent=$agent_q" >/dev/null 2>&1 || true
-get_handoff "\$SERVER/handoff?agent=$agent_q" 2>/dev/null || true
-exit 0
-EOF
-}
-
-written=0
-for dir in claude-code codex opencode; do
- mkdir -p "$HOOKS_DIR/$dir"
- for row in "${EVENTS[@]}"; do
- IFS=':' read -r event_file event_q <<< "$row"
- case "$event_file" in
- session-start)
- write_session_start_hook "$dir"
- ;;
- *)
- write_simple_hook "$dir" "$event_file" "$event_q"
- ;;
- esac
- written=$((written + 1))
- done
-done
-
-chmod +x "$HOOKS_DIR"/*/*.sh
-
-echo "✓ Regenerated $written hook scripts across $(ls "$HOOKS_DIR" | wc -l) agents."
-echo " Verify the change: git diff hooks/"
-echo " Restore on regret: git restore hooks/"
diff --git a/crates/ai-memory-cli/src/commands/install_hooks.rs b/crates/ai-memory-cli/src/commands/install_hooks.rs
index d0a05e65..67849dfe 100644
--- a/crates/ai-memory-cli/src/commands/install_hooks.rs
+++ b/crates/ai-memory-cli/src/commands/install_hooks.rs
@@ -5702,17 +5702,26 @@ model = "gpt-5"
"PowerShell hooks require the shared lib helper"
);
- for agent_dir in [
- "claude-code",
- "codex",
- "cursor",
- "gemini-cli",
- "grok",
- "devin",
- "opencode",
- "antigravity-cli",
- "kimi-code",
- ] {
+ // Enumerate the bundles instead of listing them: a hardcoded
+ // list silently skips whatever agent lands next, which is how
+ // `command-code` and `kiro-cli` shipped uncovered. `lib/` holds
+ // the shared PowerShell helper, not an agent bundle.
+ let mut agent_dirs: Vec<String> = fs::read_dir(&hooks_root)
+ .unwrap_or_else(|e| panic!("failed to read {}: {e}", hooks_root.display()))
+ .map(|entry| entry.unwrap().path())
+ .filter(|path| path.is_dir())
+ .filter_map(|path| path.file_name()?.to_str().map(str::to_string))
+ .filter(|name| name != "lib")
+ .collect();
+ agent_dirs.sort();
+ assert!(
+ !agent_dirs.is_empty(),
+ "no agent hook bundles found under {}",
+ hooks_root.display()
+ );
+
+ for agent_dir in agent_dirs {
+ let agent_dir = agent_dir.as_str();
let dir = hooks_root.join(agent_dir);
let mut sh = BTreeMap::new();
let mut ps1 = BTreeMap::new();
diff --git a/docs/v0.3-roadmap.md b/docs/v0.3-roadmap.md
index 414f5c61..f82b940f 100644
--- a/docs/v0.3-roadmap.md
+++ b/docs/v0.3-roadmap.md
@@ -81,13 +81,25 @@ audit + gitleaks on `ubuntu-latest` only. Worth adding:
## Smaller polish
-### Hook scripts as a generated artefact
-
-The 21 hook scripts live in `hooks/*/*.sh` as files. There's no
-regenerator. The shape is identical across agents (only
-`agent=open-code` vs `agent=codex` etc. differs). A `bin/regen-hooks`
-script that templates them from `CLAUDE_CODE_EVENTS` + a per-agent
-name would let us add a new event in one place.
+### Hook scripts as a generated artefact — tried, dropped
+
+A `bin/regen-hooks` templating the scripts from one flat
+agent × event matrix existed and was removed. The premise stopped
+holding: the bundles are no longer identical. Agents carry their own
+event vocabularies (`CLAUDE_CODE_EVENTS`, `KIMI_CODE_EVENTS`,
+`KIRO_CLI_V*_EVENTS`, Devin's `post-compaction`, …), each `.sh` has a
+`.ps1` peer, the bodies delegate to `hooks/_lib.sh` /
+`hooks/lib/ai-memory-hook.ps1`, and several carry per-agent behaviour
+(Claude Code's `hookSpecificOutput` wrapper, Antigravity's gated
+startup handoff). A generator faithful to that is `render_shared.rs`
+rewritten in bash.
+
+The drift it was meant to prevent is covered instead by
+`bundled_posix_and_powershell_hooks_stay_in_parity`, which enumerates
+every bundle under `hooks/` and asserts each `.sh` has a `.ps1` peer
+posting the same `event=`/`agent=`. Adding an event means adding both
+files plus the entry in the agent's event array in
+`crates/ai-memory-cli/src/commands/render_shared.rs`.
### Consolidator prompt templating