@modusensus/dsh-mneme 0.7.31 → 0.7.32
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 +24 -8
- package/lib/api.js +79 -6
- package/lib/client.js +217 -30
- package/lib/commands.js +6 -2
- package/lib/config.js +80 -14
- package/lib/dream/decisions.js +147 -14
- package/lib/dream/sleep.js +84 -34
- package/lib/dream.js +250 -60
- package/lib/embedding.js +22 -2
- package/lib/index.js +68 -37
- package/lib/inject.js +16 -9
- package/lib/lang.js +156 -0
- package/lib/local-embedder.js +23 -9
- package/lib/mirror.js +17 -14
- package/lib/reranker.js +23 -8
- package/lib/runtime/adopt.js +176 -0
- package/lib/runtime/closure.js +172 -0
- package/lib/runtime/download.js +247 -0
- package/lib/runtime/layout.js +235 -0
- package/lib/runtime/loader.js +249 -0
- package/lib/runtime/provision.js +237 -0
- package/lib/runtime/tarball.js +130 -0
- package/lib/runtime/verify.js +191 -0
- package/lib/service.js +102 -8
- package/lib/settings.js +36 -2
- package/lib/store.js +26 -8
- package/lib/summarize.js +108 -35
- package/lib/tools.js +119 -0
- package/package.json +7 -2
- package/runtime-manifest.json +596 -0
- package/scripts/build-runtime-manifest.mjs +217 -0
- package/scripts/mneme-runtime.mjs +277 -0
- package/src/api.js +79 -6
- package/src/commands.js +6 -2
- package/src/config.js +80 -14
- package/src/dream/decisions.js +147 -14
- package/src/dream/sleep.js +84 -34
- package/src/dream.js +250 -60
- package/src/embedding.js +22 -2
- package/src/index.js +68 -37
- package/src/inject.js +16 -9
- package/src/lang.js +156 -0
- package/src/local-embedder.js +23 -9
- package/src/mirror.js +17 -14
- package/src/reranker.js +23 -8
- package/src/runtime/adopt.js +176 -0
- package/src/runtime/closure.js +172 -0
- package/src/runtime/download.js +247 -0
- package/src/runtime/layout.js +235 -0
- package/src/runtime/loader.js +249 -0
- package/src/runtime/provision.js +237 -0
- package/src/runtime/tarball.js +130 -0
- package/src/runtime/verify.js +191 -0
- package/src/service.js +102 -8
- package/src/settings.js +36 -2
- package/src/store.js +26 -8
- package/src/summarize.js +108 -35
- package/src/tools.js +119 -0
- package/test/api.test.js +89 -9
- package/test/client.test.js +94 -10
- package/test/config.test.js +17 -1
- package/test/dream-candidate-hybrid.test.js +281 -0
- package/test/dream.test.js +172 -0
- package/test/embedding-ready.test.js +196 -0
- package/test/helpers/runtime-source.js +60 -0
- package/test/helpers/tar-builder.js +80 -0
- package/test/inject.test.js +37 -1
- package/test/lang.test.js +87 -0
- package/test/quality-filter.test.js +68 -0
- package/test/reasoning-effort.test.js +106 -15
- package/test/reindex-backfill.test.js +62 -0
- package/test/runtime-adopt.test.js +206 -0
- package/test/runtime-cli.test.js +89 -0
- package/test/runtime-closure.test.js +193 -0
- package/test/runtime-download.test.js +302 -0
- package/test/runtime-layout.test.js +163 -0
- package/test/runtime-loader.test.js +237 -0
- package/test/runtime-manifest.test.js +158 -0
- package/test/runtime-provision.test.js +101 -0
- package/test/runtime-tarball.test.js +82 -0
- package/test/runtime-verify.test.js +214 -0
- package/test/sleep-action-set.test.js +422 -0
- package/test/store.test.js +14 -0
- package/test/summarize.test.js +224 -0
- package/test/tools.test.js +30 -5
- package/test/webServer-optional.test.js +34 -1
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { createDreamScheduler } from "../src/dream.js";
|
|
4
|
+
import { createStore } from "../src/store.js";
|
|
5
|
+
import { createService } from "../src/service.js";
|
|
6
|
+
import { createVectorIndex } from "../src/vector-index.js";
|
|
7
|
+
import { parseEntries } from "./helpers/dream-mock.js";
|
|
8
|
+
|
|
9
|
+
// Issue #125:候选集构造从"纯时间窗口"扩成"窗口 ∪ 向量翻出的高相似组"。
|
|
10
|
+
// 实测背景:45 对「双方活跃且 sim≥0.85」里 0 对能同时进窗口(窗口覆盖率 11.7%),
|
|
11
|
+
// 纯时间窗口让"该合并的一对"几乎永远碰不到面。
|
|
12
|
+
//
|
|
13
|
+
// 观察点统一用 mock LLM 收到的候选列表(listText 里的 id=/title= 行)——那就是
|
|
14
|
+
// snapshot 的真实内容,比读内部变量更接近"LLM 实际看到了什么"。
|
|
15
|
+
|
|
16
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
17
|
+
|
|
18
|
+
function setup() {
|
|
19
|
+
const store = createStore(":memory:");
|
|
20
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
21
|
+
const vectorIndex = createVectorIndex({ store });
|
|
22
|
+
const embedder = {
|
|
23
|
+
embedSingle: async () => [1, 0, 0],
|
|
24
|
+
embed: async (texts) => texts.map(() => [1, 0, 0]),
|
|
25
|
+
schedule: () => {},
|
|
26
|
+
modelHash: "mock#1",
|
|
27
|
+
dimension: 3
|
|
28
|
+
};
|
|
29
|
+
service.setEmbedder(embedder);
|
|
30
|
+
service.setVectorIndex(vectorIndex);
|
|
31
|
+
return { store, service, vectorIndex, embedder };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Save a memory with a pinned embedding. */
|
|
35
|
+
function seed(service, vectorIndex, title, vec) {
|
|
36
|
+
const m = service.saveWithDedupe({ type: "project", title, content: `${title} 的内容`, importance: 3 }).memory;
|
|
37
|
+
vectorIndex.saveEmbedding(m.id, vec);
|
|
38
|
+
return m;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Capture the candidate titles the LLM was handed. */
|
|
42
|
+
// 注意:不能用 helpers 的 mockCtx —— 它靠 userText.startsWith("id=") 区分阶段,而
|
|
43
|
+
// semantic 可用时候选列表以「# 聚类 N」开头(语义预分组),会被误判成 summary。
|
|
44
|
+
// 这里改用 system prompt 判定,与 listText 的形态解耦。
|
|
45
|
+
function stubCtx(consolidationReply) {
|
|
46
|
+
const seen = { titles: [] };
|
|
47
|
+
const ctx = {
|
|
48
|
+
logger: { warn: () => {}, info: () => {} },
|
|
49
|
+
agentDefaultModel: { currentSelection: () => ({ provider: "mock", model: "mock-model" }) },
|
|
50
|
+
llm: {
|
|
51
|
+
async *stream(options) {
|
|
52
|
+
const sys = options.messages.find((m) => m.role === "system")?.content?.[0]?.text ?? "";
|
|
53
|
+
const userText = options.messages.find((m) => m.role === "user")?.content?.[0]?.text ?? "";
|
|
54
|
+
const isConsolidation = sys.includes("记忆巩固");
|
|
55
|
+
if (isConsolidation) {
|
|
56
|
+
seen.titles = parseEntries(userText).map((e) => e.title);
|
|
57
|
+
yield { type: "text-delta", index: 0, text: typeof consolidationReply === "function" ? consolidationReply() : consolidationReply };
|
|
58
|
+
} else {
|
|
59
|
+
yield { type: "text-delta", index: 0, text: "记忆库总览:测试桩。" };
|
|
60
|
+
}
|
|
61
|
+
yield { type: "finish", reason: { kind: "stop" } };
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
return { ctx, seen };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function capture() {
|
|
69
|
+
return stubCtx("[]");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function scheduler(config, semantic) {
|
|
73
|
+
return createDreamScheduler({ thresholdCount: 1, thresholdChars: 0, delayMs: 0, semantic });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const baseCfg = { dreamProvider: "mock", dreamModel: "mock-model" };
|
|
77
|
+
|
|
78
|
+
test("issue#125 window (default): candidates are exactly the newest dreamMaxSnapshotSize entries", async () => {
|
|
79
|
+
const { store, service } = setup();
|
|
80
|
+
// 同毫秒保存会让 updated_at 并列、排序退化到 id(UUID 随机)——插小延时保证严格递增。
|
|
81
|
+
for (let i = 0; i < 5; i++) {
|
|
82
|
+
service.saveWithDedupe({ type: "project", title: `记忆${i}`, content: `内容${i}`, importance: 3 });
|
|
83
|
+
await sleep(3);
|
|
84
|
+
}
|
|
85
|
+
const { ctx, seen } = capture();
|
|
86
|
+
await scheduler().runDream(ctx, service, { ...baseCfg, dreamMaxSnapshotSize: 3 });
|
|
87
|
+
assert.deepEqual(seen.titles, ["记忆4", "记忆3", "记忆2"], "newest three, updated_at desc — 与 v0.7.31 逐条一致");
|
|
88
|
+
store.close();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("issue#125 hybrid: a highly similar pair sitting outside the window is pulled in", async () => {
|
|
92
|
+
const { store, service, vectorIndex, embedder } = setup();
|
|
93
|
+
// 窗口外(最老):一对高相似(同向量 → cos 1.0)
|
|
94
|
+
const a = seed(service, vectorIndex, "老旧方案A", [1, 0, 0]);
|
|
95
|
+
await sleep(3);
|
|
96
|
+
const b = seed(service, vectorIndex, "老旧方案B", [1, 0, 0]);
|
|
97
|
+
await sleep(3);
|
|
98
|
+
// 窗口内(最新 3 条):彼此不相似(正交)
|
|
99
|
+
seed(service, vectorIndex, "近期甲", [0, 1, 0]);
|
|
100
|
+
await sleep(3);
|
|
101
|
+
seed(service, vectorIndex, "近期乙", [0, 0, 1]);
|
|
102
|
+
await sleep(3);
|
|
103
|
+
seed(service, vectorIndex, "近期丙", [0.5, 0.5, 0.7]);
|
|
104
|
+
|
|
105
|
+
const semantic = { embedder, vectorIndex };
|
|
106
|
+
const { ctx, seen } = capture();
|
|
107
|
+
await scheduler({}, semantic).runDream(ctx, service, {
|
|
108
|
+
...baseCfg, dreamMaxSnapshotSize: 3, dreamCandidateMode: "hybrid"
|
|
109
|
+
});
|
|
110
|
+
assert.ok(seen.titles.includes(a.title) && seen.titles.includes(b.title),
|
|
111
|
+
"the out-of-window similar pair reached the candidate list");
|
|
112
|
+
store.close();
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("issue#125 hybrid: dreamCandidateMax widens the cap, the window is still included", async () => {
|
|
116
|
+
const { store, service, vectorIndex, embedder } = setup();
|
|
117
|
+
const a = seed(service, vectorIndex, "外窗A", [1, 0, 0]);
|
|
118
|
+
await sleep(3);
|
|
119
|
+
const b = seed(service, vectorIndex, "外窗B", [1, 0, 0]);
|
|
120
|
+
await sleep(3);
|
|
121
|
+
seed(service, vectorIndex, "窗内甲", [0, 1, 0]);
|
|
122
|
+
await sleep(3);
|
|
123
|
+
seed(service, vectorIndex, "窗内乙", [0, 0, 1]);
|
|
124
|
+
|
|
125
|
+
const semantic = { embedder, vectorIndex };
|
|
126
|
+
const { ctx, seen } = capture();
|
|
127
|
+
await scheduler({}, semantic).runDream(ctx, service, {
|
|
128
|
+
...baseCfg, dreamMaxSnapshotSize: 2, dreamCandidateMode: "hybrid", dreamCandidateMax: 4
|
|
129
|
+
});
|
|
130
|
+
assert.equal(seen.titles.length, 4, "candidate total capped at max(window, dreamCandidateMax)");
|
|
131
|
+
assert.ok(seen.titles.includes(a.title) && seen.titles.includes(b.title), "vector group kept");
|
|
132
|
+
assert.equal(new Set(seen.titles).size, seen.titles.length, "no duplicate members");
|
|
133
|
+
store.close();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test("issue#125 hybrid: three mutually similar entries arrive as one contiguous group", async () => {
|
|
137
|
+
const { store, service, vectorIndex, embedder } = setup();
|
|
138
|
+
// 一簇 3 条互相相似(同向量)
|
|
139
|
+
const c1 = seed(service, vectorIndex, "簇甲", [1, 0, 0]);
|
|
140
|
+
await sleep(3);
|
|
141
|
+
const c2 = seed(service, vectorIndex, "簇乙", [1, 0, 0]);
|
|
142
|
+
await sleep(3);
|
|
143
|
+
const c3 = seed(service, vectorIndex, "簇丙", [1, 0, 0]);
|
|
144
|
+
await sleep(3);
|
|
145
|
+
seed(service, vectorIndex, "窗外无关", [0, 1, 0]);
|
|
146
|
+
|
|
147
|
+
const semantic = { embedder, vectorIndex };
|
|
148
|
+
const { ctx, seen } = capture();
|
|
149
|
+
await scheduler({}, semantic).runDream(ctx, service, {
|
|
150
|
+
...baseCfg, dreamMaxSnapshotSize: 1, dreamCandidateMode: "hybrid", dreamCandidateMax: 4
|
|
151
|
+
});
|
|
152
|
+
const idx = [c1.title, c2.title, c3.title].map((t) => seen.titles.indexOf(t));
|
|
153
|
+
assert.ok(idx.every((i) => i >= 0), "all cluster members are candidates");
|
|
154
|
+
const sorted = [...idx].sort((x, y) => x - y);
|
|
155
|
+
assert.deepEqual(sorted, [sorted[0], sorted[0] + 1, sorted[0] + 2],
|
|
156
|
+
"cluster members are contiguous — 一簇按一个候选单元展开,不会被拆成散落的对");
|
|
157
|
+
store.close();
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test("issue#125 hybrid without an embedder falls back to the plain window (never fails)", async () => {
|
|
161
|
+
const { store, service } = setup();
|
|
162
|
+
for (let i = 0; i < 4; i++) {
|
|
163
|
+
service.saveWithDedupe({ type: "project", title: `回滚${i}`, content: `内容${i}`, importance: 3 });
|
|
164
|
+
await sleep(3);
|
|
165
|
+
}
|
|
166
|
+
const { ctx, seen } = capture();
|
|
167
|
+
// semantic = null → hybrid 分支不激活
|
|
168
|
+
const result = await scheduler({}, null).runDream(ctx, service, {
|
|
169
|
+
...baseCfg, dreamMaxSnapshotSize: 2, dreamCandidateMode: "hybrid"
|
|
170
|
+
});
|
|
171
|
+
assert.equal(seen.titles.length, 2, "falls back to the window");
|
|
172
|
+
assert.equal(result.ok, true, "the run still succeeds");
|
|
173
|
+
assert.deepEqual(seen.titles, ["回滚3", "回滚2"]);
|
|
174
|
+
store.close();
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test("issue#125 hybrid: a similarity cluster outside the window is resolvable in one run", async () => {
|
|
178
|
+
const { store, service, vectorIndex, embedder } = setup();
|
|
179
|
+
const a = seed(service, vectorIndex, "旧版本", [1, 0, 0]);
|
|
180
|
+
await sleep(3);
|
|
181
|
+
const b = seed(service, vectorIndex, "新版本", [1, 0, 0]);
|
|
182
|
+
await sleep(3);
|
|
183
|
+
seed(service, vectorIndex, "近期条目", [0, 1, 0]);
|
|
184
|
+
|
|
185
|
+
const semantic = { embedder, vectorIndex };
|
|
186
|
+
// LLM 只对这一对下一道 merge 决策——它在窗口外,window 档下永远不会被看到。
|
|
187
|
+
const { ctx } = stubCtx(() => JSON.stringify([
|
|
188
|
+
{ action: "merge", ids: [b.id, a.id], keepSource: b.id, title: "版本总览", content: "合并后", importance: 4 }
|
|
189
|
+
]));
|
|
190
|
+
const result = await scheduler({}, semantic).runDream(ctx, service, {
|
|
191
|
+
...baseCfg, dreamMaxSnapshotSize: 2, dreamCandidateMode: "hybrid"
|
|
192
|
+
});
|
|
193
|
+
assert.equal(result.ok, true, "the out-of-window pair is adjudicated (not a whole-batch rejection)");
|
|
194
|
+
assert.equal(store.getById(a.id).archived, true, "the older version was merged away");
|
|
195
|
+
assert.equal(store.getById(b.id).title, "版本总览");
|
|
196
|
+
store.close();
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
// ------------------------------------------------------- backfill & 降级路径
|
|
200
|
+
|
|
201
|
+
test("issue#125 hybrid: entries without a cached vector are embedded on the fly", async () => {
|
|
202
|
+
const { store, service, vectorIndex, embedder } = setup();
|
|
203
|
+
// 故意不预设向量:collectCandidateVectors 必须自己补齐后再比较。
|
|
204
|
+
const a = service.saveWithDedupe({ type: "project", title: "无向量A", content: "内容A", importance: 3 }).memory;
|
|
205
|
+
await sleep(3);
|
|
206
|
+
const b = service.saveWithDedupe({ type: "project", title: "无向量B", content: "内容B", importance: 3 }).memory;
|
|
207
|
+
await sleep(3);
|
|
208
|
+
seed(service, vectorIndex, "窗内条目", [0, 1, 0]);
|
|
209
|
+
const { ctx, seen } = capture();
|
|
210
|
+
await scheduler({}, { embedder, vectorIndex }).runDream(ctx, service, {
|
|
211
|
+
...baseCfg, dreamMaxSnapshotSize: 1, dreamCandidateMode: "hybrid", dreamCandidateMax: 4
|
|
212
|
+
});
|
|
213
|
+
assert.ok(seen.titles.includes(a.title) && seen.titles.includes(b.title),
|
|
214
|
+
"missing vectors are backfilled (embedder.embed), then the pair is compared");
|
|
215
|
+
store.close();
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test("issue#125 hybrid: cross-type pairs join only when allowCrossTypeMerge is on", async () => {
|
|
219
|
+
const { store, service, vectorIndex, embedder } = setup();
|
|
220
|
+
const proj = service.saveWithDedupe({ type: "project", title: "项目侧", content: "x", importance: 3 }).memory;
|
|
221
|
+
vectorIndex.saveEmbedding(proj.id, [1, 0, 0]);
|
|
222
|
+
await sleep(3);
|
|
223
|
+
const pref = service.saveWithDedupe({ type: "preference", title: "偏好侧", content: "y", importance: 3 }).memory;
|
|
224
|
+
vectorIndex.saveEmbedding(pref.id, [1, 0, 0]);
|
|
225
|
+
await sleep(3);
|
|
226
|
+
seed(service, vectorIndex, "窗内条目", [0, 1, 0]);
|
|
227
|
+
const semantic = { embedder, vectorIndex };
|
|
228
|
+
const cfg = { ...baseCfg, dreamMaxSnapshotSize: 1, dreamCandidateMode: "hybrid", dreamCandidateMax: 4 };
|
|
229
|
+
|
|
230
|
+
const off = capture();
|
|
231
|
+
await scheduler({}, semantic).runDream(off.ctx, service, cfg);
|
|
232
|
+
assert.ok(!off.seen.titles.includes(pref.title), "cross-type pair stays out by default (same-type only)");
|
|
233
|
+
|
|
234
|
+
const on = capture();
|
|
235
|
+
await scheduler({}, semantic).runDream(on.ctx, service, { ...cfg, allowCrossTypeMerge: true });
|
|
236
|
+
assert.ok(on.seen.titles.includes(proj.title) && on.seen.titles.includes(pref.title),
|
|
237
|
+
"cross-type pair enters the candidate set once explicitly allowed");
|
|
238
|
+
store.close();
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
test("issue#125 hybrid: a throwing embedder degrades to the plain window", async () => {
|
|
242
|
+
const { store, service, vectorIndex } = setup();
|
|
243
|
+
for (let i = 0; i < 3; i++) {
|
|
244
|
+
service.saveWithDedupe({ type: "project", title: `降级${i}`, content: `内容${i}`, importance: 3 });
|
|
245
|
+
await sleep(3);
|
|
246
|
+
}
|
|
247
|
+
const boom = {
|
|
248
|
+
embedSingle: async () => { throw new Error("embed down"); },
|
|
249
|
+
embed: async () => { throw new Error("embed down"); },
|
|
250
|
+
schedule: () => {},
|
|
251
|
+
modelHash: "x",
|
|
252
|
+
dimension: 3
|
|
253
|
+
};
|
|
254
|
+
const { ctx, seen } = capture();
|
|
255
|
+
const result = await scheduler({}, { embedder: boom, vectorIndex }).runDream(ctx, service, {
|
|
256
|
+
...baseCfg, dreamMaxSnapshotSize: 2, dreamCandidateMode: "hybrid"
|
|
257
|
+
});
|
|
258
|
+
assert.equal(result.ok, true, "向量层是增强而非依赖:嵌入失败不能让 run 失败");
|
|
259
|
+
assert.deepEqual(seen.titles, ["降级2", "降级1"], "falls back to the plain window");
|
|
260
|
+
store.close();
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test("issue#125 hybrid: a throwing vector index degrades to the plain window", async () => {
|
|
264
|
+
const { store, service, embedder } = setup();
|
|
265
|
+
for (let i = 0; i < 3; i++) {
|
|
266
|
+
service.saveWithDedupe({ type: "project", title: `索引坏${i}`, content: `内容${i}`, importance: 3 });
|
|
267
|
+
await sleep(3);
|
|
268
|
+
}
|
|
269
|
+
// getEmbedding 在 hybridCandidateMembers 的 try 内抛错 → 整体回落纯窗口。
|
|
270
|
+
const badIndex = {
|
|
271
|
+
getEmbedding: () => { throw new Error("index down"); },
|
|
272
|
+
saveEmbedding: () => {}
|
|
273
|
+
};
|
|
274
|
+
const { ctx, seen } = capture();
|
|
275
|
+
const result = await scheduler({}, { embedder, vectorIndex: badIndex }).runDream(ctx, service, {
|
|
276
|
+
...baseCfg, dreamMaxSnapshotSize: 2, dreamCandidateMode: "hybrid"
|
|
277
|
+
});
|
|
278
|
+
assert.equal(result.ok, true, "run still succeeds");
|
|
279
|
+
assert.deepEqual(seen.titles, ["索引坏2", "索引坏1"], "falls back to the plain window");
|
|
280
|
+
store.close();
|
|
281
|
+
});
|
package/test/dream.test.js
CHANGED
|
@@ -27,6 +27,56 @@ test("unknown id rejects whole list", () => {
|
|
|
27
27
|
assert.ok(errors.some((e) => e.includes("zzz")));
|
|
28
28
|
});
|
|
29
29
|
|
|
30
|
+
// --- Issue #135: 唯一前缀解析 -------------------------------------------------
|
|
31
|
+
// 模型常把 36 位 UUID 回填成前缀(报告实测 78/78 个 8 位前缀都能唯一对应窗口内
|
|
32
|
+
// 真实记忆,且 few-shot 示例的 "m1"/"m2" 占位符在诱导这种缩写)。校验前的唯一
|
|
33
|
+
// 前缀解析把它们还原成完整 id,而不是逐条 unknown id 后被覆盖率闸整单拒绝。
|
|
34
|
+
|
|
35
|
+
test("Issue #135: unique short prefixes resolve to full ids (ids + keepSource)", () => {
|
|
36
|
+
const idA = "f77a0d17-2586-41fe-a789-d69f4d1cee83";
|
|
37
|
+
const idB = "bc17c9ed-e84c-4b9f-bc36-48f8e9a6b3e0";
|
|
38
|
+
const snap = snapshot([idA, idB]);
|
|
39
|
+
const decisions = [
|
|
40
|
+
{ action: "merge", ids: ["f77a0d17", "bc17c9ed"], title: "合并", content: "合并后的内容", importance: 4, keepSource: "f77a0d17" }
|
|
41
|
+
];
|
|
42
|
+
const { ok, errors, resolvedShortIds } = validateDecisions(decisions, snap);
|
|
43
|
+
assert.equal(ok, true, JSON.stringify(errors));
|
|
44
|
+
assert.equal(resolvedShortIds, 3, "two ids + keepSource resolved");
|
|
45
|
+
assert.deepEqual(decisions[0].ids, [idA, idB], "ids rewritten in place");
|
|
46
|
+
assert.equal(decisions[0].keepSource, idA, "keepSource rewritten");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("Issue #135: conflict winner/loser short prefixes resolve", () => {
|
|
50
|
+
const idA = "f77a0d17-2586-41fe-a789-d69f4d1cee83";
|
|
51
|
+
const idB = "bc17c9ed-e84c-4b9f-bc36-48f8e9a6b3e0";
|
|
52
|
+
const snap = snapshot([idA, idB]);
|
|
53
|
+
const decisions = [{ action: "conflict", winner: "f77a0d17", loser: "bc17c9ed", reason: "内容矛盾" }];
|
|
54
|
+
const { ok, errors, resolvedShortIds } = validateDecisions(decisions, snap);
|
|
55
|
+
assert.equal(ok, true, JSON.stringify(errors));
|
|
56
|
+
assert.equal(resolvedShortIds, 2);
|
|
57
|
+
assert.equal(decisions[0].winner, idA, "winner rewritten");
|
|
58
|
+
assert.equal(decisions[0].loser, idB, "loser rewritten");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("Issue #135: ambiguous prefix stays unresolved and reports unknown id", () => {
|
|
62
|
+
const snap = snapshot([
|
|
63
|
+
"aaaaaaaa-1111-4111-8111-111111111111",
|
|
64
|
+
"aaaaaaaa-2222-4222-8222-222222222222"
|
|
65
|
+
]);
|
|
66
|
+
const { ok, errors, resolvedShortIds } = validateDecisions([{ action: "archive", ids: ["aaaaaaaa"] }], snap);
|
|
67
|
+
assert.equal(ok, false);
|
|
68
|
+
assert.equal(resolvedShortIds, 0, "ambiguous prefix is never guessed");
|
|
69
|
+
assert.ok(errors.some((e) => e.includes("unknown id")), JSON.stringify(errors));
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("Issue #135: unmatched prefix is left for the validator to report", () => {
|
|
73
|
+
const snap = snapshot(["f77a0d17-2586-41fe-a789-d69f4d1cee83"]);
|
|
74
|
+
const { ok, errors, resolvedShortIds } = validateDecisions([{ action: "archive", ids: ["deadbeef"] }], snap);
|
|
75
|
+
assert.equal(ok, false);
|
|
76
|
+
assert.equal(resolvedShortIds, 0, "no match means no rewrite");
|
|
77
|
+
assert.ok(errors.some((e) => e.includes("deadbeef")), JSON.stringify(errors));
|
|
78
|
+
});
|
|
79
|
+
|
|
30
80
|
test("invalid action rejects", () => {
|
|
31
81
|
const snap = snapshot(["a"]);
|
|
32
82
|
const { ok } = validateDecisions([{ action: "explode", ids: ["a"] }], snap);
|
|
@@ -486,6 +536,36 @@ test("runDream fails safe on invalid decisions", async () => {
|
|
|
486
536
|
store.close();
|
|
487
537
|
});
|
|
488
538
|
|
|
539
|
+
test("Issue #135: failed validation persists { _validationFailed, errors, skipped } into the run row", async () => {
|
|
540
|
+
const { store, service } = dreamSetup();
|
|
541
|
+
const saved = service.saveWithDedupe({ type: "preference", title: "语言", content: "中文" });
|
|
542
|
+
let calls = 0;
|
|
543
|
+
const ctx = {
|
|
544
|
+
llm: {
|
|
545
|
+
stream: async function* () {
|
|
546
|
+
calls++;
|
|
547
|
+
yield { type: "text-delta", text: calls === 1 ? JSON.stringify([{ action: "archive", ids: ["deadbeef"] }]) : "summary" };
|
|
548
|
+
yield { type: "finish", reason: { kind: "ok" } };
|
|
549
|
+
}
|
|
550
|
+
},
|
|
551
|
+
logger: { warn: () => {}, info: () => {} }
|
|
552
|
+
};
|
|
553
|
+
const dream = createDreamScheduler({ thresholdCount: 1, thresholdChars: 0, delayMs: 0 });
|
|
554
|
+
const result = await dream.runDream(ctx, service, { dreamProvider: "deepseek", dreamModel: "deepseek-chat" });
|
|
555
|
+
assert.equal(result.ok, false, "coverage gate rejects (0% explicit coverage)");
|
|
556
|
+
assert.ok(Array.isArray(result.decisions) && result.decisions[0]?._validationFailed === true,
|
|
557
|
+
"failure result carries the validation detail");
|
|
558
|
+
assert.ok(result.decisions[0].skipped.some((s) => s.error.includes("deadbeef")),
|
|
559
|
+
"skipped keeps the per-item reason");
|
|
560
|
+
// 落库验证:dream_runs.decisions 不再是 NULL,事后可离线定位失败原因。
|
|
561
|
+
const run = store.getDreamRun(result.runId);
|
|
562
|
+
assert.ok(run?.decisions?.[0]?._validationFailed === true, "detail persisted in dream_runs.decisions");
|
|
563
|
+
assert.ok(run.decisions[0].skipped.length >= 1);
|
|
564
|
+
const lang = store.getById(saved.memory.id);
|
|
565
|
+
assert.equal(lang.archived, false, "unknown-id decision must not touch real memories");
|
|
566
|
+
store.close();
|
|
567
|
+
});
|
|
568
|
+
|
|
489
569
|
// --- item ①: CAS guard against concurrent edits ----------------------------
|
|
490
570
|
|
|
491
571
|
test("applyDecisions CAS: merge onto a concurrently-edited target is skipped as a conflict, not applied", () => {
|
|
@@ -1029,6 +1109,49 @@ test("issue#89: dream run with a skipped invalid decision lands the valid subset
|
|
|
1029
1109
|
store.close();
|
|
1030
1110
|
});
|
|
1031
1111
|
|
|
1112
|
+
// Issue #104(跟进 #137 失败路径):degraded(合法子集已应用)轮的跳过明细此前只进
|
|
1113
|
+
// logger.warn,splice 之后就地从 decisions 里消失,落库的已是幸存列表——离线回放
|
|
1114
|
+
// dream_runs 无法判断被跳的是跨类型 merge、update 保护期还是 unknown id。这里断言
|
|
1115
|
+
// 明细进了独立的 skipped 列(而非内联进 decisions),且携带可定位的逐条原因。
|
|
1116
|
+
test("issue#104: degraded run persists the skipped decision detail into dream_runs.skipped", async () => {
|
|
1117
|
+
const { store, service } = dreamSetup();
|
|
1118
|
+
const a = service.saveWithDedupe({ type: "project", title: "旧A", content: "过时A" }).memory;
|
|
1119
|
+
const b = service.saveWithDedupe({ type: "project", title: "旧B", content: "过时B" }).memory;
|
|
1120
|
+
const pref = service.saveWithDedupe({ type: "preference", title: "语言", content: "中文" }).memory;
|
|
1121
|
+
const c = service.saveWithDedupe({ type: "project", title: "旧C", content: "过时C" }).memory;
|
|
1122
|
+
const ctx = {
|
|
1123
|
+
logger: { warn: () => {}, info: () => {} },
|
|
1124
|
+
llm: {
|
|
1125
|
+
async *stream(options) {
|
|
1126
|
+
const userText = options.messages.find((m) => m.role === "user")?.content?.[0]?.text ?? "";
|
|
1127
|
+
if (userText.startsWith("id=")) {
|
|
1128
|
+
// 一条跨类型 merge(非法 → 跳过)+ 两条合法 archive(显式覆盖 2/4,过 50% 下限)
|
|
1129
|
+
yield { type: "text-delta", index: 0, text: JSON.stringify([
|
|
1130
|
+
{ action: "merge", ids: [pref.id, a.id], keepSource: pref.id, title: "跨类型", content: "不应合并", importance: 4 },
|
|
1131
|
+
{ action: "archive", ids: [b.id], reason: "stale" },
|
|
1132
|
+
{ action: "archive", ids: [c.id], reason: "stale" }
|
|
1133
|
+
]) };
|
|
1134
|
+
} else {
|
|
1135
|
+
yield { type: "text-delta", index: 0, text: "记忆库总览:degraded 轮的跳过明细可从审计行回放。" };
|
|
1136
|
+
}
|
|
1137
|
+
yield { type: "finish", reason: { kind: "stop" } };
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
};
|
|
1141
|
+
const dream = createDreamScheduler({ thresholdCount: 1, thresholdChars: 0, delayMs: 0 });
|
|
1142
|
+
const result = await dream.runDream(ctx, service, { dreamProvider: "mock", dreamModel: "mock-model" });
|
|
1143
|
+
assert.equal(result.status, "degraded", "run degrades on the skipped decision");
|
|
1144
|
+
// 落库验证:skipped 列不再是 NULL,逐条带 index/action/error,事后无需重跑即可定位成因。
|
|
1145
|
+
const run = store.getDreamRun(result.runId);
|
|
1146
|
+
assert.ok(Array.isArray(run?.skipped) && run.skipped.length === 1, "skipped detail persisted in dream_runs.skipped");
|
|
1147
|
+
assert.equal(run.skipped[0].index, 0, "the skipped entry is the cross-type merge");
|
|
1148
|
+
assert.equal(run.skipped[0].action, "merge");
|
|
1149
|
+
assert.match(run.skipped[0].error, /multiple types/, "per-item skip reason is kept");
|
|
1150
|
+
// 独立字段,不污染按「决策数组」消费 decisions 的下游读者。
|
|
1151
|
+
assert.ok(!run.decisions.some((d) => d && d._skipped !== undefined), "decisions stays free of inline markers");
|
|
1152
|
+
store.close();
|
|
1153
|
+
});
|
|
1154
|
+
|
|
1032
1155
|
test("issue#89: dreamSkipInvalid:false restores the whole-batch strict rejection", async () => {
|
|
1033
1156
|
const { store, service } = dreamSetup();
|
|
1034
1157
|
const a = service.saveWithDedupe({ type: "project", title: "旧A", content: "过时A" }).memory;
|
|
@@ -1076,3 +1199,52 @@ test("issue#89: minIntervalMs throttles re-triggering regardless of run outcome"
|
|
|
1076
1199
|
assert.equal(dream.maybeSchedule(service), true, "interval elapsed → eligible again (baseline unmoved by failure)");
|
|
1077
1200
|
store.close();
|
|
1078
1201
|
});
|
|
1202
|
+
|
|
1203
|
+
// --- Issue #126:新动作(supersede / differentiate)在 dream 路径也必须产 receipt ---
|
|
1204
|
+
// buildRecordReceipts 此前只认 merge/conflict/update,新动作会静默不产 receipt,让
|
|
1205
|
+
// receipt_chain 留洞。dream 的 consolidation prompt 虽然不含这两个动作,但校验器
|
|
1206
|
+
// 的 ACTIONS 是共用的——模型自由发挥(或将来把动作集扩到 dream)时审计不能丢。
|
|
1207
|
+
|
|
1208
|
+
test("issue#126: a supersede decision on the dream path produces a supersede receipt", async () => {
|
|
1209
|
+
const { store, service } = dreamSetup();
|
|
1210
|
+
const oldMem = service.saveWithDedupe({ type: "project", title: "方案 v1", content: "用 pm2 常驻" }).memory;
|
|
1211
|
+
const newMem = service.saveWithDedupe({ type: "project", title: "方案 v2", content: "改用 systemd" }).memory;
|
|
1212
|
+
const ctx = mockCtx({
|
|
1213
|
+
onConsolidation: () => JSON.stringify([
|
|
1214
|
+
{ action: "supersede", winner: newMem.id, loser: oldMem.id, reason: "版本演进" }
|
|
1215
|
+
])
|
|
1216
|
+
});
|
|
1217
|
+
const dream = createDreamScheduler({ thresholdCount: 1, thresholdChars: 0, delayMs: 0 });
|
|
1218
|
+
const result = await dream.runDream(ctx, service, { dreamProvider: "mock", dreamModel: "mock-model" });
|
|
1219
|
+
assert.equal(result.status, "ok", "supersede applied and the summary landed");
|
|
1220
|
+
assert.equal(store.getById(oldMem.id).archived, true, "older entry archived");
|
|
1221
|
+
assert.equal(store.getById(newMem.id).content, "改用 systemd", "winner body untouched");
|
|
1222
|
+
const receipts = store.listReceipts();
|
|
1223
|
+
const supersedeReceipt = receipts.find((r) => r.kind === "supersede");
|
|
1224
|
+
assert.ok(supersedeReceipt, "supersede receipt lands in receipt_chain");
|
|
1225
|
+
assert.equal(supersedeReceipt.record_id, oldMem.id, "receipt records the superseded side");
|
|
1226
|
+
assert.equal(supersedeReceipt.winner_id, newMem.id);
|
|
1227
|
+
assert.equal(supersedeReceipt.loser_id, oldMem.id);
|
|
1228
|
+
store.close();
|
|
1229
|
+
});
|
|
1230
|
+
|
|
1231
|
+
test("issue#126: a differentiate decision on the dream path produces one receipt per entry", async () => {
|
|
1232
|
+
const { store, service } = dreamSetup();
|
|
1233
|
+
const a = service.saveWithDedupe({ type: "project", title: "内网端口", content: "内网走 22" }).memory;
|
|
1234
|
+
const b = service.saveWithDedupe({ type: "project", title: "外网端口", content: "外网映射 2222" }).memory;
|
|
1235
|
+
const ctx = mockCtx({
|
|
1236
|
+
onConsolidation: () => JSON.stringify([
|
|
1237
|
+
{ action: "differentiate", ids: [a.id, b.id], distinctions: ["内网直连", "外网映射"] }
|
|
1238
|
+
])
|
|
1239
|
+
});
|
|
1240
|
+
const dream = createDreamScheduler({ thresholdCount: 1, thresholdChars: 0, delayMs: 0 });
|
|
1241
|
+
const result = await dream.runDream(ctx, service, { dreamProvider: "mock", dreamModel: "mock-model" });
|
|
1242
|
+
assert.equal(result.status, "ok", "differentiate applied and the summary landed");
|
|
1243
|
+
assert.equal(store.getById(a.id).archived, false, "neither side is archived");
|
|
1244
|
+
assert.equal(store.getById(b.id).archived, false);
|
|
1245
|
+
const receipts = store.listReceipts().filter((r) => r.kind === "differentiate");
|
|
1246
|
+
assert.equal(receipts.length, 2, "one receipt per differentiated entry");
|
|
1247
|
+
assert.deepEqual(receipts.map((r) => r.record_id).sort(), [a.id, b.id].sort());
|
|
1248
|
+
assert.ok(receipts.every((r) => r.count_before === 1 && r.count_after === 1), "no count change for differentiate");
|
|
1249
|
+
store.close();
|
|
1250
|
+
});
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { createEmbedder } from "../src/embedding.js";
|
|
4
|
+
|
|
5
|
+
// issue #135: the legacy OpenAI-compatible embedder is an object literal with no
|
|
6
|
+
// async init, so api.js's generic `"ready" in embedder` branch used to fall
|
|
7
|
+
// through to "assume usable" — the status card stayed green while baseUrl /
|
|
8
|
+
// apiKey / model were all empty and not a single vector could be produced
|
|
9
|
+
// ("绿色假阳性"). These tests pin the real predicate and the getter semantics.
|
|
10
|
+
|
|
11
|
+
const FULL = {
|
|
12
|
+
enabled: true,
|
|
13
|
+
baseUrl: "https://api.example.com/v1",
|
|
14
|
+
apiKey: "sk-test",
|
|
15
|
+
model: "text-embedding-3-small"
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/** Build the embedder against a settings double + a recording store. */
|
|
19
|
+
function make(cfg, { live = false } = {}) {
|
|
20
|
+
const writes = [];
|
|
21
|
+
const box = { cfg };
|
|
22
|
+
const embedder = createEmbedder({
|
|
23
|
+
store: { setEmbedding: (id, vector) => writes.push({ id, vector }) },
|
|
24
|
+
settings: { getVectorConfig: () => (live ? box.cfg : cfg) },
|
|
25
|
+
logger: { info() {}, warn() {} }
|
|
26
|
+
});
|
|
27
|
+
return { embedder, writes, box };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
test("#135 legacy OpenAI embedder reports ready/configured from the config", () => {
|
|
31
|
+
const { embedder } = make(FULL);
|
|
32
|
+
assert.equal(embedder.ready, true, "fully configured → ready");
|
|
33
|
+
assert.equal(embedder.configured, true);
|
|
34
|
+
|
|
35
|
+
// Any of the four fields missing → not configured. `enabled` flips to false,
|
|
36
|
+
// the rest are blanked (the shape the settings panel leaves behind when the
|
|
37
|
+
// user never filled the vector section in).
|
|
38
|
+
for (const key of ["enabled", "baseUrl", "apiKey", "model"]) {
|
|
39
|
+
const broken = { ...FULL, [key]: key === "enabled" ? false : "" };
|
|
40
|
+
const { embedder: e } = make(broken);
|
|
41
|
+
assert.equal(e.configured, false, `blank ${key} → not configured`);
|
|
42
|
+
assert.equal(e.ready, false, `blank ${key} → not ready`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Defensive: a settings double returning {} (or nothing) must not throw.
|
|
46
|
+
assert.equal(make({}).embedder.ready, false);
|
|
47
|
+
assert.equal(make(undefined).embedder.ready, false);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("#135 ready/configured are live getters, not boot-time snapshots", () => {
|
|
51
|
+
// The panel writes vector-config through settings; the legacy embedder reads
|
|
52
|
+
// it per call (same as embed()), so fixing the config must not need a restart.
|
|
53
|
+
const { embedder, box } = make(FULL, { live: true });
|
|
54
|
+
assert.equal(embedder.ready, true);
|
|
55
|
+
box.cfg = { ...FULL, apiKey: "" };
|
|
56
|
+
assert.equal(embedder.ready, false, "getter re-reads the live config");
|
|
57
|
+
assert.equal(embedder.configured, false);
|
|
58
|
+
box.cfg = FULL;
|
|
59
|
+
assert.equal(embedder.ready, true, "…and recovers once the config is complete");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("#135 unconfigured embedder never reaches the network", async () => {
|
|
63
|
+
const { embedder, writes } = make({ ...FULL, apiKey: "" });
|
|
64
|
+
// A real fetch here would go out with an empty bearer token.
|
|
65
|
+
assert.equal(await embedder.embed("hello"), null);
|
|
66
|
+
assert.equal(await embedder.embedSingle("hello"), null);
|
|
67
|
+
|
|
68
|
+
embedder.schedule({ id: "m1", title: "t", content: "c" });
|
|
69
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
70
|
+
assert.deepEqual(writes, [], "schedule() must not store an embedding");
|
|
71
|
+
|
|
72
|
+
// A memory without an id is a no-op (never a store write with undefined id).
|
|
73
|
+
embedder.schedule(null);
|
|
74
|
+
embedder.schedule({});
|
|
75
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
76
|
+
assert.deepEqual(writes, []);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("#135 unconfigured embedder reports no model fingerprint or dimension", () => {
|
|
80
|
+
const { embedder } = make(FULL);
|
|
81
|
+
assert.equal(embedder.name, "OpenAI", "explicit name for the status card");
|
|
82
|
+
assert.equal(embedder.dimension, undefined, "no dimension before a first embed");
|
|
83
|
+
// model#hex — same fingerprint format the local embedders use.
|
|
84
|
+
assert.match(embedder.modelHash, /^text-embedding-3-small#[0-9a-f]+$/);
|
|
85
|
+
assert.equal(make({ ...FULL, model: "" }).embedder.modelHash, undefined);
|
|
86
|
+
assert.equal(make({ ...FULL, enabled: false }).embedder.modelHash, undefined);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// ── 覆盖补齐:embed 成功/降级、embedSingle 兜底、reindexMissing、schedule 成功路径 ──
|
|
90
|
+
|
|
91
|
+
/** 临时替换全局 fetch(embedText 直接调它),fn 结束后恢复。 */
|
|
92
|
+
async function withFetch(impl, fn) {
|
|
93
|
+
const original = globalThis.fetch;
|
|
94
|
+
globalThis.fetch = impl;
|
|
95
|
+
try {
|
|
96
|
+
return await fn();
|
|
97
|
+
} finally {
|
|
98
|
+
globalThis.fetch = original;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const okResponse = (embedding) => ({
|
|
103
|
+
ok: true,
|
|
104
|
+
json: async () => ({ data: [{ embedding }] })
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test("embed: 完整配置时走真实 fetch,成功返回向量并记下维度", async () => {
|
|
108
|
+
const { embedder } = make(FULL);
|
|
109
|
+
await withFetch(async () => okResponse([1, 2, 3, 4]), async () => {
|
|
110
|
+
const vec = await embedder.embed("猫咪");
|
|
111
|
+
assert.deepEqual(vec, [1, 2, 3, 4]);
|
|
112
|
+
assert.equal(embedder.dimension, 4, "成功嵌入后记下维度供指纹用");
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("embed: 网络失败 / 非 2xx / 响应不可用都降级为 null,不抛", async () => {
|
|
117
|
+
const { embedder } = make(FULL);
|
|
118
|
+
await withFetch(async () => { throw new Error("ECONNREFUSED"); }, async () => {
|
|
119
|
+
assert.equal(await embedder.embed("x"), null);
|
|
120
|
+
});
|
|
121
|
+
await withFetch(async () => ({ ok: false }), async () => {
|
|
122
|
+
assert.equal(await embedder.embed("x"), null);
|
|
123
|
+
});
|
|
124
|
+
await withFetch(async () => ({ ok: true, json: async () => { throw new Error("bad json"); } }), async () => {
|
|
125
|
+
assert.equal(await embedder.embed("x"), null);
|
|
126
|
+
});
|
|
127
|
+
await withFetch(async () => ({ ok: true, json: async () => ({ data: [] }) }), async () => {
|
|
128
|
+
assert.equal(await embedder.embed("x"), null, "空 data 数组不可用");
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test("embedSingle: embed 缺失时兜底返回 null(统一旧接口契约)", async () => {
|
|
133
|
+
const { embedder } = make(FULL);
|
|
134
|
+
assert.equal(await embedder.embedSingle.call({}, "x"), null, "无 embed 方法时返回 null 而非抛错");
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("reindexMissing: 逐行回填并统计 indexed/skipped,有成功才记模型指纹", async () => {
|
|
138
|
+
let markCalls = 0;
|
|
139
|
+
const rows = [{ id: "m1", title: "t1", content: "c1" }, { id: "m2", title: "t2", content: "c2" }];
|
|
140
|
+
const writes = [];
|
|
141
|
+
const embedder = createEmbedder({
|
|
142
|
+
store: {
|
|
143
|
+
setEmbedding: (id, v) => writes.push({ id, v }),
|
|
144
|
+
needsEmbedding: () => rows
|
|
145
|
+
},
|
|
146
|
+
settings: { getVectorConfig: () => FULL },
|
|
147
|
+
vectorIndex: { markModel: () => { markCalls++; } }
|
|
148
|
+
});
|
|
149
|
+
let call = 0;
|
|
150
|
+
await withFetch(async () => okResponse([call++, 0]), async () => {
|
|
151
|
+
const result = await embedder.reindexMissing(50);
|
|
152
|
+
assert.equal(result.indexed, 2);
|
|
153
|
+
assert.equal(result.skipped, 0);
|
|
154
|
+
assert.equal(writes.length, 2);
|
|
155
|
+
assert.equal(markCalls, 1, "有成功回填才记模型指纹");
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test("reindexMissing: 未启用时直接返回 0/0,不碰 store", async () => {
|
|
160
|
+
const { embedder } = make({ ...FULL, enabled: false });
|
|
161
|
+
const result = await embedder.reindexMissing(50);
|
|
162
|
+
assert.deepEqual(result, { indexed: 0, skipped: 0 });
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test("reindexMissing: 嵌入失败的行走 skipped,不算 indexed", async () => {
|
|
166
|
+
const writes = [];
|
|
167
|
+
const embedder = createEmbedder({
|
|
168
|
+
store: {
|
|
169
|
+
setEmbedding: (id, v) => writes.push({ id, v }),
|
|
170
|
+
needsEmbedding: () => [{ id: "m1", title: "t", content: "c" }]
|
|
171
|
+
},
|
|
172
|
+
settings: { getVectorConfig: () => FULL }
|
|
173
|
+
});
|
|
174
|
+
await withFetch(async () => ({ ok: false }), async () => {
|
|
175
|
+
const result = await embedder.reindexMissing(50);
|
|
176
|
+
assert.equal(result.indexed, 0);
|
|
177
|
+
assert.equal(result.skipped, 1, "嵌入失败的行走 skipped,不算 indexed");
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test("schedule: 嵌入成功时写入 store 并记 info 日志(embedFor 成功路径)", async () => {
|
|
182
|
+
const writes = [];
|
|
183
|
+
let logged = 0;
|
|
184
|
+
const embedder = createEmbedder({
|
|
185
|
+
store: { setEmbedding: (id, v) => writes.push({ id, v }) },
|
|
186
|
+
settings: { getVectorConfig: () => FULL },
|
|
187
|
+
logger: { info: () => { logged++; }, warn() {} },
|
|
188
|
+
vectorIndex: { markModel() {} }
|
|
189
|
+
});
|
|
190
|
+
await withFetch(async () => okResponse([0.1, 0.2]), async () => {
|
|
191
|
+
embedder.schedule({ id: "m9", title: "t", content: "c" });
|
|
192
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
193
|
+
});
|
|
194
|
+
assert.equal(writes.length, 1);
|
|
195
|
+
assert.equal(logged, 1, "成功嵌入记一行 info 日志");
|
|
196
|
+
});
|