dsh-knit 0.15.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.
@@ -131,7 +131,8 @@ test('使用情况:快照之外的读如实记成 outside,不解释成「失
131
131
  assert.equal(usage.stats.outside, 2)
132
132
  assert.deepEqual(usage.stats.outsideReads.sort(), ['y.md', 'z.md'])
133
133
  assert.equal(usage.stats.primaryFollowThrough, false)
134
- assert.equal(usage.stats.firstReadTier, null)
134
+ // v0.16:读过但读在包外 ⇒ 'outside'。null 只留给「一次都还没读」
135
+ assert.equal(usage.stats.firstReadTier, 'outside')
135
136
  const outside = usage.reads.find((r) => r.rel === 'z.md')
136
137
  assert.equal(outside.tier, null)
137
138
  assert.equal(outside.outside, true)
@@ -143,7 +144,9 @@ test('使用情况:Knit 还没给过上下文就自己读了 ⇒ 那一篇算
143
144
  ingestEvents(store, 's1', readEvents([['a.md', 10]], 100), { root: ROOT })
144
145
  const usage = usageFor(store, 's1')
145
146
  assert.equal(usage.stats.outside, 1)
146
- assert.equal(usage.stats.firstReadTier, null)
147
+ // 一份快照都没给过 ⇒ 那一读只能算包外
148
+ assert.equal(usage.stats.firstReadTier, 'outside')
149
+ assert.equal(usage.stats.firstReadEpoch, null)
147
150
  assert.equal(usage.stats.primaryFollowThrough, false)
148
151
  })
149
152
 
@@ -158,9 +161,16 @@ test('使用情况:读的时候用的是**当时**那份快照,不是最新
158
161
 
159
162
  const usage = usageFor(store, 's1')
160
163
  const row = usage.reads.find((r) => r.rel === 'a.md')
161
- // 行上是「当时」的层(primary),不是现在这份(related)
164
+ // v0.16:`tier` / `rank` / `outside` 全是**读那一刻**冻结的归因,
165
+ // 因此行上的 `tier` 与 `firstTier` 一致 —— 不许等出报告时再拿最新快照重判。
162
166
  assert.equal(row.firstTier, 'primary')
163
- assert.equal(row.tier, 'related')
167
+ assert.equal(row.tier, 'primary')
168
+ assert.equal(row.outside, false)
169
+ // 换过上下文 ⇒ 两个 Epoch;那一读只算进**它当时**那个 Epoch 的主看篇
170
+ assert.equal(usage.stats.churn.epochs, 2)
171
+ assert.equal(usage.epochs.length, 2)
172
+ assert.equal(usage.epochs[0].primary.read, true)
173
+ assert.equal(usage.epochs[1].primary.read, false)
164
174
  })
165
175
 
