dsh-progress-union 0.4.0 → 0.5.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 +146 -119
- package/README.md +28 -0
- package/client.js +1 -1
- package/docs/consumer-guide.md +210 -210
- package/dsh.plugin.json +1 -1
- package/index.js +545 -30
- package/package.json +1 -1
- package/test/client.test.mjs +5 -1
- package/test/consumer.test.mjs +101 -101
- package/test/goal.test.mjs +110 -110
- package/test/harness.mjs +6 -2
- package/test/host.test.mjs +67 -0
- package/test/plan.test.mjs +274 -0
- package/test/settings.test.mjs +4 -1
- package/test/stats.test.mjs +54 -54
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* v0.5.0 验收:plan 文档(codeplan 的 spec/tasks/checklist)解析 + 目录选择 + 零注入门。
|
|
3
|
+
* - parseChecklistMarkdown:Phase 分组 / 两种勾选态 / t\d+ id / ✅ 证据尾 / 裸列表
|
|
4
|
+
* - parseSpecOutline:H1–H3 骨架 + 首节行 + 上限
|
|
5
|
+
* - planDirCandidates / pickPlanDir:mtime 最新、显式 slug 优先、目录没了不换别的
|
|
6
|
+
* - sessionCwd:cwd 在 session **顶层**(实测),header 只作兼容兜底
|
|
7
|
+
*/
|
|
8
|
+
import assert from 'node:assert/strict'
|
|
9
|
+
import fs from 'node:fs'
|
|
10
|
+
import os from 'node:os'
|
|
11
|
+
import path from 'node:path'
|
|
12
|
+
import test from 'node:test'
|
|
13
|
+
|
|
14
|
+
process.env.DSH_HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'union-plan-test-'))
|
|
15
|
+
const { SESSION, createHarness, jsonReq, callRoute, flush } = await import('./harness.mjs')
|
|
16
|
+
const { parseChecklistMarkdown, parseSpecOutline, normalizeStatus, planDirCandidates, pickPlanDir } =
|
|
17
|
+
await import('../index.js')
|
|
18
|
+
|
|
19
|
+
async function bootedWithPlan({ config = {}, planBody = '## Phase 1: 做点事\n- [ ] t01 第一件事\n- [x] t02 第二件事\n' } = {}) {
|
|
20
|
+
const cwd = fs.mkdtempSync(path.join(os.tmpdir(), 'union-plan-e2e-'))
|
|
21
|
+
const dir = path.join(cwd, '.agents', 'plans', 'demo-plan')
|
|
22
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
23
|
+
fs.writeFileSync(path.join(dir, 'tasks.md'), planBody, 'utf8')
|
|
24
|
+
fs.writeFileSync(path.join(dir, 'spec.md'), '# Spec: 演示\n\n## 需求\n- 让助手读计划\n', 'utf8')
|
|
25
|
+
const harness = createHarness({ cwd, config })
|
|
26
|
+
await harness.run()
|
|
27
|
+
await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
|
|
28
|
+
return { harness, cwd }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function observerOf(harness) {
|
|
32
|
+
return harness.contexts.get('progress-union-observer')()
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
test('端到端:plan 文档进观察块(planSync=read 默认)', async () => {
|
|
36
|
+
const { harness } = await bootedWithPlan()
|
|
37
|
+
// 观察块门控要求板上有活干
|
|
38
|
+
await harness.toolMap.get('progress_update').definition.execute(
|
|
39
|
+
{ changes: [{ title: '做点事', status: 'doing' }] },
|
|
40
|
+
{ agent: harness.agent },
|
|
41
|
+
)
|
|
42
|
+
const text = observerOf(harness)
|
|
43
|
+
assert.match(text, /\[plan\] demo-plan/, 'plan 行必须出现')
|
|
44
|
+
assert.match(text, /勾选 1\/2/, '勾选计数来自 tasks.md')
|
|
45
|
+
assert.match(text, /当前 Phase 1: 做点事/, '当前 Phase 取第一个含未勾项的 Phase')
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
test('端到端:planSync=off 时观察块零 plan 注入(0 token)', async () => {
|
|
49
|
+
const { harness } = await bootedWithPlan({ config: { planSync: 'off' } })
|
|
50
|
+
await harness.toolMap.get('progress_update').definition.execute(
|
|
51
|
+
{ changes: [{ title: '做点事', status: 'doing' }] },
|
|
52
|
+
{ agent: harness.agent },
|
|
53
|
+
)
|
|
54
|
+
const text = observerOf(harness)
|
|
55
|
+
assert.doesNotMatch(text, /\[plan\]/, 'off 时不得出现 plan 行')
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
test('端到端:cwd 拿不到(无 session.cwd)时静默不注入,不抛错', async () => {
|
|
59
|
+
const harness = createHarness() // 不给 cwd
|
|
60
|
+
await harness.run()
|
|
61
|
+
await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
|
|
62
|
+
await harness.toolMap.get('progress_update').definition.execute(
|
|
63
|
+
{ changes: [{ title: '做点事', status: 'doing' }] },
|
|
64
|
+
{ agent: harness.agent },
|
|
65
|
+
)
|
|
66
|
+
assert.doesNotMatch(observerOf(harness), /\[plan\]/)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test('/progress plan <slug>:绑定不存在的 slug 报错并列出候选', async () => {
|
|
70
|
+
const { harness } = await bootedWithPlan()
|
|
71
|
+
const cmd = harness.commandMap.get('progress')
|
|
72
|
+
const bad = cmd.handler({ agent: harness.agent, rawInput: 'plan ghost-plan' })
|
|
73
|
+
assert.equal(bad.kind, 'error')
|
|
74
|
+
assert.match(bad.text, /找不到计划/)
|
|
75
|
+
assert.match(bad.text, /demo-plan/, '报错要列出可用候选')
|
|
76
|
+
|
|
77
|
+
const ok = cmd.handler({ agent: harness.agent, rawInput: 'plan demo-plan' })
|
|
78
|
+
assert.equal(ok.kind, 'success')
|
|
79
|
+
const off = cmd.handler({ agent: harness.agent, rawInput: 'plan off' })
|
|
80
|
+
assert.equal(off.kind, 'success')
|
|
81
|
+
assert.match(off.text, /已解绑/)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test('助手输入带 <plan> 段(planBrief 是任务细化原料)', async () => {
|
|
85
|
+
// 助手输入与观察块共用同一个 scanPlan:这里用 direct 助手通道截获真实请求体,
|
|
86
|
+
// 断言 <plan> 段确实进了 user 消息(不是只进观察块)。
|
|
87
|
+
const { harness, cwd } = await bootedWithPlan({
|
|
88
|
+
config: { assistant: { mode: 'direct', baseUrl: 'http://x/v1', model: 'm', authority: 'slots', minIntervalMs: 0, autoApply: true } },
|
|
89
|
+
})
|
|
90
|
+
await harness.toolMap.get('progress_update').definition.execute(
|
|
91
|
+
{ changes: [{ title: '做点事', status: 'doing' }] },
|
|
92
|
+
{ agent: harness.agent },
|
|
93
|
+
)
|
|
94
|
+
const seen = []
|
|
95
|
+
const original = globalThis.fetch
|
|
96
|
+
globalThis.fetch = async (_url, init) => {
|
|
97
|
+
seen.push(String(init && init.body || ''))
|
|
98
|
+
return { ok: true, status: 200, async json() { return { choices: [{ message: { content: '{"board":{"changes":[]},"round":{"status":"continue"},"note":"ok"}' } }] } } }
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
// 模型久未同步:推进若干步让 maybeAskAssistant 触发
|
|
102
|
+
let now = Date.now()
|
|
103
|
+
for (let i = 0; i < 7; i += 1) {
|
|
104
|
+
now += 200000 // 越过 maxIdleMs
|
|
105
|
+
harness.emit('step/start', {})
|
|
106
|
+
}
|
|
107
|
+
await new Promise((r) => setTimeout(r, 120))
|
|
108
|
+
await flush()
|
|
109
|
+
const joined = seen.join('\n')
|
|
110
|
+
assert.ok(seen.length > 0, `助手应被调用至少一次(实得 ${seen.length});cwd=${cwd}`)
|
|
111
|
+
// 注意 body 是 JSON 字符串,引号已转义 → 用 includes 而不是正则
|
|
112
|
+
assert.ok(joined.includes('<plan slug=\\"demo-plan\\"'), '助手输入必须带 plan 段')
|
|
113
|
+
assert.ok(joined.includes('勾选:1/2'), '勾选计数来自 tasks.md')
|
|
114
|
+
assert.ok(joined.includes('Phase 1: 做点事 1/2'), '各 Phase 进度也要在')
|
|
115
|
+
assert.ok(joined.includes('未完:t01'), '未完项带任务 id')
|
|
116
|
+
assert.ok(joined.includes('spec 大纲'), 'spec 大纲也要在')
|
|
117
|
+
assert.ok(joined.includes('</plan>'), '段闭合')
|
|
118
|
+
} finally {
|
|
119
|
+
globalThis.fetch = original
|
|
120
|
+
}
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
test('parseChecklistMarkdown:Phase 分组 + 两态勾选 + id 提取', () => {
|
|
125
|
+
const md = [
|
|
126
|
+
'# Tasks',
|
|
127
|
+
'',
|
|
128
|
+
'> 勾选点位约定:`- [ ]`→`- [x]`',
|
|
129
|
+
'',
|
|
130
|
+
'## Phase 1: 基础设施',
|
|
131
|
+
'- [ ] t01 搭建项目结构',
|
|
132
|
+
'- [x] t02 配置依赖',
|
|
133
|
+
'',
|
|
134
|
+
'## Phase 2: 核心实现',
|
|
135
|
+
'- [ ] t03 实现核心逻辑',
|
|
136
|
+
].join('\n')
|
|
137
|
+
const r = parseChecklistMarkdown(md)
|
|
138
|
+
assert.deepEqual(r.counts, { total: 3, done: 1, pending: 2 })
|
|
139
|
+
const phases = r.phases.filter((p) => p.title)
|
|
140
|
+
assert.deepEqual(phases.map((p) => p.title), ['Phase 1: 基础设施', 'Phase 2: 核心实现'])
|
|
141
|
+
assert.equal(phases[0].tasks.length, 2)
|
|
142
|
+
assert.equal(phases[0].tasks[0].id, 't01')
|
|
143
|
+
assert.equal(phases[0].tasks[0].text, '搭建项目结构')
|
|
144
|
+
assert.equal(phases[0].tasks[0].checked, false)
|
|
145
|
+
assert.equal(phases[0].tasks[1].checked, true)
|
|
146
|
+
assert.equal(phases[1].tasks[0].id, 't03')
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
test('parseChecklistMarkdown:✅ 证据尾只在勾选态剥离;未勾选保留原文', () => {
|
|
150
|
+
const md = [
|
|
151
|
+
'## Must Pass',
|
|
152
|
+
'- [x] 核心功能测试 ✅ test 12/12 pass(2026-11-26)',
|
|
153
|
+
'- [ ] 边界条件处理 ✅ 这不该被当证据',
|
|
154
|
+
].join('\n')
|
|
155
|
+
const r = parseChecklistMarkdown(md)
|
|
156
|
+
const [done, pending] = r.phases[0].tasks
|
|
157
|
+
assert.equal(done.text, '核心功能测试')
|
|
158
|
+
assert.match(done.evidence, /test 12\/12 pass/)
|
|
159
|
+
// 未勾选:不剥尾,整行当正文(否则会误删正文里的 ✅)
|
|
160
|
+
assert.match(pending.text, /这不该被当证据/)
|
|
161
|
+
assert.equal(pending.evidence, '')
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
test('parseChecklistMarkdown:无标题裸列表落 phase="";`* [X]` 与大小写容错', () => {
|
|
165
|
+
const md = ['- [X] 大写勾', '* [ ] 星号项'].join('\n')
|
|
166
|
+
const r = parseChecklistMarkdown(md)
|
|
167
|
+
assert.equal(r.phases.length, 1)
|
|
168
|
+
assert.equal(r.phases[0].title, '')
|
|
169
|
+
assert.equal(r.counts.total, 2)
|
|
170
|
+
assert.equal(r.counts.done, 1)
|
|
171
|
+
assert.equal(r.phases[0].tasks[1].text, '星号项')
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
test('parseChecklistMarkdown:空/坏输入不抛错', () => {
|
|
175
|
+
for (const input of ['', null, undefined, 'no lists here']) {
|
|
176
|
+
const r = parseChecklistMarkdown(input)
|
|
177
|
+
assert.deepEqual(r.counts, { total: 0, done: 0, pending: 0 })
|
|
178
|
+
}
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
test('parseSpecOutline:H1–H3 骨架 + 每节首行', () => {
|
|
182
|
+
const md = [
|
|
183
|
+
'# Spec: 某事',
|
|
184
|
+
'',
|
|
185
|
+
'## 需求',
|
|
186
|
+
'- [功能需求列表]',
|
|
187
|
+
'',
|
|
188
|
+
'## 技术方案',
|
|
189
|
+
'| 模块 | 改动 |',
|
|
190
|
+
'## 约束',
|
|
191
|
+
'- 不新增依赖',
|
|
192
|
+
].join('\n')
|
|
193
|
+
const out = parseSpecOutline(md)
|
|
194
|
+
assert.deepEqual(out.map((s) => s.title), ['Spec: 某事', '需求', '技术方案', '约束'])
|
|
195
|
+
assert.equal(out[1].first, '- [功能需求列表]')
|
|
196
|
+
// 表格行不当首行摘要
|
|
197
|
+
assert.equal(out[2].first, '')
|
|
198
|
+
assert.equal(out[3].first, '- 不新增依赖')
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
test('normalizeStatus:官方 todo 词表与常见写法都归一', () => {
|
|
202
|
+
const cases = {
|
|
203
|
+
pending: 'todo', 'not-started': 'todo', open: 'todo',
|
|
204
|
+
in_progress: 'doing', 'in-progress': 'doing', running: 'doing', WIP: 'doing',
|
|
205
|
+
completed: 'done', complete: 'done', FINISHED: 'done',
|
|
206
|
+
waiting: 'blocked', failed: 'blocked', stuck: 'blocked',
|
|
207
|
+
' doing ': 'doing',
|
|
208
|
+
}
|
|
209
|
+
for (const [raw, want] of Object.entries(cases)) assert.equal(normalizeStatus(raw), want, raw)
|
|
210
|
+
// 不认识 / 非字符串:undefined(由调用方决定宽容或抛错)
|
|
211
|
+
for (const raw of ['半吊子', '', null, undefined, 42]) assert.equal(normalizeStatus(raw), undefined, String(raw))
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
function mkPlan(cwd, slug, files) {
|
|
215
|
+
const dir = path.join(cwd, '.agents', 'plans', slug)
|
|
216
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
217
|
+
for (const [name, body] of Object.entries(files)) fs.writeFileSync(path.join(dir, name), body, 'utf8')
|
|
218
|
+
return dir
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
test('planDirCandidates / pickPlanDir:mtime 最新优先;无任务文件的目录被跳过', () => {
|
|
222
|
+
const cwd = fs.mkdtempSync(path.join(os.tmpdir(), 'union-plan-dir-'))
|
|
223
|
+
const older = mkPlan(cwd, 'old-plan', { 'tasks.md': '## A\n- [ ] t01 x\n' })
|
|
224
|
+
const newer = mkPlan(cwd, 'new-plan', { 'tasks.md': '## B\n- [ ] t02 y\n' })
|
|
225
|
+
// 造一个只有 spec.md 的目录:应被跳过(没有勾选表可喂)
|
|
226
|
+
mkPlan(cwd, 'spec-only', { 'spec.md': '# Spec\n## 需求\n' })
|
|
227
|
+
const past = Date.now() - 3600 * 1000
|
|
228
|
+
fs.utimesSync(path.join(older, 'tasks.md'), new Date(past), new Date(past))
|
|
229
|
+
const list = planDirCandidates(cwd)
|
|
230
|
+
assert.deepEqual(list.map((x) => x.slug), ['new-plan', 'old-plan'])
|
|
231
|
+
assert.equal(pickPlanDir(cwd).slug, 'new-plan')
|
|
232
|
+
assert.equal(pickPlanDir(cwd, 'old-plan').slug, 'old-plan')
|
|
233
|
+
// 显式绑了一个不存在的 slug:返回 null,不悄悄换成别的计划
|
|
234
|
+
assert.equal(pickPlanDir(cwd, 'ghost-plan'), null)
|
|
235
|
+
// cwd 为空/不存在:null,不抛
|
|
236
|
+
assert.equal(pickPlanDir(''), null)
|
|
237
|
+
assert.equal(pickPlanDir(path.join(cwd, 'nope')), null)
|
|
238
|
+
void newer
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
test('sync 回勾:板项 done 且正文命中 → tasks.md 从 [ ] 变 [x] 并追加 ✅ 证据', async () => {
|
|
242
|
+
const { harness } = await bootedWithPlan({ config: { planSync: 'sync' } })
|
|
243
|
+
// 板项标题必须在 tasks.md 正文里出现,才会被回勾(不猜 id)
|
|
244
|
+
await harness.toolMap.get('progress_update').definition.execute(
|
|
245
|
+
{ changes: [{ title: '第一件事', status: 'done', current: '跑通了 12/12' }] },
|
|
246
|
+
{ agent: harness.agent },
|
|
247
|
+
)
|
|
248
|
+
const file = path.join(harness.agent.session.cwd, '.agents', 'plans', 'demo-plan', 'tasks.md')
|
|
249
|
+
const after = fs.readFileSync(file, 'utf8')
|
|
250
|
+
assert.match(after, /- \[x\] t01 第一件事 ✅(验证:跑通了 12\/12;\d{4}-\d{2}-\d{2})/, '应回勾并追加证据')
|
|
251
|
+
assert.match(after, /\n- \[x\] t02 第二件事/, '原有的勾选保持不变')
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
test('sync 回勾护栏:标题不匹配 / 已勾选的行绝不动', async () => {
|
|
255
|
+
const body = '## Phase 1\n- [ ] t01 完全无关的任务\n- [x] t02 已勾选的任务\n'
|
|
256
|
+
const { harness } = await bootedWithPlan({ config: { planSync: 'sync' }, planBody: body })
|
|
257
|
+
await harness.toolMap.get('progress_update').definition.execute(
|
|
258
|
+
{ changes: [{ title: '板里另起一条', status: 'done' }] },
|
|
259
|
+
{ agent: harness.agent },
|
|
260
|
+
)
|
|
261
|
+
const file = path.join(harness.agent.session.cwd, '.agents', 'plans', 'demo-plan', 'tasks.md')
|
|
262
|
+
assert.equal(fs.readFileSync(file, 'utf8'), body, '不命中就不写文件,一个字节都不改')
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
test('read 模式(默认)绝不写 tasks.md', async () => {
|
|
266
|
+
const body = '## Phase 1\n- [ ] t01 第一件事\n'
|
|
267
|
+
const { harness } = await bootedWithPlan({ planBody: body })
|
|
268
|
+
await harness.toolMap.get('progress_update').definition.execute(
|
|
269
|
+
{ changes: [{ title: '第一件事', status: 'done' }] },
|
|
270
|
+
{ agent: harness.agent },
|
|
271
|
+
)
|
|
272
|
+
const file = path.join(harness.agent.session.cwd, '.agents', 'plans', 'demo-plan', 'tasks.md')
|
|
273
|
+
assert.equal(fs.readFileSync(file, 'utf8'), body, '默认 read 模式不得写用户文件')
|
|
274
|
+
})
|
package/test/settings.test.mjs
CHANGED
|
@@ -11,6 +11,7 @@ import fs from 'node:fs'
|
|
|
11
11
|
import os from 'node:os'
|
|
12
12
|
import path from 'node:path'
|
|
13
13
|
import test from 'node:test'
|
|
14
|
+
import { fileURLToPath } from 'node:url'
|
|
14
15
|
|
|
15
16
|
process.env.DSH_HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'union-test-'))
|
|
16
17
|
const { Config } = await import('../index.js')
|
|
@@ -66,7 +67,9 @@ test('Config:外层对象不得自己标 volatile(嵌套 volatile 会被宿
|
|
|
66
67
|
})
|
|
67
68
|
|
|
68
69
|
test('客户端:设置卡双座位(settings.section + plugins.bundle.config)且走 configForms 句柄', () => {
|
|
69
|
-
|
|
70
|
+
// fileURLToPath:跨平台取文件路径。`new URL(...).pathname` 在 Linux 上会被
|
|
71
|
+
// `.replace(/^[/]/,'')` 误伤成相对路径(CI 上 ENOENT,本地 Windows 永不暴露)。
|
|
72
|
+
const file = fileURLToPath(new URL('../client.js', import.meta.url))
|
|
70
73
|
const src = fs.readFileSync(file, 'utf8')
|
|
71
74
|
assert.match(src, /registerSettingsSeat\('settings\.section'/, '必须注册 settings.section 槽')
|
|
72
75
|
assert.match(src, /id: 'dsh-progress-union'/, '分节 id 必须固定')
|
package/test/stats.test.mjs
CHANGED
|
@@ -1,54 +1,54 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Should-Pass 验收:stats 学习链兼容——读鲸程(dsh-progress-task-bar)既有
|
|
3
|
-
* `$DSH_HOME/dsh-progress-stats.json` 样本:不迁移、不改路径、不损坏原样本,
|
|
4
|
-
* 新样本只在原数组尾部追加并按同一 schema 回写。
|
|
5
|
-
*/
|
|
6
|
-
import assert from 'node:assert/strict'
|
|
7
|
-
import fs from 'node:fs'
|
|
8
|
-
import os from 'node:os'
|
|
9
|
-
import path from 'node:path'
|
|
10
|
-
import test from 'node:test'
|
|
11
|
-
|
|
12
|
-
const HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'union-stats-'))
|
|
13
|
-
process.env.DSH_HOME = HOME
|
|
14
|
-
const STATS = path.join(HOME, 'dsh-progress-stats.json')
|
|
15
|
-
|
|
16
|
-
// 鲸程原样写出的文件:s=第几步,r=当时离任务结束还剩几秒。
|
|
17
|
-
const LEGACY = Array.from({ length: 25 }, (_, i) => ({ s: 1, r: 60 + i * 10 }))
|
|
18
|
-
fs.writeFileSync(STATS, JSON.stringify({ version: 1, updatedAt: 1000, samples: LEGACY }))
|
|
19
|
-
|
|
20
|
-
const { SESSION, createHarness, jsonReq, callRoute, withClock } = await import('./harness.mjs')
|
|
21
|
-
|
|
22
|
-
test('既有样本被读取成本机学习档,且新样本追加回写不损坏原样本', async () => {
|
|
23
|
-
const clock = withClock()
|
|
24
|
-
try {
|
|
25
|
-
const harness = createHarness()
|
|
26
|
-
await harness.run()
|
|
27
|
-
await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
|
|
28
|
-
|
|
29
|
-
harness.emit('turn/start', {})
|
|
30
|
-
harness.emit('step/start', {})
|
|
31
|
-
clock.advance(30000)
|
|
32
|
-
|
|
33
|
-
// 25 条 s=1 样本 → bucket [1,2) 满 20 条,etaBasis 应从 'prior' 升到 'local'
|
|
34
|
-
const learned = harness.provided.get('progressAnchor').get(SESSION)
|
|
35
|
-
assert.equal(learned.time.etaBasis, 'local')
|
|
36
|
-
assert.equal(learned.time.etaP75Min, 4) // p75 = 240s
|
|
37
|
-
|
|
38
|
-
// 结束回合:产生一条真实样本 { s: 1, r: 30 }
|
|
39
|
-
harness.emit('turn/end', {})
|
|
40
|
-
|
|
41
|
-
const saved = JSON.parse(fs.readFileSync(STATS, 'utf8'))
|
|
42
|
-
assert.equal(saved.version, 1, 'schema version 保持 1(未迁移)')
|
|
43
|
-
assert.ok(saved.updatedAt > 1000, 'updatedAt 已刷新')
|
|
44
|
-
assert.deepEqual(saved.samples.slice(0, LEGACY.length), LEGACY, '既有 25 条原样保留')
|
|
45
|
-
assert.deepEqual(saved.samples.at(-1), { s: 1, r: 30 }, '新样本追加在尾部')
|
|
46
|
-
assert.equal(saved.samples.length, LEGACY.length + 1)
|
|
47
|
-
|
|
48
|
-
// 不迁移:DSH_HOME 里没有第二份 stats 文件
|
|
49
|
-
const extra = fs.readdirSync(HOME).filter((f) => /progress.*stats/.test(f) && f !== path.basename(STATS))
|
|
50
|
-
assert.deepEqual(extra, [], '没有生成迁移副本')
|
|
51
|
-
} finally {
|
|
52
|
-
clock.restore()
|
|
53
|
-
}
|
|
54
|
-
})
|
|
1
|
+
/**
|
|
2
|
+
* Should-Pass 验收:stats 学习链兼容——读鲸程(dsh-progress-task-bar)既有
|
|
3
|
+
* `$DSH_HOME/dsh-progress-stats.json` 样本:不迁移、不改路径、不损坏原样本,
|
|
4
|
+
* 新样本只在原数组尾部追加并按同一 schema 回写。
|
|
5
|
+
*/
|
|
6
|
+
import assert from 'node:assert/strict'
|
|
7
|
+
import fs from 'node:fs'
|
|
8
|
+
import os from 'node:os'
|
|
9
|
+
import path from 'node:path'
|
|
10
|
+
import test from 'node:test'
|
|
11
|
+
|
|
12
|
+
const HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'union-stats-'))
|
|
13
|
+
process.env.DSH_HOME = HOME
|
|
14
|
+
const STATS = path.join(HOME, 'dsh-progress-stats.json')
|
|
15
|
+
|
|
16
|
+
// 鲸程原样写出的文件:s=第几步,r=当时离任务结束还剩几秒。
|
|
17
|
+
const LEGACY = Array.from({ length: 25 }, (_, i) => ({ s: 1, r: 60 + i * 10 }))
|
|
18
|
+
fs.writeFileSync(STATS, JSON.stringify({ version: 1, updatedAt: 1000, samples: LEGACY }))
|
|
19
|
+
|
|
20
|
+
const { SESSION, createHarness, jsonReq, callRoute, withClock } = await import('./harness.mjs')
|
|
21
|
+
|
|
22
|
+
test('既有样本被读取成本机学习档,且新样本追加回写不损坏原样本', async () => {
|
|
23
|
+
const clock = withClock()
|
|
24
|
+
try {
|
|
25
|
+
const harness = createHarness()
|
|
26
|
+
await harness.run()
|
|
27
|
+
await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
|
|
28
|
+
|
|
29
|
+
harness.emit('turn/start', {})
|
|
30
|
+
harness.emit('step/start', {})
|
|
31
|
+
clock.advance(30000)
|
|
32
|
+
|
|
33
|
+
// 25 条 s=1 样本 → bucket [1,2) 满 20 条,etaBasis 应从 'prior' 升到 'local'
|
|
34
|
+
const learned = harness.provided.get('progressAnchor').get(SESSION)
|
|
35
|
+
assert.equal(learned.time.etaBasis, 'local')
|
|
36
|
+
assert.equal(learned.time.etaP75Min, 4) // p75 = 240s
|
|
37
|
+
|
|
38
|
+
// 结束回合:产生一条真实样本 { s: 1, r: 30 }
|
|
39
|
+
harness.emit('turn/end', {})
|
|
40
|
+
|
|
41
|
+
const saved = JSON.parse(fs.readFileSync(STATS, 'utf8'))
|
|
42
|
+
assert.equal(saved.version, 1, 'schema version 保持 1(未迁移)')
|
|
43
|
+
assert.ok(saved.updatedAt > 1000, 'updatedAt 已刷新')
|
|
44
|
+
assert.deepEqual(saved.samples.slice(0, LEGACY.length), LEGACY, '既有 25 条原样保留')
|
|
45
|
+
assert.deepEqual(saved.samples.at(-1), { s: 1, r: 30 }, '新样本追加在尾部')
|
|
46
|
+
assert.equal(saved.samples.length, LEGACY.length + 1)
|
|
47
|
+
|
|
48
|
+
// 不迁移:DSH_HOME 里没有第二份 stats 文件
|
|
49
|
+
const extra = fs.readdirSync(HOME).filter((f) => /progress.*stats/.test(f) && f !== path.basename(STATS))
|
|
50
|
+
assert.deepEqual(extra, [], '没有生成迁移副本')
|
|
51
|
+
} finally {
|
|
52
|
+
clock.restore()
|
|
53
|
+
}
|
|
54
|
+
})
|