@calmo/task-runner 1.1.1 → 1.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.
Files changed (35) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/coverage/TaskGraphValidator.ts.html +463 -0
  3. package/coverage/TaskRunner.ts.html +163 -52
  4. package/coverage/coverage-final.json +2 -1
  5. package/coverage/index.html +22 -7
  6. package/coverage/lcov-report/TaskGraphValidator.ts.html +463 -0
  7. package/coverage/lcov-report/TaskRunner.ts.html +163 -52
  8. package/coverage/lcov-report/index.html +22 -7
  9. package/coverage/lcov.info +190 -99
  10. package/dist/TaskGraph.d.ts +18 -0
  11. package/dist/TaskGraph.js +2 -0
  12. package/dist/TaskGraph.js.map +1 -0
  13. package/dist/TaskGraphValidator.d.ts +17 -0
  14. package/dist/TaskGraphValidator.js +103 -0
  15. package/dist/TaskGraphValidator.js.map +1 -0
  16. package/dist/TaskRunner.d.ts +1 -0
  17. package/dist/TaskRunner.js +44 -9
  18. package/dist/TaskRunner.js.map +1 -1
  19. package/dist/contracts/ITaskGraphValidator.d.ts +13 -0
  20. package/dist/contracts/ITaskGraphValidator.js +2 -0
  21. package/dist/contracts/ITaskGraphValidator.js.map +1 -0
  22. package/dist/contracts/ValidationError.d.ts +11 -0
  23. package/dist/contracts/ValidationError.js +2 -0
  24. package/dist/contracts/ValidationError.js.map +1 -0
  25. package/dist/contracts/ValidationResult.d.ts +10 -0
  26. package/dist/contracts/ValidationResult.js +2 -0
  27. package/dist/contracts/ValidationResult.js.map +1 -0
  28. package/package.json +1 -1
  29. package/src/TaskGraph.ts +19 -0
  30. package/src/TaskGraphValidator.ts +126 -0
  31. package/src/TaskRunner.ts +50 -13
  32. package/src/contracts/ITaskGraphValidator.ts +14 -0
  33. package/src/contracts/ValidationError.ts +11 -0
  34. package/src/contracts/ValidationResult.ts +11 -0
  35. package/test-report.xml +51 -23
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Represents a single task in the task graph.
3
+ */
4
+ export interface Task {
5
+ /** Unique identifier for the task. */
6
+ id: string;
7
+ /** An array of task IDs that this task directly depends on. */
8
+ dependencies: string[];
9
+ /** Allows for any other properties specific to the task's payload or configuration. */
10
+ [key: string]: unknown;
11
+ }
12
+
13
+ /**
14
+ * Represents the entire collection of tasks and their interdependencies.
15
+ */
16
+ export interface TaskGraph {
17
+ /** An array of tasks that make up the graph. */
18
+ tasks: Task[];
19
+ }
@@ -0,0 +1,126 @@
1
+ import { ITaskGraphValidator } from "./contracts/ITaskGraphValidator.js";
2
+ import { ValidationResult } from "./contracts/ValidationResult.js";
3
+ import { ValidationError } from "./contracts/ValidationError.js";
4
+ import { TaskGraph } from "./TaskGraph.js";
5
+
6
+ export class TaskGraphValidator implements ITaskGraphValidator {
7
+ /**
8
+ * Validates a given task graph for structural integrity.
9
+ * Checks for:
10
+ * 1. Duplicate task IDs.
11
+ * 2. Missing dependencies (tasks that depend on non-existent IDs).
12
+ * 3. Circular dependencies (cycles in the graph).
13
+ *
14
+ * @param taskGraph The task graph to validate.
15
+ * @returns A ValidationResult object indicating the outcome of the validation.
16
+ */
17
+ validate(taskGraph: TaskGraph): ValidationResult {
18
+ const errors: ValidationError[] = [];
19
+
20
+ // 1. Check for duplicate tasks
21
+ const taskIds = new Set<string>();
22
+ for (const task of taskGraph.tasks) {
23
+ if (taskIds.has(task.id)) {
24
+ errors.push({
25
+ type: "duplicate_task",
26
+ message: `Duplicate task detected with ID: ${task.id}`,
27
+ details: { taskId: task.id }
28
+ });
29
+ } else {
30
+ taskIds.add(task.id);
31
+ }
32
+ }
33
+
34
+ // 2. Check for missing dependencies
35
+ for (const task of taskGraph.tasks) {
36
+ for (const dependenceId of task.dependencies) {
37
+ if (!taskIds.has(dependenceId)) {
38
+ errors.push({
39
+ type: "missing_dependency",
40
+ message: `Task '${task.id}' depends on missing task '${dependenceId}'`,
41
+ details: { taskId: task.id, missingDependencyId: dependenceId }
42
+ });
43
+ }
44
+ }
45
+ }
46
+
47
+ // 3. Check for cycles
48
+ // Only run cycle detection if there are no missing dependencies, otherwise we might chase non-existent nodes.
49
+ const hasMissingDependencies = errors.some(e => e.type === "missing_dependency");
50
+
51
+ if (hasMissingDependencies) {
52
+ return {
53
+ isValid: errors.length === 0,
54
+ errors
55
+ };
56
+ }
57
+
58
+ // Build adjacency list
59
+ const adjacencyList = new Map<string, string[]>();
60
+ for (const task of taskGraph.tasks) {
61
+ adjacencyList.set(task.id, task.dependencies);
62
+ }
63
+
64
+ const visited = new Set<string>();
65
+ const recursionStack = new Set<string>();
66
+
67
+ for (const task of taskGraph.tasks) {
68
+ if (visited.has(task.id)) {
69
+ continue;
70
+ }
71
+
72
+ const path: string[] = [];
73
+ if (this.detectCycle(task.id, path, visited, recursionStack, adjacencyList)) {
74
+ // Extract the actual cycle from the path
75
+ // The path might look like A -> B -> C -> B (if we started at A and found cycle B-C-B)
76
+ const cycleStart = path[path.length - 1];
77
+ const cycleStartIndex = path.indexOf(cycleStart);
78
+ const cyclePath = path.slice(cycleStartIndex);
79
+
80
+ errors.push({
81
+ type: "cycle",
82
+ message: `Cycle detected: ${cyclePath.join(" -> ")}`,
83
+ details: { cyclePath }
84
+ });
85
+ // Break after first cycle found to avoid spamming similar errors
86
+ break;
87
+ }
88
+ }
89
+
90
+ return {
91
+ isValid: errors.length === 0,
92
+ errors
93
+ };
94
+ }
95
+
96
+ private detectCycle(
97
+ taskId: string,
98
+ path: string[],
99
+ visited: Set<string>,
100
+ recursionStack: Set<string>,
101
+ adjacencyList: Map<string, string[]>
102
+ ): boolean {
103
+ visited.add(taskId);
104
+ recursionStack.add(taskId);
105
+ path.push(taskId);
106
+
107
+ const dependencies = adjacencyList.get(taskId)!;
108
+ for (const dependenceId of dependencies) {
109
+ if (
110
+ !visited.has(dependenceId) &&
111
+ this.detectCycle(dependenceId, path, visited, recursionStack, adjacencyList)
112
+ ) {
113
+ return true;
114
+ } else if (recursionStack.has(dependenceId)) {
115
+ // Cycle detected
116
+ // Add the dependency to complete the visual cycle
117
+ path.push(dependenceId);
118
+ return true;
119
+ }
120
+ }
121
+
122
+ recursionStack.delete(taskId);
123
+ path.pop();
124
+ return false;
125
+ }
126
+ }
package/src/TaskRunner.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import { TaskStep } from "./TaskStep.js";
2
2
  import { TaskResult } from "./TaskResult.js";
