@modusensus/dsh-mneme 0.2.4 → 0.2.5

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 (45) hide show
  1. package/README.md +10 -9
  2. package/lib/config.js +5 -2
  3. package/lib/dream/decisions.js +174 -62
  4. package/lib/dream.js +30 -5
  5. package/lib/index.js +3 -1
  6. package/lib/mirror.js +7 -1
  7. package/lib/service.js +117 -3
  8. package/lib/store.js +40 -0
  9. package/lib/tools.js +47 -4
  10. package/package.json +3 -1
  11. package/scripts/benchmark-embed.js +201 -0
  12. package/scripts/benchmark-rerank.js +166 -0
  13. package/scripts/e2e-dsh.js +216 -0
  14. package/scripts/stress-dsh.js +255 -0
  15. package/scripts/sync-lib.js +47 -0
  16. package/src/config.js +5 -2
  17. package/src/dream/decisions.js +174 -62
  18. package/src/dream.js +30 -5
  19. package/src/index.js +3 -1
  20. package/src/mirror.js +7 -1
  21. package/src/service.js +117 -3
  22. package/src/store.js +40 -0
  23. package/src/tools.js +47 -4
  24. package/test/api.test.js +385 -0
  25. package/test/audit.test.js +290 -0
  26. package/test/client.test.js +26 -0
  27. package/test/clustering.test.js +100 -0
  28. package/test/commands.test.js +69 -0
  29. package/test/config.test.js +31 -0
  30. package/test/dream.test.js +526 -0
  31. package/test/helpers/dream-mock.js +82 -0
  32. package/test/inject.test.js +82 -0
  33. package/test/local-embedder.test.js +227 -0
  34. package/test/mirror.test.js +249 -0
  35. package/test/reflection.test.js +226 -0
  36. package/test/reranker.test.js +197 -0
  37. package/test/semantic.test.js +123 -0
  38. package/test/service-search.test.js +169 -0
  39. package/test/service.test.js +198 -0
  40. package/test/settings.test.js +101 -0
  41. package/test/store.test.js +293 -0
  42. package/test/stress.test.js +209 -0
  43. package/test/summarize.test.js +156 -0
  44. package/test/tools.test.js +265 -0
  45. package/test/vector-index.test.js +205 -0
