dsh-knit 0.14.0 → 0.16.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 +279 -0
- package/README.en.md +81 -23
- package/README.md +71 -20
- package/package.json +3 -2
- package/src/client/client.js +237 -98
- package/src/host/feedback.js +858 -0
- package/src/host/index.js +171 -7
- package/src/host/tool.js +63 -3
- package/test/client.test.mjs +279 -31
- package/test/context-delta.test.mjs +322 -0
- package/test/context-effectiveness.test.mjs +309 -0
- package/test/context-feedback-eval.test.mjs +220 -0
- package/test/feedback.test.mjs +247 -0
- package/test/host-http.test.mjs +249 -3
- package/test/i18n.test.mjs +22 -23
- package/test/path.test.mjs +6 -6
- package/test/peek.test.mjs +80 -26
- package/test/tool.test.mjs +132 -5
- package/tools/context-feedback-eval.mjs +362 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context Feedback Eval 的**确定性**对照(v0.15)。
|
|
3
|
+
*
|
|
4
|
+
* 这里不是「再测一遍 feedback.js」—— 那些在 `test/feedback.test.mjs` 与
|
|
5
|
+
* `test/context-delta.test.mjs` 里。这里测的是**量尺本身在真实装配管线上成立**:
|
|
6
|
+
* 同一个工作区、同一批事件,只改一个变量 —— **包交没交给 agent**。
|
|
7
|
+
*
|
|
8
|
+
* Control(`deliver:false`) :没人给过上下文 ⇒ 那次读必须落在包外
|
|
9
|
+
* Treatment(`deliver:true`) :读了「先看」的那一篇 ⇒ 必须记成 follow-through
|
|
10
|
+
*
|
|
11
|
+
* 这条对照是这个功能**唯一**能自证的地方:如果 Control 臂也能测出「读命中 primary」,
|
|
12
|
+
* 那这个量尺就是在自说自话(`tools/context-feedback-eval.mjs --control` 里同一条判据)。
|
|
13
|
+
*
|
|
14
|
+
* ⚠️ 工作区一律来自 `makeWorkspace()`(AGENTS.md §6.8:样本不许借仓库里的真实文件),
|
|
15
|
+
* 且用 `quantum-anchor` 这种**只出现在一篇里**的词把 primary 钉死 —— 靠「BM25 大概会选它」
|
|
16
|
+
* 的断言会随排序引擎的调整漂移。
|
|
17
|
+
*/
|
|
18
|
+
import test from 'node:test'
|
|
19
|
+
import assert from 'node:assert/strict'
|
|
20
|
+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
|
|
21
|
+
import { tmpdir } from 'node:os'
|
|
22
|
+
import { join } from 'node:path'
|
|
23
|
+
|
|
24
|
+
import { makeWorkspace } from './fixture.mjs'
|
|
25
|
+
import {
|
|
26
|
+
findLatestSessionLog, findSessionLogById, metricsFor, parseEvents, replaySession, scopeFor,
|
|
27
|
+
} from '../tools/context-feedback-eval.mjs'
|
|
28
|
+
|
|
29
|
+
/** 目标文档:`quantum-anchor` 只在这里出现,所以它必须是 primary。 */
|
|
30
|
+
const TARGET = 'docs/target.md'
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 造一个工作区。`extra` 走 `makeWorkspace()` —— 里面已经有 README / 笔记 / CHANGELOG 三篇。
|
|
34
|
+
* @returns {string} 工作区绝对路径
|
|
35
|
+
*/
|
|
36
|
+
function workspace() {
|
|
37
|
+
return makeWorkspace([
|
|
38
|
+
[TARGET, '# 排序目标\n\n唯一命中词 quantum-anchor 出现在这篇里,相关性排序也提到了。\n'],
|
|
39
|
+
])
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** 用户消息(`isSubstantiveTask` 认的那种)。 */
|
|
43
|
+
function userMessage(seq, text) {
|
|
44
|
+
return {
|
|
45
|
+
type: 'user/message',
|
|
46
|
+
seq,
|
|
47
|
+
time: 1000 + seq,
|
|
48
|
+
data: { role: 'user', content: [{ type: 'text', text }], source: { kind: 'user' } },
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** 一次真实的 `read` 调用(`arguments` 是 JSON **字符串** —— 宿主就是这样存的)。 */
|
|
53
|
+
function readCall(seq, filePath) {
|
|
54
|
+
const callId = `c${seq}`
|
|
55
|
+
return {
|
|
56
|
+
type: 'tool/call',
|
|
57
|
+
seq,
|
|
58
|
+
time: 1000 + seq,
|
|
59
|
+
data: { turn: 1, step: 1, callId, name: 'read', arguments: JSON.stringify({ file_path: filePath }) },
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** 对应的成功返回。 */
|
|
64
|
+
function toolResult(seq, callSeq) {
|
|
65
|
+
const callId = `c${callSeq}`
|
|
66
|
+
return {
|
|
67
|
+
type: 'tool/result',
|
|
68
|
+
seq,
|
|
69
|
+
time: 1000 + seq,
|
|
70
|
+
sourceEventSeqs: [callSeq],
|
|
71
|
+
data: {
|
|
72
|
+
turn: 1, step: 1,
|
|
73
|
+
message: {
|
|
74
|
+
role: 'tool',
|
|
75
|
+
source: { kind: 'tool', callId },
|
|
76
|
+
toolCallId: callId,
|
|
77
|
+
content: [{ type: 'text', text: '<path>…</path>' }],
|
|
78
|
+
isError: false,
|
|
79
|
+
id: `m${seq}`,
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 一份完整的事件流:一句话 → 读「先看」那篇 → 再读一个 Knit 根本索引不到的文件。
|
|
87
|
+
* @returns {object[]} 事件
|
|
88
|
+
*/
|
|
89
|
+
function treatmentEvents() {
|
|
90
|
+
return [
|
|
91
|
+
userMessage(1, '项目的相关性排序是怎么算的?quantum-anchor 在哪一篇里'),
|
|
92
|
+
readCall(2, TARGET),
|
|
93
|
+
toolResult(3, 2),
|
|
94
|
+
readCall(4, 'package.json'),
|
|
95
|
+
toolResult(5, 4),
|
|
96
|
+
]
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
test('评测:交过包才能记成 follow-through —— Control 与 Treatment 只差这一个变量', async () => {
|
|
100
|
+
const root = workspace()
|
|
101
|
+
const events = treatmentEvents()
|
|
102
|
+
|
|
103
|
+
const control = await replaySession(events, { root, sessionId: 'ctl', deliver: false })
|
|
104
|
+
const treated = await replaySession(events, { root, sessionId: 'trt', deliver: true })
|
|
105
|
+
|
|
106
|
+
// 前提:这篇确实是「先看」的那一篇(否则下面两条断言测的是别的东西)
|
|
107
|
+
assert.equal(treated.metrics.primaryRel, TARGET, 'quantum-anchor 应当把这篇钉成 primary')
|
|
108
|
+
|
|
109
|
+
// Treatment:读了 primary ⇒ follow-through,第一次读落在 primary 层
|
|
110
|
+
assert.equal(treated.packs > 0, true, 'Treatment 必须真的交过包')
|
|
111
|
+
assert.equal(treated.metrics.primaryRead, true)
|
|
112
|
+
assert.equal(treated.metrics.firstReadTier, 'primary')
|
|
113
|
+
assert.equal(treated.metrics.firstReadRel, TARGET)
|
|
114
|
+
|
|
115
|
+
// Control:一份包都没交 ⇒ 那次读只能落在包外,且**不许**记成 follow-through
|
|
116
|
+
assert.equal(control.packs, 0)
|
|
117
|
+
assert.equal(control.metrics.primaryRead, false)
|
|
118
|
+
// v0.16:读过但没人在那一层给过上下文 ⇒ 'outside'(null 只留给「一次都没读」)
|
|
119
|
+
assert.equal(control.metrics.firstReadTier, 'outside')
|
|
120
|
+
assert.equal(control.metrics.outside, 2, '两次读都该在包外')
|
|
121
|
+
|
|
122
|
+
// 两边的读**次数**一样 —— 变的只是「有没有人给过上下文」
|
|
123
|
+
assert.equal(control.metrics.reads, treated.metrics.reads)
|
|
124
|
+
assert.equal(control.metrics.distinct, treated.metrics.distinct)
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
test('评测:包外的读要拆成「Knit 看得到却没进包」与「Knit 根本不索引」', async () => {
|
|
128
|
+
const root = workspace()
|
|
129
|
+
const result = await replaySession(treatmentEvents(), { root, sessionId: 'scope' })
|
|
130
|
+
|
|
131
|
+
// `package.json` 不是 `.md` ⇒ 永远不可能进包。它**不算漏**。
|
|
132
|
+
assert.deepEqual(result.scope.outOfScopeReads, ['package.json'])
|
|
133
|
+
assert.equal(result.scope.outOfScope, 1)
|
|
134
|
+
// 被读的那篇 `.md` 在扫描结果里、也在包里 ⇒ 既不是漏也不是域外
|
|
135
|
+
assert.equal(result.scope.inScope, 1)
|
|
136
|
+
assert.deepEqual(result.scope.missed, [])
|
|
137
|
+
|
|
138
|
+
// Control 臂里,同一篇 `.md` 就变成了**真的漏**(Knit 看得到它,却没人把包交出去)
|
|
139
|
+
const control = await replaySession(treatmentEvents(), { root, sessionId: 'scope-ctl', deliver: false })
|
|
140
|
+
assert.deepEqual(control.scope.missed, [TARGET])
|
|
141
|
+
assert.equal(control.scope.outOfScope, 1)
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
test('评测:话题换了之后快照跟着换(churn 记的是事实,不是估算)', async () => {
|
|
145
|
+
const root = workspace()
|
|
146
|
+
const events = [
|
|
147
|
+
...treatmentEvents(),
|
|
148
|
+
userMessage(6, '换个话题:把发布记录整理一下'),
|
|
149
|
+
readCall(7, TARGET),
|
|
150
|
+
toolResult(8, 7),
|
|
151
|
+
]
|
|
152
|
+
const result = await replaySession(events, { root, sessionId: 'churn' })
|
|
153
|
+
|
|
154
|
+
assert.equal(result.metrics.contextChanges >= 1, true, '任务变了必须换一份包')
|
|
155
|
+
assert.equal(result.metrics.taskChanged >= 1, true)
|
|
156
|
+
assert.equal(result.metrics.reads, 3)
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
test('评测:同样的输入必须给出逐字一样的读数(两次回放 deepEqual)', async () => {
|
|
160
|
+
const root = workspace()
|
|
161
|
+
const first = await replaySession(treatmentEvents(), { root, sessionId: 'det' })
|
|
162
|
+
const second = await replaySession(treatmentEvents(), { root, sessionId: 'det' })
|
|
163
|
+
assert.deepEqual(first.metrics, second.metrics)
|
|
164
|
+
assert.deepEqual(first.scope, second.scope)
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
test('评测:坏行不废整批(半截的尾行很常见)', () => {
|
|
168
|
+
const text = [
|
|
169
|
+
'{"type":"session","id":"s","cwd":"/tmp"}',
|
|
170
|
+
'{"type":"user/message","seq":1,"data":{}}',
|
|
171
|
+
'{"broken"',
|
|
172
|
+
'',
|
|
173
|
+
'不是 JSON',
|
|
174
|
+
].join('\n')
|
|
175
|
+
const events = parseEvents(text)
|
|
176
|
+
assert.equal(events.length, 2)
|
|
177
|
+
assert.equal(events[0].type, 'session')
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
test('评测:metricsFor 的键是稳定的(下游脚本靠它)', () => {
|
|
181
|
+
const metrics = metricsFor(null)
|
|
182
|
+
assert.deepEqual(Object.keys(metrics).sort(), [
|
|
183
|
+
'appeared', 'contextChanges', 'continuedReadsAfterExit', 'disappeared', 'distinct', 'epochs',
|
|
184
|
+
'firstReadRel', 'firstReadTier', 'moved', 'outside', 'outsideReads', 'primaryRead', 'primaryRel',
|
|
185
|
+
'reads', 'reentries', 'supportingCoverage', 'taskChanged',
|
|
186
|
+
])
|
|
187
|
+
assert.equal(metrics.reads, 0)
|
|
188
|
+
assert.equal(metrics.primaryRead, false)
|
|
189
|
+
assert.equal(metrics.firstReadTier, null)
|
|
190
|
+
assert.equal(metrics.epochs, 0)
|
|
191
|
+
assert.equal(metrics.continuedReadsAfterExit, 0)
|
|
192
|
+
assert.equal(metrics.reentries, 0)
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
test('评测:日志文件名按形状认(v0–v4 三代并存),不写死 v4', () => {
|
|
196
|
+
const dir = mkdtempSync(join(tmpdir(), 'knit-sessions-'))
|
|
197
|
+
try {
|
|
198
|
+
const ws = join(dir, 'workspace')
|
|
199
|
+
// 老会话只留 v3(本机 98 份这种),新会话是 v4
|
|
200
|
+
const old3 = join(ws, 'sess-old')
|
|
201
|
+
const new4 = join(ws, 'sess-new')
|
|
202
|
+
mkdirSync(old3, { recursive: true })
|
|
203
|
+
mkdirSync(new4, { recursive: true })
|
|
204
|
+
writeFileSync(join(old3, 'session.v3.jsonl.zstd'), 'x')
|
|
205
|
+
writeFileSync(join(new4, 'session.v4.jsonl.zstd'), 'y')
|
|
206
|
+
|
|
207
|
+
assert.equal(findSessionLogById('sess-old', dir), join(old3, 'session.v3.jsonl.zstd'))
|
|
208
|
+
assert.equal(findSessionLogById('sess-new', dir), join(new4, 'session.v4.jsonl.zstd'))
|
|
209
|
+
assert.equal(findSessionLogById('nope', dir), null)
|
|
210
|
+
// 「最近一份」按 mtime 选,不按版本号选
|
|
211
|
+
assert.equal(findLatestSessionLog(dir), join(new4, 'session.v4.jsonl.zstd'))
|
|
212
|
+
} finally {
|
|
213
|
+
rmSync(dir, { recursive: true, force: true })
|
|
214
|
+
}
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
test('评测:scopeFor 不认识 usage 时给空拆分,不抛', () => {
|
|
218
|
+
const scope = scopeFor(null, ['a.md'])
|
|
219
|
+
assert.deepEqual(scope, { inScope: 0, missed: [], missedReads: 0, outOfScope: 0, outOfScopeReads: [] })
|
|
220
|
+
})
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Knit v0.15 · **read evidence adapter 与路径归一化的单元测试**
|
|
3
|
+
*
|
|
4
|
+
* 被测对象是 `src/host/feedback.js` 里**最靠近真实世界**的那一层:
|
|
5
|
+
* 会话事件 → 「Agent 真的读了哪一篇」。这一层的错法只有两种,两种都很贵:
|
|
6
|
+
* - **多记**(把 `grep` 命中、失败的读取、别人的路径算成读)→ Usage 变成假证据;
|
|
7
|
+
* - **少记**(把真实的读漏掉)→ Usage 永远说「没读」。
|
|
8
|
+
*
|
|
9
|
+
* 事件形状全部照抄 2026-09-30 实测的真实会话文件
|
|
10
|
+
* (`~/.dsh/sessions/…/session.v4.jsonl.zstd`):
|
|
11
|
+
* `tool/call` `data = {turn, step, callId, name, arguments}`(`arguments` 是 **JSON 字符串**)
|
|
12
|
+
* `tool/result` `data = {turn, step, message}`,`message = {toolCallId, source:{callId}, isError, …}`
|
|
13
|
+
*/
|
|
14
|
+
import test from 'node:test'
|
|
15
|
+
import assert from 'node:assert/strict'
|
|
16
|
+
|
|
17
|
+
import {
|
|
18
|
+
normalizeRel, normalizeReadEvidence, snapshotOf, diffContext,
|
|
19
|
+
MAX_PENDING_CALLS,
|
|
20
|
+
} from '../src/host/feedback.js'
|
|
21
|
+
|
|
22
|
+
const ROOT = '/w'
|
|
23
|
+
|
|
24
|
+
/** 造一个事件。 */
|
|
25
|
+
function ev(seq, type, data, time = seq * 1000) {
|
|
26
|
+
return { type, seq, time, data }
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** 造一个 `tool/call`。 */
|
|
30
|
+
function call(seq, callId, name, args) {
|
|
31
|
+
return ev(seq, 'tool/call', {
|
|
32
|
+
turn: 1, step: 1, callId, name, arguments: JSON.stringify(args),
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 造一个 `tool/result`。 */
|
|
37
|
+
function result(seq, callId, { isError = false } = {}) {
|
|
38
|
+
return ev(seq, 'tool/result', {
|
|
39
|
+
turn: 1,
|
|
40
|
+
step: 1,
|
|
41
|
+
message: {
|
|
42
|
+
role: 'tool',
|
|
43
|
+
toolCallId: callId,
|
|
44
|
+
source: { kind: 'tool', callId },
|
|
45
|
+
isError,
|
|
46
|
+
content: [{ type: 'text', text: '<path>/w/a.md</path>\n<content>x</content>' }],
|
|
47
|
+
},
|
|
48
|
+
})
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
test('路径:绝对路径落在工作区里就转成相对路径,落在外面就丢掉', () => {
|
|
52
|
+
assert.equal(normalizeRel(ROOT, '/w/a/b.md'), 'a/b.md')
|
|
53
|
+
assert.equal(normalizeRel(ROOT, '/w/a/b.md'), 'a/b.md')
|
|
54
|
+
assert.equal(normalizeRel(ROOT, 'a/b.md'), 'a/b.md')
|
|
55
|
+
assert.equal(normalizeRel(ROOT, './a/b.md'), 'a/b.md')
|
|
56
|
+
assert.equal(normalizeRel(ROOT, 'a//b.md'), 'a/b.md')
|
|
57
|
+
// 工作区之外 —— 一个字都不能留
|
|
58
|
+
assert.equal(normalizeRel(ROOT, '/other/x.md'), '')
|
|
59
|
+
assert.equal(normalizeRel(ROOT, '/w/../x.md'), '')
|
|
60
|
+
assert.equal(normalizeRel(ROOT, '../x.md'), '')
|
|
61
|
+
assert.equal(normalizeRel(ROOT, '../../etc/passwd'), '')
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
test('路径:URL、空串、非法类型、超长、NUL 一律丢掉(不猜)', () => {
|
|
65
|
+
assert.equal(normalizeRel(ROOT, 'https://example.com/a.md'), '')
|
|
66
|
+
assert.equal(normalizeRel(ROOT, 'file:///w/a.md'), '')
|
|
67
|
+
assert.equal(normalizeRel(ROOT, ''), '')
|
|
68
|
+
assert.equal(normalizeRel(ROOT, ' '), '')
|
|
69
|
+
assert.equal(normalizeRel(ROOT, null), '')
|
|
70
|
+
assert.equal(normalizeRel(ROOT, 42), '')
|
|
71
|
+
assert.equal(normalizeRel(ROOT, { rel: 'a.md' }), '')
|
|
72
|
+
assert.equal(normalizeRel(ROOT, `a${'b'.repeat(2000)}.md`), '')
|
|
73
|
+
assert.equal(normalizeRel(ROOT, 'a\0b.md'), '')
|
|
74
|
+
assert.equal(normalizeRel('', '/w/a.md'), '')
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('证据:`read` 的调用 + 成功结果 = 一次真实的读', () => {
|
|
78
|
+
const events = [
|
|
79
|
+
call(10, 'c1', 'read', { file_path: '/w/knit/src/host/relevance.js' }),
|
|
80
|
+
result(11, 'c1'),
|
|
81
|
+
]
|
|
82
|
+
const { reads, cursor } = normalizeReadEvidence(events, { root: ROOT, fromSeq: -1 })
|
|
83
|
+
assert.equal(reads.length, 1)
|
|
84
|
+
assert.equal(reads[0].rel, 'knit/src/host/relevance.js')
|
|
85
|
+
assert.equal(reads[0].callSeq, 10)
|
|
86
|
+
assert.equal(reads[0].seq, 11)
|
|
87
|
+
assert.equal(reads[0].time, 11000)
|
|
88
|
+
assert.equal(cursor, 11)
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
test('证据:只有非 read 的工具时一条都不记 —— grep / glob 命中不是读过(规格 §9)', () => {
|
|
92
|
+
const events = [
|
|
93
|
+
call(1, 'c1', 'glob', { pattern: '**/*.md' }),
|
|
94
|
+
result(2, 'c1'),
|
|
95
|
+
call(3, 'c2', 'grep', { pattern: 'bm25', path: '/w/README.md' }),
|
|
96
|
+
result(4, 'c2'),
|
|
97
|
+
call(5, 'c3', 'bash', { command: 'cat /w/a.md' }),
|
|
98
|
+
result(6, 'c3'),
|
|
99
|
+
call(7, 'c4', 'knit_docs', { limit: 5 }),
|
|
100
|
+
result(8, 'c4'),
|
|
101
|
+
]
|
|
102
|
+
const { reads } = normalizeReadEvidence(events, { root: ROOT })
|
|
103
|
+
assert.deepEqual(reads, [])
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
test('证据:失败的读不记账(isError:true)', () => {
|
|
107
|
+
const events = [
|
|
108
|
+
call(1, 'c1', 'read', { file_path: '/w/a.md' }),
|
|
109
|
+
result(2, 'c1', { isError: true }),
|
|
110
|
+
call(3, 'c2', 'read', { file_path: '/w/b.md' }),
|
|
111
|
+
result(4, 'c2'),
|
|
112
|
+
]
|
|
113
|
+
const { reads } = normalizeReadEvidence(events, { root: ROOT })
|
|
114
|
+
assert.equal(reads.length, 1)
|
|
115
|
+
assert.equal(reads[0].rel, 'b.md')
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
test('证据:工作区之外的读不记账', () => {
|
|
119
|
+
const events = [
|
|
120
|
+
call(1, 'c1', 'read', { file_path: '/etc/passwd' }),
|
|
121
|
+
result(2, 'c1'),
|
|
122
|
+
call(3, 'c2', 'read', { file_path: '/w/ok.md' }),
|
|
123
|
+
result(4, 'c2'),
|
|
124
|
+
]
|
|
125
|
+
const { reads } = normalizeReadEvidence(events, { root: ROOT })
|
|
126
|
+
assert.deepEqual(reads.map((r) => r.rel), ['ok.md'])
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
test('证据:`arguments` 不是合法 JSON 时丢掉,不猜路径', () => {
|
|
130
|
+
const events = [
|
|
131
|
+
{ type: 'tool/call', seq: 1, time: 1, data: { callId: 'c1', name: 'read', arguments: '{oops' } },
|
|
132
|
+
{ type: 'tool/result', seq: 2, time: 2, data: { message: { toolCallId: 'c1', isError: false } } },
|
|
133
|
+
]
|
|
134
|
+
const { reads } = normalizeReadEvidence(events, { root: ROOT })
|
|
135
|
+
assert.deepEqual(reads, [])
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
test('证据:`arguments` 已经是对象时也认(适配两种投递形态)', () => {
|
|
139
|
+
const events = [
|
|
140
|
+
{ type: 'tool/call', seq: 1, time: 1, data: { callId: 'c1', name: 'read', arguments: { path: '/w/x.md' } } },
|
|
141
|
+
{ type: 'tool/result', seq: 2, time: 2, data: { message: { toolCallId: 'c1' } } },
|
|
142
|
+
]
|
|
143
|
+
const { reads } = normalizeReadEvidence(events, { root: ROOT })
|
|
144
|
+
assert.deepEqual(reads.map((r) => r.rel), ['x.md'])
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
test('证据:还没返回的调用挂在 pending 里,跨轮次才配上对', () => {
|
|
148
|
+
const first = normalizeReadEvidence([
|
|
149
|
+
call(1, 'c1', 'read', { file_path: '/w/a.md' }),
|
|
150
|
+
], { root: ROOT, fromSeq: -1 })
|
|
151
|
+
assert.deepEqual(first.reads, [])
|
|
152
|
+
assert.equal(first.pending.size, 1)
|
|
153
|
+
|
|
154
|
+
// 第二轮:结果到了,游标推进到 1,pending 从上一次带过来
|
|
155
|
+
const second = normalizeReadEvidence([
|
|
156
|
+
result(2, 'c1'),
|
|
157
|
+
], { root: ROOT, fromSeq: first.cursor, pending: first.pending })
|
|
158
|
+
assert.deepEqual(second.reads.map((r) => r.rel), ['a.md'])
|
|
159
|
+
assert.equal(second.pending.size, 0)
|
|
160
|
+
assert.equal(second.cursor, 2)
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
test('证据:游标是幂等的 —— 同一批事件喂两遍只算一遍', () => {
|
|
164
|
+
const events = [call(10, 'c1', 'read', { file_path: '/w/a.md' }), result(11, 'c1')]
|
|
165
|
+
const once = normalizeReadEvidence(events, { root: ROOT, fromSeq: -1 })
|
|
166
|
+
const twice = normalizeReadEvidence(events, { root: ROOT, fromSeq: once.cursor, pending: once.pending })
|
|
167
|
+
assert.equal(once.reads.length, 1)
|
|
168
|
+
assert.deepEqual(twice.reads, [])
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
test('证据:没有编号的事件不参与(`session` 头行与坏行都被跳过)', () => {
|
|
172
|
+
const events = [
|
|
173
|
+
{ type: 'session', data: { id: 's1' } },
|
|
174
|
+
{ type: 'tool/call', data: { callId: 'x', name: 'read', arguments: '{"file_path":"/w/a.md"}' } },
|
|
175
|
+
null,
|
|
176
|
+
{ type: 'tool/result', seq: 2, data: { message: { toolCallId: 'x', isError: false } } },
|
|
177
|
+
]
|
|
178
|
+
const { reads, cursor } = normalizeReadEvidence(events, { root: ROOT })
|
|
179
|
+
assert.deepEqual(reads, [])
|
|
180
|
+
assert.equal(cursor, 2)
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
test('证据:只调用不返回的会话不会把 pending 撑大(有上限)', () => {
|
|
184
|
+
const events = []
|
|
185
|
+
for (let i = 0; i < MAX_PENDING_CALLS + 20; i += 1) {
|
|
186
|
+
events.push(call(i + 1, `c${i}`, 'read', { file_path: `/w/a${i}.md` }))
|
|
187
|
+
}
|
|
188
|
+
const { pending } = normalizeReadEvidence(events, { root: ROOT })
|
|
189
|
+
assert.equal(pending.size, MAX_PENDING_CALLS)
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
test('快照:三层被压成 rel / 层 / 层内名次,签名只由内容决定', () => {
|
|
193
|
+
const pack = {
|
|
194
|
+
mode: 'relevance', topic: 'knit', task: '做 v0.15',
|
|
195
|
+
primary: [{ rel: 'a.md' }], supporting: [{ rel: 'b.md' }, { rel: 'c.md' }], related: [{ rel: 'd.md' }],
|
|
196
|
+
totals: { matched: 4, total: 50 },
|
|
197
|
+
}
|
|
198
|
+
const snap = snapshotOf(pack, { seq: 7, at: 1234 })
|
|
199
|
+
assert.deepEqual(snap.items, [
|
|
200
|
+
{ rel: 'a.md', tier: 'primary', rank: 1 },
|
|
201
|
+
{ rel: 'b.md', tier: 'supporting', rank: 1 },
|
|
202
|
+
{ rel: 'c.md', tier: 'supporting', rank: 2 },
|
|
203
|
+
{ rel: 'd.md', tier: 'related', rank: 1 },
|
|
204
|
+
])
|
|
205
|
+
assert.equal(snap.seq, 7)
|
|
206
|
+
assert.equal(snap.at, 1234)
|
|
207
|
+
assert.equal(snap.total, 50)
|
|
208
|
+
// 同样的内容 ⇒ 同样的签名;任务换了 ⇒ 签名就变
|
|
209
|
+
assert.equal(snapshotOf(pack, { seq: 99 }).sig, snap.sig)
|
|
210
|
+
assert.notEqual(snapshotOf({ ...pack, task: '别的任务' }).sig, snap.sig)
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
test('差异:进来的 / 出去的 / 换层的 / 任务变了的分别报清楚', () => {
|
|
214
|
+
const before = snapshotOf({
|
|
215
|
+
task: 'A', primary: [{ rel: 'a.md' }], supporting: [{ rel: 'b.md' }], related: [{ rel: 'c.md' }],
|
|
216
|
+
})
|
|
217
|
+
const after = snapshotOf({
|
|
218
|
+
task: 'B',
|
|
219
|
+
primary: [{ rel: 'b.md' }],
|
|
220
|
+
supporting: [{ rel: 'a.md' }, { rel: 'e.md' }],
|
|
221
|
+
related: [],
|
|
222
|
+
})
|
|
223
|
+
const diff = diffContext(before, after)
|
|
224
|
+
assert.deepEqual(diff.appeared, [{ rel: 'e.md', tier: 'supporting', rank: 2 }])
|
|
225
|
+
assert.deepEqual(diff.disappeared, [{ rel: 'c.md', tier: 'related', rank: 1 }])
|
|
226
|
+
// 顺序是「按新快照的层内顺序」,确定性由 Map 插入顺序保证
|
|
227
|
+
assert.deepEqual(diff.moved.map((m) => [m.rel, m.from.tier, m.to.tier]), [
|
|
228
|
+
['b.md', 'supporting', 'primary'],
|
|
229
|
+
['a.md', 'primary', 'supporting'],
|
|
230
|
+
])
|
|
231
|
+
assert.equal(diff.taskChanged, true)
|
|
232
|
+
assert.equal(diff.stable, false)
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
test('差异:完全一样时 stable,没有假的「变化」', () => {
|
|
236
|
+
const pack = { task: 'A', primary: [{ rel: 'a.md' }], supporting: [], related: [] }
|
|
237
|
+
const diff = diffContext(snapshotOf(pack), snapshotOf(pack))
|
|
238
|
+
assert.equal(diff.stable, true)
|
|
239
|
+
assert.deepEqual(diff.appeared, [])
|
|
240
|
+
assert.deepEqual(diff.moved, [])
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
test('快照:形状不对的输入不会抛,只是产出空快照', () => {
|
|
244
|
+
assert.deepEqual(snapshotOf(null).items, [])
|
|
245
|
+
assert.deepEqual(snapshotOf(undefined).items, [])
|
|
246
|
+
assert.deepEqual(snapshotOf({ primary: 'nope', supporting: [null, { rel: 7 }] }).items, [])
|
|
247
|
+
})
|