3
+ import { TaskGraphValidator } from "./TaskGraphValidator.js";
4
+ import { TaskGraph } from "./TaskGraph.js";
3
5
 
4
6
  /**
5
7
  * Define the payload for every possible event in the lifecycle.
@@ -51,6 +53,7 @@ type ListenerMap<TContext> = {
51
53
  export class TaskRunner<TContext> {
52
54
  private running = new Set<string>();
53
55
  private listeners: ListenerMap<TContext> = {};
56
+ private validator = new TaskGraphValidator();
54
57
 
55
58
  /**
56
59
  * @param context The shared context object to be passed to each task.
@@ -128,6 +131,53 @@ export class TaskRunner<TContext> {
128
131
  * and values are the corresponding TaskResult objects.
129
132
  */
130
133
  async execute(steps: TaskStep<TContext>[]): Promise<Map<string, TaskResult>> {
134
+ // Validate the task graph before execution
135
+ const taskGraph: TaskGraph = {
136
+ tasks: steps.map((step) => ({
137
+ id: step.name,
138
+ dependencies: step.dependencies ?? [],
139
+ })),
140
+ };
141
+
142
+ const validationResult = this.validator.validate(taskGraph);
143
+ if (!validationResult.isValid) {
144
+ // Construct error message compatible with legacy tests
145
+ const affectedTasks = new Set<string>();
146
+ const errorDetails: string[] = [];
147
+
148
+ for (const error of validationResult.errors) {
149
+ errorDetails.push(error.message);
150
+ switch (error.type) {
151
+ case "cycle": {
152
+ // details is { cyclePath: string[] }
153
+ const path = (error.details as { cyclePath: string[] }).cyclePath;
154
+ // The last element duplicates the first in the path representation, so valid unique tasks are slice(0, -1) or just all as Set handles uniq
155
+ path.forEach((t) => affectedTasks.add(t));
156
+ break;
157
+ }
158
+ case "missing_dependency": {
159
+ // details is { taskId: string, missingDependencyId: string }
160
+ const d = error.details as { taskId: string };
161
+ affectedTasks.add(d.taskId);
162
+ break;
163
+ }
164
+ case "duplicate_task": {
165
+ const d = error.details as { taskId: string };
166
+ affectedTasks.add(d.taskId);
167
+ break;
168
+ }
169
+ }
170
+ }
171
+
172
+ // Legacy error format: "Circular dependency or missing dependency detected. Unable to run tasks: A, B"
173
+ const taskList = Array.from(affectedTasks).join(", ");
174
+ const legacyMessage = `Circular dependency or missing dependency detected. Unable to run tasks: ${taskList}`;
175
+ const detailedMessage = `Task graph validation failed: ${errorDetails.join("; ")}`;
176
+
177
+ // Combine them to satisfy both legacy tests (checking for legacy message) and new requirements (clear details)
178
+ throw new Error(`${legacyMessage} | ${detailedMessage}`);
179
+ }
180
+
131
181
  this.emit("workflowStart", { context: this.context, steps });
132
182
 
133
183
  const results = new Map<string, TaskResult>();
@@ -146,7 +196,6 @@ export class TaskRunner<TContext> {
146
196
 
147
197
  // Skip tasks with failed dependencies
148
198
  for (const step of pendingSteps) {
149
- if (results.has(step.name)) continue;
150
199
  const deps = step.dependencies ?? [];
151
200
  const failedDep = deps.find(
152
201
  (dep) => results.has(dep) && results.get(dep)?.status !== "success"
@@ -161,18 +210,6 @@ export class TaskRunner<TContext> {
161
210
  }
162
211
  }
163
212
 
164
- if (
165
- readySteps.length === 0 &&
166
- this.running.size === 0 &&
167
- results.size < steps.length
168
- ) {
169
- const unrunnableSteps = steps.filter((s) => !results.has(s.name));
170
- const unrunnableStepNames = unrunnableSteps.map((s) => s.name);
171
- throw new Error(
172
- `Circular dependency or missing dependency detected. Unable to run tasks: ${unrunnableStepNames.join(", ")}`
173
- );
174
- }
175
-
176
213
  await Promise.all(
177
214
  readySteps.map(async (step) => {
178
215
  this.running.add(step.name);
@@ -0,0 +1,14 @@
1
+ import { TaskGraph } from "../TaskGraph.js";
2
+ import { ValidationResult } from "./ValidationResult.js";
3
+
4
+ /**
5
+ * Defines the interface for a task graph validator.
6
+ */
7
+ export interface ITaskGraphValidator {
8
+ /**
9
+ * Validates a given task graph for structural integrity.
10
+ * @param taskGraph The task graph to validate.
11
+ * @returns A ValidationResult object indicating the outcome of the validation.
12
+ */
13
+ validate(taskGraph: TaskGraph): ValidationResult;
14
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Describes a specific validation error found in the task graph.
3
+ */
4
+ export interface ValidationError {
5
+ /** The type of validation error. */
6
+ type: "cycle" | "missing_dependency" | "duplicate_task";
7
+ /** A human-readable message describing the error. */
8
+ message: string;
9
+ /** Optional detailed information about the error, e.g., the cycle path, or the task with a missing dependency. */
10
+ details?: unknown;
11
+ }
@@ -0,0 +1,11 @@
1
+ import { ValidationError } from "./ValidationError.js";
2
+
3
+ /**
4
+ * The result of a task graph validation operation.
5
+ */
6
+ export interface ValidationResult {
7
+ /** True if the graph is valid, false otherwise. */
8
+ isValid: boolean;
9
+ /** An array of ValidationError objects if the graph is not valid. Empty if isValid is true. */
10
+ errors: ValidationError[];
11
+ }
package/test-report.xml CHANGED
@@ -1,7 +1,7 @@
1
1
  <?xml version="1.0" encoding="UTF-8" ?>
2
- <testsuites name="vitest tests" tests="19" failures="0" errors="0" time="0.135083259">
3
- <testsuite name="tests/ComplexScenario.test.ts" timestamp="2026-01-18T03:50:46.197Z" hostname="runnervmmtnos" tests="2" failures="0" errors="0" skipped="0" time="0.009883356">
4
- <testcase classname="tests/ComplexScenario.test.ts" name="Complex Scenario Integration Tests &gt; 1. All steps execute when all succeed and context is hydrated" time="0.006132912">
2
+ <testsuites name="vitest tests" tests="31" failures="0" errors="0" time="0.148869921">
3
+ <testsuite name="tests/ComplexScenario.test.ts" timestamp="2026-01-18T03:56:21.866Z" hostname="runnervmmtnos" tests="2" failures="0" errors="0" skipped="0" time="0.008692833">
4
+ <testcase classname="tests/ComplexScenario.test.ts" name="Complex Scenario Integration Tests &gt; 1. All steps execute when all succeed and context is hydrated" time="0.005600185">
5
5
  <system-out>
6
6
  Running StepA
7
7
  Running StepB
@@ -15,45 +15,73 @@ Running StepG
15
15
 
16
16
  </system-out>
17
17
  </testcase>
18
- <testcase classname="tests/ComplexScenario.test.ts" name="Complex Scenario Integration Tests &gt; 2. The &apos;skip&apos; propagation works as intended if something breaks up in the tree" time="0.00153012">
18
+ <testcase classname="tests/ComplexScenario.test.ts" name="Complex Scenario Integration Tests &gt; 2. The &apos;skip&apos; propagation works as intended if something breaks up in the tree" time="0.001185557">
19
19
  </testcase>
20
20
  </testsuite>
21
- <testsuite name="tests/TaskRunner.test.ts" timestamp="2026-01-18T03:50:46.199Z" hostname="runnervmmtnos" tests="10" failures="0" errors="0" skipped="0" time="0.109323687">
22
- <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should run tasks in the correct sequential order" time="0.002455516">
21
+ <testsuite name="tests/TaskGraphValidator.test.ts" timestamp="2026-01-18T03:56:21.867Z" hostname="runnervmmtnos" tests="9" failures="0" errors="0" skipped="0" time="0.00859907">
22
+ <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should be instantiated" time="0.001784141">
23
23
  </testcase>
24
- <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should handle an empty list of tasks gracefully" time="0.000305152">
24
+ <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should return valid result for empty graph" time="0.001494487">
25
25
  </testcase>
26
- <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should run independent tasks in parallel" time="0.100731975">
26
+ <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should detect duplicate tasks" time="0.001370324">
27
27
  </testcase>
28
- <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should skip dependent tasks if a root task fails" time="0.00046882">
28
+ <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should detect missing dependencies" time="0.000348985">
29
29
  </testcase>
30
- <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should throw an error for &apos;circular dependency&apos;" time="0.00185594">
30
+ <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should detect cycles" time="0.000306475">
31
31
  </testcase>
32
- <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should throw an error for &apos;missing dependency&apos;" time="0.000228108">
32
+ <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should return valid for a correct graph" time="0.000329659">
33
33
  </testcase>
34
- <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should handle tasks that throw an error during execution" time="0.000243576">
34
+ <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should not detect cycles if missing dependencies are present" time="0.000273113">
35
35
  </testcase>
36
- <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should skip tasks whose dependencies are skipped" time="0.000262202">
36
+ <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should detect more complex cycles" time="0.000237105">
37
37
  </testcase>
38
- <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should handle tasks that throw a non-Error object during execution" time="0.000268865">
38
+ <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should handle cycle detection with no cycles but shared dependencies" time="0.000156013">
39
39
  </testcase>
40
- <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should handle duplicate steps where one gets skipped due to failed dependency" time="0.00040515">
40
+ </testsuite>
41
+ <testsuite name="tests/TaskRunner.test.ts" timestamp="2026-01-18T03:56:21.869Z" hostname="runnervmmtnos" tests="10" failures="0" errors="0" skipped="0" time="0.111209003">
42
+ <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should run tasks in the correct sequential order" time="0.003746958">
43
+ </testcase>
44
+ <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should handle an empty list of tasks gracefully" time="0.00041004">
45
+ </testcase>
46
+ <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should run independent tasks in parallel" time="0.100646475">
47
+ </testcase>
48
+ <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should skip dependent tasks if a root task fails" time="0.000392958">
49
+ </testcase>
50
+ <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should throw an error for &apos;circular dependency&apos;" time="0.001934242">
51
+ </testcase>
52
+ <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should throw an error for &apos;missing dependency&apos;" time="0.000290306">
53
+ </testcase>
54
+ <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should handle tasks that throw an error during execution" time="0.00024479">
55
+ </testcase>
56
+ <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should skip tasks whose dependencies are skipped" time="0.000380384">
57
+ </testcase>
58
+ <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should handle tasks that throw a non-Error object during execution" time="0.000391555">
59
+ </testcase>
60
+ <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should handle duplicate steps where one gets skipped due to failed dependency" time="0.00036763">
41
61
  </testcase>
42
62
  </testsuite>
43
- <testsuite name="tests/TaskRunnerEvents.test.ts" timestamp="2026-01-18T03:50:46.201Z" hostname="runnervmmtnos" tests="7" failures="0" errors="0" skipped="0" time="0.015876216">
44
- <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should fire all lifecycle events in a successful run" time="0.004415742">
63
+ <testsuite name="tests/TaskRunnerEvents.test.ts" timestamp="2026-01-18T03:56:21.871Z" hostname="runnervmmtnos" tests="7" failures="0" errors="0" skipped="0" time="0.013775682">
64
+ <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should fire all lifecycle events in a successful run" time="0.004847334">
65
+ </testcase>
66
+ <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should fire taskSkipped event when dependency fails" time="0.004274688">
45
67
  </testcase>
46
- <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should fire taskSkipped event when dependency fails" time="0.005824924">
68
+ <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should not crash if a listener throws an error" time="0.001365544">
47
69
  </testcase>
48
- <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should not crash if a listener throws an error" time="0.001840071">
70
+ <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should fire workflow events even for empty step list" time="0.000304822">
49
71
  </testcase>
50
- <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should fire workflow events even for empty step list" time="0.000423804">
72
+ <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should handle unsubscribe correctly" time="0.000354125">
51
73
  </testcase>
52
- <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should handle unsubscribe correctly" time="0.000460765">
74
+ <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should safely handle off() when no listeners exist" time="0.00015962">
75
+ </testcase>
76
+ <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should support multiple listeners for the same event" time="0.000313028">
77
+ </testcase>
78
+ </testsuite>
79
+ <testsuite name="tests/integration.test.ts" timestamp="2026-01-18T03:56:21.873Z" hostname="runnervmmtnos" tests="3" failures="0" errors="0" skipped="0" time="0.006593333">
80
+ <testcase classname="tests/integration.test.ts" name="TaskRunner Validation Integration &gt; should throw validation error with clear message for duplicate tasks" time="0.004083429">
53
81
  </testcase>
54
- <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should safely handle off() when no listeners exist" time="0.000210355">
82
+ <testcase classname="tests/integration.test.ts" name="TaskRunner Validation Integration &gt; should throw validation error with clear message for missing dependencies" time="0.000367681">
55
83
  </testcase>
56
- <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should support multiple listeners for the same event" time="0.000482585">
84
+ <testcase classname="tests/integration.test.ts" name="TaskRunner Validation Integration &gt; should throw validation error with clear message for cycles" time="0.000389051">
57
85
  </testcase>
58
86
  </testsuite>
59
87
  </testsuites>