dsh-project-flow 0.9.0 → 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.
Files changed (48) hide show
  1. package/CHANGELOG.md +0 -0
  2. package/README.md +58 -11
  3. package/cordis.patch.yml +30 -11
  4. package/docs/DETAIL.md +6 -6
  5. package/docs/FEATURES.md +12 -5
  6. package/docs/LAUNCH.md +3 -3
  7. package/docs/MOMENTS.md +4 -4
  8. package/docs/USAGE.md +2 -2
  9. package/domains.mjs +1 -1
  10. package/evidence.mjs +19 -3
  11. package/flow.schema.mjs +192 -35
  12. package/gate.mjs +49 -23
  13. package/index.js +639 -120
  14. package/locale/en.json +15 -4
  15. package/locale/zh.json +17 -6
  16. package/package.json +5 -3
  17. package/tests/_heartbeat-regression.test.mjs +124 -0
  18. package/tests/_wd_sandbox/.project-flow/demo/run.log +1 -0
  19. package/tests/_wd_sandbox/a.txt +1 -0
  20. package/tests/aesthetic.test.mjs +534 -0
  21. package/tests/assertions-e2e.mjs +234 -0
  22. package/tests/bench-check.mjs +32 -0
  23. package/tests/client-bundle.test.mjs +707 -0
  24. package/tests/contract.test.mjs +137 -0
  25. package/tests/desktop.test.mjs +130 -0
  26. package/tests/domains.test.mjs +220 -0
  27. package/tests/e2e.mjs +300 -0
  28. package/tests/evidence.test.mjs +408 -0
  29. package/tests/extra-phase.test.mjs +133 -0
  30. package/tests/feedback.test.mjs +187 -0
  31. package/tests/flow-create.test.mjs +168 -0
  32. package/tests/flow-gate.test.mjs +232 -0
  33. package/tests/flow-schema.test.mjs +213 -0
  34. package/tests/gate.test.mjs +246 -0
  35. package/tests/issue-loop.test.mjs +254 -0
  36. package/tests/issues-e2e.mjs +246 -0
  37. package/tests/panel-independence.test.mjs +78 -0
  38. package/tests/panel-report.test.mjs +129 -0
  39. package/tests/patch-ledger.test.mjs +280 -0
  40. package/tests/paths.mjs +94 -0
  41. package/tests/preview-path.test.mjs +69 -0
  42. package/tests/qa-assertions.test.mjs +352 -0
  43. package/tests/run-all.mjs +90 -0
  44. package/tests/stage-model.test.mjs +120 -0
  45. package/tests/stop-resume.test.mjs +260 -0
  46. package/tests/watch-root.test.mjs +167 -0
  47. package/tests/watchdog-tokens.test.mjs +49 -0
  48. package/tests/watchdog.test.mjs +301 -0
