dsh-progress-union 0.8.4 → 0.9.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.
@@ -1,171 +1,171 @@
1
- /**
2
- * Phase 7 验收:观察者(辅助观察最近输出/清单;板滞后及时补与催办)。
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, flush } = 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
- return harness.toolMap.get('progress_update').definition.execute(args, { agent: harness.agent })
22
- }
23
-
24
- function observerText(harness) {
25
- const fn = harness.contexts.get('progress-union-observer')
26
- assert.ok(fn, 'progress-union-observer 上下文必须注册')
27
- return fn()
28
- }
29
-
30
- test('观察块:清单标记 + 最近输出 + 最近动作 + 官方 todo 行', async () => {
31
- const harness = await booted()
32
- await execUpdate(harness, {
33
- changes: [
34
- { title: '接口改造', status: 'done' },
35
- { title: '补集成测试', status: 'doing', current: '写 observer 用例', next: '跑全量' },
36
- { title: '发版', status: 'todo' },
37
- ],
38
- })
39
- harness.emit('assistant/message', { message: { content: [{ type: 'text', text: '接口改造已完成,接下来补测试。' }] } })
40
- harness.emit('tool/call', { name: 'Bash', arguments: JSON.stringify({ command: 'npm test' }) })
41
- harness.emit('todo/write', { todos: [
42
- { content: '接口改造', status: 'completed' },
43
- { content: '补集成测试', status: 'in_progress' },
44
- { content: '发版', status: 'pending' },
45
- ] })
46
-
47
- const text = observerText(harness)
48
- assert.match(text, /<progress-observer revision="1"/)
49
- assert.match(text, /\[x\] 接口改造/)
50
- assert.match(text, /\[~\] 补集成测试(0%) · 当前:写 observer 用例 · 下一步:跑全量/)
51
- assert.match(text, /\[ \] 发版/)
52
- assert.match(text, /最近输出:「接口改造已完成,接下来补测试。/)
53
- assert.match(text, /最近动作:Bash/)
54
- assert.match(text, /\[官方todo\] 完成 1\/3,未完:补集成测试、发版/)
55
- assert.doesNotMatch(text, /滞后/, '未超阈值不得催办')
56
- })
57
-
58
- test('滞后刚需:超 N 步未写板 → 催办出现;模型写板后消失', async () => {
59
- const harness = await booted()
60
- await execUpdate(harness, { changes: [{ title: '唯一任务', status: 'doing' }] })
61
- for (let i = 0; i < 6; i++) harness.emit('step/start', {})
62
- const text = observerText(harness)
63
- assert.match(text, /⚠ 进度已滞后(6 步前)/)
64
- assert.match(text, /先调用 progress_update 同步再继续/)
65
-
66
- // 模型自己同步(无变化确认也算)→ 催办消失
67
- await execUpdate(harness, { changes: [] })
68
- assert.doesNotMatch(observerText(harness), /滞后/)
69
- })
70
-
71
- test('观察者自动补 current:事实性最近动作写入 doing 项(updatedBy=observer),不抵消催办', async () => {
72
- const harness = await booted()
73
- const added = await execUpdate(harness, { changes: [{ title: '造帧', status: 'doing', current: '热身' }] })
74
- const itemId = added.changed[0].id
75
- harness.emit('tool/call', { name: 'Bash', arguments: JSON.stringify({ command: 'render --frames 900' }) })
76
- for (let i = 0; i < 6; i++) harness.emit('step/start', {})
77
- await flush()
78
-
79
- const snap = (await callRoute(harness, '/progress-union/snapshot', { method: 'GET', url: `/progress-union/snapshot?sessionId=${SESSION}`, headers: {} })).json()
80
- const item = snap.items.find((x) => x.id === itemId)
81
- assert.match(item.current, /^自动同步:Bash · command=render --frames 900/)
82
- assert.equal(item.updatedBy, 'observer')
83
- assert.equal(item.status, 'doing', '观察者不得改状态')
84
- assert.ok(snap.revision >= 2)
85
-
86
- // 自动补不重置\"模型同步\"计数:催办仍在(状态/百分比仍需模型确认)
87
- assert.match(observerText(harness), /⚠ 进度已滞后/)
88
- })
89
-
90
- test('progress_update 拉通道:返回 inFlight 明细 + time 字段', async () => {
91
- const harness = await booted()
92
- const r = await execUpdate(harness, {
93
- changes: [
94
- { title: '甲', status: 'done' },
95
- { title: '乙', status: 'doing', current: '做乙', next: '做丙' },
96
- { title: '丙', status: 'blocked', note: '等确认' },
97
- ],
98
- })
99
- assert.equal(r.inFlight.length, 2)
100
- assert.deepEqual(r.inFlight.map((x) => x.status), ['doing', 'blocked'])
101
- assert.equal(typeof r.time.etaBasis, 'string')
102
- assert.equal(typeof r.time.steps, 'number')
103
- assert.equal(typeof r.time.staleMin, 'number')
104
- })
105
-
106
- test('最近改动:带路径的工具调用入块,同路径去重、只留最近 4 个', async () => {
107
- const harness = await booted()
108
- await execUpdate(harness, { changes: [{ title: '改代码', status: 'doing' }] })
109
- const call = (file, tool = 'Edit') => harness.emit('tool/call', { name: tool, arguments: JSON.stringify({ file_path: file }) })
110
- call('E:\\proj\\src\\a.ts')
111
- call('E:\\proj\\src\\b.ts', 'Write')
112
- call('E:\\proj\\src\\a.ts') // 同路径去重,留最新
113
- call('E:\\proj\\src\\c.ts')
114
- call('E:\\proj\\src\\d.ts')
115
- call('E:\\proj\\src\\e.ts')
116
-
117
- const text = observerText(harness)
118
- const line = text.split('\n').find((l) => l.startsWith('最近改动:'))
119
- assert.ok(line, '最近改动行必须存在')
120
- assert.equal((line.match(/(/g) || []).length, 4, '最多 4 条')
121
- assert.doesNotMatch(line, /b\.ts/, '被后 4 条挤出(上限保留最近)')
122
- assert.match(line, /a\.ts(Edit)/, '同路径重写后保留最新一条')
123
- assert.equal((line.match(/a\.ts/g) || []).length, 1, '同路径只出现一次(去重)')
124
- })
125
-
126
- test('清单对比:todo 与板完成数漂移才出对比行(算术,非语义)', async () => {
127
- const harness = await booted()
128
- await execUpdate(harness, {
129
- changes: [{ title: '甲', status: 'done' }, { title: '乙', status: 'doing' }, { title: '丙', status: 'todo' }],
130
- })
131
- harness.emit('todo/write', { todos: [
132
- { content: '甲', status: 'completed' },
133
- { content: '乙', status: 'completed' },
134
- { content: '丙', status: 'pending' },
135
- ] })
136
- const drifted = observerText(harness)
137
- assert.match(drifted, /对比:官方 todo 完成 2\/3 · 板完成 1\/3(进度不一致/)
138
-
139
- // 对齐后不再出对比行
140
- await execUpdate(harness, { changes: [{ title: '乙', status: 'done' }] })
141
- assert.doesNotMatch(observerText(harness), /对比:/)
142
- })
143
-
144
- test('观察者自补的 current 在块里标注待确认', async () => {
145
- const harness = await booted()
146
- await execUpdate(harness, { changes: [{ title: '造帧', status: 'doing', current: '热身' }] })
147
- harness.emit('tool/call', { name: 'Bash', arguments: JSON.stringify({ command: 'render' }) })
148
- for (let i = 0; i < 6; i++) harness.emit('step/start', {})
149
- await flush()
150
- assert.match(observerText(harness), /当前:自动同步:Bash · command=render(观察者补,待确认)/)
151
- })
152
-
153
- test('门控:关板/全完成/空板时观察块为空(0 token)', async () => {
154
- const harness = createHarness()
155
- await harness.run()
156
- assert.equal(observerText(harness), '', '未启用不得注入')
157
-
158
- await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
159
- assert.equal(observerText(harness), '', '空板不注入(kickoff 负责)')
160
-
161
- await execUpdate(harness, { changes: [{ title: '唯一任务', status: 'done' }] })
162
- assert.equal(observerText(harness), '', '全完成不打扰')
163
- })
164
-
165
- test('可关:observer.enabled=false 时零注入', async () => {
166
- const harness = createHarness({ config: { observer: { enabled: false } } })
167
- await harness.run()
168
- await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
169
- await execUpdate(harness, { changes: [{ title: '任务', status: 'doing' }] })
170
- assert.equal(observerText(harness), '')
1
+ /**
2
+ * Phase 7 验收:观察者(辅助观察最近输出/清单;板滞后及时补与催办)。
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, flush } = 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
+ return harness.toolMap.get('progress_update').definition.execute(args, { agent: harness.agent })
22
+ }
23
+
24
+ function observerText(harness) {
25
+ const fn = harness.contexts.get('progress-union-observer')
26
+ assert.ok(fn, 'progress-union-observer 上下文必须注册')
27
+ return fn()
28
+ }
29
+
30
+ test('观察块:清单标记 + 最近输出 + 最近动作 + 官方 todo 行', async () => {
31
+ const harness = await booted()
32
+ await execUpdate(harness, {
33
+ changes: [
34
+ { title: '接口改造', status: 'done' },
35
+ { title: '补集成测试', status: 'doing', current: '写 observer 用例', next: '跑全量' },
36
+ { title: '发版', status: 'todo' },
37
+ ],
38
+ })
39
+ harness.emit('assistant/message', { message: { content: [{ type: 'text', text: '接口改造已完成,接下来补测试。' }] } })
40
+ harness.emit('tool/call', { name: 'Bash', arguments: JSON.stringify({ command: 'npm test' }) })
41
+ harness.emit('todo/write', { todos: [
42
+ { content: '接口改造', status: 'completed' },
43
+ { content: '补集成测试', status: 'in_progress' },
44
+ { content: '发版', status: 'pending' },
45
+ ] })
46
+
47
+ const text = observerText(harness)
48
+ assert.match(text, /<progress-observer revision="1"/)
49
+ assert.match(text, /\[x\] 接口改造/)
50
+ assert.match(text, /\[~\] 补集成测试(0%) · 当前:写 observer 用例 · 下一步:跑全量/)
51
+ assert.match(text, /\[ \] 发版/)
52
+ assert.match(text, /最近输出:「接口改造已完成,接下来补测试。/)
53
+ assert.match(text, /最近动作:Bash/)
54
+ assert.match(text, /\[官方todo\] 完成 1\/3,未完:补集成测试、发版/)
55
+ assert.doesNotMatch(text, /滞后/, '未超阈值不得催办')
56
+ })
57
+
58
+ test('滞后刚需:超 N 步未写板 → 催办出现;模型写板后消失', async () => {
59
+ const harness = await booted()
60
+ await execUpdate(harness, { changes: [{ title: '唯一任务', status: 'doing' }] })
61
+ for (let i = 0; i < 6; i++) harness.emit('step/start', {})
62
+ const text = observerText(harness)
63
+ assert.match(text, /⚠ 进度已滞后(6 步前)/)
64
+ assert.match(text, /先调用 progress_update 同步再继续/)
65
+
66
+ // 模型自己同步(无变化确认也算)→ 催办消失
67
+ await execUpdate(harness, { changes: [] })
68
+ assert.doesNotMatch(observerText(harness), /滞后/)
69
+ })
70
+
71
+ test('观察者自动补 current:事实性最近动作写入 doing 项(updatedBy=observer),不抵消催办', async () => {
72
+ const harness = await booted()
73
+ const added = await execUpdate(harness, { changes: [{ title: '造帧', status: 'doing', current: '热身' }] })
74
+ const itemId = added.changed[0].id
75
+ harness.emit('tool/call', { name: 'Bash', arguments: JSON.stringify({ command: 'render --frames 900' }) })
76
+ for (let i = 0; i < 6; i++) harness.emit('step/start', {})
77
+ await flush()
78
+
79
+ const snap = (await callRoute(harness, '/progress-union/snapshot', { method: 'GET', url: `/progress-union/snapshot?sessionId=${SESSION}`, headers: {} })).json()
80
+ const item = snap.items.find((x) => x.id === itemId)
81
+ assert.match(item.current, /^自动同步:Bash · command=render --frames 900/)
82
+ assert.equal(item.updatedBy, 'observer')
83
+ assert.equal(item.status, 'doing', '观察者不得改状态')
84
+ assert.ok(snap.revision >= 2)
85
+
86
+ // 自动补不重置\"模型同步\"计数:催办仍在(状态/百分比仍需模型确认)
87
+ assert.match(observerText(harness), /⚠ 进度已滞后/)
88
+ })
89
+
90
+ test('progress_update 拉通道:返回 inFlight 明细 + time 字段', async () => {
91
+ const harness = await booted()
92
+ const r = await execUpdate(harness, {
93
+ changes: [
94
+ { title: '甲', status: 'done' },
95
+ { title: '乙', status: 'doing', current: '做乙', next: '做丙' },
96
+ { title: '丙', status: 'blocked', note: '等确认' },
97
+ ],
98
+ })
99
+ assert.equal(r.inFlight.length, 2)
100
+ assert.deepEqual(r.inFlight.map((x) => x.status), ['doing', 'blocked'])
101
+ assert.equal(typeof r.time.etaBasis, 'string')
102
+ assert.equal(typeof r.time.steps, 'number')
103
+ assert.equal(typeof r.time.staleMin, 'number')
104
+ })
105
+
106
+ test('最近改动:带路径的工具调用入块,同路径去重、只留最近 4 个', async () => {
107
+ const harness = await booted()
108
+ await execUpdate(harness, { changes: [{ title: '改代码', status: 'doing' }] })
109
+ const call = (file, tool = 'Edit') => harness.emit('tool/call', { name: tool, arguments: JSON.stringify({ file_path: file }) })
110
+ call('E:\\proj\\src\\a.ts')
111
+ call('E:\\proj\\src\\b.ts', 'Write')
112
+ call('E:\\proj\\src\\a.ts') // 同路径去重,留最新
113
+ call('E:\\proj\\src\\c.ts')
114
+ call('E:\\proj\\src\\d.ts')
115
+ call('E:\\proj\\src\\e.ts')
116
+
117
+ const text = observerText(harness)
118
+ const line = text.split('\n').find((l) => l.startsWith('最近改动:'))
119
+ assert.ok(line, '最近改动行必须存在')
120
+ assert.equal((line.match(/(/g) || []).length, 4, '最多 4 条')
121
+ assert.doesNotMatch(line, /b\.ts/, '被后 4 条挤出(上限保留最近)')
122
+ assert.match(line, /a\.ts(Edit)/, '同路径重写后保留最新一条')
123
+ assert.equal((line.match(/a\.ts/g) || []).length, 1, '同路径只出现一次(去重)')
124
+ })
125
+
126
+ test('清单对比:todo 与板完成数漂移才出对比行(算术,非语义)', async () => {
127
+ const harness = await booted()
128
+ await execUpdate(harness, {
129
+ changes: [{ title: '甲', status: 'done' }, { title: '乙', status: 'doing' }, { title: '丙', status: 'todo' }],
130
+ })
131
+ harness.emit('todo/write', { todos: [
132
+ { content: '甲', status: 'completed' },
133
+ { content: '乙', status: 'completed' },
134
+ { content: '丙', status: 'pending' },
135
+ ] })
136
+ const drifted = observerText(harness)
137
+ assert.match(drifted, /对比:官方 todo 完成 2\/3 · 板完成 1\/3(进度不一致/)
138
+
139
+ // 对齐后不再出对比行
140
+ await execUpdate(harness, { changes: [{ title: '乙', status: 'done' }] })
141
+ assert.doesNotMatch(observerText(harness), /对比:/)
142
+ })
143
+
144
+ test('观察者自补的 current 在块里标注待确认', async () => {
145
+ const harness = await booted()
146
+ await execUpdate(harness, { changes: [{ title: '造帧', status: 'doing', current: '热身' }] })
147
+ harness.emit('tool/call', { name: 'Bash', arguments: JSON.stringify({ command: 'render' }) })
148
+ for (let i = 0; i < 6; i++) harness.emit('step/start', {})
149
+ await flush()
150
+ assert.match(observerText(harness), /当前:自动同步:Bash · command=render(观察者补,待确认)/)
151
+ })
152
+
153
+ test('门控:关板/全完成/空板时观察块为空(0 token)', async () => {
154
+ const harness = createHarness()
155
+ await harness.run()
156
+ assert.equal(observerText(harness), '', '未启用不得注入')
157
+
158
+ await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
159
+ assert.equal(observerText(harness), '', '空板不注入(kickoff 负责)')
160
+
161
+ await execUpdate(harness, { changes: [{ title: '唯一任务', status: 'done' }] })
162
+ assert.equal(observerText(harness), '', '全完成不打扰')
163
+ })
164
+
165
+ test('可关:observer.enabled=false 时零注入', async () => {
166
+ const harness = createHarness({ config: { observer: { enabled: false } } })
167
+ await harness.run()
168
+ await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
169
+ await execUpdate(harness, { changes: [{ title: '任务', status: 'doing' }] })
170
+ assert.equal(observerText(harness), '')
171
171
  })