mixgram 1.3.7 → 2.0.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.
Files changed (37) hide show
  1. package/README.md +68 -345
  2. package/bin/mixgram.js +51 -160
  3. package/examples/embedding-demo.js +22 -142
  4. package/package.json +19 -19
  5. package/spec/bench-indexing.js +23 -211
  6. package/spec/bench-search.js +26 -212
  7. package/spec/run-tests.js +1 -214
  8. package/spec/scenarios/scenarios.js +277 -607
  9. package/spec/sdd.md +14 -988
  10. package/spec/tdd.md +15 -1232
  11. package/spec/test-large-document.js +20 -91
  12. package/src/config.js +57 -48
  13. package/src/core/documents/documents.js +62 -224
  14. package/src/core/embeddings/embedder.js +14 -54
  15. package/src/core/embeddings/queue.js +33 -106
  16. package/src/core/embeddings/vectorStore.js +30 -111
  17. package/src/core/embeddings/worker-process.js +23 -74
  18. package/src/core/indexing/indexer.js +51 -237
  19. package/src/core/indexing/parser.js +13 -114
  20. package/src/core/indexing/reindex.js +28 -137
  21. package/src/core/search/search.js +64 -158
  22. package/src/db/migrations/001_initial.sql +14 -63
  23. package/src/db/sqlite.js +19 -29
  24. package/src/fs/paths.js +11 -101
  25. package/src/fs/watcher.js +15 -48
  26. package/src/mcp/cli-adapter.js +32 -110
  27. package/src/mcp/server.js +64 -141
  28. package/src/mcp/tool-registry.js +21 -132
  29. package/src/mcp/tools.js +27 -199
  30. package/src/utils/markdown.js +15 -82
  31. package/spec/fixtures/realistic-content.js +0 -106
  32. package/spec/reporter.js +0 -127
  33. package/spec/run-tests-runner.js +0 -40
  34. package/src/core/embeddings/worker.js +0 -27
  35. package/src/core/prompts/prompts.js +0 -15
  36. package/src/core/sessions/sessions.js +0 -27
  37. package/src/utils/ids.js +0 -27
@@ -1,626 +1,296 @@
1
- /**
2
- * User-facing scenario definitions for Mixgram tests.
3
- * Each scenario has name, goal, and run(ctx). run() returns { passed, failed }.
4
- * ctx: { h, parse, config, fs, reporter, ok } where ok(cond, msg) counts and optionally prints check.
5
- */
6
-
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import fs from 'fs';
4
+ import os from 'os';
7
5
  import path from 'path';
8
-
9
- import {
10
- decisionSqliteIndex,
11
- decisionSqliteIndexRevision2,
12
- patternIncrementalReindex,
13
- patternIncrementalReindexProject,
14
- sessionSummaryContent,
15
- promptContent,
16
- timelineFirst,
17
- timelineSecond,
18
- manualEditSnippet,
19
- semanticPersistenceDoc,
20
- semanticPersistenceQuery,
21
- semanticFallbackDoc,
22
- noteMcpArchitecture,
23
- PROJECT_NAME
24
- } from '../fixtures/realistic-content.js';
25
- import { toMarkdown } from '../../src/utils/markdown.js';
26
-
27
- function parse(res) {
28
- return JSON.parse(res.content[0].text);
6
+ import { spawnSync } from 'child_process';
7
+ import { once } from 'events';
8
+ import Database from 'better-sqlite3';
9
+ import { Client } from '@modelcontextprotocol/sdk/client/index.js';
10
+ import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';
11
+ import { loadConfig } from '../../src/config.js';
12
+ import { getDb, closeDb } from '../../src/db/sqlite.js';
13
+ import { saveDocument, getDocument, deleteDocument } from '../../src/core/documents/documents.js';
14
+ import { reindex } from '../../src/core/indexing/reindex.js';
15
+ import { search } from '../../src/core/search/search.js';
16
+ import { parse, toMarkdown } from '../../src/utils/markdown.js';
17
+ import { startWatcher } from '../../src/fs/watcher.js';
18
+ import { getVectorStore } from '../../src/core/embeddings/vectorStore.js';
19
+ import { createServer, startEmbeddingWorker } from '../../src/mcp/server.js';
20
+ import { createToolHandlers } from '../../src/mcp/tools.js';
21
+ import { parseToolArgs, getToolByName } from '../../src/mcp/cli-adapter.js';
22
+
23
+ function fixture(t, overrides = {}) {
24
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'mixgram-v2-test-'));
25
+ const config = loadConfig({ collections: { general: path.join(root, 'general'), research: path.join(root, 'research') }, sqlitePath: path.join(root, 'index.db'), watch: false, ...overrides });
26
+ t.after(() => { closeDb(config); fs.rmSync(root, { recursive: true, force: true }); });
27
+ return { root, config };
29
28
  }
29
+ const unpack = result => JSON.parse(result.content[0].text);
30
30
 
