dsh-progress-union 0.5.2 → 0.5.5-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.
@@ -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
+ })