@@ -0,0 +1,26 @@
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { readFileSync, existsSync } from "node:fs";
4
+ import { fileURLToPath } from "node:url";
5
+ import { dirname, join } from "node:path";
6
+
7
+ const root = join(dirname(fileURLToPath(import.meta.url)), "..");
8
+ const clientSource = readFileSync(join(root, "lib/client.js"), "utf8");
9
+ const pkg = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
10
+
11
+ // The Web client bundle registers itself via __ModuleLoader__.load. DSH
12
+ // resolves the bundle by plugin package name, so the registered id must match
13
+ // package.json `name` exactly (a mismatch surfaces as "loaded without
14
+ // registering '@modusensus/dsh-mneme'" in the DSH client).
15
+ test("client bundle registers under the package name", () => {
16
+ const match = clientSource.match(/__ModuleLoader__\.load\(\{\s*id:\s*"([^"]+)"/);
17
+ assert.ok(match, "client bundle must call __ModuleLoader__.load with an id");
18
+ assert.equal(match[1], pkg.name, "registered id must equal package.json name");
19
+ });
20
+
21
+ // client.js is hand-authored under lib/ only (no src/ counterpart), so the
22
+ // src->lib sync must never prune it.
23
+ test("client bundle is lib-only with no src counterpart", () => {
24
+ assert.equal(existsSync(join(root, "src/client.js")), false, "src/ must not contain client.js");
25
+ assert.equal(existsSync(join(root, "lib/client.js")), true, "lib/client.js must exist");
26
+ });
@@ -0,0 +1,100 @@
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { cosineSimilarity, kMeans, clusterMemories, findPotentialConflicts } from "../src/dream/clustering.js";
4
+
5
+ test("cosineSimilarity: identical vectors => 1", () => {
6
+ assert.equal(cosineSimilarity([1, 2, 3], [1, 2, 3]), 1);
7
+ });
8
+
9
+ test("cosineSimilarity: orthogonal vectors => 0 (approx)", () => {
10
+ assert.ok(Math.abs(cosineSimilarity([1, 0], [0, 1])) < 1e-9);
11
+ });
12
+
13
+ test("cosineSimilarity: mismatched or empty lengths => 0", () => {
14
+ assert.equal(cosineSimilarity([1, 2], [1, 2, 3]), 0);
15
+ assert.equal(cosineSimilarity([], []), 0);
16
+ });
17
+
18
+ test("cosineSimilarity: zero-norm vector => 0", () => {
19
+ assert.equal(cosineSimilarity([0, 0], [1, 1]), 0);
20
+ });
21
+
22
+ // Three well-separated point groups in the plane.
23
+ const GROUP1 = [[0, 0], [0, 0.1], [0.1, 0], [0.1, 0.1]];
24
+ const GROUP2 = [[10, 10], [10, 10.1], [10.1, 10], [10.1, 10.1]];
25
+ const GROUP3 = [[-10, -10], [-10, -9.9], [-9.9, -10], [-9.9, -9.9]];
26
+ const SEPARATED = [...GROUP1, ...GROUP2, ...GROUP3];
27
+
28
+ test("kMeans: three separated groups cluster correctly (labels may permute)", () => {
29
+ const clusters = kMeans(SEPARATED, 3, { maxIter: 100 });
30
+ assert.equal(clusters.length, 3);
31
+ // Content-based check: each cluster must fit exactly one expected group.
32
+ const expectedGroups = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]];
33
+ for (const c of clusters) {
34
+ assert.ok(c.length >= 1);
35
+ const fits = expectedGroups.filter((g) => c.every((idx) => g.includes(idx)));
36
+ assert.equal(fits.length, 1, `cluster ${JSON.stringify(c)} spans multiple groups`);
37
+ }
38
+ // Every index assigned exactly once.
39
+ assert.deepEqual(clusters.flat().sort((a, b) => a - b), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
40
+ });
41
+
42
+ test("kMeans: k >= n returns a single cluster of all indices", () => {
43
+ assert.deepEqual(kMeans(SEPARATED, 12), [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]);
44
+ assert.deepEqual(kMeans(SEPARATED, 0), [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]]);
45
+ });
46
+
47
+ test("kMeans: empty input => no clusters", () => {
48
+ assert.deepEqual(kMeans([], 3), []);
49
+ });
50
+
51
+ test("kMeans: empty clusters get reseeded and still terminate", () => {
52
+ // Many points near two poles with k=3 forces one cluster empty on seed.
53
+ const vectors = [
54
+ ...Array.from({ length: 30 }, () => [0, 0]),
55
+ ...Array.from({ length: 30 }, () => [1, 1])
56
+ ];
57
+ const clusters = kMeans(vectors, 3, { maxIter: 50 });
58
+ assert.equal(clusters.length, 3);
59
+ assert.equal(clusters.flat().length, vectors.length);
60
+ });
61
+
62
+ test("clusterMemories: preserves object references and group count", () => {
63
+ const memories = SEPARATED.map((v, i) => ({ id: `m${i}`, type: "project", title: `t${i}` }));
64
+ const groups = clusterMemories(memories, SEPARATED, 3);
65
+ assert.equal(groups.length, 3);
66
+ assert.equal(groups.flat().length, memories.length);
67
+ // Reference identity preserved: every group member is an object from input.
68
+ const all = groups.flat();
69
+ for (const m of all) assert.ok(memories.includes(m), "cluster contains a foreign object");
70
+ // Full coverage: each source object appears exactly once across groups.
71
+ assert.equal(new Set(all).size, memories.length);
72
+ assert.equal(groups.flat().find((m) => m.id === "m5"), memories[5]);
73
+ });
74
+
75
+ test("findPotentialConflicts: only same-type highly-similar pairs reported", () => {
76
+ const memories = [
77
+ { id: "a", type: "preference", content: "like cold coffee" },
78
+ { id: "b", type: "preference", content: "hate cold coffee" },
79
+ { id: "c", type: "project", content: "ship the plugin" }
80
+ ];
81
+ const vectors = [
82
+ [1, 0, 0],
83
+ [0.99, 0.02, 0], // nearly parallel to a, same type => conflict
84
+ [0, 1, 0] // different type, ignored even if near
85
+ ];
86
+ const pairs = findPotentialConflicts(memories, vectors, 0.85);
87
+ assert.equal(pairs.length, 1);
88
+ assert.equal(pairs[0].a, memories[0]);
89
+ assert.equal(pairs[0].b, memories[1]);
90
+ assert.ok(pairs[0].similarity > 0.85);
91
+ });
92
+
93
+ test("findPotentialConflicts: below threshold or different type => no pairs", () => {
94
+ const memories = [
95
+ { id: "x", type: "preference" },
96
+ { id: "y", type: "project" }
97
+ ];
98
+ assert.deepEqual(findPotentialConflicts(memories, [[1, 0], [0, 1]]), []);
99
+ assert.deepEqual(findPotentialConflicts(memories, [[1, 0], [0.9, 0.1]], 0.95), []);
100
+ });
@@ -0,0 +1,69 @@
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { createStore } from "../src/store.js";
4
+ import { createSettings } from "../src/settings.js";
5
+ import { createCommandManager } from "../src/commands.js";
6
+
7
+ function setup() {
8
+ const store = createStore(":memory:");
9
+ const settings = createSettings(store.db);
10
+ const registered = new Map(); // name -> definition
11
+ const ctx = {
12
+ commands: {
13
+ register(def) {
14
+ registered.set(def.name, def);
15
+ return () => registered.delete(def.name);
16
+ }
17
+ }
18
+ };
19
+ const manager = createCommandManager({ ctx, settings, logger: null });
20
+ return { store, settings, manager, registered };
21
+ }
22
+
23
+ test("sync registers all stored commands", () => {
24
+ const { store, settings, manager, registered } = setup();
25
+ settings.addCommand({ name: "agenda", description: "d", instruction: "列议程" });
26
+ settings.addCommand({ name: "banner", instruction: "打印横幅" });
27
+ manager.sync();
28
+ assert.equal(registered.size, 2);
29
+ assert.ok(registered.has("agenda") && registered.has("banner"));
30
+ store.close();
31
+ });
32
+
33
+ test("add registers live; remove unregisters live", () => {
34
+ const { store, manager, registered } = setup();
35
+ const cmd = manager.add({ name: "review", description: "r", instruction: "审查代码" });
36
+ assert.ok(registered.has("review"), "registered after add");
37
+ assert.equal(registered.get("review").description, "r");
38
+ assert.equal(manager.remove(cmd.id), true);
39
+ assert.ok(!registered.has("review"), "unregistered after remove");
40
+ store.close();
41
+ });
42
+
43
+ test("handler returns the instruction as success text", async () => {
44
+ const { store, manager, registered } = setup();
45
+ manager.add({ name: "fmt", description: "格式化", instruction: "请按项目的 lint 规则格式化当前文件" });
46
+ const def = registered.get("fmt");
47
+ assert.ok(def, "definition registered");
48
+ const result = await def.handler({ agent: {}, rawInput: "", signal: null });
49
+ assert.equal(result.kind, "success");
50
+ assert.equal(result.text, "请按项目的 lint 规则格式化当前文件");
51
+ store.close();
52
+ });
53
+
54
+ test("remove of missing id returns false", () => {
55
+ const { store, manager } = setup();
56
+ assert.equal(manager.remove("nope"), false);
57
+ store.close();
58
+ });
59
+
60
+ test("dispose unregisters everything", () => {
61
+ const { store, settings, manager, registered } = setup();
62
+ settings.addCommand({ name: "a", instruction: "x" });
63
+ settings.addCommand({ name: "b", instruction: "y" });
64
+ manager.sync();
65
+ assert.equal(registered.size, 2);
66
+ manager.dispose();
67
+ assert.equal(registered.size, 0);
68
+ store.close();
69
+ });
@@ -0,0 +1,31 @@
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { readFileSync } from "node:fs";
4
+ import { fileURLToPath } from "node:url";
5
+ import { Config } from "../src/config.js";
6
+
7
+ // ---------------------------------------------------------------- rerank opt-in (item ⑥)
8
+
9
+ test("rerank is opt-in: default config does not enable the local reranker", () => {
10
+ const cfg = Config({});
11
+ assert.equal(cfg.rerankEnabled, false, "rerankEnabled defaults to false");
12
+ assert.equal(cfg.rerankProvider, "none", "rerankProvider defaults to none");
13
+ // The plugin gate in index.js: LocalReranker is only constructed when both
14
+ // hold — under defaults the gate is closed, so onnxruntime is never loaded.
15
+ assert.equal(cfg.rerankEnabled && cfg.rerankProvider === "local", false, "gate closed by default");
16
+ const enabled = Config({ rerankEnabled: true, rerankProvider: "local" });
17
+ assert.equal(enabled.rerankEnabled && enabled.rerankProvider === "local", true, "explicit opt-in opens the gate");
18
+ });
19
+
20
+ test("startup probe: the reranker module never statically imports transformers/onnxruntime", () => {
21
+ // LocalReranker is imported eagerly by index.js, so a bare install must not
22
+ // pull onnxruntime in at module load. The heavy load is a lazy dynamic import
23
+ // that only runs inside init(), which index.js calls only when the opt-in gate
24
+ // is open (rerankEnabled && rerankProvider === "local").
25
+ const src = readFileSync(new URL("../src/reranker.js", import.meta.url), "utf8");
26
+ assert.ok(
27
+ !src.includes('from "@huggingface/transformers"'),
28
+ "no static transformers.js import in reranker.js"
29
+ );
30
+ assert.match(src, /await import\("@huggingface\/transformers"\)/, "transformers.js loads lazily via dynamic import");
31
+ });