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,215 +1,215 @@
1
- /**
2
- * Phase 8 验收:辅助 LLM(审查/监视)通道。
3
- * - 直连端点、滞后触发、只改 current/next/percent(updatedBy=assistant);
4
- * - 每回合配额 + 最小间隔节流;失败静默入 assistantError;
5
- * - 快照只回非机密字段(apiKey 只回布尔);关着时零 fetch。
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
- function stubFetch(handler) {
17
- const calls = []
18
- const original = globalThis.fetch
19
- globalThis.fetch = async (url, init) => {
20
- calls.push({ url: String(url), init })
21
- return handler(url, init, calls.length)
22
- }
23
- return {
24
- calls,
25
- restore: () => {
26
- globalThis.fetch = original
27
- },
28
- }
29
- }
30
-
31
- function proposal(content) {
32
- return {
33
- ok: true,
34
- status: 200,
35
- async json() {
36
- return { choices: [{ message: { content } }] }
37
- },
38
- }
39
- }
40
-
41
- async function booted(config) {
42
- const harness = createHarness({ config })
43
- await harness.run()
44
- await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
45
- return harness
46
- }
47
-
48
- function execUpdate(harness, args) {
49
- return harness.toolMap.get('progress_update').definition.execute(args, { agent: harness.agent })
50
- }
51
-
52
- async function settle(ms = 30) {
53
- await new Promise((resolve) => setTimeout(resolve, ms))
54
- }
55
-
56
- async function snapshotOf(harness) {
57
- const res = await callRoute(harness, '/progress-union/snapshot', { method: 'GET', url: `/progress-union/snapshot?sessionId=${SESSION}`, headers: {} })
58
- return res.json()
59
- }
60
-
61
- test('命中的提议落地:只改 current/percent,updatedBy=assistant,快照不含密钥明文', async () => {
62
- const harness = await booted({
63
- assistant: { mode: 'direct', baseUrl: 'http://127.0.0.1:9999/v1', apiKey: 'sekret', model: 'mini', minIntervalMs: 0, maxCallsPerTurn: 1, timeoutMs: 5000 },
64
- })
65
- const added = await execUpdate(harness, { changes: [{ title: '造帧', status: 'doing', percent: 10, current: '热身' }] })
66
- const itemId = added.changed[0].id
67
- const stub = stubFetch(() => proposal('```json\n{"board":{"changes":[{"kind":"edit","id":"' + itemId + '","current":"审查:跑集成测试","percent":60},{"kind":"edit","id":"p_fake","current":"x"}]},"round":{"status":"continue","summary":"推进中"},"note":"滞后 6 步,已校准"}\n```'))
68
- try {
69
- harness.emit('tool/call', { name: 'Bash', arguments: JSON.stringify({ command: 'npm test' }) })
70
- for (let i = 0; i < 6; i++) harness.emit('step/start', {})
71
- await settle()
72
- assert.equal(stub.calls.length, 1, '滞后触发一次审查')
73
- assert.ok(stub.calls[0].url.endsWith('/chat/completions'))
74
- assert.equal(stub.calls[0].init.headers.authorization, 'Bearer sekret')
75
-
76
- const snap = await snapshotOf(harness)
77
- const item = snap.items.find((x) => x.id === itemId)
78
- assert.equal(item.current, '审查:跑集成测试')
79
- assert.equal(item.percent, 60)
80
- assert.equal(item.status, 'doing', '审查不得改状态')
81
- assert.equal(item.updatedBy, 'assistant')
82
- assert.equal(snap.items.length, 1, '不存在的 id 被丢弃,不得新增任务')
83
-
84
- assert.equal(snap.assistant.enabled, true)
85
- assert.equal(snap.assistant.hasApiKey, true)
86
- assert.equal('apiKey' in snap.assistant, false, '密钥不得回传')
87
- assert.match(harness.contexts.get('progress-union-observer')(), /助手审查:滞后 6 步,已校准/)
88
- } finally {
89
- stub.restore()
90
- }
91
- })
92
-
93
- test('关着时零 fetch;autoApply=false 只记录建议不落地', async () => {
94
- const off = await booted()
95
- await execUpdate(off, { changes: [{ title: '任务', status: 'doing' }] })
96
- const stubOff = stubFetch(() => proposal('{"changes":[],"note":"x"}'))
97
- try {
98
- for (let i = 0; i < 6; i++) off.emit('step/start', {})
99
- await settle()
100
- assert.equal(stubOff.calls.length, 0, '未启用不得发起请求')
101
- assert.equal((await snapshotOf(off)).assistant.enabled, false)
102
- } finally {
103
- stubOff.restore()
104
- }
105
-
106
- const dry = await booted({ assistant: { mode: 'direct', baseUrl: 'http://127.0.0.1:9999/v1', model: 'mini', minIntervalMs: 0, autoApply: false } })
107
- const added = await execUpdate(dry, { changes: [{ title: '任务', status: 'doing', percent: 10, current: '原样' }] })
108
- const itemId = added.changed[0].id
109
- const stubDry = stubFetch(() => proposal('{"board":{"changes":[{"kind":"edit","id":"' + itemId + '","percent":90}]},"round":{"status":"continue"},"note":"建议 90%"}'))
110
- try {
111
- for (let i = 0; i < 6; i++) dry.emit('step/start', {})
112
- await settle()
113
- const snap = await snapshotOf(dry)
114
- assert.equal(snap.items[0].percent, 10, 'autoApply=false 不落地')
115
- assert.match(dry.contexts.get('progress-union-observer')(), /助手审查:建议 90%/)
116
- } finally {
117
- stubDry.restore()
118
- }
119
- })
120
-
121
- test('失败静默:HTTP 错误/坏 JSON 只进 assistantError,不阻断板', async () => {
122
- const harness = await booted({ assistant: { mode: 'direct', baseUrl: 'http://127.0.0.1:9999/v1', model: 'mini', minIntervalMs: 0 } })
123
- await execUpdate(harness, { changes: [{ title: '任务', status: 'doing' }] })
124
- const stub = stubFetch(() => ({ ok: false, status: 503, async json() { return {} } }))
125
- try {
126
- for (let i = 0; i < 6; i++) harness.emit('step/start', {})
127
- await settle()
128
- const text = harness.contexts.get('progress-union-observer')()
129
- assert.match(text, /助手审查失败:HTTP 503/)
130
- assert.equal((await snapshotOf(harness)).items[0].status, 'doing', '板本体不受影响')
131
- } finally {
132
- stub.restore()
133
- }
134
- })
135
-
136
- test('节流与每回合配额:minIntervalMs 内不再发;新回合重置配额', async () => {
137
- const harness = await booted({ assistant: { mode: 'direct', baseUrl: 'http://127.0.0.1:9999/v1', model: 'mini', minIntervalMs: 60_000, maxCallsPerTurn: 1 } })
138
- await execUpdate(harness, { changes: [{ title: '任务', status: 'doing' }] })
139
- const stub = stubFetch(() => proposal('{"changes":[],"note":"ok"}'))
140
- try {
141
- for (let i = 0; i < 6; i++) harness.emit('step/start', {})
142
- await settle()
143
- assert.equal(stub.calls.length, 1)
144
- for (let i = 0; i < 6; i++) harness.emit('step/start', {})
145
- await settle()
146
- assert.equal(stub.calls.length, 1, '最小间隔内不得重发')
147
- } finally {
148
- stub.restore()
149
- }
150
- })
151
-
152
- test('assistant 逐叶子 volatile:live ref 每回合重读,改值不需重启', async () => {
153
- const ref = (getter) => ({ get: getter })
154
- const live = { mode: 'direct', baseUrl: 'http://127.0.0.1:9999/v1', model: 'mini' }
155
- const harness = createHarness({
156
- config: {
157
- assistant: {
158
- mode: ref(() => live.mode),
159
- baseUrl: ref(() => live.baseUrl),
160
- model: ref(() => live.model),
161
- minIntervalMs: ref(() => 0),
162
- },
163
- },
164
- })
165
- await harness.run()
166
- const on = await snapshotOf(harness)
167
- assert.equal(on.assistant.mode, 'direct')
168
- assert.equal(on.assistant.enabled, true)
169
- live.mode = ''
170
- const off = await snapshotOf(harness)
171
- assert.equal(off.assistant.enabled, false, '设置页关掉后下一次读即生效')
172
- })
173
- test('registered 模式:走宿主 llm.stream(system 走 options.system,content 为块数组),消费 text-delta + 槽位管理 + 轮次草案', async () => {
174
- const streamCalls = []
175
- const llm = {
176
- async *stream(options) {
177
- streamCalls.push(options)
178
- yield { type: 'text-delta', text: '{}' }
179
- yield { type: 'finish', reason: { kind: 'stop' } }
180
- },
181
- }
182
- const harness = createHarness({
183
- config: { assistant: { mode: 'registered', provider: 'local', model: 'qwen', minIntervalMs: 0, maxCallsPerTurn: 1 } },
184
- llm,
185
- })
186
- await harness.run()
187
- await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
188
- const added = await execUpdate(harness, { changes: [{ title: '任务', status: 'doing', percent: 10 }] })
189
- const itemId = added.changed[0].id
190
- llm.stream = async function* (options) {
191
- streamCalls.push(options)
192
- yield { type: 'text-delta', text: '{"board":{"changes":[' }
193
- yield { type: 'text-delta', text: `{"kind":"edit","id":"${itemId}","percent":75}` }
194
- yield { type: 'text-delta', text: ']},"round":{"status":"continue","summary":"推进中"},"note":"registered 校准"}' }
195
- yield { type: 'finish', reason: { kind: 'stop' } }
196
- }
197
- for (let i = 0; i < 6; i++) harness.emit('step/start', {})
198
- await settle()
199
- assert.equal(streamCalls.length, 1, 'registered 走 llm.stream 而非 fetch')
200
- assert.equal(streamCalls[0].provider, 'local')
201
- assert.equal(streamCalls[0].model, 'qwen')
202
- // 契约守卫(2026-11-26 事故回归):system 必须在 options.system,不能藏在 messages 里,
203
- // 否则 adapter 不映射 system 槽 → DeepSeek 回落到内置 "You are a helpful assistant."
204
- assert.equal(typeof streamCalls[0].system, 'string')
205
- assert.match(streamCalls[0].system, /progress assistant/, 'system 必须是 ASSISTANT_SYSTEM 正文')
206
- assert.deepEqual(streamCalls[0].messages.map((m) => m.role), ['user'], 'messages 里不得出现 role:system')
207
- const blocks = streamCalls[0].messages[0].content
208
- assert.ok(Array.isArray(blocks), 'content 必须是 ContentBlock[],不能是裸字符串')
209
- assert.equal(blocks[0].type, 'text')
210
- assert.match(blocks[0].text, /目标|\[~\]/, '输入正文必须带上板清单')
211
- const snap = await snapshotOf(harness)
212
- assert.equal(snap.items[0].percent, 75, '助手改了槽位')
213
- assert.equal(snap.items[0].updatedBy, 'assistant')
214
- assert.match(harness.contexts.get('progress-union-observer')(), /助手轮次草案.*推进中/)
1
+ /**
2
+ * Phase 8 验收:辅助 LLM(审查/监视)通道。
3
+ * - 直连端点、滞后触发、只改 current/next/percent(updatedBy=assistant);
4
+ * - 每回合配额 + 最小间隔节流;失败静默入 assistantError;
5
+ * - 快照只回非机密字段(apiKey 只回布尔);关着时零 fetch。
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
+ function stubFetch(handler) {
17
+ const calls = []
18
+ const original = globalThis.fetch
19
+ globalThis.fetch = async (url, init) => {
20
+ calls.push({ url: String(url), init })
21
+ return handler(url, init, calls.length)
22
+ }
23
+ return {
24
+ calls,
25
+ restore: () => {
26
+ globalThis.fetch = original
27
+ },
28
+ }
29
+ }
30
+
31
+ function proposal(content) {
32
+ return {
33
+ ok: true,
34
+ status: 200,
35
+ async json() {
36
+ return { choices: [{ message: { content } }] }
37
+ },
38
+ }
39
+ }
40
+
41
+ async function booted(config) {
42
+ const harness = createHarness({ config })
43
+ await harness.run()
44
+ await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
45
+ return harness
46
+ }
47
+
48
+ function execUpdate(harness, args) {
49
+ return harness.toolMap.get('progress_update').definition.execute(args, { agent: harness.agent })
50
+ }
51
+
52
+ async function settle(ms = 30) {
53
+ await new Promise((resolve) => setTimeout(resolve, ms))
54
+ }
55
+
56
+ async function snapshotOf(harness) {
57
+ const res = await callRoute(harness, '/progress-union/snapshot', { method: 'GET', url: `/progress-union/snapshot?sessionId=${SESSION}`, headers: {} })
58
+ return res.json()
59
+ }
60
+
61
+ test('命中的提议落地:只改 current/percent,updatedBy=assistant,快照不含密钥明文', async () => {
62
+ const harness = await booted({
63
+ assistant: { mode: 'direct', baseUrl: 'http://127.0.0.1:9999/v1', apiKey: 'sekret', model: 'mini', minIntervalMs: 0, maxCallsPerTurn: 1, timeoutMs: 5000 },
64
+ })
65
+ const added = await execUpdate(harness, { changes: [{ title: '造帧', status: 'doing', percent: 10, current: '热身' }] })
66
+ const itemId = added.changed[0].id
67
+ const stub = stubFetch(() => proposal('```json\n{"board":{"changes":[{"kind":"edit","id":"' + itemId + '","current":"审查:跑集成测试","percent":60},{"kind":"edit","id":"p_fake","current":"x"}]},"round":{"status":"continue","summary":"推进中"},"note":"滞后 6 步,已校准"}\n```'))
68
+ try {
69
+ harness.emit('tool/call', { name: 'Bash', arguments: JSON.stringify({ command: 'npm test' }) })
70
+ for (let i = 0; i < 6; i++) harness.emit('step/start', {})
71
+ await settle()
72
+ assert.equal(stub.calls.length, 1, '滞后触发一次审查')
73
+ assert.ok(stub.calls[0].url.endsWith('/chat/completions'))
74
+ assert.equal(stub.calls[0].init.headers.authorization, 'Bearer sekret')
75
+
76
+ const snap = await snapshotOf(harness)
77
+ const item = snap.items.find((x) => x.id === itemId)
78
+ assert.equal(item.current, '审查:跑集成测试')
79
+ assert.equal(item.percent, 60)
80
+ assert.equal(item.status, 'doing', '审查不得改状态')
81
+ assert.equal(item.updatedBy, 'assistant')
82
+ assert.equal(snap.items.length, 1, '不存在的 id 被丢弃,不得新增任务')
83
+
84
+ assert.equal(snap.assistant.enabled, true)
85
+ assert.equal(snap.assistant.hasApiKey, true)
86
+ assert.equal('apiKey' in snap.assistant, false, '密钥不得回传')
87
+ assert.match(harness.contexts.get('progress-union-observer')(), /助手审查:滞后 6 步,已校准/)
88
+ } finally {
89
+ stub.restore()
90
+ }
91
+ })
92
+
93
+ test('关着时零 fetch;autoApply=false 只记录建议不落地', async () => {
94
+ const off = await booted()
95
+ await execUpdate(off, { changes: [{ title: '任务', status: 'doing' }] })
96
+ const stubOff = stubFetch(() => proposal('{"changes":[],"note":"x"}'))
97
+ try {
98
+ for (let i = 0; i < 6; i++) off.emit('step/start', {})
99
+ await settle()
100
+ assert.equal(stubOff.calls.length, 0, '未启用不得发起请求')
101
+ assert.equal((await snapshotOf(off)).assistant.enabled, false)
102
+ } finally {
103
+ stubOff.restore()
104
+ }
105
+
106
+ const dry = await booted({ assistant: { mode: 'direct', baseUrl: 'http://127.0.0.1:9999/v1', model: 'mini', minIntervalMs: 0, autoApply: false } })
107
+ const added = await execUpdate(dry, { changes: [{ title: '任务', status: 'doing', percent: 10, current: '原样' }] })
108
+ const itemId = added.changed[0].id
109
+ const stubDry = stubFetch(() => proposal('{"board":{"changes":[{"kind":"edit","id":"' + itemId + '","percent":90}]},"round":{"status":"continue"},"note":"建议 90%"}'))
110
+ try {
111
+ for (let i = 0; i < 6; i++) dry.emit('step/start', {})
112
+ await settle()
113
+ const snap = await snapshotOf(dry)
114
+ assert.equal(snap.items[0].percent, 10, 'autoApply=false 不落地')
115
+ assert.match(dry.contexts.get('progress-union-observer')(), /助手审查:建议 90%/)
116
+ } finally {
117
+ stubDry.restore()
118
+ }
119
+ })
120
+
121
+ test('失败静默:HTTP 错误/坏 JSON 只进 assistantError,不阻断板', async () => {
122
+ const harness = await booted({ assistant: { mode: 'direct', baseUrl: 'http://127.0.0.1:9999/v1', model: 'mini', minIntervalMs: 0 } })
123
+ await execUpdate(harness, { changes: [{ title: '任务', status: 'doing' }] })
124
+ const stub = stubFetch(() => ({ ok: false, status: 503, async json() { return {} } }))
125
+ try {
126
+ for (let i = 0; i < 6; i++) harness.emit('step/start', {})
127
+ await settle()
128
+ const text = harness.contexts.get('progress-union-observer')()
129
+ assert.match(text, /助手审查失败:HTTP 503/)
130
+ assert.equal((await snapshotOf(harness)).items[0].status, 'doing', '板本体不受影响')
131
+ } finally {
132
+ stub.restore()
133
+ }
134
+ })
135
+
136
+ test('节流与每回合配额:minIntervalMs 内不再发;新回合重置配额', async () => {
137
+ const harness = await booted({ assistant: { mode: 'direct', baseUrl: 'http://127.0.0.1:9999/v1', model: 'mini', minIntervalMs: 60_000, maxCallsPerTurn: 1 } })
138
+ await execUpdate(harness, { changes: [{ title: '任务', status: 'doing' }] })
139
+ const stub = stubFetch(() => proposal('{"changes":[],"note":"ok"}'))
140
+ try {
141
+ for (let i = 0; i < 6; i++) harness.emit('step/start', {})
142
+ await settle()
143
+ assert.equal(stub.calls.length, 1)
144
+ for (let i = 0; i < 6; i++) harness.emit('step/start', {})
145
+ await settle()
146
+ assert.equal(stub.calls.length, 1, '最小间隔内不得重发')
147
+ } finally {
148
+ stub.restore()
149
+ }
150
+ })
151
+
152
+ test('assistant 逐叶子 volatile:live ref 每回合重读,改值不需重启', async () => {
153
+ const ref = (getter) => ({ get: getter })
154
+ const live = { mode: 'direct', baseUrl: 'http://127.0.0.1:9999/v1', model: 'mini' }
155
+ const harness = createHarness({
156
+ config: {
157
+ assistant: {
158
+ mode: ref(() => live.mode),
159
+ baseUrl: ref(() => live.baseUrl),
160
+ model: ref(() => live.model),
161
+ minIntervalMs: ref(() => 0),
162
+ },
163
+ },
164
+ })
165
+ await harness.run()
166
+ const on = await snapshotOf(harness)
167
+ assert.equal(on.assistant.mode, 'direct')
168
+ assert.equal(on.assistant.enabled, true)
169
+ live.mode = ''
170
+ const off = await snapshotOf(harness)
171
+ assert.equal(off.assistant.enabled, false, '设置页关掉后下一次读即生效')
172
+ })
173
+ test('registered 模式:走宿主 llm.stream(system 走 options.system,content 为块数组),消费 text-delta + 槽位管理 + 轮次草案', async () => {
174
+ const streamCalls = []
175
+ const llm = {
176
+ async *stream(options) {
177
+ streamCalls.push(options)
178
+ yield { type: 'text-delta', text: '{}' }
179
+ yield { type: 'finish', reason: { kind: 'stop' } }
180
+ },
181
+ }
182
+ const harness = createHarness({
183
+ config: { assistant: { mode: 'registered', provider: 'local', model: 'qwen', minIntervalMs: 0, maxCallsPerTurn: 1 } },
184
+ llm,
185
+ })
186
+ await harness.run()
187
+ await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
188
+ const added = await execUpdate(harness, { changes: [{ title: '任务', status: 'doing', percent: 10 }] })
189
+ const itemId = added.changed[0].id
190
+ llm.stream = async function* (options) {
191
+ streamCalls.push(options)
192
+ yield { type: 'text-delta', text: '{"board":{"changes":[' }
193
+ yield { type: 'text-delta', text: `{"kind":"edit","id":"${itemId}","percent":75}` }
194
+ yield { type: 'text-delta', text: ']},"round":{"status":"continue","summary":"推进中"},"note":"registered 校准"}' }
195
+ yield { type: 'finish', reason: { kind: 'stop' } }
196
+ }
197
+ for (let i = 0; i < 6; i++) harness.emit('step/start', {})
198
+ await settle()
199
+ assert.equal(streamCalls.length, 1, 'registered 走 llm.stream 而非 fetch')
200
+ assert.equal(streamCalls[0].provider, 'local')
201
+ assert.equal(streamCalls[0].model, 'qwen')
202
+ // 契约守卫(2026-11-26 事故回归):system 必须在 options.system,不能藏在 messages 里,
203
+ // 否则 adapter 不映射 system 槽 → DeepSeek 回落到内置 "You are a helpful assistant."
204
+ assert.equal(typeof streamCalls[0].system, 'string')
205
+ assert.match(streamCalls[0].system, /progress assistant/, 'system 必须是 ASSISTANT_SYSTEM 正文')
206
+ assert.deepEqual(streamCalls[0].messages.map((m) => m.role), ['user'], 'messages 里不得出现 role:system')
207
+ const blocks = streamCalls[0].messages[0].content
208
+ assert.ok(Array.isArray(blocks), 'content 必须是 ContentBlock[],不能是裸字符串')
209
+ assert.equal(blocks[0].type, 'text')
210
+ assert.match(blocks[0].text, /目标|\[~\]/, '输入正文必须带上板清单')
211
+ const snap = await snapshotOf(harness)
212
+ assert.equal(snap.items[0].percent, 75, '助手改了槽位')
213
+ assert.equal(snap.items[0].updatedBy, 'assistant')
214
+ assert.match(harness.contexts.get('progress-union-observer')(), /助手轮次草案.*推进中/)
215
215
  })