dsh-plain-talk 0.1.14 → 0.1.16

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/dsh/check.js CHANGED
@@ -1,14 +1,19 @@
1
1
  // Reply checker for dsh-plain-talk.
2
2
  //
3
- // This is deliberately a checker, not a gate: it reports what a reader would
4
- // stumble on, with the exact replacement form. The model decides what to keep.
3
+ // Advisory, not a gate. It reports what a reader would stumble on and how to
4
+ // fix it; the model decides what to keep.
5
5
  //
6
- // Three principles learned from real sessions in this workspace:
7
- // - Common terms and project names are not errors. Only opaque codes the
8
- // reader has never seen (EXP-7, DEV-1K, run-3) need a first-use gloss.
9
- // - A pointer is not a citation: `file.py:42` alone sends the reader away.
10
- // - Cross-turn wording ("上述", "上一轮") is the main reason a report cannot
11
- // be read on its own.
6
+ // What it deliberately does NOT judge: reply length, list count, whether a
7
+ // next step exists, whether a term is professional, and whether "此前" is
8
+ // legitimate. Those are only wrong when the reader genuinely lacks the context,
9
+ // which this checker cannot know.
10
+ //
11
+ // What it does report:
12
+ // - cross-turn wording that sends the reader to a message they may not have;
13
+ // - opaque codes the reader has never seen (EXP-7, DEV-1K, run-3);
14
+ // - pointer-only citations (a file:line with no finding);
15
+ // - internal plumbing (session ids, tool names) — a warning, because a user
16
+ // who asked for those details should get them.
12
17
 
