create-ai-memory 0.18.1 → 0.19.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 CHANGED
@@ -95,7 +95,7 @@ sequenceDiagram
95
95
  Note over Vault: nothing overwritten -- a fresh file every session
96
96
  Note over Sleep: overnight, unattended -- installed by ai-mem-sleep-schedule
97
97
  Sleep->>Vault: archive unfilled logs after a day, logs older than 90d (newest 5 filled kept)
98
- Sleep->>Vault: lint links, flag dangling wikilinks and consolidation candidates
98
+ Sleep->>Vault: lint links, rank links to unwritten notes, flag consolidation candidates
99
99
  Sleep->>Backup: commit + push the archive move, if the vault is git-backed
100
100
  Note over Vault,Sleep: _lessons/ are never archived and never pruned
101
101
  You->>Shell: claude-start (next time)
@@ -316,7 +316,8 @@ optional. Set `AI_MEM_ROOT` in `~/.zshrc` first if you do not want the default
316
316
  | `ai-lesson <topic-slug> <problem> <solution>` | Append a dated Problem/Solution entry to a cross-project `_lessons/<topic-slug>.md` -- decisions, mistakes, solutions worth recalling outside the current project |
317
317
  | `ai-about-me [--review] [harness]` | Fill `_about_me/` by interview. The agent asks one question at a time, asks about real past events before hypotheticals, asks "why does that matter?" to find the reason behind each rule, and writes a note only after you approve its draft. `--review` proposes rules found in 2 or more of your lessons and session logs; you accept, change or reject each one. The harness defaults to the first in `AI_MEM_HARNESSES`. When `_about_me/` holds only placeholders, the next launch tells you once, on stderr, never the agent |
318
318
  | `ai-mem-ingest --source-id <id> --title <t> --date <ISO> [...]` | File a distilled external artifact -- a meeting, a recorded call, a video, a voice note -- as one note in `_transcripts/`. Writes the summary, decisions, action items, attendee `[[wikilinks]]` and a link to the source; it never embeds the raw transcript body, because a full transcript is bulk the agent pays for and rarely needs. Re-ingesting the same `--source-id` updates that one note instead of filing a second copy. `ai-mem-search` covers `_transcripts/` with no extra path |
