dsh-progress-union 0.6.3 → 0.7.0-rc.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 +387 -373
- package/LICENSE +21 -21
- package/README.md +286 -286
- package/anchor.js +39 -39
- package/client.js +2993 -2993
- package/cordis.patch.yml +4 -4
- 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 +1 -1
- package/icon.svg +7 -7
- package/index.js +4993 -4568
- package/package.json +93 -79
- package/test/anchor.test.mjs +139 -139
- package/test/assistant.test.mjs +214 -214
- package/test/client.test.mjs +190 -190
- package/test/download-classify.test.mjs +199 -0
- package/test/e2e.test.mjs +77 -77
- package/test/goal-drive.test.mjs +17 -17
- package/test/harness.mjs +249 -249
- package/test/host.test.mjs +251 -251
- package/test/observer.test.mjs +170 -170
- package/test/parse-progress.test.mjs +78 -0
- package/test/settings.test.mjs +81 -81
- package/test/star-panel.test.mjs +181 -181
- package/test/time.test.mjs +147 -147
- package/test/todo-sync.test.mjs +155 -155
package/test/time.test.mjs
CHANGED
|
@@ -1,147 +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
|
-
})
|
|
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
|
+
})
|
package/test/todo-sync.test.mjs
CHANGED
|
@@ -1,155 +1,155 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* v0.3.0 验收:板 → 官方 todo 镜像 + 助手冲突裁决。
|
|
3
|
-
* - 映射(done→completed / 首条 doing→in_progress / todo+blocked→pending、content 唯一);
|
|
4
|
-
* - 200ms 去抖单次写入、回环不触发裁决、off 不写、无 append 静默降级;
|
|
5
|
-
* - 三种 resolution 落地与助手失败两侧不改。
|
|
6
|
-
*/
|
|
7
|
-
import assert from 'node:assert/strict'
|
|
8
|
-
import fs from 'node:fs'
|
|
9
|
-
import os from 'node:os'
|
|
10
|
-
import path from 'node:path'
|
|
11
|
-
import test from 'node:test'
|
|
12
|
-
|
|
13
|
-
process.env.DSH_HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'union-test-'))
|
|
14
|
-
const { SESSION, createHarness, jsonReq, callRoute } = await import('./harness.mjs')
|
|
15
|
-
|
|
16
|
-
async function booted(config = {}) {
|
|
17
|
-
const harness = createHarness({ config: { ...config, todo: { mirrorThrottleMs: 5, ...(config.todo || {}) } } })
|
|
18
|
-
await harness.run()
|
|
19
|
-
await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
|
|
20
|
-
return harness
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function execUpdate(harness, args) {
|
|
24
|
-
return harness.toolMap.get('progress_update').definition.execute(args, { agent: harness.agent })
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async function settle(ms = 30) {
|
|
28
|
-
await new Promise((resolve) => setTimeout(resolve, ms))
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function stubFetch(handler) {
|
|
32
|
-
const calls = []
|
|
33
|
-
const original = globalThis.fetch
|
|
34
|
-
globalThis.fetch = async (url, init) => {
|
|
35
|
-
calls.push({ url: String(url), init })
|
|
36
|
-
return handler(url, init, calls.length)
|
|
37
|
-
}
|
|
38
|
-
return { calls, restore: () => { globalThis.fetch = original } }
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function proposal(content) {
|
|
42
|
-
return { ok: true, status: 200, async json() { return { choices: [{ message: { content } }] } } }
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
async function snapshot(harness) {
|
|
46
|
-
const res = await callRoute(harness, '/progress-union/snapshot', { method: 'GET', url: `/progress-union/snapshot?sessionId=${SESSION}`, headers: {} })
|
|
47
|
-
return res.json()
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
test('映射:done→completed、仅首条 doing→in_progress、todo/blocked→pending、content 去重、in_progress≤1', async () => {
|
|
51
|
-
const harness = await booted()
|
|
52
|
-
await execUpdate(harness, { changes: [
|
|
53
|
-
{ title: '甲', status: 'done' },
|
|
54
|
-
{ title: '乙', status: 'doing' },
|
|
55
|
-
{ title: '丙', status: 'doing' },
|
|
56
|
-
{ title: '丁', status: 'blocked' },
|
|
57
|
-
{ title: '乙', status: 'todo' }, // 重复标题,需去重
|
|
58
|
-
] })
|
|
59
|
-
await settle()
|
|
60
|
-
const writes = harness.appends.filter((w) => w.type === 'todo/write')
|
|
61
|
-
assert.equal(writes.length, 1, '去抖后只写一次')
|
|
62
|
-
const todos = writes[0].data.todos
|
|
63
|
-
assert.deepEqual(todos.map((t) => t.status), ['completed', 'pending', 'in_progress', 'pending'])
|
|
64
|
-
assert.deepEqual(todos.map((t) => t.content), ['甲', '乙', '丙', '丁'])
|
|
65
|
-
assert.equal(todos.filter((t) => t.status === 'in_progress').length, 1)
|
|
66
|
-
assert.equal((await snapshot(harness)).todo.sync, 'reconcile')
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
test('去抖与回环:连续写板 5 次只镜像一次;自家 hash 命中不触发裁决', async () => {
|
|
70
|
-
const harness = await booted({
|
|
71
|
-
assistant: { mode: 'direct', baseUrl: 'http://127.0.0.1:9999/v1', model: 'mini', minIntervalMs: 0 },
|
|
72
|
-
})
|
|
73
|
-
const stub = stubFetch(() => proposal('{"resolution":"keep-board","note":"no-op"}'))
|
|
74
|
-
try {
|
|
75
|
-
for (let i = 0; i < 5; i += 1) await execUpdate(harness, { changes: [{ title: `任务${i}`, status: i === 0 ? 'doing' : 'todo' }] })
|
|
76
|
-
await settle(50)
|
|
77
|
-
const writes = harness.appends.filter((w) => w.type === 'todo/write')
|
|
78
|
-
assert.equal(writes.length, 1, '5 次写板 → 1 次镜像')
|
|
79
|
-
// 把镜像内容当自家事件回灌:hash 命中,零裁决
|
|
80
|
-
harness.emit('todo/write', { todos: writes[0].data.todos })
|
|
81
|
-
await settle()
|
|
82
|
-
assert.equal(stub.calls.length, 0, '自家写入不得触发裁决')
|
|
83
|
-
} finally {
|
|
84
|
-
stub.restore()
|
|
85
|
-
}
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
test('reconcile 三支:adopt-todo 覆盖板、keep-board 重镜像、merge 增量落地', async () => {
|
|
89
|
-
for (const [resolution, body, expected] of [
|
|
90
|
-
['adopt-todo', '{"resolution":"adopt-todo","board":{"changes":[]},"note":"todo 更新"}', (snap) => assert.deepEqual(snap.items.map((i) => i.title).sort(), ['新任务'])],
|
|
91
|
-
['keep-board', '{"resolution":"keep-board","board":{"changes":[]},"note":"板为准"}', (snap) => assert.equal(snap.items[0].title, '原任务')],
|
|
92
|
-
['merge', '{"resolution":"merge","board":{"changes":[{"id":"__SELF__","percent":80}]},"note":"合并"}', (snap) => assert.equal(snap.items[0].percent, 80)],
|
|
93
|
-
]) {
|
|
94
|
-
const harness = await booted({
|
|
95
|
-
assistant: { mode: 'direct', baseUrl: 'http://127.0.0.1:9999/v1', model: 'mini', minIntervalMs: 0 },
|
|
96
|
-
todo: { maxReconcileIntervalMs: 0 },
|
|
97
|
-
})
|
|
98
|
-
const added = await execUpdate(harness, { changes: [{ title: '原任务', status: 'doing', percent: 10 }] })
|
|
99
|
-
const selfId = added.changed[0].id
|
|
100
|
-
await settle() // 先镜像落定
|
|
101
|
-
const stub = stubFetch(() => proposal(body.replace('__SELF__', selfId)))
|
|
102
|
-
try {
|
|
103
|
-
harness.emit('todo/write', { todos: [
|
|
104
|
-
{ content: resolution === 'adopt-todo' ? '新任务' : '外部任务', status: 'in_progress' },
|
|
105
|
-
] })
|
|
106
|
-
await settle(60)
|
|
107
|
-
const snap = await snapshot(harness)
|
|
108
|
-
expected(snap)
|
|
109
|
-
assert.match(harness.contexts.get('progress-union-observer')(), /todo 裁决:/)
|
|
110
|
-
} finally {
|
|
111
|
-
stub.restore()
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
test('助手失败/坏 JSON/未配置:两侧均不改,观察块出现漂移行', async () => {
|
|
117
|
-
const harness = await booted({
|
|
118
|
-
assistant: { mode: 'direct', baseUrl: 'http://127.0.0.1:9999/v1', model: 'mini', minIntervalMs: 0 },
|
|
119
|
-
todo: { maxReconcileIntervalMs: 0 },
|
|
120
|
-
})
|
|
121
|
-
await execUpdate(harness, { changes: [{ title: '原任务', status: 'doing', percent: 10 }] })
|
|
122
|
-
await settle()
|
|
123
|
-
const snapBefore = await snapshot(harness)
|
|
124
|
-
const stub = stubFetch(() => ({ ok: false, status: 503, async json() { return {} } }))
|
|
125
|
-
try {
|
|
126
|
-
harness.emit('todo/write', { todos: [
|
|
127
|
-
{ content: '外部任务', status: 'in_progress' },
|
|
128
|
-
{ content: '外部任务2', status: 'completed' },
|
|
129
|
-
] })
|
|
130
|
-
await settle(60)
|
|
131
|
-
const snapAfter = await snapshot(harness)
|
|
132
|
-
assert.deepEqual(snapAfter.items, snapBefore.items, '失败不改板')
|
|
133
|
-
assert.equal(harness.appends.length, 1, '失败不重新镜像')
|
|
134
|
-
assert.match(harness.contexts.get('progress-union-observer')(), /\[todo 漂移\]/)
|
|
135
|
-
assert.match(harness.contexts.get('progress-union-observer')(), /助手审查失败:HTTP 503/)
|
|
136
|
-
} finally {
|
|
137
|
-
stub.restore()
|
|
138
|
-
}
|
|
139
|
-
})
|
|
140
|
-
|
|
141
|
-
test('todoSync=off 零写入;宿主无 session.append 静默降级', async () => {
|
|
142
|
-
const off = await booted({ todoSync: 'off' })
|
|
143
|
-
await execUpdate(off, { changes: [{ title: '任务', status: 'doing' }] })
|
|
144
|
-
await settle(30)
|
|
145
|
-
assert.equal(off.appends.length, 0, 'off 时零 todo/write')
|
|
146
|
-
|
|
147
|
-
const harness = createHarness({ config: { todo: { mirrorThrottleMs: 5 } } })
|
|
148
|
-
delete harness.agent.session.append // 旧宿主:无 append
|
|
149
|
-
await harness.run()
|
|
150
|
-
await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
|
|
151
|
-
await execUpdate(harness, { changes: [{ title: '任务', status: 'doing' }] })
|
|
152
|
-
await settle(30)
|
|
153
|
-
assert.equal((await snapshot(harness)).items.length, 1, '板不受影响')
|
|
154
|
-
assert.equal((await snapshot(harness)).todo.unsupported, true, '快照标记 unsupported')
|
|
155
|
-
})
|
|
1
|
+
/**
|
|
2
|
+
* v0.3.0 验收:板 → 官方 todo 镜像 + 助手冲突裁决。
|
|
3
|
+
* - 映射(done→completed / 首条 doing→in_progress / todo+blocked→pending、content 唯一);
|
|
4
|
+
* - 200ms 去抖单次写入、回环不触发裁决、off 不写、无 append 静默降级;
|
|
5
|
+
* - 三种 resolution 落地与助手失败两侧不改。
|
|
6
|
+
*/
|
|
7
|
+
import assert from 'node:assert/strict'
|
|
8
|
+
import fs from 'node:fs'
|
|
9
|
+
import os from 'node:os'
|
|
10
|
+
import path from 'node:path'
|
|
11
|
+
import test from 'node:test'
|
|
12
|
+
|
|
13
|
+
process.env.DSH_HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'union-test-'))
|
|
14
|
+
const { SESSION, createHarness, jsonReq, callRoute } = await import('./harness.mjs')
|
|
15
|
+
|
|
16
|
+
async function booted(config = {}) {
|
|
17
|
+
const harness = createHarness({ config: { ...config, todo: { mirrorThrottleMs: 5, ...(config.todo || {}) } } })
|
|
18
|
+
await harness.run()
|
|
19
|
+
await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
|
|
20
|
+
return harness
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function execUpdate(harness, args) {
|
|
24
|
+
return harness.toolMap.get('progress_update').definition.execute(args, { agent: harness.agent })
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function settle(ms = 30) {
|
|
28
|
+
await new Promise((resolve) => setTimeout(resolve, ms))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function stubFetch(handler) {
|
|
32
|
+
const calls = []
|
|
33
|
+
const original = globalThis.fetch
|
|
34
|
+
globalThis.fetch = async (url, init) => {
|
|
35
|
+
calls.push({ url: String(url), init })
|
|
36
|
+
return handler(url, init, calls.length)
|
|
37
|
+
}
|
|
38
|
+
return { calls, restore: () => { globalThis.fetch = original } }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function proposal(content) {
|
|
42
|
+
return { ok: true, status: 200, async json() { return { choices: [{ message: { content } }] } } }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function snapshot(harness) {
|
|
46
|
+
const res = await callRoute(harness, '/progress-union/snapshot', { method: 'GET', url: `/progress-union/snapshot?sessionId=${SESSION}`, headers: {} })
|
|
47
|
+
return res.json()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
test('映射:done→completed、仅首条 doing→in_progress、todo/blocked→pending、content 去重、in_progress≤1', async () => {
|
|
51
|
+
const harness = await booted()
|
|
52
|
+
await execUpdate(harness, { changes: [
|
|
53
|
+
{ title: '甲', status: 'done' },
|
|
54
|
+
{ title: '乙', status: 'doing' },
|
|
55
|
+
{ title: '丙', status: 'doing' },
|
|
56
|
+
{ title: '丁', status: 'blocked' },
|
|
57
|
+
{ title: '乙', status: 'todo' }, // 重复标题,需去重
|
|
58
|
+
] })
|
|
59
|
+
await settle()
|
|
60
|
+
const writes = harness.appends.filter((w) => w.type === 'todo/write')
|
|
61
|
+
assert.equal(writes.length, 1, '去抖后只写一次')
|
|
62
|
+
const todos = writes[0].data.todos
|
|
63
|
+
assert.deepEqual(todos.map((t) => t.status), ['completed', 'pending', 'in_progress', 'pending'])
|
|
64
|
+
assert.deepEqual(todos.map((t) => t.content), ['甲', '乙', '丙', '丁'])
|
|
65
|
+
assert.equal(todos.filter((t) => t.status === 'in_progress').length, 1)
|
|
66
|
+
assert.equal((await snapshot(harness)).todo.sync, 'reconcile')
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test('去抖与回环:连续写板 5 次只镜像一次;自家 hash 命中不触发裁决', async () => {
|
|
70
|
+
const harness = await booted({
|
|
71
|
+
assistant: { mode: 'direct', baseUrl: 'http://127.0.0.1:9999/v1', model: 'mini', minIntervalMs: 0 },
|
|
72
|
+
})
|
|
73
|
+
const stub = stubFetch(() => proposal('{"resolution":"keep-board","note":"no-op"}'))
|
|
74
|
+
try {
|
|
75
|
+
for (let i = 0; i < 5; i += 1) await execUpdate(harness, { changes: [{ title: `任务${i}`, status: i === 0 ? 'doing' : 'todo' }] })
|
|
76
|
+
await settle(50)
|
|
77
|
+
const writes = harness.appends.filter((w) => w.type === 'todo/write')
|
|
78
|
+
assert.equal(writes.length, 1, '5 次写板 → 1 次镜像')
|
|
79
|
+
// 把镜像内容当自家事件回灌:hash 命中,零裁决
|
|
80
|
+
harness.emit('todo/write', { todos: writes[0].data.todos })
|
|
81
|
+
await settle()
|
|
82
|
+
assert.equal(stub.calls.length, 0, '自家写入不得触发裁决')
|
|
83
|
+
} finally {
|
|
84
|
+
stub.restore()
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
test('reconcile 三支:adopt-todo 覆盖板、keep-board 重镜像、merge 增量落地', async () => {
|
|
89
|
+
for (const [resolution, body, expected] of [
|
|
90
|
+
['adopt-todo', '{"resolution":"adopt-todo","board":{"changes":[]},"note":"todo 更新"}', (snap) => assert.deepEqual(snap.items.map((i) => i.title).sort(), ['新任务'])],
|
|
91
|
+
['keep-board', '{"resolution":"keep-board","board":{"changes":[]},"note":"板为准"}', (snap) => assert.equal(snap.items[0].title, '原任务')],
|
|
92
|
+
['merge', '{"resolution":"merge","board":{"changes":[{"id":"__SELF__","percent":80}]},"note":"合并"}', (snap) => assert.equal(snap.items[0].percent, 80)],
|
|
93
|
+
]) {
|
|
94
|
+
const harness = await booted({
|
|
95
|
+
assistant: { mode: 'direct', baseUrl: 'http://127.0.0.1:9999/v1', model: 'mini', minIntervalMs: 0 },
|
|
96
|
+
todo: { maxReconcileIntervalMs: 0 },
|
|
97
|
+
})
|
|
98
|
+
const added = await execUpdate(harness, { changes: [{ title: '原任务', status: 'doing', percent: 10 }] })
|
|
99
|
+
const selfId = added.changed[0].id
|
|
100
|
+
await settle() // 先镜像落定
|
|
101
|
+
const stub = stubFetch(() => proposal(body.replace('__SELF__', selfId)))
|
|
102
|
+
try {
|
|
103
|
+
harness.emit('todo/write', { todos: [
|
|
104
|
+
{ content: resolution === 'adopt-todo' ? '新任务' : '外部任务', status: 'in_progress' },
|
|
105
|
+
] })
|
|
106
|
+
await settle(60)
|
|
107
|
+
const snap = await snapshot(harness)
|
|
108
|
+
expected(snap)
|
|
109
|
+
assert.match(harness.contexts.get('progress-union-observer')(), /todo 裁决:/)
|
|
110
|
+
} finally {
|
|
111
|
+
stub.restore()
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
test('助手失败/坏 JSON/未配置:两侧均不改,观察块出现漂移行', async () => {
|
|
117
|
+
const harness = await booted({
|
|
118
|
+
assistant: { mode: 'direct', baseUrl: 'http://127.0.0.1:9999/v1', model: 'mini', minIntervalMs: 0 },
|
|
119
|
+
todo: { maxReconcileIntervalMs: 0 },
|
|
120
|
+
})
|
|
121
|
+
await execUpdate(harness, { changes: [{ title: '原任务', status: 'doing', percent: 10 }] })
|
|
122
|
+
await settle()
|
|
123
|
+
const snapBefore = await snapshot(harness)
|
|
124
|
+
const stub = stubFetch(() => ({ ok: false, status: 503, async json() { return {} } }))
|
|
125
|
+
try {
|
|
126
|
+
harness.emit('todo/write', { todos: [
|
|
127
|
+
{ content: '外部任务', status: 'in_progress' },
|
|
128
|
+
{ content: '外部任务2', status: 'completed' },
|
|
129
|
+
] })
|
|
130
|
+
await settle(60)
|
|
131
|
+
const snapAfter = await snapshot(harness)
|
|
132
|
+
assert.deepEqual(snapAfter.items, snapBefore.items, '失败不改板')
|
|
133
|
+
assert.equal(harness.appends.length, 1, '失败不重新镜像')
|
|
134
|
+
assert.match(harness.contexts.get('progress-union-observer')(), /\[todo 漂移\]/)
|
|
135
|
+
assert.match(harness.contexts.get('progress-union-observer')(), /助手审查失败:HTTP 503/)
|
|
136
|
+
} finally {
|
|
137
|
+
stub.restore()
|
|
138
|
+
}
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
test('todoSync=off 零写入;宿主无 session.append 静默降级', async () => {
|
|
142
|
+
const off = await booted({ todoSync: 'off' })
|
|
143
|
+
await execUpdate(off, { changes: [{ title: '任务', status: 'doing' }] })
|
|
144
|
+
await settle(30)
|
|
145
|
+
assert.equal(off.appends.length, 0, 'off 时零 todo/write')
|
|
146
|
+
|
|
147
|
+
const harness = createHarness({ config: { todo: { mirrorThrottleMs: 5 } } })
|
|
148
|
+
delete harness.agent.session.append // 旧宿主:无 append
|
|
149
|
+
await harness.run()
|
|
150
|
+
await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
|
|
151
|
+
await execUpdate(harness, { changes: [{ title: '任务', status: 'doing' }] })
|
|
152
|
+
await settle(30)
|
|
153
|
+
assert.equal((await snapshot(harness)).items.length, 1, '板不受影响')
|
|
154
|
+
assert.equal((await snapshot(harness)).todo.unsupported, true, '快照标记 unsupported')
|
|
155
|
+
})
|