@modusensus/dsh-mneme 0.9.3 → 0.9.5
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 +56 -10
- package/bin/cli.mjs +215 -2
- package/docs/CONFIGURATION.md +10 -2
- package/docs/DAEMON.md +9 -2
- package/docs/DISTILL.md +66 -4
- package/docs/SHARE.md +552 -0
- package/docs/SLEEP.md +1 -1
- package/docs/STORAGE.md +1 -1
- package/lib/api-standalone.js +286 -8
- package/lib/api.js +254 -3
- package/lib/client.js +1000 -50
- package/lib/config.js +50 -1
- package/lib/document.js +15 -2
- package/lib/dream/decisions.js +88 -3
- package/lib/dream/sleep.js +63 -13
- package/lib/dream.js +66 -10
- package/lib/index.js +6 -1
- package/lib/inject.js +10 -25
- package/lib/lang.js +48 -6
- package/lib/memory-source.js +9 -0
- package/lib/organize.js +9 -3
- package/lib/pipeline.js +62 -1
- package/lib/profile-extract.js +161 -0
- package/lib/provenance-http.js +126 -0
- package/lib/serve.js +13 -0
- package/lib/service.js +70 -6
- package/lib/settings.js +17 -13
- package/lib/share-http.js +281 -0
- package/lib/share-import.js +234 -0
- package/lib/share.js +335 -0
- package/lib/store.js +272 -11
- package/lib/summarize.js +118 -8
- package/lib/tools.js +240 -1
- package/lib/user-settings.js +78 -0
- package/package.json +1 -1
- package/src/api-standalone.js +286 -8
- package/src/api.js +254 -3
- package/src/config.js +50 -1
- package/src/document.js +15 -2
- package/src/dream/decisions.js +88 -3
- package/src/dream/sleep.js +63 -13
- package/src/dream.js +66 -10
- package/src/index.js +6 -1
- package/src/inject.js +10 -25
- package/src/lang.js +48 -6
- package/src/memory-source.js +9 -0
- package/src/organize.js +9 -3
- package/src/pipeline.js +62 -1
- package/src/profile-extract.js +161 -0
- package/src/provenance-http.js +126 -0
- package/src/serve.js +13 -0
- package/src/service.js +70 -6
- package/src/settings.js +17 -13
- package/src/share-http.js +281 -0
- package/src/share-import.js +234 -0
- package/src/share.js +335 -0
- package/src/store.js +272 -11
- package/src/summarize.js +118 -8
- package/src/tools.js +240 -1
- package/src/user-settings.js +78 -0
- package/test/api.test.js +384 -3
- package/test/audit.test.js +2 -2
- package/test/cli-receive.test.js +390 -0
- package/test/cli-share.test.js +437 -0
- package/test/client-share.test.js +225 -0
- package/test/client.test.js +215 -6
- package/test/dedupe-candidate-window.test.js +180 -0
- package/test/dream-candidate-hybrid.test.js +2 -2
- package/test/dream-fence.test.js +164 -0
- package/test/dream.test.js +87 -5
- package/test/helpers/dream-mock.js +6 -2
- package/test/lang.test.js +11 -0
- package/test/llm-message-source.test.js +1 -1
- package/test/mint-source.test.js +1 -1
- package/test/organize-tool.test.js +232 -0
- package/test/persona-addressing.test.js +83 -0
- package/test/persona-settings.test.js +30 -8
- package/test/pipeline-r1r2.test.js +129 -0
- package/test/policy-epoch.test.js +2 -2
- package/test/profile-extract.test.js +269 -0
- package/test/provenance-http.test.js +184 -0
- package/test/readme-claims.test.js +45 -0
- package/test/reasoning-effort.test.js +10 -10
- package/test/scope-candidates.test.js +7 -4
- package/test/scope-state.test.js +130 -1
- package/test/serve.test.js +62 -0
- package/test/share-foreign-rows.test.js +144 -0
- package/test/share-http.test.js +144 -0
- package/test/share-import.test.js +577 -0
- package/test/share-imported-guard.test.js +311 -0
- package/test/share-imported-identity.test.js +111 -0
- package/test/share-pack.test.js +511 -0
- package/test/share-receive-http.test.js +756 -0
- package/test/share-routes.test.js +337 -0
- package/test/share-store.test.js +133 -0
- package/test/share-validate.test.js +101 -0
- package/test/standalone-api.test.js +524 -5
- package/test/summarize-parse-diagnostics.test.js +75 -0
- package/test/summarize.test.js +240 -2
- package/test/tools-dto-schema-matrix.test.js +48 -5
- package/test/tools.test.js +6 -1
- package/test/user-settings-preview.test.js +127 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
// issue #231 的第二批:把已造好的整理引擎接到 agent 的调用面上(organizeEnabled
|
|
2
|
+
// + memory_organize 工具)。src/organize.js 的本体语义由 test/organize.test.js 守,
|
|
3
|
+
// 本文件只守**出口**这一层:闸门、报告/落地的两步协议、以及 strictScope 下
|
|
4
|
+
// 「看不见的行按不存在处理」这条可信上下文。
|
|
5
|
+
import test from "node:test";
|
|
6
|
+
import assert from "node:assert/strict";
|
|
7
|
+
import { createStore } from "../src/store.js";
|
|
8
|
+
import { createService } from "../src/service.js";
|
|
9
|
+
import { createTools } from "../src/tools.js";
|
|
10
|
+
import { applyLightModePreset } from "../src/config.js";
|
|
11
|
+
|
|
12
|
+
function setup(config = {}) {
|
|
13
|
+
const store = createStore(":memory:");
|
|
14
|
+
const service = createService({ store, mirror: null, config });
|
|
15
|
+
const registered = [];
|
|
16
|
+
const ctx = {
|
|
17
|
+
tools: {
|
|
18
|
+
register(def) {
|
|
19
|
+
registered.push(def);
|
|
20
|
+
return () => {};
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
createTools(ctx, service, config, undefined);
|
|
25
|
+
const byName = Object.fromEntries(registered.map((def) => [def.name, def]));
|
|
26
|
+
return { store, service, byName, registered };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const CANDIDATES = [
|
|
30
|
+
{ type: "project", title: "发布只在包目录内", content: "根目录发过坏包,发布必须在 dsh-mneme/ 内执行。" },
|
|
31
|
+
{ type: "project", title: "另一条", content: "第二条候选的正文。" }
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
test("memory_organize registers when organizeEnabled is on", async () => {
|
|
35
|
+
const { byName } = setup({ organizeEnabled: true });
|
|
36
|
+
const tool = byName.memory_organize;
|
|
37
|
+
assert.ok(tool, "organizeEnabled=true 应当注册 memory_organize");
|
|
38
|
+
const report = await tool.execute({ mode: "dryRun", candidates: CANDIDATES });
|
|
39
|
+
assert.equal(report.status, "ok");
|
|
40
|
+
assert.equal(report.counts.total, 2, "两条候选都应进报告");
|
|
41
|
+
assert.equal(report.run_id ? typeof report.run_id : "none", "string", "dryRun 必须回一个 run_id 供 apply 引用");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// 默认关=零行为变化是仓库红线(AGENTS.md 自查清单第 1 条)。这条现在就会绿——
|
|
45
|
+
// 它防的是「以后有人把默认翻成 true」,而不是今天的缺失实现。
|
|
46
|
+
test("memory_organize stays unregistered by default", () => {
|
|
47
|
+
const { registered } = setup();
|
|
48
|
+
assert.equal(registered.some((t) => t.name === "memory_organize"), false, "organizeEnabled 缺省时不得注册");
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("lightMode preset turns organize off, but an explicit user switch wins", () => {
|
|
52
|
+
// 轻量档那一格是**预设**,不是强制(合并顺序「用户开关 > 轻量预设 > bundle」在
|
|
53
|
+
// index.js),所以这里断言的是预设值本身——同 continuity.test.js 的
|
|
54
|
+
// continuityRescueEnabled 那条。工具侧的注册与否由上一条默认关用例守着。
|
|
55
|
+
assert.equal(applyLightModePreset({ lightMode: true }).organizeEnabled, false);
|
|
56
|
+
assert.equal(applyLightModePreset({ lightMode: true, organizeEnabled: true }).organizeEnabled, false, "预设写死 false,赢的是合并时的用户开关,不是这里");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("dryRun writes no memory rows but leaves one organize audit row", () => {
|
|
60
|
+
const { store, byName } = setup({ organizeEnabled: true });
|
|
61
|
+
return byName.memory_organize.execute({ mode: "dryRun", candidates: CANDIDATES }).then((report) => {
|
|
62
|
+
assert.deepEqual(store.list({ limit: null }), [], "比对是报告,不是动作:记忆表必须一行不写");
|
|
63
|
+
const runs = store.listDreamRuns({ limit: 10 });
|
|
64
|
+
assert.equal(runs.length, 1, "dryRun 只留 dream_runs 一行回执");
|
|
65
|
+
assert.equal(runs[0].run_type, "organize");
|
|
66
|
+
assert.equal(runs[0].id, report.run_id);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("apply lands the decisions it references and reports counters", async () => {
|
|
71
|
+
const { store, byName } = setup({ organizeEnabled: true });
|
|
72
|
+
const report = await byName.memory_organize.execute({ mode: "dryRun", candidates: CANDIDATES });
|
|
73
|
+
const applied = await byName.memory_organize.execute({
|
|
74
|
+
mode: "apply",
|
|
75
|
+
run_id: report.run_id,
|
|
76
|
+
decisions: [
|
|
77
|
+
{ action: "save", index: 0 },
|
|
78
|
+
{ action: "discard", index: 1 }
|
|
79
|
+
]
|
|
80
|
+
});
|
|
81
|
+
assert.equal(applied.saved, 1, "只数新建的行");
|
|
82
|
+
assert.equal(applied.discarded, 1);
|
|
83
|
+
assert.equal(applied.status, "ok");
|
|
84
|
+
assert.equal(store.list({ limit: null }).length, 1, "一条 save、一条 discard → 库里只有一行");
|
|
85
|
+
const types = store.listDreamRuns({ limit: 10 }).map((r) => r.run_type);
|
|
86
|
+
assert.deepEqual(types, ["organize", "organize"], "报告与落地各留一行审计");
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("apply refuses a run_id that was never reported", async () => {
|
|
90
|
+
const { byName } = setup({ organizeEnabled: true });
|
|
91
|
+
await assert.rejects(
|
|
92
|
+
() => byName.memory_organize.execute({ mode: "apply", run_id: "no-such-run", decisions: [{ action: "discard", index: 0 }] }),
|
|
93
|
+
/unknown organize run/,
|
|
94
|
+
"没有比对过的候选一律不落库(organize 硬规则 2)"
|
|
95
|
+
);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("apply refuses a missing run_id", async () => {
|
|
99
|
+
const { byName } = setup({ organizeEnabled: true });
|
|
100
|
+
await assert.rejects(
|
|
101
|
+
() => byName.memory_organize.execute({ mode: "apply", decisions: [{ action: "discard", index: 0 }] }),
|
|
102
|
+
/run_id is required/,
|
|
103
|
+
"apply 必须引用一次真实 dryRun"
|
|
104
|
+
);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// 整理接口不能成为第二条「写进库却不带 scope」的通道:memory_save 在 scopeEnabled
|
|
108
|
+
// 下按会话身份盖 auto 章,organize 落地的候选必须同源。显式参数改盖 explicit,
|
|
109
|
+
// 与 memory_save / memory_register_document 同一规则。
|
|
110
|
+
test("save lands candidates stamped with the session scope (auto), like memory_save", async () => {
|
|
111
|
+
const { store, byName } = setup({ organizeEnabled: true, scopeEnabled: true });
|
|
112
|
+
const exec = { agent: { session: { id: "s1", header: { agentPreset: "me", cwd: "D:\\p" } } } };
|
|
113
|
+
const report = await byName.memory_organize.execute({ mode: "dryRun", candidates: CANDIDATES }, exec);
|
|
114
|
+
await byName.memory_organize.execute({ mode: "apply", run_id: report.run_id, decisions: [{ action: "save", index: 0 }] }, exec);
|
|
115
|
+
const row = store.list({ limit: null })[0];
|
|
116
|
+
assert.equal(row.agent_scope, "me", "缺省吃会话身份");
|
|
117
|
+
assert.equal(row.agent_scope_source, "auto");
|
|
118
|
+
assert.equal(row.workspace_scope, "D:\\p");
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("explicit scope arguments stamp as explicit and win over the session label", async () => {
|
|
122
|
+
const { store, byName } = setup({ organizeEnabled: true, scopeEnabled: true });
|
|
123
|
+
const exec = { agent: { session: { id: "s1", header: { agentPreset: "me", cwd: "D:\\p" } } } };
|
|
124
|
+
const report = await byName.memory_organize.execute(
|
|
125
|
+
{ mode: "dryRun", candidates: CANDIDATES, agent_scope: "other-agent" },
|
|
126
|
+
exec
|
|
127
|
+
);
|
|
128
|
+
await byName.memory_organize.execute({ mode: "apply", run_id: report.run_id, decisions: [{ action: "save", index: 0 }] }, exec);
|
|
129
|
+
const row = store.list({ limit: null })[0];
|
|
130
|
+
assert.equal(row.agent_scope, "other-agent", "显式参数盖在会话身份之上");
|
|
131
|
+
assert.equal(row.agent_scope_source, "explicit");
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("unknown mode is rejected", async () => {
|
|
135
|
+
const { byName } = setup({ organizeEnabled: true });
|
|
136
|
+
await assert.rejects(() => byName.memory_organize.execute({ mode: "force" }), /unknown mode/);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
// #170 复核项 4 的口径延伸到整理接口的**两侧**:报告侧不得泄漏存在性,落地侧不得
|
|
140
|
+
// 绕过 scope 治理改库。用例形状是刻意挑的:比对键只看三列**值**(sensitivity/agent/
|
|
141
|
+
// workspace),可见性还多认一个 `explicit` 来源,所以要造出「按值该命中、但会话看不见」
|
|
142
|
+
// 那一格——匿名会话(解析不到 agentPreset)对 explicit 行 fail-closed。
|
|
143
|
+
// 报告侧少了过滤就是探测口:`agent_scope` 是模型自己给的参数,A 会话可以拿 B 的标题问
|
|
144
|
+
// 「那一档有没有这行」,拿回 id/title 就等于确认存在——memory_get 在同一条件下回「不存在」。
|
|
145
|
+
test("dryRun reports an invisible match as new, with no id or title", async () => {
|
|
146
|
+
const config = { organizeEnabled: true, scopeEnabled: true, strictScope: true };
|
|
147
|
+
const { store, byName } = setup(config);
|
|
148
|
+
store.save({
|
|
149
|
+
type: "project",
|
|
150
|
+
title: "发布只在包目录内",
|
|
151
|
+
content: "team-a 名下的旧行。",
|
|
152
|
+
agent_scope: "team-a",
|
|
153
|
+
agent_scope_source: "explicit",
|
|
154
|
+
workspace_scope: "D:\\p",
|
|
155
|
+
workspace_scope_source: "auto"
|
|
156
|
+
});
|
|
157
|
+
const exec = { agent: { session: { id: "s-anon", header: { cwd: "D:\\p" } } } };
|
|
158
|
+
const report = await byName.memory_organize.execute(
|
|
159
|
+
{
|
|
160
|
+
mode: "dryRun",
|
|
161
|
+
candidates: [{ type: "project", title: "发布只在包目录内", content: "换一种说法的同一件事。" }],
|
|
162
|
+
agent_scope: "team-a",
|
|
163
|
+
workspace_scope: "D:\\p"
|
|
164
|
+
},
|
|
165
|
+
exec
|
|
166
|
+
);
|
|
167
|
+
assert.equal(report.items[0].verdict, "new", "看不见的行按不存在处理 → 报告只能说「新条目」");
|
|
168
|
+
assert.equal(report.items[0].match, undefined, "不得带回落点 id/title/sim");
|
|
169
|
+
assert.equal(report.counts.new, 1);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
// 反面对照:strictScope 是**可选**硬过滤,关掉它时那行本来就可读(A2 只降权)。少了这条,
|
|
173
|
+
// 过滤就变成「闸门关着也少召回」的第二重惩罚——修安全问题不许顺手改默认行为。
|
|
174
|
+
test("the same row is still reported as exact when strictScope is off", async () => {
|
|
175
|
+
const config = { organizeEnabled: true, scopeEnabled: true };
|
|
176
|
+
const { store, byName } = setup(config);
|
|
177
|
+
const row = store.save({
|
|
178
|
+
type: "project",
|
|
179
|
+
title: "发布只在包目录内",
|
|
180
|
+
content: "team-a 名下的旧行。",
|
|
181
|
+
agent_scope: "team-a",
|
|
182
|
+
agent_scope_source: "explicit",
|
|
183
|
+
workspace_scope: "D:\\p",
|
|
184
|
+
workspace_scope_source: "auto"
|
|
185
|
+
});
|
|
186
|
+
const exec = { agent: { session: { id: "s-anon", header: { cwd: "D:\\p" } } } };
|
|
187
|
+
const report = await byName.memory_organize.execute(
|
|
188
|
+
{
|
|
189
|
+
mode: "dryRun",
|
|
190
|
+
candidates: [{ type: "project", title: "发布只在包目录内", content: "换一种说法的同一件事。" }],
|
|
191
|
+
agent_scope: "team-a",
|
|
192
|
+
workspace_scope: "D:\\p"
|
|
193
|
+
},
|
|
194
|
+
exec
|
|
195
|
+
);
|
|
196
|
+
assert.equal(report.items[0].verdict, "exact");
|
|
197
|
+
assert.equal(report.items[0].match.id, row.id, "闸门关着时照常给出落点,否则比对报告失去意义");
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
// 落地侧:报告已按「不存在」说了,archive 也不许真的动那一行(`opts.hiddenIds` 由可信
|
|
201
|
+
// 调用层算好传入,绝不取 payload 字段——那是调用方可自选的内容,不是授权)。
|
|
202
|
+
test("archive of a row the caller cannot see is skipped as out of scope", async () => {
|
|
203
|
+
const config = { organizeEnabled: true, scopeEnabled: true, strictScope: true };
|
|
204
|
+
const { store, byName } = setup(config);
|
|
205
|
+
const teamRow = store.save({
|
|
206
|
+
type: "project",
|
|
207
|
+
title: "发布只在包目录内",
|
|
208
|
+
content: "team-a 名下的旧行。",
|
|
209
|
+
agent_scope: "team-a",
|
|
210
|
+
agent_scope_source: "explicit",
|
|
211
|
+
workspace_scope: "D:\\p",
|
|
212
|
+
workspace_scope_source: "auto"
|
|
213
|
+
});
|
|
214
|
+
const exec = { agent: { session: { id: "s-anon", header: { cwd: "D:\\p" } } } };
|
|
215
|
+
const report = await byName.memory_organize.execute(
|
|
216
|
+
{
|
|
217
|
+
mode: "dryRun",
|
|
218
|
+
candidates: [{ type: "project", title: "发布只在包目录内", content: "换一种说法的同一件事。" }],
|
|
219
|
+
agent_scope: "team-a",
|
|
220
|
+
workspace_scope: "D:\\p"
|
|
221
|
+
},
|
|
222
|
+
exec
|
|
223
|
+
);
|
|
224
|
+
const applied = await byName.memory_organize.execute(
|
|
225
|
+
{ mode: "apply", run_id: report.run_id, decisions: [{ action: "archive", id: teamRow.id }] },
|
|
226
|
+
exec
|
|
227
|
+
);
|
|
228
|
+
assert.equal(applied.archived, 0, "看不见的行按不存在处理,不得归档");
|
|
229
|
+
assert.equal(applied.skipped.length, 1);
|
|
230
|
+
assert.match(applied.skipped[0].error, /out of scope/, "文案维持「unknown, archived or out of scope」:不泄漏存在性");
|
|
231
|
+
assert.equal(store.getById(teamRow.id).archived, false, "他人的行原样未动");
|
|
232
|
+
});
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// 修复一:人格寻址的兜底口径。default 卡的存在理由是「这一帧解析不到会话身份」,
|
|
2
|
+
// 而不是「所有没建卡的 agent 都该拿到它」——后者会把某个人格无条件塞进外部 bot 的
|
|
3
|
+
// 会话(DS Bot 那类:它自己带身份与设定,两处身份声明撞在同一帧里)。
|
|
4
|
+
// 现状(personaStrictAddressing 缺省 = false)已由 test/persona-inject.test.js:113
|
|
5
|
+
// 与 :124 钉住,本文件第一条照样锁它——这是 opt-in,不是把别人的行为改了。
|
|
6
|
+
import test from "node:test";
|
|
7
|
+
import assert from "node:assert/strict";
|
|
8
|
+
import { createStore } from "../src/store.js";
|
|
9
|
+
import { createService } from "../src/service.js";
|
|
10
|
+
import { createInjector } from "../src/inject.js";
|
|
11
|
+
import { createSettings } from "../src/settings.js";
|
|
12
|
+
|
|
13
|
+
function setup(over = {}) {
|
|
14
|
+
const store = createStore(":memory:");
|
|
15
|
+
const service = createService({ store, mirror: null, config: {} });
|
|
16
|
+
const settings = createSettings(store.db);
|
|
17
|
+
const contexts = [];
|
|
18
|
+
const ctx = {
|
|
19
|
+
systemPrompt: {
|
|
20
|
+
context(def) {
|
|
21
|
+
contexts.push(def);
|
|
22
|
+
return () => {};
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
createInjector(ctx, service, settings, { maxInjectedItems: 3, importanceThreshold: 3, ...over });
|
|
27
|
+
return { store, settings, settingsCtx: contexts.find((c) => c.name === "user-settings") };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const withPreset = (agentPreset) => ({ agent: { session: { id: "s1", header: { agentPreset } } } });
|
|
31
|
+
|
|
32
|
+
// 种子:default 卡(Amarre)开着 + 一张 zephyr 卡。
|
|
33
|
+
function seed(settings, zephyrEnabled = true) {
|
|
34
|
+
settings.setPersona({ name: "Amarre", personality: "轻松幽默", enabled: true });
|
|
35
|
+
settings.setPersonaFor("zephyr", { name: "泽菲尔", personality: "冷危", enabled: zephyrEnabled });
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
test("缺省(开关关):没建卡的身份仍落 default——本 PR 不改默认行为", () => {
|
|
39
|
+
const { store, settings, settingsCtx } = setup({ personaEnabled: true });
|
|
40
|
+
seed(settings);
|
|
41
|
+
const text = settingsCtx.text(withPreset("unknown-bot"));
|
|
42
|
+
assert.ok(text.includes("Amarre"), "personaStrictAddressing 缺省时行为逐字节不变");
|
|
43
|
+
store.close();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("strict on:没建卡的身份不注入任何人格行", () => {
|
|
47
|
+
const { store, settings, settingsCtx } = setup({ personaEnabled: true, personaStrictAddressing: true });
|
|
48
|
+
seed(settings);
|
|
49
|
+
settings.setProfile("用户是规划背景");
|
|
50
|
+
const text = settingsCtx.text(withPreset("unknown-bot"));
|
|
51
|
+
assert.ok(!text.includes("Amarre"), "外部身份不该收到别人的人格");
|
|
52
|
+
assert.ok(!text.includes("你的人格名字"), "无人格时整段人格行都不出现,不是留个空标签");
|
|
53
|
+
store.close();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("strict on:显式关掉的卡不再回落 default——这让「关掉这张卡」第一次有意义", () => {
|
|
57
|
+
const { store, settings, settingsCtx } = setup({ personaEnabled: true, personaStrictAddressing: true });
|
|
58
|
+
seed(settings, false);
|
|
59
|
+
const text = settingsCtx.text(withPreset("zephyr"));
|
|
60
|
+
assert.ok(!text.includes("泽菲尔"), "关掉的卡本身不注入");
|
|
61
|
+
assert.ok(!text.includes("Amarre"), "关掉之后落回 default 等于没关");
|
|
62
|
+
store.close();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("strict on:解析不到身份(无 session.header)照旧用 default 卡", () => {
|
|
66
|
+
const { store, settings, settingsCtx } = setup({ personaEnabled: true, personaStrictAddressing: true });
|
|
67
|
+
seed(settings);
|
|
68
|
+
const text = settingsCtx.text({});
|
|
69
|
+
assert.ok(text.includes("你的人格名字:Amarre"), "无名会话正是 default 卡的受众,strict 不许把它一起关掉");
|
|
70
|
+
store.close();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("strict on:命中的卡照常注入,画像与规则不受影响", () => {
|
|
74
|
+
const { store, settings, settingsCtx } = setup({ personaEnabled: true, personaStrictAddressing: true });
|
|
75
|
+
seed(settings);
|
|
76
|
+
settings.setProfile("用户是规划背景");
|
|
77
|
+
settings.setRules(["不提及用户名"]);
|
|
78
|
+
const hit = settingsCtx.text(withPreset("zephyr"));
|
|
79
|
+
assert.ok(hit.includes("你的人格名字:泽菲尔"), "身份命中时行为与现状一致");
|
|
80
|
+
assert.ok(hit.includes("- 用户画像:用户是规划背景"), "画像恒注入:本开关只管人格行");
|
|
81
|
+
assert.ok(hit.includes("- 规则:不提及用户名"), "规则恒注入:同上");
|
|
82
|
+
store.close();
|
|
83
|
+
});
|
|
@@ -7,6 +7,7 @@ import test from "node:test";
|
|
|
7
7
|
import assert from "node:assert/strict";
|
|
8
8
|
import { createStore } from "../src/store.js";
|
|
9
9
|
import { createSettings } from "../src/settings.js";
|
|
10
|
+
import { resolvePersonaCard } from "../src/user-settings.js";
|
|
10
11
|
|
|
11
12
|
function setup() {
|
|
12
13
|
const store = createStore(":memory:");
|
|
@@ -81,10 +82,14 @@ test("v1 legacy single-object persona reads as default (zero migration)", () =>
|
|
|
81
82
|
store.close();
|
|
82
83
|
});
|
|
83
84
|
|
|
84
|
-
|
|
85
|
+
// 寻址口径住在 src/user-settings.js(注入与面板预览共用一份),不在 settings 里——
|
|
86
|
+
// settings 只存卡。原来那两个 getPersonaFor 用例因此改指向 resolvePersonaCard:
|
|
87
|
+
// 断言的是同一件事,但落在唯一那份实现上。
|
|
88
|
+
test("byAgent card round-trips via setPersonaFor/resolvePersonaCard", () => {
|
|
85
89
|
const { store, settings } = setup();
|
|
86
90
|
settings.setPersonaFor("zephyr", { name: "泽菲尔", personality: "冷危占有", enabled: true });
|
|
87
|
-
assert.deepEqual(settings
|
|
91
|
+
assert.deepEqual(resolvePersonaCard(settings, "zephyr").card, { name: "泽菲尔", personality: "冷危占有", enabled: true });
|
|
92
|
+
assert.equal(resolvePersonaCard(settings, "zephyr").source, "byAgent");
|
|
88
93
|
// default 仍在且未被影响
|
|
89
94
|
assert.deepEqual(settings.getPersona().default, {});
|
|
90
95
|
// 卡列表含 zephyr
|
|
@@ -92,17 +97,34 @@ test("byAgent card round-trips via setPersonaFor/getPersonaFor", () => {
|
|
|
92
97
|
store.close();
|
|
93
98
|
});
|
|
94
99
|
|
|
95
|
-
test("
|
|
100
|
+
test("缺省寻址:身份没建卡、或卡被关掉,都落 default", () => {
|
|
96
101
|
const { store, settings } = setup();
|
|
97
102
|
settings.setPersona({ name: "Amarre", enabled: true }); // 单对象形写 default
|
|
98
103
|
settings.setPersonaFor("zephyr", { name: "泽菲尔", enabled: false }); // 卡存在但关
|
|
99
|
-
|
|
100
|
-
assert.equal(settings.getPersonaFor("unknown-bot").name, "Amarre");
|
|
104
|
+
assert.equal(resolvePersonaCard(settings, "unknown-bot").card.name, "Amarre");
|
|
101
105
|
// 身份卡存在但 enabled=false → 落 default(兜底优先,不注入半张卡)
|
|
102
|
-
assert.equal(settings
|
|
106
|
+
assert.equal(resolvePersonaCard(settings, "zephyr").card.name, "Amarre");
|
|
103
107
|
// 身份缺失(null/空串)→ default
|
|
104
|
-
assert.equal(settings
|
|
105
|
-
assert.equal(settings
|
|
108
|
+
assert.equal(resolvePersonaCard(settings, null).card.name, "Amarre");
|
|
109
|
+
assert.equal(resolvePersonaCard(settings, "").card.name, "Amarre");
|
|
110
|
+
store.close();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("strict 寻址:身份没建卡与卡被关掉都不注入,无名会话仍用 default", () => {
|
|
114
|
+
const { store, settings } = setup();
|
|
115
|
+
settings.setPersona({ name: "Amarre", enabled: true });
|
|
116
|
+
settings.setPersonaFor("zephyr", { name: "泽菲尔", enabled: false });
|
|
117
|
+
const strict = { strict: true };
|
|
118
|
+
for (const id of ["unknown-bot", "zephyr"]) {
|
|
119
|
+
const { card, source } = resolvePersonaCard(settings, id, strict);
|
|
120
|
+
assert.equal(source, "none", `${id} 在严格寻址下不该拿到任何卡`);
|
|
121
|
+
assert.deepEqual(card, {});
|
|
122
|
+
}
|
|
123
|
+
// 开着的身份卡照样命中(strict 不改变命中语义)
|
|
124
|
+
settings.setPersonaFor("mentor", { name: "导师", enabled: true });
|
|
125
|
+
assert.equal(resolvePersonaCard(settings, "mentor", strict).source, "byAgent");
|
|
126
|
+
// 解析不到身份 = default 卡的受众,strict 不动这一格
|
|
127
|
+
assert.equal(resolvePersonaCard(settings, null, strict).card.name, "Amarre");
|
|
106
128
|
store.close();
|
|
107
129
|
});
|
|
108
130
|
|
|
@@ -147,3 +147,132 @@ test("坏输入不抛", () => {
|
|
|
147
147
|
assert.doesNotThrow(() => suggestRetirements(p, { runs: [{}] }));
|
|
148
148
|
assert.doesNotThrow(() => suggestRetirements(p, { runs: [{ at: null, steps: null }] }));
|
|
149
149
|
});
|
|
150
|
+
|
|
151
|
+
// #461:violationSeen 跨整本账本共享,而键里的 `run.at ?? ""` 在 at 缺失时塌缩成
|
|
152
|
+
// 空串——于是**所有**无时间戳的 run,其同 (step, need) 违规会被并成一条。去重本意是
|
|
153
|
+
// 「同一轮里同一条只报一次」,不是「所有轮共享一条」。内容不同的两条 run 该产出两条。
|
|
154
|
+
test("#461:两条内容不同、都无 run.at 的 run,同类违规不被并成一条", () => {
|
|
155
|
+
const p = makePipeline([
|
|
156
|
+
step("A", { tool: "npm test" }),
|
|
157
|
+
step("B", { tool: "git push", needs: ["A"] })
|
|
158
|
+
]);
|
|
159
|
+
// 都只做了 B、都没带 at,但走的不是同一条路:by 不同 → 内容指纹不同 → 各报一条
|
|
160
|
+
const runs = [
|
|
161
|
+
{ at: null, by: "human", triggeredBy: "demo", steps: [{ id: "B", at: "x" }] },
|
|
162
|
+
{ at: undefined, by: "agent", triggeredBy: "nightly", steps: [{ id: "B", at: "x" }] }
|
|
163
|
+
];
|
|
164
|
+
const v = suggestRetirements(p, { runs }).filter((s) => s.kind === "violation-precedence");
|
|
165
|
+
assert.equal(v.length, 2, "两条独立的 run 各报一条,不被键塌缩吞掉");
|
|
166
|
+
assert.equal(v[0].stepId, "B");
|
|
167
|
+
assert.equal(v[1].stepId, "B");
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// #507 评审:同一个 run 在账本里出现两次,同一条违规仍只报一次。
|
|
171
|
+
// 位置索引做不到这件事——它只在「本次遍历的数组顺序」里唯一,同一条 run 出现在
|
|
172
|
+
// 下标 0 和 1 时会拿到两个键。所以 at 缺失时兜底用内容指纹,不用索引。
|
|
173
|
+
test("#507:同一条 run 出现两次,同类违规仍只报一次", () => {
|
|
174
|
+
const p = makePipeline([
|
|
175
|
+
step("A", { tool: "npm test" }),
|
|
176
|
+
step("B", { tool: "git push", needs: ["A"] })
|
|
177
|
+
]);
|
|
178
|
+
const same = { at: null, by: "human", triggeredBy: "demo", steps: [{ id: "B", at: "x" }] };
|
|
179
|
+
const v = suggestRetirements(p, { runs: [same, same] }).filter((s) => s.kind === "violation-precedence");
|
|
180
|
+
assert.equal(v.length, 1, "同一条 run 的两份记录是同一个观测,不是两次违规");
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
// #507 评审(CodeRabbit):两类键必须不相撞。第 0 条缺 at,而另一条的 at 字面量
|
|
184
|
+
// 正好是 "#0"——用位置索引兜底时两者生成同一个键,后一条违规被静默吞掉。
|
|
185
|
+
test("#507:at 字面量与兜底键不相撞(第 0 条缺 at vs 另一条 at 是 \"#0\")", () => {
|
|
186
|
+
const p = makePipeline([
|
|
187
|
+
step("A", { tool: "npm test" }),
|
|
188
|
+
step("B", { tool: "git push", needs: ["A"] })
|
|
189
|
+
]);
|
|
190
|
+
const runs = [
|
|
191
|
+
{ at: null, by: "human", triggeredBy: "demo", steps: [{ id: "B", at: "x" }] },
|
|
192
|
+
{ at: "#0", by: "human", triggeredBy: "demo", steps: [{ id: "B", at: "x" }] }
|
|
193
|
+
];
|
|
194
|
+
const v = suggestRetirements(p, { runs }).filter((s) => s.kind === "violation-precedence");
|
|
195
|
+
assert.equal(v.length, 2, "字面量 #0 与兜底键不是同一个东西,两条都要报");
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// at 存在时行为不变:同 at 的重复违规仍然只报一条(原有去重语义不能被修坏)。
|
|
199
|
+
test("#461:at 存在时同轮重复违规仍只报一条(回归保护)", () => {
|
|
200
|
+
const p = makePipeline([
|
|
201
|
+
step("A", { tool: "npm test" }),
|
|
202
|
+
step("B", { tool: "git push", needs: ["A"] })
|
|
203
|
+
]);
|
|
204
|
+
const runs = [
|
|
205
|
+
RUN("2026-10-17T00:00:00Z", [{ id: "B", at: "x" }]),
|
|
206
|
+
RUN("2026-10-17T00:00:00Z", [{ id: "B", at: "x" }])
|
|
207
|
+
];
|
|
208
|
+
const v = suggestRetirements(p, { runs }).filter((s) => s.kind === "violation-precedence");
|
|
209
|
+
assert.equal(v.length, 1, "同一 at 的同一违规仍然只报一次");
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
// #507(CodeRabbit):匿名指纹必须收 step 的 at。两条都缺 run.at、只在 steps[0].at 上
|
|
213
|
+
// 不同的 run 是两次不同的观测;漏了 at 会让它们共键,后一条的 violation-precedence 被
|
|
214
|
+
// 去重吞掉。修复前(只取 step.id)报 1 条,修复后(收 at)报 2 条——这是本条的 red→green。
|
|
215
|
+
test("#507:匿名指纹收 step.at——只差 steps[0].at 的两条 at-less run 各报一条", () => {
|
|
216
|
+
const p = makePipeline([
|
|
217
|
+
step("A", { tool: "npm test" }),
|
|
218
|
+
step("B", { tool: "git push", needs: ["A"] })
|
|
219
|
+
]);
|
|
220
|
+
const runs = [
|
|
221
|
+
{ at: null, by: "human", triggeredBy: "demo", steps: [{ id: "B", at: "2026-10-17T00:00:00Z" }] },
|
|
222
|
+
{ at: undefined, by: "human", triggeredBy: "demo", steps: [{ id: "B", at: "2026-10-18T00:00:00Z" }] }
|
|
223
|
+
];
|
|
224
|
+
const v = suggestRetirements(p, { runs }).filter((s) => s.kind === "violation-precedence");
|
|
225
|
+
assert.equal(v.length, 2, "只差步骤时间的两条 at-less run 是两次观测,不能被并成一条");
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
// #507:匿名指纹**不能**把整个 step 对象序列化进去——note 是自由文本,进去会让「只差
|
|
229
|
+
// 备注的同一条观测」被当成两次。今天没有任何测试锁这条;不加,未来重构把 note 加回来
|
|
230
|
+
// 还会全绿。这是前瞻守卫:当前修复前后都是 1,但把 note 塞进指纹就会转红(见复盘)。
|
|
231
|
+
test("#507:匿名指纹排除 note——只差 steps[0].note 的两条 at-less run 仍是同一条", () => {
|
|
232
|
+
const p = makePipeline([
|
|
233
|
+
step("A", { tool: "npm test" }),
|
|
234
|
+
step("B", { tool: "git push", needs: ["A"] })
|
|
235
|
+
]);
|
|
236
|
+
const runs = [
|
|
237
|
+
{ at: null, by: "human", triggeredBy: "demo", steps: [{ id: "B", at: "x", note: "第一次跑,网络慢" }] },
|
|
238
|
+
{ at: undefined, by: "human", triggeredBy: "demo", steps: [{ id: "B", at: "x", note: "重跑,还是慢" }] }
|
|
239
|
+
];
|
|
240
|
+
const v = suggestRetirements(p, { runs }).filter((s) => s.kind === "violation-precedence");
|
|
241
|
+
assert.equal(v.length, 1, "note 是自由文本不是身份,只差备注应仍算同一条观测");
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
// #507:锁定旗标语义。指纹覆盖归一化 step 的全部结构化字段(含 skipped/detoured/refuted),
|
|
245
|
+
// 而这三个布尔对去重正确性是**惰性**的(violation 判定只读 doneIds 与 id 是否存在,
|
|
246
|
+
// refuted 根本不被读)。代价如实记在这里:两条只差惰性旗标的 at-less run 会各报一条
|
|
247
|
+
// **逐字相同**的提议。锁成 2 + deepEqual,防止将来无声漂移;anon 路径在生产不可达
|
|
248
|
+
// (readStoredRun 强制 run.at),故此代价不落到真实账本上。
|
|
249
|
+
test("#507:只差 refuted 的两条 at-less run 给出两条逐字相同的提议(锁旗标语义)", () => {
|
|
250
|
+
const p = makePipeline([
|
|
251
|
+
step("A", { tool: "npm test" }),
|
|
252
|
+
step("B", { tool: "git push", needs: ["A"] })
|
|
253
|
+
]);
|
|
254
|
+
const runs = [
|
|
255
|
+
{ at: null, by: "human", triggeredBy: "demo", steps: [{ id: "B", at: "x", refuted: false }] },
|
|
256
|
+
{ at: null, by: "human", triggeredBy: "demo", steps: [{ id: "B", at: "x", refuted: true }] }
|
|
257
|
+
];
|
|
258
|
+
const v = suggestRetirements(p, { runs }).filter((s) => s.kind === "violation-precedence");
|
|
259
|
+
assert.equal(v.length, 2, "refuted 在指纹里:两条只差 refuted 的 run 拿到两个键");
|
|
260
|
+
assert.deepEqual(v[0], v[1], "两条提议逐字相同——这正是把惰性旗标收进指纹的代价");
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
// #507:锁编码必须是**无歧义**序列化(JSON.stringify),不能退回手写 `${id}@${at}`。
|
|
264
|
+
// run 侧 step id 只过 isSafeSegment(src/pipeline-runs.js:83),`@` `,` `:` 都是合法 id:
|
|
265
|
+
// 手写拼接会把 [{A,t1},{B,t1},{C,t1}] 与 [{"A@t1,B",t1},{C,t1}] 撞成同一个键,run2 的
|
|
266
|
+
// (C,P) 违规被静默吞掉(只报 3 条)。当前实现与更早的 id-only 实现都报 4;退回手写拼接会红。
|
|
267
|
+
test("#507:step id 含 @/, 不撞键(锁 JSON.stringify 编码,手写拼接会漏报)", () => {
|
|
268
|
+
const p = makePipeline([
|
|
269
|
+
step("P"),
|
|
270
|
+
step("A", { needs: ["P"] }), step("B", { needs: ["P"] }), step("C", { needs: ["P"] })
|
|
271
|
+
]);
|
|
272
|
+
const runs = [
|
|
273
|
+
{ at: null, by: "human", triggeredBy: "demo", steps: [{ id: "A", at: "t1" }, { id: "B", at: "t1" }, { id: "C", at: "t1" }] },
|
|
274
|
+
{ at: null, by: "human", triggeredBy: "demo", steps: [{ id: "A@t1,B", at: "t1" }, { id: "C", at: "t1" }] }
|
|
275
|
+
];
|
|
276
|
+
const v = suggestRetirements(p, { runs }).filter((s) => s.kind === "violation-precedence");
|
|
277
|
+
assert.equal(v.length, 4, "两条 run 内容不同:run1 断 A/B/C 三条边、run2 断 C 一条,共 4 条");
|
|
278
|
+
});
|
|
@@ -45,9 +45,9 @@ function mockCtx({ onConsolidation, summaryText = "记忆库总览:用户偏
|
|
|
45
45
|
llm: {
|
|
46
46
|
async *stream(options) {
|
|
47
47
|
const userText = options.messages.find((m) => m.role === "user")?.content?.[0]?.text ?? "";
|
|
48
|
-
if (userText.
|
|
48
|
+
if (userText.includes("| type=")) {
|
|
49
49
|
yield { type: "block-start", index: 0, blockType: "text" };
|
|
50
|
-
yield { type: "text-delta", index: 0, text: onConsolidation ? onConsolidation(userText) : "[]" };
|
|
50
|
+
yield { type: "text-delta", index: 0, text: onConsolidation ? onConsolidation(userText, options.purpose) : "[]" };
|
|
51
51
|
yield { type: "block-end", index: 0, block: { type: "text" } };
|
|
52
52
|
yield { type: "finish", reason: { kind: "stop" } };
|
|
53
53
|
return;
|