@calmo/task-runner 3.0.0 → 3.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.
Files changed (49) hide show
  1. package/.jules/nexus.md +5 -0
  2. package/CHANGELOG.md +12 -0
  3. package/coverage/coverage-final.json +6 -5
  4. package/coverage/index.html +12 -12
  5. package/coverage/lcov-report/index.html +12 -12
  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 +184 -19
  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/StandardExecutionStrategy.ts.html +1 -1
  18. package/coverage/lcov-report/src/strategies/index.html +19 -4
  19. package/coverage/lcov.info +247 -209
  20. package/coverage/src/EventBus.ts.html +4 -4
  21. package/coverage/src/TaskGraphValidator.ts.html +38 -38
  22. package/coverage/src/TaskRunner.ts.html +184 -19
  23. package/coverage/src/TaskRunnerBuilder.ts.html +1 -1
  24. package/coverage/src/TaskRunnerExecutionConfig.ts.html +17 -2
  25. package/coverage/src/TaskStateManager.ts.html +34 -34
  26. package/coverage/src/WorkflowExecutor.ts.html +30 -30
  27. package/coverage/src/contracts/RunnerEvents.ts.html +1 -1
  28. package/coverage/src/contracts/index.html +1 -1
  29. package/coverage/src/index.html +9 -9
  30. package/coverage/src/strategies/DryRunExecutionStrategy.ts.html +178 -0
  31. package/coverage/src/strategies/StandardExecutionStrategy.ts.html +1 -1
  32. package/coverage/src/strategies/index.html +19 -4
  33. package/dist/TaskRunner.d.ts +12 -0
  34. package/dist/TaskRunner.js +45 -1
  35. package/dist/TaskRunner.js.map +1 -1
  36. package/dist/TaskRunnerExecutionConfig.d.ts +5 -0
  37. package/dist/strategies/DryRunExecutionStrategy.d.ts +16 -0
  38. package/dist/strategies/DryRunExecutionStrategy.js +25 -0
  39. package/dist/strategies/DryRunExecutionStrategy.js.map +1 -0
  40. package/openspec/changes/add-concurrency-control/proposal.md +6 -6
  41. package/openspec/changes/add-task-retry-policy/proposal.md +8 -5
  42. package/openspec/changes/add-workflow-preview/proposal.md +5 -4
  43. package/openspec/changes/feat-per-task-timeout/proposal.md +25 -0
  44. package/openspec/changes/feat-per-task-timeout/tasks.md +27 -0
  45. package/package.json +1 -1
  46. package/src/TaskRunner.ts +56 -1
  47. package/src/TaskRunnerExecutionConfig.ts +5 -0
  48. package/src/strategies/DryRunExecutionStrategy.ts +31 -0
  49. package/test-report.xml +102 -82
