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
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Knit · **关系投影**(v0.21)的测试
|
|
3
|
+
*
|
|
4
|
+
* 靶心是**证据诚实性**,不是召回:每一条关系都必须能说清证据在哪(哪一行、哪种约定),
|
|
5
|
+
* 说不清就不产生关系。所以这一组的重点在「不产生」和「确定性」这两件事上。
|
|
6
|
+
*
|
|
7
|
+
* 全是纯函数测试:不起宿主、不读盘、不 spawn。
|
|
8
|
+
*/
|
|
9
|
+
import test from 'node:test'
|
|
10
|
+
import assert from 'node:assert/strict'
|
|
11
|
+
import { readFileSync } from 'node:fs'
|
|
12
|
+
import { join } from 'node:path'
|
|
13
|
+
|
|
14
|
+
import { createIndex } from '../src/host/links.js'
|
|
15
|
+
import { scan, buildContextFor } from '../src/host/index.js'
|
|
16
|
+
import { makeWorkspace } from './fixture.mjs'
|
|
17
|
+
import {
|
|
18
|
+
importsOf,
|
|
19
|
+
resolveSpec,
|
|
20
|
+
testsOf,
|
|
21
|
+
buildRelationFacts,
|
|
22
|
+
relationsOf,
|
|
23
|
+
resetRelationCache,
|
|
24
|
+
MAX_RELATIONS_PER_ITEM,
|
|
25
|
+
MAX_TOOL_RELATIONS_PER_ITEM,
|
|
26
|
+
} from '../src/host/relations.js'
|
|
27
|
+
|
|
28
|
+
/** 造一条候选记录(与 `collectDocs()` 产出的形状一致:rel / head / mtimeMs)。 */
|
|
29
|
+
const cand = (rel, head, mtimeMs = 1000) => ({ rel, head, mtimeMs })
|
|
30
|
+
|
|
31
|
+
/* ── 1 · importsOf:三种真实写法 + 行号 ───────────────── */
|
|
32
|
+
|
|
33
|
+
test('importsOf:认 `import … from`、裸 `import`、`require` 三种写法,行号正确', () => {
|
|
34
|
+
const text = [
|
|
35
|
+
"import { a } from './a.js'",
|
|
36
|
+
'',
|
|
37
|
+
"import './side.css'",
|
|
38
|
+
"const b = require('../lib/b')",
|
|
39
|
+
"import x from 'node:test'",
|
|
40
|
+
].join('\n')
|
|
41
|
+
assert.deepEqual(importsOf(text), [
|
|
42
|
+
{ spec: './a.js', line: 1 },
|
|
43
|
+
{ spec: './side.css', line: 3 },
|
|
44
|
+
{ spec: '../lib/b', line: 4 },
|
|
45
|
+
{ spec: 'node:test', line: 5 },
|
|
46
|
+
])
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
test('importsOf:同一个指定符只报一次(保留首次出现的行号)', () => {
|
|
50
|
+
const text = "import a from './x.js'\nimport b from './x.js'\n"
|
|
51
|
+
assert.deepEqual(importsOf(text), [{ spec: './x.js', line: 1 }])
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
test('importsOf:空 / 非字符串输入返回空数组,不抛', () => {
|
|
55
|
+
assert.deepEqual(importsOf(''), [])
|
|
56
|
+
assert.deepEqual(importsOf(null), [])
|
|
57
|
+
assert.deepEqual(importsOf(undefined), [])
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
/* ── 2 · resolveSpec:只认相对指定符 ──────────────────── */
|
|
61
|
+
|
|
62
|
+
test('resolveSpec:裸指定符 / 绝对路径 / 出工作区 一律不产生关系', () => {
|
|
63
|
+
const idx = createIndex(['src/a.js', 'src/b.ts', 'lib/index.js', 'docs/c.md', 'src/d.mjs'])
|
|
64
|
+
assert.equal(resolveSpec('node:test', 'src/a.js', idx), null)
|
|
65
|
+
assert.equal(resolveSpec('react', 'src/a.js', idx), null)
|
|
66
|
+
assert.equal(resolveSpec('/abs/x.js', 'src/a.js', idx), null)
|
|
67
|
+
assert.equal(resolveSpec('../../x.js', 'src/a.js', idx), null, '爬出工作区不算')
|
|
68
|
+
assert.equal(resolveSpec('./nope', 'src/a.js', idx), null, '候选集里没有就不算')
|
|
69
|
+
assert.equal(resolveSpec('', 'src/a.js', idx), null)
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
/* ── 3 · resolveSpec:扩展名补全顺序 ──────────────────── */
|
|
73
|
+
|
|
74
|
+
test('resolveSpec:原样 → 补扩展名 → `/index.<ext>`,顺序固定', () => {
|
|
75
|
+
const idx = createIndex(['src/a.js', 'src/b.ts', 'lib/index.js', 'docs/c.md', 'src/d.mjs'])
|
|
76
|
+
assert.equal(resolveSpec('./b.ts', 'src/a.js', idx), 'src/b.ts', '原样命中')
|
|
77
|
+
assert.equal(resolveSpec('./b', 'src/a.js', idx), 'src/b.ts', '补扩展名')
|
|
78
|
+
assert.equal(resolveSpec('./d', 'src/a.js', idx), 'src/d.mjs', '补 .mjs')
|
|
79
|
+
assert.equal(resolveSpec('../lib', 'src/a.js', idx), 'lib/index.js', '补 /index.<ext>')
|
|
80
|
+
assert.equal(resolveSpec('../docs/c.md', 'src/a.js', idx), 'docs/c.md', '可以指向 md')
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
/* ── 4 · testsOf:命名约定 + 歧义不算 ─────────────────── */
|
|
84
|
+
|
|
85
|
+
test('testsOf:双向都能认(被测方 / 测试方),用的都是文件名这条可核验的事实', () => {
|
|
86
|
+
const idx = createIndex(['src/host/links.js', 'test/links.test.mjs', 'src/util.js'])
|
|
87
|
+
assert.equal(testsOf('src/host/links.js', idx), 'test/links.test.mjs')
|
|
88
|
+
assert.equal(testsOf('test/links.test.mjs', idx), 'src/host/links.js')
|
|
89
|
+
// 没有任何测试文件的篇不产生关系
|
|
90
|
+
assert.equal(testsOf('src/util.js', idx), null)
|
|
91
|
+
assert.equal(testsOf('test/links.test.mjs', createIndex(['test/links.test.mjs'])), null)
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
test('testsOf:同名歧义一律不算(与 v0.12 的 basename 规矩一致)', () => {
|
|
95
|
+
const idx = createIndex([
|
|
96
|
+
'src/host/links.js', 'other/links.js', 'test/links.test.mjs',
|
|
97
|
+
])
|
|
98
|
+
assert.equal(testsOf('test/links.test.mjs', idx), null, '两个同名被测方 ⇒ 不算')
|
|
99
|
+
assert.equal(testsOf('src/host/links.js', idx), 'test/links.test.mjs')
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
test('testsOf:同目录的 `.test` / `.spec` 也算,但两个都在就不算', () => {
|
|
103
|
+
const one = createIndex(['src/a.js', 'src/a.test.js'])
|
|
104
|
+
assert.equal(testsOf('src/a.js', one), 'src/a.test.js')
|
|
105
|
+
const two = createIndex(['src/a.js', 'src/a.test.js', 'src/a.spec.js'])
|
|
106
|
+
assert.equal(testsOf('src/a.js', two), null, '两个候选 ⇒ 歧义 ⇒ 不算')
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
/* ── 5 · 源仍然只有 Markdown(D5 的守卫原意) ──────────── */
|
|
110
|
+
|
|
111
|
+
test('buildRelationFacts:md 能指向 md(references)与代码(documents)', () => {
|
|
112
|
+
resetRelationCache()
|
|
113
|
+
const facts = buildRelationFacts([
|
|
114
|
+
cand('docs/spec.md', '设计见 `src/host/links.js` 与 `docs/other.md`'),
|
|
115
|
+
cand('docs/other.md', ''),
|
|
116
|
+
cand('src/host/links.js', ''),
|
|
117
|
+
], { root: 't-refs' })
|
|
118
|
+
|
|
119
|
+
assert.deepEqual(relationsOf(facts, 'docs/spec.md').relations, [
|
|
120
|
+
{ type: 'references', dir: 'out', other: 'docs/other.md', line: 1 },
|
|
121
|
+
{ type: 'documents', dir: 'out', other: 'src/host/links.js', line: 1 },
|
|
122
|
+
])
|
|
123
|
+
// 反向边由同一条边推出来(`in` 的行号属于源文件那一行)
|
|
124
|
+
assert.deepEqual(relationsOf(facts, 'src/host/links.js').relations, [
|
|
125
|
+
{ type: 'documents', dir: 'in', other: 'docs/spec.md', line: 1 },
|
|
126
|
+
])
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
test('buildRelationFacts:代码注释里写个路径**不产生** references / documents', () => {
|
|
130
|
+
resetRelationCache()
|
|
131
|
+
const facts = buildRelationFacts([
|
|
132
|
+
cand('src/util.js', '// see `docs/real.md` for details\n'),
|
|
133
|
+
cand('docs/real.md', '# design\n'),
|
|
134
|
+
], { root: 't-src-only' })
|
|
135
|
+
|
|
136
|
+
assert.deepEqual(relationsOf(facts, 'src/util.js').relations, [], '代码不能当引用关系的源')
|
|
137
|
+
assert.deepEqual(relationsOf(facts, 'docs/real.md').relations, [])
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
test('buildRelationFacts:代码 → 代码是 imports,行号是导入那一行', () => {
|
|
141
|
+
resetRelationCache()
|
|
142
|
+
const facts = buildRelationFacts([
|
|
143
|
+
cand('test/deep-scale.test.mjs', "import { GATES } from '../tools/deep-benchmark.mjs'\n"),
|
|
144
|
+
cand('tools/deep-benchmark.mjs', "import { scan } from '../src/host/index.js'\n"),
|
|
145
|
+
cand('src/host/index.js', ''),
|
|
146
|
+
], { root: 't-imports' })
|
|
147
|
+
|
|
148
|
+
assert.deepEqual(relationsOf(facts, 'test/deep-scale.test.mjs').relations, [
|
|
149
|
+
{ type: 'imports', dir: 'out', other: 'tools/deep-benchmark.mjs', line: 1 },
|
|
150
|
+
])
|
|
151
|
+
assert.deepEqual(relationsOf(facts, 'tools/deep-benchmark.mjs').relations, [
|
|
152
|
+
{ type: 'imports', dir: 'in', other: 'test/deep-scale.test.mjs', line: 1 },
|
|
153
|
+
{ type: 'imports', dir: 'out', other: 'src/host/index.js', line: 1 },
|
|
154
|
+
])
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
test('buildRelationFacts:自定义路径跳到别的篇,不产生边(对端不在候选集)', () => {
|
|
158
|
+
resetRelationCache()
|
|
159
|
+
const facts = buildRelationFacts([
|
|
160
|
+
cand('a.md', '见 `不在候选集里.md`'),
|
|
161
|
+
], { root: 't-outside' })
|
|
162
|
+
assert.deepEqual(relationsOf(facts, 'a.md').relations, [])
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
/* ── 6 · 截断顺序确定性 ───────────────────────────────── */
|
|
166
|
+
|
|
167
|
+
test('relationsOf:截断顺序 = type 优先级 → in 先 → 行号升序 → 对端字典序,并如实给 total', () => {
|
|
168
|
+
resetRelationCache()
|
|
169
|
+
const facts = buildRelationFacts([
|
|
170
|
+
// 三个 references(两个同行 + 一个 in 边)之后再一条 documents ⇒ 3 条上限会把 documents 截掉
|
|
171
|
+
cand('main.md', '见 `z.md` `a.md`\n见 `src/x.js`'),
|
|
172
|
+
cand('hub.md', '第一行\n\n见 `main.md`'),
|
|
173
|
+
cand('z.md', ''),
|
|
174
|
+
cand('a.md', ''),
|
|
175
|
+
cand('src/x.js', ''),
|
|
176
|
+
], { root: 't-truncate' })
|
|
177
|
+
|
|
178
|
+
assert.equal(MAX_RELATIONS_PER_ITEM, 3)
|
|
179
|
+
const got = relationsOf(facts, 'main.md')
|
|
180
|
+
assert.equal(got.total, 4, '被截断的条数要如实给出来')
|
|
181
|
+
assert.deepEqual(got.relations.map((r) => `${r.type}:${r.dir}:${r.other}`), [
|
|
182
|
+
'references:in:hub.md',
|
|
183
|
+
'references:out:a.md',
|
|
184
|
+
'references:out:z.md',
|
|
185
|
+
], 'documents 是优先级最低的一类,先被截掉')
|
|
186
|
+
|
|
187
|
+
// 上限可调(工具侧 2 条走的是同一条路径)
|
|
188
|
+
assert.deepEqual(relationsOf(facts, 'main.md', 2).relations.map((r) => r.other), ['hub.md', 'a.md'])
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
test('buildRelationFacts:同一对(类型 + 方向 + 对端)只留一条,行号取最靠前的', () => {
|
|
192
|
+
resetRelationCache()
|
|
193
|
+
const facts = buildRelationFacts([
|
|
194
|
+
// 两种写法都指向同一篇(`docs/x.md` 与 x.md 在候选集里解析成同一个 rel)
|
|
195
|
+
cand('a.md', '见 `docs/x.md`\n见 `x.md`'),
|
|
196
|
+
cand('docs/x.md', ''),
|
|
197
|
+
], { root: 't-dedup' })
|
|
198
|
+
const got = relationsOf(facts, 'a.md')
|
|
199
|
+
assert.equal(got.total, 1)
|
|
200
|
+
assert.deepEqual(got.relations, [{ type: 'references', dir: 'out', other: 'docs/x.md', line: 1 }])
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
/* ── 7 · 端到端:证据必须能读回原文核验(实现说明 §10.7) ──── */
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* 造一个**真的能互相指到**的工作区。
|
|
207
|
+
*
|
|
208
|
+
* 条目之间的路径故意写成带扩展名的完整相对路径 —— 这样「那一行里应该有对方」
|
|
209
|
+
* 可以用 `basename` 直接断言,不必把解析器再实现一遍(用被测代码去验被测代码,
|
|
210
|
+
* 等于没验)。
|
|
211
|
+
*/
|
|
212
|
+
function refWorkspace() {
|
|
213
|
+
return makeWorkspace([
|
|
214
|
+
['docs/spec.md', [
|
|
215
|
+
'# 插件设计',
|
|
216
|
+
'',
|
|
217
|
+
'实现见 `src/host/plugin.js`(v0.19 起代码也进语料)。',
|
|
218
|
+
'背景见 `docs/other.md`。',
|
|
219
|
+
'',
|
|
220
|
+
].join('\n')],
|
|
221
|
+
['docs/other.md', '# 背景\n\n无关内容。\n'],
|
|
222
|
+
['src/host/plugin.js', [
|
|
223
|
+
"import { load } from '../util/helper.js'",
|
|
224
|
+
'',
|
|
225
|
+
'export function plugin () { return load() }',
|
|
226
|
+
'',
|
|
227
|
+
].join('\n')],
|
|
228
|
+
['src/util/helper.js', 'export function load () { return 1 }\n'],
|
|
229
|
+
])
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
test('端到端: 每条关系都能按 other + line 读回原文,那一行真的有那处提及', async () => {
|
|
233
|
+
const root = refWorkspace()
|
|
234
|
+
resetRelationCache()
|
|
235
|
+
const r = await scan(root, 60, { sort: 'relevance', kind: 'context', query: 'plugin 插件设计 helper' })
|
|
236
|
+
const pack = await buildContextFor(root, { ranked: r.ranked, topic: r.topic, total: r.total })
|
|
237
|
+
const items = [...pack.primary, ...pack.supporting, ...pack.related]
|
|
238
|
+
|
|
239
|
+
let checked = 0
|
|
240
|
+
for (const item of items) {
|
|
241
|
+
for (const rel of (item.relations || [])) {
|
|
242
|
+
// `tests` 的证据是**文件名约定**,不是某一行文字 ⇒ 它没有 line,也不该被读回。
|
|
243
|
+
if (rel.type === 'tests') {
|
|
244
|
+
assert.equal(rel.line, undefined, `tests 不该有行号:${JSON.stringify(rel)}`)
|
|
245
|
+
continue
|
|
246
|
+
}
|
|
247
|
+
assert.ok(Number.isInteger(rel.line) && rel.line >= 1, `行号必须是 1 起的整数:${JSON.stringify(rel)}`)
|
|
248
|
+
|
|
249
|
+
// 行号属于**「谁在提这件事」的那个文件**:out ⇒ 本条目提对方;in ⇒ 对方提本条目。
|
|
250
|
+
// 于是被念到名字的那一篇也随之互换 —— 断言必须跟着换,否则 in 边会被误判成编造。
|
|
251
|
+
const owner = rel.dir === 'in' ? rel.other : item.rel
|
|
252
|
+
const mentioned = rel.dir === 'in' ? item.rel : rel.other
|
|
253
|
+
const text = readFileSync(join(root, owner), 'utf8')
|
|
254
|
+
const line = text.split('\n')[rel.line - 1] || ''
|
|
255
|
+
const base = mentioned.slice(mentioned.lastIndexOf('/') + 1)
|
|
256
|
+
assert.ok(
|
|
257
|
+
line.includes(base),
|
|
258
|
+
`${owner} 第 ${rel.line} 行里没有 ${base} —— 证据是编的:${JSON.stringify(line)}`,
|
|
259
|
+
)
|
|
260
|
+
checked += 1
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
assert.ok(checked >= 3, `这个夹具至少要产出 3 条可核验的关系,实测 ${checked} 条`)
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
test('端到端: 关系是投影 —— 关掉它,三层的成员与顺序逐字相同(实现说明 §10.8)', async () => {
|
|
267
|
+
const root = refWorkspace()
|
|
268
|
+
resetRelationCache()
|
|
269
|
+
const r = await scan(root, 60, { sort: 'relevance', kind: 'context', query: 'plugin 插件设计 helper' })
|
|
270
|
+
const withRel = await buildContextFor(root, { ranked: r.ranked, topic: r.topic, total: r.total })
|
|
271
|
+
const without = await buildContextFor(root, {
|
|
272
|
+
ranked: r.ranked, topic: r.topic, total: r.total, withRelations: false,
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
const relsOf = (pack) => ['primary', 'supporting', 'related']
|
|
276
|
+
.map((tier) => pack[tier].map((i) => i.rel).join(','))
|
|
277
|
+
.join(' | ')
|
|
278
|
+
assert.equal(relsOf(withRel), relsOf(without), '关系不许改变任何一层的成员或顺序')
|
|
279
|
+
|
|
280
|
+
const strip = (item) => {
|
|
281
|
+
const copy = { ...item }
|
|
282
|
+
delete copy.relations
|
|
283
|
+
delete copy.relationsTotal
|
|
284
|
+
return copy
|
|
285
|
+
}
|
|
286
|
+
const flat = (pack) => [...pack.primary, ...pack.supporting, ...pack.related]
|
|
287
|
+
const a = flat(withRel)
|
|
288
|
+
const b = flat(without)
|
|
289
|
+
assert.equal(a.length, b.length)
|
|
290
|
+
for (let i = 0; i < a.length; i += 1) {
|
|
291
|
+
assert.deepEqual(strip(a[i]), b[i], '除 relations / relationsTotal 外,条目必须逐字相同')
|
|
292
|
+
assert.equal(b[i].provenance, a[i].provenance)
|
|
293
|
+
}
|
|
294
|
+
// 反向自检:这个夹具**确实**产出了关系,否则上面那条「相同」是空话。
|
|
295
|
+
assert.ok(a.some((i) => Array.isArray(i.relations) && i.relations.length > 0),
|
|
296
|
+
'夹具没产出任何关系 ⇒ 这条守卫是假的')
|
|
297
|
+
})
|
|
298
|
+
|
|
299
|
+
test('端到端: 面板载荷有界 —— 每条结果的关系投影 ≤ 300 B(实测 ≈228 B)', async () => {
|
|
300
|
+
// 需求 §6.7 的「每文件最多 2~3 条」是**预算约束**,不是建议:
|
|
301
|
+
// 关系只能投影到已进包的那几行,复用短字符串,不许把关系全量塞进 payload。
|
|
302
|
+
const extra = []
|
|
303
|
+
for (let i = 1; i <= 12; i += 1) {
|
|
304
|
+
const refs = [1, 2, 3].map((k) => `docs/peer-${((i + k) % 12) + 1}.md`).join('` `')
|
|
305
|
+
extra.push([`docs/peer-${i}.md`, `# 同伴 ${i}\n\n命中点:payloadbudget 的同伴 ${i}。\n\n见 \`${refs}\`。\n`])
|
|
306
|
+
}
|
|
307
|
+
const root = makeWorkspace(extra)
|
|
308
|
+
resetRelationCache()
|
|
309
|
+
const r = await scan(root, 80, { sort: 'relevance', kind: 'context', query: 'payloadbudget 同伴' })
|
|
310
|
+
const withRel = await buildContextFor(root, { ranked: r.ranked, topic: r.topic, total: r.total })
|
|
311
|
+
const without = await buildContextFor(root, {
|
|
312
|
+
ranked: r.ranked, topic: r.topic, total: r.total, withRelations: false,
|
|
313
|
+
})
|
|
314
|
+
const items = [...withRel.primary, ...withRel.supporting, ...withRel.related]
|
|
315
|
+
assert.ok(items.length >= 5, `要有足够的结果才量得到载荷:${items.length}`)
|
|
316
|
+
assert.ok(items.every((i) => !Array.isArray(i.relations) || i.relations.length <= MAX_RELATIONS_PER_ITEM),
|
|
317
|
+
`每项最多 ${MAX_RELATIONS_PER_ITEM} 条关系`)
|
|
318
|
+
|
|
319
|
+
const bytes = (pack) => Buffer.byteLength(JSON.stringify(pack), 'utf8')
|
|
320
|
+
const relBytes = bytes(withRel) - bytes(without)
|
|
321
|
+
const perItem = relBytes / items.length
|
|
322
|
+
console.log(` R2 实测:面板载荷 ${bytes(withRel)} B(基线 ${bytes(without)} B),`
|
|
323
|
+
+ `关系 ${relBytes} B / ${items.length} 项 = ${perItem.toFixed(0)} B/项`)
|
|
324
|
+
assert.ok(perItem <= 300, `关系投影超预算:${perItem.toFixed(0)} B/项 > 300 B/项`)
|
|
325
|
+
assert.ok(bytes(withRel) < 16000, `面板载荷越过 16000 B 门槛:${bytes(withRel)} B`)
|
|
326
|
+
})
|
|
327
|
+
|
|
328
|
+
/* ── 8 · 工具侧:每项 ≤2 条,一行说完(实现说明 §9) ───────── */
|
|
329
|
+
|
|
330
|
+
test('工具侧: 关系渲染成一行、最多 2 条,方向与行号都如实给出', async () => {
|
|
331
|
+
const { renderToolText } = await import('../src/host/tool.js')
|
|
332
|
+
const item = {
|
|
333
|
+
rel: 'a.md', title: '甲', summary: '', mtimeMs: 1, source: 'doc',
|
|
334
|
+
reason: { code: 'direct', terms: ['甲'], fields: 1, term: '甲' },
|
|
335
|
+
provenance: 'retrieval',
|
|
336
|
+
relations: [
|
|
337
|
+
{ type: 'references', dir: 'out', other: 'docs/other.md', line: 7 },
|
|
338
|
+
{ type: 'imports', dir: 'in', other: 'src/x.js', line: 2 },
|
|
339
|
+
{ type: 'tests', dir: 'out', other: 'test/a.test.mjs' },
|
|
340
|
+
],
|
|
341
|
+
relationsTotal: 5,
|
|
342
|
+
}
|
|
343
|
+
const value = {
|
|
344
|
+
mode: 'relevance', topic: '甲', total: 1,
|
|
345
|
+
primary: [item], supporting: [], related: [], totals: { matched: 1, total: 1 },
|
|
346
|
+
}
|
|
347
|
+
const lines = renderToolText(value).split('\n')
|
|
348
|
+
const relLines = lines.filter((l) => l.startsWith(' relations: '))
|
|
349
|
+
assert.equal(relLines.length, 1, `一条结果最多一行 relations:${relLines.join(' | ')}`)
|
|
350
|
+
assert.equal(relLines[0], ' relations: references → docs/other.md:7; imports ← src/x.js:2',
|
|
351
|
+
'方向(→ 本项引用对方 / ← 对方引用本项)与行号必须都在')
|
|
352
|
+
assert.equal(MAX_TOOL_RELATIONS_PER_ITEM, 2)
|
|
353
|
+
assert.ok(!relLines[0].includes('a.test.mjs'), '第三条(tests)必须被截掉')
|
|
354
|
+
// 没有关系就不渲染这一行(不写「无」)
|
|
355
|
+
const bare = renderToolText({
|
|
356
|
+
...value,
|
|
357
|
+
primary: [{ ...item, relations: [] }],
|
|
358
|
+
})
|
|
359
|
+
assert.ok(!bare.includes(' relations: '), '没有关系时不许出现空行')
|
|
360
|
+
})
|
package/test/tier1.test.mjs
CHANGED
|
@@ -454,7 +454,7 @@ test('过滤:输入即缩小列表并显示命中计数', async () => {
|
|
|
454
454
|
let nodes = await mounted.renderAgain(true)
|
|
455
455
|
|
|
456
456
|
assert.equal(byClass(nodes, 'knit-doc').length, 3, '初始 3 篇')
|
|
457
|
-
assert.equal(textByClass(nodes, 'knit-count'), '3
|
|
457
|
+
assert.equal(textByClass(nodes, 'knit-count'), '3', 'v0.21.x:计数只剩阿拉伯数字')
|
|
458
458
|
|
|
459
459
|
const input = byClass(nodes, 'knit-filter')[0]
|
|
460
460
|
assert.ok(input, '应有过滤输入框')
|
|
@@ -462,7 +462,7 @@ test('过滤:输入即缩小列表并显示命中计数', async () => {
|
|
|
462
462
|
nodes = await mounted.renderAgain()
|
|
463
463
|
|
|
464
464
|
assert.equal(byClass(nodes, 'knit-doc').length, 1, '应只剩竞品分析')
|
|
465
|
-
assert.equal(textByClass(nodes, 'knit-count'), '1 / 3
|
|
465
|
+
assert.equal(textByClass(nodes, 'knit-count'), '1 / 3', '应显示命中计数(只有数字)')
|
|
466
466
|
})
|
|
467
467
|
|
|
468
468
|
test('过滤:能按摘要和路径匹配,大小写不敏感', async () => {
|
package/test/tool.test.mjs
CHANGED
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
} from '../src/host/tool.js'
|
|
24
24
|
import { scan, apply, readDocument, buildContextFor, publicScanPayload } from '../src/host/index.js'
|
|
25
25
|
import { SNIPPET_CHARS } from '../src/host/passage.js'
|
|
26
|
+
import { MAX_TOOL_RELATIONS_PER_ITEM } from '../src/host/relations.js'
|
|
26
27
|
// 校验器与真机探针**共用同一份** —— 两边各写一份必然漂移,
|
|
27
28
|
// 而它锁的恰恰是「工具返回值 vs 它自己的 schema」这件最容易腐坏的事。
|
|
28
29
|
import { validateAgainst } from '../tools/json-schema-subset.mjs'
|
|
@@ -209,7 +210,21 @@ test('定义形状: 参数与输出 schema 与 defineTool 的编译产物一致'
|
|
|
209
210
|
assert.equal(items.additionalProperties, false, `${tier} 的条目必须是严格对象`)
|
|
210
211
|
assert.deepEqual(
|
|
211
212
|
items.required,
|
|
212
|
-
['rel', 'title', 'summary', 'mtimeMs', 'kind', 'source', 'reason'],
|
|
213
|
+
['rel', 'title', 'summary', 'mtimeMs', 'kind', 'source', 'reason', 'provenance'],
|
|
214
|
+
)
|
|
215
|
+
// v0.21:来源三值必须**声明**(同 `kind` 那条教训 —— 装配层每条都给,
|
|
216
|
+
// schema 少声明一个字段就会在**校验层**静默失败),且不是 `source` 的替代品。
|
|
217
|
+
assert.deepEqual(
|
|
218
|
+
items.properties.provenance.enum,
|
|
219
|
+
['retrieval', 'relation', 'manual'],
|
|
220
|
+
)
|
|
221
|
+
assert.deepEqual(
|
|
222
|
+
Object.keys(items.properties.relations.items.properties), ['type', 'dir', 'other', 'line'],
|
|
223
|
+
`${tier} 的关系事实只允许这四个字段(不下发正文、不下发绝对路径)`,
|
|
224
|
+
)
|
|
225
|
+
assert.deepEqual(
|
|
226
|
+
items.properties.relations.items.required, ['type', 'dir', 'other'],
|
|
227
|
+
'`line` 是可选:`tests` 的证据是文件名约定,没有行号,不编一个',
|
|
213
228
|
)
|
|
214
229
|
// 定义里不许出现 score / raw —— 相对分数会被模型当成绝对置信度
|
|
215
230
|
assert.ok(!Object.keys(items.properties).includes('score'), `${tier} 里不该有 score`)
|
|
@@ -440,6 +455,69 @@ test('集成: 同一条任务下,knit_docs 的命中段与面板载荷逐条
|
|
|
440
455
|
}
|
|
441
456
|
})
|
|
442
457
|
|
|
458
|
+
test('集成: 同一条任务下,关系行在面板与工具两侧同源(v0.21 §10.17)', async () => {
|
|
459
|
+
// 与上一条同一个承诺:一个引擎、两个面。面板每项最多投影 3 条、工具最多 2 条
|
|
460
|
+
// (§6.7 的载荷预算),但**前 2 条必须逐字相同** —— 否则用户点开的证据行
|
|
461
|
+
// 与模型读到的行号会是两回事,而两侧各自的测试都还是绿的。
|
|
462
|
+
const root = makeWorkspace([
|
|
463
|
+
['docs/spec.md', '# 规格\n\n实现见 `src/host/plugin.js`,另见 `docs/other.md` 与 `docs/third.md`。\n'],
|
|
464
|
+
['docs/other.md', '# 另一篇\n\n补充说明。\n'],
|
|
465
|
+
['docs/third.md', '# 第三篇\n\n补充说明。\n'],
|
|
466
|
+
['src/host/plugin.js', "import { load } from '../util/helper.js'\n\nexport const plugin = load()\n"],
|
|
467
|
+
['src/util/helper.js', 'export const load = () => 1\n'],
|
|
468
|
+
])
|
|
469
|
+
const query = 'plugin 规格 helper'
|
|
470
|
+
const session = fakeSession([userMessage(1, query)], { cwd: root, id: 'sess-rel-face' })
|
|
471
|
+
|
|
472
|
+
// ① 面板路径:宿主真实的那次装配(`contextPayload` 就是这么调的)
|
|
473
|
+
const payload = await scan(root, 40, {
|
|
474
|
+
session, sessionId: 'sess-rel-face', sort: 'relevance', kind: 'context', query,
|
|
475
|
+
})
|
|
476
|
+
const pack = await buildContextFor(root, {
|
|
477
|
+
ranked: payload.ranked, topic: payload.topic, task: payload.task, total: payload.total,
|
|
478
|
+
})
|
|
479
|
+
const panelItems = new Map()
|
|
480
|
+
for (const tier of ['pinned', 'primary', 'supporting', 'related']) {
|
|
481
|
+
for (const item of pack[tier] || []) panelItems.set(item.rel, item)
|
|
482
|
+
}
|
|
483
|
+
const panelWithRel = [...panelItems].filter(([, item]) => (item.relations || []).length > 0)
|
|
484
|
+
assert.ok(panelWithRel.length > 0, '夹具里至少该有一项带关系,否则这条是空测试')
|
|
485
|
+
|
|
486
|
+
// ② 工具路径:喂**同一份** scan 产物,差异只可能来自工具自己的投影
|
|
487
|
+
const value = await runTool(
|
|
488
|
+
fakeExec(session), { limit: 40, query }, { scan: async () => payload, read: readDocument },
|
|
489
|
+
)
|
|
490
|
+
const items = allItems(value)
|
|
491
|
+
|
|
492
|
+
for (const [rel, panel] of panelWithRel) {
|
|
493
|
+
const item = items.find((entry) => entry.rel === rel)
|
|
494
|
+
assert.ok(item, `面板给出了 ${rel} 的关系,工具也必须给出这一篇`)
|
|
495
|
+
assert.deepEqual(
|
|
496
|
+
item.relations,
|
|
497
|
+
panel.relations.slice(0, MAX_TOOL_RELATIONS_PER_ITEM),
|
|
498
|
+
`${rel} 的关系:工具必须是面板的前 ${MAX_TOOL_RELATIONS_PER_ITEM} 条(同一条、同一序号)`,
|
|
499
|
+
)
|
|
500
|
+
}
|
|
501
|
+
// ③ 反向:工具给了关系的,面板也不能缺
|
|
502
|
+
for (const item of items) {
|
|
503
|
+
if (!Array.isArray(item.relations) || item.relations.length === 0) continue
|
|
504
|
+
assert.ok((panelItems.get(item.rel) || {}).relations, `工具给出了 ${item.rel} 的关系,面板也必须带`)
|
|
505
|
+
}
|
|
506
|
+
// ④ 截断口径:工具侧最多 2 条,被截时如实写总数(不是「那就是全部」)
|
|
507
|
+
let truncated = 0
|
|
508
|
+
for (const item of items) {
|
|
509
|
+
if (!Array.isArray(item.relations)) continue
|
|
510
|
+
assert.ok(item.relations.length <= MAX_TOOL_RELATIONS_PER_ITEM)
|
|
511
|
+
const panel = panelItems.get(item.rel)
|
|
512
|
+
if (panel.relations.length > MAX_TOOL_RELATIONS_PER_ITEM) {
|
|
513
|
+
truncated += 1
|
|
514
|
+
assert.equal(item.relationsTotal, panel.relationsTotal || panel.relations.length,
|
|
515
|
+
`${item.rel} 被截断了,「relationsTotal」必须如实说出真数`)
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
assert.ok(truncated > 0, '夹具里至少该有一项超过 2 条关系,否则截断那一段没被验到')
|
|
519
|
+
})
|
|
520
|
+
|
|
443
521
|
test('agentScope: 从 exec 里取出的三个字段都可用', () => {
|
|
444
522
|
const session = fakeSession([])
|
|
445
523
|
const scope = agentScope(fakeExec(session))
|