create-ai-memory 0.14.0 → 0.14.2

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.
@@ -82,10 +82,45 @@ function frontmatter(text) {
82
82
  return out;
83
83
  }
84
84
 
85
- /** First H1, else the filename -- what a human would call this note. */
86
- function titleOf(text, path) {
87
- const h1 = text.match(/^#\s+(.+)$/m);
88
- return (h1 && h1[1].trim()) || basename(path, '.md');
85
+ /** The note's first H1, or null. */
86
+ function h1Of(text) {
87
+ const m = text.match(/^#\s+(.+)$/m);
88
+ return m ? m[1].trim() : null;
89
+ }
90
+
91
+ /**
92
+ * What to call a note on the graph.
93
+ *
94
+ * The obvious answer -- its first H1 -- is wrong here, because notes are
95
+ * created from templates and inherit the template's heading. On a real vault
96
+ * that made 354 notes read "Session Outcome" and 34 read "Project Snapshot":
97
+ * every project and every log sharing two labels between them, which is worse
98
+ * than no labels at all.
99
+ *
100
+ * So an H1 is only a title if it is *this note's* H1. One shared by several
101
+ * notes is boilerplate by definition, and the rule needs no list of known
102
+ * template headings to maintain -- it corrects itself for any template,
103
+ * including ones a user writes.
104
+ *
105
+ * @param {string} text the note
106
+ * @param {string} path its path
107
+ * @param {object} fm parsed frontmatter
108
+ * @param {Map} h1Counts how many notes share each H1
109
+ */
110
+ function titleOf(text, path, fm, h1Counts) {
111
+ if (fm.title) return fm.title;
112
+ if (fm.project_name) return fm.project_name;
113
+
114
+ const h1 = h1Of(text);
115
+ if (h1 && h1Counts.get(h1) === 1) return h1;
116
+
117
+ // Fall back to the filename, which is always specific. A session log's name
118
+ // is <project>-<date>_<time>, and the project is already obvious from the
119
+ // node it is attached to -- so show the moment, which is the part that
120
+ // distinguishes it from its forty siblings.
121
+ const name = basename(path, '.md');
122
+ const stamp = name.match(/(\d{4}-\d{2}-\d{2})_(\d{2})-(\d{2})-\d{2}$/);
123
+ return stamp ? stamp[1] + ' ' + stamp[2] + ':' + stamp[3] : name;
89
124
  }
90
125
 
91
126
  const LINK_RE = /\[\[([^\]|#]+)/g;
@@ -103,6 +138,11 @@ function buildGraph() {
103
138
  const nodes = [];
104
139
  const byBasename = new Map();
105
140
 
141
+ // Read once, keep the text: the vault is a few megabytes of markdown, and
142
+ // reading every file twice to count headings first would double the work
143
+ // for no reason.
144
+ const read = [];
145
+ const h1Counts = new Map();
106
146
  for (const f of files) {
107
147
  let text;
108
148
  try {
@@ -110,10 +150,16 @@ function buildGraph() {
110
150
  } catch {
111
151
  continue;
112
152
  }
153
+ read.push([f, text]);
154
+ const h = h1Of(text);
155
+ if (h) h1Counts.set(h, (h1Counts.get(h) || 0) + 1);
156
+ }
157
+
158
+ for (const [f, text] of read) {
113
159
  const fm = frontmatter(text);
114
160
  const node = {
115
161
  id: relative(VAULT, f),
116
- title: fm.title || titleOf(text, f),
162
+ title: titleOf(text, f, fm, h1Counts),
117
163
  type: fm.type || 'untyped',
118
164
  tags: Array.isArray(fm.tags) ? fm.tags : fm.tags ? [fm.tags] : [],
119
165
  description: fm.description || '',
@@ -225,16 +271,38 @@ server.on('error', (err) => {
225
271
  }
226
272
  const url = 'http://127.0.0.1:' + PORT;
227
273
  http.get(url + '/api/graph', (res) => {
228
- if (res.statusCode !== 200) return bail();
229
- res.resume();
230
- console.log('ai-mem-serve: already running');
231
- console.log(' ' + url);
232
- openBrowser(url);
233
- process.exit(0);
234
- }).on('error', bail);
235
-
236
- function bail() {
237
- console.error('ai-mem-serve: port ' + PORT + ' is in use by something else. Pass a different port.');
274
+ if (res.statusCode !== 200) return bail('something else');
275
+ let body = '';
276
+ res.setEncoding('utf8');
277
+ res.on('data', (c) => { body += c; });
278
+ res.on('end', () => {
279
+ // Being an ai-mem-serve is not enough -- it has to be serving THIS
280
+ // vault. A leftover server from a test run or another vault answers
281
+ // /api/graph perfectly well, and attaching to it silently shows the
282
+ // wrong notes while reporting success. That is the failure this whole
283
+ // tool exists to avoid, so compare the vault it names.
284
+ let serving;
285
+ try {
286
+ serving = JSON.parse(body).vault;
287
+ } catch {
288
+ return bail('something else');
289
+ }
290
+ if (serving !== VAULT) {
291
+ console.error('ai-mem-serve: port ' + PORT + ' is already serving a different vault:');
292
+ console.error(' running: ' + serving);
293
+ console.error(' wanted: ' + VAULT);
294
+ console.error('Stop that server, or pass another port: ai-mem-serve ' + (PORT + 1));
295
+ process.exit(1);
296
+ }
297
+ console.log('ai-mem-serve: already running');
298
+ console.log(' ' + url);
299
+ openBrowser(url);
300
+ process.exit(0);
301
+ });
302
+ }).on('error', () => bail('something else'));
303
+
304
+ function bail(what) {
305
+ console.error('ai-mem-serve: port ' + PORT + ' is in use by ' + what + '. Pass a different port.');
238
306
  process.exit(1);
239
307
  }
240
308
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-ai-memory",
3
- "version": "0.14.0",
3
+ "version": "0.14.2",
4
4
  "description": "Persistent, agent-agnostic session memory for AI coding CLIs (Claude Code, Codex, Gemini, Cursor, opencode). One Markdown vault, any agent.",
5
5
  "bin": {
6
6
  "create-ai-memory": "bin/create-ai-memory.js",
package/shell/ai-mem.zsh CHANGED
@@ -279,6 +279,31 @@ __ai_mem_ensure_vault() {
279
279
  cp "$src" "$dst"
280
280
  fi
281
281
  done
282
+
283
+ # Seeding only fills in a MISSING file, which is right -- these are the
284
+ # user's notes and templates, and an upgrade must not overwrite edits they
285
+ # made. But that leaves an existing vault on whatever templates it had
286
+ # when it was created: `npm update` never touches them, and neither does
287
+ # install.sh. The session log template is the one where that silently
288
+ # matters, because every future log is copied from it -- a template with
289
+ # no `type:` rebuilds a backlog of non-conforming notes one session at a
290
+ # time, and nothing surfaces it.
291
+ #
292
+ # So repair exactly that one field, additively, and leave everything else
293
+ # in the file alone. This runs on session prep, so an existing install
294
+ # heals on its next launch rather than waiting for someone to think to run
295
+ # ai-mem-lint --fix.
296
+ local tmpl="$AI_MEM_SESSION_DIR/_session_template.md"
297
+ if [[ -f "$tmpl" ]] && ! grep -qm1 '^type: ' "$tmpl"; then
298
+ __ai_mem_guard "$tmpl" || return 1
299
+ local tmp="$tmpl.aimem-upgrade"
300
+ if [[ "$(head -1 "$tmpl")" == "---" ]]; then
301
+ { head -1 "$tmpl"; print -r -- "type: ai-session-log"; tail -n +2 "$tmpl"; } > "$tmp"
302
+ else
303
+ { print -rl -- "---" "type: ai-session-log" "---" ""; cat "$tmpl"; } > "$tmp"
304
+ fi
305
+ mv "$tmp" "$tmpl"
306
+ fi
282
307
  }
283
308
 
284
309
  __ai_mem_prepare_session() {
@@ -825,6 +850,30 @@ ai-mem-lint() {
825
850
  fi
826
851
  done < <(find "$AI_MEM_PROJECT_DIR" -maxdepth 1 -name "*.md" -print0 2>/dev/null)
827
852
 
853
+ # The template is skipped by the loop above, and rightly -- it is not a
854
+ # note. But it is what every future log is copied from, so a template
855
+ # without `type:` quietly re-creates the whole backlog one session at a
856
+ # time. An existing install keeps its own copy of the template and never
857
+ # picks up a newer one, so fixing the shipped file is not enough.
858
+ local tmpl="$AI_MEM_SESSION_DIR/_session_template.md"
859
+ if [[ -f "$tmpl" ]] && ! grep -qm1 '^type: ' "$tmpl"; then
860
+ if (( fix )); then
861
+ __ai_mem_guard "$tmpl" >/dev/null && {
862
+ local ttmp="$tmpl.aimem-backfill"
863
+ if [[ "$(head -1 "$tmpl")" == "---" ]]; then
864
+ { head -1 "$tmpl"; print -r -- "type: ai-session-log"; tail -n +2 "$tmpl"; } > "$ttmp"
865
+ else
866
+ { print -rl -- "---" "type: ai-session-log" "---" ""; cat "$tmpl"; } > "$ttmp"
867
+ fi
868
+ mv "$ttmp" "$tmpl"
869
+ print -r -- "added \`type:\` to the session log template, so new logs carry it"
870
+ }
871
+ else
872
+ print -r -- "the session log template has no \`type:\` field, so every new log will lack one. Fix with: ai-mem-lint --fix"
873
+ (( issues++ ))
874
+ fi
875
+ fi
876
+
828
877
  if (( $#untyped_files )); then
829
878
  if (( fix )); then
830
879
  # Written by hand rather than with `sed -i`, whose in-place flag