create-ai-memory 0.14.2 → 0.15.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 +369 -293
- package/bin/ai-mem-mcp.js +2 -0
- package/bin/ai-mem-serve.js +11 -1
- package/bin/create-ai-memory.js +11 -1
- package/hooks/claude/session-start.sh +9 -0
- package/hooks/claude/session-summary.sh +21 -2
- package/install.sh +3 -3
- package/package.json +1 -1
- package/shell/adapters.zsh +15 -0
- package/shell/ai-mem.zsh +645 -26
- package/web/viewer.html +25 -2
package/shell/ai-mem.zsh
CHANGED
|
@@ -18,8 +18,11 @@
|
|
|
18
18
|
# an agent then reports as success. Do not "tidy" these back to one
|
|
19
19
|
# underscore -- tests/run.sh asserts the naming for exactly this reason.
|
|
20
20
|
|
|
21
|
-
# Directory holding this module, used to source sibling files.
|
|
22
|
-
|
|
21
|
+
# Directory holding this module, used to source sibling files. Exported so a
|
|
22
|
+
# non-interactive child shell can re-source the module by absolute path
|
|
23
|
+
# (`zsh -c 'source "$AI_MEM_HOME/ai-mem.zsh"; ...'`) without a hard-coded path
|
|
24
|
+
# and without the cost of a full interactive shell (`zsh -ic`).
|
|
25
|
+
export AI_MEM_HOME="${0:A:h}"
|
|
23
26
|
|
|
24
27
|
# Where the shipped example notes and templates live, used to auto-scaffold a
|
|
25
28
|
# vault on first use so install.sh is optional (plugin-manager installs work).
|
|
@@ -98,6 +101,30 @@ __ai_mem_vault_backup() {
|
|
|
98
101
|
# ai-note/ai-lesson -- e.g. the update-session-log skill, which edits the
|
|
99
102
|
# Session Outcome section directly and has no other reason to know this
|
|
100
103
|
# function exists.
|
|
104
|
+
# The graph viewer and the MCP server are Node scripts shipped beside this
|
|
105
|
+
# module. They are declared as npm `bin` entries too, but `npm create` copies
|
|
106
|
+
# the tool into a folder rather than installing it globally, so nothing puts
|
|
107
|
+
# them on PATH -- exposing them as functions is what makes the documented
|
|
108
|
+
# commands work for everyone, however they installed.
|
|
109
|
+
#
|
|
110
|
+
# The bundled copy wins over anything on PATH: it is guaranteed to match the
|
|
111
|
+
# module the shell just sourced, which a separately-installed global may not.
|
|
112
|
+
__ai_mem_node_bin() {
|
|
113
|
+
local script="$AI_MEM_HOME/../bin/$1"
|
|
114
|
+
shift
|
|
115
|
+
if [[ -f "$script" ]]; then
|
|
116
|
+
command node "$script" "$@"
|
|
117
|
+
elif command -v "${1:-}" >/dev/null 2>&1; then
|
|
118
|
+
command "$@"
|
|
119
|
+
else
|
|
120
|
+
print -r -- "$script is missing -- reinstall with: npm create ai-memory@latest" >&2
|
|
121
|
+
return 1
|
|
122
|
+
fi
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
ai-mem-serve() { __ai_mem_node_bin ai-mem-serve.js "$@" }
|
|
126
|
+
ai-mem-mcp() { __ai_mem_node_bin ai-mem-mcp.js "$@" }
|
|
127
|
+
|
|
101
128
|
ai-mem-vault-backup() {
|
|
102
129
|
# Fail loudly if the helper is absent. Without this the function still
|
|
103
130
|
# "works": HEAD does not move, so it prints "nothing to commit" and exits
|
|
@@ -115,11 +142,46 @@ ai-mem-vault-backup() {
|
|
|
115
142
|
after="$(git -C "${AI_MEM_ROOT:-}" rev-parse HEAD 2>/dev/null || true)"
|
|
116
143
|
if [[ -z "$before" && -z "$after" ]]; then
|
|
117
144
|
echo "ai-mem-vault-backup: vault isn't git-backed, nothing to do"
|
|
118
|
-
|
|
119
|
-
echo "ai-mem-vault-backup: pushed"
|
|
120
|
-
else
|
|
121
|
-
echo "ai-mem-vault-backup: nothing to commit"
|
|
145
|
+
return 0
|
|
122
146
|
fi
|
|
147
|
+
|
|
148
|
+
# "pushed" used to mean "HEAD moved", which is what a COMMIT does. The push
|
|
149
|
+
# is best-effort and swallows its own errors, so a commit that never left
|
|
150
|
+
# the machine still reported success -- observed for real: three commits
|
|
151
|
+
# sat unpushed for an hour while every backup said "pushed", because the
|
|
152
|
+
# credential in use had lost access to the vault repo.
|
|
153
|
+
#
|
|
154
|
+
# Ask the only question that matters: does the remote have what we have.
|
|
155
|
+
local upstream local_sha remote_sha
|
|
156
|
+
upstream="$(git -C "$AI_MEM_ROOT" rev-parse --abbrev-ref '@{u}' 2>/dev/null || true)"
|
|
157
|
+
if [[ -z "$upstream" ]]; then
|
|
158
|
+
if [[ "$before" != "$after" ]]; then
|
|
159
|
+
echo "ai-mem-vault-backup: committed, but this branch has no upstream -- nothing was pushed"
|
|
160
|
+
return 1
|
|
161
|
+
fi
|
|
162
|
+
echo "ai-mem-vault-backup: nothing to commit (no upstream configured)"
|
|
163
|
+
return 0
|
|
164
|
+
fi
|
|
165
|
+
|
|
166
|
+
git -C "$AI_MEM_ROOT" fetch -q origin 2>/dev/null
|
|
167
|
+
local_sha="$(git -C "$AI_MEM_ROOT" rev-parse HEAD 2>/dev/null || true)"
|
|
168
|
+
remote_sha="$(git -C "$AI_MEM_ROOT" rev-parse '@{u}' 2>/dev/null || true)"
|
|
169
|
+
|
|
170
|
+
if [[ -n "$local_sha" && "$local_sha" == "$remote_sha" ]]; then
|
|
171
|
+
if [[ "$before" != "$after" ]]; then
|
|
172
|
+
echo "ai-mem-vault-backup: pushed"
|
|
173
|
+
else
|
|
174
|
+
echo "ai-mem-vault-backup: nothing to commit"
|
|
175
|
+
fi
|
|
176
|
+
return 0
|
|
177
|
+
fi
|
|
178
|
+
|
|
179
|
+
local ahead
|
|
180
|
+
ahead="$(git -C "$AI_MEM_ROOT" rev-list --count '@{u}..HEAD' 2>/dev/null || echo '?')"
|
|
181
|
+
print -r -- "ai-mem-vault-backup: NOT PUSHED -- $ahead commit(s) exist only on this machine." >&2
|
|
182
|
+
print -r -- " The vault is committed locally but the remote does not have it. Check credentials and push by hand:" >&2
|
|
183
|
+
print -r -- " git -C \"$AI_MEM_ROOT\" push" >&2
|
|
184
|
+
return 1
|
|
123
185
|
}
|
|
124
186
|
# Read a vault note only when it exists inside the memory root.
|
|
125
187
|
#
|
|
@@ -136,6 +198,38 @@ ai-mem-vault-backup() {
|
|
|
136
198
|
# target, a target outside the vault, or a diff that would leave nothing --
|
|
137
199
|
# because injecting a note twice merely costs tokens, while dropping one
|
|
138
200
|
# costs the agent context it was promised.
|
|
201
|
+
# Bound what a single note can contribute to the launch prompt.
|
|
202
|
+
#
|
|
203
|
+
# Every other path into the prompt is already bounded: session-log fields at
|
|
204
|
+
# 500 chars each, the lesson index at 200 slugs, search output at a flat cap,
|
|
205
|
+
# and the project note is a path rather than its contents. The inlined notes
|
|
206
|
+
# were the one exception, so a profile that grows makes EVERY session in
|
|
207
|
+
# EVERY project more expensive, forever, and nothing reports it.
|
|
208
|
+
#
|
|
209
|
+
# Truncation states the real total and where to read the rest, because a
|
|
210
|
+
# truncated note the agent cannot tell from a complete one is worse than a
|
|
211
|
+
# long one -- it would answer from half a profile believing it had all of it.
|
|
212
|
+
#
|
|
213
|
+
# The cut lands on a line boundary. Markdown cut mid-line can leave an
|
|
214
|
+
# unterminated code fence or half a heading, which reads as content rather
|
|
215
|
+
# than as damage.
|
|
216
|
+
__ai_mem_cap_note() {
|
|
217
|
+
local text="$1" source_ref="${2:-the note}"
|
|
218
|
+
local cap="${AI_MEM_NOTE_MAX_CHARS:-8000}"
|
|
219
|
+
# 0 disables the cap outright, for anyone who wants the old behaviour.
|
|
220
|
+
if (( cap <= 0 )) || (( ${#text} <= cap )); then
|
|
221
|
+
print -r -- "$text"
|
|
222
|
+
return 0
|
|
223
|
+
fi
|
|
224
|
+
local head_text="${text[1,$cap]}"
|
|
225
|
+
local trimmed="${head_text%$'\n'*}"
|
|
226
|
+
# A single line longer than the whole cap leaves nothing after trimming;
|
|
227
|
+
# keep the hard cut rather than emitting an empty note.
|
|
228
|
+
[[ -n "${trimmed//[[:space:]]/}" ]] && head_text="$trimmed"
|
|
229
|
+
print -r -- "$head_text"
|
|
230
|
+
print -r -- "[truncated at ${cap} of ${#text} chars -- read the rest with: cat \"$source_ref\"]"
|
|
231
|
+
}
|
|
232
|
+
|
|
139
233
|
__ai_mem_note_contents() {
|
|
140
234
|
local note_path="${1:-}"
|
|
141
235
|
if [[ -z "$note_path" || ! -f "$note_path" ]]; then
|
|
@@ -149,7 +243,7 @@ __ai_mem_note_contents() {
|
|
|
149
243
|
local mirror
|
|
150
244
|
mirror="$(sed -n '2,/^---$/p' "$note_path" 2>/dev/null | sed -n 's/^mirror_of:[[:space:]]*//p' | head -1)"
|
|
151
245
|
if [[ -z "$mirror" ]]; then
|
|
152
|
-
|
|
246
|
+
__ai_mem_cap_note "$(<"$note_path")" "$note_path"
|
|
153
247
|
return 0
|
|
154
248
|
fi
|
|
155
249
|
|
|
@@ -160,7 +254,7 @@ __ai_mem_note_contents() {
|
|
|
160
254
|
# every separator outright is both the safer rule and the simpler one.
|
|
161
255
|
local target="$AI_MEM_ROOT/$mirror"
|
|
162
256
|
if [[ "$mirror" == */* || ! -f "$target" ]]; then
|
|
163
|
-
|
|
257
|
+
__ai_mem_cap_note "$(<"$note_path")" "$note_path"
|
|
164
258
|
return 0
|
|
165
259
|
fi
|
|
166
260
|
|
|
@@ -174,7 +268,7 @@ __ai_mem_note_contents() {
|
|
|
174
268
|
fi
|
|
175
269
|
|
|
176
270
|
print -r -- "(mirror of ${mirror}, shown above -- only the lines that differ from it are repeated here)"
|
|
177
|
-
|
|
271
|
+
__ai_mem_cap_note "$unique" "$note_path"
|
|
178
272
|
}
|
|
179
273
|
|
|
180
274
|
__ai_mem_resolve_project() {
|
|
@@ -249,14 +343,30 @@ __ai_mem_latest_session_log() {
|
|
|
249
343
|
project_name="$(__ai_mem_resolve_project)"
|
|
250
344
|
fi
|
|
251
345
|
|
|
252
|
-
local latest=""
|
|
346
|
+
local logs="" latest=""
|
|
253
347
|
local project_session_dir
|
|
254
348
|
project_session_dir="$(__ai_mem_project_session_dir "$project_name")"
|
|
255
349
|
|
|
256
|
-
|
|
257
|
-
if [[ -z "$
|
|
258
|
-
|
|
350
|
+
logs="$(find "$project_session_dir" -maxdepth 1 -type f -name "${project_name}-*.md" 2>/dev/null | sort -r)" || true
|
|
351
|
+
if [[ -z "$logs" ]]; then
|
|
352
|
+
logs="$(find "$AI_MEM_SESSION_DIR" -maxdepth 1 -type f -name "${project_name}-*.md" 2>/dev/null | sort -r)" || true
|
|
259
353
|
fi
|
|
354
|
+
[[ -n "$logs" ]] || return 0
|
|
355
|
+
|
|
356
|
+
# The newest file is often an untouched template: a session that was
|
|
357
|
+
# opened and closed, or one still running in another terminal. Pointing
|
|
358
|
+
# the next session at it hands over nothing, while real context sits one
|
|
359
|
+
# file back. So take the newest log with any line that is not template
|
|
360
|
+
# scaffolding (frontmatter, blanks, the heading, [placeholder] bullets).
|
|
361
|
+
# One awk pass over newest-first files, stopping at the first hit. When
|
|
362
|
+
# every log is unfilled, keep the newest, as before.
|
|
363
|
+
latest="$(awk '
|
|
364
|
+
FNR == 1 { fm = ($0 == "---"); if (fm) next }
|
|
365
|
+
fm { if ($0 == "---") fm = 0; next }
|
|
366
|
+
/^[[:space:]]*$/ || $0 == "# Session Outcome" || /^\* \*\*[^*]+:\*\*[[:space:]]*\[.*\]$/ { next }
|
|
367
|
+
{ print FILENAME; exit }
|
|
368
|
+
' "${(@f)logs}" 2>/dev/null)" || true
|
|
369
|
+
[[ -n "$latest" ]] || latest="${logs%%$'\n'*}"
|
|
260
370
|
if [[ -n "$latest" ]]; then
|
|
261
371
|
__ai_mem_guard "$latest" || return 1
|
|
262
372
|
print -r -- "$latest"
|
|
@@ -293,7 +403,22 @@ __ai_mem_ensure_vault() {
|
|
|
293
403
|
# in the file alone. This runs on session prep, so an existing install
|
|
294
404
|
# heals on its next launch rather than waiting for someone to think to run
|
|
295
405
|
# ai-mem-lint --fix.
|
|
406
|
+
# The `project:` line must be a [[wikilink]] or every log created from this
|
|
407
|
+
# template is orphaned from its project note -- which is what the graph
|
|
408
|
+
# edges and ai-mem-lint both key on. A vault whose template predates that
|
|
409
|
+
# produced disconnected logs silently, one per session.
|
|
296
410
|
local tmpl="$AI_MEM_SESSION_DIR/_session_template.md"
|
|
411
|
+
if [[ -f "$tmpl" ]] && grep -qm1 '^project: {{project_name}}$' "$tmpl"; then
|
|
412
|
+
__ai_mem_guard "$tmpl" || return 1
|
|
413
|
+
local ptmp="$tmpl.aimem-upgrade"
|
|
414
|
+
sed 's|^project: {{project_name}}$|project: "[[{{project_name}}]]"|' "$tmpl" > "$ptmp" && mv "$ptmp" "$tmpl"
|
|
415
|
+
fi
|
|
416
|
+
if [[ -f "$tmpl" ]] && ! grep -qm1 '^previous:' "$tmpl"; then
|
|
417
|
+
__ai_mem_guard "$tmpl" || return 1
|
|
418
|
+
local vtmp="$tmpl.aimem-upgrade"
|
|
419
|
+
awk '/^project:/ { print; print "previous: \"{{previous_session_link}}\""; next } { print }' "$tmpl" > "$vtmp" && mv "$vtmp" "$tmpl"
|
|
420
|
+
fi
|
|
421
|
+
|
|
297
422
|
if [[ -f "$tmpl" ]] && ! grep -qm1 '^type: ' "$tmpl"; then
|
|
298
423
|
__ai_mem_guard "$tmpl" || return 1
|
|
299
424
|
local tmp="$tmpl.aimem-upgrade"
|
|
@@ -441,6 +566,16 @@ __ai_mem_context_prompt() {
|
|
|
441
566
|
fi
|
|
442
567
|
fi
|
|
443
568
|
|
|
569
|
+
# A project note straight from the template is worse than no note: the
|
|
570
|
+
# prompt points at it as "Project context", the agent opens it, and finds
|
|
571
|
+
# "[What problem this repository solves]". That reads as context and
|
|
572
|
+
# carries none -- the same present-but-empty failure as a search that
|
|
573
|
+
# returns nothing while looking authoritative. Say so, and say what to do.
|
|
574
|
+
local project_state=""
|
|
575
|
+
if [[ -f "$project_note" ]] && grep -q '\[What problem this repository solves\]' "$project_note" 2>/dev/null; then
|
|
576
|
+
project_state=" -- NOT YET FILLED IN: it is still the blank template, so it holds no context. Read the repo (README, package manifest, AGENTS.md/CLAUDE.md, recent git log) and fill it in early in this session; every future session on this repo starts from it."
|
|
577
|
+
fi
|
|
578
|
+
|
|
444
579
|
cat <<EOF
|
|
445
580
|
Read these notes before doing anything else:
|
|
446
581
|
- Global profile:
|
|
@@ -449,7 +584,7 @@ $(__ai_mem_note_contents "$AI_MEM_GLOBAL")
|
|
|
449
584
|
- Standards:
|
|
450
585
|
$(__ai_mem_note_contents "$AI_MEM_STANDARDS")
|
|
451
586
|
|
|
452
|
-
- Project context: $project_note
|
|
587
|
+
- Project context: $project_note$project_state
|
|
453
588
|
$previous_session_block
|
|
454
589
|
- Active session log: $session_note
|
|
455
590
|
$(__ai_mem_lesson_index)
|
|
@@ -457,7 +592,9 @@ $(__ai_mem_lesson_index)
|
|
|
457
592
|
Use the Obsidian vault as the persistent memory layer.
|
|
458
593
|
Treat the global profile and standards note as the shared baseline for every run.
|
|
459
594
|
Keep durable preferences and project facts in the vault, and keep the active session log updated with decisions, blockers, and next steps.
|
|
460
|
-
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
|
|
595
|
+
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 zsh functions from the sourced module, so \`command -v\` finds them but \`which\` under bash will not. 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.
|
|
596
|
+
When you hit a blocker -- an error you do not immediately understand, a test failing for an unclear reason, a decision you cannot settle from the code in front of you, or a second failed attempt at the same thing -- search the vault BEFORE guessing again. You have likely been here before, and \`_lessons/\` exists because the answer usually was written down; lessons are ranked above session logs in the results, so a hit under \`_lessons/\` is the recorded fix.
|
|
597
|
+
Search ONE distinctive word, not a sentence. Matching is literal substring, so a whole error line ('command not found: sed') finds nothing while 'command not found' finds it, and a bare tool name ('sed', 'git') returns hundreds of irrelevant lines. Pick the most unusual word in the symptom and try two or three of them separately.
|
|
461
598
|
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.
|
|
462
599
|
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.
|
|
463
600
|
EOF
|
|
@@ -503,6 +640,13 @@ __ai_mem_export_active() {
|
|
|
503
640
|
export AI_MEM_ACTIVE_PROJECT="${1:-}"
|
|
504
641
|
export AI_MEM_PREVIOUS_SESSION_LOG="${2:-}"
|
|
505
642
|
export AI_MEM_ACTIVE_SESSION_LOG="${3:-}"
|
|
643
|
+
# These exports outlive the agent in the user's shell. The Claude hooks
|
|
644
|
+
# compare this against the repo they run in, so a later plain `claude` in a
|
|
645
|
+
# different repo does not write that repo's commits into this project's log.
|
|
646
|
+
# The common dir, not the toplevel, so every worktree of one repo matches.
|
|
647
|
+
local git_dir
|
|
648
|
+
git_dir="$(git rev-parse --git-common-dir 2>/dev/null)" || git_dir=""
|
|
649
|
+
export AI_MEM_ACTIVE_GIT_DIR="${git_dir:+${git_dir:A}}"
|
|
506
650
|
}
|
|
507
651
|
|
|
508
652
|
ai-start() {
|
|
@@ -671,7 +815,7 @@ __ai_session_start() {
|
|
|
671
815
|
# registered agent that has a matching adapter. Users extend by appending to
|
|
672
816
|
# AI_MEM_AGENTS (space-separated) and defining __ai_adapter_<name>.
|
|
673
817
|
source "$AI_MEM_HOME/adapters.zsh"
|
|
674
|
-
: "${AI_MEM_AGENTS:=claude codex gemini cursor opencode}"
|
|
818
|
+
: "${AI_MEM_AGENTS:=claude codex agy gemini cursor opencode}"
|
|
675
819
|
for _ai_agent in ${(z)AI_MEM_AGENTS}; do
|
|
676
820
|
if typeset -f "__ai_adapter_$_ai_agent" >/dev/null; then
|
|
677
821
|
# A same-named alias (e.g. from another plugin) makes zsh refuse to
|
|
@@ -801,7 +945,149 @@ ai-lesson() {
|
|
|
801
945
|
printf -- '\n## %s [[%s]]\n\n### Problem\n%s\n\n### Solution\n%s\n' \
|
|
802
946
|
"$timestamp" "$project_name" "$problem" "$solution" >> "$lesson_file"
|
|
803
947
|
__ai_mem_vault_backup
|
|
804
|
-
|
|
948
|
+
# Make the reinforcement visible: a second-or-later entry is the same
|
|
949
|
+
# lesson firing again, which ai-mem-search now ranks higher. Derived from
|
|
950
|
+
# the entries themselves, so nothing is stored twice.
|
|
951
|
+
local recalls
|
|
952
|
+
recalls="$(grep -c '^## [0-9]' "$lesson_file" 2>/dev/null)"
|
|
953
|
+
if (( recalls > 1 )); then
|
|
954
|
+
printf 'Reinforced %s -- %d recalls now, so it ranks higher in ai-mem-search\n' "$lesson_file" "$recalls"
|
|
955
|
+
else
|
|
956
|
+
printf 'Appended to %s\n' "$lesson_file"
|
|
957
|
+
fi
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
# Ingests a distilled record of an external artifact (meeting, video, voice
|
|
961
|
+
# note, ...) into the vault as ONE flat note per source_id under _transcripts/.
|
|
962
|
+
# The heavy lifting -- transcription and distillation -- happens upstream
|
|
963
|
+
# (fimbria); this only takes the already-distilled metadata as flags and files
|
|
964
|
+
# it in reversible Markdown. Deliberately NOT a raw-transcript store: there is
|
|
965
|
+
# no --text flag, the note LINKS to --transcript-path instead of embedding it
|
|
966
|
+
# (distill, don't dump). Mirrors ai-lesson's one-file-per-slug create-or-update
|
|
967
|
+
# shape so ai-mem-search covers it for free -- no new search path.
|
|
968
|
+
#
|
|
969
|
+
# Metadata arrives as flags, not JSON: parsing arbitrary JSON in pure zsh would
|
|
970
|
+
# need jq, and a new external dependency is exactly what this project refuses.
|
|
971
|
+
ai-mem-ingest() {
|
|
972
|
+
local source_id="" title="" date="" kind="" source_uri="" transcript_path=""
|
|
973
|
+
local project="" summary=""
|
|
974
|
+
local -a persons decisions actions
|
|
975
|
+
persons=(); decisions=(); actions=()
|
|
976
|
+
|
|
977
|
+
while (( $# )); do
|
|
978
|
+
case "$1" in
|
|
979
|
+
--source-id) source_id="${2:-}"; shift 2 ;;
|
|
980
|
+
--title) title="${2:-}"; shift 2 ;;
|
|
981
|
+
--date) date="${2:-}"; shift 2 ;;
|
|
982
|
+
--kind) kind="${2:-}"; shift 2 ;;
|
|
983
|
+
--source-uri) source_uri="${2:-}"; shift 2 ;;
|
|
984
|
+
--transcript-path) transcript_path="${2:-}"; shift 2 ;;
|
|
985
|
+
--project) project="${2:-}"; shift 2 ;;
|
|
986
|
+
--summary) summary="${2:-}"; shift 2 ;;
|
|
987
|
+
--person) persons+=("${2:-}"); shift 2 ;;
|
|
988
|
+
--decision) decisions+=("${2:-}"); shift 2 ;;
|
|
989
|
+
--action) actions+=("${2:-}"); shift 2 ;;
|
|
990
|
+
*) print -r -- "ai-mem-ingest: unknown flag: $1" >&2; return 1 ;;
|
|
991
|
+
esac
|
|
992
|
+
done
|
|
993
|
+
|
|
994
|
+
if [[ -z "$source_id" || -z "$title" || -z "$date" ]]; then
|
|
995
|
+
print -r -- "Usage: ai-mem-ingest --source-id <id> --title <t> --date <ISO> [--kind <meeting|video|voice-note|other>] [--source-uri <path/url>] [--transcript-path <path>] [--project <name>] [--person <name> ...] [--summary <text>] [--decision <text> ...] [--action <text> ...]" >&2
|
|
996
|
+
print -r -- "ai-mem-ingest: --source-id, --title, and --date are required" >&2
|
|
997
|
+
return 1
|
|
998
|
+
fi
|
|
999
|
+
|
|
1000
|
+
# Dedup key: the filename is slug(source_id), slugified flat exactly like
|
|
1001
|
+
# ai-lesson so a crafted source_id cannot path-traverse out of _transcripts/,
|
|
1002
|
+
# and a re-ingest of the same source_id lands on the same file (update, not
|
|
1003
|
+
# a duplicate).
|
|
1004
|
+
local slug
|
|
1005
|
+
slug="$(print -r -- "$source_id" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9]+/-/g; s/^-+|-+$//g')"
|
|
1006
|
+
if [[ -z "$slug" ]]; then
|
|
1007
|
+
print -r -- "ai-mem-ingest: --source-id must contain at least one letter or digit" >&2
|
|
1008
|
+
return 1
|
|
1009
|
+
fi
|
|
1010
|
+
|
|
1011
|
+
local transcript_file="$AI_MEM_ROOT/_transcripts/${slug}.md"
|
|
1012
|
+
__ai_mem_guard "$transcript_file" || return 1
|
|
1013
|
+
|
|
1014
|
+
# Optional project wikilink: an explicit --project wins, else the pinned
|
|
1015
|
+
# active session's project. Unlike ai-lesson this does NOT fall back to the
|
|
1016
|
+
# cwd basename -- an ingested artifact may belong to no project at all.
|
|
1017
|
+
local project_name="$project"
|
|
1018
|
+
[[ -z "$project_name" && -n "${AI_MEM_ACTIVE_PROJECT:-}" ]] && project_name="$AI_MEM_ACTIVE_PROJECT"
|
|
1019
|
+
|
|
1020
|
+
mkdir -p "$AI_MEM_ROOT/_transcripts"
|
|
1021
|
+
|
|
1022
|
+
# Build the whole note in a temp file and mv it into place, so a re-ingest
|
|
1023
|
+
# OVERWRITES (updates) the one file rather than appending a second copy.
|
|
1024
|
+
local tmp
|
|
1025
|
+
tmp="$(mktemp)"
|
|
1026
|
+
{
|
|
1027
|
+
print -r -- "---"
|
|
1028
|
+
print -r -- "type: ai-transcript"
|
|
1029
|
+
print -r -- "date: $date"
|
|
1030
|
+
print -r -- "title: $title"
|
|
1031
|
+
[[ -n "$kind" ]] && print -r -- "kind: $kind"
|
|
1032
|
+
print -r -- "source_uri: $source_uri"
|
|
1033
|
+
print -r -- "source_id: $source_id"
|
|
1034
|
+
[[ -n "$project_name" ]] && print -r -- "project: \"[[$project_name]]\""
|
|
1035
|
+
print -r -- "---"
|
|
1036
|
+
print -r -- ""
|
|
1037
|
+
print -r -- "# $title"
|
|
1038
|
+
|
|
1039
|
+
if (( ${#persons} )); then
|
|
1040
|
+
local -a plinks; plinks=()
|
|
1041
|
+
local p
|
|
1042
|
+
for p in "${persons[@]}"; do plinks+=("[[$p]]"); done
|
|
1043
|
+
print -r -- ""
|
|
1044
|
+
print -r -- "Attendees: ${(j: :)plinks}"
|
|
1045
|
+
fi
|
|
1046
|
+
|
|
1047
|
+
if [[ -n "$summary" ]]; then
|
|
1048
|
+
print -r -- ""
|
|
1049
|
+
print -r -- "## Summary"
|
|
1050
|
+
print -r -- ""
|
|
1051
|
+
print -r -- "$summary"
|
|
1052
|
+
fi
|
|
1053
|
+
|
|
1054
|
+
print -r -- ""
|
|
1055
|
+
print -r -- "## Decisions"
|
|
1056
|
+
print -r -- ""
|
|
1057
|
+
if (( ${#decisions} )); then
|
|
1058
|
+
local d
|
|
1059
|
+
for d in "${decisions[@]}"; do print -r -- "- $d"; done
|
|
1060
|
+
fi
|
|
1061
|
+
|
|
1062
|
+
print -r -- ""
|
|
1063
|
+
print -r -- "## Action items"
|
|
1064
|
+
print -r -- ""
|
|
1065
|
+
if (( ${#actions} )); then
|
|
1066
|
+
local a
|
|
1067
|
+
for a in "${actions[@]}"; do print -r -- "- $a"; done
|
|
1068
|
+
fi
|
|
1069
|
+
|
|
1070
|
+
# Distill, don't dump: only a relative link to the source transcript,
|
|
1071
|
+
# never its body. There is no flag that could carry the body.
|
|
1072
|
+
if [[ -n "$transcript_path" ]]; then
|
|
1073
|
+
print -r -- ""
|
|
1074
|
+
print -r -- "## Source"
|
|
1075
|
+
print -r -- ""
|
|
1076
|
+
print -r -- "- Transcript: [$transcript_path]($transcript_path)"
|
|
1077
|
+
fi
|
|
1078
|
+
} > "$tmp"
|
|
1079
|
+
|
|
1080
|
+
local existed=0
|
|
1081
|
+
[[ -f "$transcript_file" ]] && existed=1
|
|
1082
|
+
mv "$tmp" "$transcript_file"
|
|
1083
|
+
|
|
1084
|
+
__ai_mem_vault_backup
|
|
1085
|
+
|
|
1086
|
+
if (( existed )); then
|
|
1087
|
+
printf 'Updated %s\n' "$transcript_file"
|
|
1088
|
+
else
|
|
1089
|
+
printf 'Ingested %s\n' "$transcript_file"
|
|
1090
|
+
fi
|
|
805
1091
|
}
|
|
806
1092
|
# Lints the vault's links: session logs missing a project wikilink, previous
|
|
807
1093
|
# links pointing at a note that no longer exists, and project notes nothing
|
|
@@ -874,6 +1160,28 @@ ai-mem-lint() {
|
|
|
874
1160
|
fi
|
|
875
1161
|
fi
|
|
876
1162
|
|
|
1163
|
+
# Logs already written from a stale template stay orphaned until repaired.
|
|
1164
|
+
# Reporting them without offering the fix leaves the user to hand-edit
|
|
1165
|
+
# hundreds of files, which nobody does.
|
|
1166
|
+
local -a orphan_files
|
|
1167
|
+
while IFS= read -r -d '' f; do
|
|
1168
|
+
[[ "${f:t}" == "_session_template.md" ]] && continue
|
|
1169
|
+
grep -qm1 -E '^project:.*\[\[.*\]\]' "$f" 2>/dev/null && continue
|
|
1170
|
+
grep -qm1 -E '^project: .+' "$f" 2>/dev/null && orphan_files+=("$f")
|
|
1171
|
+
done < <(find "$AI_MEM_SESSION_DIR" -maxdepth 2 -name "*.md" -print0 2>/dev/null)
|
|
1172
|
+
|
|
1173
|
+
if (( $#orphan_files && fix )); then
|
|
1174
|
+
local pname otmp
|
|
1175
|
+
for f in $orphan_files; do
|
|
1176
|
+
__ai_mem_guard "$f" >/dev/null || continue
|
|
1177
|
+
pname="$(grep -m1 '^project: ' "$f" | sed 's/^project: *//; s/^"//; s/"$//')"
|
|
1178
|
+
[[ -n "$pname" ]] || continue
|
|
1179
|
+
otmp="$f.aimem-link"
|
|
1180
|
+
sed "s|^project: .*$|project: \"[[${pname}]]\"|" "$f" > "$otmp" && mv "$otmp" "$f"
|
|
1181
|
+
done
|
|
1182
|
+
print -r -- "linked $#orphan_files session log(s) back to their project note"
|
|
1183
|
+
fi
|
|
1184
|
+
|
|
877
1185
|
if (( $#untyped_files )); then
|
|
878
1186
|
if (( fix )); then
|
|
879
1187
|
# Written by hand rather than with `sed -i`, whose in-place flag
|
|
@@ -897,6 +1205,39 @@ ai-mem-lint() {
|
|
|
897
1205
|
fi
|
|
898
1206
|
fi
|
|
899
1207
|
|
|
1208
|
+
|
|
1209
|
+
# Dangling wikilinks: a [[target]] pointing at a note that does not exist --
|
|
1210
|
+
# a dead edge in the graph. Detection only: the dead link is surfaced, never
|
|
1211
|
+
# auto-removed, because the intended target is unknowable and a note's own
|
|
1212
|
+
# prose is the author's to edit. Wikilinks resolve by basename (the same way
|
|
1213
|
+
# ai-mem-serve draws them), so the node set is every note's basename.
|
|
1214
|
+
#
|
|
1215
|
+
# One grep over the vault, never a grep per file -- a per-file scan is the
|
|
1216
|
+
# 25s antipattern ai-mem-search documents. Scope is the vault root derived
|
|
1217
|
+
# from AI_MEM_SESSION_DIR (its parent), the same subtree the rest of this
|
|
1218
|
+
# linter walks, so a caller that redirects the session dir redirects this too.
|
|
1219
|
+
local vault="${AI_MEM_SESSION_DIR:h}"
|
|
1220
|
+
typeset -A _known
|
|
1221
|
+
local nf
|
|
1222
|
+
for nf in ${(f)"$(find "$vault" -name '*.md' -not -path '*/.git/*' 2>/dev/null)"}; do
|
|
1223
|
+
_known[${nf:t:r}]=1
|
|
1224
|
+
done
|
|
1225
|
+
local -a dangling
|
|
1226
|
+
local line src tgt
|
|
1227
|
+
for line in ${(f)"$(grep -rHoE --include='*.md' --exclude-dir=.git -- '\[\[[^]]+\]\]' "$vault" 2>/dev/null)"}; do
|
|
1228
|
+
src="${line%%:\[\[*}" # path, before the ':[[' grep separator
|
|
1229
|
+
tgt="${line##*:\[\[}"; tgt="${tgt%\]\]}" # inside the brackets
|
|
1230
|
+
tgt="${tgt%%\|*}"; tgt="${tgt%%#*}" # drop |alias and #heading
|
|
1231
|
+
tgt="${tgt## }"; tgt="${tgt%% }"
|
|
1232
|
+
[[ -z "$tgt" ]] && continue
|
|
1233
|
+
[[ -n "${_known[$tgt]-}" ]] && continue
|
|
1234
|
+
dangling+=( "${src:t} -> [[${tgt}]]" )
|
|
1235
|
+
done
|
|
1236
|
+
if (( $#dangling )); then
|
|
1237
|
+
print -r -- "$#dangling dangling wikilink(s) -- a [[link]] that resolves to no note:"
|
|
1238
|
+
for line in $dangling; do print -r -- " $line"; done
|
|
1239
|
+
(( issues += $#dangling ))
|
|
1240
|
+
fi
|
|
900
1241
|
if (( issues == 0 )); then
|
|
901
1242
|
print -r -- "ok: vault links are clean"
|
|
902
1243
|
else
|
|
@@ -905,6 +1246,211 @@ ai-mem-lint() {
|
|
|
905
1246
|
return $(( issues > 0 ? 1 : 0 ))
|
|
906
1247
|
}
|
|
907
1248
|
|
|
1249
|
+
|
|
1250
|
+
# ai-mem-sleep: the vault's "bedtime" pass -- consolidation, decay and lint in
|
|
1251
|
+
# one run, modelled on how a brain sleeps. It NEVER prunes a lesson: the
|
|
1252
|
+
# failure->fix records in _lessons/ are the durable knowledge that must survive,
|
|
1253
|
+
# and reinforcement already strengthens them. Only ephemeral SESSION LOGS decay,
|
|
1254
|
+
# and even they are ARCHIVED (moved under the project's _archive/, which
|
|
1255
|
+
# ai-mem-search skips), never deleted -- git keeps every move reversible.
|
|
1256
|
+
#
|
|
1257
|
+
# Dry run by default: it reports what would move and nothing changes on disk.
|
|
1258
|
+
# --apply performs the archive moves and backs the vault up. Tunables:
|
|
1259
|
+
# AI_MEM_SLEEP_KEEP newest logs per project always kept hot (default 5)
|
|
1260
|
+
# AI_MEM_SLEEP_DAYS archive logs older than this many days (default 90)
|
|
1261
|
+
# AI_MEM_SLEEP_CONSOLIDATE flag a project with this many hot logs (default 8)
|
|
1262
|
+
ai-mem-sleep() {
|
|
1263
|
+
local apply=0
|
|
1264
|
+
[[ "${1:-}" == "--apply" ]] && apply=1
|
|
1265
|
+
|
|
1266
|
+
zmodload zsh/datetime 2>/dev/null
|
|
1267
|
+
local keep="${AI_MEM_SLEEP_KEEP:-5}"
|
|
1268
|
+
local days="${AI_MEM_SLEEP_DAYS:-90}"
|
|
1269
|
+
local ripe="${AI_MEM_SLEEP_CONSOLIDATE:-8}"
|
|
1270
|
+
# Cut-off date as a string. Filenames carry YYYY-MM-DD, which sorts and
|
|
1271
|
+
# compares chronologically as text, so no BSD-vs-GNU `date` parsing is
|
|
1272
|
+
# needed -- strftime on an epoch offset is portable and dependency-free.
|
|
1273
|
+
local cutoff
|
|
1274
|
+
cutoff="$(strftime '%Y-%m-%d' $(( EPOCHSECONDS - days * 86400 )))"
|
|
1275
|
+
|
|
1276
|
+
if (( apply )); then
|
|
1277
|
+
print -r -- "ai-mem-sleep: applying. Keep newest $keep per project; archive session logs dated before $cutoff."
|
|
1278
|
+
else
|
|
1279
|
+
print -r -- "ai-mem-sleep: dry run (nothing moves). Keep newest $keep per project; would archive logs dated before $cutoff."
|
|
1280
|
+
fi
|
|
1281
|
+
print -r -- "Lessons are never touched -- only ephemeral session logs, and they are archived, not deleted."
|
|
1282
|
+
print -r -- "--"
|
|
1283
|
+
|
|
1284
|
+
local sessions_root="$AI_MEM_SESSION_DIR"
|
|
1285
|
+
if [[ ! -d "$sessions_root" ]]; then
|
|
1286
|
+
print -r -- "no session logs at $sessions_root -- nothing to do"
|
|
1287
|
+
return 0
|
|
1288
|
+
fi
|
|
1289
|
+
|
|
1290
|
+
local -i total_archived=0
|
|
1291
|
+
local project pdir log sidecar stem logdate
|
|
1292
|
+
local -a logs stale
|
|
1293
|
+
local -i i hot remaining
|
|
1294
|
+
for pdir in "$sessions_root"/*(/N); do
|
|
1295
|
+
project="${pdir:t}"
|
|
1296
|
+
# Skip the archive dir itself and any bookkeeping dir (leading _ or .).
|
|
1297
|
+
[[ "$project" == [_.]* ]] && continue
|
|
1298
|
+
|
|
1299
|
+
# Newest first: a log's name starts with the project slug, so name order
|
|
1300
|
+
# is date order within one project. (On) sorts by name, descending.
|
|
1301
|
+
logs=( "$pdir"/*.md(N.On) )
|
|
1302
|
+
(( ${#logs} )) || continue
|
|
1303
|
+
|
|
1304
|
+
# The newest KEEP stay hot however old they are, so a dormant project
|
|
1305
|
+
# keeps a readable recent history instead of emptying out.
|
|
1306
|
+
hot=$(( keep < ${#logs} ? keep : ${#logs} ))
|
|
1307
|
+
stale=()
|
|
1308
|
+
for (( i = hot + 1; i <= ${#logs}; i++ )); do
|
|
1309
|
+
log="${logs[i]}"
|
|
1310
|
+
stem="${${log:t:r}%%_*}" # "proj-2026-08-20_10-00-00.md" -> "proj-2026-08-20"
|
|
1311
|
+
logdate="${stem: -10}" # -> "2026-08-20"
|
|
1312
|
+
[[ "$logdate" =~ '^[0-9]{4}-[0-9]{2}-[0-9]{2}$' ]] || continue # not a dated log
|
|
1313
|
+
[[ "$logdate" < "$cutoff" ]] || continue # not old enough
|
|
1314
|
+
stale+=( "$log" )
|
|
1315
|
+
done
|
|
1316
|
+
remaining=$(( ${#logs} - ${#stale} ))
|
|
1317
|
+
|
|
1318
|
+
if (( ${#stale} )); then
|
|
1319
|
+
print -r -- "$project: ${#stale} log(s) to archive (of ${#logs}, keeping newest $hot)"
|
|
1320
|
+
if (( apply )); then
|
|
1321
|
+
mkdir -p "$pdir/_archive"
|
|
1322
|
+
for log in "${stale[@]}"; do
|
|
1323
|
+
mv -- "$log" "$pdir/_archive/"
|
|
1324
|
+
sidecar="${log:r}.startsha"
|
|
1325
|
+
[[ -f "$sidecar" ]] && mv -- "$sidecar" "$pdir/_archive/"
|
|
1326
|
+
done
|
|
1327
|
+
fi
|
|
1328
|
+
(( total_archived += ${#stale} ))
|
|
1329
|
+
fi
|
|
1330
|
+
|
|
1331
|
+
# Consolidation candidate: many still-hot logs are unconsolidated
|
|
1332
|
+
# knowledge -- a nudge to distil them into a durable lesson.
|
|
1333
|
+
if (( remaining >= ripe )); then
|
|
1334
|
+
print -r -- "$project: $remaining hot logs -- consolidation candidate (distil into a _lessons/ note)"
|
|
1335
|
+
fi
|
|
1336
|
+
done
|
|
1337
|
+
|
|
1338
|
+
print -r -- "--"
|
|
1339
|
+
if (( apply && total_archived > 0 )); then
|
|
1340
|
+
__ai_mem_vault_backup
|
|
1341
|
+
print -r -- "Archived $total_archived session log(s) -- reversible: in each project's _archive/ and in git history."
|
|
1342
|
+
elif (( total_archived > 0 )); then
|
|
1343
|
+
print -r -- "Would archive $total_archived session log(s). Re-run with --apply to move them."
|
|
1344
|
+
else
|
|
1345
|
+
print -r -- "No session logs old enough to archive."
|
|
1346
|
+
fi
|
|
1347
|
+
|
|
1348
|
+
print -r -- "--"
|
|
1349
|
+
print -r -- "lint:"
|
|
1350
|
+
ai-mem-lint
|
|
1351
|
+
}
|
|
1352
|
+
|
|
1353
|
+
# ai-mem-sleep-schedule: run the bedtime pass on its own, nightly. A scheduled
|
|
1354
|
+
# shell does not source ~/.zshrc, so the job sources the module directly
|
|
1355
|
+
# ($AI_MEM_HOME/ai-mem.zsh) before calling ai-mem-sleep --apply. macOS uses a
|
|
1356
|
+
# launchd LaunchAgent; every other platform uses crontab.
|
|
1357
|
+
#
|
|
1358
|
+
# ai-mem-sleep-schedule print what it would install
|
|
1359
|
+
# ai-mem-sleep-schedule --install [--at HH:MM] install it (default 03:00)
|
|
1360
|
+
# ai-mem-sleep-schedule --uninstall remove it
|
|
1361
|
+
#
|
|
1362
|
+
# Dry run by default -- nothing is scheduled until --install. AI_MEM_LAUNCHAGENTS
|
|
1363
|
+
# overrides where the plist is written (used by the tests).
|
|
1364
|
+
ai-mem-sleep-schedule() {
|
|
1365
|
+
local action="print" at="03:00"
|
|
1366
|
+
while (( $# )); do
|
|
1367
|
+
case "$1" in
|
|
1368
|
+
--install) action="install" ;;
|
|
1369
|
+
--uninstall) action="uninstall" ;;
|
|
1370
|
+
--at) at="${2:-}"; shift ;;
|
|
1371
|
+
*) print -r -- "usage: ai-mem-sleep-schedule [--install|--uninstall] [--at HH:MM]"; return 2 ;;
|
|
1372
|
+
esac
|
|
1373
|
+
shift
|
|
1374
|
+
done
|
|
1375
|
+
if [[ ! "$at" =~ '^([01][0-9]|2[0-3]):[0-5][0-9]$' ]]; then
|
|
1376
|
+
print -r -- "ai-mem-sleep-schedule: --at wants HH:MM (24-hour), got '$at'"
|
|
1377
|
+
return 2
|
|
1378
|
+
fi
|
|
1379
|
+
local hh="${at%%:*}" mm="${at##*:}"
|
|
1380
|
+
local module="$AI_MEM_HOME/ai-mem.zsh"
|
|
1381
|
+
local cmd="source ${(q)module}; ai-mem-sleep --apply"
|
|
1382
|
+
local label="app.ai-mem.sleep"
|
|
1383
|
+
local log="${AI_MEM_ROOT:h}/ai-mem-sleep.log"
|
|
1384
|
+
|
|
1385
|
+
if [[ "$OSTYPE" == darwin* ]]; then
|
|
1386
|
+
local plist="${AI_MEM_LAUNCHAGENTS:-$HOME/Library/LaunchAgents}/${label}.plist"
|
|
1387
|
+
local body='<?xml version="1.0" encoding="UTF-8"?>
|
|
1388
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
1389
|
+
<plist version="1.0"><dict>
|
|
1390
|
+
<key>Label</key><string>'"$label"'</string>
|
|
1391
|
+
<key>ProgramArguments</key>
|
|
1392
|
+
<array><string>/bin/zsh</string><string>-c</string><string>'"$cmd"'</string></array>
|
|
1393
|
+
<key>StartCalendarInterval</key>
|
|
1394
|
+
<dict><key>Hour</key><integer>'"${hh#0}"'</integer><key>Minute</key><integer>'"${mm#0}"'</integer></dict>
|
|
1395
|
+
<key>StandardOutPath</key><string>'"$log"'</string>
|
|
1396
|
+
<key>StandardErrorPath</key><string>'"$log"'</string>
|
|
1397
|
+
</dict></plist>'
|
|
1398
|
+
case "$action" in
|
|
1399
|
+
print)
|
|
1400
|
+
print -r -- "would install a launchd agent (daily at $at):"
|
|
1401
|
+
print -r -- " $plist"
|
|
1402
|
+
print -r -- "$body"
|
|
1403
|
+
print -r -- "--"
|
|
1404
|
+
print -r -- "install with: ai-mem-sleep-schedule --install --at $at" ;;
|
|
1405
|
+
install)
|
|
1406
|
+
mkdir -p "${plist:h}"
|
|
1407
|
+
print -r -- "$body" > "$plist" || { print -r -- "could not write $plist"; return 1 }
|
|
1408
|
+
if [[ -z "${AI_MEM_SLEEP_NO_LAUNCHCTL-}" ]] && command -v launchctl >/dev/null 2>&1; then
|
|
1409
|
+
launchctl unload "$plist" 2>/dev/null
|
|
1410
|
+
if launchctl load "$plist" 2>/dev/null; then
|
|
1411
|
+
print -r -- "scheduled: $label runs daily at $at"
|
|
1412
|
+
else
|
|
1413
|
+
print -r -- "wrote $plist, but launchctl load failed -- it will load at next login"
|
|
1414
|
+
fi
|
|
1415
|
+
else
|
|
1416
|
+
print -r -- "wrote $plist (launchctl not found; loads at next login)"
|
|
1417
|
+
fi ;;
|
|
1418
|
+
uninstall)
|
|
1419
|
+
[[ -z "${AI_MEM_SLEEP_NO_LAUNCHCTL-}" ]] && command -v launchctl >/dev/null 2>&1 && launchctl unload "$plist" 2>/dev/null
|
|
1420
|
+
if [[ -f "$plist" ]]; then
|
|
1421
|
+
rm -f "$plist" && print -r -- "removed $label"
|
|
1422
|
+
else
|
|
1423
|
+
print -r -- "no schedule at $plist"
|
|
1424
|
+
fi ;;
|
|
1425
|
+
esac
|
|
1426
|
+
return 0
|
|
1427
|
+
fi
|
|
1428
|
+
|
|
1429
|
+
# Linux and other: crontab. The comment marker lets --uninstall find its own
|
|
1430
|
+
# line without disturbing anything else the user has scheduled.
|
|
1431
|
+
local cronline="${mm#0} ${hh#0} * * * /bin/zsh -c '${cmd}' >> ${log} 2>&1 # ${label}"
|
|
1432
|
+
case "$action" in
|
|
1433
|
+
print)
|
|
1434
|
+
print -r -- "would add this crontab line (daily at $at):"
|
|
1435
|
+
print -r -- " $cronline"
|
|
1436
|
+
print -r -- "--"
|
|
1437
|
+
print -r -- "install with: ai-mem-sleep-schedule --install --at $at" ;;
|
|
1438
|
+
install)
|
|
1439
|
+
if ! command -v crontab >/dev/null 2>&1; then
|
|
1440
|
+
print -r -- "crontab not found -- add this to your scheduler yourself:"
|
|
1441
|
+
print -r -- " $cronline"
|
|
1442
|
+
return 1
|
|
1443
|
+
fi
|
|
1444
|
+
if { crontab -l 2>/dev/null | grep -v "# ${label}\$"; print -r -- "$cronline"; } | crontab -; then
|
|
1445
|
+
print -r -- "scheduled: crontab entry $label runs daily at $at"
|
|
1446
|
+
else
|
|
1447
|
+
print -r -- "crontab update failed"; return 1
|
|
1448
|
+
fi ;;
|
|
1449
|
+
uninstall)
|
|
1450
|
+
if ! command -v crontab >/dev/null 2>&1; then print -r -- "crontab not found"; return 1; fi
|
|
1451
|
+
crontab -l 2>/dev/null | grep -v "# ${label}\$" | crontab - && print -r -- "removed $label from crontab" ;;
|
|
1452
|
+
esac
|
|
1453
|
+
}
|
|
908
1454
|
# Full-text search across the vault. Plain grep -- no new dependency, and at
|
|
909
1455
|
# vault scale (a personal notes collection, not a codebase) there is no real
|
|
910
1456
|
# performance reason to reach for anything faster. Always resolves one hop
|
|
@@ -938,7 +1484,10 @@ ai-mem-search() {
|
|
|
938
1484
|
# -F: the caller is usually an agent passing a free-text term, where a
|
|
939
1485
|
# stray . ( | must match literally rather than as a regex metacharacter.
|
|
940
1486
|
local raw
|
|
941
|
-
|
|
1487
|
+
# --exclude-dir=_archive: session logs that ai-mem-sleep has retired are out
|
|
1488
|
+
# of the hot search path (their durable content lives on in a lesson). They
|
|
1489
|
+
# stay in git and on disk, just not in a default search.
|
|
1490
|
+
raw="$(grep -rniF --exclude-dir=.git --exclude-dir=_archive --exclude='_session_template.md' --exclude='_project_template.md' --exclude='_lesson_template.md' -- "$term" "$search_root" 2>/dev/null)"
|
|
942
1491
|
|
|
943
1492
|
if [[ -z "$raw" ]]; then
|
|
944
1493
|
print -r -- "no matches for '$term' in $search_root"
|
|
@@ -955,14 +1504,53 @@ ai-mem-search() {
|
|
|
955
1504
|
# -- while the grep that actually searched it took 0.16s. Measuring grep
|
|
956
1505
|
# alone said this command was fast; measuring the command said otherwise.
|
|
957
1506
|
# Interval syntax like {4} is avoided so this works on a stock BSD awk.
|
|
1507
|
+
# Lessons outrank everything else, then newest-first within each group.
|
|
1508
|
+
#
|
|
1509
|
+
# Recency alone was wrong for the case that matters most: hitting a blocker
|
|
1510
|
+
# you have hit before. Session logs outnumber lessons roughly four to one
|
|
1511
|
+
# and are always newer, so they swept the result set -- searching
|
|
1512
|
+
# "snapshot" on a real vault returned 25 session logs and none of the
|
|
1513
|
+
# THREE lessons that answer it exactly. The durable answer was in the
|
|
1514
|
+
# vault and unreachable.
|
|
1515
|
+
#
|
|
1516
|
+
# Sort key is rank + reinforcement + timestamp. "1" for _lessons/, "0"
|
|
1517
|
+
# for everything else, so a lesson with an old date still beats today's
|
|
1518
|
+
# chatter. Within the lessons tier, a lesson RECALLED more often ranks
|
|
1519
|
+
# above a once-seen one -- long-term potentiation for the vault: a pathway
|
|
1520
|
+
# that keeps firing is strengthened. Timestamp breaks ties below that.
|
|
1521
|
+
#
|
|
1522
|
+
# Reinforcement is the count of dated entries the lesson already carries.
|
|
1523
|
+
# ai-lesson appends one every time the same problem is hit again, so the
|
|
1524
|
+
# count is already in the data -- no separate counter to keep in sync (a
|
|
1525
|
+
# shadowed derived field is its own class of bug), and it is read in ONE
|
|
1526
|
+
# grep over the lesson headers, never a per-file scan. Only whole-vault
|
|
1527
|
+
# searches consult it; a project-scoped search sees no lessons anyway.
|
|
1528
|
+
local strengths=""
|
|
1529
|
+
[[ -z "$project" ]] && strengths="$(grep -rc '^## [0-9]' "$AI_MEM_ROOT/_lessons" 2>/dev/null)"
|
|
1530
|
+
|
|
958
1531
|
local sorted
|
|
959
|
-
sorted="$(
|
|
1532
|
+
sorted="$(awk -v root="$search_root/" '
|
|
1533
|
+
# First input: <lesson-file-path>:<entry-count>. Build the strength map.
|
|
1534
|
+
FNR == NR {
|
|
1535
|
+
if (match($0, /:[0-9]+$/)) {
|
|
1536
|
+
str[substr($0, 1, RSTART - 1)] = substr($0, RSTART + 1) + 0
|
|
1537
|
+
}
|
|
1538
|
+
next
|
|
1539
|
+
}
|
|
960
1540
|
{
|
|
961
1541
|
ts = "0000-00-00_00-00-00"
|
|
962
1542
|
if (match($0, /[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]_[0-9][0-9]-[0-9][0-9]-[0-9][0-9]/))
|
|
963
1543
|
ts = substr($0, RSTART, RLENGTH)
|
|
964
|
-
|
|
965
|
-
|
|
1544
|
+
rel = $0
|
|
1545
|
+
sub(root, "", rel)
|
|
1546
|
+
is_lesson = (rel ~ /^_lessons\//)
|
|
1547
|
+
rank = is_lesson ? "1" : "0"
|
|
1548
|
+
# The file this match came from, to look up its reinforcement count.
|
|
1549
|
+
path = $0
|
|
1550
|
+
sub(/:[0-9]+:.*$/, "", path)
|
|
1551
|
+
strength = is_lesson ? sprintf("%03d", str[path] + 0) : "000"
|
|
1552
|
+
print rank "|" strength "|" ts "|" $0
|
|
1553
|
+
}' <(print -r -- "$strengths") <(print -r -- "$raw") | sort -r | cut -d'|' -f4-)"
|
|
966
1554
|
|
|
967
1555
|
# Bound the output. The consumer is normally an agent with a finite
|
|
968
1556
|
# context window, and an unbounded dump is actively harmful there in a
|
|
@@ -973,9 +1561,32 @@ ai-mem-search() {
|
|
|
973
1561
|
# distractor for a model, so fewer, cleaner hits beat more of them.
|
|
974
1562
|
# Count goes FIRST -- the reader needs the size of the result set before
|
|
975
1563
|
# the result set, not after it.
|
|
976
|
-
local total limit
|
|
1564
|
+
local total limit per_file
|
|
977
1565
|
total="$(print -r -- "$raw" | wc -l | tr -d ' ')"
|
|
978
|
-
limit="${AI_MEM_SEARCH_LIMIT:-
|
|
1566
|
+
limit="${AI_MEM_SEARCH_LIMIT:-25}"
|
|
1567
|
+
per_file="${AI_MEM_SEARCH_PER_FILE:-1}"
|
|
1568
|
+
|
|
1569
|
+
# Spend the line budget on distinct sources rather than on whichever note
|
|
1570
|
+
# happens to be chattiest. Measured on a real vault: 'prisma' matched 82
|
|
1571
|
+
# files, but one long session log took 9 of the 40 slots and the output
|
|
1572
|
+
# reached only 14 of them. Capping lines per file reaches roughly three
|
|
1573
|
+
# times as many notes for the same tokens, which is the number that
|
|
1574
|
+
# actually decides whether the answer is in there.
|
|
1575
|
+
#
|
|
1576
|
+
# Applied after the sort, so the lines kept from each file are its newest.
|
|
1577
|
+
# Only engage when the budget actually binds. If every match fits, showing
|
|
1578
|
+
# all of them beats withholding lines from a note that had three -- the
|
|
1579
|
+
# cap exists to allocate a scarce budget, and nothing is scarce here.
|
|
1580
|
+
local spread
|
|
1581
|
+
if (( total > limit )); then
|
|
1582
|
+
spread="$(print -r -- "$sorted" | awk -v cap="$per_file" '
|
|
1583
|
+
{ f = $0; sub(/:[0-9]+:.*$/, "", f); if (++seen[f] <= cap) print }')"
|
|
1584
|
+
else
|
|
1585
|
+
spread="$sorted"
|
|
1586
|
+
fi
|
|
1587
|
+
local files_shown
|
|
1588
|
+
files_shown="$(print -r -- "$spread" | head -n "$limit" | awk '
|
|
1589
|
+
{ f = $0; sub(/:[0-9]+:.*$/, "", f); seen[f] = 1 } END { print length(seen) }')"
|
|
979
1590
|
|
|
980
1591
|
print -r -- "$total match(es) for '$term'"
|
|
981
1592
|
print -r -- "paths below $search_root"
|
|
@@ -983,13 +1594,21 @@ ai-mem-search() {
|
|
|
983
1594
|
# Strip the repeated vault root (a third to a half of all output bytes on
|
|
984
1595
|
# a real vault, with zero information lost -- it is printed once above)
|
|
985
1596
|
# and clamp very long lines, which are common in prose notes.
|
|
986
|
-
|
|
1597
|
+
local shown
|
|
1598
|
+
shown="$(print -r -- "$spread" | head -n "$limit")"
|
|
1599
|
+
print -r -- "$shown" \
|
|
987
1600
|
| sed "s|^${search_root}/||" \
|
|
988
1601
|
| awk '{ if (length($0) > 200) print substr($0, 1, 200) " [...]"; else print }'
|
|
989
1602
|
|
|
990
|
-
|
|
1603
|
+
local shown_n
|
|
1604
|
+
shown_n="$(print -r -- "$shown" | wc -l | tr -d ' ')"
|
|
1605
|
+
if (( total > shown_n )); then
|
|
991
1606
|
print -r -- "--"
|
|
992
|
-
|
|
1607
|
+
# The withheld count stays explicit. "showing 25 of 240" implies it,
|
|
1608
|
+
# but the reader has to be able to tell "hidden" from "absent" without
|
|
1609
|
+
# doing arithmetic -- that distinction is the whole reason this line
|
|
1610
|
+
# exists.
|
|
1611
|
+
print -r -- "showing $shown_n of $total ($(( total - shown_n )) hidden), across $files_shown file(s), at most $per_file line(s) each -- for more lines per file raise AI_MEM_SEARCH_PER_FILE, or narrow with: ai-mem-search '$term' <project>"
|
|
993
1612
|
fi
|
|
994
1613
|
# One hop out: resolve [[wikilinks]] found on matched lines to their
|
|
995
1614
|
# target note (currently: project notes only -- the confirmed use case
|