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.
- package/CHANGELOG.md +0 -0
- package/README.md +58 -11
- package/cordis.patch.yml +30 -11
- package/docs/DETAIL.md +6 -6
- package/docs/FEATURES.md +12 -5
- package/docs/LAUNCH.md +3 -3
- package/docs/MOMENTS.md +4 -4
- package/docs/USAGE.md +2 -2
- package/domains.mjs +1 -1
- package/evidence.mjs +19 -3
- package/flow.schema.mjs +192 -35
- package/gate.mjs +49 -23
- package/index.js +639 -120
- package/locale/en.json +15 -4
- package/locale/zh.json +17 -6
- package/package.json +5 -3
- package/tests/_heartbeat-regression.test.mjs +124 -0
- package/tests/aesthetic.test.mjs +534 -0
- package/tests/assertions-e2e.mjs +234 -0
- package/tests/bench-check.mjs +32 -0
- package/tests/client-bundle.test.mjs +707 -0
- package/tests/contract.test.mjs +137 -0
- package/tests/desktop.test.mjs +130 -0
- package/tests/domains.test.mjs +220 -0
- package/tests/e2e.mjs +300 -0
- package/tests/evidence.test.mjs +408 -0
- package/tests/extra-phase.test.mjs +133 -0
- package/tests/feedback.test.mjs +187 -0
- package/tests/flow-create.test.mjs +168 -0
- package/tests/flow-gate.test.mjs +232 -0
- package/tests/flow-schema.test.mjs +213 -0
- package/tests/gate.test.mjs +246 -0
- package/tests/issue-loop.test.mjs +254 -0
- package/tests/issues-e2e.mjs +246 -0
- package/tests/panel-independence.test.mjs +78 -0
- package/tests/panel-report.test.mjs +129 -0
- package/tests/patch-ledger.test.mjs +280 -0
- package/tests/paths.mjs +100 -0
- package/tests/preview-path.test.mjs +69 -0
- package/tests/qa-assertions.test.mjs +352 -0
- package/tests/run-all.mjs +90 -0
- package/tests/stage-model.test.mjs +120 -0
- package/tests/stop-resume.test.mjs +260 -0
- package/tests/watch-root.test.mjs +167 -0
- package/tests/watchdog-tokens.test.mjs +53 -0
- package/tests/watchdog.test.mjs +305 -0
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import { test } from 'node:test'
|
|
2
|
+
import { pathToFileURL } from 'node:url'
|
|
3
|
+
import assert from 'node:assert/strict'
|
|
4
|
+
import { mkdir, writeFile, rm, utimes } from 'node:fs/promises'
|
|
5
|
+
import { join, dirname } from 'node:path'
|
|
6
|
+
import { tmpdir } from 'node:os'
|
|
7
|
+
import { fileURLToPath } from 'node:url'
|
|
8
|
+
const {
|
|
9
|
+
fingerprint,
|
|
10
|
+
RepeatBreaker,
|
|
11
|
+
BudgetBreaker,
|
|
12
|
+
Watchdog,
|
|
13
|
+
WatchdogFault,
|
|
14
|
+
FAULT,
|
|
15
|
+
} = await import(pathToFileURL(sibling('watchdog.mjs')).href + `?t=${Date.now()}`)
|
|
16
|
+
// 上面这条相对引用容易踩坑:它指向 gate 目录里的副本,插件升级后副本就过期了,
|
|
17
|
+
// 于是「拿旧副本验新结论」——套件全绿、线上是坏的。paths.mjs 里记过这个教训。
|
|
18
|
+
// 下面这条断言把它钉住:被验的模块必须就是装好的那一份。
|
|
19
|
+
import { sibling } from './paths.mjs'
|
|
20
|
+
import { readFileSync } from 'node:fs'
|
|
21
|
+
import { fileURLToPath as _f } from 'node:url'
|
|
22
|
+
const _gateWatchdog = readFileSync(sibling('watchdog.mjs'), 'utf8')
|
|
23
|
+
const _liveWatchdog = readFileSync(sibling('watchdog.mjs'), 'utf8')
|
|
24
|
+
if (_gateWatchdog !== _liveWatchdog) {
|
|
25
|
+
throw new Error('gate/watchdog.mjs 和装好的插件不一致:这份测试验的是过期副本,请先同步再跑')
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const here = dirname(fileURLToPath(import.meta.url))
|
|
29
|
+
// 沙箱必须建在系统临时目录:这里以前是 join(here, '_wd_sandbox'),
|
|
30
|
+
// 而 tests/ 会随包发布——跑一次测试就把 .project-flow/demo/run.log 这类
|
|
31
|
+
// 运行时产物留在包里,下次 npm publish 一起发出去。
|
|
32
|
+
const sandbox = join(tmpdir(), 'pf-wd-sandbox')
|
|
33
|
+
|
|
34
|
+
// ── 指纹 ────────────────────────────────────────────
|
|
35
|
+
|
|
36
|
+
test('同一个错换个行号还是同一个指纹', () => {
|
|
37
|
+
const a = fingerprint('Error: failed at src/app.js:41:12')
|
|
38
|
+
const b = fingerprint('Error: failed at src/app.js:88:3')
|
|
39
|
+
assert.equal(a, b)
|
|
40
|
+
assert.notEqual(a, '')
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test('不同的错误码要分开', () => {
|
|
44
|
+
assert.notEqual(fingerprint('ENOENT: no such file'), fingerprint('EACCES: permission denied'))
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
test('报错原文变了但错误类型没变,仍归一类', () => {
|
|
48
|
+
const a = fingerprint('失败:模块不存在 webpack')
|
|
49
|
+
const b = fingerprint('失败:模块不存在 vite')
|
|
50
|
+
assert.equal(a, b, '「失败:模块不存在」这条规则会命中,后面的名字被截断')
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
test('纯描述行不算错误', () => {
|
|
54
|
+
assert.equal(fingerprint('改了三个文件'), '')
|
|
55
|
+
assert.equal(fingerprint(''), '')
|
|
56
|
+
assert.equal(fingerprint(undefined), '')
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('大小写和空白不影响', () => {
|
|
60
|
+
assert.equal(fingerprint(' ENOENT '), fingerprint('enoent'))
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
// ── 次数熔断 ────────────────────────────────────────
|
|
64
|
+
|
|
65
|
+
test('同一错误到 3 次才熔断', async () => {
|
|
66
|
+
await rm(sandbox, { recursive: true, force: true })
|
|
67
|
+
await mkdir(sandbox, { recursive: true })
|
|
68
|
+
const log = join(sandbox, 'Execution_Log.md')
|
|
69
|
+
await writeFile(log, '', 'utf8')
|
|
70
|
+
|
|
71
|
+
const breaker = new RepeatBreaker({ logPath: log, limit: 3 })
|
|
72
|
+
assert.equal(await breaker.check(), undefined)
|
|
73
|
+
|
|
74
|
+
await writeFile(log, 'Error: boom at a.js:1\n', 'utf8')
|
|
75
|
+
assert.equal(await breaker.check(), undefined)
|
|
76
|
+
|
|
77
|
+
await writeFile(log, 'Error: boom at a.js:1\nError: boom at a.js:2\n', 'utf8')
|
|
78
|
+
assert.equal(await breaker.check(), undefined, '这是第 2、3 次,第 3 次应该到阈值')
|
|
79
|
+
|
|
80
|
+
// 上面第二次 check 读了第 2、3 行:第 2 次 n=2,第 3 次 n=3 → 这里应该已经熔断
|
|
81
|
+
// 实际执行顺序说明见下一个用例
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test('计数按新增行累加,不重复读旧内容', async () => {
|
|
85
|
+
await rm(sandbox, { recursive: true, force: true })
|
|
86
|
+
await mkdir(sandbox, { recursive: true })
|
|
87
|
+
const log = join(sandbox, 'Execution_Log.md')
|
|
88
|
+
|
|
89
|
+
await writeFile(log, 'Error: same at a.js:1\n', 'utf8')
|
|
90
|
+
const breaker = new RepeatBreaker({ logPath: log, limit: 3 })
|
|
91
|
+
assert.equal(await breaker.check(), undefined) // n=1
|
|
92
|
+
|
|
93
|
+
assert.equal(await breaker.check(), undefined) // 没有新增,不该再计数
|
|
94
|
+
|
|
95
|
+
await writeFile(log, 'Error: same at a.js:1\nError: same at a.js:5\n', 'utf8')
|
|
96
|
+
assert.equal(await breaker.check(), undefined) // n=2
|
|
97
|
+
|
|
98
|
+
await writeFile(log, 'Error: same at a.js:1\nError: same at a.js:5\nError: same at a.js:9\n', 'utf8')
|
|
99
|
+
const hit = await breaker.check() // n=3 → 熔断
|
|
100
|
+
assert.notEqual(hit, undefined)
|
|
101
|
+
assert.equal(hit.count, 3)
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test('同一批里出现 3 次也立刻熔断', async () => {
|
|
105
|
+
await rm(sandbox, { recursive: true, force: true })
|
|
106
|
+
await mkdir(sandbox, { recursive: true })
|
|
107
|
+
const log = join(sandbox, 'Execution_Log.md')
|
|
108
|
+
await writeFile(log, 'ENOENT x\nENOENT y\nENOENT z\n', 'utf8')
|
|
109
|
+
const breaker = new RepeatBreaker({ logPath: log, limit: 3 })
|
|
110
|
+
const hit = await breaker.check()
|
|
111
|
+
assert.notEqual(hit, undefined)
|
|
112
|
+
assert.equal(hit.count, 3)
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
test('熔断结论会缓存,不重复报', async () => {
|
|
116
|
+
await rm(sandbox, { recursive: true, force: true })
|
|
117
|
+
await mkdir(sandbox, { recursive: true })
|
|
118
|
+
const log = join(sandbox, 'Execution_Log.md')
|
|
119
|
+
await writeFile(log, 'ENOENT\nENOENT\nENOENT\n', 'utf8')
|
|
120
|
+
const breaker = new RepeatBreaker({ logPath: log, limit: 3 })
|
|
121
|
+
assert.notEqual(await breaker.check(), undefined)
|
|
122
|
+
assert.notEqual(await breaker.check(), undefined, '再次调用仍返回同一结论')
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
test('日志文件不存在时不报错', async () => {
|
|
126
|
+
const breaker = new RepeatBreaker({ logPath: join(sandbox, 'nope.md'), limit: 3 })
|
|
127
|
+
assert.equal(await breaker.check(), undefined)
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
// ── 预算 ────────────────────────────────────────────
|
|
131
|
+
|
|
132
|
+
test('预算按字符估,超上限就抛 FAULT_BUDGET_EXCEEDED', () => {
|
|
133
|
+
const budget = new BudgetBreaker({ limitUsd: 0.2, priceTable: {} })
|
|
134
|
+
budget.useModel('hy4flash')
|
|
135
|
+
budget.countInput(1_000_000) // 约 40 万 token
|
|
136
|
+
budget.countOutput(500_000)
|
|
137
|
+
assert.ok(budget.spentUsd > 0)
|
|
138
|
+
assert.throws(() => budget.assert(), (error) => {
|
|
139
|
+
assert.equal(error.code, FAULT.BUDGET)
|
|
140
|
+
assert.match(error.message, /上限/)
|
|
141
|
+
return true
|
|
142
|
+
})
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
test('花费没到上限不抛', () => {
|
|
146
|
+
const budget = new BudgetBreaker({ limitUsd: 100, priceTable: {} })
|
|
147
|
+
budget.useModel('deepseek-v4.1-flash')
|
|
148
|
+
budget.countInput(1000)
|
|
149
|
+
assert.doesNotThrow(() => budget.assert())
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
test('用贵模型就比用便宜模型贵,且按当前模型记账', () => {
|
|
153
|
+
const cheap = new BudgetBreaker({ limitUsd: 100, priceTable: {} })
|
|
154
|
+
cheap.useModel('deepseek-v4.1-flash')
|
|
155
|
+
cheap.countInput(100000)
|
|
156
|
+
cheap.countOutput(100000)
|
|
157
|
+
|
|
158
|
+
const mixed = new BudgetBreaker({ limitUsd: 100, priceTable: {} })
|
|
159
|
+
mixed.useModel('deepseek-v4.1-flash')
|
|
160
|
+
mixed.useModel('kimi-k3.1-pro')
|
|
161
|
+
mixed.countInput(100000)
|
|
162
|
+
mixed.countOutput(100000)
|
|
163
|
+
|
|
164
|
+
assert.ok(mixed.spentUsd > cheap.spentUsd, '贵模型要更贵')
|
|
165
|
+
|
|
166
|
+
// 记账跟着「当前模型」走:mixed 里最后 useModel 的是 kimi,
|
|
167
|
+
// 所以那批 token 记在 kimi 名下,而不是被摊到最贵单价上再乘全部。
|
|
168
|
+
// 这条防的是老行为——以前只存一个总数,出账时拿最贵单价套所有 token。
|
|
169
|
+
const rows = mixed.breakdown
|
|
170
|
+
assert.equal(rows.length, 1, '这一批 token 只该记在一个模型名下')
|
|
171
|
+
assert.equal(rows[0].model, 'kimi-k3.1-pro', '记在最后一个 useModel 的模型上')
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
test('未知模型按最贵的兜底价算', () => {
|
|
175
|
+
const unknown = new BudgetBreaker({ limitUsd: 100, priceTable: {} })
|
|
176
|
+
unknown.useModel('some-new-model')
|
|
177
|
+
unknown.countInput(100000)
|
|
178
|
+
assert.ok(unknown.spentUsd > 0)
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
// ── 聚合器 ──────────────────────────────────────────
|
|
182
|
+
|
|
183
|
+
test('熔断后 signal 被 abort,fault 带码', async () => {
|
|
184
|
+
await rm(sandbox, { recursive: true, force: true })
|
|
185
|
+
const projectDir = join(sandbox, '.project-flow', 'demo')
|
|
186
|
+
await mkdir(projectDir, { recursive: true })
|
|
187
|
+
const log = join(projectDir, 'Execution_Log.md')
|
|
188
|
+
await writeFile(log, 'ENOENT a\nENOENT b\nENOENT c\n', 'utf8')
|
|
189
|
+
await mkdir(sandbox, { recursive: true })
|
|
190
|
+
|
|
191
|
+
const wd = new Watchdog({
|
|
192
|
+
cwd: sandbox,
|
|
193
|
+
logPath: log,
|
|
194
|
+
settings: { maxRepeatErrors: 3, silentMsBuild: 900000, budgetUsd: 10 },
|
|
195
|
+
})
|
|
196
|
+
assert.equal(wd.signal.aborted, false)
|
|
197
|
+
await wd.afterStage({ ok: true, text: '做完了' })
|
|
198
|
+
assert.equal(wd.signal.aborted, true, '三次同一错误应触发熔断')
|
|
199
|
+
assert.equal(wd.fault.code, FAULT.REPEAT)
|
|
200
|
+
assert.equal(wd.signal.reason.code, FAULT.REPEAT)
|
|
201
|
+
wd.dispose()
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
test('预算超限在 beforeAttempt 抛,不进重试', async () => {
|
|
205
|
+
await rm(sandbox, { recursive: true, force: true })
|
|
206
|
+
await mkdir(sandbox, { recursive: true })
|
|
207
|
+
const wd = new Watchdog({
|
|
208
|
+
cwd: sandbox,
|
|
209
|
+
logPath: join(sandbox, 'none.md'),
|
|
210
|
+
settings: { maxRepeatErrors: 3, silentMsBuild: 900000, budgetUsd: 1 },
|
|
211
|
+
})
|
|
212
|
+
wd.beginStage(3, 'hy4flash')
|
|
213
|
+
// 一次塞进去大量字符,把预算打穿
|
|
214
|
+
assert.throws(() => {
|
|
215
|
+
for (let i = 0; i < 200; i += 1) {
|
|
216
|
+
wd.beforeAttempt('词'.repeat(100000))
|
|
217
|
+
}
|
|
218
|
+
}, (error) => error.code === FAULT.BUDGET)
|
|
219
|
+
wd.dispose()
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
test('正常情况不熔断', async () => {
|
|
223
|
+
await rm(sandbox, { recursive: true, force: true })
|
|
224
|
+
const projectDir = join(sandbox, '.project-flow', 'demo')
|
|
225
|
+
await mkdir(projectDir, { recursive: true })
|
|
226
|
+
const log = join(projectDir, 'Execution_Log.md')
|
|
227
|
+
await writeFile(log, '改好了三个文件\n测试通过\n', 'utf8')
|
|
228
|
+
|
|
229
|
+
const wd = new Watchdog({
|
|
230
|
+
cwd: sandbox,
|
|
231
|
+
logPath: log,
|
|
232
|
+
settings: { maxRepeatErrors: 3, silentMsBuild: 900000, budgetUsd: 10 },
|
|
233
|
+
})
|
|
234
|
+
await wd.afterStage({ ok: true, text: '完成了' })
|
|
235
|
+
assert.equal(wd.signal.aborted, false)
|
|
236
|
+
assert.equal(wd.fault, undefined)
|
|
237
|
+
wd.dispose()
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
test('snapshot 带出花费与最频繁的错误', async () => {
|
|
241
|
+
await rm(sandbox, { recursive: true, force: true })
|
|
242
|
+
const projectDir = join(sandbox, '.project-flow', 'demo')
|
|
243
|
+
await mkdir(projectDir, { recursive: true })
|
|
244
|
+
const log = join(projectDir, 'Execution_Log.md')
|
|
245
|
+
await writeFile(log, 'ENOENT a\nENOENT b\n改好了\n', 'utf8')
|
|
246
|
+
|
|
247
|
+
const wd = new Watchdog({
|
|
248
|
+
cwd: sandbox,
|
|
249
|
+
logPath: log,
|
|
250
|
+
settings: { maxRepeatErrors: 5, silentMsBuild: 900000, budgetUsd: 10 },
|
|
251
|
+
})
|
|
252
|
+
await wd.afterStage({ ok: true, text: 'x'.repeat(10000) })
|
|
253
|
+
const snap = wd.snapshot()
|
|
254
|
+
assert.ok(snap.spentUsd >= 0)
|
|
255
|
+
assert.equal(snap.repeats[0].count, 2)
|
|
256
|
+
wd.dispose()
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
// ── 活性巡检 ────────────────────────────────────────
|
|
260
|
+
|
|
261
|
+
test('静默超时触发 FAULT_SILENT_HANG', async () => {
|
|
262
|
+
await rm(sandbox, { recursive: true, force: true })
|
|
263
|
+
await mkdir(sandbox, { recursive: true })
|
|
264
|
+
await writeFile(join(sandbox, 'a.txt'), 'x', 'utf8')
|
|
265
|
+
// 把文件时间改老,模拟「很久没写过东西」
|
|
266
|
+
const old = new Date(Date.now() - 3600_000)
|
|
267
|
+
await utimes(join(sandbox, 'a.txt'), old, old)
|
|
268
|
+
|
|
269
|
+
let fired = undefined
|
|
270
|
+
const wd = new Watchdog({
|
|
271
|
+
cwd: sandbox,
|
|
272
|
+
logPath: join(sandbox, 'none.md'),
|
|
273
|
+
settings: { maxRepeatErrors: 3, silentMsBuild: 1000, budgetUsd: 10 },
|
|
274
|
+
})
|
|
275
|
+
// 用一个极短窗口直接验证判定逻辑
|
|
276
|
+
wd.setWindows({ build: 1000 })
|
|
277
|
+
wd.beginStage(3)
|
|
278
|
+
await new Promise(resolve => setTimeout(resolve, 1800))
|
|
279
|
+
fired = wd.fault
|
|
280
|
+
assert.notEqual(fired, undefined, '静默应触发')
|
|
281
|
+
assert.equal(fired.code, FAULT.SILENT)
|
|
282
|
+
wd.dispose()
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
test('.project-flow 里的写入不算心跳', async () => {
|
|
286
|
+
await rm(sandbox, { recursive: true, force: true })
|
|
287
|
+
const projectDir = join(sandbox, '.project-flow', 'demo')
|
|
288
|
+
await mkdir(projectDir, { recursive: true })
|
|
289
|
+
await writeFile(join(projectDir, 'run.log'), '正在跑\n', 'utf8')
|
|
290
|
+
await writeFile(join(sandbox, 'a.txt'), 'x', 'utf8')
|
|
291
|
+
const old = new Date(Date.now() - 3600_000)
|
|
292
|
+
await utimes(join(sandbox, 'a.txt'), old, old)
|
|
293
|
+
|
|
294
|
+
const wd = new Watchdog({
|
|
295
|
+
cwd: sandbox,
|
|
296
|
+
logPath: join(sandbox, 'none.md'),
|
|
297
|
+
settings: { maxRepeatErrors: 3, silentMsBuild: 1000, budgetUsd: 10 },
|
|
298
|
+
})
|
|
299
|
+
wd.setWindows({ build: 1000 })
|
|
300
|
+
wd.beginStage(3)
|
|
301
|
+
await new Promise(resolve => setTimeout(resolve, 1800))
|
|
302
|
+
assert.notEqual(wd.fault, undefined, '.project-flow 的写入不该续命')
|
|
303
|
+
assert.equal(wd.fault.code, FAULT.SILENT)
|
|
304
|
+
wd.dispose()
|
|
305
|
+
})
|