166
176
  test('幂等:同一批事件喂两遍,读只算一遍', () => {
@@ -290,6 +300,9 @@ test('给 Agent 的摘要:只有事实、没有分数;没可说的就返回
290
300
  supportingTotal: 2,
291
301
  readsOutside: 1,
292
302
  contextChanges: 0,
303
+ epochs: 1,
304
+ continuedReadsAfterExit: 0,
305
+ reEntries: 0,
293
306
  })
294
307
  assert.equal(getAuditSummary(null), '')
295
308
  assert.equal(auditPayload(null), null)
@@ -300,9 +313,9 @@ test('Usage 的形状是稳定的(给面板与工具用的字段一个不少
300
313
  noteSnapshot(store, 's1', pack({ primary: ['a.md'], supporting: ['b.md'], related: ['c.md'] }), { seq: 1, at: 1 })
301
314
  const usage = usageFrom(store.sessions.get('s1'))
302
315
  assert.deepEqual(Object.keys(usage.stats).sort(), [
303
- 'byTier', 'churn', 'distinct', 'firstReadAt', 'firstReadRel', 'firstReadTier',
304
- 'outside', 'outsideReads', 'primaryFollowThrough', 'primaryRel', 'reads',
305
- 'supportingCoverage',
316
+ 'byTier', 'churn', 'continuedReadsAfterExit', 'distinct', 'firstReadAt', 'firstReadEpoch',
317
+ 'firstReadRel', 'firstReadTier', 'outside', 'outsideReads', 'primaryFollowThrough',
318
+ 'primaryRel', 'reEntries', 'reads', 'supportingCoverage',
306
319
  ])
307
320
  assert.equal(usage.stats.supportingCoverage, 0)
308
321
  assert.equal(usage.stats.churn.snapshots, 0)
@@ -0,0 +1,309 @@
1
+ /**
2
+ * Knit v0.16 · **Context Effectiveness 的行为测试**(SDD §24.3 / §24.4 / §24.5 / §24.6)。
3
+ *
4
+ * 这里只测 v0.15 里**做不到**的那件事:一次读落在哪一层,必须**在读发生的那一刻**结算并冻结,
5
+ * 之后上下文怎么换都不许改口。
6
+ *
7
+ * 1. **历史归因**(§24.3)—— 读发生在 Epoch 1、出报告时已经是 Epoch 2,`tier` / `rank` /
8
+ * `inside` / `epochId` 仍然是 Epoch 1 的值;
9
+ * 2. **持续性**(§24.4)—— 离开上下文之后还读 ⇒ 记成事实 `continuedReadAfterExit`,
10
+ * 不许解释成「Agent 不认可新上下文」;
11
+ * 3. **重新进入**(§24.5)—— 离开过、又回到包里、再读 ⇒ `reEntry`;
12
+ * 4. **规则一致**(§24.6)—— runtime 记下来的归因,逐字段等于拿同一份
13
+ * `(snapshots, reads)` 直接调 `snapshotAt()` + `attributeRead()` 现算的结果。
14
+ * 对齐的是**规则**,不是数字(交付时刻不可观测,见 SDD §3 G4)。
15
+ *
16
+ * ⚠️ 一条纪律:`tier` / `rank` / `outside` 是**首读时**的归因(`firstRead`),
17
+ * 不是「它现在在哪一层」—— 后者根本不存在,那是 Current View ≠ Historical View。
18
+ */
19
+ import test from 'node:test'
20
+ import assert from 'node:assert/strict'
21
+
22
+ import {
23
+ createStore, activateAudit, ingestEvents, noteSnapshot, usageFor, snapshotOf,
24
+ snapshotAt, attributeRead, attributeReadFacts, MAX_SNAPSHOTS_PER_SESSION,
25
+ } from '../src/host/feedback.js'
26
+
27
+ const ROOT = '/w'
28
+
29
+ /** 造一份 Context Pack(只给测试要用的字段)。 */
30
+ function pack(options = {}) {
31
+ const rows = (list) => (list || []).map((rel) => ({ rel }))
32
+ return {
33
+ mode: 'relevance',
34
+ topic: options.topic || 'knit',
35
+ task: options.task || '',
36
+ primary: rows(options.primary),
37
+ supporting: rows(options.supporting),
38
+ related: rows(options.related),
39
+ totals: { matched: 0, total: Number.isInteger(options.total) ? options.total : 10 },
40
+ }
41
+ }
42
+
43
+ /**
44
+ * 一次成功的读 = `tool/call` + `tool/result` 两件事(与宿主事件流同形)。
45
+ * 归因认的是**结果**那件事的 seq(`callSeq` 只是配对用),所以 `readAt('a.md', 20)`
46
+ * 记下来的 `firstRead.seq` 是 21。
47
+ * @param {string} rel - 路径
48
+ * @param {number} seq - `tool/call` 的 seq
49
+ * @returns {object[]} 两件事
50
+ */
51
+ function readAt(rel, seq) {
52
+ const callId = `c${seq}`
53
+ return [
54
+ {
55
+ type: 'tool/call',
56
+ seq,
57
+ time: seq,
58
+ data: { turn: 1, step: 1, callId, name: 'read', arguments: JSON.stringify({ file_path: rel }) },
59
+ },
60
+ {
61
+ type: 'tool/result',
62
+ seq: seq + 1,
63
+ time: seq + 1,
64
+ data: { message: { role: 'tool', toolCallId: callId, source: { kind: 'tool', callId }, isError: false } },
65
+ },
66
+ ]
67
+ }
68
+
69
+ /** 一个开着账、只交过几份包的会话。 */
70
+ function audited(id) {
71
+ const store = createStore()
72
+ activateAudit(store, id)
73
+ return store
74
+ }
75
+
76
+ /* ── 1. 历史归因(§24.3)────────────────────────────────────── */
77
+
78
+ test('历史归因:换成新 Context 之后再看,旧读的层与 Epoch 一个字节都不许变', () => {
79
+ const store = audited('s1')
80
+ // Epoch 1:a.md 是主看篇
81
+ noteSnapshot(store, 's1', pack({ primary: ['a.md'], supporting: ['b.md'] }), { seq: 10, at: 1000 })
82
+ // 读发生在 Epoch 1(call seq 20 → result seq 21)
83
+ ingestEvents(store, 's1', readAt('a.md', 20), { root: ROOT })
84
+ // Epoch 2:换任务,a.md 掉出包外
85
+ noteSnapshot(store, 's1', pack({ topic: 'other', primary: ['c.md'] }), { seq: 40, at: 2000 })
86
+
87
+ const usage = usageFor(store, 's1')
88
+ const row = usage.reads.find((r) => r.rel === 'a.md')
89
+
90
+ // 冻结值:读发生时 a.md 在 primary、第 1 个 Epoch、快照 seq 10
91
+ assert.equal(row.firstRead.seq, 21)
92
+ assert.equal(row.firstRead.snapshotSeq, 10)
93
+ assert.equal(row.firstRead.epochId, 1)
94
+ assert.equal(row.firstRead.tier, 'primary')
95
+ assert.equal(row.firstRead.rank, 1)
96
+ assert.equal(row.firstRead.inside, true)
97
+
98
+ // 「现在」是 Epoch 2,包里已经没有 a.md 了 —— 但那是 Current View,不是历史
99
+ assert.equal(usage.epochId, 2)
100
+ assert.equal(usage.items.some((item) => item.rel === 'a.md'), false)
101
+ assert.equal(usage.stats.firstReadTier, 'primary')
102
+ assert.equal(usage.stats.firstReadEpoch, 1)
103
+ // 兼容别名也只能是首读的归因(v0.15 这里会拿最新快照重判成 outside)
104
+ assert.equal(row.tier, 'primary')
105
+ assert.equal(row.outside, false)
106
+ // 它算进的是**当时**那个 Epoch,不是现在这个
107
+ assert.equal(usage.epochs[0].primary.read, true)
108
+ assert.equal(usage.epochs[1].primary.read, false)
109
+ assert.equal(usage.stats.primaryFollowThrough, false)
110
+ assert.equal(usage.stats.byTier.primary.read, 0)
111
+ })
112
+
113
+ test('当前视图可变、历史视图不可变:换回来也不重算', () => {
114
+ const store = audited('s2')
115
+ noteSnapshot(store, 's2', pack({ primary: ['a.md'] }), { seq: 10, at: 1 })
116
+ ingestEvents(store, 's2', readAt('a.md', 20), { root: ROOT })
117
+ noteSnapshot(store, 's2', pack({ primary: ['b.md'] }), { seq: 40, at: 2 })
118
+ noteSnapshot(store, 's2', pack({ primary: ['a.md'] }), { seq: 60, at: 3 })
119
+
120
+ const usage = usageFor(store, 's2')
121
+ const row = usage.reads.find((r) => r.rel === 'a.md')
122
+ assert.equal(row.firstRead.epochId, 1)
123
+ assert.equal(row.firstRead.tier, 'primary')
124
+ assert.equal(usage.epochId, 3)
125
+ })
126
+
127
+ /* ── 2. 离开之后仍然被读(§24.4)────────────────────────────── */
128
+
129
+ test('持续读:离开上下文之后还读那篇 ⇒ 只记事实,不加解释', () => {
130
+ const store = audited('s3')
131
+ noteSnapshot(store, 's3', pack({ primary: ['a.md'], supporting: ['b.md'] }), { seq: 10, at: 1 })
132
+ // Epoch 2:a.md 出包
133
+ noteSnapshot(store, 's3', pack({ supporting: ['b.md'] }), { seq: 20, at: 2 })
134
+ // 出包之后仍然读它
135
+ ingestEvents(store, 's3', readAt('a.md', 30), { root: ROOT })
136
+
137
+ const usage = usageFor(store, 's3')
138
+ const row = usage.reads.find((r) => r.rel === 'a.md')
139
+ assert.equal(row.firstRead.epochId, 2)
140
+ assert.equal(row.firstRead.inside, false)
141
+ assert.equal(row.firstRead.tier, null)
142
+ assert.equal(row.continuedReadAfterExit, true)
143
+ assert.equal(row.reEntry, false)
144
+ assert.equal(usage.stats.continuedReadsAfterExit, 1)
145
+ assert.equal(usage.stats.reEntries, 0)
146
+ assert.equal(usage.stats.firstReadTier, 'outside')
147
+ assert.equal(usage.epochs[1].continuedReadsAfterExit, 1)
148
+ // 摘要里只有事实,没有「Agent 不认可」这种话
149
+ assert.equal(/不认可|不信任|confidence|score/i.test(JSON.stringify(usage.epochs)), false)
150
+ })
151
+
152
+ /* ── 3. 离开又回来(§24.5)──────────────────────────────────── */
153
+
154
+ test('重新进入:离开 → 又回到包里 → 再读 ⇒ reEntry 与 tierAtRead 都是事实', () => {
155
+ const store = audited('s4')
156
+ noteSnapshot(store, 's4', pack({ primary: ['a.md'] }), { seq: 10, at: 1 })
157
+ noteSnapshot(store, 's4', pack({ primary: ['c.md'] }), { seq: 20, at: 2 })
158
+ noteSnapshot(store, 's4', pack({ primary: ['c.md'], supporting: ['a.md'] }), { seq: 30, at: 3 })
159
+ ingestEvents(store, 's4', readAt('a.md', 40), { root: ROOT })
160
+
161
+ const usage = usageFor(store, 's4')
162
+ const row = usage.reads.find((r) => r.rel === 'a.md')
163
+ assert.equal(row.firstRead.epochId, 3)
164
+ assert.equal(row.firstRead.tier, 'supporting')
165
+ assert.equal(row.firstRead.inside, true)
166
+ assert.equal(row.reEntry, true)
167
+ // 这一次读本身在包里 ⇒ 不是「离开之后仍在读」
168
+ assert.equal(row.continuedReadAfterExit, false)
169
+ assert.equal(usage.stats.reEntries, 1)
170
+ assert.equal(usage.epochs[2].reEntries, 1)
171
+ })
172
+
173
+ /* ── 4. 两个事实的判据(纯函数直接问)──────────────────────── */
174
+
175
+ test('事实判据:更早的窗口里从来没有过这篇 ⇒ 两个都是 false', () => {
176
+ const snapshots = [
177
+ { ...snapshotOf(pack({ primary: ['a.md'] }), { seq: 10, at: 1 }), epochId: 1 },
178
+ { ...snapshotOf(pack({ primary: ['b.md'] }), { seq: 20, at: 2 }), epochId: 2 },
179
+ ]
180
+ assert.deepEqual(attributeReadFacts(snapshots, 'z.md', 30), { continuedReadAfterExit: false, reEntry: false })
181
+ // 一直在包里、且没离开过 ⇒ 也不是「重新进入」
182
+ assert.deepEqual(attributeReadFacts(snapshots, 'a.md', 15), { continuedReadAfterExit: false, reEntry: false })
183
+ // 早于任何快照的读:连快照都配不上,两个事实都无从谈起
184
+ assert.deepEqual(attributeReadFacts(snapshots, 'a.md', 5), { continuedReadAfterExit: false, reEntry: false })
185
+ assert.deepEqual(attributeReadFacts(null, 'a.md', 30), { continuedReadAfterExit: false, reEntry: false })
186
+ })
187
+
188
+ test('事实判据:出包那一读算持续读,回到包里那一读算重新进入', () => {
189
+ const snapshots = [
190
+ { ...snapshotOf(pack({ primary: ['a.md'] }), { seq: 10, at: 1 }), epochId: 1 },
191
+ { ...snapshotOf(pack({ primary: ['b.md'] }), { seq: 20, at: 2 }), epochId: 2 },
192
+ { ...snapshotOf(pack({ primary: ['b.md'], related: ['a.md'] }), { seq: 30, at: 3 }), epochId: 3 },
193
+ ]
194
+ assert.deepEqual(attributeReadFacts(snapshots, 'a.md', 25), { continuedReadAfterExit: true, reEntry: false })
195
+ assert.deepEqual(attributeReadFacts(snapshots, 'a.md', 35), { continuedReadAfterExit: false, reEntry: true })
196
+ })
197
+
198
+ test('attributeRead:不在包里也照样给出「当时那份快照」的身份', () => {
199
+ const snapshot = { ...snapshotOf(pack({ primary: ['a.md'] }), { seq: 10, at: 1 }), epochId: 4 }
200
+ assert.deepEqual(attributeRead(snapshot, 'a.md'), {
201
+ snapshotSeq: 10, epochId: 4, tierAtRead: 'primary', rankAtRead: 1, insideAtRead: true,
202
+ })
203
+ assert.deepEqual(attributeRead(snapshot, 'z.md'), {
204
+ snapshotSeq: 10, epochId: 4, tierAtRead: null, rankAtRead: null, insideAtRead: false,
205
+ })
206
+ assert.deepEqual(attributeRead(null, 'a.md'), {
207
+ snapshotSeq: null, epochId: null, tierAtRead: null, rankAtRead: null, insideAtRead: false,
208
+ })
209
+ })
210
+
211
+ /* ── 5. 规则一致(§24.6)────────────────────────────────────── */
212
+
213
+ test('规则一致:runtime 记下的归因,逐字段等于 snapshotAt() + attributeRead() 现算的结果', () => {
214
+ const store = audited('s5')
215
+ noteSnapshot(store, 's5', pack({ primary: ['a.md'], supporting: ['b.md', 'c.md'] }), { seq: 10, at: 1 })
216
+ noteSnapshot(store, 's5', pack({ supporting: ['b.md'], related: ['a.md'] }), { seq: 30, at: 2 })
217
+ ingestEvents(store, 's5', readAt('a.md', 20), { root: ROOT })
218
+ ingestEvents(store, 's5', readAt('z.md', 40), { root: ROOT })
219
+
220
+ const state = store.sessions.get('s5')
221
+ const usage = usageFor(store, 's5')
222
+ assert.equal(usage.reads.length, 2)
223
+ for (const row of usage.reads) {
224
+ const attributed = attributeRead(snapshotAt(state.snapshots, row.firstRead.seq), row.rel)
225
+ assert.deepEqual(
226
+ {
227
+ snapshotSeq: row.firstRead.snapshotSeq,
228
+ epochId: row.firstRead.epochId,
229
+ tierAtRead: row.firstRead.tier,
230
+ rankAtRead: row.firstRead.rank,
231
+ insideAtRead: row.firstRead.inside,
232
+ },
233
+ {
234
+ snapshotSeq: attributed.snapshotSeq,
235
+ epochId: attributed.epochId,
236
+ tierAtRead: attributed.tierAtRead,
237
+ rankAtRead: attributed.rankAtRead,
238
+ insideAtRead: attributed.insideAtRead,
239
+ },
240
+ `${row.rel} 的归因必须是那两个纯函数算出来的`,
241
+ )
242
+ }
243
+ })
244
+
245
+ test('规则一致:同样的输入喂两个 store,逐字段 deepEqual(没有隐藏的当前时刻依赖)', () => {
246
+ const build = (id) => {
247
+ const store = audited(id)
248
+ noteSnapshot(store, id, pack({ primary: ['a.md'] }), { seq: 10, at: 1 })
249
+ ingestEvents(store, id, readAt('a.md', 20), { root: ROOT })
250
+ noteSnapshot(store, id, pack({ primary: ['b.md'] }), { seq: 30, at: 2 })
251
+ ingestEvents(store, id, readAt('a.md', 40), { root: ROOT })
252
+ return usageFor(store, id)
253
+ }
254
+ assert.deepEqual(build('left'), build('right'))
255
+ })
256
+
257
+ /* ── 6. Epoch 的边界与上限 ──────────────────────────────────── */
258
+
259
+ test('Epoch 与快照同生共死:同签名不新建,换了内容才 +1,Delta 带 fromEpoch/toEpoch', () => {
260
+ const store = audited('s6')
261
+ noteSnapshot(store, 's6', pack({ primary: ['a.md'] }), { seq: 10, at: 1 })
262
+ noteSnapshot(store, 's6', pack({ primary: ['a.md'] }), { seq: 20, at: 2 })
263
+ let usage = usageFor(store, 's6')
264
+ assert.equal(usage.stats.churn.epochs, 1)
265
+ assert.equal(usage.stats.churn.changes, 0)
266
+ assert.equal(usage.epochs.length, 1)
267
+
268
+ noteSnapshot(store, 's6', pack({ primary: ['b.md'] }), { seq: 30, at: 3 })
269
+ usage = usageFor(store, 's6')
270
+ assert.equal(usage.epochId, 2)
271
+ assert.equal(usage.epochs.length, 2)
272
+ assert.equal(usage.stats.churn.epochs, 2)
273
+ assert.equal(usage.stats.churn.changes, 1)
274
+ assert.equal(usage.delta.fromEpoch, 1)
275
+ assert.equal(usage.delta.toEpoch, 2)
276
+ })
277
+
278
+ test('上限:Epoch 明细只留最近 20 份,「换过几次」仍是累计事实', () => {
279
+ const store = audited('s7')
280
+ const total = MAX_SNAPSHOTS_PER_SESSION + 3
281
+ for (let i = 0; i < total; i += 1) {
282
+ noteSnapshot(store, 's7', pack({ primary: [`p${i}.md`] }), { seq: 10 + i, at: 100 + i })
283
+ }
284
+ const usage = usageFor(store, 's7')
285
+ assert.equal(usage.epochs.length, MAX_SNAPSHOTS_PER_SESSION)
286
+ // 编号是累计的:被淘汰的 Epoch 也算「换过」
287
+ assert.equal(usage.stats.churn.epochs, total)
288
+ assert.equal(usage.stats.churn.changes, total - 1)
289
+ // 客户端在用的旧键名 = 变化次数(`usage.changes` 文案)
290
+ assert.equal(usage.stats.churn.snapshots, total - 1)
291
+ assert.equal(usage.stats.churn.deltas, total - 1)
292
+ // 明细里留下的是最新的那 20 个
293
+ assert.equal(usage.epochs[usage.epochs.length - 1].epochId, total)
294
+ })
295
+
296
+ test('还没交过任何包:Epoch 编号为 0,没有快照可配', () => {
297
+ const store = audited('s8')
298
+ ingestEvents(store, 's8', readAt('a.md', 20), { root: ROOT })
299
+ const usage = usageFor(store, 's8')
300
+ const row = usage.reads[0]
301
+ // 一份包都没交过 ⇒ 没有「当前 Epoch」可指(null,不是 0 —— 0 是个编号)
302
+ assert.equal(usage.epochId, null)
303
+ assert.equal(usage.epochs.length, 0)
304
+ assert.equal(usage.stats.churn.epochs, 0)
305
+ assert.equal(row.firstRead.snapshotSeq, null)
306
+ assert.equal(row.firstRead.epochId, null)
307
+ assert.equal(row.firstRead.inside, false)
308
+ assert.equal(usage.stats.firstReadTier, 'outside')
309
+ })
@@ -17,9 +17,14 @@
17
17
  */
18
18
  import test from 'node:test'
19
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'
20
23
 
21
24
  import { makeWorkspace } from './fixture.mjs'
22
- import { metricsFor, parseEvents, replaySession, scopeFor } from '../tools/context-feedback-eval.mjs'
25
+ import {
26
+ findLatestSessionLog, findSessionLogById, metricsFor, parseEvents, replaySession, scopeFor,
27
+ } from '../tools/context-feedback-eval.mjs'
23
28
 
24
29
  /** 目标文档:`quantum-anchor` 只在这里出现,所以它必须是 primary。 */
25
30
  const TARGET = 'docs/target.md'
@@ -110,7 +115,8 @@ test('评测:交过包才能记成 follow-through —— Control 与 Treatment
110
115
  // Control:一份包都没交 ⇒ 那次读只能落在包外,且**不许**记成 follow-through
111
116
  assert.equal(control.packs, 0)
112
117
  assert.equal(control.metrics.primaryRead, false)
113
- assert.equal(control.metrics.firstReadTier, null)
118
+ // v0.16:读过但没人在那一层给过上下文 ⇒ 'outside'(null 只留给「一次都没读」)
119
+ assert.equal(control.metrics.firstReadTier, 'outside')
114
120
  assert.equal(control.metrics.outside, 2, '两次读都该在包外')
115
121
 
116
122
  // 两边的读**次数**一样 —— 变的只是「有没有人给过上下文」
@@ -174,13 +180,38 @@ test('评测:坏行不废整批(半截的尾行很常见)', () => {
174
180
  test('评测:metricsFor 的键是稳定的(下游脚本靠它)', () => {
175
181
  const metrics = metricsFor(null)
176
182
  assert.deepEqual(Object.keys(metrics).sort(), [
177
- 'appeared', 'contextChanges', 'disappeared', 'distinct', 'firstReadRel', 'firstReadTier',
178
- 'moved', 'outside', 'outsideReads', 'primaryRead', 'primaryRel', 'reads',
179
- 'supportingCoverage', 'taskChanged',
183
+ 'appeared', 'contextChanges', 'continuedReadsAfterExit', 'disappeared', 'distinct', 'epochs',
184
+ 'firstReadRel', 'firstReadTier', 'moved', 'outside', 'outsideReads', 'primaryRead', 'primaryRel',
185
+ 'reads', 'reentries', 'supportingCoverage', 'taskChanged',
180
186
  ])
181
187
  assert.equal(metrics.reads, 0)
182
188
  assert.equal(metrics.primaryRead, false)
183
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
+ }
184
215
  })
185
216
 
186
217
  test('评测:scopeFor 不认识 usage 时给空拆分,不抛', () => {