dsh-progress-union 0.5.1 → 0.5.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/test/e2e.test.mjs CHANGED
@@ -1,77 +1,77 @@
1
- /**
2
- * Phase 5 端到端冒烟(task_22):建板 → 用户改 → 不 ack 读锚点 → ack 后再读 →
3
- * 时钟推进 12 分钟读 staleMin;另验 KV 缺席时锚点仍出且 persistent=false。
4
- */
5
- import assert from 'node:assert/strict'
6
- import fs from 'node:fs'
7
- import os from 'node:os'
8
- import path from 'node:path'
9
- import test from 'node:test'
10
-
11
- process.env.DSH_HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'union-test-'))
12
- const { SESSION, createHarness, jsonReq, callRoute, withClock } = await import('./harness.mjs')
13
-
14
- function execUpdate(harness, args) {
15
- return harness.toolMap.get('progress_update').definition.execute(args, { agent: harness.agent })
16
- }
17
-
18
- test('端到端:建板→用户改→ack→陈旧推进,锚点各阶段状态正确迁移', async () => {
19
- const harness = createHarness()
20
- await harness.run()
21
- await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
22
- const anchor = () => harness.provided.get('progressAnchor').get(SESSION)
23
-
24
- // 阶段 0:启用但空板
25
- assert.equal(anchor().board.summary.total, 0)
26
- assert.match(harness.contexts.get('progress-union-kickoff')(), /第一个工具调用必须是 progress_update/)
27
-
28
- // 阶段 1:建板(3 项,1 doing)
29
- const added = await execUpdate(harness, {
30
- changes: [{ title: '抓帧', status: 'doing', current: '渲染第 1/900 帧' }, { title: '合成' }, { title: '字幕' }],
31
- })
32
- assert.equal(added.summary.total, 3)
33
- assert.equal(anchor().board.inFlight.length, 1)
34
- assert.equal(anchor().board.inFlight[0].title, '抓帧')
35
-
36
- // 阶段 2:用户面板改"合成"备注 → pending=1,userEdits 出现,时间戳推进
37
- const itemId = (await callRoute(harness, '/progress-union/snapshot', { method: 'GET', url: `/progress-union/snapshot?sessionId=${SESSION}`, headers: {} })).json().items[1].id
38
- await callRoute(harness, '/progress-union/action', jsonReq('POST', '/progress-union/action', { sessionId: SESSION, changes: [{ id: itemId, note: '用 4K' }] }))
39
- const pending = anchor()
40
- assert.equal(pending.board.pendingUserChanges, 1)
41
- assert.equal(pending.userEdits[0].fields[0].field, 'note')
42
- assert.ok(pending.revision > added.revision)
43
-
44
- // 阶段 3:agent ack → pending=0,锚点 revision 不因确认增长
45
- const acked = await execUpdate(harness, { changes: [], ackUserRevision: pending.revision })
46
- assert.equal(acked.revision, pending.revision)
47
- assert.equal(anchor().board.pendingUserChanges, 0)
48
- assert.equal(anchor().userEdits.length, 0)
49
-
50
- // 阶段 4:真实时钟推进 12 分钟无更新 → staleMin=12 且 kickoff 提醒复现
51
- const clock = withClock()
52
- try {
53
- clock.advance(12 * 60 * 1000)
54
- const later = harness.provided.get('progressAnchor').get(SESSION)
55
- assert.equal(later.time.staleMin, 12)
56
- assert.match(harness.contexts.get('progress-union-kickoff')(), /12 分钟没有更新/)
57
- } finally {
58
- clock.restore()
59
- }
60
-
61
- // 阶段 5:完成推进 → percent 迁移
62
- await execUpdate(harness, { changes: [{ id: added.changed[0].id, status: 'done' }] })
63
- const afterDone = anchor()
64
- assert.equal(afterDone.board.summary.done, 1)
65
- assert.equal(afterDone.board.summary.percent, 33) // (100+0+0)/3
66
- })
67
-
68
- test('KV 缺席:内存态可用,锚点照出且 persistent=false', async () => {
69
- const harness = createHarness({ storageless: true })
70
- await harness.run()
71
- await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
72
- await execUpdate(harness, { changes: [{ title: '仅内存', status: 'doing' }] })
73
- const anchor = harness.provided.get('progressAnchor').get(SESSION)
74
- assert.equal(anchor.persistent, false)
75
- assert.equal(anchor.board.summary.total, 1)
76
- assert.equal(anchor.board.inFlight[0].title, '仅内存')
77
- })
1
+ /**
2
+ * Phase 5 端到端冒烟(task_22):建板 → 用户改 → 不 ack 读锚点 → ack 后再读 →
3
+ * 时钟推进 12 分钟读 staleMin;另验 KV 缺席时锚点仍出且 persistent=false。
4
+ */
5
+ import assert from 'node:assert/strict'
6
+ import fs from 'node:fs'
7
+ import os from 'node:os'
8
+ import path from 'node:path'
9
+ import test from 'node:test'
10
+
11
+ process.env.DSH_HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'union-test-'))
12
+ const { SESSION, createHarness, jsonReq, callRoute, withClock } = await import('./harness.mjs')
13
+
14
+ function execUpdate(harness, args) {
15
+ return harness.toolMap.get('progress_update').definition.execute(args, { agent: harness.agent })
16
+ }
17
+
18
+ test('端到端:建板→用户改→ack→陈旧推进,锚点各阶段状态正确迁移', async () => {
19
+ const harness = createHarness()
20
+ await harness.run()
21
+ await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
22
+ const anchor = () => harness.provided.get('progressAnchor').get(SESSION)
23
+
24
+ // 阶段 0:启用但空板
25
+ assert.equal(anchor().board.summary.total, 0)
26
+ assert.match(harness.contexts.get('progress-union-kickoff')(), /第一个工具调用必须是 progress_update/)
27
+
28
+ // 阶段 1:建板(3 项,1 doing)
29
+ const added = await execUpdate(harness, {
30
+ changes: [{ title: '抓帧', status: 'doing', current: '渲染第 1/900 帧' }, { title: '合成' }, { title: '字幕' }],
31
+ })
32
+ assert.equal(added.summary.total, 3)
33
+ assert.equal(anchor().board.inFlight.length, 1)
34
+ assert.equal(anchor().board.inFlight[0].title, '抓帧')
35
+
36
+ // 阶段 2:用户面板改"合成"备注 → pending=1,userEdits 出现,时间戳推进
37
+ const itemId = (await callRoute(harness, '/progress-union/snapshot', { method: 'GET', url: `/progress-union/snapshot?sessionId=${SESSION}`, headers: {} })).json().items[1].id
38
+ await callRoute(harness, '/progress-union/action', jsonReq('POST', '/progress-union/action', { sessionId: SESSION, changes: [{ id: itemId, note: '用 4K' }] }))
39
+ const pending = anchor()
40
+ assert.equal(pending.board.pendingUserChanges, 1)
41
+ assert.equal(pending.userEdits[0].fields[0].field, 'note')
42
+ assert.ok(pending.revision > added.revision)
43
+
44
+ // 阶段 3:agent ack → pending=0,锚点 revision 不因确认增长
45
+ const acked = await execUpdate(harness, { changes: [], ackUserRevision: pending.revision })
46
+ assert.equal(acked.revision, pending.revision)
47
+ assert.equal(anchor().board.pendingUserChanges, 0)
48
+ assert.equal(anchor().userEdits.length, 0)
49
+
50
+ // 阶段 4:真实时钟推进 12 分钟无更新 → staleMin=12 且 kickoff 提醒复现
51
+ const clock = withClock()
52
+ try {
53
+ clock.advance(12 * 60 * 1000)
54
+ const later = harness.provided.get('progressAnchor').get(SESSION)
55
+ assert.equal(later.time.staleMin, 12)
56
+ assert.match(harness.contexts.get('progress-union-kickoff')(), /12 分钟没有更新/)
57
+ } finally {
58
+ clock.restore()
59
+ }
60
+
61
+ // 阶段 5:完成推进 → percent 迁移
62
+ await execUpdate(harness, { changes: [{ id: added.changed[0].id, status: 'done' }] })
63
+ const afterDone = anchor()
64
+ assert.equal(afterDone.board.summary.done, 1)
65
+ assert.equal(afterDone.board.summary.percent, 33) // (100+0+0)/3
66
+ })
67
+
68
+ test('KV 缺席:内存态可用,锚点照出且 persistent=false', async () => {
69
+ const harness = createHarness({ storageless: true })
70
+ await harness.run()
71
+ await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
72
+ await execUpdate(harness, { changes: [{ title: '仅内存', status: 'doing' }] })
73
+ const anchor = harness.provided.get('progressAnchor').get(SESSION)
74
+ assert.equal(anchor.persistent, false)
75
+ assert.equal(anchor.board.summary.total, 1)
76
+ assert.equal(anchor.board.inFlight[0].title, '仅内存')
77
+ })
@@ -0,0 +1,127 @@
1
+ /**
2
+ * /progress-goal 自动续轮驱动器验收(回归 session-3353b1ac 的「设了目标却不续轮」):
3
+ * - agent/status idle → 必须经 agent.followup 派发一轮带自有 source 的续轮消息;
4
+ * - driveGoal 必须拿到 agent(driveStateFor 的 live state 带 agent),否则 TypeError 被静默吞掉;
5
+ * - pre-step 身份栅栏:自己派发的轮次放行,伪造/过期轮次 reject;
6
+ * - 预算到顶转 blocked;官方目标 active 时让路不排队。
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-goal-drive-'))
15
+ const { SESSION, createHarness, jsonReq, callRoute, flush } = await import('./harness.mjs')
16
+
17
+ const GOAL_SOURCE_KIND = 'plugin:progress-goal-goal'
18
+
19
+ async function booted(config = {}) {
20
+ const harness = createHarness({ config })
21
+ await harness.run()
22
+ await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
23
+ return harness
24
+ }
25
+
26
+ function cmd(harness, input) {
27
+ return harness.commandMap.get('progress-goal').handler({ agent: harness.agent, rawInput: input })
28
+ }
29
+
30
+ function update(harness, changes) {
31
+ return harness.toolMap.get('progress_update').definition.execute({ changes }, { agent: harness.agent })
32
+ }
33
+
34
+ /** 宿主在回合收尾时把 agent 状态翻到 idle;插件在该边沿派发下一轮。 */
35
+ async function idle(harness) {
36
+ for (const listener of harness.listeners.filter((l) => l.type === 'agent/status')) {
37
+ listener.handler({ agent: harness.agent, status: 'idle' })
38
+ }
39
+ await flush()
40
+ await new Promise((resolve) => setTimeout(resolve, 10))
41
+ }
42
+
43
+ async function snapshotOf(harness) {
44
+ const res = await callRoute(harness, '/progress-union/snapshot', { method: 'GET', url: `/progress-union/snapshot?sessionId=${SESSION}`, headers: {} })
45
+ return res.json()
46
+ }
47
+
48
+ async function preStep(harness, message) {
49
+ const hook = harness.agentHooks.find((h) => h.type === 'agent/pre-step')
50
+ assert.ok(hook, 'pre-step hook 必须已安装')
51
+ return hook.handler(
52
+ { turn: 1, step: 1, messages: [message] },
53
+ async () => ({ kind: 'enter', messages: [message] }),
54
+ )
55
+ }
56
+
57
+ test('idle 派发一轮续轮消息(回归:driveGoal 拿不到 agent)', async () => {
58
+ const h = await booted()
59
+ await update(h, [{ title: 't1', status: 'doing', percent: 40 }])
60
+ await cmd(h, '把活干完')
61
+ assert.equal(h.agent.followups.length, 0)
62
+
63
+ await idle(h)
64
+
65
+ assert.equal(h.agent.followups.length, 1, 'idle 必须派发一轮')
66
+ const message = h.agent.followups[0]
67
+ assert.equal(message.role, 'user')
68
+ assert.equal(message.source.kind, GOAL_SOURCE_KIND)
69
+ assert.equal(message.source.round, 1)
70
+ assert.equal(typeof message.source.goalId, 'string')
71
+ assert.match(message.content[0].text, /<progress_goal_round>/)
72
+ assert.match(message.content[0].text, /Objective: "把活干完"/)
73
+ assert.match(message.content[0].text, /Round: 1\/256/)
74
+ assert.match(message.content[0].text, /progress_goal_report/, '续轮消息必须交代收尾/受阻的上报口径')
75
+
76
+ const snap = await snapshotOf(h)
77
+ assert.equal(snap.goal.roundsStarted, 1, '轮次计数必须落盘,避免重复派发')
78
+ assert.equal(snap.goal.phase, 'active')
79
+ })
80
+
81
+ test('没有目标时 idle 不派发、不抛错', async () => {
82
+ const h = await booted()
83
+ await idle(h)
84
+ assert.equal(h.agent.followups.length, 0)
85
+ })
86
+
87
+ test('pre-step 身份栅栏:自己的轮次放行,伪造/过期轮次 reject', async () => {
88
+ const h = await booted()
89
+ await update(h, [{ title: 't1', status: 'doing' }])
90
+ await cmd(h, '目标X')
91
+ await idle(h)
92
+ const message = h.agent.followups[0]
93
+
94
+ const decision = await preStep(h, message)
95
+ assert.equal(decision.kind, 'enter', '插件自己派发的轮次必须放行')
96
+
97
+ // 伪造:round 对不上当前 roundsStarted
98
+ const forged = { ...message, id: 'forged', source: { ...message.source, round: 99 } }
99
+ const rejected = await preStep(h, forged)
100
+ assert.equal(rejected.kind, 'reject')
101
+ })
102
+
103
+ test('预算到顶转 blocked,不再派发', async () => {
104
+ const h = await booted()
105
+ await update(h, [{ title: 't1', status: 'doing' }])
106
+ await cmd(h, '目标Y')
107
+ await cmd(h, 'budget 1')
108
+
109
+ await idle(h)
110
+ assert.equal(h.agent.followups.length, 1)
111
+
112
+ await idle(h)
113
+ assert.equal(h.agent.followups.length, 1, '预算用尽后不得再派发')
114
+ const snap = await snapshotOf(h)
115
+ assert.equal(snap.goal.phase, 'blocked')
116
+ assert.match(snap.goal.blockedReason, /预算上限 1 轮/)
117
+ })
118
+
119
+ test('官方目标 active 时让路:不排队', async () => {
120
+ const h = await booted()
121
+ await update(h, [{ title: 't1', status: 'doing' }])
122
+ await cmd(h, '目标Z')
123
+ const original = h.ctx.get.bind(h.ctx)
124
+ h.ctx.get = (name) => (name === 'goals' ? { get: () => ({ phase: 'active', activation: 'armed' }) } : original(name))
125
+ await idle(h)
126
+ assert.equal(h.agent.followups.length, 0, '官方目标在跑时不得抢轮')
127
+ })