dsh-workflow-execute 0.0.0-stage → 1.0.0

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/src/runs.js ADDED
@@ -0,0 +1,237 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ * dsh-workflow-execute — 运行台账(v0.2 执行驱动)。
5
+ *
6
+ * run = 一次「按工作流驱动执行」:发起时冻结工作流快照(YAML),宿主逐环节开全新
7
+ * dsh 会话推进,门禁/返工/流转在代码层强制。台账落 DSH_HOME/dsh-workflow-execute/runs.json
8
+ * (原子写 + 防抖),revision 变化走既有 SSE 广播。
9
+ *
10
+ * 纯函数部分(流转解析 / 环节查找 / 步数上限)单独导出,供单测。
11
+ */
12
+
13
+ const fsP = require('node:fs/promises')
14
+ const path = require('node:path')
15
+ const { randomUUID } = require('node:crypto')
16
+ const { StoreError } = require('./store.js')
17
+
18
+ const FLOW_KEYWORDS = new Set(['next', 'end', 'break'])
19
+ const MAX_RUNS_KEPT = 100
20
+ const PERSIST_DEBOUNCE_MS = 300
21
+
22
+ /** 解析流转值:'next' | 'end' | 'break' | { jump: linkId }。非法/缺省返回 null(调用方决定语义)。 */
23
+ function parseFlow(value) {
24
+ if (value === undefined || value === null || value === '') return null
25
+ const v = String(value).trim()
26
+ if (FLOW_KEYWORDS.has(v)) return v
27
+ if (v.startsWith('goto:')) return { jump: v.slice(5).trim() }
28
+ return { jump: v }
29
+ }
30
+
31
+ /** 工作流快照 → 全局环节查找表 { linkId → { phaseIndex, linkIndex, phase, link } }(保持声明序)。 */
32
+ function indexLinks(snapshot) {
33
+ const phases = snapshot && Array.isArray(snapshot.phases) ? snapshot.phases : []
34
+ const map = new Map()
35
+ phases.forEach((ph, phaseIndex) => {
36
+ const links = ph && Array.isArray(ph.links) ? ph.links : []
37
+ links.forEach((link, linkIndex) => {
38
+ if (link && link.id) map.set(link.id, { phaseIndex, linkIndex, phase: ph, link })
39
+ })
40
+ })
41
+ return { phases, map }
42
+ }
43
+
44
+ /** on_success='next' 的下一个环节:同阶段下一个,否则下一阶段第一个;没有 → null(运行结束)。 */
45
+ function nextLinkAfter(snapshot, linkId) {
46
+ const { phases, map } = indexLinks(snapshot)
47
+ const cur = map.get(linkId)
48
+ if (!cur) return null
49
+ const phase = phases[cur.phaseIndex]
50
+ const links = Array.isArray(phase.links) ? phase.links : []
51
+ if (cur.linkIndex + 1 < links.length) return links[cur.linkIndex + 1].id
52
+ for (let pi = cur.phaseIndex + 1; pi < phases.length; pi++) {
53
+ const nextLinks = Array.isArray(phases[pi].links) ? phases[pi].links : []
54
+ if (nextLinks.length > 0 && nextLinks[0].id) return nextLinks[0].id
55
+ }
56
+ return null
57
+ }
58
+
59
+ /** 工作流快照的第一个环节 id;没有环节 → null。 */
60
+ function firstLinkId(snapshot) {
61
+ const { phases } = indexLinks(snapshot)
62
+ for (const ph of phases) {
63
+ const links = Array.isArray(ph.links) ? ph.links : []
64
+ if (links.length > 0 && links[0].id) return links[0].id
65
+ }
66
+ return null
67
+ }
68
+
69
+ /**
70
+ * 运行名称 = 用户需求首行摘要(截断)。不带工作流名——同一工作流反复发起时工作流名
71
+ * 是相同前缀,反而让条目看起来都一样;需求为空才退回工作流名。
72
+ */
73
+ function deriveRunName({ displayName, processName, userInput }) {
74
+ const firstLine = String(userInput || '').trim().split('\n')[0].replace(/\s+/g, ' ').trim()
75
+ if (firstLine === '') return displayName || processName || 'run'
76
+ return firstLine.length > 24 ? firstLine.slice(0, 24) + '…' : firstLine
77
+ }
78
+
79
+ /** 环节执行记录(trail 条目)。 */
80
+ function makeAttempt(phaseId, linkId, attempt, extra) {
81
+ return Object.assign({
82
+ phaseId, linkId, attempt,
83
+ status: 'running', // running | done | gate_failed | skipped | failed | abandoned
84
+ sessionId: undefined,
85
+ outputTail: '',
86
+ gate: undefined, // { name, score, minScore, passed, reason }(最严门)
87
+ error: undefined,
88
+ startedAt: Date.now(),
89
+ finishedAt: undefined,
90
+ }, extra || {})
91
+ }
92
+
93
+ /** 摘要(列表行)。 */
94
+ function runSummary(run) {
95
+ const done = run.trail.filter((t) => t.status === 'done').length
96
+ const total = new Set([...indexLinks(run.snapshot).map.keys()]).size
97
+ // 左列表要能一眼区分同名工作流的不同运行:带出需求首行(压成单行、截断)
98
+ const request = String(run.userInput || '').replace(/\s+/g, ' ').trim().slice(0, 60)
99
+ return {
100
+ id: run.id, processId: run.processId, processName: run.processName, displayName: run.displayName,
101
+ runName: run.runName || run.displayName || run.processName,
102
+ request,
103
+ status: run.status, doneLinks: done, totalLinks: total, stepCount: run.stepCount,
104
+ usage: run.usage,
105
+ current: run.current ? { linkId: run.current.linkId, attempt: run.current.attempt } : null,
106
+ pendingBreak: !!run.pendingBreak,
107
+ workspaceId: run.workspaceId, error: run.error,
108
+ createdAt: run.createdAt, updatedAt: run.updatedAt, finishedAt: run.finishedAt,
109
+ }
110
+ }
111
+
112
+ /** 运行台账。 */
113
+ class RunStore {
114
+ /**
115
+ * @param {() => string} file 台账文件路径
116
+ * @param {{info?: Function, warn?: Function}} logger
117
+ */
118
+ constructor(file, logger) {
119
+ this.file = file
120
+ this.logger = logger || console
121
+ this.revision = 0
122
+ this.runs = new Map() // id → run
123
+ this.listeners = new Set()
124
+ this.loaded = false
125
+ this.persistTimer = null
126
+ this.persistChain = Promise.resolve()
127
+ }
128
+
129
+ subscribe(fn) { this.listeners.add(fn); return () => { this.listeners.delete(fn) } }
130
+ emit() { for (const fn of this.listeners) { try { fn(this.revision) } catch {} } }
131
+ touch(run) { run.updatedAt = Date.now(); this.revision += 1; this.schedulePersist(); this.emit() }
132
+
133
+ async load() {
134
+ let data
135
+ try { data = JSON.parse(await fsP.readFile(this.file, 'utf8')) } catch { data = null }
136
+ this.runs = new Map()
137
+ if (data && Array.isArray(data.runs)) {
138
+ for (const run of data.runs) this.runs.set(run.id, run)
139
+ }
140
+ // 宿主重启恢复:running/awaiting 的在飞状态不可信 → paused
141
+ for (const run of this.runs.values()) {
142
+ if (run.status === 'running') {
143
+ run.status = 'paused'
144
+ run.error = '宿主重启,运行已中断(可继续)'
145
+ if (run.current) {
146
+ const cur = [...run.trail].reverse().find((t) => t.status === 'running')
147
+ if (cur) cur.status = 'abandoned'
148
+ }
149
+ }
150
+ }
151
+ this.loaded = true
152
+ this.revision += 1
153
+ this.emit()
154
+ }
155
+
156
+ schedulePersist() {
157
+ if (this.persistTimer) return
158
+ this.persistTimer = setTimeout(() => {
159
+ this.persistTimer = null
160
+ this.persistChain = this.persistChain.then(() => this.persistNow()).catch((e) => {
161
+ this.logger.warn && this.logger.warn(`dsh-workflow-execute: runs persist: ${e && e.message}`)
162
+ })
163
+ }, PERSIST_DEBOUNCE_MS)
164
+ if (typeof this.persistTimer.unref === 'function') this.persistTimer.unref()
165
+ }
166
+
167
+ async persistNow() {
168
+ const runs = [...this.runs.values()]
169
+ .sort((a, b) => b.createdAt - a.createdAt)
170
+ .slice(0, MAX_RUNS_KEPT)
171
+ const keep = new Set(runs.map((r) => r.id))
172
+ for (const id of [...this.runs.keys()]) if (!keep.has(id)) this.runs.delete(id)
173
+ await fsP.mkdir(path.dirname(this.file), { recursive: true })
174
+ const tmp = `${this.file}.tmp-${process.pid}-${Math.random().toString(36).slice(2, 8)}`
175
+ await fsP.writeFile(tmp, JSON.stringify({ version: 1, runs }, null, 1), 'utf8')
176
+ await fsP.rename(tmp, this.file)
177
+ }
178
+
179
+ create({ processId, processName, displayName, snapshotYaml, snapshot, workspaceId, model, provider, userInput, sessionMode }) {
180
+ const run = {
181
+ id: 'run-' + randomUUID().slice(0, 8),
182
+ processId, processName, displayName,
183
+ runName: deriveRunName({ displayName, processName, userInput }),
184
+ snapshotYaml, snapshot,
185
+ workspaceId: workspaceId || undefined,
186
+ sessionMode: sessionMode === 'multi' ? 'multi' : 'single',
187
+ userInput: typeof userInput === 'string' && userInput.trim() !== '' ? userInput.trim().slice(0, 4000) : undefined,
188
+ model: model || undefined, provider: provider || undefined,
189
+ status: 'queued', // queued|running|paused|awaiting|done|stopped|failed
190
+ trail: [],
191
+ usage: { inputTokens: 0, outputTokens: 0, totalTokens: 0, calls: 0 }, // 全运行 token 合计(环节 + 门禁评审)
192
+ stepCount: 0,
193
+ rework: {}, // linkId → gate-fail 重试次数
194
+ current: null, // { phaseId, linkId, attempt }
195
+ pendingBreak: null, // { linkId, gate, attempt, reason }
196
+ limits: {
197
+ maxStepExecutions: snapshot && snapshot.limits && Number.isInteger(snapshot.limits.max_step_executions)
198
+ ? snapshot.limits.max_step_executions : null,
199
+ },
200
+ error: undefined,
201
+ createdAt: Date.now(), updatedAt: Date.now(), finishedAt: undefined,
202
+ }
203
+ this.runs.set(run.id, run)
204
+ this.touch(run)
205
+ return run
206
+ }
207
+
208
+ get(id) { return this.runs.get(String(id)) }
209
+ list() { return [...this.runs.values()].sort((a, b) => b.createdAt - a.createdAt) }
210
+ byStatus(...statuses) { return this.list().filter((r) => statuses.includes(r.status)) }
211
+
212
+ /** 删除一条运行记录(运行中的不允许删)。 */
213
+ async remove(id) {
214
+ const run = this.get(id)
215
+ if (!run) throw new StoreError('not_found', '运行不存在')
216
+ if (['queued', 'running', 'paused', 'awaiting'].includes(run.status)) {
217
+ throw new StoreError('invalid_input', '运行还没结束,先停止再删除')
218
+ }
219
+ this.runs.delete(run.id)
220
+ await this.persistNow()
221
+ this.revision += 1
222
+ this.emit()
223
+ return { id: run.id }
224
+ }
225
+
226
+ /** 面向 runner 的运行详情(含完整 trail)。 */
227
+ detail(id) {
228
+ const run = this.get(id)
229
+ if (!run) return undefined
230
+ return { ...run, summary: runSummary(run) }
231
+ }
232
+ }
233
+
234
+ module.exports = {
235
+ RunStore, runSummary, makeAttempt, parseFlow, indexLinks, nextLinkAfter, firstLinkId, deriveRunName,
236
+ FLOW_KEYWORDS, MAX_RUNS_KEPT,
237
+ }