319
- | `ai-mem-lint [--fix]` | Check the vault's links: orphaned session logs, dangling `previous` links (a link to a log in the project's `_archive/` resolves, as it does in Obsidian), unreferenced project notes, notes missing the `type:` field, and **dangling `[[wikilinks]]`** -- a link pointing at a note that does not exist (a dead edge in the graph), reported but never auto-removed. `--fix` backfills `type:` into session logs written before the field existed |
319
+ | `ai-mem-lint [--fix]` | Check the vault's links: orphaned session logs, dangling `previous` links (a link to a log in the project's `_archive/` resolves, as it does in Obsidian), unreferenced project notes, and notes missing the `type:` field. Links to notes not written yet (`[[Name]]` with no `Name.md`) are **not** errors -- Obsidian creates the note when the link is clicked -- so they get one ranked line instead: `most linked: [[checkout-api]] 17, ...`. A target many notes link to is a missing note worth writing. `[[...]]` in code and `{{placeholders}}` are ignored. `--fix` backfills `type:` into session logs written before the field existed |
320
+ | `ai-mem-recall` | Does the memory get used? Every whole-vault `ai-mem-search` appends the lessons it showed (time, project, slug, query) to `.ai-mem/recall.tsv` in the vault. `ai-mem-recall` reads that log and the dated entries in `_lessons/`: how many lessons searches showed, and for each **repeat** entry (the same problem came back) whether a search showed that lesson in the 24 hours before. A repeat no search showed is a **recall miss**: the answer was in the vault and the agent never saw it. The log holds your search queries and is backed up with the vault |
320
321
  | `ai-mem-search <term> [project]` | Case-insensitive literal search across the vault (or one project's logs). Lessons rank first, and within them by *reinforcement* -- a lesson recalled more often (more dated entries) ranks above a once-seen one -- then by date; archived logs (see `ai-mem-sleep`) are skipped. Paths print relative to a root stated once in the header. Output is capped (`AI_MEM_SEARCH_LIMIT`, default 25) with an explicit `N hidden` notice, because the usual caller is an agent with a finite context window. Also resolves any `[[wikilink]]` on a matched line to its project note -- one hop out along the graph, always on, not a flag to remember |
321
322
  | `ai-mem-sleep [--apply]` | The vault's "bedtime" pass: archive stale session logs, flag consolidation candidates, and lint, in one run. Session logs older than `AI_MEM_SLEEP_DAYS` (default 90) move out of the hot search path -- always keeping the newest `AI_MEM_SLEEP_KEEP` (default 5) filled logs per project -- while `_lessons/` are never touched, because durable failure->fix knowledge is exactly what must survive. An **unfilled** log -- the template with nothing written, plus at most an Auto Session Log that saw no commits and a clean tree -- is archived once it is dated before yesterday, and never takes a KEEP slot: most sessions close without a note, and counting them pushed every real log out of the hot set. Dry-run by default; `--apply` moves logs into each project's `_archive/` (reversible via git) and backs up |
322
323
  | `ai-mem-sleep-schedule [--install\|--uninstall] [--at HH:MM]` | Run the bedtime pass on its own, nightly (default 03:00). macOS installs a launchd agent; other platforms a crontab entry. The scheduled job sets `AI_MEM_ROOT` and sources the module first, since a cron/launchd shell does not read `~/.zshrc`. On macOS, a vault in `~/Documents`, `~/Desktop`, `~/Downloads` or iCloud Drive gets a warning: macOS can deny a launchd job read access there, and the pass then does nothing. Check the job log after its first run. Dry-run by default -- prints exactly what it would install; `--install` schedules it, `--uninstall` removes it |
@@ -554,7 +555,7 @@ away what was hard to learn.
554
555
  | **Strengthen** | A lesson recalled again ranks higher, not just newer -- long-term potentiation for the vault. | `ai-mem-search` ranking |
555
556
  | **Consolidate** | Many session logs about one thing distil into a single durable lesson. | `ai-lesson` |
556
557
  | **Decay** | Old session logs move out of the hot search path into `_archive/`, still on disk and in git. Unfilled logs go after a day. | `ai-mem-sleep` |
557
- | **Prune (detect)** | A dangling `[[wikilink]]` -- a dead edge -- is surfaced, never auto-removed. | `ai-mem-lint` |
558
+ | **Prune (detect)** | Links to notes not written yet are ranked by how many notes link to them, never auto-removed. | `ai-mem-lint` |
558
559
  | **Schedule** | Run the whole pass nightly on its own. | `ai-mem-sleep-schedule` |
559
560
 
560
561
  **One rule is inviolable: a lesson is never pruned.** The failure-to-fix records
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-ai-memory",
3
- "version": "0.18.1",
3
+ "version": "0.19.0",
4
4
  "description": "Persistent, harness-agnostic session memory for AI coding CLIs (Claude Code, Codex, Gemini, Cursor, opencode, omp). One Markdown vault, any harness.",
5
5
  "bin": {
6
6
  "create-ai-memory": "bin/create-ai-memory.js",
package/shell/ai-mem.zsh CHANGED
@@ -1219,6 +1219,22 @@ ai-lesson() {
1219
1219
 
1220
1220
  local project_name lesson_file timestamp
1221
1221
  project_name="$(__ai_mem_current_project)" || return 1
1222
+ # Run from inside the vault, the project resolves to the vault's own repo
1223
+ # or folder name: on a real vault 36 lesson entries were tagged with the
1224
+ # vault repo's name, a project that does not exist. Inside the vault, use
1225
+ # the active session's project, or refuse rather than guess.
1226
+ if [[ -z "${AI_MEM_FORCE_PROJECT:-}" ]]; then
1227
+ local here_root vault_root
1228
+ here_root="$(git rev-parse --show-toplevel 2>/dev/null)"
1229
+ vault_root="$(git -C "$AI_MEM_ROOT" rev-parse --show-toplevel 2>/dev/null)"
1230
+ if [[ "${PWD:A}/" == "${AI_MEM_ROOT:A}/"* || ( -n "$here_root" && "$here_root" == "$vault_root" ) ]]; then
1231
+ if [[ -z "${AI_MEM_ACTIVE_PROJECT:-}" ]]; then
1232
+ echo "ai-lesson: run from inside the memory vault, so the project is unknown. Run it from the project's repo, or set AI_MEM_ACTIVE_PROJECT=<project>." >&2
1233
+ return 1
1234
+ fi
1235
+ project_name="$AI_MEM_ACTIVE_PROJECT"
1236
+ fi
1237
+ fi
1222
1238
  lesson_file="$AI_MEM_ROOT/_lessons/${slug}.md"
1223
1239
  timestamp="$(date '+%Y-%m-%d %H:%M')"
1224
1240
 
@@ -1507,17 +1523,19 @@ ai-mem-lint() {
1507
1523
  fi
1508
1524
 
1509
1525
 
1510
- # Dangling wikilinks: a [[target]] pointing at a note that does not exist --
1511
- # a dead edge in the graph. Detection only: the dead link is surfaced, never
1512
- # auto-removed, because the intended target is unknowable and a note's own
1513
- # prose is the author's to edit. Wikilinks resolve by basename (the same way
1514
- # ai-mem-serve draws them), so the node set is every note's basename. A
1515
- # folder path ([[_idea/x]], [[../dir/x]]) resolves by its last segment.
1526
+ # Links to notes not written yet: a [[target]] with no note of that name.
1527
+ # Obsidian treats these as intentional -- clicking one creates the note --
1528
+ # so they are reported as a ranked summary, NOT counted as issues. A target
1529
+ # linked from many notes is a missing hub worth writing; a target linked
1530
+ # once is a breadcrumb. On a real vault 206 of 220 lint issues were these
1531
+ # (people in meeting transcripts, projects without a note), and listing
1532
+ # each one as an error buried the 14 that were real. Wikilinks resolve by
1533
+ # basename (the same way ai-mem-serve draws them); a folder path
1534
+ # ([[_idea/x]], [[../dir/x]]) resolves by its last segment.
1516
1535
  #
1517
- # Not links, so never reported (28 of 280 findings on a real vault were
1518
- # these): text inside `inline code` or a fenced code block, where a shell
1519
- # test such as [[ -n $X ]] looks like a wikilink, and {{placeholder}}
1520
- # targets in the note templates.
1536
+ # Not links, so never counted: text inside `inline code` or a fenced code
1537
+ # block, where a shell test such as [[ -n $X ]] looks like a wikilink, and
1538
+ # {{placeholder}} targets in the note templates.
1521
1539
  #
1522
1540
  # One awk pass over the vault, never a process per file -- a per-file scan
1523
1541
  # is the 25s antipattern ai-mem-search documents. Scope is the vault root
@@ -1529,10 +1547,10 @@ ai-mem-lint() {
1529
1547
  known_file="$(mktemp)"
1530
1548
  find "$vault" -name '*.md' -not -path '*/.git/*' 2>/dev/null \
1531
1549
  | awk '{ sub(/.*\//, ""); sub(/\.md$/, ""); print }' > "$known_file"
1532
- local -a dangling
1533
- dangling=( ${(f)"$(find "$vault" -name '*.md' -not -path '*/.git/*' -exec awk -v kf="$known_file" '
1550
+ local unwritten
1551
+ unwritten="$(find "$vault" -name '*.md' -not -path '*/.git/*' -exec awk -v kf="$known_file" '
1534
1552
  FILENAME == kf { known[$0] = 1; next }
1535
- FNR == 1 { fence = 0; src = FILENAME; sub(/.*\//, "", src) }
1553
+ FNR == 1 { fence = 0 }
1536
1554
  /^[ \t]*(```|~~~)/ { fence = !fence; next }
1537
1555
  fence { next }
1538
1556
  {
@@ -1546,15 +1564,19 @@ ai-mem-lint() {
1546
1564
  if (t == "" || index(t, "{{")) continue
1547
1565
  b = t; sub(/.*\//, "", b)
1548
1566
  if ((t in known) || (b in known)) continue
1549
- print src " -> [[" t "]]"
1567
+ print t
1550
1568
  }
1551
- }' "$known_file" {} + 2>/dev/null)"} )
1569
+ }' "$known_file" {} + 2>/dev/null)"
1552
1570
  rm -f "$known_file"
1553
- local line
1554
- if (( $#dangling )); then
1555
- print -r -- "$#dangling dangling wikilink(s) -- a [[link]] that resolves to no note:"
1556
- for line in $dangling; do print -r -- " $line"; done
1557
- (( issues += $#dangling ))
1571
+ if [[ -n "$unwritten" ]]; then
1572
+ # Most-linked first, ties by name; the top 10 only, with the total.
1573
+ local n_links n_targets top
1574
+ n_links="$(print -r -- "$unwritten" | wc -l | tr -d ' ')"
1575
+ n_targets="$(print -r -- "$unwritten" | sort -u | wc -l | tr -d ' ')"
1576
+ top="$(print -r -- "$unwritten" | sort | uniq -c | sort -k1,1nr -k2 | head -10 \
1577
+ | awk '{ c = $1; sub(/^ *[0-9]+ /, ""); printf "%s[[%s]] %s", (NR > 1 ? ", " : ""), $0, c }')"
1578
+ print -r -- "note: $n_links link(s) to $n_targets note(s) not written yet. Not errors -- Obsidian creates a note when its link is clicked. Write the most-linked first; find where one is used with: ai-mem-search '[[name]]'"
1579
+ print -r -- " most linked: $top"
1558
1580
  fi
1559
1581
  if (( issues == 0 )); then
1560
1582
  print -r -- "ok: vault links are clean"
@@ -1855,8 +1877,9 @@ ai-mem-search() {
1855
1877
  local raw
1856
1878
  # --exclude-dir=_archive: session logs that ai-mem-sleep has retired are out
1857
1879
  # of the hot search path (their durable content lives on in a lesson). They
1858
- # stay in git and on disk, just not in a default search.
1859
- 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)"
1880
+ # stay in git and on disk, just not in a default search. .ai-mem holds the
1881
+ # recall log (see __ai_mem_log_recall), whose queries must not match.
1882
+ raw="$(grep -rniF --exclude-dir=.git --exclude-dir=_archive --exclude-dir=.ai-mem --exclude='_session_template.md' --exclude='_project_template.md' --exclude='_lesson_template.md' -- "$term" "$search_root" 2>/dev/null)"
1860
1883
 
1861
1884
  # Exact phrase first, then each word. A multi-word term used to be one
1862
1885
  # literal phrase, so word order decided whether anything came back:
@@ -1873,7 +1896,7 @@ ai-mem-search() {
1873
1896
  local -a patterns
1874
1897
  local w
1875
1898
  for w in $words; do patterns+=(-e "$w"); done
1876
- raw="$(grep -rniF --exclude-dir=.git --exclude-dir=_archive --exclude='_session_template.md' --exclude='_project_template.md' --exclude='_lesson_template.md' "${patterns[@]}" "$search_root" 2>/dev/null)"
1899
+ raw="$(grep -rniF --exclude-dir=.git --exclude-dir=_archive --exclude-dir=.ai-mem --exclude='_session_template.md' --exclude='_project_template.md' --exclude='_lesson_template.md' "${patterns[@]}" "$search_root" 2>/dev/null)"
1877
1900
  else
1878
1901
  words=()
1879
1902
  fi
@@ -2001,6 +2024,7 @@ ai-mem-search() {
2001
2024
  print -r -- "$shown" \
2002
2025
  | sed "s|^${search_root}/||" \
2003
2026
  | awk '{ if (length($0) > 200) print substr($0, 1, 200) " [...]"; else print }'
2027
+ [[ -z "$project" ]] && __ai_mem_log_recall "$term" "$shown"
2004
2028
 
2005
2029
  local shown_n
2006
2030
  shown_n="$(print -r -- "$shown" | wc -l | tr -d ' ')"
@@ -2113,3 +2137,88 @@ __ai_mem_search_rank_words() {
2113
2137
  }
2114
2138
  }' <(print -r -- "$strengths") <(print -r -- "$raw") | sort -r | cut -d'|' -f7-
2115
2139
  }
2140
+
2141
+ # Append one line per lesson that a whole-vault search SHOWED to the caller
2142
+ # (not every match: a hidden hit was not recalled). ai-mem-recall reads this to
2143
+ # answer the question the vault could not answer before: when a problem came
2144
+ # back, had the agent found the lesson for it? Columns, tab-separated:
2145
+ # local time (YYYY-MM-DD HH:MM), epoch seconds, active project, slug, query.
2146
+ # Best-effort: a write failure never fails the search. The file lives in
2147
+ # .ai-mem/, which search, lint and the graph viewer all skip.
2148
+ # Args: <query> <shown result lines, absolute paths>.
2149
+ __ai_mem_log_recall() {
2150
+ local query="$1" shown="$2" slugs
2151
+ slugs="$(print -r -- "$shown" | awk -v pre="$AI_MEM_ROOT/_lessons/" '
2152
+ index($0, pre) == 1 { s = substr($0, length(pre) + 1); sub(/\.md:[0-9]+:.*/, "", s)
2153
+ if (s !~ /\// && !(s in seen)) { seen[s] = 1; print s } }')"
2154
+ [[ -n "$slugs" ]] || return 0
2155
+ zmodload zsh/datetime 2>/dev/null
2156
+ local dir="$AI_MEM_ROOT/.ai-mem" now epoch slug
2157
+ now="$(strftime '%Y-%m-%d %H:%M' "$EPOCHSECONDS")"; epoch="$EPOCHSECONDS"
2158
+ query="${${query//$'\t'/ }//$'\n'/ }"
2159
+ mkdir -p "$dir" 2>/dev/null || return 0
2160
+ for slug in ${(f)slugs}; do
2161
+ print -r -- "$now"$'\t'"$epoch"$'\t'"${AI_MEM_ACTIVE_PROJECT:-}"$'\t'"$slug"$'\t'"$query"
2162
+ done >> "$dir/recall.tsv" 2>/dev/null
2163
+ return 0
2164
+ }
2165
+
2166
+ # ai-mem-recall: does the memory get used? Reads the recall log written by
2167
+ # ai-mem-search and the dated entries in _lessons/, from the first logged
2168
+ # search onward, and reports:
2169
+ # - how many lessons searches surfaced, of how many exist;
2170
+ # - each REPEAT entry (a second or later entry in an existing lesson, i.e.
2171
+ # the same problem came back): was that lesson shown by a search in the
2172
+ # 24 hours before the entry was written? "found" means the memory did its
2173
+ # job of surfacing the answer. "not found" means the answer was in the
2174
+ # vault but the agent never saw it -- a recall miss, the number to drive
2175
+ # down. Whether a found lesson then helped cannot be read from files.
2176
+ # ponytail: a fixed 24h window stands in for "the same session"; a session
2177
+ # id in both the log and the lesson entry is the upgrade if the window misleads.
2178
+ ai-mem-recall() {
2179
+ __ai_mem_paths
2180
+ zmodload zsh/datetime 2>/dev/null
2181
+ local log="$AI_MEM_ROOT/.ai-mem/recall.tsv"
2182
+ if [[ ! -s "$log" ]]; then
2183
+ print -r -- "ai-mem-recall: no searches logged yet. ai-mem-search records each lesson it shows; run this again after some sessions."
2184
+ return 0
2185
+ fi
2186
+ local start
2187
+ start="$(head -1 "$log" | cut -f1)"
2188
+ # Dated entries since logging began: <slug> <entry index> <YYYY-MM-DD HH:MM>.
2189
+ local -a rows
2190
+ rows=( ${(f)"$(find "$AI_MEM_ROOT/_lessons" -maxdepth 1 -name '*.md' ! -name '_lesson_template.md' -exec awk -v start="$start" '
2191
+ FNR == 1 { n = 0; s = FILENAME; sub(/.*\//, "", s); sub(/\.md$/, "", s) }
2192
+ /^## [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]/ {
2193
+ n++; t = substr($0, 4, 16); if (t >= start) print s "\t" n "\t" t; next }
2194
+ /^## [0-9]/ { n++ }
2195
+ ' {} + 2>/dev/null)"} )
2196
+ local row t
2197
+ local entries=""
2198
+ for row in $rows; do
2199
+ t="${row##*$'\t'}"
2200
+ entries+="$row"$'\t'"$(strftime -r '%Y-%m-%d %H:%M' "$t" 2>/dev/null || print 0)"$'\n'
2201
+ done
2202
+ local total
2203
+ total="$(find "$AI_MEM_ROOT/_lessons" -maxdepth 1 -name '*.md' ! -name '_lesson_template.md' | wc -l | tr -d ' ')"
2204
+ awk -F'\t' -v total="$total" -v start="$start" '
2205
+ FNR == NR { if ($4 != "") { surfaced[$4] = 1; at[$4, ++k[$4]] = $2; searches[$2 "\t" $5] = 1 } next }
2206
+ $1 == "" { next }
2207
+ $2 == 1 { new++; next }
2208
+ {
2209
+ rep++; hit = 0
2210
+ # +60: entry times are whole minutes, so a search in the same
2211
+ # minute can carry a later epoch than the entry heading.
2212
+ for (i = 1; i <= k[$1]; i++) if (at[$1, i] <= $4 + 60 && at[$1, i] >= $4 - 86400) { hit = 1; break }
2213
+ if (hit) found++; else { missed++; miss = miss "\n " $1 " (" $3 ")" }
2214
+ }
2215
+ END {
2216
+ ns = 0; for (s in searches) ns++
2217
+ nl = 0; for (s in surfaced) nl++
2218
+ print "recall since " start " (first logged search)"
2219
+ print " searches that showed a lesson: " ns "; lessons shown: " nl " of " total
2220
+ print " lesson entries written since: " new + rep " (" new + 0 " new lesson(s), " rep + 0 " repeat(s) of an existing lesson)"
2221
+ if (rep) print " repeats found by a search in the 24h before: " found + 0 " of " rep "; not found (recall misses): " missed + 0
2222
+ if (missed) print "recall misses -- the lesson existed but no search showed it:" miss
2223
+ }' "$log" <(print -rn -- "$entries")
2224
+ }