dsh-project-flow 0.0.0-stage → 0.8.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/gate.mjs ADDED
@@ -0,0 +1,389 @@
1
+ /**
2
+ * 意图澄清门控(readiness gate)核心逻辑,纯函数、无 I/O。
3
+ *
4
+ * v2 改造要点——把「总分」和「扣分清单」并成一张表:
5
+ *
6
+ * 1. 模型只交一份 JSON:{ score, blindspots, assumptions }。
7
+ * score 是模型自报的主观大盘(0-100);
8
+ * blindspots 是五个固定维度的客观清单,每项要么给结论,要么留空。
9
+ *
10
+ * 2. 放行条件是「与」,不是「或」:
11
+ * 只有 score >= 95 且 五个维度全部有条目 时才算就绪。
12
+ * 分数高但清单有空格 → 拦;清单填满但分数不够 → 也拦。
13
+ *
14
+ * 3. 为什么分数不能单独说了算:分数是模型自己打的,改一个数字零成本;
15
+ * 盲区清单要写出「这个问题我问清了什么」,是编不出来的具体内容。
16
+ * 两者同时满足,才叫「确实问过」。
17
+ *
18
+ * 4. assumptions 不参与放行,只决定第 5 段验收要核哪几条。
19
+ */
20
+
21
+ /** 分数门槛,和插件里原有的 INTENT_CONFIDENCE_FLOOR 对齐。 */
22
+ export const SCORE_FLOOR = 95
23
+
24
+ /**
25
+ * 一条结论最少要几个字(去掉标点空白后算)。
26
+ *
27
+ * 这是为了拦住把「已确认」「OK」「无问题」写进去凑数的——代码判不了
28
+ * 文字有没有营养,但能判它够不够长。6 个字已经是很松的下限,
29
+ * 认真写一句结论随手就超过。
30
+ */
31
+ export const MIN_CONCLUSION_LENGTH = 6
32
+
33
+ /**
34
+ * 五个核心维度。顺序即输出顺序,UI 和提示词都按这个来。
35
+ * key 用英文,方便模型稳定输出;给人看的中文名放 label。
36
+ */
37
+ export const DIMENSIONS = [
38
+ { key: 'permissions', label: '权限', hint: '谁来做、要不要授权、账号和凭证归谁、有没有动不到的东西' },
39
+ { key: 'concurrency', label: '并发', hint: '同时几个人用、会不会撞车、要不要排队或加锁、量级多大' },
40
+ { key: 'edge_cases', label: '异常边界', hint: '出错怎么办、空数据、超时、重试、失败能不能回滚' },
41
+ { key: 'stack', label: '技术栈', hint: '用什么语言框架、跑在哪、能不能加依赖、跟现有代码怎么接' },
42
+ { key: 'data_flow', label: '数据流向', hint: '数据从哪来到哪去、存哪儿、谁能读、要不要脱敏或留存' },
43
+ ]
44
+
45
+ export const DIMENSION_KEYS = DIMENSIONS.map(d => d.key)
46
+
47
+ /**
48
+ * 就绪度评估的输入结构。
49
+ * blindspots 必填五个键,值是要么一条结论(字符串),要么空串/null(表示没问)。
50
+ */
51
+ export const READINESS_SCHEMA = {
52
+ type: 'object',
53
+ additionalProperties: false,
54
+ required: ['score', 'blindspots'],
55
+ properties: {
56
+ score: {
57
+ type: 'integer',
58
+ minimum: 0,
59
+ maximum: 100,
60
+ description: `你主观判断的完备度,0-100。低于 ${SCORE_FLOOR} 不放行。`,
61
+ },
62
+ blindspots: {
63
+ type: 'object',
64
+ additionalProperties: false,
65
+ required: DIMENSION_KEYS,
66
+ description: '五个硬性维度的结论。有结论就写一句具体的;没问清楚就留空串。留空 = 一票否决。',
67
+ properties: Object.fromEntries(
68
+ DIMENSIONS.map(d => [d.key, { type: 'string', description: `${d.label}:${d.hint}` }]),
69
+ ),
70
+ },
71
+ assumptions: {
72
+ type: 'array',
73
+ items: { type: 'string', minLength: 1 },
74
+ description: '没问、按默认值定了的地方。不参与放行,只决定验收时核什么。',
75
+ },
76
+ },
77
+ }
78
+
79
+ /** 从模型回复里取出 JSON:先找 ```json 围栏,再退化成整体解析。 */
80
+ export function extractReadiness(text) {
81
+ if (typeof text !== 'string' || text.trim() === '') return undefined
82
+ const fenced = [...text.matchAll(/```(?:json)?\s*\n([\s\S]*?)```/gu)]
83
+ const candidates = [...fenced.map(m => m[1]), text]
84
+ for (const raw of candidates.reverse()) {
85
+ const trimmed = raw.trim()
86
+ // 截取最外层花括号,容忍前后夹带解释文字
87
+ const start = trimmed.indexOf('{')
88
+ const end = trimmed.lastIndexOf('}')
89
+ if (start === -1 || end === -1 || end <= start) continue
90
+ try {
91
+ return JSON.parse(trimmed.slice(start, end + 1))
92
+ } catch {
93
+ continue
94
+ }
95
+ }
96
+ return undefined
97
+ }
98
+
99
+ /** 归一化一条结论:去空白、压空格、限长。空串返回 ''。 */
100
+ function normalizeLine(value, cap = 200) {
101
+ if (typeof value !== 'string') return ''
102
+ const line = value.trim().replace(/\s+/gu, ' ')
103
+ if (line === '') return ''
104
+ return line.length > cap ? `${line.slice(0, cap)}…` : line
105
+ }
106
+
107
+ /**
108
+ * 这条结论算不算「真的写了」。
109
+ * 空串不算;去掉空白和标点后不够 MIN_CONCLUSION_LENGTH 个字也不算——
110
+ * 「已确认。」这类凑数的话在这一步就被拦下,而不是等第 5 段才发现。
111
+ */
112
+ function isFilled(line) {
113
+ if (line === '') return false
114
+ return line.replace(/[\s\p{P}]/gu, '').length >= MIN_CONCLUSION_LENGTH
115
+ }
116
+
117
+ /** 归一化假设列表:去空白、去空串、去重、限条数。 */
118
+ function normalizeList(value, cap = 12) {
119
+ if (!Array.isArray(value)) return []
120
+ const seen = new Set()
121
+ const out = []
122
+ for (const item of value) {
123
+ const line = normalizeLine(item)
124
+ if (line === '' || seen.has(line)) continue
125
+ seen.add(line)
126
+ out.push(line)
127
+ if (out.length >= cap) break
128
+ }
129
+ return out
130
+ }
131
+
132
+ /** 判断一个对象是不是已经裁决过的 verdict。 */
133
+ function isVerdict(value) {
134
+ return value !== null
135
+ && typeof value === 'object'
136
+ && typeof value.ready === 'boolean'
137
+ && typeof value.uiState === 'string'
138
+ && typeof value.score === 'number'
139
+ && Array.isArray(value.emptyBlindspots)
140
+ && Array.isArray(value.vagueBlindspots)
141
+ && Array.isArray(value.assumptions)
142
+ }
143
+
144
+ /**
145
+ * 门控裁决:唯一的真相来源。UI 高亮、工具拦截都读这个结果。
146
+ *
147
+ * @returns {{
148
+ * ready:boolean, score:number|null, floor:number,
149
+ * blindspots:Record<string,string>, emptyBlindspots:string[],
150
+ * emptyLabels:string[], assumptions:string[], reason:string, uiState:string,
151
+ * unmet:string[]
152
+ * }}
153
+ */
154
+ export function evaluateReadiness(raw) {
155
+ // 幂等:裁决结果本身还能再喂回来(guardToolCall 会转发给 UI),
156
+ // 已归一化的 verdict 直接当答案用,不能重新按原始 schema 校验一遍。
157
+ if (isVerdict(raw)) return raw
158
+
159
+ const unmet = []
160
+
161
+ if (raw === undefined || raw === null) {
162
+ return {
163
+ ready: false,
164
+ score: null,
165
+ floor: SCORE_FLOOR,
166
+ blindspots: {},
167
+ emptyBlindspots: [...DIMENSION_KEYS],
168
+ vagueBlindspots: [],
169
+ emptyLabels: DIMENSIONS.map(d => d.label),
170
+ assumptions: [],
171
+ unmet: ['还没有交需求完备度自查表'],
172
+ reason: '模型没有交《需求完备度自查表》。先让它输出包含 score、blindspots、assumptions 的 JSON,再谈能不能进下一步。',
173
+ uiState: 'missing',
174
+ }
175
+ }
176
+
177
+ // ---- 结构校验:类型错一律当不通过,绝不默认放行 ----
178
+ const malformed = []
179
+ const rawScore = raw.score
180
+ let score = null
181
+ if (typeof rawScore === 'number' && Number.isFinite(rawScore)) {
182
+ score = Math.round(rawScore)
183
+ } else {
184
+ malformed.push('score 必须是 0-100 的数字')
185
+ }
186
+ if (raw.blindspots === null || typeof raw.blindspots !== 'object' || Array.isArray(raw.blindspots)) {
187
+ malformed.push('blindspots 必须是按五个维度分组的对象')
188
+ }
189
+
190
+ const assumptions = normalizeList(raw.assumptions)
191
+
192
+ if (malformed.length > 0) {
193
+ return {
194
+ ready: false,
195
+ score,
196
+ floor: SCORE_FLOOR,
197
+ blindspots: {},
198
+ emptyBlindspots: [...DIMENSION_KEYS],
199
+ vagueBlindspots: [],
200
+ emptyLabels: DIMENSIONS.map(d => d.label),
201
+ assumptions,
202
+ unmet: malformed,
203
+ reason: `自查表格式不对:${malformed.join(';')}。`,
204
+ uiState: 'malformed',
205
+ }
206
+ }
207
+
208
+ // ---- 逐维度归一化,记下空着的和写得太笼统的 ----
209
+ const blindspots = {}
210
+ const emptyBlindspots = []
211
+ const vagueBlindspots = []
212
+ const emptyLabels = []
213
+ for (const dim of DIMENSIONS) {
214
+ const line = normalizeLine(raw.blindspots[dim.key])
215
+ blindspots[dim.key] = line
216
+ if (line === '') {
217
+ emptyBlindspots.push(dim.key)
218
+ emptyLabels.push(dim.label)
219
+ } else if (!isFilled(line)) {
220
+ vagueBlindspots.push(dim.key)
221
+ emptyLabels.push(`${dim.label}(「${line}」太笼统)`)
222
+ }
223
+ }
224
+
225
+ // ---- 两个条件独立判,都满足才放行 ----
226
+ const scoreShort = score === null || score < SCORE_FLOOR
227
+ const gapsPresent = emptyBlindspots.length > 0 || vagueBlindspots.length > 0
228
+
229
+ if (scoreShort) unmet.push(`score 只有 ${score ?? '(缺失)'},没到 ${SCORE_FLOOR}`)
230
+ if (emptyBlindspots.length > 0) unmet.push(`这 ${emptyBlindspots.length} 个维度还没有结论:${emptyBlindspots.map(k => DIMENSIONS.find(d => d.key === k).label).join('、')}`)
231
+ if (vagueBlindspots.length > 0) unmet.push(`这 ${vagueBlindspots.length} 个维度写得太笼统,要说具体结论:${vagueBlindspots.map(k => DIMENSIONS.find(d => d.key === k).label).join('、')}`)
232
+
233
+ if (scoreShort || gapsPresent) {
234
+ return {
235
+ ready: false,
236
+ score,
237
+ floor: SCORE_FLOOR,
238
+ blindspots,
239
+ emptyBlindspots,
240
+ vagueBlindspots,
241
+ emptyLabels,
242
+ assumptions,
243
+ unmet,
244
+ reason: `门控未解锁。${unmet.join(';')}。`,
245
+ uiState: 'blocked',
246
+ }
247
+ }
248
+
249
+ return {
250
+ ready: true,
251
+ score,
252
+ floor: SCORE_FLOOR,
253
+ blindspots,
254
+ emptyBlindspots: [],
255
+ vagueBlindspots: [],
256
+ emptyLabels: [],
257
+ assumptions,
258
+ unmet: [],
259
+ reason: assumptions.length === 0
260
+ ? `分数 ${score} 达标,五个盲区全部有结论,门控已解锁。`
261
+ : `分数 ${score} 达标,五个盲区全部有结论,门控已解锁;另有 ${assumptions.length} 条假设妥协,验收时要逐条核。`,
262
+ uiState: 'ready',
263
+ }
264
+ }
265
+
266
+ /**
267
+ * 工具调用拦截器:注册成 DSH 的前置钩子后,模型任何「往下走」的动作都会先过这里。
268
+ *
269
+ * 关键取舍——白名单而不是黑名单:
270
+ * 拦截器只放任「问问题、查状态、看文件」这类不产生推进后果的工具,
271
+ * 其余一律拦。这样模型以后新增工具也不会漏网,不必跟着工具清单改代码。
272
+ */
273
+ export const PROGRESS_TOOLS = new Set([
274
+ 'project_run', // 一旦跑起来就是三段子会话,最贵的一步
275
+ 'project_resume', // 从磁盘状态接力,等于承认意图已定
276
+ 'project_rollback', // 回滚不只是查,会真改文件
277
+ ])
278
+
279
+ /** 意图还没澄清完,这些工具仍然放行:不推进、只读、只提问。 */
280
+ export const ALLOWED_WHILE_BLOCKED = new Set([
281
+ 'ask_user_question',
282
+ 'project_readiness',
283
+ 'project_status',
284
+ 'project_doctor',
285
+ 'project_snapshots',
286
+ 'project_models',
287
+ 'read',
288
+ 'glob',
289
+ 'grep',
290
+ 'web_search',
291
+ 'web_fetch',
292
+ 'todo_write',
293
+ ])
294
+
295
+ /**
296
+ * @param {{toolName:string, readiness:object|undefined}} input
297
+ * @returns {{allow:boolean, message?:string}}
298
+ */
299
+ export function guardToolCall({ toolName, readiness }) {
300
+ const verdict = evaluateReadiness(readiness)
301
+ if (verdict.ready) return { allow: true }
302
+
303
+ const nextSteps = verdict.emptyLabels.length > 0
304
+ ? [
305
+ `缺结论的维度:${verdict.emptyLabels.join('、')}。用 ask_user_question 一次问一个,问一个填一个。`,
306
+ '每个维度要写出具体结论,不是写「已确认」这种空话。',
307
+ ]
308
+ : [`把 score 提到 ${SCORE_FLOOR} 再交一次。`]
309
+
310
+ if (PROGRESS_TOOLS.has(toolName)) {
311
+ return {
312
+ allow: false,
313
+ message: [
314
+ `拦截:${toolName} 属于推进动作,但需求完备度自查表还没过门控。`,
315
+ verdict.reason,
316
+ '',
317
+ '改做这三件事之一:',
318
+ ...nextSteps.map((line, i) => `${i + 1}. ${line}`),
319
+ `${nextSteps.length + 1}. 确实问不出来的,写进 assumptions 作为假设妥协,写清验收时怎么核。`,
320
+ '',
321
+ '注意:把维度留空、只把分数报到 95 以上,是不算的。分数是自评,清单是证据,两样都要。',
322
+ ].join('\n'),
323
+ }
324
+ }
325
+
326
+ if (!ALLOWED_WHILE_BLOCKED.has(toolName)) {
327
+ return {
328
+ allow: false,
329
+ message: [
330
+ `拦截:${toolName} 在意图未澄清时不该调用。`,
331
+ verdict.reason,
332
+ '先把五个维度的结论补齐;只读工具和不推进的查询仍然放行。',
333
+ ].join('\n'),
334
+ }
335
+ }
336
+
337
+ return { allow: true }
338
+ }
339
+
340
+ /**
341
+ * 文本层正则兜底:有些人绕过工具、直接在回复里宣布「开始规划」。
342
+ * 读到这种宣告且门控未过,就把回复改写成追问提示。
343
+ */
344
+ const ADVANCE_CLAIMS = [
345
+ /(?:开始|进入|进入第?[二三四2-4]段|直接)(?:规划|实施|动手|开工)/u,
346
+ /(?:就按这个|按这个思路|那我)(?:直接)?(?:做|开工|开始)/u,
347
+ /^\s*(?:好的|收到|明白)[,,].*(?:我这就|马上)(?:开始|动手)/u,
348
+ ]
349
+
350
+ export function interceptAdvanceClaim({ text, readiness }) {
351
+ const verdict = evaluateReadiness(readiness)
352
+ if (verdict.ready) return { block: false }
353
+ const claimed = ADVANCE_CLAIMS.some(re => re.test(text ?? ''))
354
+ if (!claimed) return { block: false }
355
+ return {
356
+ block: true,
357
+ message: [
358
+ '门控拦截:你刚才宣布进入下一步,但自查表还没清零。',
359
+ verdict.reason,
360
+ '先问用户一个具体问题,再重新填表。',
361
+ ].join('\n'),
362
+ }
363
+ }
364
+
365
+ /** UI 按钮状态:只有 ready 才允许高亮并且可点。 */
366
+ export function buttonState(readiness) {
367
+ const verdict = evaluateReadiness(readiness)
368
+ const labels = {
369
+ ready: '规划阶段就绪',
370
+ blocked: '还有盲区没结论',
371
+ missing: '待评估',
372
+ malformed: '评估格式有误',
373
+ }
374
+ return {
375
+ visible: verdict.uiState !== 'missing',
376
+ highlighted: verdict.ready,
377
+ disabled: !verdict.ready,
378
+ label: labels[verdict.uiState],
379
+ detail: verdict.reason,
380
+ score: verdict.score,
381
+ floor: verdict.floor,
382
+ emptyDimensions: verdict.emptyLabels,
383
+ vagueBlindspots: verdict.vagueBlindspots,
384
+ blindspots: verdict.blindspots,
385
+ assumptions: verdict.assumptions,
386
+ }
387
+ }
388
+
389
+
package/icon.svg ADDED
@@ -0,0 +1,12 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" width="48" height="48" role="img" aria-label="Project flow">
2
+ <rect x="3" y="3" width="42" height="42" rx="10" fill="#1f2937"/>
3
+ <path d="M14 16 L24 16" stroke="#6b7280" stroke-width="2" stroke-linecap="round"/>
4
+ <path d="M24 16 L24 24" stroke="#6b7280" stroke-width="2" stroke-linecap="round"/>
5
+ <path d="M24 24 L34 24" stroke="#6b7280" stroke-width="2" stroke-linecap="round"/>
6
+ <path d="M34 24 L34 32" stroke="#6b7280" stroke-width="2" stroke-linecap="round"/>
7
+ <circle cx="14" cy="16" r="4" fill="#22c55e"/>
8
+ <circle cx="24" cy="16" r="3.2" fill="#94a3b8"/>
9
+ <circle cx="24" cy="24" r="3.2" fill="#94a3b8"/>
10
+ <circle cx="34" cy="24" r="3.2" fill="#94a3b8"/>
11
+ <circle cx="34" cy="32" r="4" fill="#f59e0b"/>
12
+ </svg>