@tagma/sdk 0.6.12 → 0.7.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 (125) hide show
  1. package/README.md +56 -15
  2. package/dist/bootstrap.d.ts +6 -6
  3. package/dist/bootstrap.d.ts.map +1 -1
  4. package/dist/bootstrap.js +5 -6
  5. package/dist/bootstrap.js.map +1 -1
  6. package/dist/config.d.ts +8 -0
  7. package/dist/config.d.ts.map +1 -0
  8. package/dist/config.js +5 -0
  9. package/dist/config.js.map +1 -0
  10. package/dist/core/dataflow.d.ts +23 -0
  11. package/dist/core/dataflow.d.ts.map +1 -0
  12. package/dist/core/dataflow.js +99 -0
  13. package/dist/core/dataflow.js.map +1 -0
  14. package/dist/core/log-prune.d.ts +16 -0
  15. package/dist/core/log-prune.d.ts.map +1 -0
  16. package/dist/core/log-prune.js +34 -0
  17. package/dist/core/log-prune.js.map +1 -0
  18. package/dist/core/preflight.d.ts +13 -0
  19. package/dist/core/preflight.d.ts.map +1 -0
  20. package/dist/core/preflight.js +61 -0
  21. package/dist/core/preflight.js.map +1 -0
  22. package/dist/core/run-context.d.ts +52 -0
  23. package/dist/core/run-context.d.ts.map +1 -0
  24. package/dist/core/run-context.js +156 -0
  25. package/dist/core/run-context.js.map +1 -0
  26. package/dist/core/run-state.d.ts +25 -0
  27. package/dist/core/run-state.d.ts.map +1 -0
  28. package/dist/core/run-state.js +93 -0
  29. package/dist/core/run-state.js.map +1 -0
  30. package/dist/core/scheduler.d.ts +13 -0
  31. package/dist/core/scheduler.d.ts.map +1 -0
  32. package/dist/core/scheduler.js +35 -0
  33. package/dist/core/scheduler.js.map +1 -0
  34. package/dist/core/task-executor.d.ts +13 -0
  35. package/dist/core/task-executor.d.ts.map +1 -0
  36. package/dist/core/task-executor.js +623 -0
  37. package/dist/core/task-executor.js.map +1 -0
  38. package/dist/core/trigger-errors.d.ts +9 -0
  39. package/dist/core/trigger-errors.d.ts.map +1 -0
  40. package/dist/core/trigger-errors.js +15 -0
  41. package/dist/core/trigger-errors.js.map +1 -0
  42. package/dist/engine.d.ts +6 -14
  43. package/dist/engine.d.ts.map +1 -1
  44. package/dist/engine.js +68 -1035
  45. package/dist/engine.js.map +1 -1
  46. package/dist/index.d.ts +9 -0
  47. package/dist/index.d.ts.map +1 -0
  48. package/dist/index.js +6 -0
  49. package/dist/index.js.map +1 -0
  50. package/dist/pipeline-definition.d.ts +3 -0
  51. package/dist/pipeline-definition.d.ts.map +1 -0
  52. package/dist/pipeline-definition.js +4 -0
  53. package/dist/pipeline-definition.js.map +1 -0
  54. package/dist/pipeline-runner.d.ts +2 -1
  55. package/dist/pipeline-runner.d.ts.map +1 -1
  56. package/dist/pipeline-runner.js +2 -2
  57. package/dist/pipeline-runner.js.map +1 -1
  58. package/dist/plugins.d.ts +5 -0
  59. package/dist/plugins.d.ts.map +1 -0
  60. package/dist/plugins.js +3 -0
  61. package/dist/plugins.js.map +1 -0
  62. package/dist/ports.d.ts +4 -0
  63. package/dist/ports.d.ts.map +1 -1
  64. package/dist/ports.js +27 -4
  65. package/dist/ports.js.map +1 -1
  66. package/dist/registry.d.ts +3 -19
  67. package/dist/registry.d.ts.map +1 -1
  68. package/dist/registry.js +7 -35
  69. package/dist/registry.js.map +1 -1
  70. package/dist/tagma.d.ts +24 -0
  71. package/dist/tagma.d.ts.map +1 -0
  72. package/dist/tagma.js +23 -0
  73. package/dist/tagma.js.map +1 -0
  74. package/dist/utils-api.d.ts +2 -0
  75. package/dist/utils-api.d.ts.map +1 -0
  76. package/dist/utils-api.js +2 -0
  77. package/dist/utils-api.js.map +1 -0
  78. package/dist/validate-raw.d.ts +4 -4
  79. package/dist/validate-raw.js +91 -132
  80. package/dist/validate-raw.js.map +1 -1
  81. package/dist/yaml.d.ts +4 -0
  82. package/dist/yaml.d.ts.map +1 -0
  83. package/dist/yaml.js +3 -0
  84. package/dist/yaml.js.map +1 -0
  85. package/package.json +53 -8
  86. package/src/bootstrap.ts +6 -6
  87. package/src/config.ts +26 -0
  88. package/src/core/dataflow.test.ts +166 -0
  89. package/src/core/dataflow.ts +161 -0
  90. package/src/core/log-prune.test.ts +58 -0
  91. package/src/core/log-prune.ts +43 -0
  92. package/src/core/preflight.test.ts +49 -0
  93. package/src/core/preflight.ts +89 -0
  94. package/src/core/run-context.test.ts +244 -0
  95. package/src/core/run-context.ts +207 -0
  96. package/src/core/run-state.test.ts +98 -0
  97. package/src/core/run-state.ts +122 -0
  98. package/src/core/scheduler.test.ts +83 -0
  99. package/src/core/scheduler.ts +42 -0
  100. package/src/core/task-executor.ts +769 -0
  101. package/src/core/trigger-errors.ts +15 -0
  102. package/src/engine-ports-mixed.test.ts +68 -411
  103. package/src/engine-ports.test.ts +37 -341
  104. package/src/engine.ts +80 -1248
  105. package/src/index.ts +28 -0
  106. package/src/pipeline-definition.ts +5 -0
  107. package/src/pipeline-runner.test.ts +5 -9
  108. package/src/pipeline-runner.ts +3 -2
  109. package/src/plugin-registry.test.ts +7 -10
  110. package/src/plugins.ts +18 -0
  111. package/src/ports.test.ts +80 -0
  112. package/src/ports.ts +36 -4
  113. package/src/registry.ts +7 -49
  114. package/src/schema-ports.test.ts +41 -214
  115. package/src/tagma.test.ts +84 -0
  116. package/src/tagma.ts +47 -0
  117. package/src/utils-api.ts +8 -0
  118. package/src/validate-raw-ports.test.ts +80 -393
  119. package/src/validate-raw.ts +93 -137
  120. package/src/yaml.ts +11 -0
  121. package/dist/sdk.d.ts +0 -32
  122. package/dist/sdk.d.ts.map +0 -1
  123. package/dist/sdk.js +0 -41
  124. package/dist/sdk.js.map +0 -1
  125. package/src/sdk.ts +0 -151
