@calmo/task-runner 1.2.2 → 1.2.3

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 (43) hide show
  1. package/.gemini/commands/openspec/apply.toml +21 -0
  2. package/.gemini/commands/openspec/archive.toml +25 -0
  3. package/.gemini/commands/openspec/proposal.toml +26 -0
  4. package/AGENTS.md +18 -0
  5. package/CHANGELOG.md +16 -0
  6. package/GEMINI.md +8 -0
  7. package/coverage/coverage-final.json +4 -4
  8. package/coverage/index.html +9 -9
  9. package/coverage/lcov-report/index.html +9 -9
  10. package/coverage/lcov-report/src/EventBus.ts.html +4 -4
  11. package/coverage/lcov-report/src/TaskGraphValidator.ts.html +35 -5
  12. package/coverage/lcov-report/src/TaskRunner.ts.html +7 -109
  13. package/coverage/lcov-report/src/WorkflowExecutor.ts.html +166 -109
  14. package/coverage/lcov-report/src/contracts/RunnerEvents.ts.html +1 -1
  15. package/coverage/lcov-report/src/contracts/index.html +1 -1
  16. package/coverage/lcov-report/src/index.html +16 -16
  17. package/coverage/lcov.info +120 -144
  18. package/coverage/src/EventBus.ts.html +4 -4
  19. package/coverage/src/TaskGraphValidator.ts.html +35 -5
  20. package/coverage/src/TaskRunner.ts.html +7 -109
  21. package/coverage/src/WorkflowExecutor.ts.html +166 -109
  22. package/coverage/src/contracts/RunnerEvents.ts.html +1 -1
  23. package/coverage/src/contracts/index.html +1 -1
  24. package/coverage/src/index.html +16 -16
  25. package/dist/TaskGraphValidator.d.ts +6 -0
  26. package/dist/TaskGraphValidator.js +9 -0
  27. package/dist/TaskGraphValidator.js.map +1 -1
  28. package/dist/TaskRunner.js +1 -32
  29. package/dist/TaskRunner.js.map +1 -1
  30. package/dist/WorkflowExecutor.d.ts +9 -0
  31. package/dist/WorkflowExecutor.js +74 -54
  32. package/dist/WorkflowExecutor.js.map +1 -1
  33. package/dist/contracts/ITaskGraphValidator.d.ts +6 -0
  34. package/openspec/AGENTS.md +456 -0
  35. package/openspec/changes/add-external-task-cancellation/proposal.md +14 -0
  36. package/openspec/changes/add-external-task-cancellation/tasks.md +10 -0
  37. package/openspec/project.md +31 -0
  38. package/package.json +1 -1
  39. package/src/TaskGraphValidator.ts +10 -0
  40. package/src/TaskRunner.ts +1 -35
  41. package/src/WorkflowExecutor.ts +81 -62
  42. package/src/contracts/ITaskGraphValidator.ts +7 -0
  43. package/test-report.xml +47 -39
