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.
Files changed (70) hide show
  1. package/CHANGELOG.md +197 -12
  2. package/README.md +25 -0
  3. package/checks/assertion.mjs +104 -0
  4. package/checks/command.mjs +416 -0
  5. package/checks/requirement.mjs +91 -0
  6. package/commands/slash.mjs +754 -0
  7. package/commands/steer.mjs +43 -0
  8. package/config/settings.mjs +217 -0
  9. package/docs/FEATURES.md +9 -6
  10. package/docs/anchors/file-whitelist.md +1 -1
  11. package/http/routes.mjs +252 -0
  12. package/index.js +173 -8612
  13. package/issues/issue.mjs +322 -0
  14. package/package.json +16 -1
  15. package/persistence/lock.mjs +67 -0
  16. package/persistence/log.mjs +43 -0
  17. package/persistence/recovery.mjs +118 -0
  18. package/persistence/state.mjs +202 -0
  19. package/pricing/estimate.mjs +72 -0
  20. package/prompts/rules.mjs +192 -0
  21. package/rollback/ledger.mjs +237 -0
  22. package/runner/pipeline.mjs +1190 -0
  23. package/runner/stage.mjs +370 -0
  24. package/runner/tools/context.mjs +64 -0
  25. package/runner/tools/decorate-cost.mjs +49 -0
  26. package/runner/tools/doctor.mjs +59 -0
  27. package/runner/tools/estimate.mjs +63 -0
  28. package/runner/tools/execute-run.mjs +255 -0
  29. package/runner/tools/models.mjs +55 -0
  30. package/runner/tools/resume.mjs +136 -0
  31. package/runner/tools/rollback.mjs +291 -0
  32. package/runner/tools/run.mjs +262 -0
  33. package/runner/tools/set.mjs +65 -0
  34. package/runner/tools/snapshots.mjs +83 -0
  35. package/runner/tools/status.mjs +70 -0
  36. package/runner/tools.mjs +94 -0
  37. package/shared/constants.mjs +273 -0
  38. package/shared/tool.mjs +32 -0
  39. package/shared/util.mjs +231 -0
  40. package/snapshot/repo.mjs +452 -0
  41. package/state/registry.mjs +40 -0
  42. package/tests/aesthetic.test.mjs +20 -12
  43. package/tests/apply-wiring.test.mjs +172 -0
  44. package/tests/assertions-e2e.mjs +2 -1
  45. package/tests/atomic-write.test.mjs +129 -0
  46. package/tests/client-bundle.test.mjs +3 -3
  47. package/tests/command-whitelist.test.mjs +135 -0
  48. package/tests/contract.test.mjs +2 -2
  49. package/tests/desktop.test.mjs +2 -2
  50. package/tests/domains.test.mjs +10 -7
  51. package/tests/e2e.mjs +2 -1
  52. package/tests/extra-phase.test.mjs +2 -2
  53. package/tests/feedback.test.mjs +2 -1
  54. package/tests/flow-gate.test.mjs +16 -56
  55. package/tests/issues-e2e.mjs +2 -1
  56. package/tests/panel-independence.test.mjs +2 -2
  57. package/tests/panel-report.test.mjs +2 -2
  58. package/tests/paths.mjs +70 -1
  59. package/tests/preview-path.test.mjs +2 -1
  60. package/tests/qa-assertions.test.mjs +10 -3
  61. package/tests/run-all.mjs +5 -0
  62. package/tests/snapshot-core.test.mjs +234 -0
  63. package/tests/stage-model.test.mjs +2 -2
  64. package/tests/stop-resume.test.mjs +7 -2
  65. package/tests/watch-root.test.mjs +2 -1
  66. package/ui/prompt.mjs +101 -0
  67. package/workspace/models.mjs +111 -0
  68. package/workspace/panel.mjs +359 -0
  69. package/workspace/paths.mjs +393 -0
  70. package/workspace/resume.mjs +107 -0
