@calmo/task-runner 4.0.4 → 4.1.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 +5 -0
- package/.release-please-manifest.json +1 -1
- package/AGENTS.md +1 -0
- package/CHANGELOG.md +19 -0
- package/CODE_OF_CONDUCT.md +131 -0
- package/CONTRIBUTING.md +89 -0
- package/dist/TaskResult.d.ts +9 -0
- package/dist/TaskRunner.js +47 -34
- package/dist/TaskRunner.js.map +1 -1
- package/dist/TaskStateManager.d.ts +22 -6
- package/dist/TaskStateManager.js +101 -45
- package/dist/TaskStateManager.js.map +1 -1
- package/dist/WorkflowExecutor.js +17 -10
- package/dist/WorkflowExecutor.js.map +1 -1
- 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-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 +9 -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/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/specs/release-pr/spec.md +31 -0
- package/openspec/specs/task-runner/spec.md +12 -0
- package/package.json +1 -1
- package/src/TaskResult.ts +9 -0
- package/src/TaskRunner.ts +55 -36
- package/src/TaskStateManager.ts +114 -46
- package/src/WorkflowExecutor.ts +21 -11
- 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
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# Workflow State Persistence Specification
|
|
2
|
+
|
|
3
|
+
## Purpose
|
|
4
|
+
|
|
5
|
+
Enables the `TaskRunner` to save its execution state and resume from that state later, allowing for recovery from failures or pausing long-running workflows without re-executing completed tasks.
|
|
6
|
+
|
|
7
|
+
## Requirements
|
|
8
|
+
|
|
9
|
+
### Requirement: State Snapshot Exposure
|
|
10
|
+
|
|
11
|
+
The system SHALL provide a mechanism to retrieve the current execution state of a workflow.
|
|
12
|
+
|
|
13
|
+
#### Scenario: Retrieving state from TaskRunner
|
|
14
|
+
- **WHEN** a workflow is running or completed
|
|
15
|
+
- **THEN** the `TaskRunner` (or its underlying `TaskStateManager`) SHALL expose a method to retrieve a snapshot of the current state.
|
|
16
|
+
- **AND** the snapshot SHALL contain the `results` of all executed tasks.
|
|
17
|
+
- **AND** the snapshot SHALL be serializable (e.g., to JSON).
|
|
18
|
+
|
|
19
|
+
### Requirement: Hydrated Initialization
|
|
20
|
+
|
|
21
|
+
The system SHALL allow initializing a `TaskRunner` with a pre-existing state snapshot.
|
|
22
|
+
|
|
23
|
+
#### Scenario: Initializing with a snapshot
|
|
24
|
+
- **GIVEN** a valid state snapshot from a previous execution
|
|
25
|
+
- **WHEN** the `TaskRunner` is built using `TaskRunnerBuilder`
|
|
26
|
+
- **THEN** the builder SHALL accept the snapshot as an initial state.
|
|
27
|
+
- **AND** the `TaskRunner` SHALL start with the internal state reflecting the snapshot (i.e., known task results).
|
|
28
|
+
|
|
29
|
+
### Requirement: Resumable Execution Logic
|
|
30
|
+
|
|
31
|
+
The `WorkflowExecutor` SHALL respect the initial hydrated state during execution, skipping already completed tasks.
|
|
32
|
+
|
|
33
|
+
#### Scenario: Skipping successful tasks
|
|
34
|
+
- **GIVEN** a `TaskRunner` initialized with a snapshot where Task A is marked as `success`
|
|
35
|
+
- **WHEN** `execute()` is called
|
|
36
|
+
- **THEN** Task A SHALL NOT be executed again.
|
|
37
|
+
- **AND** Task A SHALL be considered completed for the purpose of checking dependencies of downstream tasks.
|
|
38
|
+
|
|
39
|
+
#### Scenario: Re-running non-successful tasks
|
|
40
|
+
- **GIVEN** a `TaskRunner` initialized with a snapshot where Task B is marked as `failure`, `cancelled`, or `skipped`
|
|
41
|
+
- **WHEN** `execute()` is called
|
|
42
|
+
- **THEN** Task B SHOULD be evaluated for execution (subject to dependency checks).
|
|
43
|
+
|
|
44
|
+
#### Scenario: Handling Context
|
|
45
|
+
- **GIVEN** a resumed workflow
|
|
46
|
+
- **THEN** it is the caller's responsibility to provide the necessary `Context` for task execution.
|
|
47
|
+
- **AND** the `TaskRunner` SHALL NOT attempt to automatically restore the context object from the state snapshot (as it may contain non-serializable data).
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# release-pr Specification
|
|
2
|
+
|
|
3
|
+
## Purpose
|
|
4
|
+
TBD - created by archiving change adopt-release-pr. Update Purpose after archive.
|
|
5
|
+
## Requirements
|
|
6
|
+
### Requirement: Release PR Generation
|
|
7
|
+
The system MUST automatically maintain a "Release PR" that targets the `main` branch. This PR must accumulate all conventional changes since the last release, calculating the next semantic version and generating a corresponding changelog entry.
|
|
8
|
+
|
|
9
|
+
#### Scenario: Feature commit triggers PR update
|
|
10
|
+
Given the latest release is `v1.0.0`
|
|
11
|
+
And a developer merges a commit with message `feat: add awesome feature` to `main`
|
|
12
|
+
Then the system should create or update the Release PR
|
|
13
|
+
And the PR title should be `chore: release 1.1.0`
|
|
14
|
+
And the PR body should contain the changelog entry for "add awesome feature"
|
|
15
|
+
And the `package.json` version in the PR should be `1.1.0`
|
|
16
|
+
|
|
17
|
+
#### Scenario: Fix commit triggers patch update
|
|
18
|
+
Given the latest release is `v1.0.0`
|
|
19
|
+
And a developer merges a commit `fix: urgent bug` to `main`
|
|
20
|
+
Then the Release PR should be updated to target version `1.0.1`
|
|
21
|
+
|
|
22
|
+
### Requirement: Release Publication
|
|
23
|
+
The system MUST execute the release process (git tag, GitHub Release, npm publish) ONLY when the Release PR is merged into `main`.
|
|
24
|
+
|
|
25
|
+
#### Scenario: Merge triggers publish
|
|
26
|
+
Given the Release PR for `v1.1.0` exists
|
|
27
|
+
When a maintainer merges the PR into `main`
|
|
28
|
+
Then the system should create a GitHub Release `v1.1.0`
|
|
29
|
+
And the system should publish the package to the configured registry (NPM)
|
|
30
|
+
And the system should NOT publish any other commits merged to `main` until the next Release PR merge
|
|
31
|
+
|
|
@@ -160,3 +160,15 @@ The system SHALL provide a `RetryingExecutionStrategy` that implements `IExecuti
|
|
|
160
160
|
|
|
161
161
|
- **WHEN** `retry.backoff` is 'exponential'
|
|
162
162
|
- **THEN** the delay SHALL increase for each attempt (e.g., `delay * 2^attempt`).
|
|
163
|
+
|
|
164
|
+
### Requirement: Task Execution Metrics
|
|
165
|
+
|
|
166
|
+
The system SHALL record timing metrics for each executed task, including start time, end time, and duration.
|
|
167
|
+
|
|
168
|
+
#### Scenario: Successful execution
|
|
169
|
+
- **WHEN** a task completes successfully
|
|
170
|
+
- **THEN** the task result contains the start timestamp, end timestamp, and duration in milliseconds
|
|
171
|
+
|
|
172
|
+
#### Scenario: Failed execution
|
|
173
|
+
- **WHEN** a task fails
|
|
174
|
+
- **THEN** the task result contains the start timestamp, end timestamp, and duration in milliseconds
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@calmo/task-runner",
|
|
3
|
-
"version": "4.0
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"description": "A lightweight, type-safe, and domain-agnostic task orchestration engine. It resolves a Directed Acyclic Graph (DAG) of steps, executes independent tasks in parallel, and manages a shared context across the pipeline.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
package/src/TaskResult.ts
CHANGED
|
@@ -12,4 +12,13 @@ export interface TaskResult {
|
|
|
12
12
|
error?: string;
|
|
13
13
|
/** Optional data produced by the step for later inspection. */
|
|
14
14
|
data?: unknown;
|
|
15
|
+
/** Optional execution metrics for the task. */
|
|
16
|
+
metrics?: {
|
|
17
|
+
/** Start time in milliseconds (performance.now). */
|
|
18
|
+
startTime: number;
|
|
19
|
+
/** End time in milliseconds (performance.now). */
|
|
20
|
+
endTime: number;
|
|
21
|
+
/** Duration in milliseconds. */
|
|
22
|
+
duration: number;
|
|
23
|
+
};
|
|
15
24
|
}
|
package/src/TaskRunner.ts
CHANGED
|
@@ -78,19 +78,59 @@ export class TaskRunner<TContext> {
|
|
|
78
78
|
*/
|
|
79
79
|
public static getMermaidGraph<T>(steps: TaskStep<T>[]): string {
|
|
80
80
|
const graphLines = ["graph TD"];
|
|
81
|
+
const idMap = new Map<string, string>();
|
|
82
|
+
const usedIds = new Set<string>();
|
|
83
|
+
const baseIdCounters = new Map<string, number>();
|
|
81
84
|
|
|
82
|
-
const
|
|
85
|
+
const getUniqueId = (name: string) => {
|
|
86
|
+
if (idMap.has(name)) {
|
|
87
|
+
return idMap.get(name)!;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const sanitized = this.sanitizeMermaidId(name);
|
|
91
|
+
let uniqueId = sanitized;
|
|
92
|
+
|
|
93
|
+
// First check if the base sanitized ID is available
|
|
94
|
+
if (!usedIds.has(uniqueId)) {
|
|
95
|
+
usedIds.add(uniqueId);
|
|
96
|
+
idMap.set(name, uniqueId);
|
|
97
|
+
return uniqueId;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// If not, use the counter for this base ID
|
|
101
|
+
let counter = baseIdCounters.get(sanitized) || 1;
|
|
102
|
+
|
|
103
|
+
while (usedIds.has(uniqueId)) {
|
|
104
|
+
uniqueId = `${sanitized}_${counter}`;
|
|
105
|
+
counter++;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
baseIdCounters.set(sanitized, counter);
|
|
109
|
+
|
|
110
|
+
usedIds.add(uniqueId);
|
|
111
|
+
idMap.set(name, uniqueId);
|
|
112
|
+
return uniqueId;
|
|
113
|
+
};
|
|
83
114
|
|
|
115
|
+
// Pre-calculate IDs for all steps to ensure stable generation order
|
|
116
|
+
// We sort steps by name to ensure deterministic ID generation regardless of input order if names clash
|
|
117
|
+
// But input order is usually significant in graph definition, so we'll stick to input order.
|
|
118
|
+
// However, we must process all step NAMES first.
|
|
84
119
|
for (const step of steps) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
120
|
+
getUniqueId(step.name);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
for (const step of steps) {
|
|
124
|
+
const stepId = getUniqueId(step.name);
|
|
125
|
+
graphLines.push(` ${stepId}[${JSON.stringify(step.name)}]`);
|
|
88
126
|
}
|
|
89
127
|
|
|
90
128
|
for (const step of steps) {
|
|
91
129
|
if (step.dependencies) {
|
|
130
|
+
const stepId = getUniqueId(step.name);
|
|
92
131
|
for (const dep of step.dependencies) {
|
|
93
|
-
|
|
132
|
+
const depId = getUniqueId(dep);
|
|
133
|
+
graphLines.push(` ${depId} --> ${stepId}`);
|
|
94
134
|
}
|
|
95
135
|
}
|
|
96
136
|
}
|
|
@@ -171,37 +211,16 @@ export class TaskRunner<TContext> {
|
|
|
171
211
|
timeout: number,
|
|
172
212
|
signal?: AbortSignal
|
|
173
213
|
): Promise<Map<string, TaskResult>> {
|
|
174
|
-
|
|
175
|
-
const
|
|
176
|
-
controller.abort(new Error(`Workflow timed out after ${timeout}ms`));
|
|
177
|
-
}, timeout);
|
|
178
|
-
|
|
179
|
-
let effectiveSignal = controller.signal;
|
|
180
|
-
let onAbort: (() => void) | undefined;
|
|
181
|
-
|
|
182
|
-
// Handle combination of signals if user provided one
|
|
183
|
-
if (signal) {
|
|
184
|
-
if (signal.aborted) {
|
|
185
|
-
// If already aborted, use it directly (WorkflowExecutor handles early abort)
|
|
186
|
-
// We can cancel timeout immediately
|
|
187
|
-
clearTimeout(timeoutId);
|
|
188
|
-
effectiveSignal = signal;
|
|
189
|
-
} else {
|
|
190
|
-
// Listen to user signal to abort our controller
|
|
191
|
-
onAbort = () => {
|
|
192
|
-
controller.abort(signal.reason);
|
|
193
|
-
};
|
|
194
|
-
signal.addEventListener("abort", onAbort);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
214
|
+
// Create a timeout signal that aborts after the specified time
|
|
215
|
+
const timeoutSignal = AbortSignal.timeout(timeout);
|
|
197
216
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
217
|
+
// If there's a user-provided signal, combine them.
|
|
218
|
+
// Otherwise, use the timeout signal directly.
|
|
219
|
+
const effectiveSignal = signal
|
|
220
|
+
? AbortSignal.any([signal, timeoutSignal])
|
|
221
|
+
: timeoutSignal;
|
|
222
|
+
|
|
223
|
+
return executor.execute(steps, effectiveSignal);
|
|
224
|
+
// No explicit clean up needed for AbortSignal.timeout as it is handled by the platform
|
|
206
225
|
}
|
|
207
226
|
}
|
package/src/TaskStateManager.ts
CHANGED
|
@@ -11,6 +11,11 @@ export class TaskStateManager<TContext> {
|
|
|
11
11
|
private pendingSteps = new Set<TaskStep<TContext>>();
|
|
12
12
|
private running = new Set<string>();
|
|
13
13
|
|
|
14
|
+
// Optimization structures
|
|
15
|
+
private dependencyGraph = new Map<string, TaskStep<TContext>[]>();
|
|
16
|
+
private dependencyCounts = new Map<string, number>();
|
|
17
|
+
private readyQueue: TaskStep<TContext>[] = [];
|
|
18
|
+
|
|
14
19
|
constructor(private eventBus: EventBus<TContext>) {}
|
|
15
20
|
|
|
16
21
|
/**
|
|
@@ -21,50 +26,39 @@ export class TaskStateManager<TContext> {
|
|
|
21
26
|
this.pendingSteps = new Set(steps);
|
|
22
27
|
this.results.clear();
|
|
23
28
|
this.running.clear();
|
|
24
|
-
|
|
29
|
+
this.readyQueue = [];
|
|
25
30
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
* Emits `taskSkipped` for skipped tasks.
|
|
29
|
-
* @returns An array of tasks that are ready to run.
|
|
30
|
-
*/
|
|
31
|
-
processDependencies(): TaskStep<TContext>[] {
|
|
32
|
-
const toRemove: TaskStep<TContext>[] = [];
|
|
33
|
-
const toRun: TaskStep<TContext>[] = [];
|
|
31
|
+
this.dependencyGraph.clear();
|
|
32
|
+
this.dependencyCounts.clear();
|
|
34
33
|
|
|
35
|
-
for (const step of
|
|
34
|
+
for (const step of steps) {
|
|
36
35
|
const deps = step.dependencies ?? [];
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
break;
|
|
36
|
+
this.dependencyCounts.set(step.name, deps.length);
|
|
37
|
+
|
|
38
|
+
if (deps.length === 0) {
|
|
39
|
+
this.readyQueue.push(step);
|
|
40
|
+
} else {
|
|
41
|
+
for (const dep of deps) {
|
|
42
|
+
if (!this.dependencyGraph.has(dep)) {
|
|
43
|
+
this.dependencyGraph.set(dep, []);
|
|
44
|
+
}
|
|
45
|
+
this.dependencyGraph.get(dep)!.push(step);
|
|
48
46
|
}
|
|
49
47
|
}
|
|
50
|
-
|
|
51
|
-
if (failedDep) {
|
|
52
|
-
const depResult = this.results.get(failedDep);
|
|
53
|
-
const depError = depResult?.error ? `: ${depResult.error}` : "";
|
|
54
|
-
const result: TaskResult = {
|
|
55
|
-
status: "skipped",
|
|
56
|
-
message: `Skipped because dependency '${failedDep}' failed${depError}`,
|
|
57
|
-
};
|
|
58
|
-
this.markSkipped(step, result);
|
|
59
|
-
toRemove.push(step);
|
|
60
|
-
} else if (!blocked) {
|
|
61
|
-
toRun.push(step);
|
|
62
|
-
toRemove.push(step);
|
|
63
|
-
}
|
|
64
48
|
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Processes the pending steps to identify tasks that can be started.
|
|
53
|
+
* Emits `taskSkipped` for skipped tasks (handled during cascade).
|
|
54
|
+
* @returns An array of tasks that are ready to run.
|
|
55
|
+
*/
|
|
56
|
+
processDependencies(): TaskStep<TContext>[] {
|
|
57
|
+
const toRun = [...this.readyQueue];
|
|
58
|
+
this.readyQueue = [];
|
|
65
59
|
|
|
66
|
-
//
|
|
67
|
-
for (const step of
|
|
60
|
+
// Remove them from pendingSteps as they are now handed off to the executor
|
|
61
|
+
for (const step of toRun) {
|
|
68
62
|
this.pendingSteps.delete(step);
|
|
69
63
|
}
|
|
70
64
|
|
|
@@ -90,6 +84,39 @@ export class TaskStateManager<TContext> {
|
|
|
90
84
|
this.running.delete(step.name);
|
|
91
85
|
this.results.set(step.name, result);
|
|
92
86
|
this.eventBus.emit("taskEnd", { step, result });
|
|
87
|
+
|
|
88
|
+
if (result.status === "success") {
|
|
89
|
+
this.handleSuccess(step.name);
|
|
90
|
+
} else {
|
|
91
|
+
this.cascadeFailure(step.name);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Marks a task as skipped and emits `taskSkipped`.
|
|
97
|
+
* @param step The task that was skipped.
|
|
98
|
+
* @param result The result object (status: skipped).
|
|
99
|
+
*/
|
|
100
|
+
markSkipped(step: TaskStep<TContext>, result: TaskResult): void {
|
|
101
|
+
if (this.internalMarkSkipped(step, result)) {
|
|
102
|
+
this.cascadeFailure(step.name);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Internal method to mark skipped without triggering cascade (to be used inside cascade loop).
|
|
108
|
+
* Returns true if the task was actually marked skipped (was not already finished).
|
|
109
|
+
*/
|
|
110
|
+
private internalMarkSkipped(step: TaskStep<TContext>, result: TaskResult): boolean {
|
|
111
|
+
if (this.results.has(step.name)) {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
this.running.delete(step.name);
|
|
116
|
+
this.results.set(step.name, result);
|
|
117
|
+
this.pendingSteps.delete(step);
|
|
118
|
+
this.eventBus.emit("taskSkipped", { step, result });
|
|
119
|
+
return true;
|
|
93
120
|
}
|
|
94
121
|
|
|
95
122
|
/**
|
|
@@ -97,10 +124,10 @@ export class TaskStateManager<TContext> {
|
|
|
97
124
|
* @param message The cancellation message.
|
|
98
125
|
*/
|
|
99
126
|
cancelAllPending(message: string): void {
|
|
127
|
+
this.readyQueue = []; // Clear ready queue
|
|
128
|
+
|
|
100
129
|
// Iterate over pendingSteps to cancel them
|
|
101
130
|
for (const step of this.pendingSteps) {
|
|
102
|
-
// Also check running? No, running tasks are handled by AbortSignal in Executor.
|
|
103
|
-
// We only cancel what is pending and hasn't started.
|
|
104
131
|
if (!this.results.has(step.name) && !this.running.has(step.name)) {
|
|
105
132
|
const result: TaskResult = {
|
|
106
133
|
status: "cancelled",
|
|
@@ -136,13 +163,54 @@ export class TaskStateManager<TContext> {
|
|
|
136
163
|
}
|
|
137
164
|
|
|
138
165
|
/**
|
|
139
|
-
*
|
|
140
|
-
* @param step The task that was skipped.
|
|
141
|
-
* @param result The result object (status: skipped).
|
|
166
|
+
* Handles successful completion of a task by updating dependents.
|
|
142
167
|
*/
|
|
143
|
-
|
|
144
|
-
this.
|
|
145
|
-
|
|
146
|
-
|
|
168
|
+
private handleSuccess(stepName: string): void {
|
|
169
|
+
const dependents = this.dependencyGraph.get(stepName);
|
|
170
|
+
if (!dependents) return;
|
|
171
|
+
|
|
172
|
+
for (const dependent of dependents) {
|
|
173
|
+
const currentCount = this.dependencyCounts.get(dependent.name)!;
|
|
174
|
+
const newCount = currentCount - 1;
|
|
175
|
+
this.dependencyCounts.set(dependent.name, newCount);
|
|
176
|
+
|
|
177
|
+
if (newCount === 0) {
|
|
178
|
+
// Task is ready. Ensure it's still pending.
|
|
179
|
+
if (this.pendingSteps.has(dependent)) {
|
|
180
|
+
this.readyQueue.push(dependent);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Cascades failure/skipping to dependents.
|
|
188
|
+
*/
|
|
189
|
+
private cascadeFailure(failedStepName: string): void {
|
|
190
|
+
const queue = [failedStepName];
|
|
191
|
+
// Use a set to track visited nodes in this cascade pass to avoid redundant processing,
|
|
192
|
+
// although checking results.has() in internalMarkSkipped also prevents it.
|
|
193
|
+
|
|
194
|
+
while (queue.length > 0) {
|
|
195
|
+
const currentName = queue.shift()!;
|
|
196
|
+
const dependents = this.dependencyGraph.get(currentName);
|
|
197
|
+
|
|
198
|
+
if (!dependents) continue;
|
|
199
|
+
|
|
200
|
+
// Get the result of the failed/skipped dependency to propagate error info if available
|
|
201
|
+
const currentResult = this.results.get(currentName);
|
|
202
|
+
const depError = currentResult?.error ? `: ${currentResult.error}` : "";
|
|
203
|
+
|
|
204
|
+
for (const dependent of dependents) {
|
|
205
|
+
const result: TaskResult = {
|
|
206
|
+
status: "skipped",
|
|
207
|
+
message: `Skipped because dependency '${currentName}' failed${depError}`,
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
if (this.internalMarkSkipped(dependent, result)) {
|
|
211
|
+
queue.push(dependent.name);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
147
215
|
}
|
|
148
216
|
}
|
package/src/WorkflowExecutor.ts
CHANGED
|
@@ -4,13 +4,14 @@ import { EventBus } from "./EventBus.js";
|
|
|
4
4
|
import { TaskStateManager } from "./TaskStateManager.js";
|
|
5
5
|
import { IExecutionStrategy } from "./strategies/IExecutionStrategy.js";
|
|
6
6
|
import { ExecutionConstants } from "./ExecutionConstants.js";
|
|
7
|
+
import { PriorityQueue } from "./utils/PriorityQueue.js";
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Handles the execution of the workflow steps.
|
|
10
11
|
* @template TContext The shape of the shared context object.
|
|
11
12
|
*/
|
|
12
13
|
export class WorkflowExecutor<TContext> {
|
|
13
|
-
private readyQueue
|
|
14
|
+
private readyQueue = new PriorityQueue<TaskStep<TContext>>();
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* @param context The shared context object.
|
|
@@ -114,14 +115,11 @@ export class WorkflowExecutor<TContext> {
|
|
|
114
115
|
|
|
115
116
|
// Add newly ready tasks to the queue
|
|
116
117
|
for (const task of newlyReady) {
|
|
117
|
-
this.readyQueue.push(task);
|
|
118
|
+
this.readyQueue.push(task, task.priority ?? 0);
|
|
118
119
|
}
|
|
119
120
|
|
|
120
|
-
// Sort by priority (descending) once after adding new tasks
|
|
121
|
-
this.readyQueue.sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
|
|
122
|
-
|
|
123
121
|
// Execute ready tasks while respecting concurrency limit
|
|
124
|
-
while (this.readyQueue.
|
|
122
|
+
while (!this.readyQueue.isEmpty()) {
|
|
125
123
|
if (
|
|
126
124
|
this.concurrency !== undefined &&
|
|
127
125
|
executingPromises.size >= this.concurrency
|
|
@@ -129,7 +127,7 @@ export class WorkflowExecutor<TContext> {
|
|
|
129
127
|
break;
|
|
130
128
|
}
|
|
131
129
|
|
|
132
|
-
const step = this.readyQueue.
|
|
130
|
+
const step = this.readyQueue.pop()!;
|
|
133
131
|
|
|
134
132
|
const taskPromise = this.executeTaskStep(step, signal)
|
|
135
133
|
.finally(() => {
|
|
@@ -194,16 +192,28 @@ export class WorkflowExecutor<TContext> {
|
|
|
194
192
|
|
|
195
193
|
this.stateManager.markRunning(step);
|
|
196
194
|
|
|
195
|
+
const startTime = performance.now();
|
|
196
|
+
let result: TaskResult;
|
|
197
|
+
|
|
197
198
|
try {
|
|
198
|
-
|
|
199
|
-
this.stateManager.markCompleted(step, result);
|
|
199
|
+
result = await this.strategy.execute(step, this.context, signal);
|
|
200
200
|
} catch (error) {
|
|
201
|
-
|
|
201
|
+
result = {
|
|
202
202
|
status: "failure",
|
|
203
203
|
message: ExecutionConstants.EXECUTION_STRATEGY_FAILED,
|
|
204
204
|
error: error instanceof Error ? error.message : String(error),
|
|
205
205
|
};
|
|
206
|
-
this.stateManager.markCompleted(step, result);
|
|
207
206
|
}
|
|
207
|
+
|
|
208
|
+
const endTime = performance.now();
|
|
209
|
+
|
|
210
|
+
// Always inject metrics to ensure accuracy
|
|
211
|
+
result.metrics = {
|
|
212
|
+
startTime,
|
|
213
|
+
endTime,
|
|
214
|
+
duration: endTime - startTime,
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
this.stateManager.markCompleted(step, result);
|
|
208
218
|
}
|
|
209
219
|
}
|
|
@@ -16,8 +16,7 @@ export class DryRunExecutionStrategy<
|
|
|
16
16
|
* @returns A promise resolving to a success result.
|
|
17
17
|
*/
|
|
18
18
|
async execute(
|
|
19
|
-
|
|
20
|
-
_step: TaskStep<TContext>,
|
|
19
|
+
step: TaskStep<TContext>,
|
|
21
20
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
22
21
|
_context: TContext,
|
|
23
22
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
@@ -25,7 +24,7 @@ export class DryRunExecutionStrategy<
|
|
|
25
24
|
): Promise<TaskResult> {
|
|
26
25
|
return Promise.resolve({
|
|
27
26
|
status: "success",
|
|
28
|
-
message: "Dry run: simulated success",
|
|
27
|
+
message: "Dry run: simulated success " + step.name,
|
|
29
28
|
});
|
|
30
29
|
}
|
|
31
30
|
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
export class PriorityQueue<T> {
|
|
2
|
+
private heap: { item: T; priority: number; sequenceId: number }[] = [];
|
|
3
|
+
private sequenceCounter = 0;
|
|
4
|
+
|
|
5
|
+
push(item: T, priority: number): void {
|
|
6
|
+
const node = { item, priority, sequenceId: this.sequenceCounter++ };
|
|
7
|
+
this.heap.push(node);
|
|
8
|
+
this.bubbleUp();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
pop(): T | undefined {
|
|
12
|
+
if (this.heap.length === 0) return undefined;
|
|
13
|
+
if (this.heap.length === 1) return this.heap.pop()!.item;
|
|
14
|
+
|
|
15
|
+
const top = this.heap[0];
|
|
16
|
+
this.heap[0] = this.heap.pop()!;
|
|
17
|
+
this.sinkDown();
|
|
18
|
+
return top.item;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
peek(): T | undefined {
|
|
22
|
+
return this.heap[0]?.item;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
size(): number {
|
|
26
|
+
return this.heap.length;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
isEmpty(): boolean {
|
|
30
|
+
return this.heap.length === 0;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
clear(): void {
|
|
34
|
+
this.heap = [];
|
|
35
|
+
this.sequenceCounter = 0;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
private bubbleUp(): void {
|
|
39
|
+
let index = this.heap.length - 1;
|
|
40
|
+
const element = this.heap[index];
|
|
41
|
+
|
|
42
|
+
while (index > 0) {
|
|
43
|
+
const parentIndex = Math.floor((index - 1) / 2);
|
|
44
|
+
const parent = this.heap[parentIndex];
|
|
45
|
+
|
|
46
|
+
if (this.compare(element, parent) <= 0) break;
|
|
47
|
+
|
|
48
|
+
this.heap[index] = parent;
|
|
49
|
+
this.heap[parentIndex] = element;
|
|
50
|
+
index = parentIndex;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private sinkDown(): void {
|
|
55
|
+
let index = 0;
|
|
56
|
+
const length = this.heap.length;
|
|
57
|
+
const element = this.heap[0];
|
|
58
|
+
|
|
59
|
+
while (true) {
|
|
60
|
+
const leftChildIndex = 2 * index + 1;
|
|
61
|
+
const rightChildIndex = 2 * index + 2;
|
|
62
|
+
let swapIndex: number | null = null;
|
|
63
|
+
|
|
64
|
+
if (leftChildIndex < length) {
|
|
65
|
+
if (this.compare(this.heap[leftChildIndex], element) > 0) {
|
|
66
|
+
swapIndex = leftChildIndex;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (rightChildIndex < length) {
|
|
71
|
+
const rightChild = this.heap[rightChildIndex];
|
|
72
|
+
const leftChild = this.heap[leftChildIndex];
|
|
73
|
+
|
|
74
|
+
if (
|
|
75
|
+
(swapIndex === null && this.compare(rightChild, element) > 0) ||
|
|
76
|
+
(swapIndex !== null && this.compare(rightChild, leftChild) > 0)
|
|
77
|
+
) {
|
|
78
|
+
swapIndex = rightChildIndex;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (swapIndex === null) break;
|
|
83
|
+
|
|
84
|
+
this.heap[index] = this.heap[swapIndex];
|
|
85
|
+
this.heap[swapIndex] = element;
|
|
86
|
+
index = swapIndex;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Returns positive if a > b (a should come before b)
|
|
91
|
+
private compare(
|
|
92
|
+
a: { priority: number; sequenceId: number },
|
|
93
|
+
b: { priority: number; sequenceId: number }
|
|
94
|
+
): number {
|
|
95
|
+
if (a.priority !== b.priority) {
|
|
96
|
+
return a.priority - b.priority;
|
|
97
|
+
}
|
|
98
|
+
// Lower sequenceId means earlier insertion, so it has higher priority
|
|
99
|
+
return b.sequenceId - a.sequenceId;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
## 1. Implementation
|
|
2
|
-
|
|
3
|
-
- [ ] 1.1 Update `TaskResult` interface in `src/TaskResult.ts` to include `metrics`.
|
|
4
|
-
- [ ] 1.2 Update `WorkflowExecutor.ts` to capture start/end times and calculate duration.
|
|
5
|
-
- [ ] 1.3 Update `WorkflowExecutor.ts` to inject metrics into the `TaskResult`.
|
|
6
|
-
- [ ] 1.4 Add unit tests in `tests/TaskMetrics.test.ts` to verify metrics are present and correct.
|
/package/openspec/changes/{adopt-release-pr → archive/2026-01-22-adopt-release-pr}/design.md
RENAMED
|
File without changes
|
/package/openspec/changes/{adopt-release-pr → archive/2026-01-22-adopt-release-pr}/proposal.md
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|