@tagma/sdk 0.7.3 → 0.7.5

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 (230) hide show
  1. package/README.md +85 -57
  2. package/dist/approval.d.ts +2 -12
  3. package/dist/approval.d.ts.map +1 -1
  4. package/dist/approval.js +1 -90
  5. package/dist/approval.js.map +1 -1
  6. package/dist/bootstrap.d.ts +1 -1
  7. package/dist/bootstrap.d.ts.map +1 -1
  8. package/dist/completions/file-exists.js +1 -1
  9. package/dist/completions/file-exists.js.map +1 -1
  10. package/dist/completions/output-check.d.ts.map +1 -1
  11. package/dist/completions/output-check.js +17 -4
  12. package/dist/completions/output-check.js.map +1 -1
  13. package/dist/config.d.ts +4 -4
  14. package/dist/config.d.ts.map +1 -1
  15. package/dist/config.js +2 -2
  16. package/dist/config.js.map +1 -1
  17. package/dist/dataflow.d.ts +3 -0
  18. package/dist/dataflow.d.ts.map +1 -0
  19. package/dist/dataflow.js +2 -0
  20. package/dist/dataflow.js.map +1 -0
  21. package/dist/drivers/opencode.d.ts.map +1 -1
  22. package/dist/drivers/opencode.js +23 -71
  23. package/dist/drivers/opencode.js.map +1 -1
  24. package/dist/engine.d.ts +5 -56
  25. package/dist/engine.d.ts.map +1 -1
  26. package/dist/engine.js +7 -297
  27. package/dist/engine.js.map +1 -1
  28. package/dist/index.d.ts +4 -6
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +2 -4
  31. package/dist/index.js.map +1 -1
  32. package/dist/logger.d.ts +2 -60
  33. package/dist/logger.d.ts.map +1 -1
  34. package/dist/logger.js +1 -153
  35. package/dist/logger.js.map +1 -1
  36. package/dist/middlewares/static-context.d.ts.map +1 -1
  37. package/dist/middlewares/static-context.js +1 -2
  38. package/dist/middlewares/static-context.js.map +1 -1
  39. package/dist/pipeline-runner.d.ts.map +1 -1
  40. package/dist/pipeline-runner.js +2 -2
  41. package/dist/pipeline-runner.js.map +1 -1
  42. package/dist/plugins.d.ts +2 -2
  43. package/dist/plugins.d.ts.map +1 -1
  44. package/dist/plugins.js +1 -1
  45. package/dist/plugins.js.map +1 -1
  46. package/dist/runner.d.ts +1 -35
  47. package/dist/runner.d.ts.map +1 -1
  48. package/dist/runner.js +1 -610
  49. package/dist/runner.js.map +1 -1
  50. package/dist/runtime/adapters/stdin-approval.d.ts +2 -0
  51. package/dist/runtime/adapters/stdin-approval.d.ts.map +1 -0
  52. package/dist/runtime/adapters/stdin-approval.js +2 -0
  53. package/dist/runtime/adapters/stdin-approval.js.map +1 -0
  54. package/dist/runtime/adapters/websocket-approval.d.ts +2 -0
  55. package/dist/runtime/adapters/websocket-approval.d.ts.map +1 -0
  56. package/dist/runtime/adapters/websocket-approval.js +2 -0
  57. package/dist/runtime/adapters/websocket-approval.js.map +1 -0
  58. package/dist/runtime/bun-process-runner.d.ts +2 -0
  59. package/dist/runtime/bun-process-runner.d.ts.map +1 -0
  60. package/dist/runtime/bun-process-runner.js +2 -0
  61. package/dist/runtime/bun-process-runner.js.map +1 -0
  62. package/dist/runtime.d.ts +2 -8
  63. package/dist/runtime.d.ts.map +1 -1
  64. package/dist/runtime.js +1 -7
  65. package/dist/runtime.js.map +1 -1
  66. package/dist/schema.d.ts.map +1 -1
  67. package/dist/schema.js +3 -4
  68. package/dist/schema.js.map +1 -1
  69. package/dist/tagma.d.ts +3 -4
  70. package/dist/tagma.d.ts.map +1 -1
  71. package/dist/tagma.js +2 -3
  72. package/dist/tagma.js.map +1 -1
  73. package/dist/triggers/file.d.ts.map +1 -1
  74. package/dist/triggers/file.js +74 -108
  75. package/dist/triggers/file.js.map +1 -1
  76. package/dist/triggers/manual.d.ts.map +1 -1
  77. package/dist/triggers/manual.js +1 -2
  78. package/dist/triggers/manual.js.map +1 -1
  79. package/dist/types.d.ts +1 -2
  80. package/dist/types.d.ts.map +1 -1
  81. package/dist/types.js +1 -12
  82. package/dist/types.js.map +1 -1
  83. package/dist/utils-api.d.ts +1 -1
  84. package/dist/utils-api.d.ts.map +1 -1
  85. package/dist/utils-api.js +1 -1
  86. package/dist/utils-api.js.map +1 -1
  87. package/dist/validate-raw.d.ts.map +1 -1
  88. package/dist/validate-raw.js +5 -12
  89. package/dist/validate-raw.js.map +1 -1
  90. package/package.json +20 -22
  91. package/dist/adapters/stdin-approval.d.ts +0 -6
  92. package/dist/adapters/stdin-approval.d.ts.map +0 -1
  93. package/dist/adapters/stdin-approval.js +0 -90
  94. package/dist/adapters/stdin-approval.js.map +0 -1
  95. package/dist/adapters/websocket-approval.d.ts +0 -28
  96. package/dist/adapters/websocket-approval.d.ts.map +0 -1
  97. package/dist/adapters/websocket-approval.js +0 -147
  98. package/dist/adapters/websocket-approval.js.map +0 -1
  99. package/dist/core/dataflow.d.ts +0 -23
  100. package/dist/core/dataflow.d.ts.map +0 -1
  101. package/dist/core/dataflow.js +0 -99
  102. package/dist/core/dataflow.js.map +0 -1
  103. package/dist/core/log-prune.d.ts +0 -16
  104. package/dist/core/log-prune.d.ts.map +0 -1
  105. package/dist/core/log-prune.js +0 -34
  106. package/dist/core/log-prune.js.map +0 -1
  107. package/dist/core/preflight.d.ts +0 -13
  108. package/dist/core/preflight.d.ts.map +0 -1
  109. package/dist/core/preflight.js +0 -61
  110. package/dist/core/preflight.js.map +0 -1
  111. package/dist/core/run-context.d.ts +0 -55
  112. package/dist/core/run-context.d.ts.map +0 -1
  113. package/dist/core/run-context.js +0 -158
  114. package/dist/core/run-context.js.map +0 -1
  115. package/dist/core/run-state.d.ts +0 -25
  116. package/dist/core/run-state.d.ts.map +0 -1
  117. package/dist/core/run-state.js +0 -93
  118. package/dist/core/run-state.js.map +0 -1
  119. package/dist/core/scheduler.d.ts +0 -13
  120. package/dist/core/scheduler.d.ts.map +0 -1
  121. package/dist/core/scheduler.js +0 -35
  122. package/dist/core/scheduler.js.map +0 -1
  123. package/dist/core/task-executor.d.ts +0 -13
  124. package/dist/core/task-executor.d.ts.map +0 -1
  125. package/dist/core/task-executor.js +0 -601
  126. package/dist/core/task-executor.js.map +0 -1
  127. package/dist/core/trigger-errors.d.ts +0 -9
  128. package/dist/core/trigger-errors.d.ts.map +0 -1
  129. package/dist/core/trigger-errors.js +0 -15
  130. package/dist/core/trigger-errors.js.map +0 -1
  131. package/dist/dag.d.ts +0 -45
  132. package/dist/dag.d.ts.map +0 -1
  133. package/dist/dag.js +0 -177
  134. package/dist/dag.js.map +0 -1
  135. package/dist/hooks.d.ts +0 -73
  136. package/dist/hooks.d.ts.map +0 -1
  137. package/dist/hooks.js +0 -106
  138. package/dist/hooks.js.map +0 -1
  139. package/dist/pipeline-definition.d.ts +0 -3
  140. package/dist/pipeline-definition.d.ts.map +0 -1
  141. package/dist/pipeline-definition.js +0 -4
  142. package/dist/pipeline-definition.js.map +0 -1
  143. package/dist/ports.d.ts +0 -196
  144. package/dist/ports.d.ts.map +0 -1
  145. package/dist/ports.js +0 -688
  146. package/dist/ports.js.map +0 -1
  147. package/dist/prompt-doc.d.ts +0 -70
  148. package/dist/prompt-doc.d.ts.map +0 -1
  149. package/dist/prompt-doc.js +0 -154
  150. package/dist/prompt-doc.js.map +0 -1
  151. package/dist/registry.d.ts +0 -67
  152. package/dist/registry.d.ts.map +0 -1
  153. package/dist/registry.js +0 -293
  154. package/dist/registry.js.map +0 -1
  155. package/dist/task-ref.d.ts +0 -55
  156. package/dist/task-ref.d.ts.map +0 -1
  157. package/dist/task-ref.js +0 -103
  158. package/dist/task-ref.js.map +0 -1
  159. package/dist/utils.d.ts +0 -13
  160. package/dist/utils.d.ts.map +0 -1
  161. package/dist/utils.js +0 -177
  162. package/dist/utils.js.map +0 -1
  163. package/src/adapters/stdin-approval.ts +0 -106
  164. package/src/adapters/websocket-approval.ts +0 -224
  165. package/src/approval.ts +0 -131
  166. package/src/bootstrap.ts +0 -55
  167. package/src/completions/exit-code.ts +0 -34
  168. package/src/completions/file-exists.ts +0 -66
  169. package/src/completions/output-check.test.ts +0 -50
  170. package/src/completions/output-check.ts +0 -92
  171. package/src/config-ops.test.ts +0 -70
  172. package/src/config-ops.ts +0 -328
  173. package/src/config.ts +0 -26
  174. package/src/core/dataflow.test.ts +0 -166
  175. package/src/core/dataflow.ts +0 -161
  176. package/src/core/log-prune.test.ts +0 -58
  177. package/src/core/log-prune.ts +0 -43
  178. package/src/core/preflight.test.ts +0 -49
  179. package/src/core/preflight.ts +0 -89
  180. package/src/core/run-context.test.ts +0 -256
  181. package/src/core/run-context.ts +0 -211
  182. package/src/core/run-state.test.ts +0 -98
  183. package/src/core/run-state.ts +0 -122
  184. package/src/core/scheduler.test.ts +0 -83
  185. package/src/core/scheduler.ts +0 -42
  186. package/src/core/task-executor.ts +0 -743
  187. package/src/core/trigger-errors.ts +0 -15
  188. package/src/dag.test.ts +0 -56
  189. package/src/dag.ts +0 -245
  190. package/src/drivers/opencode.ts +0 -410
  191. package/src/engine-ports-mixed.test.ts +0 -156
  192. package/src/engine-ports.test.ts +0 -166
  193. package/src/engine-task-type.test.ts +0 -56
  194. package/src/engine.ts +0 -458
  195. package/src/hooks.ts +0 -193
  196. package/src/index.ts +0 -33
  197. package/src/logger.ts +0 -182
  198. package/src/middlewares/static-context.ts +0 -49
  199. package/src/pipeline-definition.ts +0 -5
  200. package/src/pipeline-runner.test.ts +0 -91
  201. package/src/pipeline-runner.ts +0 -194
  202. package/src/plugin-registry.test.ts +0 -382
  203. package/src/plugins.ts +0 -21
  204. package/src/ports.test.ts +0 -678
  205. package/src/ports.ts +0 -925
  206. package/src/prompt-doc.test.ts +0 -174
  207. package/src/prompt-doc.ts +0 -169
  208. package/src/registry.ts +0 -353
  209. package/src/runner.test.ts +0 -142
  210. package/src/runner.ts +0 -666
  211. package/src/runtime.ts +0 -20
  212. package/src/schema-ports.test.ts +0 -172
  213. package/src/schema.test.ts +0 -213
  214. package/src/schema.ts +0 -379
  215. package/src/tagma.test.ts +0 -155
  216. package/src/tagma.ts +0 -62
  217. package/src/task-ref.test.ts +0 -401
  218. package/src/task-ref.ts +0 -121
  219. package/src/triggers/file.ts +0 -164
  220. package/src/triggers/manual.ts +0 -86
  221. package/src/types.ts +0 -18
  222. package/src/utils-api.ts +0 -8
  223. package/src/utils.test.ts +0 -28
  224. package/src/utils.ts +0 -203
  225. package/src/validate-raw-plugin-types.test.ts +0 -60
  226. package/src/validate-raw-ports.test.ts +0 -136
  227. package/src/validate-raw.ts +0 -852
  228. package/src/yaml-compiler.test.ts +0 -108
  229. package/src/yaml-compiler.ts +0 -110
  230. package/src/yaml.ts +0 -11