@@ -36,6 +36,18 @@ export declare class TaskRunner<TContext> {
36
36
  * @returns The TaskRunner instance for chaining.
37
37
  */
38
38
  setExecutionStrategy(strategy: IExecutionStrategy<TContext>): this;
39
+ /**
40
+ * Generates a Mermaid.js graph representation of the task workflow.
41
+ * @param steps The list of tasks to visualize.
42
+ * @returns A string containing the Mermaid graph definition.
43
+ */
44
+ static getMermaidGraph<T>(steps: TaskStep<T>[]): string;
45
+ /**
46
+ * Sanitizes a string for use as a Mermaid node ID.
47
+ * @param id The string to sanitize.
48
+ * @returns The sanitized string.
49
+ */
50
+ private static sanitizeMermaidId;
39
51
  /**
40
52
  * Executes a list of tasks, respecting their dependencies and running
41
53
  * independent tasks in parallel.
@@ -3,6 +3,7 @@ import { EventBus } from "./EventBus.js";
3
3
  import { WorkflowExecutor } from "./WorkflowExecutor.js";
4
4
  import { TaskStateManager } from "./TaskStateManager.js";
5
5
  import { StandardExecutionStrategy } from "./strategies/StandardExecutionStrategy.js";
6
+ import { DryRunExecutionStrategy } from "./strategies/DryRunExecutionStrategy.js";
6
7
  /**
7
8
  * The main class that orchestrates the execution of a list of tasks
8
9
  * based on their dependencies, with support for parallel execution.
@@ -46,6 +47,45 @@ export class TaskRunner {
46
47
  this.executionStrategy = strategy;
47
48
  return this;
48
49
  }
50
+ /**
51
+ * Generates a Mermaid.js graph representation of the task workflow.
52
+ * @param steps The list of tasks to visualize.
53
+ * @returns A string containing the Mermaid graph definition.
54
+ */
55
+ static getMermaidGraph(steps) {
56
+ const graphLines = ["graph TD"];
57
+ // Helper to sanitize node names or wrap them if needed
58
+ // For simplicity, we just wrap in quotes and use the name as ID if it's simple
59
+ // or generate an ID if strictly needed. Here we assume names are unique IDs.
60
+ // We will wrap names in quotes for the label, but use the name as the ID.
61
+ // Actually, Mermaid ID cannot have spaces without quotes.
62
+ const safeId = (name) => JSON.stringify(name);
63
+ const sanitize = (name) => this.sanitizeMermaidId(name);
64
+ // Add all nodes first to ensure they exist
65
+ for (const step of steps) {
66
+ // Using the name as both ID and Label for simplicity
67
+ // Format: ID["Label"]
68
+ // safeId returns a quoted string (e.g. "Task Name"), so we use it directly as the label
69
+ graphLines.push(` ${sanitize(step.name)}[${safeId(step.name)}]`);
70
+ }
71
+ // Add edges
72
+ for (const step of steps) {
73
+ if (step.dependencies) {
74
+ for (const dep of step.dependencies) {
75
+ graphLines.push(` ${sanitize(dep)} --> ${sanitize(step.name)}`);
76
+ }
77
+ }
78
+ }
79
+ return [...new Set(graphLines)].join("\n");
80
+ }
81
+ /**
82
+ * Sanitizes a string for use as a Mermaid node ID.
83
+ * @param id The string to sanitize.
84
+ * @returns The sanitized string.
85
+ */
86
+ static sanitizeMermaidId(id) {
87
+ return id.replaceAll(/ /g, "_").replaceAll(/:/g, "_").replaceAll(/"/g, "_");
88
+ }
49
89
  /**
50
90
  * Executes a list of tasks, respecting their dependencies and running
51
91
  * independent tasks in parallel.
@@ -67,7 +107,11 @@ export class TaskRunner {
67
107
  throw new Error(this.validator.createErrorMessage(validationResult));
68
108
  }
69
109
  const stateManager = new TaskStateManager(this.eventBus);
70
- const executor = new WorkflowExecutor(this.context, this.eventBus, stateManager, this.executionStrategy);
110
+ let strategy = this.executionStrategy;
111
+ if (config?.dryRun) {
112
+ strategy = new DryRunExecutionStrategy();
113
+ }
114
+ const executor = new WorkflowExecutor(this.context, this.eventBus, stateManager, strategy);
71
115
  // We need to handle the timeout cleanup properly.
72
116
  if (config?.timeout !== undefined) {
73
117
  const controller = new AbortController();
@@ -1 +1 @@
1
- {"version":3,"file":"TaskRunner.js","sourceRoot":"","sources":["../src/TaskRunner.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAG7D,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD,OAAO,EAAE,yBAAyB,EAAE,MAAM,2CAA2C,CAAC;AAKtF;;;;GAIG;AACH,MAAM,OAAO,UAAU;IAQD;IAPZ,QAAQ,GAAG,IAAI,QAAQ,EAAY,CAAC;IACpC,SAAS,GAAG,IAAI,kBAAkB,EAAE,CAAC;IACrC,iBAAiB,GAAiC,IAAI,yBAAyB,EAAE,CAAC;IAE1F;;OAEG;IACH,YAAoB,OAAiB;QAAjB,YAAO,GAAP,OAAO,CAAU;IAAG,CAAC;IAEzC;;;;OAIG;IACI,EAAE,CACP,KAAQ,EACR,QAA0C;QAE1C,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;IACpC,CAAC;IAED;;;;OAIG;IACI,GAAG,CACR,KAAQ,EACR,QAA0C;QAE1C,sBAAsB;QACtB,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,QAAQ,CAAC,CAA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E;wBACZ,UAAU,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;oBAC3C,CAAC,CAAC;oBACF,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;gBACpD,CAAC;YACJ,CAAC;YAED,IAAI,CAAC;gBACH,OAAO,MAAM,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,eAAe,CAAC,CAAC;YACxD,CAAC;oBAAS,CAAC;gBACT,YAAY,CAAC,SAAS,CAAC,CAAC;gBACxB,IAAI,MAAM,CAAC,MAAM,IAAI,OAAO,EAAE,CAAC;oBAC5B,MAAM,CAAC,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;gBACvD,CAAC;YACH,CAAC;QACJ,CAAC;aAAM,CAAC;YACL,OAAO,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;QAClD,CAAC;IACH,CAAC;CACF"}
@@ -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
  }
@@ -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"}
@@ -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)
@@ -4,9 +4,10 @@
4
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
5
 
6
6
  ## What Changes
7
- - Add a `dryRun` execution mode to `TaskRunner`.
8
- - Add a helper method `getMermaidGraph(steps)` to generate a Mermaid.js diagram of the dependency graph.
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.
9
10
 
10
11
  ## Impact
11
- - Affected specs: `task-runner`
12
- - Affected code: `src/TaskRunner.ts`, `src/TaskRunnerExecutionConfig.ts`
12
+ - **New Components**: `DryRunExecutionStrategy`, `generateMermaidGraph`
13
+ - **Affected Components**: `WorkflowExecutor` (indirectly, via strategy injection)
@@ -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.1.0",
4
4
  "description": "",
5
5
  "repository": {
6
6
  "type": "git",
package/src/TaskRunner.ts CHANGED
@@ -9,6 +9,7 @@ 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 { DryRunExecutionStrategy } from "./strategies/DryRunExecutionStrategy.js";
12
13
 
13
14
  // Re-export types for backward compatibility
14
15
  export { RunnerEventPayloads, RunnerEventListener, TaskRunnerExecutionConfig };
@@ -64,6 +65,54 @@ export class TaskRunner<TContext> {
64
65
  return this;
65
66
  }
66
67
 
68
+ /**
69
+ * Generates a Mermaid.js graph representation of the task workflow.
70
+ * @param steps The list of tasks to visualize.
71
+ * @returns A string containing the Mermaid graph definition.
72
+ */
73
+ public static getMermaidGraph<T>(steps: TaskStep<T>[]): string {
74
+ const graphLines = ["graph TD"];
75
+
76
+ // Helper to sanitize node names or wrap them if needed
77
+ // For simplicity, we just wrap in quotes and use the name as ID if it's simple
78
+ // or generate an ID if strictly needed. Here we assume names are unique IDs.
79
+ // We will wrap names in quotes for the label, but use the name as the ID.
80
+ // Actually, Mermaid ID cannot have spaces without quotes.
81
+ const safeId = (name: string) => JSON.stringify(name);
82
+ const sanitize = (name: string) => this.sanitizeMermaidId(name);
83
+
84
+
85
+ // Add all nodes first to ensure they exist
86
+ for (const step of steps) {
87
+ // Using the name as both ID and Label for simplicity
88
+ // Format: ID["Label"]
89
+ // safeId returns a quoted string (e.g. "Task Name"), so we use it directly as the label
90
+ graphLines.push(` ${sanitize(step.name)}[${safeId(step.name)}]`);
91
+ }
92
+
93
+ // Add edges
94
+ for (const step of steps) {
95
+ if (step.dependencies) {
96
+ for (const dep of step.dependencies) {
97
+ graphLines.push(
98
+ ` ${sanitize(dep)} --> ${sanitize(step.name)}`
99
+ );
100
+ }
101
+ }
102
+ }
103
+
104
+ return [...new Set(graphLines)].join("\n");
105
+ }
106
+
107
+ /**
108
+ * Sanitizes a string for use as a Mermaid node ID.
109
+ * @param id The string to sanitize.
110
+ * @returns The sanitized string.
111
+ */
112
+ private static sanitizeMermaidId(id: string): string {
113
+ return id.replaceAll(/ /g, "_").replaceAll(/:/g, "_").replaceAll(/"/g, "_");
114
+ }
115
+
67
116
  /**
68
117
  * Executes a list of tasks, respecting their dependencies and running
69
118
  * independent tasks in parallel.
@@ -90,11 +139,17 @@ export class TaskRunner<TContext> {
90
139
  }
91
140
 
92
141
  const stateManager = new TaskStateManager(this.eventBus);
142
+
143
+ let strategy = this.executionStrategy;
144
+ if (config?.dryRun) {
145
+ strategy = new DryRunExecutionStrategy<TContext>();
146
+ }
147
+
93
148
  const executor = new WorkflowExecutor(
94
149
  this.context,
95
150
  this.eventBus,
96
151
  stateManager,
97
- this.executionStrategy
152
+ strategy
98
153
  );
99
154
 
100
155
  // 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
  }
@@ -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
+ }