13
18
  const ALLOW = new Set([
14
19
  'api', 'json', 'jsonl', 'yaml', 'yml', 'html', 'css', 'http', 'https', 'url', 'uri',
@@ -40,12 +45,15 @@ const BACKREF_EN = [
40
45
  const PLUMBING = [
41
46
  /\bsession[-_][0-9a-z-]{6,}/i,
42
47
  /子代理|子会话|父代理|父会话|\bsubagent\b|\bparent agent\b|report sent to/i,
43
- /\b(?:plain_talk_check|run_code|ops_bash|ops_grep|todo_write)\b/,
48
+ /\b(?:plain_talk_check|plain_talk_vocab|run_code|ops_bash|ops_grep|todo_write)\b/,
44
49
  ]
45
50
 
46
51
  const PREAMBLE = /^(好的|好|当然|没问题|收到|明白|我来|让我|让我先|我们先|首先,我|先看一[下看])/
47
52
  const CLOSER = /(需要我|要不要|如果你愿意|希望(这|以上)|随时告诉我|还需要我|要我继续)/
48
53
 
54
+ // An explicit gloss right after the code: 是指 / 指的是 / 也就是 / —— / :
55
+ const GLOSS_MARKER = /^\s*(?:是指|指的是|意思是|也就是|即|——|--|:)\s*\S/
56
+
49
57
  const FILE_EXT = 'py|js|mjs|cjs|ts|tsx|json|jsonl|md|ya?ml|toml|sh|ps1|bat|csv|tsv|txt|ipynb|log|html|css|tex|bib|pdf|zip|sql|db|pt|pth|ckpt|npy|npz|parquet|docx|xlsx|pptx'
50
58
 
51
59
  const IDENT_PATTERNS = [
@@ -58,20 +66,28 @@ const IDENT_PATTERNS = [
58
66
  ]
59
67
 
60
68
  export const LIMITS = {
61
- boxChars: 300,
62
- boxLines: 12,
63
- listRun: 5,
64
- identifierWarns: 8,
69
+ // 篇幅与条数默认不设上限:长度不是质量指标(见 test/REPORT.md、ANALYSIS-0.1.11-to-0.1.14.md)。
70
+ // 需要阈值时,调用 analyze(draft, { limits: { boxChars: n, boxLines: n, listRun: n } })。
71
+ boxChars: Infinity,
72
+ boxLines: Infinity,
73
+ listRun: Infinity,
65
74
  }
66
75
 
67
76
  const cjkCount = (s) => (s.match(/[\u4e00-\u9fff]/g) ?? []).length
68
77
  const latinCount = (s) => (s.match(/[A-Za-z]/g) ?? []).length
69
78
  const len = (s) => [...s].length
70
79
 
71
- function stripCode(text) {
72
- return text
73
- .replace(/```[\s\S]*?```/g, ' ')
74
- .replace(/`[^`\n]*`/g, ' ')
80
+ /** Drop fenced code blocks: their contents are not prose and not identifiers. */
81
+ function stripFences(text) {
82
+ return String(text).replace(/```[\s\S]*?```/g, ' ')
83
+ }
84
+
85
+ /**
86
+ * Prose view: fenced blocks and inline code both removed. Inline code holds
87
+ * names, not sentences, so it must not trigger wording rules.
88
+ */
89
+ function proseOnly(text) {
90
+ return stripFences(text).replace(/`[^`\n]*`/g, ' ')
75
91
  }
76
92
 
77
93
  export function isCommonTerm(term) {
@@ -80,7 +96,12 @@ export function isCommonTerm(term) {
80
96
  return ALLOW.has(text.toLowerCase()) || MODEL_FAMILY.test(text)
81
97
  }
82
98
 
83
- export function scanIdentifiers(input) { const text = stripCode(String(input ?? ''))
99
+ /**
100
+ * Names in a draft. Inline code IS scanned: `EXP-7` and `rules.js` in backticks
101
+ * are exactly the names a reader may not know.
102
+ */
103
+ export function scanIdentifiers(input) {
104
+ const text = stripFences(String(input ?? ''))
84
105
  const found = []
85
106
  for (const [kind, re] of IDENT_PATTERNS) {
86
107
  re.lastIndex = 0
@@ -113,26 +134,45 @@ function isAllowed(token, known) {
113
134
  return false
114
135
  }
115
136
 
116
- function explainedAfter(text, end) {
117
- const after = text.slice(end, end + 60)
118
- const m = /^\s*[((]([^))]{4,})[))]/.exec(after)
119
- if (!m) return false
120
- return cjkCount(m[1]) >= 3 || latinCount(m[1]) >= 8
137
+ function looksLikeGloss(text) {
138
+ return cjkCount(text) >= 3 || latinCount(text) >= 8
121
139
  }
122
140
 
123
- function knownFrom(text, known) {
124
- return { text, known }
141
+ // A name can also be introduced by a preceding clause: "为推理流程增加实例
142
+ // 验证的实验 EXP-7". Require a 的-noun right before the name so that merely
143
+ // mentioning a code is still reported.
144
+ const PREFIX_GLOSS = /的[^\s,。;、]{1,10}\s*$/
145
+
146
+ /** A gloss can sit before the name as well as after it, and need not be in parentheses. */
147
+ function isCjkGloss(value) {
148
+ const body = String(value || "")
149
+ if (cjkCount(body) < 4) return false
150
+ return !/[<>{}\[\]$;]|=>|::|\(\)/.test(body)
151
+ }
152
+
153
+ function explainedNear(text, start, end) {
154
+ const before = text.slice(Math.max(0, start - 32), start)
155
+ const mBefore = /[((]([^))]{4,})[))]\s*$/.exec(before)
156
+ if (mBefore && looksLikeGloss(mBefore[1])) return true
157
+ if (cjkCount(before) >= 4 && PREFIX_GLOSS.test(before)) return true
158
+
159
+ const after = text.slice(end, end + 60).replace(/^[`"'”」』))\]]+/, "")
160
+ const mAfter = /^\s*[((]([^))]{4,})[))]/.exec(after)
161
+ if (mAfter && (looksLikeGloss(mAfter[1]) || isCjkGloss(mAfter[1]))) return true
162
+ if (GLOSS_MARKER.test(after)) return true
163
+ return false
125
164
  }
126
165
 
127
166
  /**
128
167
  * Analyze one reply draft.
129
168
  *
130
169
  * @param draft - the reply text the model is about to send.
131
- * @param options - `knownTokens` (project/user vocabulary) and `language`
170
+ * @param options - `knownTokens` (confirmed reader vocabulary) and `language`
132
171
  * (`zh` when the user writes Chinese).
133
172
  * @returns issues plus the measurements the README benchmark reports.
134
173
  */
135
174
  export function analyze(draft, options = {}) {
175
+ const limits = { ...LIMITS, ...(options.limits ?? {}) }
136
176
  const text = String(draft ?? '')
137
177
  const known = new Set((options.knownTokens ?? []).map((t) => String(t)))
138
178
  const knownLower = new Set([...known].map((t) => t.toLowerCase()))
@@ -141,48 +181,71 @@ export function analyze(draft, options = {}) {
141
181
 
142
182
  const add = (severity, rule, message, fix) => issues.push({ severity, rule, message, fix })
143
183
 
144
- const narrative = stripCode(text)
184
+ const narrative = proseOnly(text)
145
185
 
146
- // 1. Cross-turn wording.
186
+ // 1. Cross-turn wording. A warning: it is only wrong when the referent is not
187
+ // in this reply, which the checker cannot decide.
147
188
  const backrefs = [
148
189
  ...BACKREF_ZH.filter((w) => narrative.includes(w)),
149
190
  ...BACKREF_EN.filter((w) => narrative.toLowerCase().includes(w)),
150
191
  ]
151
192
  if (backrefs.length) {
152
- add('error', 'backref', `出现回指词:${backrefs.slice(0, 5).join('、')}。`, '把指代的东西重新写一遍,读者没有前文可翻。')
193
+ add(
194
+ 'warn',
195
+ 'backref',
196
+ `出现可能依赖前文说法:${backrefs.slice(0, 5).join('、')}。`,
197
+ '如果指的是本轮回复里已经说明过的内容,可以保留;否则把指代的东西写出来。',
198
+ )
153
199
  }
154
200
 
155
- // 2. Internal plumbing.
201
+ // 2. Internal plumbing. A warning: a user who asked for the session id,
202
+ // commit hash or tool list should get it verbatim.
156
203
  const plumbing = PLUMBING.find((re) => re.test(narrative))
157
204
  if (plumbing) {
158
- add('error', 'internal', '出现会话号、子代理或工具名等内部信息。', '删掉;只写用户能看见的对象。')
205
+ add(
206
+ 'warn',
207
+ 'internal',
208
+ '出现会话号、子代理或工具名等内部信息。',
209
+ '用户没要求就删掉;用户明确要看会话号、提交号或工具调用情况时,照给。',
210
+ )
159
211
  }
160
212
 
161
- // 3. Pointer-only citations.
213
+ // 3. Pointer-only citations: a location with no finding.
162
214
  const pointers = []
163
215
  const pointerRe = new RegExp(`(?:[A-Za-z]:[\\\\/]|/)?[\\w.-]+(?:[\\\\/][\\w.-]+)*\\.(?:${FILE_EXT}):\\d+`, 'gi')
164
- pointerRe.lastIndex = 0
165
- let pm
166
- while ((pm = pointerRe.exec(text))) pointers.push(pm[0])
216
+ let pointer
217
+ while ((pointer = pointerRe.exec(text))) pointers.push(pointer[0])
167
218
  if (pointers.length) {
168
- const unquoted = pointers.filter((pointer) => {
169
- const at = text.indexOf(pointer)
170
- const window = text.slice(Math.max(0, at - 220), at + pointer.length + 220)
171
- return !/「[^」]{4,}」|"[^"]{4,}"|『[^』]{4,}』|`[^`]{4,}`|```/.test(window)
219
+ const bare = pointers.filter((item) => {
220
+ const at = text.indexOf(item)
221
+ const window = text.slice(Math.max(0, at - 260), at + item.length + 260)
222
+ const lineStart = text.lastIndexOf('\n', at) + 1
223
+ const lineEndRaw = text.indexOf('\n', at + item.length)
224
+ const lineEnd = lineEndRaw < 0 ? text.length : lineEndRaw
225
+ const line = text.slice(lineStart, lineEnd)
226
+ const sentence = line
227
+ if (/(发现|原因|报错|错误|异常|结果|根因|日志|堆栈|表现为|是因为|error|exception|traceback|panic)/i.test(sentence)) return false
228
+ return !/「[^」]{4,}」|"[^"]{4,}"|『[^』]{4,}』|```/.test(window)
172
229
  })
173
- if (unquoted.length) {
174
- add('error', 'citation', `只有位置没有原文:${unquoted.slice(0, 3).join('、')}。`, '把那几行原文贴出来,再标位置。')
230
+ if (bare.length) {
231
+ add(
232
+ 'warn',
233
+ 'citation',
234
+ `只给了位置、没给发现:${bare.slice(0, 3).join('、')}。`,
235
+ '先说那里写了什么、说明什么都行;要贴原文时贴关键那几行。',
236
+ )
175
237
  }
176
238
  }
177
239
 
178
240
  // 4. Unexplained opaque identifiers.
179
241
  const seen = new Set()
180
242
  const unknown = []
181
- for (const token of scanIdentifiers(narrative)) {
243
+ const scannable = stripFences(text)
244
+ for (const token of scanIdentifiers(text)) {
182
245
  if (seen.has(token.token)) continue
183
246
  seen.add(token.token)
184
247
  if (isAllowed(token.token, known) || isAllowed(token.token, knownLower)) continue
185
- if (explainedAfter(narrative, token.end)) continue
248
+ if (explainedNear(scannable, token.start, token.end)) continue
186
249
  unknown.push(token.token)
187
250
  }
188
251
  if (unknown.length) {
@@ -190,49 +253,61 @@ export function analyze(draft, options = {}) {
190
253
  'warn',
191
254
  'identifier',
192
255
  `有 ${unknown.length} 个代号没有说明:${unknown.slice(0, 6).join('、')}${unknown.length > 6 ? ' 等' : ''}。`,
193
- '第一次出现写成「EXP-7(一句话说明它是什么)」;用户在本对话用过的词和项目名不用解释。',
256
+ '第一次出现时用一句话说明它是什么;用户已经知道的词不用解释。',
194
257
  )
195
258
  }
196
259
 
197
- // 5. Shape.
260
+ // 5. Optional shape limits. Off by default; a caller may set them. When a
261
+ // caller does, the result is a statistic (info), never an error: length is
262
+ // not a quality verdict.
198
263
  const lines = text.split('\n').filter((l) => l.trim())
199
264
  const cjk = cjkCount(narrative)
200
- if (cjk > LIMITS.boxChars || lines.length > LIMITS.boxLines) {
201
- add('warn', 'length', `回复有 ${cjk} 个汉字、${lines.length} 行,超过默认的 ${LIMITS.boxChars} 字 / ${LIMITS.boxLines} 行。`, '删掉读者不决策所需的内容;用户要细节时再展开。')
265
+ if (limits.boxChars !== Infinity || limits.boxLines !== Infinity) {
266
+ if (cjk > limits.boxChars || lines.length > limits.boxLines) {
267
+ add(
268
+ 'info',
269
+ 'length',
270
+ `回复有 ${cjk} 个汉字、${lines.length} 行,超过调用方设定的 ${limits.boxChars} 个汉字 / ${limits.boxLines} 行。`,
271
+ '这是调用方设的阈值,不是表达规则;只在确实该删的时候删。',
272
+ )
273
+ }
202
274
  }
203
275
 
204
276
  let run = 0
205
277
  let maxRun = 0
206
278
  for (const line of text.split('\n')) {
207
- if (/^\s*(?:[-*+]|\d+[.)])\s+/.test(line)) { run++; maxRun = Math.max(maxRun, run) } else if (!line.trim()) { run = 0 }
279
+ if (/^\s*(?:[-*+]|\d+[.)])\s+/.test(line)) {
280
+ run++
281
+ maxRun = Math.max(maxRun, run)
282
+ } else if (!line.trim()) {
283
+ run = 0
284
+ }
208
285
  }
209
- if (maxRun > LIMITS.listRun) {
210
- add('warn', 'list', `有一段列表 ${maxRun} 条,超过 5 条。`, '合并或只留最要紧的五条。')
286
+ if (maxRun > limits.listRun) {
287
+ add('info', 'list', `有一段列表 ${maxRun} 条,超过调用方设定的 ${limits.listRun} 条。`, '这是调用方设的阈值,不是表达规则。')
211
288
  }
212
289
 
213
290
  const first = lines[0] ?? ''
214
291
  if (PREAMBLE.test(first)) {
215
292
  add('warn', 'preamble', `开头是「${first.slice(0, 20)}」,属于寒暄或过程宣告。`, '第一行直接给结论。')
216
293
  }
217
- const tail = text.slice(-180)
218
- const closer = CLOSER.exec(tail)
294
+ const closer = CLOSER.exec(text.slice(-180))
219
295
  if (closer) {
220
- add('warn', 'closer', `结尾出现「${closer[0]}」,在征求额外任务。`, '删掉;需要下一步就只给一个具体动作。')
296
+ add('warn', 'closer', `结尾出现「${closer[0]}」,在征求额外任务。`, '需要下一步就只给一个具体动作;不需要就不用问。')
221
297
  }
222
298
 
223
- // 6. Language mismatch.
299
+ // 6. Language mismatch, judged against the user's latest message only.
224
300
  const total = cjk + latinCount(narrative)
225
301
  const cjkRatio = total === 0 ? 0 : cjk / total
226
302
  if (language === 'zh' && cjkRatio < 0.2 && latinCount(narrative) > 40) {
227
303
  add('warn', 'language', '用户用中文提问,但这条回复几乎全是英文。', '用中文写;命令、路径、报错原文保持原样。')
228
304
  }
305
+
306
+ // No "missing next step" rule: a finished answer may legitimately have none.
229
307
  const hasNext = /下一步|接下来要|接下来请|你可以先|建议你先|Next step/i.test(text)
230
- if (!hasNext && (cjk > 200 || lines.length > 10)) {
231
- add('warn', 'next', '回复较长,但没有给出一个明确的下一步。', '结尾补一个用户现在就能做的事;没有就写"没有待办"。')
232
- }
233
308
 
234
309
  return {
235
- ok: !issues.some((i) => i.severity === 'error'),
310
+ ok: !issues.some((issue) => issue.severity === 'error'),
236
311
  issues,
237
312
  stats: {
238
313
  chars: text.length,