31
- export const scenarios = [
32
- {
33
- name: 'Save and find a project decision',
34
- goal: 'Save a project-scoped decision, then find it via search and get_observation.',
35
- run: async function (ctx) {
36
- const { h, reporter, fs } = ctx;
37
- const parseRes = ctx.parse || parse;
38
- let p = 0, f = 0;
39
- const ok = (cond, msg) => { if (cond) p++; else f++; reporter.check(msg, cond); };
40
-
41
- reporter.startScenario(this.name, this.goal);
42
-
43
- const save1 = await h.mem_save(decisionSqliteIndex);
44
- const s1 = parseRes(save1);
45
- ctx.shared = ctx.shared || {};
46
- ctx.shared.decisionDoc = s1;
47
- reporter.step('mem_save', { title: decisionSqliteIndex.title, topic_key: decisionSqliteIndex.topic_key }, { id: s1.id, path: s1.path, created: s1.created });
48
- ok(s1.success && s1.id && s1.created === true, 'save success');
49
- ok(fs.existsSync(s1.path), 'file created');
50
- ok(s1.path.includes('docs') || s1.path.replace(/\\/g, '/').includes('docs'), 'project doc under project memory root');
51
- ok(path.basename(s1.path) === 'architecture-sqlite-derived-index.md', 'project doc filename omits redundant prefixes');
52
-
53
- const search1 = await h.mem_search({ query: 'derived SQLite index', project: PROJECT_NAME });
54
- const search1Parsed = parseRes(search1);
55
- reporter.step('mem_search', { query: 'derived SQLite index', project: PROJECT_NAME }, search1Parsed, { highlight: ['title', 'snippet', 'score'] });
56
- ok(search1Parsed.results.length >= 1, 'searchable');
57
-
58
- const obs1 = await h.mem_get_observation({ id: s1.id });
59
- const obs1Parsed = parseRes(obs1);
60
- reporter.step('mem_get_observation', { id: s1.id }, { title: obs1Parsed.title });
61
- ok(obs1Parsed.title === decisionSqliteIndex.title, 'get_observation title');
62
-
63
- reporter.endScenario(p, f);
64
- return { passed: p, failed: f };
65
- }
66
- },
67
- {
68
- name: 'Home-scoped memory and merged search',
69
- goal: 'Save a home-scoped pattern and find it in merged project search.',
70
- run: async function (ctx) {
71
- const { h, reporter } = ctx;
72
- const parseRes = ctx.parse || parse;
73
- let p = 0, f = 0;
74
- const ok = (cond, msg) => { if (cond) p++; else f++; reporter.check(msg, cond); };
75
-
76
- reporter.startScenario(this.name, this.goal);
77
-
78
- const save2 = await h.mem_save(patternIncrementalReindex);
79
- const s2 = parseRes(save2);
80
- reporter.step('mem_save (home)', { scope: 'home', topic_key: patternIncrementalReindex.topic_key }, { path: s2.path, created: s2.created });
81
- ok(s2.success && s2.path && s2.path.includes('home'), 'home save');
82
- ok(path.basename(s2.path) === 'incremental-reindexing.md', 'home doc filename omits folder category');
83
-
84
- const searchMerged = await h.mem_search({ query: 'reindex', scope_mode: 'merged', project: PROJECT_NAME });
85
- const mergedParsed = parseRes(searchMerged);
86
- reporter.step('mem_search (merged)', { query: 'reindex', scope_mode: 'merged' }, mergedParsed, { highlight: ['title', 'scope', 'snippet'] });
87
- ok(mergedParsed.results.length >= 1, 'merged search');
88
-
89
- reporter.endScenario(p, f);
90
- return { passed: p, failed: f };
91
- }
92
- },
93
- {
94
- name: 'Update by topic_key (upsert)',
95
- goal: 'Saving again with same topic_key updates the document instead of creating a duplicate.',
96
- run: async function (ctx) {
97
- const { h, reporter } = ctx;
98
- const parseRes = ctx.parse || parse;
99
- let p = 0, f = 0;
100
- const ok = (cond, msg) => { if (cond) p++; else f++; reporter.check(msg, cond); };
101
- if (!ctx.shared?.decisionDoc) {
102
- const save1 = await h.mem_save(decisionSqliteIndex);
103
- ctx.shared = ctx.shared || {};
104
- ctx.shared.decisionDoc = parseRes(save1);
105
- }
106
-
107
- reporter.startScenario(this.name, this.goal);
108
-
109
- const save3 = await h.mem_save(decisionSqliteIndexRevision2);
110
- const s3 = parseRes(save3);
111
- reporter.step('mem_save (same topic_key)', { topic_key: decisionSqliteIndexRevision2.topic_key }, { id: s3.id, created: s3.created });
112
- ok(s3.success && s3.created === false, 'update not create');
113
-
114
- const obs3 = await h.mem_get_observation({ id: s3.id });
115
- const obs3Parsed = parseRes(obs3);
116
- reporter.step('mem_get_observation', {}, { contentPreview: (obs3Parsed.content || '').slice(0, 100) });
117
- ok(obs3Parsed.content && obs3Parsed.content.includes('Revision two'), 'updated content');
118
-
119
- reporter.endScenario(p, f);
120
- return { passed: p, failed: f };
121
- }
122
- },
123
- {
124
- name: 'Update document by id',
125
- goal: 'mem_update by id changes title and content.',
126
- run: async function (ctx) {
127
- const { h, reporter } = ctx;
128
- const parseRes = ctx.parse || parse;
129
- let p = 0, f = 0;
130
- const ok = (cond, msg) => { if (cond) p++; else f++; reporter.check(msg, cond); };
131
- const s1 = ctx.shared?.decisionDoc || parseRes(await h.mem_save(decisionSqliteIndex));
132
- if (!ctx.shared) ctx.shared = { decisionDoc: s1 }; else ctx.shared.decisionDoc = s1;
133
-
134
- reporter.startScenario(this.name, this.goal);
135
-
136
- const upd = await h.mem_update({ id: s1.id, title: 'Updated title', content: 'Updated body.' });
137
- const updParsed = parseRes(upd);
138
- reporter.step('mem_update', { id: s1.id, title: 'Updated title' }, updParsed);
139
- ok(updParsed.success, 'update success');
140
-
141
- const obs4 = await h.mem_get_observation({ id: s1.id });
142
- const obs4Parsed = parseRes(obs4);
143
- reporter.step('mem_get_observation', {}, { title: obs4Parsed.title, contentPreview: (obs4Parsed.content || '').slice(0, 50) });
144
- ok(obs4Parsed.title === 'Updated title' && obs4Parsed.content && obs4Parsed.content.includes('Updated body'), 'content updated');
145
-
146
- reporter.endScenario(p, f);
147
- return { passed: p, failed: f };
148
- }
149
- },
150
- {
151
- name: 'Search returns results with snippets',
152
- goal: 'mem_search returns an array of results with title, snippet, score.',
153
- run: async function (ctx) {
154
- const { h, reporter } = ctx;
155
- const parseRes = ctx.parse || parse;
156
- let p = 0, f = 0;
157
- const ok = (cond, msg) => { if (cond) p++; else f++; reporter.check(msg, cond); };
158
-
159
- reporter.startScenario(this.name, this.goal);
160
-
161
- const search2 = await h.mem_search({ query: 'updated body', project: PROJECT_NAME, limit: 5 });
162
- const res = parseRes(search2).results;
163
- reporter.step('mem_search', { query: 'updated body', limit: 5 }, { results: res }, { highlight: ['title', 'snippet', 'score'] });
164
- ok(Array.isArray(res), 'results array');
165
- ok(res.length >= 1, 'has results');
166
- ok(res.length === 0 || res[0].snippet != null, 'snippets present');
167
-
168
- reporter.endScenario(p, f);
169
- return { passed: p, failed: f };
170
- }
171
- },
172
- {
173
- name: 'Search with hyphen and slash in query (FTS5 sanitization)',
174
- goal: 'mem_search does not throw on queries containing "-" or "/" (no "syntax error near /" or "no such column").',
175
- run: async function (ctx) {
176
- const { h, reporter } = ctx;
177
- const parseRes = ctx.parse || parse;
178
- let p = 0, f = 0;
179
- const ok = (cond, msg) => { if (cond) p++; else f++; reporter.check(msg, cond); };
180
-
181
- reporter.startScenario(this.name, this.goal);
182
-
183
- const queries = ['sdd-init', 'sdd-init/alchemy-tycoon', 'alchemy-tycoon sdd-init'];
184
- for (const query of queries) {
185
- let res;
186
- try {
187
- res = await h.mem_search({ query, project: PROJECT_NAME, limit: 5 });
188
- } catch (e) {
189
- reporter.check(`mem_search("${query}") does not throw`, false);
190
- f++;
191
- reporter.endScenario(p, f);
192
- return { passed: p, failed: f };
193
- }
194
- const data = parseRes(res).results;
195
- ok(Array.isArray(data), `mem_search("${query}") returns array`);
196
- }
197
-
198
- reporter.endScenario(p, f);
199
- return { passed: p, failed: f };
200
- }
201
- },
202
- {
203
- name: 'Search with OR query (FTS5 operator case)',
204
- goal: 'mem_save then mem_search with query using OR (e.g. mcp OR "model context protocol") returns the saved document.',
205
- run: async function (ctx) {
206
- const { h, reporter } = ctx;
207
- const parseRes = ctx.parse || parse;
208
- let p = 0, f = 0;
209
- const ok = (cond, msg) => { if (cond) p++; else f++; reporter.check(msg, cond); };
210
-
211
- reporter.startScenario(this.name, this.goal);
212
-
213
- const saveRes = await h.mem_save(noteMcpArchitecture);
214
- const saveData = parseRes(saveRes);
215
- reporter.step('mem_save', { title: noteMcpArchitecture.title, type: noteMcpArchitecture.type }, { id: saveData.id, created: saveData.created });
216
- ok(saveData.success && saveData.id, 'mem_save success');
217
-
218
- const searchRes = await h.mem_search({ query: 'mcp OR "model context protocol"', limit: 10 });
219
- const searchData = parseRes(searchRes);
220
- reporter.step('mem_search', { query: 'mcp OR "model context protocol"' }, searchData, { highlight: ['title', 'snippet'] });
221
- ok(Array.isArray(searchData.results), 'results is array');
222
- ok(searchData.results.length >= 1, 'OR query returns at least one result');
223
- const found = searchData.results.some(
224
- (r) => (r.title && r.title.includes('Other note')) || (r.snippet && (r.snippet.toLowerCase().includes('mcp') || r.snippet.toLowerCase().includes('model context protocol')))
225
- );
226
- ok(found, 'saved note is in OR query results');
227
-
228
- reporter.endScenario(p, f);
229
- return { passed: p, failed: f };
230
- }
231
- },
232
- {
233
- name: 'Context for project',
234
- goal: 'mem_context returns usable context (recent or query-based).',
235
- run: async function (ctx) {
236
- const { h, reporter } = ctx;
237
- let p = 0, f = 0;
238
- const ok = (cond, msg) => { if (cond) p++; else f++; reporter.check(msg, cond); };
239
-
240
- reporter.startScenario(this.name, this.goal);
241
-
242
- const ctxRes = await h.mem_context({ project: PROJECT_NAME, limit: 5 });
243
- const text = ctxRes.content[0].text;
244
- reporter.step('mem_context', { project: PROJECT_NAME, limit: 5 }, text?.slice(0, 300) || '(empty)');
245
- ok(typeof text === 'string' && text.length >= 0, 'mem_context returns text');
246
-
247
- reporter.endScenario(p, f);
248
- return { passed: p, failed: f };
249
- }
250
- },
251
- {
252
- name: 'Stats',
253
- goal: 'mem_stats returns document/session/prompt counts and embeddings_enabled.',
254
- run: async function (ctx) {
255
- const { h, reporter } = ctx;
256
- const parseRes = ctx.parse || parse;
257
- let p = 0, f = 0;
258
- const ok = (cond, msg) => { if (cond) p++; else f++; reporter.check(msg, cond); };
259
-
260
- reporter.startScenario(this.name, this.goal);
261
-
262
- const stats = await h.mem_stats();
263
- const st = parseRes(stats);
264
- reporter.step('mem_stats', {}, st);
265
- ok(typeof st.documents === 'number' && st.embeddings_enabled === false, 'mem_stats shape');
266
-
267
- reporter.endScenario(p, f);
268
- return { passed: p, failed: f };
269
- }
270
- },
271
- {
272
- name: 'Session lifecycle',
273
- goal: 'mem_session_start and mem_session_end create and close a session.',
274
- run: async function (ctx) {
275
- const { h, reporter } = ctx;
276
- const parseRes = ctx.parse || parse;
277
- let p = 0, f = 0;
278
- const ok = (cond, msg) => { if (cond) p++; else f++; reporter.check(msg, cond); };
279
-
280
- reporter.startScenario(this.name, this.goal);
281
-
282
- const sessStart = await h.mem_session_start({ project: PROJECT_NAME });
283
- const sess = parseRes(sessStart);
284
- reporter.step('mem_session_start', { project: PROJECT_NAME }, { session_id: sess.session_id });
285
- ok(sess.success && sess.session_id, 'session_start');
286
- const sessionId = sess.session_id;
287
-
288
- const sessEnd = await h.mem_session_end({ session_id: sessionId });
289
- const endParsed = parseRes(sessEnd);
290
- reporter.step('mem_session_end', { session_id: sessionId }, endParsed);
291
- ok(endParsed.success, 'session_end');
292
-
293
- reporter.endScenario(p, f);
294
- return { passed: p, failed: f };
295
- }
296
- },
297
- {
298
- name: 'Session summary becomes searchable',
299
- goal: 'mem_session_summary stores a summary that appears in search.',
300
- run: async function (ctx) {
301
- const { h, reporter } = ctx;
302
- const parseRes = ctx.parse || parse;
303
- let p = 0, f = 0;
304
- const ok = (cond, msg) => { if (cond) p++; else f++; reporter.check(msg, cond); };
305
-
306
- reporter.startScenario(this.name, this.goal);
307
-
308
- const sessStart2 = await h.mem_session_start({ project: PROJECT_NAME });
309
- const sessionId2 = parseRes(sessStart2).session_id;
310
- const summaryRes = await h.mem_session_summary({ session_id: sessionId2, content: sessionSummaryContent });
311
- reporter.step('mem_session_summary', { content: sessionSummaryContent.slice(0, 50) + '...' }, parseRes(summaryRes));
312
- ok(parseRes(summaryRes).success, 'session_summary success');
313
-
314
- const searchSummary = await h.mem_search({ query: 'decided SQLite', project: PROJECT_NAME });
315
- const summarySearchParsed = parseRes(searchSummary);
316
- reporter.step('mem_search (summary)', { query: 'decided SQLite' }, summarySearchParsed, { highlight: ['title', 'snippet'] });
317
- ok(summarySearchParsed.results.length >= 1, 'summary searchable');
318
-
319
- reporter.endScenario(p, f);
320
- return { passed: p, failed: f };
321
- }
322
- },
323
- {
324
- name: 'Save prompt',
325
- goal: 'mem_save_prompt stores a prompt and stats reflect it.',
326
- run: async function (ctx) {
327
- const { h, reporter } = ctx;
328
- const parseRes = ctx.parse || parse;
329
- let p = 0, f = 0;
330
- const ok = (cond, msg) => { if (cond) p++; else f++; reporter.check(msg, cond); };
331
-
332
- reporter.startScenario(this.name, this.goal);
333
- const sessStart = await h.mem_session_start({ project: PROJECT_NAME });
334
- const sessionId2 = parseRes(sessStart).session_id;
335
-
336
- const promptRes = await h.mem_save_prompt({ session_id: sessionId2, content: promptContent });
337
- reporter.step('mem_save_prompt', { content: promptContent.slice(0, 40) + '...' }, parseRes(promptRes));
338
- ok(parseRes(promptRes).success, 'mem_save_prompt success');
339
-
340
- const stats2 = await h.mem_stats();
341
- ok(parseRes(stats2).prompts >= 1, 'prompts count');
342
-
343
- reporter.endScenario(p, f);
344
- return { passed: p, failed: f };
345
- }
346
- },
347
- {
348
- name: 'Timeline around an observation',
349
- goal: 'mem_timeline returns before/focus/after for an observation in a session.',
350
- run: async function (ctx) {
351
- const { h, reporter } = ctx;
352
- const parseRes = ctx.parse || parse;
353
- let p = 0, f = 0;
354
- const ok = (cond, msg) => { if (cond) p++; else f++; reporter.check(msg, cond); };
355
-
356
- reporter.startScenario(this.name, this.goal);
357
-
358
- const sessStart3 = await h.mem_session_start({ project: PROJECT_NAME });
359
- const sessionId3 = parseRes(sessStart3).session_id;
360
- const docA = parseRes(await h.mem_save({ ...timelineFirst, session_id: sessionId3 }));
361
- const docB = parseRes(await h.mem_save({ ...timelineSecond, session_id: sessionId3 }));
362
- reporter.step('mem_save x2 (same session)', { session_id: sessionId3 }, { docA: docA.id, docB: docB.id });
363
-
364
- const timelineRes = await h.mem_timeline({ observation_id: docB.id });
365
- const tl = parseRes(timelineRes);
366
- reporter.step('mem_timeline', { observation_id: docB.id }, { focus: tl.focus?.title, beforeCount: tl.before?.length, afterCount: tl.after?.length });
367
- ok(tl.focus && tl.focus.id === docB.id, 'timeline focus');
368
- ok(Array.isArray(tl.before) && tl.before.length >= 1, 'timeline before');
369
-
370
- reporter.endScenario(p, f);
371
- return { passed: p, failed: f };
372
- }
373
- },
374
- {
375
- name: 'Manual edit and reindex',
376
- goal: 'Editing a Markdown file and reindexing makes the new content searchable.',
377
- run: async function (ctx) {
378
- const { h, reporter, fs } = ctx;
379
- const parseRes = ctx.parse || parse;
380
- let p = 0, f = 0;
381
- const ok = (cond, msg) => { if (cond) p++; else f++; reporter.check(msg, cond); };
382
- const s1 = ctx.shared?.decisionDoc;
383
- if (!s1) throw new Error('Requires decisionDoc from earlier scenario');
384
- const docPath = s1.path;
385
-
386
- reporter.startScenario(this.name, this.goal);
387
-
388
- let raw = fs.readFileSync(docPath, 'utf8');
389
- raw = raw.replace('Updated body.', manualEditSnippet);
390
- fs.writeFileSync(docPath, raw);
391
- reporter.step('Manual edit', { path: docPath }, 'replaced "Updated body." with realistic snippet');
392
-
393
- await h.mem_reindex({ full: true });
394
- reporter.step('mem_reindex', { full: true }, {});
395
-
396
- const searchA = await h.mem_search({ query: 'Manual edits' });
397
- const searchAParsed = parseRes(searchA);
398
- reporter.step('mem_search (after edit)', { query: 'Manual edits' }, searchAParsed, { highlight: ['title', 'snippet'] });
399
- ok(searchAParsed.results.length >= 1, 'edited phrase searchable');
400
-
401
- const obsEdit = await h.mem_get_observation({ id: s1.id });
402
- const obsEditParsed = parseRes(obsEdit);
403
- ok(obsEditParsed.content && obsEditParsed.content.includes(manualEditSnippet.slice(0, 20)), 'get_observation reflects edit');
404
-
405
- reporter.endScenario(p, f);
406
- return { passed: p, failed: f };
407
- }
408
- },
409
- {
410
- name: 'Full reindex from disk',
411
- goal: 'fullReindex rebuilds the index from Markdown files; search works after.',
412
- run: async function (ctx) {
413
- const { h, reporter, config } = ctx;
414
- const parseRes = ctx.parse || parse;
415
- let p = 0, f = 0;
416
- const ok = (cond, msg) => { if (cond) p++; else f++; reporter.check(msg, cond); };
417
- if (!ctx.shared?.decisionDoc) throw new Error('Requires decisionDoc');
418
-
419
- reporter.startScenario(this.name, this.goal);
420
-
421
- const { fullReindex } = await import('../../src/core/indexing/reindex.js');
422
- const reindexResult = fullReindex(config);
423
- reporter.step('fullReindex', {}, reindexResult);
424
- ok(reindexResult.indexed >= 1, 'full reindex indexed');
425
-
426
- const searchAfter = await h.mem_search({ query: 'Manual edits' });
427
- ok(parseRes(searchAfter).results.length >= 1, 'searchable after rebuild');
428
-
429
- reporter.endScenario(p, f);
430
- return { passed: p, failed: f };
431
- }
432
- },
433
- {
434
- name: 'Move file and reindex (path is source of location)',
435
- goal: 'Moving a .md file to another folder: incremental reindex desindexa old path and indexa new path; doc remains findable by id.',
436
- run: async function (ctx) {
437
- const { h, reporter, config, fs } = ctx;
438
- const parseRes = ctx.parse || parse;
439
- let p = 0, f = 0;
440
- const ok = (cond, msg) => { if (cond) p++; else f++; reporter.check(msg, cond); };
441
- if (!ctx.shared?.decisionDoc) throw new Error('Requires decisionDoc');
442
-
443
- reporter.startScenario(this.name, this.goal);
444
-
445
- const docId = ctx.shared.decisionDoc.id;
446
- const oldPath = ctx.shared.decisionDoc.path;
447
- if (!fs.existsSync(oldPath)) throw new Error('decisionDoc path missing');
448
- const raw = fs.readFileSync(oldPath, 'utf8');
449
- const rootDir = path.dirname(path.dirname(oldPath));
450
- const architectureDir = path.join(rootDir, 'architecture');
451
- if (!fs.existsSync(architectureDir)) fs.mkdirSync(architectureDir, { recursive: true });
452
- const newPath = path.join(architectureDir, path.basename(oldPath));
453
- fs.writeFileSync(newPath, raw);
454
- fs.unlinkSync(oldPath);
455
- reporter.step('Move file', { from: oldPath, to: newPath }, {});
456
-
457
- const reindexRes = await h.mem_reindex({ full: false });
458
- const reindexParsed = parseRes(reindexRes);
459
- reporter.step('mem_reindex', { full: false }, reindexParsed);
460
- ok(reindexParsed.indexed >= 1 || reindexParsed.removed >= 1, 'reindex touched index');
461
-
462
- const obsRes = await h.mem_get_observation({ id: docId });
463
- const obs = parseRes(obsRes);
464
- ok(!obs.error && obs.id === docId, 'get_observation by id still works after move');
465
- ok(obs.title != null || (obs.content && obs.content.length > 0), 'observation has title or content');
466
-
467
- reporter.endScenario(p, f);
468
- return { passed: p, failed: f };
469
- }
470
- },
471
- {
472
- name: 'No duplicates after incremental reindex',
473
- goal: 'Running incremental reindex without file changes does not duplicate documents.',
474
- run: async function (ctx) {
475
- const { h, reporter } = ctx;
476
- const parseRes = ctx.parse || parse;
477
- let p = 0, f = 0;
478
- const ok = (cond, msg) => { if (cond) p++; else f++; reporter.check(msg, cond); };
479
-
480
- reporter.startScenario(this.name, this.goal);
481
-
482
- const stats3 = await h.mem_stats();
483
- await h.mem_reindex({ full: false });
484
- const stats4 = await h.mem_stats();
485
- const docs3 = parseRes(stats3).documents;
486
- const docs4 = parseRes(stats4).documents;
487
- reporter.step('mem_reindex (incremental)', { full: false }, { docsBefore: docs3, docsAfter: docs4 });
488
- ok(docs4 === docs3, 'no duplicate docs');
489
-
490
- reporter.endScenario(p, f);
491
- return { passed: p, failed: f };
492
- }
493
- },
494
- {
495
- name: 'Home in merged search',
496
- goal: 'Merged search returns both project and home results; home note appears.',
497
- run: async function (ctx) {
498
- const { h, reporter } = ctx;
499
- const parseRes = ctx.parse || parse;
500
- let p = 0, f = 0;
501
- const ok = (cond, msg) => { if (cond) p++; else f++; reporter.check(msg, cond); };
502
-
503
- reporter.startScenario(this.name, this.goal);
504
-
505
- const homeSearch = await h.mem_search({ query: 'reindex', scope_mode: 'merged' });
506
- const homeResults = parseRes(homeSearch).results;
507
- reporter.step('mem_search (merged)', { query: 'reindex', scope_mode: 'merged' }, { results: homeResults }, { highlight: ['scope', 'title', 'snippet'] });
508
- const homeInResults = homeResults.some((r) => r.scope === 'home');
509
- ok(homeInResults, 'home in merged');
510
-
511
- reporter.endScenario(p, f);
512
- return { passed: p, failed: f };
513
- }
514
- },
515
- {
516
- name: 'Project outranks home in merged',
517
- goal: 'In merged mode, project results rank before home for same topic.',
518
- run: async function (ctx) {
519
- const { h, reporter } = ctx;
520
- const parseRes = ctx.parse || parse;
521
- let p = 0, f = 0;
522
- const ok = (cond, msg) => { if (cond) p++; else f++; reporter.check(msg, cond); };
523
-
524
- reporter.startScenario(this.name, this.goal);
31
+ function edit(filePath, fn) {
32
+ const doc = parse(fs.readFileSync(filePath, 'utf8'));
33
+ fn(doc);
34
+ fs.writeFileSync(filePath, toMarkdown(doc.frontmatter, doc.body));
35
+ }
525
36
 
526
- const rankSearch = await h.mem_search({ query: 'reindex', scope_mode: 'merged', project: PROJECT_NAME, limit: 5 });
527
- const rankResults = parseRes(rankSearch).results;
528
- reporter.step('mem_search (merged)', { query: 'reindex', limit: 5 }, { results: rankResults }, { highlight: ['scope', 'title', 'score'] });
529
- const firstProject = rankResults.find((r) => r.scope === 'project');
530
- const firstHome = rankResults.find((r) => r.scope === 'home');
531
- const projectFirst = !firstHome || !firstProject || rankResults.indexOf(firstProject) <= rankResults.indexOf(firstHome);
532
- ok(projectFirst, 'project first');
37
+ async function eventually(check) {
38
+ const end = Date.now() + 5000;
39
+ while (Date.now() < end) {
40
+ if (check()) return;
41
+ await new Promise(resolve => setTimeout(resolve, 30));
42
+ }
43
+ assert.ok(check(), 'Timed out waiting for filesystem event');
44
+ }
533
45
 
534
- reporter.endScenario(p, f);
535
- return { passed: p, failed: f };
536
- }
537
- },
538
- {
539
- name: 'Project save does not overwrite home',
540
- goal: 'Saving a project-scoped note with same topic_key as home does not overwrite the home note.',
541
- run: async function (ctx) {
542
- const { h, reporter } = ctx;
543
- const parseRes = ctx.parse || parse;
544
- let p = 0, f = 0;
545
- const ok = (cond, msg) => { if (cond) p++; else f++; reporter.check(msg, cond); };
46
+ test('save creates distinct identities, upserts only explicit keys and preserves omitted fields', t => {
47
+ const { config } = fixture(t);
48
+ const a = saveDocument(config, { title: 'Same', content: 'Body', key: 'topic', tags: ['a'], type: 'recipe' });
49
+ const b = saveDocument(config, { title: 'Same' });
50
+ assert.notEqual(a.path, b.path);
51
+ assert.equal(fs.existsSync(a.path), true);
52
+ const updated = saveDocument(config, { key: 'topic', title: 'Renamed' });
53
+ assert.equal(updated.id, a.id);
54
+ assert.equal(updated.path, a.path);
55
+ assert.equal(updated.created, false);
56
+ assert.deepEqual(getDocument(config, a.id).tags, ['a']);
57
+ assert.equal(getDocument(config, a.id).content.trim(), 'Body');
58
+ assert.equal(getDocument(config, a.id).type, 'recipe');
59
+ const other = saveDocument(config, { collection: 'research', key: 'topic' });
60
+ assert.notEqual(other.id, a.id);
61
+ saveDocument(config, { id: a.id, content: '', tags: [], key: null });
62
+ assert.equal(getDocument(config, a.id).content, '');
63
+ assert.deepEqual(getDocument(config, a.id).tags, []);
64
+ assert.equal(getDocument(config, a.id).key, null);
65
+ assert.throws(() => saveDocument(config, { id: 'missing' }), /not found/);
66
+ assert.throws(() => saveDocument(config, { id: a.id, collection: 'research' }), /Move the file/);
67
+ assert.throws(() => saveDocument(config, { collection: 'missing' }), /Unknown collection/);
68
+ });
546
69
 
547
- reporter.startScenario(this.name, this.goal);
70
+ test('key conflicts fail before writing', t => {
71
+ const { config } = fixture(t);
72
+ const a = saveDocument(config, { key: 'one', content: 'original' });
73
+ saveDocument(config, { key: 'two' });
74
+ assert.throws(() => saveDocument(config, { id: a.id, key: 'two', content: 'changed' }), /Key already/);
75
+ assert.equal(getDocument(config, a.id).content.trim(), 'original');
76
+ });
548
77
 
549
- await h.mem_save(patternIncrementalReindexProject);
550
- reporter.step('mem_save (project, same topic_key)', { topic_key: patternIncrementalReindexProject.topic_key }, {});
78
+ test('rebuild from an empty SQLite file preserves durable metadata and deletion', async t => {
79
+ const { config } = fixture(t);
80
+ const a = saveDocument(config, { key: 'a', tags: ['durable'], type: 'custom', content: 'needle' });
81
+ const b = saveDocument(config, { collection: 'research', content: 'deleted' });
82
+ deleteDocument(config, b.id);
83
+ const before = getDocument(config, a.id);
84
+ closeDb(config);
85
+ fs.unlinkSync(config.sqlitePath);
86
+ const result = reindex(config, { full: true });
87
+ assert.deepEqual(result.errors, []);
88
+ assert.deepEqual(getDocument(config, a.id), before);
89
+ assert.throws(() => getDocument(config, b.id), /not found/);
90
+ assert.equal(getDb(config).prepare('SELECT deleted_at FROM documents WHERE id=?').get(b.id).deleted_at !== null, true);
91
+ assert.equal((await search(config, { tags: ['durable'] })).results[0].id, a.id);
92
+ deleteDocument(config, b.id, { hardDelete: true });
93
+ assert.equal(fs.existsSync(b.path), false);
94
+ });
551
95
 
552
- const homeObs = await h.mem_search({ query: 'Reindex only changed', scope_mode: 'home-only' });
553
- const homeObsParsed = parseRes(homeObs);
554
- reporter.step('mem_search (home-only)', { query: 'Reindex only changed', scope_mode: 'home-only' }, homeObsParsed, { highlight: ['title', 'snippet'] });
555
- ok(homeObsParsed.results.length >= 1, 'home note intact');
96
+ test('manual frontmatter edits and file moves are authoritative in both reindex modes', t => {
97
+ const { config } = fixture(t);
98
+ const a = saveDocument(config, { title: 'Before', tags: ['old'], content: 'before' });
99
+ edit(a.path, doc => { doc.frontmatter.title = 'After'; doc.frontmatter.tags = ['new']; doc.body = 'after'; });
100
+ assert.equal(reindex(config).indexed, 1);
101
+ assert.equal(getDocument(config, a.id).title, 'After');
102
+ assert.deepEqual(getDocument(config, a.id).tags, ['new']);
103
+ assert.equal(reindex(config).skipped, 1);
104
+ fs.mkdirSync(config.collections.research, { recursive: true });
105
+ fs.renameSync(a.path, path.join(config.collections.research, path.basename(a.path)));
106
+ assert.deepEqual(reindex(config).errors, []);
107
+ assert.equal(getDocument(config, a.id).collection, 'research');
108
+ assert.deepEqual(reindex(config, { full: true }).errors, []);
109
+ assert.equal(getDocument(config, a.id).title, 'After');
110
+ });
556
111
 
557
- reporter.endScenario(p, f);
558
- return { passed: p, failed: f };
559
- }
560
- },
561
- {
562
- name: 'Semantic / embeddings (save and search)',
563
- goal: 'With embeddings enabled, save and search work (FTS or hybrid).',
564
- run: async function (ctx) {
565
- const { reporter } = ctx;
566
- const hSemantic = ctx.hSemantic;
567
- const parseRes = ctx.parse || parse;
568
- if (!hSemantic) {
569
- reporter.startScenario(this.name, this.goal);
570
- reporter.check('skipped (embeddings not in use)', true);
571
- reporter.endScenario(1, 0);
572
- return { passed: 1, failed: 0 };
573
- }
574
- let p = 0, f = 0;
575
- const ok = (cond, msg) => { if (cond) p++; else f++; reporter.check(msg, cond); };
112
+ test('reindex reports invalid files and duplicate ids', t => {
113
+ const { config } = fixture(t);
114
+ const a = saveDocument(config, { content: 'valid' });
115
+ fs.copyFileSync(a.path, path.join(config.collections.general, 'duplicate.md'));
116
+ fs.writeFileSync(path.join(config.collections.general, 'broken.md'), '---\ntags: nope\n---\ninvalid');
117
+ const result = reindex(config, { full: true });
118
+ assert.equal(result.errors.length, 2);
119
+ assert.equal(result.indexed, 1);
120
+ });
576
121
 
577
- reporter.startScenario(this.name, this.goal);
122
+ test('watcher handles add, metadata changes, delete and errors', async t => {
123
+ const { config } = fixture(t, { indexing: { watchStabilityMs: 20 } });
124
+ for (const root of Object.values(config.collections)) fs.mkdirSync(root, { recursive: true });
125
+ const errors = [];
126
+ const previousPolling = process.env.CHOKIDAR_USEPOLLING;
127
+ process.env.CHOKIDAR_USEPOLLING = 'true';
128
+ const watcher = startWatcher(config, error => errors.push(error));
129
+ if (previousPolling === undefined) delete process.env.CHOKIDAR_USEPOLLING;
130
+ else process.env.CHOKIDAR_USEPOLLING = previousPolling;
131
+ t.after(() => watcher.close());
132
+ await once(watcher, 'ready');
133
+ const file = path.join(config.collections.general, 'manual.md');
134
+ fs.writeFileSync(file, toMarkdown({ id: 'manual', title: 'Manual' }, 'body'));
135
+ await eventually(() => getDb(config).prepare('SELECT id FROM documents WHERE id=?').get('manual'));
136
+ edit(file, doc => { doc.frontmatter.tags = ['watched']; doc.frontmatter.title = 'Changed'; });
137
+ await eventually(() => getDb(config).prepare('SELECT title FROM documents WHERE id=?').get('manual')?.title === 'Changed');
138
+ assert.deepEqual(getDocument(config, 'manual').tags, ['watched']);
139
+ fs.unlinkSync(file);
140
+ await eventually(() => !getDb(config).prepare('SELECT id FROM documents WHERE id=?').get('manual'));
141
+ fs.writeFileSync(file, '---\ntags: nope\n---\n');
142
+ await eventually(() => errors.length > 0);
143
+ await watcher.close();
144
+ });
578
145
 
579
- const saveSem = await hSemantic.mem_save(semanticFallbackDoc);
580
- const saveSemParsed = parseRes(saveSem);
581
- reporter.step('mem_save (embeddings on)', { title: semanticFallbackDoc.title }, saveSemParsed);
582
- ok(saveSemParsed.success, 'save with embeddings');
146
+ test('text and recent searches apply collection, type and all tags before pagination', async t => {
147
+ const { config } = fixture(t);
148
+ saveDocument(config, { collection: 'research', title: 'needle needle', tags: ['x','y'], type: 'recipe' });
149
+ saveDocument(config, { title: 'needle', tags: ['x'], type: 'recipe' });
150
+ saveDocument(config, { title: 'needle', tags: ['x','y'], type: 'note' });
151
+ const a = saveDocument(config, { title: 'needle', tags: ['x','y'], type: 'recipe' });
152
+ const b = saveDocument(config, { title: 'needle', tags: ['x','y'], type: 'recipe' });
153
+ for (const query of [undefined, 'needle']) {
154
+ const options = { query, collection: 'general', type: 'recipe', tags: ['x','y'] };
155
+ const all = (await search(config, options)).results;
156
+ assert.deepEqual(new Set(all.map(r => r.id)), new Set([a.id,b.id]));
157
+ const page = (await search(config, { ...options, limit: 1, offset: 1 })).results;
158
+ assert.deepEqual(page, [all[1]]);
159
+ }
160
+ assert.equal((await search(config)).results.length, 5);
161
+ assert.equal((await search(config, { query: 'absent OR needle' })).results.length, 5);
162
+ await assert.rejects(search(config, { limit: 0 }), /Invalid pagination/);
163
+ });
583
164
 
584
- const searchSem = await hSemantic.mem_search({ query: 'semantic fallback', project: PROJECT_NAME });
585
- const searchSemParsed = parseRes(searchSem);
586
- reporter.step('mem_search', { query: 'semantic fallback' }, searchSemParsed, { highlight: ['title', 'snippet', 'score'] });
587
- ok(searchSemParsed.results.length >= 1, 'search returns results');
165
+ test('real vector store filters before ranking, fuses duplicate matches and ignores stale vectors', async t => {
166
+ const { config } = fixture(t, { embeddings: { enabled: true, dimensions: 2, similarityThreshold: 0 } });
167
+ config.getQueryEmbedding = async () => new Float32Array([1,0]);
168
+ const store = await getVectorStore(config);
169
+ const add = async (payload, vector) => {
170
+ const doc = saveDocument(config, payload);
171
+ const row = getDb(config).prepare('SELECT content_hash FROM documents WHERE id=?').get(doc.id);
172
+ await store.insert(config, { id: doc.id, content_hash: row.content_hash, embedding: vector });
173
+ return doc;
174
+ };
175
+ for (let i = 0; i < 25; i++) await add({ collection: 'research', title: 'needle' }, [1,0]);
176
+ const both = await add({ title: 'needle', tags: ['x','y'], type: 'custom' }, [0.9,0.1]);
177
+ const semantic = await add({ title: 'different', tags: ['x','y'], type: 'custom' }, [0.8,0.2]);
178
+ await add({ title: 'needle', tags: ['x'], type: 'custom' }, [1,0]);
179
+ const options = { query: 'needle', collection: 'general', tags: ['x','y'], type: 'custom' };
180
+ const result = await search(config, options);
181
+ assert.equal(result.mode, 'hybrid');
182
+ assert.equal(result.results.length, 2);
183
+ assert.equal(result.results[0].id, both.id);
184
+ assert.equal(result.results[1].id, semantic.id);
185
+ assert.deepEqual((await search(config, { ...options, limit: 1, offset: 1 })).results, [result.results[1]]);
186
+ await store.insert(config, { id: both.id, content_hash: getDb(config).prepare('SELECT content_hash FROM documents WHERE id=?').get(both.id).content_hash, embedding: [1,0] });
187
+ assert.equal((await search(config, options)).results.length, 2);
188
+ saveDocument(config, { id: semantic.id, content: 'new content' });
189
+ assert.equal((await search(config, options)).results.length, 1);
190
+ config.getQueryEmbedding = async () => { throw new Error('unavailable'); };
191
+ const degraded = await search(config, options);
192
+ assert.equal(degraded.degraded, true);
193
+ assert.equal(degraded.mode, 'text');
194
+ assert.equal(degraded.results[0].id, both.id);
195
+ });
588
196
 
589
- reporter.endScenario(p, f);
590
- return { passed: p, failed: f };
591
- }
592
- },
593
- {
594
- name: 'Error handling: update/get invalid id',
595
- goal: 'mem_update and mem_get_observation with non-existent id return clear errors.',
596
- run: async function (ctx) {
597
- const { h, reporter } = ctx;
598
- const parseRes = ctx.parse || parse;
599
- let p = 0, f = 0;
600
- const ok = (cond, msg) => { if (cond) p++; else f++; reporter.check(msg, cond); };
197
+ test('a committed file survives an indexing failure and can be repaired', t => {
198
+ const { config } = fixture(t);
199
+ const a = saveDocument(config, { content: 'before' });
200
+ getDb(config).exec("CREATE TRIGGER fail_index BEFORE UPDATE ON documents BEGIN SELECT RAISE(ABORT,'index unavailable'); END");
201
+ const result = saveDocument(config, { id: a.id, content: 'after' });
202
+ assert.equal(result.saved, true);
203
+ assert.equal(result.indexed, false);
204
+ assert.match(result.error, /index unavailable/);
205
+ assert.equal(parse(fs.readFileSync(a.path, 'utf8')).body.trim(), 'after');
206
+ getDb(config).exec('DROP TRIGGER fail_index');
207
+ assert.deepEqual(reindex(config).errors, []);
208
+ assert.equal(getDb(config).prepare('SELECT body FROM documents WHERE id=?').get(a.id).body.trim(), 'after');
209
+ });
601
210
 
602
- reporter.startScenario(this.name, this.goal);
211
+ test('configuration deep merges, rejects overlap and refuses legacy databases without modifying them', t => {
212
+ const { root, config } = fixture(t, { embeddings: { enabled: true } });
213
+ assert.equal(config.embeddings.dimensions, 1024);
214
+ assert.equal(Object.keys(config.collections).length, 2);
215
+ assert.throws(() => loadConfig({ collections: { general: root, nested: path.join(root, 'sub') } }), /overlap/);
216
+ const legacyPath = path.join(root, 'legacy.db');
217
+ const legacy = new Database(legacyPath);
218
+ legacy.exec("CREATE TABLE documents (id TEXT); INSERT INTO documents VALUES ('keep')");
219
+ legacy.close();
220
+ const before = fs.readFileSync(legacyPath);
221
+ const oldConfig = loadConfig({ sqlitePath: legacyPath });
222
+ assert.throws(() => getDb(oldConfig), /Legacy database/);
223
+ assert.deepEqual(fs.readFileSync(legacyPath), before);
224
+ });
603
225
 
604
- const updBad = await h.mem_update({ id: 'nonexist01', title: 'No', content: 'No' });
605
- const updBadParsed = parseRes(updBad);
606
- reporter.step('mem_update (bad id)', { id: 'nonexist01' }, updBadParsed);
607
- ok(updBadParsed.success === false && updBadParsed.error, 'update fails cleanly');
226
+ test('two configurations isolate database connections, vector extensions and worker cleanup', async t => {
227
+ const a = fixture(t, { embeddings: { enabled: true, dimensions: 2 } }).config;
228
+ const b = fixture(t, { embeddings: { enabled: true, dimensions: 2 } }).config;
229
+ saveDocument(a, { title: 'Only A' });
230
+ assert.equal(getDb(b).prepare('SELECT COUNT(*) AS n FROM documents').get().n, 0);
231
+ await getVectorStore(a);
232
+ await getVectorStore(b);
233
+ const stopA = startEmbeddingWorker(a);
234
+ const stopB = startEmbeddingWorker(b);
235
+ assert.notEqual(a.getQueryEmbedding, b.getQueryEmbedding);
236
+ stopA();
237
+ assert.equal(a.getQueryEmbedding, undefined);
238
+ assert.equal(typeof b.getQueryEmbedding, 'function');
239
+ stopB();
240
+ closeDb(a);
241
+ assert.equal(getDb(b).prepare('SELECT COUNT(*) AS n FROM documents').get().n, 0);
242
+ });
608
243
 
609
- const getBad = await h.mem_get_observation({ id: 'nonexist02' });
610
- const getBadParsed = parseRes(getBad);
611
- reporter.step('mem_get_observation (bad id)', { id: 'nonexist02' }, getBadParsed);
612
- ok(getBadParsed.error === 'Observation not found' || !!getBadParsed.error, 'get_observation error');
244
+ test('MCP and CLI expose the same six tools and preserve omitted, empty and invalid values', async t => {
245
+ const { root, config } = fixture(t);
246
+ const configPath = path.join(root, 'config.json');
247
+ fs.writeFileSync(configPath, JSON.stringify(config));
248
+ const instance = createServer(config);
249
+ t.after(() => closeDb(instance.config));
250
+ const client = new Client({ name: 'test', version: '1' });
251
+ const [a,b] = InMemoryTransport.createLinkedPair();
252
+ await instance.mcpServer.connect(a);
253
+ await client.connect(b);
254
+ t.after(() => client.close());
255
+ const tools = await client.listTools();
256
+ assert.deepEqual(tools.tools.map(t => t.name).sort(), ['mem_delete','mem_get','mem_reindex','mem_save','mem_search','mem_stats']);
257
+ const saved = unpack(await client.callTool({ name: 'mem_save', arguments: { title: 'Shared', tags: ['keep'], content: 'body' } }));
258
+ const cli = (...args) => spawnSync(process.execPath, ['bin/mixgram.js', ...args, '--config', configPath], { cwd: new URL('../../', import.meta.url), encoding: 'utf8' });
259
+ const update = cli('mem_save', '--id', saved.id, '--title', 'Renamed');
260
+ assert.equal(update.status, 0, update.stderr);
261
+ const read = unpack(await client.callTool({ name: 'mem_get', arguments: { id: saved.id } }));
262
+ assert.deepEqual(read.tags, ['keep']);
263
+ assert.equal(read.title, 'Renamed');
264
+ const clear = cli('mem_save', '--id', saved.id, '--content', '', '--no-tags');
265
+ assert.equal(clear.status, 0, clear.stderr);
266
+ const cleared = unpack(await client.callTool({ name: 'mem_get', arguments: { id: saved.id } }));
267
+ assert.equal(cleared.content, '');
268
+ assert.deepEqual(cleared.tags, []);
269
+ const cliSearch = cli('mem_search', '--query', 'Renamed');
270
+ const mcpSearch = unpack(await client.callTool({ name: 'mem_search', arguments: { query: 'Renamed' } }));
271
+ assert.deepEqual(JSON.parse(cliSearch.stdout), mcpSearch);
272
+ assert.notEqual(cli('mem_search', '--limit', '-1').status, 0);
273
+ assert.equal((await createToolHandlers(config).mem_search({ limit: -1 })).isError, true);
274
+ assert.notEqual(cli('mem_save', '--typo', 'x').status, 0);
275
+ assert.equal((await client.callTool({ name: 'mem_save', arguments: { typo: 'x' } })).isError, true);
276
+ assert.equal((await client.callTool({ name: 'mem_search', arguments: { limit: -1 } })).isError, true);
277
+ assert.deepEqual(parseToolArgs(getToolByName('mem_save'), ['--title', 'x']), { title: 'x' });
278
+ await client.close();
279
+ });
613
280
 
614
- reporter.endScenario(p, f);
615
- return { passed: p, failed: f };
616
- }
617
- }
618
- ];
619
281
 
620
- // Fix binding of `this` for each scenario's run()
621
- scenarios.forEach((s) => {
622
- const run = s.run;
623
- s.run = function (ctx) {
624
- return run.call(s, ctx);
625
- };
282
+ test('enabling embeddings queues existing documents without restarting unchanged work', t => {
283
+ const { config } = fixture(t);
284
+ const saved = saveDocument(config, { content: 'existing knowledge' });
285
+ config.embeddings.enabled = true;
286
+ reindex(config);
287
+ const db = getDb(config);
288
+ const job = () => db.prepare('SELECT * FROM embedding_jobs WHERE document_id=?').get(saved.id);
289
+ assert.equal(job().status, 'pending');
290
+ db.prepare("UPDATE embedding_jobs SET status='completed'").run();
291
+ reindex(config);
292
+ assert.equal(job().status, 'completed');
293
+ saveDocument(config, { id: saved.id, content: 'changed knowledge' });
294
+ assert.equal(job().status, 'pending');
295
+ assert.notEqual(job().content_hash, undefined);
626
296
  });