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.
@@ -140,6 +140,7 @@ test('apply: 只给 tools(webServer 缺席)时仍然注册工具', () => {
140
140
  // 这条会红 —— 那时 webServer 一缺席,工具也一起没了。
141
141
  const registered = []
142
142
  const rootCtx = {
143
+ on: () => {},
143
144
  inject: (deps, cb) => {
144
145
  if (deps.includes('tools')) {
145
146
  cb({
@@ -156,7 +157,7 @@ test('apply: 只给 tools(webServer 缺席)时仍然注册工具', () => {
156
157
  })
157
158
 
158
159
  test('apply: webServer 与 tools 都缺席时不抛错', () => {
159
- assert.doesNotThrow(() => apply({ inject: () => {} }))
160
+ assert.doesNotThrow(() => apply({ on: () => {}, inject: () => {} }))
160
161
  })
161
162
 
162
163
  /* ── 定义形状(与官方 defineTool 的产物对齐)─────────── */
@@ -728,13 +729,14 @@ function fakeBridge({ summary = () => 'Usage since the last pack: 0 reads', note
728
729
  return {
729
730
  calls,
730
731
  bridge: {
731
- summary: (root, sessionId, session) => {
732
- calls.push({ fn: 'summary', root, sessionId, hasSession: !!session })
733
- return summary(root, sessionId, session)
732
+ // v0.16:桥只收 `(sessionId)` / `(sessionId, pack)` —— 读证据由订阅那条路进 store
733
+ summary: (sessionId) => {
734
+ calls.push({ fn: 'summary', sessionId })
735
+ return summary(sessionId)
734
736
  },
735
- note: (root, sessionId, session, pack) => {
736
- calls.push({ fn: 'note', root, sessionId, hasSession: !!session, pack })
737
- return note(root, sessionId, session, pack)
737
+ note: (sessionId, pack) => {
738
+ calls.push({ fn: 'note', sessionId, pack })
739
+ return note(sessionId, pack)
738
740
  },
739
741
  },
740
742
  }
@@ -760,9 +762,9 @@ test('audit: true 时先问 summary 再 note,并把这一行放进返回的 us
760
762
  '顺序是契约:summary 报告的是**上一份**包,必须先问;note 记的是刚交出的这份',
761
763
  )
762
764
  const noted = calls.find((c) => c.fn === 'note')
763
- assert.equal(noted.root, PROJECT_ROOT, 'note 拿到的是工作区根')
764
765
  assert.equal(noted.sessionId, 'sess-tool-test', 'note 拿到的是会话 id')
765
- assert.equal(noted.hasSession, true, 'note 还拿到会话本身(它要去读事件)')
766
+ assert.equal(noted.root, undefined, 'v0.16:桥不再收工作区根(读路径由订阅回调自己归一化)')
767
+ assert.equal(noted.hasSession, undefined, 'v0.16:桥不再收会话对象(它不再去拉事件)')
766
768
  assert.ok(noted.pack && noted.pack.totals, 'note 记的是刚交出去的那份包(含 totals)')
767
769
  assert.equal(noted.pack.totals.total, 3)
768
770
  })
@@ -1,20 +1,27 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
- * Context Feedback Eval —— 「Context Pack 交出去之后,真的被用了吗」的量尺(v0.15)。
3
+ * Context Feedback Eval —— 「Context Pack 交出去之后,真的被用了吗」的量尺(v0.16)。
4
4
  *
5
5
  * **为什么要有它**:v0.14 已经能算出「这个任务该先看哪一篇」,但没有任何证据表明
6
6
  * agent 真的去读了那一篇。`feedback.js` 把这件事变成可测量的事实,这个脚本负责
7
7
  * **在真实会话日志上**把五个指标算出来:
8
8
  *
9
9
  * · Primary follow-through —— 主看的那篇最终被读了吗
10
- * · First read tier —— 第一次读落在哪一层(primary / supporting / outside)
10
+ * · First read tier —— 第一次读落在哪一层(primary / supporting / related / outside)
11
11
  * · Supporting coverage —— 辅助篇里被读到的比例
12
12
  * · Outside-context —— 有多少次读**不在**当时那份包里面
13
- * · Delta churn —— 上下文换过几次、进出换层各多少
13
+ * · Context churn —— 上下文换过几次、进出换层各多少
14
+ * · Continued / re-entry —— 离开上下文后还读了几次、重新进来后又读了几次
14
15
  *
15
16
  * **它是回放的,不是监听的**:拿一份会话日志(`~/.dsh/sessions/<工作区>/<会话>/session.v4.jsonl.zstd`),
16
17
  * 按时间顺序把每个「真实 read 发生的那一刻」重演一遍 —— 用**磁盘上这份代码**的
17
- * `scan()` + `buildContextFor()` 现算出那一刻的包,再喂给**同一个** `feedback.js`。
18
+ * `scan()` + `buildContextFor()` 现算出那一刻的包,再喂给**同一个** `feedback.js`
19
+ * (`noteSnapshot()` / `ingestEvents()` / `usageFor()`)。
20
+ *
21
+ * ⚠️ **归因规则只有一份**:runtime 与这里都不自己算「这一读落在哪一层」——
22
+ * 都走 `feedback.js` 的 `snapshotAt()` / `attributeRead()`。所以两边的**规则**逐字一致;
23
+ * 对不齐的只是「交付时刻」(面板轮询 / 工具调用是采样,这里是每个读一份合成包),
24
+ * 那是**数字**层面的差异,不是规则层面的(见 `Knit_评审-v0.16-基于DSH开放能力.md` §2)。
18
25
  * 所以:不需要重启宿主、不需要订阅 event bus、零网络、零模型、不写盘。
19
26
  * 历史会话也能补测(v0.14 的实验会话一样能算)。
20
27
  *
@@ -60,7 +67,10 @@ export function metricsFor(usage) {
60
67
  supportingCoverage: stats.supportingCoverage === undefined ? null : stats.supportingCoverage,
61
68
  outside: stats.outside || 0,
62
69
  outsideReads: stats.outsideReads || [],
63
- contextChanges: churn.snapshots || 0,
70
+ contextChanges: churn.changes || 0,
71
+ epochs: churn.epochs || 0,
72
+ continuedReadsAfterExit: stats.continuedReadsAfterExit || 0,
73
+ reentries: stats.reEntries || 0,
64
74
  appeared: churn.appeared || 0,
65
75
  disappeared: churn.disappeared || 0,
66
76
  moved: churn.moved || 0,
@@ -120,13 +130,12 @@ export function findLatestSessionLog(sessionsDir = join(homedir(), '.dsh', 'sess
120
130
  let bestAt = -1
121
131
  for (const workspace of listDirs(sessionsDir)) {
122
132
  for (const session of listDirs(workspace)) {
123
- for (const name of ['session.v4.jsonl.zstd', 'session.v4.jsonl']) {
124
- const file = join(session, name)
133
+ for (const file of sessionLogsIn(session)) {
125
134
  try {
126
135
  const at = statSync(file).mtimeMs
127
136
  if (at > bestAt) { bestAt = at; best = file }
128
137
  } catch {
129
- // 没有这个文件(或读不动):继续找
138
+ // 读不动:继续找
130
139
  }
131
140
  }
132
141
  }
@@ -134,6 +143,29 @@ export function findLatestSessionLog(sessionsDir = join(homedir(), '.dsh', 'sess
134
143
  return best
135
144
  }
136
145
 
146
+ /**
147
+ * 列一个会话目录里的日志,**格式版本从新到旧**。
148
+ *
149
+ * ⚠️ 不写死 `session.v4.jsonl.zstd`:宿主的 `SESSION_FORMAT_VERSION` 会往前走,
150
+ * 而且同一台机器上老会话还留在 v0 / v3 上(本机三代并存)。只认形状,不认具体版本号。
151
+ *
152
+ * @param {string} dir - 会话目录
153
+ * @returns {string[]} 日志绝对路径
154
+ */
155
+ export function sessionLogsIn(dir) {
156
+ let names = []
157
+ try {
158
+ names = readdirSync(dir)
159
+ } catch {
160
+ return []
161
+ }
162
+ return names
163
+ .map((name) => ({ name, match: /^session\.v(\d+)\.jsonl(\.zstd)?$/.exec(name) }))
164
+ .filter((entry) => entry.match)
165
+ .sort((a, b) => Number(b.match[1]) - Number(a.match[1]) || a.name.localeCompare(b.name))
166
+ .map((entry) => join(dir, entry.name))
167
+ }
168
+
137
169
  /**
138
170
  * 按会话 id 找日志(id 是目录名的一部分,直接找同名目录)。
139
171
  *
@@ -145,10 +177,8 @@ export function findSessionLogById(sessionId, sessionsDir = join(homedir(), '.ds
145
177
  for (const workspace of listDirs(sessionsDir)) {
146
178
  for (const session of listDirs(workspace)) {
147
179
  if (basename(session) !== sessionId) continue
148
- for (const name of ['session.v4.jsonl.zstd', 'session.v4.jsonl']) {
149
- const file = join(session, name)
150
- if (existsSync(file)) return file
151
- }
180
+ const [file] = sessionLogsIn(session)
181
+ if (file) return file
152
182
  }
153
183
  }
154
184
  return null
@@ -311,10 +341,12 @@ async function main() {
311
341
  console.log(` 其中 Knit 看得到的:${scope.inScope} 篇(漏 ${scope.missedReads} 次)· 看不到的:${scope.outOfScope} 篇(非 .md / 超出上限)`)
312
342
  if (scope.missed.length) console.log(` ⚠️ 真的漏了:${scope.missed.join('、')}`)
313
343
  if (m.outsideReads.length) console.log(` 不在包里的:${m.outsideReads.join('、')}`)
314
- console.log(`Delta churn : 换过 ${m.contextChanges} 次 · 进 ${m.appeared} / 出 ${m.disappeared} / 换层 ${m.moved}${m.taskChanged ? ` · 任务变了 ${m.taskChanged}` : ''}`)
344
+ console.log(`Context churn : ${m.epochs} 个 Epoch(换过 ${m.contextChanges} 次)· 进 ${m.appeared} / 出 ${m.disappeared} / 换层 ${m.moved}${m.taskChanged ? ` · 任务变了 ${m.taskChanged}` : ''}`)
345
+ console.log(`Continued / re-entry : 离开后还读了 ${m.continuedReadsAfterExit} 篇 · 重新进来后又读了 ${m.reentries} 篇`)
315
346
  console.log('')
316
347
  // 量尺自检:Control 臂如果还能测出「落在层里」的读,那说明**量尺坏了**,不是结论。
317
- if (flag('--control') && m.firstReadTier !== null) {
348
+ // v0.16:一次都没读才是 null;读过但在包外是 'outside' —— 后者在 Control 臂是**正常的**。
349
+ if (flag('--control') && m.firstReadTier !== null && m.firstReadTier !== 'outside') {
318
350
  console.error('✗ Control 臂不该有落在层里的读 —— 量尺本身坏了')
319
351
  return 1
320
352
  }