@@ -1,211 +0,0 @@
1
- import type {
2
- AbortReason,
3
- OnFailure,
4
- Permissions,
5
- PipelineConfig,
6
- RunEventPayload,
7
- TaskConfig,
8
- TaskState,
9
- TaskStatus,
10
- } from '../types';
11
- import type { Dag } from '../dag';
12
- import type { UpstreamBindingData } from '../ports';
13
- import {
14
- executeHook,
15
- buildTaskContext,
16
- type PipelineInfo,
17
- type TaskInfo,
18
- type TrackInfo,
19
- } from '../hooks';
20
- import type { TagmaRuntime } from '../runtime';
21
- import { isTerminal } from './run-state';
22
- import { nowISO } from '../utils';
23
-
24
- function isPromptTaskConfig(
25
- task: TaskConfig,
26
- ): task is TaskConfig & { readonly prompt: string; readonly command?: undefined } {
27
- return task.prompt !== undefined && task.command === undefined;
28
- }
29
-
30
- export interface RunContextOptions {
31
- readonly runId: string;
32
- readonly dag: Dag;
33
- readonly config: PipelineConfig;
34
- readonly workDir: string;
35
- readonly pipelineInfo: PipelineInfo;
36
- readonly onEvent?: (event: RunEventPayload) => void;
37
- readonly runtime: TagmaRuntime;
38
- }
39
-
40
- /**
41
- * Per-run state container. Owns the maps and abort tracking that
42
- * `runPipeline` previously held as closure locals, plus the small
43
- * methods that read/write that state. Scheduler, dataflow, and
44
- * task-executor extractions in later phases pass `ctx` instead of
45
- * relying on closure capture.
46
- */
47
- export class RunContext {
48
- readonly runId: string;
49
- readonly dag: Dag;
50
- readonly config: PipelineConfig;
51
- readonly workDir: string;
52
- readonly pipelineInfo: PipelineInfo;
53
- readonly onEvent?: (event: RunEventPayload) => void;
54
- readonly runtime: TagmaRuntime;
55
-
56
- readonly states = new Map<string, TaskState>();
57
- readonly sessionMap = new Map<string, string>();
58
- readonly normalizedMap = new Map<string, string>();
59
- readonly outputValuesMap = new Map<string, Readonly<Record<string, unknown>>>();
60
- readonly bindingDataMap = new Map<string, UpstreamBindingData>();
61
- readonly resolvedInputsMap = new Map<string, Readonly<Record<string, unknown>>>();
62
- readonly directDownstreams: Map<string, string[]>;
63
- readonly abortController = new AbortController();
64
- abortReason: AbortReason | null = null;
65
-
66
- constructor(options: RunContextOptions) {
67
- this.runId = options.runId;
68
- this.dag = options.dag;
69
- this.config = options.config;
70
- this.workDir = options.workDir;
71
- this.pipelineInfo = options.pipelineInfo;
72
- this.onEvent = options.onEvent;
73
- this.runtime = options.runtime;
74
-
75
- for (const [id, node] of this.dag.nodes) {
76
- this.states.set(id, {
77
- config: node.task,
78
- trackConfig: node.track,
79
- status: 'idle',
80
- result: null,
81
- startedAt: null,
82
- finishedAt: null,
83
- });
84
- }
85
-
86
- this.directDownstreams = new Map<string, string[]>();
87
- for (const [id] of this.dag.nodes) this.directDownstreams.set(id, []);
88
- for (const [id, node] of this.dag.nodes) {
89
- for (const upstream of node.dependsOn) {
90
- const list = this.directDownstreams.get(upstream);
91
- if (list) list.push(id);
92
- }
93
- }
94
- }
95
-
96
- emit(event: RunEventPayload): void {
97
- this.onEvent?.(event);
98
- }
99
-
100
- setTaskStatus(taskId: string, newStatus: TaskStatus): void {
101
- const state = this.states.get(taskId)!;
102
- // Terminal lock: once a task reaches a terminal state it must not be
103
- // re-transitioned. This prevents stop_all from marking running tasks as
104
- // skipped and then having their in-flight processTask promise overwrite
105
- // that with success/failed, producing an invalid double transition.
106
- if (isTerminal(state.status)) return;
107
- state.status = newStatus;
108
- const result = state.result;
109
- const cfg = state.config;
110
- this.emit({
111
- type: 'task_update',
112
- runId: this.runId,
113
- taskId,
114
- status: newStatus,
115
- startedAt: state.startedAt ?? undefined,
116
- finishedAt: state.finishedAt ?? undefined,
117
- durationMs: result?.durationMs,
118
- exitCode: result?.exitCode,
119
- stdout: result?.stdout,
120
- stderr: result?.stderr,
121
- stdoutPath: result?.stdoutPath ?? null,
122
- stderrPath: result?.stderrPath ?? null,
123
- stdoutBytes: result?.stdoutBytes ?? null,
124
- stderrBytes: result?.stderrBytes ?? null,
125
- sessionId: result?.sessionId ?? null,
126
- normalizedOutput: result?.normalizedOutput ?? null,
127
- inputs: this.resolvedInputsMap.get(taskId) ?? null,
128
- outputs: this.outputValuesMap.get(taskId) ?? null,
129
- resolvedDriver: cfg.driver ?? null,
130
- resolvedModel: cfg.model ?? null,
131
- resolvedPermissions: (cfg.permissions as Permissions | undefined) ?? null,
132
- });
133
- }
134
-
135
- getOnFailure(taskId: string): OnFailure {
136
- return this.dag.nodes.get(taskId)?.track.on_failure ?? 'skip_downstream';
137
- }
138
-
139
- isDependencySatisfied(depId: string): 'satisfied' | 'unsatisfied' | 'skip' {
140
- const depState = this.states.get(depId);
141
- if (!depState) return 'skip';
142
- switch (depState.status) {
143
- case 'success':
144
- return 'satisfied';
145
- case 'skipped':
146
- return 'skip';
147
- case 'failed':
148
- case 'timeout':
149
- case 'blocked':
150
- return this.getOnFailure(depId) === 'ignore' ? 'satisfied' : 'skip';
151
- default:
152
- return 'unsatisfied';
153
- }
154
- }
155
-
156
- /**
157
- * H3: stop_all marks every still-waiting task across every track as
158
- * skipped and aborts in-flight tasks via the shared signal. The
159
- * terminal lock in setTaskStatus prevents any later re-transition
160
- * should a completed running task try to overwrite the skipped state.
161
- */
162
- applyStopAll(): void {
163
- if (this.abortReason === null) this.abortReason = 'stop_all';
164
- this.abortController.abort();
165
- for (const [id, state] of this.states) {
166
- if (state.status === 'waiting') {
167
- state.finishedAt = nowISO();
168
- this.setTaskStatus(id, 'skipped');
169
- }
170
- }
171
- }
172
-
173
- buildTaskInfoObj(taskId: string): TaskInfo {
174
- const state = this.states.get(taskId)!;
175
- return {
176
- id: taskId,
177
- name: state.config.name,
178
- type: isPromptTaskConfig(state.config) ? 'ai' : 'command',
179
- status: state.status,
180
- exit_code: state.result?.exitCode ?? null,
181
- duration_ms: state.result?.durationMs ?? null,
182
- stderr_path: state.result?.stderrPath ?? null,
183
- session_id: state.result?.sessionId ?? null,
184
- started_at: state.startedAt,
185
- finished_at: state.finishedAt,
186
- };
187
- }
188
-
189
- trackInfoOf(taskId: string): TrackInfo {
190
- const node = this.dag.nodes.get(taskId)!;
191
- return { id: node.track.id, name: node.track.name };
192
- }
193
-
194
- async fireHook(
195
- taskId: string,
196
- event: 'task_success' | 'task_failure',
197
- ): Promise<void> {
198
- await executeHook(
199
- this.config.hooks,
200
- event,
201
- buildTaskContext(
202
- event,
203
- this.pipelineInfo,
204
- this.trackInfoOf(taskId),
205
- this.buildTaskInfoObj(taskId),
206
- ),
207
- this.workDir,
208
- this.abortController.signal,
209
- );
210
- }
211
- }
@@ -1,98 +0,0 @@
1
- import { describe, expect, test } from 'bun:test';
2
- import {
3
- isTerminal,
4
- freezeStates,
5
- summarizeStates,
6
- toRunTaskState,
7
- } from './run-state';
8
- import type { TaskState, TaskStatus } from '../types';
9
-
10
- describe('isTerminal', () => {
11
- test('returns true for terminal statuses', () => {
12
- expect(isTerminal('success')).toBe(true);
13
- expect(isTerminal('failed')).toBe(true);
14
- expect(isTerminal('timeout')).toBe(true);
15
- expect(isTerminal('skipped')).toBe(true);
16
- expect(isTerminal('blocked')).toBe(true);
17
- });
18
- test('returns false for non-terminal statuses', () => {
19
- expect(isTerminal('idle')).toBe(false);
20
- expect(isTerminal('waiting')).toBe(false);
21
- expect(isTerminal('running')).toBe(false);
22
- });
23
- });
24
-
25
- function makeState(status: TaskStatus, opts: Partial<TaskState> = {}): TaskState {
26
- return {
27
- config: { id: 't', name: 't' } as TaskState['config'],
28
- trackConfig: { id: 'tr', name: 'tr', tasks: [] } as TaskState['trackConfig'],
29
- status,
30
- result: null,
31
- startedAt: null,
32
- finishedAt: null,
33
- ...opts,
34
- };
35
- }
36
-
37
- describe('freezeStates', () => {
38
- test('produces a deep copy that survives source mutation', () => {
39
- const src = new Map<string, TaskState>();
40
- src.set('a', makeState('success'));
41
- const frozen = freezeStates(src);
42
- src.get('a')!.status = 'failed';
43
- expect(frozen.get('a')!.status).toBe('success');
44
- });
45
-
46
- test('copies result object so mutation does not bleed through', () => {
47
- const src = new Map<string, TaskState>();
48
- src.set('a', makeState('success', {
49
- result: {
50
- exitCode: 0,
51
- stdout: 'x',
52
- stderr: '',
53
- stdoutPath: null,
54
- stderrPath: null,
55
- durationMs: 1,
56
- sessionId: null,
57
- normalizedOutput: null,
58
- outputs: null,
59
- } as TaskState['result'],
60
- }));
61
- const frozen = freezeStates(src);
62
- src.get('a')!.result!.stdout = 'mutated';
63
- expect(frozen.get('a')!.result!.stdout).toBe('x');
64
- });
65
- });
66
-
67
- describe('summarizeStates', () => {
68
- test('counts each terminal status into its own bucket', () => {
69
- const m = new Map<string, TaskState>();
70
- m.set('a', makeState('success'));
71
- m.set('b', makeState('failed'));
72
- m.set('c', makeState('skipped'));
73
- m.set('d', makeState('timeout'));
74
- m.set('e', makeState('blocked'));
75
- m.set('f', makeState('running'));
76
- expect(summarizeStates(m)).toEqual({
77
- total: 6,
78
- success: 1,
79
- failed: 1,
80
- skipped: 1,
81
- timeout: 1,
82
- blocked: 1,
83
- });
84
- });
85
- });
86
-
87
- describe('toRunTaskState', () => {
88
- test('projects null result onto wire shape with empty stdout/stderr', () => {
89
- const wire = toRunTaskState('t1', 'trk', 'Task 1', makeState('idle'));
90
- expect(wire.taskId).toBe('t1');
91
- expect(wire.trackId).toBe('trk');
92
- expect(wire.status).toBe('idle');
93
- expect(wire.stdout).toBe('');
94
- expect(wire.stderr).toBe('');
95
- expect(wire.exitCode).toBeNull();
96
- expect(wire.logs).toEqual([]);
97
- });
98
- });
@@ -1,122 +0,0 @@
1
- import type {
2
- TaskState,
3
- TaskStatus,
4
- RunTaskState,
5
- Permissions,
6
- } from '../types';
7
-
8
- export function isTerminal(status: TaskStatus): boolean {
9
- return (
10
- status === 'success' ||
11
- status === 'failed' ||
12
- status === 'timeout' ||
13
- status === 'skipped' ||
14
- status === 'blocked'
15
- );
16
- }
17
-
18
- /** Return a deep-copied, caller-safe snapshot of the states map. */
19
- export function freezeStates(
20
- states: Map<string, TaskState>,
21
- ): ReadonlyMap<string, TaskState> {
22
- const copy = new Map<string, TaskState>();
23
- for (const [id, s] of states) {
24
- copy.set(id, {
25
- config: { ...s.config },
26
- trackConfig: { ...s.trackConfig },
27
- status: s.status,
28
- result: s.result ? { ...s.result } : null,
29
- startedAt: s.startedAt,
30
- finishedAt: s.finishedAt,
31
- });
32
- }
33
- return copy;
34
- }
35
-
36
- export interface RunSummary {
37
- total: number;
38
- success: number;
39
- failed: number;
40
- skipped: number;
41
- timeout: number;
42
- blocked: number;
43
- }
44
-
45
- /**
46
- * Tally terminal task counts. Idle/waiting/running tasks are counted in
47
- * `total` but not in any per-status bucket — same semantics as the
48
- * original engine.ts summary loop.
49
- */
50
- export function summarizeStates(
51
- states: ReadonlyMap<string, TaskState>,
52
- ): RunSummary {
53
- const summary: RunSummary = {
54
- total: 0,
55
- success: 0,
56
- failed: 0,
57
- skipped: 0,
58
- timeout: 0,
59
- blocked: 0,
60
- };
61
- for (const [, state] of states) {
62
- summary.total++;
63
- switch (state.status) {
64
- case 'success':
65
- summary.success++;
66
- break;
67
- case 'failed':
68
- summary.failed++;
69
- break;
70
- case 'skipped':
71
- summary.skipped++;
72
- break;
73
- case 'timeout':
74
- summary.timeout++;
75
- break;
76
- case 'blocked':
77
- summary.blocked++;
78
- break;
79
- }
80
- }
81
- return summary;
82
- }
83
-
84
- /**
85
- * Project the engine's internal TaskState onto the wire RunTaskState
86
- * shape. `logs` / `totalLogCount` default to empty — they are populated
87
- * on the server side from streamed `task_log` events, not from state.
88
- */
89
- export function toRunTaskState(
90
- taskId: string,
91
- trackId: string,
92
- taskName: string,
93
- state: TaskState,
94
- ): RunTaskState {
95
- const result = state.result;
96
- const cfg = state.config;
97
- return {
98
- taskId,
99
- trackId,
100
- taskName,
101
- status: state.status,
102
- startedAt: state.startedAt,
103
- finishedAt: state.finishedAt,
104
- durationMs: result?.durationMs ?? null,
105
- exitCode: result?.exitCode ?? null,
106
- stdout: result?.stdout ?? '',
107
- stderr: result?.stderr ?? '',
108
- stdoutPath: result?.stdoutPath ?? null,
109
- stderrPath: result?.stderrPath ?? null,
110
- stdoutBytes: result?.stdoutBytes ?? null,
111
- stderrBytes: result?.stderrBytes ?? null,
112
- sessionId: result?.sessionId ?? null,
113
- normalizedOutput: result?.normalizedOutput ?? null,
114
- resolvedDriver: cfg.driver ?? null,
115
- resolvedModel: cfg.model ?? null,
116
- resolvedPermissions: (cfg.permissions as Permissions | undefined) ?? null,
117
- outputs: result?.outputs ?? null,
118
- inputs: null,
119
- logs: [],
120
- totalLogCount: 0,
121
- };
122
- }
@@ -1,83 +0,0 @@
1
- import { describe, expect, test } from 'bun:test';
2
- import { buildDag } from '../dag';
3
- import type { PipelineConfig } from '../types';
4
- import { RunContext } from './run-context';
5
- import {
6
- allTasksTerminal,
7
- findLaunchableTasks,
8
- skipNonTerminalTasks,
9
- } from './scheduler';
10
-
11
- function makeContext(config: PipelineConfig): RunContext {
12
- return new RunContext({
13
- runId: 'run_scheduler',
14
- dag: buildDag(config),
15
- config,
16
- workDir: '/tmp/wd',
17
- pipelineInfo: {
18
- name: config.name,
19
- run_id: 'run_scheduler',
20
- started_at: '2026-04-26T00:00:00Z',
21
- },
22
- });
23
- }
24
-
25
- const chainConfig: PipelineConfig = {
26
- name: 'p',
27
- tracks: [
28
- {
29
- id: 't',
30
- name: 'T',
31
- tasks: [
32
- { id: 'a', name: 'A', command: 'echo a' },
33
- { id: 'b', name: 'B', command: 'echo b', depends_on: ['a'] },
34
- { id: 'c', name: 'C', command: 'echo c' },
35
- ],
36
- },
37
- ],
38
- };
39
-
40
- describe('findLaunchableTasks', () => {
41
- test('returns waiting roots that are not already running', () => {
42
- const ctx = makeContext(chainConfig);
43
- for (const state of ctx.states.values()) state.status = 'waiting';
44
-
45
- expect(findLaunchableTasks(ctx, new Set())).toEqual(['t.a', 't.c']);
46
- expect(findLaunchableTasks(ctx, new Set(['t.a']))).toEqual(['t.c']);
47
- });
48
-
49
- test('returns dependents only after dependencies are terminal', () => {
50
- const ctx = makeContext(chainConfig);
51
- for (const state of ctx.states.values()) state.status = 'waiting';
52
-
53
- expect(findLaunchableTasks(ctx, new Set())).not.toContain('t.b');
54
- ctx.states.get('t.a')!.status = 'success';
55
- expect(findLaunchableTasks(ctx, new Set())).toContain('t.b');
56
- });
57
- });
58
-
59
- describe('allTasksTerminal', () => {
60
- test('returns true only when every task is terminal', () => {
61
- const ctx = makeContext(chainConfig);
62
- for (const state of ctx.states.values()) state.status = 'success';
63
- expect(allTasksTerminal(ctx)).toBe(true);
64
- ctx.states.get('t.b')!.status = 'waiting';
65
- expect(allTasksTerminal(ctx)).toBe(false);
66
- });
67
- });
68
-
69
- describe('skipNonTerminalTasks', () => {
70
- test('marks every non-terminal task as skipped and leaves terminal tasks alone', () => {
71
- const ctx = makeContext(chainConfig);
72
- ctx.states.get('t.a')!.status = 'success';
73
- ctx.states.get('t.b')!.status = 'waiting';
74
- ctx.states.get('t.c')!.status = 'running';
75
-
76
- skipNonTerminalTasks(ctx, '2026-04-26T01:00:00Z');
77
-
78
- expect(ctx.states.get('t.a')!.status).toBe('success');
79
- expect(ctx.states.get('t.b')!.status).toBe('skipped');
80
- expect(ctx.states.get('t.b')!.finishedAt).toBe('2026-04-26T01:00:00Z');
81
- expect(ctx.states.get('t.c')!.status).toBe('skipped');
82
- });
83
- });
@@ -1,42 +0,0 @@
1
- import type { RunContext } from './run-context';
2
- import { isTerminal } from './run-state';
3
- import { nowISO } from '../utils';
4
-
5
- /**
6
- * Return waiting tasks whose dependency states are terminal and which are not
7
- * already in flight. The caller owns actually launching them.
8
- */
9
- export function findLaunchableTasks(
10
- ctx: RunContext,
11
- runningTaskIds: ReadonlySet<string>,
12
- ): string[] {
13
- const launchable: string[] = [];
14
- for (const [id, state] of ctx.states) {
15
- if (state.status !== 'waiting' || runningTaskIds.has(id)) continue;
16
- const node = ctx.dag.nodes.get(id)!;
17
- const allDepsTerminal =
18
- node.dependsOn.length === 0 ||
19
- node.dependsOn.every((depId) => isTerminal(ctx.states.get(depId)!.status));
20
- if (allDepsTerminal) launchable.push(id);
21
- }
22
- return launchable;
23
- }
24
-
25
- export function allTasksTerminal(ctx: RunContext): boolean {
26
- return [...ctx.states.values()].every((state) => isTerminal(state.status));
27
- }
28
-
29
- /**
30
- * Abort cleanup helper: after in-flight tasks settle, any remaining
31
- * non-terminal tasks are waiting/idle tasks that were never started.
32
- */
33
- export function skipNonTerminalTasks(
34
- ctx: RunContext,
35
- finishedAt = nowISO(),
36
- ): void {
37
- for (const [id, state] of ctx.states) {
38
- if (isTerminal(state.status)) continue;
39
- state.finishedAt = finishedAt;
40
- ctx.setTaskStatus(id, 'skipped');
41
- }
42
- }