dsh-project-flow 0.9.3 → 0.10.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 +197 -12
- package/README.md +25 -0
- package/checks/assertion.mjs +104 -0
- package/checks/command.mjs +416 -0
- package/checks/requirement.mjs +91 -0
- package/commands/slash.mjs +754 -0
- package/commands/steer.mjs +43 -0
- package/config/settings.mjs +217 -0
- package/docs/FEATURES.md +9 -6
- package/docs/anchors/file-whitelist.md +1 -1
- package/http/routes.mjs +252 -0
- package/index.js +173 -8612
- package/issues/issue.mjs +322 -0
- package/package.json +16 -1
- package/persistence/lock.mjs +67 -0
- package/persistence/log.mjs +43 -0
- package/persistence/recovery.mjs +118 -0
- package/persistence/state.mjs +202 -0
- package/pricing/estimate.mjs +72 -0
- package/prompts/rules.mjs +192 -0
- package/rollback/ledger.mjs +237 -0
- package/runner/pipeline.mjs +1190 -0
- package/runner/stage.mjs +370 -0
- package/runner/tools/context.mjs +64 -0
- package/runner/tools/decorate-cost.mjs +49 -0
- package/runner/tools/doctor.mjs +59 -0
- package/runner/tools/estimate.mjs +63 -0
- package/runner/tools/execute-run.mjs +255 -0
- package/runner/tools/models.mjs +55 -0
- package/runner/tools/resume.mjs +136 -0
- package/runner/tools/rollback.mjs +291 -0
- package/runner/tools/run.mjs +262 -0
- package/runner/tools/set.mjs +65 -0
- package/runner/tools/snapshots.mjs +83 -0
- package/runner/tools/status.mjs +70 -0
- package/runner/tools.mjs +94 -0
- package/shared/constants.mjs +273 -0
- package/shared/tool.mjs +32 -0
- package/shared/util.mjs +231 -0
- package/snapshot/repo.mjs +452 -0
- package/state/registry.mjs +40 -0
- package/tests/aesthetic.test.mjs +20 -12
- package/tests/apply-wiring.test.mjs +172 -0
- package/tests/assertions-e2e.mjs +2 -1
- package/tests/atomic-write.test.mjs +129 -0
- package/tests/client-bundle.test.mjs +3 -3
- package/tests/command-whitelist.test.mjs +135 -0
- package/tests/contract.test.mjs +2 -2
- package/tests/desktop.test.mjs +2 -2
- package/tests/domains.test.mjs +10 -7
- package/tests/e2e.mjs +2 -1
- package/tests/extra-phase.test.mjs +2 -2
- package/tests/feedback.test.mjs +2 -1
- package/tests/flow-gate.test.mjs +16 -56
- package/tests/issues-e2e.mjs +2 -1
- package/tests/panel-independence.test.mjs +2 -2
- package/tests/panel-report.test.mjs +2 -2
- package/tests/paths.mjs +70 -1
- package/tests/preview-path.test.mjs +2 -1
- package/tests/qa-assertions.test.mjs +10 -3
- package/tests/run-all.mjs +5 -0
- package/tests/snapshot-core.test.mjs +234 -0
- package/tests/stage-model.test.mjs +2 -2
- package/tests/stop-resume.test.mjs +7 -2
- package/tests/watch-root.test.mjs +2 -1
- package/ui/prompt.mjs +101 -0
- package/workspace/models.mjs +111 -0
- package/workspace/panel.mjs +359 -0
- package/workspace/paths.mjs +393 -0
- package/workspace/resume.mjs +107 -0
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* rollbackTool。正文原样搬自 runner/tools.mjs 724-1007。
|
|
3
|
+
*/
|
|
4
|
+
import { assertProjectName, cwdOf } from '../../workspace/paths.mjs'
|
|
5
|
+
import { defineTool } from '../../shared/tool.mjs'
|
|
6
|
+
import { existsSync } from 'node:fs'
|
|
7
|
+
import { gitAvailable, listSnapshots, resolveSnapshot, restoreAll, restorePrecise, snapshotDiff, takeSnapshot } from '../../snapshot/repo.mjs'
|
|
8
|
+
import { isAbsolute } from 'node:path'
|
|
9
|
+
import { ledgerPath, rollbackByLedger } from '../../rollback/ledger.mjs'
|
|
10
|
+
import { logLine } from '../../persistence/log.mjs'
|
|
11
|
+
import { messageOf, normalizePaths, nowIso } from '../../shared/util.mjs'
|
|
12
|
+
import { readState, writeState } from '../../persistence/state.mjs'
|
|
13
|
+
import { settingsOf, snapshotExcludesOf } from '../../config/settings.mjs'
|
|
14
|
+
|
|
15
|
+
export function create(ctx, rt) {
|
|
16
|
+
const rollbackTool = defineTool({
|
|
17
|
+
name: 'project_rollback',
|
|
18
|
+
description: '把工作区回滚到某个快照。mode=full 整棵恢复到目标快照(连新增文件一起清掉);mode=precise 只还原指定那次实施动过的文件,不碰你手改的其他东西;mode=ledger 按补丁台账还原最后一批写入,只动 agent 真的写过的路径。默认先干跑给 diff,dryRun=false 才真回滚;真回滚前会自动再打一张 pre-rollback 快照,回错了还能再回来。',
|
|
19
|
+
parameters: {
|
|
20
|
+
project: {
|
|
21
|
+
type: 'string',
|
|
22
|
+
required: true,
|
|
23
|
+
description: '项目名。',
|
|
24
|
+
},
|
|
25
|
+
to: {
|
|
26
|
+
type: 'string',
|
|
27
|
+
description: '回滚目标:不填=最近一次实施之前;写「2」=第 2 轮实施之前;也可以写快照 commit 或标签里的关键字。',
|
|
28
|
+
},
|
|
29
|
+
mode: {
|
|
30
|
+
type: 'string',
|
|
31
|
+
description: 'full=整棵恢复(默认);precise=只还原那一次实施动过的文件;ledger=按补丁台账还原最后一批写入。',
|
|
32
|
+
},
|
|
33
|
+
dryRun: {
|
|
34
|
+
type: 'boolean',
|
|
35
|
+
description: '默认 true,只报告将要发生什么;false 才真的改文件。',
|
|
36
|
+
},
|
|
37
|
+
cwd: {
|
|
38
|
+
type: 'string',
|
|
39
|
+
description: '.project-flow 所在的绝对目录;不填就用当前对话的工作目录。',
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
output: {
|
|
43
|
+
schema: {
|
|
44
|
+
type: 'object',
|
|
45
|
+
additionalProperties: false,
|
|
46
|
+
required: ['line', 'ok', 'dryRun', 'target', 'changes', 'details'],
|
|
47
|
+
properties: {
|
|
48
|
+
ok: { type: 'boolean' },
|
|
49
|
+
dryRun: { type: 'boolean' },
|
|
50
|
+
target: { type: 'string' },
|
|
51
|
+
line: { type: 'string' },
|
|
52
|
+
changes: { type: 'array', items: { type: 'string' } },
|
|
53
|
+
details: { type: 'string' },
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
render: (_args, value) => [{
|
|
57
|
+
type: 'text',
|
|
58
|
+
text: [
|
|
59
|
+
value.line,
|
|
60
|
+
...value.changes.map(line => ` ${line}`),
|
|
61
|
+
value.details,
|
|
62
|
+
].filter(part => part !== '').join('\n'),
|
|
63
|
+
}],
|
|
64
|
+
},
|
|
65
|
+
async execute(args, exec) {
|
|
66
|
+
const cwd = args.cwd !== undefined && isAbsolute(args.cwd) ? args.cwd : cwdOf(ctx, exec.agent)
|
|
67
|
+
const project = assertProjectName(args.project)
|
|
68
|
+
const dryRun = args.dryRun !== false
|
|
69
|
+
// ledger 模式不走 git 快照,走台账。
|
|
70
|
+
const isLedger = args.mode === 'ledger'
|
|
71
|
+
const mode = isLedger ? 'ledger' : (args.mode === 'precise' ? 'precise' : 'full')
|
|
72
|
+
const fail = (line, details = '') => ({
|
|
73
|
+
ok: false, dryRun, target: '', line, changes: [], details,
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
// ── ledger 模式:按补丁台账回滚 ──
|
|
77
|
+
if (isLedger) {
|
|
78
|
+
// state 必须在这里读:下面写 rollbacks 记录要用它。
|
|
79
|
+
// 以前它在 ledger 分支之后才声明,走这条路一律 ReferenceError,
|
|
80
|
+
// 而错误被外层 catch 吞成一句「台账回滚失败」——看着像台账坏了。
|
|
81
|
+
const state = await readState(cwd, project)
|
|
82
|
+
|
|
83
|
+
// 台账不存在时报错,不静默回滚全部
|
|
84
|
+
const ledgerFile = ledgerPath(cwd, project)
|
|
85
|
+
if (!existsSync(ledgerFile)) {
|
|
86
|
+
return fail(
|
|
87
|
+
`项目 ${project}:没有补丁台账(${ledgerFile} 不存在),mode=ledger 没法回滚。`,
|
|
88
|
+
'台账在实施段每次写文件时自动追加。如果这个项目是在台账功能加上之前跑的,就没有台账。',
|
|
89
|
+
)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// 干跑只算差异:不打快照、不写盘、不追加台账。
|
|
93
|
+
// 以前这里不读 dryRun,默认 true 一进来就直接真回滚了。
|
|
94
|
+
let ledgerResult
|
|
95
|
+
try {
|
|
96
|
+
ledgerResult = await rollbackByLedger(cwd, project, { dryRun })
|
|
97
|
+
} catch (error) {
|
|
98
|
+
return fail(`台账回滚失败:${messageOf(error)}`, '台账里有坏 JSON 行,一个文件都不会动。')
|
|
99
|
+
}
|
|
100
|
+
if (ledgerResult.ok !== true && ledgerResult.reason !== undefined) {
|
|
101
|
+
return fail(`台账回滚失败:${ledgerResult.reason}`)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const restored = ledgerResult.restored ?? []
|
|
105
|
+
const failed = ledgerResult.failed ?? []
|
|
106
|
+
|
|
107
|
+
if (dryRun) {
|
|
108
|
+
return {
|
|
109
|
+
ok: true,
|
|
110
|
+
dryRun: true,
|
|
111
|
+
target: `ledger:${ledgerResult.batchId}`,
|
|
112
|
+
line: `台账回滚 · 干跑(batchId=${ledgerResult.batchId},共 ${ledgerResult.count} 条):会还原 ${restored.length} 个,${failed.length} 个还原不了。没动磁盘。`,
|
|
113
|
+
changes: [...restored, ...failed],
|
|
114
|
+
details: (ledgerResult.plan ?? []).map(p => ` ${p.action.padEnd(9)} ${p.path} — ${p.note}`).join('\n'),
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// 真回滚之前才打这张保险快照:干跑不在快照仓库里留垃圾。
|
|
119
|
+
if (await gitAvailable() === true) {
|
|
120
|
+
const guard = await takeSnapshot(cwd, project, `pre-rollback-ledger ${nowIso().slice(0, 19)}`, {
|
|
121
|
+
excludes: snapshotExcludesOf(settingsOf()),
|
|
122
|
+
})
|
|
123
|
+
if (guard.ok === true) {
|
|
124
|
+
await logLine(cwd, project, `台账回滚前快照 → ${guard.commit.slice(0, 8)}`)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
await logLine(cwd, project, `台账回滚:还原 ${restored.length} 个文件${failed.length > 0 ? `,${failed.length} 个失败` : ''}(batchId=${ledgerResult.batchId})`)
|
|
129
|
+
await writeState(cwd, project, {
|
|
130
|
+
rollbacks: [
|
|
131
|
+
...(Array.isArray(state?.rollbacks) ? state.rollbacks : []),
|
|
132
|
+
{ to: 'ledger', mode, at: nowIso(), batchId: ledgerResult.batchId, remaining: failed.length },
|
|
133
|
+
],
|
|
134
|
+
})
|
|
135
|
+
return {
|
|
136
|
+
ok: failed.length === 0,
|
|
137
|
+
dryRun: false,
|
|
138
|
+
target: `ledger:${ledgerResult.batchId}`,
|
|
139
|
+
line: `台账回滚(batchId=${ledgerResult.batchId}):还原 ${restored.length} 个文件${failed.length > 0 ? `,${failed.length} 个失败` : ''}。`,
|
|
140
|
+
changes: [...restored, ...failed],
|
|
141
|
+
details: failed.length === 0
|
|
142
|
+
? '只动了台账里记录的文件,其他文件原样保留。'
|
|
143
|
+
: `失败:\n${failed.map(f => ` ${f}`).join('\n')}`,
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (await gitAvailable() !== true) {
|
|
148
|
+
return fail(`项目 ${project}:这台机器上没有可用的 git,回滚做不了。`)
|
|
149
|
+
}
|
|
150
|
+
const settings = settingsOf()
|
|
151
|
+
const state = await readState(cwd, project)
|
|
152
|
+
const attempts = Array.isArray(state?.attempts) ? state.attempts : []
|
|
153
|
+
if (attempts.length === 0 && (args.to ?? '') === '') {
|
|
154
|
+
return fail(
|
|
155
|
+
`项目 ${project}:没有实施记录,不知道要回滚到哪。`,
|
|
156
|
+
'用 project_snapshots 看有哪些快照,再用 to 指定 commit。',
|
|
157
|
+
)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Resolve the target: a round number, a commit, or the last attempt.
|
|
161
|
+
const requested = (args.to ?? '').trim()
|
|
162
|
+
let target
|
|
163
|
+
let chosenAttempt
|
|
164
|
+
if (requested === '' || requested === 'last' || requested === '最近') {
|
|
165
|
+
chosenAttempt = attempts[attempts.length - 1]
|
|
166
|
+
target = chosenAttempt?.before
|
|
167
|
+
} else if (/^\d+$/u.test(requested)) {
|
|
168
|
+
chosenAttempt = attempts.find(attempt => attempt.round === Number(requested))
|
|
169
|
+
target = chosenAttempt?.before
|
|
170
|
+
if (target === undefined) {
|
|
171
|
+
return fail(`项目 ${project}:没有第 ${requested} 轮实施的记录。`, `现有轮次:${attempts.map(a => a.round).join('、')}`)
|
|
172
|
+
}
|
|
173
|
+
} else {
|
|
174
|
+
target = await resolveSnapshot(cwd, project, requested)
|
|
175
|
+
if (target === undefined) {
|
|
176
|
+
const snaps = await listSnapshots(cwd, project, 20)
|
|
177
|
+
return fail(
|
|
178
|
+
`项目 ${project}:找不到快照「${requested}」。`,
|
|
179
|
+
snaps.length === 0 ? '这个项目还没有快照。' : `可用快照:\n${snaps.map(s => ` ${s.commit.slice(0, 8)} ${s.label}`).join('\n')}`,
|
|
180
|
+
)
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (typeof target !== 'string' || target === '') {
|
|
185
|
+
return fail(
|
|
186
|
+
`项目 ${project}:这一轮实施没有记录到快照(可能当时 git 不可用)。`,
|
|
187
|
+
'用 project_snapshots 挑一个 commit 作为 to 再试。',
|
|
188
|
+
)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const short = target.slice(0, 8)
|
|
192
|
+
|
|
193
|
+
// Precise mode only touches the files that attempt reported or was seen
|
|
194
|
+
// changing; full mode restores everything the snapshot knows about.
|
|
195
|
+
const precisePaths = mode === 'precise'
|
|
196
|
+
? normalizePaths(chosenAttempt?.files)
|
|
197
|
+
: []
|
|
198
|
+
|
|
199
|
+
if (mode === 'precise' && precisePaths.length === 0) {
|
|
200
|
+
return fail(
|
|
201
|
+
`项目 ${project}:这一轮实施没有记录到改动文件清单,没法精准回滚。`,
|
|
202
|
+
'改用 mode="full" 整棵恢复,或者用 project_snapshots 拿 commit 自己判断。',
|
|
203
|
+
)
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const changes = mode === 'precise'
|
|
207
|
+
? await snapshotDiff(cwd, project, target).then(list => list.filter(entry => precisePaths.includes(entry.path)))
|
|
208
|
+
: await snapshotDiff(cwd, project, target)
|
|
209
|
+
|
|
210
|
+
const describe = changes.map(entry => `${entry.status} ${entry.path}`)
|
|
211
|
+
const targetLine = `目标快照 ${short}${chosenAttempt === undefined ? '' : `(第 ${chosenAttempt.round} 轮实施之前)`},模式 ${mode === 'full' ? '整棵恢复' : '只还原那一次动过的文件'}`
|
|
212
|
+
|
|
213
|
+
if (changes.length === 0) {
|
|
214
|
+
return {
|
|
215
|
+
ok: true,
|
|
216
|
+
dryRun,
|
|
217
|
+
target,
|
|
218
|
+
line: `${targetLine}:工作区已经和这个快照一致,不用回滚。`,
|
|
219
|
+
changes: [],
|
|
220
|
+
details: '',
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (dryRun) {
|
|
225
|
+
return {
|
|
226
|
+
ok: true,
|
|
227
|
+
dryRun: true,
|
|
228
|
+
target,
|
|
229
|
+
line: `${targetLine}:将要改动 ${changes.length} 个文件(干跑,未动任何文件)。`,
|
|
230
|
+
changes: describe,
|
|
231
|
+
details: mode === 'precise'
|
|
232
|
+
? `只动这些文件;其他内容保持原样。确认无误后用 dryRun=false 执行。`
|
|
233
|
+
: `整棵恢复会把这些文件一起还原,快照里没有的文件会被删掉。确认无误后用 dryRun=false 执行。`,
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Always keep an escape hatch: snapshot the current (bad) state first.
|
|
238
|
+
const guard = await takeSnapshot(cwd, project, `pre-rollback ${short}`, {
|
|
239
|
+
excludes: snapshotExcludesOf(settings),
|
|
240
|
+
})
|
|
241
|
+
const guardNote = guard.ok === true
|
|
242
|
+
? `回滚前的现场已存为快照 ${guard.commit.slice(0, 8)},回错了还能再回去。`
|
|
243
|
+
: `回滚前的现场没能存下来(${guard.reason}),这一步不可逆。`
|
|
244
|
+
|
|
245
|
+
// Precise mode: only the files this attempt touched. Files it created do
|
|
246
|
+
// not exist in the snapshot, so they are deleted rather than checked out.
|
|
247
|
+
// 这段逻辑现在住在 repo.mjs(restorePrecise),和 full 模式共用同一套
|
|
248
|
+
// 「哪些算新增」的判定,不靠这里再算一遍。
|
|
249
|
+
let result
|
|
250
|
+
if (mode === 'precise') {
|
|
251
|
+
result = await restorePrecise(cwd, project, target, precisePaths, changes)
|
|
252
|
+
} else {
|
|
253
|
+
result = await restoreAll(cwd, project, target)
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (result.ok !== true) {
|
|
257
|
+
return fail(`回滚失败:${result.reason ?? '未知原因'}`, guardNote)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const left = await snapshotDiff(cwd, project, target)
|
|
261
|
+
const leftPrecise = mode === 'precise'
|
|
262
|
+
? left.filter(entry => precisePaths.includes(entry.path))
|
|
263
|
+
: left
|
|
264
|
+
await logLine(cwd, project, `回滚到 ${short}(${mode}),剩 ${leftPrecise.length} 处差异`)
|
|
265
|
+
await writeState(cwd, project, {
|
|
266
|
+
rollbacks: [
|
|
267
|
+
...(Array.isArray(state?.rollbacks) ? state.rollbacks : []),
|
|
268
|
+
{ to: target, mode, at: nowIso(), remaining: leftPrecise.length },
|
|
269
|
+
],
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
return {
|
|
273
|
+
ok: true,
|
|
274
|
+
dryRun: false,
|
|
275
|
+
target,
|
|
276
|
+
line: leftPrecise.length === 0
|
|
277
|
+
? `${targetLine}:已回滚,工作区和目标快照一致。`
|
|
278
|
+
: `${targetLine}:已回滚,但还有 ${leftPrecise.length} 处差异(多半是你自己的手改,或排除规则挡住了)。`,
|
|
279
|
+
changes: describe,
|
|
280
|
+
details: [
|
|
281
|
+
result.removed === undefined ? '' : `删掉了 ${result.removed} 个快照里不存在的文件。`,
|
|
282
|
+
leftPrecise.length === 0 ? '' : `剩下的差异:\n${leftPrecise.map(e => ` ${e.status} ${e.path}`).join('\n')}`,
|
|
283
|
+
guardNote,
|
|
284
|
+
].filter(part => part !== '').join('\n'),
|
|
285
|
+
}
|
|
286
|
+
},
|
|
287
|
+
})
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
return rollbackTool
|
|
291
|
+
}
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* runTool。正文原样搬自 runner/tools.mjs 32-268。
|
|
3
|
+
*/
|
|
4
|
+
import { createExecuteRun } from './execute-run.mjs'
|
|
5
|
+
import { FLOW_DIR_NAME, INTENT_CONFIDENCE_FLOOR } from '../../shared/constants.mjs'
|
|
6
|
+
import { assertIntentConverged, cwdOf, ensureFlowRoot, flowGateMessage, flowModeOf, flowRootConfigured, insideFlow, rememberReadiness, rememberRoot } from '../../workspace/paths.mjs'
|
|
7
|
+
import { buildResumePlan } from '../../workspace/resume.mjs'
|
|
8
|
+
import { defineTool } from '../../shared/tool.mjs'
|
|
9
|
+
import { isAbsolute } from 'node:path'
|
|
10
|
+
import { logLine } from '../../persistence/log.mjs'
|
|
11
|
+
import { pfState } from '../../state/registry.mjs'
|
|
12
|
+
import { readState, writeRememberedContext } from '../../persistence/state.mjs'
|
|
13
|
+
import { settingsOf } from '../../config/settings.mjs'
|
|
14
|
+
import { stateDir } from '../../shared/util.mjs'
|
|
15
|
+
import { statusLine } from '../../workspace/panel.mjs'
|
|
16
|
+
|
|
17
|
+
export function create(ctx, rt) {
|
|
18
|
+
const executeRun = createExecuteRun(ctx, rt)
|
|
19
|
+
// 估价与花费那段是所有工具共用的收尾装饰,单独一个文件,
|
|
20
|
+
// 由聚合层建好之后从 rt 传进来。
|
|
21
|
+
const { decorateCost } = rt
|
|
22
|
+
|
|
23
|
+
const runTool = defineTool({
|
|
24
|
+
name: 'project_run',
|
|
25
|
+
description: '跑一个项目的规划、实施、质检三段流水,返回一份压缩结果。三段各开独立子会话,彼此只传任务书不传过程,所以对话里只留这一个结果。要在和用户确认完意图、列好要求清单之后再调用。',
|
|
26
|
+
parameters: {
|
|
27
|
+
project: {
|
|
28
|
+
type: 'string',
|
|
29
|
+
required: true,
|
|
30
|
+
description: '项目简称,小写加连字符;会作为 .project-flow/ 下的产物目录名。',
|
|
31
|
+
},
|
|
32
|
+
intent: {
|
|
33
|
+
type: 'string',
|
|
34
|
+
required: true,
|
|
35
|
+
description: '确认过的意图,一段话,用用户自己的说法。',
|
|
36
|
+
},
|
|
37
|
+
requirements: {
|
|
38
|
+
type: 'array',
|
|
39
|
+
required: true,
|
|
40
|
+
items: { type: 'string' },
|
|
41
|
+
description: '用户的要求,一条一行,要能逐条核对。质检环节会照着这些逐项验。要带断言就在这一条里加一行 `check: <命令>`,或者改用 checks 参数。',
|
|
42
|
+
},
|
|
43
|
+
checks: {
|
|
44
|
+
type: 'array',
|
|
45
|
+
items: {
|
|
46
|
+
type: 'object',
|
|
47
|
+
additionalProperties: false,
|
|
48
|
+
required: ['req', 'check'],
|
|
49
|
+
properties: {
|
|
50
|
+
req: { type: 'integer', description: 'requirements 里第几条,1 起。' },
|
|
51
|
+
check: { type: 'string', description: '一条命令,退出码等于 checkExit(默认 0)才算过。' },
|
|
52
|
+
checkExit: { type: 'integer', description: '期望退出码,默认 0。' },
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
description: '每条要求的可执行判据:写进 Requirement.md 的前置块,第 4 阶由插件自己跑,不看模型怎么说。',
|
|
56
|
+
},
|
|
57
|
+
constraints: {
|
|
58
|
+
type: 'string',
|
|
59
|
+
description: '边界:不能碰的文件、不要用的技术、预算或风格限制。',
|
|
60
|
+
},
|
|
61
|
+
forbidden: {
|
|
62
|
+
type: 'array',
|
|
63
|
+
items: { type: 'string' },
|
|
64
|
+
description: '不许碰的路径(目录或文件,相对绝对都行)。实施段跑完会拿它比对台账,碰到就判 FAIL 并要求回滚。',
|
|
65
|
+
},
|
|
66
|
+
doneWhen: {
|
|
67
|
+
type: 'array',
|
|
68
|
+
items: { type: 'string' },
|
|
69
|
+
description: '算做完的判据,一句话一条。和 requirements 一起写进 contract.json,每一阶都会重贴。',
|
|
70
|
+
},
|
|
71
|
+
intentConfidence: {
|
|
72
|
+
type: 'integer',
|
|
73
|
+
required: true,
|
|
74
|
+
description: `你现在对用户真实意图的把握,0-100。低于 ${INTENT_CONFIDENCE_FLOOR} 会直接拒绝执行——继续问,别硬跑。`,
|
|
75
|
+
},
|
|
76
|
+
questionsAsked: {
|
|
77
|
+
type: 'integer',
|
|
78
|
+
required: true,
|
|
79
|
+
description: '第 1 段你实际问过用户几个问题(一次一个)。0 会被拒:没问过就宣称有把握,通常是拿猜测当事实。',
|
|
80
|
+
},
|
|
81
|
+
blindspots: {
|
|
82
|
+
type: 'object',
|
|
83
|
+
required: true,
|
|
84
|
+
additionalProperties: false,
|
|
85
|
+
description: '《需求完备度自查表》的盲区清单。五个键都要给结论,留空一个就拒绝执行,override 也放不过去。',
|
|
86
|
+
properties: {
|
|
87
|
+
permissions: { type: 'string', description: '权限:谁来做、要不要授权、账号凭证归谁、有没有动不到的东西。' },
|
|
88
|
+
concurrency: { type: 'string', description: '并发:同时几个人用、会不会撞车、要不要排队加锁、量级多大。' },
|
|
89
|
+
edge_cases: { type: 'string', description: '异常边界:出错怎么办、空数据、超时、重试、失败能不能回滚。' },
|
|
90
|
+
stack: { type: 'string', description: '技术栈:用什么语言框架、跑在哪、能不能加依赖、跟现有代码怎么接。' },
|
|
91
|
+
data_flow: { type: 'string', description: '数据流向:数据从哪来到哪去、存哪儿、谁能读、要不要脱敏或留存。' },
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
assumptions: {
|
|
95
|
+
type: 'array',
|
|
96
|
+
items: { type: 'string' },
|
|
97
|
+
description: '你没问、但按默认值定了的地方,一条一句。这些会写进 Requirement.md,质检时逐条核。',
|
|
98
|
+
},
|
|
99
|
+
override: {
|
|
100
|
+
type: 'boolean',
|
|
101
|
+
description: `用户明确说「就按这个做、别再问了」时传 true,跳过 ${INTENT_CONFIDENCE_FLOOR} 分门槛。必须同时给 overrideReason。`,
|
|
102
|
+
},
|
|
103
|
+
overrideReason: {
|
|
104
|
+
type: 'string',
|
|
105
|
+
description: 'override 放行的依据:用户原话。没写会被拒。',
|
|
106
|
+
},
|
|
107
|
+
maxRework: {
|
|
108
|
+
type: 'integer',
|
|
109
|
+
description: '最多返工几次,超过就停机问你。不填用配置里的值。',
|
|
110
|
+
},
|
|
111
|
+
restart: {
|
|
112
|
+
type: 'boolean',
|
|
113
|
+
description: '默认 false:同一个项目再跑一次会自动接上未完成的那一段,已经跑过的不重做。true 才是真的推倒重来,Requirement.md 会按这次的意图重写。',
|
|
114
|
+
},
|
|
115
|
+
cwd: {
|
|
116
|
+
type: 'string',
|
|
117
|
+
description: '产物放哪个绝对目录;不填就用当前对话的工作目录。',
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
output: {
|
|
121
|
+
schema: {
|
|
122
|
+
type: 'object',
|
|
123
|
+
additionalProperties: false,
|
|
124
|
+
required: ['project', 'outcome', 'stage', 'rework', 'plan', 'buildSummary', 'verdict', 'issues', 'artifacts', 'note'],
|
|
125
|
+
properties: {
|
|
126
|
+
project: { type: 'string' },
|
|
127
|
+
outcome: { type: 'string' },
|
|
128
|
+
stage: { type: 'integer' },
|
|
129
|
+
rework: { type: 'integer' },
|
|
130
|
+
plan: { type: 'string' },
|
|
131
|
+
buildSummary: { type: 'string' },
|
|
132
|
+
verdict: { type: 'string' },
|
|
133
|
+
issues: { type: 'array', items: { type: 'string' } },
|
|
134
|
+
artifacts: { type: 'string' },
|
|
135
|
+
note: { type: 'string' },
|
|
136
|
+
evidence: { type: 'string' },
|
|
137
|
+
evidenceDigest: { type: 'string' },
|
|
138
|
+
// 钱:开工前是预估,跑完是实际。两个都要给——用户明确要求过
|
|
139
|
+
// 「执行前估算、完成后按各模型费率统计成人民币」。
|
|
140
|
+
estimate: { type: 'string' },
|
|
141
|
+
cost: { type: 'string' },
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
render: (_args, value) => [{
|
|
145
|
+
type: 'text',
|
|
146
|
+
text: [
|
|
147
|
+
statusLine({ project: value.project, stage: value.stage, status: value.outcome, rework: value.rework }),
|
|
148
|
+
value.verdict,
|
|
149
|
+
value.note,
|
|
150
|
+
value.estimate === undefined || value.estimate === '' ? '' : value.estimate,
|
|
151
|
+
value.cost === undefined || value.cost === '' ? '' : value.cost,
|
|
152
|
+
].filter(part => part !== '').join('\n'),
|
|
153
|
+
}],
|
|
154
|
+
},
|
|
155
|
+
async execute(args, exec) {
|
|
156
|
+
const parent = exec.agent
|
|
157
|
+
if (parent === undefined) throw new Error('project_run requires a calling agent')
|
|
158
|
+
// Stage 1 gate: refuse to spend three child agents on a guessed intent.
|
|
159
|
+
const confirmation = assertIntentConverged({
|
|
160
|
+
confidence: args.intentConfidence,
|
|
161
|
+
questionsAsked: args.questionsAsked,
|
|
162
|
+
blindspots: args.blindspots,
|
|
163
|
+
assumptions: args.assumptions,
|
|
164
|
+
overridden: args.override,
|
|
165
|
+
overrideReason: args.overrideReason,
|
|
166
|
+
})
|
|
167
|
+
// 存一份给前置门控用:这次过了,下一个会推进的工具就不该再被同一道门拦。
|
|
168
|
+
rememberReadiness(parent, {
|
|
169
|
+
score: confirmation.confidence,
|
|
170
|
+
blindspots: confirmation.blindspots,
|
|
171
|
+
assumptions: confirmation.assumptions,
|
|
172
|
+
})
|
|
173
|
+
const settings = settingsOf()
|
|
174
|
+
if (pfState.running.has(args.project)) throw new Error(`项目 ${args.project} 正在运行,等这一轮结束`)
|
|
175
|
+
if (pfState.running.size >= settings.maxConcurrentProjects) {
|
|
176
|
+
throw new Error(`同时只能跑 ${settings.maxConcurrentProjects} 个项目,先等一个跑完`)
|
|
177
|
+
}
|
|
178
|
+
let cwd = args.cwd !== undefined && isAbsolute(args.cwd) ? args.cwd : cwdOf(ctx, parent)
|
|
179
|
+
// 这道门是给人的,不是给模型的。但「建目录」这一步不该也压给人:
|
|
180
|
+
// 不在工作流目录里就先替他建出来,然后把这次的工作目录指过去。
|
|
181
|
+
// 边界没松——活还是在「工作流」里干——只是不用他自己去 mkdir 再切工作区。
|
|
182
|
+
//
|
|
183
|
+
// 但**配了绝对 flowRoot 就不许挪**:那代表用户明确表态「只在那儿用」。
|
|
184
|
+
// 擅自把活挪过去,会在用户工程目录旁边另起一个 .project-flow,
|
|
185
|
+
// 项目就散了——用户的原话是「默认工作区里不可以跑工作流」。
|
|
186
|
+
let createError
|
|
187
|
+
let createAttempt
|
|
188
|
+
if (!flowRootConfigured(settings)
|
|
189
|
+
&& settings.autoCreateFlowDir === true
|
|
190
|
+
&& flowModeOf(settings) === 'strict'
|
|
191
|
+
&& !insideFlow(cwd, settings)) {
|
|
192
|
+
const ensured = await ensureFlowRoot(cwd, settings)
|
|
193
|
+
if (ensured.created === true) cwd = ensured.root
|
|
194
|
+
// 建不出来要留话:后面那句拒绝只说「不在工作流目录里」,看不出是权限问题。
|
|
195
|
+
createError = ensured.error
|
|
196
|
+
createAttempt = ensured.attempted
|
|
197
|
+
}
|
|
198
|
+
// 在这儿开过工就记下来:下次在别的目录打开会话,状态栏照样知道该看哪儿。
|
|
199
|
+
// 不记的话,人一换目录,那边就只剩一句「未启用」——正是用户报的那个问题。
|
|
200
|
+
if (insideFlow(cwd, settings)) void rememberRoot(cwd)
|
|
201
|
+
if (!insideFlow(cwd, settings)) {
|
|
202
|
+
return {
|
|
203
|
+
project: args.project,
|
|
204
|
+
outcome: 'blocked',
|
|
205
|
+
stage: 0,
|
|
206
|
+
rework: 0,
|
|
207
|
+
plan: '',
|
|
208
|
+
buildSummary: '',
|
|
209
|
+
verdict: `不在「${FLOW_DIR_NAME}」目录里`,
|
|
210
|
+
issues: [],
|
|
211
|
+
artifacts: stateDir(cwd, args.project),
|
|
212
|
+
note: flowGateMessage(cwd, settings) + (createError === undefined ? '' : `\n\n工作流目录本来该自动建出来的,但建失败了:${createAttempt}\n${createError}\n换个能写的盘,或者把 flowRoot 配成你确定有权限的绝对路径。`),
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
// 门控过了就记下来:下次在同一个目录开工,五格能直接沿用,不用再问用户一遍。
|
|
216
|
+
void writeRememberedContext(cwd, {
|
|
217
|
+
project: args.project,
|
|
218
|
+
intent: args.intent,
|
|
219
|
+
blindspots: confirmation.blindspots,
|
|
220
|
+
assumptions: confirmation.assumptions,
|
|
221
|
+
})
|
|
222
|
+
// 幂等入口:同一个项目再调一次 project_run,不该把已经跑过的段推倒重来。
|
|
223
|
+
// 有未完成的 state 就自动接上去,要真正重开才传 restart: true。
|
|
224
|
+
if (args.restart !== true) {
|
|
225
|
+
const existing = await readState(cwd, args.project)
|
|
226
|
+
const unfinished = existing !== undefined
|
|
227
|
+
&& !(typeof existing.stage === 'number' && existing.stage >= 4 && existing.status === 'passed')
|
|
228
|
+
if (unfinished) {
|
|
229
|
+
const plan = await buildResumePlan(cwd, args.project, settings)
|
|
230
|
+
if (plan.kind === 'plan') {
|
|
231
|
+
await logLine(cwd, args.project, `project_run 命中未完成的第 ${plan.stage} 段,自动改走续跑`)
|
|
232
|
+
return executeRun({
|
|
233
|
+
...plan.options,
|
|
234
|
+
parent,
|
|
235
|
+
signal: exec.signal,
|
|
236
|
+
mode: `${plan.options.mode}(project_run 自动续跑)`,
|
|
237
|
+
}).then(result => decorateCost(result, settings, args.requirements, cwd))
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return executeRun({
|
|
242
|
+
cwd,
|
|
243
|
+
project: args.project,
|
|
244
|
+
intent: args.intent,
|
|
245
|
+
requirements: args.requirements,
|
|
246
|
+
checks: args.checks,
|
|
247
|
+
constraints: args.constraints ?? '',
|
|
248
|
+
forbidden: args.forbidden,
|
|
249
|
+
doneWhen: args.doneWhen,
|
|
250
|
+
confirmation,
|
|
251
|
+
maxRework: args.maxRework ?? settings.maxRework,
|
|
252
|
+
parent,
|
|
253
|
+
signal: exec.signal,
|
|
254
|
+
seed: undefined,
|
|
255
|
+
mode: 'run',
|
|
256
|
+
}).then(result => decorateCost(result, settings, args.requirements, cwd))
|
|
257
|
+
},
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
return runTool
|
|
262
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* setTool。正文原样搬自 runner/tools.mjs 1246-1298。
|
|
3
|
+
*/
|
|
4
|
+
import { defineTool } from '../../shared/tool.mjs'
|
|
5
|
+
import { overrideFile, settingsOf, writeOverride } from '../../config/settings.mjs'
|
|
6
|
+
|
|
7
|
+
export function create(ctx, rt) {
|
|
8
|
+
const { unknownModelMessage } = rt
|
|
9
|
+
|
|
10
|
+
const setTool = defineTool({
|
|
11
|
+
name: 'project_set_model',
|
|
12
|
+
description: '改某一阶段用的模型,改完立即生效不用重启。stage 取 规划/实施/质检 或 plan/build/check;model 留空表示恢复成继承当前模型。',
|
|
13
|
+
parameters: {
|
|
14
|
+
stage: {
|
|
15
|
+
type: 'string',
|
|
16
|
+
required: true,
|
|
17
|
+
description: '要改的阶段:规划(第2阶)、实施(第3阶)、质检(第4阶)。',
|
|
18
|
+
},
|
|
19
|
+
model: {
|
|
20
|
+
type: 'string',
|
|
21
|
+
description: '模型 id,例如 hy4flash、deepseek-v4.1-flash、glm-5.3-flash。留空表示清空,恢复继承当前模型。',
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
output: {
|
|
25
|
+
schema: {
|
|
26
|
+
type: 'object',
|
|
27
|
+
additionalProperties: false,
|
|
28
|
+
required: ['line', 'ok'],
|
|
29
|
+
properties: {
|
|
30
|
+
ok: { type: 'boolean' },
|
|
31
|
+
line: { type: 'string' },
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
render: (_args, value) => [{ type: 'text', text: value.line }],
|
|
35
|
+
},
|
|
36
|
+
async execute(args) {
|
|
37
|
+
const map = {
|
|
38
|
+
规划: 'planModel', plan: 'planModel',
|
|
39
|
+
实施: 'buildModel', build: 'buildModel',
|
|
40
|
+
质检: 'checkModel', check: 'checkModel',
|
|
41
|
+
}
|
|
42
|
+
const key = map[args.stage]
|
|
43
|
+
if (key === undefined) {
|
|
44
|
+
return { ok: false, line: `不认识的阶段「${args.stage}」。用 规划 / 实施 / 质检。` }
|
|
45
|
+
}
|
|
46
|
+
const value = (args.model ?? '').trim()
|
|
47
|
+
// 写错名字当场拒,别等跑起来才在回退日志里看见。
|
|
48
|
+
const unknown = await unknownModelMessage(value)
|
|
49
|
+
if (unknown !== undefined) return { ok: false, line: unknown }
|
|
50
|
+
if (value === '' || value === '清空' || value === 'reset') {
|
|
51
|
+
await writeOverride({ [key]: '' })
|
|
52
|
+
return { ok: true, line: `已把「${args.stage}」恢复成继承当前模型,立即生效。` }
|
|
53
|
+
}
|
|
54
|
+
await writeOverride({ [key]: value })
|
|
55
|
+
const settings = settingsOf()
|
|
56
|
+
const now = key === 'planModel' ? settings.planModel : key === 'buildModel' ? settings.buildModel : settings.checkModel
|
|
57
|
+
return {
|
|
58
|
+
ok: true,
|
|
59
|
+
line: `已把「${args.stage}」设成 ${now},立即生效,不用重启。配置文件:${overrideFile()}`,
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
return setTool
|
|
65
|
+
}
|