@modusensus/dsh-mneme 0.7.32 → 0.8.0
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 +42 -20
- package/lib/api.js +60 -0
- package/lib/client.js +152 -2
- package/lib/commands.js +30 -3
- package/lib/config.js +21 -0
- package/lib/inject.js +39 -5
- package/lib/lang.js +12 -0
- package/lib/scope.js +83 -0
- package/lib/service.js +287 -62
- package/lib/settings.js +7 -0
- package/lib/store.js +100 -5
- package/lib/tools.js +86 -12
- package/package.json +1 -1
- package/src/api.js +60 -0
- package/src/commands.js +30 -3
- package/src/config.js +21 -0
- package/src/inject.js +39 -5
- package/src/lang.js +12 -0
- package/src/scope.js +83 -0
- package/src/service.js +287 -62
- package/src/settings.js +7 -0
- package/src/store.js +100 -5
- package/src/tools.js +86 -12
- package/test/api.test.js +90 -4
- package/test/client.test.js +58 -0
- package/test/commands.test.js +40 -3
- package/test/inject.test.js +93 -0
- package/test/scope-retrieval.test.js +197 -0
- package/test/scope-storage.test.js +247 -0
- package/test/scope-strict.test.js +165 -0
- package/test/service.test.js +76 -0
package/test/inject.test.js
CHANGED
|
@@ -154,3 +154,96 @@ test("Issue #129: 扁平的 data.content 形状仍被兼容", () => {
|
|
|
154
154
|
assert.ok(text.includes("问题"), "query present");
|
|
155
155
|
assert.ok(text.includes("扁平形状的回答"), "flat content shape still supported");
|
|
156
156
|
});
|
|
157
|
+
|
|
158
|
+
// ---- issue #162: 注入边界花括号转义(v0.7.4 #40 修复被 v0.7.11 误删后恢复) ----
|
|
159
|
+
|
|
160
|
+
test("issue #162: {{...}} in memory content is escaped at the injection boundary", () => {
|
|
161
|
+
const { contexts, service } = setup();
|
|
162
|
+
service.saveWithDedupe({ type: "preference", title: "模板记忆", content: "{{hl|highlight}} 和 {{挖空}} {{关键词}}", importance: 5 });
|
|
163
|
+
const text = contexts[0].text({});
|
|
164
|
+
assert.ok(text.includes("模板记忆"), "memory still rendered by title");
|
|
165
|
+
assert.ok(text.includes("{\\{hl|highlight}\\}"), "ASCII template braces escaped");
|
|
166
|
+
assert.ok(text.includes("{\\{挖空}\\}"), "CJK template braces escaped");
|
|
167
|
+
assert.ok(!text.includes("{{"), "no raw {{ survives into the prompt");
|
|
168
|
+
assert.ok(!text.includes("}}"), "no raw }} survives into the prompt");
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test("issue #162: odd brace runs like {{{a}}} are escaped too", () => {
|
|
172
|
+
const { contexts, service } = setup();
|
|
173
|
+
service.saveWithDedupe({ type: "preference", title: "三层模板", content: "{{{a}}} 和 }}} 结尾", importance: 5 });
|
|
174
|
+
const text = contexts[0].text({});
|
|
175
|
+
assert.ok(!text.includes("{{"), "no raw {{ for odd brace runs");
|
|
176
|
+
assert.ok(!text.includes("}}"), "no raw }} for odd brace runs");
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test("issue #162: {{...}} in user profile/rules is escaped", () => {
|
|
180
|
+
const { contexts, settings } = setup();
|
|
181
|
+
settings.setProfile("我叫{{名字}},前端开发者");
|
|
182
|
+
settings.setRules(["回答时用 {{hl|term}}", "以 }} 结尾的规则"]);
|
|
183
|
+
const settingsCtx = contexts.find((c) => c.name === "user-settings");
|
|
184
|
+
const text = settingsCtx.text({});
|
|
185
|
+
assert.ok(text.includes("{\\{名字}\\}"), "profile braces escaped");
|
|
186
|
+
assert.ok(text.includes("{\\{hl|term}\\}"), "rule braces escaped");
|
|
187
|
+
assert.ok(!text.includes("{{"), "no raw {{ in user-settings block");
|
|
188
|
+
assert.ok(!text.includes("}}"), "no raw }} in user-settings block");
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test("issue #162: hot-context rounds with {{...}} are escaped", () => {
|
|
192
|
+
const { contexts } = setup();
|
|
193
|
+
const text = contexts[0].text({
|
|
194
|
+
agent: {
|
|
195
|
+
session: {
|
|
196
|
+
id: "s1",
|
|
197
|
+
events: [
|
|
198
|
+
{ type: "user/message", data: { source: { kind: "user" }, content: ["用 {{hl|你好}} 测试"] } },
|
|
199
|
+
{ type: "assistant/message", data: { source: { kind: "assistant" }, content: ["返回 {{答案}}"] } }
|
|
200
|
+
]
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
assert.ok(text.includes("[短期上下文]"), "hot context rendered");
|
|
205
|
+
assert.ok(!text.includes("{{"), "no raw {{ in hot context");
|
|
206
|
+
assert.ok(!text.includes("}}"), "no raw }} in hot context");
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test("issue #162: reasoning parts are excluded from hot memory (docker --format case)", () => {
|
|
210
|
+
const { contexts } = setup();
|
|
211
|
+
// issue #162 的触发源:assistant 的 reasoning part 是 { type: "reasoning", text }
|
|
212
|
+
// 结构,内容里出现 docker --format "{{.Server.Version}}" 时,若被注入 hot memory
|
|
213
|
+
// 会让 DSH interpolate() 扫到非法变量名 throw → 整轮永久失败。textOf 必须跳过它。
|
|
214
|
+
const text = contexts[0].text({
|
|
215
|
+
agent: {
|
|
216
|
+
session: {
|
|
217
|
+
id: "s1",
|
|
218
|
+
events: [
|
|
219
|
+
{ type: "user/message", data: { source: { kind: "user" }, content: ["检查 docker 版本"] } },
|
|
220
|
+
{
|
|
221
|
+
type: "assistant/message",
|
|
222
|
+
data: {
|
|
223
|
+
source: { kind: "assistant" },
|
|
224
|
+
content: [
|
|
225
|
+
{ type: "reasoning", text: "运行 docker version --format \"{{.Server.Version}}\" 查看版本" },
|
|
226
|
+
{ type: "text", text: "已检查,Docker 版本 27.1.1。" }
|
|
227
|
+
]
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
]
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
// reasoning 不应进入 hot memory
|
|
235
|
+
assert.ok(!text.includes("Server.Version"), "reasoning text excluded from hot memory");
|
|
236
|
+
// 正文保留
|
|
237
|
+
assert.ok(text.includes("27.1.1"), "assistant text part still injected");
|
|
238
|
+
// 整个注入块没有任何原始 {{ (双保险:即使将来 reasoning 被引入,也会被转义)
|
|
239
|
+
assert.ok(!text.includes("{{"), "no raw {{ anywhere in injected context");
|
|
240
|
+
assert.ok(!text.includes("}}"), "no raw }} anywhere in injected context");
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
test("issue #162: escapePromptVariables=false passes {{...}} through verbatim", () => {
|
|
244
|
+
const { contexts, service } = setup({ escapePromptVariables: false });
|
|
245
|
+
service.saveWithDedupe({ type: "preference", title: "模板", content: "{{挖空}} 与 {{hl|term}}", importance: 5 });
|
|
246
|
+
const text = contexts[0].text({});
|
|
247
|
+
assert.ok(text.includes("{{挖空}}"), "raw braces preserved when escaping disabled");
|
|
248
|
+
assert.ok(text.includes("{{hl|term}}"), "raw ASCII braces preserved when escaping disabled");
|
|
249
|
+
});
|
|
@@ -0,0 +1,197 @@
|
|
|
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 { createTools } from "../src/tools.js";
|
|
6
|
+
|
|
7
|
+
// v0.8.0 A2(issue #17):检索加权 + occurred_at 时间过滤。
|
|
8
|
+
// 当前 scope 加成、他 scope 降权但保留可见;occurred 窗在 store.list/count 与
|
|
9
|
+
// 搜索融合池两处同口径(COALESCE 回退 created_at)。
|
|
10
|
+
|
|
11
|
+
test("store.list/count filter by occurred_at with created_at fallback", () => {
|
|
12
|
+
const store = createStore(":memory:");
|
|
13
|
+
store.save({ type: "project", title: "aug", content: "x", occurred_at: "2026-08-15T00:00:00Z" });
|
|
14
|
+
store.save({ type: "project", title: "sep", content: "x", occurred_at: "2026-09-01T00:00:00Z" });
|
|
15
|
+
// 未标注 occurred_at → 回退 created_at(今天,2026-09-13)参与比较。
|
|
16
|
+
store.save({ type: "project", title: "legacy", content: "x" });
|
|
17
|
+
|
|
18
|
+
const aug = store.list({ occurredFrom: "2026-08-01", occurredTo: "2026-08-31" });
|
|
19
|
+
assert.deepEqual(aug.map((m) => m.title), ["aug"]);
|
|
20
|
+
assert.equal(store.count("project", { occurredFrom: "2026-08-01", occurredTo: "2026-08-31" }), 1);
|
|
21
|
+
|
|
22
|
+
// 回退方向:窗口从「今天」起(日期从刚写入行的 created_at 推导,不依赖
|
|
23
|
+
// 测试机时钟/时区),未标注行靠 created_at 落入;sep 的 occurred 是月初,
|
|
24
|
+
// 在窗口外。
|
|
25
|
+
const legacyRow = store.list({}).find((m) => m.title === "legacy");
|
|
26
|
+
const today = legacyRow.created_at.slice(0, 10);
|
|
27
|
+
const sinceToday = store.list({ occurredFrom: today }).map((m) => m.title);
|
|
28
|
+
assert.deepEqual(sinceToday, ["legacy"]);
|
|
29
|
+
|
|
30
|
+
// 非法边界值被忽略(不进 WHERE),三行都在。
|
|
31
|
+
assert.equal(store.list({ occurredFrom: "garbage", occurredTo: 42 }).length, 3);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("service.list/count pass the occurred window through", () => {
|
|
35
|
+
const store = createStore(":memory:");
|
|
36
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
37
|
+
store.save({ type: "decision", title: "old", content: "x", occurred_at: "2026-01-10T00:00:00Z" });
|
|
38
|
+
store.save({ type: "decision", title: "new", content: "x", occurred_at: "2026-06-10T00:00:00Z" });
|
|
39
|
+
const rows = service.list({ occurredFrom: "2026-06-01" });
|
|
40
|
+
assert.deepEqual(rows.map((m) => m.title), ["new"]);
|
|
41
|
+
assert.equal(service.count("decision", { occurredFrom: "2026-06-01" }), 1);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("searchMemories applies the occurred window to the fused pool", async () => {
|
|
45
|
+
const store = createStore(":memory:");
|
|
46
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
47
|
+
store.save({ type: "project", title: "needle aug", content: "hit", occurred_at: "2026-08-15T00:00:00Z" });
|
|
48
|
+
store.save({ type: "project", title: "needle legacy", content: "hit" });
|
|
49
|
+
|
|
50
|
+
const all = await service.searchMemories("needle", { mode: "keyword" });
|
|
51
|
+
assert.equal(all.length, 2);
|
|
52
|
+
|
|
53
|
+
const windowed = await service.searchMemories("needle", {
|
|
54
|
+
mode: "keyword",
|
|
55
|
+
occurredFrom: "2026-08-01",
|
|
56
|
+
occurredTo: "2026-08-31"
|
|
57
|
+
});
|
|
58
|
+
assert.deepEqual(windowed.map((m) => m.title), ["needle aug"]);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("searchMemories scope weighting: current scope boosted, foreign demoted but visible", async () => {
|
|
62
|
+
const store = createStore(":memory:");
|
|
63
|
+
const service = createService({ store, mirror: null, config: { scopeEnabled: true } });
|
|
64
|
+
// 打分构造(scoreKeyword:标题命中 1 × 0.8,正文命中 0.6 × 0.8):
|
|
65
|
+
// 无加权时 alpha (0.8) 排在 beta (0.48) 前。
|
|
66
|
+
store.save({ type: "project", title: "alpha needle", content: "x", agent_scope: "coder" });
|
|
67
|
+
store.save({ type: "project", title: "beta", content: "needle", agent_scope: "novelist" });
|
|
68
|
+
|
|
69
|
+
// 当前会话是 novelist:beta 命中 ×1.25=0.6,alpha 他 scope ×0.5=0.4 → 翻转,
|
|
70
|
+
// 但 alpha 仍可见(降权不是过滤)。
|
|
71
|
+
const weighted = await service.searchMemories("needle", {
|
|
72
|
+
mode: "keyword",
|
|
73
|
+
scope: { agent_scope: "novelist", workspace_scope: null }
|
|
74
|
+
});
|
|
75
|
+
assert.deepEqual(weighted.map((m) => m.title), ["beta", "alpha needle"]);
|
|
76
|
+
|
|
77
|
+
// 无 scope 传参 → 不动分,保持基线排序。
|
|
78
|
+
const neutral = await service.searchMemories("needle", { mode: "keyword" });
|
|
79
|
+
assert.deepEqual(neutral.map((m) => m.title), ["alpha needle", "beta"]);
|
|
80
|
+
|
|
81
|
+
// scope 全 NULL(解析不出)→ 同样不动分。
|
|
82
|
+
const nullScope = await service.searchMemories("needle", {
|
|
83
|
+
mode: "keyword",
|
|
84
|
+
scope: { agent_scope: null, workspace_scope: null }
|
|
85
|
+
});
|
|
86
|
+
assert.deepEqual(nullScope.map((m) => m.title), ["alpha needle", "beta"]);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("searchMemories weighting is off entirely when the flag is off", async () => {
|
|
90
|
+
const store = createStore(":memory:");
|
|
91
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
92
|
+
store.save({ type: "project", title: "alpha needle", content: "x", agent_scope: "coder" });
|
|
93
|
+
store.save({ type: "project", title: "beta", content: "needle", agent_scope: "novelist" });
|
|
94
|
+
const rows = await service.searchMemories("needle", {
|
|
95
|
+
mode: "keyword",
|
|
96
|
+
scope: { agent_scope: "novelist", workspace_scope: null }
|
|
97
|
+
});
|
|
98
|
+
assert.deepEqual(rows.map((m) => m.title), ["alpha needle", "beta"]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("unscoped rows rank with matched rows under weighting (never suppressed)", async () => {
|
|
102
|
+
const store = createStore(":memory:");
|
|
103
|
+
const service = createService({ store, mirror: null, config: { scopeEnabled: true } });
|
|
104
|
+
store.save({ type: "project", title: "global needle", content: "x" });
|
|
105
|
+
store.save({ type: "project", title: "foreign", content: "needle", agent_scope: "someone-else" });
|
|
106
|
+
const rows = await service.searchMemories("needle", {
|
|
107
|
+
mode: "keyword",
|
|
108
|
+
scope: { agent_scope: "me", workspace_scope: null }
|
|
109
|
+
});
|
|
110
|
+
// 未标注行与命中行同列吃 BOOST(×1.25),foreign 行 ×0.5——不被任何会话压掉。
|
|
111
|
+
assert.deepEqual(rows.map((m) => m.title), ["global needle", "foreign"]);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
function setupTools(config) {
|
|
115
|
+
const store = createStore(":memory:");
|
|
116
|
+
const service = createService({ store, mirror: null, config });
|
|
117
|
+
const registered = [];
|
|
118
|
+
createTools(
|
|
119
|
+
{ tools: { register(def) { registered.push(def); return () => {}; } } },
|
|
120
|
+
service,
|
|
121
|
+
config
|
|
122
|
+
);
|
|
123
|
+
const pick = (name) => registered.find((def) => def.name === name);
|
|
124
|
+
return { store, service, pick };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async function runHandler(def, args, exec) {
|
|
128
|
+
const handler = def.execute.bind(def);
|
|
129
|
+
return handler(args, exec);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
test("toApiList surfaces scope annotation and occurred_at", () => {
|
|
133
|
+
const store = createStore(":memory:");
|
|
134
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
135
|
+
store.save({
|
|
136
|
+
type: "pitfall",
|
|
137
|
+
title: "stamped",
|
|
138
|
+
content: "x",
|
|
139
|
+
agent_scope: "coder",
|
|
140
|
+
workspace_scope: "D:\\proj",
|
|
141
|
+
sensitivity: "personal",
|
|
142
|
+
occurred_at: "2026-09-12T08:00:00Z"
|
|
143
|
+
});
|
|
144
|
+
const [row] = service.toApiList([store.getById(
|
|
145
|
+
service.list({}).find((m) => m.title === "stamped").id
|
|
146
|
+
)]);
|
|
147
|
+
assert.equal(row.agent_scope, "coder");
|
|
148
|
+
assert.equal(row.workspace_scope, "D:\\proj");
|
|
149
|
+
assert.equal(row.sensitivity, "personal");
|
|
150
|
+
assert.equal(row.occurred_at, "2026-09-12T08:00:00.000Z");
|
|
151
|
+
// 未标注行四字段为 undefined(JSON 序列化时省略)。
|
|
152
|
+
store.save({ type: "pitfall", title: "plain", content: "x" });
|
|
153
|
+
const [plain] = service.toApiList([service.list({}).find((m) => m.title === "plain")]);
|
|
154
|
+
assert.equal(plain.agent_scope, undefined);
|
|
155
|
+
assert.equal(plain.occurred_at, undefined);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test("memory_search passes the session scope and occurred window through", async () => {
|
|
159
|
+
const { store, pick } = setupTools({ scopeEnabled: true });
|
|
160
|
+
store.save({ type: "project", title: "alpha needle", content: "x", agent_scope: "coder" });
|
|
161
|
+
store.save({
|
|
162
|
+
type: "project", title: "beta", content: "needle", agent_scope: "novelist",
|
|
163
|
+
occurred_at: "2026-08-15T00:00:00Z"
|
|
164
|
+
});
|
|
165
|
+
const search = pick("memory_search");
|
|
166
|
+
const exec = { agent: { session: { id: "s1", header: { agentPreset: "novelist", cwd: "D:\\p" } } } };
|
|
167
|
+
const args = { query: "needle", occurred_from: "2026-08-01", occurred_to: "2026-08-31" };
|
|
168
|
+
const out = await runHandler(search, args, exec);
|
|
169
|
+
// 窗口内只剩 beta;其 agent_scope 命中当前会话(novelist),标注随行透出。
|
|
170
|
+
assert.equal(out.items.length, 1);
|
|
171
|
+
assert.equal(out.items[0].title, "beta");
|
|
172
|
+
assert.equal(out.items[0].agent_scope, "novelist");
|
|
173
|
+
assert.equal(out.items[0].occurred_at, "2026-08-15T00:00:00.000Z");
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
test("memory_list filters by the occurred window and keeps total consistent", async () => {
|
|
177
|
+
const { store, pick } = setupTools({});
|
|
178
|
+
store.save({ type: "decision", title: "old", content: "x", occurred_at: "2026-01-10T00:00:00Z" });
|
|
179
|
+
store.save({ type: "decision", title: "new", content: "x", occurred_at: "2026-06-10T00:00:00Z" });
|
|
180
|
+
const list = pick("memory_list");
|
|
181
|
+
const args = { type: "decision", occurred_from: "2026-06-01" };
|
|
182
|
+
const out = await runHandler(list, args, undefined);
|
|
183
|
+
assert.deepEqual(out.items.map((m) => m.title), ["new"]);
|
|
184
|
+
assert.equal(out.total, 1);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test("memory_get returns scope annotation and occurred_at", async () => {
|
|
188
|
+
const { store, pick } = setupTools({});
|
|
189
|
+
const saved = store.save({
|
|
190
|
+
type: "pitfall", title: "t", content: "c",
|
|
191
|
+
agent_scope: "coder", occurred_at: "2026-09-12T08:00:00Z"
|
|
192
|
+
});
|
|
193
|
+
const get = pick("memory_get");
|
|
194
|
+
const out = await runHandler(get, { id: saved.id }, undefined);
|
|
195
|
+
assert.equal(out.memory.agent_scope, "coder");
|
|
196
|
+
assert.equal(out.memory.occurred_at, "2026-09-12T08:00:00.000Z");
|
|
197
|
+
});
|
|
@@ -0,0 +1,247 @@
|
|
|
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 { createTools } from "../src/tools.js";
|
|
6
|
+
import { createScopeResolver } from "../src/scope.js";
|
|
7
|
+
|
|
8
|
+
// v0.8.0 A1(issue #17):scope 隔离存储层。
|
|
9
|
+
// 四列可空(agent_scope/workspace_scope/sensitivity/occurred_at)、迁移纯增量
|
|
10
|
+
// 不带 DEFAULT;写入标注走 scopeEnabled 门控;去重键扩展 (type, title, scope…)。
|
|
11
|
+
|
|
12
|
+
test("migration adds four nullable scope columns without defaults", () => {
|
|
13
|
+
const store = createStore(":memory:");
|
|
14
|
+
const cols = store.db.prepare("PRAGMA table_info(memories)").all();
|
|
15
|
+
for (const name of ["agent_scope", "workspace_scope", "sensitivity", "occurred_at"]) {
|
|
16
|
+
const col = cols.find((c) => c.name === name);
|
|
17
|
+
assert.ok(col, `column ${name} should exist`);
|
|
18
|
+
assert.equal(col.notnull, 0, `${name} must be nullable`);
|
|
19
|
+
assert.equal(col.dflt_value, null, `${name} must not carry a DEFAULT (存量零重写)`);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("rows written without scope fields read back unscoped (legacy semantics)", () => {
|
|
24
|
+
const store = createStore(":memory:");
|
|
25
|
+
const row = store.save({ type: "project", title: "legacy shape", content: "body" });
|
|
26
|
+
assert.equal(row.agent_scope, undefined);
|
|
27
|
+
assert.equal(row.workspace_scope, undefined);
|
|
28
|
+
assert.equal(row.sensitivity, undefined);
|
|
29
|
+
assert.equal(row.occurred_at, undefined);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("store.save persists and normalizes scope fields; dirty input lands as NULL", () => {
|
|
33
|
+
const store = createStore(":memory:");
|
|
34
|
+
const kept = store.save({
|
|
35
|
+
type: "project",
|
|
36
|
+
title: "scoped",
|
|
37
|
+
content: "c",
|
|
38
|
+
agent_scope: " coder-agent ",
|
|
39
|
+
workspace_scope: "D:\\proj",
|
|
40
|
+
sensitivity: "personal",
|
|
41
|
+
occurred_at: "2026-09-13T10:00:00+08:00"
|
|
42
|
+
});
|
|
43
|
+
assert.equal(kept.agent_scope, "coder-agent");
|
|
44
|
+
assert.equal(kept.workspace_scope, "D:\\proj");
|
|
45
|
+
assert.equal(kept.sensitivity, "personal");
|
|
46
|
+
// 时区归一:A2 的时间过滤按 ISO 字典序直接比较。
|
|
47
|
+
assert.equal(kept.occurred_at, "2026-09-13T02:00:00.000Z");
|
|
48
|
+
|
|
49
|
+
const dirty = store.save({
|
|
50
|
+
type: "project",
|
|
51
|
+
title: "dirty",
|
|
52
|
+
content: "c",
|
|
53
|
+
agent_scope: " ",
|
|
54
|
+
workspace_scope: 42,
|
|
55
|
+
sensitivity: "",
|
|
56
|
+
occurred_at: "not-a-date"
|
|
57
|
+
});
|
|
58
|
+
assert.equal(dirty.agent_scope, undefined);
|
|
59
|
+
assert.equal(dirty.workspace_scope, undefined);
|
|
60
|
+
assert.equal(dirty.sensitivity, undefined);
|
|
61
|
+
assert.equal(dirty.occurred_at, undefined);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test("store.update keeps scope columns untouched (write-time annotation is immutable)", () => {
|
|
65
|
+
const store = createStore(":memory:");
|
|
66
|
+
const saved = store.save({ type: "decision", title: "t", content: "old", agent_scope: "a1", workspace_scope: "w1" });
|
|
67
|
+
store.update(saved.id, { content: "new", importance: 4 });
|
|
68
|
+
const row = store.getById(saved.id);
|
|
69
|
+
assert.equal(row.content, "new");
|
|
70
|
+
assert.equal(row.agent_scope, "a1");
|
|
71
|
+
assert.equal(row.workspace_scope, "w1");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("scopeEnabled=false keeps legacy dedupe: cross-scope same-title still merges", () => {
|
|
75
|
+
const store = createStore(":memory:");
|
|
76
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
77
|
+
store.save({ type: "project", title: "T", content: "scoped body", agent_scope: "agentA" });
|
|
78
|
+
const { action } = service.saveWithDedupe({ type: "project", title: "T", content: "new body" });
|
|
79
|
+
assert.equal(action, "merged");
|
|
80
|
+
assert.equal(store.count("project"), 1);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("scopeEnabled=true: dedupe key extends with agent_scope/workspace_scope/sensitivity", () => {
|
|
84
|
+
const store = createStore(":memory:");
|
|
85
|
+
const service = createService({ store, mirror: null, config: { scopeEnabled: true } });
|
|
86
|
+
const save = (fields) => service.saveWithDedupe({ type: "project", title: "T", content: "body", ...fields });
|
|
87
|
+
|
|
88
|
+
assert.equal(save({ agent_scope: "agentA" }).action, "created");
|
|
89
|
+
// 同标题、不同 agent_scope → 独立行,绝不互相合并。
|
|
90
|
+
assert.equal(save({ agent_scope: "agentB" }).action, "created");
|
|
91
|
+
// 不同 workspace_scope → 独立行。
|
|
92
|
+
assert.equal(save({ agent_scope: "agentA", workspace_scope: "D:\\proj-a" }).action, "created");
|
|
93
|
+
// 不同 sensitivity → 独立行。
|
|
94
|
+
assert.equal(save({ agent_scope: "agentA", sensitivity: "personal" }).action, "created");
|
|
95
|
+
// 三元组全同 → 合并。
|
|
96
|
+
assert.equal(save({ agent_scope: "agentA" }).action, "merged");
|
|
97
|
+
assert.equal(store.count("project"), 4);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("scopeEnabled=true: unscoped writes match legacy NULL rows but not stamped ones", () => {
|
|
101
|
+
const store = createStore(":memory:");
|
|
102
|
+
const service = createService({ store, mirror: null, config: { scopeEnabled: true } });
|
|
103
|
+
// 存量形状(NULL scope)。
|
|
104
|
+
store.save({ type: "preference", title: "P", content: "legacy" });
|
|
105
|
+
// 未标注新写入与存量 NULL 行互相匹配。
|
|
106
|
+
assert.equal(service.saveWithDedupe({ type: "preference", title: "P", content: "x" }).action, "merged");
|
|
107
|
+
// 已标注行与 NULL 行不匹配 → 独立新行。
|
|
108
|
+
assert.equal(service.saveWithDedupe({ type: "preference", title: "P", content: "x", agent_scope: "agentA" }).action, "created");
|
|
109
|
+
assert.equal(store.count("preference"), 2);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("scopeEnabled=true: _mergeInto across scopes is rejected and falls back to a fresh row", () => {
|
|
113
|
+
const store = createStore(":memory:");
|
|
114
|
+
const service = createService({ store, mirror: null, config: { scopeEnabled: true } });
|
|
115
|
+
const scoped = store.save({ type: "project", title: "S", content: "scoped", agent_scope: "agentA" });
|
|
116
|
+
const cross = service.saveWithDedupe({
|
|
117
|
+
type: "project", title: "S", content: "summary text", _mergeInto: scoped.id
|
|
118
|
+
});
|
|
119
|
+
assert.equal(cross.action, "created", "cross-scope semantic merge must not leak into the scoped row");
|
|
120
|
+
const same = service.saveWithDedupe({
|
|
121
|
+
type: "project", title: "S2", content: "more text", agent_scope: "agentA", _mergeInto: scoped.id
|
|
122
|
+
});
|
|
123
|
+
assert.equal(same.action, "merged", "same-scope explicit target still merges");
|
|
124
|
+
assert.equal(same.memory.id, scoped.id);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
function setupTools(config) {
|
|
128
|
+
const store = createStore(":memory:");
|
|
129
|
+
const service = createService({ store, mirror: null, config });
|
|
130
|
+
const registered = [];
|
|
131
|
+
createTools(
|
|
132
|
+
{ tools: { register(def) { registered.push(def); return () => {}; } } },
|
|
133
|
+
service,
|
|
134
|
+
config
|
|
135
|
+
);
|
|
136
|
+
const memorySave = registered.find((def) => def.name === "memory_save");
|
|
137
|
+
assert.ok(memorySave, "memory_save registered");
|
|
138
|
+
return { store, memorySave };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async function runMemorySave(memorySave, args, exec) {
|
|
142
|
+
const handler = memorySave.execute.bind(memorySave);
|
|
143
|
+
return handler(args, exec);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
test("memory_save stamps resolved scope onto the row when the flag is on", async () => {
|
|
147
|
+
const { store, memorySave } = setupTools({ scopeEnabled: true });
|
|
148
|
+
const exec = { agent: { session: { id: "s1", header: { agentPreset: "coder", cwd: "D:\\proj" } } } };
|
|
149
|
+
const args = { type: "pitfall", title: "T", content: "c", sensitivity: "personal", occurred_at: "2026-09-12T00:00:00Z" };
|
|
150
|
+
const out = await runMemorySave(memorySave, args, exec);
|
|
151
|
+
const row = store.getById(out.id);
|
|
152
|
+
assert.equal(row.agent_scope, "coder");
|
|
153
|
+
assert.equal(row.workspace_scope, "D:\\proj");
|
|
154
|
+
assert.equal(row.sensitivity, "personal");
|
|
155
|
+
assert.equal(row.occurred_at, "2026-09-12T00:00:00.000Z");
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
test("memory_save without exec context writes unscoped rows (flag on, nothing resolvable)", async () => {
|
|
159
|
+
const { store, memorySave } = setupTools({ scopeEnabled: true });
|
|
160
|
+
const out = await runMemorySave(memorySave, { type: "pitfall", title: "T", content: "c" }, undefined);
|
|
161
|
+
const row = store.getById(out.id);
|
|
162
|
+
assert.equal(row.agent_scope, undefined);
|
|
163
|
+
assert.equal(row.workspace_scope, undefined);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("memory_save with the flag off writes rows without any scope annotation", async () => {
|
|
167
|
+
const { store, memorySave } = setupTools({});
|
|
168
|
+
const exec = { agent: { session: { id: "s1", header: { agentPreset: "coder", cwd: "D:\\proj" } } } };
|
|
169
|
+
const out = await runMemorySave(memorySave, { type: "pitfall", title: "T", content: "c" }, exec);
|
|
170
|
+
const row = store.getById(out.id);
|
|
171
|
+
assert.equal(row.agent_scope, undefined);
|
|
172
|
+
assert.equal(row.workspace_scope, undefined);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
test("createScopeResolver returns null when scopeEnabled is off", () => {
|
|
176
|
+
const resolve = createScopeResolver({ ctx: {}, config: {} });
|
|
177
|
+
assert.equal(resolve({ agent: { session: { id: "s1" } } }), null);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test("resolver stamps agentPreset and header cwd; registry hit wins over header cwd", () => {
|
|
181
|
+
const resolve = createScopeResolver({
|
|
182
|
+
config: { scopeEnabled: true },
|
|
183
|
+
ctx: { workspaceRegistry: { list: () => [{ path: "C:\\canon", sessionIds: ["s1"] }] } }
|
|
184
|
+
});
|
|
185
|
+
const exec = { agent: { session: { id: "s1", header: { agentPreset: "coder", cwd: "D:\\raw" } } } };
|
|
186
|
+
assert.deepEqual(resolve(exec), { agent_scope: "coder", workspace_scope: "C:\\canon" });
|
|
187
|
+
|
|
188
|
+
const resolveNoHit = createScopeResolver({
|
|
189
|
+
config: { scopeEnabled: true },
|
|
190
|
+
ctx: { workspaceRegistry: { list: () => [{ path: "C:\\canon", sessionIds: ["other"] }] } }
|
|
191
|
+
});
|
|
192
|
+
assert.deepEqual(resolveNoHit(exec), { agent_scope: "coder", workspace_scope: "D:\\raw" });
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test("resolver falls back to the static .header property on old hosts", () => {
|
|
196
|
+
const resolve = createScopeResolver({ config: { scopeEnabled: true }, ctx: {} });
|
|
197
|
+
const exec = { agent: { session: { id: "s1", header: { agentPreset: "novelist" } } } };
|
|
198
|
+
assert.deepEqual(resolve(exec), { agent_scope: "novelist", workspace_scope: null });
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
test("session.header wins over requestHeader() (EpochHeader must not shadow the durable header)", () => {
|
|
202
|
+
const resolve = createScopeResolver({ config: { scopeEnabled: true }, ctx: {} });
|
|
203
|
+
// 本地 web 复验(2026-09-13)实测踩中的形状:requestHeader() 返回请求级
|
|
204
|
+
// EpochHeader(模型路由头,无 agentPreset/cwd),曾在首个请求后遮蔽
|
|
205
|
+
// session.header,导致标注全空。
|
|
206
|
+
const exec = {
|
|
207
|
+
agent: {
|
|
208
|
+
session: {
|
|
209
|
+
id: "s1",
|
|
210
|
+
header: { agentPreset: "coder", cwd: "D:\\proj" },
|
|
211
|
+
requestHeader: () => ({ config: { provider: "deepseek-official", model: "deepseek-v4-flash" } })
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
assert.deepEqual(resolve(exec), { agent_scope: "coder", workspace_scope: "D:\\proj" });
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test("hosts without .header fall back to requestHeader().config shape", () => {
|
|
219
|
+
const resolve = createScopeResolver({ config: { scopeEnabled: true }, ctx: {} });
|
|
220
|
+
const exec = {
|
|
221
|
+
agent: {
|
|
222
|
+
session: {
|
|
223
|
+
id: "s2",
|
|
224
|
+
requestHeader: () => ({ config: { agentPreset: "legacy", cwd: "E:\\old" } })
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
assert.deepEqual(resolve(exec), { agent_scope: "legacy", workspace_scope: "E:\\old" });
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test("resolver never throws: registry explosion degrades to header cwd and warns once", () => {
|
|
232
|
+
const warnings = [];
|
|
233
|
+
const resolve = createScopeResolver({
|
|
234
|
+
config: { scopeEnabled: true },
|
|
235
|
+
ctx: { workspaceRegistry: { list: () => { throw new Error("boom"); } } },
|
|
236
|
+
logger: { warn: (m) => warnings.push(String(m)) }
|
|
237
|
+
});
|
|
238
|
+
const exec = { agent: { session: { id: "s1", header: { agentPreset: "coder", cwd: "D:\\raw" } } } };
|
|
239
|
+
assert.deepEqual(resolve(exec), { agent_scope: "coder", workspace_scope: "D:\\raw" });
|
|
240
|
+
assert.equal(warnings.length, 1, "warn exactly once, never block the write path");
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
test("resolver without a session or header lands all-NULL", () => {
|
|
244
|
+
const resolve = createScopeResolver({ config: { scopeEnabled: true }, ctx: {} });
|
|
245
|
+
assert.deepEqual(resolve(undefined), { agent_scope: null, workspace_scope: null });
|
|
246
|
+
assert.deepEqual(resolve({ agent: { session: { id: "s2" } } }), { agent_scope: null, workspace_scope: null });
|
|
247
|
+
});
|