dsh-project-flow 0.9.2 → 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 +0 -0
- 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/DETAIL.md +2 -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 -8538
- 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 +6 -0
- package/tests/snapshot-core.test.mjs +234 -0
- package/tests/stage-model.test.mjs +2 -2
- package/tests/steer-command.test.mjs +151 -0
- 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,393 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 路径与门控:工作流根在哪、要不要拦、会话 cwd 怎么解析。
|
|
3
|
+
*/
|
|
4
|
+
import { isAbsolute, resolve, join, sep, relative, basename, dirname } from 'node:path'
|
|
5
|
+
import { blindspotGaps, dimensionLabel } from '../gate.mjs'
|
|
6
|
+
import { existsSync } from 'node:fs'
|
|
7
|
+
import { mkdir, readFile } from 'node:fs/promises'
|
|
8
|
+
import { tmpdir, homedir } from 'node:os'
|
|
9
|
+
import { FLOW_DIR_NAME, FLOW_GATE_MODES, INTENT_CONFIDENCE_FLOOR, LAST_ROOT_FILE } from '../shared/constants.mjs'
|
|
10
|
+
import { dirExists, messageOf, writeFileAtomic } from '../shared/util.mjs'
|
|
11
|
+
import { pfState } from '../state/registry.mjs'
|
|
12
|
+
|
|
13
|
+
export function cwdOf(ctx, agent) {
|
|
14
|
+
const candidates = []
|
|
15
|
+
if (agent !== undefined) {
|
|
16
|
+
// 会话自己记的 cwd 排第一:那才是用户在左边工作区里选的那个文件夹。
|
|
17
|
+
//
|
|
18
|
+
// workingDirectory 服务排在后面当兜底——它有时候给的是「进程启动目录」,
|
|
19
|
+
// 不是会话目录。照它算,用户在左边切到 D:\我的项目,插件拿到的还是
|
|
20
|
+
// C:\...\default-workspace,于是工作流建在默认工作区、状态栏也显示那儿。
|
|
21
|
+
// 用户报的「只有默认工作区」就是这个。
|
|
22
|
+
const recorded = agent.session?.header?.cwd
|
|
23
|
+
if (typeof recorded === 'string' && isAbsolute(recorded)) candidates.push(recorded)
|
|
24
|
+
const workingDirectory = ctx.get('workingDirectory')
|
|
25
|
+
if (workingDirectory !== undefined) {
|
|
26
|
+
try {
|
|
27
|
+
candidates.push(workingDirectory.get(agent.session))
|
|
28
|
+
} catch {
|
|
29
|
+
// 拿不到就靠上面那个来源
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const valid = candidates.filter(item => typeof item === 'string' && item !== '')
|
|
34
|
+
// 两个来源打架时,优先选那个真在「工作流」里的——那是明确的意图信号。
|
|
35
|
+
const inFlow = valid.find(item => item.split(/[\\/]+/u).includes(FLOW_DIR_NAME))
|
|
36
|
+
return inFlow ?? valid[0] ?? process.cwd()
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function assertNoEmptyBlindspots(blindspots) {
|
|
40
|
+
const gap = blindspotGaps(blindspots)
|
|
41
|
+
if (gap.gaps === 0) return
|
|
42
|
+
const parts = []
|
|
43
|
+
if (gap.empty.length > 0) parts.push(`还没结论:${gap.empty.map(dimensionLabel).join('、')}`)
|
|
44
|
+
if (gap.vague.length > 0) {
|
|
45
|
+
parts.push(`写得太笼统,要说具体:${gap.vague.map(key => `${dimensionLabel(key)}(「${gap.lines[key]}」)`).join('、')}`)
|
|
46
|
+
}
|
|
47
|
+
throw new Error(
|
|
48
|
+
[
|
|
49
|
+
`盲区清单没过门控——${parts.join(';')}`,
|
|
50
|
+
'',
|
|
51
|
+
'下一步只有两条路:',
|
|
52
|
+
'· 用 ask_user_question 一个一个问掉,问一个填一条,每条写成一句真结论;',
|
|
53
|
+
'· 确实问不出来、且做错也能承受的,写进 assumptions 并写清验收时怎么核。',
|
|
54
|
+
'override 只管「把握够不够」,管不了「盲区有没有问」——这两件事分开判。',
|
|
55
|
+
].join('\n'),
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function assertIntentConverged(confirmation) {
|
|
60
|
+
if (confirmation === undefined || confirmation === null) {
|
|
61
|
+
throw new Error(
|
|
62
|
+
'还没问过用户就想开跑。先问清意图:用 ask_user_question 一次问一个问题,'
|
|
63
|
+
+ `问到有把握为止(至少问到 ${INTENT_CONFIDENCE_FLOOR} 分信心),`
|
|
64
|
+
+ '再把 intentConfidence、questionsAsked、assumptions 传给 project_run。',
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// 一票否决:五个盲区必须先全有结论,override 也放不过去。
|
|
69
|
+
assertNoEmptyBlindspots(confirmation.blindspots)
|
|
70
|
+
// 存一份,两个 return 分支都要带出去(写进 Requirement.md 供质检核)。
|
|
71
|
+
const blindspots = confirmation.blindspots
|
|
72
|
+
|
|
73
|
+
const confidence = confirmation.confidence
|
|
74
|
+
if (confidence === undefined || confidence === null) {
|
|
75
|
+
throw new Error(
|
|
76
|
+
'还没有 intentConfidence:说明第 1 段还没走。先问清意图——用 ask_user_question '
|
|
77
|
+
+ `一次只问一个问题,问到有把握为止(至少 ${INTENT_CONFIDENCE_FLOOR} 分),`
|
|
78
|
+
+ '再带上 intentConfidence、questionsAsked、assumptions 调 project_run。',
|
|
79
|
+
)
|
|
80
|
+
}
|
|
81
|
+
if (typeof confidence !== 'number' || !Number.isFinite(confidence)) {
|
|
82
|
+
throw new Error('intentConfidence 要一个 0-100 的数字:你现在对用户真实意图的把握有多大。')
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// The user's explicit go-ahead outranks the confidence score: when they say
|
|
86
|
+
// "stop asking, build it", the honest reading is 100, not the caller's guess.
|
|
87
|
+
// Checked before the floor so an override can never be blocked by it —
|
|
88
|
+
// but it does NOT outrank the blindspot check above: an override releases
|
|
89
|
+
// "am I sure enough", never "did I actually ask".
|
|
90
|
+
const overridden = confirmation.overridden === true
|
|
91
|
+
if (overridden) {
|
|
92
|
+
const reason = typeof confirmation.overrideReason === 'string' ? confirmation.overrideReason.trim() : ''
|
|
93
|
+
if (reason === '') {
|
|
94
|
+
throw new Error('用了 override 就要在 overrideReason 里写清用户是怎么说的,比如「用户说就按这个做」。')
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
confidence,
|
|
98
|
+
questionsAsked: typeof confirmation.questionsAsked === 'number' ? confirmation.questionsAsked : 0,
|
|
99
|
+
assumptions: Array.isArray(confirmation.assumptions)
|
|
100
|
+
? confirmation.assumptions.filter(line => typeof line === 'string' && line.trim() !== '')
|
|
101
|
+
: [],
|
|
102
|
+
blindspots,
|
|
103
|
+
overridden: true,
|
|
104
|
+
overrideReason: reason,
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (confidence < INTENT_CONFIDENCE_FLOOR) {
|
|
109
|
+
throw new Error(
|
|
110
|
+
`intentConfidence 只有 ${confidence},没到 ${INTENT_CONFIDENCE_FLOOR}。`
|
|
111
|
+
+ '继续问,别接着往下跑——规划段拿到的意图越含糊,实施段跑偏得越远,返工也救不回来。'
|
|
112
|
+
+ '把还差的那几点问清楚,或者让用户明确说「就按这个做,别再问了」再用 override 放行。',
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const asked = confirmation.questionsAsked
|
|
117
|
+
if (typeof asked !== 'number' || !Number.isInteger(asked) || asked < 1) {
|
|
118
|
+
throw new Error(
|
|
119
|
+
'questionsAsked 至少要是 1:一条都没问过就宣称有把握,通常是把猜测当成了事实。'
|
|
120
|
+
+ '真的一问都不用问(用户已经把要求写成了清单),把用户原话说清楚再传 1。',
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return {
|
|
125
|
+
confidence,
|
|
126
|
+
questionsAsked: asked,
|
|
127
|
+
assumptions: Array.isArray(confirmation.assumptions)
|
|
128
|
+
? confirmation.assumptions.filter(line => typeof line === 'string' && line.trim() !== '')
|
|
129
|
+
: [],
|
|
130
|
+
blindspots,
|
|
131
|
+
overridden: false,
|
|
132
|
+
overrideReason: '',
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function rememberReadiness(agent, readiness) {
|
|
137
|
+
if (agent === undefined || agent === null || typeof agent !== 'object') return
|
|
138
|
+
pfState.readinessByAgent.set(agent, readiness)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function lastReadinessOf(exec) {
|
|
142
|
+
const agent = exec?.agent
|
|
143
|
+
if (agent === undefined || agent === null || typeof agent !== 'object') return undefined
|
|
144
|
+
return pfState.readinessByAgent.get(agent)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function hitsForbidden(filePath, forbidden) {
|
|
148
|
+
if (typeof filePath !== 'string' || filePath === '') return ''
|
|
149
|
+
const norm = value => value.replace(/\\+/gu, '/').replace(/\/+$/u, '').toLowerCase()
|
|
150
|
+
const target = norm(filePath)
|
|
151
|
+
for (const entry of Array.isArray(forbidden) ? forbidden : []) {
|
|
152
|
+
const rule = norm(String(entry))
|
|
153
|
+
if (rule === '') continue
|
|
154
|
+
if (target === rule || target.startsWith(`${rule}/`)) return String(entry)
|
|
155
|
+
}
|
|
156
|
+
return ''
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function stageModelOf(settings, flow, stageNo, fallback) {
|
|
160
|
+
const phase = flow?.phases?.[stageNo - 1]
|
|
161
|
+
const configured = phase !== undefined && phase !== null && typeof phase.model === 'object' && phase.model !== null
|
|
162
|
+
? phase.model
|
|
163
|
+
: null
|
|
164
|
+
const fromPhase = key => {
|
|
165
|
+
if (configured === null) return ''
|
|
166
|
+
const value = configured[key]
|
|
167
|
+
return typeof value === 'string' ? value.trim() : ''
|
|
168
|
+
}
|
|
169
|
+
const phaseProvider = fromPhase('provider')
|
|
170
|
+
const phaseModel = fromPhase('model')
|
|
171
|
+
return {
|
|
172
|
+
provider: settings.provider,
|
|
173
|
+
llmProvider: phaseProvider !== '' ? phaseProvider : fallback.llmProvider,
|
|
174
|
+
model: phaseModel !== '' ? phaseModel : fallback.model,
|
|
175
|
+
// 这个模型是 flow.yaml 指定的,还是配置里来的——回退日志要说清是谁的锅。
|
|
176
|
+
source: phaseModel !== '' ? 'flow.yaml' : fallback.llmProvider === '' && fallback.model === '' ? 'inherit' : 'config',
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function assertProjectName(project) {
|
|
181
|
+
if (typeof project !== 'string' || project.trim() === '') {
|
|
182
|
+
throw new Error('项目名不能为空。用一个小写加连字符的简称,比如 my-site。')
|
|
183
|
+
}
|
|
184
|
+
const name = project.trim()
|
|
185
|
+
if (name === '.' || name === '..') {
|
|
186
|
+
throw new Error(`项目名「${name}」不能是相对路径。换一个小写加连字符的简称。`)
|
|
187
|
+
}
|
|
188
|
+
if (name.includes('/') || name.includes('\\') || name.includes(':')) {
|
|
189
|
+
throw new Error(`项目名「${name}」里不能有斜杠或冒号,它只是一个目录名。换一个,比如 my-site。`)
|
|
190
|
+
}
|
|
191
|
+
if (/[\u0000-\u001f]/u.test(name)) {
|
|
192
|
+
throw new Error('项目名里不能有控制字符。换一个小写加连字符的简称。')
|
|
193
|
+
}
|
|
194
|
+
if (name !== project) {
|
|
195
|
+
throw new Error(`项目名「${project}」前后不能有空白。去掉空白再试。`)
|
|
196
|
+
}
|
|
197
|
+
return name
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function onSystemDrive(target) {
|
|
201
|
+
if (process.platform !== 'win32') return false
|
|
202
|
+
const system = (process.env.SystemDrive ?? 'C:').replace(/[\\/]+$/u, '').toLowerCase()
|
|
203
|
+
const normalized = String(target ?? '').replace(/\\+/gu, '/').toLowerCase()
|
|
204
|
+
return normalized === system.toLowerCase() || normalized.startsWith(`${system.toLowerCase()}/`)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export function pickDataDrive() {
|
|
208
|
+
if (process.platform !== 'win32') return undefined
|
|
209
|
+
for (const letter of ['D', 'E', 'F', 'G']) {
|
|
210
|
+
try {
|
|
211
|
+
if (existsSync(`${letter}:\\`)) return `${letter}:`
|
|
212
|
+
} catch {
|
|
213
|
+
// 这个盘符不可访问,试下一个
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return undefined
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export function flowRootOf(cwd, settings) {
|
|
220
|
+
const configured = typeof settings.flowRoot === 'string' ? settings.flowRoot.trim() : ''
|
|
221
|
+
if (configured !== '' && isAbsolute(configured)) return resolve(configured)
|
|
222
|
+
const base = resolve(cwd)
|
|
223
|
+
// 不往系统盘上建工作流。
|
|
224
|
+
//
|
|
225
|
+
// 用户的默认工作区就在 C 盘(DSH 装在 C,默认工作区跟着走),于是
|
|
226
|
+
// 「谁的工作区就在谁下面建」等于把项目数据全塞进系统盘。他明确说了
|
|
227
|
+
// 「默认不要在此盘」——那就是别在 C 盘上建。
|
|
228
|
+
//
|
|
229
|
+
// 有别的盘就挪过去(<盘>:\工作流),单盘机器上留在原地。
|
|
230
|
+
if (onSystemDrive(base)) {
|
|
231
|
+
const data = pickDataDrive()
|
|
232
|
+
if (data !== undefined) return join(`${data}${sep}`, FLOW_DIR_NAME)
|
|
233
|
+
}
|
|
234
|
+
return join(base, configured === '' ? FLOW_DIR_NAME : configured)
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export function flowRootConfigured(settings) {
|
|
238
|
+
const configured = typeof settings.flowRoot === 'string' ? settings.flowRoot.trim() : ''
|
|
239
|
+
return configured !== '' && isAbsolute(configured)
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export function flowRootTargetOf(cwd, settings) {
|
|
243
|
+
const root = flowRootOf(cwd, settings)
|
|
244
|
+
try {
|
|
245
|
+
const nested = join(root, FLOW_DIR_NAME)
|
|
246
|
+
if (existsSync(join(nested, '.project-flow'))) return nested
|
|
247
|
+
} catch {
|
|
248
|
+
// 读不到就算了,退回配置根
|
|
249
|
+
}
|
|
250
|
+
return root
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export function flowModeOf(settings) {
|
|
254
|
+
const raw = typeof settings?.flowGate === 'string' ? settings.flowGate.trim().toLowerCase() : ''
|
|
255
|
+
return FLOW_GATE_MODES.includes(raw) ? raw : 'off'
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export function insideFlow(cwd, settings) {
|
|
259
|
+
if (flowModeOf(settings) !== 'strict') return true
|
|
260
|
+
const configured = typeof settings.flowRoot === 'string' ? settings.flowRoot.trim() : ''
|
|
261
|
+
const here = resolve(cwd)
|
|
262
|
+
// strict 下配了绝对路径:只认那个子树的,别处一律不算。
|
|
263
|
+
if (configured !== '' && isAbsolute(configured)) {
|
|
264
|
+
const root = resolve(configured)
|
|
265
|
+
const rel = relative(root, here)
|
|
266
|
+
return rel === '' || (!rel.startsWith('..') && !isAbsolute(rel))
|
|
267
|
+
}
|
|
268
|
+
// strict 且没配绝对路径:认路径里有没有这一段。
|
|
269
|
+
// cwd 本身叫这个名字(把工作流目录直接当工作区开),或者它在某个同名目录下面。
|
|
270
|
+
const name = (configured === '' ? FLOW_DIR_NAME : configured).replace(/[\\/]+$/u, '')
|
|
271
|
+
return here.split(/[\\/]/u).includes(name)
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export function flowGateMessage(cwd, settings) {
|
|
275
|
+
const target = flowRootTargetOf(cwd, settings)
|
|
276
|
+
const configured = flowRootConfigured(settings)
|
|
277
|
+
return [
|
|
278
|
+
`这个目录不是工作流目录,工作模式只在「${FLOW_DIR_NAME}」里开。`,
|
|
279
|
+
`应该在:${target}`,
|
|
280
|
+
configured
|
|
281
|
+
? '(位置来自配置项 flowRoot。)'
|
|
282
|
+
: `(flowGate=strict,只认当前工作区下的「${FLOW_DIR_NAME}」目录。)`,
|
|
283
|
+
'',
|
|
284
|
+
configured
|
|
285
|
+
? '把工作区切到上面那个目录再开。配置里写死了绝对路径,插件不会替你在别处另起一个。'
|
|
286
|
+
: '不用你自己建:第一次开工时插件会把这个目录建出来,然后到里面干活。',
|
|
287
|
+
'确实不想要这道边界:/flow-set 门控 off。',
|
|
288
|
+
].join('\n')
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export async function ensureFlowRoot(cwd, settings) {
|
|
292
|
+
if (insideFlow(cwd, settings)) {
|
|
293
|
+
return { root: cwd, created: false, moved: false }
|
|
294
|
+
}
|
|
295
|
+
// 用 flowRootTargetOf 而不是 flowRootOf:配了绝对路径时,真正的项目数据
|
|
296
|
+
// 常常在它下面那层「工作流」里(用户配 D:\某个目录,项目在 D:\某个目录\工作流)。
|
|
297
|
+
// 用外层的话,工作目录会指到没有 .project-flow 的地方,项目一个都看不见。
|
|
298
|
+
const root = flowRootTargetOf(cwd, settings)
|
|
299
|
+
// mkdir recursive 对已存在的目录不报错,所以「建成功了」和「这次新建了」
|
|
300
|
+
// 是两件事,得分开判。以前把两者混成一个 created,于是每次调用都报
|
|
301
|
+
// 「新建了」——状态栏和日志里都会说一句假话。
|
|
302
|
+
const existed = existsSync(root)
|
|
303
|
+
try {
|
|
304
|
+
await mkdir(root, { recursive: true })
|
|
305
|
+
} catch (error) {
|
|
306
|
+
// 建不出来就说清是哪条路走不通。
|
|
307
|
+
//
|
|
308
|
+
// 以前这里静默退回,用户看到的只有一句「不在工作流目录里」——他完全
|
|
309
|
+
// 不知道该干什么:是权限问题?盘只读?还是路径本身不合法?
|
|
310
|
+
// 一句「建不出来」比一句「不行」有用得多。
|
|
311
|
+
return { root: cwd, created: false, moved: false, error: messageOf(error), attempted: root }
|
|
312
|
+
}
|
|
313
|
+
return { root, created: !existed, moved: true }
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export function isScratchPath(cwd) {
|
|
317
|
+
try {
|
|
318
|
+
const scratch = resolve(tmpdir()).toLowerCase()
|
|
319
|
+
return resolve(cwd).toLowerCase().startsWith(scratch)
|
|
320
|
+
} catch {
|
|
321
|
+
return false
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export function stateHome() {
|
|
326
|
+
return process.env.DSH_HOME ?? join(homedir(), '.dsh')
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
export async function cwdFromSessionStore(sid) {
|
|
330
|
+
if (typeof sid !== 'string' || sid === '') return undefined
|
|
331
|
+
// sid 只可能是 id,不该带路径分隔符;挡一下,别让它变成越界读文件
|
|
332
|
+
if (sid.includes('/') || sid.includes('\\') || sid.includes('..')) return undefined
|
|
333
|
+
const file = join(stateHome(), 'storages', 'session_projcache', 'sessions', `${sid}.json`)
|
|
334
|
+
try {
|
|
335
|
+
const parsed = JSON.parse(await readFile(file, 'utf8'))
|
|
336
|
+
const cwd = parsed?.record?.identity?.cwd
|
|
337
|
+
return typeof cwd === 'string' && cwd !== '' && isAbsolute(cwd) ? cwd : undefined
|
|
338
|
+
} catch {
|
|
339
|
+
return undefined
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
export async function rememberRoot(cwd) {
|
|
344
|
+
try {
|
|
345
|
+
if (typeof cwd !== 'string' || cwd === '' || !isAbsolute(cwd)) return
|
|
346
|
+
// 临时区里的东西不算「我的工作流」:测试、eval、沙箱全在那儿。
|
|
347
|
+
const scratch = resolve(tmpdir())
|
|
348
|
+
const here = resolve(cwd)
|
|
349
|
+
if (here === scratch || here.startsWith(`${scratch}${sep}`)) return
|
|
350
|
+
if (!here.split(/[\\/]+/u).includes(FLOW_DIR_NAME)) return
|
|
351
|
+
const file = join(stateHome(), LAST_ROOT_FILE)
|
|
352
|
+
const current = await readFile(file, 'utf8').catch(() => '')
|
|
353
|
+
if (current.trim() === here) return
|
|
354
|
+
await mkdir(stateHome(), { recursive: true })
|
|
355
|
+
await writeFileAtomic(file, `${here}\n`)
|
|
356
|
+
} catch {
|
|
357
|
+
// 记不住只影响跨目录显示,不该拖累任何正常流程
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
export function flowAncestor(cwd) {
|
|
362
|
+
let current = resolve(cwd)
|
|
363
|
+
for (;;) {
|
|
364
|
+
if (basename(current) === FLOW_DIR_NAME) return current
|
|
365
|
+
const parent = dirname(current)
|
|
366
|
+
if (parent === current) return ''
|
|
367
|
+
current = parent
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
export function focusOf(cwd, root) {
|
|
372
|
+
if (root === '') return ''
|
|
373
|
+
const here = resolve(cwd)
|
|
374
|
+
if (here === resolve(root)) return ''
|
|
375
|
+
const rel = relative(resolve(root), here)
|
|
376
|
+
if (rel === '' || rel.startsWith('..') || isAbsolute(rel)) return ''
|
|
377
|
+
return rel.split(/[\\/]+/u)[0] ?? ''
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
export async function watchRootOf(cwd, settings) {
|
|
381
|
+
if (!insideFlow(cwd, settings)) return cwd
|
|
382
|
+
// 工作区根本身:流程数据通常在它下面的「工作流」子目录里。
|
|
383
|
+
// 用户的工作区根是 D:\新建文件夹 (4),流程在 D:\新建文件夹 (4)\工作流——
|
|
384
|
+
// 那才是他说的「工作流这一栏」,父目录自己的 .project-flow 是另一回事。
|
|
385
|
+
const nested = join(resolve(cwd), FLOW_DIR_NAME)
|
|
386
|
+
if (await dirExists(join(nested, '.project-flow'))) return nested
|
|
387
|
+
// cwd 自己就有 .project-flow:它本身就是一个完整工作区,别再往上找
|
|
388
|
+
if (await dirExists(join(cwd, '.project-flow'))) return cwd
|
|
389
|
+
// 在工作流里、但落在某个项目目录下:回到工作流根
|
|
390
|
+
const root = flowAncestor(cwd)
|
|
391
|
+
return root === '' ? cwd : root
|
|
392
|
+
}
|
|
393
|
+
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 续跑:从中断那一段接上,已经通过的不重做。
|
|
3
|
+
*/
|
|
4
|
+
import { readdir } from 'node:fs/promises'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
import { sweep } from '../persistence/recovery.mjs'
|
|
7
|
+
import { readArtifact, readState } from '../persistence/state.mjs'
|
|
8
|
+
import { ARTIFACT } from '../shared/constants.mjs'
|
|
9
|
+
import { extractSection, stateDir, withoutHeading } from '../shared/util.mjs'
|
|
10
|
+
import { pfState } from '../state/registry.mjs'
|
|
11
|
+
|
|
12
|
+
export async function buildResumePlan(cwd, project, settings) {
|
|
13
|
+
let state = await readState(cwd, project)
|
|
14
|
+
let healed = []
|
|
15
|
+
if (state === undefined) {
|
|
16
|
+
// state 没了就按产物重建一次,这是自愈,不是覆盖
|
|
17
|
+
healed = await sweep(cwd, settings, project, true)
|
|
18
|
+
state = await readState(cwd, project)
|
|
19
|
+
if (state === undefined) return { kind: 'no-state', healed }
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const intent = typeof state.intent === 'string' ? state.intent : ''
|
|
23
|
+
const requirements = Array.isArray(state.requirements) ? state.requirements.map(String) : []
|
|
24
|
+
if (intent === '' || requirements.length === 0) return { kind: 'no-intent', state, healed }
|
|
25
|
+
|
|
26
|
+
const planArtifact = await readArtifact(cwd, project, ARTIFACT.plan)
|
|
27
|
+
const planText = withoutHeading(extractSection(planArtifact, '## 施工任务书', true))
|
|
28
|
+
const currentTicket = extractSection(planArtifact, '## 施工任务书')
|
|
29
|
+
// 实施自述从 Execution_Log.md 的尾巴上取,日志是追加写的
|
|
30
|
+
const logText = await readArtifact(cwd, project, ARTIFACT.log)
|
|
31
|
+
const buildSummary = logText === undefined ? '' : logText.slice(-3000)
|
|
32
|
+
|
|
33
|
+
const stage = typeof state.stage === 'number' ? state.stage : 0
|
|
34
|
+
if (stage >= 4 && state.status === 'passed') {
|
|
35
|
+
return { kind: 'already-passed', state, healed, stage, planText, buildSummary }
|
|
36
|
+
}
|
|
37
|
+
// 停在规划段的时候 Plan.md 还没写出来,没有施工任务书是正常的:
|
|
38
|
+
// 从第 2 阶重跑规划就行,不该拦在这里。只有已经进过实施段才真的需要任务书。
|
|
39
|
+
if (stage >= 3 && currentTicket === '') return { kind: 'no-ticket', state, healed, stage }
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
kind: 'plan',
|
|
43
|
+
state,
|
|
44
|
+
healed,
|
|
45
|
+
stage,
|
|
46
|
+
options: {
|
|
47
|
+
cwd,
|
|
48
|
+
project,
|
|
49
|
+
intent,
|
|
50
|
+
requirements,
|
|
51
|
+
constraints: '',
|
|
52
|
+
maxRework: typeof state.maxRework === 'number' ? state.maxRework : settings.maxRework,
|
|
53
|
+
seed: {
|
|
54
|
+
startStage: Math.max(2, Math.min(stage, 4)),
|
|
55
|
+
planText,
|
|
56
|
+
currentTicket,
|
|
57
|
+
buildSummary,
|
|
58
|
+
rework: typeof state.rework === 'number' ? state.rework : 0,
|
|
59
|
+
verdict: typeof state.verdict === 'string' ? state.verdict : '',
|
|
60
|
+
issues: [],
|
|
61
|
+
attempts: Array.isArray(state.attempts) ? state.attempts : [],
|
|
62
|
+
},
|
|
63
|
+
mode: `resume-from-stage-${stage}`,
|
|
64
|
+
},
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export async function pickResumable(cwd, settings) {
|
|
69
|
+
let names = []
|
|
70
|
+
try {
|
|
71
|
+
const entries = await readdir(join(cwd, '.project-flow'), { withFileTypes: true })
|
|
72
|
+
names = entries.filter(entry => entry.isDirectory()).map(entry => entry.name)
|
|
73
|
+
} catch {
|
|
74
|
+
return undefined
|
|
75
|
+
}
|
|
76
|
+
let best
|
|
77
|
+
for (const name of names) {
|
|
78
|
+
if (pfState.panelStops.has(name)) continue
|
|
79
|
+
const state = await readState(cwd, name)
|
|
80
|
+
if (state === undefined) continue
|
|
81
|
+
const stage = typeof state.stage === 'number' ? state.stage : 0
|
|
82
|
+
if (stage >= 4 && state.status === 'passed') continue
|
|
83
|
+
const at = typeof state.interruptedAt === 'string' ? state.interruptedAt : ''
|
|
84
|
+
const score = `${at}|${typeof state.updatedAt === 'string' ? state.updatedAt : ''}`
|
|
85
|
+
if (best === undefined || score > best.score) best = { name, score }
|
|
86
|
+
}
|
|
87
|
+
return best?.name
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export async function latestNumbered(cwd, project, prefix) {
|
|
91
|
+
let files
|
|
92
|
+
try {
|
|
93
|
+
files = await readdir(stateDir(cwd, project))
|
|
94
|
+
} catch {
|
|
95
|
+
return undefined
|
|
96
|
+
}
|
|
97
|
+
const pattern = new RegExp(`^${prefix}-(\\d+)\\.md$`, 'u')
|
|
98
|
+
const found = files
|
|
99
|
+
.map(name => {
|
|
100
|
+
const match = pattern.exec(name)
|
|
101
|
+
return match === null ? undefined : { name, index: Number(match[1]) }
|
|
102
|
+
})
|
|
103
|
+
.filter(entry => entry !== undefined)
|
|
104
|
+
.sort((left, right) => left.index - right.index)
|
|
105
|
+
return found.length === 0 ? undefined : found[found.length - 1]
|
|
106
|
+
}
|
|
107
|
+
|