dsh-progress-union 0.1.6 → 0.1.7

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.
@@ -0,0 +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
+ })
@@ -0,0 +1,147 @@
1
+ /**
2
+ * Phase 2 验收:时间引擎(事件层 + ETA 三档回落 + 协议收敛 + cooldown/stale)。
3
+ */
4
+ import assert from 'node:assert/strict'
5
+ import fs from 'node:fs'
6
+ import os from 'node:os'
7
+ import path from 'node:path'
8
+ import test from 'node:test'
9
+
10
+ process.env.DSH_HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'union-test-'))
11
+ const { SESSION, createHarness, jsonReq, callRoute, withClock } = await import('./harness.mjs')
12
+
13
+ async function booted() {
14
+ const harness = createHarness()
15
+ await harness.run()
16
+ await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
17
+ return harness
18
+ }
19
+
20
+ function execUpdate(harness, args) {
21
+ const tool = harness.toolMap.get('progress_update')
22
+ return tool.definition.execute(args, { agent: harness.agent })
23
+ }
24
+
25
+ function anchorOf(harness, atMs) {
26
+ return harness.provided.get('progressAnchor').get(SESSION, atMs)
27
+ }
28
+
29
+ test('turn/start + step/start 驱动 steps/elapsed;无板无 todo 走 prior', async () => {
30
+ const harness = createHarness()
31
+ await harness.run()
32
+ harness.emit('turn/start', {})
33
+ for (let i = 0; i < 4; i++) harness.emit('step/start', {})
34
+ const started = anchorOf(harness).time.startedAt
35
+ assert.ok(started > 0)
36
+ const at = started + 60_000
37
+ const anchor = anchorOf(harness, at)
38
+ assert.equal(anchor.time.steps, 4)
39
+ assert.equal(anchor.time.elapsedSec, 60)
40
+ assert.equal(anchor.time.etaBasis, 'prior') // 临时 DSH_HOME 无学习样本
41
+ assert.equal(anchor.time.todoTotal, 0)
42
+ })
43
+
44
+ test('board 档最优先:阶段取板、blocked 不计剩余', async () => {
45
+ const harness = await booted()
46
+ const added = await execUpdate(harness, {
47
+ changes: [
48
+ { title: '甲', status: 'done' },
49
+ { title: '乙', status: 'blocked', note: '等人确认' },
50
+ { title: '丙', status: 'todo' },
51
+ ],
52
+ })
53
+ harness.emit('turn/start', {})
54
+ const started = anchorOf(harness).time.startedAt
55
+ const at = started + 60_000 // elapsed 60s, done=1 → perItemSec=60, left=1(丙) → p50=60
56
+ const anchor = anchorOf(harness, at)
57
+ assert.equal(anchor.time.etaBasis, 'board')
58
+ assert.equal(anchor.time.elapsedSec, 60)
59
+ // p50=60 → remain=max(5,60-36)/60=0.4min → etaMin=max(0,round(.4))=0
60
+ assert.equal(anchor.time.etaMin, 0)
61
+ // inFlight 含 blocked(等人不是等时间,但要出现在锚点)
62
+ assert.deepEqual(anchor.board.inFlight.map((i) => i.status), ['blocked'])
63
+ assert.equal(anchor.board.summary.total, 3)
64
+ assert.equal(anchor.board.summary.done, 1)
65
+ void added
66
+
67
+ // /dsh-progress/status:板开时阶段直接取 items 标题(不再关键词猜测)
68
+ const res = await callRoute(harness, '/dsh-progress/status', { method: 'GET', url: '/dsh-progress/status', headers: {} })
69
+ const task = res.json().task
70
+ assert.deepEqual(task.stages, ['甲', '乙', '丙'])
71
+ assert.equal(task.boardDriven, true)
72
+ assert.equal(task.autoStage, false)
73
+ })
74
+
75
+ test('todo 档:板关闭时用官方 todo 完成度', async () => {
76
+ const harness = createHarness()
77
+ await harness.run()
78
+ harness.emit('turn/start', {})
79
+ harness.emit('todo/write', { todos: [{ content: 'a', status: 'completed' }, { content: 'b', status: 'completed' }, { content: 'c', status: 'pending' }, { content: 'd', status: 'pending' }] })
80
+ const started = anchorOf(harness).time.startedAt
81
+ const at = started + 120_000 // perItemSec=60, left=2 → p50=120
82
+ const anchor = anchorOf(harness, at)
83
+ assert.equal(anchor.time.etaBasis, 'todo')
84
+ assert.equal(anchor.time.todoDone, 2)
85
+ assert.equal(anchor.time.todoTotal, 4)
86
+ // p50=120, elapsed=120 → max(5,120-72)=48s → etaMin=1
87
+ assert.equal(anchor.time.etaMin, 1)
88
+ })
89
+
90
+ test('kickoff 冷却:全完成后一回合只推一次', async () => {
91
+ const harness = await booted()
92
+ await execUpdate(harness, { changes: [{ title: '唯一任务', status: 'done' }] })
93
+ const kickoff = harness.contexts.get('progress-union-kickoff')
94
+ const first = kickoff()
95
+ assert.match(first, /已全部完成/)
96
+ assert.equal(kickoff(), '', '60s 冷却内不得重复推')
97
+ void harness
98
+ })
99
+
100
+ test('stale:板开 13 分钟未更新 → anchor.time.staleMin ≥ 11', async () => {
101
+ const harness = await booted()
102
+ await execUpdate(harness, { changes: [{ title: '任务', status: 'doing', current: '在做' }] })
103
+ const base = Date.now()
104
+ const anchor = anchorOf(harness, base + 13 * 60 * 1000)
105
+ assert.equal(anchor.time.staleMin, 13)
106
+ assert.equal(anchor.board.inFlight[0].status, 'doing')
107
+ })
108
+
109
+ test('单协议收敛:板开不注入文件上报协议,板关注入且 kind=plugin:dsh-progress-union', async () => {
110
+ const clock = withClock()
111
+ try {
112
+ // 板开
113
+ const on = await booted()
114
+ const preStep = on.agentHooks.find((h) => h.type === 'agent/pre-step')
115
+ assert.ok(preStep, 'agent/created 应装 pre-step 钩子')
116
+ const freshUser = { source: { kind: 'user' } }
117
+ const decision = { kind: 'enter', messages: [freshUser] }
118
+ const out = await preStep.handler({ messages: [freshUser] }, async () => decision)
119
+ assert.equal(out.messages.length, 1, '板开时不得追加协议消息')
120
+
121
+ // 板关(全新 harness 不启用)
122
+ const off = createHarness()
123
+ await off.run()
124
+ const preStepOff = off.agentHooks.find((h) => h.type === 'agent/pre-step')
125
+ const decisionOff = { kind: 'enter', messages: [{ source: { kind: 'user' } }] }
126
+ const outOff = await preStepOff.handler({ messages: [{ source: { kind: 'user' } }] }, async () => decisionOff)
127
+ assert.equal(outOff.messages.length, 2)
128
+ const injected = outOff.messages[1]
129
+ assert.equal(injected.source.kind, 'plugin:dsh-progress-union')
130
+ assert.match(injected.content[0].text, /dsh-progress/)
131
+ } finally {
132
+ clock.restore()
133
+ }
134
+ })
135
+
136
+ test('turn/end 收任务并学样本;结束后锚点时间层归零但不丢 steps', async () => {
137
+ const harness = createHarness()
138
+ await harness.run()
139
+ harness.emit('turn/start', {})
140
+ harness.emit('step/start', {})
141
+ harness.emit('step/start', {})
142
+ harness.emit('turn/end', {})
143
+ const anchor = anchorOf(harness, Date.now() + 3600_000)
144
+ assert.equal(anchor.time.startedAt, 0, '任务已结束不应再给进行中锚')
145
+ assert.equal(anchor.time.etaBasis, 'none')
146
+ assert.equal(anchor.time.elapsedSec, 0)
147
+ })