create-ai-memory 0.12.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +403 -3
- package/bin/ai-mem-mcp.js +274 -0
- package/bin/ai-mem-serve.js +240 -0
- package/package.json +11 -7
- package/shell/adapters.zsh +25 -12
- package/shell/ai-mem.zsh +238 -93
- package/vault-template/_session_logs/_session_template.md +1 -0
- package/web/viewer.html +666 -0
package/shell/ai-mem.zsh
CHANGED
|
@@ -1,8 +1,22 @@
|
|
|
1
1
|
# === AI CLI + Obsidian memory ===
|
|
2
2
|
# Portable, agent-agnostic session memory. Source this from ~/.zshrc after
|
|
3
3
|
# exporting AI_MEM_ROOT to point at your vault. Zsh-only (uses print -r, ${(s)},
|
|
4
|
-
# and associative arrays). Add a new agent by defining
|
|
4
|
+
# and associative arrays). Add a new agent by defining __ai_adapter_<name> in adapters.zsh and
|
|
5
5
|
# listing it in AI_MEM_AGENTS; a matching <name>-start function is generated.
|
|
6
|
+
#
|
|
7
|
+
# Private helpers are named __ai_* with TWO leading underscores, deliberately.
|
|
8
|
+
# Claude Code snapshots the interactive shell and replays that snapshot for
|
|
9
|
+
# every agent-run command, and its snapshot drops every function whose name
|
|
10
|
+
# starts with a single underscore -- the filter is aimed at zsh's ~1000
|
|
11
|
+
# completion functions (_git, _ssh, ...), and single-underscore private
|
|
12
|
+
# helpers are collateral. Measured on one real shell: 1018 single-underscore
|
|
13
|
+
# functions defined, 0 survived; all 9 double-underscore ones did.
|
|
14
|
+
#
|
|
15
|
+
# The failure that causes is silent, not loud. A public function survives,
|
|
16
|
+
# calls a helper that no longer exists, and carries on: ai-mem-vault-backup
|
|
17
|
+
# printed "nothing to commit" and exited 0 while backing up nothing, which
|
|
18
|
+
# an agent then reports as success. Do not "tidy" these back to one
|
|
19
|
+
# underscore -- tests/run.sh asserts the naming for exactly this reason.
|
|
6
20
|
|
|
7
21
|
# Directory holding this module, used to source sibling files.
|
|
8
22
|
AI_MEM_HOME="${0:A:h}"
|
|
@@ -19,11 +33,11 @@ AI_MEM_TEMPLATE_SRC="${AI_MEM_HOME:h}/vault-template"
|
|
|
19
33
|
# uses whatever behaviour was current whenever this terminal was opened,
|
|
20
34
|
# which can be months. Comparing the two makes the staleness visible.
|
|
21
35
|
# cksum is POSIX, so this needs no macOS/Linux branch.
|
|
22
|
-
|
|
36
|
+
__ai_mem_fingerprint() {
|
|
23
37
|
cksum "$AI_MEM_HOME/ai-mem.zsh" "$AI_MEM_HOME/adapters.zsh" 2>/dev/null \
|
|
24
38
|
| awk '{ printf "%s-", $1 }'
|
|
25
39
|
}
|
|
26
|
-
export AI_MEM_SOURCED_FINGERPRINT="$(
|
|
40
|
+
export AI_MEM_SOURCED_FINGERPRINT="$(__ai_mem_fingerprint)"
|
|
27
41
|
# Vault root. Override in ~/.zshrc; defaults to a hidden dir under $HOME.
|
|
28
42
|
: "${AI_MEM_ROOT:=$HOME/.ai-memory/_Ai_Memory}"
|
|
29
43
|
if [[ -d "$AI_MEM_ROOT" ]]; then
|
|
@@ -35,12 +49,17 @@ export AI_MEM_STANDARDS="$AI_MEM_ROOT/_Standards.md"
|
|
|
35
49
|
export AI_MEM_PROJECT_DIR="$AI_MEM_ROOT/_projects"
|
|
36
50
|
export AI_MEM_SESSION_DIR="$AI_MEM_ROOT/_session_logs"
|
|
37
51
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
52
|
+
# `note_path`, not `path`: in zsh `path` is the special array tied to $PATH,
|
|
53
|
+
# so `local path=...` inside a function replaces command lookup with the
|
|
54
|
+
# string being passed in and every external command becomes "command not
|
|
55
|
+
# found". This function only uses builtins, so it survived the mistake -- but
|
|
56
|
+
# the next editor to add a `grep` here would not.
|
|
57
|
+
__ai_mem_guard() {
|
|
58
|
+
local note_path="${1:-}"
|
|
59
|
+
case "$note_path" in
|
|
41
60
|
"$AI_MEM_ROOT"|"$AI_MEM_ROOT"/*) return 0 ;;
|
|
42
61
|
*)
|
|
43
|
-
echo "Refusing to touch non-memory path: $
|
|
62
|
+
echo "Refusing to touch non-memory path: $note_path"
|
|
44
63
|
return 1
|
|
45
64
|
;;
|
|
46
65
|
esac
|
|
@@ -53,7 +72,7 @@ _ai_mem_guard() {
|
|
|
53
72
|
# outside this sourced shell, so it can't call this function directly).
|
|
54
73
|
# No-ops silently if AI_MEM_ROOT isn't its own git repo; never fails the
|
|
55
74
|
# caller on a push error (no network, no remote configured, etc).
|
|
56
|
-
|
|
75
|
+
__ai_mem_vault_backup() {
|
|
57
76
|
local vault_git_root
|
|
58
77
|
vault_git_root="$(git -C "${AI_MEM_ROOT:-}" rev-parse --show-toplevel 2>/dev/null || true)"
|
|
59
78
|
[[ -n "$vault_git_root" ]] || return 0
|
|
@@ -73,7 +92,6 @@ _ai_mem_vault_backup() {
|
|
|
73
92
|
rmdir "$lock_dir" 2>/dev/null
|
|
74
93
|
fi
|
|
75
94
|
return 0
|
|
76
|
-
return 0
|
|
77
95
|
}
|
|
78
96
|
|
|
79
97
|
# Public entry point for anything that writes to the vault outside
|
|
@@ -81,9 +99,19 @@ _ai_mem_vault_backup() {
|
|
|
81
99
|
# Session Outcome section directly and has no other reason to know this
|
|
82
100
|
# function exists.
|
|
83
101
|
ai-mem-vault-backup() {
|
|
102
|
+
# Fail loudly if the helper is absent. Without this the function still
|
|
103
|
+
# "works": HEAD does not move, so it prints "nothing to commit" and exits
|
|
104
|
+
# 0, and the agent reading that reports a successful backup of a vault
|
|
105
|
+
# nothing was written to. A wrong answer that looks like a right one is
|
|
106
|
+
# the worst outcome for a memory tool, so refuse instead of guessing.
|
|
107
|
+
if (( ! $+functions[__ai_mem_vault_backup] )); then
|
|
108
|
+
print -r -- "ai-mem-vault-backup: helper __ai_mem_vault_backup is not defined -- this shell's function table is incomplete (stale or filtered snapshot). Re-run in a fresh terminal, or commit and push \$AI_MEM_ROOT by hand." >&2
|
|
109
|
+
return 1
|
|
110
|
+
fi
|
|
111
|
+
|
|
84
112
|
local before after
|
|
85
113
|
before="$(git -C "${AI_MEM_ROOT:-}" rev-parse HEAD 2>/dev/null || true)"
|
|
86
|
-
|
|
114
|
+
__ai_mem_vault_backup
|
|
87
115
|
after="$(git -C "${AI_MEM_ROOT:-}" rev-parse HEAD 2>/dev/null || true)"
|
|
88
116
|
if [[ -z "$before" && -z "$after" ]]; then
|
|
89
117
|
echo "ai-mem-vault-backup: vault isn't git-backed, nothing to do"
|
|
@@ -94,17 +122,62 @@ ai-mem-vault-backup() {
|
|
|
94
122
|
fi
|
|
95
123
|
}
|
|
96
124
|
# Read a vault note only when it exists inside the memory root.
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
125
|
+
#
|
|
126
|
+
# A note may declare `mirror_of: <other-note>` in its frontmatter, as the
|
|
127
|
+
# shipped _Standards.md does for _Global_Profile.md. The mirror exists so the
|
|
128
|
+
# shared rules stay visible wherever only one of the two gets read -- but
|
|
129
|
+
# injecting BOTH in full into the SAME prompt restates text the model just
|
|
130
|
+
# finished reading. Measured on a real vault: 72 of the mirror's 80 unique
|
|
131
|
+
# lines were already verbatim in its source, ~2900 of the launch prompt's
|
|
132
|
+
# ~7200 tokens spent saying the same thing twice.
|
|
133
|
+
#
|
|
134
|
+
# So a mirror contributes only the lines that actually differ. This fails
|
|
135
|
+
# OPEN in every uncertain case -- no frontmatter, an unreadable or missing
|
|
136
|
+
# target, a target outside the vault, or a diff that would leave nothing --
|
|
137
|
+
# because injecting a note twice merely costs tokens, while dropping one
|
|
138
|
+
# costs the agent context it was promised.
|
|
139
|
+
__ai_mem_note_contents() {
|
|
140
|
+
local note_path="${1:-}"
|
|
141
|
+
if [[ -z "$note_path" || ! -f "$note_path" ]]; then
|
|
142
|
+
return 0
|
|
143
|
+
fi
|
|
144
|
+
|
|
145
|
+
__ai_mem_guard "$note_path" || return 1
|
|
146
|
+
|
|
147
|
+
# Read mirror_of from the frontmatter block only (lines 2..next `---`), so
|
|
148
|
+
# the word appearing in a note's prose can never be mistaken for a claim.
|
|
149
|
+
local mirror
|
|
150
|
+
mirror="$(sed -n '2,/^---$/p' "$note_path" 2>/dev/null | sed -n 's/^mirror_of:[[:space:]]*//p' | head -1)"
|
|
151
|
+
if [[ -z "$mirror" ]]; then
|
|
152
|
+
print -r -- "$(<"$note_path")"
|
|
153
|
+
return 0
|
|
154
|
+
fi
|
|
155
|
+
|
|
156
|
+
# A mirror names a sibling note, never a path. __ai_mem_guard matches on
|
|
157
|
+
# the string, so "$AI_MEM_ROOT/../../etc/passwd" would satisfy it -- and
|
|
158
|
+
# this value comes out of a file's frontmatter, so accepting a path here
|
|
159
|
+
# would turn any note into a request to read an arbitrary file. Rejecting
|
|
160
|
+
# every separator outright is both the safer rule and the simpler one.
|
|
161
|
+
local target="$AI_MEM_ROOT/$mirror"
|
|
162
|
+
if [[ "$mirror" == */* || ! -f "$target" ]]; then
|
|
163
|
+
print -r -- "$(<"$note_path")"
|
|
164
|
+
return 0
|
|
165
|
+
fi
|
|
166
|
+
|
|
167
|
+
# -x -F: drop only lines that are byte-for-byte identical to one in the
|
|
168
|
+
# source. Anything reworded stays, because a near-match is a real edit.
|
|
169
|
+
local unique
|
|
170
|
+
unique="$(grep -vxF -f "$target" -- "$note_path" 2>/dev/null)"
|
|
171
|
+
if [[ -z "${unique//[[:space:]]/}" ]]; then
|
|
172
|
+
print -r -- "(identical to ${mirror}; nothing further)"
|
|
100
173
|
return 0
|
|
101
174
|
fi
|
|
102
175
|
|
|
103
|
-
|
|
104
|
-
print -r -- "$
|
|
176
|
+
print -r -- "(mirror of ${mirror}, shown above -- only the lines that differ from it are repeated here)"
|
|
177
|
+
print -r -- "$unique"
|
|
105
178
|
}
|
|
106
179
|
|
|
107
|
-
|
|
180
|
+
__ai_mem_resolve_project() {
|
|
108
181
|
# Prefer the git repo we are actually standing in. Otherwise `cd`-ing between
|
|
109
182
|
# projects in one shell keeps a stale AI_MEM_ACTIVE_PROJECT pinned, so a later
|
|
110
183
|
# claude-start writes its log under the wrong project's folder.
|
|
@@ -124,16 +197,16 @@ _ai_mem_resolve_project() {
|
|
|
124
197
|
basename "$PWD"
|
|
125
198
|
}
|
|
126
199
|
|
|
127
|
-
|
|
200
|
+
__ai_mem_project_session_dir() {
|
|
128
201
|
local project_name="${1:-}"
|
|
129
202
|
if [[ -z "$project_name" ]]; then
|
|
130
|
-
project_name="$(
|
|
203
|
+
project_name="$(__ai_mem_resolve_project)"
|
|
131
204
|
fi
|
|
132
205
|
|
|
133
206
|
print -r -- "$AI_MEM_SESSION_DIR/$project_name"
|
|
134
207
|
}
|
|
135
208
|
|
|
136
|
-
|
|
209
|
+
__ai_mem_graphify_repo_root() {
|
|
137
210
|
local git_root=""
|
|
138
211
|
git_root="$(git rev-parse --show-toplevel 2>/dev/null)" || true
|
|
139
212
|
if [[ -n "$git_root" ]]; then
|
|
@@ -143,8 +216,8 @@ _ai_mem_graphify_repo_root() {
|
|
|
143
216
|
fi
|
|
144
217
|
}
|
|
145
218
|
|
|
146
|
-
|
|
147
|
-
local repo_root="$(
|
|
219
|
+
__ai_mem_graphify_context() {
|
|
220
|
+
local repo_root="$(__ai_mem_graphify_repo_root)"
|
|
148
221
|
local graph_root="$repo_root/graphify-out"
|
|
149
222
|
local graph_json="$graph_root/graph.json"
|
|
150
223
|
local graph_report="$graph_root/GRAPH_REPORT.md"
|
|
@@ -170,22 +243,22 @@ _ai_mem_graphify_context() {
|
|
|
170
243
|
|
|
171
244
|
# Returns the newest saved session log for the current project.
|
|
172
245
|
# The active run gets a fresh log, so this only feeds carryover context.
|
|
173
|
-
|
|
246
|
+
__ai_mem_latest_session_log() {
|
|
174
247
|
local project_name="${1:-}"
|
|
175
248
|
if [[ -z "$project_name" ]]; then
|
|
176
|
-
project_name="$(
|
|
249
|
+
project_name="$(__ai_mem_resolve_project)"
|
|
177
250
|
fi
|
|
178
251
|
|
|
179
252
|
local latest=""
|
|
180
253
|
local project_session_dir
|
|
181
|
-
project_session_dir="$(
|
|
254
|
+
project_session_dir="$(__ai_mem_project_session_dir "$project_name")"
|
|
182
255
|
|
|
183
256
|
latest="$(find "$project_session_dir" -maxdepth 1 -type f -name "${project_name}-*.md" 2>/dev/null | sort | tail -n 1)" || true
|
|
184
257
|
if [[ -z "$latest" ]]; then
|
|
185
258
|
latest="$(find "$AI_MEM_SESSION_DIR" -maxdepth 1 -type f -name "${project_name}-*.md" 2>/dev/null | sort | tail -n 1)" || true
|
|
186
259
|
fi
|
|
187
260
|
if [[ -n "$latest" ]]; then
|
|
188
|
-
|
|
261
|
+
__ai_mem_guard "$latest" || return 1
|
|
189
262
|
print -r -- "$latest"
|
|
190
263
|
fi
|
|
191
264
|
}
|
|
@@ -193,7 +266,7 @@ _ai_mem_latest_session_log() {
|
|
|
193
266
|
# Copy the shipped profile, standards, and templates into the vault on first use.
|
|
194
267
|
# Idempotent and additive: it never overwrites a file the user already has, so a
|
|
195
268
|
# plugin-manager install (source only, no install.sh) still gets a working vault.
|
|
196
|
-
|
|
269
|
+
__ai_mem_ensure_vault() {
|
|
197
270
|
[[ -d "$AI_MEM_TEMPLATE_SRC" ]] || return 0
|
|
198
271
|
mkdir -p "$AI_MEM_PROJECT_DIR" "$AI_MEM_SESSION_DIR" "$AI_MEM_ROOT/_lessons"
|
|
199
272
|
local rel
|
|
@@ -202,29 +275,29 @@ _ai_mem_ensure_vault() {
|
|
|
202
275
|
_lessons/_lesson_template.md; do
|
|
203
276
|
local src="$AI_MEM_TEMPLATE_SRC/$rel" dst="$AI_MEM_ROOT/$rel"
|
|
204
277
|
if [[ -f "$src" && ! -f "$dst" ]]; then
|
|
205
|
-
|
|
278
|
+
__ai_mem_guard "$dst" || return 1
|
|
206
279
|
cp "$src" "$dst"
|
|
207
280
|
fi
|
|
208
281
|
done
|
|
209
282
|
}
|
|
210
283
|
|
|
211
|
-
|
|
284
|
+
__ai_mem_prepare_session() {
|
|
212
285
|
local project_name="${1:-}"
|
|
213
286
|
if [[ -z "$project_name" ]]; then
|
|
214
|
-
project_name="$(
|
|
287
|
+
project_name="$(__ai_mem_resolve_project)"
|
|
215
288
|
fi
|
|
216
289
|
|
|
217
|
-
|
|
290
|
+
__ai_mem_ensure_vault || return 1
|
|
218
291
|
|
|
219
292
|
local project_note="$AI_MEM_PROJECT_DIR/${project_name}.md"
|
|
220
293
|
local project_session_dir
|
|
221
|
-
project_session_dir="$(
|
|
294
|
+
project_session_dir="$(__ai_mem_project_session_dir "$project_name")"
|
|
222
295
|
local previous_session_note=""
|
|
223
|
-
previous_session_note="$(
|
|
296
|
+
previous_session_note="$(__ai_mem_latest_session_log "$project_name")" || return 1
|
|
224
297
|
local session_note="$project_session_dir/${project_name}-$(date +%Y-%m-%d_%H-%M-%S).md"
|
|
225
298
|
|
|
226
|
-
|
|
227
|
-
|
|
299
|
+
__ai_mem_guard "$project_note" || return 1
|
|
300
|
+
__ai_mem_guard "$session_note" || return 1
|
|
228
301
|
|
|
229
302
|
mkdir -p "$AI_MEM_PROJECT_DIR" "$AI_MEM_SESSION_DIR" "$project_session_dir"
|
|
230
303
|
|
|
@@ -251,7 +324,7 @@ _ai_mem_prepare_session() {
|
|
|
251
324
|
# Pull one "* **Label:** value" bullet out of a session log's fixed
|
|
252
325
|
# "# Session Outcome" section. Reads the log directly — no separate file is
|
|
253
326
|
# ever written, so there is nothing extra sitting in the vault.
|
|
254
|
-
|
|
327
|
+
__ai_mem_session_field() {
|
|
255
328
|
local file="$1" label="$2" value
|
|
256
329
|
[[ -f "$file" ]] || return 0
|
|
257
330
|
value="$(grep -m1 -E "^\* \*\*${label}:\*\*" "$file" | sed -E "s|^\* \*\*${label}:\*\*[[:space:]]*||")"
|
|
@@ -263,7 +336,7 @@ _ai_mem_session_field() {
|
|
|
263
336
|
|
|
264
337
|
# Defensive cap so one unusually long freeform bullet can't blow up the
|
|
265
338
|
# prompt. Never triggers for the template's intended single-line bullets.
|
|
266
|
-
|
|
339
|
+
__ai_mem_cap_field() {
|
|
267
340
|
local text="$1" cap="${2:-500}" source_ref="$3"
|
|
268
341
|
if (( ${#text} > cap )); then
|
|
269
342
|
print -r -- "${text[1,$cap]}… (truncated, ${#text} chars total — see $source_ref)"
|
|
@@ -272,7 +345,44 @@ _ai_mem_cap_field() {
|
|
|
272
345
|
fi
|
|
273
346
|
}
|
|
274
347
|
|
|
275
|
-
|
|
348
|
+
# An agent cannot search for knowledge it does not know exists, and the
|
|
349
|
+
# lesson bodies are far too large to inject -- but a slug like
|
|
350
|
+
# prisma-connection-pool-exhaustion is already the lesson compressed to a
|
|
351
|
+
# handful of tokens. Listing the names turns a blind guess into a precise
|
|
352
|
+
# ai-mem-search, at roughly 4 tokens per lesson. Recognition is the point;
|
|
353
|
+
# recall is what the agent is bad at.
|
|
354
|
+
#
|
|
355
|
+
# Names only, never bodies. The cap exists because this stops paying for
|
|
356
|
+
# itself once most entries are irrelevant to the session at hand: a long
|
|
357
|
+
# tail of near-miss titles is the most damaging kind of distractor for a
|
|
358
|
+
# model, which is the same reason ai-mem-search bounds its own output.
|
|
359
|
+
# Newest first, so the cap drops the stalest rather than the alphabetically
|
|
360
|
+
# unlucky.
|
|
361
|
+
__ai_mem_lesson_index() {
|
|
362
|
+
local dir="$AI_MEM_ROOT/_lessons"
|
|
363
|
+
[[ -d "$dir" ]] || return 0
|
|
364
|
+
|
|
365
|
+
# (N) tolerate an empty dir, (.) files only, (om) newest mtime first,
|
|
366
|
+
# (:t:r) basename without the extension. Then drop _lesson_template.
|
|
367
|
+
local -a slugs
|
|
368
|
+
slugs=("$dir"/*.md(N.om:t:r))
|
|
369
|
+
slugs=(${slugs:#_*})
|
|
370
|
+
(( $#slugs )) || return 0
|
|
371
|
+
|
|
372
|
+
local limit="${AI_MEM_LESSON_INDEX_LIMIT:-200}"
|
|
373
|
+
local total=$#slugs extra=0
|
|
374
|
+
if (( total > limit )); then
|
|
375
|
+
extra=$(( total - limit ))
|
|
376
|
+
slugs=(${slugs[1,$limit]})
|
|
377
|
+
fi
|
|
378
|
+
|
|
379
|
+
print -r -- "- Lessons already recorded ($total), listed by topic slug only -- the bodies are NOT included here. If one looks relevant to the task at hand, read it with \`ai-mem-search <slug>\` before solving the problem again from scratch."
|
|
380
|
+
print -r -- " ${(j:, :)slugs}"
|
|
381
|
+
(( extra )) && print -r -- " (+$extra older, not listed; \`ls \$AI_MEM_ROOT/_lessons\` for the rest)"
|
|
382
|
+
return 0
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
__ai_mem_context_prompt() {
|
|
276
386
|
local project_note="${1:-}"
|
|
277
387
|
local previous_session_note="${2:-}"
|
|
278
388
|
local session_note="${3:-}"
|
|
@@ -283,10 +393,10 @@ _ai_mem_context_prompt() {
|
|
|
283
393
|
previous_session_label="$previous_session_note"
|
|
284
394
|
|
|
285
395
|
local summary decisions blockers next
|
|
286
|
-
summary="$(
|
|
287
|
-
decisions="$(
|
|
288
|
-
blockers="$(
|
|
289
|
-
next="$(
|
|
396
|
+
summary="$(__ai_mem_session_field "$previous_session_note" 'High-Level Summary')"
|
|
397
|
+
decisions="$(__ai_mem_session_field "$previous_session_note" 'Important Decisions')"
|
|
398
|
+
blockers="$(__ai_mem_session_field "$previous_session_note" 'Constraints / Blockers')"
|
|
399
|
+
next="$(__ai_mem_session_field "$previous_session_note" 'Next Step')"
|
|
290
400
|
|
|
291
401
|
if [[ -z "${AI_MEM_NO_DIGEST:-}" && ( -n "$summary" || -n "$decisions" || -n "$blockers" || -n "$next" ) ]]; then
|
|
292
402
|
[[ -n "$summary" ]] || summary="—"
|
|
@@ -295,10 +405,10 @@ _ai_mem_context_prompt() {
|
|
|
295
405
|
[[ -n "$next" ]] || next="—"
|
|
296
406
|
|
|
297
407
|
previous_session_block="- Latest prior session ($previous_session_note), summarized below:"
|
|
298
|
-
previous_session_block+=$'\n'"* **Summary:** $(
|
|
299
|
-
previous_session_block+=$'\n'"* **Decisions:** $(
|
|
300
|
-
previous_session_block+=$'\n'"* **Blockers:** $(
|
|
301
|
-
previous_session_block+=$'\n'"* **Next:** $(
|
|
408
|
+
previous_session_block+=$'\n'"* **Summary:** $(__ai_mem_cap_field "$summary" 500 "$previous_session_note")"
|
|
409
|
+
previous_session_block+=$'\n'"* **Decisions:** $(__ai_mem_cap_field "$decisions" 500 "$previous_session_note")"
|
|
410
|
+
previous_session_block+=$'\n'"* **Blockers:** $(__ai_mem_cap_field "$blockers" 500 "$previous_session_note")"
|
|
411
|
+
previous_session_block+=$'\n'"* **Next:** $(__ai_mem_cap_field "$next" 500 "$previous_session_note")"
|
|
302
412
|
else
|
|
303
413
|
previous_session_block="- Latest prior session log: $previous_session_label"
|
|
304
414
|
previous_session_block+=$'\n'
|
|
@@ -309,25 +419,27 @@ _ai_mem_context_prompt() {
|
|
|
309
419
|
cat <<EOF
|
|
310
420
|
Read these notes before doing anything else:
|
|
311
421
|
- Global profile:
|
|
312
|
-
$(
|
|
422
|
+
$(__ai_mem_note_contents "$AI_MEM_GLOBAL")
|
|
313
423
|
|
|
314
424
|
- Standards:
|
|
315
|
-
$(
|
|
425
|
+
$(__ai_mem_note_contents "$AI_MEM_STANDARDS")
|
|
316
426
|
|
|
317
427
|
- Project context: $project_note
|
|
318
428
|
$previous_session_block
|
|
319
429
|
- Active session log: $session_note
|
|
430
|
+
$(__ai_mem_lesson_index)
|
|
320
431
|
|
|
321
432
|
Use the Obsidian vault as the persistent memory layer.
|
|
322
433
|
Treat the global profile and standards note as the shared baseline for every run.
|
|
323
434
|
Keep durable preferences and project facts in the vault, and keep the active session log updated with decisions, blockers, and next steps.
|
|
324
435
|
For anything not covered above -- a decision from further back, a different project, a health check on the vault's links -- run \`ai-mem-search <term> [project]\` or \`ai-mem-lint\` yourself; both are plain shell commands already on PATH. Search is case-insensitive and matches literally, and its output is capped: if it reports results hidden, narrow with a project argument or a more specific term rather than assuming you have seen everything. Start broad and narrow from there -- a first query that is too specific is the usual way to miss what you were looking for.
|
|
436
|
+
If the user asks to see, open or browse their memory rather than search it, run \`ai-mem-serve\` -- it opens the vault as a graph in their browser, and is safe to run again if it is already up.
|
|
325
437
|
When you hit a mistake, decision, or solution worth remembering across projects (not just this one), run \`ai-lesson <topic-slug> <problem> <solution>\` -- e.g. \`ai-lesson rate-limiting "a fixed-window limiter kept losing bursts of legitimate traffic" "switched to a token bucket, which absorbs bursts correctly"\`. It appends a dated Problem/Solution entry to a cross-project note at _lessons/<topic-slug>.md; ai-mem-search already covers it.
|
|
326
438
|
EOF
|
|
327
439
|
}
|
|
328
440
|
|
|
329
441
|
# Mark the current shell as having loaded AI vault context for the active repo.
|
|
330
|
-
|
|
442
|
+
__ai_mem_mark_commit_ready() {
|
|
331
443
|
local project_name="${1:-}"
|
|
332
444
|
local source="${2:-ai-context}"
|
|
333
445
|
local ready_dir="$AI_MEM_SESSION_DIR/.context-ready"
|
|
@@ -335,11 +447,11 @@ _ai_mem_mark_commit_ready() {
|
|
|
335
447
|
local token
|
|
336
448
|
|
|
337
449
|
if [[ -z "$project_name" ]]; then
|
|
338
|
-
project_name="$(
|
|
450
|
+
project_name="$(__ai_mem_resolve_project)"
|
|
339
451
|
fi
|
|
340
452
|
|
|
341
453
|
ready_file="$ready_dir/${project_name}.token"
|
|
342
|
-
|
|
454
|
+
__ai_mem_guard "$ready_file" || return 1
|
|
343
455
|
|
|
344
456
|
mkdir -p "$ready_dir"
|
|
345
457
|
|
|
@@ -358,11 +470,11 @@ _ai_mem_mark_commit_ready() {
|
|
|
358
470
|
export AI_MEM_CONTEXT_PROJECT="$project_name"
|
|
359
471
|
}
|
|
360
472
|
|
|
361
|
-
# Re-export the active-session vars in the CALLER's shell.
|
|
473
|
+
# Re-export the active-session vars in the CALLER's shell. __ai_mem_prepare_session
|
|
362
474
|
# exports them too, but it is always invoked inside $(...) command substitution, so
|
|
363
475
|
# those exports die in the subshell and never reach the launched client or its hooks
|
|
364
476
|
# (Claude's SessionStart/Stop hooks gate on AI_MEM_ACTIVE_SESSION_LOG).
|
|
365
|
-
|
|
477
|
+
__ai_mem_export_active() {
|
|
366
478
|
export AI_MEM_ACTIVE_PROJECT="${1:-}"
|
|
367
479
|
export AI_MEM_PREVIOUS_SESSION_LOG="${2:-}"
|
|
368
480
|
export AI_MEM_ACTIVE_SESSION_LOG="${3:-}"
|
|
@@ -373,11 +485,11 @@ ai-start() {
|
|
|
373
485
|
shift || true
|
|
374
486
|
|
|
375
487
|
local resolved
|
|
376
|
-
resolved="$(
|
|
488
|
+
resolved="$(__ai_mem_prepare_session "$project_name")" || return 1
|
|
377
489
|
|
|
378
490
|
local active_project project_note previous_session_note session_note
|
|
379
491
|
IFS='|' read -r active_project project_note previous_session_note session_note <<< "$resolved"
|
|
380
|
-
|
|
492
|
+
__ai_mem_export_active "$active_project" "$previous_session_note" "$session_note"
|
|
381
493
|
|
|
382
494
|
local previous_session_label="(none yet)"
|
|
383
495
|
if [[ -n "$previous_session_note" ]]; then
|
|
@@ -397,18 +509,18 @@ EOF
|
|
|
397
509
|
ai-context() {
|
|
398
510
|
local project_name="${1:-}"
|
|
399
511
|
local resolved project_note previous_session_note session_note
|
|
400
|
-
resolved="$(
|
|
512
|
+
resolved="$(__ai_mem_prepare_session "$project_name")" || return 1
|
|
401
513
|
|
|
402
514
|
local active_project
|
|
403
515
|
IFS='|' read -r active_project project_note previous_session_note session_note <<< "$resolved"
|
|
404
|
-
|
|
516
|
+
__ai_mem_export_active "$active_project" "$previous_session_note" "$session_note"
|
|
405
517
|
|
|
406
|
-
|
|
407
|
-
|
|
518
|
+
__ai_mem_mark_commit_ready "$active_project" "ai-context" || return 1
|
|
519
|
+
__ai_mem_context_prompt "$project_note" "$previous_session_note" "$session_note"
|
|
408
520
|
}
|
|
409
521
|
|
|
410
522
|
# Yes/no helper used by the session-mode prompts. Empty answer counts as no.
|
|
411
|
-
|
|
523
|
+
__ai_yesno() {
|
|
412
524
|
local prompt="$1" reply
|
|
413
525
|
while true; do
|
|
414
526
|
read -r "reply?${prompt} [y/N] "
|
|
@@ -436,18 +548,18 @@ typeset -ga AI_MEM_SKILL_ORDER
|
|
|
436
548
|
|
|
437
549
|
# Ask which optional skills to enable. Each is independent; answer y/n per skill.
|
|
438
550
|
# Echoes a pipe-joined list of chosen keys; empty means a plain session.
|
|
439
|
-
|
|
551
|
+
__ai_session_modes_pick() {
|
|
440
552
|
local modes=() key prompt
|
|
441
553
|
for key in "${AI_MEM_SKILL_ORDER[@]}"; do
|
|
442
554
|
prompt="${AI_MEM_SKILLS[$key]%%::*}"
|
|
443
555
|
[[ -n "$prompt" ]] || continue
|
|
444
|
-
|
|
556
|
+
__ai_yesno "$prompt" && modes+=("$key")
|
|
445
557
|
done
|
|
446
558
|
print -r -- "${(j:|:)modes}"
|
|
447
559
|
}
|
|
448
560
|
|
|
449
561
|
# Build the instruction block to inject from the chosen skill keys.
|
|
450
|
-
|
|
562
|
+
__ai_session_modes_instructions() {
|
|
451
563
|
local modes="$1" block="" m text
|
|
452
564
|
for m in ${(s:|:)modes}; do
|
|
453
565
|
text="${AI_MEM_SKILLS[$m]#*::}"
|
|
@@ -457,7 +569,7 @@ _ai_session_modes_instructions() {
|
|
|
457
569
|
}
|
|
458
570
|
|
|
459
571
|
# Start an AI client with the shared memory block and the chosen session mode.
|
|
460
|
-
|
|
572
|
+
__ai_session_start() {
|
|
461
573
|
local launcher="${1:-}"
|
|
462
574
|
if (( $# > 0 )); then
|
|
463
575
|
shift
|
|
@@ -467,7 +579,7 @@ _ai_session_start() {
|
|
|
467
579
|
# below would otherwise succeed quietly using the old behaviour, and the
|
|
468
580
|
# only symptom is a fix that appears not to have worked.
|
|
469
581
|
local _fp_now
|
|
470
|
-
_fp_now="$(
|
|
582
|
+
_fp_now="$(__ai_mem_fingerprint)"
|
|
471
583
|
if [[ -n "$AI_MEM_SOURCED_FINGERPRINT" && -n "$_fp_now" \
|
|
472
584
|
&& "$_fp_now" != "$AI_MEM_SOURCED_FINGERPRINT" ]]; then
|
|
473
585
|
echo "ai-memory: this shell loaded an older copy of ai-mem; the files on disk have changed since." >&2
|
|
@@ -484,15 +596,15 @@ _ai_session_start() {
|
|
|
484
596
|
fi
|
|
485
597
|
|
|
486
598
|
local resolved
|
|
487
|
-
resolved="$(
|
|
599
|
+
resolved="$(__ai_mem_prepare_session)" || return 1
|
|
488
600
|
|
|
489
601
|
local active_project project_note previous_session_note session_note
|
|
490
602
|
IFS='|' read -r active_project project_note previous_session_note session_note <<< "$resolved"
|
|
491
|
-
|
|
603
|
+
__ai_mem_export_active "$active_project" "$previous_session_note" "$session_note"
|
|
492
604
|
|
|
493
605
|
local session_modes mode_block
|
|
494
|
-
session_modes="$(
|
|
495
|
-
mode_block="$(
|
|
606
|
+
session_modes="$(__ai_session_modes_pick)" || return 1
|
|
607
|
+
mode_block="$(__ai_session_modes_instructions "$session_modes")"
|
|
496
608
|
|
|
497
609
|
# AI_SESSION_MODES carries the chosen skill keys; AI_SESSION_STYLE_LABEL
|
|
498
610
|
# carries the assembled instruction block for launchers to inject.
|
|
@@ -500,10 +612,10 @@ _ai_session_start() {
|
|
|
500
612
|
export AI_SESSION_STYLE_LABEL="$mode_block"
|
|
501
613
|
|
|
502
614
|
local memory_prompt
|
|
503
|
-
memory_prompt="$(
|
|
615
|
+
memory_prompt="$(__ai_mem_context_prompt "$project_note" "$previous_session_note" "$session_note")"
|
|
504
616
|
|
|
505
617
|
local graphify_context=""
|
|
506
|
-
graphify_context="$(
|
|
618
|
+
graphify_context="$(__ai_mem_graphify_context)" || return 1
|
|
507
619
|
if [[ -n "$graphify_context" ]]; then
|
|
508
620
|
memory_prompt+=$'\n\n'
|
|
509
621
|
memory_prompt+="$graphify_context"
|
|
@@ -518,51 +630,51 @@ _ai_session_start() {
|
|
|
518
630
|
memory_prompt+="$session_prompt"
|
|
519
631
|
fi
|
|
520
632
|
|
|
521
|
-
|
|
633
|
+
__ai_mem_mark_commit_ready "$active_project" "$launcher-start" || return 1
|
|
522
634
|
|
|
523
635
|
# Dispatch to the agent adapter. Each adapter is passed the assembled
|
|
524
636
|
# memory prompt, the mode block, and any remaining CLI args (e.g. files to
|
|
525
637
|
# open). Adapters live in adapters.zsh; add one to support a new agent.
|
|
526
|
-
if ! typeset -f "
|
|
527
|
-
echo "Unknown AI launcher: $launcher (define
|
|
638
|
+
if ! typeset -f "__ai_adapter_$launcher" >/dev/null; then
|
|
639
|
+
echo "Unknown AI launcher: $launcher (define __ai_adapter_$launcher in adapters.zsh)" >&2
|
|
528
640
|
return 1
|
|
529
641
|
fi
|
|
530
|
-
"
|
|
642
|
+
"__ai_adapter_$launcher" "$memory_prompt" "$mode_block" "$@"
|
|
531
643
|
}
|
|
532
644
|
|
|
533
645
|
# Load the agent adapters, then generate a <name>-start launcher for every
|
|
534
646
|
# registered agent that has a matching adapter. Users extend by appending to
|
|
535
|
-
# AI_MEM_AGENTS (space-separated) and defining
|
|
647
|
+
# AI_MEM_AGENTS (space-separated) and defining __ai_adapter_<name>.
|
|
536
648
|
source "$AI_MEM_HOME/adapters.zsh"
|
|
537
649
|
: "${AI_MEM_AGENTS:=claude codex gemini cursor opencode}"
|
|
538
650
|
for _ai_agent in ${(z)AI_MEM_AGENTS}; do
|
|
539
|
-
if typeset -f "
|
|
651
|
+
if typeset -f "__ai_adapter_$_ai_agent" >/dev/null; then
|
|
540
652
|
# A same-named alias (e.g. from another plugin) makes zsh refuse to
|
|
541
653
|
# `eval` a function definition of that name at all -- "defining
|
|
542
654
|
# function based on alias", parse error near '()'. Clear it first;
|
|
543
655
|
# the function this defines is what should win.
|
|
544
656
|
unalias "${_ai_agent}-start" 2>/dev/null
|
|
545
|
-
eval "${_ai_agent}-start() {
|
|
657
|
+
eval "${_ai_agent}-start() { __ai_session_start ${_ai_agent} \"\$@\"; }"
|
|
546
658
|
fi
|
|
547
659
|
done
|
|
548
660
|
unset _ai_agent
|
|
549
661
|
|
|
550
|
-
|
|
551
|
-
|
|
662
|
+
__ai_mem_current_project() {
|
|
663
|
+
__ai_mem_resolve_project
|
|
552
664
|
}
|
|
553
665
|
|
|
554
|
-
|
|
666
|
+
__ai_mem_today_session_log() {
|
|
555
667
|
local project_name="${1:-}"
|
|
556
668
|
if [[ -z "$project_name" ]]; then
|
|
557
|
-
project_name="$(
|
|
669
|
+
project_name="$(__ai_mem_resolve_project)"
|
|
558
670
|
fi
|
|
559
671
|
|
|
560
|
-
|
|
672
|
+
__ai_mem_ensure_vault || return 1
|
|
561
673
|
|
|
562
674
|
local today
|
|
563
675
|
today="$(date +%Y-%m-%d)"
|
|
564
676
|
local project_session_dir
|
|
565
|
-
project_session_dir="$(
|
|
677
|
+
project_session_dir="$(__ai_mem_project_session_dir "$project_name")"
|
|
566
678
|
|
|
567
679
|
if [[ -n "${AI_MEM_ACTIVE_SESSION_LOG:-}" && -f "$AI_MEM_ACTIVE_SESSION_LOG" ]]; then
|
|
568
680
|
case "$AI_MEM_ACTIVE_SESSION_LOG" in
|
|
@@ -581,13 +693,13 @@ _ai_mem_today_session_log() {
|
|
|
581
693
|
latest="$(find "$AI_MEM_SESSION_DIR" -maxdepth 1 -type f -name "${project_name}-${today}_*.md" 2>/dev/null | sort | tail -n 1)" || true
|
|
582
694
|
fi
|
|
583
695
|
if [[ -n "$latest" ]]; then
|
|
584
|
-
|
|
696
|
+
__ai_mem_guard "$latest" || return 1
|
|
585
697
|
print -r -- "$latest"
|
|
586
698
|
return 0
|
|
587
699
|
fi
|
|
588
700
|
|
|
589
701
|
local session_note="$project_session_dir/${project_name}-${today}_$(date +%H-%M-%S).md"
|
|
590
|
-
|
|
702
|
+
__ai_mem_guard "$session_note" || return 1
|
|
591
703
|
mkdir -p "$project_session_dir"
|
|
592
704
|
SESSION_DATE="$today" PROJECT_NAME="$project_name" perl -0pe 's/\{\{date\}\}/$ENV{SESSION_DATE}/g; s/\{\{project_name\}\}/$ENV{PROJECT_NAME}/g' \
|
|
593
705
|
"$AI_MEM_SESSION_DIR/_session_template.md" > "$session_note"
|
|
@@ -606,18 +718,18 @@ ai-note() {
|
|
|
606
718
|
fi
|
|
607
719
|
|
|
608
720
|
local project_name session_note timestamp
|
|
609
|
-
project_name="$(
|
|
610
|
-
session_note="$(
|
|
721
|
+
project_name="$(__ai_mem_current_project)" || return 1
|
|
722
|
+
session_note="$(__ai_mem_today_session_log "$project_name")" || return 1
|
|
611
723
|
timestamp="$(date +%H:%M)"
|
|
612
724
|
|
|
613
|
-
|
|
725
|
+
__ai_mem_guard "$session_note" || return 1
|
|
614
726
|
|
|
615
727
|
if ! grep -q '^### Live Notes' "$session_note" 2>/dev/null; then
|
|
616
728
|
printf '\n### Live Notes\n' >> "$session_note"
|
|
617
729
|
fi
|
|
618
730
|
|
|
619
731
|
printf '\n- %s %s\n' "$timestamp" "$note_text" >> "$session_note"
|
|
620
|
-
|
|
732
|
+
__ai_mem_vault_backup
|
|
621
733
|
printf 'Appended to %s\n' "$session_note"
|
|
622
734
|
}
|
|
623
735
|
|
|
@@ -644,11 +756,11 @@ ai-lesson() {
|
|
|
644
756
|
fi
|
|
645
757
|
|
|
646
758
|
local project_name lesson_file timestamp
|
|
647
|
-
project_name="$(
|
|
759
|
+
project_name="$(__ai_mem_current_project)" || return 1
|
|
648
760
|
lesson_file="$AI_MEM_ROOT/_lessons/${slug}.md"
|
|
649
761
|
timestamp="$(date '+%Y-%m-%d %H:%M')"
|
|
650
762
|
|
|
651
|
-
|
|
763
|
+
__ai_mem_guard "$lesson_file" || return 1
|
|
652
764
|
|
|
653
765
|
if [[ ! -f "$lesson_file" ]]; then
|
|
654
766
|
mkdir -p "$AI_MEM_ROOT/_lessons"
|
|
@@ -663,7 +775,7 @@ ai-lesson() {
|
|
|
663
775
|
|
|
664
776
|
printf -- '\n## %s [[%s]]\n\n### Problem\n%s\n\n### Solution\n%s\n' \
|
|
665
777
|
"$timestamp" "$project_name" "$problem" "$solution" >> "$lesson_file"
|
|
666
|
-
|
|
778
|
+
__ai_mem_vault_backup
|
|
667
779
|
printf 'Appended to %s\n' "$lesson_file"
|
|
668
780
|
}
|
|
669
781
|
# Lints the vault's links: session logs missing a project wikilink, previous
|
|
@@ -672,11 +784,21 @@ ai-lesson() {
|
|
|
672
784
|
# "isolated dots in Graph View" class of problem before you have to notice it
|
|
673
785
|
# by eye.
|
|
674
786
|
ai-mem-lint() {
|
|
787
|
+
local fix=0
|
|
788
|
+
[[ "${1:-}" == "--fix" ]] && fix=1
|
|
675
789
|
local issues=0 f label target found
|
|
790
|
+
# Open Knowledge Format names exactly one required frontmatter field:
|
|
791
|
+
# `type`. Every note the vault ships already declares one except session
|
|
792
|
+
# logs, which predate the field -- so a vault that has been in use carries
|
|
793
|
+
# a backlog of them. Counted rather than listed: one actionable line beats
|
|
794
|
+
# several hundred identical ones, and the fix is a single pass anyway.
|
|
795
|
+
local -a untyped_files
|
|
676
796
|
|
|
677
797
|
while IFS= read -r -d '' f; do
|
|
678
798
|
[[ "${f:t}" == "_session_template.md" ]] && continue
|
|
679
799
|
|
|
800
|
+
grep -qm1 '^type: ' "$f" 2>/dev/null || untyped_files+=("$f")
|
|
801
|
+
|
|
680
802
|
if ! grep -qm1 -E '^project:.*\[\[.*\]\]' "$f" 2>/dev/null; then
|
|
681
803
|
print -r -- "orphaned session log (no project link): $f"
|
|
682
804
|
(( issues++ ))
|
|
@@ -703,6 +825,29 @@ ai-mem-lint() {
|
|
|
703
825
|
fi
|
|
704
826
|
done < <(find "$AI_MEM_PROJECT_DIR" -maxdepth 1 -name "*.md" -print0 2>/dev/null)
|
|
705
827
|
|
|
828
|
+
if (( $#untyped_files )); then
|
|
829
|
+
if (( fix )); then
|
|
830
|
+
# Written by hand rather than with `sed -i`, whose in-place flag
|
|
831
|
+
# takes an argument on BSD and not on GNU -- a portability trap
|
|
832
|
+
# for a one-liner a user would otherwise paste from the README.
|
|
833
|
+
local tmp
|
|
834
|
+
for f in $untyped_files; do
|
|
835
|
+
__ai_mem_guard "$f" >/dev/null || continue
|
|
836
|
+
tmp="$f.aimem-backfill"
|
|
837
|
+
if [[ "$(head -1 "$f")" == "---" ]]; then
|
|
838
|
+
{ head -1 "$f"; print -r -- "type: ai-session-log"; tail -n +2 "$f"; } > "$tmp"
|
|
839
|
+
else
|
|
840
|
+
{ print -rl -- "---" "type: ai-session-log" "---" ""; cat "$f"; } > "$tmp"
|
|
841
|
+
fi
|
|
842
|
+
mv "$tmp" "$f"
|
|
843
|
+
done
|
|
844
|
+
print -r -- "backfilled \`type: ai-session-log\` into $#untyped_files session log(s)"
|
|
845
|
+
else
|
|
846
|
+
print -r -- "$#untyped_files session log(s) missing the \`type:\` frontmatter field (Open Knowledge Format requires it). Fix with: ai-mem-lint --fix"
|
|
847
|
+
(( issues++ ))
|
|
848
|
+
fi
|
|
849
|
+
fi
|
|
850
|
+
|
|
706
851
|
if (( issues == 0 )); then
|
|
707
852
|
print -r -- "ok: vault links are clean"
|
|
708
853
|
else
|
|
@@ -727,7 +872,7 @@ ai-mem-search() {
|
|
|
727
872
|
|
|
728
873
|
local search_root="$AI_MEM_ROOT"
|
|
729
874
|
if [[ -n "$project" ]]; then
|
|
730
|
-
search_root="$(
|
|
875
|
+
search_root="$(__ai_mem_project_session_dir "$project")"
|
|
731
876
|
if [[ ! -d "$search_root" ]]; then
|
|
732
877
|
print -r -- "ai-mem-search: no session logs for project '$project'"
|
|
733
878
|
return 1
|