dsh-workplan 1.7.1 → 1.8.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 +72 -0
- package/README.en.md +42 -1
- package/README.md +43 -1
- package/SECURITY.md +120 -0
- package/docs/SBOM.md +113 -0
- package/docs/cc/AVA-VULNERABILITY-ANALYSIS.md +97 -0
- package/docs/cc/CC-2026-ASSESSMENT.md +266 -0
- package/docs/cc/SECURITY-TARGET.md +244 -0
- package/docs/collaboration.md +440 -0
- package/docs/collapse-interaction-spec.md +272 -0
- package/docs/defect-report-count-inconsistency.md +370 -0
- package/docs/departments.md +194 -0
- package/docs/design-proposals/state-machine.reference.ts +836 -0
- package/docs/design-proposals/state-machine.test.reference.ts +655 -0
- package/docs/fix-report-derived-counts-1.4.3.md +199 -0
- package/docs/fix-report-phase-percent-1.5.2.md +284 -0
- package/docs/preview/progress-visual-1.5.0.html +683 -0
- package/docs/progress-visualization-clarify-20261028.md +211 -0
- package/docs/progress-visualization-design-20261028.md +607 -0
- package/docs/progress-visualization-locate-20261028.md +159 -0
- package/docs/progress-visualization-report-1.5.0.md +140 -0
- package/docs/release-notes-1.7.0.md +46 -0
- package/docs/release-notes-1.7.1.md +36 -0
- package/docs/release-notes-1.8.0.md +80 -0
- package/docs/release-notes-1.8.1.md +69 -0
- package/docs/release-record-1.5.1-20261028.md +258 -0
- package/docs/release-record-1.5.2-20261029.md +101 -0
- package/docs/release-record-1.7.0-20261030.md +58 -0
- package/docs/release-record-1.7.1-20261030.md +56 -0
- package/docs/repro-count-inconsistency.md +310 -0
- package/docs/review-collaboration-20261030.md +246 -0
- package/docs/root-cause-phase-percent.md +866 -0
- package/docs/state-progress-matrix.md +517 -0
- package/docs/status-grouping-ia.md +789 -0
- package/docs/stuck-review-1.3.0.md +646 -0
- package/docs/t4-contrast-measurement.json +496 -0
- package/docs/t4-contrast-measurement.md +177 -0
- package/docs/t8-fix-prioritization.md +105 -0
- package/docs/version-upgrade-rollback-1.5.0-20261028.md +654 -0
- package/docs/visual-refinement-spec.md +164 -0
- package/lib/host.js +20 -3
- package/lib/runtime.js +21 -0
- package/package.json +4 -2
- package/src/host.ts +20 -2
- package/src/runtime.ts +22 -0
|
@@ -0,0 +1,655 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* T4 回归测试:子任务状态机、重置语义与不变量。
|
|
3
|
+
*
|
|
4
|
+
* 每条断言都对应 docs/state-machine-design.md 里的一张表或一条不变量;
|
|
5
|
+
* 测试名称直接引用 T2 缺陷编号,修复后这些用例必须保持绿色。
|
|
6
|
+
*/
|
|
7
|
+
import test from 'node:test'
|
|
8
|
+
import assert from 'node:assert/strict'
|
|
9
|
+
|
|
10
|
+
import { computeStats, planPhase } from '../src/plan.js'
|
|
11
|
+
import {
|
|
12
|
+
ATTACH_PROGRESS,
|
|
13
|
+
BLOCKED_REASON_FALLBACK,
|
|
14
|
+
IllegalPhaseTransitionError,
|
|
15
|
+
IllegalTransitionError,
|
|
16
|
+
InvalidEntryError,
|
|
17
|
+
InvariantError,
|
|
18
|
+
PLAN_OPERATIONS,
|
|
19
|
+
PLAN_PHASES,
|
|
20
|
+
PLAN_PHASE_EVENTS,
|
|
21
|
+
START_PROGRESS,
|
|
22
|
+
SUBTASK_STATUSES,
|
|
23
|
+
StateMachineError,
|
|
24
|
+
TRANSITION_TYPES,
|
|
25
|
+
allowedPhaseEvents,
|
|
26
|
+
allowedSourceStatuses,
|
|
27
|
+
applyPhaseEvent,
|
|
28
|
+
applyPlanOperation,
|
|
29
|
+
applyTransition,
|
|
30
|
+
assertInvariants,
|
|
31
|
+
collectViolations,
|
|
32
|
+
findDepartmentViolations,
|
|
33
|
+
findEntryViolations,
|
|
34
|
+
findPhaseViolations,
|
|
35
|
+
findStatsViolations,
|
|
36
|
+
findStatusViolations,
|
|
37
|
+
isPlanPhase,
|
|
38
|
+
isSubtaskStatus,
|
|
39
|
+
normaliseEntry,
|
|
40
|
+
normaliseStatuses,
|
|
41
|
+
} from '../src/state.js'
|
|
42
|
+
import type { PlanPhase, StatusEntry, SubtaskStatus, Transition, TransitionType } from '../src/state.js'
|
|
43
|
+
|
|
44
|
+
const NOW = 1_700_000_000_000
|
|
45
|
+
|
|
46
|
+
function base(overrides: Record<string, unknown> = {}): Record<string, unknown> {
|
|
47
|
+
return {
|
|
48
|
+
subtaskId: 'T1',
|
|
49
|
+
stageId: 'stage-1',
|
|
50
|
+
title: '任务一',
|
|
51
|
+
department: 'eng',
|
|
52
|
+
role: 'dev',
|
|
53
|
+
status: 'pending',
|
|
54
|
+
progress: 0,
|
|
55
|
+
childId: null,
|
|
56
|
+
startedAt: null,
|
|
57
|
+
finishedAt: null,
|
|
58
|
+
summary: '',
|
|
59
|
+
error: null,
|
|
60
|
+
...overrides,
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function sample(status: SubtaskStatus): StatusEntry {
|
|
65
|
+
switch (status) {
|
|
66
|
+
case 'pending':
|
|
67
|
+
return normaliseEntry(base(), { now: NOW })
|
|
68
|
+
case 'in_progress':
|
|
69
|
+
return normaliseEntry(base({ status, progress: 40, childId: 'child-1', startedAt: NOW, summary: '执行中' }), {
|
|
70
|
+
now: NOW,
|
|
71
|
+
})
|
|
72
|
+
case 'done':
|
|
73
|
+
return normaliseEntry(
|
|
74
|
+
base({ status, progress: 100, childId: 'child-1', startedAt: NOW, finishedAt: NOW + 10, summary: '完成' }),
|
|
75
|
+
{ now: NOW },
|
|
76
|
+
)
|
|
77
|
+
case 'blocked':
|
|
78
|
+
return normaliseEntry(
|
|
79
|
+
base({ status, progress: 30, childId: 'child-1', startedAt: NOW, finishedAt: NOW + 10, error: '构建失败' }),
|
|
80
|
+
{ now: NOW },
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
throw new Error(`unexpected status: ${String(status)}`)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function withId(entry: StatusEntry, subtaskId: string): StatusEntry {
|
|
87
|
+
return { ...entry, subtaskId }
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function withDepartment(entry: StatusEntry, department: string): StatusEntry {
|
|
91
|
+
return { ...entry, department }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function payload(type: TransitionType): Transition {
|
|
95
|
+
switch (type) {
|
|
96
|
+
case 'start':
|
|
97
|
+
return { type, progress: START_PROGRESS, at: NOW }
|
|
98
|
+
case 'bind-child':
|
|
99
|
+
return { type, childId: 'child-9', progress: ATTACH_PROGRESS }
|
|
100
|
+
case 'report-progress':
|
|
101
|
+
return { type, progress: 55 }
|
|
102
|
+
case 'complete':
|
|
103
|
+
return { type, summary: '完成', at: NOW + 20 }
|
|
104
|
+
case 'fail':
|
|
105
|
+
return { type, error: '失败原因', at: NOW + 20 }
|
|
106
|
+
case 'reset':
|
|
107
|
+
return { type }
|
|
108
|
+
case 'restart':
|
|
109
|
+
return { type, progress: START_PROGRESS, at: NOW + 20 }
|
|
110
|
+
}
|
|
111
|
+
throw new Error(`unexpected transition: ${String(type)}`)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const EXPECTED_STATUS: Record<TransitionType, SubtaskStatus> = {
|
|
115
|
+
start: 'in_progress',
|
|
116
|
+
'bind-child': 'in_progress',
|
|
117
|
+
'report-progress': 'in_progress',
|
|
118
|
+
complete: 'done',
|
|
119
|
+
fail: 'blocked',
|
|
120
|
+
reset: 'pending',
|
|
121
|
+
restart: 'in_progress',
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/* ------------------------------------------------------------------ *
|
|
125
|
+
* §1 迁移矩阵:4 状态 × 7 动作全部枚举,不允许出现“第三种结果”
|
|
126
|
+
* ------------------------------------------------------------------ */
|
|
127
|
+
|
|
128
|
+
test('迁移矩阵覆盖 4 个状态 × 7 个动作,合法结果均满足不变量', () => {
|
|
129
|
+
let legal = 0
|
|
130
|
+
for (const status of SUBTASK_STATUSES) {
|
|
131
|
+
for (const type of TRANSITION_TYPES) {
|
|
132
|
+
try {
|
|
133
|
+
const next = applyTransition(sample(status), payload(type), { now: NOW })
|
|
134
|
+
legal += 1
|
|
135
|
+
assert.equal(next.status, EXPECTED_STATUS[type], `${status} × ${type} 的目标状态错误`)
|
|
136
|
+
assert.deepEqual(findEntryViolations(next), [], `${status} × ${type} 的产物违反字段规则`)
|
|
137
|
+
} catch (error) {
|
|
138
|
+
assert.ok(
|
|
139
|
+
error instanceof IllegalTransitionError,
|
|
140
|
+
`${status} × ${type} 抛出了非契约错误:${error instanceof Error ? error.message : String(error)}`,
|
|
141
|
+
)
|
|
142
|
+
assert.equal((error as IllegalTransitionError).from, status)
|
|
143
|
+
assert.equal((error as IllegalTransitionError).action, type)
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const allowedTotal = TRANSITION_TYPES.reduce((sum, type) => sum + allowedSourceStatuses(type).length, 0)
|
|
148
|
+
assert.equal(legal, allowedTotal)
|
|
149
|
+
assert.equal(legal, 16)
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
test('非法迁移明确报错:不允许 start 已完成任务、不允许 complete 未开始任务', () => {
|
|
153
|
+
assert.throws(() => applyTransition(sample('done'), { type: 'start' }), IllegalTransitionError)
|
|
154
|
+
assert.throws(() => applyTransition(sample('pending'), { type: 'complete' }), IllegalTransitionError)
|
|
155
|
+
assert.throws(() => applyTransition(sample('pending'), { type: 'bind-child', childId: 'c' }), IllegalTransitionError)
|
|
156
|
+
assert.throws(() => applyTransition(sample('blocked'), { type: 'report-progress', progress: 50 }), IllegalTransitionError)
|
|
157
|
+
assert.throws(() => applyTransition(sample('done'), { type: 'fail', error: 'x' }), IllegalTransitionError)
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
/* ------------------------------------------------------------------ *
|
|
161
|
+
* §2 归一化:历史脏数据先修复,再断言
|
|
162
|
+
* ------------------------------------------------------------------ */
|
|
163
|
+
|
|
164
|
+
test('pending 不得携带运行态/结果字段:六个伴随字段全部清空', () => {
|
|
165
|
+
const dirty = base({
|
|
166
|
+
status: 'pending',
|
|
167
|
+
progress: 100,
|
|
168
|
+
childId: 'child-x',
|
|
169
|
+
startedAt: NOW,
|
|
170
|
+
finishedAt: NOW,
|
|
171
|
+
summary: '遗留摘要',
|
|
172
|
+
error: '旧错误',
|
|
173
|
+
})
|
|
174
|
+
const codes = findEntryViolations(dirty).map((item) => item.code)
|
|
175
|
+
for (const code of [
|
|
176
|
+
'pending.progress',
|
|
177
|
+
'pending.childId',
|
|
178
|
+
'pending.startedAt',
|
|
179
|
+
'pending.finishedAt',
|
|
180
|
+
'pending.summary',
|
|
181
|
+
'pending.error',
|
|
182
|
+
]) {
|
|
183
|
+
assert.ok(codes.includes(code), `未捕获 ${code},实际:${codes.join(',')}`)
|
|
184
|
+
}
|
|
185
|
+
const clean = normaliseEntry(dirty, { now: NOW })
|
|
186
|
+
assert.deepEqual(clean, normaliseEntry(base(), { now: NOW }))
|
|
187
|
+
assert.deepEqual(findEntryViolations(clean), [])
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
test('in_progress 归一化:进度封顶 99、清 finishedAt/error、补 startedAt', () => {
|
|
191
|
+
const dirty = base({ status: 'in_progress', progress: 100, startedAt: null, finishedAt: NOW, error: '旧错误', summary: '进行中' })
|
|
192
|
+
const clean = normaliseEntry(dirty, { now: NOW })
|
|
193
|
+
assert.equal(clean.progress, 99)
|
|
194
|
+
assert.equal(clean.startedAt, NOW)
|
|
195
|
+
assert.equal(clean.finishedAt, null)
|
|
196
|
+
assert.equal(clean.error, null)
|
|
197
|
+
assert.equal(clean.summary, '进行中')
|
|
198
|
+
assert.deepEqual(findEntryViolations(clean), [])
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
test('done 归一化:进度固定 100、清 error、缺 finishedAt 时补 now', () => {
|
|
202
|
+
const dirty = base({ status: 'done', progress: 50, finishedAt: null, error: '旧错误', summary: '已有结论' })
|
|
203
|
+
const clean = normaliseEntry(dirty, { now: NOW })
|
|
204
|
+
assert.equal(clean.progress, 100)
|
|
205
|
+
assert.equal(clean.finishedAt, NOW)
|
|
206
|
+
assert.equal(clean.error, null)
|
|
207
|
+
assert.equal(clean.summary, '已有结论')
|
|
208
|
+
assert.deepEqual(findEntryViolations(clean), [])
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
test('blocked 归一化:进度封顶 99、缺 error 时用 summary 或兜底文案', () => {
|
|
212
|
+
const dirty = base({ status: 'blocked', progress: 100, error: ' ', summary: '上游子任务受阻:T2' })
|
|
213
|
+
const clean = normaliseEntry(dirty, { now: NOW })
|
|
214
|
+
assert.equal(clean.progress, 99)
|
|
215
|
+
assert.equal(clean.error, '上游子任务受阻:T2')
|
|
216
|
+
assert.equal(clean.finishedAt, NOW)
|
|
217
|
+
assert.deepEqual(findEntryViolations(clean), [])
|
|
218
|
+
|
|
219
|
+
const bare = normaliseEntry(base({ status: 'blocked', progress: 100 }), { now: NOW })
|
|
220
|
+
assert.equal(bare.error, BLOCKED_REASON_FALLBACK)
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
test('归一化是幂等的,且不修改输入对象', () => {
|
|
224
|
+
const dirty = base({ status: 'pending', progress: 100, childId: 'child-x', error: '旧错误' })
|
|
225
|
+
const once = normaliseEntry(dirty, { now: NOW })
|
|
226
|
+
const twice = normaliseEntry(once, { now: NOW })
|
|
227
|
+
assert.deepEqual(once, twice)
|
|
228
|
+
assert.equal(dirty.progress, 100)
|
|
229
|
+
assert.equal(dirty.childId, 'child-x')
|
|
230
|
+
|
|
231
|
+
const frozen = Object.freeze(base({ status: 'pending', progress: 80 }))
|
|
232
|
+
const out = normaliseStatuses([frozen], { now: NOW })
|
|
233
|
+
assert.equal(out.length, 1)
|
|
234
|
+
assert.equal(frozen.progress, 80)
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
test('非法状态项必须报错,而不是被静默修复', () => {
|
|
238
|
+
assert.throws(() => normaliseEntry(null), InvalidEntryError)
|
|
239
|
+
assert.throws(() => normaliseEntry({ status: 'pending' }), InvalidEntryError)
|
|
240
|
+
assert.throws(() => normaliseEntry({ subtaskId: 'T1', status: 'running' }), InvalidEntryError)
|
|
241
|
+
assert.throws(() => normaliseEntry({ subtaskId: 'T1', status: 'pending' }, { now: Number.NaN }), StateMachineError)
|
|
242
|
+
})
|
|
243
|
+
|
|
244
|
+
/* ------------------------------------------------------------------ *
|
|
245
|
+
* §3 迁移后的字段写入(含 fail 不再伪造 100%)
|
|
246
|
+
* ------------------------------------------------------------------ */
|
|
247
|
+
|
|
248
|
+
test('start:进度 5、startedAt、清空 childId/finishedAt/error', () => {
|
|
249
|
+
const next = applyTransition(sample('pending'), { type: 'start' }, { now: NOW })
|
|
250
|
+
assert.equal(next.status, 'in_progress')
|
|
251
|
+
assert.equal(next.progress, START_PROGRESS)
|
|
252
|
+
assert.equal(next.startedAt, NOW)
|
|
253
|
+
assert.equal(next.childId, null)
|
|
254
|
+
assert.equal(next.finishedAt, null)
|
|
255
|
+
assert.equal(next.error, null)
|
|
256
|
+
assert.equal(next.summary, '')
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
test('bind-child:只写 childId 并抬升进度,不降低已有进度', () => {
|
|
260
|
+
const fresh = normaliseEntry(base({ status: 'in_progress', progress: 0, startedAt: NOW }), { now: NOW })
|
|
261
|
+
const bound = applyTransition(fresh, { type: 'bind-child', childId: 'child-9' }, { now: NOW })
|
|
262
|
+
assert.equal(bound.childId, 'child-9')
|
|
263
|
+
assert.equal(bound.progress, ATTACH_PROGRESS)
|
|
264
|
+
|
|
265
|
+
const later = applyTransition(sample('in_progress'), { type: 'bind-child', childId: 'child-9' }, { now: NOW })
|
|
266
|
+
assert.equal(later.progress, 40)
|
|
267
|
+
})
|
|
268
|
+
|
|
269
|
+
test('report-progress:100 只能由 complete 写入,直接报 100 必须报错', () => {
|
|
270
|
+
const next = applyTransition(sample('in_progress'), { type: 'report-progress', progress: 99 }, { now: NOW })
|
|
271
|
+
assert.equal(next.progress, 99)
|
|
272
|
+
assert.throws(
|
|
273
|
+
() => applyTransition(sample('in_progress'), { type: 'report-progress', progress: 100 }),
|
|
274
|
+
(error: unknown) => error instanceof StateMachineError && error.code === 'INVALID_PROGRESS',
|
|
275
|
+
)
|
|
276
|
+
assert.throws(
|
|
277
|
+
() => applyTransition(sample('pending'), { type: 'start', progress: 100 }),
|
|
278
|
+
(error: unknown) => error instanceof StateMachineError && error.code === 'INVALID_PROGRESS',
|
|
279
|
+
)
|
|
280
|
+
})
|
|
281
|
+
|
|
282
|
+
test('fail:blocked 保留真实进度(封顶 99),不再伪造成 100%', () => {
|
|
283
|
+
const running = normaliseEntry(base({ status: 'in_progress', progress: 80, childId: 'c', startedAt: NOW }), { now: NOW })
|
|
284
|
+
const failed = applyTransition(running, { type: 'fail', error: '子 Agent 退出码 1', summary: '编译失败' }, { now: NOW + 5 })
|
|
285
|
+
assert.equal(failed.status, 'blocked')
|
|
286
|
+
assert.equal(failed.progress, 80)
|
|
287
|
+
assert.equal(failed.childId, 'c')
|
|
288
|
+
assert.equal(failed.startedAt, NOW)
|
|
289
|
+
assert.equal(failed.finishedAt, NOW + 5)
|
|
290
|
+
assert.equal(failed.error, '子 Agent 退出码 1')
|
|
291
|
+
assert.equal(failed.summary, '编译失败')
|
|
292
|
+
assert.deepEqual(findEntryViolations(failed), [])
|
|
293
|
+
|
|
294
|
+
assert.throws(
|
|
295
|
+
() => applyTransition(running, { type: 'fail', error: ' ' }),
|
|
296
|
+
(error: unknown) => error instanceof StateMachineError && error.code === 'MISSING_REASON',
|
|
297
|
+
)
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
test('complete:进度 100、清 error、保留摘要;重复完成幂等', () => {
|
|
301
|
+
const running = applyTransition(sample('in_progress'), { type: 'complete', summary: '完成' }, { now: NOW + 7 })
|
|
302
|
+
assert.equal(running.status, 'done')
|
|
303
|
+
assert.equal(running.progress, 100)
|
|
304
|
+
assert.equal(running.finishedAt, NOW + 7)
|
|
305
|
+
assert.equal(running.error, null)
|
|
306
|
+
assert.equal(running.summary, '完成')
|
|
307
|
+
|
|
308
|
+
const again = applyTransition(running, { type: 'complete', summary: '补充说明' }, { now: NOW + 9 })
|
|
309
|
+
assert.equal(again.progress, 100)
|
|
310
|
+
assert.equal(again.finishedAt, NOW + 7)
|
|
311
|
+
assert.equal(again.summary, '补充说明')
|
|
312
|
+
assert.deepEqual(findEntryViolations(again), [])
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
/* ------------------------------------------------------------------ *
|
|
316
|
+
* §4 重置:任意状态回到同一初始态,六个伴随字段全部清空
|
|
317
|
+
* ------------------------------------------------------------------ */
|
|
318
|
+
|
|
319
|
+
test('reset 从四个状态都回到同一初始态', () => {
|
|
320
|
+
const expected = normaliseEntry(base(), { now: NOW })
|
|
321
|
+
for (const status of SUBTASK_STATUSES) {
|
|
322
|
+
const reset = applyTransition(sample(status), { type: 'reset' }, { now: NOW })
|
|
323
|
+
assert.deepEqual(reset, expected, `${status} reset 后残留了字段`)
|
|
324
|
+
assert.equal(reset.status, 'pending')
|
|
325
|
+
assert.equal(reset.progress, 0)
|
|
326
|
+
assert.equal(reset.childId, null)
|
|
327
|
+
assert.equal(reset.startedAt, null)
|
|
328
|
+
assert.equal(reset.finishedAt, null)
|
|
329
|
+
assert.equal(reset.summary, '')
|
|
330
|
+
assert.equal(reset.error, null)
|
|
331
|
+
}
|
|
332
|
+
})
|
|
333
|
+
|
|
334
|
+
test('restart:先复位再启动,不复用上一次的 childId/摘要/错误', () => {
|
|
335
|
+
const restarted = applyTransition(sample('blocked'), { type: 'restart' }, { now: NOW + 30 })
|
|
336
|
+
assert.equal(restarted.status, 'in_progress')
|
|
337
|
+
assert.equal(restarted.progress, START_PROGRESS)
|
|
338
|
+
assert.equal(restarted.childId, null)
|
|
339
|
+
assert.equal(restarted.startedAt, NOW + 30)
|
|
340
|
+
assert.equal(restarted.finishedAt, null)
|
|
341
|
+
assert.equal(restarted.summary, '')
|
|
342
|
+
assert.equal(restarted.error, null)
|
|
343
|
+
assert.deepEqual(findEntryViolations(restarted), [])
|
|
344
|
+
})
|
|
345
|
+
|
|
346
|
+
/* ------------------------------------------------------------------ *
|
|
347
|
+
* §5 计划级操作:pause / resume / cancel 的语义是“只动该动的”
|
|
348
|
+
* ------------------------------------------------------------------ */
|
|
349
|
+
|
|
350
|
+
function planFixture(): StatusEntry[] {
|
|
351
|
+
return [
|
|
352
|
+
withId(sample('pending'), 'T1'),
|
|
353
|
+
withId(sample('in_progress'), 'T2'),
|
|
354
|
+
withId(sample('blocked'), 'T3'),
|
|
355
|
+
withId(sample('done'), 'T4'),
|
|
356
|
+
]
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
test('pause-round:只把 in_progress 归零,不抹掉 blocked/done 的事实', () => {
|
|
360
|
+
const input = planFixture()
|
|
361
|
+
const out = applyPlanOperation(input, 'pause-round', { now: NOW })
|
|
362
|
+
assert.deepEqual(out[0], input[0])
|
|
363
|
+
assert.equal(out[1].status, 'pending')
|
|
364
|
+
assert.equal(out[1].progress, 0)
|
|
365
|
+
assert.equal(out[1].childId, null)
|
|
366
|
+
assert.equal(out[1].startedAt, null)
|
|
367
|
+
assert.equal(out[1].finishedAt, null)
|
|
368
|
+
assert.equal(out[1].summary, '')
|
|
369
|
+
assert.equal(out[1].error, null)
|
|
370
|
+
assert.deepEqual(out[2], input[2], 'blocked 被错误地洗成了 pending')
|
|
371
|
+
assert.deepEqual(out[3], input[3])
|
|
372
|
+
|
|
373
|
+
const stats = computeStats(out)
|
|
374
|
+
assert.equal(stats.inProgress, 0)
|
|
375
|
+
assert.equal(stats.blocked, 1)
|
|
376
|
+
assert.equal(stats.done, 1)
|
|
377
|
+
assert.deepEqual(findStatusViolations(out), [])
|
|
378
|
+
assert.deepEqual(findPhaseViolations('paused', out), [])
|
|
379
|
+
})
|
|
380
|
+
|
|
381
|
+
test('resume:blocked/in_progress 复位为 pending,done 保留,计数不再残留', () => {
|
|
382
|
+
const input = planFixture()
|
|
383
|
+
const out = applyPlanOperation(input, 'resume', { now: NOW })
|
|
384
|
+
assert.equal(out[0].status, 'pending')
|
|
385
|
+
assert.equal(out[1].status, 'pending')
|
|
386
|
+
assert.equal(out[2].status, 'pending')
|
|
387
|
+
assert.equal(out[3].status, 'done')
|
|
388
|
+
assert.equal(out[2].progress, 0)
|
|
389
|
+
assert.equal(out[2].error, null)
|
|
390
|
+
assert.equal(out[2].finishedAt, null)
|
|
391
|
+
|
|
392
|
+
const stats = computeStats(out)
|
|
393
|
+
assert.equal(stats.blocked, 0)
|
|
394
|
+
assert.equal(stats.inProgress, 0)
|
|
395
|
+
assert.equal(stats.pending, 3)
|
|
396
|
+
assert.equal(stats.done, 1)
|
|
397
|
+
assert.deepEqual(findStatusViolations(out), [])
|
|
398
|
+
})
|
|
399
|
+
|
|
400
|
+
test('cancel:只归零正在运行的任务,取消不伪造 blocked', () => {
|
|
401
|
+
const out = applyPlanOperation(planFixture(), 'cancel', { now: NOW })
|
|
402
|
+
assert.equal(out[1].status, 'pending')
|
|
403
|
+
assert.equal(out[2].status, 'blocked')
|
|
404
|
+
assert.equal(out[3].status, 'done')
|
|
405
|
+
assert.deepEqual(findStatusViolations(out), [])
|
|
406
|
+
assert.deepEqual(findPhaseViolations('cancelled', out), [])
|
|
407
|
+
})
|
|
408
|
+
|
|
409
|
+
test('所有计划级操作都幂等,且产物满足不变量', () => {
|
|
410
|
+
for (const operation of PLAN_OPERATIONS) {
|
|
411
|
+
const once = applyPlanOperation(planFixture(), operation, { now: NOW })
|
|
412
|
+
const twice = applyPlanOperation(once, operation, { now: NOW })
|
|
413
|
+
assert.deepEqual(findStatusViolations(once), [], `${operation} 产物违反字段规则`)
|
|
414
|
+
assert.deepEqual(twice, once, `${operation} 不幂等`)
|
|
415
|
+
}
|
|
416
|
+
})
|
|
417
|
+
|
|
418
|
+
/* ------------------------------------------------------------------ *
|
|
419
|
+
* §6 不变量:stats / departments / phase 必须与同一份 statuses 一致
|
|
420
|
+
* ------------------------------------------------------------------ */
|
|
421
|
+
|
|
422
|
+
function statsFixture(): StatusEntry[] {
|
|
423
|
+
return [
|
|
424
|
+
withId(withDepartment(sample('done'), 'eng'), 'T1'),
|
|
425
|
+
withId(withDepartment(sample('blocked'), 'eng'), 'T2'),
|
|
426
|
+
withId(withDepartment(sample('in_progress'), 'qa'), 'T3'),
|
|
427
|
+
withId(withDepartment(sample('pending'), 'qa'), 'T4'),
|
|
428
|
+
]
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
const DEPARTMENTS = [
|
|
432
|
+
{ id: 'eng', total: 2, done: 1, blocked: 1, inProgress: 0 },
|
|
433
|
+
{ id: 'qa', total: 2, done: 0, blocked: 0, inProgress: 1 },
|
|
434
|
+
]
|
|
435
|
+
|
|
436
|
+
test('stats 由同一份 statuses 派生:正确值通过,任一字段漂移都被捕获', () => {
|
|
437
|
+
const statuses = statsFixture()
|
|
438
|
+
const stats = computeStats(statuses)
|
|
439
|
+
assert.deepEqual(findStatsViolations(statuses, stats), [])
|
|
440
|
+
assert.deepEqual(collectViolations({ statuses, stats, departments: DEPARTMENTS, phase: 'executing' }), [])
|
|
441
|
+
|
|
442
|
+
const cases: Array<[Record<string, unknown>, string]> = [
|
|
443
|
+
[{ ...stats, blocked: stats.blocked + 1 }, 'stats.blocked'],
|
|
444
|
+
[{ ...stats, inProgress: 0 }, 'stats.inProgress'],
|
|
445
|
+
[{ ...stats, done: 99 }, 'stats.done'],
|
|
446
|
+
[{ ...stats, pending: 0 }, 'stats.pending'],
|
|
447
|
+
[{ ...stats, total: 99 }, 'stats.total'],
|
|
448
|
+
[{ ...stats, progress: stats.progress + 1 }, 'stats.progress'],
|
|
449
|
+
]
|
|
450
|
+
for (const [broken, code] of cases) {
|
|
451
|
+
const codes = findStatsViolations(statuses, broken).map((item) => item.code)
|
|
452
|
+
assert.ok(codes.includes(code), `期望 ${code},实际 ${codes.join(',')}`)
|
|
453
|
+
}
|
|
454
|
+
})
|
|
455
|
+
|
|
456
|
+
test('stats 必须逐项等于 computeStats(statuses),不能是缓存快照(D1/D2/D4)', () => {
|
|
457
|
+
const statuses = statsFixture()
|
|
458
|
+
const stale = { total: 4, done: 1, blocked: 1, inProgress: 0, pending: 2, progress: 10 }
|
|
459
|
+
const codes = findStatsViolations(statuses, stale).map((item) => item.code)
|
|
460
|
+
assert.ok(codes.includes('stats.inProgress'))
|
|
461
|
+
assert.ok(codes.includes('stats.pending'))
|
|
462
|
+
assert.ok(codes.includes('stats.progress'))
|
|
463
|
+
assert.deepEqual(findStatsViolations(statuses, computeStats(normaliseStatuses(statuses))), [])
|
|
464
|
+
})
|
|
465
|
+
|
|
466
|
+
test('departments 与 stats 同源:任一部门计数漂移、增删部门都被捕获', () => {
|
|
467
|
+
const statuses = statsFixture()
|
|
468
|
+
assert.deepEqual(findDepartmentViolations(statuses, DEPARTMENTS), [])
|
|
469
|
+
|
|
470
|
+
const wrong = DEPARTMENTS.map((item) => (item.id === 'qa' ? { ...item, inProgress: 0 } : item))
|
|
471
|
+
assert.ok(findDepartmentViolations(statuses, wrong).some((item) => item.code === 'departments.inProgress'))
|
|
472
|
+
assert.ok(
|
|
473
|
+
findDepartmentViolations(statuses, [...DEPARTMENTS, { id: 'ops', total: 0, done: 0, blocked: 0, inProgress: 0 }]).some(
|
|
474
|
+
(item) => item.code === 'departments.extra',
|
|
475
|
+
),
|
|
476
|
+
)
|
|
477
|
+
assert.ok(findDepartmentViolations(statuses, [DEPARTMENTS[0]]).some((item) => item.code === 'departments.missing'))
|
|
478
|
+
})
|
|
479
|
+
|
|
480
|
+
test('phase 与任务集合相容:落定阶段不得仍有任务在运行', () => {
|
|
481
|
+
const allDone = [withId(sample('done'), 'T1'), withId(sample('done'), 'T2')]
|
|
482
|
+
assert.deepEqual(findPhaseViolations('done', allDone), [])
|
|
483
|
+
assert.deepEqual(findPhaseViolations('integrating', allDone), [])
|
|
484
|
+
|
|
485
|
+
const running = statsFixture()
|
|
486
|
+
assert.ok(findPhaseViolations('done', running).some((item) => item.code === 'phase.done'))
|
|
487
|
+
for (const phase of ['paused', 'blocked', 'cancelled'] as const) {
|
|
488
|
+
assert.ok(
|
|
489
|
+
findPhaseViolations(phase, running).some((item) => item.code === 'phase.terminal.inProgress'),
|
|
490
|
+
`${phase} 未捕获运行中的任务`,
|
|
491
|
+
)
|
|
492
|
+
}
|
|
493
|
+
assert.ok(findPhaseViolations('pending', [withId(sample('done'), 'T1')]).some((item) => item.code === 'phase.pending'))
|
|
494
|
+
assert.ok(findPhaseViolations('running', allDone).some((item) => item.code === 'phase.invalid'))
|
|
495
|
+
assert.ok(findPhaseViolations('done', []).some((item) => item.code === 'phase.done.empty'))
|
|
496
|
+
})
|
|
497
|
+
|
|
498
|
+
test('planPhase 对已落定的状态集合与不变量相容', () => {
|
|
499
|
+
const cases: StatusEntry[][] = [
|
|
500
|
+
[withId(sample('done'), 'T1'), withId(sample('done'), 'T2')],
|
|
501
|
+
[withId(sample('blocked'), 'T1'), withId(sample('pending'), 'T2')],
|
|
502
|
+
[withId(sample('in_progress'), 'T1'), withId(sample('pending'), 'T2')],
|
|
503
|
+
[withId(sample('pending'), 'T1')],
|
|
504
|
+
]
|
|
505
|
+
for (const statuses of cases) {
|
|
506
|
+
assert.deepEqual(findPhaseViolations(planPhase(statuses), statuses), [])
|
|
507
|
+
}
|
|
508
|
+
assert.equal(planPhase([withId(sample('done'), 'T1')]), 'integrating')
|
|
509
|
+
assert.equal(planPhase([withId(sample('done'), 'T1')], true), 'done')
|
|
510
|
+
})
|
|
511
|
+
|
|
512
|
+
test('assertInvariants 汇聚全部违规并抛出 InvariantError', () => {
|
|
513
|
+
const statuses = statsFixture()
|
|
514
|
+
const dirty = { ...computeStats(statuses), done: 99 }
|
|
515
|
+
assert.throws(
|
|
516
|
+
() => assertInvariants({ statuses, stats: dirty, departments: DEPARTMENTS, phase: 'done' }),
|
|
517
|
+
(error: unknown) =>
|
|
518
|
+
error instanceof InvariantError &&
|
|
519
|
+
error.violations.some((item) => item.code === 'stats.done') &&
|
|
520
|
+
error.violations.some((item) => item.code === 'phase.done') &&
|
|
521
|
+
error.message.includes('stats.done'),
|
|
522
|
+
)
|
|
523
|
+
})
|
|
524
|
+
|
|
525
|
+
/* ------------------------------------------------------------------ *
|
|
526
|
+
* §7 计划阶段状态机
|
|
527
|
+
* ------------------------------------------------------------------ */
|
|
528
|
+
|
|
529
|
+
test('phase 主路径:pending → planning → executing → paused → executing → blocked → integrating → done', () => {
|
|
530
|
+
let phase: PlanPhase = 'pending'
|
|
531
|
+
phase = applyPhaseEvent(phase, 'defer')
|
|
532
|
+
assert.equal(phase, 'planning')
|
|
533
|
+
phase = applyPhaseEvent(phase, 'dispatch')
|
|
534
|
+
assert.equal(phase, 'executing')
|
|
535
|
+
phase = applyPhaseEvent(phase, 'round-paused')
|
|
536
|
+
assert.equal(phase, 'paused')
|
|
537
|
+
phase = applyPhaseEvent(phase, 'dispatch')
|
|
538
|
+
assert.equal(phase, 'executing')
|
|
539
|
+
phase = applyPhaseEvent(phase, 'round-blocked')
|
|
540
|
+
assert.equal(phase, 'blocked')
|
|
541
|
+
phase = applyPhaseEvent(phase, 'dispatch')
|
|
542
|
+
assert.equal(phase, 'executing')
|
|
543
|
+
phase = applyPhaseEvent(phase, 'round-completed')
|
|
544
|
+
assert.equal(phase, 'integrating')
|
|
545
|
+
phase = applyPhaseEvent(phase, 'result-written')
|
|
546
|
+
assert.equal(phase, 'done')
|
|
547
|
+
})
|
|
548
|
+
|
|
549
|
+
test('phase 旁路:取消、降级、手动完成全部、终态封闭', () => {
|
|
550
|
+
for (const phase of ['pending', 'planning', 'executing', 'paused', 'blocked', 'integrating'] as const) {
|
|
551
|
+
assert.equal(applyPhaseEvent(phase, 'cancel'), 'cancelled')
|
|
552
|
+
}
|
|
553
|
+
assert.equal(applyPhaseEvent('pending', 'degrade'), 'degraded')
|
|
554
|
+
assert.equal(applyPhaseEvent('planning', 'degrade'), 'degraded')
|
|
555
|
+
assert.equal(applyPhaseEvent('executing', 'manual-completed-all'), 'integrating')
|
|
556
|
+
assert.equal(applyPhaseEvent('blocked', 'manual-completed-all'), 'integrating')
|
|
557
|
+
assert.equal(applyPhaseEvent('integrating', 'result-written'), 'done')
|
|
558
|
+
|
|
559
|
+
assert.deepEqual(allowedPhaseEvents('done'), [])
|
|
560
|
+
assert.deepEqual(allowedPhaseEvents('cancelled'), [])
|
|
561
|
+
assert.deepEqual(allowedPhaseEvents('degraded'), [])
|
|
562
|
+
for (const phase of ['done', 'cancelled', 'degraded'] as const) {
|
|
563
|
+
assert.throws(() => applyPhaseEvent(phase, 'dispatch'), IllegalPhaseTransitionError)
|
|
564
|
+
}
|
|
565
|
+
assert.throws(() => applyPhaseEvent('executing', 'result-written'), IllegalPhaseTransitionError)
|
|
566
|
+
})
|
|
567
|
+
|
|
568
|
+
test('phase 矩阵覆盖 9 个阶段 × 9 个事件,无第三种结果', () => {
|
|
569
|
+
let legal = 0
|
|
570
|
+
for (const phase of PLAN_PHASES) {
|
|
571
|
+
for (const event of PLAN_PHASE_EVENTS) {
|
|
572
|
+
try {
|
|
573
|
+
const next = applyPhaseEvent(phase, event)
|
|
574
|
+
legal += 1
|
|
575
|
+
assert.ok(isPlanPhase(next), `phase 迁移产生了非法阶段:${String(next)}`)
|
|
576
|
+
} catch (error) {
|
|
577
|
+
assert.ok(
|
|
578
|
+
error instanceof IllegalPhaseTransitionError,
|
|
579
|
+
`${phase} × ${event} 抛出了非契约错误:${error instanceof Error ? error.message : String(error)}`,
|
|
580
|
+
)
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
const allowedTotal = PLAN_PHASES.reduce((sum, phase) => sum + allowedPhaseEvents(phase).length, 0)
|
|
585
|
+
assert.equal(legal, allowedTotal)
|
|
586
|
+
})
|
|
587
|
+
|
|
588
|
+
test('phase 输入校验:未知阶段/事件明确报错', () => {
|
|
589
|
+
assert.throws(
|
|
590
|
+
() => applyPhaseEvent('running', 'dispatch'),
|
|
591
|
+
(error: unknown) => error instanceof StateMachineError && error.code === 'INVALID_PHASE',
|
|
592
|
+
)
|
|
593
|
+
assert.throws(
|
|
594
|
+
() => applyPhaseEvent('pending', 'launch'),
|
|
595
|
+
(error: unknown) => error instanceof StateMachineError && error.code === 'INVALID_PHASE_EVENT',
|
|
596
|
+
)
|
|
597
|
+
assert.equal(isSubtaskStatus('pending'), true)
|
|
598
|
+
assert.equal(isSubtaskStatus('running'), false)
|
|
599
|
+
})
|
|
600
|
+
|
|
601
|
+
/* ------------------------------------------------------------------ *
|
|
602
|
+
* §8 直接复刻 T2 的三个真实缺陷输入
|
|
603
|
+
* ------------------------------------------------------------------ */
|
|
604
|
+
|
|
605
|
+
test('D3 复刻:预算暂停的 pending+progress=100+childId 被归一化并被断言拦截', () => {
|
|
606
|
+
const dirty = base({
|
|
607
|
+
status: 'pending',
|
|
608
|
+
progress: 100,
|
|
609
|
+
childId: 'child-hang',
|
|
610
|
+
startedAt: NOW,
|
|
611
|
+
finishedAt: null,
|
|
612
|
+
summary: '',
|
|
613
|
+
error: '本轮预算用尽,等待续跑',
|
|
614
|
+
})
|
|
615
|
+
const codes = findEntryViolations(dirty).map((item) => item.code)
|
|
616
|
+
assert.ok(codes.includes('pending.progress'))
|
|
617
|
+
assert.ok(codes.includes('pending.childId'))
|
|
618
|
+
assert.ok(codes.includes('pending.startedAt'))
|
|
619
|
+
assert.ok(codes.includes('pending.error'))
|
|
620
|
+
|
|
621
|
+
const clean = normaliseEntry(dirty, { now: NOW })
|
|
622
|
+
const stats = computeStats([clean])
|
|
623
|
+
assert.equal(clean.progress, 0)
|
|
624
|
+
assert.equal(clean.childId, null)
|
|
625
|
+
assert.equal(stats.progress, 0)
|
|
626
|
+
assert.equal(stats.done, 0)
|
|
627
|
+
assert.deepEqual(collectViolations({ statuses: [clean], stats, phase: 'paused' }), [])
|
|
628
|
+
})
|
|
629
|
+
|
|
630
|
+
test('D4 复刻:resume 后 blocked 计数必须归零,且不允许残留 in_progress 字段', () => {
|
|
631
|
+
const before = [withId(sample('blocked'), 'T1'), withId(sample('blocked'), 'T2')]
|
|
632
|
+
assert.equal(computeStats(before).blocked, 2)
|
|
633
|
+
|
|
634
|
+
const after = applyPlanOperation(before, 'resume', { now: NOW })
|
|
635
|
+
const stats = computeStats(after)
|
|
636
|
+
assert.equal(stats.blocked, 0)
|
|
637
|
+
assert.equal(stats.inProgress, 0)
|
|
638
|
+
assert.equal(stats.pending, 2)
|
|
639
|
+
assert.deepEqual(findStatusViolations(after), [])
|
|
640
|
+
assert.deepEqual(collectViolations({ statuses: after, stats, phase: 'executing' }), [])
|
|
641
|
+
})
|
|
642
|
+
|
|
643
|
+
test('D1/D2 复刻:mid-wave 状态与派生 stats 只可能来自同一份数组', () => {
|
|
644
|
+
const statuses = [
|
|
645
|
+
withId(sample('done'), 'T1'),
|
|
646
|
+
withId(sample('done'), 'T2'),
|
|
647
|
+
withId({ ...sample('in_progress'), progress: 10 }, 'T3'),
|
|
648
|
+
]
|
|
649
|
+
const stats = computeStats(statuses)
|
|
650
|
+
assert.deepEqual(stats, { total: 3, done: 2, blocked: 0, inProgress: 1, pending: 0, progress: 70 })
|
|
651
|
+
assert.deepEqual(collectViolations({ statuses, stats, phase: 'executing' }), [])
|
|
652
|
+
|
|
653
|
+
const snapshot = { total: 3, done: 1, blocked: 0, inProgress: 0, pending: 2, progress: 33 }
|
|
654
|
+
assert.ok(findStatsViolations(statuses, snapshot).length >= 3)
|
|
655
|
+
})
|