dsh-progress-union 0.4.0 → 0.5.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.
- package/CHANGELOG.md +157 -119
- package/LICENSE +21 -21
- package/README.md +201 -173
- package/anchor.js +39 -39
- package/client.js +2959 -2894
- package/cordis.patch.yml +4 -4
- package/docs/consumer-guide.md +210 -210
- package/docs/slot-models-comparison.md +23 -23
- package/docs/slot-trail-fanout.md +31 -31
- package/docs/spirit-reference.md +38 -38
- package/dsh.plugin.json +16 -16
- package/icon.svg +7 -7
- package/index.js +3621 -3106
- package/package.json +77 -77
- package/test/anchor.test.mjs +139 -139
- package/test/assistant.test.mjs +214 -214
- package/test/client.test.mjs +58 -54
- package/test/consumer.test.mjs +101 -101
- package/test/e2e.test.mjs +77 -77
- package/test/goal.test.mjs +110 -110
- package/test/harness.mjs +220 -216
- package/test/host.test.mjs +251 -184
- package/test/observer.test.mjs +170 -170
- package/test/plan.test.mjs +274 -0
- package/test/settings.test.mjs +81 -78
- package/test/stats.test.mjs +54 -54
- package/test/time.test.mjs +147 -147
- package/test/todo-sync.test.mjs +155 -155
package/test/observer.test.mjs
CHANGED
|
@@ -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
|
})
|
|
@@ -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
|
+
})
|