Read the script
# ai-memory hook helper — find marker file + parse minimal TOML.
# Sourced by per-agent lifecycle hook scripts. POSIX shell only —
# no bash-isms, no non-standard deps (no jq, no toml crate). Keep changes
# byte-trivial because every supported agent (claude-code, codex,
# cursor, gemini-cli, kimi-code, kiro-cli, antigravity-cli, opencode,
# omp, pool) sources this same file.
# Set `_amhome` to $HOME without trailing slashes ("/" stays "/"), so the
# string patterns below treat `HOME=/home/u/` as the native walk does.
ai_memory_home() {
_amhome="${HOME:-}"
while [ "${_amhome%/}" != "$_amhome" ] && [ -n "${_amhome%/}" ]; do
_amhome="${_amhome%/}"
done
}
# 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. When cwd is
# outside HOME, stop at the nearest checkout root (`.git` file/dir); a plain
# non-git directory checks only cwd. This keeps unrelated parent markers out.
ai_memory_find_marker() {
dir="$1"
[ -z "$dir" ] && return 0
boundary=""
ai_memory_home
if [ -n "$_amhome" ]; then
case "$dir" in
"$_amhome"|"${_amhome%/}"/*) boundary="$_amhome" ;;
*)
probe="$dir"
while [ -n "$probe" ] && [ "$probe" != "/" ]; do
if [ -e "$probe/.git" ]; then
boundary="$probe"
break
fi
parent=$(dirname "$probe")
[ "$parent" = "$probe" ] && break
probe="$parent"
done
[ -n "$boundary" ] || boundary="$dir"
;;
esac
fi
while [ -n "$dir" ] && [ "$dir" != "/" ]; do
if [ -f "$dir/.ai-memory.toml" ]; then
printf '%s\n' "$dir/.ai-memory.toml"
return 0
fi
if [ -n "$boundary" ] && [ "$dir" = "$boundary" ]; 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_strip_utf8_bom() {
# Byte spelling keeps this portable across GNU/BSD sed and the awk images
# used in CI; LC_ALL=C makes the three bytes ordinary pattern characters.
LC_ALL=C sed "1s/^$(printf '\357\273\277')//"
}
ai_memory_parse_toml_key() {
file="$1"; key="$2"
[ -f "$file" ] || return 0
ai_memory_strip_utf8_bom <"$file" \
| sed -n -E "s/^[[:space:]]*${key}[[:space:]]*=[[:space:]]*\"([^\"]*)\".*/\1/p" \
| head -n 1
}
# Like ai_memory_parse_toml_key but also accepts a BARE value
# (`key = true` / `key = 6000`), so section-style flags such as
# `[briefing] inject_on_session_start = true` work quoted or not.
# Parity with `parse_toml_flag` in hook_capture.rs: line-based (section
# headers are ignored), first match wins, trailing `# comment` stripped.
ai_memory_parse_toml_flag() {
file="$1"; key="$2"
[ -f "$file" ] || return 0
ai_memory_strip_utf8_bom <"$file" \
| sed -n -E "s/^[[:space:]]*${key}[[:space:]]*=[[:space:]]*\"?([^\"#]*)\"?.*/\1/p" \
| head -n 1 | sed 's/[[:space:]]*$//'
}
# Section-scoped counterpart for the same-named [handoff] and [recovery]
# delivery keys. The exclusion mode preserves legacy handoff parsing elsewhere.
ai_memory_parse_toml_scoped_flag() {
file="$1"; section="$2"; key="$3"; exclude="$4"
[ -f "$file" ] || return 0
ai_memory_strip_utf8_bom <"$file" | LC_ALL=C awk \
-v section="$section" -v key="$key" -v exclude="$exclude" '
function trim(value) {
gsub(/^[ \t\r]+|[ \t\r]+$/, "", value)
return value
}
{
header = $0
sub(/#.*/, "", header)
header = trim(header)
if (header ~ /^\[.*\]$/) {
in_section = trim(substr(header, 2, length(header) - 2)) == section
next
}
if ((exclude == "1" && in_section) || (exclude != "1" && !in_section)) next
pattern = "^[ \t]*" key "[ \t]*="
if ($0 !~ pattern) next
value = $0
sub(pattern, "", value)
sub(/#.*/, "", value)
value = trim(value)
gsub(/^"|"$/, "", value)
if (value != "") {
print value
exit
}
}'
}
ai_memory_parse_toml_section_flag() {
ai_memory_parse_toml_scoped_flag "$1" "$2" "$3" 0
}
ai_memory_parse_toml_flag_outside_section() {
ai_memory_parse_toml_scoped_flag "$1" "$2" "$3" 1
}
# A resolved marker's `[profile]` flag as the explicit value the hook sends:
# 0 for a falsy value (0/false/no/off, any case), 1 for anything else, an
# absent key included. Parity with `profile_flag_value` in hook_capture.rs.
ai_memory_profile_flag() {
case "$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')" in
0 | false | no | off) printf '0' ;;
*) printf '1' ;;
esac
}
# Whether "$1" (a marker file) declares anything beyond a `[capture]`
# section: any root-level scope key (workspace/project/project_strategy), or
# any of the other settings ai_memory_marker_qs / ai_memory_briefing_qs
# forward (drop_subagent_captures, default_global, [briefing], [profile],
# [handoff] and [messaging] keys). Mirrors
# `declares_more_than_capture` in marker.rs. A marker with any of these is a
# resolution boundary; only a marker whose only content is `[capture]` (e.g.
# ignore_paths) is scope/settings-transparent (#668).
ai_memory_marker_declares_settings() {
file="$1"
[ -f "$file" ] || return 1
for key in workspace project proje