@calmo/task-runner 3.0.0 → 3.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 (68) hide show
  1. package/.jules/nexus.md +5 -0
  2. package/CHANGELOG.md +26 -0
  3. package/coverage/coverage-final.json +8 -6
  4. package/coverage/index.html +13 -13
  5. package/coverage/lcov-report/index.html +13 -13
  6. package/coverage/lcov-report/src/EventBus.ts.html +4 -4
  7. package/coverage/lcov-report/src/TaskGraphValidator.ts.html +38 -38
  8. package/coverage/lcov-report/src/TaskRunner.ts.html +194 -20
  9. package/coverage/lcov-report/src/TaskRunnerBuilder.ts.html +1 -1
  10. package/coverage/lcov-report/src/TaskRunnerExecutionConfig.ts.html +17 -2
  11. package/coverage/lcov-report/src/TaskStateManager.ts.html +34 -34
  12. package/coverage/lcov-report/src/WorkflowExecutor.ts.html +30 -30
  13. package/coverage/lcov-report/src/contracts/RunnerEvents.ts.html +1 -1
  14. package/coverage/lcov-report/src/contracts/index.html +1 -1
  15. package/coverage/lcov-report/src/index.html +9 -9
  16. package/coverage/lcov-report/src/strategies/DryRunExecutionStrategy.ts.html +178 -0
  17. package/coverage/lcov-report/src/strategies/RetryingExecutionStrategy.ts.html +355 -0
  18. package/coverage/lcov-report/src/strategies/StandardExecutionStrategy.ts.html +3 -3
  19. package/coverage/lcov-report/src/strategies/index.html +35 -5
  20. package/coverage/lcov.info +328 -214
  21. package/coverage/src/EventBus.ts.html +4 -4
  22. package/coverage/src/TaskGraphValidator.ts.html +38 -38
  23. package/coverage/src/TaskRunner.ts.html +194 -20
  24. package/coverage/src/TaskRunnerBuilder.ts.html +1 -1
  25. package/coverage/src/TaskRunnerExecutionConfig.ts.html +17 -2
  26. package/coverage/src/TaskStateManager.ts.html +34 -34
  27. package/coverage/src/WorkflowExecutor.ts.html +30 -30
  28. package/coverage/src/contracts/RunnerEvents.ts.html +1 -1
  29. package/coverage/src/contracts/index.html +1 -1
  30. package/coverage/src/index.html +9 -9
  31. package/coverage/src/strategies/DryRunExecutionStrategy.ts.html +178 -0
  32. package/coverage/src/strategies/RetryingExecutionStrategy.ts.html +355 -0
  33. package/coverage/src/strategies/StandardExecutionStrategy.ts.html +3 -3
  34. package/coverage/src/strategies/index.html +35 -5
  35. package/dist/TaskRunner.d.ts +12 -0
  36. package/dist/TaskRunner.js +47 -2
  37. package/dist/TaskRunner.js.map +1 -1
  38. package/dist/TaskRunnerExecutionConfig.d.ts +5 -0
  39. package/dist/TaskStep.d.ts +3 -0
  40. package/dist/contracts/TaskRetryConfig.d.ts +8 -0
  41. package/dist/contracts/TaskRetryConfig.js +2 -0
  42. package/dist/contracts/TaskRetryConfig.js.map +1 -0
  43. package/dist/index.d.ts +2 -0
  44. package/dist/index.js +1 -0
  45. package/dist/index.js.map +1 -1
  46. package/dist/strategies/DryRunExecutionStrategy.d.ts +16 -0
  47. package/dist/strategies/DryRunExecutionStrategy.js +25 -0
  48. package/dist/strategies/DryRunExecutionStrategy.js.map +1 -0
  49. package/dist/strategies/RetryingExecutionStrategy.d.ts +12 -0
  50. package/dist/strategies/RetryingExecutionStrategy.js +74 -0
  51. package/dist/strategies/RetryingExecutionStrategy.js.map +1 -0
  52. package/openspec/changes/add-concurrency-control/proposal.md +6 -6
  53. package/openspec/changes/add-task-retry-policy/proposal.md +8 -5
  54. package/openspec/changes/archive/2026-01-18-add-workflow-preview/proposal.md +13 -0
  55. package/openspec/changes/archive/2026-01-18-add-workflow-preview/tasks.md +7 -0
  56. package/openspec/changes/feat-per-task-timeout/proposal.md +25 -0
  57. package/openspec/changes/feat-per-task-timeout/tasks.md +27 -0
  58. package/package.json +1 -1
  59. package/src/TaskRunner.ts +60 -2
  60. package/src/TaskRunnerExecutionConfig.ts +5 -0
  61. package/src/TaskStep.ts +3 -0
  62. package/src/contracts/TaskRetryConfig.ts +8 -0
  63. package/src/index.ts +2 -0
  64. package/src/strategies/DryRunExecutionStrategy.ts +31 -0
  65. package/src/strategies/RetryingExecutionStrategy.ts +90 -0
  66. package/test-report.xml +122 -82
  67. package/openspec/changes/add-workflow-preview/proposal.md +0 -12
  68. package/openspec/changes/add-workflow-preview/tasks.md +0 -7
