Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
feat(hooks): _lib.sh helper for .ai-memory.toml walk-up
Pablo Winterd9edaceee73b
diff --git a/hooks/_lib.sh b/hooks/_lib.shnew file mode 100644index 00000000..2703f597--- /dev/null+++ b/hooks/_lib.sh@@ -0,0 +1,71 @@+# ai-memory hook helper — find marker file + parse minimal TOML.+# Sourced by per-agent lifecycle hook scripts. POSIX shell only —+# no bash-isms, no external deps (no jq, no toml crate). Keep changes+# byte-trivial because every supported agent (claude-code, codex,+# cursor, gemini-cli, opencode, omp) sources this same file.++# Walk up from "$1" toward $HOME (or /) looking for `.ai-memory.toml`.+# Prints the absolute path of the first marker found, or nothing.+# Stops at $HOME to avoid leaking declarations from a shared system+# user's home into another user's session on multi-user boxes.+ai_memory_find_marker() {+ dir="$1"+ [ -z "$dir" ] && return 0+ while [ -n "$dir" ] && [ "$dir" != "/" ]; do+ if [ -f "$dir/.ai-memory.toml" ]; then+ printf '%s\n' "$dir/.ai-memory.toml"+ return 0+ fi+ if [ -n "${HOME:-}" ] && [ "$dir" = "$HOME" ]; then+ return 0+ fi+ parent=$(dirname "$dir")+ [ "$parent" = "$dir" ] && return 0+ dir="$parent"+ done+}++# Parse `key = "value"` at the TOML root (no nesting, no arrays, no+# tables). Returns the first match or nothing. Ignores comments and+# blank lines by construction (the regex only matches the `key = "..."`+# shape).+ai_memory_parse_toml_key() {+ file="$1"; key="$2"+ [ -f "$file" ] || return 0+ sed -n -E "s/^[[:space:]]*${key}[[:space:]]*=[[:space:]]*\"([^\"]*)\".*/\1/p" \+ "$file" | head -n 1+}++# Extract `cwd` from a JSON payload on stdin or in $1. Returns the+# value or nothing. Tolerates leading whitespace and escaped quotes+# inside other keys by anchoring on the `cwd` key explicitly.+ai_memory_extract_cwd() {+ payload="${1:-$(cat)}"+ printf '%s' "$payload" \+ | sed -n -E 's/.*"cwd"[[:space:]]*:[[:space:]]*"([^"]*)".*/\1/p' \+ | head -n 1+}++# URL-encode the minimal set of characters that have meaning in a query+# string. Sufficient for the schema's value regex (`^[a-z0-9][a-z0-9._-]*$`)+# plus a defensive pass for anything a hand-edited marker might contain.+ai_memory_url_encode() {+ printf '%s' "$1" \+ | sed 's/%/%25/g; s/&/%26/g; s/=/%3D/g; s/?/%3F/g; s/#/%23/g; s/ /%20/g'+}++# Build a `&workspace=X&project=Y` suffix from the marker file walked up+# from "$1". Returns the suffix (with the leading `&`) or nothing. The+# server tolerates either key being absent.+ai_memory_marker_qs() {+ cwd="$1"+ [ -z "$cwd" ] && return 0+ marker=$(ai_memory_find_marker "$cwd")+ [ -z "$marker" ] && return 0+ ws=$(ai_memory_parse_toml_key "$marker" workspace)+ pr=$(ai_memory_parse_toml_key "$marker" project)+ qs=""+ [ -n "$ws" ] && qs="${qs}&workspace=$(ai_memory_url_encode "$ws")"+ [ -n "$pr" ] && qs="${qs}&project=$(ai_memory_url_encode "$pr")"+ printf '%s' "$qs"+}diff --git a/tests/hooks/test_lib.sh b/tests/hooks/test_lib.shnew file mode 100755index 00000000..7da57142--- /dev/null+++ b/tests/hooks/test_lib.sh@@ -0,0 +1,78 @@+#!/bin/sh+# Smoke tests for hooks/_lib.sh. Run from the repo root:+#+# sh tests/hooks/test_lib.sh+#+# Exits non-zero on any failure. POSIX shell + sed only, so no extra+# CI setup needed.+set -eu++# shellcheck source=../../hooks/_lib.sh+. "$(dirname "$0")/../../hooks/_lib.sh"++PASS=0+FAIL=0+TMP=$(mktemp -d)+# Pin HOME inside the temp tree so walk-up never leaves the sandbox.+ORIG_HOME=${HOME:-}+HOME="$TMP"+export HOME+trap 'rm -rf "$TMP"; HOME=$ORIG_HOME' EXIT++assert_eq() {+ desc="$1"; want="$2"; got="$3"+ if [ "$want" = "$got" ]; then+ PASS=$((PASS+1))+ printf ' ok %s\n' "$desc"+ else+ FAIL=$((FAIL+1))+ printf ' FAIL %s\n want=%s\n got =%s\n' "$desc" "$want" "$got"+ fi+}++# --- parse_toml_key ---------------------------------------------------+cat >"$TMP/sample.toml" <<EOF+# Comment line+workspace = "movvia"+project = "pe-portais"++# Trailing comment+EOF++assert_eq "parse workspace" "movvia" "$(ai_memory_parse_toml_key "$TMP/sample.toml" workspace)"+assert_eq "parse project" "pe-portais" "$(ai_memory_parse_toml_key "$TMP/sample.toml" project)"+assert_eq "absent key returns empty" "" "$(ai_memory_parse_toml_key "$TMP/sample.toml" missing)"+assert_eq "absent file returns empty" "" "$(ai_memory_parse_toml_key "$TMP/no-such-file.toml" workspace)"++# --- find_marker ------------------------------------------------------+mkdir -p "$TMP/a/b/c/d"+printf 'workspace = "deep"\n' >"$TMP/a/.ai-memory.toml"+assert_eq "walks up to find marker" "$TMP/a/.ai-memory.toml" \+ "$(ai_memory_find_marker "$TMP/a/b/c/d")"+assert_eq "no marker returns empty" "" \+ "$(ai_memory_find_marker "$TMP/nonexistent/path")"++# --- extract_cwd ------------------------------------------------------+PAYLOAD='{"session_id":"x","cwd":"/home/u/foo","tool":"Read"}'+assert_eq "extract cwd from payload" "/home/u/foo" "$(ai_memory_extract_cwd "$PAYLOAD")"+assert_eq "extract cwd from empty json" "" "$(ai_memory_extract_cwd '{}')"++# --- marker_qs --------------------------------------------------------+QS=$(ai_memory_marker_qs "$TMP/a/b/c")+assert_eq "marker_qs single key" "&workspace=deep" "$QS"++printf 'workspace = "ws1"\nproject = "p1"\n' >"$TMP/a/b/.ai-memory.toml"+QS2=$(ai_memory_marker_qs "$TMP/a/b/c")+assert_eq "closer marker wins" "&workspace=ws1&project=p1" "$QS2"++QS3=$(ai_memory_marker_qs "$TMP/nonexistent")+assert_eq "no marker -> empty qs" "" "$QS3"++# --- url_encode -------------------------------------------------------+assert_eq "url_encode passes safe slug" "movvia" "$(ai_memory_url_encode "movvia")"+assert_eq "url_encode escapes ampersand" "a%26b" "$(ai_memory_url_encode "a&b")"+assert_eq "url_encode escapes equals" "a%3Db" "$(ai_memory_url_encode "a=b")"++# --- summary ----------------------------------------------------------+printf '\n%d passed, %d failed\n' "$PASS" "$FAIL"+[ "$FAIL" -eq 0 ]