@@ -0,0 +1,89 @@
1
+ import type { PipelineConfig, TaskConfig, DriverPlugin } from '../types';
2
+ import type { Dag } from '../dag';
3
+ import type { PluginRegistry } from '../registry';
4
+
5
+ function isCommandOnly(
6
+ task: TaskConfig,
7
+ ): task is TaskConfig & { readonly command: string; readonly prompt?: undefined } {
8
+ return task.command !== undefined && task.prompt === undefined;
9
+ }
10
+
11
+ /**
12
+ * Validate that every plugin referenced by the pipeline (drivers,
13
+ * triggers, completions, middlewares) is registered, and that
14
+ * `continue_from` is only used between drivers that can hand off via
15
+ * sessionResume or text-injection. Throws with all errors aggregated
16
+ * into one message so the caller sees every misconfiguration in a
17
+ * single pass.
18
+ */
19
+ export function preflight(
20
+ config: PipelineConfig,
21
+ dag: Dag,
22
+ registry: PluginRegistry,
23
+ ): void {
24
+ const errors: string[] = [];
25
+
26
+ for (const [, node] of dag.nodes) {
27
+ const task = node.task;
28
+ const track = node.track;
29
+ const driverName = task.driver ?? track.driver ?? config.driver ?? 'opencode';
30
+
31
+ const isCommand = isCommandOnly(task);
32
+
33
+ if (!isCommand && !registry.hasHandler('drivers', driverName)) {
34
+ errors.push(`Task "${node.taskId}": driver "${driverName}" not registered`);
35
+ }
36
+
37
+ if (task.trigger && !registry.hasHandler('triggers', task.trigger.type)) {
38
+ errors.push(
39
+ `Task "${node.taskId}": trigger type "${task.trigger.type}" not registered`,
40
+ );
41
+ }
42
+
43
+ if (task.completion && !registry.hasHandler('completions', task.completion.type)) {
44
+ errors.push(
45
+ `Task "${node.taskId}": completion type "${task.completion.type}" not registered`,
46
+ );
47
+ }
48
+
49
+ const mws = task.middlewares ?? track.middlewares ?? [];
50
+ for (const mw of mws) {
51
+ if (!registry.hasHandler('middlewares', mw.type)) {
52
+ errors.push(
53
+ `Task "${node.taskId}": middleware type "${mw.type}" not registered`,
54
+ );
55
+ }
56
+ }
57
+
58
+ if (task.continue_from && registry.hasHandler('drivers', driverName)) {
59
+ const driver = registry.getHandler<DriverPlugin>('drivers', driverName);
60
+ if (!driver.capabilities.sessionResume) {
61
+ const upstreamId = node.resolvedContinueFrom;
62
+ if (upstreamId) {
63
+ const upstream = dag.nodes.get(upstreamId);
64
+ if (upstream) {
65
+ const upstreamDriverName =
66
+ upstream.task.driver ?? upstream.track.driver ?? config.driver ?? 'opencode';
67
+ const upstreamDriver = registry.hasHandler('drivers', upstreamDriverName)
68
+ ? registry.getHandler<DriverPlugin>('drivers', upstreamDriverName)
69
+ : null;
70
+ const canNormalize = typeof upstreamDriver?.parseResult === 'function';
71
+
72
+ if (!canNormalize) {
73
+ errors.push(
74
+ `Task "${node.taskId}" uses continue_from: "${task.continue_from}", ` +
75
+ `but upstream task "${upstreamId}" its driver ` +
76
+ `does not implement parseResult for text-injection handoff. ` +
77
+ `Use a driver with parseResult, or remove continue_from.`,
78
+ );
79
+ }
80
+ }
81
+ }
82
+ }
83
+ }
84
+ }
85
+
86
+ if (errors.length > 0) {
87
+ throw new Error(`Preflight validation failed:\n - ${errors.join('\n - ')}`);
88
+ }
89
+ }
@@ -0,0 +1,244 @@
1
+ import { describe, expect, test } from 'bun:test';
2
+ import { RunContext } from './run-context';
3
+ import { buildDag } from '../dag';
4
+ import type { PipelineConfig, RunEventPayload } from '../types';
5
+ import type { PipelineInfo } from '../hooks';
6
+
7
+ function makeContext(overrides: Partial<{
8
+ config: PipelineConfig;
9
+ onEvent: (e: RunEventPayload) => void;
10
+ }> = {}): { ctx: RunContext; events: RunEventPayload[] } {
11
+ const config: PipelineConfig = overrides.config ?? {
12
+ name: 'p',
13
+ tracks: [
14
+ {
15
+ id: 't',
16
+ name: 'T',
17
+ tasks: [
18
+ { id: 'a', name: 'A', command: 'echo a' },
19
+ { id: 'b', name: 'B', command: 'echo b', depends_on: ['a'] },
20
+ ],
21
+ },
22
+ ],
23
+ };
24
+ const events: RunEventPayload[] = [];
25
+ const onEvent = overrides.onEvent ?? ((e: RunEventPayload) => { events.push(e); });
26
+ const ctx = new RunContext({
27
+ runId: 'run_test',
28
+ dag: buildDag(config),
29
+ config,
30
+ workDir: '/tmp/wd',
31
+ pipelineInfo: { name: config.name, run_id: 'run_test', started_at: '2026-04-26T00:00:00Z' } as PipelineInfo,
32
+ onEvent,
33
+ });
34
+ return { ctx, events };
35
+ }
36
+
37
+ describe('RunContext constructor', () => {
38
+ test('initializes one idle state per dag node', () => {
39
+ const { ctx } = makeContext();
40
+ expect(ctx.states.size).toBe(2);
41
+ expect(ctx.states.get('t.a')!.status).toBe('idle');
42
+ expect(ctx.states.get('t.b')!.status).toBe('idle');
43
+ });
44
+
45
+ test('initializes all state maps as empty', () => {
46
+ const { ctx } = makeContext();
47
+ expect(ctx.sessionMap.size).toBe(0);
48
+ expect(ctx.normalizedMap.size).toBe(0);
49
+ expect(ctx.outputValuesMap.size).toBe(0);
50
+ expect(ctx.bindingDataMap.size).toBe(0);
51
+ expect(ctx.resolvedInputsMap.size).toBe(0);
52
+ });
53
+
54
+ test('computes directDownstreams reverse adjacency from dag', () => {
55
+ const { ctx } = makeContext();
56
+ expect([...ctx.directDownstreams.get('t.a')!]).toEqual(['t.b']);
57
+ expect([...ctx.directDownstreams.get('t.b')!]).toEqual([]);
58
+ });
59
+
60
+ test('starts with abortReason null and a fresh AbortController', () => {
61
+ const { ctx } = makeContext();
62
+ expect(ctx.abortReason).toBeNull();
63
+ expect(ctx.abortController.signal.aborted).toBe(false);
64
+ });
65
+ });
66
+
67
+ describe('RunContext.emit', () => {
68
+ test('forwards events to onEvent', () => {
69
+ const { ctx, events } = makeContext();
70
+ ctx.emit({ type: 'run_end', runId: 'run_test', success: true, abortReason: null });
71
+ expect(events).toHaveLength(1);
72
+ expect(events[0]).toEqual({ type: 'run_end', runId: 'run_test', success: true, abortReason: null });
73
+ });
74
+
75
+ test('is a no-op when onEvent is undefined', () => {
76
+ const config: PipelineConfig = {
77
+ name: 'p',
78
+ tracks: [{ id: 't', name: 'T', tasks: [{ id: 'a', name: 'A', command: 'echo a' }] }],
79
+ };
80
+ const ctx = new RunContext({
81
+ runId: 'run_test',
82
+ dag: buildDag(config),
83
+ config,
84
+ workDir: '/tmp/wd',
85
+ pipelineInfo: { name: 'p', run_id: 'run_test', started_at: 'now' } as PipelineInfo,
86
+ });
87
+ expect(() => ctx.emit({ type: 'run_end', runId: 'run_test', success: true, abortReason: null })).not.toThrow();
88
+ });
89
+ });
90
+
91
+ describe('RunContext.setTaskStatus', () => {
92
+ test('transitions a non-terminal task and emits task_update', () => {
93
+ const { ctx, events } = makeContext();
94
+ ctx.setTaskStatus('t.a', 'waiting');
95
+ expect(ctx.states.get('t.a')!.status).toBe('waiting');
96
+ expect(events).toHaveLength(1);
97
+ expect(events[0].type).toBe('task_update');
98
+ if (events[0].type === 'task_update') {
99
+ expect(events[0].taskId).toBe('t.a');
100
+ expect(events[0].status).toBe('waiting');
101
+ }
102
+ });
103
+
104
+ test('refuses to re-transition a terminal task (terminal lock)', () => {
105
+ const { ctx, events } = makeContext();
106
+ ctx.setTaskStatus('t.a', 'success');
107
+ events.length = 0;
108
+ ctx.setTaskStatus('t.a', 'failed');
109
+ expect(ctx.states.get('t.a')!.status).toBe('success');
110
+ expect(events).toHaveLength(0);
111
+ });
112
+
113
+ test('echoes resolvedInputs and outputs from the maps in the emitted event', () => {
114
+ const { ctx, events } = makeContext();
115
+ ctx.resolvedInputsMap.set('t.a', { x: 1 });
116
+ ctx.outputValuesMap.set('t.a', { y: 2 });
117
+ ctx.setTaskStatus('t.a', 'running');
118
+ if (events[0].type === 'task_update') {
119
+ expect(events[0].inputs).toEqual({ x: 1 });
120
+ expect(events[0].outputs).toEqual({ y: 2 });
121
+ } else {
122
+ throw new Error('expected task_update');
123
+ }
124
+ });
125
+ });
126
+
127
+ describe('RunContext.getOnFailure', () => {
128
+ test('returns the track-level on_failure setting', () => {
129
+ const config: PipelineConfig = {
130
+ name: 'p',
131
+ tracks: [
132
+ {
133
+ id: 't',
134
+ name: 'T',
135
+ on_failure: 'stop_all',
136
+ tasks: [{ id: 'a', name: 'A', command: 'echo a' }],
137
+ },
138
+ ],
139
+ };
140
+ const { ctx } = makeContext({ config });
141
+ expect(ctx.getOnFailure('t.a')).toBe('stop_all');
142
+ });
143
+
144
+ test('defaults to skip_downstream when track does not specify', () => {
145
+ const { ctx } = makeContext();
146
+ expect(ctx.getOnFailure('t.a')).toBe('skip_downstream');
147
+ });
148
+ });
149
+
150
+ describe('RunContext.isDependencySatisfied', () => {
151
+ test('returns satisfied for success', () => {
152
+ const { ctx } = makeContext();
153
+ ctx.states.get('t.a')!.status = 'success';
154
+ expect(ctx.isDependencySatisfied('t.a')).toBe('satisfied');
155
+ });
156
+
157
+ test('returns skip for skipped', () => {
158
+ const { ctx } = makeContext();
159
+ ctx.states.get('t.a')!.status = 'skipped';
160
+ expect(ctx.isDependencySatisfied('t.a')).toBe('skip');
161
+ });
162
+
163
+ test('returns skip for failed under default policy, satisfied under ignore', () => {
164
+ const cfgSkip: PipelineConfig = {
165
+ name: 'p',
166
+ tracks: [{ id: 't', name: 'T', tasks: [{ id: 'a', name: 'A', command: 'echo a' }] }],
167
+ };
168
+ const cfgIgnore: PipelineConfig = {
169
+ name: 'p',
170
+ tracks: [
171
+ {
172
+ id: 't',
173
+ name: 'T',
174
+ on_failure: 'ignore',
175
+ tasks: [{ id: 'a', name: 'A', command: 'echo a' }],
176
+ },
177
+ ],
178
+ };
179
+ const a = makeContext({ config: cfgSkip }).ctx;
180
+ a.states.get('t.a')!.status = 'failed';
181
+ expect(a.isDependencySatisfied('t.a')).toBe('skip');
182
+
183
+ const b = makeContext({ config: cfgIgnore }).ctx;
184
+ b.states.get('t.a')!.status = 'failed';
185
+ expect(b.isDependencySatisfied('t.a')).toBe('satisfied');
186
+ });
187
+
188
+ test('returns unsatisfied for non-terminal statuses', () => {
189
+ const { ctx } = makeContext();
190
+ ctx.states.get('t.a')!.status = 'running';
191
+ expect(ctx.isDependencySatisfied('t.a')).toBe('unsatisfied');
192
+ });
193
+ });
194
+
195
+ describe('RunContext.applyStopAll', () => {
196
+ test('aborts the controller, sets abortReason, marks waiting tasks as skipped', () => {
197
+ const { ctx } = makeContext();
198
+ ctx.states.get('t.a')!.status = 'waiting';
199
+ ctx.states.get('t.b')!.status = 'waiting';
200
+ ctx.applyStopAll();
201
+ expect(ctx.abortReason).toBe('stop_all');
202
+ expect(ctx.abortController.signal.aborted).toBe(true);
203
+ expect(ctx.states.get('t.a')!.status).toBe('skipped');
204
+ expect(ctx.states.get('t.b')!.status).toBe('skipped');
205
+ });
206
+
207
+ test('does not overwrite an existing abortReason', () => {
208
+ const { ctx } = makeContext();
209
+ ctx.abortReason = 'timeout';
210
+ ctx.applyStopAll();
211
+ expect(ctx.abortReason).toBe('timeout');
212
+ expect(ctx.abortController.signal.aborted).toBe(true);
213
+ });
214
+
215
+ test('leaves running and terminal tasks alone', () => {
216
+ const { ctx } = makeContext();
217
+ ctx.states.get('t.a')!.status = 'running';
218
+ ctx.states.get('t.b')!.status = 'success';
219
+ ctx.applyStopAll();
220
+ expect(ctx.states.get('t.a')!.status).toBe('running');
221
+ expect(ctx.states.get('t.b')!.status).toBe('success');
222
+ });
223
+ });
224
+
225
+ describe('RunContext.buildTaskInfoObj / trackInfoOf', () => {
226
+ test('buildTaskInfoObj reports type=command for command tasks', () => {
227
+ const { ctx } = makeContext();
228
+ expect(ctx.buildTaskInfoObj('t.a').type).toBe('command');
229
+ });
230
+
231
+ test('buildTaskInfoObj reports type=ai for prompt tasks', () => {
232
+ const config: PipelineConfig = {
233
+ name: 'p',
234
+ tracks: [{ id: 't', name: 'T', tasks: [{ id: 'a', name: 'A', prompt: 'hi' }] }],
235
+ };
236
+ const { ctx } = makeContext({ config });
237
+ expect(ctx.buildTaskInfoObj('t.a').type).toBe('ai');
238
+ });
239
+
240
+ test('trackInfoOf returns the track id and name', () => {
241
+ const { ctx } = makeContext();
242
+ expect(ctx.trackInfoOf('t.a')).toEqual({ id: 't', name: 'T' });
243
+ });
244
+ });
@@ -0,0 +1,207 @@
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 { isTerminal } from './run-state';
21
+ import { nowISO } from '../utils';
22
+
23
+ function isPromptTaskConfig(
24
+ task: TaskConfig,
25
+ ): task is TaskConfig & { readonly prompt: string; readonly command?: undefined } {
26
+ return task.prompt !== undefined && task.command === undefined;
27
+ }
28
+
29
+ export interface RunContextOptions {
30
+ readonly runId: string;
31
+ readonly dag: Dag;
32
+ readonly config: PipelineConfig;
33
+ readonly workDir: string;
34
+ readonly pipelineInfo: PipelineInfo;
35
+ readonly onEvent?: (event: RunEventPayload) => void;
36
+ }
37
+
38
+ /**
39
+ * Per-run state container. Owns the maps and abort tracking that
40
+ * `runPipeline` previously held as closure locals, plus the small
41
+ * methods that read/write that state. Scheduler, dataflow, and
42
+ * task-executor extractions in later phases pass `ctx` instead of
43
+ * relying on closure capture.
44
+ */
45
+ export class RunContext {
46
+ readonly runId: string;
47
+ readonly dag: Dag;
48
+ readonly config: PipelineConfig;
49
+ readonly workDir: string;
50
+ readonly pipelineInfo: PipelineInfo;
51
+ readonly onEvent?: (event: RunEventPayload) => void;
52
+
53
+ readonly states = new Map<string, TaskState>();
54
+ readonly sessionMap = new Map<string, string>();
55
+ readonly normalizedMap = new Map<string, string>();
56
+ readonly outputValuesMap = new Map<string, Readonly<Record<string, unknown>>>();
57
+ readonly bindingDataMap = new Map<string, UpstreamBindingData>();
58
+ readonly resolvedInputsMap = new Map<string, Readonly<Record<string, unknown>>>();
59
+ readonly directDownstreams: Map<string, string[]>;
60
+ readonly abortController = new AbortController();
61
+ abortReason: AbortReason | null = null;
62
+
63
+ constructor(options: RunContextOptions) {
64
+ this.runId = options.runId;
65
+ this.dag = options.dag;
66
+ this.config = options.config;
67
+ this.workDir = options.workDir;
68
+ this.pipelineInfo = options.pipelineInfo;
69
+ this.onEvent = options.onEvent;
70
+
71
+ for (const [id, node] of this.dag.nodes) {
72
+ this.states.set(id, {
73
+ config: node.task,
74
+ trackConfig: node.track,
75
+ status: 'idle',
76
+ result: null,
77
+ startedAt: null,
78
+ finishedAt: null,
79
+ });
80
+ }
81
+
82
+ this.directDownstreams = new Map<string, string[]>();
83
+ for (const [id] of this.dag.nodes) this.directDownstreams.set(id, []);
84
+ for (const [id, node] of this.dag.nodes) {
85
+ for (const upstream of node.dependsOn) {
86
+ const list = this.directDownstreams.get(upstream);
87
+ if (list) list.push(id);
88
+ }
89
+ }
90
+ }
91
+
92
+ emit(event: RunEventPayload): void {
93
+ this.onEvent?.(event);
94
+ }
95
+
96
+ setTaskStatus(taskId: string, newStatus: TaskStatus): void {
97
+ const state = this.states.get(taskId)!;
98
+ // Terminal lock: once a task reaches a terminal state it must not be
99
+ // re-transitioned. This prevents stop_all from marking running tasks as
100
+ // skipped and then having their in-flight processTask promise overwrite
101
+ // that with success/failed, producing an invalid double transition.
102
+ if (isTerminal(state.status)) return;
103
+ state.status = newStatus;
104
+ const result = state.result;
105
+ const cfg = state.config;
106
+ this.emit({
107
+ type: 'task_update',
108
+ runId: this.runId,
109
+ taskId,
110
+ status: newStatus,
111
+ startedAt: state.startedAt ?? undefined,
112
+ finishedAt: state.finishedAt ?? undefined,
113
+ durationMs: result?.durationMs,
114
+ exitCode: result?.exitCode,
115
+ stdout: result?.stdout,
116
+ stderr: result?.stderr,
117
+ stdoutPath: result?.stdoutPath ?? null,
118
+ stderrPath: result?.stderrPath ?? null,
119
+ stdoutBytes: result?.stdoutBytes ?? null,
120
+ stderrBytes: result?.stderrBytes ?? null,
121
+ sessionId: result?.sessionId ?? null,
122
+ normalizedOutput: result?.normalizedOutput ?? null,
123
+ inputs: this.resolvedInputsMap.get(taskId) ?? null,
124
+ outputs: this.outputValuesMap.get(taskId) ?? null,
125
+ resolvedDriver: cfg.driver ?? null,
126
+ resolvedModel: cfg.model ?? null,
127
+ resolvedPermissions: (cfg.permissions as Permissions | undefined) ?? null,
128
+ });
129
+ }
130
+
131
+ getOnFailure(taskId: string): OnFailure {
132
+ return this.dag.nodes.get(taskId)?.track.on_failure ?? 'skip_downstream';
133
+ }
134
+
135
+ isDependencySatisfied(depId: string): 'satisfied' | 'unsatisfied' | 'skip' {
136
+ const depState = this.states.get(depId);
137
+ if (!depState) return 'skip';
138
+ switch (depState.status) {
139
+ case 'success':
140
+ return 'satisfied';
141
+ case 'skipped':
142
+ return 'skip';
143
+ case 'failed':
144
+ case 'timeout':
145
+ case 'blocked':
146
+ return this.getOnFailure(depId) === 'ignore' ? 'satisfied' : 'skip';
147
+ default:
148
+ return 'unsatisfied';
149
+ }
150
+ }
151
+
152
+ /**
153
+ * H3: stop_all marks every still-waiting task across every track as
154
+ * skipped and aborts in-flight tasks via the shared signal. The
155
+ * terminal lock in setTaskStatus prevents any later re-transition
156
+ * should a completed running task try to overwrite the skipped state.
157
+ */
158
+ applyStopAll(): void {
159
+ if (this.abortReason === null) this.abortReason = 'stop_all';
160
+ this.abortController.abort();
161
+ for (const [id, state] of this.states) {
162
+ if (state.status === 'waiting') {
163
+ state.finishedAt = nowISO();
164
+ this.setTaskStatus(id, 'skipped');
165
+ }
166
+ }
167
+ }
168
+
169
+ buildTaskInfoObj(taskId: string): TaskInfo {
170
+ const state = this.states.get(taskId)!;
171
+ return {
172
+ id: taskId,
173
+ name: state.config.name,
174
+ type: isPromptTaskConfig(state.config) ? 'ai' : 'command',
175
+ status: state.status,
176
+ exit_code: state.result?.exitCode ?? null,
177
+ duration_ms: state.result?.durationMs ?? null,
178
+ stderr_path: state.result?.stderrPath ?? null,
179
+ session_id: state.result?.sessionId ?? null,
180
+ started_at: state.startedAt,
181
+ finished_at: state.finishedAt,
182
+ };
183
+ }
184
+
185
+ trackInfoOf(taskId: string): TrackInfo {
186
+ const node = this.dag.nodes.get(taskId)!;
187
+ return { id: node.track.id, name: node.track.name };
188
+ }
189
+
190
+ async fireHook(
191
+ taskId: string,
192
+ event: 'task_success' | 'task_failure',
193
+ ): Promise<void> {
194
+ await executeHook(
195
+ this.config.hooks,
196
+ event,
197
+ buildTaskContext(
198
+ event,
199
+ this.pipelineInfo,
200
+ this.trackInfoOf(taskId),
201
+ this.buildTaskInfoObj(taskId),
202
+ ),
203
+ this.workDir,
204
+ this.abortController.signal,
205
+ );
206
+ }
207
+ }
@@ -0,0 +1,98 @@
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
+ });