dsh-project-flow 0.9.0 → 0.9.2

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 (46) 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/aesthetic.test.mjs +534 -0
  19. package/tests/assertions-e2e.mjs +234 -0
  20. package/tests/bench-check.mjs +32 -0
  21. package/tests/client-bundle.test.mjs +707 -0
  22. package/tests/contract.test.mjs +137 -0
  23. package/tests/desktop.test.mjs +130 -0
  24. package/tests/domains.test.mjs +220 -0
  25. package/tests/e2e.mjs +300 -0
  26. package/tests/evidence.test.mjs +408 -0
  27. package/tests/extra-phase.test.mjs +133 -0
  28. package/tests/feedback.test.mjs +187 -0
  29. package/tests/flow-create.test.mjs +168 -0
  30. package/tests/flow-gate.test.mjs +232 -0
  31. package/tests/flow-schema.test.mjs +213 -0
  32. package/tests/gate.test.mjs +246 -0
  33. package/tests/issue-loop.test.mjs +254 -0
  34. package/tests/issues-e2e.mjs +246 -0
  35. package/tests/panel-independence.test.mjs +78 -0
  36. package/tests/panel-report.test.mjs +129 -0
  37. package/tests/patch-ledger.test.mjs +280 -0
  38. package/tests/paths.mjs +100 -0
  39. package/tests/preview-path.test.mjs +69 -0
  40. package/tests/qa-assertions.test.mjs +352 -0
  41. package/tests/run-all.mjs +90 -0
  42. package/tests/stage-model.test.mjs +120 -0
  43. package/tests/stop-resume.test.mjs +260 -0
  44. package/tests/watch-root.test.mjs +167 -0
  45. package/tests/watchdog-tokens.test.mjs +53 -0
  46. package/tests/watchdog.test.mjs +305 -0
