create-ai-memory 0.16.0 → 0.16.1

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
@@ -817,6 +817,11 @@ a standing brief (read context first, search before solving, write back after)
817
817
  -- without it a model has no reason to suspect a memory exists. Tool
818
818
  descriptions stay terse because schemas are re-sent every turn.
819
819
 
820
+ A GUI session log is created by the first write, not by a read. Each MCP
821
+ server keeps one log path for its lifetime; `add_note` (or an agent that
822
+ writes the session outcome) creates the file there. A conversation that only
823
+ reads context leaves no empty note in the vault.
824
+
820
825
  **Do not register this for a terminal agent.** Claude Code, Codex, Gemini and
821
826
  opencode have a shell and should call `ai-mem-search` directly.
822
827
 
package/bin/ai-mem-mcp.js CHANGED
@@ -43,6 +43,22 @@ const VERSION = (() => {
43
43
  }
44
44
  })();
45
45
 
46
+ /**
47
+ * The global session log this server writes to, pinned for its lifetime.
48
+ *
49
+ * Every zsh call is a fresh process, so the path the module picks on
50
+ * `initialize` would be lost by the next call. Pinning it keeps one server
51
+ * on one log. The file itself is created by the first write, so a
52
+ * conversation that only reads leaves no empty note in the vault.
53
+ */
54
+ let sessionLog = '';
55
+
56
+ /** Remember the "Active session log" path from an ai-context result. */
57
+ function pinSessionLog(text) {
58
+ const m = /^- Active session log: (.+?\.md)(?: -- |$)/m.exec(text || '');
59
+ if (m) sessionLog = m[1];
60
+ }
61
+
46
62
  /**
47
63
  * Run a zsh snippet with the module sourced, and return its output.
48
64
  *
@@ -65,6 +81,9 @@ function zsh(snippet) {
65
81
  AI_MEM_FORCE_PROJECT: GUI_PROJECT,
66
82
  AI_MEM_GLOBALIZE_ROOT: GUI_ROOT,
67
83
  AI_MEM_GLOBAL_SESSION: '1',
84
+ // Always set, so a project log exported by a parent *-start shell is
85
+ // never mistaken for this GUI session.
86
+ AI_MEM_ACTIVE_SESSION_LOG: sessionLog,
68
87
  },
69
88
  maxBuffer: 8 * 1024 * 1024,
70
89
  });
@@ -220,8 +239,12 @@ function callTool(name, args = {}) {
220
239
  switch (name) {
221
240
  case 'search_memory':
222
241
  return zsh(`ai-mem-search ${q(args.term)} ${args.project ? q(args.project) : ''}`);
223
- case 'get_context':
224
- return zsh(`ai-context ${q(args.project || GUI_PROJECT)}`);
242
+ case 'get_context': {
243
+ const project = args.project || GUI_PROJECT;
244
+ const result = zsh(`ai-context ${q(project)}`);
245
+ if (result.ok && project === GUI_PROJECT) pinSessionLog(result.text);
246
+ return result;
247
+ }
225
248
  // ai-note and ai-lesson both push the vault themselves, so a GUI write is
226
249
  // committed and backed up exactly like a terminal one -- no separate step
227
250
  // for the model to forget.
@@ -267,9 +290,10 @@ function handle(req) {
267
290
  if (id === undefined) return;
268
291
 
269
292
  if (method === 'initialize') {
270
- // Initialize is the GUI equivalent of a CLI launcher: create the globalized
271
- // session log even if the model never remembers to call a tool first.
272
- zsh(`ai-context ${q(GUI_PROJECT)} >/dev/null && print -r -- primed`);
293
+ // Initialize is the GUI equivalent of a CLI launcher: load the context and
294
+ // pin this server's global session log even if the model never calls a
295
+ // tool first. The log file is created by the first write, not here.
296
+ pinSessionLog(zsh(`ai-context ${q(GUI_PROJECT)}`).text);
273
297
  return send({
274
298
  jsonrpc: '2.0',
275
299
  id,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-ai-memory",
3
- "version": "0.16.0",
3
+ "version": "0.16.1",
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
@@ -356,7 +356,7 @@ __ai_mem_latest_session_log() {
356
356
  latest="$(awk '
357
357
  FNR == 1 { fm = ($0 == "---"); if (fm) next }
358
358
  fm { if ($0 == "---") fm = 0; next }
359
- /^[[:space:]]*$/ || $0 == "# Session Outcome" || /^\* \*\*[^*]+:\*\*[[:space:]]*\[.*\]$/ { next }
359
+ /^[[:space:]]*$/ || $0 == "# Session Outcome" || $0 == "# Global Session Notes" || /^\* \*\*[^*]+:\*\*[[:space:]]*\[.*\]$/ { next }
360
360
  { print FILENAME; exit }
361
361
  ' "${(@f)logs}" 2>/dev/null)" || true
362
362
  [[ -n "$latest" ]] || latest="${logs%%$'\n'*}"
@@ -472,30 +472,38 @@ __ai_mem_prepare_session() {
472
472
  "$AI_MEM_PROJECT_DIR/_project_template.md" > "$project_note"
473
473
  fi
474
474
 
475
- # A blank note with this second's timestamp belongs to a separate session.
476
- # Wait for the next timestamp instead of overwriting its previous-session
477
- # link. A note with Live Notes is the active GUI/CLI session and must be
478
- # reused.
479
- if [[ -f "$session_note" ]] && ! grep -q '^### Live Notes' "$session_note" 2>/dev/null; then
480
- while [[ -f "$session_note" ]]; do
481
- sleep 1
482
- session_stamp="$(date +%Y-%m-%d_%H-%M-%S)"
483
- session_note="$project_session_dir/${project_name}-${session_stamp}.md"
484
- done
485
- fi
486
-
487
475
  local prev_link=""
488
476
  if [[ -n "$previous_session_note" ]]; then
489
477
  prev_link="[[${previous_session_note:t:r}]]"
490
478
  fi
491
479
 
492
- # Context reads can follow a GUI write in the same second. Reusing an
493
- # existing timestamped note preserves Live Notes and any other append.
494
- if [[ ! -f "$session_note" ]]; then
495
- if (( global_session )); then
496
- local session_date="$(date +%Y-%m-%d)"
497
- print -rl -- "---" "type: ai-global-session" "date: $session_date" "previous: \"$prev_link\"" "---" "" "# Global Session Notes" > "$session_note"
498
- else
480
+ if (( global_session )); then
481
+ # GUI clients start the MCP server (initialize reads context) and then
482
+ # call get_context before almost every answer. Writing a skeleton on
483
+ # each read left ~290 empty notes in a week, chained into a strand of
484
+ # junk in the graph. So a global log is only a PATH here: the MCP
485
+ # server pins it in AI_MEM_ACTIVE_SESSION_LOG for its lifetime, and
486
+ # the file is created by the first write (ai-note, or an agent that
487
+ # writes the session outcome). A read-only conversation leaves nothing.
488
+ local active="${AI_MEM_ACTIVE_SESSION_LOG:-}"
489
+ if [[ "$active" == "$project_session_dir/${project_name}-$(date +%Y-%m-%d)_"*.md ]]; then
490
+ session_note="$active"
491
+ fi
492
+ else
493
+ # A blank note with this second's timestamp belongs to a separate
494
+ # session. Wait for the next timestamp instead of overwriting its
495
+ # previous-session link. A note with Live Notes is the active CLI
496
+ # session and must be reused.
497
+ if [[ -f "$session_note" ]] && ! grep -q '^### Live Notes' "$session_note" 2>/dev/null; then
498
+ while [[ -f "$session_note" ]]; do
499
+ sleep 1
500
+ session_stamp="$(date +%Y-%m-%d_%H-%M-%S)"
501
+ session_note="$project_session_dir/${project_name}-${session_stamp}.md"
502
+ done
503
+ fi
504
+ # Context reads can follow a write in the same second. Reusing an
505
+ # existing timestamped note preserves Live Notes and any other append.
506
+ if [[ ! -f "$session_note" ]]; then
499
507
  SESSION_DATE="$(date +%Y-%m-%d)" PROJECT_NAME="$project_name" PREV_LINK="$prev_link" perl -0pe 's/\{\{date\}\}/$ENV{SESSION_DATE}/g; s/\{\{project_name\}\}/$ENV{PROJECT_NAME}/g; s/\{\{previous_session_link\}\}/$ENV{PREV_LINK}/g' \
500
508
  "$AI_MEM_SESSION_DIR/_session_template.md" > "$session_note"
501
509
  fi
@@ -743,6 +751,13 @@ __ai_mem_context_prompt() {
743
751
  fi
744
752
  local project_label="$project_note"
745
753
  [[ -n "$project_label" ]] || project_label="none (GUI/MCP global memory; no repository context)"
754
+ # A global (GUI/MCP) log is created on the first write, so its path may
755
+ # not exist yet. Say so; an agent that tries to read it would otherwise
756
+ # take "not found" for an error.
757
+ local session_state=""
758
+ if [[ -n "$session_note" && ! -e "$session_note" ]]; then
759
+ session_state=" -- not created yet; the first ai-note or session-log write creates it at this path"
760
+ fi
746
761
  # Hard "no"s go first: models follow early instructions most reliably,
747
762
  # and the note cap truncates from the end. The index sits with the other
748
763
  # pointers. Each block carries its own trailing newline so an absent
@@ -763,7 +778,7 @@ $(__ai_mem_note_contents "$AI_MEM_STANDARDS")
763
778
 
764
779
  ${about_index}- Project context: $project_label$project_state
765
780
  $previous_session_block
766
- - Active session log: $session_note
781
+ - Active session log: $session_note$session_state
767
782
  $(__ai_mem_lesson_index)
768
783
 
769
784
  Use the Obsidian vault as the persistent memory layer.
@@ -1045,6 +1060,19 @@ __ai_mem_current_project() {
1045
1060
  __ai_mem_resolve_project
1046
1061
  }
1047
1062
 
1063
+ # Create a global (GUI/MCP) session log at $1 if it does not exist yet. Global
1064
+ # logs are created lazily, on the first write, so a read-only conversation
1065
+ # leaves no empty note behind. `previous:` links the newest log with content.
1066
+ __ai_mem_create_global_log() {
1067
+ local session_note="$1" project_name="$2" previous prev_link=""
1068
+ [[ -f "$session_note" ]] && return 0
1069
+ __ai_mem_guard "$session_note" || return 1
1070
+ previous="$(__ai_mem_latest_session_log "$project_name")" || return 1
1071
+ [[ -n "$previous" ]] && prev_link="[[${previous:t:r}]]"
1072
+ mkdir -p "${session_note:h}"
1073
+ print -rl -- "---" "type: ai-global-session" "date: $(date +%Y-%m-%d)" "previous: \"$prev_link\"" "---" "" "# Global Session Notes" > "$session_note"
1074
+ }
1075
+
1048
1076
  __ai_mem_today_session_log() {
1049
1077
  __ai_mem_paths
1050
1078
  local project_name="${1:-}"
@@ -1059,10 +1087,18 @@ __ai_mem_today_session_log() {
1059
1087
  local project_session_dir
1060
1088
  project_session_dir="$(__ai_mem_project_session_dir "$project_name")"
1061
1089
 
1062
- if [[ -n "${AI_MEM_ACTIVE_SESSION_LOG:-}" && -f "$AI_MEM_ACTIVE_SESSION_LOG" ]]; then
1090
+ local global_session=0
1091
+ [[ "${AI_MEM_GLOBAL_SESSION:-}" == "1" ]] && global_session=1
1092
+
1093
+ # A global session's path is pinned by the MCP server before the file
1094
+ # exists; the first write creates it there.
1095
+ if [[ -n "${AI_MEM_ACTIVE_SESSION_LOG:-}" ]] && { (( global_session )) || [[ -f "$AI_MEM_ACTIVE_SESSION_LOG" ]]; }; then
1063
1096
  case "$AI_MEM_ACTIVE_SESSION_LOG" in
1064
1097
  "$AI_MEM_ROOT"|"$AI_MEM_ROOT"/*)
1065
- if [[ "$AI_MEM_ACTIVE_SESSION_LOG" == "$project_session_dir/${project_name}-${today}_"* ]]; then
1098
+ if [[ "$AI_MEM_ACTIVE_SESSION_LOG" == "$project_session_dir/${project_name}-${today}_"*.md ]]; then
1099
+ if (( global_session )); then
1100
+ __ai_mem_create_global_log "$AI_MEM_ACTIVE_SESSION_LOG" "$project_name" || return 1
1101
+ fi
1066
1102
  print -r -- "$AI_MEM_ACTIVE_SESSION_LOG"
1067
1103
  return 0
1068
1104
  fi
@@ -1083,6 +1119,11 @@ __ai_mem_today_session_log() {
1083
1119
 
1084
1120
  local session_note="$project_session_dir/${project_name}-${today}_$(date +%H-%M-%S).md"
1085
1121
  __ai_mem_guard "$session_note" || return 1
1122
+ if (( global_session )); then
1123
+ __ai_mem_create_global_log "$session_note" "$project_name" || return 1
1124
+ print -r -- "$session_note"
1125
+ return 0
1126
+ fi
1086
1127
  mkdir -p "$project_session_dir"
1087
1128
  SESSION_DATE="$today" PROJECT_NAME="$project_name" perl -0pe 's/\{\{date\}\}/$ENV{SESSION_DATE}/g; s/\{\{project_name\}\}/$ENV{PROJECT_NAME}/g' \
1088
1129
  "$AI_MEM_SESSION_DIR/_session_template.md" > "$session_note"