dsh-project-flow 0.13.0 → 0.13.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 +112 -0
- package/README.md +31 -10
- package/bin/flow.mjs +365 -36
- package/host/codex.mjs +292 -43
- package/package.json +2 -8
- package/runner/pipeline.mjs +30 -0
- package/tests/client-bundle.test.mjs +3 -3
- package/tests/codex-discovery.test.mjs +147 -0
- package/tests/e2e.mjs +1 -1
- package/tests/flow-cli.test.mjs +191 -0
- package/tests/flow-create.test.mjs +1 -1
- package/tests/flow-gate.test.mjs +1 -1
- package/tests/paths.mjs +27 -17
- package/tests/run-all.mjs +2 -0
package/bin/flow.mjs
CHANGED
|
@@ -4,11 +4,13 @@
|
|
|
4
4
|
*
|
|
5
5
|
* 用法
|
|
6
6
|
* ────
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
7
|
+
* flow doctor [--cwd <目录>]
|
|
8
|
+
* flow init [--out task.json] [--force]
|
|
9
|
+
* flow run [--spec task.json] [--cwd <目录>] [--project <名字>]
|
|
10
|
+
* [--model <模型>] [--sandbox read-only|workspace-write|danger-full-access]
|
|
11
|
+
* [--mode safe|quick] [--max-rework 2] [--dry-run]
|
|
10
12
|
*
|
|
11
|
-
* task.json
|
|
13
|
+
* task.json 长这样(`flow init` 会写一份):
|
|
12
14
|
*
|
|
13
15
|
* {
|
|
14
16
|
* "intent": "把日志级别从 info 改成 debug",
|
|
@@ -22,29 +24,56 @@
|
|
|
22
24
|
* 「怎么起一个子会话」这一件事。把那一件事换成 `codex exec`,其余的全都白拿。
|
|
23
25
|
*
|
|
24
26
|
* 这条入口存在的意义是证明那件事真的成立——而不是为了再维护一套产品。
|
|
27
|
+
*
|
|
28
|
+
* 它跑的是 Codex CLI,不是 ChatGPT 网页端
|
|
29
|
+
* ────────────────────────────────────
|
|
30
|
+
* 这里调的是本机装的 `codex` 可执行文件(`codex exec --json`)。ChatGPT 网页
|
|
31
|
+
* 对话框、ChatGPT 的插件/连接器、MCP 客户端都走不到这条路上:那边没有本地
|
|
32
|
+
* 文件系统权限,也跑不了你的判据命令。不要把它理解成「插件接进了 ChatGPT」。
|
|
25
33
|
*/
|
|
26
34
|
|
|
27
|
-
import {
|
|
28
|
-
import {
|
|
29
|
-
import {
|
|
35
|
+
import { spawnSync } from 'node:child_process'
|
|
36
|
+
import { existsSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
|
|
37
|
+
import { homedir } from 'node:os'
|
|
38
|
+
import { basename, delimiter, dirname, join, resolve } from 'node:path'
|
|
30
39
|
import { configure, settingsOf } from '../config/settings.mjs'
|
|
31
40
|
import { createPipeline } from '../runner/pipeline.mjs'
|
|
32
41
|
import { createCodexCtx, findCodexBinary } from '../host/codex.mjs'
|
|
33
42
|
import { Watchdog } from '../watchdog.mjs'
|
|
34
43
|
import { stateDir, stripBom } from '../shared/util.mjs'
|
|
35
44
|
|
|
45
|
+
const SANDBOX_MODES = ['read-only', 'workspace-write', 'danger-full-access']
|
|
46
|
+
|
|
47
|
+
/** 数改动文件时不算进去的目录:产物和临时状态不是「改了项目」。 */
|
|
48
|
+
const NOT_PROJECT_CHANGES = ['.project-flow/', '.flow-home/', 'node_modules/']
|
|
49
|
+
|
|
50
|
+
const TEMPLATE = `${JSON.stringify({
|
|
51
|
+
intent: '给这个项目加一个健康检查接口',
|
|
52
|
+
requirements: [
|
|
53
|
+
'访问 /health 返回 JSON,里面 status 是 ok',
|
|
54
|
+
'现有测试全部还能跑过',
|
|
55
|
+
],
|
|
56
|
+
checks: [
|
|
57
|
+
{ req: 1, check: 'node -e "process.exit(0)"', checkExit: 0 },
|
|
58
|
+
{ req: 2, check: 'npm test', checkExit: 0 },
|
|
59
|
+
],
|
|
60
|
+
constraints: '只改实现和测试,CI 配置和依赖锁文件不要动',
|
|
61
|
+
forbidden: ['.env', 'secrets/'],
|
|
62
|
+
}, null, 2)}\n`
|
|
63
|
+
|
|
36
64
|
function usage(code = 0) {
|
|
37
|
-
const text = `flow —— 在 DSH
|
|
65
|
+
const text = `flow —— 在 DSH 之外跑同一套四阶流水(宿主是 Codex CLI)
|
|
38
66
|
|
|
39
|
-
node
|
|
67
|
+
flow doctor 检查环境:node / codex / 依赖 / 目录 / 配置
|
|
68
|
+
flow init 写一份能直接跑的任务书模板
|
|
69
|
+
flow run [选项] 跑一次
|
|
40
70
|
|
|
41
|
-
|
|
42
|
-
--spec <文件> 任务书 JSON
|
|
43
|
-
可选:constraints / forbidden / mode
|
|
71
|
+
run 的选项
|
|
72
|
+
--spec <文件> 任务书 JSON。不给就在工作目录里找 task.json
|
|
44
73
|
--cwd <目录> 工作目录,默认当前目录
|
|
45
74
|
--project <名字> 项目名,默认取工作目录名
|
|
46
75
|
--model <模型> 传给 codex 的模型,留空用 codex 自己的配置
|
|
47
|
-
--sandbox <模式>
|
|
76
|
+
--sandbox <模式> ${SANDBOX_MODES.join(' / ')}
|
|
48
77
|
默认 workspace-write(能写工作区,碰不到外面)
|
|
49
78
|
--mode <模式> safe(默认,四阶)或 quick(跳过规划和质检子会话)
|
|
50
79
|
--max-rework <n> 最多返工几次,默认 2
|
|
@@ -66,7 +95,7 @@ function parseArgs(argv) {
|
|
|
66
95
|
continue
|
|
67
96
|
}
|
|
68
97
|
const key = token.slice(2)
|
|
69
|
-
if (key === 'dry-run' || key === 'help') {
|
|
98
|
+
if (key === 'dry-run' || key === 'help' || key === 'force') {
|
|
70
99
|
out[key] = true
|
|
71
100
|
continue
|
|
72
101
|
}
|
|
@@ -92,7 +121,7 @@ function readSpec(path) {
|
|
|
92
121
|
throw new Error(`任务书不是合法 JSON:${error.message}`)
|
|
93
122
|
}
|
|
94
123
|
if (typeof spec?.intent !== 'string' || spec.intent.trim() === '') {
|
|
95
|
-
throw new Error('任务书缺 intent')
|
|
124
|
+
throw new Error('任务书缺 intent:一句话说明这次要做什么,没有它后面全是猜')
|
|
96
125
|
}
|
|
97
126
|
const requirements = Array.isArray(spec.requirements)
|
|
98
127
|
? spec.requirements.filter(line => typeof line === 'string' && line.trim() !== '')
|
|
@@ -110,54 +139,323 @@ function readSpec(path) {
|
|
|
110
139
|
}
|
|
111
140
|
}
|
|
112
141
|
|
|
113
|
-
|
|
114
|
-
|
|
142
|
+
// ── 数改动文件 ─────────────────────────────────────────────
|
|
143
|
+
|
|
144
|
+
function gitOutput(cwd, args) {
|
|
145
|
+
const result = spawnSync('git', ['-C', cwd, ...args], {
|
|
146
|
+
encoding: 'utf8',
|
|
147
|
+
windowsHide: true,
|
|
148
|
+
timeout: 30000,
|
|
149
|
+
})
|
|
150
|
+
if (result.error !== undefined || result.status !== 0) return undefined
|
|
151
|
+
return result.stdout
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* 这次改了项目里几个文件。
|
|
156
|
+
*
|
|
157
|
+
* 走 git status 而不是自己记账:记账那份在这个进程里拿不到(它是子会话写的
|
|
158
|
+
* 文件),而 git 是现成的事实源。不是 git 仓库就返回 undefined——报告里写
|
|
159
|
+
* 「数不出来」,不要印一个 0 假装没改动。
|
|
160
|
+
*/
|
|
161
|
+
function filesChangedOf(cwd) {
|
|
162
|
+
const text = gitOutput(cwd, ['status', '--porcelain'])
|
|
163
|
+
if (text === undefined) return undefined
|
|
164
|
+
const paths = new Set()
|
|
165
|
+
for (const line of text.split(/\r?\n/u)) {
|
|
166
|
+
if (line.trim() === '') continue
|
|
167
|
+
// porcelain v1:两个状态位 + 空格 + 路径;重命名是「旧 -> 新」
|
|
168
|
+
let path = line.slice(3).trim()
|
|
169
|
+
const arrow = path.indexOf(' -> ')
|
|
170
|
+
if (arrow !== -1) path = path.slice(arrow + 4)
|
|
171
|
+
path = path.replace(/^"|"$/gu, '')
|
|
172
|
+
if (NOT_PROJECT_CHANGES.some(prefix => path.startsWith(prefix))) continue
|
|
173
|
+
paths.add(path)
|
|
174
|
+
}
|
|
175
|
+
return paths.size
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** 回滚还管不管用:快照仓在、而且里面有东西。 */
|
|
179
|
+
function rollbackOf(cwd, project) {
|
|
180
|
+
const gitDir = join(stateDir(cwd, project), 'snap.git')
|
|
181
|
+
if (!existsSync(gitDir)) {
|
|
182
|
+
return { ok: false, detail: '没有快照(这个目录不是 git 仓库,或者快照关了)' }
|
|
183
|
+
}
|
|
184
|
+
const result = spawnSync('git', [`--git-dir=${gitDir}`, 'rev-list', '--count', 'HEAD'], {
|
|
185
|
+
encoding: 'utf8',
|
|
186
|
+
windowsHide: true,
|
|
187
|
+
timeout: 30000,
|
|
188
|
+
})
|
|
189
|
+
if (result.error !== undefined || result.status !== 0) return { ok: true, detail: `快照仓在 ${gitDir}` }
|
|
190
|
+
const count = String(result.stdout).trim()
|
|
191
|
+
return { ok: true, detail: `可用,${count} 张快照` }
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// ── 报告 ──────────────────────────────────────────────────
|
|
195
|
+
|
|
196
|
+
/** 三阶就停 vs 四阶判完才停,下一步完全不同,报告里要分开说。 */
|
|
197
|
+
function nextStepOf(outcome) {
|
|
198
|
+
if (outcome === 'passed') return '可以收工了。不满意的地方改要求重跑,别手改产物。'
|
|
199
|
+
if (outcome === 'stopped') return '停在这儿是因为再跑也不会变好:要么自己动手,要么把要求放宽再跑。'
|
|
200
|
+
return '看上面那几条断言,先改代码再跑一次;「继续」只重跑没过的部分。'
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* 把 pipeline 返回的东西压成给人看的一段。
|
|
205
|
+
*
|
|
206
|
+
* 用户最后想知道的就七件事:过没过、要求几条过了、判据几条过了、改了几个
|
|
207
|
+
* 文件、返工几次、证据在哪、能不能回滚。别把这些埋在日志里让他自己找。
|
|
208
|
+
*/
|
|
209
|
+
function report(result, extras = {}) {
|
|
115
210
|
const lines = []
|
|
116
|
-
const
|
|
211
|
+
const outcome = String(result.outcome ?? '?')
|
|
212
|
+
const badge = outcome === 'passed' ? 'PASS' : outcome.toUpperCase()
|
|
117
213
|
lines.push('')
|
|
118
|
-
lines.push(
|
|
119
|
-
|
|
214
|
+
lines.push(`${badge}|停在第 ${result.stage ?? '?'} 阶`)
|
|
215
|
+
|
|
216
|
+
const assertions = result.assertions ?? {}
|
|
217
|
+
const req = assertions.requirements ?? {}
|
|
218
|
+
const checks = assertions.checks ?? {}
|
|
219
|
+
const dash = '还没验'
|
|
220
|
+
lines.push(` 要求:${req.passed ?? dash}${req.total === undefined ? '' : `/${req.total}`} 条过`)
|
|
221
|
+
lines.push(` 判据:${checks.passed ?? dash}${checks.total === undefined ? '' : `/${checks.total}`} 条过`
|
|
222
|
+
+ (checks.failed === undefined ? '' : `(未过 ${checks.failed},判不了 ${checks.unverified ?? 0})`))
|
|
223
|
+
lines.push(` 改动文件:${extras.files === undefined ? '数不出来(工作目录不是 git 仓库)' : `${extras.files} 个`}`)
|
|
224
|
+
lines.push(` 返工:${result.rework ?? 0} 次`)
|
|
225
|
+
lines.push(` 证据:${result.evidence === '' || result.evidence === undefined ? '(这一轮没生成)' : result.evidence}`)
|
|
226
|
+
lines.push(` 回滚:${extras.rollback?.detail ?? '不知道'}`)
|
|
227
|
+
|
|
228
|
+
if (typeof result.verdict === 'string' && result.verdict !== '') {
|
|
229
|
+
lines.push('')
|
|
230
|
+
lines.push(`判词:${result.verdict}`)
|
|
231
|
+
}
|
|
120
232
|
// 这些是质检给出的观察,不是「没闭环的问题」——只有返工那轮才算闭环与否。
|
|
121
233
|
// 一次就过的时候把它们叫成「没闭环」,读起来像这活还有毛病没修。
|
|
122
234
|
if (Array.isArray(result.issues) && result.issues.length > 0) {
|
|
123
|
-
lines.push(
|
|
235
|
+
lines.push('')
|
|
236
|
+
lines.push(outcome === 'passed' ? '质检记下的几点:' : '还没解决的几条:')
|
|
124
237
|
for (const issue of result.issues.slice(0, 10)) lines.push(` - ${issue}`)
|
|
125
238
|
}
|
|
126
239
|
if (typeof result.note === 'string' && result.note.trim() !== '') {
|
|
127
240
|
lines.push('')
|
|
128
241
|
lines.push(result.note.trim())
|
|
129
242
|
}
|
|
243
|
+
lines.push('')
|
|
244
|
+
lines.push(`下一步:${nextStepOf(outcome)}`)
|
|
130
245
|
return lines.join('\n')
|
|
131
246
|
}
|
|
132
247
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
248
|
+
/**
|
|
249
|
+
* 出错了给人话说一句。
|
|
250
|
+
*
|
|
251
|
+
* 堆栈对写插件的人有用,对用插件的人就是噪音。这里把它翻成「哪一类、下一步
|
|
252
|
+
* 干嘛」,堆栈仍然打,但放在最后。
|
|
253
|
+
*/
|
|
254
|
+
function classifyError(error) {
|
|
255
|
+
const text = `${error?.code ?? ''} ${error?.message ?? error}`
|
|
256
|
+
if (/ENOENT|not recognized|not found|找不到/iu.test(text)) {
|
|
257
|
+
return { kind: '缺东西', next: '跑 flow doctor,它会把 node / codex / 依赖逐项报出来。' }
|
|
258
|
+
}
|
|
259
|
+
if (/EACCES|EPERM|access is denied|permission|权限|拒绝访问/iu.test(text)) {
|
|
260
|
+
return { kind: '权限不足', next: '换个能写的目录,或者把 --sandbox 调成 danger-full-access。' }
|
|
261
|
+
}
|
|
262
|
+
if (/ERR_MODULE_NOT_FOUND/iu.test(text)) {
|
|
263
|
+
return { kind: '依赖没装', next: '在包目录里跑一次 npm i(@deepseek-ai/schemastery 是 peer 依赖)。' }
|
|
139
264
|
}
|
|
140
|
-
if (
|
|
141
|
-
|
|
142
|
-
|
|
265
|
+
if (/不是合法 JSON|Unexpected token/iu.test(text)) {
|
|
266
|
+
return { kind: '任务书读不了', next: 'flow init --force 重新生成一份,别用会加 BOM 的编辑器另存。' }
|
|
267
|
+
}
|
|
268
|
+
if (/exit code|退出码/iu.test(text)) {
|
|
269
|
+
return { kind: '命令跑挂了', next: '看那条命令自己的输出,先把命令单独跑通再说。' }
|
|
270
|
+
}
|
|
271
|
+
return { kind: '没归类', next: '把下面这段堆栈贴到 issues 里,标一下系统。' }
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// ── doctor ────────────────────────────────────────────────
|
|
275
|
+
|
|
276
|
+
const MARK = { ok: '✓', bad: '✗', warn: '!' }
|
|
277
|
+
|
|
278
|
+
function row(mark, label, detail) {
|
|
279
|
+
return ` ${MARK[mark]} ${label}:${detail}`
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function codexVersionOf(bin) {
|
|
283
|
+
const result = spawnSync(bin, ['--version'], {
|
|
284
|
+
encoding: 'utf8',
|
|
285
|
+
windowsHide: true,
|
|
286
|
+
timeout: 15000,
|
|
287
|
+
// .cmd 包装脚本不带 shell 起不来;.exe 加了反而会把参数再解释一遍。
|
|
288
|
+
shell: process.platform === 'win32' && /\.(cmd|bat)$/iu.test(bin),
|
|
289
|
+
})
|
|
290
|
+
if (result.error !== undefined) return { ok: false, detail: String(result.error.message ?? result.error) }
|
|
291
|
+
if (result.status !== 0) return { ok: false, detail: `跑 --version 退出码 ${result.status}` }
|
|
292
|
+
const first = String(result.stdout ?? '').trim().split(/\r?\n/u)[0]
|
|
293
|
+
return { ok: true, detail: first === '' ? '能跑,但没报版本号' : first }
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
async function dependencyOk() {
|
|
297
|
+
try {
|
|
298
|
+
await import('@deepseek-ai/schemastery')
|
|
299
|
+
return { ok: true, detail: '能加载 @deepseek-ai/schemastery' }
|
|
300
|
+
} catch (error) {
|
|
301
|
+
if (error?.code === 'ERR_MODULE_NOT_FOUND') {
|
|
302
|
+
return { ok: false, detail: '没装(peer 依赖,跑一行 npm i 就好)' }
|
|
303
|
+
}
|
|
304
|
+
return { ok: false, detail: String(error?.message ?? error) }
|
|
143
305
|
}
|
|
306
|
+
}
|
|
144
307
|
|
|
308
|
+
function writableOk(dir) {
|
|
309
|
+
const probe = join(dir, `.flow-doctor-${process.pid}.tmp`)
|
|
310
|
+
try {
|
|
311
|
+
writeFileSync(probe, 'ok')
|
|
312
|
+
rmSync(probe, { force: true })
|
|
313
|
+
return { ok: true, detail: dir }
|
|
314
|
+
} catch (error) {
|
|
315
|
+
return { ok: false, detail: `${dir}(${error?.code ?? error?.message ?? error})` }
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function dshConfigOk() {
|
|
320
|
+
const home = typeof process.env.DSH_HOME === 'string' && process.env.DSH_HOME !== ''
|
|
321
|
+
? process.env.DSH_HOME
|
|
322
|
+
: join(homedir(), '.dsh')
|
|
323
|
+
const path = join(home, 'project-flow.json')
|
|
324
|
+
if (!existsSync(path)) return { ok: true, skipped: true, detail: `没有 ${path},全用包里的默认值` }
|
|
325
|
+
try {
|
|
326
|
+
const parsed = JSON.parse(stripBom(readFileSync(path, 'utf8')))
|
|
327
|
+
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
328
|
+
return { ok: false, detail: `${path} 不是一个 JSON 对象` }
|
|
329
|
+
}
|
|
330
|
+
return { ok: true, detail: `${path}(${Object.keys(parsed).length} 个键,能解析)` }
|
|
331
|
+
} catch (error) {
|
|
332
|
+
return { ok: false, detail: `${path} 解析不了:${error?.message ?? error}` }
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
async function cmdDoctor(args) {
|
|
337
|
+
const cwd = resolve(typeof args.cwd === 'string' ? args.cwd : process.cwd())
|
|
338
|
+
const blockers = []
|
|
339
|
+
const lines = ['flow doctor —— 能不能跑,逐项看', '']
|
|
340
|
+
|
|
341
|
+
// ① Node
|
|
342
|
+
const major = Number(String(process.versions.node).split('.')[0])
|
|
343
|
+
if (major >= 20) {
|
|
344
|
+
lines.push(row('ok', 'Node', `${process.versions.node}`))
|
|
345
|
+
} else {
|
|
346
|
+
lines.push(row('bad', 'Node', `${process.versions.node} —— 需要 20 或更新`))
|
|
347
|
+
blockers.push('Node 太旧:升级到 20 以上再来。')
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// ② codex
|
|
351
|
+
const bin = findCodexBinary()
|
|
352
|
+
if (bin === '') {
|
|
353
|
+
lines.push(row('bad', 'codex', '没找到(查过 CODEX_CLI_PATH、PATH、AppData\\Local\\OpenAI\\Codex\\bin)'))
|
|
354
|
+
blockers.push('装 Codex CLI,或者用环境变量 CODEX_CLI_PATH 指到 codex 可执行文件。')
|
|
355
|
+
} else {
|
|
356
|
+
const version = codexVersionOf(bin)
|
|
357
|
+
lines.push(row(version.ok ? 'ok' : 'bad', 'codex', `${bin}|${version.detail}`))
|
|
358
|
+
if (!version.ok) blockers.push('codex 找到了但跑不动:重装一次 Codex CLI。')
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// ③ 依赖
|
|
362
|
+
const dep = await dependencyOk()
|
|
363
|
+
lines.push(row(dep.ok ? 'ok' : 'bad', '依赖', dep.detail))
|
|
364
|
+
if (!dep.ok) blockers.push('在包目录里跑 npm i。')
|
|
365
|
+
|
|
366
|
+
// ④ 工作目录
|
|
367
|
+
if (!existsSync(cwd)) {
|
|
368
|
+
lines.push(row('bad', '工作目录', `${cwd} 不存在`))
|
|
369
|
+
blockers.push('--cwd 指错了,或者先建这个目录。')
|
|
370
|
+
} else {
|
|
371
|
+
const writable = writableOk(cwd)
|
|
372
|
+
lines.push(row(writable.ok ? 'ok' : 'bad', '工作目录可写', writable.detail))
|
|
373
|
+
if (!writable.ok) blockers.push('换一个能写的目录,或者把 sandbox 调到 danger-full-access(仍然要目录本身可写)。')
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// ⑤ 沙箱
|
|
377
|
+
lines.push(row('ok', 'sandbox 可选值', SANDBOX_MODES.join(' / ')))
|
|
378
|
+
lines.push(row('warn', 'sandbox 现状', '能不能写只有真跑一次才知道;默认 workspace-write,写不进去就换 danger-full-access'))
|
|
379
|
+
|
|
380
|
+
// ⑥ DSH 配置
|
|
381
|
+
const config = dshConfigOk()
|
|
382
|
+
lines.push(row(config.ok ? (config.skipped ? 'warn' : 'ok') : 'bad', 'DSH 配置', config.detail))
|
|
383
|
+
if (!config.ok) blockers.push('把 ~/.dsh/project-flow.json 修好或者直接删掉(删掉就全用默认值)。')
|
|
384
|
+
|
|
385
|
+
// ⑦ git
|
|
386
|
+
if (gitOutput(cwd, ['rev-parse', '--is-inside-work-tree']) === undefined) {
|
|
387
|
+
lines.push(row('warn', 'git', '这个目录不是 git 仓库:流水照跑,但没有快照、也不能回滚'))
|
|
388
|
+
} else {
|
|
389
|
+
lines.push(row('ok', 'git', '是 git 仓库,快照和回滚都可用'))
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
lines.push('')
|
|
393
|
+
if (blockers.length === 0) {
|
|
394
|
+
lines.push('结论:能跑。下一步 —— flow init 写任务书,然后 flow run --dry-run 确认一遍再真跑。')
|
|
395
|
+
} else {
|
|
396
|
+
lines.push(`结论:还有 ${blockers.length} 件要处理:`)
|
|
397
|
+
for (const item of blockers) lines.push(` - ${item}`)
|
|
398
|
+
}
|
|
399
|
+
process.stdout.write(`${lines.join('\n')}\n`)
|
|
400
|
+
process.exit(blockers.length === 0 ? 0 : 1)
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function cmdInit(args) {
|
|
404
|
+
const cwd = resolve(typeof args.cwd === 'string' ? args.cwd : process.cwd())
|
|
405
|
+
const out = resolve(cwd, typeof args.out === 'string' ? args.out : 'task.json')
|
|
406
|
+
if (existsSync(out) && args.force !== true) {
|
|
407
|
+
process.stderr.write(`${out} 已经在了。要覆盖就加 --force,不然就换个 --out。\n`)
|
|
408
|
+
process.exit(2)
|
|
409
|
+
}
|
|
410
|
+
try {
|
|
411
|
+
writeFileSync(out, TEMPLATE, 'utf8')
|
|
412
|
+
} catch (error) {
|
|
413
|
+
const { kind, next } = classifyError(error)
|
|
414
|
+
process.stderr.write(`写不了 ${out}(${kind}):${error?.message ?? error}\n下一步:${next}\n`)
|
|
415
|
+
process.exit(2)
|
|
416
|
+
}
|
|
417
|
+
process.stdout.write(`${out} 写好了。\n\n`
|
|
418
|
+
+ '接下来:\n'
|
|
419
|
+
+ ' 1. 把 intent 改成要做的事,requirements 一条一行写成人能判对错的样子\n'
|
|
420
|
+
+ ' 2. checks 里第几条对应第几条要求,命令退出码 0 就算过\n'
|
|
421
|
+
+ ' 3. flow run --dry-run 看环境,再 flow run\n')
|
|
422
|
+
process.exit(0)
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// ── run ───────────────────────────────────────────────────
|
|
426
|
+
|
|
427
|
+
async function cmdRun(args) {
|
|
145
428
|
const cwd = resolve(typeof args.cwd === 'string' ? args.cwd : process.cwd())
|
|
146
429
|
if (!existsSync(cwd)) {
|
|
147
430
|
process.stderr.write(`工作目录不存在:${cwd}\n`)
|
|
148
431
|
process.exit(2)
|
|
149
432
|
}
|
|
433
|
+
|
|
434
|
+
// 不给 --spec 就在工作目录里找 task.json:`flow init` 写完直接 `flow run`
|
|
435
|
+
// 是新手最自然的顺序,不该因为他没记住那个参数就报错。
|
|
436
|
+
const specPath = typeof args.spec === 'string' ? resolve(cwd, args.spec) : join(cwd, 'task.json')
|
|
437
|
+
if (!existsSync(specPath)) {
|
|
438
|
+
process.stderr.write(
|
|
439
|
+
`找不到任务书:${specPath}\n`
|
|
440
|
+
+ '先跑 flow init 生成一份,或者用 --spec 指到你的文件。\n',
|
|
441
|
+
)
|
|
442
|
+
process.exit(2)
|
|
443
|
+
}
|
|
444
|
+
|
|
150
445
|
const project = typeof args.project === 'string' && args.project !== ''
|
|
151
446
|
? args.project
|
|
152
447
|
: basename(cwd)
|
|
153
448
|
|
|
154
|
-
const spec = readSpec(
|
|
449
|
+
const spec = readSpec(specPath)
|
|
155
450
|
const bin = findCodexBinary()
|
|
156
451
|
if (bin === '') {
|
|
157
452
|
process.stderr.write(
|
|
158
453
|
'找不到 codex 可执行文件。\n'
|
|
159
|
-
+ '
|
|
160
|
-
+ '
|
|
454
|
+
+ '查过这三个位置:环境变量 CODEX_CLI_PATH、PATH 上的 codex、'
|
|
455
|
+
+ '%LOCALAPPDATA%\\OpenAI\\Codex\\bin\\<build>\\codex.exe\n'
|
|
456
|
+
+ '装了 Codex 桌面版的话,CLI 二进制一般就在上面那个目录里;'
|
|
457
|
+
+ '用 npm 装的话先看 npm bin 在不在 PATH 上。\n'
|
|
458
|
+
+ '跑 flow doctor 可以逐项看到底卡在哪。\n',
|
|
161
459
|
)
|
|
162
460
|
process.exit(2)
|
|
163
461
|
}
|
|
@@ -179,6 +477,11 @@ async function main() {
|
|
|
179
477
|
process.stdout.write(`要求 ${spec.requirements.length} 条,判据 ${spec.checks.length} 条\n`)
|
|
180
478
|
|
|
181
479
|
if (args['dry-run'] === true) {
|
|
480
|
+
const dep = await dependencyOk()
|
|
481
|
+
if (!dep.ok) {
|
|
482
|
+
process.stderr.write(`\n依赖没装好:${dep.detail}\n`)
|
|
483
|
+
process.exit(2)
|
|
484
|
+
}
|
|
182
485
|
process.stdout.write('\n环境没问题(--dry-run:没起子会话,没花钱)\n')
|
|
183
486
|
return
|
|
184
487
|
}
|
|
@@ -212,7 +515,16 @@ async function main() {
|
|
|
212
515
|
sandbox: typeof args.sandbox === 'string' ? args.sandbox : 'workspace-write',
|
|
213
516
|
watchdog,
|
|
214
517
|
onRun: info => {
|
|
215
|
-
|
|
518
|
+
const images = info.images === undefined || info.images === 0 ? '' : `,图片 ${info.images} 张`
|
|
519
|
+
process.stdout.write(` 起子会话:${info.label}(prompt ${info.promptChars} 字${images})\n`)
|
|
520
|
+
},
|
|
521
|
+
onImage: info => {
|
|
522
|
+
if (info.skipped === 0) return
|
|
523
|
+
process.stderr.write(
|
|
524
|
+
`\n有 ${info.skipped} 张图片没能传给 codex(这个内容块里找不到 base64 数据)。\n`
|
|
525
|
+
+ 'codex 能收图,但得是真实文件;认不出的块只能跳过。'
|
|
526
|
+
+ '如果这阶的活依赖看图,先把图存成文件再在 prompt 里给路径。\n\n',
|
|
527
|
+
)
|
|
216
528
|
},
|
|
217
529
|
})
|
|
218
530
|
|
|
@@ -243,7 +555,10 @@ async function main() {
|
|
|
243
555
|
watchdog.dispose()
|
|
244
556
|
}
|
|
245
557
|
|
|
246
|
-
process.stdout.write(report(result
|
|
558
|
+
process.stdout.write(report(result, {
|
|
559
|
+
files: filesChangedOf(cwd),
|
|
560
|
+
rollback: rollbackOf(cwd, project),
|
|
561
|
+
}))
|
|
247
562
|
|
|
248
563
|
// 两个花费数分开摆:插件的预算是按字符估的(它拿不到 token 数),
|
|
249
564
|
// codex 自己报的是真账。把估算值单独标出来,别让人以为那就是账单。
|
|
@@ -260,7 +575,21 @@ async function main() {
|
|
|
260
575
|
process.exit(result?.outcome === 'passed' ? 0 : 1)
|
|
261
576
|
}
|
|
262
577
|
|
|
578
|
+
async function main() {
|
|
579
|
+
const args = parseArgs(process.argv.slice(2))
|
|
580
|
+
if (args.help === true) usage(0)
|
|
581
|
+
const command = args._[0]
|
|
582
|
+
if (command === undefined) usage(2)
|
|
583
|
+
if (command === 'doctor') return cmdDoctor(args)
|
|
584
|
+
if (command === 'init') return cmdInit(args)
|
|
585
|
+
if (command === 'run') return cmdRun(args)
|
|
586
|
+
process.stderr.write(`不认识的子命令:${command}\n\n可用的:doctor、init、run\n`)
|
|
587
|
+
usage(2)
|
|
588
|
+
}
|
|
589
|
+
|
|
263
590
|
main().catch(error => {
|
|
264
|
-
|
|
591
|
+
const { kind, next } = classifyError(error)
|
|
592
|
+
process.stderr.write(`\n出错了(${kind}):${error?.message ?? error}\n下一步:${next}\n`)
|
|
593
|
+
if (process.env.FLOW_DEBUG === '1') process.stderr.write(`\n${error?.stack ?? ''}\n`)
|
|
265
594
|
process.exit(1)
|
|
266
595
|
})
|