@calmo/task-runner 3.7.0 → 3.8.1

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 (68) hide show
  1. package/.github/workflows/ci.yml +3 -0
  2. package/.jules/nexus.md +5 -0
  3. package/.jules/sentinel.md +6 -0
  4. package/AGENTS.md +1 -0
  5. package/CHANGELOG.md +16 -0
  6. package/README.md +2 -0
  7. package/coverage/coverage-final.json +8 -7
  8. package/coverage/index.html +20 -20
  9. package/coverage/lcov-report/index.html +20 -20
  10. package/coverage/lcov-report/src/EventBus.ts.html +4 -4
  11. package/coverage/lcov-report/src/TaskGraphValidationError.ts.html +1 -1
  12. package/coverage/lcov-report/src/TaskGraphValidator.ts.html +142 -82
  13. package/coverage/lcov-report/src/TaskRunner.ts.html +94 -79
  14. package/coverage/lcov-report/src/TaskRunnerBuilder.ts.html +1 -1
  15. package/coverage/lcov-report/src/TaskRunnerExecutionConfig.ts.html +1 -1
  16. package/coverage/lcov-report/src/TaskStateManager.ts.html +42 -54
  17. package/coverage/lcov-report/src/WorkflowExecutor.ts.html +56 -47
  18. package/coverage/lcov-report/src/contracts/ErrorTypes.ts.html +103 -0
  19. package/coverage/lcov-report/src/contracts/RunnerEvents.ts.html +1 -1
  20. package/coverage/lcov-report/src/contracts/index.html +23 -8
  21. package/coverage/lcov-report/src/index.html +15 -15
  22. package/coverage/lcov-report/src/strategies/DryRunExecutionStrategy.ts.html +1 -1
  23. package/coverage/lcov-report/src/strategies/RetryingExecutionStrategy.ts.html +5 -5
  24. package/coverage/lcov-report/src/strategies/StandardExecutionStrategy.ts.html +3 -3
  25. package/coverage/lcov-report/src/strategies/index.html +1 -1
  26. package/coverage/lcov.info +361 -332
  27. package/coverage/src/EventBus.ts.html +4 -4
  28. package/coverage/src/TaskGraphValidationError.ts.html +1 -1
  29. package/coverage/src/TaskGraphValidator.ts.html +142 -82
  30. package/coverage/src/TaskRunner.ts.html +94 -79
  31. package/coverage/src/TaskRunnerBuilder.ts.html +1 -1
  32. package/coverage/src/TaskRunnerExecutionConfig.ts.html +1 -1
  33. package/coverage/src/TaskStateManager.ts.html +42 -54
  34. package/coverage/src/WorkflowExecutor.ts.html +56 -47
  35. package/coverage/src/contracts/ErrorTypes.ts.html +103 -0
  36. package/coverage/src/contracts/RunnerEvents.ts.html +1 -1
  37. package/coverage/src/contracts/index.html +23 -8
  38. package/coverage/src/index.html +15 -15
  39. package/coverage/src/strategies/DryRunExecutionStrategy.ts.html +1 -1
  40. package/coverage/src/strategies/RetryingExecutionStrategy.ts.html +5 -5
  41. package/coverage/src/strategies/StandardExecutionStrategy.ts.html +3 -3
  42. package/coverage/src/strategies/index.html +1 -1
  43. package/dist/TaskGraphValidator.d.ts +3 -0
  44. package/dist/TaskGraphValidator.js +33 -26
  45. package/dist/TaskGraphValidator.js.map +1 -1
  46. package/dist/TaskRunner.d.ts +4 -0
  47. package/dist/TaskRunner.js +39 -45
  48. package/dist/TaskRunner.js.map +1 -1
  49. package/dist/TaskStateManager.js +1 -5
  50. package/dist/TaskStateManager.js.map +1 -1
  51. package/dist/TaskStep.d.ts +6 -0
  52. package/dist/WorkflowExecutor.js +2 -0
  53. package/dist/WorkflowExecutor.js.map +1 -1
  54. package/dist/contracts/ErrorTypes.d.ts +6 -0
  55. package/dist/contracts/ErrorTypes.js +7 -0
  56. package/dist/contracts/ErrorTypes.js.map +1 -0
  57. package/dist/contracts/ValidationError.d.ts +2 -1
  58. package/openspec/changes/feat-task-metrics/proposal.md +17 -0
  59. package/openspec/changes/feat-task-metrics/tasks.md +6 -0
  60. package/package.json +14 -3
  61. package/src/TaskGraphValidator.ts +52 -32
  62. package/src/TaskRunner.ts +52 -47
  63. package/src/TaskStateManager.ts +1 -5
  64. package/src/TaskStep.ts +7 -0
  65. package/src/WorkflowExecutor.ts +3 -0
  66. package/src/contracts/ErrorTypes.ts +6 -0
  67. package/src/contracts/ValidationError.ts +10 -1
  68. package/test-report.xml +145 -123
