@kangyishuai/dsh-next-step 0.4.2 → 0.5.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/README.md +37 -30
- package/README.zh-CN.md +38 -31
- package/lib/client.js +462 -34
- package/lib/events.js +1 -1
- package/lib/followup.js +205 -0
- package/lib/gates.js +14 -1
- package/lib/generate-status.js +107 -0
- package/lib/inflight.js +141 -0
- package/lib/prompt.js +268 -9
- package/lib/report.js +122 -10
- package/lib/scheme.js +1 -1
- package/lib/service.js +422 -143
- package/lib/session-records.js +53 -0
- package/lib/settings.js +28 -10
- package/lib/turn-dispatch.js +49 -0
- package/lib/write-plan.js +25 -0
- package/locale/en.json +13 -0
- package/locale/zh.json +13 -0
- package/package.json +1 -1
package/lib/prompt.js
CHANGED
|
@@ -9,6 +9,13 @@
|
|
|
9
9
|
// - `label` 就是点击后原样发送的用户消息(WYSIWYG);2–20 字、必须是「动词 + 具体对象」。
|
|
10
10
|
// - **宽容解析**:超长的展示文本截断、不合格的单条 option 丢掉,不再因为一个字段超长就整张卡作废。
|
|
11
11
|
// 只有「结构不可用 / 三档判定缺失 / 一条可用建议都不剩」这三种才算失败,交给 service 做一次修复调用。
|
|
12
|
+
//
|
|
13
|
+
// 落地不变量(见 docs/adr/0023-next-step-grounded-cards.md):
|
|
14
|
+
// - **任何一次要产出卡片内容的调用,都必须带同一段对话**。修复调用原来不带,于是模型只能把
|
|
15
|
+
// 「上一次输出为空」写成一张卡(「这一轮没有拿到任何对话内容…把对话贴进来?」);
|
|
16
|
+
// - **空输出不是格式问题**:重发原请求,不拿空字符串去「修格式」;
|
|
17
|
+
// - **结构合法不等于有用**:卡片里至少要有一个对象能在对话里原样找到(`isGroundedIn`),否则丢弃;
|
|
18
|
+
// - **对话本身是空的就不调模型**:那是接线故障(`deriveMessages` 没接上),不是模型的问题。
|
|
12
19
|
|
|
13
20
|
import { PROMPT_VERSION } from "./scheme.js";
|
|
14
21
|
|
|
@@ -38,6 +45,22 @@ export const STANCE_MAX = 40;
|
|
|
38
45
|
export const OPTIONS_MAX = 2;
|
|
39
46
|
/** 修复调用里回贴模型原文的最大长度(避免把上次超长输出再喂回去撑爆上下文)。 */
|
|
40
47
|
export const REPAIR_ECHO_MAX = 4000;
|
|
48
|
+
/**
|
|
49
|
+
* 能做「落地校验」的对话材料上限(每行字符数)。
|
|
50
|
+
*
|
|
51
|
+
* 校验用的是「模型看到的那段对话」的**未裁剪**版本:卡片摘要里的数字/文件名常常出现在
|
|
52
|
+
* 回答的后半段,而喂给模型的 transcript 每条只留 800 字。这里放宽到 4000 字,只用于机械比对,
|
|
53
|
+
* 不发给模型。实测 909 张正常卡里 908 张通过(唯一不过的那张,对象全部是中文虚指)。
|
|
54
|
+
*/
|
|
55
|
+
export const MATERIAL_CLIP = 4000;
|
|
56
|
+
/**
|
|
57
|
+
* 判定「这段对话有没有可依据的内容」的最小字符数。
|
|
58
|
+
*
|
|
59
|
+
* 不是质量阈值,而是**接线检查**:`deriveMessages()` 没接上、或被宿主改名时 transcript 为空,
|
|
60
|
+
* 此时无论模型多强都只能编——历史事故里出现的正是这种卡(「这一轮没有拿到任何对话内容」)。
|
|
61
|
+
* 实测本机 909 张正常卡的 transcript 最短 935 字,离 40 很远,所以这个阈值不会误伤真实回合。
|
|
62
|
+
*/
|
|
63
|
+
export const MIN_CONTEXT_CHARS = 40;
|
|
41
64
|
/** 重新导出:提示词版本位,service 写卡片与事件时要用。 */
|
|
42
65
|
export { PROMPT_VERSION };
|
|
43
66
|
|
|
@@ -162,6 +185,203 @@ export function ghostAgreesWithLabel(ghost, label) {
|
|
|
162
185
|
return false;
|
|
163
186
|
}
|
|
164
187
|
|
|
188
|
+
/**
|
|
189
|
+
* 这些词只说明「是哪一类东西」,指认不了「是哪一件事」——对话里出现也不算落地证据。
|
|
190
|
+
* 前一组是**生成过程自己的词**:模型一旦自述生成过程(「上一次输出为空」「JSON 解析失败」),
|
|
191
|
+
* 卡片就会出现这些词;后一组是泛化名词,任何一轮的对话里都能找到。
|
|
192
|
+
*/
|
|
193
|
+
const NOT_IDENTITY = new Set([
|
|
194
|
+
// 生成过程 / 协议字段
|
|
195
|
+
"json",
|
|
196
|
+
"summary",
|
|
197
|
+
"question",
|
|
198
|
+
"value",
|
|
199
|
+
"options",
|
|
200
|
+
"option",
|
|
201
|
+
"stance",
|
|
202
|
+
"label",
|
|
203
|
+
"nextprompt",
|
|
204
|
+
"diff",
|
|
205
|
+
"pr",
|
|
206
|
+
"api",
|
|
207
|
+
"md",
|
|
208
|
+
"e2e",
|
|
209
|
+
// 泛化名词
|
|
210
|
+
"文件名",
|
|
211
|
+
"命令",
|
|
212
|
+
"目录",
|
|
213
|
+
"文件",
|
|
214
|
+
"代码",
|
|
215
|
+
"测试",
|
|
216
|
+
"结果",
|
|
217
|
+
"数值",
|
|
218
|
+
"版本",
|
|
219
|
+
"分支",
|
|
220
|
+
"仓库",
|
|
221
|
+
"提交",
|
|
222
|
+
"修复",
|
|
223
|
+
"实现",
|
|
224
|
+
"功能",
|
|
225
|
+
"问题",
|
|
226
|
+
"改动",
|
|
227
|
+
"更新",
|
|
228
|
+
"文档",
|
|
229
|
+
"卡片",
|
|
230
|
+
"对话",
|
|
231
|
+
"原文",
|
|
232
|
+
"消息",
|
|
233
|
+
"请求",
|
|
234
|
+
"输出",
|
|
235
|
+
"输入",
|
|
236
|
+
"回复",
|
|
237
|
+
"内容",
|
|
238
|
+
"字段",
|
|
239
|
+
"解析",
|
|
240
|
+
"报错",
|
|
241
|
+
"错误",
|
|
242
|
+
"任务",
|
|
243
|
+
"步骤",
|
|
244
|
+
"流程",
|
|
245
|
+
"结论",
|
|
246
|
+
"摘要",
|
|
247
|
+
"提问",
|
|
248
|
+
"建议",
|
|
249
|
+
"决策卡",
|
|
250
|
+
"上一轮",
|
|
251
|
+
"上一次",
|
|
252
|
+
"上一条",
|
|
253
|
+
"这一轮",
|
|
254
|
+
"这一步",
|
|
255
|
+
"下一步",
|
|
256
|
+
"继续",
|
|
257
|
+
"可以",
|
|
258
|
+
"需要",
|
|
259
|
+
"以下",
|
|
260
|
+
"这段",
|
|
261
|
+
]);
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* 从文本里抽「能指认是哪一件事」的对象:版本号、文件名、命令、PR 号、带数字/下划线的标识符,
|
|
265
|
+
* 以及 ≥4 字、不像泛化名词的中文片段。
|
|
266
|
+
* 与 `concreteTokens` 的区别:后者用于**按钮文案**(宁松勿严,只要沾上具体对象就放行),
|
|
267
|
+
* 这里用于**落地校验**(宁严勿松:泛化名词和生成过程自己的词都不算对象)。
|
|
268
|
+
* @param {string} text
|
|
269
|
+
* @returns {Set<string>}
|
|
270
|
+
*/
|
|
271
|
+
export function identityTokens(text) {
|
|
272
|
+
const source = typeof text === "string" ? text : "";
|
|
273
|
+
const found = new Set();
|
|
274
|
+
for (const match of source.matchAll(/[A-Za-z0-9_./#-]{3,}/gu)) {
|
|
275
|
+
const token = match[0].replace(/^[./#-]+|[./#-]+$/gu, "").toLowerCase();
|
|
276
|
+
if (token.length < 3 || NOT_IDENTITY.has(token)) continue;
|
|
277
|
+
// 有数字 / 带路径分隔符 / 足够长的英文标识符:都能指认具体对象
|
|
278
|
+
if (/[0-9]/u.test(token) || /[./_#-]/u.test(token) || token.length >= 4) found.add(token);
|
|
279
|
+
}
|
|
280
|
+
for (const match of source.matchAll(/[\u4e00-\u9fa5]{4,}/gu)) {
|
|
281
|
+
const run = match[0];
|
|
282
|
+
// 中文动词/介词常粘在名词前面(「把二分查找」),去掉最多两个前导字再比对
|
|
283
|
+
for (let cut = 0; cut <= 2; cut += 1) {
|
|
284
|
+
const token = run.slice(cut);
|
|
285
|
+
if (token.length < 4) break;
|
|
286
|
+
if (!NOT_IDENTITY.has(token)) found.add(token);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return found;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* 落地校验:卡片里至少有一个对象,能在**模型看到的那段对话**里原样找到。
|
|
294
|
+
*
|
|
295
|
+
* 这是「卡片必须在讲这一段对话」的机械判据,补的是提示词管不到的那一半:
|
|
296
|
+
* 结构合法、文案漂亮,但整段都在讲生成过程或者凭空想象的卡片,一律不落地。
|
|
297
|
+
*
|
|
298
|
+
* 两条通道,命中一条即落地:
|
|
299
|
+
* 1. **具体对象原样出现**(`identityTokens`):文件名、命令、版本号、PR 号这类硬证据;
|
|
300
|
+
* 2. **共享 ≥2 条中文 4 字片段**:短回答(一句话解释一个概念)里,卡片通常是在**改写**答案而不是
|
|
301
|
+
* 引用文件名,一个具体对象都没有。这条通道按「有没有在讲同一件事」判,而不是按「有没有抄到 ID」。
|
|
302
|
+
* 中文 4-gram 能扛住前后粘着的动词(「用一句话把二分查找定义为」里含「二分查找」)。
|
|
303
|
+
*
|
|
304
|
+
* 实测(本机 1038 条历史卡片,其中 119 条自述式):只有通道 1 时正常卡 918/919、拦下自述卡 98/119;
|
|
305
|
+
* 加上通道 2 之后正常卡 919/919、仍然拦下 98/119 —— 只赚不亏。没有它,一句话回答的回合会被误杀
|
|
306
|
+
* (2026-11-21 桌面 --deep 实测:卡片共享 12 条 4 字片段、0 个具体对象,被误判成「与对话无关」)。
|
|
307
|
+
* @param {string} text 卡片正文(summary + question + options + nextPrompt)
|
|
308
|
+
* @param {string} material 模型看到的对话材料(可以未裁剪)
|
|
309
|
+
* @returns {boolean}
|
|
310
|
+
*/
|
|
311
|
+
export function isGroundedIn(text, material) {
|
|
312
|
+
const haystack = typeof material === "string" ? material.toLowerCase() : "";
|
|
313
|
+
if (haystack.trim() === "") return false;
|
|
314
|
+
for (const token of identityTokens(text)) {
|
|
315
|
+
if (haystack.includes(token)) return true;
|
|
316
|
+
}
|
|
317
|
+
return sharedChineseGramCount(text, haystack) >= CN_GRAM_MIN;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** 中文 4-gram 的窗口大小。 */
|
|
321
|
+
const CN_GRAM = 4;
|
|
322
|
+
/** 卡片与对话至少共享这么多条 4 字片段才算在讲同一件事。 */
|
|
323
|
+
const CN_GRAM_MIN = 2;
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* 卡片与对话共享多少条不同的中文 4 字片段(只看卡片侧,逐条在对话里找)。
|
|
327
|
+
* @param {string} text 卡片正文
|
|
328
|
+
* @param {string} haystack 已小写的对话材料
|
|
329
|
+
* @returns {number}
|
|
330
|
+
*/
|
|
331
|
+
export function sharedChineseGramCount(text, haystack) {
|
|
332
|
+
const source = typeof text === "string" ? text : "";
|
|
333
|
+
const hay = typeof haystack === "string" ? haystack : "";
|
|
334
|
+
if (hay === "") return 0;
|
|
335
|
+
const seen = new Set();
|
|
336
|
+
for (const match of source.matchAll(/[\u4e00-\u9fa5]{4,}/gu)) {
|
|
337
|
+
const run = match[0];
|
|
338
|
+
for (let index = 0; index + CN_GRAM <= run.length; index += 1) {
|
|
339
|
+
const gram = run.slice(index, index + CN_GRAM);
|
|
340
|
+
if (!seen.has(gram) && hay.includes(gram)) seen.add(gram);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
return seen.size;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* 卡片正文:落地校验要看全部展示文本,不只是按钮文案。
|
|
348
|
+
* @param {{ summary?: string, question?: string, options?: { label?: string }[], nextPrompt?: string }} suggestion
|
|
349
|
+
* @returns {string}
|
|
350
|
+
*/
|
|
351
|
+
export function groundableText(suggestion) {
|
|
352
|
+
if (!suggestion || typeof suggestion !== "object") return "";
|
|
353
|
+
const parts = [suggestion.summary, suggestion.question, suggestion.nextPrompt];
|
|
354
|
+
for (const option of Array.isArray(suggestion.options) ? suggestion.options : []) {
|
|
355
|
+
parts.push(option?.label);
|
|
356
|
+
}
|
|
357
|
+
return parts.filter((part) => typeof part === "string" && part !== "").join("\n");
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* 这段对话是否够长到能作为判断依据(接线检查,不是质量阈值)。
|
|
362
|
+
* @param {string} transcript
|
|
363
|
+
* @returns {boolean}
|
|
364
|
+
*/
|
|
365
|
+
export function hasContext(transcript) {
|
|
366
|
+
const text = typeof transcript === "string" ? transcript.trim() : "";
|
|
367
|
+
return text.length >= MIN_CONTEXT_CHARS;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* 解析失败后走哪条恢复路径:
|
|
372
|
+
* - `retry`:首次输出**是空的**。空输出不是格式问题,拿它去「修格式」只会让模型对着
|
|
373
|
+
* 「(空)」编一张卡片(历史上 95 条垃圾卡就是这么来的);正确做法是把**同一个请求**
|
|
374
|
+
* 原样再发一次。
|
|
375
|
+
* - `repair`:有内容但格式不对(不是 JSON / 缺字段 / 按钮文案超长)。这时确实该修格式,
|
|
376
|
+
* 且修复请求必须带上同一段对话。
|
|
377
|
+
* @param {{ raw?: unknown, problem?: unknown }} input
|
|
378
|
+
* @returns {"retry" | "repair"}
|
|
379
|
+
*/
|
|
380
|
+
export function recoveryPlan(input = {}) {
|
|
381
|
+
const raw = typeof input.raw === "string" ? input.raw.trim() : "";
|
|
382
|
+
return raw === "" ? "retry" : "repair";
|
|
383
|
+
}
|
|
384
|
+
|
|
165
385
|
/**
|
|
166
386
|
* 系统提示词:判断这一轮是否值得继续(三档),并把对话压成一张决策卡,严格 JSON。
|
|
167
387
|
*
|
|
@@ -205,8 +425,12 @@ export function buildSystemPrompt(options = {}) {
|
|
|
205
425
|
" 它和 options 必须是**同一个建议**的两种说法(可以更长、更口语),不许各说一件事。",
|
|
206
426
|
`7. 语言:跟随对话中用户使用的语言(当前界面语言 ${locale},仅供参考)。`,
|
|
207
427
|
'8. **即使 value 判为 "no",也要把 summary / question / options 写完整** —— 用户可能手动要这张卡。',
|
|
208
|
-
"9.
|
|
209
|
-
|
|
428
|
+
"9. **这段对话就是你手上的全部材料,它不缺东西**:绝不要要求用户「把对话/原文/改动/回复贴过来」,",
|
|
429
|
+
' 也不要把「信息不足」当成一个方向。对话里确实没有能写进 label 的具体对象时:value 判 "no",',
|
|
430
|
+
" summary 只写这段对话里唯一可核查的事实,只给 1 条 label —— 宁可不给建议,也不要写一张要材料的卡。",
|
|
431
|
+
"10. **不要描述你自己这次生成的过程**:不要出现「上一次输出」「JSON」「解析失败」「summary / question 字段」",
|
|
432
|
+
" 「决策卡内容」「本次请求」这类字样 —— 它们不是对话里的事实,写出来只会让用户看到一张自言自语的空卡。",
|
|
433
|
+
"11. 拿不准就不要编:任何一条 label 找不到对话里的具体对象时,直接减少 options 数量(最少 1 条)。",
|
|
210
434
|
].join("\n");
|
|
211
435
|
}
|
|
212
436
|
|
|
@@ -229,41 +453,76 @@ export function buildMessages(input = {}) {
|
|
|
229
453
|
/**
|
|
230
454
|
* 失败原因 → 给修复调用的中文说明。
|
|
231
455
|
* @param {"json" | "value" | "options"} kind
|
|
456
|
+
* @param {unknown} [detail] `parseSuggestion` 的 problem.detail(用来给出可执行的收紧信息)
|
|
232
457
|
* @returns {string}
|
|
233
458
|
*/
|
|
234
|
-
export function problemText(kind) {
|
|
459
|
+
export function problemText(kind, detail) {
|
|
235
460
|
if (kind === "value") {
|
|
236
461
|
return 'value 必须是 "must" / "could" / "no" 三者之一(不要用 needed 之类的旧字段代替)';
|
|
237
462
|
}
|
|
238
463
|
if (kind === "options") {
|
|
239
|
-
|
|
464
|
+
const base = `options 里没有一条可用的建议:需要 1–${OPTIONS_MAX} 条,label 为 ${LABEL_MIN}–${LABEL_MAX} 字、必须是「动词 + 对话里的具体对象」,不能是「检查定义是否准确」这类放到哪一轮都成立的空话`;
|
|
465
|
+
// 把「你上一次到底写了多少字」直接写进修复请求:模型数不准自己的字数(实测 20 字上限
|
|
466
|
+
// 被写成 21–22 字,只提示「2–20 字」修不回来),给出实际数字才知道要压多少。
|
|
467
|
+
const overshoot = overlongLabelHint(detail);
|
|
468
|
+
return overshoot === "" ? base : `${base}。${overshoot}`;
|
|
240
469
|
}
|
|
241
470
|
return "不是合法 JSON,或缺少 summary / question 这两个必填文本字段";
|
|
242
471
|
}
|
|
243
472
|
|
|
244
473
|
/**
|
|
245
|
-
*
|
|
474
|
+
* 从解析失败的 detail 里读「上一次 label 到底几字」,生成一句可执行的收紧提示。
|
|
475
|
+
* @param {unknown} detail `parseSuggestion` 的 problem.detail(如 `dropped-option:0:label-length:22`)
|
|
476
|
+
* @returns {string} 没有长度问题时返回空串
|
|
477
|
+
*/
|
|
478
|
+
export function overlongLabelHint(detail) {
|
|
479
|
+
const text = typeof detail === "string" ? detail : "";
|
|
480
|
+
/** @type {number[]} */
|
|
481
|
+
const lengths = [];
|
|
482
|
+
for (const match of text.matchAll(/label-length:(\d+)/gu)) {
|
|
483
|
+
const value = Number(match[1]);
|
|
484
|
+
if (Number.isSafeInteger(value) && value > 0) lengths.push(value);
|
|
485
|
+
}
|
|
486
|
+
if (lengths.length === 0) return "";
|
|
487
|
+
const longest = Math.max(...lengths);
|
|
488
|
+
return `你上一次的 label 有 ${lengths.join(" / ")} 字,上限是 ${LABEL_MAX} 字(最长那条超了 ${longest - LABEL_MAX} 字)——请压到 ${LABEL_MAX} 字以内,并保留同一个具体对象`;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* 构造「修复」调用:**把同一段对话再带上**,然后回贴上次原文与具体问题,只要求它修格式。
|
|
493
|
+
*
|
|
494
|
+
* 为什么必须带对话(否则就是历史事故的根因):修复只回贴「上一次输出 + 报错」,模型手里就只剩
|
|
495
|
+
* 「上一次输出为空、缺少 summary / question」这一条信息;它的唯一出路是把这条信息写成一张卡:
|
|
496
|
+
* 「这一轮没有拿到任何对话内容……把对话贴进来?」。这张卡结构合法、能过校验、还能显示出来 ——
|
|
497
|
+
* 于是「没有对话内容」的卡反复出现在用户面前。一次修复不能凭空修内容,它必须看得见原料。
|
|
246
498
|
*
|
|
247
499
|
* 刻意只用一条 user 消息、不伪造 assistant 轮次(宿主 adapter 对 assistant 消息有额外要求,
|
|
248
500
|
* 伪造容易在 provider 侧被拒)。
|
|
249
|
-
* @param {{ raw: unknown, problem: "json" | "value" | "options" }} input
|
|
501
|
+
* @param {{ transcript?: string, raw: unknown, problem: "json" | "value" | "options", detail?: unknown }} input
|
|
250
502
|
* @returns {{ role: "user", content: { type: "text", text: string }[] }[]}
|
|
251
503
|
*/
|
|
252
504
|
export function buildRepairMessage(input) {
|
|
505
|
+
const transcript = typeof input.transcript === "string" ? input.transcript.trim() : "";
|
|
253
506
|
const raw = typeof input.raw === "string" ? input.raw : String(input.raw ?? "");
|
|
254
507
|
const echoed = raw.length > REPAIR_ECHO_MAX ? `${raw.slice(0, REPAIR_ECHO_MAX)}…` : raw;
|
|
255
508
|
const text = [
|
|
256
|
-
"
|
|
257
|
-
`问题:${problemText(input.problem)}。`,
|
|
509
|
+
"以下是最近的对话(与上一次请求相同),请据此修正你上一次的决策卡输出。",
|
|
258
510
|
"",
|
|
259
|
-
"
|
|
511
|
+
"对话:",
|
|
512
|
+
transcript === "" ? "(没有可用的对话内容)" : transcript,
|
|
513
|
+
"",
|
|
514
|
+
"你上一次的输出:",
|
|
260
515
|
'"""',
|
|
261
516
|
echoed === "" ? "(空)" : echoed,
|
|
262
517
|
'"""',
|
|
263
518
|
"",
|
|
519
|
+
`问题:${problemText(input.problem, input.detail)}。`,
|
|
520
|
+
"",
|
|
264
521
|
"请只输出修正后的 JSON 对象,结构如下(字段含义与之前的要求完全相同):",
|
|
265
522
|
'{ "value": "must" | "could" | "no", "reason": "…", "summary": "…", "question": "…", "options": [ { "stance": "…", "label": "…" } ], "nextPrompt": "…" }',
|
|
266
523
|
"不要输出任何解释、不要用代码围栏。",
|
|
524
|
+
"summary / question / options 的内容必须仍然来自上面这段对话;不要描述这次修正本身," +
|
|
525
|
+
"也不要要求用户补充对话内容 —— 对话就在上面。",
|
|
267
526
|
].join("\n");
|
|
268
527
|
return [{ role: "user", content: [{ type: "text", text }] }];
|
|
269
528
|
}
|
package/lib/report.js
CHANGED
|
@@ -23,6 +23,14 @@ export const MIN_SHOWN = 25;
|
|
|
23
23
|
export const REPORT_KEEP = 20;
|
|
24
24
|
/** Wilson 区间的 z 值(95%)。 */
|
|
25
25
|
export const WILSON_Z = 1.96;
|
|
26
|
+
/** 非空格式失败占比超过这个值,才判「解析 / 修复调用」需要动。 */
|
|
27
|
+
export const SCHEMA_FAILURE_SHARE = 0.15;
|
|
28
|
+
/** 空输出至少这么多条、且占 schema 失败过半,才判「模型空输出」而不是格式问题。 */
|
|
29
|
+
export const MIN_EMPTY_GENERATIONS = 5;
|
|
30
|
+
/** 空输出占 schema 失败的比例阈值(高于它说明主要矛盾不是解析)。 */
|
|
31
|
+
export const EMPTY_SHARE_THRESHOLD = 0.5;
|
|
32
|
+
/** 空输出的 detail 归一类(service 写入的 `json:empty`)。 */
|
|
33
|
+
export const EMPTY_DETAIL = "json:empty";
|
|
26
34
|
|
|
27
35
|
/**
|
|
28
36
|
* 逐行解析事件文件(坏行跳过并计数,绝不抛错)。
|
|
@@ -84,6 +92,38 @@ export function intervalsOverlap(left, right) {
|
|
|
84
92
|
return Math.max(left.low, right.low) <= Math.min(left.high, right.high);
|
|
85
93
|
}
|
|
86
94
|
|
|
95
|
+
/**
|
|
96
|
+
* schema 类 error 的 `detail` 归一成一个可数的桶。
|
|
97
|
+
*
|
|
98
|
+
* 为什么需要这层(AUD-HIST-01):0.4.3 的 `schema` 错误从 pv3 的 19 涨到 pv4 的 102,但其中
|
|
99
|
+
* 79 条(77%)是**模型返回空文本**(`json:empty`)——那是 0.4.3 把「空输出重发原请求」的恢复
|
|
100
|
+
* 路径暴露出来的口径变化,不是校验变严。报表若把两者混在一个 `schemaShare` 里,就会得出
|
|
101
|
+
* 「解析要放宽」的错误结论。所以这里只做「空 / 非空」两类的拆分,供 summarize 与 verdicts 用。
|
|
102
|
+
*
|
|
103
|
+
* 识别的形状(service.js 实际写入):
|
|
104
|
+
* - `model output did not match the card schema (json:empty)` → `json:empty`
|
|
105
|
+
* - `... (json:no-object)` / `... (json:unparsable)` / `... (value:...)` → 原样保留
|
|
106
|
+
* - `... (options:dropped-option:0:label-length:22)` → 归成 `options`(内部细节不参与计数)
|
|
107
|
+
* - `generated card failed validation` → `card-validation`
|
|
108
|
+
* - 缺失 / 认不出 → `unknown`(按非空格式失败处理,宁可算进 schemaShare 也不藏起来)
|
|
109
|
+
* @param {unknown} detail
|
|
110
|
+
* @returns {string}
|
|
111
|
+
*/
|
|
112
|
+
export function schemaDetailBucket(detail) {
|
|
113
|
+
const text = typeof detail === "string" ? detail.trim() : "";
|
|
114
|
+
if (text === "") return "unknown";
|
|
115
|
+
if (/^generated card failed validation/u.test(text)) return "card-validation";
|
|
116
|
+
// 截断的 200 字 detail 仍保留结尾的 `(...)`,所以从右括号回取即可。
|
|
117
|
+
const wrapped = /\(([^()]*)\)\s*$/u.exec(text);
|
|
118
|
+
const tail = (wrapped === null ? text : wrapped[1]).trim();
|
|
119
|
+
if (tail === "") return "unknown";
|
|
120
|
+
const colon = tail.indexOf(":");
|
|
121
|
+
if (colon === -1) return tail;
|
|
122
|
+
// options 的 detail 自带冒号(dropped-option:0:label-length:22),桶名只留一类。
|
|
123
|
+
if (tail.slice(0, colon) === "options") return "options";
|
|
124
|
+
return tail;
|
|
125
|
+
}
|
|
126
|
+
|
|
87
127
|
/**
|
|
88
128
|
* label 长度分桶(复盘按钮文案用)。
|
|
89
129
|
* @param {number} length
|
|
@@ -230,14 +270,19 @@ export function summarize(events) {
|
|
|
230
270
|
bump(byTier, String(turn.tier ?? "unknown"), isClicked);
|
|
231
271
|
bump(byOptionCount, String(Array.isArray(cand.opts) ? cand.opts.length : 0), isClicked);
|
|
232
272
|
bump(byPromptVersion, String(turn.pv ?? 0), isClicked);
|
|
233
|
-
if (action !== undefined && action.kind === "clicked" && Number.isInteger(action.index)) {
|
|
234
|
-
bump(byIndex, String(action.index), isClicked);
|
|
235
|
-
}
|
|
236
273
|
const opts = Array.isArray(cand.opts) ? cand.opts : [];
|
|
237
|
-
const clickIndex =
|
|
274
|
+
const clickIndex =
|
|
275
|
+
action !== undefined && action.kind === "clicked" && Number.isInteger(action.index)
|
|
276
|
+
? action.index
|
|
277
|
+
: -1;
|
|
238
278
|
for (let index = 0; index < opts.length; index += 1) {
|
|
279
|
+
const hit = clickIndex === index;
|
|
280
|
+
// 分母 = 该位置被**展示**的次数,点击率才是 clicked/n。
|
|
281
|
+
// 早先只在点击时 bump(n 恒等于点击次数),于是 `second-option-useless` 要求的 n>=20
|
|
282
|
+
// 结构上永远达不到——真实数据里第二粒 0/72 点击,结论永远报不出来(AUD-NS-03)。
|
|
283
|
+
bump(byIndex, String(index), hit);
|
|
239
284
|
const labelLen = Number(opts[index]?.len ?? 0);
|
|
240
|
-
bump(byLabelBucket, labelBucketOf(labelLen),
|
|
285
|
+
bump(byLabelBucket, labelBucketOf(labelLen), hit);
|
|
241
286
|
}
|
|
242
287
|
} else {
|
|
243
288
|
suppressed += 1;
|
|
@@ -263,6 +308,17 @@ export function summarize(events) {
|
|
|
263
308
|
errorKinds[String(error.kind ?? "unknown")] =
|
|
264
309
|
(errorKinds[String(error.kind ?? "unknown")] ?? 0) + 1;
|
|
265
310
|
|
|
311
|
+
// schema 失败拆成「模型返回空文本」与「非空格式失败」两类(口径见 schemaDetailBucket)。
|
|
312
|
+
const schemaErrors = errors.filter((error) => error.kind === "schema");
|
|
313
|
+
/** @type {Record<string, number>} */
|
|
314
|
+
const schemaDetails = {};
|
|
315
|
+
for (const error of schemaErrors) {
|
|
316
|
+
const bucket = schemaDetailBucket(error.detail);
|
|
317
|
+
schemaDetails[bucket] = (schemaDetails[bucket] ?? 0) + 1;
|
|
318
|
+
}
|
|
319
|
+
const emptyGenerations = schemaDetails[EMPTY_DETAIL] ?? 0;
|
|
320
|
+
const formatFailures = schemaErrors.length - emptyGenerations;
|
|
321
|
+
|
|
266
322
|
const gateOkTurns = (gateCounts.ok ?? 0) + errors.length;
|
|
267
323
|
return {
|
|
268
324
|
scheme: typeof meta?.scheme === "string" ? meta.scheme : "v0.4",
|
|
@@ -303,8 +359,22 @@ export function summarize(events) {
|
|
|
303
359
|
byLabelBucket,
|
|
304
360
|
byPromptVersion,
|
|
305
361
|
gates: gateCounts,
|
|
306
|
-
errors: {
|
|
307
|
-
|
|
362
|
+
errors: {
|
|
363
|
+
total: errors.length,
|
|
364
|
+
byKind: errorKinds,
|
|
365
|
+
// 仅 schema 类(格式失败 / 空输出 / 落地后校验失败)的 detail 拆分。
|
|
366
|
+
byDetail: schemaDetails,
|
|
367
|
+
schema: schemaErrors.length,
|
|
368
|
+
empty: emptyGenerations,
|
|
369
|
+
format: formatFailures,
|
|
370
|
+
},
|
|
371
|
+
// 格式失败率:分子只算**非空**的格式失败(空输出是 provider/输出长度问题,不是「解析要放宽」)。
|
|
372
|
+
schemaShare: gateOkTurns === 0 ? 0 : formatFailures / gateOkTurns,
|
|
373
|
+
// 空输出占 schema 失败的比例;没有 schema 失败时没分母 → null(不是 0)。
|
|
374
|
+
emptyShare: schemaErrors.length === 0 ? null : emptyGenerations / schemaErrors.length,
|
|
375
|
+
// 落地校验丢弃率:结构合法但讲不到这段对话的卡(自述生成过程 / 凭空想象)。
|
|
376
|
+
// 单独一个指标,因为它对应的病根和「格式失败」完全不同(见 docs/adr/0023)。
|
|
377
|
+
ungroundedShare: gateOkTurns === 0 ? 0 : (errorKinds.ungrounded ?? 0) / gateOkTurns,
|
|
308
378
|
insufficient: turns.size < MIN_TURNS || shown < MIN_SHOWN,
|
|
309
379
|
};
|
|
310
380
|
}
|
|
@@ -370,14 +440,43 @@ export function verdicts(metrics) {
|
|
|
370
440
|
});
|
|
371
441
|
}
|
|
372
442
|
|
|
373
|
-
|
|
443
|
+
// 格式失败率只算非空输出:空输出由下面的 empty-generations 单独判读。
|
|
444
|
+
if (metrics.schemaShare > SCHEMA_FAILURE_SHARE) {
|
|
445
|
+
const format = Number(metrics.errors?.format ?? 0);
|
|
374
446
|
out.push({
|
|
375
447
|
code: "schema-failures",
|
|
376
|
-
text: `有 ${Math.round(metrics.schemaShare * 100)}%
|
|
448
|
+
text: `有 ${Math.round(metrics.schemaShare * 100)}% 的生成是非空格式失败(${format}/${metrics.turns + metrics.errors.total})。`,
|
|
377
449
|
action: "解析还需要放宽,或修复调用没有生效——先看 error.detail 的前缀分布。",
|
|
378
450
|
});
|
|
379
451
|
}
|
|
380
452
|
|
|
453
|
+
// 空输出不是「格式需要放宽」:同一请求重发几乎仍是空(本机 81 次只救回 2 次)。
|
|
454
|
+
// 它通常是 provider / 输出长度(maxTokens)问题,提示词与解析都不该先动。
|
|
455
|
+
const emptyGenerations = Number(metrics.errors?.empty ?? 0);
|
|
456
|
+
const schemaFailures = Number(metrics.errors?.schema ?? 0);
|
|
457
|
+
const emptyShare = typeof metrics.emptyShare === "number" ? metrics.emptyShare : null;
|
|
458
|
+
if (
|
|
459
|
+
schemaFailures > 0 &&
|
|
460
|
+
emptyGenerations >= MIN_EMPTY_GENERATIONS &&
|
|
461
|
+
emptyShare !== null &&
|
|
462
|
+
emptyShare >= EMPTY_SHARE_THRESHOLD
|
|
463
|
+
) {
|
|
464
|
+
out.push({
|
|
465
|
+
code: "empty-generations",
|
|
466
|
+
text: `schema 失败里有 ${Math.round(emptyShare * 100)}% 是模型返回空文本(${emptyGenerations}/${schemaFailures})。`,
|
|
467
|
+
action:
|
|
468
|
+
"这不是「解析要放宽」:空输出通常是 provider / 输出长度(maxTokens)问题,同一请求重发大概率仍为空——先看模型与路由、调 maxTokens,不要动校验。",
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if ((metrics.ungroundedShare ?? 0) > 0.05) {
|
|
473
|
+
out.push({
|
|
474
|
+
code: "ungrounded-cards",
|
|
475
|
+
text: `有 ${Math.round(metrics.ungroundedShare * 100)}% 的生成因为「卡片与对话无关」被丢弃(${metrics.errors.byKind.ungrounded ?? 0}/${metrics.turns + metrics.errors.total})。`,
|
|
476
|
+
action: "这通常是模型在自述生成过程:先确认修复调用带了对话,再收紧提示词第 9–10 条。",
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
|
|
381
480
|
if ((metrics.gates.budget ?? 0) > 0) {
|
|
382
481
|
out.push({
|
|
383
482
|
code: "budget-hit",
|
|
@@ -415,7 +514,7 @@ function ciText(ci) {
|
|
|
415
514
|
export function renderMarkdown(metrics, list) {
|
|
416
515
|
const lines = [];
|
|
417
516
|
const iso = (/** @type {number} */ at) =>
|
|
418
|
-
at > 0 ? new Date(at).toISOString().replace("T", " ").slice(0, 16)
|
|
517
|
+
at > 0 ? `${new Date(at).toISOString().replace("T", " ").slice(0, 16)}Z` : "—";
|
|
419
518
|
lines.push("# next-step 复盘报告");
|
|
420
519
|
lines.push("");
|
|
421
520
|
lines.push(`- 生成时间:${iso(metrics.lastAt)}`);
|
|
@@ -468,6 +567,16 @@ export function renderMarkdown(metrics, list) {
|
|
|
468
567
|
.join(" · ") || "无"
|
|
469
568
|
})`,
|
|
470
569
|
);
|
|
570
|
+
const schemaTotal = Number(metrics.errors?.schema ?? 0);
|
|
571
|
+
if (schemaTotal > 0) {
|
|
572
|
+
const details =
|
|
573
|
+
Object.entries(metrics.errors?.byDetail ?? {})
|
|
574
|
+
.map(([key, value]) => `${key}=${value}`)
|
|
575
|
+
.join(" · ") || "无";
|
|
576
|
+
lines.push(
|
|
577
|
+
` - schema 拆分:空输出 ${Number(metrics.errors?.empty ?? 0)}(占 schema ${pct(metrics.emptyShare ?? null)})· 非空格式失败 ${Number(metrics.errors?.format ?? schemaTotal)}(detail:${details})`,
|
|
578
|
+
);
|
|
579
|
+
}
|
|
471
580
|
lines.push("");
|
|
472
581
|
lines.push("## 4. 内容与按钮");
|
|
473
582
|
lines.push("");
|
|
@@ -513,6 +622,9 @@ export function renderMarkdown(metrics, list) {
|
|
|
513
622
|
lines.push("");
|
|
514
623
|
lines.push("口径说明:忽略 = 出过的卡 − 点击 − 主动关闭(左连接,不写状态机);");
|
|
515
624
|
lines.push("曝光 = 卡片真正进入视口(IntersectionObserver 相交 ≥0.5 且持续 ≥300ms);");
|
|
625
|
+
lines.push(
|
|
626
|
+
"schema 失败拆成「模型返回空文本(json:empty)」与「非空格式失败」;格式失败率不含空输出。",
|
|
627
|
+
);
|
|
516
628
|
lines.push("比例区间为 Wilson 95%,两组比较只在区间不重叠时才算有差别。");
|
|
517
629
|
return `${lines.join("\n")}\n`;
|
|
518
630
|
}
|