dsh-project-flow 0.11.0 → 0.13.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/CHANGELOG.md +121 -0
- package/README.md +60 -8
- package/bin/flow.mjs +266 -0
- package/config/settings.mjs +18 -0
- package/cordis.patch.yml +7 -0
- package/host/codex.mjs +338 -0
- package/package.json +9 -3
- package/runner/pipeline.mjs +94 -28
- package/shared/constants.mjs +14 -0
- package/tests/codex-adapter.test.mjs +139 -0
- package/tests/delivery-mode.test.mjs +94 -0
- package/tests/fixtures/fake-codex.mjs +49 -0
- package/tests/run-all.mjs +2 -0
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Codex 适配器:把 `codex exec` 包成假 ctx。
|
|
3
|
+
*
|
|
4
|
+
* 这一层的价值在于「pipeline 一行都不用改就能换个宿主」。所以测的也是这件事:
|
|
5
|
+
* 事件解不解得对、工具边界配不配得上、退出码翻不翻得成 stopReason。
|
|
6
|
+
*
|
|
7
|
+
* 用假 codex(tests/fixtures/fake-codex.mjs)跑,不花钱、不联网。
|
|
8
|
+
* 真机那一条已经在 D:\flow-codex-test 上跑通过了,那个不用天天跑。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { test } from 'node:test'
|
|
12
|
+
import { sibling } from './paths.mjs'
|
|
13
|
+
import { pathToFileURL } from 'node:url'
|
|
14
|
+
import assert from 'node:assert/strict'
|
|
15
|
+
import { mkdtemp, rm } from 'node:fs/promises'
|
|
16
|
+
import { tmpdir } from 'node:os'
|
|
17
|
+
import { join } from 'node:path'
|
|
18
|
+
|
|
19
|
+
const { createCodexCtx, findCodexBinary } = await import(
|
|
20
|
+
pathToFileURL(sibling('host/codex.mjs')).href + `?t=${Date.now()}`
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
const FAKE = sibling('tests/fixtures/fake-codex.mjs')
|
|
24
|
+
const NODE = process.execPath
|
|
25
|
+
|
|
26
|
+
/** 用假 codex 造一个 ctx。剧本走环境变量,用完还原。 */
|
|
27
|
+
async function withFake(script, fn) {
|
|
28
|
+
const cwd = await mkdtemp(join(tmpdir(), 'flow-codex-'))
|
|
29
|
+
const previous = process.env.FAKE_CODEX_SCRIPT
|
|
30
|
+
process.env.FAKE_CODEX_SCRIPT = script
|
|
31
|
+
const ctx = createCodexCtx({ bin: NODE, binArgs: [FAKE], cwd, model: 'fake-model', sandbox: 'read-only' })
|
|
32
|
+
try {
|
|
33
|
+
await fn(ctx, cwd)
|
|
34
|
+
} finally {
|
|
35
|
+
if (previous === undefined) delete process.env.FAKE_CODEX_SCRIPT
|
|
36
|
+
else process.env.FAKE_CODEX_SCRIPT = previous
|
|
37
|
+
// 子进程刚被杀掉时 Windows 还会占一小会儿目录,直接 rm 会 EBUSY。
|
|
38
|
+
// 这跟适配器无关,是文件系统的结算延迟。
|
|
39
|
+
await rm(cwd, { recursive: true, force: true, maxRetries: 6, retryDelay: 150 })
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
test('正常一轮:拿到文本、收到 usage', async () => {
|
|
44
|
+
await withFake('ok', async (ctx, cwd) => {
|
|
45
|
+
const activation = await ctx.subagents.startActivation({
|
|
46
|
+
label: '测试阶',
|
|
47
|
+
prompt: [{ type: 'text', text: '跑一下' }],
|
|
48
|
+
cwd,
|
|
49
|
+
})
|
|
50
|
+
assert.equal(activation.kind, 'external', '必须报 external:本地会话那套事件监听在这里没有对象可挂')
|
|
51
|
+
const result = await activation.result
|
|
52
|
+
assert.equal(result.stopReason, 'completed')
|
|
53
|
+
// output 必须是块数组。给字符串的话 textOf 会静默返回空串,而且不报错。
|
|
54
|
+
assert.ok(Array.isArray(result.output), 'output 要是内容块数组')
|
|
55
|
+
assert.match(result.output[0].text, /hello\.txt/u)
|
|
56
|
+
const usage = ctx.usage()
|
|
57
|
+
assert.equal(usage.turns, 1)
|
|
58
|
+
assert.equal(usage.input, 1000)
|
|
59
|
+
assert.equal(usage.output, 50)
|
|
60
|
+
assert.equal(usage.cached, 200)
|
|
61
|
+
})
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
test('命令执行的前后会被报成工具开始 / 结束', async () => {
|
|
65
|
+
await withFake('ok', async (ctx, cwd) => {
|
|
66
|
+
const events = []
|
|
67
|
+
const watchdog = {
|
|
68
|
+
touch: () => events.push('touch'),
|
|
69
|
+
toolStart: label => events.push(`start:${label}`),
|
|
70
|
+
toolEnd: () => events.push('end'),
|
|
71
|
+
}
|
|
72
|
+
const ctx2 = createCodexCtx({ bin: NODE, binArgs: [FAKE], cwd, watchdog })
|
|
73
|
+
const activation = await ctx2.subagents.startActivation({ label: 'x', prompt: '跑一下', cwd })
|
|
74
|
+
await activation.result
|
|
75
|
+
|
|
76
|
+
assert.ok(events.includes('start:echo hi'), `要报出工具开始并带上命令,实际:${events.join(',')}`)
|
|
77
|
+
// start 必须排在 end 前面,配不上就是泄漏或者永久 busy
|
|
78
|
+
const startAt = events.indexOf('start:echo hi')
|
|
79
|
+
const endAt = events.indexOf('end')
|
|
80
|
+
assert.ok(startAt >= 0 && endAt > startAt, `start/end 顺序不对:${events.join(',')}`)
|
|
81
|
+
assert.ok(events.filter(e => e === 'touch').length >= 3, '每个事件都该顶一下心跳')
|
|
82
|
+
void ctx
|
|
83
|
+
})
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
test('没跑到 turn.completed 要判失败,不能当成功', async () => {
|
|
87
|
+
await withFake('noturn', async (ctx, cwd) => {
|
|
88
|
+
const activation = await ctx.subagents.startActivation({ label: 'x', prompt: '跑一下', cwd })
|
|
89
|
+
const result = await activation.result
|
|
90
|
+
assert.equal(result.stopReason, 'error', '断了半截还给 completed,等于把没干完的活当成功')
|
|
91
|
+
assert.match(result.diagnostic, /turn\.completed/u, '要说清是没跑到收尾,而不是笼统一句失败')
|
|
92
|
+
})
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
test('退出码非零要把 stderr 带出来', async () => {
|
|
96
|
+
await withFake('fail', async (ctx, cwd) => {
|
|
97
|
+
const activation = await ctx.subagents.startActivation({ label: '实施', prompt: '跑一下', cwd })
|
|
98
|
+
const result = await activation.result
|
|
99
|
+
assert.equal(result.stopReason, 'error')
|
|
100
|
+
assert.match(result.diagnostic, /退出码 1/u)
|
|
101
|
+
assert.match(result.diagnostic, /something went wrong/u, '不带 stderr 的话用户只能盯着一个退出码猜')
|
|
102
|
+
})
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
test('取消能真的把子进程收掉', async () => {
|
|
106
|
+
await withFake('hang', async (ctx, cwd) => {
|
|
107
|
+
const controller = new AbortController()
|
|
108
|
+
const activation = await ctx.subagents.startActivation({
|
|
109
|
+
label: 'x',
|
|
110
|
+
prompt: '跑一下',
|
|
111
|
+
cwd,
|
|
112
|
+
signal: controller.signal,
|
|
113
|
+
})
|
|
114
|
+
setTimeout(() => controller.abort(), 300)
|
|
115
|
+
const result = await activation.result
|
|
116
|
+
assert.equal(result.stopReason, 'error')
|
|
117
|
+
assert.match(result.diagnostic, /已取消/u)
|
|
118
|
+
await activation.dispose()
|
|
119
|
+
})
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
test('prompt 是空的时候不许起空会话', async () => {
|
|
123
|
+
await withFake('ok', async (ctx, cwd) => {
|
|
124
|
+
const activation = await ctx.subagents.startActivation({ label: 'x', prompt: [], cwd })
|
|
125
|
+
const result = await activation.result
|
|
126
|
+
assert.equal(result.stopReason, 'error')
|
|
127
|
+
assert.match(result.diagnostic, /prompt 是空的/u, '空 prompt 起 codex 会白烧一次调用')
|
|
128
|
+
})
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
test('findCodexBinary 找不到时给空串,不抛', () => {
|
|
132
|
+
const got = findCodexBinary({ USERPROFILE: 'Z:\\definitely-not-here', HOME: 'Z:\\definitely-not-here' })
|
|
133
|
+
assert.equal(got, '')
|
|
134
|
+
assert.equal(typeof got, 'string')
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
test('createCodexCtx 缺 bin 要当场报错', () => {
|
|
138
|
+
assert.throws(() => createCodexCtx({}), /codex 可执行文件路径/u)
|
|
139
|
+
})
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 交付模式:safe 走完整四阶,quick 跳过两次子会话。
|
|
3
|
+
*
|
|
4
|
+
* 这个测试只钉一件事:模式解析不能把方向弄反。
|
|
5
|
+
*
|
|
6
|
+
* 写错一个字母就当 safe,是刻意的——反过来(认不出来当 quick)会让用户
|
|
7
|
+
* 以为自己在跑完整流程,其实质检那一步被跳过了。那种错不会报错,只会
|
|
8
|
+
* 在出事那天才被发现。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { test } from 'node:test'
|
|
12
|
+
import { sibling } from './paths.mjs'
|
|
13
|
+
import { pathToFileURL } from 'node:url'
|
|
14
|
+
import assert from 'node:assert/strict'
|
|
15
|
+
import { mkdtemp, rm, writeFile } from 'node:fs/promises'
|
|
16
|
+
import { tmpdir } from 'node:os'
|
|
17
|
+
import { join } from 'node:path'
|
|
18
|
+
|
|
19
|
+
const sleep = () => undefined
|
|
20
|
+
|
|
21
|
+
// settingsOf 会把 %DSH_HOME%/project-flow.json 压在 patch 值上面,
|
|
22
|
+
// 所以每个用例都要一个干净的 DSH_HOME,否则读的是用户的真配置。
|
|
23
|
+
const home = await mkdtemp(join(tmpdir(), 'flow-delivery-'))
|
|
24
|
+
process.env.DSH_HOME = home
|
|
25
|
+
|
|
26
|
+
const { configure, settingsOf, normalizeDeliveryMode } = await import(
|
|
27
|
+
pathToFileURL(sibling('config/settings.mjs')).href + `?t=${Date.now()}`
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
const overrideFile = join(home, 'project-flow.json')
|
|
31
|
+
|
|
32
|
+
async function withOverride(value, fn) {
|
|
33
|
+
await writeFile(overrideFile, JSON.stringify(value), 'utf8')
|
|
34
|
+
try {
|
|
35
|
+
configure({})
|
|
36
|
+
await fn(settingsOf())
|
|
37
|
+
} finally {
|
|
38
|
+
await rm(overrideFile, { force: true })
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
test('默认是 safe——不配就是完整四阶', () => {
|
|
43
|
+
configure({})
|
|
44
|
+
assert.equal(settingsOf().deliveryMode, 'safe')
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
test('配了 quick 就是 quick', async () => {
|
|
48
|
+
await withOverride({ deliveryMode: 'quick' }, settings => {
|
|
49
|
+
assert.equal(settings.deliveryMode, 'quick')
|
|
50
|
+
assert.equal(normalizeDeliveryMode(settings.deliveryMode), 'quick')
|
|
51
|
+
})
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
test('大小写和空格都吃掉', () => {
|
|
55
|
+
for (const raw of ['quick', 'QUICK', 'Quick', ' quick ', '\tquick\n']) {
|
|
56
|
+
assert.equal(normalizeDeliveryMode(raw), 'quick', `${JSON.stringify(raw)} 应该认成 quick`)
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
test('写错了退回 safe,而不是悄悄变 quick', () => {
|
|
61
|
+
for (const raw of ['fast', 'quickmode', 'quick-', 'safe ', 'SAFE', '', ' ', undefined, null, 0, 123, true, {}, []]) {
|
|
62
|
+
assert.equal(normalizeDeliveryMode(raw), 'safe', `${JSON.stringify(raw)} 必须退回 safe`)
|
|
63
|
+
}
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test('配置文件里写错值也只得到 safe——手改配置最容易出这种事', async () => {
|
|
67
|
+
for (const bad of ['fast', 'Quickmode', 'on', 'true', 1, null]) {
|
|
68
|
+
await withOverride({ deliveryMode: bad }, settings => {
|
|
69
|
+
assert.equal(
|
|
70
|
+
normalizeDeliveryMode(settings.deliveryMode),
|
|
71
|
+
'safe',
|
|
72
|
+
`配置写 ${JSON.stringify(bad)} 却拿到了 ${settings.deliveryMode},方向反了`,
|
|
73
|
+
)
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
test('配置文件里写 Quick(大写 Q)仍然认成 quick', async () => {
|
|
79
|
+
await withOverride({ deliveryMode: 'Quick' }, settings => {
|
|
80
|
+
// settingsOf 的字符串合并是原样透传,所以这里拿到的是 'Quick';
|
|
81
|
+
// 真正生效靠 pipeline 里再过一遍 normalizeDeliveryMode。
|
|
82
|
+
assert.equal(normalizeDeliveryMode(settings.deliveryMode), 'quick')
|
|
83
|
+
})
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
test('配了 safe 就是 safe', async () => {
|
|
87
|
+
await withOverride({ deliveryMode: 'safe' }, settings => {
|
|
88
|
+
assert.equal(normalizeDeliveryMode(settings.deliveryMode), 'safe')
|
|
89
|
+
})
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
// 收尾:把这个用例临时改过的环境变量还回去,别影响同进程别的测试
|
|
93
|
+
process.on('exit', () => { void rm(home, { recursive: true, force: true }) })
|
|
94
|
+
void sleep
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 假的 codex:输出一串预设的 JSONL,用来测解析链。
|
|
3
|
+
*
|
|
4
|
+
* 真 codex 要花钱、要联网、要几十秒。而适配器里真正容易错的是
|
|
5
|
+
* 「事件怎么解、工具边界怎么配对、退出码怎么翻成 stopReason」——这些
|
|
6
|
+
* 只要事件流一样就能验,不必真调模型。
|
|
7
|
+
*
|
|
8
|
+
* 剧本从环境变量 FAKE_CODEX_SCRIPT 读,取值:
|
|
9
|
+
* ok 正常一轮,带一次命令执行
|
|
10
|
+
* noturn 没有 turn.completed(模拟中途断线)
|
|
11
|
+
* fail 退出码 1
|
|
12
|
+
* hang 一直不说话(测超时/取消)
|
|
13
|
+
*/
|
|
14
|
+
const mode = process.env.FAKE_CODEX_SCRIPT ?? 'ok'
|
|
15
|
+
|
|
16
|
+
function emit(obj) {
|
|
17
|
+
process.stdout.write(`${JSON.stringify(obj)}\n`)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
emit({ type: 'thread.started', thread_id: 'fake-thread' })
|
|
21
|
+
emit({ type: 'turn.started' })
|
|
22
|
+
|
|
23
|
+
if (mode === 'ok') {
|
|
24
|
+
emit({ type: 'item.completed', item: { id: 'i0', type: 'agent_message', text: '我来看一下工作目录。' } })
|
|
25
|
+
emit({
|
|
26
|
+
type: 'item.started',
|
|
27
|
+
item: { id: 'i1', type: 'command_execution', command: 'echo hi', aggregated_output: '', exit_code: null, status: 'in_progress' },
|
|
28
|
+
})
|
|
29
|
+
// 故意中间停一会儿:真 codex 跑命令就是这个节奏,这里要验的是
|
|
30
|
+
// 「命令已经 started、还没 completed」这段时间能不能被认出来。
|
|
31
|
+
setTimeout(() => {
|
|
32
|
+
emit({
|
|
33
|
+
type: 'item.completed',
|
|
34
|
+
item: { id: 'i1', type: 'command_execution', command: 'echo hi', aggregated_output: 'hi\n', exit_code: 0, status: 'completed' },
|
|
35
|
+
})
|
|
36
|
+
emit({ type: 'item.completed', item: { id: 'i2', type: 'agent_message', text: '做完了,hello.txt 里写的是 ok。' } })
|
|
37
|
+
emit({ type: 'turn.completed', usage: { input_tokens: 1000, output_tokens: 50, cached_input_tokens: 200 } })
|
|
38
|
+
process.exit(0)
|
|
39
|
+
}, 120)
|
|
40
|
+
} else if (mode === 'noturn') {
|
|
41
|
+
emit({ type: 'item.completed', item: { id: 'i0', type: 'agent_message', text: '说到一半' } })
|
|
42
|
+
process.exit(0)
|
|
43
|
+
} else if (mode === 'fail') {
|
|
44
|
+
process.stderr.write('something went wrong\n')
|
|
45
|
+
process.exit(1)
|
|
46
|
+
} else if (mode === 'hang') {
|
|
47
|
+
// 什么都不发,等被杀
|
|
48
|
+
setInterval(() => {}, 1000)
|
|
49
|
+
}
|
package/tests/run-all.mjs
CHANGED
|
@@ -32,6 +32,8 @@ const SUITES = [
|
|
|
32
32
|
['停止与继续', 'stop-resume.test.mjs'],
|
|
33
33
|
['命令把话送到模型', 'steer-command.test.mjs'],
|
|
34
34
|
['工作流目录门控', 'flow-gate.test.mjs'],
|
|
35
|
+
['交付模式', 'delivery-mode.test.mjs'],
|
|
36
|
+
['Codex 适配器', 'codex-adapter.test.mjs'],
|
|
35
37
|
['状态栏看哪个目录', 'watch-root.test.mjs'],
|
|
36
38
|
['质检读预览图的路径', 'preview-path.test.mjs'],
|
|
37
39
|
['diff / 停机说明 / 盲区记忆', 'feedback.test.mjs'],
|