@modusensus/dsh-mneme 0.8.1 → 0.8.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 +14 -3
- package/lib/api-standalone.js +12 -0
- package/lib/api.js +12 -2
- package/lib/client.js +5 -1
- package/lib/config.js +18 -0
- package/lib/dream/decisions.js +55 -8
- package/lib/dream/sleep.js +36 -5
- package/lib/dream.js +18 -7
- package/lib/entities/extractor.js +2 -2
- package/lib/index.js +8 -0
- package/lib/inject.js +43 -1
- package/lib/lang.js +2 -2
- package/lib/local-embedder.js +21 -1
- package/lib/reranker.js +47 -8
- package/lib/runtime/model-fetch.js +361 -0
- package/lib/service.js +37 -5
- package/lib/settings.js +1 -0
- package/lib/store.js +85 -25
- package/lib/summarize.js +2 -2
- package/lib/tools.js +6 -19
- package/lib/vector-index.js +2 -8
- package/package.json +2 -1
- package/scripts/sync-test-badge.mjs +61 -0
- package/src/api-standalone.js +12 -0
- package/src/api.js +12 -2
- package/src/config.js +18 -0
- package/src/dream/decisions.js +55 -8
- package/src/dream/sleep.js +36 -5
- package/src/dream.js +18 -7
- package/src/entities/extractor.js +2 -2
- package/src/index.js +8 -0
- package/src/inject.js +43 -1
- package/src/lang.js +2 -2
- package/src/local-embedder.js +21 -1
- package/src/reranker.js +47 -8
- package/src/runtime/model-fetch.js +361 -0
- package/src/service.js +37 -5
- package/src/settings.js +1 -0
- package/src/store.js +85 -25
- package/src/summarize.js +2 -2
- package/src/tools.js +6 -19
- package/src/vector-index.js +2 -8
- package/test/api.test.js +10 -4
- package/test/dream-archive-guardrail.test.js +116 -0
- package/test/dream-coverage-degrade.test.js +86 -0
- package/test/dream.test.js +15 -11
- package/test/inject-first-round-bm25.test.js +89 -0
- package/test/inject-rotation.test.js +161 -0
- package/test/llm-message-source.test.js +177 -0
- package/test/local-embedder.test.js +20 -0
- package/test/reranker-single-logit.test.js +73 -0
- package/test/runtime-model-fetch.test.js +348 -0
- package/test/skip-invalid-restore.test.js +7 -5
- package/test/sleep-scheduler-rearm.test.js +89 -0
- package/test/sleep.test.js +2 -2
- package/test/standalone-api.test.js +34 -1
- package/test/store-vector-cache.test.js +98 -0
- package/test/tools-memory-get-schema.test.js +65 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { createStore } from "../src/store.js";
|
|
4
|
+
import { createService } from "../src/service.js";
|
|
5
|
+
import { createSleepScheduler } from "../src/dream/sleep.js";
|
|
6
|
+
|
|
7
|
+
// 回归(issue #187):idle 计时器到点时若撞上 sleepMinIntervalHours(CD)未满,
|
|
8
|
+
// maybeSchedule 返回 false 后没有任何东西重排——armIdleTimer 的回调把返回值丢掉、
|
|
9
|
+
// idleTimer 已被置 null,"CD 到期"成了没有监听者的时刻,库里攒着待清理的新写入
|
|
10
|
+
// 却什么都不发生,要等用户恰好再写一次才救回来。两个断言方向:
|
|
11
|
+
// 1. 撞 CD 后必须按剩余窗口重排,CD 到点自行起跑;
|
|
12
|
+
// 2. 调度器构造时就该挂表——重启后零写入也应有闹钟(同族缺陷:无表可 firing)。
|
|
13
|
+
// 全程注入时钟/定时器,不依赖真实等待。
|
|
14
|
+
|
|
15
|
+
function makeHarness(overrides = {}) {
|
|
16
|
+
let nowMs = 1_000_000;
|
|
17
|
+
const timers = [];
|
|
18
|
+
let seq = 1;
|
|
19
|
+
const store = createStore(":memory:");
|
|
20
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
21
|
+
let runs = 0;
|
|
22
|
+
const sched = createSleepScheduler({
|
|
23
|
+
service,
|
|
24
|
+
config: { sleepModeEnabled: true, sleepIdleMinutes: 5, sleepMinIntervalHours: 1, ...overrides },
|
|
25
|
+
logger: { warn: () => {} },
|
|
26
|
+
onRun: async () => { runs++; return { ok: true }; },
|
|
27
|
+
now: () => nowMs,
|
|
28
|
+
setTimeoutFn: (fn, delay) => {
|
|
29
|
+
const t = { id: seq++, at: nowMs + delay, fn };
|
|
30
|
+
timers.push(t);
|
|
31
|
+
return t.id;
|
|
32
|
+
},
|
|
33
|
+
clearTimeoutFn: (id) => {
|
|
34
|
+
const i = timers.findIndex((t) => t.id === id);
|
|
35
|
+
if (i >= 0) timers.splice(i, 1);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
// 触发最早到点的那个表;回调内部可能再挂新表,继续留在队列里等下一次触发。
|
|
39
|
+
const fireNext = async () => {
|
|
40
|
+
timers.sort((a, b) => a.at - b.at);
|
|
41
|
+
const t = timers.shift();
|
|
42
|
+
if (!t) throw new Error("no armed timer to fire");
|
|
43
|
+
nowMs = t.at;
|
|
44
|
+
await t.fn();
|
|
45
|
+
};
|
|
46
|
+
return {
|
|
47
|
+
sched,
|
|
48
|
+
store,
|
|
49
|
+
timers,
|
|
50
|
+
fireNext,
|
|
51
|
+
runs: () => runs,
|
|
52
|
+
now: () => nowMs,
|
|
53
|
+
write: () => { nowMs += 1000; sched.noteWrite(); }
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
test("sleep #187: CD collision re-arms a timer instead of dropping the round", async () => {
|
|
58
|
+
const h = makeHarness();
|
|
59
|
+
h.write();
|
|
60
|
+
await h.fireNext();
|
|
61
|
+
assert.equal(h.runs(), 1, "first run fires after the idle window");
|
|
62
|
+
const firstRunAt = h.now();
|
|
63
|
+
|
|
64
|
+
h.write(); // 落在 CD 窗口内的写入(默认 8h 时几乎必中)
|
|
65
|
+
await h.fireNext(); // idle 到点 → CD 未满 → 修复前:整轮被静默丢弃
|
|
66
|
+
assert.ok(h.timers.length > 0, "a timer must be re-armed after the CD collision");
|
|
67
|
+
await h.fireNext(); // 重排的表 → CD 到点 → 自行起跑
|
|
68
|
+
assert.equal(h.runs(), 2, "second run fires on its own at the CD boundary");
|
|
69
|
+
assert.ok(
|
|
70
|
+
h.now() >= firstRunAt + 3_600_000,
|
|
71
|
+
`second run must wait out the full min interval (fired at +${h.now() - firstRunAt}ms)`
|
|
72
|
+
);
|
|
73
|
+
h.store.close();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("sleep #187: scheduler arms its first timer at construction (no write needed)", async () => {
|
|
77
|
+
const h = makeHarness();
|
|
78
|
+
assert.equal(h.timers.length, 1, "construction arms the idle timer");
|
|
79
|
+
await h.fireNext();
|
|
80
|
+
assert.equal(h.runs(), 1, "a quiet boot still reaches its first sleep cycle");
|
|
81
|
+
h.store.close();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("sleep #187: dispose cancels the construction-armed timer", async () => {
|
|
85
|
+
const h = makeHarness();
|
|
86
|
+
await h.sched.dispose();
|
|
87
|
+
assert.equal(h.timers.length, 0, "dispose clears the armed timer");
|
|
88
|
+
h.store.close();
|
|
89
|
+
});
|
package/test/sleep.test.js
CHANGED
|
@@ -165,8 +165,8 @@ test("sleep: dispose clears the timer and prevents further runs", async () => {
|
|
|
165
165
|
setTimeoutFn: () => 1,
|
|
166
166
|
clearTimeoutFn: () => { cleared = true; }
|
|
167
167
|
});
|
|
168
|
-
|
|
169
|
-
assert.equal(cleared, false, "timer armed, not yet cleared");
|
|
168
|
+
// #187 起构造即挂表——dispose 必须能清掉这张(以及 noteWrite 重挂过的任何)表。
|
|
169
|
+
assert.equal(cleared, false, "timer armed at construction, not yet cleared");
|
|
170
170
|
await sched.dispose();
|
|
171
171
|
assert.equal(cleared, true, "idle timer cleared on dispose");
|
|
172
172
|
assert.equal(sched.shouldRun(1_000_000 + 6 * 60000), false, "disposed never runs");
|
|
@@ -40,7 +40,7 @@ test("GET /health is open without a token; every other route 401s", async () =>
|
|
|
40
40
|
assert.equal(health.status, 200);
|
|
41
41
|
assert.deepEqual(await health.json(), { ok: true });
|
|
42
42
|
|
|
43
|
-
for (const path of ["/status", "/memories", "/memories/x", "/search?q=x"]) {
|
|
43
|
+
for (const path of ["/status", "/profile", "/rules", "/memories", "/memories/x", "/search?q=x"]) {
|
|
44
44
|
const res = await fetch(`${base}${path}`);
|
|
45
45
|
assert.equal(res.status, 401, `${path} requires a token`);
|
|
46
46
|
assert.deepEqual(await res.json(), { error: "unauthorized" });
|
|
@@ -53,6 +53,39 @@ test("GET /health is open without a token; every other route 401s", async () =>
|
|
|
53
53
|
}
|
|
54
54
|
});
|
|
55
55
|
|
|
56
|
+
test("GET /profile and /rules return saved settings", async () => {
|
|
57
|
+
const { base, auth, settings, close } = await setup();
|
|
58
|
+
try {
|
|
59
|
+
settings.setProfile("我是后端工程师");
|
|
60
|
+
settings.setRules(["先验证再修改", "默认使用中文回复"]);
|
|
61
|
+
|
|
62
|
+
const profile = await fetch(`${base}/profile`, { headers: auth });
|
|
63
|
+
assert.equal(profile.status, 200);
|
|
64
|
+
assert.deepEqual(await profile.json(), { profile: "我是后端工程师" });
|
|
65
|
+
|
|
66
|
+
const rules = await fetch(`${base}/rules`, { headers: auth });
|
|
67
|
+
assert.equal(rules.status, 200);
|
|
68
|
+
assert.deepEqual(await rules.json(), { rules: ["先验证再修改", "默认使用中文回复"] });
|
|
69
|
+
} finally {
|
|
70
|
+
close();
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("GET /profile and /rules return empty values when unset", async () => {
|
|
75
|
+
const { base, auth, close } = await setup();
|
|
76
|
+
try {
|
|
77
|
+
const profile = await fetch(`${base}/profile`, { headers: auth });
|
|
78
|
+
assert.equal(profile.status, 200);
|
|
79
|
+
assert.deepEqual(await profile.json(), { profile: "" });
|
|
80
|
+
|
|
81
|
+
const rules = await fetch(`${base}/rules`, { headers: auth });
|
|
82
|
+
assert.equal(rules.status, 200);
|
|
83
|
+
assert.deepEqual(await rules.json(), { rules: [] });
|
|
84
|
+
} finally {
|
|
85
|
+
close();
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
56
89
|
test("token is persisted into settings kv and reused across restarts", async () => {
|
|
57
90
|
const store = createStore(":memory:");
|
|
58
91
|
const service = createService({ store, mirror: null, config: {} });
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { createStore } from "../src/store.js";
|
|
4
|
+
|
|
5
|
+
// 回归(issue #202):检索路径的固定成本。此前 searchVector 每次调用
|
|
6
|
+
// SELECT * 全表 + 逐行 JSON.parse(993 × 512 维 ≈ 48ms,余弦本身只占 3%),
|
|
7
|
+
// all() 拖着 embedding 列(5k 行 ≈ 9.4MB)且 ORDER BY 无索引走临时 B 树。
|
|
8
|
+
// 本批修复:updated_at 索引入 schema;all() 显式列清单排除 embedding;
|
|
9
|
+
// searchVector 只取 id 列 + 解析缓存(setEmbedding 为单一失效点)+ Top-N 回表。
|
|
10
|
+
|
|
11
|
+
function make() {
|
|
12
|
+
const store = createStore(":memory:");
|
|
13
|
+
return store;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
test("#202: updated_at index is created on open (existing DBs included)", () => {
|
|
17
|
+
const store = make();
|
|
18
|
+
const row = store.db.prepare(
|
|
19
|
+
"SELECT name FROM sqlite_master WHERE type='index' AND name='idx_memories_updated'"
|
|
20
|
+
).get();
|
|
21
|
+
assert.ok(row, "idx_memories_updated exists after open");
|
|
22
|
+
store.close();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test("#202: all() returns full rows (minus embedding) ordered by updated_at desc", () => {
|
|
26
|
+
const store = make();
|
|
27
|
+
store.save({ type: "project", title: "旧", content: "c1", importance: 3 });
|
|
28
|
+
store.save({ type: "decision", title: "新", content: "c2", importance: 3 });
|
|
29
|
+
const rows = store.all();
|
|
30
|
+
assert.equal(rows.length, 2);
|
|
31
|
+
assert.equal(rows[0].title, "新", "most recently updated first");
|
|
32
|
+
for (const row of rows) {
|
|
33
|
+
assert.equal(row.content?.length > 0, true, "content present");
|
|
34
|
+
assert.equal("embedding" in row, false, "embedding never leaks into row objects");
|
|
35
|
+
}
|
|
36
|
+
store.close();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test("#202: searchVector cache invalidates on setEmbedding rewrite", () => {
|
|
40
|
+
const store = make();
|
|
41
|
+
const { id } = store.save({ type: "project", title: "A", content: "a" });
|
|
42
|
+
store.setEmbedding(id, [1, 0, 0]);
|
|
43
|
+
|
|
44
|
+
// 第一次检索:缓存 v1([1,0,0] 方向命中)
|
|
45
|
+
const hitV1 = store.searchVector([1, 0, 0], { limit: 5 });
|
|
46
|
+
assert.equal(hitV1[0]?.id, id, "v1 direction matches");
|
|
47
|
+
|
|
48
|
+
// 重写为正交向量后,旧方向必须不再命中、新方向命中——缓存失效点生效。
|
|
49
|
+
// (正交余弦=0,threshold=0 会连 0 分一起入选,所以用 0.5 的阈值区分。)
|
|
50
|
+
store.setEmbedding(id, [0, 1, 0]);
|
|
51
|
+
const hitStale = store.searchVector([1, 0, 0], { limit: 5, threshold: 0.5 });
|
|
52
|
+
assert.equal(hitStale.some((r) => r.id === id), false, "stale vector must not match after rewrite");
|
|
53
|
+
const hitV2 = store.searchVector([0, 1, 0], { limit: 5 });
|
|
54
|
+
assert.equal(hitV2[0]?.id, id, "new vector matches after rewrite");
|
|
55
|
+
store.close();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("#202: setEmbedding(null) drops the row from vector search", () => {
|
|
59
|
+
const store = make();
|
|
60
|
+
const { id } = store.save({ type: "project", title: "A", content: "a" });
|
|
61
|
+
store.setEmbedding(id, [1, 0, 0]);
|
|
62
|
+
assert.equal(store.searchVector([1, 0, 0], { limit: 5 }).length, 1);
|
|
63
|
+
store.setEmbedding(id, null);
|
|
64
|
+
assert.equal(store.searchVector([1, 0, 0], { limit: 5 }).length, 0, "cleared embedding leaves the search set");
|
|
65
|
+
store.close();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("#202: searchVector returns full memory rows with scores, sorted desc", () => {
|
|
69
|
+
const store = make();
|
|
70
|
+
const a = store.save({ type: "project", title: "标题A", content: "内容A", importance: 4 });
|
|
71
|
+
const b = store.save({ type: "decision", title: "标题B", content: "内容B", importance: 2 });
|
|
72
|
+
store.setEmbedding(a.id, [1, 0, 0]);
|
|
73
|
+
store.setEmbedding(b.id, [0.6, 0.8, 0]);
|
|
74
|
+
|
|
75
|
+
const hits = store.searchVector([1, 0, 0], { limit: 5 });
|
|
76
|
+
assert.equal(hits.length, 2);
|
|
77
|
+
assert.equal(hits[0].id, a.id, "closest vector first");
|
|
78
|
+
assert.ok(hits[0].score > hits[1].score, "descending scores");
|
|
79
|
+
for (const hit of hits) {
|
|
80
|
+
assert.ok(hit.title && hit.content, "full fields refetched for top hits");
|
|
81
|
+
assert.equal(typeof hit.score, "number");
|
|
82
|
+
assert.equal(hit.importance > 0, true, "non-score fields intact");
|
|
83
|
+
}
|
|
84
|
+
store.close();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("#202: getParsedEmbedding / getEmbeddings reflect rewrites (dedup path cache)", () => {
|
|
88
|
+
const store = make();
|
|
89
|
+
const { id } = store.save({ type: "project", title: "A", content: "a" });
|
|
90
|
+
store.setEmbedding(id, [1, 2, 3]);
|
|
91
|
+
assert.deepEqual(store.getParsedEmbedding(id), [1, 2, 3]);
|
|
92
|
+
assert.deepEqual(store.getEmbeddings([id]).get(id), [1, 2, 3]);
|
|
93
|
+
|
|
94
|
+
store.setEmbedding(id, [4, 5, 6]);
|
|
95
|
+
assert.deepEqual(store.getParsedEmbedding(id), [4, 5, 6], "cache-first read sees the rewrite");
|
|
96
|
+
assert.deepEqual(store.getEmbeddings([id]).get(id), [4, 5, 6]);
|
|
97
|
+
store.close();
|
|
98
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// #184 回归:memory_get 的内联输出 schema 漏声明 v0.8.1 的 scope 来源三键
|
|
2
|
+
// (agent_scope_source / workspace_scope_source / scope_decided_at),且
|
|
3
|
+
// additionalProperties:false → toApiList 对被标注行条件展开这些键后,宿主
|
|
4
|
+
// in-process 校验直接拒收("invalid output"),表现为「个别 id 必失败、多数
|
|
5
|
+
// 正常」。本文件用生产同款校验器(@deepseek-ai/dsh-tools 的
|
|
6
|
+
// validateJsonSchemaValue)复现并防漂移;修复 = memory_get 复用与
|
|
7
|
+
// list/search 共享的 MEMORY_ITEM_SCHEMA,不再维护内联副本。
|
|
8
|
+
import test from "node:test";
|
|
9
|
+
import assert from "node:assert/strict";
|
|
10
|
+
import { validateJsonSchemaValue } from "@deepseek-ai/dsh-tools";
|
|
11
|
+
import { createStore } from "../src/store.js";
|
|
12
|
+
import { createService } from "../src/service.js";
|
|
13
|
+
import { createTools, MEMORY_ITEM_SCHEMA } from "../src/tools.js";
|
|
14
|
+
|
|
15
|
+
function setup() {
|
|
16
|
+
const store = createStore(":memory:");
|
|
17
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
18
|
+
const registered = [];
|
|
19
|
+
const ctx = {
|
|
20
|
+
tools: {
|
|
21
|
+
register(def) {
|
|
22
|
+
registered.push(def);
|
|
23
|
+
return () => {};
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
createTools(ctx, service, {}, undefined);
|
|
28
|
+
const byName = Object.fromEntries(registered.map((def) => [def.name, def]));
|
|
29
|
+
return { store, service, byName };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// memory_get.execute 的返回 = { memory: toApiList([getById(id)])[0] }——契约
|
|
33
|
+
// 测试直接对「toApiList 产出的 DTO ↔ 声明的 output schema」做校验(DTO 键的
|
|
34
|
+
// 唯一来源就是 toApiList,execute 只是包装)。
|
|
35
|
+
test("memory_get output validates for fully scope-annotated rows (#184)", () => {
|
|
36
|
+
const { store, service, byName } = setup();
|
|
37
|
+
const saved = store.save({
|
|
38
|
+
type: "preference",
|
|
39
|
+
title: "annotated row",
|
|
40
|
+
content: "带全量 scope 来源标注的记忆行",
|
|
41
|
+
source: "test",
|
|
42
|
+
agent_scope: "coder",
|
|
43
|
+
agent_scope_source: "auto",
|
|
44
|
+
workspace_scope: "proj-alpha",
|
|
45
|
+
workspace_scope_source: "auto",
|
|
46
|
+
scope_decided_at: "2026-09-15T00:00:00Z"
|
|
47
|
+
});
|
|
48
|
+
const get = byName.memory_get;
|
|
49
|
+
const dto = service.toApiList([store.getById(saved.id)])[0];
|
|
50
|
+
const res = { memory: dto };
|
|
51
|
+
assert.deepEqual(validateJsonSchemaValue(get.output.schema, res), []);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("memory_get item schema reuses MEMORY_ITEM_SCHEMA (no inline copy) (#184)", () => {
|
|
55
|
+
const { byName } = setup();
|
|
56
|
+
const get = byName.memory_get;
|
|
57
|
+
// defineTool 编译会克隆 schema,恒等不成立——断言键集与严格性同源。
|
|
58
|
+
const compiled = get.output.schema.properties.memory;
|
|
59
|
+
assert.deepEqual(
|
|
60
|
+
Object.keys(compiled.properties).sort(),
|
|
61
|
+
Object.keys(MEMORY_ITEM_SCHEMA.properties).sort()
|
|
62
|
+
);
|
|
63
|
+
assert.equal(compiled.additionalProperties, false);
|
|
64
|
+
assert.deepEqual(compiled.required.sort(), ["content", "created_at", "id", "importance", "title", "type", "updated_at"]);
|
|
65
|
+
});
|