@@ -0,0 +1,234 @@
1
+ /**
2
+ * 断言执行器的端到端四场戏。
3
+ *
4
+ * 前面那套 qa-assertions.test.mjs 验的是函数;这里验的是「跑完整条流水之后,
5
+ * 落在磁盘上的东西对不对」——命令真的被执行了吗、报告里真的写了退出码吗、
6
+ * 中途改判据真的会被抓住吗。
7
+ *
8
+ * 用的是假 subagents(和 e2e.mjs 一个套路):三阶子会话回放固定文本,
9
+ * 但断言这一层是真的起进程、真的读写文件。
10
+ */
11
+ import { mkdtemp, mkdir, writeFile, readFile, rm } from 'node:fs/promises'
12
+ import { existsSync } from 'node:fs'
13
+ import { join, dirname } from 'node:path'
14
+ import { tmpdir } from 'node:os'
15
+ import { fileURLToPath, pathToFileURL } from 'node:url'
16
+
17
+ const here = dirname(fileURLToPath(import.meta.url))
18
+ const LIVE = 'C:/Users/ASUS/.dsh/profiles/desktop/node_modules/dsh-project-flow/index.js'
19
+ // 不吃用户自己的 project-flow.json:他界面里设了哪个模型是另一回事。
20
+ process.env.DSH_HOME = await mkdtemp(join(tmpdir(), 'pf-assert-home-'))
21
+ const mod = await import(pathToFileURL(LIVE).href + `?t=${Date.now()}`)
22
+
23
+ const NODE = JSON.stringify(process.execPath)
24
+
25
+ function fakeSubagents(script) {
26
+ return {
27
+ startActivation(options) {
28
+ const reply = script(options)
29
+ return {
30
+ result: (async () => {
31
+ if (reply === undefined) return { stopReason: 'error', diagnostic: '脚本没给这一段' }
32
+ if (typeof reply === 'object' && reply.stopReason !== undefined) {
33
+ await reply.before?.()
34
+ return { stopReason: reply.stopReason, output: [{ type: 'text', text: reply.text ?? '' }] }
35
+ }
36
+ const text = typeof reply === 'string' ? reply : reply.text
37
+ if (typeof reply === 'object') await reply.before?.()
38
+ return { stopReason: 'completed', output: [{ type: 'text', text }] }
39
+ })(),
40
+ dispose: async () => {},
41
+ }
42
+ },
43
+ }
44
+ }
45
+
46
+ function fakeCtx(subagents) {
47
+ const tools = new Map()
48
+ const effects = []
49
+ const ctx = {
50
+ subagents,
51
+ tools: { register(tool) { tools.set(tool.name, tool); return () => tools.delete(tool.name) } },
52
+ systemPrompt: { section() { return () => {} }, getSectionOrder() { return 2800 } },
53
+ effect(fn) { effects.push(fn) },
54
+ inject(_names, fn) { fn({ get: () => undefined, effect: () => {} }) },
55
+ get() { return undefined },
56
+ on() { return () => {} },
57
+ logger: { info() {}, warn() {}, error() {} },
58
+ root: {},
59
+ }
60
+ return { ctx, tools, effects }
61
+ }
62
+
63
+ const results = []
64
+ const check = (label, fn) => {
65
+ let ok = false
66
+ let detail = ''
67
+ try {
68
+ const out = fn()
69
+ ok = out === true || (out !== null && typeof out === 'object' && out.ok === true)
70
+ detail = out !== null && typeof out === 'object' && typeof out.detail === 'string' ? out.detail : ''
71
+ } catch (error) {
72
+ ok = false
73
+ detail = error.message
74
+ }
75
+ results.push({ label, ok, detail })
76
+ }
77
+
78
+ const planReply = ['```json', JSON.stringify({ plan: '把 src.txt 改成 hello world', buildTicket: '改 src.txt' }), '```'].join('\n')
79
+ const buildReply = ['```json', JSON.stringify({ summary: '改了 src.txt', files: ['src.txt'] }), '```'].join('\n')
80
+ // 质检一律说 PASS:这一层要证明的正是「模型说 PASS 也翻不了命令的案」。
81
+ const qaReply = ['```json', JSON.stringify({ verdict: 'PASS', reasons: ['看起来都做到了'], acceptable_risks: [] }), '```'].join('\n')
82
+
83
+ async function runOnce({ requirements, checks, onBuild }) {
84
+ const base = join(tmpdir(), '工作流')
85
+ await mkdir(base, { recursive: true })
86
+ const dir = await mkdtemp(join(base, 'pf-assert-'))
87
+ await writeFile(join(dir, 'src.txt'), 'hello\n', 'utf8')
88
+ const stateDir = join(dir, '.project-flow', 'assert-demo')
89
+
90
+ const subagents = fakeSubagents(options => {
91
+ if (options.label.includes('规划')) return planReply
92
+ if (options.label.includes('实施')) {
93
+ return {
94
+ text: buildReply,
95
+ before: async () => {
96
+ await writeFile(join(dir, 'src.txt'), 'hello\nworld\n', 'utf8')
97
+ if (onBuild !== undefined) await onBuild({ dir, stateDir })
98
+ },
99
+ }
100
+ }
101
+ if (options.label.includes('质检')) return qaReply
102
+ return undefined
103
+ })
104
+
105
+ const { ctx, tools, effects } = fakeCtx(subagents)
106
+ mod.apply(ctx, { testCommand: '', testTimeoutMs: 60000 })
107
+ for (const fn of effects) fn()
108
+
109
+ const runTool = tools.get('project_run')
110
+ if (runTool === undefined) throw new Error('project_run 没注册上')
111
+ const agent = { session: { header: { cwd: dir } } }
112
+ const value = await runTool.execute({
113
+ project: 'assert-demo',
114
+ intent: '把 src.txt 改一下,并且这件事要能被命令验',
115
+ requirements,
116
+ checks,
117
+ intentConfidence: 96,
118
+ questionsAsked: 2,
119
+ blindspots: {
120
+ permissions: '只动这个临时目录,不碰别处',
121
+ concurrency: '单人单次运行,不会撞车',
122
+ edge_cases: '任何一步失败就停下等人看',
123
+ stack: '纯文本改动,不加任何依赖',
124
+ data_flow: '读 src.txt 再写回去',
125
+ },
126
+ assumptions: [],
127
+ }, { agent, signal: undefined })
128
+
129
+ const read = async name => {
130
+ const path = join(stateDir, name)
131
+ return existsSync(path) ? readFile(path, 'utf8') : ''
132
+ }
133
+ return {
134
+ dir,
135
+ value,
136
+ stateDir,
137
+ requirement: await read('Requirement.md'),
138
+ report: await read('QA_Report.md'),
139
+ state: await read('state.json'),
140
+ fingerprint: await read('requirement.sha'),
141
+ }
142
+ }
143
+
144
+ // ── 一:判据没过,第 4 阶判 FAIL,报告里带 stdout ─────────────
145
+
146
+ const s1 = await runOnce({
147
+ requirements: [`src.txt 里要有 world\ncheck: ${NODE} -e "console.error('MISMATCH: 没有 world'); process.exit(1)"`],
148
+ })
149
+ check('一:判据没过 → 流水判 FAIL,模型说 PASS 也不算', () => ({
150
+ // 判 FAIL 之后会按正常流程返工,返工次数用完才停——这一步要验的只是
151
+ // 「模型说 PASS、命令说没过」的时候,最后不能是 passed。
152
+ ok: s1.value.outcome !== 'passed' && /FAIL/u.test(s1.value.verdict),
153
+ detail: `${s1.value.outcome}|${s1.value.verdict}`,
154
+ }))
155
+ check('一:Requirement.md 里真的写了 check 前置块', () => ({
156
+ ok: /^---\nchecks:/u.test(s1.requirement),
157
+ detail: s1.requirement.slice(0, 60),
158
+ }))
159
+ check('一:QA_Report.md 的 assertions 带 exit 和 stdout 尾巴', () => ({
160
+ ok: /assertions:/u.test(s1.report) && /exit: 1/u.test(s1.report) && /MISMATCH: 没有 world/u.test(s1.report),
161
+ detail: s1.report.slice(0, 200),
162
+ }))
163
+ check('一:报告里那条断言记成 fail', () => /status: fail/u.test(s1.report))
164
+ check('一:未过的那条进了 issues,第 3 阶才照着改', () =>
165
+ s1.value.issues.some(line => line.includes('第 1 条判据没过')))
166
+
167
+ // ── 二:一条有 check、一条没有 → 没那条记 unverified,不许算 PASS ──
168
+
169
+ const s2 = await runOnce({
170
+ requirements: [
171
+ `src.txt 要有 world\ncheck: ${NODE} -e "process.exit(0)"`,
172
+ '代码要好读',
173
+ ],
174
+ })
175
+ check('二:混着写时,没 check 的那条不许算 PASS(整轮 FAIL)', () => ({
176
+ ok: s2.value.outcome !== 'passed' && /FAIL/u.test(s2.value.verdict),
177
+ detail: `${s2.value.outcome}|${s2.value.verdict}`,
178
+ }))
179
+ check('二:报告里第 2 条记成 unverified,第 1 条记成 pass', () => ({
180
+ ok: /- req: 2[\s\S]{0,200}?status: unverified/u.test(s2.report) && /- req: 1[\s\S]{0,200}?status: pass/u.test(s2.report),
181
+ detail: s2.report.slice(0, 300),
182
+ }))
183
+ check('二:判不了的原因写进了 issues', () =>
184
+ s2.value.issues.some(line => line.includes('没有 check,判不了')))
185
+
186
+ // ── 三:中途改判据 → 第 4 阶停机报「判据被改」────────────────
187
+
188
+ const s3 = await runOnce({
189
+ requirements: [`src.txt 要有 world\ncheck: ${NODE} -e "process.exit(0)"`],
190
+ onBuild: async ({ stateDir }) => {
191
+ // 实施段动手脚:把 check 换成一个永远过的命令。指纹是第 2 阶记的,
192
+ // 第 4 阶复算就会发现对不上。
193
+ const path = join(stateDir, 'Requirement.md')
194
+ const text = await readFile(path, 'utf8')
195
+ await writeFile(path, text.replace(/cmd: .*/u, 'cmd: "node -e \\"process.exit(0)\\""'), 'utf8')
196
+ },
197
+ })
198
+ check('三:判据被改 → 停机,判词就是「判据被改」', () => ({
199
+ ok: s3.value.outcome === 'blocked' && s3.value.verdict === '判据被改',
200
+ detail: `${s3.value.outcome}|${s3.value.verdict}`,
201
+ }))
202
+ check('三:状态记成 criteria-tampered,不是「没通过」', () => ({
203
+ ok: /criteria-tampered/u.test(s3.state),
204
+ detail: s3.state.slice(0, 200),
205
+ }))
206
+ check('三:指纹确实变了(这就是判据)', () => s3.fingerprint.trim().length === 64)
207
+
208
+ // ── 四:白名单拦得住 ──────────────────────────────────────────
209
+
210
+ const s4 = await runOnce({
211
+ requirements: ['把目录清干净\ncheck: rm -rf /'],
212
+ })
213
+ check('四:rm -rf / 被白名单拒掉,不当命令跑', () => ({
214
+ ok: /status: denied/u.test(s4.report) && /白名单拒绝/u.test(s4.report),
215
+ detail: s4.report.slice(0, 300),
216
+ }))
217
+ check('四:被拒的命令也算 FAIL,不会偷偷算过', () => ({
218
+ ok: /denied: true/u.test(s4.report) && /FAIL/u.test(s4.value.verdict),
219
+ detail: s4.value.verdict,
220
+ }))
221
+ check('四:报告里写清了是哪条规则拦的', () => /不在白名单里/u.test(s4.report))
222
+
223
+ // ── 收尾 ──────────────────────────────────────────────────────
224
+
225
+ let failed = 0
226
+ for (const item of results) {
227
+ console.log(`${item.ok ? '✔' : '✖'} ${item.label}${item.ok || item.detail === '' ? '' : `\n → ${item.detail}`}`)
228
+ if (!item.ok) failed += 1
229
+ }
230
+ console.log(`\n${results.length - failed}/${results.length} 通过`)
231
+ for (const dir of [s1.dir, s2.dir, s3.dir, s4.dir]) {
232
+ await rm(dir, { recursive: true, force: true }).catch(() => {})
233
+ }
234
+ process.exit(failed === 0 ? 0 : 1)
@@ -0,0 +1,32 @@
1
+ /**
2
+ * 把插件的回归语料(bench/run.mjs)接进这一套。
3
+ *
4
+ * 为什么不在 run-all 里直接跑 bench/run.mjs:那个脚本在插件目录里,
5
+ * 而这套测试的 cwd 是 gate 目录。包一层,顺手把它的输出整段带出来——
6
+ * 挂了的时候要看见是哪条语料变了,不是一个光秃秃的退出码。
7
+ */
8
+ import { spawn } from 'node:child_process'
9
+ import { existsSync } from 'node:fs'
10
+ import { join } from 'node:path'
11
+ import { LIVE_DIR } from './paths.mjs'
12
+
13
+ const bench = join(LIVE_DIR, 'bench', 'run.mjs')
14
+ if (!existsSync(bench)) {
15
+ console.log(`找不到 ${bench}`)
16
+ console.log('回归语料要在插件目录里:bench/run.mjs + bench/corpus/')
17
+ process.exit(1)
18
+ }
19
+
20
+ const child = spawn(process.execPath, [bench], {
21
+ cwd: LIVE_DIR,
22
+ stdio: ['ignore', 'pipe', 'pipe'],
23
+ env: { ...process.env, PF_LIVE_DIR: LIVE_DIR },
24
+ })
25
+
26
+ let text = ''
27
+ child.stdout.on('data', chunk => { text += chunk })
28
+ child.stderr.on('data', chunk => { text += chunk })
29
+ child.on('close', code => {
30
+ console.log(text.trimEnd())
31
+ process.exit(code === 0 ? 0 : 1)
32
+ })