@@ -0,0 +1,16 @@
1
+ import { IExecutionStrategy } from "./IExecutionStrategy.js";
2
+ import { TaskStep } from "../TaskStep.js";
3
+ import { TaskResult } from "../TaskResult.js";
4
+ /**
5
+ * Execution strategy that simulates task execution without running the actual logic.
6
+ */
7
+ export declare class DryRunExecutionStrategy<TContext> implements IExecutionStrategy<TContext> {
8
+ /**
9
+ * Simulates execution by returning a success result immediately.
10
+ * @param step The task step (ignored).
11
+ * @param context The shared context (ignored).
12
+ * @param signal Optional abort signal (ignored).
13
+ * @returns A promise resolving to a success result.
14
+ */
15
+ execute(_step: TaskStep<TContext>, _context: TContext, _signal?: AbortSignal): Promise<TaskResult>;
16
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Execution strategy that simulates task execution without running the actual logic.
3
+ */
4
+ export class DryRunExecutionStrategy {
5
+ /**
6
+ * Simulates execution by returning a success result immediately.
7
+ * @param step The task step (ignored).
8
+ * @param context The shared context (ignored).
9
+ * @param signal Optional abort signal (ignored).
10
+ * @returns A promise resolving to a success result.
11
+ */
12
+ async execute(
13
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
14
+ _step,
15
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
16
+ _context,
17
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
18
+ _signal) {
19
+ return Promise.resolve({
20
+ status: "success",
21
+ message: "Dry run: simulated success",
22
+ });
23
+ }
24
+ }
25
+ //# sourceMappingURL=DryRunExecutionStrategy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DryRunExecutionStrategy.js","sourceRoot":"","sources":["../../src/strategies/DryRunExecutionStrategy.ts"],"names":[],"mappings":"AAIA;;GAEG;AACH,MAAM,OAAO,uBAAuB;IAGlC;;;;;;OAMG;IACH,KAAK,CAAC,OAAO;IACX,6DAA6D;IAC7D,KAAyB;IACzB,6DAA6D;IAC7D,QAAkB;IAClB,6DAA6D;IAC7D,OAAqB;QAErB,OAAO,OAAO,CAAC,OAAO,CAAC;YACrB,MAAM,EAAE,SAAS;YACjB,OAAO,EAAE,4BAA4B;SACtC,CAAC,CAAC;IACL,CAAC;CACF"}
@@ -0,0 +1,12 @@
1
+ import { IExecutionStrategy } from "./IExecutionStrategy.js";
2
+ import { TaskStep } from "../TaskStep.js";
3
+ import { TaskResult } from "../TaskResult.js";
4
+ /**
5
+ * Execution strategy that retries tasks upon failure based on their retry configuration.
6
+ */
7
+ export declare class RetryingExecutionStrategy<TContext> implements IExecutionStrategy<TContext> {
8
+ private innerStrategy;
9
+ constructor(innerStrategy: IExecutionStrategy<TContext>);
10
+ execute(step: TaskStep<TContext>, context: TContext, signal?: AbortSignal): Promise<TaskResult>;
11
+ private sleep;
12
+ }
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Execution strategy that retries tasks upon failure based on their retry configuration.
3
+ */
4
+ export class RetryingExecutionStrategy {
5
+ innerStrategy;
6
+ constructor(innerStrategy) {
7
+ this.innerStrategy = innerStrategy;
8
+ }
9
+ async execute(step, context, signal) {
10
+ const config = step.retry;
11
+ if (!config) {
12
+ return this.innerStrategy.execute(step, context, signal);
13
+ }
14
+ let attempt = 0;
15
+ while (true) {
16
+ // Check for cancellation before execution
17
+ if (signal?.aborted) {
18
+ return {
19
+ status: "cancelled",
20
+ message: "Task cancelled before execution",
21
+ };
22
+ }
23
+ const result = await this.innerStrategy.execute(step, context, signal);
24
+ if (result.status === "success" || result.status === "cancelled" || result.status === "skipped") {
25
+ return result;
26
+ }
27
+ // Task failed, check if we should retry
28
+ if (attempt >= config.attempts) {
29
+ return result; // Max attempts reached, return failure
30
+ }
31
+ attempt++;
32
+ // Calculate delay
33
+ let delay = config.delay;
34
+ if (config.backoff === "exponential") {
35
+ delay = config.delay * Math.pow(2, attempt - 1);
36
+ }
37
+ // Wait for delay, respecting cancellation
38
+ try {
39
+ await this.sleep(delay, signal);
40
+ }
41
+ catch (e) {
42
+ if (signal?.aborted) {
43
+ return {
44
+ status: "cancelled",
45
+ message: "Task cancelled during retry delay",
46
+ };
47
+ }
48
+ throw e;
49
+ }
50
+ }
51
+ }
52
+ sleep(ms, signal) {
53
+ return new Promise((resolve, reject) => {
54
+ if (signal?.aborted) {
55
+ reject(new Error("AbortError"));
56
+ return;
57
+ }
58
+ const timer = setTimeout(() => {
59
+ cleanup();
60
+ resolve();
61
+ }, ms);
62
+ const onAbort = () => {
63
+ clearTimeout(timer);
64
+ cleanup();
65
+ reject(new Error("AbortError"));
66
+ };
67
+ const cleanup = () => {
68
+ signal?.removeEventListener("abort", onAbort);
69
+ };
70
+ signal?.addEventListener("abort", onAbort);
71
+ });
72
+ }
73
+ }
74
+ //# sourceMappingURL=RetryingExecutionStrategy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RetryingExecutionStrategy.js","sourceRoot":"","sources":["../../src/strategies/RetryingExecutionStrategy.ts"],"names":[],"mappings":"AAIA;;GAEG;AACH,MAAM,OAAO,yBAAyB;IAChB;IAApB,YAAoB,aAA2C;QAA3C,kBAAa,GAAb,aAAa,CAA8B;IAAG,CAAC;IAEnE,KAAK,CAAC,OAAO,CACX,IAAwB,EACxB,OAAiB,EACjB,MAAoB;QAEpB,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC;QAC1B,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,OAAO,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;QAC3D,CAAC;QAED,IAAI,OAAO,GAAG,CAAC,CAAC;QAChB,OAAO,IAAI,EAAE,CAAC;YACZ,0CAA0C;YAC1C,IAAI,MAAM,EAAE,OAAO,EAAE,CAAC;gBACpB,OAAO;oBACL,MAAM,EAAE,WAAW;oBACnB,OAAO,EAAE,iCAAiC;iBAC3C,CAAC;YACJ,CAAC;YAED,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;YAEvE,IAAI,MAAM,CAAC,MAAM,KAAK,SAAS,IAAI,MAAM,CAAC,MAAM,KAAK,WAAW,IAAI,MAAM,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;gBAChG,OAAO,MAAM,CAAC;YAChB,CAAC;YAED,wCAAwC;YACxC,IAAI,OAAO,IAAI,MAAM,CAAC,QAAQ,EAAE,CAAC;gBAC/B,OAAO,MAAM,CAAC,CAAC,uCAAuC;YACxD,CAAC;YAED,OAAO,EAAE,CAAC;YAEV,kBAAkB;YAClB,IAAI,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;YACzB,IAAI,MAAM,CAAC,OAAO,KAAK,aAAa,EAAE,CAAC;gBACrC,KAAK,GAAG,MAAM,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,CAAC,CAAC;YAClD,CAAC;YAED,0CAA0C;YAC1C,IAAI,CAAC;gBACH,MAAM,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;YAClC,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACb,IAAI,MAAM,EAAE,OAAO,EAAE,CAAC;oBACpB,OAAO;wBACL,MAAM,EAAE,WAAW;wBACnB,OAAO,EAAE,mCAAmC;qBAC7C,CAAC;gBACJ,CAAC;gBACD,MAAM,CAAC,CAAC;YACR,CAAC;QACH,CAAC;IACH,CAAC;IAEO,KAAK,CAAC,EAAU,EAAE,MAAoB;QAC5C,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrC,IAAI,MAAM,EAAE,OAAO,EAAE,CAAC;gBACpB,MAAM,CAAC,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC;gBAChC,OAAO;YACT,CAAC;YAED,MAAM,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE;gBAC5B,OAAO,EAAE,CAAC;gBACV,OAAO,EAAE,CAAC;YACZ,CAAC,EAAE,EAAE,CAAC,CAAC;YAEP,MAAM,OAAO,GAAG,GAAG,EAAE;gBACnB,YAAY,CAAC,KAAK,CAAC,CAAC;gBACpB,OAAO,EAAE,CAAC;gBACV,MAAM,CAAC,IAAI,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC;YAClC,CAAC,CAAC;YAEF,MAAM,OAAO,GAAG,GAAG,EAAE;gBACnB,MAAM,EAAE,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;YAChD,CAAC,CAAC;YAEF,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QAC7C,CAAC,CAAC,CAAC;IACL,CAAC;CACF"}
@@ -4,11 +4,11 @@
4
4
  The current implementation executes *all* independent tasks in parallel. In large graphs with many independent nodes, this could overwhelm system resources (CPU, memory) or trigger external API rate limits.
5
5
 
6
6
  ## What Changes
7
- - Add a concurrency control mechanism to the `TaskRunner`.
8
- - Add `concurrency: number` to the `TaskRunnerExecutionConfig`.
9
- - Implement a task queue to hold tasks that are ready but waiting for a free slot.
10
- - Update `WorkflowExecutor` to respect the concurrency limit.
7
+ - Add `concurrency: number` optional property to `WorkflowExecutor`.
8
+ - Modify `WorkflowExecutor.processLoop` to respect the concurrency limit.
9
+ - Track the active promise count.
10
+ - Only start new tasks from the ready queue if `active < concurrency`.
11
+ - Continue to defer ready tasks until slots open up.
11
12
 
12
13
  ## Impact
13
- - Affected specs: `task-runner`
14
- - Affected code: `src/TaskRunner.ts`, `src/WorkflowExecutor.ts`, `src/TaskRunnerExecutionConfig.ts`
14
+ - **Affected Components**: `WorkflowExecutor`
@@ -4,10 +4,13 @@
4
4
  Tasks currently run once and fail immediately if they throw an error or return a failure status. Network glitches or transient issues can therefore cause an entire workflow to fail unnecessarily.
5
5
 
6
6
  ## What Changes
7
- - Allow tasks to define a retry policy.
8
- - Update `TaskStep` interface to include optional `retry` configuration.
9
- - Implement logic in `TaskRunner` (or `WorkflowExecutor`) to catch failures, check the retry policy, and re-execute the task after the specified delay.
7
+ - Add `TaskRetryConfig` interface to define retry behavior (attempts, delay, backoff).
8
+ - Update `TaskStep` interface to include optional `retry: TaskRetryConfig`.
9
+ - Implement `RetryingExecutionStrategy` which decorates any `IExecutionStrategy`.
10
+ - It catches failures from the inner strategy.
11
+ - It checks the retry policy.
12
+ - It waits and re-executes the step if applicable.
10
13
 
11
14
  ## Impact
12
- - Affected specs: `task-runner`
13
- - Affected code: `src/TaskStep.ts`, `src/WorkflowExecutor.ts` (or `TaskRunner.ts`), `src/contracts/TaskRetryConfig.ts`
15
+ - **New Components**: `RetryingExecutionStrategy`, `TaskRetryConfig`
16
+ - **Affected Components**: `TaskStep` (interface update)
@@ -0,0 +1,13 @@
1
+ # Change: Add Workflow Preview
2
+
3
+ ## Why
4
+ It can be difficult to understand the execution flow of complex dependency graphs just by looking at the code. Users also currently cannot easily verify the execution plan without running the side effects, which carries risk.
5
+
6
+ ## What Changes
7
+ - Add a `DryRunExecutionStrategy` which implements `IExecutionStrategy`. This allows `WorkflowExecutor` to simulate execution without side effects.
8
+ - Add a standalone utility `generateMermaidGraph(steps: TaskStep[])` to generate a Mermaid.js diagram of the dependency graph.
9
+ - Expose these features via the main `TaskRunner` facade if applicable, or as separate utilities.
10
+
11
+ ## Impact
12
+ - **New Components**: `DryRunExecutionStrategy`, `generateMermaidGraph`
13
+ - **Affected Components**: `WorkflowExecutor` (indirectly, via strategy injection)
@@ -0,0 +1,7 @@
1
+ ## Implementation
2
+ - [x] 1.1 Update `TaskRunnerExecutionConfig` to include an optional `dryRun: boolean` property.
3
+ - [x] 1.2 Implement `dryRun` logic in `WorkflowExecutor` (traverse graph, validate order, skip `step.run()`, return `skipped` or `success` pseudo-status).
4
+ - [x] 1.3 Implement `getMermaidGraph(steps: TaskStep[])` method (can be static or instance method on `TaskRunner`).
5
+ - [x] 1.4 Ensure `dryRun` respects other configs like `concurrency` (if applicable) to simulate actual timing/order if possible, or just strict topological order.
6
+ - [x] 1.5 Add unit tests for `dryRun` ensuring no side effects occur.
7
+ - [x] 1.6 Add unit tests for `getMermaidGraph` output correctness (nodes and edges).
@@ -0,0 +1,25 @@
1
+ # Feature: Per-Task Timeout
2
+
3
+ ## 🎯 User Story
4
+ "As a developer, I want to define a maximum execution time for specific tasks so that a single hung task (e.g., a stalled network request) fails fast without blocking the rest of the independent tasks or waiting for the global workflow timeout."
5
+
6
+ ## ❓ Why
7
+ Currently, the `TaskRunner` allows a **global** timeout for the entire `execute()` call. However, this is insufficient for granular control:
8
+ 1. **Varying Latency**: Some tasks are expected to be fast (local validation), others slow (data fetching). A global timeout of 30s is too loose for the fast ones.
9
+ 2. **Boilerplate**: Developers currently have to manually implement `setTimeout`, `Promise.race`, and `AbortController` logic inside every `run()` method to handle timeouts properly.
10
+ 3. **Resilience**: A single "zombie" task can hold up the entire pipeline until the global timeout kills everything. Per-task timeouts allow failing that specific task (and skipping its dependents) while letting other independent tasks continue.
11
+
12
+ ## 🛠️ What Changes
13
+ 1. **Interface Update**: Update `TaskStep<T>` to accept an optional `timeout` property (in milliseconds).
14
+ 2. **Execution Strategy**: Update `StandardExecutionStrategy` to:
15
+ - Create a local timeout timer for the task.
16
+ - Create a combined `AbortSignal` (merging the workflow's signal and the local timeout).
17
+ - Race the task execution against the timer.
18
+ - Return a specific failure result if the timeout wins.
19
+
20
+ ## ✅ Acceptance Criteria
21
+ - [ ] A task with `timeout: 100` must fail if the `run` method takes > 100ms.
22
+ - [ ] The error message for a timed-out task should clearly state "Task timed out after 100ms".
23
+ - [ ] The `AbortSignal` passed to the task's `run` method must be triggered when the timeout occurs.
24
+ - [ ] If the Global Workflow is cancelled *before* the task times out, the task should receive the cancellation signal immediately.
25
+ - [ ] A task completing *before* the timeout should clear the timer to prevent open handles.
@@ -0,0 +1,27 @@
1
+ # Engineering Tasks
2
+
3
+ - [ ] **Task 1: Update Interface**
4
+ - Modify `src/TaskStep.ts` to add `timeout?: number;` to the `TaskStep` interface.
5
+ - Document the property (milliseconds).
6
+
7
+ - [ ] **Task 2: Add Timeout Logic to StandardExecutionStrategy**
8
+ - Modify `src/strategies/StandardExecutionStrategy.ts`.
9
+ - Inside `execute`:
10
+ - Check if `step.timeout` is defined.
11
+ - If yes, create an `AbortController`.
12
+ - Set a `setTimeout` to trigger the controller.
13
+ - Use `Promise.race` (or simply pass the new signal and wait) to handle the timeout.
14
+ - **Crucial**: Ensure the new signal respects the *parent* `signal` (if global cancel happens, local signal must also abort).
15
+ - **Crucial**: Clean up the timer (`clearTimeout`) in a `finally` block.
16
+
17
+ - [ ] **Task 3: Unit Tests**
18
+ - Create `tests/strategies/StandardExecutionStrategy.timeout.test.ts`.
19
+ - Test case: Task finishes before timeout (success).
20
+ - Test case: Task runs longer than timeout (failure/error).
21
+ - Test case: Task receives AbortSignal on timeout.
22
+ - Test case: Global cancellation overrides local timeout.
23
+
24
+ - [ ] **Task 4: Integration Test**
25
+ - Update `tests/TaskRunner.test.ts` or create `tests/timeouts.test.ts`.
26
+ - Define a workflow with a slow task and a short timeout.
27
+ - Verify that the slow task fails, dependents are skipped, and independent tasks still complete (if any).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@calmo/task-runner",
3
- "version": "3.0.0",
3
+ "version": "3.2.0",
4
4
  "description": "",
5
5
  "repository": {
6
6
  "type": "git",
package/src/TaskRunner.ts CHANGED
@@ -9,6 +9,8 @@ import { TaskRunnerExecutionConfig } from "./TaskRunnerExecutionConfig.js";
9
9
  import { TaskStateManager } from "./TaskStateManager.js";
10
10
  import { IExecutionStrategy } from "./strategies/IExecutionStrategy.js";
11
11
  import { StandardExecutionStrategy } from "./strategies/StandardExecutionStrategy.js";
12
+ import { RetryingExecutionStrategy } from "./strategies/RetryingExecutionStrategy.js";
13
+ import { DryRunExecutionStrategy } from "./strategies/DryRunExecutionStrategy.js";
12
14
 
13
15
  // Re-export types for backward compatibility
14
16
  export { RunnerEventPayloads, RunnerEventListener, TaskRunnerExecutionConfig };
@@ -21,7 +23,9 @@ export { RunnerEventPayloads, RunnerEventListener, TaskRunnerExecutionConfig };
21
23
  export class TaskRunner<TContext> {
22
24
  private eventBus = new EventBus<TContext>();
23
25
  private validator = new TaskGraphValidator();
24
- private executionStrategy: IExecutionStrategy<TContext> = new StandardExecutionStrategy();
26
+ private executionStrategy: IExecutionStrategy<TContext> = new RetryingExecutionStrategy(
27
+ new StandardExecutionStrategy()
28
+ );
25
29
 
26
30
  /**
27
31
  * @param context The shared context object to be passed to each task.
@@ -64,6 +68,54 @@ export class TaskRunner<TContext> {
64
68
  return this;
65
69
  }
66
70
 
71
+ /**
72
+ * Generates a Mermaid.js graph representation of the task workflow.
73
+ * @param steps The list of tasks to visualize.
74
+ * @returns A string containing the Mermaid graph definition.
75
+ */
76
+ public static getMermaidGraph<T>(steps: TaskStep<T>[]): string {
77
+ const graphLines = ["graph TD"];
78
+
79
+ // Helper to sanitize node names or wrap them if needed
80
+ // For simplicity, we just wrap in quotes and use the name as ID if it's simple
81
+ // or generate an ID if strictly needed. Here we assume names are unique IDs.
82
+ // We will wrap names in quotes for the label, but use the name as the ID.
83
+ // Actually, Mermaid ID cannot have spaces without quotes.
84
+ const safeId = (name: string) => JSON.stringify(name);
85
+ const sanitize = (name: string) => this.sanitizeMermaidId(name);
86
+
87
+
88
+ // Add all nodes first to ensure they exist
89
+ for (const step of steps) {
90
+ // Using the name as both ID and Label for simplicity
91
+ // Format: ID["Label"]
92
+ // safeId returns a quoted string (e.g. "Task Name"), so we use it directly as the label
93
+ graphLines.push(` ${sanitize(step.name)}[${safeId(step.name)}]`);
94
+ }
95
+
96
+ // Add edges
97
+ for (const step of steps) {
98
+ if (step.dependencies) {
99
+ for (const dep of step.dependencies) {
100
+ graphLines.push(
101
+ ` ${sanitize(dep)} --> ${sanitize(step.name)}`
102
+ );
103
+ }
104
+ }
105
+ }
106
+
107
+ return [...new Set(graphLines)].join("\n");
108
+ }
109
+
110
+ /**
111
+ * Sanitizes a string for use as a Mermaid node ID.
112
+ * @param id The string to sanitize.
113
+ * @returns The sanitized string.
114
+ */
115
+ private static sanitizeMermaidId(id: string): string {
116
+ return id.replaceAll(/ /g, "_").replaceAll(/:/g, "_").replaceAll(/"/g, "_");
117
+ }
118
+
67
119
  /**
68
120
  * Executes a list of tasks, respecting their dependencies and running
69
121
  * independent tasks in parallel.
@@ -90,11 +142,17 @@ export class TaskRunner<TContext> {
90
142
  }
91
143
 
92
144
  const stateManager = new TaskStateManager(this.eventBus);
145
+
146
+ let strategy = this.executionStrategy;
147
+ if (config?.dryRun) {
148
+ strategy = new DryRunExecutionStrategy<TContext>();
149
+ }
150
+
93
151
  const executor = new WorkflowExecutor(
94
152
  this.context,
95
153
  this.eventBus,
96
154
  stateManager,
97
- this.executionStrategy
155
+ strategy
98
156
  );
99
157
 
100
158
  // We need to handle the timeout cleanup properly.
@@ -10,4 +10,9 @@ export interface TaskRunnerExecutionConfig {
10
10
  * A timeout in milliseconds for the entire workflow.
11
11
  */
12
12
  timeout?: number;
13
+ /**
14
+ * If true, the runner will simulate execution without running the actual tasks.
15
+ * Useful for verifying the execution order and graph structure.
16
+ */
17
+ dryRun?: boolean;
13
18
  }
package/src/TaskStep.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { TaskResult } from "./TaskResult.js";
2
+ import { TaskRetryConfig } from "./contracts/TaskRetryConfig.js";
2
3
 
3
4
  /**
4
5
  * Represents a single, executable step within a workflow.
@@ -9,6 +10,8 @@ export interface TaskStep<TContext> {
9
10
  name: string;
10
11
  /** An optional list of task names that must complete successfully before this step can run. */
11
12
  dependencies?: string[];
13
+ /** Optional retry configuration for the task. */
14
+ retry?: TaskRetryConfig;
12
15
  /**
13
16
  * The core logic of the task.
14
17
  * @param context The shared context object, allowing for state to be passed between tasks.
@@ -0,0 +1,8 @@
1
+ export interface TaskRetryConfig {
2
+ /** Number of retries (excluding the initial run). */
3
+ attempts: number;
4
+ /** Delay in milliseconds between retries. */
5
+ delay: number;
6
+ /** Backoff strategy: 'fixed' (default) or 'exponential'. */
7
+ backoff?: "fixed" | "exponential";
8
+ }
package/src/index.ts CHANGED
@@ -2,7 +2,9 @@ export { TaskRunner } from "./TaskRunner.js";
2
2
  export { TaskRunnerBuilder } from "./TaskRunnerBuilder.js";
3
3
  export { TaskStateManager } from "./TaskStateManager.js";
4
4
  export { StandardExecutionStrategy } from "./strategies/StandardExecutionStrategy.js";
5
+ export { RetryingExecutionStrategy } from "./strategies/RetryingExecutionStrategy.js";
5
6
  export type { IExecutionStrategy } from "./strategies/IExecutionStrategy.js";
7
+ export type { TaskRetryConfig } from "./contracts/TaskRetryConfig.js";
6
8
  export type { TaskStep } from "./TaskStep.js";
7
9
  export type { TaskResult } from "./TaskResult.js";
8
10
  export type { TaskStatus } from "./TaskStatus.js";
@@ -0,0 +1,31 @@
1
+ import { IExecutionStrategy } from "./IExecutionStrategy.js";
2
+ import { TaskStep } from "../TaskStep.js";
3
+ import { TaskResult } from "../TaskResult.js";
4
+
5
+ /**
6
+ * Execution strategy that simulates task execution without running the actual logic.
7
+ */
8
+ export class DryRunExecutionStrategy<TContext>
9
+ implements IExecutionStrategy<TContext>
10
+ {
11
+ /**
12
+ * Simulates execution by returning a success result immediately.
13
+ * @param step The task step (ignored).
14
+ * @param context The shared context (ignored).
15
+ * @param signal Optional abort signal (ignored).
16
+ * @returns A promise resolving to a success result.
17
+ */
18
+ async execute(
19
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
20
+ _step: TaskStep<TContext>,
21
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
22
+ _context: TContext,
23
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
24
+ _signal?: AbortSignal
25
+ ): Promise<TaskResult> {
26
+ return Promise.resolve({
27
+ status: "success",
28
+ message: "Dry run: simulated success",
29
+ });
30
+ }
31
+ }
@@ -0,0 +1,90 @@
1
+ import { IExecutionStrategy } from "./IExecutionStrategy.js";
2
+ import { TaskStep } from "../TaskStep.js";
3
+ import { TaskResult } from "../TaskResult.js";
4
+
5
+ /**
6
+ * Execution strategy that retries tasks upon failure based on their retry configuration.
7
+ */
8
+ export class RetryingExecutionStrategy<TContext> implements IExecutionStrategy<TContext> {
9
+ constructor(private innerStrategy: IExecutionStrategy<TContext>) {}
10
+
11
+ async execute(
12
+ step: TaskStep<TContext>,
13
+ context: TContext,
14
+ signal?: AbortSignal
15
+ ): Promise<TaskResult> {
16
+ const config = step.retry;
17
+ if (!config) {
18
+ return this.innerStrategy.execute(step, context, signal);
19
+ }
20
+
21
+ let attempt = 0;
22
+ while (true) {
23
+ // Check for cancellation before execution
24
+ if (signal?.aborted) {
25
+ return {
26
+ status: "cancelled",
27
+ message: "Task cancelled before execution",
28
+ };
29
+ }
30
+
31
+ const result = await this.innerStrategy.execute(step, context, signal);
32
+
33
+ if (result.status === "success" || result.status === "cancelled" || result.status === "skipped") {
34
+ return result;
35
+ }
36
+
37
+ // Task failed, check if we should retry
38
+ if (attempt >= config.attempts) {
39
+ return result; // Max attempts reached, return failure
40
+ }
41
+
42
+ attempt++;
43
+
44
+ // Calculate delay
45
+ let delay = config.delay;
46
+ if (config.backoff === "exponential") {
47
+ delay = config.delay * Math.pow(2, attempt - 1);
48
+ }
49
+
50
+ // Wait for delay, respecting cancellation
51
+ try {
52
+ await this.sleep(delay, signal);
53
+ } catch (e) {
54
+ if (signal?.aborted) {
55
+ return {
56
+ status: "cancelled",
57
+ message: "Task cancelled during retry delay",
58
+ };
59
+ }
60
+ throw e;
61
+ }
62
+ }
63
+ }
64
+
65
+ private sleep(ms: number, signal?: AbortSignal): Promise<void> {
66
+ return new Promise((resolve, reject) => {
67
+ if (signal?.aborted) {
68
+ reject(new Error("AbortError"));
69
+ return;
70
+ }
71
+
72
+ const timer = setTimeout(() => {
73
+ cleanup();
74
+ resolve();
75
+ }, ms);
76
+
77
+ const onAbort = () => {
78
+ clearTimeout(timer);
79
+ cleanup();
80
+ reject(new Error("AbortError"));
81
+ };
82
+
83
+ const cleanup = () => {
84
+ signal?.removeEventListener("abort", onAbort);
85
+ };
86
+
87
+ signal?.addEventListener("abort", onAbort);
88
+ });
89
+ }
90
+ }