-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathengine.ts
More file actions
365 lines (325 loc) · 11 KB
/
Copy pathengine.ts
File metadata and controls
365 lines (325 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
/**
* Workflow execution engine.
*
* Pure functions that run workflow pipelines. The engine itself performs
* no I/O — mutations are returned in results and the caller decides
* whether to apply them (via the `onMutations` callback).
*
* Uses `@eserstack/functions/task` for lazy composition, timeout handling,
* and result-based error recovery.
*
* ```
* runWorkflow(workflow, registry, options)
* │
* ├─ For each step:
* │ ├─ resolveStep() → { name, options, continueOnError, timeout }
* │ ├─ Lookup tool in registry
* │ ├─ Merge step options + run options
* │ ├─ Run tool via task.withTimeout()
* │ ├─ On error: if continueOnError → failed step; else propagate error
* │ ├─ Call onMutations() if mutations produced
* │ └─ Call onStepEnd()
* │
* └─ Return aggregate WorkflowResult
*
* resolveIncludes(workflow, allWorkflows)
* ├─ For each included id, find in allWorkflows
* ├─ Prepend their steps to current workflow's steps
* ├─ Track visited ids → detect circular includes → throw
* └─ Return flattened WorkflowDefinition
* ```
*
* @module
*/
import * as task from "@eserstack/functions/task";
import * as results from "@eserstack/primitives/results";
import type { Registry } from "./registry.ts";
import type {
ResolvedStep,
RunOptions,
StepResult,
WorkflowDefinition,
WorkflowError,
WorkflowResult,
WorkflowsConfig,
WorkflowStepConfig,
WorkflowToolResult,
} from "./types.ts";
/**
* Create a WorkflowError value.
*/
const workflowError = (message: string): WorkflowError => ({
_tag: "WorkflowError",
message,
});
/**
* Parse a step config into a resolved step with name, options, and engine directives.
*
* Engine directives (`continueOnError`, `timeout`) are extracted from the options
* and placed on the ResolvedStep. They are NOT passed to the tool.
*
* - `"fix-eof"` → `{ name: "fix-eof", options: {}, continueOnError: false }`
* - `{ "check-json": { exclude: ["x"], timeout: 120 } }` → timeout extracted, options clean
*/
export const resolveStep = (step: WorkflowStepConfig): ResolvedStep => {
if (typeof step === "string") {
return { name: step, options: {}, continueOnError: false };
}
const entries = Object.entries(step);
if (entries.length !== 1) {
throw new Error(
`Invalid step config: expected exactly one key, got ${entries.length}`,
);
}
const [name, rawOptions] = entries[0]!;
const options: Record<string, unknown> = {};
let continueOnError = false;
let timeout: number | undefined;
for (const [key, value] of Object.entries(rawOptions)) {
if (key === "continueOnError") {
continueOnError = value === true;
} else if (key === "timeout") {
// YAML timeout is in seconds, engine uses milliseconds
timeout = (value as number) * 1000;
} else {
options[key] = value;
}
}
return { name, options, continueOnError, timeout };
};
/**
* Create a Task that runs a single tool step with timeout.
* Returns a Task<WorkflowToolResult, WorkflowError, void>.
*/
const createStepTask = (
toolRun: () => Promise<WorkflowToolResult>,
timeoutMs: number,
stepName: string,
): task.Task<WorkflowToolResult, WorkflowError> =>
task.withTimeout(
task.fromPromise(
toolRun,
(error) =>
workflowError(
error instanceof Error ? error.message : String(error),
),
),
timeoutMs,
workflowError(
`Step '${stepName}' timed out after ${(timeoutMs / 1000).toFixed(0)}s`,
),
);
/**
* Run a single workflow definition against a registry of tools.
*
* Returns a `Task<WorkflowResult, WorkflowError>` — the computation is
* lazy and does not execute until `task.runTask()` is called.
*
* @param workflow - Workflow to run
* @param registry - Tool registry
* @param options - Run options
* @returns Task wrapping the workflow execution
*/
export const runWorkflow = (
workflow: WorkflowDefinition,
registry: Registry,
options: RunOptions = {},
): task.Task<WorkflowResult, WorkflowError> =>
task.task<WorkflowResult, WorkflowError>(async () => {
const startTime = performance.now();
const stepResults: StepResult[] = [];
const defaultTimeout = options.defaultTimeout ?? 60_000;
for (const stepConfig of workflow.steps) {
const resolved = resolveStep(stepConfig);
// Filter by --only flag
if (options.only !== undefined && resolved.name !== options.only) {
continue;
}
// Resolve tool from registry
const tool = registry.get(resolved.name);
if (tool === undefined) {
return results.fail(
workflowError(
`Unknown tool '${resolved.name}' in workflow '${workflow.id}'. ` +
`Registered tools: ${registry.names().join(", ") || "(none)"}`,
),
);
}
// Merge step-level options with run-level options (engine directives already stripped)
const mergedOptions: Record<string, unknown> = {
...resolved.options,
root: options.root ?? ".",
fix: options.fix ?? false,
_args: options.args ?? [],
};
// Pass changed files for incremental mode
if (options.changedFiles !== undefined) {
mergedOptions["_changedFiles"] = options.changedFiles;
}
// Notify step start
options.onStepStart?.(resolved.name);
const stepStart = performance.now();
const timeoutMs = resolved.timeout ?? defaultTimeout;
const stepTask = createStepTask(
() => tool.run(mergedOptions),
timeoutMs,
resolved.name,
);
const stepTaskResult = await task.runTask(stepTask);
let toolResult: WorkflowToolResult;
if (results.isOk(stepTaskResult)) {
toolResult = stepTaskResult.value;
} else if (resolved.continueOnError) {
// Create a failed step result from the error
toolResult = {
name: resolved.name,
passed: false,
issues: [{ message: stepTaskResult.error.message }],
mutations: [],
stats: {},
};
} else {
return stepTaskResult;
}
const durationMs = performance.now() - stepStart;
const stepResult: StepResult = {
...toolResult,
durationMs,
};
stepResults.push(stepResult);
// Apply mutations between steps so subsequent tools see the fixed state
if (
toolResult.mutations.length > 0 && options.onMutations !== undefined
) {
await options.onMutations(toolResult.mutations);
}
// Notify step end
options.onStepEnd?.(stepResult);
}
const totalDurationMs = performance.now() - startTime;
return results.ok({
workflowId: workflow.id,
passed: stepResults.every((s) => s.passed),
steps: stepResults,
totalDurationMs,
});
});
/**
* Resolve includes by flattening included workflow steps before the current workflow's steps.
* Detects circular includes.
*
* @param workflow - Workflow with potential includes
* @param allWorkflows - All available workflow definitions
* @param visited - Set of visited workflow ids (for cycle detection)
* @returns A new WorkflowDefinition with includes resolved (steps flattened)
*/
export const resolveIncludes = (
workflow: WorkflowDefinition,
allWorkflows: readonly WorkflowDefinition[],
visited: ReadonlySet<string> = new Set(),
): WorkflowDefinition => {
if (
workflow.includes === undefined || workflow.includes.length === 0
) {
return workflow;
}
const currentVisited = new Set(visited);
currentVisited.add(workflow.id);
const prependedSteps: WorkflowStepConfig[] = [];
for (const includeId of workflow.includes) {
if (currentVisited.has(includeId)) {
throw new Error(
`Circular include detected: workflow '${workflow.id}' includes '${includeId}' ` +
`which is already in the include chain: ${
[...currentVisited].join(" → ")
}`,
);
}
const included = allWorkflows.find((w) => w.id === includeId);
if (included === undefined) {
throw new Error(
`Workflow '${workflow.id}' includes '${includeId}' but no workflow with that id exists. ` +
`Available: ${allWorkflows.map((w) => w.id).join(", ") || "(none)"}`,
);
}
// Recursively resolve includes of the included workflow
const resolved = resolveIncludes(included, allWorkflows, currentVisited);
prependedSteps.push(...resolved.steps);
}
return {
...workflow,
steps: [...prependedSteps, ...workflow.steps],
includes: undefined, // Resolved — no longer needed
};
};
/**
* Run a workflow by id from a full config, resolving includes.
*
* @param workflowId - Workflow id to find and run
* @param config - Full workflow configuration
* @param registry - Tool registry
* @param options - Run options
* @returns Task wrapping the workflow execution
*/
export const runWorkflowWithConfig = (
workflowId: string,
config: WorkflowsConfig,
registry: Registry,
options: RunOptions = {},
): task.Task<WorkflowResult, WorkflowError> =>
task.task<WorkflowResult, WorkflowError>(async () => {
const workflow = config.workflows.find((w) => w.id === workflowId);
if (workflow === undefined) {
return results.fail(
workflowError(
`Workflow '${workflowId}' not found. ` +
`Available: ${
config.workflows.map((w) => w.id).join(", ") || "(none)"
}`,
),
);
}
const resolved = resolveIncludes(workflow, config.workflows);
return await task.runTask(runWorkflow(resolved, registry, options));
});
/**
* Run all workflows matching a given event, resolving includes.
*
* @param event - Event name to match (e.g., "precommit")
* @param workflows - Available workflow definitions
* @param registry - Tool registry
* @param options - Run options
* @returns Task wrapping an array of workflow results
*/
export const runByEvent = (
event: string,
workflows: readonly WorkflowDefinition[],
registry: Registry,
options: RunOptions = {},
): task.Task<readonly WorkflowResult[], WorkflowError> =>
task.task<readonly WorkflowResult[], WorkflowError>(async () => {
const matching = workflows.filter((w) => w.on.includes(event));
if (matching.length === 0) {
return results.fail(
workflowError(
`No workflows found for event '${event}'. ` +
`Available: ${
workflows.map((w) => `${w.id} (${w.on.join(", ")})`).join(
"; ",
) || "(none)"
}`,
),
);
}
const workflowResults: WorkflowResult[] = [];
for (const workflow of matching) {
const resolved = resolveIncludes(workflow, workflows);
const result = await task.runTask(
runWorkflow(resolved, registry, options),
);
if (results.isFail(result)) return result;
workflowResults.push(result.value);
}
return results.ok(workflowResults);
});