@@ -28,78 +28,97 @@ export class WorkflowExecutor<TContext> {
28
28
 
29
29
  const results = new Map<string, TaskResult>();
30
30
  const executingPromises = new Set<Promise<void>>();
31
+ const pendingSteps = new Set(steps);
31
32
 
32
- // Helper to process pending steps and launch ready ones
33
- const processPendingSteps = () => {
34
- const pendingSteps = steps.filter(
35
- (step) => !results.has(step.name) && !this.running.has(step.name)
36
- );
33
+ // Initial pass
34
+ this.processQueue(pendingSteps, results, executingPromises);
37
35
 
38
- // 1. Identify and mark skipped tasks
39
- for (const step of pendingSteps) {
40
- const deps = step.dependencies ?? [];
41
- const failedDep = deps.find(
42
- (dep) => results.has(dep) && results.get(dep)?.status !== "success"
43
- );
44
- if (failedDep) {
45
- const result: TaskResult = {
46
- status: "skipped",
47
- message: `Skipped due to failed dependency: ${failedDep}`,
48
- };
49
- results.set(step.name, result);
50
- this.eventBus.emit("taskSkipped", { step, result });
51
- }
52
- }
36
+ while (results.size < steps.length && executingPromises.size > 0) {
37
+ // Wait for the next task to finish
38
+ await Promise.race(executingPromises);
39
+ // After a task finishes, check for new work
40
+ this.processQueue(pendingSteps, results, executingPromises);
41
+ }
53
42
 
54
- // Re-filter pending steps as some might have been skipped above
55
- const readySteps = steps.filter((step) => {
56
- if (results.has(step.name) || this.running.has(step.name)) return false;
57
- const deps = step.dependencies ?? [];
58
- return deps.every(
59
- (dep) => results.has(dep) && results.get(dep)?.status === "success"
60
- );
61
- });
43
+ this.eventBus.emit("workflowEnd", { context: this.context, results });
44
+ return results;
45
+ }
62
46
 
63
- // 2. Launch ready tasks
64
- for (const step of readySteps) {
65
- this.running.add(step.name);
66
- this.eventBus.emit("taskStart", { step });
47
+ /**
48
+ * Logic to identify tasks that can be started or must be skipped.
49
+ * Iterate only over pending steps to avoid O(N^2) checks on completed tasks.
50
+ */
51
+ private processQueue(
52
+ pendingSteps: Set<TaskStep<TContext>>,
53
+ results: Map<string, TaskResult>,
54
+ executingPromises: Set<Promise<void>>
55
+ ): void {
56
+ const toRemove: TaskStep<TContext>[] = [];
57
+ const toRun: TaskStep<TContext>[] = [];
67
58
 
68
- const taskPromise = (async () => {
69
- try {
70
- const result = await step.run(this.context);
71
- results.set(step.name, result);
72
- } catch (e) {
73
- results.set(step.name, {
74
- status: "failure",
75
- error: e instanceof Error ? e.message : String(e),
76
- });
77
- } finally {
78
- this.running.delete(step.name);
79
- const result = results.get(step.name)!;
80
- this.eventBus.emit("taskEnd", { step, result });
81
- }
82
- })();
59
+ for (const step of pendingSteps) {
60
+ const deps = step.dependencies ?? [];
61
+ let blocked = false;
62
+ let failedDep: string | undefined;
83
63
 
84
- // Wrap the task promise to ensure we can track it in the Set
85
- const trackedPromise = taskPromise.then(() => {
86
- executingPromises.delete(trackedPromise);
87
- });
88
- executingPromises.add(trackedPromise);
64
+ for (const dep of deps) {
65
+ const depResult = results.get(dep);
66
+ if (!depResult) {
67
+ // Dependency not finished yet
68
+ blocked = true;
69
+ } else if (depResult.status !== "success") {
70
+ failedDep = dep;
71
+ break;
72
+ }
89
73
  }
90
- };
91
74
 
92
- // Initial check to start independent tasks
93
- processPendingSteps();
75
+ if (failedDep) {
76
+ const result: TaskResult = {
77
+ status: "skipped",
78
+ message: `Skipped due to failed dependency: ${failedDep}`,
79
+ };
80
+ results.set(step.name, result);
81
+ this.eventBus.emit("taskSkipped", { step, result });
82
+ toRemove.push(step);
83
+ } else if (!blocked) {
84
+ toRun.push(step);
85
+ toRemove.push(step);
86
+ }
87
+ }
94
88
 
95
- while (results.size < steps.length && executingPromises.size > 0) {
96
- // Wait for the next task to finish
97
- await Promise.race(executingPromises);
98
- // After a task finishes, check for new work
99
- processPendingSteps();
89
+ // Cleanup pending set
90
+ for (const step of toRemove) {
91
+ pendingSteps.delete(step);
100
92
  }
101
93
 
102
- this.eventBus.emit("workflowEnd", { context: this.context, results });
103
- return results;
94
+ // Execute ready tasks
95
+ for (const step of toRun) {
96
+ const taskPromise = this.runStep(step, results).then(() => {
97
+ executingPromises.delete(taskPromise);
98
+ });
99
+ executingPromises.add(taskPromise);
100
+ }
101
+ }
102
+
103
+ /**
104
+ * Handles the lifecycle of a single task execution.
105
+ */
106
+ private async runStep(step: TaskStep<TContext>, results: Map<string, TaskResult>): Promise<void> {
107
+ this.running.add(step.name);
108
+ this.eventBus.emit("taskStart", { step });
109
+
110
+ try {
111
+ const result = await step.run(this.context);
112
+ results.set(step.name, result);
113
+ } catch (e) {
114
+ results.set(step.name, {
115
+ status: "failure",
116
+ error: e instanceof Error ? e.message : String(e),
117
+ });
118
+ } finally {
119
+ this.running.delete(step.name);
120
+ const result = results.get(step.name)!;
121
+ this.eventBus.emit("taskEnd", { step, result });
122
+ }
104
123
  }
105
124
  }
@@ -11,4 +11,11 @@ export interface ITaskGraphValidator {
11
11
  * @returns A ValidationResult object indicating the outcome of the validation.
12
12
  */
13
13
  validate(taskGraph: TaskGraph): ValidationResult;
14
+
15
+ /**
16
+ * Creates a human-readable error message from a validation result.
17
+ * @param result The validation result containing errors.
18
+ * @returns A formatted error string.
19
+ */
20
+ createErrorMessage(result: ValidationResult): string;
14
21
  }
package/test-report.xml CHANGED
@@ -1,7 +1,7 @@
1
1
  <?xml version="1.0" encoding="UTF-8" ?>
2
- <testsuites name="vitest tests" tests="32" failures="0" errors="0" time="0.171985314">
3
- <testsuite name="tests/ComplexScenario.test.ts" timestamp="2026-01-18T05:44:30.743Z" hostname="runnervmmtnos" tests="2" failures="0" errors="0" skipped="0" time="0.008824338">
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.005524092">
2
+ <testsuites name="vitest tests" tests="35" failures="0" errors="0" time="0.22304865">
3
+ <testsuite name="tests/ComplexScenario.test.ts" timestamp="2026-01-18T14:25:37.497Z" hostname="runnervmmtnos" tests="2" failures="0" errors="0" skipped="0" time="0.010231018">
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.006235483">
5
5
  <system-out>
6
6
  Running StepA
7
7
  Running StepB
@@ -15,77 +15,85 @@ 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.001386436">
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.00138321">
19
19
  </testcase>
20
20
  </testsuite>
21
- <testsuite name="tests/EventBus.test.ts" timestamp="2026-01-18T05:44:30.744Z" hostname="runnervmmtnos" tests="1" failures="0" errors="0" skipped="0" time="0.019386386">
22
- <testcase classname="tests/EventBus.test.ts" name="EventBus &gt; should handle async listeners throwing errors without crashing" time="0.017035792">
21
+ <testsuite name="tests/EventBus.test.ts" timestamp="2026-01-18T14:25:37.498Z" hostname="runnervmmtnos" tests="1" failures="0" errors="0" skipped="0" time="0.018978727">
22
+ <testcase classname="tests/EventBus.test.ts" name="EventBus &gt; should handle async listeners throwing errors without crashing" time="0.016583628">
23
23
  </testcase>
24
24
  </testsuite>
25
- <testsuite name="tests/TaskGraphValidator.test.ts" timestamp="2026-01-18T05:44:30.745Z" hostname="runnervmmtnos" tests="9" failures="0" errors="0" skipped="0" time="0.010354972">
26
- <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should be instantiated" time="0.001917275">
25
+ <testsuite name="tests/TaskGraphValidator.test.ts" timestamp="2026-01-18T14:25:37.499Z" hostname="runnervmmtnos" tests="9" failures="0" errors="0" skipped="0" time="0.007340745">
26
+ <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should be instantiated" time="0.001387989">
27
27
  </testcase>
28
- <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should return valid result for empty graph" time="0.00165162">
28
+ <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should return valid result for empty graph" time="0.001089122">
29
29
  </testcase>
30
- <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should detect duplicate tasks" time="0.001990723">
30
+ <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should detect duplicate tasks" time="0.001244392">
31
31
  </testcase>
32
- <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should detect missing dependencies" time="0.00050915">
32
+ <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should detect missing dependencies" time="0.000319586">
33
33
  </testcase>
34
- <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should detect cycles" time="0.000507055">
34
+ <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should detect cycles" time="0.000454157">
35
35
  </testcase>
36
- <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should return valid for a correct graph" time="0.00041269">
36
+ <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should return valid for a correct graph" time="0.000401499">
37
37
  </testcase>
38
- <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should not detect cycles if missing dependencies are present" time="0.00036427">
38
+ <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should not detect cycles if missing dependencies are present" time="0.000238875">
39
39
  </testcase>
40
- <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should detect more complex cycles" time="0.000319536">
40
+ <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should detect more complex cycles" time="0.000207848">
41
41
  </testcase>
42
- <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should handle cycle detection with no cycles but shared dependencies" time="0.000239367">
42
+ <testcase classname="tests/TaskGraphValidator.test.ts" name="TaskGraphValidator &gt; should handle cycle detection with no cycles but shared dependencies" time="0.000228516">
43
43
  </testcase>
44
44
  </testsuite>
45
- <testsuite name="tests/TaskRunner.test.ts" timestamp="2026-01-18T05:44:30.747Z" hostname="runnervmmtnos" tests="10" failures="0" errors="0" skipped="0" time="0.112430041">
46
- <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should run tasks in the correct sequential order" time="0.004173253">
45
+ <testsuite name="tests/TaskRunner.test.ts" timestamp="2026-01-18T14:25:37.501Z" hostname="runnervmmtnos" tests="10" failures="0" errors="0" skipped="0" time="0.111638951">
46
+ <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should run tasks in the correct sequential order" time="0.002716698">
47
47
  </testcase>
48
- <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should handle an empty list of tasks gracefully" time="0.00045054">
48
+ <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should handle an empty list of tasks gracefully" time="0.000317112">
49
49
  </testcase>
50
- <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should run independent tasks in parallel" time="0.099953731">
50
+ <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should run independent tasks in parallel" time="0.10065072">
51
51
  </testcase>
52
- <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should skip dependent tasks if a root task fails" time="0.000542632">
52
+ <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should skip dependent tasks if a root task fails" time="0.000465769">
53
53
  </testcase>
54
- <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should throw an error for &apos;circular dependency&apos;" time="0.002602664">
54
+ <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should throw an error for &apos;circular dependency&apos;" time="0.002694306">
55
55
  </testcase>
56
- <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should throw an error for &apos;missing dependency&apos;" time="0.000402671">
56
+ <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should throw an error for &apos;missing dependency&apos;" time="0.00041795">
57
57
  </testcase>
58
- <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should handle tasks that throw an error during execution" time="0.000365011">
58
+ <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should handle tasks that throw an error during execution" time="0.000376783">
59
59
  </testcase>
60
- <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should skip tasks whose dependencies are skipped" time="0.000483552">
60
+ <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should skip tasks whose dependencies are skipped" time="0.000786016">
61
61
  </testcase>
62
- <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should handle tasks that throw a non-Error object during execution" time="0.000424773">
62
+ <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should handle tasks that throw a non-Error object during execution" time="0.000431165">
63
63
  </testcase>
64
- <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should handle duplicate steps where one gets skipped due to failed dependency" time="0.000467091">
64
+ <testcase classname="tests/TaskRunner.test.ts" name="TaskRunner &gt; should handle duplicate steps where one gets skipped due to failed dependency" time="0.000468434">
65
65
  </testcase>
66
66
  </testsuite>
67
- <testsuite name="tests/TaskRunnerEvents.test.ts" timestamp="2026-01-18T05:44:30.748Z" hostname="runnervmmtnos" tests="7" failures="0" errors="0" skipped="0" time="0.015344879">
68
- <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should fire all lifecycle events in a successful run" time="0.005483039">
67
+ <testsuite name="tests/TaskRunnerEvents.test.ts" timestamp="2026-01-18T14:25:37.503Z" hostname="runnervmmtnos" tests="7" failures="0" errors="0" skipped="0" time="0.012381248">
68
+ <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should fire all lifecycle events in a successful run" time="0.00382187">
69
69
  </testcase>
70
- <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should fire taskSkipped event when dependency fails" time="0.004914606">
70
+ <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should fire taskSkipped event when dependency fails" time="0.003792205">
71
71
  </testcase>
72
- <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should not crash if a listener throws an error" time="0.001456386">
72
+ <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should not crash if a listener throws an error" time="0.001438714">
73
73
  </testcase>
74
- <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should fire workflow events even for empty step list" time="0.000272478">
74
+ <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should fire workflow events even for empty step list" time="0.000282257">
75
75
  </testcase>
76
- <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should handle unsubscribe correctly" time="0.000554882">
76
+ <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should handle unsubscribe correctly" time="0.000443788">
77
77
  </testcase>
78
- <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should safely handle off() when no listeners exist" time="0.000162473">
78
+ <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should safely handle off() when no listeners exist" time="0.000271827">
79
79
  </testcase>
80
- <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should support multiple listeners for the same event" time="0.000345023">
80
+ <testcase classname="tests/TaskRunnerEvents.test.ts" name="TaskRunner Events &gt; should support multiple listeners for the same event" time="0.000377915">
81
81
  </testcase>
82
82
  </testsuite>
83
- <testsuite name="tests/integration.test.ts" timestamp="2026-01-18T05:44:30.750Z" hostname="runnervmmtnos" tests="3" failures="0" errors="0" skipped="0" time="0.005644698">
84
- <testcase classname="tests/integration.test.ts" name="TaskRunner Validation Integration &gt; should throw validation error with clear message for duplicate tasks" time="0.003288223">
83
+ <testsuite name="tests/WorkflowExecutor.test.ts" timestamp="2026-01-18T14:25:37.504Z" hostname="runnervmmtnos" tests="3" failures="0" errors="0" skipped="0" time="0.057149901">
84
+ <testcase classname="tests/WorkflowExecutor.test.ts" name="WorkflowExecutor &gt; should execute steps sequentially when dependencies exist" time="0.003667222">
85
85
  </testcase>
86
- <testcase classname="tests/integration.test.ts" name="TaskRunner Validation Integration &gt; should throw validation error with clear message for missing dependencies" time="0.000378435">
86
+ <testcase classname="tests/WorkflowExecutor.test.ts" name="WorkflowExecutor &gt; should skip dependent steps if dependency fails" time="0.000479825">
87
87
  </testcase>
88
- <testcase classname="tests/integration.test.ts" name="TaskRunner Validation Integration &gt; should throw validation error with clear message for cycles" time="0.000325437">
88
+ <testcase classname="tests/WorkflowExecutor.test.ts" name="WorkflowExecutor &gt; should run independent steps in parallel" time="0.05083559">
89
+ </testcase>
90
+ </testsuite>
91
+ <testsuite name="tests/integration.test.ts" timestamp="2026-01-18T14:25:37.505Z" hostname="runnervmmtnos" tests="3" failures="0" errors="0" skipped="0" time="0.00532806">
92
+ <testcase classname="tests/integration.test.ts" name="TaskRunner Validation Integration &gt; should throw validation error with clear message for duplicate tasks" time="0.003098401">
93
+ </testcase>
94
+ <testcase classname="tests/integration.test.ts" name="TaskRunner Validation Integration &gt; should throw validation error with clear message for missing dependencies" time="0.000340456">
95
+ </testcase>
96
+ <testcase classname="tests/integration.test.ts" name="TaskRunner Validation Integration &gt; should throw validation error with clear message for cycles" time="0.000287436">
89
97
  </testcase>
90
98
  </testsuite>
91
99
  </testsuites>