@calmo/task-runner 4.0.4 → 4.2.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/.github/workflows/codeql.yml +1 -1
- package/.github/workflows/release-please.yml +2 -2
- package/.jules/nexus.md +10 -0
- package/.release-please-manifest.json +1 -1
- package/AGENTS.md +3 -0
- package/CHANGELOG.md +46 -0
- package/CODE_OF_CONDUCT.md +131 -0
- package/CONTRIBUTING.md +89 -0
- package/README.md +34 -0
- package/conductor/code_styleguides/general.md +23 -0
- package/conductor/code_styleguides/javascript.md +51 -0
- package/conductor/code_styleguides/typescript.md +43 -0
- package/conductor/product-guidelines.md +14 -0
- package/conductor/product.md +16 -0
- package/conductor/setup_state.json +1 -0
- package/conductor/tech-stack.md +19 -0
- package/conductor/workflow.md +334 -0
- package/dist/EventBus.js +16 -16
- package/dist/EventBus.js.map +1 -1
- package/dist/PluginManager.d.ts +22 -0
- package/dist/PluginManager.js +39 -0
- package/dist/PluginManager.js.map +1 -0
- package/dist/TaskGraphValidator.d.ts +1 -1
- package/dist/TaskGraphValidator.js +16 -21
- package/dist/TaskGraphValidator.js.map +1 -1
- package/dist/TaskResult.d.ts +9 -0
- package/dist/TaskRunner.d.ts +8 -1
- package/dist/TaskRunner.js +64 -41
- package/dist/TaskRunner.js.map +1 -1
- package/dist/TaskStateManager.d.ts +22 -6
- package/dist/TaskStateManager.js +105 -45
- package/dist/TaskStateManager.js.map +1 -1
- package/dist/WorkflowExecutor.js +36 -20
- package/dist/WorkflowExecutor.js.map +1 -1
- package/dist/contracts/Plugin.d.ts +30 -0
- package/dist/contracts/Plugin.js +2 -0
- package/dist/contracts/Plugin.js.map +1 -0
- package/dist/strategies/DryRunExecutionStrategy.d.ts +1 -1
- package/dist/strategies/DryRunExecutionStrategy.js +2 -4
- package/dist/strategies/DryRunExecutionStrategy.js.map +1 -1
- package/dist/utils/PriorityQueue.d.ts +13 -0
- package/dist/utils/PriorityQueue.js +82 -0
- package/dist/utils/PriorityQueue.js.map +1 -0
- package/openspec/changes/add-middleware-support/proposal.md +19 -0
- package/openspec/changes/add-middleware-support/specs/task-runner/spec.md +34 -0
- package/openspec/changes/add-middleware-support/tasks.md +9 -0
- package/openspec/changes/add-resource-concurrency/proposal.md +18 -0
- package/openspec/changes/add-resource-concurrency/specs/task-runner/spec.md +25 -0
- package/openspec/changes/add-resource-concurrency/tasks.md +10 -0
- package/openspec/changes/allow-plugin-hooks/design.md +51 -0
- package/openspec/changes/allow-plugin-hooks/proposal.md +12 -0
- package/openspec/changes/allow-plugin-hooks/specs/post-task/spec.md +21 -0
- package/openspec/changes/allow-plugin-hooks/specs/pre-task/spec.md +32 -0
- package/openspec/changes/allow-plugin-hooks/tasks.md +7 -0
- package/openspec/changes/{feat-task-metrics → archive/2026-01-22-feat-task-metrics}/proposal.md +1 -1
- package/openspec/changes/archive/2026-01-22-feat-task-metrics/tasks.md +6 -0
- package/openspec/changes/archive/2026-02-15-implement-plugin-system/design.md +45 -0
- package/openspec/changes/archive/2026-02-15-implement-plugin-system/proposal.md +13 -0
- package/openspec/changes/archive/2026-02-15-implement-plugin-system/specs/plugin-context/spec.md +17 -0
- package/openspec/changes/archive/2026-02-15-implement-plugin-system/specs/plugin-loading/spec.md +27 -0
- package/openspec/changes/archive/2026-02-15-implement-plugin-system/tasks.md +7 -0
- package/openspec/changes/feat-completion-dependencies/proposal.md +58 -0
- package/openspec/changes/feat-completion-dependencies/tasks.md +46 -0
- package/openspec/changes/feat-conditional-retries/proposal.md +18 -0
- package/openspec/changes/feat-conditional-retries/specs/task-runner/spec.md +23 -0
- package/openspec/changes/feat-conditional-retries/tasks.md +37 -0
- package/openspec/changes/feat-state-persistence/specs/task-runner/spec.md +47 -0
- package/openspec/changes/feat-task-loop/proposal.md +22 -0
- package/openspec/changes/feat-task-loop/specs/task-runner/spec.md +34 -0
- package/openspec/changes/feat-task-loop/tasks.md +8 -0
- package/openspec/specs/plugin-context/spec.md +19 -0
- package/openspec/specs/plugin-loading/spec.md +29 -0
- package/openspec/specs/release-pr/spec.md +31 -0
- package/openspec/specs/task-runner/spec.md +12 -0
- package/package.json +1 -1
- package/src/EventBus.ts +7 -8
- package/src/PluginManager.ts +41 -0
- package/src/TaskGraphValidator.ts +22 -24
- package/src/TaskResult.ts +9 -0
- package/src/TaskRunner.ts +78 -46
- package/src/TaskStateManager.ts +118 -46
- package/src/WorkflowExecutor.ts +45 -22
- package/src/contracts/Plugin.ts +32 -0
- package/src/strategies/DryRunExecutionStrategy.ts +2 -3
- package/src/utils/PriorityQueue.ts +101 -0
- package/openspec/changes/feat-task-metrics/tasks.md +0 -6
- /package/openspec/changes/{adopt-release-pr → archive/2026-01-22-adopt-release-pr}/design.md +0 -0
- /package/openspec/changes/{adopt-release-pr → archive/2026-01-22-adopt-release-pr}/proposal.md +0 -0
- /package/openspec/changes/{adopt-release-pr → archive/2026-01-22-adopt-release-pr}/specs/release-pr/spec.md +0 -0
- /package/openspec/changes/{adopt-release-pr → archive/2026-01-22-adopt-release-pr}/tasks.md +0 -0
- /package/openspec/changes/{feat-task-metrics → archive/2026-01-22-feat-task-metrics}/specs/001-generic-task-runner/spec.md +0 -0
package/dist/TaskRunner.js
CHANGED
|
@@ -5,6 +5,7 @@ import { TaskStateManager } from "./TaskStateManager.js";
|
|
|
5
5
|
import { TaskGraphValidationError } from "./TaskGraphValidationError.js";
|
|
6
6
|
import { StandardExecutionStrategy } from "./strategies/StandardExecutionStrategy.js";
|
|
7
7
|
import { RetryingExecutionStrategy } from "./strategies/RetryingExecutionStrategy.js";
|
|
8
|
+
import { PluginManager } from "./PluginManager.js";
|
|
8
9
|
import { DryRunExecutionStrategy } from "./strategies/DryRunExecutionStrategy.js";
|
|
9
10
|
/**
|
|
10
11
|
* The main class that orchestrates the execution of a list of tasks
|
|
@@ -16,11 +17,13 @@ export class TaskRunner {
|
|
|
16
17
|
eventBus = new EventBus();
|
|
17
18
|
validator = new TaskGraphValidator();
|
|
18
19
|
executionStrategy = new RetryingExecutionStrategy(new StandardExecutionStrategy());
|
|
20
|
+
pluginManager;
|
|
19
21
|
/**
|
|
20
22
|
* @param context The shared context object to be passed to each task.
|
|
21
23
|
*/
|
|
22
24
|
constructor(context) {
|
|
23
25
|
this.context = context;
|
|
26
|
+
this.pluginManager = new PluginManager({ events: this.eventBus });
|
|
24
27
|
}
|
|
25
28
|
/**
|
|
26
29
|
* Subscribe to an event.
|
|
@@ -36,16 +39,23 @@ export class TaskRunner {
|
|
|
36
39
|
* @param callback The callback to remove.
|
|
37
40
|
*/
|
|
38
41
|
off(event, callback) {
|
|
39
|
-
/* v8 ignore next 1 */
|
|
40
42
|
this.eventBus.off(event, callback);
|
|
41
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Registers a plugin.
|
|
46
|
+
* @param plugin The plugin to register.
|
|
47
|
+
* @returns The TaskRunner instance for chaining.
|
|
48
|
+
*/
|
|
49
|
+
use(plugin) {
|
|
50
|
+
this.pluginManager.use(plugin);
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
42
53
|
/**
|
|
43
54
|
* Sets the execution strategy to be used.
|
|
44
55
|
* @param strategy The execution strategy.
|
|
45
56
|
* @returns The TaskRunner instance for chaining.
|
|
46
57
|
*/
|
|
47
58
|
setExecutionStrategy(strategy) {
|
|
48
|
-
/* v8 ignore next 2 */
|
|
49
59
|
this.executionStrategy = strategy;
|
|
50
60
|
return this;
|
|
51
61
|
}
|
|
@@ -55,19 +65,54 @@ export class TaskRunner {
|
|
|
55
65
|
* @returns A string containing the Mermaid graph definition.
|
|
56
66
|
*/
|
|
57
67
|
static getMermaidGraph(steps) {
|
|
58
|
-
const graphLines = ["graph TD"];
|
|
59
|
-
const
|
|
68
|
+
const graphLines = new Set(["graph TD"]);
|
|
69
|
+
const idMap = new Map();
|
|
70
|
+
const usedIds = new Set();
|
|
71
|
+
const baseIdCounters = new Map();
|
|
72
|
+
const getUniqueId = (name) => {
|
|
73
|
+
const existingId = idMap.get(name);
|
|
74
|
+
if (existingId !== undefined) {
|
|
75
|
+
return existingId;
|
|
76
|
+
}
|
|
77
|
+
const sanitized = this.sanitizeMermaidId(name);
|
|
78
|
+
let uniqueId = sanitized;
|
|
79
|
+
// First check if the base sanitized ID is available
|
|
80
|
+
if (!usedIds.has(uniqueId)) {
|
|
81
|
+
usedIds.add(uniqueId);
|
|
82
|
+
idMap.set(name, uniqueId);
|
|
83
|
+
return uniqueId;
|
|
84
|
+
}
|
|
85
|
+
// If not, use the counter for this base ID
|
|
86
|
+
let counter = baseIdCounters.get(sanitized) || 1;
|
|
87
|
+
while (usedIds.has(uniqueId)) {
|
|
88
|
+
uniqueId = `${sanitized}_${counter}`;
|
|
89
|
+
counter++;
|
|
90
|
+
}
|
|
91
|
+
baseIdCounters.set(sanitized, counter);
|
|
92
|
+
usedIds.add(uniqueId);
|
|
93
|
+
idMap.set(name, uniqueId);
|
|
94
|
+
return uniqueId;
|
|
95
|
+
};
|
|
96
|
+
// We process nodes in input order to ensure deterministic ID generation
|
|
97
|
+
// (getUniqueId is called for each unique step name in order, populating the cache).
|
|
98
|
+
const processedNamesForNodes = new Set();
|
|
60
99
|
for (const step of steps) {
|
|
61
|
-
|
|
100
|
+
if (!processedNamesForNodes.has(step.name)) {
|
|
101
|
+
const stepId = getUniqueId(step.name);
|
|
102
|
+
graphLines.add(` ${stepId}[${JSON.stringify(step.name)}]`);
|
|
103
|
+
processedNamesForNodes.add(step.name);
|
|
104
|
+
}
|
|
62
105
|
}
|
|
63
106
|
for (const step of steps) {
|
|
64
107
|
if (step.dependencies) {
|
|
108
|
+
const stepId = getUniqueId(step.name);
|
|
65
109
|
for (const dep of step.dependencies) {
|
|
66
|
-
|
|
110
|
+
const depId = getUniqueId(dep);
|
|
111
|
+
graphLines.add(` ${depId} --> ${stepId}`);
|
|
67
112
|
}
|
|
68
113
|
}
|
|
69
114
|
}
|
|
70
|
-
return [...
|
|
115
|
+
return [...graphLines].join("\n");
|
|
71
116
|
}
|
|
72
117
|
/**
|
|
73
118
|
* Sanitizes a string for use as a Mermaid node ID.
|
|
@@ -86,6 +131,8 @@ export class TaskRunner {
|
|
|
86
131
|
* and values are the corresponding TaskResult objects.
|
|
87
132
|
*/
|
|
88
133
|
async execute(steps, config) {
|
|
134
|
+
// Initialize plugins
|
|
135
|
+
await this.pluginManager.initialize();
|
|
89
136
|
// Validate the task graph before execution
|
|
90
137
|
const taskGraph = {
|
|
91
138
|
tasks: steps.map((step) => ({
|
|
@@ -106,45 +153,21 @@ export class TaskRunner {
|
|
|
106
153
|
if (config?.timeout !== undefined) {
|
|
107
154
|
return this.executeWithTimeout(executor, steps, config.timeout, config.signal);
|
|
108
155
|
}
|
|
109
|
-
|
|
110
|
-
return executor.execute(steps, config?.signal);
|
|
111
|
-
}
|
|
156
|
+
return executor.execute(steps, config?.signal);
|
|
112
157
|
}
|
|
113
158
|
/**
|
|
114
159
|
* Executes tasks with a timeout, ensuring resources are cleaned up.
|
|
115
160
|
*/
|
|
116
161
|
async executeWithTimeout(executor, steps, timeout, signal) {
|
|
117
|
-
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
// If already aborted, use it directly (WorkflowExecutor handles early abort)
|
|
127
|
-
// We can cancel timeout immediately
|
|
128
|
-
clearTimeout(timeoutId);
|
|
129
|
-
effectiveSignal = signal;
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
// Listen to user signal to abort our controller
|
|
133
|
-
onAbort = () => {
|
|
134
|
-
controller.abort(signal.reason);
|
|
135
|
-
};
|
|
136
|
-
signal.addEventListener("abort", onAbort);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
try {
|
|
140
|
-
return await executor.execute(steps, effectiveSignal);
|
|
141
|
-
}
|
|
142
|
-
finally {
|
|
143
|
-
clearTimeout(timeoutId);
|
|
144
|
-
if (signal && onAbort) {
|
|
145
|
-
signal.removeEventListener("abort", onAbort);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
162
|
+
// Create a timeout signal that aborts after the specified time
|
|
163
|
+
const timeoutSignal = AbortSignal.timeout(timeout);
|
|
164
|
+
// If there's a user-provided signal, combine them.
|
|
165
|
+
// Otherwise, use the timeout signal directly.
|
|
166
|
+
const effectiveSignal = signal
|
|
167
|
+
? AbortSignal.any([signal, timeoutSignal])
|
|
168
|
+
: timeoutSignal;
|
|
169
|
+
return executor.execute(steps, effectiveSignal);
|
|
170
|
+
// No explicit clean up needed for AbortSignal.timeout as it is handled by the platform
|
|
148
171
|
}
|
|
149
172
|
}
|
|
150
173
|
//# sourceMappingURL=TaskRunner.js.map
|
package/dist/TaskRunner.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TaskRunner.js","sourceRoot":"","sources":["../src/TaskRunner.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAM7D,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAC;AAEzE,OAAO,EAAE,yBAAyB,EAAE,MAAM,2CAA2C,CAAC;AACtF,OAAO,EAAE,yBAAyB,EAAE,MAAM,2CAA2C,CAAC;
|
|
1
|
+
{"version":3,"file":"TaskRunner.js","sourceRoot":"","sources":["../src/TaskRunner.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAM7D,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,wBA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MAAM,aAAa,GAAG,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAEnD,mDAAmD;QACnD,8CAA8C;QAC9C,MAAM,eAAe,GAAG,MAAM;YAC5B,CAAC,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;YAC1C,CAAC,CAAC,aAAa,CAAC;QAElB,OAAO,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,eAAe,CAAC,CAAC;QAChD,uFAAuF;IACzF,CAAC;CACF"}
|
|
@@ -10,6 +10,9 @@ export declare class TaskStateManager<TContext> {
|
|
|
10
10
|
private results;
|
|
11
11
|
private pendingSteps;
|
|
12
12
|
private running;
|
|
13
|
+
private dependencyGraph;
|
|
14
|
+
private dependencyCounts;
|
|
15
|
+
private readyQueue;
|
|
13
16
|
constructor(eventBus: EventBus<TContext>);
|
|
14
17
|
/**
|
|
15
18
|
* Initializes the state with the given steps.
|
|
@@ -17,8 +20,8 @@ export declare class TaskStateManager<TContext> {
|
|
|
17
20
|
*/
|
|
18
21
|
initialize(steps: TaskStep<TContext>[]): void;
|
|
19
22
|
/**
|
|
20
|
-
* Processes the pending steps to identify tasks that can be started
|
|
21
|
-
* Emits `taskSkipped` for skipped tasks.
|
|
23
|
+
* Processes the pending steps to identify tasks that can be started.
|
|
24
|
+
* Emits `taskSkipped` for skipped tasks (handled during cascade).
|
|
22
25
|
* @returns An array of tasks that are ready to run.
|
|
23
26
|
*/
|
|
24
27
|
processDependencies(): TaskStep<TContext>[];
|
|
@@ -34,6 +37,17 @@ export declare class TaskStateManager<TContext> {
|
|
|
34
37
|
* @param result The result of the task.
|
|
35
38
|
*/
|
|
36
39
|
markCompleted(step: TaskStep<TContext>, result: TaskResult): void;
|
|
40
|
+
/**
|
|
41
|
+
* Marks a task as skipped and emits `taskSkipped`.
|
|
42
|
+
* @param step The task that was skipped.
|
|
43
|
+
* @param result The result object (status: skipped).
|
|
44
|
+
*/
|
|
45
|
+
markSkipped(step: TaskStep<TContext>, result: TaskResult): void;
|
|
46
|
+
/**
|
|
47
|
+
* Internal method to mark skipped without triggering cascade (to be used inside cascade loop).
|
|
48
|
+
* Returns true if the task was actually marked skipped (was not already finished).
|
|
49
|
+
*/
|
|
50
|
+
private internalMarkSkipped;
|
|
37
51
|
/**
|
|
38
52
|
* Cancels all pending tasks that haven't started yet.
|
|
39
53
|
* @param message The cancellation message.
|
|
@@ -52,9 +66,11 @@ export declare class TaskStateManager<TContext> {
|
|
|
52
66
|
*/
|
|
53
67
|
hasPendingTasks(): boolean;
|
|
54
68
|
/**
|
|
55
|
-
*
|
|
56
|
-
* @param step The task that was skipped.
|
|
57
|
-
* @param result The result object (status: skipped).
|
|
69
|
+
* Handles successful completion of a task by updating dependents.
|
|
58
70
|
*/
|
|
59
|
-
|
|
71
|
+
private handleSuccess;
|
|
72
|
+
/**
|
|
73
|
+
* Cascades failure/skipping to dependents.
|
|
74
|
+
*/
|
|
75
|
+
private cascadeFailure;
|
|
60
76
|
}
|
package/dist/TaskStateManager.js
CHANGED
|
@@ -7,6 +7,10 @@ export class TaskStateManager {
|
|
|
7
7
|
results = new Map();
|
|
8
8
|
pendingSteps = new Set();
|
|
9
9
|
running = new Set();
|
|
10
|
+
// Optimization structures
|
|
11
|
+
dependencyGraph = new Map();
|
|
12
|
+
dependencyCounts = new Map();
|
|
13
|
+
readyQueue = [];
|
|
10
14
|
constructor(eventBus) {
|
|
11
15
|
this.eventBus = eventBus;
|
|
12
16
|
}
|
|
@@ -18,47 +22,37 @@ export class TaskStateManager {
|
|
|
18
22
|
this.pendingSteps = new Set(steps);
|
|
19
23
|
this.results.clear();
|
|
20
24
|
this.running.clear();
|
|
25
|
+
this.readyQueue = [];
|
|
26
|
+
this.dependencyGraph.clear();
|
|
27
|
+
this.dependencyCounts.clear();
|
|
28
|
+
for (const step of steps) {
|
|
29
|
+
const deps = step.dependencies ?? [];
|
|
30
|
+
this.dependencyCounts.set(step.name, deps.length);
|
|
31
|
+
if (deps.length === 0) {
|
|
32
|
+
this.readyQueue.push(step);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
for (const dep of deps) {
|
|
36
|
+
let dependents = this.dependencyGraph.get(dep);
|
|
37
|
+
if (dependents === undefined) {
|
|
38
|
+
dependents = [];
|
|
39
|
+
this.dependencyGraph.set(dep, dependents);
|
|
40
|
+
}
|
|
41
|
+
dependents.push(step);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
21
45
|
}
|
|
22
46
|
/**
|
|
23
|
-
* Processes the pending steps to identify tasks that can be started
|
|
24
|
-
* Emits `taskSkipped` for skipped tasks.
|
|
47
|
+
* Processes the pending steps to identify tasks that can be started.
|
|
48
|
+
* Emits `taskSkipped` for skipped tasks (handled during cascade).
|
|
25
49
|
* @returns An array of tasks that are ready to run.
|
|
26
50
|
*/
|
|
27
51
|
processDependencies() {
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
let blocked = false;
|
|
33
|
-
let failedDep;
|
|
34
|
-
for (const dep of deps) {
|
|
35
|
-
const depResult = this.results.get(dep);
|
|
36
|
-
if (!depResult) {
|
|
37
|
-
// Dependency not finished yet
|
|
38
|
-
blocked = true;
|
|
39
|
-
}
|
|
40
|
-
else if (depResult.status !== "success") {
|
|
41
|
-
failedDep = dep;
|
|
42
|
-
break;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
if (failedDep) {
|
|
46
|
-
const depResult = this.results.get(failedDep);
|
|
47
|
-
const depError = depResult?.error ? `: ${depResult.error}` : "";
|
|
48
|
-
const result = {
|
|
49
|
-
status: "skipped",
|
|
50
|
-
message: `Skipped because dependency '${failedDep}' failed${depError}`,
|
|
51
|
-
};
|
|
52
|
-
this.markSkipped(step, result);
|
|
53
|
-
toRemove.push(step);
|
|
54
|
-
}
|
|
55
|
-
else if (!blocked) {
|
|
56
|
-
toRun.push(step);
|
|
57
|
-
toRemove.push(step);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
// Cleanup pending set
|
|
61
|
-
for (const step of toRemove) {
|
|
52
|
+
const toRun = [...this.readyQueue];
|
|
53
|
+
this.readyQueue = [];
|
|
54
|
+
// Remove them from pendingSteps as they are now handed off to the executor
|
|
55
|
+
for (const step of toRun) {
|
|
62
56
|
this.pendingSteps.delete(step);
|
|
63
57
|
}
|
|
64
58
|
return toRun;
|
|
@@ -81,16 +75,45 @@ export class TaskStateManager {
|
|
|
81
75
|
this.running.delete(step.name);
|
|
82
76
|
this.results.set(step.name, result);
|
|
83
77
|
this.eventBus.emit("taskEnd", { step, result });
|
|
78
|
+
if (result.status === "success") {
|
|
79
|
+
this.handleSuccess(step.name);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
this.cascadeFailure(step.name);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Marks a task as skipped and emits `taskSkipped`.
|
|
87
|
+
* @param step The task that was skipped.
|
|
88
|
+
* @param result The result object (status: skipped).
|
|
89
|
+
*/
|
|
90
|
+
markSkipped(step, result) {
|
|
91
|
+
if (this.internalMarkSkipped(step, result)) {
|
|
92
|
+
this.cascadeFailure(step.name);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Internal method to mark skipped without triggering cascade (to be used inside cascade loop).
|
|
97
|
+
* Returns true if the task was actually marked skipped (was not already finished).
|
|
98
|
+
*/
|
|
99
|
+
internalMarkSkipped(step, result) {
|
|
100
|
+
if (this.results.has(step.name)) {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
this.running.delete(step.name);
|
|
104
|
+
this.results.set(step.name, result);
|
|
105
|
+
this.pendingSteps.delete(step);
|
|
106
|
+
this.eventBus.emit("taskSkipped", { step, result });
|
|
107
|
+
return true;
|
|
84
108
|
}
|
|
85
109
|
/**
|
|
86
110
|
* Cancels all pending tasks that haven't started yet.
|
|
87
111
|
* @param message The cancellation message.
|
|
88
112
|
*/
|
|
89
113
|
cancelAllPending(message) {
|
|
114
|
+
this.readyQueue = []; // Clear ready queue
|
|
90
115
|
// Iterate over pendingSteps to cancel them
|
|
91
116
|
for (const step of this.pendingSteps) {
|
|
92
|
-
// Also check running? No, running tasks are handled by AbortSignal in Executor.
|
|
93
|
-
// We only cancel what is pending and hasn't started.
|
|
94
117
|
if (!this.results.has(step.name) && !this.running.has(step.name)) {
|
|
95
118
|
const result = {
|
|
96
119
|
status: "cancelled",
|
|
@@ -122,14 +145,51 @@ export class TaskStateManager {
|
|
|
122
145
|
return this.pendingSteps.size > 0;
|
|
123
146
|
}
|
|
124
147
|
/**
|
|
125
|
-
*
|
|
126
|
-
* @param step The task that was skipped.
|
|
127
|
-
* @param result The result object (status: skipped).
|
|
148
|
+
* Handles successful completion of a task by updating dependents.
|
|
128
149
|
*/
|
|
129
|
-
|
|
130
|
-
this.
|
|
131
|
-
|
|
132
|
-
|
|
150
|
+
handleSuccess(stepName) {
|
|
151
|
+
const dependents = this.dependencyGraph.get(stepName);
|
|
152
|
+
if (!dependents)
|
|
153
|
+
return;
|
|
154
|
+
for (const dependent of dependents) {
|
|
155
|
+
const currentCount = this.dependencyCounts.get(dependent.name);
|
|
156
|
+
const newCount = currentCount - 1;
|
|
157
|
+
this.dependencyCounts.set(dependent.name, newCount);
|
|
158
|
+
if (newCount === 0) {
|
|
159
|
+
// Task is ready. Ensure it's still pending.
|
|
160
|
+
if (this.pendingSteps.has(dependent)) {
|
|
161
|
+
this.readyQueue.push(dependent);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Cascades failure/skipping to dependents.
|
|
168
|
+
*/
|
|
169
|
+
cascadeFailure(failedStepName) {
|
|
170
|
+
const queue = [failedStepName];
|
|
171
|
+
// Optimization: Use index pointer instead of shift() to avoid O(N) array re-indexing
|
|
172
|
+
let head = 0;
|
|
173
|
+
// Use a set to track visited nodes in this cascade pass to avoid redundant processing,
|
|
174
|
+
// although checking results.has() in internalMarkSkipped also prevents it.
|
|
175
|
+
while (head < queue.length) {
|
|
176
|
+
const currentName = queue[head++];
|
|
177
|
+
const dependents = this.dependencyGraph.get(currentName);
|
|
178
|
+
if (!dependents)
|
|
179
|
+
continue;
|
|
180
|
+
// Get the result of the failed/skipped dependency to propagate error info if available
|
|
181
|
+
const currentResult = this.results.get(currentName);
|
|
182
|
+
const depError = currentResult?.error ? `: ${currentResult.error}` : "";
|
|
183
|
+
for (const dependent of dependents) {
|
|
184
|
+
const result = {
|
|
185
|
+
status: "skipped",
|
|
186
|
+
message: `Skipped because dependency '${currentName}' failed${depError}`,
|
|
187
|
+
};
|
|
188
|
+
if (this.internalMarkSkipped(dependent, result)) {
|
|
189
|
+
queue.push(dependent.name);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
133
193
|
}
|
|
134
194
|
}
|
|
135
195
|
//# sourceMappingURL=TaskStateManager.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TaskStateManager.js","sourceRoot":"","sources":["../src/TaskStateManager.ts"],"names":[],"mappings":"AAIA;;;GAGG;AACH,MAAM,OAAO,gBAAgB;
|
|
1
|
+
{"version":3,"file":"TaskStateManager.js","sourceRoot":"","sources":["../src/TaskStateManager.ts"],"names":[],"mappings":"AAIA;;;GAGG;AACH,MAAM,OAAO,gBAAgB;IAUP;IATZ,OAAO,GAAG,IAAI,GAAG,EAAsB,CAAC;IACxC,YAAY,GAAG,IAAI,GAAG,EAAsB,CAAC;IAC7C,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;IAEpC,0BAA0B;IAClB,eAAe,GAAG,IAAI,GAAG,EAAgC,CAAC;IAC1D,gBAAgB,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC7C,UAAU,GAAyB,EAAE,CAAC;IAE9C,YAAoB,QAA4B;QAA5B,aAAQ,GAAR,QAAQ,CAAoB;IAAG,CAAC;IAEpD;;;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AC,GAAG,CAAC,WAAW,CAAC,CAAC;YACpD,MAAM,QAAQ,GAAG,aAAa,EAAE,KAAK,CAAC,CAAC,CAAC,KAAK,aAAa,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YAExE,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;gBACnC,MAAM,MAAM,GAAe;oBACzB,MAAM,EAAE,SAAS;oBACjB,OAAO,EAAE,+BAA+B,WAAW,WAAW,QAAQ,EAAE;iBACzE,CAAC;gBAEF,IAAI,IAAI,CAAC,mBAAmB,CAAC,SAAS,EAAE,MAAM,CAAC,EAAE,CAAC;oBAChD,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;gBAC7B,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
|
package/dist/WorkflowExecutor.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ExecutionConstants } from "./ExecutionConstants.js";
|
|
2
|
+
import { PriorityQueue } from "./utils/PriorityQueue.js";
|
|
2
3
|
/**
|
|
3
4
|
* Handles the execution of the workflow steps.
|
|
4
5
|
* @template TContext The shape of the shared context object.
|
|
@@ -9,7 +10,7 @@ export class WorkflowExecutor {
|
|
|
9
10
|
stateManager;
|
|
10
11
|
strategy;
|
|
11
12
|
concurrency;
|
|
12
|
-
readyQueue =
|
|
13
|
+
readyQueue = new PriorityQueue();
|
|
13
14
|
/**
|
|
14
15
|
* @param context The shared context object.
|
|
15
16
|
* @param eventBus The event bus to emit events.
|
|
@@ -49,8 +50,20 @@ export class WorkflowExecutor {
|
|
|
49
50
|
signal.addEventListener("abort", onAbort);
|
|
50
51
|
}
|
|
51
52
|
try {
|
|
53
|
+
// Create a signal mechanism to wait for any task completion
|
|
54
|
+
let signalResolver;
|
|
55
|
+
let signalPromise = new Promise((resolve) => {
|
|
56
|
+
signalResolver = resolve;
|
|
57
|
+
});
|
|
58
|
+
const signalWork = () => {
|
|
59
|
+
signalResolver();
|
|
60
|
+
// Reset the promise for the next wait
|
|
61
|
+
signalPromise = new Promise((resolve) => {
|
|
62
|
+
signalResolver = resolve;
|
|
63
|
+
});
|
|
64
|
+
};
|
|
52
65
|
// Initial pass
|
|
53
|
-
this.processLoop(executingPromises, signal);
|
|
66
|
+
this.processLoop(executingPromises, signalWork, signal);
|
|
54
67
|
while (this.stateManager.hasPendingTasks() ||
|
|
55
68
|
this.stateManager.hasRunningTasks() ||
|
|
56
69
|
executingPromises.size > 0) {
|
|
@@ -62,15 +75,11 @@ export class WorkflowExecutor {
|
|
|
62
75
|
}
|
|
63
76
|
else {
|
|
64
77
|
// Wait for the next task to finish
|
|
65
|
-
await
|
|
78
|
+
await signalPromise;
|
|
66
79
|
}
|
|
67
80
|
if (signal?.aborted) {
|
|
68
81
|
this.stateManager.cancelAllPending(ExecutionConstants.WORKFLOW_CANCELLED);
|
|
69
82
|
}
|
|
70
|
-
else {
|
|
71
|
-
// After a task finishes, check for new work
|
|
72
|
-
this.processLoop(executingPromises, signal);
|
|
73
|
-
}
|
|
74
83
|
}
|
|
75
84
|
// Ensure everything is accounted for (e.g. if loop exited early)
|
|
76
85
|
this.stateManager.cancelAllPending(ExecutionConstants.WORKFLOW_CANCELLED);
|
|
@@ -87,26 +96,25 @@ export class WorkflowExecutor {
|
|
|
87
96
|
/**
|
|
88
97
|
* Logic to identify tasks that can be started and run them.
|
|
89
98
|
*/
|
|
90
|
-
processLoop(executingPromises, signal) {
|
|
99
|
+
processLoop(executingPromises, signalWork, signal) {
|
|
91
100
|
const newlyReady = this.stateManager.processDependencies();
|
|
92
101
|
// Add newly ready tasks to the queue
|
|
93
102
|
for (const task of newlyReady) {
|
|
94
|
-
this.readyQueue.push(task);
|
|
103
|
+
this.readyQueue.push(task, task.priority ?? 0);
|
|
95
104
|
}
|
|
96
|
-
// Sort by priority (descending) once after adding new tasks
|
|
97
|
-
this.readyQueue.sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
|
|
98
105
|
// Execute ready tasks while respecting concurrency limit
|
|
99
|
-
while (this.readyQueue.
|
|
106
|
+
while (!this.readyQueue.isEmpty()) {
|
|
100
107
|
if (this.concurrency !== undefined &&
|
|
101
108
|
executingPromises.size >= this.concurrency) {
|
|
102
109
|
break;
|
|
103
110
|
}
|
|
104
|
-
const step = this.readyQueue.
|
|
105
|
-
const taskPromise = this.executeTaskStep(step, signal)
|
|
106
|
-
.finally(() => {
|
|
111
|
+
const step = this.readyQueue.pop();
|
|
112
|
+
const taskPromise = this.executeTaskStep(step, signal).finally(() => {
|
|
107
113
|
executingPromises.delete(taskPromise);
|
|
108
114
|
// When a task finishes, we try to run more
|
|
109
|
-
this.processLoop(executingPromises, signal);
|
|
115
|
+
this.processLoop(executingPromises, signalWork, signal);
|
|
116
|
+
// Signal that a task has completed
|
|
117
|
+
signalWork();
|
|
110
118
|
});
|
|
111
119
|
executingPromises.add(taskPromise);
|
|
112
120
|
}
|
|
@@ -155,18 +163,26 @@ export class WorkflowExecutor {
|
|
|
155
163
|
return;
|
|
156
164
|
}
|
|
157
165
|
this.stateManager.markRunning(step);
|
|
166
|
+
const startTime = performance.now();
|
|
167
|
+
let result;
|
|
158
168
|
try {
|
|
159
|
-
|
|
160
|
-
this.stateManager.markCompleted(step, result);
|
|
169
|
+
result = await this.strategy.execute(step, this.context, signal);
|
|
161
170
|
}
|
|
162
171
|
catch (error) {
|
|
163
|
-
|
|
172
|
+
result = {
|
|
164
173
|
status: "failure",
|
|
165
174
|
message: ExecutionConstants.EXECUTION_STRATEGY_FAILED,
|
|
166
175
|
error: error instanceof Error ? error.message : String(error),
|
|
167
176
|
};
|
|
168
|
-
this.stateManager.markCompleted(step, result);
|
|
169
177
|
}
|
|
178
|
+
const endTime = performance.now();
|
|
179
|
+
// Always inject metrics to ensure accuracy
|
|
180
|
+
result.metrics = {
|
|
181
|
+
startTime,
|
|
182
|
+
endTime,
|
|
183
|
+
duration: endTime - startTime,
|
|
184
|
+
};
|
|
185
|
+
this.stateManager.markCompleted(step, result);
|
|
170
186
|
}
|
|
171
187
|
}
|
|
172
188
|
//# sourceMappingURL=WorkflowExecutor.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WorkflowExecutor.js","sourceRoot":"","sources":["../src/WorkflowExecutor.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"WorkflowExecutor.js","sourceRoot":"","sources":["../src/WorkflowExecutor.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAEzD;;;GAGG;AACH,MAAM,OAAO,gBAAgB;IAWjB;IACA;IACA;IACA;IACA;IAdF,UAAU,GAAG,IAAI,aAAa,EAAsB,CAAC;IAE7D;;;;;;OAMG;IACH,YACU,OAAiB,EACjB,QAA4B,EAC5B,YAAwC,EACxC,QAAsC,EACtC,WAAoB;QAJpB,YAAO,GAAP,OAAO,CAAU;QACjB,aAAQ,GAAR,QAAQ,CAAoB;QAC5B,iBAAY,GAAZ,YAAY,CAA4B;QACxC,aAAQ,GAAR,QAAQ,CAA8B;QACtC,gBAAW,GAAX,WAAW,CAAS;IAC3B,CAAC;IAEJ;;;;;OAKG;IACH,KAAK,CAAC,OAAO,CACX,KAA2B,EAC3B,MAAoB;QAEpB,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,eAAe,EAAE,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;QACtE,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;QAEpC,2BAA2B;QAC3B,IAAI,MAAM,EAAE,OAAO,EAAE,CAAC;YACpB,IAAI,CAAC,YAAY,CAAC,gBAAgB,CAChC,kBAAkB,CAAC,sBAAsB,CAC1C,CAAC;YACF,MAAM,OAAO,GAAG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GAAG;YACf,SAAS;YACT,OAAO;YACP,QAAQ,EAAE,OAAO,GAAG,SAAS;SAC9B,CAAC;QAEF,IAAI,CAAC,YAAY,CAAC,aAAa,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAChD,CAAC;CACF"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { EventBus } from "../EventBus.js";
|
|
2
|
+
/**
|
|
3
|
+
* Context provided to a plugin during installation.
|
|
4
|
+
* exposes capabilities to hook into the runner lifecycle and context.
|
|
5
|
+
*/
|
|
6
|
+
export interface PluginContext<TContext> {
|
|
7
|
+
/**
|
|
8
|
+
* The event bus instance to subscribe to runner events.
|
|
9
|
+
*/
|
|
10
|
+
events: EventBus<TContext>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Interface that all plugins must implement.
|
|
14
|
+
*/
|
|
15
|
+
export interface Plugin<TContext> {
|
|
16
|
+
/**
|
|
17
|
+
* Unique name of the plugin.
|
|
18
|
+
*/
|
|
19
|
+
name: string;
|
|
20
|
+
/**
|
|
21
|
+
* Semantic version of the plugin.
|
|
22
|
+
*/
|
|
23
|
+
version: string;
|
|
24
|
+
/**
|
|
25
|
+
* Called when the plugin is installed into the runner.
|
|
26
|
+
* This is where the plugin should subscribe to events or perform setup.
|
|
27
|
+
* @param context The plugin context exposing runner capabilities.
|
|
28
|
+
*/
|
|
29
|
+
install(context: PluginContext<TContext>): void | Promise<void>;
|
|
30
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Plugin.js","sourceRoot":"","sources":["../../src/contracts/Plugin.ts"],"names":[],"mappings":""}
|