@@ -0,0 +1,237 @@
1
+ /**
2
+ * 补丁台账:逐行 JSON 追加写;回滚按台账精确还原,只动 agent 真写过的路径。
3
+ */
4
+ import { join, isAbsolute, relative } from 'node:path'
5
+ import { mkdirSync, appendFileSync, readFileSync } from 'node:fs'
6
+ import { rm } from 'node:fs/promises'
7
+ import { fileSha256, messageOf, nowIso, stateDir } from '../shared/util.mjs'
8
+ import { snapshotCommand } from '../snapshot/repo.mjs'
9
+
10
+ export function ledgerDir(cwd, project) {
11
+ return join(stateDir(cwd, project), 'ledger')
12
+ }
13
+
14
+ export function ledgerPath(cwd, project) {
15
+ return join(ledgerDir(cwd, project), 'patch-ledger.jsonl')
16
+ }
17
+
18
+ export function normalizedPatchPath(path) {
19
+ const text = typeof path === 'string' ? path : String(path ?? '')
20
+ return text.replace(/\\/gu, '/').replace(/^\.\//u, '').trim()
21
+ }
22
+
23
+ export function appendLedgerEntry(cwd, project, entry) {
24
+ const path = ledgerPath(cwd, project)
25
+ try {
26
+ mkdirSync(ledgerDir(cwd, project), { recursive: true })
27
+ const line = JSON.stringify(entry) + '\n'
28
+ appendFileSync(path, line, 'utf8')
29
+ } catch {
30
+ // 台账写失败不该中断实施段——但要在日志里留痕。
31
+ }
32
+ }
33
+
34
+ export function readLedger(cwd, project) {
35
+ const path = ledgerPath(cwd, project)
36
+ let text
37
+ try {
38
+ text = readFileSync(path, 'utf8')
39
+ } catch {
40
+ return []
41
+ }
42
+ const lines = text.split(/\r?\n/u).filter(line => line.trim() !== '')
43
+ const entries = []
44
+ const bad = []
45
+ for (let i = 0; i < lines.length; i += 1) {
46
+ try {
47
+ const obj = JSON.parse(lines[i])
48
+ if (obj !== null && typeof obj === 'object' && !Array.isArray(obj)) {
49
+ entries.push(obj)
50
+ } else {
51
+ bad.push({ line: i + 1, text: lines[i].slice(0, 100) })
52
+ }
53
+ } catch {
54
+ bad.push({ line: i + 1, text: lines[i].slice(0, 100) })
55
+ }
56
+ }
57
+ if (bad.length > 0) {
58
+ const msg = `台账有 ${bad.length} 行坏 JSON:` + bad.map(b => `第 ${b.line} 行(${b.text})`).join('、')
59
+ throw new Error(msg)
60
+ }
61
+ return entries
62
+ }
63
+
64
+ export async function recordFileWrite(cwd, project, filePath, batchId, stage, index, beforeCommit = '') {
65
+ const abs = isAbsolute(filePath) ? filePath : join(cwd, filePath)
66
+ const rel = normalizedPatchPath(isAbsolute(filePath) ? relative(cwd, filePath) : filePath)
67
+ const beforeSha = await fileSha256(abs)
68
+ return {
69
+ index,
70
+ batchId,
71
+ stage,
72
+ path: rel,
73
+ beforeSha,
74
+ beforeCommit: typeof beforeCommit === 'string' ? beforeCommit : '',
75
+ afterSha: null,
76
+ status: 'pending',
77
+ ts: nowIso(),
78
+ }
79
+ }
80
+
81
+ export function commitLedgerEntry(cwd, project, entry, afterSha) {
82
+ const committed = { ...entry, afterSha, status: 'applied', ts: nowIso() }
83
+ appendLedgerEntry(cwd, project, committed)
84
+ return committed
85
+ }
86
+
87
+ export async function rollbackByLedger(cwd, project, { dryRun = true } = {}) {
88
+ const entries = readLedger(cwd, project)
89
+ if (entries.length === 0) {
90
+ return { ok: false, reason: '台账为空,没有可回滚的条目' }
91
+ }
92
+ const applied = entries.filter(e => e.status === 'applied')
93
+ if (applied.length === 0) {
94
+ return { ok: false, reason: '台账里没有 applied 条目' }
95
+ }
96
+ // 按 batchId 分组,取最后那个 batch
97
+ const batchIds = [...new Set(applied.map(e => e.batchId))]
98
+ const lastBatch = batchIds[batchIds.length - 1]
99
+ const batchEntries = applied.filter(e => e.batchId === lastBatch)
100
+
101
+ // ── plan:算这一批每个文件会怎么动 ──
102
+ //
103
+ // 判断「要不要还原」不看 beforeSha:那个字段在实施后才写,值其实是写入后的。
104
+ // 唯一可信的旧内容来源是 beforeCommit 那张快照。
105
+ //
106
+ // 比「当前 == 快照」用 git 自己的 blob 哈希(`git hash-object` 算的是同一种
107
+ // sha1),不走 cat-file 二进制管道——那条路会被编码转换弄坏内容。
108
+ const plans = []
109
+ for (const entry of batchEntries) {
110
+ const abs = join(cwd, entry.path)
111
+ const currentSha = await fileSha256(abs).catch(() => null)
112
+ const hasCommit = typeof entry.beforeCommit === 'string' && entry.beforeCommit !== ''
113
+
114
+ if (!hasCommit) {
115
+ // 老条目,没记快照:没有内容来源,明确说还原不了,不假装成功
116
+ if (currentSha === null) {
117
+ plans.push({ entry, action: 'unchanged', note: '文件已不在,且台账没记快照' })
118
+ } else {
119
+ plans.push({
120
+ entry,
121
+ action: 'blocked',
122
+ note: '台账没记实施前的快照(老版本写的条目),没有内容来源,不能自动还原',
123
+ })
124
+ }
125
+ continue
126
+ }
127
+
128
+ // 快照里有没有这个文件
129
+ const inTree = await snapshotCommand(cwd, project, ['rev-parse', '--verify', '--quiet', `${entry.beforeCommit}:${entry.path}`])
130
+ if (!inTree.ok) {
131
+ // 快照里没有 = 写入前它不存在 → 还原就是删掉
132
+ plans.push({
133
+ entry,
134
+ action: currentSha === null ? 'unchanged' : 'delete',
135
+ note: currentSha === null ? '文件已不在,无需处理' : '删除(写入前不存在)',
136
+ })
137
+ continue
138
+ }
139
+
140
+ if (currentSha === null) {
141
+ plans.push({ entry, action: 'restore', note: `文件已删,从 ${entry.beforeCommit.slice(0, 8)} 重建` })
142
+ continue
143
+ }
144
+
145
+ // 当前文件内容的 git sha1,跟快照里那个 blob 直接比
146
+ const currentGitSha = await snapshotCommand(cwd, project, ['hash-object', entry.path])
147
+ if (currentGitSha.ok && currentGitSha.stdout.trim() === inTree.stdout.trim()) {
148
+ plans.push({ entry, action: 'unchanged', note: '已与写入前一致' })
149
+ continue
150
+ }
151
+ plans.push({ entry, action: 'restore', note: `还原到 ${entry.beforeCommit.slice(0, 8)}` })
152
+ }
153
+
154
+ // ── 干跑:到此为止 ──
155
+ if (dryRun) {
156
+ return {
157
+ ok: true,
158
+ dryRun: true,
159
+ batchId: lastBatch,
160
+ count: batchEntries.length,
161
+ restored: plans.filter(p => p.action === 'restore' || p.action === 'delete').map(p => `${p.entry.path}(${p.note})`),
162
+ failed: plans.filter(p => p.action === 'blocked').map(p => `${p.entry.path}(${p.note})`),
163
+ plan: plans.map(p => ({ path: p.entry.path, action: p.action, note: p.note })),
164
+ }
165
+ }
166
+
167
+ // ── apply:真动磁盘 ──
168
+ const restored = []
169
+ const failed = []
170
+ for (const item of plans) {
171
+ const { entry } = item
172
+ const abs = join(cwd, entry.path)
173
+ if (item.action === 'unchanged') {
174
+ restored.push(`${entry.path}(已一致)`)
175
+ continue
176
+ }
177
+ if (item.action === 'blocked') {
178
+ failed.push(`${entry.path}(${item.note})`)
179
+ continue
180
+ }
181
+ try {
182
+ if (item.action === 'delete') {
183
+ await rm(abs, { force: true })
184
+ restored.push(`${entry.path}(删除)`)
185
+ } else {
186
+ // 从实施前那张快照把这个文件 checkout 回来
187
+ const result = await snapshotCommand(cwd, project, ['checkout', entry.beforeCommit, '--', entry.path], { timeoutMs: 300000 })
188
+ if (!result.ok) {
189
+ failed.push(`${entry.path}(快照里 checkout 失败:${(result.stderr || result.message || '').trim().slice(0, 80)})`)
190
+ continue
191
+ }
192
+ restored.push(`${entry.path}(还原到 ${entry.beforeCommit.slice(0, 8)})`)
193
+ }
194
+ // 只有真动过文件的才记 reverted
195
+ appendLedgerEntry(cwd, project, {
196
+ index: entry.index,
197
+ batchId: entry.batchId,
198
+ stage: entry.stage,
199
+ path: entry.path,
200
+ beforeSha: entry.afterSha,
201
+ afterSha: entry.beforeSha,
202
+ beforeCommit: entry.beforeCommit,
203
+ status: 'reverted',
204
+ rollbackOf: entry.index,
205
+ ts: nowIso(),
206
+ })
207
+ } catch (error) {
208
+ failed.push(`${entry.path}(${messageOf(error)})`)
209
+ }
210
+ }
211
+ return {
212
+ ok: failed.length === 0,
213
+ dryRun: false,
214
+ batchId: lastBatch,
215
+ count: batchEntries.length,
216
+ restored,
217
+ failed,
218
+ plan: plans.map(p => ({ path: p.entry.path, action: p.action, note: p.note })),
219
+ }
220
+ }
221
+
222
+ export function checkLedgerCoverage(cwd, project, diffPaths) {
223
+ const entries = readLedger(cwd, project)
224
+ const ledgerPaths = new Set(entries
225
+ .filter(e => e.status === 'applied')
226
+ .map(e => normalizedPatchPath(e.path)))
227
+ const diff = Array.isArray(diffPaths) ? diffPaths : []
228
+ const outOfBounds = diff
229
+ .map(p => normalizedPatchPath(p))
230
+ .filter(p => !ledgerPaths.has(p))
231
+ return {
232
+ total: diff.length,
233
+ inLedger: diff.length - outOfBounds.length,
234
+ outOfBounds,
235
+ }
236
+ }
237
+