dsh-project-flow 0.9.2 → 0.9.3
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 +0 -0
- package/docs/DETAIL.md +2 -0
- package/index.js +99 -25
- package/package.json +1 -1
- package/tests/run-all.mjs +1 -0
- package/tests/steer-command.test.mjs +151 -0
package/CHANGELOG.md
CHANGED
|
Binary file
|
package/docs/DETAIL.md
CHANGED
|
@@ -166,6 +166,8 @@ project_rollback { project: "my-app", to: "1", mode: "full", dryRun: false }
|
|
|
166
166
|
|
|
167
167
|
`/flow` 只是把「开工作模式」这个意图告诉模型,真正的四阶执行还是由模型调 `project_run` 完成。这样做的好处是不依赖界面面板,插件不会因为 UI 崩溃而用不了。
|
|
168
168
|
|
|
169
|
+
「告诉模型」是把指令作为一条用户消息提交进会话(`agent.steer`),不是只画在界面上。这条区别很重要:宿主 `@deepseek-ai/dsh-commands` 的契约规定命令返回的 `CommandResult.text` 只进 `command/done` 事件、由界面渲染,**不进模型上下文**。只 `return text` 的命令看起来执行了,模型却什么都没收到——0.8.3 到 0.9.2 的 `/flow` 和 `/flow-resume` 就是这个问题,0.9.3 改掉了。
|
|
170
|
+
|
|
169
171
|
### 改模型:不用改配置文件,不用重启
|
|
170
172
|
|
|
171
173
|
```
|
package/index.js
CHANGED
|
@@ -25,7 +25,7 @@ import { join, isAbsolute, dirname, resolve, relative, basename, sep } from 'nod
|
|
|
25
25
|
import { fileURLToPath } from 'node:url'
|
|
26
26
|
import { homedir, tmpdir } from 'node:os'
|
|
27
27
|
import { execFile } from 'node:child_process'
|
|
28
|
-
import { createHash } from 'node:crypto'
|
|
28
|
+
import { createHash, randomUUID } from 'node:crypto'
|
|
29
29
|
import z from '@deepseek-ai/schemastery'
|
|
30
30
|
import { loadFlow, describeFlowProblems, BUILTIN_FLOW } from './flow.schema.mjs'
|
|
31
31
|
import {
|
|
@@ -839,6 +839,72 @@ export function cwdOf(ctx, agent) {
|
|
|
839
839
|
return inFlow ?? valid[0] ?? process.cwd()
|
|
840
840
|
}
|
|
841
841
|
|
|
842
|
+
/**
|
|
843
|
+
* 把一段指令真正交到模型手里。
|
|
844
|
+
*
|
|
845
|
+
* 宿主契约(@deepseek-ai/dsh-commands 的 README「Model Experience」)写得很清楚:
|
|
846
|
+
* 命令的 `CommandResult.text` 只进 `command/done` 生命周期事件,由界面渲染,
|
|
847
|
+
* **不会**提交成用户消息。所以 handler 里 `return { kind, text }` 只能让界面
|
|
848
|
+
* 看起来执行了,模型一个字节都收不到。
|
|
849
|
+
*
|
|
850
|
+
* 命令要模型做事,就得自己往接收方 agent 的收件箱里塞一条 user 消息——
|
|
851
|
+
* dsh-plan-mode 的 `/plan` 就是这么做的。这里照同一条路走:
|
|
852
|
+
*
|
|
853
|
+
* 1. 有 `agent.steer` 且能拿到宿主的 `createUserMessage`:用官方构造器;
|
|
854
|
+
* 2. 有 `agent.steer` 但拿不到那个包:手搓同形状的消息(id / role / content / source
|
|
855
|
+
* 四个字段就是会话校验的全部要求),不引入硬依赖;
|
|
856
|
+
* 3. 老宿主两样都没有:返回 false,调用方退回纯文本,至少不崩。
|
|
857
|
+
*
|
|
858
|
+
* @param agent 接收这条指令的 agent(命令的 invocation.agent)。
|
|
859
|
+
* @param text 要交给模型的完整指令。
|
|
860
|
+
* @returns 是否真的交出去了。
|
|
861
|
+
*/
|
|
862
|
+
export async function steerInstruction(agent, text) {
|
|
863
|
+
if (agent === undefined || typeof agent.steer !== 'function') return false
|
|
864
|
+
if (typeof text !== 'string' || text.trim() === '') return false
|
|
865
|
+
const message = await buildUserMessage(text)
|
|
866
|
+
try {
|
|
867
|
+
agent.steer(message)
|
|
868
|
+
return true
|
|
869
|
+
} catch {
|
|
870
|
+
// 宿主在别的地方炸了(比如收件箱已关闭)也算没交出去,让调用方兜底。
|
|
871
|
+
return false
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
/**
|
|
876
|
+
* 造一条 user 消息。优先用宿主自己的构造器,拿不到就按同样的形状手搓。
|
|
877
|
+
*
|
|
878
|
+
* 动态 import 是故意的:`@deepseek-ai/dsh-llm` 是宿主的包,不写在
|
|
879
|
+
* package.json 的 dependencies 里——装了插件却解析不到它的时候,
|
|
880
|
+
* 静态 import 会让整个插件起不来。动态取失败就降级,不影响加载。
|
|
881
|
+
*
|
|
882
|
+
* 结果缓存一次:解析失败会抛错,每次敲命令都白试一遍没必要。
|
|
883
|
+
*/
|
|
884
|
+
let creatorPromise
|
|
885
|
+
function buildUserMessage(text) {
|
|
886
|
+
creatorPromise ??= (async () => {
|
|
887
|
+
try {
|
|
888
|
+
const llm = await import('@deepseek-ai/dsh-llm')
|
|
889
|
+
return typeof llm.createUserMessage === 'function' ? llm.createUserMessage : null
|
|
890
|
+
} catch {
|
|
891
|
+
// 没装、路径变了、导出改了——都走手搓那份
|
|
892
|
+
return null
|
|
893
|
+
}
|
|
894
|
+
})()
|
|
895
|
+
return creatorPromise.then(createUserMessage => (createUserMessage === null
|
|
896
|
+
? {
|
|
897
|
+
id: randomUUID(),
|
|
898
|
+
role: 'user',
|
|
899
|
+
content: [{ type: 'text', text }],
|
|
900
|
+
source: { kind: 'user' },
|
|
901
|
+
}
|
|
902
|
+
: createUserMessage({
|
|
903
|
+
content: [{ type: 'text', text }],
|
|
904
|
+
source: { kind: 'user' },
|
|
905
|
+
})))
|
|
906
|
+
}
|
|
907
|
+
|
|
842
908
|
/**
|
|
843
909
|
* Gate for stage 1: the intent must be converged before stages 2-4 run.
|
|
844
910
|
*
|
|
@@ -7809,22 +7875,26 @@ async function decorateCost(result, settings, requirements, cwd) {
|
|
|
7809
7875
|
const ask = text === ''
|
|
7810
7876
|
? '我要开工作模式跑一个任务,但我还没说是什么。请先问我一个问题,问出我要做什么。'
|
|
7811
7877
|
: `我要开工作模式跑这个任务:${text}`
|
|
7812
|
-
|
|
7813
|
-
|
|
7814
|
-
|
|
7815
|
-
|
|
7816
|
-
|
|
7817
|
-
|
|
7818
|
-
|
|
7819
|
-
|
|
7820
|
-
|
|
7821
|
-
|
|
7822
|
-
|
|
7823
|
-
|
|
7824
|
-
|
|
7825
|
-
|
|
7826
|
-
|
|
7878
|
+
const instruction = [
|
|
7879
|
+
ask,
|
|
7880
|
+
'',
|
|
7881
|
+
'从现在起按工作模式执行。第 1 段是硬门槛,不是走过场:',
|
|
7882
|
+
`· 用 ask_user_question 一次只问一个问题,问到你对我的真实意图有 ${INTENT_CONFIDENCE_FLOOR} 分把握为止;`,
|
|
7883
|
+
'· 每答一次重新评估,够了就停,别凑问题;',
|
|
7884
|
+
'· 我问过几个问题、你最后的把握是多少、哪些地方是你自己定的默认值,都要在调 project_run 时交上来,',
|
|
7885
|
+
` 低于 ${INTENT_CONFIDENCE_FLOOR} 分或一个问题都没问过,工具会直接报错、不开跑;`,
|
|
7886
|
+
'· 如果我说「就按这个做、别再问了」,传 override: true 并把我这句话写进 overrideReason。',
|
|
7887
|
+
...(created === true
|
|
7888
|
+
? [`· 调 project_run 时带上 cwd: "${gateCwd}"——工作目录已经建在这里,别再用会话默认目录。`]
|
|
7889
|
+
: []),
|
|
7890
|
+
].join('\n')
|
|
7891
|
+
// 把指令真的交给模型。交不出去(老宿主)才退回原来的纯文本,
|
|
7892
|
+
// 让界面至少有内容,别静默什么都不发生。
|
|
7893
|
+
if (await steerInstruction(invocation.agent, instruction)) {
|
|
7894
|
+
return { kind: 'success', text: '已开工作模式,任务已交给模型。' }
|
|
7827
7895
|
}
|
|
7896
|
+
// 交不出去的那个老宿主:原样把指令显示在界面上,内容和 steer 出去的完全一致。
|
|
7897
|
+
return { kind: 'success', text: instruction }
|
|
7828
7898
|
},
|
|
7829
7899
|
})
|
|
7830
7900
|
const offStatusCmd = commands.register({
|
|
@@ -7899,16 +7969,20 @@ async function decorateCost(result, settings, requirements, cwd) {
|
|
|
7899
7969
|
}
|
|
7900
7970
|
}
|
|
7901
7971
|
const state = await readState(cwd, project)
|
|
7902
|
-
|
|
7903
|
-
|
|
7904
|
-
|
|
7905
|
-
state
|
|
7906
|
-
|
|
7907
|
-
|
|
7908
|
-
|
|
7909
|
-
|
|
7910
|
-
|
|
7972
|
+
// 这段的每一句都是要模型去做的动作,所以整段走 steer;只留一句回执给界面。
|
|
7973
|
+
const instruction = [
|
|
7974
|
+
state === undefined
|
|
7975
|
+
? `没找到项目「${project}」的 state.json。先调 project_doctor 看看 .project-flow 里剩下什么,别凭猜重建状态。`
|
|
7976
|
+
: `项目「${project}」现在:${statusLine(state)}`,
|
|
7977
|
+
'',
|
|
7978
|
+
state === undefined
|
|
7979
|
+
? `体检结果出来后,再决定要不要调 project_resume,project="${project}"。`
|
|
7980
|
+
: `接下来调 project_resume,project="${project}"。它会把遗留锁、损坏的 state 处理掉,从中断的那一段接着跑,已经通过的段不重做。`,
|
|
7981
|
+
].join('\n')
|
|
7982
|
+
if (await steerInstruction(invocation.agent, instruction)) {
|
|
7983
|
+
return { kind: 'success', text: `已让模型接续「${project}」。` }
|
|
7911
7984
|
}
|
|
7985
|
+
return { kind: 'success', text: instruction }
|
|
7912
7986
|
},
|
|
7913
7987
|
})
|
|
7914
7988
|
const offDoctorCmd = commands.register({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-project-flow",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.3",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Four-stage project workflow: requirement, plan, build, QA, with an intent gate, a rework loop, self-healing recovery, a watchdog that stops runaway runs, snapshot rollback with a retention cap, a single status light, and a machine-readable Completion_Evidence.json.",
|
|
6
6
|
"type": "module",
|
package/tests/run-all.mjs
CHANGED
|
@@ -25,6 +25,7 @@ const SUITES = [
|
|
|
25
25
|
['守护器心跳回归', '_heartbeat-regression.test.mjs'],
|
|
26
26
|
['token 计数单测', 'watchdog-tokens.test.mjs'],
|
|
27
27
|
['停止与继续', 'stop-resume.test.mjs'],
|
|
28
|
+
['命令把话送到模型', 'steer-command.test.mjs'],
|
|
28
29
|
['工作流目录门控', 'flow-gate.test.mjs'],
|
|
29
30
|
['状态栏看哪个目录', 'watch-root.test.mjs'],
|
|
30
31
|
['质检读预览图的路径', 'preview-path.test.mjs'],
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 命令到底有没有把话送到模型手里。
|
|
3
|
+
*
|
|
4
|
+
* 用户报的问题(issue #1):`/flow 测试` 界面上渲染出一整段指令,模型却什么都没收到。
|
|
5
|
+
* 根因是宿主契约——`CommandResult.text` 只进 `command/done`,由界面渲染,不提交成用户消息。
|
|
6
|
+
*
|
|
7
|
+
* 这一套不验函数长得对不对,只验三件事:
|
|
8
|
+
* 1. 有 steer 的宿主上,指令真的被塞进 agent 的收件箱,且形状过了会话校验;
|
|
9
|
+
* 2. 返回值只留一句回执,不再把整段指令当 text 返回(那正是误以为成功的来源);
|
|
10
|
+
* 3. 老宿主没有 steer 时退回纯文本,不崩、也不静默。
|
|
11
|
+
*
|
|
12
|
+
* 用真插件、真命令处理器,只有 agent 和宿主校验是假的。
|
|
13
|
+
*/
|
|
14
|
+
import { test } from 'node:test'
|
|
15
|
+
import assert from 'node:assert/strict'
|
|
16
|
+
import { mkdtemp } from 'node:fs/promises'
|
|
17
|
+
import { join } from 'node:path'
|
|
18
|
+
import { tmpdir } from 'node:os'
|
|
19
|
+
import { pathToFileURL } from 'node:url'
|
|
20
|
+
import { requireTarget } from './paths.mjs'
|
|
21
|
+
|
|
22
|
+
// 不读用户自己的运行时配置:他在界面上设了哪个模型是他的事。
|
|
23
|
+
process.env.DSH_HOME = await mkdtemp(join(tmpdir(), 'pf-steer-home-'))
|
|
24
|
+
|
|
25
|
+
const mod = await import(pathToFileURL(requireTarget()).href + `?t=${Date.now()}`)
|
|
26
|
+
|
|
27
|
+
/** 假 ctx:只接住命令注册,别的按需给空实现。 */
|
|
28
|
+
function fakeCtx() {
|
|
29
|
+
const commands = new Map()
|
|
30
|
+
const ctx = {
|
|
31
|
+
tools: { register() { return () => {} } },
|
|
32
|
+
systemPrompt: { section() { return () => {} }, getSectionOrder() { return 2800 } },
|
|
33
|
+
effect(fn) { fn() },
|
|
34
|
+
inject(names, fn) {
|
|
35
|
+
if (!names.includes('commands')) return
|
|
36
|
+
fn({
|
|
37
|
+
get: key => (key === 'commands'
|
|
38
|
+
? { register(definition) { commands.set(definition.name, definition); return () => commands.delete(definition.name) } }
|
|
39
|
+
: undefined),
|
|
40
|
+
effect: inner => inner(),
|
|
41
|
+
})
|
|
42
|
+
},
|
|
43
|
+
get() { return undefined },
|
|
44
|
+
on() { return () => {} },
|
|
45
|
+
logger: { info() {}, warn() {}, error() {} },
|
|
46
|
+
root: {},
|
|
47
|
+
}
|
|
48
|
+
mod.apply(ctx, {})
|
|
49
|
+
return commands
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const commands = fakeCtx()
|
|
53
|
+
const cwd = await mkdtemp(join(tmpdir(), 'pf-steer-'))
|
|
54
|
+
|
|
55
|
+
/** 接口和真 agent 一致的最小实现:只记下 steer 收到的消息。 */
|
|
56
|
+
function fakeAgent({ withSteer = true } = {}) {
|
|
57
|
+
const steered = []
|
|
58
|
+
const agent = { session: { header: { cwd } } }
|
|
59
|
+
if (withSteer) agent.steer = message => { steered.push(message) }
|
|
60
|
+
return { agent, steered }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 宿主会话对 user 消息的硬要求(dsh-session 的 assertMessageEventShape)。
|
|
65
|
+
* 形状不对的消息会在 append 时被拒,等于又变成「什么都没发生」。
|
|
66
|
+
*/
|
|
67
|
+
function assertShippableUserMessage(message) {
|
|
68
|
+
assert.equal(typeof message, 'object', 'steer 的参数应该是消息对象')
|
|
69
|
+
assert.equal(typeof message.id, 'string', '消息要有 id')
|
|
70
|
+
assert.ok(message.id.length > 0, 'id 不能是空串')
|
|
71
|
+
assert.equal(message.role, 'user', '这必须是 user 角色的消息')
|
|
72
|
+
assert.ok(Array.isArray(message.content), 'content 要是数组')
|
|
73
|
+
assert.ok(message.content.some(block => block.type === 'text' && typeof block.text === 'string' && block.text !== ''),
|
|
74
|
+
'content 里要有一段非空文本')
|
|
75
|
+
assert.equal(typeof message.source, 'object', 'source 要有')
|
|
76
|
+
assert.equal(typeof message.source.kind, 'string', 'source.kind 要是字符串')
|
|
77
|
+
assert.ok(message.source.kind.length > 0, 'source.kind 不能是空串')
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const textOf = message => message.content.filter(block => block.type === 'text').map(block => block.text).join('\n')
|
|
81
|
+
|
|
82
|
+
test('/flow 的指令进了 agent 的收件箱,形状过得了会话校验', async () => {
|
|
83
|
+
const { agent, steered } = fakeAgent()
|
|
84
|
+
const result = await commands.get('flow').handler({ rawInput: '给仓库加一个 CHANGELOG 段落', agent, signal: undefined })
|
|
85
|
+
assert.equal(steered.length, 1, '正好 steer 一条,不多不少')
|
|
86
|
+
assertShippableUserMessage(steered[0])
|
|
87
|
+
assert.match(textOf(steered[0]), /CHANGELOG/u, '用户输入的那句话要原样带进指令')
|
|
88
|
+
assert.match(textOf(steered[0]), /ask_user_question/u, '第 1 阶的提问要求要在指令里')
|
|
89
|
+
assert.match(textOf(steered[0]), /project_run/u, '要指明交给哪个工具')
|
|
90
|
+
assert.equal(result.kind, 'success')
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
test('/flow 的返回值只是一句回执,不再谎报整段指令', async () => {
|
|
94
|
+
const { agent } = fakeAgent()
|
|
95
|
+
const result = await commands.get('flow').handler({ rawInput: '随便一个活', agent, signal: undefined })
|
|
96
|
+
assert.ok(result.text.length < 40, `回执要短,实际是 ${result.text.length} 字`)
|
|
97
|
+
assert.ok(!result.text.includes('ask_user_question'), '门槛说明不该再出现在返回值里')
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
test('/flow 留空输入时先让模型问,而不是自己编一个任务', async () => {
|
|
101
|
+
const { agent, steered } = fakeAgent()
|
|
102
|
+
await commands.get('flow').handler({ rawInput: '', agent, signal: undefined })
|
|
103
|
+
assert.equal(steered.length, 1)
|
|
104
|
+
assert.match(textOf(steered[0]), /还没说是什么/u)
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
test('/flow 在没 steer 的老宿主上退回纯文本,不崩也不静默', async () => {
|
|
108
|
+
const { agent, steered } = fakeAgent({ withSteer: false })
|
|
109
|
+
const result = await commands.get('flow').handler({ rawInput: '老宿主', agent, signal: undefined })
|
|
110
|
+
assert.equal(steered.length, 0, '没有 steer 就什么都不该塞')
|
|
111
|
+
assert.equal(result.kind, 'success', '不能改成报错')
|
|
112
|
+
assert.match(result.text, /ask_user_question/u, '退回时要把指令原样显示出来,让用户至少能看见')
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
test('/flow-resume 带项目名时走 steer', async () => {
|
|
116
|
+
const { agent, steered } = fakeAgent()
|
|
117
|
+
const result = await commands.get('flow-resume').handler({ rawInput: '不存在的项目', agent, signal: undefined })
|
|
118
|
+
assert.equal(steered.length, 1)
|
|
119
|
+
assertShippableUserMessage(steered[0])
|
|
120
|
+
assert.match(textOf(steered[0]), /project_resume|project_doctor/u, '要指明接下来调哪个工具')
|
|
121
|
+
assert.equal(result.kind, 'success')
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
test('/flow-resume 不带项目名是纯展示:列清单,不动模型', async () => {
|
|
125
|
+
const { agent, steered } = fakeAgent()
|
|
126
|
+
const result = await commands.get('flow-resume').handler({ rawInput: '', agent, signal: undefined })
|
|
127
|
+
assert.equal(steered.length, 0, '没指定项目时不该替用户决定续哪个')
|
|
128
|
+
assert.equal(typeof result.text, 'string')
|
|
129
|
+
assert.ok(result.text.length > 0)
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
test('纯展示类命令一律不 steer', async () => {
|
|
133
|
+
const names = ['flow-status', 'flow-models', 'flow-cost', 'flow-prices', 'flow-snapshots', 'flow-patterns', 'flow-domain', 'flow-doctor', 'flow-set']
|
|
134
|
+
for (const name of names) {
|
|
135
|
+
const command = commands.get(name)
|
|
136
|
+
assert.ok(command !== undefined, `${name} 应该注册着,别在测试里凭空假设`)
|
|
137
|
+
const { agent, steered } = fakeAgent()
|
|
138
|
+
await command.handler({ rawInput: '', agent, signal: undefined })
|
|
139
|
+
assert.equal(steered.length, 0, `${name} 是纯展示,不该往对话里塞消息`)
|
|
140
|
+
}
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
test('steerInstruction 对坏输入说不,而不是抛出去', async () => {
|
|
144
|
+
assert.equal(await mod.steerInstruction(undefined, 'x'), false, '没有 agent 就交不出去')
|
|
145
|
+
assert.equal(await mod.steerInstruction({}, 'x'), false, 'agent 没有 steer 就交不出去')
|
|
146
|
+
assert.equal(await mod.steerInstruction({ steer() {} }, ' '), false, '空指令没有意义')
|
|
147
|
+
const seen = []
|
|
148
|
+
const ok = await mod.steerInstruction({ steer: message => seen.push(message) }, '一句话')
|
|
149
|
+
assert.equal(ok, true)
|
|
150
|
+
assertShippableUserMessage(seen[0])
|
|
151
|
+
})
|