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,137 +1,137 @@
1
- /**
2
- * 0.7.0 rc.2 验收:R5 工序可解释。
3
- * 层 1 = describeToolStep 规则表;层 2 = o.tools 的 gloss + tool/result 回显;
4
- * 层 3 = 观察块末尾的静态叙述纪律行(softNarrate)。
5
- * 硬约束:toolGloss.enabled=false 时观察块输出与 0.6.x 逐字一致。
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
- const { describeToolStep, glossCommand, resultDigest } = await import('../index.js')
16
-
17
- async function booted(config) {
18
- const harness = createHarness({ config })
19
- await harness.run()
20
- await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
21
- await harness.toolMap.get('progress_update').definition.execute({ changes: [{ title: '改造接口', status: 'doing' }] }, { agent: harness.agent })
22
- return harness
23
- }
24
-
25
- function observerText(harness) {
26
- const fn = harness.contexts.get('progress-union-observer')
27
- assert.ok(fn, 'progress-union-observer 上下文必须注册')
28
- return fn()
29
- }
30
-
31
- function actionLine(text) {
32
- const line = String(text).split('\n').find((l) => l.startsWith('最近动作:'))
33
- return line || ''
34
- }
35
-
36
- // ── 层 1:规则表(纯函数)────────────────────────────────────────────────
37
-
38
- test('glossCommand:git/docker/包管理器二跳与首词表', () => {
39
- assert.equal(glossCommand('git clone https://x/y.git'), '克隆仓库')
40
- assert.equal(glossCommand('git commit -m "msg"'), '提交改动')
41
- assert.equal(glossCommand('docker build -t img .'), '构建镜像')
42
- assert.equal(glossCommand('npm test'), '跑测试')
43
- assert.equal(glossCommand('pnpm add zod'), '加依赖')
44
- assert.equal(glossCommand('uv pip install ruff'), '装 Python 包')
45
- assert.equal(glossCommand('ls -la'), '列目录')
46
- assert.equal(glossCommand('npx --yes create-vite@3 app'), '临时跑 create-vite')
47
- // 首段才是本轮意图:复合命令不再被后段带偏
48
- assert.equal(glossCommand('npm ci && npm test'), '按锁文件装依赖')
49
- // 表外词:返回空串,不编「执行 x」
50
- assert.equal(glossCommand('myCustomTool --run'), '')
51
- })
52
-
53
- test('describeToolStep:文件类与检索类工具走参数名,路径只留末段', () => {
54
- assert.equal(describeToolStep('read', JSON.stringify({ file_path: 'src/deep/nested/mod.ts' })), '读文件 mod.ts')
55
- assert.equal(describeToolStep('write', JSON.stringify({ path: 'a/b/out.md' })), '写文件 out.md')
56
- assert.equal(describeToolStep('edit', JSON.stringify({ filePath: 'a/b/c.py' })), '改文件 c.py')
57
- assert.equal(describeToolStep('str_replace_editor', JSON.stringify({ command: 'view', path: 'x/y.ts' })), '读文件 y.ts')
58
- assert.equal(describeToolStep('apply_patch', JSON.stringify({ input: '*** Begin Patch\n*** Update File: pkg/lib/entry.js\n@@' })), '改文件 entry.js')
59
- assert.equal(describeToolStep('grep', JSON.stringify({ pattern: 'unwrapLive' })), '搜 unwrapLive')
60
- assert.equal(describeToolStep('webfetch', JSON.stringify({ url: 'https://docs.example.com/a/b?x=1' })), '抓网页 docs.example.com')
61
- assert.equal(describeToolStep('websearch', JSON.stringify({ query: 'cordis volatile schema' })), '搜网页 cordis volatile schema')
62
- assert.equal(describeToolStep('todo_write', JSON.stringify({ todos: [1, 2, 3].map((i) => ({ content: 't' + i })) })), '更新官方清单 ×3')
63
- assert.equal(describeToolStep('run_code', JSON.stringify({ code: 'tools.shell(1); tools.read(2)' })), 'PTC 内层 shell+read')
64
- })
65
-
66
- test('describeToolStep:act 透传后仍出人话(不记成 act)', () => {
67
- const raw = JSON.stringify({ tool: 'bash', input: { command: 'git push origin main' } })
68
- assert.equal(describeToolStep('act', raw), '推送远端')
69
- })
70
-
71
- test('describeToolStep:猜不出退回 key=value,全空则空串(不写空话)', () => {
72
- assert.equal(describeToolStep('frobnicate', JSON.stringify({ widget: 'fluxcapacitor-mark-ii' })), 'widget=fluxcapacitor-mark-ii')
73
- // 兼容兜底也要有界:单值截 24
74
- assert.equal(describeToolStep('frobnicate', JSON.stringify({ widget: 'supercalifragilisticexpialidocious' })), 'widget=supercalifragilisticexpi')
75
- assert.equal(describeToolStep('frobnicate', '{}'), '')
76
- assert.equal(describeToolStep('frobnicate', ''), '')
77
- // 命令型但参数不是命令:同样不硬编
78
- assert.equal(describeToolStep('bash', JSON.stringify({ cwd: '/tmp' })), 'cwd=/tmp')
79
- })
80
-
81
- test('describeToolStep:maxLen 生效且截断带省略号', () => {
82
- const out = describeToolStep('search', JSON.stringify({ pattern: 'a'.repeat(80) }), 20)
83
- assert.ok(out.length <= 20, `长度 ${out.length} 应 ≤20`)
84
- assert.ok(out.endsWith('…'), '截断必须带省略号')
85
- })
86
-
87
- test('resultDigest:≤60 字、区分成败、无输出不写空话', () => {
88
- const long = 'x'.repeat(200)
89
- const ok = resultDigest(long, false)
90
- assert.ok(ok.startsWith('结果:'))
91
- assert.ok(ok.length <= 60, `长度 ${ok.length} 应 ≤60`)
92
- assert.equal(resultDigest(' built 42 frames ', true), '失败:built 42 frames')
93
- assert.equal(resultDigest('', true), '失败(无输出)')
94
- assert.equal(resultDigest('', false), '')
95
- })
96
-
97
- // ── 层 2/3:接进观察者(真事件流)──────────────────────────────────────
98
-
99
- test('层 2:最近动作带人话,tool/result 追加结果尾巴', async () => {
100
- const harness = await booted()
101
- harness.emit('tool/call', { name: 'Bash', callId: 'c1', arguments: JSON.stringify({ command: 'npm test' }) })
102
- assert.match(actionLine(observerText(harness)), /^最近动作:Bash 跑测试$/)
103
- harness.emit('tool/result', { message: { toolCallId: 'c1', isError: false, content: [{ type: 'text', text: '188 passed in 6.4s' }] } })
104
- assert.match(actionLine(observerText(harness)), /^最近动作:Bash 跑测试 → 结果:188 passed in 6\.4s$/)
105
- })
106
-
107
- test('层 2:连续同名同参仍走 ×N,且新动作会让上一条的结果尾巴作废', async () => {
108
- const harness = await booted()
109
- for (let i = 0; i < 3; i++) harness.emit('tool/call', { name: 'Bash', callId: 'c' + i, arguments: JSON.stringify({ command: 'npm test' }) })
110
- assert.match(actionLine(observerText(harness)), /^最近动作:Bash×3 跑测试$/)
111
- })
112
-
113
- test('层 3:softNarrate 默认开,出静态「▸ 动作 → 本步目的」纪律行', async () => {
114
- const harness = await booted()
115
- const text = observerText(harness)
116
- assert.match(text, /^▸ 每步工具前用一行写清「▸ <动作> → <本步目的>」/m)
117
- })
118
-
119
- test('softNarrate:false 只撤纪律行,人话动作保留', async () => {
120
- const harness = await booted({ toolGloss: { softNarrate: false } })
121
- harness.emit('tool/call', { name: 'Bash', callId: 'c1', arguments: JSON.stringify({ command: 'git status' }) })
122
- const text = observerText(harness)
123
- assert.doesNotMatch(text, /▸ 每步工具前/, '关掉后不得再推叙述纪律')
124
- assert.equal(actionLine(text), '最近动作:Bash 看仓库状态')
125
- })
126
-
127
- test('enabled:false:最近动作逐字退回 0.6.x 的 name×count,且不算 gloss', async () => {
128
- const harness = await booted({ toolGloss: { enabled: false } })
129
- harness.emit('tool/call', { name: 'Bash', callId: 'c1', arguments: JSON.stringify({ command: 'npm test' }) })
130
- harness.emit('tool/call', { name: 'Bash', callId: 'c2', arguments: JSON.stringify({ command: 'npm test' }) })
131
- harness.emit('tool/call', { name: 'Read', callId: 'c3', arguments: JSON.stringify({ file_path: 'a/b.ts' }) })
132
- harness.emit('tool/result', { message: { toolCallId: 'c3', isError: false, content: [{ type: 'text', text: 'export const x = 1' }] } })
133
- // 0.6.x 原算法重算一遍:name(同名连续计数)+ ' → ' 连接,无人话、无结果尾巴、无纪律行
134
- const legacy = '最近动作:' + ['Bash×2', 'Read'].join(' → ')
135
- assert.equal(actionLine(observerText(harness)), legacy)
136
- assert.doesNotMatch(observerText(harness), /▸ 每步工具前/)
137
- })
1
+ /**
2
+ * 0.7.0 rc.2 验收:R5 工序可解释。
3
+ * 层 1 = describeToolStep 规则表;层 2 = o.tools 的 gloss + tool/result 回显;
4
+ * 层 3 = 观察块末尾的静态叙述纪律行(softNarrate)。
5
+ * 硬约束:toolGloss.enabled=false 时观察块输出与 0.6.x 逐字一致。
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
+ const { describeToolStep, glossCommand, resultDigest } = await import('../index.js')
16
+
17
+ async function booted(config) {
18
+ const harness = createHarness({ config })
19
+ await harness.run()
20
+ await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
21
+ await harness.toolMap.get('progress_update').definition.execute({ changes: [{ title: '改造接口', status: 'doing' }] }, { agent: harness.agent })
22
+ return harness
23
+ }
24
+
25
+ function observerText(harness) {
26
+ const fn = harness.contexts.get('progress-union-observer')
27
+ assert.ok(fn, 'progress-union-observer 上下文必须注册')
28
+ return fn()
29
+ }
30
+
31
+ function actionLine(text) {
32
+ const line = String(text).split('\n').find((l) => l.startsWith('最近动作:'))
33
+ return line || ''
34
+ }
35
+
36
+ // ── 层 1:规则表(纯函数)────────────────────────────────────────────────
37
+
38
+ test('glossCommand:git/docker/包管理器二跳与首词表', () => {
39
+ assert.equal(glossCommand('git clone https://x/y.git'), '克隆仓库')
40
+ assert.equal(glossCommand('git commit -m "msg"'), '提交改动')
41
+ assert.equal(glossCommand('docker build -t img .'), '构建镜像')
42
+ assert.equal(glossCommand('npm test'), '跑测试')
43
+ assert.equal(glossCommand('pnpm add zod'), '加依赖')
44
+ assert.equal(glossCommand('uv pip install ruff'), '装 Python 包')
45
+ assert.equal(glossCommand('ls -la'), '列目录')
46
+ assert.equal(glossCommand('npx --yes create-vite@3 app'), '临时跑 create-vite')
47
+ // 首段才是本轮意图:复合命令不再被后段带偏
48
+ assert.equal(glossCommand('npm ci && npm test'), '按锁文件装依赖')
49
+ // 表外词:返回空串,不编「执行 x」
50
+ assert.equal(glossCommand('myCustomTool --run'), '')
51
+ })
52
+
53
+ test('describeToolStep:文件类与检索类工具走参数名,路径只留末段', () => {
54
+ assert.equal(describeToolStep('read', JSON.stringify({ file_path: 'src/deep/nested/mod.ts' })), '读文件 mod.ts')
55
+ assert.equal(describeToolStep('write', JSON.stringify({ path: 'a/b/out.md' })), '写文件 out.md')
56
+ assert.equal(describeToolStep('edit', JSON.stringify({ filePath: 'a/b/c.py' })), '改文件 c.py')
57
+ assert.equal(describeToolStep('str_replace_editor', JSON.stringify({ command: 'view', path: 'x/y.ts' })), '读文件 y.ts')
58
+ assert.equal(describeToolStep('apply_patch', JSON.stringify({ input: '*** Begin Patch\n*** Update File: pkg/lib/entry.js\n@@' })), '改文件 entry.js')
59
+ assert.equal(describeToolStep('grep', JSON.stringify({ pattern: 'unwrapLive' })), '搜 unwrapLive')
60
+ assert.equal(describeToolStep('webfetch', JSON.stringify({ url: 'https://docs.example.com/a/b?x=1' })), '抓网页 docs.example.com')
61
+ assert.equal(describeToolStep('websearch', JSON.stringify({ query: 'cordis volatile schema' })), '搜网页 cordis volatile schema')
62
+ assert.equal(describeToolStep('todo_write', JSON.stringify({ todos: [1, 2, 3].map((i) => ({ content: 't' + i })) })), '更新官方清单 ×3')
63
+ assert.equal(describeToolStep('run_code', JSON.stringify({ code: 'tools.shell(1); tools.read(2)' })), 'PTC 内层 shell+read')
64
+ })
65
+
66
+ test('describeToolStep:act 透传后仍出人话(不记成 act)', () => {
67
+ const raw = JSON.stringify({ tool: 'bash', input: { command: 'git push origin main' } })
68
+ assert.equal(describeToolStep('act', raw), '推送远端')
69
+ })
70
+
71
+ test('describeToolStep:猜不出退回 key=value,全空则空串(不写空话)', () => {
72
+ assert.equal(describeToolStep('frobnicate', JSON.stringify({ widget: 'fluxcapacitor-mark-ii' })), 'widget=fluxcapacitor-mark-ii')
73
+ // 兼容兜底也要有界:单值截 24
74
+ assert.equal(describeToolStep('frobnicate', JSON.stringify({ widget: 'supercalifragilisticexpialidocious' })), 'widget=supercalifragilisticexpi')
75
+ assert.equal(describeToolStep('frobnicate', '{}'), '')
76
+ assert.equal(describeToolStep('frobnicate', ''), '')
77
+ // 命令型但参数不是命令:同样不硬编
78
+ assert.equal(describeToolStep('bash', JSON.stringify({ cwd: '/tmp' })), 'cwd=/tmp')
79
+ })
80
+
81
+ test('describeToolStep:maxLen 生效且截断带省略号', () => {
82
+ const out = describeToolStep('search', JSON.stringify({ pattern: 'a'.repeat(80) }), 20)
83
+ assert.ok(out.length <= 20, `长度 ${out.length} 应 ≤20`)
84
+ assert.ok(out.endsWith('…'), '截断必须带省略号')
85
+ })
86
+
87
+ test('resultDigest:≤60 字、区分成败、无输出不写空话', () => {
88
+ const long = 'x'.repeat(200)
89
+ const ok = resultDigest(long, false)
90
+ assert.ok(ok.startsWith('结果:'))
91
+ assert.ok(ok.length <= 60, `长度 ${ok.length} 应 ≤60`)
92
+ assert.equal(resultDigest(' built 42 frames ', true), '失败:built 42 frames')
93
+ assert.equal(resultDigest('', true), '失败(无输出)')
94
+ assert.equal(resultDigest('', false), '')
95
+ })
96
+
97
+ // ── 层 2/3:接进观察者(真事件流)──────────────────────────────────────
98
+
99
+ test('层 2:最近动作带人话,tool/result 追加结果尾巴', async () => {
100
+ const harness = await booted()
101
+ harness.emit('tool/call', { name: 'Bash', callId: 'c1', arguments: JSON.stringify({ command: 'npm test' }) })
102
+ assert.match(actionLine(observerText(harness)), /^最近动作:Bash 跑测试$/)
103
+ harness.emit('tool/result', { message: { toolCallId: 'c1', isError: false, content: [{ type: 'text', text: '188 passed in 6.4s' }] } })
104
+ assert.match(actionLine(observerText(harness)), /^最近动作:Bash 跑测试 → 结果:188 passed in 6\.4s$/)
105
+ })
106
+
107
+ test('层 2:连续同名同参仍走 ×N,且新动作会让上一条的结果尾巴作废', async () => {
108
+ const harness = await booted()
109
+ for (let i = 0; i < 3; i++) harness.emit('tool/call', { name: 'Bash', callId: 'c' + i, arguments: JSON.stringify({ command: 'npm test' }) })
110
+ assert.match(actionLine(observerText(harness)), /^最近动作:Bash×3 跑测试$/)
111
+ })
112
+
113
+ test('层 3:softNarrate 默认开,出静态「▸ 动作 → 本步目的」纪律行', async () => {
114
+ const harness = await booted()
115
+ const text = observerText(harness)
116
+ assert.match(text, /^▸ 每步工具前用一行写清「▸ <动作> → <本步目的>」/m)
117
+ })
118
+
119
+ test('softNarrate:false 只撤纪律行,人话动作保留', async () => {
120
+ const harness = await booted({ toolGloss: { softNarrate: false } })
121
+ harness.emit('tool/call', { name: 'Bash', callId: 'c1', arguments: JSON.stringify({ command: 'git status' }) })
122
+ const text = observerText(harness)
123
+ assert.doesNotMatch(text, /▸ 每步工具前/, '关掉后不得再推叙述纪律')
124
+ assert.equal(actionLine(text), '最近动作:Bash 看仓库状态')
125
+ })
126
+
127
+ test('enabled:false:最近动作逐字退回 0.6.x 的 name×count,且不算 gloss', async () => {
128
+ const harness = await booted({ toolGloss: { enabled: false } })
129
+ harness.emit('tool/call', { name: 'Bash', callId: 'c1', arguments: JSON.stringify({ command: 'npm test' }) })
130
+ harness.emit('tool/call', { name: 'Bash', callId: 'c2', arguments: JSON.stringify({ command: 'npm test' }) })
131
+ harness.emit('tool/call', { name: 'Read', callId: 'c3', arguments: JSON.stringify({ file_path: 'a/b.ts' }) })
132
+ harness.emit('tool/result', { message: { toolCallId: 'c3', isError: false, content: [{ type: 'text', text: 'export const x = 1' }] } })
133
+ // 0.6.x 原算法重算一遍:name(同名连续计数)+ ' → ' 连接,无人话、无结果尾巴、无纪律行
134
+ const legacy = '最近动作:' + ['Bash×2', 'Read'].join(' → ')
135
+ assert.equal(actionLine(observerText(harness)), legacy)
136
+ assert.doesNotMatch(observerText(harness), /▸ 每步工具前/)
137
+ })