dsh-progress-union 0.5.2 → 0.5.4

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,147 @@
1
+ /**
2
+ * 浮窗暂停/继续(目标控制面)验收:
3
+ * - POST /progress-union/goal {action:'pause'} → 落 paused + 撤回排队中的续轮 + 中断运行中的回合;
4
+ * - {action:'resume'} → 重新武装 + 立刻点火,下一轮真的被派发(不是只改状态);
5
+ * - {action:'status'} → 只读回声;
6
+ * - 非法 action / 缺 sessionId → 400;
7
+ * - 暂停后不会偷偷再跑一轮(idle 边沿不会派发);继续后能接上前进。
8
+ */
9
+ import assert from 'node:assert/strict'
10
+ import fs from 'node:fs'
11
+ import os from 'node:os'
12
+ import path from 'node:path'
13
+ import test from 'node:test'
14
+
15
+ process.env.DSH_HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'union-goal-control-'))
16
+ const { SESSION, createHarness, jsonReq, callRoute, flush } = await import('./harness.mjs')
17
+
18
+ async function booted() {
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
+ return harness
23
+ }
24
+
25
+ function cmd(harness, input) {
26
+ return harness.commandMap.get('progress-goal').handler({ agent: harness.agent, rawInput: input })
27
+ }
28
+
29
+ async function goalCmd(harness, input) {
30
+ const result = await cmd(harness, input)
31
+ await flush()
32
+ await flush()
33
+ return result
34
+ }
35
+
36
+ async function update(harness, changes) {
37
+ const r = await harness.toolMap.get('progress_update').definition.execute({ changes }, { agent: harness.agent })
38
+ return r
39
+ }
40
+
41
+ async function goalControl(harness, action, sessionId = SESSION) {
42
+ const res = await callRoute(harness, '/progress-union/goal', jsonReq('POST', '/progress-union/goal', { sessionId, action }))
43
+ return { status: res.status, body: res.json() }
44
+ }
45
+
46
+ async function idle(harness) {
47
+ for (const listener of harness.listeners.filter((l) => l.type === 'agent/status')) {
48
+ listener.handler({ agent: harness.agent, status: 'idle' })
49
+ }
50
+ await flush()
51
+ await new Promise((resolve) => setTimeout(resolve, 10))
52
+ }
53
+
54
+ test('/progress-union/goal pause:落 paused 并撤掉排队中的续轮', async () => {
55
+ const h = await booted()
56
+ await update(h, [{ title: 't1', status: 'doing' }])
57
+ await goalCmd(h, '目标P')
58
+ assert.equal(h.agent.followups.length, 1)
59
+ assert.equal(h.inbox.nextTurn.length, 1, '第 1 轮应先排进 inbox')
60
+
61
+ const { status, body } = await goalControl(h, 'pause')
62
+ assert.equal(status, 200)
63
+ assert.equal(body.ok, true)
64
+ assert.equal(body.goal.phase, 'paused')
65
+ assert.equal(body.goal.activation, 'disarmed')
66
+ assert.equal(h.inbox.nextTurn.length, 0, '暂停必须撤回已排队的续轮,不能暂停后偷偷再跑一轮')
67
+
68
+ await idle(h)
69
+ assert.equal(h.agent.followups.length, 1, '暂停后 idle 边沿不得再派发')
70
+ })
71
+
72
+ test('/progress-union/goal resume:重新武装并立刻派发下一轮(主动继续)', async () => {
73
+ const h = await booted()
74
+ await update(h, [{ title: 't1', status: 'doing' }])
75
+ await goalCmd(h, '目标Q')
76
+ h.claim(h.agent.followups[0]) // 第 1 轮跑掉
77
+ await goalControl(h, 'pause')
78
+
79
+ const before = h.agent.followups.length
80
+ const { status, body } = await goalControl(h, 'resume')
81
+ await flush()
82
+ await flush()
83
+ assert.equal(status, 200)
84
+ assert.equal(body.ok, true)
85
+ assert.equal(body.goal.phase, 'active')
86
+ assert.equal(body.goal.activation, 'armed')
87
+ assert.equal(h.agent.followups.length, before + 1, 'resume 必须立刻点火,而不是等用户再发一条消息')
88
+ const round = h.agent.followups[h.agent.followups.length - 1]
89
+ assert.equal(round.source.round, 2)
90
+ assert.equal(h.inbox.nextTurn.length, 1)
91
+ })
92
+
93
+ test('resume 后回合收尾能继续推进(恢复的是循环,不是一次性)', async () => {
94
+ const h = await booted()
95
+ await update(h, [{ title: 't1', status: 'doing' }])
96
+ await goalCmd(h, '目标R')
97
+ h.claim(h.agent.followups[0])
98
+ await goalControl(h, 'pause')
99
+ await goalControl(h, 'resume')
100
+ const queued = h.claim(h.inbox.nextTurn[0])
101
+ assert.ok(queued)
102
+
103
+ const hook = h.agentHooks.find((x) => x.type === 'agent/pre-step')
104
+ const decision = await hook.handler(
105
+ { turn: 1, step: 1, messages: [queued] },
106
+ async () => ({ kind: 'enter', messages: [queued] }),
107
+ )
108
+ assert.equal(decision.kind, 'enter', '恢复后的轮次必须被身份栅栏放行')
109
+ await idle(h)
110
+ assert.equal(h.agent.followups.length, 3, '回合收尾后应继续派发下一轮')
111
+ })
112
+
113
+ test('/progress-union/goal status:只读回声,不改状态', async () => {
114
+ const h = await booted()
115
+ await update(h, [{ title: 't1', status: 'doing' }])
116
+ await goalCmd(h, '目标S')
117
+ const { status, body } = await goalControl(h, 'status')
118
+ assert.equal(status, 200)
119
+ assert.equal(body.goal.phase, 'active')
120
+ assert.equal(body.goal.objective, '目标S')
121
+ assert.equal(h.inbox.nextTurn.length, 1, 'status 不得动 inbox')
122
+ })
123
+
124
+ test('非法 action / 缺 sessionId → 400', async () => {
125
+ const h = await booted()
126
+ await goalCmd(h, '目标T')
127
+ let r = await goalControl(h, 'explode')
128
+ assert.equal(r.status, 400)
129
+ assert.match(String(r.body.error), /pause\/resume\/status/)
130
+ r = await goalControl(h, 'pause', '')
131
+ assert.equal(r.status, 400)
132
+ assert.match(String(r.body.error), /sessionId/)
133
+ })
134
+
135
+ test('暂停时回合正在跑:中断该回合', async () => {
136
+ const h = await booted()
137
+ await update(h, [{ title: 't1', status: 'doing' }])
138
+ await goalCmd(h, '目标U')
139
+ h.agent.status = 'running'
140
+ let cancelled = null
141
+ h.agent.cancel = (cause, options) => { cancelled = { cause, options } }
142
+ const { body } = await goalControl(h, 'pause')
143
+ assert.equal(body.goal.phase, 'paused')
144
+ assert.ok(cancelled, '暂停必须中断运行中的回合')
145
+ assert.equal(cancelled.options.keepInbox, true, '中断保留用户排队消息,别把用户输入一起丢了')
146
+ h.agent.status = 'idle'
147
+ })
@@ -1,9 +1,11 @@
1
1
  /**
2
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 时让路不排队。
3
+ * - 建目标即点火,经 agent.followup 派发一轮带自有 source 的续轮消息;
4
+ * - 回合收尾(agent/status idle)→ 派发下一轮;同轮未消费时不得连排两轮;
5
+ * - pre-step 身份栅栏:自己派发/重启遗留的轮次放行,伪造轮次丢弃;
6
+ * - 重启自愈:进程重启后内存 attempt 丢失,排队中的一轮仍能被收养(不静默丢);
7
+ * - 重启自愈:armed 目标在会话重新挂载时补点火,不依赖用户再发一条消息;
8
+ * - 预算到顶转 blocked;官方目标 active 时让路。
7
9
  */