package/src/TaskRunner.ts CHANGED
@@ -79,23 +79,14 @@ export class TaskRunner<TContext> {
79
79
  public static getMermaidGraph<T>(steps: TaskStep<T>[]): string {
80
80
  const graphLines = ["graph TD"];
81
81
 
82
- // Helper to sanitize node names or wrap them if needed
83
- // For simplicity, we just wrap in quotes and use the name as ID if it's simple
84
- // or generate an ID if strictly needed. Here we assume names are unique IDs.
85
- // We will wrap names in quotes for the label, but use the name as the ID.
86
- // Actually, Mermaid ID cannot have spaces without quotes.
87
- const safeId = (name: string) => JSON.stringify(name);
88
82
  const sanitize = (name: string) => this.sanitizeMermaidId(name);
89
83
 
90
- // Add all nodes first to ensure they exist
91
84
  for (const step of steps) {
92
- // Using the name as both ID and Label for simplicity
93
- // Format: ID["Label"]
94
- // safeId returns a quoted string (e.g. "Task Name"), so we use it directly as the label
95
- graphLines.push(` ${sanitize(step.name)}[${safeId(step.name)}]`);
85
+ graphLines.push(
86
+ ` ${sanitize(step.name)}[${JSON.stringify(step.name)}]`
87
+ );
96
88
  }
97
89
 
98
- // Add edges
99
90
  for (const step of steps) {
100
91
  if (step.dependencies) {
101
92
  for (const dep of step.dependencies) {
@@ -113,7 +104,7 @@ export class TaskRunner<TContext> {
113
104
  * @returns The sanitized string.
114
105
  */
115
106
  private static sanitizeMermaidId(id: string): string {
116
- return id.replaceAll(/ /g, "_").replaceAll(/:/g, "_").replaceAll(/"/g, "_");
107
+ return id.replaceAll(/[^a-zA-Z0-9_-]/g, "_");
117
108
  }
118
109
 
119
110
  /**
@@ -159,44 +150,58 @@ export class TaskRunner<TContext> {
159
150
  config?.concurrency
160
151
  );
161
152
 
162
- // We need to handle the timeout cleanup properly.
163
153
  if (config?.timeout !== undefined) {
164
- const controller = new AbortController();
165
- const timeoutId = setTimeout(() => {
166
- controller.abort(
167
- new Error(`Workflow timed out after ${config.timeout}ms`)
168
- );
169
- }, config.timeout);
170
-
171
- let effectiveSignal = controller.signal;
172
- let onAbort: (() => void) | undefined;
173
-
174
- // Handle combination of signals if user provided one
175
- if (config.signal) {
176
- if (config.signal.aborted) {
177
- // If already aborted, use it directly (WorkflowExecutor handles early abort)
178
- // We can cancel timeout immediately
179
- clearTimeout(timeoutId);
180
- effectiveSignal = config.signal;
181
- } else {
182
- // Listen to user signal to abort our controller
183
- onAbort = () => {
184
- controller.abort(config.signal?.reason);
185
- };
186
- config.signal.addEventListener("abort", onAbort);
187
- }
188
- }
154
+ return this.executeWithTimeout(
155
+ executor,
156
+ steps,
157
+ config.timeout,
158
+ config.signal
159
+ );
160
+ } else {
161
+ return executor.execute(steps, config?.signal);
162
+ }
163
+ }
189
164
 
190
- try {
191
- return await executor.execute(steps, effectiveSignal);
192
- } finally {
165
+ /**
166
+ * Executes tasks with a timeout, ensuring resources are cleaned up.
167
+ */
168
+ private async executeWithTimeout(
169
+ executor: WorkflowExecutor<TContext>,
170
+ steps: TaskStep<TContext>[],
171
+ timeout: number,
172
+ signal?: AbortSignal
173
+ ): Promise<Map<string, TaskResult>> {
174
+ const controller = new AbortController();
175
+ const timeoutId = setTimeout(() => {
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
193
187
  clearTimeout(timeoutId);
194
- if (config.signal && onAbort) {
195
- config.signal.removeEventListener("abort", onAbort);
196
- }
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
+ }
197
+
198
+ try {
199
+ return await executor.execute(steps, effectiveSignal);
200
+ } finally {
201
+ clearTimeout(timeoutId);
202
+ if (signal && onAbort) {
203
+ signal.removeEventListener("abort", onAbort);
197
204
  }
198
- } else {
199
- return executor.execute(steps, config?.signal);
200
205
  }
201
206
  }
202
207
  }
@@ -101,20 +101,16 @@ export class TaskStateManager<TContext> {
101
101
  for (const step of this.pendingSteps) {
102
102
  // Also check running? No, running tasks are handled by AbortSignal in Executor.
103
103
  // We only cancel what is pending and hasn't started.
104
- /* v8 ignore next 1 */
105
104
  if (!this.results.has(step.name) && !this.running.has(step.name)) {
106
105
  const result: TaskResult = {
107
106
  status: "cancelled",
108
107
  message,
109
108
  };
110
109
  this.results.set(step.name, result);
110
+ this.eventBus.emit("taskEnd", { step, result });
111
111
  }
112
112
  }
113
113
  // Clear pending set as they are now "done" (cancelled)
114
- // Wait, if we clear pending steps, processDependencies won't pick them up.
115
- // The loop in Executor relies on results.size or pendingSteps.
116
- // The previous implementation iterated `steps` (all steps) to cancel.
117
- // Here we iterate `pendingSteps`.
118
114
  this.pendingSteps.clear();
119
115
  }
120
116
 
package/src/TaskStep.ts CHANGED
@@ -18,6 +18,13 @@ export interface TaskStep<TContext> {
18
18
  */
19
19
  condition?: (context: TContext) => boolean | Promise<boolean>;
20
20
 
21
+ /**
22
+ * Optional priority.
23
+ * Higher values are picked first. Default is 0.
24
+ * Only affects ordering when multiple tasks are ready and concurrency slots are limited.
25
+ */
26
+ priority?: number;
27
+
21
28
  /**
22
29
  * The core logic of the task.
23
30
  * @param context The shared context object, allowing for state to be passed between tasks.
@@ -114,6 +114,9 @@ export class WorkflowExecutor<TContext> {
114
114
  this.readyQueue.push(task);
115
115
  }
116
116
 
117
+ // Sort by priority (descending) once after adding new tasks
118
+ this.readyQueue.sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
119
+
117
120
  // Execute ready tasks while respecting concurrency limit
118
121
  while (this.readyQueue.length > 0) {
119
122
  if (
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Error type constants for task graph validation.
3
+ */
4
+ export const ERROR_DUPLICATE_TASK = "duplicate_task" as const;
5
+ export const ERROR_MISSING_DEPENDENCY = "missing_dependency" as const;
6
+ export const ERROR_CYCLE = "cycle" as const;
@@ -1,9 +1,18 @@
1
+ import {
2
+ ERROR_CYCLE,
3
+ ERROR_DUPLICATE_TASK,
4
+ ERROR_MISSING_DEPENDENCY,
5
+ } from "./ErrorTypes.js";
6
+
1
7
  /**
2
8
  * Describes a specific validation error found in the task graph.
3
9
  */
4
10
  export interface ValidationError {
5
11
  /** The type of validation error. */
6
- type: "cycle" | "missing_dependency" | "duplicate_task";
12
+ type:
13
+ | typeof ERROR_CYCLE
14
+ | typeof ERROR_MISSING_DEPENDENCY
15
+ | typeof ERROR_DUPLICATE_TASK;
7
16
  /** A human-readable message describing the error. */
8
17
  message: string;
9
18
  /** Optional detailed information about the error, e.g., the cycle path, or the task with a missing dependency. */