dsh-project-flow 0.8.3 → 0.9.1
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/CHANGELOG.md +0 -0
- package/README.md +166 -358
- package/bench/corpus/qa-fail-3-issues.md +33 -0
- package/bench/corpus/qa-fail-assertion.md +20 -0
- package/bench/corpus/qa-fail-missing-detect.md +21 -0
- package/bench/corpus/qa-pass.md +9 -0
- package/bench/corpus/qa-reverify-partial.md +30 -0
- package/bench/corpus/qa-unverified-only.md +15 -0
- package/bench/corpus/requirement-no-check.md +8 -0
- package/bench/corpus/requirement-with-check.md +16 -0
- package/bench/corpus/reverify-table-not-defects.md +18 -0
- package/bench/run.mjs +142 -0
- package/client.js +195 -9
- package/cordis.patch.yml +29 -9
- package/docs/CHECKS.md +100 -0
- package/docs/DETAIL.md +6 -6
- package/docs/FEATURES.md +12 -5
- package/docs/ISSUES.md +75 -0
- package/docs/LAUNCH.md +3 -3
- package/docs/LEDGER.md +46 -0
- package/docs/MOMENTS.md +4 -4
- package/docs/USAGE.md +2 -2
- package/docs/adr/0001-four-stage-isolation.md +25 -0
- package/docs/adr/0002-file-handoff-not-context.md +27 -0
- package/docs/adr/0003-diff-over-self-report.md +27 -0
- package/docs/adr/0004-issue-level-rework.md +30 -0
- package/docs/adr/0005-assertion-over-prose.md +30 -0
- package/docs/anchors/assertion-exec.md +39 -0
- package/docs/anchors/cost-accounting.md +44 -0
- package/docs/anchors/diff-reconciliation.md +35 -0
- package/docs/anchors/file-whitelist.md +29 -0
- package/docs/anchors/intent-gate.md +41 -0
- package/docs/anchors/issue-loop.md +40 -0
- package/docs/anchors/patch-ledger.md +38 -0
- package/docs/anchors/watchdog.md +30 -0
- package/domains.mjs +1 -1
- package/evidence.mjs +19 -3
- package/flow.schema.mjs +600 -0
- package/gate.mjs +49 -23
- package/index.js +2800 -134
- package/locale/en.json +15 -4
- package/locale/zh.json +17 -6
- package/package.json +11 -2
- package/tests/_heartbeat-regression.test.mjs +124 -0
- package/tests/_wd_sandbox/.project-flow/demo/run.log +1 -0
- package/tests/_wd_sandbox/a.txt +1 -0
- package/tests/aesthetic.test.mjs +534 -0
- package/tests/assertions-e2e.mjs +234 -0
- package/tests/bench-check.mjs +32 -0
- package/tests/client-bundle.test.mjs +707 -0
- package/tests/contract.test.mjs +137 -0
- package/tests/desktop.test.mjs +130 -0
- package/tests/domains.test.mjs +220 -0
- package/tests/e2e.mjs +300 -0
- package/tests/evidence.test.mjs +408 -0
- package/tests/extra-phase.test.mjs +133 -0
- package/tests/feedback.test.mjs +187 -0
- package/tests/flow-create.test.mjs +168 -0
- package/tests/flow-gate.test.mjs +232 -0
- package/tests/flow-schema.test.mjs +213 -0
- package/tests/gate.test.mjs +246 -0
- package/tests/issue-loop.test.mjs +254 -0
- package/tests/issues-e2e.mjs +246 -0
- package/tests/panel-independence.test.mjs +78 -0
- package/tests/panel-report.test.mjs +129 -0
- package/tests/patch-ledger.test.mjs +280 -0
- package/tests/paths.mjs +94 -0
- package/tests/preview-path.test.mjs +69 -0
- package/tests/qa-assertions.test.mjs +352 -0
- package/tests/run-all.mjs +90 -0
- package/tests/stage-model.test.mjs +120 -0
- package/tests/stop-resume.test.mjs +260 -0
- package/tests/watch-root.test.mjs +167 -0
- package/tests/watchdog-tokens.test.mjs +49 -0
- package/tests/watchdog.test.mjs +301 -0
package/flow.schema.mjs
ADDED
|
@@ -0,0 +1,600 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* flow.yaml 的解析与校验。
|
|
3
|
+
*
|
|
4
|
+
* 为什么手写:红线 2 不许加依赖。这个文件只认本插件 schema 用得到的写法——
|
|
5
|
+
* 缩进 map、块级 seq、行内 [a, b] 和 { a: 1 }、# 注释、引号标量。超出子集的
|
|
6
|
+
* 写法一律报语法错并给行号,不猜、不兜底。猜错的 YAML 比报错危险得多:
|
|
7
|
+
* 它会让一个写错的阶段配置看起来像生效了。
|
|
8
|
+
*
|
|
9
|
+
* 校验分两档:
|
|
10
|
+
* errors 阻止保存,project_run 直接拒跑
|
|
11
|
+
* warnings 只打日志,照常跑
|
|
12
|
+
*
|
|
13
|
+
* 行号是必须的。不给行号的配置错误,用户拿着只能从头数。
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** 认得的顶层键。其余进 warnings,不拦。 */
|
|
17
|
+
const TOP_KEYS = ['process', 'limits', 'phases']
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 节点行号挂在解析结果的这个符号上。
|
|
21
|
+
*
|
|
22
|
+
* 用 Symbol 而不是 `__line` 这类普通键:Symbol 不会被 JSON.stringify 序列化,
|
|
23
|
+
* 也不参与 Object.keys,深比较时天然被忽略。校验通过后这个标记就完成了使命。
|
|
24
|
+
*/
|
|
25
|
+
const NODE_LINE = Symbol('flow.node.line')
|
|
26
|
+
|
|
27
|
+
/** 取一个节点在原文里的起始行;取不到返回 0(表示「没有位置信息」)。 */
|
|
28
|
+
function lineOf(node) {
|
|
29
|
+
const value = node !== null && typeof node === 'object' ? node[NODE_LINE] : undefined
|
|
30
|
+
return Number.isInteger(value) ? value : 0
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** 认得的 phase 键。 */
|
|
34
|
+
const PHASE_KEYS = ['id', 'reads', 'writes', 'gate', 'model', 'instructions']
|
|
35
|
+
|
|
36
|
+
/** 认得的 gate 键。 */
|
|
37
|
+
const GATE_KEYS = ['kind', 'on_fail', 'max_rework']
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 真正接进执行流程的 gate.kind。**只有这一个。**
|
|
41
|
+
*
|
|
42
|
+
* 以前这里列了四个(intent / artifact_present / assertions_exit_zero / verdict),
|
|
43
|
+
* 但执行器只实现了 artifact_present——其余三个写进 flow.yaml 会被解析、被校验
|
|
44
|
+
* 通过、然后被静默忽略。配置看起来生效了、实际没生效,比直接报错危险得多。
|
|
45
|
+
*
|
|
46
|
+
* 所以现在:写三个里的任何一个,validateFlow 直接给 errors 并拒绝运行,
|
|
47
|
+
* 报错里说清「这个判定在四阶的哪一段里做」,让人知道该去哪改。
|
|
48
|
+
*/
|
|
49
|
+
const SUPPORTED_GATE_KINDS = ['artifact_present']
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 曾经暴露过、但没接进执行流程的判定。留着这张表是为了给出「你应该去哪」,
|
|
53
|
+
* 而不是一句「不支持」。
|
|
54
|
+
*/
|
|
55
|
+
const UNSUPPORTED_GATE_HINTS = {
|
|
56
|
+
intent: '第 1 阶的意图门控由 project_run 的参数(intentConfidence / blindspots)负责,不在 flow.yaml 里配。',
|
|
57
|
+
assertions_exit_zero: '判据执行发生在第 4 阶质检,用 Requirement.md 前置块里的 check 命令,不在 flow.yaml 里配。',
|
|
58
|
+
verdict: '质检判 FAIL 后按问题清单返工,由 flow.yaml 最后一个 phase 的 on_fail 表达,不用 gate.kind=verdict。',
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** 内置四阶的 id。不在这四个里的环节要靠 instructions 才知道该做什么。 */
|
|
62
|
+
const BUILTIN_PHASE_IDS = ['requirement', 'plan', 'build', 'qa']
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 去掉行尾注释。
|
|
66
|
+
*
|
|
67
|
+
* 引号里的 # 不是注释——`cmd: "echo #1"` 这种写法真的会出现。所以逐字符扫,
|
|
68
|
+
* 碰到引号就进去,出来之前不认 #。
|
|
69
|
+
*/
|
|
70
|
+
function stripComment(line) {
|
|
71
|
+
let quote = ''
|
|
72
|
+
for (let i = 0; i < line.length; i += 1) {
|
|
73
|
+
const ch = line[i]
|
|
74
|
+
if (quote !== '') {
|
|
75
|
+
if (ch === '\\' && quote === '"') { i += 1; continue }
|
|
76
|
+
if (ch === quote) quote = ''
|
|
77
|
+
continue
|
|
78
|
+
}
|
|
79
|
+
if (ch === '"' || ch === "'") { quote = ch; continue }
|
|
80
|
+
if (ch === '#') return line.slice(0, i)
|
|
81
|
+
}
|
|
82
|
+
return line
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** 标量还原:引号走 JSON 转义,纯数字转 number,true/false 转 boolean。 */
|
|
86
|
+
function scalar(text) {
|
|
87
|
+
const t = text.trim()
|
|
88
|
+
if (t.length >= 2 && t.startsWith('"') && t.endsWith('"')) {
|
|
89
|
+
try { return String(JSON.parse(t)) } catch { return t.slice(1, -1) }
|
|
90
|
+
}
|
|
91
|
+
if (t.length >= 2 && t.startsWith("'") && t.endsWith("'")) return t.slice(1, -1)
|
|
92
|
+
if (t === 'true') return true
|
|
93
|
+
if (t === 'false') return false
|
|
94
|
+
if (t === 'null' || t === '~') return null
|
|
95
|
+
if (/^-?\d+$/u.test(t)) return Number.parseInt(t, 10)
|
|
96
|
+
if (/^-?\d+\.\d+$/u.test(t)) return Number.parseFloat(t)
|
|
97
|
+
return t
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** 行内 [a, b] / { a: 1 }。返回 null 表示「这不是行内写法」。 */
|
|
101
|
+
function inlineValue(text) {
|
|
102
|
+
const t = text.trim()
|
|
103
|
+
if (t.startsWith('[') && t.endsWith(']')) {
|
|
104
|
+
const inner = t.slice(1, -1).trim()
|
|
105
|
+
if (inner === '') return []
|
|
106
|
+
return splitTopLevel(inner, ',').map(part => scalar(part))
|
|
107
|
+
}
|
|
108
|
+
if (t.startsWith('{') && t.endsWith('}')) {
|
|
109
|
+
const inner = t.slice(1, -1).trim()
|
|
110
|
+
const out = {}
|
|
111
|
+
if (inner === '') return out
|
|
112
|
+
for (const part of splitTopLevel(inner, ',')) {
|
|
113
|
+
const at = part.indexOf(':')
|
|
114
|
+
if (at === -1) return null
|
|
115
|
+
out[part.slice(0, at).trim()] = scalar(part.slice(at + 1))
|
|
116
|
+
}
|
|
117
|
+
return out
|
|
118
|
+
}
|
|
119
|
+
return null
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** 按分隔符切,但不切引号和括号里面的。 */
|
|
123
|
+
function splitTopLevel(text, sep) {
|
|
124
|
+
const parts = []
|
|
125
|
+
let depth = 0
|
|
126
|
+
let quote = ''
|
|
127
|
+
let start = 0
|
|
128
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
129
|
+
const ch = text[i]
|
|
130
|
+
if (quote !== '') {
|
|
131
|
+
if (ch === '\\' && quote === '"') { i += 1; continue }
|
|
132
|
+
if (ch === quote) quote = ''
|
|
133
|
+
continue
|
|
134
|
+
}
|
|
135
|
+
if (ch === '"' || ch === "'") { quote = ch; continue }
|
|
136
|
+
if (ch === '[' || ch === '{') { depth += 1; continue }
|
|
137
|
+
if (ch === ']' || ch === '}') { depth -= 1; continue }
|
|
138
|
+
if (ch === sep && depth === 0) {
|
|
139
|
+
parts.push(text.slice(start, i))
|
|
140
|
+
start = i + 1
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
parts.push(text.slice(start))
|
|
144
|
+
return parts
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* 解析 flow.yaml。
|
|
149
|
+
*
|
|
150
|
+
* 返回 { value, errors }。errors 每条 { line, message },value 可能残缺——
|
|
151
|
+
* 语法错了就别拿它跑,但要把能读出来的部分带回去,方便报错时说清哪一段坏了。
|
|
152
|
+
*/
|
|
153
|
+
/**
|
|
154
|
+
* 把块标量(`key: |` / `key: >`)的内容拼成一个字符串。
|
|
155
|
+
*
|
|
156
|
+
* 块标量的内容行必须按原样保留——里面的 `#` 不是注释,空行不是「没内容」。
|
|
157
|
+
* 所以这一步在读原始行时做,不走 stripComment,也不走「跳过空行」。
|
|
158
|
+
*
|
|
159
|
+
* @param lines 原始行数组
|
|
160
|
+
* @param start 标题行的下一行下标
|
|
161
|
+
* @param headerIndent 标题行的缩进
|
|
162
|
+
* @returns {{ text:string, next:number }}
|
|
163
|
+
*/
|
|
164
|
+
function readBlockScalar(lines, start, headerIndent, style, chomp) {
|
|
165
|
+
const body = []
|
|
166
|
+
let cursor = start
|
|
167
|
+
let contentIndent = -1
|
|
168
|
+
while (cursor < lines.length) {
|
|
169
|
+
const line = lines[cursor]
|
|
170
|
+
if (line.trim() === '') {
|
|
171
|
+
// 空行属于内容,但它同时也会终止「还没确定缩进」的阶段
|
|
172
|
+
body.push('')
|
|
173
|
+
cursor += 1
|
|
174
|
+
continue
|
|
175
|
+
}
|
|
176
|
+
const indent = line.length - line.trimStart().length
|
|
177
|
+
if (indent <= headerIndent) break
|
|
178
|
+
if (contentIndent === -1) contentIndent = indent
|
|
179
|
+
// 缩进比基准少的行(但仍在标题之下)保留相对缩进,别把用户的排版吃掉
|
|
180
|
+
body.push(line.length >= contentIndent ? line.slice(contentIndent) : line.trimStart())
|
|
181
|
+
cursor += 1
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// 尾部空行按 chomp 规则处理:- 全删,默认留一个换行,+ 全留
|
|
185
|
+
let trailing = 0
|
|
186
|
+
while (body.length - trailing > 0 && body[body.length - 1 - trailing].trim() === '') trailing += 1
|
|
187
|
+
const core = body.slice(0, body.length - trailing)
|
|
188
|
+
|
|
189
|
+
let text
|
|
190
|
+
if (style === '|') {
|
|
191
|
+
text = core.join('\n')
|
|
192
|
+
} else {
|
|
193
|
+
// `>` 折叠:相邻非空行用空格连起来,空行变成换行
|
|
194
|
+
const out = []
|
|
195
|
+
for (const line of core) {
|
|
196
|
+
if (line.trim() === '') out.push('\n')
|
|
197
|
+
else if (out.length === 0) out.push(line)
|
|
198
|
+
else if (out[out.length - 1] === '\n') out.push(line)
|
|
199
|
+
else out[out.length - 1] += ` ${line}`
|
|
200
|
+
}
|
|
201
|
+
text = out.join('')
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (chomp === '-') {
|
|
205
|
+
text = text.replace(/\n+$/u, '')
|
|
206
|
+
} else if (chomp === '+') {
|
|
207
|
+
text = `${text}${'\n'.repeat(trailing)}`
|
|
208
|
+
} else if (text !== '') {
|
|
209
|
+
text = `${text}\n`
|
|
210
|
+
}
|
|
211
|
+
return { text, next: cursor }
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export function parseFlowYaml(text) {
|
|
215
|
+
const errors = []
|
|
216
|
+
const root = {}
|
|
217
|
+
if (typeof text !== 'string' || text.trim() === '') {
|
|
218
|
+
return { value: root, errors: [{ line: 0, message: '文件是空的' }] }
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// 预处理:留下有内容的行,记住原行号。
|
|
222
|
+
//
|
|
223
|
+
// 块标量在这一步就吃掉:它的内容行里 `#` 不是注释、空行也算内容,
|
|
224
|
+
// 交给后面那套「剥注释 + 跳过空行」的流程会被啃得只剩半句。
|
|
225
|
+
const rows = []
|
|
226
|
+
const rawLines = text.split(/\r?\n/u)
|
|
227
|
+
let i = 0
|
|
228
|
+
while (i < rawLines.length) {
|
|
229
|
+
const stripped = stripComment(rawLines[i])
|
|
230
|
+
if (stripped.trim() === '') { i += 1; continue }
|
|
231
|
+
const indent = stripped.length - stripped.trimStart().length
|
|
232
|
+
const trimmed = stripped.trim()
|
|
233
|
+
|
|
234
|
+
// 只认「一行一个 key: |」的写法;`- id: |` 这种挂在下标量上的不处理,
|
|
235
|
+
// 它极少出现,而且要处理得连下标量的缩进一起算,容易把别的写法读飞。
|
|
236
|
+
const header = /^([^\-|>][^:]*?):\s*([|>])([+-]?)\s*$/u.exec(trimmed)
|
|
237
|
+
if (header !== null) {
|
|
238
|
+
const style = header[2]
|
|
239
|
+
const chomp = header[3]
|
|
240
|
+
const block = readBlockScalar(rawLines, i + 1, indent, style, chomp)
|
|
241
|
+
rows.push({ indent, text: `${header[1]}:`, blockValue: block.text, line: i + 1 })
|
|
242
|
+
i = block.next
|
|
243
|
+
continue
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
rows.push({ indent, text: trimmed, line: i + 1 })
|
|
247
|
+
i += 1
|
|
248
|
+
}
|
|
249
|
+
if (rows.length === 0) return { value: root, errors: [{ line: 0, message: '文件里只有注释' }] }
|
|
250
|
+
|
|
251
|
+
// 块标量的值按原行号存一份:consumeMapEntry 拿得到行号,用不着靠遍历去找,
|
|
252
|
+
// 也不怕两行恰好同号(`- id: |` 这种目前不处理,但索引方式不该依赖它)。
|
|
253
|
+
const blockValues = new Map()
|
|
254
|
+
for (const row of rows) {
|
|
255
|
+
if (row.blockValue !== undefined) blockValues.set(row.line, row.blockValue)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
let cursor = 0
|
|
259
|
+
|
|
260
|
+
/** 解析一个缩进块,返回 [值, 是否 seq]。 */
|
|
261
|
+
function parseBlock(indent) {
|
|
262
|
+
if (cursor >= rows.length) return [{}, false]
|
|
263
|
+
const isSeq = rows[cursor].text.startsWith('- ') || rows[cursor].text === '-'
|
|
264
|
+
if (isSeq) {
|
|
265
|
+
const arr = []
|
|
266
|
+
while (cursor < rows.length && rows[cursor].indent === indent && (rows[cursor].text.startsWith('- ') || rows[cursor].text === '-')) {
|
|
267
|
+
const row = rows[cursor]
|
|
268
|
+
const restRaw = row.text === '-' ? '' : row.text.slice(2)
|
|
269
|
+
const rest = restRaw.trim()
|
|
270
|
+
if (rest === '') {
|
|
271
|
+
// 只有一根横杠,内容全在下面的子块里。
|
|
272
|
+
cursor += 1
|
|
273
|
+
if (cursor < rows.length && rows[cursor].indent > indent) {
|
|
274
|
+
const [child] = parseBlock(rows[cursor].indent)
|
|
275
|
+
arr.push(child)
|
|
276
|
+
} else {
|
|
277
|
+
arr.push({})
|
|
278
|
+
}
|
|
279
|
+
continue
|
|
280
|
+
}
|
|
281
|
+
// `- 一个纯标量`(不是 `- key: value`):这一项就是那个值本身。
|
|
282
|
+
// 以前不看这一条,一律当 map 项处理,于是 `reads:\n - a.md` 会变成
|
|
283
|
+
// [{}, {}] 加一句「看不懂」——README 里最常见的一种写法直接读不了。
|
|
284
|
+
if (findColon(rest) === -1) {
|
|
285
|
+
const inline = inlineValue(rest)
|
|
286
|
+
arr.push(inline === null ? scalar(rest) : inline)
|
|
287
|
+
cursor += 1
|
|
288
|
+
continue
|
|
289
|
+
}
|
|
290
|
+
// `- id: xxx`:这一个 seq 项本身就是个 map,横杠后面那截是它的第一行。
|
|
291
|
+
// 后续的兄弟字段缩进必须正好对齐这第一行内容的起始列,多一格少一格都不算。
|
|
292
|
+
// 早先按 `indent + 2` 硬算,遇到 `- id:` 这种多个空格的写法就对不上,
|
|
293
|
+
// 而更常见的是忘了吃掉后续兄弟字段,游标卡住,后面整份配置全读飞。
|
|
294
|
+
const pad = restRaw.length - restRaw.trimStart().length
|
|
295
|
+
const itemIndent = indent + 2 + pad
|
|
296
|
+
const item = {}
|
|
297
|
+
// 把起始行号挂在这个节点上(不可枚举的 symbol,不进 JSON、不影响深比较),
|
|
298
|
+
// 校验阶段报错时就能指到具体第几行。以前一律 line: 0,
|
|
299
|
+
// 文档承诺的「报错带行号」其实只对语法错成立,语义错给不出位置。
|
|
300
|
+
Object.defineProperty(item, NODE_LINE, { value: row.line, enumerable: false })
|
|
301
|
+
cursor += 1
|
|
302
|
+
consumeMapEntry(item, rest, row.line, itemIndent)
|
|
303
|
+
while (cursor < rows.length && rows[cursor].indent === itemIndent) {
|
|
304
|
+
const sub = rows[cursor]
|
|
305
|
+
cursor += 1
|
|
306
|
+
consumeMapEntry(item, sub.text, sub.line, itemIndent)
|
|
307
|
+
}
|
|
308
|
+
arr.push(item)
|
|
309
|
+
}
|
|
310
|
+
return [arr, true]
|
|
311
|
+
}
|
|
312
|
+
const obj = {}
|
|
313
|
+
Object.defineProperty(obj, NODE_LINE, { value: rows[cursor]?.line ?? 0, enumerable: false })
|
|
314
|
+
while (cursor < rows.length && rows[cursor].indent === indent) {
|
|
315
|
+
const row = rows[cursor]
|
|
316
|
+
cursor += 1
|
|
317
|
+
consumeMapEntry(obj, row.text, row.line, indent)
|
|
318
|
+
}
|
|
319
|
+
return [obj, false]
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/** 吃一条 `key: value`,value 空的话继续往下吃子块。 */
|
|
323
|
+
function consumeMapEntry(target, text, line, indent) {
|
|
324
|
+
// 块标量:值已经在预处理阶段拼好了,直接放进去。
|
|
325
|
+
const blockValue = blockValues.get(line)
|
|
326
|
+
const at = findColon(text)
|
|
327
|
+
if (at === -1) {
|
|
328
|
+
errors.push({ line, message: `这一行看不懂,应该是 \`键: 值\` 的形式:${text}` })
|
|
329
|
+
return
|
|
330
|
+
}
|
|
331
|
+
const key = text.slice(0, at).trim()
|
|
332
|
+
const rest = text.slice(at + 1).trim()
|
|
333
|
+
if (key === '') {
|
|
334
|
+
errors.push({ line, message: '冒号前面没有键名' })
|
|
335
|
+
return
|
|
336
|
+
}
|
|
337
|
+
if (blockValue !== undefined) {
|
|
338
|
+
target[key] = blockValue
|
|
339
|
+
return
|
|
340
|
+
}
|
|
341
|
+
if (rest === '') {
|
|
342
|
+
// 往下找子块。缩进必须更深,否则就是空值。
|
|
343
|
+
if (cursor < rows.length && rows[cursor].indent > indent) {
|
|
344
|
+
const [child] = parseBlock(rows[cursor].indent)
|
|
345
|
+
target[key] = child
|
|
346
|
+
} else {
|
|
347
|
+
target[key] = {}
|
|
348
|
+
}
|
|
349
|
+
return
|
|
350
|
+
}
|
|
351
|
+
const inline = inlineValue(rest)
|
|
352
|
+
target[key] = inline === null ? scalar(rest) : inline
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function findColon(text) {
|
|
356
|
+
let quote = ''
|
|
357
|
+
let depth = 0
|
|
358
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
359
|
+
const ch = text[i]
|
|
360
|
+
if (quote !== '') {
|
|
361
|
+
if (ch === '\\' && quote === '"') { i += 1; continue }
|
|
362
|
+
if (ch === quote) quote = ''
|
|
363
|
+
continue
|
|
364
|
+
}
|
|
365
|
+
if (ch === '"' || ch === "'") { quote = ch; continue }
|
|
366
|
+
if (ch === '[' || ch === '{') { depth += 1; continue }
|
|
367
|
+
if (ch === ']' || ch === '}') { depth -= 1; continue }
|
|
368
|
+
if (ch === ':' && depth === 0) return i
|
|
369
|
+
}
|
|
370
|
+
return -1
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const [first] = parseBlock(rows[0].indent)
|
|
374
|
+
Object.assign(root, first)
|
|
375
|
+
if (cursor < rows.length) {
|
|
376
|
+
errors.push({ line: rows[cursor].line, message: '这一行的缩进对不上,没法归到任何一层' })
|
|
377
|
+
}
|
|
378
|
+
return { value: root, errors }
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* 校验解析出来的结构。
|
|
383
|
+
*
|
|
384
|
+
* 返回 { ok, errors, warnings, phases }。ok=false 时 project_run 拒跑。
|
|
385
|
+
* phases 是按 flow.yaml 顺序排好的数组,后续执行只认这个。
|
|
386
|
+
*/
|
|
387
|
+
export function validateFlow(doc) {
|
|
388
|
+
const errors = []
|
|
389
|
+
const warnings = []
|
|
390
|
+
const phases = []
|
|
391
|
+
|
|
392
|
+
const err = (line, message) => errors.push({ line, message })
|
|
393
|
+
const warn = (line, message) => warnings.push({ line, message })
|
|
394
|
+
|
|
395
|
+
if (doc === null || typeof doc !== 'object' || Array.isArray(doc)) {
|
|
396
|
+
err(0, '顶层必须是一个 map')
|
|
397
|
+
return { ok: false, errors, warnings, phases }
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
for (const key of Object.keys(doc)) {
|
|
401
|
+
if (!TOP_KEYS.includes(key)) warn(0, `顶层有不认识的键 \`${key}\`,会被忽略`)
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const process = doc.process
|
|
405
|
+
if (process === undefined || process === null) {
|
|
406
|
+
err(0, '缺 `process`')
|
|
407
|
+
} else if (typeof process !== 'object') {
|
|
408
|
+
err(0, '`process` 必须是 map')
|
|
409
|
+
} else {
|
|
410
|
+
if (typeof process.name !== 'string' || process.name.trim() === '') {
|
|
411
|
+
err(0, '缺 `process.name`:没有名字的工作流没法在日志和状态里认出来')
|
|
412
|
+
}
|
|
413
|
+
if (process.version === undefined) warn(0, '`process.version` 没写,按 1 处理')
|
|
414
|
+
else if (!Number.isInteger(process.version)) warn(0, '`process.version` 应该是整数')
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const limits = doc.limits
|
|
418
|
+
if (limits !== undefined && limits !== null) {
|
|
419
|
+
if (typeof limits !== 'object' || Array.isArray(limits)) {
|
|
420
|
+
err(0, '`limits` 必须是 map')
|
|
421
|
+
} else {
|
|
422
|
+
if (limits.max_rework !== undefined && !Number.isInteger(limits.max_rework)) {
|
|
423
|
+
warn(0, '`limits.max_rework` 应该是整数,这一项会被忽略')
|
|
424
|
+
}
|
|
425
|
+
if (limits.max_total_tokens !== undefined && !Number.isInteger(limits.max_total_tokens)) {
|
|
426
|
+
warn(0, '`limits.max_total_tokens` 应该是整数,这一项会被忽略')
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
const rawPhases = doc.phases
|
|
432
|
+
if (!Array.isArray(rawPhases) || rawPhases.length === 0) {
|
|
433
|
+
err(0, '`phases` 是空的:至少要有需求、规划、实施、质检四个环节')
|
|
434
|
+
return { ok: false, errors, warnings, phases }
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
const seen = new Set()
|
|
438
|
+
rawPhases.forEach((raw, index) => {
|
|
439
|
+
const at = lineOf(raw)
|
|
440
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
441
|
+
err(at, `第 ${index + 1} 个环节不是 map`)
|
|
442
|
+
return
|
|
443
|
+
}
|
|
444
|
+
const id = typeof raw.id === 'string' ? raw.id.trim() : ''
|
|
445
|
+
if (id === '') {
|
|
446
|
+
err(at, `第 ${index + 1} 个环节没有 \`id\`:环节之间靠 id 互相指,缺了就连不起来`)
|
|
447
|
+
return
|
|
448
|
+
}
|
|
449
|
+
if (seen.has(id)) {
|
|
450
|
+
err(at, `环节 id \`${id}\` 重复了`)
|
|
451
|
+
return
|
|
452
|
+
}
|
|
453
|
+
seen.add(id)
|
|
454
|
+
|
|
455
|
+
// 不认识的键直接报错。以前只给 warning,而 warnings 不拦运行——
|
|
456
|
+
// 写错一个键名,配置看着生效了,实际整段被忽略。
|
|
457
|
+
for (const key of Object.keys(raw)) {
|
|
458
|
+
if (!PHASE_KEYS.includes(key)) {
|
|
459
|
+
err(at, `环节 \`${id}\` 有不认识的键 \`${key}\`(只认 ${PHASE_KEYS.join(' / ')})`)
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// 内置四阶各有专用实现,不写 instructions 也知道干什么。
|
|
464
|
+
// 别的环节没有专用实现,只能靠 instructions——不写就别指望它跑出东西来。
|
|
465
|
+
const instructions = typeof raw.instructions === 'string' ? raw.instructions.trim() : ''
|
|
466
|
+
if (!BUILTIN_PHASE_IDS.includes(id) && instructions === '') {
|
|
467
|
+
warn(at, `环节 \`${id}\` 不是内置四阶之一,又没写 \`instructions\`:跑起来会因为不知道该做什么而被跳过`)
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
const reads = normalizeStringList(raw.reads)
|
|
471
|
+
const writes = normalizeStringList(raw.writes)
|
|
472
|
+
if (reads === null) err(at, `环节 \`${id}\` 的 \`reads\` 必须是列表`)
|
|
473
|
+
if (writes === null) err(at, `环节 \`${id}\` 的 \`writes\` 必须是列表`)
|
|
474
|
+
|
|
475
|
+
const gate = raw.gate
|
|
476
|
+
if (gate === undefined || gate === null) {
|
|
477
|
+
warn(at, `环节 \`${id}\` 没写 \`gate\`,这一阶不设门禁`)
|
|
478
|
+
} else if (typeof gate !== 'object' || Array.isArray(gate)) {
|
|
479
|
+
err(at, `环节 \`${id}\` 的 \`gate\` 必须是 map`)
|
|
480
|
+
} else {
|
|
481
|
+
for (const key of Object.keys(gate)) {
|
|
482
|
+
if (!GATE_KEYS.includes(key)) {
|
|
483
|
+
// gate 里的键要么执行器认,要么直接报错。以前只是 warn,
|
|
484
|
+
// 而 warnings 不拦运行——写错了照样往下跑,用户以为配上了。
|
|
485
|
+
err(at, `环节 \`${id}\` 的 gate 里有执行器不支持的键 \`${key}\`(只认 ${GATE_KEYS.join(' / ')})`)
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
if (gate.kind !== undefined && !SUPPORTED_GATE_KINDS.includes(gate.kind)) {
|
|
489
|
+
const hint = UNSUPPORTED_GATE_HINTS[gate.kind]
|
|
490
|
+
err(at, hint === undefined
|
|
491
|
+
? `环节 \`${id}\` 的 \`gate.kind\` 写的是 \`${gate.kind}\`,执行器只实现 ${SUPPORTED_GATE_KINDS.join(' / ')}`
|
|
492
|
+
: `环节 \`${id}\` 的 \`gate.kind: ${gate.kind}\` 没有接进执行流程。${hint}`)
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
phases.push({
|
|
497
|
+
id,
|
|
498
|
+
index,
|
|
499
|
+
reads: reads ?? [],
|
|
500
|
+
writes: writes ?? [],
|
|
501
|
+
gate: gate === undefined || gate === null ? null : gate,
|
|
502
|
+
model: raw.model === undefined ? null : raw.model,
|
|
503
|
+
instructions: typeof raw.instructions === 'string' ? raw.instructions : '',
|
|
504
|
+
})
|
|
505
|
+
})
|
|
506
|
+
|
|
507
|
+
// on_success / on_gate_fail 从 schema 里撤掉了:执行器没有跳转能力。
|
|
508
|
+
//
|
|
509
|
+
// 它们以前被解析、被校验(连「只能指向前面的环节」这种讲究都写了),
|
|
510
|
+
// 但执行器从头到尾没读过这两个字段——写进去等于写了个注释。
|
|
511
|
+
// 「gate.on_fail」是唯一真在用的返工入口(阶段级),保留。
|
|
512
|
+
//
|
|
513
|
+
// 报错已经在上面的 PHASE_KEYS 检查里给出了(带行号),这里不再重复一遍。
|
|
514
|
+
|
|
515
|
+
// gate.on_fail 只能指向前面出现过的环节。
|
|
516
|
+
//
|
|
517
|
+
// 指到后面会成环:A 失败跳 B、B 失败跳 A,跑起来就是无限循环,
|
|
518
|
+
// 而 max_rework 管的是「同一个环节返工几次」,管不了跨环节来回跳。
|
|
519
|
+
const indexById = new Map(phases.map(p => [p.id, p.index]))
|
|
520
|
+
const checkTarget = (phase, target, field) => {
|
|
521
|
+
if (target === '') return
|
|
522
|
+
const at = indexById.get(target)
|
|
523
|
+
if (at === undefined) {
|
|
524
|
+
err(0, `环节 \`${phase.id}\` 的 \`${field}\` 指向 \`${target}\`,但没有这个环节`)
|
|
525
|
+
return
|
|
526
|
+
}
|
|
527
|
+
if (at >= phase.index) {
|
|
528
|
+
err(0, `环节 \`${phase.id}\` 的 \`${field}\` 指向 \`${target}\`(第 ${at + 1} 个),只能指向它前面出现过的环节,否则会来回跳成死循环`)
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
for (const phase of phases) {
|
|
532
|
+
const gateFail = phase.gate !== null && typeof phase.gate === 'object' ? phase.gate.on_fail : undefined
|
|
533
|
+
if (typeof gateFail === 'string' && gateFail.trim() !== '') {
|
|
534
|
+
checkTarget(phase, gateFail.trim(), 'gate.on_fail')
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
return { ok: errors.length === 0, errors, warnings, phases }
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/** 列表字段:缺省算空,字符串当单元素,别的算写错。 */
|
|
542
|
+
function normalizeStringList(value) {
|
|
543
|
+
if (value === undefined || value === null) return []
|
|
544
|
+
if (typeof value === 'string') return value.trim() === '' ? [] : [value.trim()]
|
|
545
|
+
if (!Array.isArray(value)) return null
|
|
546
|
+
const out = []
|
|
547
|
+
for (const item of value) {
|
|
548
|
+
if (typeof item !== 'string') return null
|
|
549
|
+
const t = item.trim()
|
|
550
|
+
if (t !== '') out.push(t)
|
|
551
|
+
}
|
|
552
|
+
return out
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/** 一步到位:文本进,结论出。项目里调用最多的就是这个。 */
|
|
556
|
+
export function loadFlow(text) {
|
|
557
|
+
const { value, errors: syntaxErrors } = parseFlowYaml(text)
|
|
558
|
+
if (syntaxErrors.length > 0) {
|
|
559
|
+
return { ok: false, errors: syntaxErrors, warnings: [], phases: [], doc: value }
|
|
560
|
+
}
|
|
561
|
+
const result = validateFlow(value)
|
|
562
|
+
return { ...result, doc: value }
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* 把校验结果整理成给人看的一段话。
|
|
567
|
+
*
|
|
568
|
+
* 行号一定要出现在第一行:用户拿到报错第一件事是去文件里跳行,
|
|
569
|
+
* 埋在第三行之后的正文里他看不见。
|
|
570
|
+
*/
|
|
571
|
+
export function describeFlowProblems(result) {
|
|
572
|
+
const lines = []
|
|
573
|
+
for (const e of result.errors) {
|
|
574
|
+
lines.push(e.line > 0 ? `第 ${e.line} 行:${e.message}` : e.message)
|
|
575
|
+
}
|
|
576
|
+
for (const w of result.warnings) {
|
|
577
|
+
lines.push(w.line > 0 ? `第 ${w.line} 行(提醒):${w.message}` : `提醒:${w.message}`)
|
|
578
|
+
}
|
|
579
|
+
return lines.join('\n')
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/** 内置四阶。没有 flow.yaml 时就是它,行为和 0.8.x 完全一致。 */
|
|
583
|
+
/**
|
|
584
|
+
* 不装 flow.yaml 时用的内置四阶。
|
|
585
|
+
*
|
|
586
|
+
* gate.kind 只写 artifact_present:另外那三个(intent / verdict /
|
|
587
|
+
* assertions_exit_zero)是写死的流程逻辑,不在执行器的 gate 分发里。
|
|
588
|
+
* 以前这份默认值写着 intent / verdict / assertions_exit_zero,于是
|
|
589
|
+
* 「默认配置自己过不了自己的校验」——把这份对象原样喂回 validateFlow 会报三条错。
|
|
590
|
+
* 现在默认值和执行能力对齐,测试里有一条专门守着这件事。
|
|
591
|
+
*/
|
|
592
|
+
export const BUILTIN_FLOW = {
|
|
593
|
+
process: { name: 'four-stage', version: 1 },
|
|
594
|
+
phases: [
|
|
595
|
+
{ id: 'requirement', reads: [], writes: ['Requirement.md'] },
|
|
596
|
+
{ id: 'plan', reads: ['Requirement.md'], writes: ['Plan.md'], gate: { kind: 'artifact_present' } },
|
|
597
|
+
{ id: 'build', reads: ['Plan.md'], writes: ['Execution_Log.md'], gate: { kind: 'artifact_present' } },
|
|
598
|
+
{ id: 'qa', reads: ['Requirement.md', 'Plan.md', 'Execution_Log.md'], writes: ['QA_Report.md'], gate: { kind: 'artifact_present', on_fail: 'build' } },
|
|
599
|
+
],
|
|
600
|
+
}
|