8
10
  import assert from 'node:assert/strict'
9
11
  import fs from 'node:fs'
@@ -16,8 +18,8 @@ const { SESSION, createHarness, jsonReq, callRoute, flush } = await import('./ha
16
18
 
17
19
  const GOAL_SOURCE_KIND = 'plugin:progress-goal-goal'
18
20
 
19
- async function booted(config = {}) {
20
- const harness = createHarness({ config })
21
+ async function booted(config = {}, options = {}) {
22
+ const harness = createHarness({ config, ...options })
21
23
  await harness.run()
22
24
  await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
23
25
  return harness
@@ -27,6 +29,14 @@ function cmd(harness, input) {
27
29
  return harness.commandMap.get('progress-goal').handler({ agent: harness.agent, rawInput: input })
28
30
  }
29
31
 
32
+ /** 命令里的点火是 fire-and-forget 的异步派发:等它落完再断言。 */
33
+ async function goalCmd(harness, input) {
34
+ const result = await cmd(harness, input)
35
+ await flush()
36
+ await flush()
37
+ return result
38
+ }
39
+
30
40
  function update(harness, changes) {
31
41
  return harness.toolMap.get('progress_update').definition.execute({ changes }, { agent: harness.agent })
32
42
  }
@@ -54,15 +64,22 @@ async function preStep(harness, message) {
54
64
  )
55
65
  }
56
66
 
57
- test('idle 派发一轮续轮消息(回归:driveGoal 拿不到 agent)', async () => {
67
+ /** 模拟一次完整回合:take 走排队消息 → pre-step 认领 → 回合收尾 → idle 边沿。 */
68
+ async function runTurn(harness) {
69
+ const message = harness.claim(harness.inbox.nextTurn[0])
70
+ assert.ok(message, '应当有一轮排队中的续轮消息')
71
+ const decision = await preStep(harness, message)
72
+ assert.equal(decision.kind, 'enter')
73
+ await idle(harness)
74
+ return message
75
+ }
76
+
77
+ test('建目标即点火:立刻派发第 1 轮并落盘 roundsStarted=1', async () => {
58
78
  const h = await booted()
59
79
  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)
80
+ await goalCmd(h, '把活干完')
64
81
 
65
- assert.equal(h.agent.followups.length, 1, 'idle 必须派发一轮')
82
+ assert.equal(h.agent.followups.length, 1, '建目标必须立刻点火,不能等用户再发一条消息')
66
83
  const message = h.agent.followups[0]
67
84
  assert.equal(message.role, 'user')
68
85
  assert.equal(message.source.kind, GOAL_SOURCE_KIND)
@@ -71,45 +88,134 @@ test('idle 派发一轮续轮消息(回归:driveGoal 拿不到 agent)', as
71
88
  assert.match(message.content[0].text, /<progress_goal_round>/)
72
89
  assert.match(message.content[0].text, /Objective: "把活干完"/)
73
90
  assert.match(message.content[0].text, /Round: 1\/256/)
74
- assert.match(message.content[0].text, /progress_goal_report/, '续轮消息必须交代收尾/受阻的上报口径')
91
+ assert.match(message.content[0].text, /progress_goal_report/)
75
92
 
76
93
  const snap = await snapshotOf(h)
77
94
  assert.equal(snap.goal.roundsStarted, 1, '轮次计数必须落盘,避免重复派发')
78
95
  assert.equal(snap.goal.phase, 'active')
79
96
  })
80
97
 
98
+ test('回合收尾派发下一轮;同一轮未消费时不得连排两轮', async () => {
99
+ const h = await booted()
100
+ await update(h, [{ title: 't1', status: 'doing' }])
101
+ await goalCmd(h, '目标A')
102
+
103
+ // 第 1 轮还排在 inbox 里(没被取走)→ idle 边沿不得再排一轮
104
+ await idle(h)
105
+ assert.equal(h.agent.followups.length, 1, '同轮未消费时连排两轮会白烧预算')
106
+
107
+ await runTurn(h)
108
+ assert.equal(h.agent.followups.length, 2, '回合收尾后应派发第 2 轮')
109
+ assert.equal(h.agent.followups[1].source.round, 2)
110
+ const snap = await snapshotOf(h)
111
+ assert.equal(snap.goal.roundsStarted, 2)
112
+ })
113
+
81
114
  test('没有目标时 idle 不派发、不抛错', async () => {
82
115
  const h = await booted()
83
116
  await idle(h)
84
117
  assert.equal(h.agent.followups.length, 0)
85
118
  })
86
119
 
87
- test('pre-step 身份栅栏:自己的轮次放行,伪造/过期轮次 reject', async () => {
120
+ test('pre-step 身份栅栏:自己的轮次放行,伪造轮次只丢这一条', async () => {
88
121
  const h = await booted()
89
122
  await update(h, [{ title: 't1', status: 'doing' }])
90
- await cmd(h, '目标X')
91
- await idle(h)
92
- const message = h.agent.followups[0]
123
+ await goalCmd(h, '目标X')
93
124
 
125
+ const message = h.claim(h.agent.followups[0])
94
126
  const decision = await preStep(h, message)
95
127
  assert.equal(decision.kind, 'enter', '插件自己派发的轮次必须放行')
96
128
 
97
- // 伪造:round 对不上当前 roundsStarted
129
+ // 伪造:轮次号对不上当前 roundsStarted
98
130
  const forged = { ...message, id: 'forged', source: { ...message.source, round: 99 } }
99
131
  const rejected = await preStep(h, forged)
100
132
  assert.equal(rejected.kind, 'reject')
101
133
  })
102
134
 
103
- test('预算到顶转 blocked,不再派发', async () => {
135
+ test('孤儿轮次只丢自己:同一步里的用户消息不能被连坐 reject', async () => {
104
136
  const h = await booted()
105
137
  await update(h, [{ title: 't1', status: 'doing' }])
106
- await cmd(h, '目标Y')
107
- await cmd(h, 'budget 1')
138
+ await goalCmd(h, '目标O')
139
+ const stale = h.claim(h.agent.followups[0])
140
+ const forged = { ...stale, id: 'stale-round', source: { ...stale.source, round: 42 } }
141
+ const user = { id: 'u-1', role: 'user', content: [{ type: 'text', text: '我自己的话' }], source: { kind: 'user' } }
142
+
143
+ const hook = h.agentHooks.find((x) => x.type === 'agent/pre-step')
144
+ const decision = await hook.handler(
145
+ { turn: 1, step: 1, messages: [user, forged] },
146
+ async () => ({ kind: 'enter', messages: [user, forged] }),
147
+ )
148
+ assert.equal(decision.kind, 'enter')
149
+ assert.deepEqual(decision.messages.map((m) => m.id), ['u-1'], '用户消息必须留下,孤儿轮次被摘掉')
150
+ })
108
151
 
109
- await idle(h)
110
- assert.equal(h.agent.followups.length, 1)
152
+ test('重启自愈:内存 attempt 丢失后,排队中的一轮被收养而不是静默丢弃', async () => {
153
+ const h = await booted()
154
+ await update(h, [{ title: 't1', status: 'doing' }])
155
+ await goalCmd(h, '目标R')
156
+ const message = h.agent.followups[0]
157
+
158
+ // 用同一份持久化记录重开宿主 = 重启;内存 attempt 必然为空。
159
+ const restarted = await booted({}, { seed: h.records })
160
+ restarted.agent.inbox.nextTurn.push(message)
161
+ const decision = await preStep(restarted, message)
162
+ assert.equal(decision.kind, 'enter', '重启后排队中的一轮必须被收养,否则白烧一轮预算')
163
+ })
164
+
165
+ test('重启自愈:armed 目标在会话重新挂载时自动补点火(无需用户先发消息)', async () => {
166
+ const h = await booted()
167
+ await update(h, [{ title: 't1', status: 'doing' }])
168
+ await goalCmd(h, '目标K')
169
+ h.claim(h.agent.followups[0]) // 上一轮已被消费掉,inbox 空;模拟崩在回合收尾前
170
+
171
+ const restarted = await booted({}, { seed: h.records })
172
+ assert.equal(restarted.agent.followups.length, 0)
173
+ // 重挂载的点火有 2.5s 抖动窗口(等宿主把回合基础设施铺好)
174
+ await new Promise((resolve) => setTimeout(resolve, 2800))
175
+ assert.equal(restarted.agent.followups.length, 1, '重启后必须自己续上,不能等用户说话')
176
+ assert.equal(restarted.agent.followups[0].source.round, 2)
177
+ })
178
+
179
+ test('重启自愈:上一次进程遗留的排队轮次被清掉并重排(否则它既不跑又挡新轮)', async () => {
180
+ const h = await booted()
181
+ await update(h, [{ title: 't1', status: 'doing' }])
182
+ await goalCmd(h, '目标S')
183
+ const stranded = h.claim(h.agent.followups[0]) // 崩在「已排队、未执行」之间
184
+
185
+ // 用同一份持久化记录重开宿主:attempt 空,但 inbox 里还留着上一轮
186
+ const restarted = await booted({}, { seed: h.records })
187
+ restarted.agent.inbox.nextTurn.push(stranded)
188
+ await new Promise((resolve) => setTimeout(resolve, 2800))
189
+
190
+ assert.equal(restarted.agent.followups.length, 1, '重启后必须重排一轮')
191
+ assert.equal(restarted.agent.inbox.nextTurn.includes(stranded), false, '孤儿轮次必须被撤掉')
192
+ assert.equal(restarted.agent.inbox.nextTurn.length, 1)
193
+ assert.notEqual(restarted.agent.inbox.nextTurn[0].id, stranded.id)
194
+ })
111
195
 
196
+ test('resume 接受 active:重新武装 + 点火', async () => {
197
+ const h = await booted()
198
+ await update(h, [{ title: 't1', status: 'doing' }])
199
+ await goalCmd(h, '目标M')
200
+ h.claim(h.agent.followups[0])
112
201
  await idle(h)
202
+
203
+ const before = h.agent.followups.length
204
+ const text = (await goalCmd(h, 'resume')).text
205
+ assert.match(text, /Status: active/)
206
+ assert.match(text, /Activation: armed/)
207
+ assert.ok(h.agent.followups.length >= before, 'resume 必须至少不倒退')
208
+ const snap = await snapshotOf(h)
209
+ assert.equal(snap.goal.activation, 'armed')
210
+ })
211
+
212
+ test('预算到顶转 blocked,不再派发', async () => {
213
+ const h = await booted()
214
+ await update(h, [{ title: 't1', status: 'doing' }])
215
+ await goalCmd(h, '目标Y')
216
+ await goalCmd(h, 'budget 1')
217
+
218
+ await runTurn(h) // round 1 用完,回合收尾 → 预算已到顶
113
219
  assert.equal(h.agent.followups.length, 1, '预算用尽后不得再派发')
114
220
  const snap = await snapshotOf(h)
115
221
  assert.equal(snap.goal.phase, 'blocked')
@@ -119,9 +225,8 @@ test('预算到顶转 blocked,不再派发', async () => {
119
225
  test('官方目标 active 时让路:不排队', async () => {
120
226
  const h = await booted()
121
227
  await update(h, [{ title: 't1', status: 'doing' }])
122
- await cmd(h, '目标Z')
123
228
  const original = h.ctx.get.bind(h.ctx)
124
229
  h.ctx.get = (name) => (name === 'goals' ? { get: () => ({ phase: 'active', activation: 'armed' }) } : original(name))
125
- await idle(h)
230
+ await goalCmd(h, '目标Z')
126
231
  assert.equal(h.agent.followups.length, 0, '官方目标在跑时不得抢轮')
127
232
  })
@@ -38,10 +38,11 @@ test('create/status/budget/pause/resume/clear 全命令链', async () => {
38
38
  let r = await cmd(h, '把插件改完并测试')
39
39
  assert.equal(r.kind, 'success')
40
40
  assert.match(r.text, /Status: active/)
41
- assert.match(r.text, /Rounds: 0\/256/)
41
+ // v0.5.3:建目标即点火派发第 1 轮,轮次计数立刻为 1(旧版要等下一次回合收尾才动)。
42
+ assert.match(r.text, /Rounds: 1\/256/)
42
43
 
43
44
  r = await cmd(h, 'budget 3')
44
- assert.match(r.text, /Rounds: 0\/3/)
45
+ assert.match(r.text, /Rounds: 1\/3/)
45
46
 
46
47
  r = await cmd(h, 'status')
47
48
  assert.match(r.text, /Objective: "把插件改完并测试"/)