dsh-better-debug 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.en.md +358 -0
- package/README.md +437 -2
- package/assets/stuck-gate/SKILL.md +52 -0
- package/client.js +71 -0
- package/cordis.patch.yml +52 -0
- package/lib/index.js +1031 -0
- package/lib/panel.js +505 -0
- package/lib/text.js +91 -0
- package/package.json +58 -4
- package/web/index.html +349 -0
package/lib/panel.js
ADDED
|
@@ -0,0 +1,505 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
import { fileURLToPath } from 'node:url'
|
|
4
|
+
import { charsFor, estimateTokens } from './text.js'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* dsh-better-debug 的读数面板(server 半)。
|
|
8
|
+
*
|
|
9
|
+
* 设计原则:**每一个数字都必须能从日志里逐行数出来**。
|
|
10
|
+
* 这个插件能证明的只有「我在第几步、因为什么信号、说了什么话、说完后模型下一个动作是什么」。
|
|
11
|
+
* 「省掉了多少 token」这类折算需要假设「本来会失败」,面板不做,也不展示。
|
|
12
|
+
*
|
|
13
|
+
* 数据源就是 cfg.logFile 那一个文件,没有第二个真相来源:
|
|
14
|
+
* apply pid=… logFile=… A=3 B=2 C=2 budget=3 window=6 panel=true
|
|
15
|
+
* inject session=… turn=… step=… signals=C inj=a1b2c3d4 chars=302 tokens=302 files=2 …
|
|
16
|
+
* arm session=… turn=… step=… gap=3
|
|
17
|
+
* measure session=… turn=… tool=ops_bash ok=true exit=0 cleared=1
|
|
18
|
+
* outcome session=… turn=… inj=… after=C first=ops_bash class=verify steps=1 before=edit
|
|
19
|
+
* change session=… turn=… inj=… after=C tool=ops_bash class=verify steps=1
|
|
20
|
+
* suppress session=… turn=… step=… reason=done
|
|
21
|
+
* bundled skill registered: stuck-gate <- …
|
|
22
|
+
* disabled by config, no gate installed
|
|
23
|
+
* skill register failed: …
|
|
24
|
+
*
|
|
25
|
+
* 指标措辞纪律(评审 m00176 中6):`outcome` 只说明「提示之后模型第一个动作是什么」,
|
|
26
|
+
* 它**不能**证明提示促成了改变、更不能证明它发现了缺陷——那需要对照组或历史回放。
|
|
27
|
+
* 所以字段名一律描述性(`firstAction` / `turnedWithinWindow`),不叫「增益」「有效」。
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
const WEB_ROOT = fileURLToPath(new URL('../web/', import.meta.url))
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 路由的跨代句柄。
|
|
34
|
+
*
|
|
35
|
+
* 热重载会重新 import 本模块,模块级变量全部重置;但 `webServer` 是另一个插件,
|
|
36
|
+
* 它的路由表在两代之间**是同一个对象**。所以上一代挂上去的 `/better-debug`
|
|
37
|
+
* 会留在表里,新一代再 register 就撞 `webserver: duplicate prefix route`。
|
|
38
|
+
*
|
|
39
|
+
* 用 globalThis 上的 Symbol 记住「谁挂的、怎么摘」,新一代先摘旧的再挂新的。
|
|
40
|
+
* 既能自愈,也能保证热重载后服务的是新代码而不是旧闭包。
|
|
41
|
+
*/
|
|
42
|
+
const PANEL_SLOT = Symbol.for('dsh-better-debug/panel-route')
|
|
43
|
+
|
|
44
|
+
// token 估算与文本长度回退都来自 lib/text.js:注入文案与面板用的是同一个算法,
|
|
45
|
+
// 不会出现「改了文案、面板里的字符数还是旧常量」这种漂移。
|
|
46
|
+
export { estimateTokens }
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* `key=value key="quoted value"` 扫描器。
|
|
50
|
+
* sample= 里可能带空格和等号,只有引号形态能安全切开。
|
|
51
|
+
*/
|
|
52
|
+
export function scanFields(rest) {
|
|
53
|
+
const out = {}
|
|
54
|
+
let i = 0
|
|
55
|
+
const text = String(rest ?? '')
|
|
56
|
+
while (i < text.length) {
|
|
57
|
+
while (i < text.length && text[i] === ' ') i += 1
|
|
58
|
+
if (i >= text.length) break
|
|
59
|
+
const eq = text.indexOf('=', i)
|
|
60
|
+
if (eq === -1) break
|
|
61
|
+
const key = text.slice(i, eq)
|
|
62
|
+
if (!/^[A-Za-z][A-Za-z0-9_]*$/.test(key)) {
|
|
63
|
+
// 不是 key=,跳过一个词
|
|
64
|
+
const sp = text.indexOf(' ', i)
|
|
65
|
+
i = sp === -1 ? text.length : sp + 1
|
|
66
|
+
continue
|
|
67
|
+
}
|
|
68
|
+
let value = ''
|
|
69
|
+
let cursor = eq + 1
|
|
70
|
+
if (text[cursor] === '"') {
|
|
71
|
+
cursor += 1
|
|
72
|
+
let escaped = false
|
|
73
|
+
while (cursor < text.length) {
|
|
74
|
+
const char = text[cursor]
|
|
75
|
+
if (escaped) {
|
|
76
|
+
value += char
|
|
77
|
+
escaped = false
|
|
78
|
+
} else if (char === '\\') {
|
|
79
|
+
escaped = true
|
|
80
|
+
} else if (char === '"') {
|
|
81
|
+
cursor += 1
|
|
82
|
+
break
|
|
83
|
+
} else {
|
|
84
|
+
value += char
|
|
85
|
+
}
|
|
86
|
+
cursor += 1
|
|
87
|
+
}
|
|
88
|
+
} else {
|
|
89
|
+
const sp = text.indexOf(' ', cursor)
|
|
90
|
+
value = sp === -1 ? text.slice(cursor) : text.slice(cursor, sp)
|
|
91
|
+
cursor = sp === -1 ? text.length : sp
|
|
92
|
+
}
|
|
93
|
+
out[key] = value
|
|
94
|
+
i = cursor
|
|
95
|
+
}
|
|
96
|
+
return out
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const LINE_RE = /^(\d{4}-\d{2}-\d{2}T[\d:.]+Z)\s+([\s\S]*)$/
|
|
100
|
+
|
|
101
|
+
/** 单行 → 结构化记录。认不出就返回 {kind:'other'},绝不抛。 */
|
|
102
|
+
export function parseLine(raw) {
|
|
103
|
+
const line = String(raw ?? '').replace(/\r$/, '')
|
|
104
|
+
if (line.trim().length === 0) return void 0
|
|
105
|
+
const head = LINE_RE.exec(line)
|
|
106
|
+
const at = head ? head[1] : ''
|
|
107
|
+
const body = head ? head[2] : line
|
|
108
|
+
|
|
109
|
+
if (body.startsWith('inject ')) {
|
|
110
|
+
const f = scanFields(body.slice(7))
|
|
111
|
+
const signals = String(f.signals ?? '').split('+').filter((s) => s.length > 0)
|
|
112
|
+
const chars = Number.isFinite(Number(f.chars)) && Number(f.chars) > 0 ? Number(f.chars) : charsFor(signals)
|
|
113
|
+
const tokens = Number.isFinite(Number(f.tokens)) && Number(f.tokens) > 0
|
|
114
|
+
? Number(f.tokens)
|
|
115
|
+
: estimateTokens('x'.repeat(chars))
|
|
116
|
+
return {
|
|
117
|
+
kind: 'inject',
|
|
118
|
+
at,
|
|
119
|
+
session: f.session ?? '',
|
|
120
|
+
turn: Number(f.turn),
|
|
121
|
+
step: Number(f.step),
|
|
122
|
+
// 每次注入一个 id:把 inject / outcome / change 三行串起来的唯一办法。
|
|
123
|
+
inj: f.inj ?? '',
|
|
124
|
+
signals,
|
|
125
|
+
chars,
|
|
126
|
+
tokens,
|
|
127
|
+
files: Number(f.files) || 0,
|
|
128
|
+
worstEdits: Number(f.worstEdits) || 0,
|
|
129
|
+
// A 按「自上次测量以来的连续盲改」判定;老日志没有这个字段 → 0。
|
|
130
|
+
worstStreak: Number(f.worstStreak) || 0,
|
|
131
|
+
worstErrors: Number(f.worstErrors) || 0,
|
|
132
|
+
searched: f.searched === 'true',
|
|
133
|
+
// measured=no | yes:<tool>:<ok|fail>
|
|
134
|
+
measured: f.measured ?? '',
|
|
135
|
+
sample: f.sample ?? ''
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// 交付态静默:闸门醒着、判定过、故意不注入。和「插件没挂上」必须能区分。
|
|
140
|
+
if (body.startsWith('suppress ')) {
|
|
141
|
+
const f = scanFields(body.slice(9))
|
|
142
|
+
return {
|
|
143
|
+
kind: 'suppress',
|
|
144
|
+
at,
|
|
145
|
+
session: f.session ?? '',
|
|
146
|
+
turn: Number(f.turn),
|
|
147
|
+
step: Number(f.step),
|
|
148
|
+
reason: f.reason ?? ''
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// C 只上膛不开口的那一步:用来解释「为什么改完 N 步却没提醒」。
|
|
153
|
+
if (body.startsWith('arm ')) {
|
|
154
|
+
const f = scanFields(body.slice(4))
|
|
155
|
+
return {
|
|
156
|
+
kind: 'arm',
|
|
157
|
+
at,
|
|
158
|
+
session: f.session ?? '',
|
|
159
|
+
turn: Number(f.turn),
|
|
160
|
+
step: Number(f.step),
|
|
161
|
+
gap: Number(f.gap) || 0
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// 测量结果:这是「有没有回路」的唯一硬证据,比任何动作分类都强。
|
|
166
|
+
if (body.startsWith('measure ')) {
|
|
167
|
+
const f = scanFields(body.slice(8))
|
|
168
|
+
return {
|
|
169
|
+
kind: 'measure',
|
|
170
|
+
at,
|
|
171
|
+
session: f.session ?? '',
|
|
172
|
+
turn: Number(f.turn),
|
|
173
|
+
tool: f.tool ?? '',
|
|
174
|
+
ok: f.ok === 'true',
|
|
175
|
+
exit: f.exit === '-' || f.exit === void 0 || f.exit === '' ? null : Number(f.exit),
|
|
176
|
+
cleared: Number(f.cleared) || 0
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (body.startsWith('outcome ')) {
|
|
181
|
+
const f = scanFields(body.slice(8))
|
|
182
|
+
return {
|
|
183
|
+
kind: 'outcome',
|
|
184
|
+
at,
|
|
185
|
+
session: f.session ?? '',
|
|
186
|
+
turn: Number(f.turn),
|
|
187
|
+
inj: f.inj ?? '',
|
|
188
|
+
after: String(f.after ?? ''),
|
|
189
|
+
first: f.first ?? '',
|
|
190
|
+
// `class` 是保留字,日志里写信的是 class=,读的时候两个都认。
|
|
191
|
+
cls: f.class ?? f.cls ?? 'other',
|
|
192
|
+
steps: Number(f.steps) || 0,
|
|
193
|
+
// 提示之前那一步在干什么:有它才能说「从什么动作转到了什么动作」。
|
|
194
|
+
before: f.before ?? ''
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// 窗口内第一个真转向动作(verify/search)。中位步数只能从这算。
|
|
199
|
+
if (body.startsWith('change ')) {
|
|
200
|
+
const f = scanFields(body.slice(7))
|
|
201
|
+
return {
|
|
202
|
+
kind: 'change',
|
|
203
|
+
at,
|
|
204
|
+
session: f.session ?? '',
|
|
205
|
+
turn: Number(f.turn),
|
|
206
|
+
inj: f.inj ?? '',
|
|
207
|
+
after: String(f.after ?? ''),
|
|
208
|
+
tool: f.tool ?? '',
|
|
209
|
+
cls: f.class ?? 'other',
|
|
210
|
+
steps: Number(f.steps) || 0
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (body.startsWith('apply ')) {
|
|
215
|
+
const f = scanFields(body.slice(6))
|
|
216
|
+
return {
|
|
217
|
+
kind: 'apply',
|
|
218
|
+
at,
|
|
219
|
+
pid: Number(f.pid) || 0,
|
|
220
|
+
logFile: f.logFile ?? '',
|
|
221
|
+
thresholds: { A: Number(f.A) || 0, B: Number(f.B) || 0, C: Number(f.C) || 0 },
|
|
222
|
+
budget: Number(f.budget) || 0,
|
|
223
|
+
window: Number(f.window) || 0
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (body.startsWith('bundled skill registered:')) {
|
|
228
|
+
return { kind: 'skill', at, path: body.slice('bundled skill registered:'.length).trim() }
|
|
229
|
+
}
|
|
230
|
+
if (body.startsWith('disabled by config')) return { kind: 'disabled', at }
|
|
231
|
+
if (body.startsWith('skill register failed')) return { kind: 'skill-failed', at, message: body }
|
|
232
|
+
if (body.startsWith('panel register failed') || body.startsWith('panel unavailable')) {
|
|
233
|
+
return { kind: 'panel-failed', at, message: body }
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return { kind: 'other', at, text: body }
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** 日志全文 → 记录数组。坏行跳过,不影响其余。 */
|
|
240
|
+
export function parseLog(text) {
|
|
241
|
+
const records = []
|
|
242
|
+
for (const raw of String(text ?? '').split('\n')) {
|
|
243
|
+
const record = parseLine(raw)
|
|
244
|
+
if (record !== void 0) records.push(record)
|
|
245
|
+
}
|
|
246
|
+
return records
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function median(values) {
|
|
250
|
+
if (values.length === 0) return null
|
|
251
|
+
const sorted = [...values].sort((a, b) => a - b)
|
|
252
|
+
const mid = Math.floor(sorted.length / 2)
|
|
253
|
+
return sorted.length % 2 === 0 ? (sorted[mid - 1] + sorted[mid]) / 2 : sorted[mid]
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** 这五类是「动作类别」,不是「好坏标签」。 */
|
|
257
|
+
const CLASS_KEYS = ['verify', 'search', 'edit', 'shell', 'method', 'other']
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* 记录数组 → 面板要的全部数字。
|
|
261
|
+
* `thresholds` 只用于展示「当前这道闸门的档位」,不参与统计。
|
|
262
|
+
*/
|
|
263
|
+
export function aggregate(records, options = {}) {
|
|
264
|
+
const injects = []
|
|
265
|
+
const outcomes = []
|
|
266
|
+
const changes = []
|
|
267
|
+
const measures = []
|
|
268
|
+
const arms = []
|
|
269
|
+
const applies = []
|
|
270
|
+
const skills = []
|
|
271
|
+
let disabled = 0
|
|
272
|
+
let skillFailed = 0
|
|
273
|
+
let panelFailed = 0
|
|
274
|
+
let suppressed = 0
|
|
275
|
+
|
|
276
|
+
for (const record of records) {
|
|
277
|
+
if (record.kind === 'inject') injects.push(record)
|
|
278
|
+
else if (record.kind === 'outcome') outcomes.push(record)
|
|
279
|
+
else if (record.kind === 'change') changes.push(record)
|
|
280
|
+
else if (record.kind === 'measure') measures.push(record)
|
|
281
|
+
else if (record.kind === 'arm') arms.push(record)
|
|
282
|
+
else if (record.kind === 'apply') applies.push(record)
|
|
283
|
+
else if (record.kind === 'skill') skills.push(record)
|
|
284
|
+
else if (record.kind === 'disabled') disabled += 1
|
|
285
|
+
else if (record.kind === 'skill-failed') skillFailed += 1
|
|
286
|
+
else if (record.kind === 'panel-failed') panelFailed += 1
|
|
287
|
+
else if (record.kind === 'suppress') suppressed += 1
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const bySignal = { A: 0, B: 0, C: 0 }
|
|
291
|
+
const byTag = {}
|
|
292
|
+
const sessions = new Set()
|
|
293
|
+
const turns = new Set()
|
|
294
|
+
let chars = 0
|
|
295
|
+
let tokens = 0
|
|
296
|
+
for (const inject of injects) {
|
|
297
|
+
for (const kind of inject.signals) {
|
|
298
|
+
if (bySignal[kind] !== void 0) bySignal[kind] += 1
|
|
299
|
+
}
|
|
300
|
+
const tag = inject.signals.join('+') || '?'
|
|
301
|
+
byTag[tag] = (byTag[tag] ?? 0) + 1
|
|
302
|
+
if (inject.session.length > 0) sessions.add(inject.session)
|
|
303
|
+
if (Number.isFinite(inject.turn)) turns.add(`${inject.session}#${inject.turn}`)
|
|
304
|
+
chars += inject.chars
|
|
305
|
+
tokens += inject.tokens
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// 提示后第一个动作的分布。这是描述量,不是效果量。
|
|
309
|
+
const firstClass = Object.fromEntries(CLASS_KEYS.map((key) => [key, 0]))
|
|
310
|
+
const firstTool = {}
|
|
311
|
+
for (const outcome of outcomes) {
|
|
312
|
+
const cls = firstClass[outcome.cls] === void 0 ? 'other' : outcome.cls
|
|
313
|
+
firstClass[cls] += 1
|
|
314
|
+
const tool = outcome.first || '(none)'
|
|
315
|
+
firstTool[tool] = (firstTool[tool] ?? 0) + 1
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// 窗口内的转向:只有 change 行才算「真的转去验证/搜索了」。
|
|
319
|
+
const turnedClass = Object.fromEntries(CLASS_KEYS.map((key) => [key, 0]))
|
|
320
|
+
const stepsToTurn = []
|
|
321
|
+
for (const change of changes) {
|
|
322
|
+
const cls = turnedClass[change.cls] === void 0 ? 'other' : change.cls
|
|
323
|
+
turnedClass[cls] += 1
|
|
324
|
+
if (change.steps >= 0) stepsToTurn.push(change.steps)
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const reported = outcomes.length
|
|
328
|
+
const turned = changes.length
|
|
329
|
+
|
|
330
|
+
// 埋点世代:v0.2 之前没有 `inj=`,也就一定没有 `change` / `measure` 行。
|
|
331
|
+
// 这类老日志里「0 次转向」是**没测**,不是「测了没有」——两者必须分开,
|
|
332
|
+
// 否则面板会把「指标还没上线」算成「模型一次都没转向」,那就是凭空编数据。
|
|
333
|
+
// 注意 `parseLine` 把缺失的 inj 补成空串(不是 undefined),判据要按长度判。
|
|
334
|
+
const instrumented = injects.filter((inject) => String(inject.inj ?? '').length > 0).length
|
|
335
|
+
|
|
336
|
+
const lastApply = applies.length > 0 ? applies[applies.length - 1] : null
|
|
337
|
+
|
|
338
|
+
return {
|
|
339
|
+
ok: true,
|
|
340
|
+
generatedAt: new Date().toISOString(),
|
|
341
|
+
log: {
|
|
342
|
+
file: String(options.logFile ?? ''),
|
|
343
|
+
exists: options.exists === true,
|
|
344
|
+
bytes: Number(options.bytes) || 0,
|
|
345
|
+
lines: records.length,
|
|
346
|
+
disabled,
|
|
347
|
+
skillFailed,
|
|
348
|
+
panelFailed,
|
|
349
|
+
suppressed,
|
|
350
|
+
arms: arms.length,
|
|
351
|
+
skills: skills.length,
|
|
352
|
+
skillPath: skills.length > 0 ? skills[skills.length - 1].path : ''
|
|
353
|
+
},
|
|
354
|
+
thresholds: lastApply !== null && lastApply.thresholds.A > 0
|
|
355
|
+
? lastApply.thresholds
|
|
356
|
+
: {
|
|
357
|
+
A: Number(options.thresholds?.A) || 0,
|
|
358
|
+
B: Number(options.thresholds?.B) || 0,
|
|
359
|
+
C: Number(options.thresholds?.C) || 0
|
|
360
|
+
},
|
|
361
|
+
budget: lastApply?.budget ?? 0,
|
|
362
|
+
window: lastApply?.window ?? 0,
|
|
363
|
+
applied: { count: applies.length, lastAt: lastApply ? lastApply.at : '', pid: lastApply ? lastApply.pid : 0 },
|
|
364
|
+
injects: {
|
|
365
|
+
total: injects.length,
|
|
366
|
+
sessions: sessions.size,
|
|
367
|
+
turns: turns.size,
|
|
368
|
+
chars,
|
|
369
|
+
// 注入本身的开销是实测(chars → token 折算),保留;
|
|
370
|
+
// 「省掉多少」不折算——那需要假设「模型本来会失败」,面板不编这个数。
|
|
371
|
+
tokens,
|
|
372
|
+
bySignal,
|
|
373
|
+
byTag
|
|
374
|
+
},
|
|
375
|
+
// 提示后模型第一个动作是什么。字段名描述行为,不暗示因果。
|
|
376
|
+
firstAction: {
|
|
377
|
+
reported,
|
|
378
|
+
silent: injects.length - reported,
|
|
379
|
+
byClass: firstClass,
|
|
380
|
+
byTool: firstTool
|
|
381
|
+
},
|
|
382
|
+
// 窗口内有没有真的转去取得新事实,几 step 之后转的。
|
|
383
|
+
// observed 是分母:只算**埋点上线后**的注入。老日志里 observed=0,
|
|
384
|
+
// 面板该说「还没开始记」而不是「0% 转向」。
|
|
385
|
+
turned: {
|
|
386
|
+
observed: instrumented,
|
|
387
|
+
total: turned,
|
|
388
|
+
rate: instrumented > 0 ? turned / instrumented : null,
|
|
389
|
+
byClass: turnedClass,
|
|
390
|
+
medianSteps: median(stepsToTurn)
|
|
391
|
+
},
|
|
392
|
+
// 测量是「有没有回路」的硬证据:与动作分类无关,直接数。
|
|
393
|
+
measures: {
|
|
394
|
+
total: measures.length,
|
|
395
|
+
ok: measures.filter((measure) => measure.ok).length,
|
|
396
|
+
failed: measures.filter((measure) => !measure.ok).length,
|
|
397
|
+
cleared: measures.reduce((sum, measure) => sum + measure.cleared, 0)
|
|
398
|
+
},
|
|
399
|
+
estimate: {
|
|
400
|
+
injectionTokens: tokens
|
|
401
|
+
},
|
|
402
|
+
recent: injects.slice(-40).map((inject) => ({
|
|
403
|
+
at: inject.at,
|
|
404
|
+
session: inject.session,
|
|
405
|
+
turn: inject.turn,
|
|
406
|
+
step: inject.step,
|
|
407
|
+
inj: inject.inj,
|
|
408
|
+
tag: inject.signals.join('+'),
|
|
409
|
+
chars: inject.chars,
|
|
410
|
+
worstEdits: inject.worstEdits,
|
|
411
|
+
worstStreak: inject.worstStreak || 0,
|
|
412
|
+
worstErrors: inject.worstErrors,
|
|
413
|
+
searched: inject.searched,
|
|
414
|
+
measured: inject.measured,
|
|
415
|
+
sample: inject.sample
|
|
416
|
+
}))
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/** 读日志文件;不存在就当空,不抛。 */
|
|
421
|
+
export function readAggregate(logFile, extra = {}) {
|
|
422
|
+
const thresholds = extra.thresholds
|
|
423
|
+
if (typeof logFile !== 'string' || logFile.trim().length === 0) {
|
|
424
|
+
return aggregate([], { logFile: '', exists: false, thresholds })
|
|
425
|
+
}
|
|
426
|
+
let text = ''
|
|
427
|
+
let exists = false
|
|
428
|
+
let bytes = 0
|
|
429
|
+
try {
|
|
430
|
+
text = readFileSync(logFile, 'utf8')
|
|
431
|
+
exists = true
|
|
432
|
+
bytes = Buffer.byteLength(text, 'utf8')
|
|
433
|
+
} catch {
|
|
434
|
+
exists = false
|
|
435
|
+
}
|
|
436
|
+
return aggregate(parseLog(text), { logFile, exists, bytes, thresholds })
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* 把 `/better-debug` 前缀挂到 webServer 上。
|
|
441
|
+
* 只读:没有任何写接口,面板不改变插件行为。
|
|
442
|
+
*/
|
|
443
|
+
export function registerPanel(ctx, getConfig) {
|
|
444
|
+
// 摘掉上一代留下的路由(热重载时 webServer 的路由表是跨代共享的)。
|
|
445
|
+
// 两条路都走一遍:优先用上一代记下的 disposer;若上一代是没记 disposer 的旧版本,
|
|
446
|
+
// 直接从路由表里删掉同 key 的那条(`prefixes` 是 Map<path, route>)。
|
|
447
|
+
const previous = globalThis[PANEL_SLOT]
|
|
448
|
+
if (typeof previous === 'function') {
|
|
449
|
+
try { previous() } catch {}
|
|
450
|
+
}
|
|
451
|
+
globalThis[PANEL_SLOT] = void 0
|
|
452
|
+
const table = ctx.webServer?.prefixes
|
|
453
|
+
if (table instanceof Map && table.has('/better-debug')) table.delete('/better-debug')
|
|
454
|
+
|
|
455
|
+
let dispose
|
|
456
|
+
const handler = async (req, res) => {
|
|
457
|
+
const send = (status, type, body) => {
|
|
458
|
+
res.writeHead(status, {
|
|
459
|
+
'content-type': type,
|
|
460
|
+
'cache-control': 'no-store'
|
|
461
|
+
})
|
|
462
|
+
res.end(body)
|
|
463
|
+
}
|
|
464
|
+
try {
|
|
465
|
+
const url = new URL(req.url, 'http://localhost')
|
|
466
|
+
const pathname = url.pathname
|
|
467
|
+
|
|
468
|
+
if (pathname === '/better-debug' || pathname === '/better-debug/' || pathname === '/better-debug/index.html') {
|
|
469
|
+
send(200, 'text/html; charset=utf-8', readFileSync(join(WEB_ROOT, 'index.html')))
|
|
470
|
+
return
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
if (pathname === '/better-debug/api') {
|
|
474
|
+
const cfg = getConfig()
|
|
475
|
+
const stats = readAggregate(cfg.logFile, { thresholds: cfg })
|
|
476
|
+
if (url.searchParams.get('raw') === '1') {
|
|
477
|
+
send(
|
|
478
|
+
200,
|
|
479
|
+
'application/json; charset=utf-8',
|
|
480
|
+
JSON.stringify({ id: 'dsh-better-debug', version: 2 }, null, 2)
|
|
481
|
+
)
|
|
482
|
+
return
|
|
483
|
+
}
|
|
484
|
+
send(200, 'application/json; charset=utf-8', JSON.stringify(stats))
|
|
485
|
+
return
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
send(404, 'application/json; charset=utf-8', JSON.stringify({ ok: false, error: 'not-found' }))
|
|
489
|
+
} catch (error) {
|
|
490
|
+
try {
|
|
491
|
+
send(500, 'application/json; charset=utf-8', JSON.stringify({ ok: false, error: String(error?.message ?? error) }))
|
|
492
|
+
} catch {}
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
dispose = ctx.webServer.register({ kind: 'prefix', path: '/better-debug', handler })
|
|
497
|
+
globalThis[PANEL_SLOT] = dispose
|
|
498
|
+
// 交给 cordis 管生命周期:插件卸载/热重载时自动摘路由。
|
|
499
|
+
if (typeof ctx.effect === 'function' && typeof dispose === 'function') {
|
|
500
|
+
ctx.effect(() => dispose, 'better-debug.panel-route')
|
|
501
|
+
}
|
|
502
|
+
return dispose
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
export { WEB_ROOT }
|
package/lib/text.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 注入文本的唯一来源。
|
|
3
|
+
*
|
|
4
|
+
* 为什么单独一个模块:文本长度以前在 `lib/panel.js` 里用硬编码常量
|
|
5
|
+
* `{ A: 254, B: 268, C: 256 }` 回退,文案一改常量就漂,面板统计的字符数
|
|
6
|
+
* 和真实注入量对不上(实测偏差 ~50 字符)。现在两边都调 `messageText()`,
|
|
7
|
+
* 字符数**只有一个算法**,不存在漂移。
|
|
8
|
+
*
|
|
9
|
+
* 这个模块必须保持零依赖、无副作用:`lib/index.js`(运行时)和
|
|
10
|
+
* `lib/panel.js`(只读视图)都会 import 它,panel 还被单测直接 import。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 每个信号的观察句。{n} 由运行时填。
|
|
15
|
+
*
|
|
16
|
+
* 措辞纪律:只说**观察到的事实**,不说诊断。「你在原地打转」是结论,
|
|
17
|
+
* 插件能证明的只是「这几个动作重复了」——弱证据说成确定诊断会误导模型
|
|
18
|
+
* 去解释自己而不是去建回路。
|
|
19
|
+
*/
|
|
20
|
+
export const SIGNAL_TEXT = {
|
|
21
|
+
A: '同一个文件连续改了 {n} 次,中间没有任何测量。',
|
|
22
|
+
B: '同一个操作重复报错 {n} 次,没有新事实进来。',
|
|
23
|
+
C: '上次改完已经过了 {n} 步,这期间没有任何动作,也没测量。'
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 每个信号「最匹配的那一个动作」。只给一条:给三条等于没给。
|
|
28
|
+
* A/C 的处境是「没有回路」,B 的处境是「同一堵墙」。
|
|
29
|
+
*/
|
|
30
|
+
export const SIGNAL_ACTION = {
|
|
31
|
+
A: '建一条几秒能跑完、能给出数字或事实的回路(失败测试 / 复现命令 / 一条断言 / lsp_diagnostics);建不出来就停下要现场产物。',
|
|
32
|
+
B: '用报错原文整句 + 版本搜一次(10 分钟盒,GitHub closed issue 也看),或写一个只复现这一个报错的最小脚本。',
|
|
33
|
+
C: '先测量或先搜,再动下一处;如果任务其实已经收敛,直接说明结论。'
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const HEADER = '[better-debug] 观察到的重复动作(只是观察,不是结论):'
|
|
37
|
+
const RECOVERY = '恢复条件:拿到一条新的测量结果或一条外部事实后,继续动手即可。'
|
|
38
|
+
const SKILL_HINT = '完整五条:skill `stuck-gate`。'
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 信号数组 → 注入文本。
|
|
42
|
+
* @param {Array<{kind: keyof typeof SIGNAL_TEXT, n: number, detail?: string}>} signals
|
|
43
|
+
*/
|
|
44
|
+
export function messageText(signals) {
|
|
45
|
+
const list = Array.isArray(signals) ? signals : []
|
|
46
|
+
const reasons = list.map((signal) => {
|
|
47
|
+
const template = SIGNAL_TEXT[signal.kind]
|
|
48
|
+
if (template === void 0) return ''
|
|
49
|
+
const observed = template.replace('{n}', String(signal.n))
|
|
50
|
+
return `- ${observed}${signal.detail ? ` ${signal.detail}` : ''}`
|
|
51
|
+
}).filter((line) => line.length > 0)
|
|
52
|
+
|
|
53
|
+
// 多个信号同时命中时最多给两条动作,再多模型就开始跳过正文了。
|
|
54
|
+
const actions = []
|
|
55
|
+
for (const signal of list) {
|
|
56
|
+
const action = SIGNAL_ACTION[signal.kind]
|
|
57
|
+
if (action !== void 0 && !actions.includes(action) && actions.length < 2) actions.push(action)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return [
|
|
61
|
+
HEADER,
|
|
62
|
+
...reasons,
|
|
63
|
+
'下一步先做其中一件,拿到新事实再继续改:',
|
|
64
|
+
...actions.map((action, index) => `${index + 1}) ${action}`),
|
|
65
|
+
RECOVERY,
|
|
66
|
+
SKILL_HINT
|
|
67
|
+
].join('\n')
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** 中英混排的 token 粗估:CJK 1:1,其余 ~0.32。 */
|
|
71
|
+
const CJK = /[\u2e80-\u9fff\uf900-\ufaff\u3000-\u303f\uff00-\uffef]/u
|
|
72
|
+
|
|
73
|
+
export function estimateTokens(text) {
|
|
74
|
+
let cjk = 0
|
|
75
|
+
let other = 0
|
|
76
|
+
for (const char of String(text ?? '')) {
|
|
77
|
+
if (CJK.test(char)) cjk += 1
|
|
78
|
+
else other += 1
|
|
79
|
+
}
|
|
80
|
+
return Math.round(cjk + other * 0.32)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 老日志行没有 `chars=` / `tokens=` 时按信号组合回退。
|
|
85
|
+
* 不再是「常量表」而是「真算一遍文案」,所以永远和当前版本自洽。
|
|
86
|
+
*/
|
|
87
|
+
export function charsFor(signals) {
|
|
88
|
+
const list = Array.isArray(signals) ? signals : []
|
|
89
|
+
if (list.length === 0) return 0
|
|
90
|
+
return messageText(list.map((kind) => ({ kind, n: 0 }))).length
|
|
91
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-better-debug",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "卡住闸门的代码化:同一文件连续盲改 / 同一操作重复报错 / 改完没有回路又动手,命中就在下一步注入一次提醒,逼出一条几秒能跑的反馈回路。",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./lib/index.js",
|
|
9
|
+
"./client": "./client.js",
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"lib/",
|
|
14
|
+
"assets/",
|
|
15
|
+
"web/",
|
|
16
|
+
"client.js",
|
|
17
|
+
"cordis.patch.yml",
|
|
18
|
+
"README.md",
|
|
19
|
+
"README.en.md",
|
|
20
|
+
"LICENSE"
|
|
21
|
+
],
|
|
22
|
+
"keywords": [
|
|
23
|
+
"dsh",
|
|
24
|
+
"dsh-plugin",
|
|
25
|
+
"deepseek-harness",
|
|
26
|
+
"debugging",
|
|
27
|
+
"agent-hygiene",
|
|
28
|
+
"pre-step"
|
|
29
|
+
],
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"author": "Leonlee114514",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/Leonlee114514/dsh-better-debug.git"
|
|
35
|
+
},
|
|
36
|
+
"dsh": {
|
|
37
|
+
"bundle": {
|
|
38
|
+
"patch": "./cordis.patch.yml"
|
|
39
|
+
},
|
|
40
|
+
"client": {
|
|
41
|
+
"platform": "web",
|
|
42
|
+
"inject": [
|
|
43
|
+
"slots"
|
|
44
|
+
]
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"dsh": ">=0.2.1-rc.5"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": ">=20"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"check": "node --check lib/index.js && node --check lib/panel.js && node --check lib/text.js && node --check client.js && node --check test/sim.mjs && node --check test/panel.mjs && node --check test/posix/register.mjs && node --check test/posix/hooks.mjs && node --check test/posix/path.mjs",
|
|
55
|
+
"test:unit": "node test/sim.mjs && node test/panel.mjs",
|
|
56
|
+
"test:posix": "node --import ./test/posix/register.mjs test/sim.mjs && node --import ./test/posix/register.mjs test/panel.mjs",
|
|
57
|
+
"test": "npm run test:unit && npm run test:posix",
|
|
58
|
+
"verify": "npm run check && npm test"
|
|
59
|
+
}
|
|
60
|
+
}
|