Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(hooks): finish marker integration
AkitaOnRailse577e4b60f37
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 19abb069..86764499 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -10,9 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- `.ai-memory.toml` marker file lets a directory tree declare its`workspace` (required) and `project` (optional) without depending on`basename($cwd)`. Lifecycle hook scripts walk up from `cwd` to find- the closest marker and forward the declared names as- `&workspace=X&project=Y` query params on `POST /hook` and- `GET /handoff`. Server accepts the new params as optional overrides;+ the closest marker and forward `cwd` plus the declared names as+ query params on `POST /hook` and `GET /handoff`. Server accepts the+ new params as optional overrides;absent marker means the previous behaviour (`workspace = "default"`,`project = basename(cwd)`) — fully backward compatible. See[`docs/marker-file.md`](docs/marker-file.md).diff --git a/crates/ai-memory-cli/src/commands/install_hooks.rs b/crates/ai-memory-cli/src/commands/install_hooks.rsindex 28472646..121f9d04 100644--- a/crates/ai-memory-cli/src/commands/install_hooks.rs+++ b/crates/ai-memory-cli/src/commands/install_hooks.rs@@ -946,8 +946,17 @@ fn render_agent(/// Errors propagate when source is missing, the staging dir/// can't be created, or any file copy fails.fn stage_hook_scripts(source_dir: &Path, agent_label: &str) -> Result<PathBuf> {- let dest_root = dirs::data_local_dir()- .context("could not locate the user data-local directory (e.g. ~/.local/share)")?+ let data_dir = dirs::data_local_dir()+ .context("could not locate the user data-local directory (e.g. ~/.local/share)")?;+ stage_hook_scripts_in(source_dir, agent_label, &data_dir)+}++fn stage_hook_scripts_in(+ source_dir: &Path,+ agent_label: &str,+ data_local_dir: &Path,+) -> Result<PathBuf> {+ let dest_root = data_local_dir.join("ai-memory").join("hooks").join(agent_label);@@ -1301,7 +1310,8 @@ mod tests {fs::write(bundle.join("_lib.sh"), "# shared helper\n").unwrap();stub_scripts(&agent_src, &["session-start.sh", "post-tool-use.sh"]);- let staged = stage_hook_scripts(&agent_src, "stage-shared-lib").unwrap();+ let data_dir = tmp.path().join("data");+ let staged = stage_hook_scripts_in(&agent_src, "stage-shared-lib", &data_dir).unwrap();assert!(staged.join("session-start.sh").exists());assert!(staged.join("post-tool-use.sh").exists());assert!(@@ -1327,7 +1337,8 @@ mod tests {// Note: no _lib.sh in `bundle`.stub_scripts(&agent_src, &["session-start.sh"]);- let staged = stage_hook_scripts(&agent_src, "stage-no-lib").unwrap();+ let data_dir = tmp.path().join("data");+ let staged = stage_hook_scripts_in(&agent_src, "stage-no-lib", &data_dir).unwrap();assert!(staged.join("session-start.sh").exists());assert!(!staged.join("_lib.sh").exists());}diff --git a/crates/ai-memory-hooks/src/router.rs b/crates/ai-memory-hooks/src/router.rsindex 750850b6..191566bf 100644--- a/crates/ai-memory-hooks/src/router.rs+++ b/crates/ai-memory-hooks/src/router.rs@@ -706,6 +706,7 @@ mod tests {Query(HookQuery {event: "session-start".into(),agent: Some("claude-code".into()),+ ..Default::default()}),Json(serde_json::json!({})),)@@ -887,6 +888,50 @@ mod tests {);}+ #[tokio::test]+ async fn handoff_with_workspace_marker_and_cwd_uses_basename_project() {+ let tmp = TempDir::new().unwrap();+ let state = make_state(&tmp).await;+ let cwd = "/home/u/repo";++ let (ws, proj) = resolve_project_ids(&state, Some(cwd), Some("acme"), None)+ .await+ .unwrap();+ state+ .writer+ .insert_handoff(NewHandoff {+ workspace_id: ws,+ project_id: proj,+ from_session_id: None,+ from_agent: AgentKind::ClaudeCode,+ to_agent: None,+ cwd: Some(std::path::PathBuf::from(cwd)),+ summary: "handoff summary".to_string(),+ open_questions: Vec::new(),+ next_steps: vec!["continue".to_string()],+ files_touched: Vec::new(),+ })+ .await+ .unwrap();++ let rendered = fetch_and_accept_handoff(+ &state,+ HandoffQuery {+ agent: Some("codex".into()),+ cwd: Some(cwd.into()),+ workspace: Some("acme".into()),+ project: None,+ },+ )+ .await+ .unwrap();++ assert!(+ rendered.as_deref().is_some_and(|s| s.contains("continue")),+ "workspace-only marker handoff lookup must resolve workspace + basename(cwd)"+ );+ }+/// A marker file with `project = "pe-portais"` replaces the/// basename-derived project name for every descendant `cwd`.#[tokio::test]diff --git a/docs/marker-file.md b/docs/marker-file.mdindex 6383b126..544ac8fc 100644--- a/docs/marker-file.md+++ b/docs/marker-file.md@@ -25,7 +25,10 @@ ai-memory or running CLI commands per directory.`.ai-memory.toml` in **any ancestor** of your `cwd`. Lifecycle hookswalk up from `cwd` toward `$HOME` (or `/` if `$HOME` is unset) and-use the **first** marker found. Closer markers override outer ones.+use the **first** marker found. Closer markers override outer ones. When+a marker is found, hook scripts also forward the current `cwd` so+workspace-only markers can still resolve `project = basename(cwd)` for+handoff lookups.## Schemadiff --git a/hooks/_lib.sh b/hooks/_lib.shindex be741996..003e6280 100644--- a/hooks/_lib.sh+++ b/hooks/_lib.sh@@ -54,9 +54,10 @@ ai_memory_url_encode() {| 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.+# Build a query-string suffix from the marker file walked up from "$1".+# Returns the suffix (with the leading `&`) or nothing. `cwd` is included+# whenever a marker exists so `GET /handoff` can resolve workspace-only+# markers by combining `workspace` with basename(cwd).ai_memory_marker_qs() {cwd="$1"[ -z "$cwd" ] && return 0@@ -64,7 +65,7 @@ ai_memory_marker_qs() {[ -z "$marker" ] && return 0ws=$(ai_memory_parse_toml_key "$marker" workspace)pr=$(ai_memory_parse_toml_key "$marker" project)- qs=""+ qs="&cwd=$(ai_memory_url_encode "$cwd")"[ -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/hooks/codex/post-tool-use.sh b/hooks/codex/post-tool-use.shindex ba4003fa..b35e184a 100755--- a/hooks/codex/post-tool-use.sh+++ b/hooks/codex/post-tool-use.sh@@ -1,20 +1,15 @@#!/bin/sh# codex post-tool-use 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=post-tool-use&agent=codex" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=post-tool-use&agent=codex${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/codex/pre-compact.sh b/hooks/codex/pre-compact.shindex 4d237025..4359ae59 100755--- a/hooks/codex/pre-compact.sh+++ b/hooks/codex/pre-compact.sh@@ -1,20 +1,15 @@#!/bin/sh# codex pre-compact 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=pre-compact&agent=codex" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=pre-compact&agent=codex${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/codex/pre-tool-use.sh b/hooks/codex/pre-tool-use.shindex 00f004bd..0c2a4772 100755--- a/hooks/codex/pre-tool-use.sh+++ b/hooks/codex/pre-tool-use.sh@@ -1,20 +1,15 @@#!/bin/sh# codex pre-tool-use 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=pre-tool-use&agent=codex" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=pre-tool-use&agent=codex${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/codex/session-end.sh b/hooks/codex/session-end.shindex b6bbe5b4..d8809b67 100755--- a/hooks/codex/session-end.sh+++ b/hooks/codex/session-end.sh@@ -1,20 +1,15 @@#!/bin/sh# codex session-end 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=session-end&agent=codex" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=session-end&agent=codex${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/codex/session-start.sh b/hooks/codex/session-start.shindex 9fecc07b..4f8687e5 100755--- a/hooks/codex/session-start.sh+++ b/hooks/codex/session-start.sh@@ -1,39 +1,21 @@#!/bin/sh# codex 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.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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")-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=codex" >/dev/null 2>&1 || true-get_handoff "$SERVER/handoff?agent=codex" 2>/dev/null || true+printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=session-start&agent=codex${QS}" >/dev/null 2>&1 || true+ai_memory_get_handoff "$SERVER/handoff?agent=codex${QS}" 2>/dev/null || trueexit 0diff --git a/hooks/codex/stop.sh b/hooks/codex/stop.shindex ccd1e5c6..6e2a2f22 100755--- a/hooks/codex/stop.sh+++ b/hooks/codex/stop.sh@@ -1,20 +1,15 @@#!/bin/sh# codex stop 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=stop&agent=codex" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=stop&agent=codex${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/codex/user-prompt-submit.sh b/hooks/codex/user-prompt-submit.shindex 9d34ed8b..5dbc7022 100755--- a/hooks/codex/user-prompt-submit.sh+++ b/hooks/codex/user-prompt-submit.sh@@ -1,20 +1,15 @@#!/bin/sh# codex user-prompt 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=user-prompt&agent=codex" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=user-prompt&agent=codex${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/cursor/post-tool-use.sh b/hooks/cursor/post-tool-use.shindex 410f5b77..278622e8 100755--- a/hooks/cursor/post-tool-use.sh+++ b/hooks/cursor/post-tool-use.sh@@ -1,20 +1,15 @@#!/bin/sh# cursor post-tool-use 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=post-tool-use&agent=cursor" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=post-tool-use&agent=cursor${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/cursor/pre-compact.sh b/hooks/cursor/pre-compact.shindex 6e626f14..98a280ec 100755--- a/hooks/cursor/pre-compact.sh+++ b/hooks/cursor/pre-compact.sh@@ -1,20 +1,15 @@#!/bin/sh# cursor pre-compact 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=pre-compact&agent=cursor" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=pre-compact&agent=cursor${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/cursor/pre-tool-use.sh b/hooks/cursor/pre-tool-use.shindex c9a4891c..24ca1d71 100755--- a/hooks/cursor/pre-tool-use.sh+++ b/hooks/cursor/pre-tool-use.sh@@ -1,20 +1,15 @@#!/bin/sh# cursor pre-tool-use 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=pre-tool-use&agent=cursor" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=pre-tool-use&agent=cursor${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/cursor/session-end.sh b/hooks/cursor/session-end.shindex 24876086..cf1a2999 100755--- a/hooks/cursor/session-end.sh+++ b/hooks/cursor/session-end.sh@@ -1,20 +1,15 @@#!/bin/sh# cursor session-end 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=session-end&agent=cursor" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=session-end&agent=cursor${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/cursor/session-start.sh b/hooks/cursor/session-start.shindex 48eb0dab..518362ad 100755--- a/hooks/cursor/session-start.sh+++ b/hooks/cursor/session-start.sh@@ -1,39 +1,21 @@#!/bin/sh# cursor 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.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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")-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=cursor" >/dev/null 2>&1 || true-get_handoff "$SERVER/handoff?agent=cursor" 2>/dev/null || true+printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=session-start&agent=cursor${QS}" >/dev/null 2>&1 || true+ai_memory_get_handoff "$SERVER/handoff?agent=cursor${QS}" 2>/dev/null || trueexit 0diff --git a/hooks/cursor/stop.sh b/hooks/cursor/stop.shindex 57179739..1edcd0fd 100755--- a/hooks/cursor/stop.sh+++ b/hooks/cursor/stop.sh@@ -1,20 +1,15 @@#!/bin/sh# cursor stop 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=stop&agent=cursor" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=stop&agent=cursor${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/cursor/user-prompt-submit.sh b/hooks/cursor/user-prompt-submit.shindex 20441702..20e91a74 100755--- a/hooks/cursor/user-prompt-submit.sh+++ b/hooks/cursor/user-prompt-submit.sh@@ -1,20 +1,15 @@#!/bin/sh# cursor user-prompt 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=user-prompt&agent=cursor" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=user-prompt&agent=cursor${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/gemini-cli/post-tool-use.sh b/hooks/gemini-cli/post-tool-use.shindex 0bbffcde..f3650ae0 100755--- a/hooks/gemini-cli/post-tool-use.sh+++ b/hooks/gemini-cli/post-tool-use.sh@@ -1,20 +1,15 @@#!/bin/sh# gemini-cli post-tool-use 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=post-tool-use&agent=gemini-cli" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=post-tool-use&agent=gemini-cli${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/gemini-cli/pre-compact.sh b/hooks/gemini-cli/pre-compact.shindex 13c50878..5cec886c 100755--- a/hooks/gemini-cli/pre-compact.sh+++ b/hooks/gemini-cli/pre-compact.sh@@ -1,20 +1,15 @@#!/bin/sh# gemini-cli pre-compact 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=pre-compact&agent=gemini-cli" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=pre-compact&agent=gemini-cli${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/gemini-cli/pre-tool-use.sh b/hooks/gemini-cli/pre-tool-use.shindex 71e0fa3c..a98da58d 100755--- a/hooks/gemini-cli/pre-tool-use.sh+++ b/hooks/gemini-cli/pre-tool-use.sh@@ -1,20 +1,15 @@#!/bin/sh# gemini-cli pre-tool-use 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=pre-tool-use&agent=gemini-cli" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=pre-tool-use&agent=gemini-cli${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/gemini-cli/session-end.sh b/hooks/gemini-cli/session-end.shindex 574ab5cc..0423c9ed 100755--- a/hooks/gemini-cli/session-end.sh+++ b/hooks/gemini-cli/session-end.sh@@ -1,20 +1,15 @@#!/bin/sh# gemini-cli session-end 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=session-end&agent=gemini-cli" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=session-end&agent=gemini-cli${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/gemini-cli/session-start.sh b/hooks/gemini-cli/session-start.shindex 2384c0e7..7543f319 100755--- a/hooks/gemini-cli/session-start.sh+++ b/hooks/gemini-cli/session-start.sh@@ -1,39 +1,21 @@#!/bin/sh# gemini-cli 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.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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")-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=gemini-cli" >/dev/null 2>&1 || true-get_handoff "$SERVER/handoff?agent=gemini-cli" 2>/dev/null || true+printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=session-start&agent=gemini-cli${QS}" >/dev/null 2>&1 || true+ai_memory_get_handoff "$SERVER/handoff?agent=gemini-cli${QS}" 2>/dev/null || trueexit 0diff --git a/hooks/gemini-cli/stop.sh b/hooks/gemini-cli/stop.shindex 1a7e1e0c..6b1130a0 100755--- a/hooks/gemini-cli/stop.sh+++ b/hooks/gemini-cli/stop.sh@@ -1,20 +1,15 @@#!/bin/sh# gemini-cli stop 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=stop&agent=gemini-cli" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=stop&agent=gemini-cli${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/gemini-cli/user-prompt-submit.sh b/hooks/gemini-cli/user-prompt-submit.shindex 2a292e72..c8de622e 100755--- a/hooks/gemini-cli/user-prompt-submit.sh+++ b/hooks/gemini-cli/user-prompt-submit.sh@@ -1,20 +1,15 @@#!/bin/sh# gemini-cli user-prompt 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=user-prompt&agent=gemini-cli" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=user-prompt&agent=gemini-cli${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/lib/ai-memory-hook.ps1 b/hooks/lib/ai-memory-hook.ps1index d3d6aadc..81241f5d 100644--- a/hooks/lib/ai-memory-hook.ps1+++ b/hooks/lib/ai-memory-hook.ps1@@ -36,7 +36,7 @@ function Get-AiMemoryMarkerQuery {param([string] $Cwd)$marker = Get-AiMemoryMarkerToml -Cwd $Cwdif (-not $marker) { return "" }- $qs = ""+ $qs = "&cwd=$([uri]::EscapeDataString($Cwd))"$ws = Get-AiMemoryTomlKey -File $marker -Key "workspace"if ($ws) { $qs += "&workspace=$([uri]::EscapeDataString($ws))" }$proj = Get-AiMemoryTomlKey -File $marker -Key "project"diff --git a/hooks/opencode/post-tool-use.sh b/hooks/opencode/post-tool-use.shindex fc38ff32..958dd71e 100755--- a/hooks/opencode/post-tool-use.sh+++ b/hooks/opencode/post-tool-use.sh@@ -1,20 +1,15 @@#!/bin/sh# opencode post-tool-use 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=post-tool-use&agent=open-code" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=post-tool-use&agent=open-code${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/opencode/pre-compact.sh b/hooks/opencode/pre-compact.shindex a0f73cf4..8683f934 100755--- a/hooks/opencode/pre-compact.sh+++ b/hooks/opencode/pre-compact.sh@@ -1,20 +1,15 @@#!/bin/sh# opencode pre-compact 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=pre-compact&agent=open-code" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=pre-compact&agent=open-code${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/opencode/pre-tool-use.sh b/hooks/opencode/pre-tool-use.shindex f6f14f42..61c782dc 100755--- a/hooks/opencode/pre-tool-use.sh+++ b/hooks/opencode/pre-tool-use.sh@@ -1,20 +1,15 @@#!/bin/sh# opencode pre-tool-use 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=pre-tool-use&agent=open-code" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=pre-tool-use&agent=open-code${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/opencode/session-end.sh b/hooks/opencode/session-end.shindex c72ea310..762725b2 100755--- a/hooks/opencode/session-end.sh+++ b/hooks/opencode/session-end.sh@@ -1,20 +1,15 @@#!/bin/sh# opencode session-end 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=session-end&agent=open-code" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=session-end&agent=open-code${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/opencode/session-start.sh b/hooks/opencode/session-start.shindex 345ad753..cf9af906 100755--- a/hooks/opencode/session-start.sh+++ b/hooks/opencode/session-start.sh@@ -1,39 +1,21 @@#!/bin/sh# opencode 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.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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")-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=open-code" >/dev/null 2>&1 || true-get_handoff "$SERVER/handoff?agent=open-code" 2>/dev/null || true+printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=session-start&agent=open-code${QS}" >/dev/null 2>&1 || true+ai_memory_get_handoff "$SERVER/handoff?agent=open-code${QS}" 2>/dev/null || trueexit 0diff --git a/hooks/opencode/stop.sh b/hooks/opencode/stop.shindex 42e36226..60cb28a2 100755--- a/hooks/opencode/stop.sh+++ b/hooks/opencode/stop.sh@@ -1,20 +1,15 @@#!/bin/sh# opencode stop 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=stop&agent=open-code" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=stop&agent=open-code${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/hooks/opencode/user-prompt-submit.sh b/hooks/opencode/user-prompt-submit.shindex bd1ee137..d3fc6782 100755--- a/hooks/opencode/user-prompt-submit.sh+++ b/hooks/opencode/user-prompt-submit.sh@@ -1,20 +1,15 @@#!/bin/sh# opencode user-prompt 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).+# Forwards the event JSON to the ai-memory server, fire-and-forget.+_lib_dir="$(dirname "$0")"+[ -f "$_lib_dir/_lib.sh" ] || _lib_dir="$_lib_dir/.."+. "$_lib_dir/_lib.sh"+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=user-prompt&agent=open-code" >/dev/null 2>&1 || true+PAYLOAD=$(cat)+CWD=$(ai_memory_extract_cwd "$PAYLOAD")+QS=$(ai_memory_marker_qs "$CWD")++printf '%s' "$PAYLOAD" \+ | ai_memory_post_hook "$SERVER/hook?event=user-prompt&agent=open-code${QS}" >/dev/null 2>&1 || trueexit 0diff --git a/tests/hooks/test_lib.sh b/tests/hooks/test_lib.shindex 7da57142..a6fce42d 100755--- a/tests/hooks/test_lib.sh+++ b/tests/hooks/test_lib.sh@@ -59,11 +59,11 @@ 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"+assert_eq "marker_qs single key" "&cwd=$TMP/a/b/c&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"+assert_eq "closer marker wins" "&cwd=$TMP/a/b/c&workspace=ws1&project=p1" "$QS2"QS3=$(ai_memory_marker_qs "$TMP/nonexistent")assert_eq "no marker -> empty qs" "" "$QS3"