dsh-knit 0.20.0 → 0.22.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/CHANGELOG.md +247 -0
- package/README.en.md +109 -0
- package/README.md +93 -3
- package/SECURITY.md +3 -2
- package/package.json +2 -2
- package/src/client/client.js +530 -50
- package/src/host/context.js +42 -6
- package/src/host/control.js +421 -0
- package/src/host/index-store.js +479 -0
- package/src/host/index.js +1015 -69
- package/src/host/links.js +97 -34
- package/src/host/relations.js +316 -0
- package/src/host/tool.js +96 -6
- package/test/client.test.mjs +540 -12
- package/test/code-context.test.mjs +3 -1
- package/test/context-eval.test.mjs +5 -1
- package/test/context.test.mjs +32 -2
- package/test/control.test.mjs +324 -0
- package/test/deep-corpus.mjs +110 -0
- package/test/deep-scale.test.mjs +1 -1
- package/test/fixture.mjs +50 -0
- package/test/harness.mjs +6 -2
- package/test/host-http.test.mjs +107 -0
- package/test/i18n.test.mjs +12 -6
- package/test/index-incremental.test.mjs +504 -0
- package/test/index-store.test.mjs +340 -0
- package/test/links.test.mjs +50 -0
- package/test/relations.test.mjs +360 -0
- package/test/tier1.test.mjs +2 -2
- package/test/tool.test.mjs +79 -1
package/test/context.test.mjs
CHANGED
|
@@ -207,6 +207,28 @@ test('context: Supporting —— 反方向的引用关系给的是 linkSource',
|
|
|
207
207
|
assert.equal(pack.supporting[0].reason.code, 'linkSource')
|
|
208
208
|
})
|
|
209
209
|
|
|
210
|
+
test('context: 来源三值 —— 靠引用进来的那条是 relation,命中进来的是 retrieval(v0.21 D3)', () => {
|
|
211
|
+
// §10.16:`provenance` 是**来源**(`source` 从 v0.14 起是文件角色,不是这个)。
|
|
212
|
+
// 这里要钉的是:零命中、靠引用被带进来的条目,来源不许写成 `retrieval` ——
|
|
213
|
+
// 那等于说「它是被检索出来的」,是没证据的点头(需求 §6.5)。
|
|
214
|
+
const pack = assemble([
|
|
215
|
+
doc('docs/main.md', 30, [hit('alpha', { title: true })]),
|
|
216
|
+
doc('docs/impl.md', 0),
|
|
217
|
+
], { graph: graphOf({ 'docs/main.md': ['docs/impl.md'] }) })
|
|
218
|
+
|
|
219
|
+
assert.equal(pack.primary[0].source, 'doc', 'source 仍是文件角色(v0.14 起的含义)')
|
|
220
|
+
assert.equal(pack.primary[0].provenance, 'retrieval')
|
|
221
|
+
assert.equal(pack.supporting[0].reason.code, 'linkTarget')
|
|
222
|
+
assert.equal(pack.supporting[0].provenance, 'relation',
|
|
223
|
+
'靠引用带进来的条目,来源必须是 relation')
|
|
224
|
+
|
|
225
|
+
// 三值就三个,且**只有调用方**(Phase B 的固定覆盖)能写 manual
|
|
226
|
+
for (const item of [...pack.primary, ...pack.supporting, ...pack.related]) {
|
|
227
|
+
assert.ok(['retrieval', 'relation', 'manual'].includes(item.provenance),
|
|
228
|
+
`${item.rel} 的 provenance 越界:${item.provenance}`)
|
|
229
|
+
}
|
|
230
|
+
})
|
|
231
|
+
|
|
210
232
|
test('context: Supporting —— 没有 Primary 就没有引用邻居(引用是相对 Primary 说的)', () => {
|
|
211
233
|
const pack = assemble([
|
|
212
234
|
doc('docs/lone.md', 5, [hit('alpha', { body: true, bodyTf: 1 })]),
|
|
@@ -518,11 +540,19 @@ test('context: 输出里不许出现内部分数 —— 名次本身就是答案
|
|
|
518
540
|
}
|
|
519
541
|
})
|
|
520
542
|
|
|
521
|
-
test('context: 条目字段是固定投影(rel/title/summary/mtimeMs/kind/source/reason)', () => {
|
|
543
|
+
test('context: 条目字段是固定投影(rel/title/summary/mtimeMs/kind/source/reason/provenance)', () => {
|
|
522
544
|
const pack = assemble([doc('docs/main.md', 100, [hit('alpha', { title: true })], { summary: '摘要' })])
|
|
523
545
|
assert.deepEqual(Object.keys(pack.primary[0]).sort(),
|
|
524
|
-
['kind', 'mtimeMs', 'reason', 'rel', 'source', 'summary', 'title'])
|
|
546
|
+
['kind', 'mtimeMs', 'provenance', 'reason', 'rel', 'source', 'summary', 'title'])
|
|
525
547
|
assert.deepEqual(Object.keys(pack.primary[0].reason).sort(), ['code', 'fields', 'term', 'terms'])
|
|
548
|
+
// 没有关系时**不出现** `relations` / `relationsTotal`(空数组是纯噪音),
|
|
549
|
+
// 有了才出现 —— 见 v0.21 §6.7 与 context.js 的 `project()`。
|
|
550
|
+
assert.equal('relations' in pack.primary[0], false)
|
|
551
|
+
assert.equal('relationsTotal' in pack.primary[0], false)
|
|
552
|
+
// `source` 是**文件角色**(v0.14 起),`provenance` 是**来源**(v0.21 起)——
|
|
553
|
+
// 两个字段名不能互相顶替,否则客户端与工具 schema 会静默错读。
|
|
554
|
+
assert.equal(pack.primary[0].source, 'doc')
|
|
555
|
+
assert.equal(pack.primary[0].provenance, 'retrieval')
|
|
526
556
|
})
|
|
527
557
|
|
|
528
558
|
test('context: task 是原样引用 —— 本模块不生成、不改写、也不截断', () => {
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Knit · **上下文控制层**(v0.21)的测试
|
|
3
|
+
*
|
|
4
|
+
* 靶心是**「用户的话比排序算法更硬」这句话到底有没有落地**,分两步分别证伪:
|
|
5
|
+
* · 排除在**检索之前** —— 被排掉的文件不进候选集,连 `matched` 都不算它;
|
|
6
|
+
* · 固定在**装配之后** —— 被固定的条目从三层**移出**,三层本身一字不动。
|
|
7
|
+
*
|
|
8
|
+
* 还有一件事必须测:拿不到存储时**功能照常、只是说出来**(D8 的降级路径),
|
|
9
|
+
* 因为那条路径在真机上永远不会被「正常使用」走到,只有测试能一直守着它。
|
|
10
|
+
*/
|
|
11
|
+
import test from 'node:test'
|
|
12
|
+
import assert from 'node:assert/strict'
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
normalizeControl, applyAction, excludedSet, pinnedRels, applyPinOverride,
|
|
16
|
+
createControlStore, createControlRegistry, sharedControlRegistry, emptyControl,
|
|
17
|
+
MAX_CONTROL_ROWS, CONTROL_UNIT, CONTROL_VERSION,
|
|
18
|
+
} from '../src/host/control.js'
|
|
19
|
+
import { scan, buildContextFor } from '../src/host/index.js'
|
|
20
|
+
import { makeWorkspace } from './fixture.mjs'
|
|
21
|
+
|
|
22
|
+
const rel = (item) => item.rel
|
|
23
|
+
|
|
24
|
+
/* ── 1 · 状态机:确定性、幂等、互斥 ─────────────────────── */
|
|
25
|
+
|
|
26
|
+
test('normalizeControl:畸形输入一律当空状态处理,不抛', () => {
|
|
27
|
+
for (const bad of [null, undefined, 0, 'x', [], { pinned: 'no' }, { pinned: [null, 3, {}] }]) {
|
|
28
|
+
assert.deepEqual(normalizeControl(bad), emptyControl())
|
|
29
|
+
}
|
|
30
|
+
// 缺 `at` 的记录保留(时间戳不是身份),但不合法的 `rel` 直接丢
|
|
31
|
+
assert.deepEqual(normalizeControl({ pinned: [{ rel: 'a.md' }, { rel: '' }] }), {
|
|
32
|
+
pinned: [{ rel: 'a.md', at: 0 }], excluded: [],
|
|
33
|
+
})
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
test('normalizeControl:固定项按 `at` 升序,`at` 相同按 rel 字典序(确定性)', () => {
|
|
37
|
+
const out = normalizeControl({
|
|
38
|
+
pinned: [{ rel: 'b.md', at: 5 }, { rel: 'a.md', at: 5 }, { rel: 'c.md', at: 1 }],
|
|
39
|
+
})
|
|
40
|
+
assert.deepEqual(out.pinned.map(rel), ['c.md', 'a.md', 'b.md'])
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test('applyAction:同一篇排两次不出现第二行,重复固定也不刷新 `at`', () => {
|
|
44
|
+
const first = applyAction(emptyControl(), 'pin', 'a.md', 100)
|
|
45
|
+
assert.equal(first.changed, true)
|
|
46
|
+
const again = applyAction(first.control, 'pin', 'a.md', 999)
|
|
47
|
+
assert.equal(again.changed, false)
|
|
48
|
+
assert.deepEqual(again.control.pinned, [{ rel: 'a.md', at: 100 }])
|
|
49
|
+
|
|
50
|
+
const ex = applyAction(emptyControl(), 'exclude', 'a.md', 7)
|
|
51
|
+
assert.equal(applyAction(ex.control, 'exclude', 'a.md', 8).changed, false)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
test('applyAction:固定与排除互斥(一个动作同时清掉对面那一侧)', () => {
|
|
55
|
+
const pinned = applyAction(emptyControl(), 'pin', 'a.md', 1).control
|
|
56
|
+
const excluded = applyAction(pinned, 'exclude', 'a.md', 2).control
|
|
57
|
+
assert.deepEqual(excluded.pinned, [])
|
|
58
|
+
assert.deepEqual(excluded.excluded.map(rel), ['a.md'])
|
|
59
|
+
const back = applyAction(excluded, 'pin', 'a.md', 3).control
|
|
60
|
+
assert.deepEqual(back.excluded, [])
|
|
61
|
+
assert.deepEqual(back.pinned.map(rel), ['a.md'])
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
test('applyAction:未知动作 / 非法 rel 一律不动状态,也不抛', () => {
|
|
65
|
+
const base = applyAction(emptyControl(), 'pin', 'a.md', 1).control
|
|
66
|
+
for (const [action, r] of [['delete', 'a.md'], ['pin', ''], ['pin', 42], ['restore', 'a.md']]) {
|
|
67
|
+
const out = applyAction(base, action, r, 2)
|
|
68
|
+
assert.equal(out.changed, false)
|
|
69
|
+
assert.deepEqual(out.control, base)
|
|
70
|
+
}
|
|
71
|
+
// 两个方向都能取消:unpin 拿掉固定,restore 拿掉排除
|
|
72
|
+
assert.equal(applyAction(base, 'unpin', 'a.md').changed, true)
|
|
73
|
+
assert.deepEqual(applyAction(base, 'unpin', 'a.md').control.pinned, [])
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
test('applyAction:记录数有硬上限(状态不能无限长)', () => {
|
|
77
|
+
let control = emptyControl()
|
|
78
|
+
for (let i = 0; i < MAX_CONTROL_ROWS + 20; i++) {
|
|
79
|
+
control = applyAction(control, 'pin', `f${i}.md`, i).control
|
|
80
|
+
}
|
|
81
|
+
assert.equal(control.pinned.length, MAX_CONTROL_ROWS)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test('excludedSet / pinnedRels:集合与顺序两个视图', () => {
|
|
85
|
+
const control = { pinned: [{ rel: 'p.md', at: 2 }], excluded: [{ rel: 'x.md', at: 1 }] }
|
|
86
|
+
assert.deepEqual([...excludedSet(control)], ['x.md'])
|
|
87
|
+
assert.deepEqual(pinnedRels(control), ['p.md'])
|
|
88
|
+
assert.deepEqual([...excludedSet(null)], [])
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
/* ── 2 · 固定覆盖:只从本包现成的行里取 ─────────────────── */
|
|
92
|
+
|
|
93
|
+
/** 造一个最小 pack(形状与 `buildContext()` 的产物一致)。 */
|
|
94
|
+
const fakePack = () => ({
|
|
95
|
+
primary: [{ rel: 'p.md', title: 'P', reason: {}, provenance: 'retrieval' }],
|
|
96
|
+
supporting: [{ rel: 's.md', title: 'S', reason: {}, provenance: 'retrieval' }],
|
|
97
|
+
related: [{ rel: 'r.md', title: 'R', reason: {}, provenance: 'retrieval' }],
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
test('applyPinOverride:固定项从三层移出,来源改成 `manual`,其余行原样', () => {
|
|
101
|
+
const pack = fakePack()
|
|
102
|
+
const out = applyPinOverride(pack, { pinned: [{ rel: 's.md', at: 1 }], excluded: [] })
|
|
103
|
+
assert.deepEqual(out.pinned.map(rel), ['s.md'])
|
|
104
|
+
assert.equal(out.pinned[0].provenance, 'manual')
|
|
105
|
+
assert.equal(out.pinned[0].title, 'S', '复用现成的行,不重新装配')
|
|
106
|
+
assert.deepEqual(out.supporting, [])
|
|
107
|
+
assert.deepEqual(out.primary.map(rel), ['p.md'])
|
|
108
|
+
assert.deepEqual(out.related.map(rel), ['r.md'])
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
test('applyPinOverride:顺序按用户固定的先后,不是按层', () => {
|
|
112
|
+
const pack = fakePack()
|
|
113
|
+
const out = applyPinOverride(pack, {
|
|
114
|
+
pinned: [{ rel: 'r.md', at: 1 }, { rel: 'p.md', at: 2 }], excluded: [],
|
|
115
|
+
})
|
|
116
|
+
assert.deepEqual(out.pinned.map(rel), ['r.md', 'p.md'])
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
test('applyPinOverride:固定了但这一批里没有它 —— 不硬凑,原样返回同一个对象', () => {
|
|
120
|
+
const pack = fakePack()
|
|
121
|
+
const out = applyPinOverride(pack, { pinned: [{ rel: 'gone.md', at: 1 }], excluded: [] })
|
|
122
|
+
assert.equal(out, pack, '一篇都没落到本批 ⇒ 连拷贝都不做')
|
|
123
|
+
assert.equal(out.pinned, undefined, '没有固定项时这个键不出现(不是空数组)')
|
|
124
|
+
assert.deepEqual(out.primary.map(rel), ['p.md'], '三层一篇都不许少')
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
test('applyPinOverride:没有固定项时原样返回**同一个对象**(零开销路径)', () => {
|
|
128
|
+
const pack = fakePack()
|
|
129
|
+
assert.equal(applyPinOverride(pack, emptyControl()), pack)
|
|
130
|
+
assert.equal(applyPinOverride(pack, null), pack)
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
/* ── 3 · 端到端:排除在检索之前、固定在装配之后 ─────────── */
|
|
134
|
+
|
|
135
|
+
test('端到端:排除在检索之前 —— 被排掉的那篇不进候选集,也不计入 matched', async () => {
|
|
136
|
+
const root = makeWorkspace()
|
|
137
|
+
const query = '相关性排序 sidebar'
|
|
138
|
+
const plain = await scan(root, 20, { sort: 'relevance', kind: 'context', query })
|
|
139
|
+
const victim = plain.docs[0].rel
|
|
140
|
+
const hit = await scan(root, 20, {
|
|
141
|
+
sort: 'relevance', kind: 'context', query, excluded: new Set([victim]),
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
assert.equal(hit.mode, 'relevance', '排除不该把相关性模式打回时间序')
|
|
145
|
+
assert.ok(!hit.ranked.some((doc) => doc.rel === victim), '被排掉的不该留在全量名次里')
|
|
146
|
+
assert.ok(!hit.docs.some((doc) => doc.rel === victim), '也不该留在这一页里')
|
|
147
|
+
assert.equal(hit.total, plain.total - 1)
|
|
148
|
+
assert.equal(hit.stats.controlSkipped, 1, '管道条要的是「这一批真的排掉几篇」')
|
|
149
|
+
assert.ok(plain.ranked.some((doc) => doc.rel === victim), '不传 excluded 时一切照旧')
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
test('端到端:排除一个不在这批语料里的 rel —— 计数为 0,别的什么都不变', async () => {
|
|
153
|
+
const root = makeWorkspace()
|
|
154
|
+
const plain = await scan(root, 20, { sort: 'relevance', kind: 'context', query: 'sidebar' })
|
|
155
|
+
const hit = await scan(root, 20, {
|
|
156
|
+
sort: 'relevance', kind: 'context', query: 'sidebar', excluded: new Set(['nope.txt']),
|
|
157
|
+
})
|
|
158
|
+
assert.equal(hit.stats.controlSkipped, 0)
|
|
159
|
+
assert.deepEqual(hit.docs.map(rel), plain.docs.map(rel))
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
test('端到端:固定在装配之后 —— 三层成员与顺序逐字不变(只少了被固定那一篇)', async () => {
|
|
163
|
+
const root = makeWorkspace()
|
|
164
|
+
const query = '相关性排序 sidebar'
|
|
165
|
+
const payload = await scan(root, 20, { sort: 'relevance', kind: 'context', query })
|
|
166
|
+
const plain = await buildContextFor(root, {
|
|
167
|
+
ranked: payload.ranked, topic: payload.topic, task: payload.task, total: payload.total,
|
|
168
|
+
})
|
|
169
|
+
const target = plain.supporting[0] || plain.related[0] || plain.primary[0]
|
|
170
|
+
assert.ok(target, '夹具太弱:一篇都没进包')
|
|
171
|
+
|
|
172
|
+
const pinnedPack = await buildContextFor(root, {
|
|
173
|
+
ranked: payload.ranked, topic: payload.topic, task: payload.task, total: payload.total,
|
|
174
|
+
control: { pinned: [{ rel: target.rel, at: 1 }], excluded: [] },
|
|
175
|
+
})
|
|
176
|
+
assert.deepEqual(pinnedPack.pinned.map(rel), [target.rel])
|
|
177
|
+
assert.equal(pinnedPack.pinned[0].provenance, 'manual')
|
|
178
|
+
/** 把某一篇从「那三个数组」里全部抽掉,剩下的序列必须逐字相同。 */
|
|
179
|
+
const strip = (pack) => ['primary', 'supporting', 'related']
|
|
180
|
+
.map((tier) => pack[tier].map(rel).filter((r) => r !== target.rel))
|
|
181
|
+
assert.deepEqual(strip(pinnedPack), strip(plain), '被固定的只是移出,不许重排别人')
|
|
182
|
+
const all = (pack) => ['primary', 'supporting', 'related']
|
|
183
|
+
.flatMap((tier) => pack[tier]).filter((item) => item.rel !== target.rel)
|
|
184
|
+
assert.deepEqual(all(pinnedPack), all(plain), '其余每一条的数据也该逐字相同')
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
/* ── 4 · 持久化与降级(D8)─────────────────────────────── */
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* 一个最小的假 `kv` 后端:形状与 `dsh-storage` 的 JSON 后端一致
|
|
191
|
+
* (`open(descriptor)` → `loadAll / putRecord / deleteRecord`),
|
|
192
|
+
* **介质单独放在 `medium` 里** —— 于是「重启」就是「拿同一份介质、造一个新的后端」,
|
|
193
|
+
* 而 `duplicate-mount`(同名 unit 在**同一个**后端里开两次)也能如实复现。
|
|
194
|
+
*/
|
|
195
|
+
function fakeMedium() {
|
|
196
|
+
return { tables: {} }
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function fakeBackend(medium = fakeMedium()) {
|
|
200
|
+
const opened = new Set()
|
|
201
|
+
const writes = []
|
|
202
|
+
const unitOf = (name) => ({
|
|
203
|
+
loadAll: async () => ({
|
|
204
|
+
unit: { name, version: 1 }, global: {}, tables: medium.tables,
|
|
205
|
+
}),
|
|
206
|
+
putRecord: async (table, key, value) => {
|
|
207
|
+
writes.push(['put', table, key])
|
|
208
|
+
medium.tables[table] = medium.tables[table] || {}
|
|
209
|
+
medium.tables[table][key] = value
|
|
210
|
+
},
|
|
211
|
+
deleteRecord: async (table, key) => {
|
|
212
|
+
writes.push(['del', table, key])
|
|
213
|
+
if (medium.tables[table]) delete medium.tables[table][key]
|
|
214
|
+
},
|
|
215
|
+
})
|
|
216
|
+
return {
|
|
217
|
+
medium,
|
|
218
|
+
writes,
|
|
219
|
+
kv: {
|
|
220
|
+
open: async (descriptor) => {
|
|
221
|
+
if (opened.has(descriptor.name)) throw new Error('duplicate-mount')
|
|
222
|
+
opened.add(descriptor.name)
|
|
223
|
+
return unitOf(descriptor.name)
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
test('持久化:写进去的状态能被「重启后」的新 registry 读回来', async () => {
|
|
230
|
+
const medium = fakeMedium()
|
|
231
|
+
const first = createControlRegistry({ backend: fakeBackend(medium) })
|
|
232
|
+
assert.equal(first.persisted, false, '证据到之前不许说自己记得')
|
|
233
|
+
await first.ready('/w')
|
|
234
|
+
assert.equal(first.persisted, true, '装载成功 ⇒ 这时候才有资格说记得')
|
|
235
|
+
await first.act('/w', 'pin', 'a.md', 11)
|
|
236
|
+
await first.act('/w', 'exclude', 'b.md', 12)
|
|
237
|
+
await first.flush()
|
|
238
|
+
|
|
239
|
+
// 「重启」= 同一份介质 + 一个全新的后端与 registry
|
|
240
|
+
const second = createControlRegistry({ backend: fakeBackend(medium) })
|
|
241
|
+
// 首访是**异步**装载(同步读先给空状态,装载完再补上)—— 这正是设计:不阻塞面板。
|
|
242
|
+
second.controlOf('/w')
|
|
243
|
+
await second.flush()
|
|
244
|
+
await new Promise((resolve) => setTimeout(resolve, 5))
|
|
245
|
+
assert.deepEqual(pinnedRels(second.controlOf('/w')), ['a.md'])
|
|
246
|
+
assert.deepEqual([...excludedSet(second.controlOf('/w'))], ['b.md'])
|
|
247
|
+
assert.equal(CONTROL_UNIT, 'knit_control')
|
|
248
|
+
assert.equal(CONTROL_VERSION, 1)
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
test('持久化:只写差量 —— 取消固定发的是 delete,不是把整张表重写一遍', async () => {
|
|
252
|
+
const backend = fakeBackend()
|
|
253
|
+
const registry = createControlRegistry({ backend })
|
|
254
|
+
await registry.act('/w', 'pin', 'a.md', 1)
|
|
255
|
+
await registry.act('/w', 'pin', 'c.md', 2)
|
|
256
|
+
await registry.flush()
|
|
257
|
+
const before = backend.writes.length
|
|
258
|
+
await registry.act('/w', 'unpin', 'a.md')
|
|
259
|
+
await registry.flush()
|
|
260
|
+
assert.deepEqual(backend.writes.slice(before), [['del', 'pinned', '/w\u0000a.md']])
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
test('降级:没有 storage 时功能照常,但 `persisted` 必须说「没有」', async () => {
|
|
264
|
+
const registry = createControlRegistry(null)
|
|
265
|
+
assert.equal(registry.persisted, false)
|
|
266
|
+
const out = await registry.act('/w', 'pin', 'a.md', 1)
|
|
267
|
+
assert.equal(out.changed, true)
|
|
268
|
+
assert.equal(out.persisted, false)
|
|
269
|
+
assert.deepEqual(pinnedRels(registry.controlOf('/w')), ['a.md'])
|
|
270
|
+
assert.deepEqual(await createControlStore(null).read('/w'), null)
|
|
271
|
+
|
|
272
|
+
// 后端存在但 `open` 抛(version-mismatch / malformed-medium 那类)⇒ 一样是降级,不抛
|
|
273
|
+
const broken = { kv: { open: async () => { throw new Error('version-mismatch') } } }
|
|
274
|
+
const store = createControlStore({ backend: broken })
|
|
275
|
+
assert.equal(await store.read('/w'), null)
|
|
276
|
+
assert.equal(store.persisted, false)
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
test('真机缺陷回归:写失败时 `act()` 必须在**同一次响应**里就报 false', async () => {
|
|
280
|
+
// 真机实测到的老行为:第一次点击报 `persisted: true`(只是「后端在那儿」),
|
|
281
|
+
// 写其实失败了 —— 而介质文件从来没有出现过。现在 `act()` 等写完再说。
|
|
282
|
+
const medium = fakeMedium()
|
|
283
|
+
const backend = fakeBackend(medium)
|
|
284
|
+
const store = createControlStore({ backend })
|
|
285
|
+
const unit = await backend.kv.open({ name: 'probe', version: 1, tables: ['pinned', 'excluded'], hasGlobal: false })
|
|
286
|
+
unit.putRecord = async () => { throw new Error('disk-full') }
|
|
287
|
+
const registry = createControlRegistry({ backend: { kv: { open: async () => unit } } })
|
|
288
|
+
const out = await registry.act('/w', 'pin', 'a.md', 1)
|
|
289
|
+
assert.equal(out.changed, true)
|
|
290
|
+
assert.equal(out.persisted, false, '写失败 ⇒ 同一次响应就如实说 false')
|
|
291
|
+
assert.equal(registry.persisted, false)
|
|
292
|
+
assert.equal(store.persisted, false)
|
|
293
|
+
assert.deepEqual(pinnedRels(registry.controlOf('/w')), ['a.md'], '内存态照常,功能不坏')
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
test('真机缺陷回归:同一进程里插件被加载多次 ⇒ 一个后端只留一个登记表', async () => {
|
|
297
|
+
// 这份 fake 的 `open` 第二次会抛(普通 Error,如实复现 json 后端的
|
|
298
|
+
// 「a unit has exactly one live handle」)⇒ 各自新建的第二个实例必然降级。
|
|
299
|
+
const backend = fakeBackend()
|
|
300
|
+
const first = sharedControlRegistry(backend)
|
|
301
|
+
const second = sharedControlRegistry(backend)
|
|
302
|
+
assert.equal(second, first, '同一个后端必须复用同一份登记表')
|
|
303
|
+
|
|
304
|
+
await first.act('/w', 'pin', 'a.md', 1)
|
|
305
|
+
await first.flush()
|
|
306
|
+
assert.deepEqual(pinnedRels(second.controlOf('/w')), ['a.md'], '两个实例看到的是同一份状态')
|
|
307
|
+
assert.deepEqual(backend.writes, [['put', 'pinned', '/w\u0000a.md']], '只开了一次、只写了一次')
|
|
308
|
+
|
|
309
|
+
// 反证:不走共享、各自新建的话,第二个实例的 `persisted` 会变成 false,
|
|
310
|
+
// 而模块级变量被最后一次加载覆盖 ⇒ 活跃实例恰恰是打不开的那个。
|
|
311
|
+
const shared = fakeBackend()
|
|
312
|
+
const a = createControlRegistry({ backend: shared })
|
|
313
|
+
const b = createControlRegistry({ backend: shared })
|
|
314
|
+
await a.act('/w', 'pin', 'a.md', 1)
|
|
315
|
+
await a.flush()
|
|
316
|
+
assert.equal(a.persisted, true, '写得成 ⇒ true(这次写的结果,不是「大概能写」)')
|
|
317
|
+
await b.act('/w', 'pin', 'b.md', 2)
|
|
318
|
+
await b.flush()
|
|
319
|
+
assert.equal(b.persisted, false, '第二个实例打不开 ⇒ 必须如实说没有记住')
|
|
320
|
+
assert.equal(shared.writes.length, 1, '打不开的实例一条都不该写')
|
|
321
|
+
assert.deepEqual(pinnedRels(b.controlOf('/w')), ['b.md'], '内存态照常')
|
|
322
|
+
|
|
323
|
+
assert.equal(sharedControlRegistry(null), null, '没有后端 ⇒ 返回 null(调用方退回内存态)')
|
|
324
|
+
})
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Knit v0.20 · **规模语料工厂**(R1 规模门槛的那份语料)—— 一处真源。
|
|
3
|
+
*
|
|
4
|
+
* ── 为什么它住在 `test/` 而不是 `tools/` ────────────────
|
|
5
|
+
*
|
|
6
|
+
* `test/deep-scale.test.mjs` 要造一份 1000 文件的合成工作区来守 R1 门槛,所以它
|
|
7
|
+
* 和这份工厂必须**一起进 npm 包**。`tools/` 不随包发布(`package.json` 的 `files`
|
|
8
|
+
* 只白名单了两个脚本),v0.20.0 因此漏了一条包装缺陷:registry 上那份 tarball 里
|
|
9
|
+
* `npm test` 是 638 pass / 1 fail(`ERR_MODULE_NOT_FOUND`)。
|
|
10
|
+
*
|
|
11
|
+
* 修法是搬进 `test/`,**不是**把 `tools/` 加进 `files` —— 「`tools/` 不进包」是
|
|
12
|
+
* 不变式。`tools/deep-benchmark.mjs` 现在从这里 re-export,`node tools/deep-benchmark.mjs`
|
|
13
|
+
* 的用法一个字没变。守这条不变式的闸门是 `tools/clean-room-test.mjs`(发版前必跑)。
|
|
14
|
+
*
|
|
15
|
+
* ── 四类文件(需求 §18 点名的 small / medium / large text / large code)──
|
|
16
|
+
*
|
|
17
|
+
* 小 Markdown ~1 KB 55% 中等 Markdown ~16 KB 25%
|
|
18
|
+
* 大 Markdown ~128 KB 10% 大代码 ~64 KB 10%
|
|
19
|
+
*
|
|
20
|
+
* 依赖:无(只用 Knit 自己的源码 + Node 标准库)。
|
|
21
|
+
*/
|
|
22
|
+
import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs'
|
|
23
|
+
import { tmpdir } from 'node:os'
|
|
24
|
+
import { join } from 'node:path'
|
|
25
|
+
|
|
26
|
+
import { scan, collectDocs } from '../src/host/index.js'
|
|
27
|
+
|
|
28
|
+
/** 合成语料的规模档位。 */
|
|
29
|
+
export const SIZES = [100, 500, 1000]
|
|
30
|
+
|
|
31
|
+
/** R1 的两条硬门槛。 */
|
|
32
|
+
export const GATES = { synthetic1000Ms: 1500, realMs: 1200 }
|
|
33
|
+
|
|
34
|
+
/** 四类文件的占比(按索引取模分配,保证任何 N 下四类都在)。 */
|
|
35
|
+
const MIX = [
|
|
36
|
+
{ kind: 'small-md', share: 55, bytes: 1 * 1024 },
|
|
37
|
+
{ kind: 'medium-md', share: 25, bytes: 16 * 1024 },
|
|
38
|
+
{ kind: 'large-md', share: 10, bytes: 128 * 1024 },
|
|
39
|
+
{ kind: 'large-code', share: 10, bytes: 64 * 1024 },
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
/** 造一段定长文本(中文按 3 字节算,够接近就行 —— 这是量级基准,不是字节精确测试)。 */
|
|
43
|
+
function filler(bytes, seed) {
|
|
44
|
+
const line = `第 ${seed} 段:这一段正文用于把文件撑到目标大小,与任何查询词都无关。`
|
|
45
|
+
const need = Math.max(1, Math.floor(bytes / (line.length * 3)))
|
|
46
|
+
return new Array(need).fill(line).join('\n')
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 按占比决定第 i 个文件属于哪一类。 */
|
|
50
|
+
function kindOf(i, n) {
|
|
51
|
+
let acc = 0
|
|
52
|
+
const pos = (i / n) * 100
|
|
53
|
+
for (const m of MIX) {
|
|
54
|
+
acc += m.share
|
|
55
|
+
if (pos < acc) return m
|
|
56
|
+
}
|
|
57
|
+
return MIX[0]
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** 造一个 N 篇的合成工作区;返回 { root, files, cleanup }。 */
|
|
61
|
+
export function makeScaleWorkspace(n, label = 'knit-bench') {
|
|
62
|
+
const root = mkdtempSync(join(tmpdir(), `${label}-`))
|
|
63
|
+
for (let i = 0; i < n; i += 1) {
|
|
64
|
+
const m = kindOf(i, n)
|
|
65
|
+
const rel = m.kind === 'large-code' ? `src/mod${i}.mjs` : `docs/page${i}.md`
|
|
66
|
+
const body = m.kind === 'large-code'
|
|
67
|
+
? `${filler(m.bytes, i)}\nexport function handler${i}(input) {\n return input\n}\n`
|
|
68
|
+
: `# 页面 ${i}\n\n${filler(m.bytes, i)}\n`
|
|
69
|
+
const abs = join(root, rel)
|
|
70
|
+
mkdirSync(join(abs, '..'), { recursive: true })
|
|
71
|
+
writeFileSync(abs, body)
|
|
72
|
+
}
|
|
73
|
+
return { root, files: n, cleanup: () => rmSync(root, { recursive: true, force: true }) }
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 跑一次「冷扫 + 热扫」,返回各阶段耗时。 */
|
|
77
|
+
export async function timeWorkspace(root, query = 'handler') {
|
|
78
|
+
const heap0 = process.memoryUsage().heapUsed
|
|
79
|
+
|
|
80
|
+
const t0 = process.hrtime.bigint()
|
|
81
|
+
const collected = await collectDocs(root)
|
|
82
|
+
const t1 = process.hrtime.bigint()
|
|
83
|
+
|
|
84
|
+
const payload = await scan(root, 400, { sort: 'relevance', query, kind: 'context' })
|
|
85
|
+
const t2 = process.hrtime.bigint()
|
|
86
|
+
|
|
87
|
+
const again = await scan(root, 400, { sort: 'relevance', query, kind: 'context' })
|
|
88
|
+
const t3 = process.hrtime.bigint()
|
|
89
|
+
|
|
90
|
+
const ms = (a, b) => Number(b - a) / 1e6
|
|
91
|
+
const heap1 = process.memoryUsage().heapUsed
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
collectMs: ms(t0, t1),
|
|
95
|
+
coldMs: ms(t0, t2),
|
|
96
|
+
warmMs: ms(t2, t3),
|
|
97
|
+
totalMs: ms(t0, t2),
|
|
98
|
+
heapMB: (heap1 - heap0) / 1024 / 1024,
|
|
99
|
+
counts: {
|
|
100
|
+
docs: collected.docs.length,
|
|
101
|
+
code: collected.code.length,
|
|
102
|
+
media: collected.media.length,
|
|
103
|
+
scanned: collected.docs.length + collected.code.length + collected.media.length,
|
|
104
|
+
truncated: collected.truncated,
|
|
105
|
+
codeTruncated: collected.codeTruncated,
|
|
106
|
+
},
|
|
107
|
+
ranked: payload.docs.length,
|
|
108
|
+
matched: again.docs.filter((d) => Array.isArray(d.matches) && d.matches.length > 0).length,
|
|
109
|
+
}
|
|
110
|
+
}
|
package/test/deep-scale.test.mjs
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
import test from 'node:test'
|
|
20
20
|
import assert from 'node:assert/strict'
|
|
21
21
|
|
|
22
|
-
import { makeScaleWorkspace, timeWorkspace, GATES, SIZES } from '
|
|
22
|
+
import { makeScaleWorkspace, timeWorkspace, GATES, SIZES } from './deep-corpus.mjs'
|
|
23
23
|
import { scan } from '../src/host/index.js'
|
|
24
24
|
|
|
25
25
|
test('规模门槛 R1:1000 文件、带 query 的完整 scan 必须远低于 5 秒轮询', async () => {
|
package/test/fixture.mjs
CHANGED
|
@@ -71,3 +71,53 @@ function trackForCleanup(dir) {
|
|
|
71
71
|
LIVE_DIRS.clear()
|
|
72
72
|
})
|
|
73
73
|
}
|
|
74
|
+
|
|
75
|
+
/* ── 存储替身(v0.22 的增量索引需要一份「能重启」的介质)───────── */
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 一个最小的假 `kv` 后端,形状与 `dsh-storage` 的 JSON 后端一致
|
|
79
|
+
* (`open(descriptor)` → `loadAll / putRecord / deleteRecord`)。
|
|
80
|
+
*
|
|
81
|
+
* `medium` 与后端实例**分开**,于是「重启」= 同一份 medium + 一个全新的后端,
|
|
82
|
+
* 而 `duplicate-mount`(同名 unit 在**同一个**后端里开两次)也能如实复现。
|
|
83
|
+
*
|
|
84
|
+
* @param {{tables?: object}} [medium] - 介质(省略则新建一份空的)
|
|
85
|
+
* @param {{failOpen?: boolean, brokenLoad?: boolean}} [options] - 故障注入
|
|
86
|
+
* @returns {object} 假后端(含 `medium` / `writes` / `descriptors`)
|
|
87
|
+
*/
|
|
88
|
+
export function fakeStorageBackend(medium = { tables: {} }, options = {}) {
|
|
89
|
+
const opened = new Set()
|
|
90
|
+
const writes = []
|
|
91
|
+
const descriptors = []
|
|
92
|
+
const unitOf = (name) => ({
|
|
93
|
+
loadAll: async () => {
|
|
94
|
+
if (options.brokenLoad) throw new Error('malformed-medium')
|
|
95
|
+
return { unit: { name, version: 1 }, global: {}, tables: medium.tables }
|
|
96
|
+
},
|
|
97
|
+
putRecord: async (table, key, value) => {
|
|
98
|
+
writes.push(['put', table, key])
|
|
99
|
+
medium.tables[table] = medium.tables[table] || {}
|
|
100
|
+
medium.tables[table][key] = value
|
|
101
|
+
},
|
|
102
|
+
deleteRecord: async (table, key) => {
|
|
103
|
+
writes.push(['del', table, key])
|
|
104
|
+
if (medium.tables[table]) delete medium.tables[table][key]
|
|
105
|
+
},
|
|
106
|
+
setGlobal: async () => {},
|
|
107
|
+
close: async () => { opened.delete(name) },
|
|
108
|
+
})
|
|
109
|
+
return {
|
|
110
|
+
medium,
|
|
111
|
+
writes,
|
|
112
|
+
descriptors,
|
|
113
|
+
kv: {
|
|
114
|
+
open: async (descriptor) => {
|
|
115
|
+
descriptors.push(descriptor)
|
|
116
|
+
if (options.failOpen) throw new Error('medium-unavailable')
|
|
117
|
+
if (opened.has(descriptor.name)) throw new Error('duplicate-mount')
|
|
118
|
+
opened.add(descriptor.name)
|
|
119
|
+
return unitOf(descriptor.name)
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
}
|
|
123
|
+
}
|
package/test/harness.mjs
CHANGED
|
@@ -91,7 +91,7 @@ export function loadClientModule(options = {}) {
|
|
|
91
91
|
+ 'sessionFileAddress, relTime, resolveRelative, pathImagesFor, clampRatio, splitRelPath, '
|
|
92
92
|
+ 'openKnitPanel, makeEntryButton, KnitGlyph, KnitTitle, KNIT_ICON_PATH, openLocalPath, '
|
|
93
93
|
+ 'requestPreview, fmtDuration, readKindPref, mediaUrl, isMedia, mediaLayoutFor, '
|
|
94
|
-
+ 'nextIndexFor, docOptionId, planRowMotion, mergeGhostRows, motionAllowed, motionShift, '
|
|
94
|
+
+ 'nextIndexFor, docOptionId, planRowMotion, mergeGhostRows, motionAllowed, settleEnterAnim, motionShift, '
|
|
95
95
|
+ 'rowLayoutPoint, applyRowFlips, '
|
|
96
96
|
// v0.20 Deep Context Retrieval:命中片段的行号区间与代码行锚点的几何。
|
|
97
97
|
+ 'fmtRange, firstMatchOf, anchorBandFor, '
|
|
@@ -109,7 +109,11 @@ export function loadClientModule(options = {}) {
|
|
|
109
109
|
+ 'MEDIA_LABEL_MIN_PX, MEDIA_GAP_PX, '
|
|
110
110
|
// v0.18 Context Usage Lens:纯函数与阈值(渲染层的东西仍走 DOM 断言)。
|
|
111
111
|
+ 'LIFE_GLYPH, lifeStatusOf, coverageOf, groupOutsideDocs, groupDelta, '
|
|
112
|
-
+ 'COMPACT_PANEL_PX, MAX_OUTSIDE_SHOWN, MAX_DELTA_SHOWN, MAX_DELTA_GROUP, FLASH_MS
|
|
112
|
+
+ 'COMPACT_PANEL_PX, MAX_OUTSIDE_SHOWN, MAX_DELTA_SHOWN, MAX_DELTA_GROUP, FLASH_MS, '
|
|
113
|
+
// v0.21 关系与控制:客户端只镜像了一个上限(面板每项最多投影几条关系)。
|
|
114
|
+
// 真正的截断在宿主做,客户端拿它写「共 N 条关系」那句话 —— 两个数字必须一致,
|
|
115
|
+
// `test/client.test.mjs` 有一条镜像守卫同时读两侧源码。
|
|
116
|
+
+ 'REL_MAX }\n'
|
|
113
117
|
+ " module.exports.inject = ['slots', 'locale']",
|
|
114
118
|
)
|
|
115
119
|
|