@modusensus/dsh-mneme 0.8.0 → 0.8.2
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 +15 -5
- package/lib/api-standalone.js +12 -0
- package/lib/api.js +52 -14
- package/lib/client.js +130 -8
- package/lib/config.js +8 -0
- package/lib/dream/decisions.js +55 -8
- package/lib/dream/sleep.js +79 -17
- package/lib/dream.js +61 -9
- package/lib/entities/extractor.js +2 -2
- package/lib/index.js +6 -0
- package/lib/lang.js +13 -2
- package/lib/local-embedder.js +9 -1
- package/lib/reranker.js +38 -8
- package/lib/scope.js +39 -4
- package/lib/service.js +121 -18
- package/lib/store.js +243 -40
- package/lib/summarize.js +2 -2
- package/lib/tools.js +64 -30
- package/lib/vector-index.js +2 -8
- package/lib/version-check.js +122 -0
- package/package.json +2 -1
- package/scripts/sync-test-badge.mjs +61 -0
- package/src/api-standalone.js +12 -0
- package/src/api.js +52 -14
- package/src/config.js +8 -0
- package/src/dream/decisions.js +55 -8
- package/src/dream/sleep.js +79 -17
- package/src/dream.js +61 -9
- package/src/entities/extractor.js +2 -2
- package/src/index.js +6 -0
- package/src/lang.js +13 -2
- package/src/local-embedder.js +9 -1
- package/src/reranker.js +38 -8
- package/src/scope.js +39 -4
- package/src/service.js +121 -18
- package/src/store.js +243 -40
- package/src/summarize.js +2 -2
- package/src/tools.js +64 -30
- package/src/vector-index.js +2 -8
- package/src/version-check.js +122 -0
- package/test/api.test.js +5 -0
- package/test/conflict-freeze.test.js +15 -5
- 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/llm-message-source.test.js +177 -0
- package/test/reranker-single-logit.test.js +73 -0
- package/test/scope-candidates.test.js +239 -0
- package/test/scope-explicit.test.js +452 -0
- package/test/scope-storage.test.js +13 -5
- package/test/scope-strict.test.js +107 -41
- 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
- package/test/version-check.test.js +152 -0
|
@@ -4,9 +4,13 @@ import { createStore } from "../src/store.js";
|
|
|
4
4
|
import { createService } from "../src/service.js";
|
|
5
5
|
import { createTools } from "../src/tools.js";
|
|
6
6
|
|
|
7
|
-
// v0.8.0 A3(issue #17
|
|
8
|
-
//
|
|
9
|
-
//
|
|
7
|
+
// v0.8.0 A3(issue #17)→ v0.8.1 第 3 步(issue #170 4.3,作者已确认):
|
|
8
|
+
// strictScope 硬过滤只认显式声明。可见性公式收窄为:记忆可见 ⇔ 对每一维
|
|
9
|
+
// (该维标注存在且来源=explicit 时:命中当前会话)AND。
|
|
10
|
+
// - explicit 行为硬墙:他者维度不可见(含 memory_get 无存在性泄漏);
|
|
11
|
+
// - auto / 存量 NULL 来源(v0.8.0 自动标注)不进硬过滤,只吃 A2 软加权
|
|
12
|
+
// (foreign ×0.5 保留可见);
|
|
13
|
+
// - 当前会话维度解析不到 → 该维 explicit 行一律不可见(fail-closed 收窄)。
|
|
10
14
|
|
|
11
15
|
function setup(config) {
|
|
12
16
|
const store = createStore(":memory:");
|
|
@@ -14,46 +18,69 @@ function setup(config) {
|
|
|
14
18
|
return { store, service };
|
|
15
19
|
}
|
|
16
20
|
|
|
17
|
-
test("visibility predicate: four-quadrant semantics", () => {
|
|
21
|
+
test("visibility predicate: explicit rows keep four-quadrant semantics", () => {
|
|
18
22
|
const { service } = setup({ scopeEnabled: true, strictScope: true });
|
|
19
23
|
const vis = service.isVisibleInScope;
|
|
20
24
|
const me = { agent_scope: "coder", workspace_scope: "D:\\p" };
|
|
25
|
+
const E = "explicit";
|
|
21
26
|
|
|
22
27
|
// 全局(未标注)对谁都可见。
|
|
23
28
|
assert.equal(vis({}, me), true);
|
|
24
29
|
// agent 专属:同 agent 任意 workspace 可见,他人不可见。
|
|
25
|
-
assert.equal(vis({ agent_scope: "coder" }, me), true);
|
|
26
|
-
assert.equal(vis({ agent_scope: "novelist" }, me), false);
|
|
30
|
+
assert.equal(vis({ agent_scope: "coder", agent_scope_source: E }, me), true);
|
|
31
|
+
assert.equal(vis({ agent_scope: "novelist", agent_scope_source: E }, me), false);
|
|
27
32
|
// workspace 专属:同 workspace 任意 agent 可见,别的 workspace 不可见。
|
|
28
|
-
assert.equal(vis({ workspace_scope: "D:\\p" }, me), true);
|
|
29
|
-
assert.equal(vis({ workspace_scope: "D:\\q" }, me), false);
|
|
33
|
+
assert.equal(vis({ workspace_scope: "D:\\p", workspace_scope_source: E }, me), true);
|
|
34
|
+
assert.equal(vis({ workspace_scope: "D:\\q", workspace_scope_source: E }, me), false);
|
|
30
35
|
// 双标注:精确命中才可见。
|
|
31
|
-
assert.equal(vis({ agent_scope: "coder", workspace_scope: "D:\\p" }, me), true);
|
|
32
|
-
assert.equal(vis({ agent_scope: "coder", workspace_scope: "D:\\q" }, me), false);
|
|
33
|
-
assert.equal(vis({ agent_scope: "novelist", workspace_scope: "D:\\p" }, me), false);
|
|
36
|
+
assert.equal(vis({ agent_scope: "coder", agent_scope_source: E, workspace_scope: "D:\\p", workspace_scope_source: E }, me), true);
|
|
37
|
+
assert.equal(vis({ agent_scope: "coder", agent_scope_source: E, workspace_scope: "D:\\q", workspace_scope_source: E }, me), false);
|
|
38
|
+
assert.equal(vis({ agent_scope: "novelist", agent_scope_source: E, workspace_scope: "D:\\p", workspace_scope_source: E }, me), false);
|
|
34
39
|
});
|
|
35
40
|
|
|
36
|
-
test("visibility predicate:
|
|
41
|
+
test("visibility predicate: auto and legacy rows are NOT hard walls (soft only)", () => {
|
|
42
|
+
const { service } = setup({ scopeEnabled: true, strictScope: true });
|
|
43
|
+
const vis = service.isVisibleInScope;
|
|
44
|
+
const me = { agent_scope: "coder", workspace_scope: "D:\\p" };
|
|
45
|
+
|
|
46
|
+
// auto(v0.8.1 载体自动标注)与存量 NULL 来源(v0.8.0 自动标注)他者维度
|
|
47
|
+
// 也可见——硬过滤只认 explicit。
|
|
48
|
+
assert.equal(vis({ agent_scope: "novelist" }, me), true, "legacy NULL source");
|
|
49
|
+
assert.equal(vis({ agent_scope: "novelist", agent_scope_source: "auto" }, me), true);
|
|
50
|
+
assert.equal(vis({ workspace_scope: "D:\\q", workspace_scope_source: "auto" }, me), true);
|
|
51
|
+
// 混合:agent 维 auto + workspace 维 explicit → 只 workspace 维构成硬墙。
|
|
52
|
+
assert.equal(vis({ agent_scope: "novelist", agent_scope_source: "auto", workspace_scope: "D:\\q", workspace_scope_source: "explicit" }, me), false);
|
|
53
|
+
assert.equal(vis({ agent_scope: "novelist", agent_scope_source: "explicit", workspace_scope: "D:\\q", workspace_scope_source: "auto" }, me), false);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("visibility predicate: anonymous session fail-closes only explicit rows", () => {
|
|
37
57
|
const { service } = setup({ scopeEnabled: true, strictScope: true });
|
|
38
58
|
const vis = service.isVisibleInScope;
|
|
39
59
|
const anon = { agent_scope: null, workspace_scope: null };
|
|
40
60
|
assert.equal(vis({}, anon), true);
|
|
41
|
-
assert.equal(vis({ agent_scope: "coder" }, anon), false);
|
|
42
|
-
assert.equal(vis({ workspace_scope: "D:\\p" }, anon), false);
|
|
61
|
+
assert.equal(vis({ agent_scope: "coder", agent_scope_source: "explicit" }, anon), false);
|
|
62
|
+
assert.equal(vis({ workspace_scope: "D:\\p", workspace_scope_source: "explicit" }, anon), false);
|
|
63
|
+
// auto/存量行照常可见(不冒认,但也不因身份缺失误杀软标注)。
|
|
64
|
+
assert.equal(vis({ agent_scope: "coder" }, anon), true);
|
|
65
|
+
assert.equal(vis({ agent_scope: "coder", agent_scope_source: "auto" }, anon), true);
|
|
43
66
|
});
|
|
44
67
|
|
|
45
|
-
test("searchMemories
|
|
68
|
+
test("searchMemories: explicit foreign filtered, auto/legacy foreign stay visible under strictScope", async () => {
|
|
46
69
|
const { store, service } = setup({ scopeEnabled: true, strictScope: true });
|
|
47
70
|
store.save({ type: "project", title: "global needle", content: "x" });
|
|
48
|
-
store.save({ type: "project", title: "mine needle", content: "x", agent_scope: "me" });
|
|
49
|
-
store.save({ type: "project", title: "foreign needle", content: "x", agent_scope: "other" });
|
|
71
|
+
store.save({ type: "project", title: "mine needle", content: "x", agent_scope: "me", agent_scope_source: "explicit" });
|
|
72
|
+
store.save({ type: "project", title: "explicit foreign needle", content: "x", agent_scope: "other", agent_scope_source: "explicit" });
|
|
73
|
+
store.save({ type: "project", title: "auto foreign needle", content: "x", agent_scope: "other", agent_scope_source: "auto" });
|
|
74
|
+
store.save({ type: "project", title: "legacy foreign needle", content: "x", agent_scope: "other" });
|
|
50
75
|
|
|
51
76
|
const rows = await service.searchMemories("needle", {
|
|
52
77
|
mode: "keyword",
|
|
53
78
|
scope: { agent_scope: "me", workspace_scope: null }
|
|
54
79
|
});
|
|
55
|
-
//
|
|
56
|
-
assert.deepEqual(rows.map((m) => m.title).sort(), [
|
|
80
|
+
// explicit 他者出局;auto/存量他者保留(A2 降权仍可见);未标注与命中行保留。
|
|
81
|
+
assert.deepEqual(rows.map((m) => m.title).sort(), [
|
|
82
|
+
"auto foreign needle", "global needle", "legacy foreign needle", "mine needle"
|
|
83
|
+
]);
|
|
57
84
|
});
|
|
58
85
|
|
|
59
86
|
test("strictScope off keeps A2 behavior: foreign rows demoted but visible", async () => {
|
|
@@ -66,38 +93,73 @@ test("strictScope off keeps A2 behavior: foreign rows demoted but visible", asyn
|
|
|
66
93
|
assert.deepEqual(rows.map((m) => m.title), ["foreign needle"]);
|
|
67
94
|
});
|
|
68
95
|
|
|
69
|
-
test("store.list/count visibility
|
|
96
|
+
test("store.list/count visibility: explicit hard wall + auto/legacy pass, consistent totals", () => {
|
|
70
97
|
const { store } = setup({ scopeEnabled: true, strictScope: true });
|
|
71
98
|
store.save({ type: "decision", title: "g", content: "x" });
|
|
72
|
-
store.save({ type: "decision", title: "mine", content: "x", agent_scope: "me", workspace_scope: "D:\\p" });
|
|
73
|
-
store.save({ type: "decision", title: "theirs", content: "x", agent_scope: "other" });
|
|
74
|
-
store.save({ type: "decision", title: "
|
|
99
|
+
store.save({ type: "decision", title: "mine", content: "x", agent_scope: "me", agent_scope_source: "explicit", workspace_scope: "D:\\p", workspace_scope_source: "explicit" });
|
|
100
|
+
store.save({ type: "decision", title: "theirs-explicit", content: "x", agent_scope: "other", agent_scope_source: "explicit" });
|
|
101
|
+
store.save({ type: "decision", title: "theirs-auto", content: "x", agent_scope: "other", agent_scope_source: "auto" });
|
|
102
|
+
store.save({ type: "decision", title: "theirs-legacy", content: "x", agent_scope: "other" });
|
|
103
|
+
store.save({ type: "decision", title: "other-ws-explicit", content: "x", workspace_scope: "D:\\q", workspace_scope_source: "explicit" });
|
|
75
104
|
|
|
76
105
|
const vis = { agentScope: "me", workspaceScope: "D:\\p" };
|
|
77
106
|
const titles = store.list({ visibility: vis }).map((m) => m.title).sort();
|
|
78
|
-
assert.deepEqual(titles, ["g", "mine"]);
|
|
79
|
-
assert.equal(store.count("decision", { visibility: vis }),
|
|
107
|
+
assert.deepEqual(titles, ["g", "mine", "theirs-auto", "theirs-legacy"]);
|
|
108
|
+
assert.equal(store.count("decision", { visibility: vis }), 4);
|
|
80
109
|
|
|
81
|
-
//
|
|
82
|
-
const anonTitles = store.list({ visibility: { agentScope: null, workspaceScope: null } }).map((m) => m.title);
|
|
83
|
-
assert.deepEqual(anonTitles, ["g"]);
|
|
110
|
+
// 匿名会话:explicit 行全出局,auto/存量/未标注放行。
|
|
111
|
+
const anonTitles = store.list({ visibility: { agentScope: null, workspaceScope: null } }).map((m) => m.title).sort();
|
|
112
|
+
assert.deepEqual(anonTitles, ["g", "theirs-auto", "theirs-legacy"]);
|
|
113
|
+
assert.equal(store.count("decision", { visibility: { agentScope: null, workspaceScope: null } }), 3);
|
|
84
114
|
});
|
|
85
115
|
|
|
86
|
-
test("injectCandidates hard-filters
|
|
116
|
+
test("injectCandidates hard-filters explicit foreign rows when strictScope is on", () => {
|
|
87
117
|
const { store, service } = setup({ scopeEnabled: true, strictScope: true });
|
|
88
118
|
store.save({ type: "decision", title: "global", content: "x", importance: 4 });
|
|
89
|
-
store.save({ type: "decision", title: "foreign", content: "x", importance: 4, agent_scope: "other" });
|
|
119
|
+
store.save({ type: "decision", title: "foreign", content: "x", importance: 4, agent_scope: "other", agent_scope_source: "explicit" });
|
|
120
|
+
store.save({ type: "decision", title: "auto-foreign", content: "x", importance: 4, agent_scope: "other", agent_scope_source: "auto" });
|
|
90
121
|
|
|
91
122
|
const injected = service.injectCandidates({
|
|
92
123
|
maxItems: 5,
|
|
93
124
|
threshold: 3,
|
|
94
125
|
scope: { agent_scope: "me", workspace_scope: null }
|
|
95
126
|
});
|
|
96
|
-
assert.deepEqual(injected.map((m) => m.title), ["global"]);
|
|
127
|
+
assert.deepEqual(injected.map((m) => m.title).sort(), ["auto-foreign", "global"]);
|
|
97
128
|
|
|
98
129
|
// flag 下(scope 不传)→ 他 scope 照常注入(A2 前行为)。
|
|
99
130
|
const unscoped = service.injectCandidates({ maxItems: 5, threshold: 3 });
|
|
100
|
-
assert.deepEqual(unscoped.map((m) => m.title).sort(), ["foreign", "global"]);
|
|
131
|
+
assert.deepEqual(unscoped.map((m) => m.title).sort(), ["auto-foreign", "foreign", "global"]);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("memory_update/memory_delete hide explicit foreign-scope rows under strictScope (review item 4, aligned with memory_get)", async () => {
|
|
135
|
+
const { store, pick } = setupTools({ scopeEnabled: true, strictScope: true });
|
|
136
|
+
const theirs = store.save({ type: "pitfall", title: "theirs", content: "c", agent_scope: "other", agent_scope_source: "explicit" });
|
|
137
|
+
const autoTheirs = store.save({ type: "pitfall", title: "auto-theirs", content: "c", agent_scope: "other", agent_scope_source: "auto" });
|
|
138
|
+
const update = pick("memory_update");
|
|
139
|
+
const del = pick("memory_delete");
|
|
140
|
+
|
|
141
|
+
// explicit 他 scope:update 按不存在拒绝、delete 视作不存在——不能凭 id 直改直删。
|
|
142
|
+
await assert.rejects(() => runHandler(update, { id: theirs.id, content: "tampered" }, STRICT_EXEC), /memory not found/);
|
|
143
|
+
assert.equal(store.getById(theirs.id).content, "c", "row untouched by the rejected update");
|
|
144
|
+
const delOut = await runHandler(del, { id: theirs.id }, STRICT_EXEC);
|
|
145
|
+
assert.equal(delOut.deleted, false, "delete treats invisible rows as absent (no existence leak)");
|
|
146
|
+
assert.ok(store.getById(theirs.id), "row NOT deleted");
|
|
147
|
+
|
|
148
|
+
// auto 他 scope:不构成硬墙,照常可改(与 get 的可见性口径一致)。
|
|
149
|
+
const updAuto = await runHandler(update, { id: autoTheirs.id, content: "updated" }, STRICT_EXEC);
|
|
150
|
+
assert.equal(updAuto.memory.id, autoTheirs.id);
|
|
151
|
+
|
|
152
|
+
// 行主人自己照常可改。
|
|
153
|
+
const ownerOut = await runHandler(update, { id: theirs.id, content: "owner edit" }, OTHER_EXEC);
|
|
154
|
+
assert.equal(ownerOut.memory.id, theirs.id);
|
|
155
|
+
|
|
156
|
+
// strictScope 关:update/delete 不做可见性校验(管理语义回归)。
|
|
157
|
+
const { store: store2, pick: pick2 } = setupTools({ scopeEnabled: true });
|
|
158
|
+
const t2 = store2.save({ type: "pitfall", title: "theirs", content: "c", agent_scope: "other", agent_scope_source: "explicit" });
|
|
159
|
+
await runHandler(pick2("memory_update"), { id: t2.id, content: "edited" }, STRICT_EXEC);
|
|
160
|
+
assert.equal(store2.getById(t2.id).content, "edited");
|
|
161
|
+
const del2 = await runHandler(pick2("memory_delete"), { id: t2.id }, STRICT_EXEC);
|
|
162
|
+
assert.equal(del2.deleted, true);
|
|
101
163
|
});
|
|
102
164
|
|
|
103
165
|
function setupTools(config) {
|
|
@@ -121,17 +183,21 @@ async function runHandler(def, args, exec) {
|
|
|
121
183
|
const STRICT_EXEC = { agent: { session: { id: "s1", header: { agentPreset: "me", cwd: "D:\\p" } } } };
|
|
122
184
|
const OTHER_EXEC = { agent: { session: { id: "s2", header: { agentPreset: "other", cwd: "D:\\q" } } } };
|
|
123
185
|
|
|
124
|
-
test("memory_get hides foreign-scope rows under strictScope, resolvable for the owner", async () => {
|
|
186
|
+
test("memory_get hides explicit foreign-scope rows under strictScope, resolvable for the owner", async () => {
|
|
125
187
|
const { store, pick } = setupTools({ scopeEnabled: true, strictScope: true });
|
|
126
|
-
const mine = store.save({ type: "pitfall", title: "mine", content: "c", agent_scope: "me" });
|
|
127
|
-
const theirs = store.save({ type: "pitfall", title: "theirs", content: "c", agent_scope: "other" });
|
|
188
|
+
const mine = store.save({ type: "pitfall", title: "mine", content: "c", agent_scope: "me", agent_scope_source: "explicit" });
|
|
189
|
+
const theirs = store.save({ type: "pitfall", title: "theirs", content: "c", agent_scope: "other", agent_scope_source: "explicit" });
|
|
190
|
+
const autoTheirs = store.save({ type: "pitfall", title: "auto-theirs", content: "c", agent_scope: "other", agent_scope_source: "auto" });
|
|
128
191
|
const get = pick("memory_get");
|
|
129
192
|
|
|
130
193
|
const out = await runHandler(get, { id: mine.id }, STRICT_EXEC);
|
|
131
194
|
assert.equal(out.memory.title, "mine");
|
|
132
195
|
|
|
133
|
-
// 他 scope → 按不存在处理(无存在性泄漏)。
|
|
196
|
+
// explicit 他 scope → 按不存在处理(无存在性泄漏)。
|
|
134
197
|
await assert.rejects(() => runHandler(get, { id: theirs.id }, STRICT_EXEC), /memory not found/);
|
|
198
|
+
// auto 他 scope → 可见(软加权不进硬过滤)。
|
|
199
|
+
const autoOut = await runHandler(get, { id: autoTheirs.id }, STRICT_EXEC);
|
|
200
|
+
assert.equal(autoOut.memory.title, "auto-theirs");
|
|
135
201
|
// 行主人自己照常可取。
|
|
136
202
|
const ownerOut = await runHandler(get, { id: theirs.id }, OTHER_EXEC);
|
|
137
203
|
assert.equal(ownerOut.memory.title, "theirs");
|
|
@@ -139,17 +205,17 @@ test("memory_get hides foreign-scope rows under strictScope, resolvable for the
|
|
|
139
205
|
|
|
140
206
|
test("memory_get without strictScope returns any row regardless of scope annotation", async () => {
|
|
141
207
|
const { store, pick } = setupTools({ scopeEnabled: true });
|
|
142
|
-
const theirs = store.save({ type: "pitfall", title: "theirs", content: "c", agent_scope: "other" });
|
|
208
|
+
const theirs = store.save({ type: "pitfall", title: "theirs", content: "c", agent_scope: "other", agent_scope_source: "explicit" });
|
|
143
209
|
const get = pick("memory_get");
|
|
144
210
|
const out = await runHandler(get, { id: theirs.id }, STRICT_EXEC);
|
|
145
211
|
assert.equal(out.memory.title, "theirs");
|
|
146
212
|
});
|
|
147
213
|
|
|
148
|
-
test("memory_list filters rows and keeps total consistent under strictScope", async () => {
|
|
214
|
+
test("memory_list filters explicit rows and keeps total consistent under strictScope", async () => {
|
|
149
215
|
const { store, pick } = setupTools({ scopeEnabled: true, strictScope: true });
|
|
150
216
|
store.save({ type: "decision", title: "g", content: "x" });
|
|
151
|
-
store.save({ type: "decision", title: "mine", content: "x", agent_scope: "me" });
|
|
152
|
-
store.save({ type: "decision", title: "theirs", content: "x", agent_scope: "other" });
|
|
217
|
+
store.save({ type: "decision", title: "mine", content: "x", agent_scope: "me", agent_scope_source: "explicit" });
|
|
218
|
+
store.save({ type: "decision", title: "theirs", content: "x", agent_scope: "other", agent_scope_source: "explicit" });
|
|
153
219
|
const list = pick("memory_list");
|
|
154
220
|
|
|
155
221
|
const out = await runHandler(list, { type: "decision" }, STRICT_EXEC);
|
|
@@ -158,7 +224,7 @@ test("memory_list filters rows and keeps total consistent under strictScope", as
|
|
|
158
224
|
|
|
159
225
|
// flag 关(A2 默认):他 scope 的行照常出现在浏览视图。
|
|
160
226
|
const { store: store2, pick: pick2 } = setupTools({ scopeEnabled: true });
|
|
161
|
-
store2.save({ type: "decision", title: "theirs", content: "x", agent_scope: "other" });
|
|
227
|
+
store2.save({ type: "decision", title: "theirs", content: "x", agent_scope: "other", agent_scope_source: "explicit" });
|
|
162
228
|
const out2 = await runHandler(pick2("memory_list"), { type: "decision" }, STRICT_EXEC);
|
|
163
229
|
assert.deepEqual(out2.items.map((m) => m.title), ["theirs"]);
|
|
164
230
|
assert.equal(out2.total, 1);
|
|
@@ -151,17 +151,19 @@ test("validateDecisions skipInvalid: conflict without a distinct winner/loser is
|
|
|
151
151
|
|
|
152
152
|
// ------------------------------------------------------- skipInvalid 与全局闸门
|
|
153
153
|
|
|
154
|
-
test("validateDecisions skipInvalid: valid subset below the coverage floor
|
|
154
|
+
test("validateDecisions skipInvalid: valid subset below the coverage floor degrades (Issue #104 方向 1)", () => {
|
|
155
155
|
const snap = makeSnap(["p", "j", "x"]);
|
|
156
156
|
const decisions = [
|
|
157
157
|
{ action: "archive", ids: ["p"], reason: "stale" },
|
|
158
158
|
{ action: "merge", ids: ["p", "j"], keepSource: "p", title: "跨类型", content: "m", importance: 4 }
|
|
159
159
|
];
|
|
160
|
-
const { ok, errors, skipped } = validateDecisions(decisions, snap, { skipInvalid: true });
|
|
161
|
-
assert.equal(ok,
|
|
160
|
+
const { ok, errors, skipped, coverageShortfall } = validateDecisions(decisions, snap, { skipInvalid: true });
|
|
161
|
+
assert.equal(ok, true, "valid subset applies — shortfall degrades instead of rejecting");
|
|
162
|
+
assert.deepEqual(errors, []);
|
|
162
163
|
assert.equal(skipped.length, 1);
|
|
163
|
-
assert.
|
|
164
|
-
|
|
164
|
+
assert.match(String(coverageShortfall), /coverage/, "shortfall reason surfaced for audit");
|
|
165
|
+
// claimed {p} = 1/3 = 33% < 50%;j/x 隐式 keep 补齐
|
|
166
|
+
assert.deepEqual(decisions.map((d) => d.action), ["archive", "keep", "keep"]);
|
|
165
167
|
});
|
|
166
168
|
|
|
167
169
|
test("validateDecisions skipInvalid: updates skipped for other reasons do not count toward the update cap", () => {
|
|
@@ -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
|
+
});
|