@tagma/sdk 0.6.12 → 0.7.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.
- package/README.md +56 -15
- package/dist/bootstrap.d.ts +6 -6
- package/dist/bootstrap.d.ts.map +1 -1
- package/dist/bootstrap.js +5 -6
- package/dist/bootstrap.js.map +1 -1
- package/dist/config.d.ts +8 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +5 -0
- package/dist/config.js.map +1 -0
- package/dist/core/dataflow.d.ts +23 -0
- package/dist/core/dataflow.d.ts.map +1 -0
- package/dist/core/dataflow.js +63 -0
- package/dist/core/dataflow.js.map +1 -0
- package/dist/core/log-prune.d.ts +16 -0
- package/dist/core/log-prune.d.ts.map +1 -0
- package/dist/core/log-prune.js +34 -0
- package/dist/core/log-prune.js.map +1 -0
- package/dist/core/preflight.d.ts +13 -0
- package/dist/core/preflight.d.ts.map +1 -0
- package/dist/core/preflight.js +61 -0
- package/dist/core/preflight.js.map +1 -0
- package/dist/core/run-context.d.ts +52 -0
- package/dist/core/run-context.d.ts.map +1 -0
- package/dist/core/run-context.js +156 -0
- package/dist/core/run-context.js.map +1 -0
- package/dist/core/run-state.d.ts +25 -0
- package/dist/core/run-state.d.ts.map +1 -0
- package/dist/core/run-state.js +93 -0
- package/dist/core/run-state.js.map +1 -0
- package/dist/core/scheduler.d.ts +13 -0
- package/dist/core/scheduler.d.ts.map +1 -0
- package/dist/core/scheduler.js +35 -0
- package/dist/core/scheduler.js.map +1 -0
- package/dist/core/task-executor.d.ts +13 -0
- package/dist/core/task-executor.d.ts.map +1 -0
- package/dist/core/task-executor.js +639 -0
- package/dist/core/task-executor.js.map +1 -0
- package/dist/core/trigger-errors.d.ts +9 -0
- package/dist/core/trigger-errors.d.ts.map +1 -0
- package/dist/core/trigger-errors.js +15 -0
- package/dist/core/trigger-errors.js.map +1 -0
- package/dist/engine.d.ts +6 -14
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +68 -1035
- package/dist/engine.js.map +1 -1
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/pipeline-definition.d.ts +3 -0
- package/dist/pipeline-definition.d.ts.map +1 -0
- package/dist/pipeline-definition.js +4 -0
- package/dist/pipeline-definition.js.map +1 -0
- package/dist/pipeline-runner.d.ts +2 -1
- package/dist/pipeline-runner.d.ts.map +1 -1
- package/dist/pipeline-runner.js +2 -2
- package/dist/pipeline-runner.js.map +1 -1
- package/dist/plugins.d.ts +5 -0
- package/dist/plugins.d.ts.map +1 -0
- package/dist/plugins.js +3 -0
- package/dist/plugins.js.map +1 -0
- package/dist/registry.d.ts +3 -19
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +7 -35
- package/dist/registry.js.map +1 -1
- package/dist/tagma.d.ts +24 -0
- package/dist/tagma.d.ts.map +1 -0
- package/dist/tagma.js +23 -0
- package/dist/tagma.js.map +1 -0
- package/dist/utils-api.d.ts +2 -0
- package/dist/utils-api.d.ts.map +1 -0
- package/dist/utils-api.js +2 -0
- package/dist/utils-api.js.map +1 -0
- package/dist/yaml.d.ts +4 -0
- package/dist/yaml.d.ts.map +1 -0
- package/dist/yaml.js +3 -0
- package/dist/yaml.js.map +1 -0
- package/package.json +52 -7
- package/src/bootstrap.ts +6 -6
- package/src/config.ts +26 -0
- package/src/core/dataflow.test.ts +167 -0
- package/src/core/dataflow.ts +118 -0
- package/src/core/log-prune.test.ts +58 -0
- package/src/core/log-prune.ts +43 -0
- package/src/core/preflight.test.ts +49 -0
- package/src/core/preflight.ts +89 -0
- package/src/core/run-context.test.ts +244 -0
- package/src/core/run-context.ts +207 -0
- package/src/core/run-state.test.ts +98 -0
- package/src/core/run-state.ts +122 -0
- package/src/core/scheduler.test.ts +83 -0
- package/src/core/scheduler.ts +42 -0
- package/src/core/task-executor.ts +803 -0
- package/src/core/trigger-errors.ts +15 -0
- package/src/engine.ts +80 -1248
- package/src/index.ts +28 -0
- package/src/pipeline-definition.ts +5 -0
- package/src/pipeline-runner.ts +3 -2
- package/src/plugin-registry.test.ts +7 -10
- package/src/plugins.ts +18 -0
- package/src/registry.ts +7 -49
- package/src/tagma.test.ts +84 -0
- package/src/tagma.ts +47 -0
- package/src/utils-api.ts +8 -0
- package/src/yaml.ts +11 -0
- package/dist/sdk.d.ts +0 -32
- package/dist/sdk.d.ts.map +0 -1
- package/dist/sdk.js +0 -41
- package/dist/sdk.js.map +0 -1
- package/src/sdk.ts +0 -151
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,122 @@
|
|
|
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
|
+
}
|