package/tests/e2e.mjs ADDED
@@ -0,0 +1,300 @@
1
+ /**
2
+ * 端到端:真跑一遍四阶流水,看证据包是不是真的落盘、字段是不是真的对。
3
+ *
4
+ * 前面那些套件验的是「函数对不对」,这个验的是「整条链路通不通」:
5
+ * apply → project_run → 三阶子会话(假)→ 质检 PASS → 跑测试命令 → 写 JSON。
6
+ *
7
+ * 用一个临时目录当工作区,一个假 subagents 服务,回放固定文本。
8
+ */
9
+ import { mkdtemp, mkdir, writeFile, readFile, rm, readdir } from 'node:fs/promises'
10
+ import { existsSync } from 'node:fs'
11
+ import { join, dirname } from 'node:path'
12
+ import { tmpdir } from 'node:os'
13
+ import { fileURLToPath, pathToFileURL } from 'node:url'
14
+
15
+ const here = dirname(fileURLToPath(import.meta.url))
16
+ const LIVE = 'C:/Users/ASUS/.dsh/profiles/desktop/node_modules/dsh-project-flow/index.js'
17
+ // 测试不读用户自己的运行时配置(C:\Users\ASUS\.dsh\project-flow.json):
18
+ // 他在界面上设了哪三个模型是他的事,不该让这里的断言红得莫名其妙。
19
+ process.env.DSH_HOME = await mkdtemp(join(tmpdir(), 'pf-e2e-home-'))
20
+ const mod = await import(pathToFileURL(LIVE).href + `?t=${Date.now()}`)
21
+
22
+ // ── 假 subagents:按 label 回放固定文本 ──────────────────
23
+
24
+ const calls = []
25
+ function fakeSubagents(script) {
26
+ return {
27
+ startActivation(options) {
28
+ // 记下这条 activation 到底带了没带模型覆盖:回退逻辑测的就是这个。
29
+ calls.push({ label: options.label, provider: options.provider, model: options.agentOptions?.model })
30
+ const reply = script(options)
31
+ return {
32
+ // reply 可以是字符串,也可以是 { text, before } —— before 在返回前执行,
33
+ // 用来模拟实施段真的动了文件。也可以是 { stopReason: 'error' },
34
+ // 用来模拟「模型不存在」那种起得来、但一起来就报错且一个字不吐的情况。
35
+ result: (async () => {
36
+ if (reply === undefined) return { stopReason: 'error', diagnostic: '脚本没给这一段' }
37
+ if (typeof reply === 'object' && reply.stopReason !== undefined) {
38
+ await reply.before?.()
39
+ // 故意不带 diagnostic:真实世界里模型不存在就是这个样子
40
+ return reply.stopReason === 'completed'
41
+ ? { stopReason: 'completed', output: [{ type: 'text', text: reply.text ?? '' }] }
42
+ : { stopReason: reply.stopReason }
43
+ }
44
+ const text = typeof reply === 'string' ? reply : reply.text
45
+ if (typeof reply === 'object') await reply.before?.()
46
+ return { stopReason: 'completed', output: [{ type: 'text', text }] }
47
+ })(),
48
+ dispose: async () => {},
49
+ }
50
+ },
51
+ }
52
+ }
53
+
54
+ // ── 假 ctx ────────────────────────────────────────────────
55
+
56
+ function fakeCtx(subagents) {
57
+ const tools = new Map()
58
+ const effects = []
59
+ const ctx = {
60
+ subagents,
61
+ tools: { register(tool) { tools.set(tool.name, tool); return () => tools.delete(tool.name) } },
62
+ systemPrompt: { section() { return () => {} }, getSectionOrder() { return 2800 } },
63
+ effect(fn) { effects.push(fn) },
64
+ inject(_names, fn) { fn({ get: () => undefined, effect: () => {} }) },
65
+ get() { return undefined },
66
+ on() { return () => {} },
67
+ logger: { info() {}, warn() {}, error() {} },
68
+ root: {},
69
+ }
70
+ return { ctx, tools, effects }
71
+ }
72
+
73
+ // ── 断言收集 ──────────────────────────────────────────────
74
+
75
+ const results = []
76
+ const check = (label, fn) => {
77
+ let ok = false
78
+ let detail = ''
79
+ try {
80
+ const out = fn()
81
+ ok = out === true || (out && out.ok === true)
82
+ detail = out && out.detail ? String(out.detail) : ''
83
+ } catch (error) {
84
+ ok = false
85
+ detail = error.message
86
+ }
87
+ results.push({ label, ok, detail })
88
+ }
89
+
90
+ const scriptFor = (opts = {}) => () => undefined // 不用
91
+
92
+ // ── 起一个完整的跑批 ──────────────────────────────────────
93
+
94
+ async function runOnce({ testCommand, qaExtra = '', files = [], cnyRate = undefined, routes = {} }) {
95
+ // 工作模式只在「工作流」目录里开,所以测试目录必须落在这样一段路径下面
96
+ const base = join(tmpdir(), '工作流')
97
+ await mkdir(base, { recursive: true })
98
+ const dir = await mkdtemp(join(base, 'pf-e2e-'))
99
+ await writeFile(join(dir, 'src.txt'), 'hello\n', 'utf8')
100
+
101
+ const planReply = [
102
+ '```json',
103
+ JSON.stringify({ plan: '把 src.txt 改成 hello world', buildTicket: '改 src.txt' }),
104
+ '```',
105
+ ].join('\n')
106
+ const buildReply = [
107
+ '```json',
108
+ JSON.stringify({ summary: '改了 src.txt', files: ['src.txt'] }),
109
+ '```',
110
+ ].join('\n')
111
+ const qaReply = [
112
+ '```json',
113
+ JSON.stringify({
114
+ verdict: 'PASS',
115
+ reasons: ['要求都做到了'],
116
+ acceptable_risks: ['没测并发场景,够用'],
117
+ }),
118
+ '```',
119
+ qaExtra,
120
+ ].join('\n')
121
+
122
+ const subagents = fakeSubagents(options => {
123
+ // 模拟「这个 profile 里没有这个模型」:起得来,但一起来就报错、一个字不吐
124
+ if (options.agentOptions?.model === 'ghost-model') return { stopReason: 'error' }
125
+ if (options.label.includes('规划')) return planReply
126
+ if (options.label.includes('实施')) {
127
+ // 真的改一下文件,否则快照比对拿不到任何变更,files_modified 永远是空的
128
+ return {
129
+ text: buildReply,
130
+ before: async () => {
131
+ await writeFile(join(dir, 'src.txt'), 'hello\nworld\n', 'utf8')
132
+ },
133
+ }
134
+ }
135
+ if (options.label.includes('质检')) return qaReply
136
+ return undefined
137
+ })
138
+
139
+ const { ctx, tools, effects } = fakeCtx(subagents)
140
+ // calls 是模块级的,多个场景会互相污染;只取这次跑出来的那一段
141
+ const callStart = calls.length
142
+ const config = {
143
+ ...(cnyRate === undefined ? {} : { cnyRate }),
144
+ ...routes,
145
+ testCommand: testCommand ?? '',
146
+ testTimeoutMs: 60000,
147
+ }
148
+ mod.apply(ctx, config)
149
+ for (const fn of effects) fn()
150
+
151
+ const runTool = tools.get('project_run')
152
+ if (runTool === undefined) throw new Error('project_run 没注册上')
153
+
154
+ const agent = { session: { header: { cwd: dir } } }
155
+ const value = await runTool.execute({
156
+ project: 'e2e-demo',
157
+ intent: '把 src.txt 改一下',
158
+ requirements: ['改成 hello world'],
159
+ intentConfidence: 96,
160
+ questionsAsked: 2,
161
+ blindspots: {
162
+ permissions: '只动这个临时目录,不碰别处',
163
+ concurrency: '单人单次运行,不会撞车',
164
+ edge_cases: '任何一步失败就停下等人看',
165
+ stack: '纯文本改动,不加任何依赖',
166
+ data_flow: '读 src.txt 再写回去',
167
+ },
168
+ assumptions: ['按默认编码 UTF-8'],
169
+ }, { agent, signal: undefined })
170
+
171
+ const stateDir = join(dir, '.project-flow', 'e2e-demo')
172
+ const evidencePath = join(stateDir, 'Completion_Evidence.json')
173
+ const evidence = existsSync(evidencePath)
174
+ ? JSON.parse(await readFile(evidencePath, 'utf8'))
175
+ : undefined
176
+ const files2 = await readdir(stateDir).catch(() => [])
177
+ return { dir, value, evidence, evidencePath, stateDir, files: files2, calls: calls.slice(callStart), tools }
178
+ }
179
+
180
+ // ── 场景 1:配了测试命令,且通过 ──────────────────────────
181
+
182
+ let r1
183
+ try {
184
+ r1 = await runOnce({ testCommand: 'exit 0' })
185
+ } catch (error) {
186
+ console.log('场景 1 直接抛了:', error.stack)
187
+ process.exit(1)
188
+ }
189
+
190
+ check('流水跑完并返回 passed', () => r1.value.outcome === 'passed')
191
+ check('证据包真的落盘了', () => existsSync(r1.evidencePath))
192
+ check('五个必填字段都在', () => {
193
+ const bag = ['requirement_met', 'cost_incurred', 'retry_count', 'files_modified', 'remaining_risks']
194
+ const miss = bag.filter(k => r1.evidence?.[k] === undefined)
195
+ return { ok: miss.length === 0, detail: miss.join(',') }
196
+ })
197
+ check('requirement_met 是 true(exit 0)', () => r1.evidence?.requirement_met === true)
198
+ check('exit_code 记成 0', () => r1.evidence?.requirement_source?.exit_code === 0)
199
+ check('remaining_risks 收进了质检给的风险', () =>
200
+ r1.evidence?.remaining_risks?.some(x => x.includes('并发')))
201
+ check('remaining_risks 也收进了 assumptions', () =>
202
+ r1.evidence?.remaining_risks?.some(x => x.includes('UTF-8')))
203
+ check('files_modified 里有 src.txt', () =>
204
+ r1.evidence?.files_modified?.some(f => f.path === 'src.txt'))
205
+ check('files_modified 的行数来自 git(不是 null)', () => {
206
+ const hit = r1.evidence?.files_modified?.find(f => f.path === 'src.txt')
207
+ return { ok: hit !== undefined && typeof hit.added_lines === 'number', detail: JSON.stringify(hit) }
208
+ })
209
+ check('context.started_at 不是 null', () => r1.evidence?.context?.started_at !== null)
210
+ check('cost_incurred 有 method 说明', () => typeof r1.evidence?.cost_incurred?.method === 'string')
211
+ check('返回值带了 evidence 路径', () => typeof r1.value.evidence === 'string' && r1.value.evidence !== '')
212
+ check('evidenceDigest 不是空', () => typeof r1.value.evidenceDigest === 'string' && r1.value.evidenceDigest !== '')
213
+
214
+ // ── 场景 2:测试命令返回非零 ──────────────────────────────
215
+
216
+ let r2
217
+ try {
218
+ r2 = await runOnce({ testCommand: 'exit 3' })
219
+ check('测试失败时 requirement_met 是 false', () => r2.evidence?.requirement_met === false)
220
+ check('测试失败时 exit_code 记成 3', () => r2.evidence?.requirement_source?.exit_code === 3)
221
+ check('测试失败不改写质检结论(仍 passed)', () => r2.value.outcome === 'passed')
222
+ } catch (error) {
223
+ check('场景 2 能跑完', () => ({ ok: false, detail: error.message }))
224
+ }
225
+
226
+ // ── 场景 3:没配测试命令 ──────────────────────────────────
227
+
228
+ let r3
229
+ try {
230
+ r3 = await runOnce({ testCommand: '' })
231
+ check('没配命令时 ran=false', () => r3.evidence?.requirement_source?.ran === false)
232
+ check('没配命令时 note 写明原因', () =>
233
+ /testCommand/u.test(r3.evidence?.requirement_source?.note ?? ''))
234
+ check('没配命令时 requirement_met 是 false', () => r3.evidence?.requirement_met === false)
235
+ } catch (error) {
236
+ check('场景 3 能跑完', () => ({ ok: false, detail: error.message }))
237
+ }
238
+
239
+ // ── 场景 4:测试命令根本不存在 ────────────────────────────
240
+
241
+ let r4
242
+ try {
243
+ r4 = await runOnce({ testCommand: 'definitely-not-a-real-command-xyz' })
244
+ check('命令不存在时 exit_code 不为 0', () => r4.evidence?.requirement_met === false)
245
+ check('命令不存在时 ran=true 且有 exit_code', () =>
246
+ r4.evidence?.requirement_source?.ran === true && r4.evidence?.requirement_source?.exit_code !== 0)
247
+ } catch (error) {
248
+ check('场景 4 能跑完', () => ({ ok: false, detail: error.message }))
249
+ }
250
+
251
+ // ── 场景 5:cnyRate 传小数 ────────────────────────────────
252
+
253
+ try {
254
+ const r5 = await runOnce({ testCommand: 'exit 0', cnyRate: 7.2 })
255
+ check('cnyRate=7.2 能被配置接受', () =>
256
+ r5.evidence?.cost_incurred?.cny_rate === 7.2)
257
+ } catch (error) {
258
+ check('cnyRate=7.2 能被配置接受', () => ({ ok: false, detail: error.message.slice(0, 200) }))
259
+ }
260
+
261
+ // ── 场景 6:配置里写了一个本机没有的模型 ──────────────────
262
+ //
263
+ // 这是最要命的一种坏法:不修就是「实施失败:error」,连查三天都查不出来。
264
+ // 期望的表现是自动改走主对话模型,把活干完,并且把换路这件事记下来。
265
+
266
+ let r6
267
+ try {
268
+ r6 = await runOnce({ testCommand: 'exit 0', routes: { buildModel: 'ghost-model' } })
269
+ check('模型不存在时自动改走主对话模型,流水不卡死', () => r6.value.outcome === 'passed')
270
+ check('回退之后那次子会话没再带模型覆盖(也就是继承主对话)', () => {
271
+ const build = r6.calls.filter(c => c.label.includes('实施'))
272
+ return {
273
+ ok: build.length >= 2 && build[0].model === 'ghost-model' && build[1].model === undefined,
274
+ detail: JSON.stringify(build),
275
+ }
276
+ })
277
+ check('换路这件事写进了返回值,不是悄悄吞掉', () =>
278
+ JSON.stringify(r6.value).includes('改走主对话模型'))
279
+ check('没配模型时子会话一律不带模型覆盖,直接继承主对话', () => {
280
+ const bad = r1.calls.filter(c => c.model !== undefined)
281
+ return { ok: bad.length === 0, detail: JSON.stringify(bad) }
282
+ })
283
+ } catch (error) {
284
+ check('场景 6 能跑完', () => ({ ok: false, detail: error.message }))
285
+ }
286
+
287
+ // ── 汇总 ──────────────────────────────────────────────────
288
+
289
+ let failed = 0
290
+ for (const r of results) {
291
+ console.log(`${r.ok ? '✔' : '✖'} ${r.label}${r.ok || r.detail === '' ? '' : ` → ${r.detail}`}`)
292
+ if (!r.ok) failed += 1
293
+ }
294
+ console.log(`\n${results.length - failed}/${results.length} 通过`)
295
+
296
+ await rm(r1.dir, { recursive: true, force: true }).catch(() => {})
297
+ for (const r of [r2, r3, r4, r6]) {
298
+ if (r !== undefined) await rm(r.dir, { recursive: true, force: true }).catch(() => {})
299
+ }
300
+ process.exit(failed === 0 ? 0 : 1)