@modusensus/dsh-mneme 0.5.2 → 0.5.3
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 +419 -419
- package/lib/client.js +1302 -1302
- package/lib/config.js +10 -6
- package/lib/dream.js +94 -2
- package/lib/hot-memory.js +53 -53
- package/lib/reranker.js +218 -218
- package/lib/service.js +1489 -1489
- package/package.json +1 -1
- package/src/config.js +10 -6
- package/src/dream.js +94 -2
- package/src/hot-memory.js +53 -53
- package/src/reranker.js +218 -218
- package/src/service.js +1489 -1489
- package/test/hot-memory.test.js +174 -174
- package/test/normalize-decisions.test.js +120 -0
- package/test/reasoning-effort.test.js +27 -0
- package/test/reranker.test.js +240 -240
- package/test/service-search.test.js +199 -199
package/test/hot-memory.test.js
CHANGED
|
@@ -1,174 +1,174 @@
|
|
|
1
|
-
import test from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { createHotMemory, estimateTokens } from "../src/hot-memory.js";
|
|
4
|
-
import { createStore } from "../src/store.js";
|
|
5
|
-
import { createService } from "../src/service.js";
|
|
6
|
-
import { createVectorIndex } from "../src/vector-index.js";
|
|
7
|
-
|
|
8
|
-
// --- hot memory buffer ---
|
|
9
|
-
|
|
10
|
-
test("hot memory keeps the latest rounds within maxRounds", () => {
|
|
11
|
-
const hot = createHotMemory({ maxRounds: 2, maxTokens: 10000 });
|
|
12
|
-
hot.add({ query: "第一轮", response: "答一" });
|
|
13
|
-
hot.add({ query: "第二轮", response: "答二" });
|
|
14
|
-
hot.add({ query: "第三轮", response: "答三" });
|
|
15
|
-
assert.equal(hot.rounds().length, 2);
|
|
16
|
-
assert.ok(hot.getContext().includes("第三轮"));
|
|
17
|
-
assert.ok(!hot.getContext().includes("第一轮"));
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test("hot memory enforces the token budget", () => {
|
|
21
|
-
const hot = createHotMemory({ maxRounds: 10, maxTokens: 30 });
|
|
22
|
-
hot.add({ query: "很长的第一轮问题".repeat(10), response: "很长的回答".repeat(10) });
|
|
23
|
-
hot.add({ query: "第二轮", response: "答二" });
|
|
24
|
-
// The first round alone blows the budget; the newest round survives and
|
|
25
|
-
// the buffer never empties completely.
|
|
26
|
-
const rounds = hot.rounds();
|
|
27
|
-
assert.ok(rounds.length >= 1);
|
|
28
|
-
assert.equal(rounds[rounds.length - 1].query, "第二轮");
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test("hot memory getContext uses the Q/A round format", () => {
|
|
32
|
-
const hot = createHotMemory({ maxRounds: 5, maxTokens: 10000 });
|
|
33
|
-
hot.add({ query: "Q1", response: "A1" });
|
|
34
|
-
hot.add({ query: "Q2", response: "A2" });
|
|
35
|
-
assert.equal(hot.getContext(), "Q: Q1\nA: A1\n\nQ: Q2\nA: A2");
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
test("hot memory ignores empty rounds and clears", () => {
|
|
39
|
-
const hot = createHotMemory({ maxRounds: 5, maxTokens: 10000 });
|
|
40
|
-
hot.add({ query: "", response: "x" });
|
|
41
|
-
hot.add(null);
|
|
42
|
-
assert.equal(hot.rounds().length, 0);
|
|
43
|
-
hot.add({ query: "q" });
|
|
44
|
-
hot.clear();
|
|
45
|
-
assert.equal(hot.getContext(), "");
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
test("estimateTokens counts CJK heavier than ASCII", () => {
|
|
49
|
-
assert.ok(estimateTokens("中文内容") > estimateTokens("abcd"));
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
// --- entry defense: non-positive / non-integer bounds fall back to defaults ---
|
|
53
|
-
// Bug: createHotMemory({ maxRounds: -1 }) made the eviction while-loop
|
|
54
|
-
// `while (buffer.length > maxRounds)` unbounded — after the buffer emptied,
|
|
55
|
-
// `0 > -1` stayed true and buffer.shift() on an empty array is a no-op, so
|
|
56
|
-
// every add() spun forever. Non-integer values (1.5, NaN, null) were also
|
|
57
|
-
// silently wrong. The fix clamps them to the 5/2000 defaults at the door.
|
|
58
|
-
|
|
59
|
-
test("hot memory falls back to maxRounds=5 for non-positive/invalid values", () => {
|
|
60
|
-
for (const bad of [0, -1, 1.5, NaN, null]) {
|
|
61
|
-
const hot = createHotMemory({ maxRounds: bad, maxTokens: 10000 });
|
|
62
|
-
for (let i = 0; i < 8; i++) hot.add({ query: `第${i}轮`, response: "x" });
|
|
63
|
-
assert.equal(hot.rounds().length, 5, `maxRounds=${bad} must fall back to 5, no infinite loop`);
|
|
64
|
-
assert.ok(hot.getContext().includes("第7轮"), `maxRounds=${bad}: newest round survives`);
|
|
65
|
-
assert.ok(!hot.getContext().includes("第0轮"), `maxRounds=${bad}: oldest round evicted`);
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test("hot memory falls back to maxTokens=2000 for non-positive/infinite values", () => {
|
|
70
|
-
for (const bad of [0, -1, Infinity]) {
|
|
71
|
-
const hot = createHotMemory({ maxRounds: 50, maxTokens: bad });
|
|
72
|
-
// 50 rounds at ~74 tokens each blow a 2000-token budget; the fallback must
|
|
73
|
-
// evict into (1, 50). A broken budget of 0/-1 would squeeze to 1 round and
|
|
74
|
-
// Infinity would keep all 50 — both are the pre-fix behavior.
|
|
75
|
-
for (let i = 0; i < 50; i++) hot.add({ query: `第${i}轮`, response: "长回答".repeat(40) });
|
|
76
|
-
const n = hot.rounds().length;
|
|
77
|
-
assert.ok(n > 1 && n < 50, `maxTokens=${bad} falls back to 2000 (kept ${n} rounds)`);
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
// --- service-level: BM25 fusion + semantic dedup + selective injection ---
|
|
82
|
-
|
|
83
|
-
function toyVec(text) {
|
|
84
|
-
const v = new Array(64).fill(0);
|
|
85
|
-
for (const t of String(text).toLowerCase().split(/[^\p{L}\p{N}]+/u).filter(Boolean)) {
|
|
86
|
-
let h = 0;
|
|
87
|
-
for (const ch of t) h = (h * 31 + ch.codePointAt(0)) >>> 0;
|
|
88
|
-
v[h % 64] += 1;
|
|
89
|
-
}
|
|
90
|
-
const norm = Math.sqrt(v.reduce((s, x) => s + x * x, 0)) || 1;
|
|
91
|
-
return v.map((x) => x / norm);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function setup(overrides = {}) {
|
|
95
|
-
const store = createStore(":memory:");
|
|
96
|
-
const config = {
|
|
97
|
-
entitySearchEnabled: false,
|
|
98
|
-
bm25SearchEnabled: true,
|
|
99
|
-
adaptiveThresholdEnabled: false,
|
|
100
|
-
searchSemanticDedup: true,
|
|
101
|
-
selectiveInjectEnabled: true,
|
|
102
|
-
...overrides
|
|
103
|
-
};
|
|
104
|
-
const service = createService({ store, mirror: null, config, logger: null });
|
|
105
|
-
service.setVectorIndex(createVectorIndex({ store, logger: null }));
|
|
106
|
-
service.setEmbedder({ embedSingle: async (t) => toyVec(t) });
|
|
107
|
-
return { store, service };
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
test("searchMemories fuses BM25: scattered-term queries recall both rows", async () => {
|
|
111
|
-
const { store, service } = setup();
|
|
112
|
-
const a = store.save({ type: "project", title: "异步并发模式", content: "async runtime 选用 tokio", importance: 3 });
|
|
113
|
-
const b = store.save({ type: "decision", title: "语言迁移", content: "编译模块迁移到 Rust", importance: 3 });
|
|
114
|
-
store.save({ type: "project", title: "无关", content: "夜间 ETL 脚本", importance: 3 });
|
|
115
|
-
|
|
116
|
-
// "rust 异步" is not a substring of either row — LIKE misses both; BM25
|
|
117
|
-
// must surface both rows in the merged result.
|
|
118
|
-
const results = await service.searchMemories("rust 异步", { mode: "auto", topK: 5 });
|
|
119
|
-
const ids = results.map((r) => r.id);
|
|
120
|
-
assert.ok(ids.includes(a.id), "async row must be recalled via BM25");
|
|
121
|
-
assert.ok(ids.includes(b.id), "rust row must be recalled via BM25");
|
|
122
|
-
assert.equal(results.find((r) => r.id === a.id)?.source, "bm25");
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
test("bm25SearchEnabled=false restores the two-path behavior", async () => {
|
|
126
|
-
const { store, service } = setup({ bm25SearchEnabled: false });
|
|
127
|
-
const a = store.save({ type: "project", title: "异步并发模式", content: "async runtime 选用 tokio", importance: 3 });
|
|
128
|
-
const results = await service.searchMemories("rust 异步", { mode: "auto", topK: 5 });
|
|
129
|
-
assert.ok(!results.some((r) => r.id === a.id), "no BM25 → scattered-term miss is back");
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
test("search-time semantic dedup drops near-identical embeddings", async () => {
|
|
133
|
-
const { store, service } = setup();
|
|
134
|
-
const a = store.save({ type: "project", title: "偏好 A", content: "用户喜欢深色主题编辑器", importance: 3 });
|
|
135
|
-
const b = store.save({ type: "project", title: "偏好 A 备份", content: "用户喜欢深色主题编辑器(备份)", importance: 3 });
|
|
136
|
-
store.setEmbedding(a.id, toyVec("用户喜欢深色主题编辑器"));
|
|
137
|
-
store.setEmbedding(b.id, toyVec("用户喜欢深色主题编辑器"));
|
|
138
|
-
|
|
139
|
-
// auto (not keyword): keyword mode is the documented text-only path and is
|
|
140
|
-
// exempt from dedup by design; auto exercises the dedup the way production
|
|
141
|
-
// searches run.
|
|
142
|
-
const results = await service.searchMemories("深色主题", { mode: "auto", topK: 5 });
|
|
143
|
-
const ids = results.map((r) => r.id);
|
|
144
|
-
assert.ok(ids.includes(a.id) !== ids.includes(b.id), "one of the near-duplicate pair is dropped");
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
test("searchSemanticDedup=false keeps duplicate embeddings", async () => {
|
|
148
|
-
const { store, service } = setup({ searchSemanticDedup: false });
|
|
149
|
-
const a = store.save({ type: "project", title: "偏好 A", content: "用户喜欢深色主题编辑器", importance: 3 });
|
|
150
|
-
const b = store.save({ type: "project", title: "偏好 A 备份", content: "用户喜欢深色主题编辑器(备份)", importance: 3 });
|
|
151
|
-
store.setEmbedding(a.id, toyVec("用户喜欢深色主题编辑器"));
|
|
152
|
-
store.setEmbedding(b.id, toyVec("用户喜欢深色主题编辑器"));
|
|
153
|
-
const results = await service.searchMemories("深色主题", { mode: "keyword", topK: 5 });
|
|
154
|
-
assert.equal(results.length, 2);
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
test("selective injection re-orders candidates by query similarity", () => {
|
|
158
|
-
const { store, service } = setup();
|
|
159
|
-
const thesis = store.save({ type: "project", title: "湿地论文", content: "毕业论文研究城市湿地公园", importance: 5 });
|
|
160
|
-
const plugin = store.save({ type: "project", title: "插件项目", content: "dsh-mneme 记忆插件开发", importance: 5 });
|
|
161
|
-
store.setEmbedding(thesis.id, toyVec("毕业论文研究城市湿地公园"));
|
|
162
|
-
store.setEmbedding(plugin.id, toyVec("dsh-mneme 记忆插件开发"));
|
|
163
|
-
|
|
164
|
-
// Rule-based order would put both at equal importance; the query vector is
|
|
165
|
-
// about the thesis, so topic ranking must put the thesis memory first.
|
|
166
|
-
const picked = service.injectCandidates({
|
|
167
|
-
query: "论文写作",
|
|
168
|
-
queryVector: toyVec("毕业论文研究城市湿地公园"),
|
|
169
|
-
maxItems: 2,
|
|
170
|
-
threshold: 3
|
|
171
|
-
});
|
|
172
|
-
assert.equal(picked[0].id, thesis.id);
|
|
173
|
-
assert.ok(picked.some((m) => m.id === plugin.id));
|
|
174
|
-
});
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { createHotMemory, estimateTokens } from "../src/hot-memory.js";
|
|
4
|
+
import { createStore } from "../src/store.js";
|
|
5
|
+
import { createService } from "../src/service.js";
|
|
6
|
+
import { createVectorIndex } from "../src/vector-index.js";
|
|
7
|
+
|
|
8
|
+
// --- hot memory buffer ---
|
|
9
|
+
|
|
10
|
+
test("hot memory keeps the latest rounds within maxRounds", () => {
|
|
11
|
+
const hot = createHotMemory({ maxRounds: 2, maxTokens: 10000 });
|
|
12
|
+
hot.add({ query: "第一轮", response: "答一" });
|
|
13
|
+
hot.add({ query: "第二轮", response: "答二" });
|
|
14
|
+
hot.add({ query: "第三轮", response: "答三" });
|
|
15
|
+
assert.equal(hot.rounds().length, 2);
|
|
16
|
+
assert.ok(hot.getContext().includes("第三轮"));
|
|
17
|
+
assert.ok(!hot.getContext().includes("第一轮"));
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test("hot memory enforces the token budget", () => {
|
|
21
|
+
const hot = createHotMemory({ maxRounds: 10, maxTokens: 30 });
|
|
22
|
+
hot.add({ query: "很长的第一轮问题".repeat(10), response: "很长的回答".repeat(10) });
|
|
23
|
+
hot.add({ query: "第二轮", response: "答二" });
|
|
24
|
+
// The first round alone blows the budget; the newest round survives and
|
|
25
|
+
// the buffer never empties completely.
|
|
26
|
+
const rounds = hot.rounds();
|
|
27
|
+
assert.ok(rounds.length >= 1);
|
|
28
|
+
assert.equal(rounds[rounds.length - 1].query, "第二轮");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("hot memory getContext uses the Q/A round format", () => {
|
|
32
|
+
const hot = createHotMemory({ maxRounds: 5, maxTokens: 10000 });
|
|
33
|
+
hot.add({ query: "Q1", response: "A1" });
|
|
34
|
+
hot.add({ query: "Q2", response: "A2" });
|
|
35
|
+
assert.equal(hot.getContext(), "Q: Q1\nA: A1\n\nQ: Q2\nA: A2");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("hot memory ignores empty rounds and clears", () => {
|
|
39
|
+
const hot = createHotMemory({ maxRounds: 5, maxTokens: 10000 });
|
|
40
|
+
hot.add({ query: "", response: "x" });
|
|
41
|
+
hot.add(null);
|
|
42
|
+
assert.equal(hot.rounds().length, 0);
|
|
43
|
+
hot.add({ query: "q" });
|
|
44
|
+
hot.clear();
|
|
45
|
+
assert.equal(hot.getContext(), "");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("estimateTokens counts CJK heavier than ASCII", () => {
|
|
49
|
+
assert.ok(estimateTokens("中文内容") > estimateTokens("abcd"));
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// --- entry defense: non-positive / non-integer bounds fall back to defaults ---
|
|
53
|
+
// Bug: createHotMemory({ maxRounds: -1 }) made the eviction while-loop
|
|
54
|
+
// `while (buffer.length > maxRounds)` unbounded — after the buffer emptied,
|
|
55
|
+
// `0 > -1` stayed true and buffer.shift() on an empty array is a no-op, so
|
|
56
|
+
// every add() spun forever. Non-integer values (1.5, NaN, null) were also
|
|
57
|
+
// silently wrong. The fix clamps them to the 5/2000 defaults at the door.
|
|
58
|
+
|
|
59
|
+
test("hot memory falls back to maxRounds=5 for non-positive/invalid values", () => {
|
|
60
|
+
for (const bad of [0, -1, 1.5, NaN, null]) {
|
|
61
|
+
const hot = createHotMemory({ maxRounds: bad, maxTokens: 10000 });
|
|
62
|
+
for (let i = 0; i < 8; i++) hot.add({ query: `第${i}轮`, response: "x" });
|
|
63
|
+
assert.equal(hot.rounds().length, 5, `maxRounds=${bad} must fall back to 5, no infinite loop`);
|
|
64
|
+
assert.ok(hot.getContext().includes("第7轮"), `maxRounds=${bad}: newest round survives`);
|
|
65
|
+
assert.ok(!hot.getContext().includes("第0轮"), `maxRounds=${bad}: oldest round evicted`);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("hot memory falls back to maxTokens=2000 for non-positive/infinite values", () => {
|
|
70
|
+
for (const bad of [0, -1, Infinity]) {
|
|
71
|
+
const hot = createHotMemory({ maxRounds: 50, maxTokens: bad });
|
|
72
|
+
// 50 rounds at ~74 tokens each blow a 2000-token budget; the fallback must
|
|
73
|
+
// evict into (1, 50). A broken budget of 0/-1 would squeeze to 1 round and
|
|
74
|
+
// Infinity would keep all 50 — both are the pre-fix behavior.
|
|
75
|
+
for (let i = 0; i < 50; i++) hot.add({ query: `第${i}轮`, response: "长回答".repeat(40) });
|
|
76
|
+
const n = hot.rounds().length;
|
|
77
|
+
assert.ok(n > 1 && n < 50, `maxTokens=${bad} falls back to 2000 (kept ${n} rounds)`);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// --- service-level: BM25 fusion + semantic dedup + selective injection ---
|
|
82
|
+
|
|
83
|
+
function toyVec(text) {
|
|
84
|
+
const v = new Array(64).fill(0);
|
|
85
|
+
for (const t of String(text).toLowerCase().split(/[^\p{L}\p{N}]+/u).filter(Boolean)) {
|
|
86
|
+
let h = 0;
|
|
87
|
+
for (const ch of t) h = (h * 31 + ch.codePointAt(0)) >>> 0;
|
|
88
|
+
v[h % 64] += 1;
|
|
89
|
+
}
|
|
90
|
+
const norm = Math.sqrt(v.reduce((s, x) => s + x * x, 0)) || 1;
|
|
91
|
+
return v.map((x) => x / norm);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function setup(overrides = {}) {
|
|
95
|
+
const store = createStore(":memory:");
|
|
96
|
+
const config = {
|
|
97
|
+
entitySearchEnabled: false,
|
|
98
|
+
bm25SearchEnabled: true,
|
|
99
|
+
adaptiveThresholdEnabled: false,
|
|
100
|
+
searchSemanticDedup: true,
|
|
101
|
+
selectiveInjectEnabled: true,
|
|
102
|
+
...overrides
|
|
103
|
+
};
|
|
104
|
+
const service = createService({ store, mirror: null, config, logger: null });
|
|
105
|
+
service.setVectorIndex(createVectorIndex({ store, logger: null }));
|
|
106
|
+
service.setEmbedder({ embedSingle: async (t) => toyVec(t) });
|
|
107
|
+
return { store, service };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
test("searchMemories fuses BM25: scattered-term queries recall both rows", async () => {
|
|
111
|
+
const { store, service } = setup();
|
|
112
|
+
const a = store.save({ type: "project", title: "异步并发模式", content: "async runtime 选用 tokio", importance: 3 });
|
|
113
|
+
const b = store.save({ type: "decision", title: "语言迁移", content: "编译模块迁移到 Rust", importance: 3 });
|
|
114
|
+
store.save({ type: "project", title: "无关", content: "夜间 ETL 脚本", importance: 3 });
|
|
115
|
+
|
|
116
|
+
// "rust 异步" is not a substring of either row — LIKE misses both; BM25
|
|
117
|
+
// must surface both rows in the merged result.
|
|
118
|
+
const results = await service.searchMemories("rust 异步", { mode: "auto", topK: 5 });
|
|
119
|
+
const ids = results.map((r) => r.id);
|
|
120
|
+
assert.ok(ids.includes(a.id), "async row must be recalled via BM25");
|
|
121
|
+
assert.ok(ids.includes(b.id), "rust row must be recalled via BM25");
|
|
122
|
+
assert.equal(results.find((r) => r.id === a.id)?.source, "bm25");
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test("bm25SearchEnabled=false restores the two-path behavior", async () => {
|
|
126
|
+
const { store, service } = setup({ bm25SearchEnabled: false });
|
|
127
|
+
const a = store.save({ type: "project", title: "异步并发模式", content: "async runtime 选用 tokio", importance: 3 });
|
|
128
|
+
const results = await service.searchMemories("rust 异步", { mode: "auto", topK: 5 });
|
|
129
|
+
assert.ok(!results.some((r) => r.id === a.id), "no BM25 → scattered-term miss is back");
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test("search-time semantic dedup drops near-identical embeddings", async () => {
|
|
133
|
+
const { store, service } = setup();
|
|
134
|
+
const a = store.save({ type: "project", title: "偏好 A", content: "用户喜欢深色主题编辑器", importance: 3 });
|
|
135
|
+
const b = store.save({ type: "project", title: "偏好 A 备份", content: "用户喜欢深色主题编辑器(备份)", importance: 3 });
|
|
136
|
+
store.setEmbedding(a.id, toyVec("用户喜欢深色主题编辑器"));
|
|
137
|
+
store.setEmbedding(b.id, toyVec("用户喜欢深色主题编辑器"));
|
|
138
|
+
|
|
139
|
+
// auto (not keyword): keyword mode is the documented text-only path and is
|
|
140
|
+
// exempt from dedup by design; auto exercises the dedup the way production
|
|
141
|
+
// searches run.
|
|
142
|
+
const results = await service.searchMemories("深色主题", { mode: "auto", topK: 5 });
|
|
143
|
+
const ids = results.map((r) => r.id);
|
|
144
|
+
assert.ok(ids.includes(a.id) !== ids.includes(b.id), "one of the near-duplicate pair is dropped");
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test("searchSemanticDedup=false keeps duplicate embeddings", async () => {
|
|
148
|
+
const { store, service } = setup({ searchSemanticDedup: false });
|
|
149
|
+
const a = store.save({ type: "project", title: "偏好 A", content: "用户喜欢深色主题编辑器", importance: 3 });
|
|
150
|
+
const b = store.save({ type: "project", title: "偏好 A 备份", content: "用户喜欢深色主题编辑器(备份)", importance: 3 });
|
|
151
|
+
store.setEmbedding(a.id, toyVec("用户喜欢深色主题编辑器"));
|
|
152
|
+
store.setEmbedding(b.id, toyVec("用户喜欢深色主题编辑器"));
|
|
153
|
+
const results = await service.searchMemories("深色主题", { mode: "keyword", topK: 5 });
|
|
154
|
+
assert.equal(results.length, 2);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test("selective injection re-orders candidates by query similarity", () => {
|
|
158
|
+
const { store, service } = setup();
|
|
159
|
+
const thesis = store.save({ type: "project", title: "湿地论文", content: "毕业论文研究城市湿地公园", importance: 5 });
|
|
160
|
+
const plugin = store.save({ type: "project", title: "插件项目", content: "dsh-mneme 记忆插件开发", importance: 5 });
|
|
161
|
+
store.setEmbedding(thesis.id, toyVec("毕业论文研究城市湿地公园"));
|
|
162
|
+
store.setEmbedding(plugin.id, toyVec("dsh-mneme 记忆插件开发"));
|
|
163
|
+
|
|
164
|
+
// Rule-based order would put both at equal importance; the query vector is
|
|
165
|
+
// about the thesis, so topic ranking must put the thesis memory first.
|
|
166
|
+
const picked = service.injectCandidates({
|
|
167
|
+
query: "论文写作",
|
|
168
|
+
queryVector: toyVec("毕业论文研究城市湿地公园"),
|
|
169
|
+
maxItems: 2,
|
|
170
|
+
threshold: 3
|
|
171
|
+
});
|
|
172
|
+
assert.equal(picked[0].id, thesis.id);
|
|
173
|
+
assert.ok(picked.some((m) => m.id === plugin.id));
|
|
174
|
+
});
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// Field-name normalization guard (v0.5.3): thinking-type models
|
|
2
|
+
// (deepseek-v4-flash etc.) sometimes ignore the prompt's exact decision schema
|
|
3
|
+
// and emit alias keys —实测方案 A 输出 "consolidation"/"target_ids"、方案 B
|
|
4
|
+
// 输出 "action"/"targetIds",都不是插件要求的 "action"/"ids"。normalizeDecisions
|
|
5
|
+
// 在 validateDecisions 之前把这些变体重写回规范字段名,让"语义正确但 schema
|
|
6
|
+
// 不听话"的输出仍被应用,而不是整单被拒。
|
|
7
|
+
import test from "node:test";
|
|
8
|
+
import assert from "node:assert/strict";
|
|
9
|
+
import { normalizeDecisions, createDreamScheduler } from "../src/dream.js";
|
|
10
|
+
import { createStore } from "../src/store.js";
|
|
11
|
+
import { createService } from "../src/service.js";
|
|
12
|
+
|
|
13
|
+
test("normalizeDecisions: canonical decisions pass through untouched", () => {
|
|
14
|
+
const raw = [
|
|
15
|
+
{ action: "merge", ids: ["m1", "m2"], keepSource: "m1", title: "t", content: "c", importance: 4, reason: "主题相近" },
|
|
16
|
+
{ action: "conflict", winner: "m3", loser: "m4", reason: "矛盾" },
|
|
17
|
+
{ action: "update", ids: ["m5"], content: "修正" },
|
|
18
|
+
{ action: "archive", ids: ["m6"], reason: "过时" },
|
|
19
|
+
{ action: "create", type: "pattern", title: "新模式", content: "发现", importance: 3 }
|
|
20
|
+
];
|
|
21
|
+
assert.deepEqual(normalizeDecisions(raw), raw);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("normalizeDecisions: alias keys rewritten to canonical names", () => {
|
|
25
|
+
const raw = [
|
|
26
|
+
{ consolidation: "merge", target_ids: ["m1", "m2"], keep_source: "m1", new_title: "t", new_content: "c", priority: 4, rationale: "主题相近" },
|
|
27
|
+
{ action: "conflict", winner_id: "m3", loser_id: "m4", why: "矛盾" },
|
|
28
|
+
{ action: "update", targetIds: ["m5"], merged_content: "修正" },
|
|
29
|
+
{ action: "archive", memory_ids: ["m6"] }
|
|
30
|
+
];
|
|
31
|
+
assert.deepEqual(normalizeDecisions(raw), [
|
|
32
|
+
{ action: "merge", ids: ["m1", "m2"], keepSource: "m1", title: "t", content: "c", importance: 4, reason: "主题相近" },
|
|
33
|
+
{ action: "conflict", winner: "m3", loser: "m4", reason: "矛盾" },
|
|
34
|
+
{ action: "update", ids: ["m5"], content: "修正" },
|
|
35
|
+
{ action: "archive", ids: ["m6"] }
|
|
36
|
+
]);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test("normalizeDecisions: action value synonyms normalized", () => {
|
|
40
|
+
const raw = [
|
|
41
|
+
{ action: "archived", ids: ["m1"], reason: "重复" },
|
|
42
|
+
{ action: "consolidation", target_ids: ["m2", "m3"], keep_source: "m2" },
|
|
43
|
+
{ action: "combine", targetIds: ["m4", "m5"], keeper: "m5" },
|
|
44
|
+
{ action: "Remove", memory_ids: ["m6"] }
|
|
45
|
+
];
|
|
46
|
+
const normalized = normalizeDecisions(raw);
|
|
47
|
+
assert.equal(normalized[0].action, "archive");
|
|
48
|
+
assert.equal(normalized[1].action, "merge");
|
|
49
|
+
assert.equal(normalized[1].ids[0], "m2");
|
|
50
|
+
assert.equal(normalized[2].action, "merge");
|
|
51
|
+
assert.equal(normalized[2].keepSource, "m5");
|
|
52
|
+
assert.equal(normalized[3].action, "archive");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("normalizeDecisions: wrapper object around the array is unwrapped", () => {
|
|
56
|
+
const raw = { consolidation: [{ action: "merge", ids: ["m1"], keepSource: "m1", title: "t", content: "c" }] };
|
|
57
|
+
assert.deepEqual(normalizeDecisions(raw), [
|
|
58
|
+
{ action: "merge", ids: ["m1"], keepSource: "m1", title: "t", content: "c" }
|
|
59
|
+
]);
|
|
60
|
+
const raw2 = { decisions: [{ action: "archive", target_ids: ["m9"] }] };
|
|
61
|
+
assert.deepEqual(normalizeDecisions(raw2), [{ action: "archive", ids: ["m9"] }]);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("normalizeDecisions: single decision object (non-array) normalized too", () => {
|
|
65
|
+
const raw = { action: "update", targetIds: ["m1"], new_content: "修正" };
|
|
66
|
+
assert.deepEqual(normalizeDecisions(raw), [{ action: "update", ids: ["m1"], content: "修正" }]);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("normalizeDecisions: single-string ids become a one-element array", () => {
|
|
70
|
+
const raw = [{ action: "archive", target_id: "m7" }];
|
|
71
|
+
// target_id (singular) is not an alias key — the raw key survives untouched
|
|
72
|
+
// and validateDecisions rejects it (safe side). But targetIds (plural, string)
|
|
73
|
+
// IS mapped and wrapped.
|
|
74
|
+
const raw2 = [{ action: "archive", targetIds: "m7" }];
|
|
75
|
+
assert.deepEqual(normalizeDecisions(raw2), [{ action: "archive", ids: ["m7"] }]);
|
|
76
|
+
assert.deepEqual(normalizeDecisions(raw), [{ action: "archive", target_id: "m7" }]);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("normalizeDecisions: create keeps its type field (never mistaken for action)", () => {
|
|
80
|
+
const raw = { action: "create", type: "preference", title: "语言", content: "中文", importance: 3 };
|
|
81
|
+
assert.deepEqual(normalizeDecisions(raw), [
|
|
82
|
+
{ action: "create", type: "preference", title: "语言", content: "中文", importance: 3 }
|
|
83
|
+
]);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("normalizeDecisions: null / non-parseable returns as-is (caller's no-json branch)", () => {
|
|
87
|
+
assert.equal(normalizeDecisions(null), null);
|
|
88
|
+
assert.equal(normalizeDecisions(undefined), undefined);
|
|
89
|
+
assert.equal(normalizeDecisions("not json"), "not json");
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("dream runDream applies alias-key consolidation output end-to-end", async () => {
|
|
93
|
+
const store = createStore(":memory:");
|
|
94
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
95
|
+
const dream = createDreamScheduler({ onRun: () => Promise.resolve({ ok: true, skipped: true }) });
|
|
96
|
+
const { memory: a } = service.saveWithDedupe({ type: "project", title: "插件", content: "旧", importance: 3 });
|
|
97
|
+
const { memory: b } = service.saveWithDedupe({ type: "project", title: "插件2", content: "新细节", importance: 4 });
|
|
98
|
+
const ctx = {
|
|
99
|
+
logger: { warn: () => {} },
|
|
100
|
+
agentDefaultModel: { currentSelection: () => ({ provider: "mock", model: "mock-model" }) },
|
|
101
|
+
llm: {
|
|
102
|
+
async *stream(options) {
|
|
103
|
+
const userText = options.messages.find((m) => m.role === "user")?.content?.[0]?.text ?? "";
|
|
104
|
+
if (userText.startsWith("id=")) {
|
|
105
|
+
// deepseek-v4-flash 实测风格:consolidation 当 action、target_ids 当 ids
|
|
106
|
+
yield { type: "text-delta", index: 0, text: JSON.stringify([
|
|
107
|
+
{ consolidation: "merge", target_ids: [a.id, b.id], keep_source: b.id, new_title: "插件总览", new_content: "合并内容", priority: 4 }
|
|
108
|
+
]) };
|
|
109
|
+
} else {
|
|
110
|
+
yield { type: "text-delta", index: 0, text: "记忆库总览:默认摘要。" };
|
|
111
|
+
}
|
|
112
|
+
yield { type: "finish", reason: { kind: "stop" } };
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
const result = await dream.runDream(ctx, service, {});
|
|
117
|
+
assert.equal(result.ok, true, "alias-key output must not be rejected wholesale");
|
|
118
|
+
assert.ok(result.applied > 0, "alias-key decisions still land changes");
|
|
119
|
+
store.close();
|
|
120
|
+
});
|
|
@@ -36,6 +36,11 @@ test("issue#9: reasoningEffort config defaults to none and rejects unknown value
|
|
|
36
36
|
assert.equal(cfg.sleepReasoningEffort, "none");
|
|
37
37
|
assert.equal(Config({ dreamReasoningEffort: "high" }).dreamReasoningEffort, "high");
|
|
38
38
|
assert.equal(Config({ sleepReasoningEffort: "medium" }).sleepReasoningEffort, "medium");
|
|
39
|
+
// v0.5.3: 'off' explicitly disables thinking — required for thinking-type
|
|
40
|
+
// models (deepseek-v4-flash) whose reasoning otherwise drains the whole
|
|
41
|
+
// token budget and returns an empty consolidation body.
|
|
42
|
+
assert.equal(Config({ dreamReasoningEffort: "off" }).dreamReasoningEffort, "off");
|
|
43
|
+
assert.equal(Config({ sleepReasoningEffort: "off" }).sleepReasoningEffort, "off");
|
|
39
44
|
assert.throws(() => Config({ dreamReasoningEffort: "bogus" }), "invalid effort rejected");
|
|
40
45
|
assert.throws(() => Config({ sleepReasoningEffort: "ultra" }), "invalid effort rejected");
|
|
41
46
|
});
|
|
@@ -107,6 +112,28 @@ test("issue#9: dream forwards dreamReasoningEffort on both LLM calls", async ()
|
|
|
107
112
|
store.close();
|
|
108
113
|
});
|
|
109
114
|
|
|
115
|
+
test("issue#9: dream forwards dreamReasoningEffort:off (thinking disabled) on both LLM calls", async () => {
|
|
116
|
+
const store = createStore(":memory:");
|
|
117
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
118
|
+
const dream = createDreamScheduler({ onRun: () => Promise.resolve({ ok: true, skipped: true }) });
|
|
119
|
+
const { memory: a } = service.saveWithDedupe({ type: "project", title: "插件", content: "旧", importance: 3 });
|
|
120
|
+
const { memory: b } = service.saveWithDedupe({ type: "project", title: "插件2", content: "新细节", importance: 4 });
|
|
121
|
+
const captured = [];
|
|
122
|
+
const ctx = dreamCtx({
|
|
123
|
+
captured,
|
|
124
|
+
onConsolidation: () => JSON.stringify([
|
|
125
|
+
{ action: "merge", ids: [a.id, b.id], keepSource: b.id, title: "插件总览", content: "合并内容", importance: 4 }
|
|
126
|
+
])
|
|
127
|
+
});
|
|
128
|
+
const result = await dream.runDream(ctx, service, { dreamReasoningEffort: "off" });
|
|
129
|
+
assert.equal(result.ok, true);
|
|
130
|
+
assert.equal(captured.length, 2);
|
|
131
|
+
for (const options of captured) {
|
|
132
|
+
assert.equal(options.reasoningEffort, "off", `reasoningEffort:"off" forwarded on ${options.purpose}`);
|
|
133
|
+
}
|
|
134
|
+
store.close();
|
|
135
|
+
});
|
|
136
|
+
|
|
110
137
|
// ---------------------------------------------------------------- sleep passthrough
|
|
111
138
|
|
|
112
139
|
function sleepSetup() {
|