@modusensus/dsh-mneme 0.4.7 → 0.5.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 +38 -5
- package/lib/api.js +117 -1
- package/lib/client.js +1013 -216
- package/lib/config.js +30 -1
- package/lib/dream.js +41 -12
- package/lib/hot-memory.js +53 -0
- package/lib/inject.js +84 -3
- package/lib/local-embedder.js +7 -1
- package/lib/reranker.js +4 -1
- package/lib/search/adaptive.js +22 -0
- package/lib/search/bm25.js +96 -0
- package/lib/service.js +138 -9
- package/lib/store.js +24 -0
- package/package.json +9 -1
- package/scripts/benchmark-recall.js +133 -0
- package/scripts/sync-lib.js +7 -2
- package/src/api.js +117 -1
- package/src/config.js +30 -1
- package/src/dream.js +41 -12
- package/src/hot-memory.js +53 -0
- package/src/inject.js +84 -3
- package/src/local-embedder.js +7 -1
- package/src/reranker.js +4 -1
- package/src/search/adaptive.js +22 -0
- package/src/search/bm25.js +96 -0
- package/src/service.js +138 -9
- package/src/store.js +24 -0
- package/test/benchmark.test.js +35 -0
- package/test/client.test.js +205 -15
- package/test/graph-api.test.js +175 -0
- package/test/hot-memory.test.js +174 -0
- package/test/reasoning-effort.test.js +1 -1
- package/test/recall-layer.test.js +2 -2
- package/test/reranker.test.js +43 -0
- package/test/search-fusion.test.js +90 -0
- package/test/service-search.test.js +31 -2
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Adaptive vector threshold (v0.5.0 召回率优化 1.2): replaces the fixed
|
|
2
|
+
// vectorSearchThreshold=0.65 with a query-aware cutoff.
|
|
3
|
+
// entity:/attr: prefixes → 0.5 (entity recall is name-driven; loosen)
|
|
4
|
+
// very short queries → 0.7 (<5 chars match almost anything; tighten)
|
|
5
|
+
// very long queries → 0.6 (semantically specific; loosen a little)
|
|
6
|
+
// head-gap rule → when the top-1 vs top-5 candidate gap exceeds
|
|
7
|
+
// 0.3 the head is decisive — loosen to 0.5 so
|
|
8
|
+
// the tail still reaches the reranker
|
|
9
|
+
// otherwise → 0.65 (the legacy default)
|
|
10
|
+
// Pure and total: same inputs, same cutoff, no store access.
|
|
11
|
+
export function adaptiveThreshold(query, candidates = []) {
|
|
12
|
+
const q = String(query ?? "");
|
|
13
|
+
if (q.startsWith("entity:") || q.startsWith("attr:")) return 0.5;
|
|
14
|
+
if (q.length > 0 && q.length < 5) return 0.7;
|
|
15
|
+
if (q.length > 50) return 0.6;
|
|
16
|
+
const scores = (Array.isArray(candidates) ? candidates : [])
|
|
17
|
+
.map((c) => (typeof c?._score === "number" ? c._score : typeof c?.score === "number" ? c.score : 0))
|
|
18
|
+
.filter((s) => s > 0)
|
|
19
|
+
.sort((a, b) => b - a);
|
|
20
|
+
if (scores.length >= 5 && scores[0] - scores[4] > 0.3) return 0.5;
|
|
21
|
+
return 0.65;
|
|
22
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// BM25 sparse retrieval (v0.5.0 召回率优化 1.1): the third recall path beside
|
|
2
|
+
// vector search and the LIKE keyword scan. The LIKE path only matches full
|
|
3
|
+
// substrings, so a multi-term query ("rust 异步 tokio") misses rows whose
|
|
4
|
+
// terms are scattered. BM25 scores per-token overlap with IDF weighting,
|
|
5
|
+
// which is exactly the gap: identifiers, code fragments and mixed CJK/ASCII
|
|
6
|
+
// queries recall rows the substring scan cannot see.
|
|
7
|
+
|
|
8
|
+
// Tokenizer: ASCII words keep their shape (identifiers like "dsh-mneme" or
|
|
9
|
+
// "ZFS_4421" survive as whole tokens); CJK runs become sliding bigrams
|
|
10
|
+
// (unigram only for single characters), the standard workaround for BM25's
|
|
11
|
+
// whitespace tokenization on Chinese.
|
|
12
|
+
export function tokenize(text) {
|
|
13
|
+
const raw = String(text ?? "").toLowerCase();
|
|
14
|
+
const tokens = [];
|
|
15
|
+
const ascii = raw.match(/[a-z0-9_]+/g) ?? [];
|
|
16
|
+
tokens.push(...ascii);
|
|
17
|
+
const cjkRuns = raw.match(/[\u4e00-\u9fff]+/g) ?? [];
|
|
18
|
+
for (const run of cjkRuns) {
|
|
19
|
+
if (run.length === 1) { tokens.push(run); continue; }
|
|
20
|
+
for (let i = 0; i < run.length - 1; i++) tokens.push(run.slice(i, i + 2));
|
|
21
|
+
}
|
|
22
|
+
return tokens;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const K1 = 1.5; // term-frequency saturation
|
|
26
|
+
const B = 0.75; // length normalization
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Build a BM25 index over documents: [{id, title, content}].
|
|
30
|
+
* Returns { score, search }:
|
|
31
|
+
* score(query, doc) — per-spec ad-hoc scoring (re-tokenizes the doc)
|
|
32
|
+
* search(query, {limit}) — precomputed-tf ranking, scores normalized to
|
|
33
|
+
* [0,1] by the max so BM25 hits can weight-blend with vector/keyword
|
|
34
|
+
* scores on one scale. Rows the query does not touch at all are dropped.
|
|
35
|
+
*/
|
|
36
|
+
export function createBM25Index(documents) {
|
|
37
|
+
const docs = Array.isArray(documents) ? documents.filter(Boolean) : [];
|
|
38
|
+
const N = docs.length;
|
|
39
|
+
const df = new Map();
|
|
40
|
+
const prepared = docs.map((doc) => {
|
|
41
|
+
const tokens = tokenize(`${doc.title ?? ""} ${doc.content ?? ""}`);
|
|
42
|
+
const tf = new Map();
|
|
43
|
+
for (const t of tokens) tf.set(t, (tf.get(t) ?? 0) + 1);
|
|
44
|
+
for (const t of tf.keys()) df.set(t, (df.get(t) ?? 0) + 1);
|
|
45
|
+
return { doc, tf, len: tokens.length };
|
|
46
|
+
});
|
|
47
|
+
const avgLen = N ? prepared.reduce((s, p) => s + p.len, 0) / N : 0 || 1;
|
|
48
|
+
|
|
49
|
+
const idf = (t) => {
|
|
50
|
+
const n = df.get(t) ?? 0;
|
|
51
|
+
return Math.log((N - n + 0.5) / (n + 0.5) + 1);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
function scorePrepared(queryTokens, p) {
|
|
55
|
+
let score = 0;
|
|
56
|
+
for (const t of queryTokens) {
|
|
57
|
+
const f = p.tf.get(t);
|
|
58
|
+
if (!f) continue;
|
|
59
|
+
const norm = p.len ? K1 * (1 - B + B * (p.len / avgLen)) : K1;
|
|
60
|
+
score += idf(t) * ((f * (K1 + 1)) / (f + norm));
|
|
61
|
+
}
|
|
62
|
+
return score;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
score(query, doc) {
|
|
67
|
+
const tokens = tokenize(`${doc?.title ?? ""} ${doc?.content ?? ""}`);
|
|
68
|
+
const tf = new Map();
|
|
69
|
+
for (const t of tokens) tf.set(t, (tf.get(t) ?? 0) + 1);
|
|
70
|
+
const len = tokens.length;
|
|
71
|
+
// Ad-hoc scoring can't see corpus df; fall back to tf-only saturation
|
|
72
|
+
// (df is approximated as 1 so idf ≈ log(N - 0.5 + 1) is constant).
|
|
73
|
+
let score = 0;
|
|
74
|
+
for (const t of tokenize(query)) {
|
|
75
|
+
const f = tf.get(t);
|
|
76
|
+
if (!f) continue;
|
|
77
|
+
const norm = len ? K1 * (1 - B + B * (len / avgLen)) : K1;
|
|
78
|
+
score += idf(t) * ((f * (K1 + 1)) / (f + norm));
|
|
79
|
+
}
|
|
80
|
+
return score;
|
|
81
|
+
},
|
|
82
|
+
search(query, { limit = 20 } = {}) {
|
|
83
|
+
const qTokens = tokenize(query);
|
|
84
|
+
if (!qTokens.length || !N) return [];
|
|
85
|
+
const scored = [];
|
|
86
|
+
for (const p of prepared) {
|
|
87
|
+
const s = scorePrepared(qTokens, p);
|
|
88
|
+
if (s > 0) scored.push({ row: p.doc, raw: s });
|
|
89
|
+
}
|
|
90
|
+
scored.sort((a, b) => b.raw - a.raw);
|
|
91
|
+
const top = scored.slice(0, limit);
|
|
92
|
+
const max = top[0]?.raw || 1;
|
|
93
|
+
return top.map(({ row, raw }) => ({ ...row, score: max ? raw / max : 0 }));
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
package/src/service.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { createHash, randomUUID } from "node:crypto";
|
|
2
2
|
import { TYPE_FILE } from "./mirror.js";
|
|
3
3
|
import { evaluateMemoryQuality } from "./quality-filter.js";
|
|
4
|
+
import { createBM25Index } from "./search/bm25.js";
|
|
5
|
+
import { adaptiveThreshold } from "./search/adaptive.js";
|
|
4
6
|
|
|
5
7
|
const INJECT_TYPES = new Set(["preference", "project", "decision", "summary"]);
|
|
6
8
|
|
|
@@ -307,6 +309,66 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
307
309
|
// Weighted blend factor for hybrid search; exposed so callers can tune it.
|
|
308
310
|
const DEFAULT_HYBRID_WEIGHTS = { vector: 0.6, keyword: 0.4 };
|
|
309
311
|
|
|
312
|
+
// Cosine over two plain arrays (shared by the search-time semantic dedup).
|
|
313
|
+
function cosineVec(a, b) {
|
|
314
|
+
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length || !a.length) return 0;
|
|
315
|
+
let dot = 0, na = 0, nb = 0;
|
|
316
|
+
for (let i = 0; i < a.length; i++) {
|
|
317
|
+
dot += a[i] * b[i];
|
|
318
|
+
na += a[i] * a[i];
|
|
319
|
+
nb += b[i] * b[i];
|
|
320
|
+
}
|
|
321
|
+
if (na === 0 || nb === 0) return 0;
|
|
322
|
+
return dot / (Math.sqrt(na) * Math.sqrt(nb));
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* BM25 third recall path (v0.5.0 1.1). Scores the query tokens against the
|
|
327
|
+
* live non-archived rows and returns the top `limit` hits with scores
|
|
328
|
+
* normalized to [0,1]. Failures degrade to [] — BM25 is a recall booster,
|
|
329
|
+
* never a correctness gate.
|
|
330
|
+
*/
|
|
331
|
+
function bm25Recall(q, limit) {
|
|
332
|
+
if (config?.bm25SearchEnabled === false) return [];
|
|
333
|
+
try {
|
|
334
|
+
const docs = store.list({ limit: 500, includeForgotten: false }).filter((m) => !m.archived);
|
|
335
|
+
if (!docs.length) return [];
|
|
336
|
+
return createBM25Index(docs).search(q, { limit });
|
|
337
|
+
} catch {
|
|
338
|
+
return [];
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Search-time semantic dedup (v0.5.0 2.3): greedy pass dropping candidates
|
|
344
|
+
* whose embedding similarity to an already-kept row exceeds the threshold.
|
|
345
|
+
* Rows without a stored embedding are always kept (no signal = no drop).
|
|
346
|
+
*/
|
|
347
|
+
function semanticDeduplicate(candidates) {
|
|
348
|
+
// Opt-in aggressive mode (default off): collapsing near-duplicates can
|
|
349
|
+
// drop legitimately distinct rows on small embedding models, so it ships
|
|
350
|
+
// behind searchSemanticDedup=true.
|
|
351
|
+
if (config?.searchSemanticDedup !== true || candidates.length < 2) return candidates;
|
|
352
|
+
const threshold = config?.searchSemanticDedupThreshold ?? 0.95;
|
|
353
|
+
try {
|
|
354
|
+
const vecs = store.getEmbeddings(candidates.map((c) => c.id));
|
|
355
|
+
if (vecs.size < 2) return candidates;
|
|
356
|
+
const kept = [];
|
|
357
|
+
for (const c of candidates) {
|
|
358
|
+
const v = vecs.get(c.id);
|
|
359
|
+
if (!v) { kept.push(c); continue; }
|
|
360
|
+
const dup = kept.some((k) => {
|
|
361
|
+
const kv = vecs.get(k.id);
|
|
362
|
+
return kv && cosineVec(v, kv) > threshold;
|
|
363
|
+
});
|
|
364
|
+
if (!dup) kept.push(c);
|
|
365
|
+
}
|
|
366
|
+
return kept;
|
|
367
|
+
} catch {
|
|
368
|
+
return candidates;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
310
372
|
/**
|
|
311
373
|
* Give a keyword-hit row a relevance score in [0,1]: title hits score
|
|
312
374
|
* higher than content hits, then scaled by importance (1-5). This lets
|
|
@@ -371,14 +433,39 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
371
433
|
: embedder.embed.bind(embedder);
|
|
372
434
|
const qv = await embedSingle(q);
|
|
373
435
|
if (qv?.length) {
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
436
|
+
// Adaptive threshold (v0.5.0 1.2): the fetch runs at the loosest
|
|
437
|
+
// branch floor so the head-gap rule can still re-admit the tail;
|
|
438
|
+
// the final cutoff is computed against the fetched score
|
|
439
|
+
// distribution. Explicit `threshold` wins; disabled → legacy 0.
|
|
440
|
+
const adaptive = config?.adaptiveThresholdEnabled !== false;
|
|
441
|
+
const fetchThreshold = adaptive && threshold === undefined
|
|
442
|
+
? Math.min(0.5, adaptiveThreshold(q))
|
|
443
|
+
: (threshold ?? 0);
|
|
444
|
+
const search = vectorIndex
|
|
445
|
+
? vectorIndex.search(qv, { limit: lim * 2, threshold: fetchThreshold })
|
|
446
|
+
: store.searchVector(qv, { limit: lim * 2, threshold: fetchThreshold });
|
|
447
|
+
const finalThreshold = adaptive && threshold === undefined
|
|
448
|
+
? adaptiveThreshold(q, search)
|
|
449
|
+
: (threshold ?? 0);
|
|
450
|
+
vector = search
|
|
451
|
+
.filter((m) => (m.score ?? 1) >= finalThreshold)
|
|
452
|
+
.map((m) => ({ ...m, vector: true, source: "vector" }));
|
|
378
453
|
}
|
|
379
454
|
} catch { /* vector unavailable: keep keyword results */ }
|
|
380
455
|
}
|
|
381
456
|
|
|
457
|
+
// BM25 third path (v0.5.0 1.1): IDF-weighted token overlap recalls rows
|
|
458
|
+
// whose query terms are scattered — the gap LIKE substring matching
|
|
459
|
+
// cannot close. Scores are already normalized to [0,1].
|
|
460
|
+
const bm25 = bm25Recall(q, lim).map((m) => ({ ...m, source: "bm25" }));
|
|
461
|
+
// Loose blend weight: BM25 confirms and backfills, never dominates the
|
|
462
|
+
// semantic signal. Same-memory overlap boosts, unseen ids backfill.
|
|
463
|
+
const wb = 0.3;
|
|
464
|
+
// Path bookkeeping for the boost rule below: which ids each semantic
|
|
465
|
+
// recall path surfaced.
|
|
466
|
+
const vectorIds = new Set(vector.map((m) => m.id));
|
|
467
|
+
const keywordIds = new Set(keyword.map((m) => m.id));
|
|
468
|
+
|
|
382
469
|
// Hybrid blending weights from config when provided.
|
|
383
470
|
const wv = config?.hybridSearchVectorWeight ?? DEFAULT_HYBRID_WEIGHTS.vector;
|
|
384
471
|
const wk = config?.hybridSearchKeywordWeight ?? DEFAULT_HYBRID_WEIGHTS.keyword;
|
|
@@ -387,9 +474,10 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
387
474
|
if (mode === "keyword") {
|
|
388
475
|
merged = keyword;
|
|
389
476
|
} else if (mode === "vector" || mode === "hybrid") {
|
|
390
|
-
// semantic-first: vector recalls lead, keyword
|
|
391
|
-
// Weighted blend when
|
|
392
|
-
// vector order leads (it is the semantic signal),
|
|
477
|
+
// semantic-first: vector recalls lead, keyword + BM25 fill remaining
|
|
478
|
+
// slots. Weighted blend when sides scored the same memory; otherwise
|
|
479
|
+
// vector order leads (it is the semantic signal), lexical paths
|
|
480
|
+
// backfill.
|
|
393
481
|
const byId = new Map();
|
|
394
482
|
for (const m of vector) {
|
|
395
483
|
const rec = byId.get(m.id);
|
|
@@ -404,22 +492,48 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
404
492
|
byId.set(m.id, m);
|
|
405
493
|
}
|
|
406
494
|
}
|
|
407
|
-
|
|
495
|
+
for (const m of bm25) {
|
|
496
|
+
const rec = byId.get(m.id);
|
|
497
|
+
if (rec) {
|
|
498
|
+
// Boost rule: a row the LIKE keyword path already hit carries the
|
|
499
|
+
// query as a substring, so BM25 tokens are trivially present —
|
|
500
|
+
// boosting it double-counts lexical evidence. Only vector-recalled
|
|
501
|
+
// rows (lexical hit is genuinely new information) get the boost.
|
|
502
|
+
if (keywordIds.has(m.id)) continue;
|
|
503
|
+
byId.set(m.id, { ...rec, score: (rec.score ?? 0) + wb * (m.score ?? 0) });
|
|
504
|
+
} else {
|
|
505
|
+
byId.set(m.id, { ...m, score: wb * (m.score ?? 0) });
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
const ranked = [...byId.values()]
|
|
509
|
+
.sort((a, b) => (b.score ?? 0) - (a.score ?? 0))
|
|
510
|
+
.map((r) => ({ ...r, score: Math.max(0, Math.min(1, r.score ?? 0)) }));
|
|
408
511
|
merged = ranked.slice(0, lim);
|
|
409
512
|
if (merged.length < lim && !merged.length) {
|
|
410
513
|
// Vector unavailable entirely: fall back to plain keyword.
|
|
411
514
|
merged = keyword.slice(0, lim);
|
|
412
515
|
}
|
|
413
516
|
} else {
|
|
414
|
-
// auto: keyword leads, vector
|
|
517
|
+
// auto: keyword leads, vector + BM25 fill remaining slots (legacy
|
|
518
|
+
// behavior, extended with the third path)
|
|
415
519
|
merged = keyword.slice(0, lim);
|
|
416
520
|
const seen = new Set(merged.map((m) => m.id));
|
|
417
521
|
for (const m of vector) {
|
|
418
522
|
if (merged.length >= lim) break;
|
|
419
523
|
if (!seen.has(m.id)) { seen.add(m.id); merged.push(m); }
|
|
420
524
|
}
|
|
525
|
+
for (const m of bm25) {
|
|
526
|
+
if (merged.length >= lim) break;
|
|
527
|
+
if (!seen.has(m.id)) { seen.add(m.id); merged.push(m); }
|
|
528
|
+
}
|
|
421
529
|
}
|
|
422
530
|
|
|
531
|
+
// Search-time semantic dedup (v0.5.0 2.3): near-duplicate rows are
|
|
532
|
+
// dropped before the reranker sees them, so topK slots carry distinct
|
|
533
|
+
// information instead of the same memory twice. Keyword mode is exempt —
|
|
534
|
+
// it is the documented text-only path and must not be altered by
|
|
535
|
+
// embedding state.
|
|
536
|
+
merged = mode === "keyword" ? merged : semanticDeduplicate(merged);
|
|
423
537
|
merged = merged.slice(0, lim);
|
|
424
538
|
let result = useRerank && reranker && merged.length
|
|
425
539
|
? await rerankCandidates(q, merged, lim)
|
|
@@ -806,6 +920,20 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
806
920
|
candidates = merged;
|
|
807
921
|
}
|
|
808
922
|
}
|
|
923
|
+
// Topic-ranked selection (v0.5.0 2.2): when the current query's vector is
|
|
924
|
+
// available the whole candidate list is re-ordered by similarity to that
|
|
925
|
+
// vector, so the injected slots go to memories on the current topic
|
|
926
|
+
// rather than to the rule-based order. Rows the index did not return
|
|
927
|
+
// keep their relative order after the scored ones.
|
|
928
|
+
if (config?.selectiveInjectEnabled !== false && Array.isArray(queryVector) && queryVector.length && vectorIndex) {
|
|
929
|
+
try {
|
|
930
|
+
const hits = vectorIndex.search(queryVector, { limit: 200, threshold: 0 });
|
|
931
|
+
const sim = new Map(hits.map((m) => [m.id, m.score ?? 0]));
|
|
932
|
+
if (sim.size) {
|
|
933
|
+
candidates = [...candidates].sort((a, b) => (sim.get(b.id) ?? -1) - (sim.get(a.id) ?? -1));
|
|
934
|
+
}
|
|
935
|
+
} catch { /* topic re-rank unavailable: keep rule-based order */ }
|
|
936
|
+
}
|
|
809
937
|
const selected = candidates.slice(0, maxItems);
|
|
810
938
|
touchRecalled(selected);
|
|
811
939
|
return selected;
|
|
@@ -1352,6 +1480,7 @@ export function createService({ store, mirror, config, onWrite, logger }) {
|
|
|
1352
1480
|
findEntityByName: (n) => store.findEntityByName(n),
|
|
1353
1481
|
findEntityById: (id) => store.findEntityById(id),
|
|
1354
1482
|
getAttrsByMemory: (id) => store.getAttrsByMemory(id),
|
|
1483
|
+
getCurrentAttrs: (id) => store.getCurrentAttrs(id),
|
|
1355
1484
|
migrateAttrsToMemory: (fromId, toId, now) => store.migrateAttrsToMemory(fromId, toId, now)
|
|
1356
1485
|
};
|
|
1357
1486
|
}
|
package/src/store.js
CHANGED
|
@@ -911,6 +911,29 @@ export function createStore(path) {
|
|
|
911
911
|
db.prepare("UPDATE memories SET embedding = ? WHERE id = ?").run(json, id);
|
|
912
912
|
}
|
|
913
913
|
|
|
914
|
+
/** Batch fetch stored embeddings by id (v0.5.0 search-time semantic dedup).
|
|
915
|
+
* Returns a Map(id → number[]); rows without a parseable embedding are
|
|
916
|
+
* simply absent from the map. */
|
|
917
|
+
function getEmbeddings(ids) {
|
|
918
|
+
const out = new Map();
|
|
919
|
+
const list = (Array.isArray(ids) ? ids : []).filter(Boolean);
|
|
920
|
+
for (let i = 0; i < list.length; i += 100) {
|
|
921
|
+
const chunk = list.slice(i, i + 100);
|
|
922
|
+
const rows = db.prepare(
|
|
923
|
+
`SELECT id, embedding FROM memories
|
|
924
|
+
WHERE embedding IS NOT NULL AND embedding != ''
|
|
925
|
+
AND id IN (${chunk.map(() => "?").join(",")})`
|
|
926
|
+
).all(...chunk);
|
|
927
|
+
for (const row of rows) {
|
|
928
|
+
try {
|
|
929
|
+
const vec = JSON.parse(row.embedding);
|
|
930
|
+
if (Array.isArray(vec) && vec.length) out.set(row.id, vec);
|
|
931
|
+
} catch { /* corrupt row: skip */ }
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
return out;
|
|
935
|
+
}
|
|
936
|
+
|
|
914
937
|
function embeddedCount() {
|
|
915
938
|
return db.prepare(
|
|
916
939
|
"SELECT count(*) AS c FROM memories WHERE embedding IS NOT NULL AND embedding != ''"
|
|
@@ -1852,6 +1875,7 @@ export function createStore(path) {
|
|
|
1852
1875
|
all,
|
|
1853
1876
|
search,
|
|
1854
1877
|
setEmbedding,
|
|
1878
|
+
getEmbeddings,
|
|
1855
1879
|
embeddedCount,
|
|
1856
1880
|
needsEmbedding,
|
|
1857
1881
|
searchVector,
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { runBenchmark, TEST_CASES } from "../scripts/benchmark-recall.js";
|
|
4
|
+
|
|
5
|
+
// The benchmark harness must stay a working evaluation: it runs the real
|
|
6
|
+
// searchMemories pipeline over the seeded store and the fused configuration
|
|
7
|
+
// must not fall behind the legacy one (that is the whole point of the third
|
|
8
|
+
// recall path).
|
|
9
|
+
test("benchmark harness runs both configurations", async () => {
|
|
10
|
+
const report = await runBenchmark({ topK: 5 });
|
|
11
|
+
assert.equal(report.runs.length, 2);
|
|
12
|
+
assert.equal(report.runs[0].config, "legacy");
|
|
13
|
+
assert.equal(report.runs[1].config, "fused");
|
|
14
|
+
for (const run of report.runs) {
|
|
15
|
+
assert.equal(run.rows.length, TEST_CASES.length);
|
|
16
|
+
assert.ok(run.recallAtK >= 0 && run.recallAtK <= 1);
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test("fused configuration never trails legacy on Recall@5", async () => {
|
|
21
|
+
const report = await runBenchmark({ topK: 5 });
|
|
22
|
+
const [legacy, fused] = report.runs;
|
|
23
|
+
assert.ok(
|
|
24
|
+
fused.recallAtK >= legacy.recallAtK,
|
|
25
|
+
`fused (${fused.recallAtK}) must be >= legacy (${legacy.recallAtK})`
|
|
26
|
+
);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("test cases cover the scattered-term BM25 territory", () => {
|
|
30
|
+
assert.ok(TEST_CASES.length >= 10);
|
|
31
|
+
assert.ok(TEST_CASES.some((tc) => tc.expected.length >= 2), "multi-target cases present");
|
|
32
|
+
for (const tc of TEST_CASES) {
|
|
33
|
+
assert.ok(tc.query && tc.expected.length > 0);
|
|
34
|
+
}
|
|
35
|
+
});
|
package/test/client.test.js
CHANGED
|
@@ -25,20 +25,210 @@ test("client bundle is lib-only with no src counterpart", () => {
|
|
|
25
25
|
assert.equal(existsSync(join(root, "lib/client.js")), true, "lib/client.js must exist");
|
|
26
26
|
});
|
|
27
27
|
|
|
28
|
-
//
|
|
29
|
-
//
|
|
30
|
-
//
|
|
31
|
-
//
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
assert.equal(
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
28
|
+
// The memory entry lives at the sidebar foot, not in the settings modal: the
|
|
29
|
+
// migration must register into `sidebar.footer.action` (the list slot the
|
|
30
|
+
// sidebar shell renders beside Settings) and must not keep a `settings.section`
|
|
31
|
+
// registration, or the entry would appear twice under different hosts.
|
|
32
|
+
test("memory entry registers into the sidebar foot slot", () => {
|
|
33
|
+
assert.ok(
|
|
34
|
+
clientSource.includes('ctx.slots.inject("sidebar.footer.action"'),
|
|
35
|
+
"client must inject into sidebar.footer.action"
|
|
36
|
+
);
|
|
37
|
+
assert.equal(
|
|
38
|
+
clientSource.includes('"settings.section"'),
|
|
39
|
+
false,
|
|
40
|
+
"the old settings.section registration must be gone"
|
|
41
|
+
);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// The drawer era is over as the PRIMARY surface: the sidebar entry must
|
|
45
|
+
// activate the main-area memory library tab first. The one sanctioned
|
|
46
|
+
// exception is the hero screen — the host hides the whole tab ring while a
|
|
47
|
+
// session is blank, so activation legitimately fails there and the same
|
|
48
|
+
// click falls back to a full-viewport overlay (same MemoryExplorer, not the
|
|
49
|
+
// old 420px side drawer). A dialog role stays banned either way.
|
|
50
|
+
test("tab-first activation with hero-screen overlay fallback", () => {
|
|
51
|
+
assert.equal(
|
|
52
|
+
clientSource.includes("role: \"dialog\""),
|
|
53
|
+
false,
|
|
54
|
+
"no dialog surface should remain"
|
|
55
|
+
);
|
|
56
|
+
assert.ok(
|
|
57
|
+
clientSource.includes("activateExplorerTab(t(\"memory.view.label\"))"),
|
|
58
|
+
"the sidebar trigger must activate the memory library tab by its label"
|
|
59
|
+
);
|
|
60
|
+
assert.ok(
|
|
61
|
+
clientSource.includes("activateExplorerTab(t(\"memory.view.label\")).then((ok) => { if (!ok) setOpen(true); })"),
|
|
62
|
+
"a failed tab activation must open the fallback overlay"
|
|
63
|
+
);
|
|
64
|
+
assert.ok(
|
|
65
|
+
/if \(!tab\) \{ resolve\(false\); return; \}/.test(clientSource),
|
|
66
|
+
"activation must resolve false when the tab ring is absent (hero screen)"
|
|
67
|
+
);
|
|
68
|
+
assert.ok(
|
|
69
|
+
/aria-selected.*true/.test(clientSource),
|
|
70
|
+
"activation is only confirmed once the host marks the tab selected"
|
|
71
|
+
);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// The fallback overlay reuses the full MemoryExplorer (three columns, graph,
|
|
75
|
+
// settings) at viewport size — not the old side drawer — and closes on Esc
|
|
76
|
+
// or the close button.
|
|
77
|
+
test("hero fallback overlay is a full-viewport MemoryExplorer with a close affordance", () => {
|
|
78
|
+
assert.ok(
|
|
79
|
+
clientSource.includes('.mneme-overlay{position:fixed;inset:0'),
|
|
80
|
+
"the overlay must cover the full viewport"
|
|
81
|
+
);
|
|
82
|
+
assert.ok(
|
|
83
|
+
clientSource.includes('h(MemoryExplorer, { t })'),
|
|
84
|
+
"the overlay renders the same MemoryExplorer component as the tab"
|
|
85
|
+
);
|
|
86
|
+
assert.ok(
|
|
87
|
+
clientSource.includes('e.key === "Escape"'),
|
|
88
|
+
"Esc must close the overlay"
|
|
89
|
+
);
|
|
90
|
+
assert.ok(
|
|
91
|
+
clientSource.includes('"memory.overlay.close"'),
|
|
92
|
+
"the overlay ships a localized close label in both dictionaries"
|
|
93
|
+
);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// The sidebar hands each footer action only its column state: a wide row
|
|
97
|
+
// (icon + label) when expanded, a bare rail icon when collapsed.
|
|
98
|
+
test("trigger renders a wide row or a rail icon from the wide flag", () => {
|
|
99
|
+
assert.ok(
|
|
100
|
+
/wide \? "mneme-trigger" : "mneme-trigger mneme-rail"/.test(clientSource),
|
|
101
|
+
"trigger must branch on the wide flag"
|
|
102
|
+
);
|
|
103
|
+
assert.ok(
|
|
104
|
+
/wide && h\("span", \{ className: "mneme-trigger-label" \}/.test(clientSource),
|
|
105
|
+
"the label span must render only when wide"
|
|
106
|
+
);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// The full-width memory browser lives in the conversation view ring beside
|
|
110
|
+
// Chat / Trajectory, so the client must inject into `conversation.view` with
|
|
111
|
+
// a stable entry id (the active view is persisted by that id).
|
|
112
|
+
test("explorer registers into the conversation view ring", () => {
|
|
113
|
+
assert.ok(
|
|
114
|
+
clientSource.includes('ctx.slots.inject("conversation.view"'),
|
|
115
|
+
"client must inject into conversation.view"
|
|
116
|
+
);
|
|
117
|
+
assert.ok(
|
|
118
|
+
clientSource.includes('id: "dsh-mneme-memory"'),
|
|
119
|
+
"the view entry needs a stable, unique id"
|
|
120
|
+
);
|
|
121
|
+
assert.ok(
|
|
122
|
+
clientSource.includes('"memory.view.label"'),
|
|
123
|
+
"the tab label must come from the memory.view.label dictionary key"
|
|
124
|
+
);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// The graph toggle must not read as "share": the primitives share icon is
|
|
128
|
+
// banned and a custom node-graph glyph takes its place.
|
|
129
|
+
test("graph toggle uses a node-graph glyph, not the share icon", () => {
|
|
130
|
+
assert.equal(
|
|
131
|
+
clientSource.includes("IconShareOutline16"),
|
|
132
|
+
false,
|
|
133
|
+
"IconShareOutline16 reads as share and must not appear"
|
|
134
|
+
);
|
|
135
|
+
assert.ok(
|
|
136
|
+
clientSource.includes("GraphNodesIcon"),
|
|
137
|
+
"the custom node-graph icon must back the graph toggle"
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
// Every memory feature lives in the main-area library now: the explorer
|
|
142
|
+
// hosts three sub-views (browse / graph / settings) switched by tabs whose
|
|
143
|
+
// labels come from dedicated dictionary keys.
|
|
144
|
+
test("explorer hosts browse, graph and settings sub-views", () => {
|
|
145
|
+
for (const key of ["tabMemory", "tabGraph", "tabSettings"]) {
|
|
146
|
+
assert.ok(
|
|
147
|
+
clientSource.includes(`"memory.explorer.${key}"`),
|
|
148
|
+
`sub-view labels must come from memory.explorer.${key}`
|
|
149
|
+
);
|
|
43
150
|
}
|
|
151
|
+
assert.ok(
|
|
152
|
+
clientSource.includes('h(GraphPanel, { t, focusEntity: graphFocus, onJumpMemory: jumpToMemory })'),
|
|
153
|
+
"the graph panel must be embedded as a sub-view"
|
|
154
|
+
);
|
|
155
|
+
assert.ok(
|
|
156
|
+
clientSource.includes('h(SettingsContent, { t })'),
|
|
157
|
+
"the settings forms must be embedded as a sub-view"
|
|
158
|
+
);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// The graph panel jumps back into the browser: related-memory rows and the
|
|
162
|
+
// edge source button must land on the browse tab with the target selected.
|
|
163
|
+
test("graph jump lands on the selected memory in the browser", () => {
|
|
164
|
+
assert.ok(
|
|
165
|
+
/onClick: \(\) => onJumpMemory && onJumpMemory\(m\)/.test(clientSource),
|
|
166
|
+
"related-memory rows must jump via onJumpMemory(m)"
|
|
167
|
+
);
|
|
168
|
+
assert.ok(
|
|
169
|
+
clientSource.includes("onJumpMemory({ id: selected.edge.memory_id })"),
|
|
170
|
+
"the edge source button must jump to the origin memory by id"
|
|
171
|
+
);
|
|
172
|
+
assert.ok(
|
|
173
|
+
/const jumpToMemory = \(target\) => \{/.test(clientSource),
|
|
174
|
+
"jumpToMemory must reset filters and select the target"
|
|
175
|
+
);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
// "entity:" in the browser search is the graph entry grammar: it must offer
|
|
179
|
+
// a jump chip instead of filtering the list.
|
|
180
|
+
test("entity: search grammar offers a graph jump", () => {
|
|
181
|
+
assert.ok(
|
|
182
|
+
clientSource.includes('query.trim().startsWith("entity:")'),
|
|
183
|
+
"the entity: prefix must be recognized"
|
|
184
|
+
);
|
|
185
|
+
assert.ok(
|
|
186
|
+
/onClick: \(\) => openGraphFor\(entityQuery\)/.test(clientSource),
|
|
187
|
+
"the jump chip must switch to the graph sub-view"
|
|
188
|
+
);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// The explorer is a three-pane layout: types with counts, a month→day time
|
|
192
|
+
// tree, and a detail pane rendering the untruncated content.
|
|
193
|
+
test("explorer lays out types, timeline, and full-text detail", () => {
|
|
194
|
+
assert.ok(
|
|
195
|
+
clientSource.includes('className: "mneme-xmain"'),
|
|
196
|
+
"the three-column grid must be present"
|
|
197
|
+
);
|
|
198
|
+
assert.ok(
|
|
199
|
+
clientSource.includes('className: "mneme-xdcontent"'),
|
|
200
|
+
"the detail pane must render the full content"
|
|
201
|
+
);
|
|
202
|
+
assert.ok(
|
|
203
|
+
/toLocaleDateString\(undefined, \{ year: "numeric", month: "long" \}\)/.test(clientSource),
|
|
204
|
+
"month groups must format via the host locale, not hardcoded strings"
|
|
205
|
+
);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
// The library page must read as a first-party view: the chrome resolves to
|
|
209
|
+
// the host's design tokens (layer-1 canvas, brand-blue active states) and
|
|
210
|
+
// the boxed-panel / pill-chip patterns of the drawer era must stay gone.
|
|
211
|
+
test("explorer chrome aligns with the host design system", () => {
|
|
212
|
+
assert.ok(
|
|
213
|
+
clientSource.includes("background:var(--dsw-alias-bg-layer-1)"),
|
|
214
|
+
"the page canvas must sit on the host bg-layer-1 token"
|
|
215
|
+
);
|
|
216
|
+
assert.ok(
|
|
217
|
+
/\.mneme-vtab\.mneme-active::after/.test(clientSource),
|
|
218
|
+
"active sub-tabs use the host underline treatment"
|
|
219
|
+
);
|
|
220
|
+
assert.ok(
|
|
221
|
+
clientSource.includes(".mneme-vtab.mneme-active{color:var(--dsw-alias-state-business-primary)}"),
|
|
222
|
+
"active sub-tab text must turn the host brand blue"
|
|
223
|
+
);
|
|
224
|
+
assert.equal(
|
|
225
|
+
/\.mneme-xmain\{[^}]*border:1px/.test(clientSource),
|
|
226
|
+
false,
|
|
227
|
+
"the three-column layout must not wrap itself in a boxed panel"
|
|
228
|
+
);
|
|
229
|
+
assert.equal(
|
|
230
|
+
clientSource.includes("border-radius:999px"),
|
|
231
|
+
false,
|
|
232
|
+
"pill chips belong to the drawer era and must stay gone"
|
|
233
|
+
);
|
|
44
234
|
});
|