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/host/codex.mjs
CHANGED
|
@@ -37,27 +37,110 @@
|
|
|
37
37
|
*/
|
|
38
38
|
|
|
39
39
|
import { spawn } from 'node:child_process'
|
|
40
|
-
import { existsSync, readdirSync } from 'node:fs'
|
|
41
|
-
import {
|
|
40
|
+
import { existsSync, mkdtempSync, readdirSync, rmSync, statSync, writeFileSync } from 'node:fs'
|
|
41
|
+
import { tmpdir } from 'node:os'
|
|
42
|
+
import { delimiter, isAbsolute, join } from 'node:path'
|
|
42
43
|
import { createInterface } from 'node:readline'
|
|
43
44
|
|
|
44
45
|
/** 缺省沙箱:能写工作区,但碰不到外面。 */
|
|
45
46
|
const DEFAULT_SANDBOX = 'workspace-write'
|
|
46
47
|
|
|
48
|
+
/** mediaType → 落盘扩展名。codex 的 `-i` 只吃真实文件,扩展名不对它不认。 */
|
|
49
|
+
const IMAGE_EXT = {
|
|
50
|
+
'image/png': 'png',
|
|
51
|
+
'image/jpeg': 'jpg',
|
|
52
|
+
'image/jpg': 'jpg',
|
|
53
|
+
'image/webp': 'webp',
|
|
54
|
+
'image/gif': 'gif',
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 把一个 image 内容块抠成 `{ mediaType, base64 }`,认三种写法。
|
|
59
|
+
*
|
|
60
|
+
* DSH 那边给过的形状不止一种,而且中间改过。与其约定「必须是 source.type
|
|
61
|
+
* === 'base64'」然后对旧形状静默丢图,不如三种都认一遍——丢图是安静的,
|
|
62
|
+
* 用户只会看到「模型说没看到图片」,查半天。
|
|
63
|
+
*
|
|
64
|
+
* 抠不出来返回 undefined,由调用方计数并明说「这张没传过去」。
|
|
65
|
+
*/
|
|
66
|
+
function decodeImageBlock(block) {
|
|
67
|
+
// ① { image: 'data:image/png;base64,xxxx' }
|
|
68
|
+
const inline = block.image
|
|
69
|
+
if (typeof inline === 'string' && inline.startsWith('data:')) {
|
|
70
|
+
const matched = /^data:([^;,]+);base64,(.+)$/su.exec(inline)
|
|
71
|
+
if (matched !== null) return { mediaType: matched[1].toLowerCase(), base64: matched[2] }
|
|
72
|
+
}
|
|
73
|
+
// ② { source: { type: 'base64', mediaType, data } } —— DSH 的正式形状
|
|
74
|
+
const source = block.source
|
|
75
|
+
if (source !== null && typeof source === 'object' && typeof source.data === 'string') {
|
|
76
|
+
const mediaType = typeof source.mediaType === 'string' ? source.mediaType : 'image/png'
|
|
77
|
+
return { mediaType: mediaType.toLowerCase(), base64: source.data }
|
|
78
|
+
}
|
|
79
|
+
// ③ { data, mediaType }
|
|
80
|
+
if (typeof block.data === 'string') {
|
|
81
|
+
const mediaType = typeof block.mediaType === 'string' ? block.mediaType : 'image/png'
|
|
82
|
+
return { mediaType: mediaType.toLowerCase(), base64: block.data }
|
|
83
|
+
}
|
|
84
|
+
return undefined
|
|
85
|
+
}
|
|
86
|
+
|
|
47
87
|
/**
|
|
48
|
-
*
|
|
88
|
+
* 把内容块数组拆成文字和图片。
|
|
89
|
+
*
|
|
90
|
+
* 早先这里只取文字,图片块直接丢掉——文本任务看不出毛病,一到「看截图说
|
|
91
|
+
* 哪里错位了」这种活就整段能力消失,而且没有任何提示。现在图片会落盘、
|
|
92
|
+
* 用 `-i` 传给 codex;实在抠不出来的才跳过,并且由调用方喊一声。
|
|
49
93
|
*
|
|
50
|
-
*
|
|
51
|
-
* 图片块先跳过——codex 支持 `-i`,但需要真实文件路径,而 DSH 的图片块里
|
|
52
|
-
* 是 base64。要接的话得先落盘,那是下一步的事,不在这里假装支持。
|
|
94
|
+
* @returns {{ text: string, images: Array<{mediaType: string, base64: string}>, skipped: number }}
|
|
53
95
|
*/
|
|
96
|
+
export function promptPartsOf(content) {
|
|
97
|
+
if (typeof content === 'string') return { text: content, images: [], skipped: 0 }
|
|
98
|
+
if (!Array.isArray(content)) return { text: '', images: [], skipped: 0 }
|
|
99
|
+
|
|
100
|
+
const texts = []
|
|
101
|
+
const images = []
|
|
102
|
+
let skipped = 0
|
|
103
|
+
|
|
104
|
+
for (const block of content) {
|
|
105
|
+
if (block === null || typeof block !== 'object') continue
|
|
106
|
+
if (block.type === 'text') {
|
|
107
|
+
if (typeof block.text === 'string') texts.push(block.text)
|
|
108
|
+
continue
|
|
109
|
+
}
|
|
110
|
+
if (block.type === 'image') {
|
|
111
|
+
const decoded = decodeImageBlock(block)
|
|
112
|
+
if (decoded === undefined) skipped += 1
|
|
113
|
+
else images.push(decoded)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return { text: texts.join('\n\n'), images, skipped }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** 只要文字时用这个(日志、长度统计都按文字算)。 */
|
|
54
121
|
function promptTextOf(content) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
122
|
+
return promptPartsOf(content).text
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* 图片落盘,返回文件路径。
|
|
127
|
+
*
|
|
128
|
+
* codex 的 `-i/--image` 只吃真实文件路径,没有 base64 入口,所以必须写一次
|
|
129
|
+
* 临时文件。目录单开一个 `mkdtemp`,跑完在 finish 里整棵删掉——不删的话
|
|
130
|
+
* 每次带图的任务都会在 %TEMP% 里留几张图,攒久了很脏。
|
|
131
|
+
*/
|
|
132
|
+
function spillImages(images) {
|
|
133
|
+
if (images.length === 0) return { files: [], dir: '' }
|
|
134
|
+
const dir = mkdtempSync(join(tmpdir(), 'pf-codex-img-'))
|
|
135
|
+
const files = []
|
|
136
|
+
for (let index = 0; index < images.length; index += 1) {
|
|
137
|
+
const image = images[index]
|
|
138
|
+
const ext = IMAGE_EXT[image.mediaType] ?? 'png'
|
|
139
|
+
const path = join(dir, `img-${index + 1}.${ext}`)
|
|
140
|
+
writeFileSync(path, Buffer.from(image.base64, 'base64'))
|
|
141
|
+
files.push(path)
|
|
142
|
+
}
|
|
143
|
+
return { files, dir }
|
|
61
144
|
}
|
|
62
145
|
|
|
63
146
|
/** 把一条命令压成一行,给熔断日志用。 */
|
|
@@ -76,6 +159,7 @@ function briefCommand(command) {
|
|
|
76
159
|
* @param watchdog 守护器。事件直接喂给它——它天然对应「当前在跑的那一阶」,
|
|
77
160
|
* 不需要再做一次路由。
|
|
78
161
|
* @param onRun 可选:每次起子进程时回调,用来打印进度
|
|
162
|
+
* @param onImage 可选:图片处理结果回调(传了几张、丢了几张)
|
|
79
163
|
*/
|
|
80
164
|
export function createCodexCtx(options = {}) {
|
|
81
165
|
const {
|
|
@@ -89,6 +173,7 @@ export function createCodexCtx(options = {}) {
|
|
|
89
173
|
extraArgs = [],
|
|
90
174
|
watchdog,
|
|
91
175
|
onRun,
|
|
176
|
+
onImage,
|
|
92
177
|
} = options
|
|
93
178
|
|
|
94
179
|
if (typeof bin !== 'string' || bin === '') {
|
|
@@ -119,20 +204,34 @@ export function createCodexCtx(options = {}) {
|
|
|
119
204
|
*/
|
|
120
205
|
async startActivation(spec = {}) {
|
|
121
206
|
const workdir = typeof spec.cwd === 'string' && spec.cwd !== '' ? spec.cwd : cwd
|
|
122
|
-
const
|
|
123
|
-
|
|
207
|
+
const parts = promptPartsOf(spec.prompt)
|
|
208
|
+
const prompt = parts.text
|
|
209
|
+
if (prompt === '' && parts.images.length === 0) {
|
|
124
210
|
return failedActivation('这一阶的 prompt 是空的:没有内容投给 codex,不能起空会话')
|
|
125
211
|
}
|
|
126
212
|
|
|
127
213
|
const label = typeof spec.label === 'string' ? spec.label : 'codex'
|
|
214
|
+
const spilled = spillImages(parts.images)
|
|
215
|
+
if (parts.skipped > 0) {
|
|
216
|
+
// 静默丢图是最坏的一种错:模型只会说「我没看到图」,人会以为是模型不行。
|
|
217
|
+
onImage?.({ sent: spilled.files.length, skipped: parts.skipped })
|
|
218
|
+
}
|
|
219
|
+
|
|
128
220
|
const args = [...binArgs, 'exec', '--json', '--skip-git-repo-check', '-C', workdir]
|
|
129
221
|
if (typeof model === 'string' && model !== '') args.push('-m', model)
|
|
130
222
|
if (typeof sandbox === 'string' && sandbox !== '') args.push('-s', sandbox)
|
|
223
|
+
for (const file of spilled.files) args.push('-i', file)
|
|
131
224
|
args.push(...extraArgs)
|
|
132
225
|
// 用 `-` 让 codex 从 stdin 读 prompt:中文、换行、引号全不用转义。
|
|
133
226
|
args.push('-')
|
|
134
227
|
|
|
135
|
-
onRun?.({
|
|
228
|
+
onRun?.({
|
|
229
|
+
label,
|
|
230
|
+
workdir,
|
|
231
|
+
promptChars: prompt.length,
|
|
232
|
+
images: spilled.files.length,
|
|
233
|
+
skippedImages: parts.skipped,
|
|
234
|
+
})
|
|
136
235
|
|
|
137
236
|
const controller = new AbortController()
|
|
138
237
|
const forwardAbort = () => {
|
|
@@ -148,8 +247,13 @@ export function createCodexCtx(options = {}) {
|
|
|
148
247
|
stdio: ['pipe', 'pipe', 'pipe'],
|
|
149
248
|
signal: controller.signal,
|
|
150
249
|
windowsHide: true,
|
|
250
|
+
// PATH 里找到的 codex 在 Windows 上通常是 .cmd 包装脚本,
|
|
251
|
+
// 不带 shell 直接 spawn 会 ENOENT/EINVAL。只在需要时开 shell,
|
|
252
|
+
// 别给 .exe 也套一层——那会把参数里的引号再解释一遍。
|
|
253
|
+
shell: needsShell(bin),
|
|
151
254
|
})
|
|
152
255
|
} catch (error) {
|
|
256
|
+
cleanup(spilled.dir)
|
|
153
257
|
return failedActivation(`codex 起不来:${error?.message ?? error}`)
|
|
154
258
|
}
|
|
155
259
|
|
|
@@ -227,6 +331,7 @@ export function createCodexCtx(options = {}) {
|
|
|
227
331
|
for (const _ of openTools) watchdog?.toolEnd()
|
|
228
332
|
openTools.clear()
|
|
229
333
|
try { rl.close() } catch { /* 已经关了 */ }
|
|
334
|
+
cleanup(spilled.dir)
|
|
230
335
|
resolve({
|
|
231
336
|
stopReason,
|
|
232
337
|
diagnostic,
|
|
@@ -276,13 +381,20 @@ export function createCodexCtx(options = {}) {
|
|
|
276
381
|
dispose: async () => {
|
|
277
382
|
spec.signal?.removeEventListener?.('abort', forwardAbort)
|
|
278
383
|
const alive = () => child.exitCode === null && child.signalCode === null
|
|
279
|
-
if (!alive())
|
|
384
|
+
if (!alive()) {
|
|
385
|
+
cleanup(spilled.dir)
|
|
386
|
+
return
|
|
387
|
+
}
|
|
280
388
|
try { child.kill() } catch { /* 已经退了 */ }
|
|
281
389
|
// Windows 上 SIGTERM 不一定立刻生效,尤其对方是个不肯退的 node 进程。
|
|
282
390
|
// 等一小会儿还没死就硬杀——留在后台的 codex 会继续烧钱,那不是小事。
|
|
283
391
|
await new Promise(resolve => setTimeout(resolve, 150))
|
|
284
|
-
if (!alive())
|
|
392
|
+
if (!alive()) {
|
|
393
|
+
cleanup(spilled.dir)
|
|
394
|
+
return
|
|
395
|
+
}
|
|
285
396
|
try { child.kill('SIGKILL') } catch { /* 系统不支持就直接算了 */ }
|
|
397
|
+
cleanup(spilled.dir)
|
|
286
398
|
},
|
|
287
399
|
}
|
|
288
400
|
},
|
|
@@ -290,49 +402,186 @@ export function createCodexCtx(options = {}) {
|
|
|
290
402
|
}
|
|
291
403
|
}
|
|
292
404
|
|
|
293
|
-
/**
|
|
294
|
-
function
|
|
295
|
-
return
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
405
|
+
/** 删临时目录,失败当没事。 */
|
|
406
|
+
function cleanup(dir) {
|
|
407
|
+
if (typeof dir !== 'string' || dir === '') return
|
|
408
|
+
try { rmSync(dir, { recursive: true, force: true }) } catch { /* 删不掉留给系统清 */ }
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* 这个路径要不要用 shell 起。
|
|
413
|
+
*
|
|
414
|
+
* Windows 上 `codex.cmd` / `codex.bat` 是批处理包装,`spawn` 不带 shell 会
|
|
415
|
+
* EINVAL。但 `.exe` 千万不能加——加了之后参数里那些引号会被 cmd 再解释一遍,
|
|
416
|
+
* 中文路径和判据命令都会碎。
|
|
417
|
+
*/
|
|
418
|
+
function needsShell(bin) {
|
|
419
|
+
return process.platform === 'win32' && /\.(cmd|bat)$/iu.test(bin)
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function statOf(path) {
|
|
423
|
+
try { return statSync(path) } catch { return undefined }
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function isFile(path) {
|
|
427
|
+
const stat = statOf(path)
|
|
428
|
+
return stat !== undefined && stat.isFile()
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/** 目录名里抠 semver;抠不出返回 undefined(Codex 的 build 目录名是 hash)。 */
|
|
432
|
+
function versionOf(name) {
|
|
433
|
+
const matched = /(\d+)\.(\d+)\.(\d+)/u.exec(name)
|
|
434
|
+
if (matched === null) return undefined
|
|
435
|
+
return [Number(matched[1]), Number(matched[2]), Number(matched[3])]
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
function compareVersion(left, right) {
|
|
439
|
+
for (let index = 0; index < 3; index += 1) {
|
|
440
|
+
if (left[index] !== right[index]) return left[index] - right[index]
|
|
441
|
+
}
|
|
442
|
+
return 0
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* 从候选里挑最新的。
|
|
447
|
+
*
|
|
448
|
+
* 早先是 `found[found.length - 1]`——目录枚举顺序当版本顺序用。Windows 上
|
|
449
|
+
* `readdirSync` 的顺序既不是字母序也不是时间序,升级一次可能挑到旧的那个,
|
|
450
|
+
* 而且症状是「新功能没生效」,最难查的一种。
|
|
451
|
+
*
|
|
452
|
+
* 排序键三层,逐层比:
|
|
453
|
+
* 1. 目录名里能解析出版本号的优先(那是明确的版本,不是猜的)
|
|
454
|
+
* 2. 有版本号的之间按版本号降序
|
|
455
|
+
* 3. 都没有版本号时按 mtime 降序——Codex 的 `<build>` 是 hash,升级会换
|
|
456
|
+
* 一个新目录,新的那份 mtime 一定更新
|
|
457
|
+
*
|
|
458
|
+
* @param candidates [{ path, name, mtimeMs }]
|
|
459
|
+
*/
|
|
460
|
+
export function newestCandidate(candidates) {
|
|
461
|
+
if (candidates.length === 0) return ''
|
|
462
|
+
const scored = candidates.map(item => ({ ...item, version: versionOf(item.name) }))
|
|
463
|
+
scored.sort((left, right) => {
|
|
464
|
+
if ((left.version !== undefined) !== (right.version !== undefined)) {
|
|
465
|
+
return left.version !== undefined ? -1 : 1
|
|
466
|
+
}
|
|
467
|
+
if (left.version !== undefined && right.version !== undefined) {
|
|
468
|
+
const byVersion = compareVersion(right.version, left.version)
|
|
469
|
+
if (byVersion !== 0) return byVersion
|
|
470
|
+
}
|
|
471
|
+
return (right.mtimeMs ?? 0) - (left.mtimeMs ?? 0)
|
|
472
|
+
})
|
|
473
|
+
return scored[0].path
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/** 在 PATH 里找。Windows 上要连同 PATHEXT 一起试。 */
|
|
477
|
+
function findOnPath(env, names) {
|
|
478
|
+
const raw = env.PATH ?? env.Path ?? env.path ?? ''
|
|
479
|
+
if (typeof raw !== 'string' || raw === '') return ''
|
|
480
|
+
for (const dir of raw.split(delimiter)) {
|
|
481
|
+
if (dir.trim() === '') continue
|
|
482
|
+
for (const name of names) {
|
|
483
|
+
const candidate = join(dir.trim(), name)
|
|
484
|
+
if (isFile(candidate)) return candidate
|
|
485
|
+
}
|
|
299
486
|
}
|
|
487
|
+
return ''
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/** 各平台常见的安装位置。找不到 PATH 上那个时按这个兜底。 */
|
|
491
|
+
function knownDirs(env, home) {
|
|
492
|
+
if (process.platform === 'win32') {
|
|
493
|
+
const local = env.LOCALAPPDATA ?? (home === '' ? '' : join(home, 'AppData', 'Local'))
|
|
494
|
+
return local === '' ? [] : [join(local, 'OpenAI', 'Codex', 'bin')]
|
|
495
|
+
}
|
|
496
|
+
return [
|
|
497
|
+
home === '' ? '' : join(home, '.codex', 'bin'),
|
|
498
|
+
home === '' ? '' : join(home, '.local', 'bin'),
|
|
499
|
+
'/opt/homebrew/bin',
|
|
500
|
+
'/usr/local/bin',
|
|
501
|
+
].filter(dir => dir !== '')
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function binaryNames() {
|
|
505
|
+
return process.platform === 'win32'
|
|
506
|
+
? ['codex.exe', 'codex.cmd', 'codex.bat']
|
|
507
|
+
: ['codex']
|
|
300
508
|
}
|
|
301
509
|
|
|
302
510
|
/**
|
|
303
511
|
* 找本机的 codex 可执行文件。
|
|
304
512
|
*
|
|
305
|
-
*
|
|
513
|
+
* 顺序(前一个命中就返回,不再往下猜):
|
|
306
514
|
*
|
|
307
|
-
* 1. `CODEX_CLI_PATH` ——
|
|
308
|
-
* 2. `
|
|
309
|
-
*
|
|
310
|
-
*
|
|
515
|
+
* 1. `CODEX_CLI_PATH` —— 人明确指的那个,不猜
|
|
516
|
+
* 2. `PATH` 上的 `codex` —— 用户多半是用 `npm i -g` 或官方安装脚本装的,
|
|
517
|
+
* 这种装法本来就该能用,之前漏了它
|
|
518
|
+
* 3. `%LOCALAPPDATA%\OpenAI\Codex\bin\<build>\codex.exe` —— Codex 桌面版
|
|
519
|
+
* 自带的 CLI。`<build>` 是 hash,升级会换目录,所以按 mtime 取最新
|
|
520
|
+
* 4. macOS / Linux 的常见位置(`~/.codex/bin`、`~/.local/bin`、
|
|
521
|
+
* `/opt/homebrew/bin`、`/usr/local/bin`)
|
|
311
522
|
*
|
|
312
|
-
* 不去碰 `Program Files\WindowsApps
|
|
313
|
-
*
|
|
523
|
+
* 不去碰 `Program Files\WindowsApps\...`:那是桌面应用自带的副本,路径带
|
|
524
|
+
* 版本号还有 ACL 限制,拿来当 CLI 用是会踩坑的。
|
|
314
525
|
*
|
|
315
526
|
* @returns 找到的绝对路径;找不到返回空串,由调用方决定怎么报
|
|
316
527
|
*/
|
|
317
528
|
export function findCodexBinary(env = process.env) {
|
|
318
529
|
const home = env.USERPROFILE ?? env.HOME ?? ''
|
|
319
530
|
|
|
531
|
+
// ① 显式指定。相对路径按 HOME 展开,省得用户填一个只有自己 shell 认得的写法。
|
|
320
532
|
if (typeof env.CODEX_CLI_PATH === 'string' && env.CODEX_CLI_PATH !== '') {
|
|
533
|
+
const explicit = env.CODEX_CLI_PATH
|
|
534
|
+
const resolved = isAbsolute(explicit) || home === '' ? explicit : join(home, explicit)
|
|
535
|
+
const stat = statOf(resolved)
|
|
536
|
+
if (stat !== undefined && stat.isFile()) return resolved
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// ② PATH。这是最该优先的一条:从 npm / brew 装的 codex 就在这儿。
|
|
540
|
+
const fromPath = findOnPath(env, binaryNames())
|
|
541
|
+
if (fromPath !== '') return fromPath
|
|
542
|
+
|
|
543
|
+
// ③④ 已知安装目录。同一层目录下可能有多个 build,挑最新的。
|
|
544
|
+
const names = binaryNames()
|
|
545
|
+
const candidates = []
|
|
546
|
+
for (const dir of knownDirs(env, home)) {
|
|
547
|
+
let entries
|
|
321
548
|
try {
|
|
322
|
-
if (existsSync(
|
|
323
|
-
|
|
549
|
+
if (!existsSync(dir)) continue
|
|
550
|
+
entries = readdirSync(dir, { withFileTypes: true })
|
|
551
|
+
} catch {
|
|
552
|
+
// 目录读不动就当没装
|
|
553
|
+
continue
|
|
554
|
+
}
|
|
555
|
+
for (const entry of entries) {
|
|
556
|
+
// 两种布局:<dir>/codex.exe(直接装)和 <dir>/<build>/codex.exe(桌面版)
|
|
557
|
+
if (entry.isFile()) {
|
|
558
|
+
if (names.includes(entry.name)) {
|
|
559
|
+
const path = join(dir, entry.name)
|
|
560
|
+
const stat = statOf(path)
|
|
561
|
+
if (stat !== undefined) candidates.push({ path, name: entry.name, mtimeMs: stat.mtimeMs })
|
|
562
|
+
}
|
|
563
|
+
continue
|
|
564
|
+
}
|
|
565
|
+
if (!entry.isDirectory()) continue
|
|
566
|
+
for (const name of names) {
|
|
567
|
+
const path = join(dir, entry.name, name)
|
|
568
|
+
const stat = statOf(path)
|
|
569
|
+
if (stat !== undefined && stat.isFile()) {
|
|
570
|
+
candidates.push({ path, name: entry.name, mtimeMs: stat.mtimeMs })
|
|
571
|
+
break
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
}
|
|
324
575
|
}
|
|
325
576
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
// 目录读不动就当没装
|
|
577
|
+
return newestCandidate(candidates)
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
/** 一起手就失败(比如 prompt 空)时用的占位 activation。 */
|
|
581
|
+
function failedActivation(message) {
|
|
582
|
+
return {
|
|
583
|
+
kind: 'external',
|
|
584
|
+
result: Promise.resolve({ stopReason: 'error', diagnostic: message, output: [] }),
|
|
585
|
+
dispose: async () => {},
|
|
336
586
|
}
|
|
337
|
-
return ''
|
|
338
587
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-project-flow",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Delivery control for AI coding agents: every requirement gets a runnable check whose exit code decides the verdict, every write goes into a patch ledger so rollback can restore exactly that file, every round leaves a snapshot diff and a machine-readable completion evidence pack, and spend is capped before each call. Two paces: quick (one subagent) for small edits, safe (four gated stages) for long jobs.",
|
|
6
6
|
"type": "module",
|
|
@@ -100,13 +100,7 @@
|
|
|
100
100
|
}
|
|
101
101
|
},
|
|
102
102
|
"peerDependencies": {
|
|
103
|
-
"@deepseek-ai/schemastery": "*"
|
|
104
|
-
"@deepseek-ai/dsh-client-ui-conversation": "*"
|
|
105
|
-
},
|
|
106
|
-
"peerDependenciesMeta": {
|
|
107
|
-
"@deepseek-ai/dsh-client-ui-conversation": {
|
|
108
|
-
"optional": true
|
|
109
|
-
}
|
|
103
|
+
"@deepseek-ai/schemastery": "*"
|
|
110
104
|
},
|
|
111
105
|
"bin": {
|
|
112
106
|
"flow": "./bin/flow.mjs"
|
package/runner/pipeline.mjs
CHANGED
|
@@ -60,10 +60,20 @@ export function createPipeline(ctx) {
|
|
|
60
60
|
}
|
|
61
61
|
const healedNote = healed.length === 0 ? '' : `自愈记录:${healed.join(';')}`
|
|
62
62
|
let snapshotNote = ''
|
|
63
|
+
/**
|
|
64
|
+
* 断言汇总。给外部入口(bin/flow.mjs)用:那边要打印「判据 5/5 过」,
|
|
65
|
+
* 而 gate / assertions 都是第 4 阶的局部变量。
|
|
66
|
+
*
|
|
67
|
+
* 放在这一层由 craft 兜一下,所有返回点都带得上,不用一个个调用点去传。
|
|
68
|
+
* 没跑到第 4 阶就停的时候保持 null——那是「还没验」,不是「零条验过」,
|
|
69
|
+
* 两者在报告里必须长得不一样。
|
|
70
|
+
*/
|
|
71
|
+
let assertionSummary = null
|
|
63
72
|
const craft = patch => ({
|
|
64
73
|
project,
|
|
65
74
|
artifacts,
|
|
66
75
|
issues: [],
|
|
76
|
+
assertions: assertionSummary,
|
|
67
77
|
...patch,
|
|
68
78
|
note: [patch.note, healedNote, snapshotNote, ...routeNotes]
|
|
69
79
|
.filter(part => typeof part === 'string' && part !== '')
|
|
@@ -920,6 +930,26 @@ export function createPipeline(ctx) {
|
|
|
920
930
|
tailChars: settings.checkStdoutTailChars,
|
|
921
931
|
})
|
|
922
932
|
const gate = gateAssertions(assertions)
|
|
933
|
+
// 一条要求可能挂好几条 check,全过才算这条过;判不了的(unverified)
|
|
934
|
+
// 和根本没有 check 的(uncovered)都不算过。给外部入口一个能直接印
|
|
935
|
+
// 「要求 3/3」的数字,别让它拿 check 数冒充要求数。
|
|
936
|
+
const failedReqs = new Set()
|
|
937
|
+
for (const item of gate.failed) failedReqs.add(item.req)
|
|
938
|
+
for (const item of gate.unverified) failedReqs.add(item.req)
|
|
939
|
+
for (const req of gate.uncovered) failedReqs.add(req)
|
|
940
|
+
const passedReqs = new Set()
|
|
941
|
+
for (const item of gate.passed) if (!failedReqs.has(item.req)) passedReqs.add(item.req)
|
|
942
|
+
assertionSummary = {
|
|
943
|
+
verdict: gate.verdict,
|
|
944
|
+
inPlay: criteriaInPlay(assertions),
|
|
945
|
+
requirements: { total: gate.total, passed: passedReqs.size },
|
|
946
|
+
checks: {
|
|
947
|
+
total: gate.checked,
|
|
948
|
+
passed: gate.passed.length,
|
|
949
|
+
failed: gate.failed.length,
|
|
950
|
+
unverified: gate.unverified.length + gate.uncovered.length,
|
|
951
|
+
},
|
|
952
|
+
}
|
|
923
953
|
if (Array.isArray(assertions) && assertions.length > 0) {
|
|
924
954
|
await logLine(cwd, project, criteriaInPlay(assertions)
|
|
925
955
|
? `断言:${gate.verdict}|过 ${gate.passed.length} / 未过 ${gate.failed.length} / 判不了 ${gate.unverified.length + gate.uncovered.length}`
|
|
@@ -456,12 +456,12 @@ check('不在工作流目录时,浮层说清工作流到底在哪', () => {
|
|
|
456
456
|
ranBefore: false,
|
|
457
457
|
projects: [],
|
|
458
458
|
routes: undefined,
|
|
459
|
-
cwd: 'C:\\Users\\
|
|
460
|
-
wherePath: 'D
|
|
459
|
+
cwd: 'C:\\Users\\someone\\OneDrive\\docs\\default-workspace',
|
|
460
|
+
wherePath: 'D:\\某个盘\\工作流',
|
|
461
461
|
}, true)))
|
|
462
462
|
return {
|
|
463
463
|
ok: text.includes('不在工作流目录')
|
|
464
|
-
&& text.includes('工作流在:D
|
|
464
|
+
&& text.includes('工作流在:D:\\某个盘\\工作流')
|
|
465
465
|
&& !text.includes('当前工作区:')
|
|
466
466
|
&& !text.includes('停止第'),
|
|
467
467
|
detail: text,
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* codex 可执行文件的自动发现。
|
|
3
|
+
*
|
|
4
|
+
* 这块错起来特别安静:找到的是旧那个 build,功能就是没生效,没有任何报错。
|
|
5
|
+
* 早先是 `found[found.length - 1]`——把目录枚举顺序当版本顺序用,Windows 上
|
|
6
|
+
* 那个顺序既不是字母序也不是时间序,纯看文件系统心情。
|
|
7
|
+
*
|
|
8
|
+
* 这里用假目录树把每一层都钉死,不碰真机上的 codex。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { test } from 'node:test'
|
|
12
|
+
import assert from 'node:assert/strict'
|
|
13
|
+
import { mkdir, mkdtemp, rm, utimes, writeFile } from 'node:fs/promises'
|
|
14
|
+
import { tmpdir } from 'node:os'
|
|
15
|
+
import { dirname, join } from 'node:path'
|
|
16
|
+
import { pathToFileURL } from 'node:url'
|
|
17
|
+
import { sibling } from './paths.mjs'
|
|
18
|
+
|
|
19
|
+
const { findCodexBinary, newestCandidate } = await import(
|
|
20
|
+
pathToFileURL(sibling('host/codex.mjs')).href + `?t=${Date.now()}`
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
const BIN_NAME = process.platform === 'win32' ? 'codex.exe' : 'codex'
|
|
24
|
+
|
|
25
|
+
async function scratch(fn) {
|
|
26
|
+
const root = await mkdtemp(join(tmpdir(), 'pf-codex-find-'))
|
|
27
|
+
try {
|
|
28
|
+
await fn(root)
|
|
29
|
+
} finally {
|
|
30
|
+
await rm(root, { recursive: true, force: true, maxRetries: 6, retryDelay: 150 })
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** 造一个假 codex,mtime 往前挪 seconds 秒,用来排出「谁更新」。 */
|
|
35
|
+
async function touch(path, ageSeconds) {
|
|
36
|
+
await mkdir(dirname(path), { recursive: true })
|
|
37
|
+
await writeFile(path, '')
|
|
38
|
+
const when = new Date(Date.now() - ageSeconds * 1000)
|
|
39
|
+
await utimes(path, when, when)
|
|
40
|
+
return path
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 干净的 env。
|
|
45
|
+
*
|
|
46
|
+
* PATH 一定要清空:本机真装着 codex,留着 PATH 的话每一条都会先命中它,
|
|
47
|
+
* 测试就变成「验证本机装没装 codex」,跟被测逻辑没关系了。
|
|
48
|
+
*/
|
|
49
|
+
function envOf(root, extra = {}) {
|
|
50
|
+
return {
|
|
51
|
+
USERPROFILE: root,
|
|
52
|
+
HOME: root,
|
|
53
|
+
LOCALAPPDATA: join(root, 'AppData', 'Local'),
|
|
54
|
+
PATH: '',
|
|
55
|
+
...extra,
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function desktopBase(root) {
|
|
60
|
+
return join(root, 'AppData', 'Local', 'OpenAI', 'Codex', 'bin')
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
test('已知目录里有多个 build:取 mtime 最新的那个', async () => {
|
|
64
|
+
await scratch(async root => {
|
|
65
|
+
const base = desktopBase(root)
|
|
66
|
+
await touch(join(base, 'oldbuild', BIN_NAME), 3600)
|
|
67
|
+
const newest = await touch(join(base, 'newbuild', BIN_NAME), 60)
|
|
68
|
+
assert.equal(findCodexBinary(envOf(root)), newest)
|
|
69
|
+
})
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
test('目录名带版本号时按版本号比,不是按字符串也不是按时间', async () => {
|
|
73
|
+
await scratch(async root => {
|
|
74
|
+
const base = desktopBase(root)
|
|
75
|
+
// 1.9.0 更新(mtime 更近),但 1.10.0 版本更高。
|
|
76
|
+
// 按字符串比 '1.9.0' > '1.10.0',按时间比也是 1.9.0 赢——两条错路都在这儿。
|
|
77
|
+
await touch(join(base, '1.9.0', BIN_NAME), 10)
|
|
78
|
+
const higher = await touch(join(base, '1.10.0', BIN_NAME), 3600)
|
|
79
|
+
assert.equal(findCodexBinary(envOf(root)), higher)
|
|
80
|
+
})
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
test('CODEX_CLI_PATH 指哪打哪,压过 PATH 和 AppData', async () => {
|
|
84
|
+
await scratch(async root => {
|
|
85
|
+
const explicit = await touch(join(root, 'my-codex', BIN_NAME), 7200)
|
|
86
|
+
await touch(join(desktopBase(root), '9.9.9', BIN_NAME), 0)
|
|
87
|
+
await touch(join(root, 'npm-bin', BIN_NAME), 0)
|
|
88
|
+
const env = envOf(root, { CODEX_CLI_PATH: explicit, PATH: join(root, 'npm-bin') })
|
|
89
|
+
assert.equal(findCodexBinary(env), explicit)
|
|
90
|
+
})
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
test('CODEX_CLI_PATH 指到不存在的文件,当没配,继续往下找', async () => {
|
|
94
|
+
await scratch(async root => {
|
|
95
|
+
const fallback = await touch(join(desktopBase(root), '2.0.0', BIN_NAME), 0)
|
|
96
|
+
const env = envOf(root, { CODEX_CLI_PATH: join(root, 'nope', BIN_NAME) })
|
|
97
|
+
assert.equal(findCodexBinary(env), fallback)
|
|
98
|
+
})
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
test('PATH 上的 codex 优先于 AppData 里那份', async () => {
|
|
102
|
+
await scratch(async root => {
|
|
103
|
+
const onPath = await touch(join(root, 'npm-bin', BIN_NAME), 7200)
|
|
104
|
+
await touch(join(desktopBase(root), '9.9.9', BIN_NAME), 0)
|
|
105
|
+
const env = envOf(root, { PATH: [join(root, 'somewhere'), join(root, 'npm-bin')].join(process.platform === 'win32' ? ';' : ':') })
|
|
106
|
+
assert.equal(findCodexBinary(env), onPath)
|
|
107
|
+
})
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
test('哪儿都没有就给空串,不抛', async () => {
|
|
111
|
+
await scratch(async root => {
|
|
112
|
+
assert.equal(findCodexBinary(envOf(root)), '')
|
|
113
|
+
})
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
// ── 排序本身 ────────────────────────────────────────────
|
|
117
|
+
|
|
118
|
+
test('newestCandidate:空数组给空串', () => {
|
|
119
|
+
assert.equal(newestCandidate([]), '')
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
test('newestCandidate:有版本号的压过没版本号的', () => {
|
|
123
|
+
// build 目录是 hash 的时候 mtime 才可信;一旦有一个能读出真实版本,
|
|
124
|
+
// 那个版本是明确信息,比时间可靠。
|
|
125
|
+
const picked = newestCandidate([
|
|
126
|
+
{ path: 'hash', name: '45e749ac65b97f3a', mtimeMs: 9e12 },
|
|
127
|
+
{ path: 'versioned', name: '1.0.0', mtimeMs: 1 },
|
|
128
|
+
])
|
|
129
|
+
assert.equal(picked, 'versioned')
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
test('newestCandidate:都没有版本号时按 mtime 降序', () => {
|
|
133
|
+
const picked = newestCandidate([
|
|
134
|
+
{ path: 'a', name: 'alpha', mtimeMs: 100 },
|
|
135
|
+
{ path: 'b', name: 'beta', mtimeMs: 300 },
|
|
136
|
+
{ path: 'c', name: 'gamma', mtimeMs: 200 },
|
|
137
|
+
])
|
|
138
|
+
assert.equal(picked, 'b')
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
test('newestCandidate:版本号相同再看 mtime', () => {
|
|
142
|
+
const picked = newestCandidate([
|
|
143
|
+
{ path: 'a', name: '2.0.0', mtimeMs: 100 },
|
|
144
|
+
{ path: 'b', name: '2.0.0', mtimeMs: 900 },
|
|
145
|
+
])
|
|
146
|
+
assert.equal(picked, 'b')
|
|
147
|
+
})
|
package/tests/e2e.mjs
CHANGED
|
@@ -15,7 +15,7 @@ import { LIVE_DIR } from './paths.mjs'
|
|
|
15
15
|
|
|
16
16
|
const here = dirname(fileURLToPath(import.meta.url))
|
|
17
17
|
const LIVE = join(LIVE_DIR, 'index.js')
|
|
18
|
-
//
|
|
18
|
+
// 测试不读用户自己的运行时配置(~/.dsh/project-flow.json):
|
|
19
19
|
// 他在界面上设了哪三个模型是他的事,不该让这里的断言红得莫名其妙。
|
|
20
20
|
process.env.DSH_HOME = await mkdtemp(join(tmpdir(), 'pf-e2e-home-'))
|
|
21
21
|
const mod = await import(pathToFileURL(LIVE).href + `?t=${Date.now()}`)
|