@modusensus/dsh-mneme 0.8.6 → 0.8.7
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 +18 -148
- package/bin/cli.mjs +689 -603
- package/lib/api-standalone.js +57 -1
- package/lib/api.js +34 -11
- package/lib/client.js +289 -59
- package/lib/config.js +54 -6
- package/lib/content-hash.js +54 -0
- package/lib/document-index.js +94 -0
- package/lib/document.js +93 -11
- package/lib/index.js +50 -11
- package/lib/inject.js +6 -2
- package/lib/lang.js +15 -4
- package/lib/maintenance.js +142 -0
- package/lib/mirror.js +147 -15
- package/lib/recall-stats.js +52 -2
- package/lib/scope.js +34 -6
- package/lib/service.js +86 -7
- package/lib/settings.js +2 -1
- package/lib/store.js +231 -21
- package/lib/tools.js +27 -9
- package/lib/write-admission.js +259 -0
- package/package.json +4 -4
- package/src/api-standalone.js +57 -1
- package/src/api.js +34 -11
- package/src/config.js +54 -6
- package/src/content-hash.js +54 -0
- package/src/document-index.js +94 -0
- package/src/document.js +93 -11
- package/src/index.js +50 -11
- package/src/inject.js +6 -2
- package/src/lang.js +15 -4
- package/src/maintenance.js +142 -0
- package/src/mirror.js +147 -15
- package/src/recall-stats.js +52 -2
- package/src/scope.js +34 -6
- package/src/service.js +86 -7
- package/src/settings.js +2 -1
- package/src/store.js +231 -21
- package/src/tools.js +27 -9
- package/src/write-admission.js +259 -0
- package/test/api.test.js +12 -0
- package/test/client.test.js +90 -5
- package/test/content-hash.test.js +204 -0
- package/test/document-dir.test.js +233 -0
- package/test/document.test.js +151 -3
- package/test/inject-parent-gate.test.js +106 -0
- package/test/inject-pools.test.js +8 -3
- package/test/llm-audit.test.js +42 -0
- package/test/maintenance.test.js +391 -0
- package/test/mirror-types.test.js +208 -0
- package/test/recall-evals.test.js +18 -0
- package/test/recall-stats.test.js +66 -0
- package/test/write-admission.test.js +345 -0
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
// #254 写入准入(第一阶段:只计量,不拦截)。
|
|
2
|
+
//
|
|
3
|
+
// 为什么先只计量:会话写入预算的 N 与同话题冷却的 X 在没有真实分布时拍任何数字
|
|
4
|
+
// 都是误杀面(维护者 2026-09-23 拍板「先记录不拦,由遥测定」)。所以这一阶段只把
|
|
5
|
+
// 两个闸门的原始测量点写进 llm_audit_logs(status='skipped'、metadata.gate='g1'
|
|
6
|
+
// 且同话题重复时附 metadata.g2),不做判定、不改任何写入行为;第二阶段把拦截打开
|
|
7
|
+
// 时才引入阈值与二次确认(memory_save({confirm:true}))。
|
|
8
|
+
//
|
|
9
|
+
// 为什么复用 llm_audit_logs:它是仓库既有的「后台动作回执」表(bookkeeping,不
|
|
10
|
+
// 触发写钩子,见 store.js 的表注释)。第二阶段落地时「第一次 skipped/gate →
|
|
11
|
+
// 第二次 confirm:true 放行」在审计里成对出现,因果可回放。
|
|
12
|
+
//
|
|
13
|
+
// 一行一个新建行(不是一行一个信号):这样「会话内新建条数」就是按 session_key
|
|
14
|
+
// 的直接计数,不需要任何 JSON 解析;g2 命中时作为该行的 metadata.g2 附带,不做第
|
|
15
|
+
// 二行。测量点只取在新行上——并入已有行是去重机制在正常工作,既不进预算也不记行。
|
|
16
|
+
// 会话身份来自工具 exec(scope.js › sessionKeyOf):没有会话身份的写入(dream /
|
|
17
|
+
// summarize / import / organize)不进预算,预算管的是「一次对话里写飞了」。
|
|
18
|
+
//
|
|
19
|
+
// 穿透口(与 #275 拍板 5 的 pinned 豁免同口径):constraint / preference 是 #249
|
|
20
|
+
// 第一批立的逐字保真池,永不进预算;仍记一行审计(metadata.exempt='pinned')以便
|
|
21
|
+
// 观察穿透频率。穿透行既不参与 g2 判定,也不推进同话题的时间基准——它整个不在闸门
|
|
22
|
+
// 里,成为下一次比较的基准会把 g2 的样本混进穿透流量。
|
|
23
|
+
//
|
|
24
|
+
// 内容哈希(exact duplicate,第三类测量点,维护者 2026-09-24 拍板):归一化口径定在
|
|
25
|
+
// content-hash.js,命中即「同一内容又被写了一次」。判据用哈希而不是相似度——相似度
|
|
26
|
+
// 阈值在同一件事换个说法 / 不同的事共享话题词之间来回挪,只会换一种错法。候选集先按
|
|
27
|
+
// type 与 scope 三维等值收窄再比哈希(不是全表扫),并把归档行一并纳入:「出口止体积、
|
|
28
|
+
// 不止重复」,被质量闸归档的同一个事实不在 saveWithDedupe 的候选集里(store.list 默认
|
|
29
|
+
// 排除归档),同样的内容再写一次就是一条新行,这正是这条信号要量的穿透。两个穿透口与
|
|
30
|
+
// pinned 豁免同口径:pinned 在这里返回得更早,连候选集查询都不发。
|
|
31
|
+
//
|
|
32
|
+
// 信号跟着同一行审计走,所以内容哈希与 g1/g2 覆盖同一批写入(会话内新建行);无会话
|
|
33
|
+
// 身份的系统写入(dream / summarize / import / organize)要另立行形状,不在本批。
|
|
34
|
+
//
|
|
35
|
+
// 与 llmAudit.enabled 的关系:那个开关同时关掉审计行的启动期清理(index.js 的
|
|
36
|
+
// deleteOldLlmAudits),所以关掉时本模块一行都不写,否则就是在无保留期的表里做
|
|
37
|
+
// 按写入频次增长。
|
|
38
|
+
import { PINNED_MEMORY_TYPES } from "./service.js";
|
|
39
|
+
import { contentHashOf } from "./content-hash.js";
|
|
40
|
+
|
|
41
|
+
// 审计口径(llm_audit_logs 的既有列):trigger_source = 组件名,operation_type =
|
|
42
|
+
// 动作名,status='skipped' = 本行没有产生任何 LLM 花费(与 summarize 的间隔门、
|
|
43
|
+
// 尖峰门同款读法)。
|
|
44
|
+
export const ADMISSION_TRIGGER_SOURCE = "writeAdmission";
|
|
45
|
+
export const ADMISSION_OPERATION_TYPE = "write_admission";
|
|
46
|
+
export const ADMISSION_GATE_SESSION_BUDGET = "g1";
|
|
47
|
+
|
|
48
|
+
// 首次见到某会话时的回填深度:话题表由审计行重建,只回填最近这么多行。更早的
|
|
49
|
+
// 话题会漏(测量口径可接受),换来的是一次查询与有界的常驻内存。
|
|
50
|
+
export const TOPIC_LOOKBACK_ROWS = 200;
|
|
51
|
+
|
|
52
|
+
// 话题锚只收机械可判的两类:issue/PR 引用与文件路径。两条都带前缀或分隔符,误报
|
|
53
|
+
// 面小;自由文本里看不出确定性的「同话题」,硬判只会把误杀面提前引进来(#254 已
|
|
54
|
+
// 定:计量信号用确定性锚,不用相似度阈值——同一件事换个说法就掉下去,不同的事
|
|
55
|
+
// 共享话题词又顶上来,阈值怎么挪都是在两种误判之间来回)。
|
|
56
|
+
// 两处收紧都来自误报样本:`(?<![#\w])` 挡掉 Markdown 标题 `###1` 与紧跟标识符的
|
|
57
|
+
// 井号;扩展名要求首字符是字母,挡掉 `3.5/2.0` 这类比值(否则一个算式就成了话题)。
|
|
58
|
+
const ISSUE_REF = /(?<![#\w])#\d{1,7}\b/g;
|
|
59
|
+
const FILE_PATH = /(?:[\w.-]+[\\/])+[\w.-]+\.[A-Za-z][A-Za-z0-9]{0,5}\b/g;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 写入文本里的确定性话题锚:小写归一 + 路径分隔符统一为 `/` + 去重 + 排序。
|
|
63
|
+
* 同一话题在不同轮次必须逐字节相等,才能用等值比较而不是相似度比较——所以
|
|
64
|
+
* `src\service.js` 与 `src/service.js` 必须归到同一个锚。
|
|
65
|
+
* @param {{title?: string, content?: string, tags?: string[]}} memory
|
|
66
|
+
* @returns {string[]} 排序后的话题锚
|
|
67
|
+
*/
|
|
68
|
+
export function extractTopicKeys(memory) {
|
|
69
|
+
const text = [memory?.title, memory?.content, ...(Array.isArray(memory?.tags) ? memory.tags : [])]
|
|
70
|
+
.filter((s) => typeof s === "string")
|
|
71
|
+
.join("\n");
|
|
72
|
+
const keys = new Set();
|
|
73
|
+
const add = (raw) => keys.add(raw.toLowerCase().replace(/\\/g, "/"));
|
|
74
|
+
for (const m of text.matchAll(ISSUE_REF)) add(m[0]);
|
|
75
|
+
for (const m of text.matchAll(FILE_PATH)) add(m[0]);
|
|
76
|
+
return [...keys].sort();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 写入准入(#254)。返回的两个方法就是三个消费方共用的那份接口:
|
|
81
|
+
* evaluate 给出决策形状(阶段一恒 allow),record 把测量点写成审计行。
|
|
82
|
+
* 阶段二在 evaluate 里引入阈值与 confirm 分支;#249 的注入提示与 #275 的水位计数
|
|
83
|
+
* 都读同一份 decision/审计行,而不是各写一套判定。
|
|
84
|
+
*
|
|
85
|
+
* @param {object} deps
|
|
86
|
+
* @param {object} deps.store 存储层(listLlmAudits / saveLlmAudit)
|
|
87
|
+
* @param {object} [deps.config] 已解析配置(读 llmAudit.enabled)
|
|
88
|
+
* @param {object} [deps.logger]
|
|
89
|
+
* @param {() => number} [deps.now] 时钟注入(测试用)
|
|
90
|
+
*/
|
|
91
|
+
export function createWriteAdmission({ store, config, logger, now = Date.now } = {}) {
|
|
92
|
+
// sessionKey → Map<话题锚, 最近一次新建行时刻(ms)>。真相源是审计行,这里只是它的
|
|
93
|
+
// 增量视图:进程重启后按需回填。FIFO 上限防长驻进程内存无界(同 store.js 的
|
|
94
|
+
// embedding 解析缓存口径)。
|
|
95
|
+
const topicTables = new Map();
|
|
96
|
+
const SESSION_CACHE_MAX = 200;
|
|
97
|
+
|
|
98
|
+
const warn = (msg) => {
|
|
99
|
+
// 计量是旁路:任何一环失败都不能影响写入,日志自身故障也一样。
|
|
100
|
+
try {
|
|
101
|
+
logger?.warn?.(msg);
|
|
102
|
+
} catch { /* ignore */ }
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/** 某会话的话题表(首见时从审计行回填,之后走内存增量)。 */
|
|
106
|
+
function topicTable(sessionKey) {
|
|
107
|
+
const cached = topicTables.get(sessionKey);
|
|
108
|
+
if (cached) return cached;
|
|
109
|
+
const table = new Map();
|
|
110
|
+
try {
|
|
111
|
+
const rows = store?.listLlmAudits?.({ sessionKey, limit: TOPIC_LOOKBACK_ROWS }) ?? [];
|
|
112
|
+
for (const row of rows) {
|
|
113
|
+
const metadata = row?.metadata;
|
|
114
|
+
if (!metadata || typeof metadata !== "object" || !Array.isArray(metadata.topics)) continue;
|
|
115
|
+
// 穿透行(pinned)整个不在闸门里,也就不该进基准表——只挡内存路径不挡回填,
|
|
116
|
+
// 重启后口径就变了(同一个话题会因为一条穿透行而被判成重复)。
|
|
117
|
+
if (metadata.exempt) continue;
|
|
118
|
+
const at = Date.parse(row.timestamp);
|
|
119
|
+
// 坏时间戳跳过:落到 0 会让 gap 变成几十年,一个离群值就能带偏按分位定的 X。
|
|
120
|
+
if (!Number.isFinite(at)) continue;
|
|
121
|
+
for (const topic of metadata.topics) {
|
|
122
|
+
// 审计行按时间倒序返回,先到的那条就是该话题最近一次写入——后来的不覆盖。
|
|
123
|
+
if (!table.has(topic)) table.set(topic, at);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
} catch (e) {
|
|
127
|
+
// 回填失败降级为「本会话此前的话题不可见」:漏一个 g2 测量点,不影响写入。
|
|
128
|
+
warn(`[dsh-mneme] write admission topic backfill failed: ${String(e)}`);
|
|
129
|
+
}
|
|
130
|
+
if (topicTables.size >= SESSION_CACHE_MAX && !topicTables.has(sessionKey)) {
|
|
131
|
+
topicTables.delete(topicTables.keys().next().value);
|
|
132
|
+
}
|
|
133
|
+
topicTables.set(sessionKey, table);
|
|
134
|
+
return table;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 内容哈希候选集查询(#254 第三类测量点)。失败降级为「没命中」:漏一个测量点,
|
|
139
|
+
* 不影响写入,也不让计量反噬。
|
|
140
|
+
* 多条命中时的取舍(活区优先):有活跃/未遗忘的命中就报它(判据是「去重候选集本该
|
|
141
|
+
* 拦住」),只有归档/遗忘行命中才报它们。store 侧已把活跃行排在窗口头部,这里的 find
|
|
142
|
+
* 是防御性重复:排序口径将来再变,本判据也不跟着变。
|
|
143
|
+
* archived 与 forgotten 分开回:两者都是「出口」,但穿透含义不同——只命中已遗忘行时
|
|
144
|
+
* 若只回 archived=false,读审计的人会以为库里有活跃重复;而 saveWithDedupe 的候选集
|
|
145
|
+
* 本来就排除遗忘行(store.list 默认 includeForgotten=false),这类命中同样是穿透。
|
|
146
|
+
* @returns {{memory_id: string, archived: boolean, forgotten: boolean}|null}
|
|
147
|
+
*/
|
|
148
|
+
function lookupContentDup(memory) {
|
|
149
|
+
try {
|
|
150
|
+
const hash = contentHashOf(memory);
|
|
151
|
+
if (!hash) return null;
|
|
152
|
+
const hits = store?.findContentHashMatches?.({
|
|
153
|
+
type: memory?.type,
|
|
154
|
+
hash,
|
|
155
|
+
agent_scope: memory?.agent_scope,
|
|
156
|
+
workspace_scope: memory?.workspace_scope,
|
|
157
|
+
sensitivity: memory?.sensitivity
|
|
158
|
+
}) ?? [];
|
|
159
|
+
const hit = hits.find((h) => !h.archived && !h.forgotten) ?? hits[0];
|
|
160
|
+
return hit ? { memory_id: hit.id, archived: hit.archived === true, forgotten: hit.forgotten === true } : null;
|
|
161
|
+
} catch (e) {
|
|
162
|
+
warn(`[dsh-mneme] write admission content hash lookup failed: ${String(e)}`);
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* 阶段一决策:恒 allow(本模块里没有阈值),只算出三个闸门的测量点。返回形状一次
|
|
169
|
+
* 定死,阶段二只往里加分支、不改字段:
|
|
170
|
+
* decision — "allow" | "confirm"(阶段一只可能 allow)
|
|
171
|
+
* gate — "g1" | null(null = 这次写入不进预算,不记行)
|
|
172
|
+
* topics — 本次写入的确定性话题锚
|
|
173
|
+
* repeat — {topic, gapMs} | null(g2 的命中面)
|
|
174
|
+
* dup — {memory_id, archived, forgotten} | null(内容哈希的命中面)
|
|
175
|
+
* exempt — null | "pinned"
|
|
176
|
+
* @param {{memory: object, sessionKey?: string|null}} input
|
|
177
|
+
*/
|
|
178
|
+
function evaluate({ memory, sessionKey } = {}) {
|
|
179
|
+
const verdict = { decision: "allow", gate: null, topics: [], repeat: null, dup: null, exempt: null };
|
|
180
|
+
// 无会话身份 = 系统写入(dream / summarize / import / organize),不进预算。
|
|
181
|
+
if (!sessionKey) return verdict;
|
|
182
|
+
// 审计关掉时不记行、也不推进话题表(见文件头:那个开关连启动期清理一起关掉)。
|
|
183
|
+
if (config?.llmAudit?.enabled === false) return verdict;
|
|
184
|
+
const topics = extractTopicKeys(memory);
|
|
185
|
+
const pinned = PINNED_MEMORY_TYPES.has(String(memory?.type ?? ""));
|
|
186
|
+
if (pinned) return { ...verdict, gate: ADMISSION_GATE_SESSION_BUDGET, topics, exempt: "pinned" };
|
|
187
|
+
const dup = lookupContentDup(memory);
|
|
188
|
+
const table = topicTable(sessionKey);
|
|
189
|
+
const at = now();
|
|
190
|
+
let repeat = null;
|
|
191
|
+
for (const topic of topics) {
|
|
192
|
+
if (!table.has(topic)) continue;
|
|
193
|
+
const gapMs = Math.max(0, at - table.get(topic));
|
|
194
|
+
// 同一行可能命中多个锚:取最近的那次(间隔最短=最有价值的那次重复)。
|
|
195
|
+
if (!repeat || gapMs < repeat.gapMs) repeat = { topic, gapMs };
|
|
196
|
+
}
|
|
197
|
+
return { ...verdict, gate: ADMISSION_GATE_SESSION_BUDGET, topics, repeat, dup };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* 把测量点落成审计行(一行一个新建行)。三个信号的读法:
|
|
202
|
+
* g1 会话写入预算:`GROUP BY session_key` 计数即得「会话内新建条数」分布;要剔
|
|
203
|
+
* 掉穿透行(pinned 不进预算)就加 `json_extract(metadata,'$.exempt') IS NULL`。
|
|
204
|
+
* g2 同话题冷却:`json_extract(metadata,'$.g2.gap_ms')` 即得「同话题新建行间隔」
|
|
205
|
+
* 分布。间隔算的是同一话题两次**新建行**之间——标题命中走并入的那次不在这里
|
|
206
|
+
* (并入正是冷却要做的事,已经做到了)。
|
|
207
|
+
* dup 内容哈希:`json_extract(metadata,'$.dup.memory_id')` 非空即「这条新行的内容
|
|
208
|
+
* 与某条既有行归一化后逐字节相同」;`$.dup.archived` 区分命中那条在活区还是
|
|
209
|
+
* 归档区——归档区命中就是去重候选集漏掉的那一类穿透。
|
|
210
|
+
* 计量绝不影响写入:任何异常只 warn。
|
|
211
|
+
* @returns {object[]} 落下的审计行(无测量点或失败时为空数组)
|
|
212
|
+
*/
|
|
213
|
+
function record({ sessionKey, verdict, memoryId } = {}) {
|
|
214
|
+
if (!sessionKey || !verdict || verdict.gate == null) return [];
|
|
215
|
+
const at = now();
|
|
216
|
+
const topics = Array.isArray(verdict.topics) ? verdict.topics : [];
|
|
217
|
+
const rows = [];
|
|
218
|
+
try {
|
|
219
|
+
rows.push(store.saveLlmAudit({
|
|
220
|
+
timestamp: new Date(at).toISOString(),
|
|
221
|
+
trigger_source: ADMISSION_TRIGGER_SOURCE,
|
|
222
|
+
operation_type: ADMISSION_OPERATION_TYPE,
|
|
223
|
+
// 不是模型调用,但该列 NOT NULL——用显式占位串,免得被当成某条路由的
|
|
224
|
+
// 花费来源统计进去。
|
|
225
|
+
model_id: "-",
|
|
226
|
+
status: "skipped",
|
|
227
|
+
related_memory_ids: memoryId ? [memoryId] : [],
|
|
228
|
+
session_key: sessionKey,
|
|
229
|
+
metadata: {
|
|
230
|
+
gate: verdict.gate,
|
|
231
|
+
topics,
|
|
232
|
+
...(verdict.exempt ? { exempt: verdict.exempt } : {}),
|
|
233
|
+
...(verdict.repeat ? { g2: { topic: verdict.repeat.topic, gap_ms: verdict.repeat.gapMs } } : {}),
|
|
234
|
+
// dup 两个出口分字段落盘:archived 与 forgotten 是两类不同的穿透,合成一个布尔
|
|
235
|
+
// 会让「只剩遗忘行命中」读起来像「库里有活跃重复」(见 lookupContentDup 注释)。
|
|
236
|
+
...(verdict.dup
|
|
237
|
+
? { dup: {
|
|
238
|
+
memory_id: verdict.dup.memory_id,
|
|
239
|
+
archived: verdict.dup.archived === true,
|
|
240
|
+
forgotten: verdict.dup.forgotten === true
|
|
241
|
+
} }
|
|
242
|
+
: {})
|
|
243
|
+
}
|
|
244
|
+
}));
|
|
245
|
+
} catch (e) {
|
|
246
|
+
// 审计失败只 warn(同 saveLlmAudit 的调用惯例),写入本身照常完成。
|
|
247
|
+
warn(`[dsh-mneme] write admission audit failed: ${String(e)}`);
|
|
248
|
+
}
|
|
249
|
+
// 只有真正落库、且不是穿透口的行才推进话题表:审计写失败时内存视图不能跑在审计
|
|
250
|
+
// 前面(重启回填会得到另一条时间线),pinned 写入整个不在闸门里。
|
|
251
|
+
if (rows.length > 0 && !verdict.exempt) {
|
|
252
|
+
const table = topicTable(sessionKey);
|
|
253
|
+
for (const topic of topics) table.set(topic, at);
|
|
254
|
+
}
|
|
255
|
+
return rows;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return { evaluate, record };
|
|
259
|
+
}
|
package/test/api.test.js
CHANGED
|
@@ -1010,6 +1010,18 @@ test("GET /api/dsh-mneme/export markdown feeds straight back through import", as
|
|
|
1010
1010
|
const md = res.body;
|
|
1011
1011
|
assert.ok(md.includes("- **ID**: `" + a.memory.id + "`"), "anchor line present (readHumanEdits-compatible)");
|
|
1012
1012
|
|
|
1013
|
+
// 导出是一个文档(#278 审查 F1/F3):frontmatter 只能有一份并在最前,分节不带
|
|
1014
|
+
// 各自的文件头;而且它的覆盖声明必须与正文条目数一致——「说一套写一套」比没有
|
|
1015
|
+
// 这个字段更糟,外部工具会照着它聚合。
|
|
1016
|
+
assert.equal([...md.matchAll(/^type: /gm)].length, 1, "只有一个 frontmatter 的 type 键");
|
|
1017
|
+
assert.match(md, /^type: memory-export$/m);
|
|
1018
|
+
assert.match(md, /^coverage: all$/m, "导出含归档/已遗忘行,声明要说实话");
|
|
1019
|
+
assert.equal(
|
|
1020
|
+
Number(md.match(/^covered: (\d+)$/m)[1]),
|
|
1021
|
+
(md.match(/^- \*\*ID\*\*: /gm) ?? []).length,
|
|
1022
|
+
"covered 必须等于实际导出的条目数"
|
|
1023
|
+
);
|
|
1024
|
+
|
|
1013
1025
|
// 黄金用例:导出文本原样导入 → 解析出全部条目,且字段无漂移
|
|
1014
1026
|
const back = new FakeRes();
|
|
1015
1027
|
await importRoute.handler(req("/api/dsh-mneme/import", "POST", { type: "preference", markdown: md }), back);
|
package/test/client.test.js
CHANGED
|
@@ -25,6 +25,15 @@ test("client bundle is lib-only with no src counterpart", () => {
|
|
|
25
25
|
assert.equal(existsSync(join(root, "lib/client.js")), true, "lib/client.js must exist");
|
|
26
26
|
});
|
|
27
27
|
|
|
28
|
+
// 归档侧第五指标(#275)自己按「归档区为空就整段省略」门控,但整卡还有一道 hasData 门:
|
|
29
|
+
// 那道门只认召回回执与活跃僵尸行时,「全归档 + 窗口内无回执」的库会直接 return null,
|
|
30
|
+
// 指标永远不显示(自动评审 #312 指出的回归)。这里锁死 hasData 必须把 archive.total 计进来。
|
|
31
|
+
test("status card: hasData counts the archive metric", () => {
|
|
32
|
+
const m = clientSource.match(/const hasData =[^;]+;/);
|
|
33
|
+
assert.ok(m, "the status card must declare hasData");
|
|
34
|
+
assert.match(m[0], /d\.archive\?\.total/, "hasData must count archive.total, not just runs/active rows");
|
|
35
|
+
});
|
|
36
|
+
|
|
28
37
|
// The memory entry lives at the sidebar foot, not in the settings modal: the
|
|
29
38
|
// migration must register into `sidebar.footer.action` (the list slot the
|
|
30
39
|
// sidebar shell renders beside Settings) and must not keep a `settings.section`
|
|
@@ -208,8 +217,8 @@ test("entry tracks native class, inherits native sizing, and lifts the toolbar d
|
|
|
208
217
|
"the entry must use the native button width, which reserves its horizontal margins"
|
|
209
218
|
);
|
|
210
219
|
assert.ok(
|
|
211
|
-
clientSource.includes(".mneme-topentry{display:flex;flex-direction:column}"),
|
|
212
|
-
"the entry wrapper must stretch the native button within a column flex layout"
|
|
220
|
+
clientSource.includes(".mneme-topentry{display:flex;flex-direction:column;position:relative}"),
|
|
221
|
+
"the entry wrapper must stretch the native button within a column flex layout (position:relative hosts the #177 badge)"
|
|
213
222
|
);
|
|
214
223
|
assert.equal(
|
|
215
224
|
clientSource.includes(".mneme-topentry-native .mneme-topentry-label{flex:1;min-width:0;text-align:left;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}"),
|
|
@@ -313,6 +322,35 @@ test("graph toggle uses a node-graph glyph, not the share icon", () => {
|
|
|
313
322
|
);
|
|
314
323
|
});
|
|
315
324
|
|
|
325
|
+
// #287 跨代图标守卫:primitives 在 0.1.7 把归档图标从「像素后缀」改名成「字重后缀」
|
|
326
|
+
// (IconArchiveOutline20 → …OutlineRegular / …OutlineMedium),旧名不留别名,而
|
|
327
|
+
// peerDependencies 仍同时覆盖 0.1.6 与 0.1.7 两代宿主。只认某一代的名字,另一代就会
|
|
328
|
+
// 把 undefined 交给 h(),落成 slot entry 里的 React #130(该 entry 整块崩),因此必须
|
|
329
|
+
// 探测两代名字、并在全缺时降级为不渲染图标。
|
|
330
|
+
test("#287: archive glyph probes both naming generations and degrades to no glyph", () => {
|
|
331
|
+
assert.ok(
|
|
332
|
+
/primitives\.IconArchiveOutline20\s*\?\?\s*primitives\.IconArchiveOutlineRegular\s*\?\?\s*primitives\.IconArchiveOutlineMedium/.test(clientSource),
|
|
333
|
+
"the glyph must probe the 0.1.6 pixel name before the 0.1.7 weight names"
|
|
334
|
+
);
|
|
335
|
+
assert.equal(
|
|
336
|
+
/h\(IconArchiveOutline20/.test(clientSource),
|
|
337
|
+
false,
|
|
338
|
+
"the raw constant must never reach h() — on a host lacking that name it renders undefined (React #130)"
|
|
339
|
+
);
|
|
340
|
+
assert.ok(
|
|
341
|
+
/const renderArchiveIcon = \(props\) => \(IconArchive \? h\(IconArchive, props\) : null\)/.test(clientSource),
|
|
342
|
+
"the glyph must render through a null-guarded helper so a missing icon degrades to nothing"
|
|
343
|
+
);
|
|
344
|
+
for (const site of [
|
|
345
|
+
"renderArchiveIcon({ size: 15 })", // 浮层标题栏
|
|
346
|
+
"renderArchiveIcon({ size: wide ? 16 : 18 })", // 侧栏 trigger
|
|
347
|
+
"renderArchiveIcon({ size: wide ? 15 : 18 })", // portal 到宿主原生侧栏的入口
|
|
348
|
+
"renderArchiveIcon({ size })" // better-sidebar tab 图标
|
|
349
|
+
]) {
|
|
350
|
+
assert.ok(clientSource.includes(site), `every render site must go through the helper: ${site}`);
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
|
|
316
354
|
// 方案 A:查询收敛。状态页只做仪表盘(小页预览 + 服务端 total + 查看全部),
|
|
317
355
|
// 沉淀/归档的完整浏览走记忆库的 deposited/archived 筛选视图(chip 预置 +
|
|
318
356
|
// 状态页入口跳转),详情抽屉给归档记忆一个反向的「恢复」。
|
|
@@ -665,7 +703,7 @@ test("settings feedback card: prefilled issue + mailto + browse, version from /i
|
|
|
665
703
|
);
|
|
666
704
|
// 2. GitHub 新建 issue:issues/new?title=&body= 预填环境信息(当前仓库无模板)
|
|
667
705
|
assert.ok(
|
|
668
|
-
clientSource.includes("https://github.com/slow-stack/
|
|
706
|
+
clientSource.includes("https://github.com/slow-stack/mneme/issues/new?title="),
|
|
669
707
|
"the issue link must prefill title+body on issues/new"
|
|
670
708
|
);
|
|
671
709
|
assert.ok(
|
|
@@ -679,7 +717,7 @@ test("settings feedback card: prefilled issue + mailto + browse, version from /i
|
|
|
679
717
|
);
|
|
680
718
|
// 4. 浏览已知问题:跳仓库 issues 列表页(去重前置步骤)
|
|
681
719
|
assert.ok(
|
|
682
|
-
clientSource.includes('href: "https://github.com/slow-stack/
|
|
720
|
+
clientSource.includes('href: "https://github.com/slow-stack/mneme/issues"'),
|
|
683
721
|
"the browse link must open the repo issues list"
|
|
684
722
|
);
|
|
685
723
|
// 5. 双语 i18n
|
|
@@ -844,8 +882,55 @@ test("a11y: entity graph svg exposes a count summary", () => {
|
|
|
844
882
|
}
|
|
845
883
|
});
|
|
846
884
|
|
|
847
|
-
// --- issue #
|
|
885
|
+
// --- issue #177:冲突队列交互与视觉重做 ---
|
|
848
886
|
|
|
887
|
+
// 相似度进度条:只从「相似度 X / similarity X」锚定短语回收数字(防把年份当
|
|
888
|
+
// 相似度),A/B 卡片带侧色类,冻结徽章替代预裁决阶段的「已归档」徽章。
|
|
889
|
+
test("conflict queue: similarity bar, side colors and frozen badge", () => {
|
|
890
|
+
assert.ok(clientSource.includes("similarityOf(it.reason)"), "reason must be parsed for similarity");
|
|
891
|
+
assert.ok(clientSource.includes("/(?:相似度|similarity)\\s*([01](?:\\.\\d+)?)/i"), "parse must anchor on the similarity phrase");
|
|
892
|
+
for (const key of ["memory.status.conflictQueue.similarity", "memory.status.conflictQueue.frozenBadge"]) {
|
|
893
|
+
const occurrences = clientSource.split(`"${key}"`).length - 1;
|
|
894
|
+
assert.ok(occurrences >= 2, `i18n key ${key} must exist in both zh and en (got ${occurrences})`);
|
|
895
|
+
}
|
|
896
|
+
assert.ok(clientSource.includes('"mneme-conflict-side--a"'), "side A must carry the A color class");
|
|
897
|
+
assert.ok(clientSource.includes('"mneme-conflict-side--b"'), "side B must carry the B color class");
|
|
898
|
+
assert.ok(!/s\.archived && h\("span", \{ className: "mneme-badge mneme-badge--archived"/.test(clientSource),
|
|
899
|
+
"the pre-ruling archived badge must be replaced by the frozen badge");
|
|
900
|
+
assert.ok(clientSource.includes("mneme-badge--frozen"), "frozen badge class must exist");
|
|
901
|
+
});
|
|
902
|
+
|
|
903
|
+
// 词级 diff:LCS 对齐是纯函数,锁它的存在、上限护栏与渲染接线(无损性由
|
|
904
|
+
// difftest 独立验证,此处只锁形状)。
|
|
905
|
+
test("conflict queue: word diff is wired with size guards", () => {
|
|
906
|
+
assert.ok(clientSource.includes("function wordDiff(aText, bText)"), "wordDiff must exist");
|
|
907
|
+
assert.ok(clientSource.includes("mneme-conflict-mark--del"), "del highlight class must exist");
|
|
908
|
+
assert.ok(clientSource.includes("mneme-conflict-mark--ins"), "ins highlight class must exist");
|
|
909
|
+
assert.ok(clientSource.includes("n > 800 || m > 800"), "DP must bail on oversized inputs");
|
|
910
|
+
});
|
|
911
|
+
|
|
912
|
+
// 空态教育卡:0 冲突时也渲染(原来整块 return null),解释冻结是什么。
|
|
913
|
+
test("conflict queue: empty-state explainer card", () => {
|
|
914
|
+
assert.ok(!/if \(!items \|\| items\.length === 0\) return null;/.test(clientSource),
|
|
915
|
+
"empty queue must render the explainer instead of nothing");
|
|
916
|
+
for (const key of ["memory.status.conflictQueue.emptyTitle", "memory.status.conflictQueue.emptyBody"]) {
|
|
917
|
+
const occurrences = clientSource.split(`"${key}"`).length - 1;
|
|
918
|
+
assert.ok(occurrences >= 2, `i18n key ${key} must exist in both zh and en (got ${occurrences})`);
|
|
919
|
+
}
|
|
920
|
+
});
|
|
921
|
+
|
|
922
|
+
// 状态卡待处理高亮 + 侧边栏入口 badge(挂 portal 与回退两个按钮)。
|
|
923
|
+
test("conflict queue: actionable status card and sidebar entry badge", () => {
|
|
924
|
+
assert.ok(clientSource.includes('"mneme-conflict-jump"'), "pending>0 card must carry the highlight class");
|
|
925
|
+
assert.ok(clientSource.includes("mneme-statuscard--actionable"), "highlight CSS must exist");
|
|
926
|
+
assert.ok((clientSource.match(/h\(ConflictBadge, \{ pending \}\)/g) || []).length === 2,
|
|
927
|
+
"badge must be wired into both entry buttons");
|
|
928
|
+
assert.ok((clientSource.match(/= useConflictBadgeCount\(t\)/g) || []).length === 2,
|
|
929
|
+
"both entry components must subscribe to the badge count");
|
|
930
|
+
assert.ok(clientSource.includes('"/api/dsh-mneme/dream-status"'), "badge must reuse the dream-status endpoint");
|
|
931
|
+
});
|
|
932
|
+
|
|
933
|
+
// --- issue #179:注入预览(状态页卡片 + /inject-preview 端点透传的旁路快照) ---
|
|
849
934
|
test("a11y+preview: inject preview card is wired on the status tab", () => {
|
|
850
935
|
assert.ok(clientSource.includes('"/api/dsh-mneme/inject-preview"'), "card must fetch the preview endpoint");
|
|
851
936
|
assert.ok(clientSource.includes("h(InjectPreviewCard, { t })"), "status grid must render the preview card");
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { DatabaseSync } from "node:sqlite";
|
|
4
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { createStore } from "../src/store.js";
|
|
8
|
+
import { contentHashOf, normalizeForHash } from "../src/content-hash.js";
|
|
9
|
+
|
|
10
|
+
// #254 内容哈希(写入准入的确定性计量锚)。
|
|
11
|
+
// 这批用例锁三件事:归一化口径(只差格式不算两次)、派生列随内容走(insert/update/
|
|
12
|
+
// CAS 三处都得重算,漏一处标记就在说谎)、以及老库打开时的迁移顺序(索引必须在加列
|
|
13
|
+
// 之后建,否则 exec(SCHEMA) 直接报 no such column,插件整树加载失败)。
|
|
14
|
+
|
|
15
|
+
test("归一化口径:大小写/空白/标点/全角差异落在同一个键上", () => {
|
|
16
|
+
assert.equal(normalizeForHash("Hello, World!\n"), "hello world");
|
|
17
|
+
assert.equal(normalizeForHash("hello world"), "hello world", "NFKC 折全角");
|
|
18
|
+
assert.equal(
|
|
19
|
+
contentHashOf({ title: "Task A", content: "Line1.\n\nLine2" }),
|
|
20
|
+
contentHashOf({ title: "task a", content: "line1 line2" })
|
|
21
|
+
);
|
|
22
|
+
// 分隔符防拼接歧义:('ab','c') 与 ('a','bc') 不能同键
|
|
23
|
+
assert.notEqual(contentHashOf({ title: "ab", content: "c" }), contentHashOf({ title: "a", content: "bc" }));
|
|
24
|
+
assert.notEqual(contentHashOf({ title: "t", content: "同一件事" }), contentHashOf({ title: "t", content: "另一件事" }));
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("归一化口径:同义词与词序不折叠(哈希只认逐字节相同)", () => {
|
|
28
|
+
assert.notEqual(contentHashOf({ title: "t", content: "a b" }), contentHashOf({ title: "t", content: "b a" }));
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("已知等价类:去标点让 3.5 与 35 同键(口径选择的结果,不是判据出错)", () => {
|
|
32
|
+
// 这条把「已知会误报」的样子钉住,防止有人把文件头的「命中即精确重复」当承诺、
|
|
33
|
+
// 进而以为这是个 bug 去改归一化——改口径要带存量重算(见 content-hash.js 文件头)。
|
|
34
|
+
assert.equal(contentHashOf({ title: "t", content: "版本 3.5" }), contentHashOf({ title: "t", content: "版本 35" }));
|
|
35
|
+
assert.equal(contentHashOf({ title: "t", content: "a-b" }), contentHashOf({ title: "t", content: "ab" }));
|
|
36
|
+
// 但数字本身没被折叠:不同数值不落同一键
|
|
37
|
+
assert.notEqual(contentHashOf({ title: "t", content: "3.5" }), contentHashOf({ title: "t", content: "3.6" }));
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("空标题空正文没有可比内容 → null(不互相匹配成「全是重复」)", () => {
|
|
41
|
+
assert.equal(contentHashOf({ title: "", content: "" }), null);
|
|
42
|
+
assert.equal(contentHashOf({ title: " ", content: "\n\t" }), null);
|
|
43
|
+
assert.equal(contentHashOf({}), null);
|
|
44
|
+
assert.ok(contentHashOf({ title: "只有标题" }));
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("save 落 content_hash,findContentHashMatches 按 type + scope 收窄", () => {
|
|
48
|
+
const store = createStore(":memory:");
|
|
49
|
+
const a = store.save({ type: "project", title: "同一件事", content: "正文", agent_scope: "A" });
|
|
50
|
+
assert.equal(a.content_hash, contentHashOf({ title: "同一件事", content: "正文" }));
|
|
51
|
+
|
|
52
|
+
const sameScope = store.findContentHashMatches({ type: "project", hash: a.content_hash, agent_scope: "A" });
|
|
53
|
+
assert.deepEqual(sameScope.map((m) => m.id), [a.id]);
|
|
54
|
+
|
|
55
|
+
// 跨 scope 永不互判(与 saveWithDedupe 的三维去重键同口径)
|
|
56
|
+
const otherScope = store.findContentHashMatches({ type: "project", hash: a.content_hash, agent_scope: "B" });
|
|
57
|
+
assert.equal(otherScope.length, 0);
|
|
58
|
+
const unannotated = store.findContentHashMatches({ type: "project", hash: a.content_hash });
|
|
59
|
+
assert.equal(unannotated.length, 0, "已标注行不与未标注行匹配");
|
|
60
|
+
|
|
61
|
+
// 入参先按 store 自己的口径归一,免得调用方传 " A " 就漏配
|
|
62
|
+
assert.equal(store.findContentHashMatches({ type: "project", hash: a.content_hash, agent_scope: " A " }).length, 1);
|
|
63
|
+
// type 不同不互判
|
|
64
|
+
assert.equal(store.findContentHashMatches({ type: "preference", hash: a.content_hash, agent_scope: "A" }).length, 0);
|
|
65
|
+
store.close();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("归档行在候选集内(#275 分界:出口止体积、不止重复)", () => {
|
|
69
|
+
const store = createStore(":memory:");
|
|
70
|
+
const saved = store.save({ type: "project", title: "被归档的事实", content: "同样的正文" });
|
|
71
|
+
store.setArchived(saved.id, true);
|
|
72
|
+
const hits = store.findContentHashMatches({ type: "project", hash: saved.content_hash });
|
|
73
|
+
assert.deepEqual(hits, [{ id: saved.id, archived: true, forgotten: false }]);
|
|
74
|
+
store.close();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("同键命中超过窗口宽度时,活跃行仍在窗口内(归档动作会顶 updated_at)", () => {
|
|
78
|
+
// 回归(自动评审 #311 store.js:1055):LIMIT 先于调用方的「活区优先」执行。若只按
|
|
79
|
+
// updated_at 倒序,第 11 条起写下的归档行会把更早的活跃行挤出窗口,调用方只能看见
|
|
80
|
+
// 归档命中,把「活跃重复」错记成「归档重复」——正是 dup 信号要分流的两类。
|
|
81
|
+
const store = createStore(":memory:");
|
|
82
|
+
const live = store.save({ type: "project", title: "窗口内", content: "同一段正文" });
|
|
83
|
+
assert.ok(live.content_hash, "写入即落 content_hash");
|
|
84
|
+
const sinks = [];
|
|
85
|
+
for (let i = 0; i < 10; i++) {
|
|
86
|
+
sinks.push(store.save({ type: "project", title: "窗口内", content: "同一段正文" }));
|
|
87
|
+
}
|
|
88
|
+
for (const row of sinks) store.setArchived(row.id, true);
|
|
89
|
+
const hits = store.findContentHashMatches({ type: "project", hash: live.content_hash });
|
|
90
|
+
assert.equal(hits.length, 10, "窗口宽度不变");
|
|
91
|
+
assert.deepEqual(hits[0], { id: live.id, archived: false, forgotten: false });
|
|
92
|
+
store.close();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("update 与 compareAndUpdate 都重算 content_hash", () => {
|
|
96
|
+
const store = createStore(":memory:");
|
|
97
|
+
const saved = store.save({ type: "project", title: "T", content: "旧正文" });
|
|
98
|
+
assert.equal(store.findContentHashMatches({ type: "project", hash: contentHashOf({ title: "T", content: "旧正文" }) }).length, 1);
|
|
99
|
+
|
|
100
|
+
const updated = store.update(saved.id, { content: "新正文" });
|
|
101
|
+
assert.equal(updated.content_hash, contentHashOf({ title: "T", content: "新正文" }));
|
|
102
|
+
assert.equal(
|
|
103
|
+
store.findContentHashMatches({ type: "project", hash: contentHashOf({ title: "T", content: "旧正文" }) }).length,
|
|
104
|
+
0,
|
|
105
|
+
"旧内容的锚必须失效"
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
const cas = store.compareAndUpdate(updated.id, updated.updated_at, { title: "T2" });
|
|
109
|
+
assert.equal(cas.content_hash, contentHashOf({ title: "T2", content: "新正文" }));
|
|
110
|
+
assert.equal(
|
|
111
|
+
store.findContentHashMatches({ type: "project", hash: contentHashOf({ title: "T", content: "新正文" }) }).length,
|
|
112
|
+
0,
|
|
113
|
+
"CAS 路径漏重算的话标记会指向不再存在的内容"
|
|
114
|
+
);
|
|
115
|
+
store.close();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test("demoteToSummary 与 restoreContent 也重算 content_hash", () => {
|
|
119
|
+
const store = createStore(":memory:");
|
|
120
|
+
const saved = store.save({ type: "project", title: "T", content: "完整正文" });
|
|
121
|
+
const demoted = store.demoteToSummary(saved.id, "摘要行");
|
|
122
|
+
assert.equal(demoted.content_hash, contentHashOf({ title: "T", content: "摘要行" }));
|
|
123
|
+
assert.equal(
|
|
124
|
+
store.findContentHashMatches({ type: "project", hash: contentHashOf({ title: "T", content: "完整正文" }) }).length,
|
|
125
|
+
0,
|
|
126
|
+
"降级后的行装的是摘要,锚不能还指着完整正文"
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
const restored = store.restoreContent(saved.id);
|
|
130
|
+
assert.equal(restored.content_hash, contentHashOf({ title: "T", content: "完整正文" }));
|
|
131
|
+
store.close();
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("老库打开:加列 + 建索引 + 存量回填(索引顺序错会直接开不起来)", () => {
|
|
135
|
+
const dir = mkdtempSync(join(tmpdir(), "dsh-mneme-hash-migrate-"));
|
|
136
|
+
const dbPath = join(dir, "legacy.db");
|
|
137
|
+
try {
|
|
138
|
+
const legacy = new DatabaseSync(dbPath);
|
|
139
|
+
legacy.exec(`CREATE TABLE memories (
|
|
140
|
+
id TEXT PRIMARY KEY, type TEXT NOT NULL, title TEXT NOT NULL, content TEXT NOT NULL,
|
|
141
|
+
tags TEXT NOT NULL DEFAULT '[]', importance INTEGER NOT NULL DEFAULT 3,
|
|
142
|
+
forgotten INTEGER NOT NULL DEFAULT 0, archived INTEGER NOT NULL DEFAULT 0,
|
|
143
|
+
source TEXT, created_at TEXT NOT NULL, updated_at TEXT NOT NULL
|
|
144
|
+
);`);
|
|
145
|
+
legacy
|
|
146
|
+
.prepare(
|
|
147
|
+
"INSERT INTO memories (id, type, title, content, tags, importance, forgotten, archived, source, created_at, updated_at) VALUES (?, ?, ?, ?, '[]', 3, 0, ?, NULL, ?, ?)"
|
|
148
|
+
)
|
|
149
|
+
.run("legacy-1", "project", "老库事实", "同一件事的正文", 0, "2026-01-01T00:00:00.000Z", "2026-01-01T00:00:00.000Z");
|
|
150
|
+
legacy
|
|
151
|
+
.prepare(
|
|
152
|
+
"INSERT INTO memories (id, type, title, content, tags, importance, forgotten, archived, source, created_at, updated_at) VALUES (?, ?, ?, ?, '[]', 3, 0, ?, NULL, ?, ?)"
|
|
153
|
+
)
|
|
154
|
+
.run("legacy-2", "project", "被归档的老事实", "归档区里同一件事的正文", 1, "2026-01-02T00:00:00.000Z", "2026-01-02T00:00:00.000Z");
|
|
155
|
+
legacy.close();
|
|
156
|
+
|
|
157
|
+
const store = createStore(dbPath);
|
|
158
|
+
const index = store.db
|
|
159
|
+
.prepare("SELECT name FROM sqlite_master WHERE type='index' AND name='idx_memories_content_hash'")
|
|
160
|
+
.get();
|
|
161
|
+
assert.ok(index, "老库也要建上索引(候选集靠它 seek,否则每次写入都是全表扫)");
|
|
162
|
+
|
|
163
|
+
// 索引不只是「存在」:回填的 NULL 扫描与准入的候选集查询都得真的走它。列序写反
|
|
164
|
+
// (type 打头)时前者退化成全表扫——每次打开都要把全部正文读一遍。
|
|
165
|
+
const planOf = (sql, ...params) =>
|
|
166
|
+
store.db.prepare(`EXPLAIN QUERY PLAN ${sql}`).all(...params).map((r) => r.detail).join(" | ");
|
|
167
|
+
assert.match(
|
|
168
|
+
planOf("SELECT id, title, content FROM memories WHERE content_hash IS NULL"),
|
|
169
|
+
/idx_memories_content_hash/,
|
|
170
|
+
"存量回填走索引"
|
|
171
|
+
);
|
|
172
|
+
assert.match(
|
|
173
|
+
planOf("SELECT id FROM memories WHERE type = ? AND content_hash = ?", "project", "x"),
|
|
174
|
+
/idx_memories_content_hash/,
|
|
175
|
+
"候选集查询走索引"
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
const backfilled = store.getById("legacy-1");
|
|
179
|
+
assert.equal(backfilled.content_hash, contentHashOf({ title: "老库事实", content: "同一件事的正文" }));
|
|
180
|
+
assert.deepEqual(
|
|
181
|
+
store.findContentHashMatches({ type: "project", hash: backfilled.content_hash }).map((m) => m.id),
|
|
182
|
+
["legacy-1"]
|
|
183
|
+
);
|
|
184
|
+
const archivedBackfilled = store.getById("legacy-2");
|
|
185
|
+
assert.deepEqual(
|
|
186
|
+
store.findContentHashMatches({ type: "project", hash: archivedBackfilled.content_hash }),
|
|
187
|
+
[{ id: "legacy-2", archived: true, forgotten: false }],
|
|
188
|
+
"存量归档行回填后也要在候选集里(不然这条信号在老库上量不到归档区)"
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
// 回填后仍能正常写入(幂等迁移没有把表锁死)
|
|
192
|
+
const fresh = store.save({ type: "project", title: "新行", content: "新正文" });
|
|
193
|
+
assert.ok(fresh.content_hash);
|
|
194
|
+
const firstPassHash = archivedBackfilled.content_hash;
|
|
195
|
+
store.close();
|
|
196
|
+
|
|
197
|
+
// 再开一次:只算 NULL 行,已有锚原样保留
|
|
198
|
+
const again = createStore(dbPath);
|
|
199
|
+
assert.equal(again.getById("legacy-2").content_hash, firstPassHash, "重复打开不得改写已有锚");
|
|
200
|
+
again.close();
|
|
201
|
+
} finally {
|
|
202
|
+
rmSync(dir, { recursive: true, force: true });
|
|
203
|
+
}
|
|
204
|
+
});
|