@tagma/sdk 0.6.11 → 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 +91 -18
- 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-ops.d.ts +4 -2
- package/dist/config-ops.d.ts.map +1 -1
- package/dist/config-ops.js +16 -2
- package/dist/config-ops.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 +71 -990
- 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/ports.d.ts +23 -1
- package/dist/ports.d.ts.map +1 -1
- package/dist/ports.js +160 -0
- package/dist/ports.js.map +1 -1
- 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/schema.d.ts.map +1 -1
- package/dist/schema.js +7 -3
- package/dist/schema.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/validate-raw.js +118 -0
- package/dist/validate-raw.js.map +1 -1
- 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 +53 -8
- package/src/bootstrap.ts +6 -6
- package/src/config-ops.ts +12 -2
- 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-ports.test.ts +66 -0
- package/src/engine-task-type.test.ts +56 -0
- package/src/engine.ts +86 -1180
- 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/ports.test.ts +127 -0
- package/src/ports.ts +224 -1
- package/src/registry.ts +7 -49
- package/src/schema-ports.test.ts +86 -0
- package/src/schema.ts +7 -3
- package/src/tagma.test.ts +84 -0
- package/src/tagma.ts +47 -0
- package/src/utils-api.ts +8 -0
- package/src/validate-raw-ports.test.ts +66 -0
- package/src/validate-raw.ts +137 -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 -147
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import { mkdtempSync, mkdirSync, readdirSync, rmSync } from 'node:fs';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { pruneLogDirs } from './log-prune';
|
|
6
|
+
|
|
7
|
+
function fixture(): string {
|
|
8
|
+
return mkdtempSync(join(tmpdir(), 'tagma-prune-'));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
describe('pruneLogDirs', () => {
|
|
12
|
+
test('returns silently when logsDir does not exist', async () => {
|
|
13
|
+
const root = fixture();
|
|
14
|
+
try {
|
|
15
|
+
await expect(
|
|
16
|
+
pruneLogDirs(join(root, 'nope'), 5, 'run_live'),
|
|
17
|
+
).resolves.toBeUndefined();
|
|
18
|
+
} finally {
|
|
19
|
+
rmSync(root, { recursive: true, force: true });
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test('keeps the live run plus (keep-1) most recent historical runs', async () => {
|
|
24
|
+
const root = fixture();
|
|
25
|
+
try {
|
|
26
|
+
for (const id of ['run_001', 'run_002', 'run_003', 'run_004', 'run_005']) {
|
|
27
|
+
mkdirSync(join(root, id));
|
|
28
|
+
}
|
|
29
|
+
await pruneLogDirs(root, 3, 'run_005'); // keep=3 → 1 live + 2 historical
|
|
30
|
+
expect(readdirSync(root).sort()).toEqual(['run_003', 'run_004', 'run_005']);
|
|
31
|
+
} finally {
|
|
32
|
+
rmSync(root, { recursive: true, force: true });
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('never deletes the excluded live runId even if it would be pruned', async () => {
|
|
37
|
+
const root = fixture();
|
|
38
|
+
try {
|
|
39
|
+
for (const id of ['run_001', 'run_002', 'run_003']) mkdirSync(join(root, id));
|
|
40
|
+
await pruneLogDirs(root, 1, 'run_001');
|
|
41
|
+
expect(readdirSync(root).sort()).toEqual(['run_001']);
|
|
42
|
+
} finally {
|
|
43
|
+
rmSync(root, { recursive: true, force: true });
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('ignores entries that do not look like run dirs', async () => {
|
|
48
|
+
const root = fixture();
|
|
49
|
+
try {
|
|
50
|
+
mkdirSync(join(root, 'run_001'));
|
|
51
|
+
mkdirSync(join(root, 'not_a_run'));
|
|
52
|
+
await pruneLogDirs(root, 1, 'run_001');
|
|
53
|
+
expect(readdirSync(root).sort()).toEqual(['not_a_run', 'run_001']);
|
|
54
|
+
} finally {
|
|
55
|
+
rmSync(root, { recursive: true, force: true });
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { readdir, rm } from 'node:fs/promises';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Delete the oldest subdirectories under `logsDir`, keeping only the
|
|
6
|
+
* most recent `keep` total runs (including the currently-live run
|
|
7
|
+
* identified by `excludeRunId`). Directories are sorted
|
|
8
|
+
* lexicographically; because runIds are prefixed with a base-36
|
|
9
|
+
* timestamp, lexicographic order equals chronological order.
|
|
10
|
+
*
|
|
11
|
+
* `excludeRunId` is always skipped from deletion even if it would
|
|
12
|
+
* otherwise be pruned — this prevents a concurrent run from removing a
|
|
13
|
+
* live log directory that is still in use.
|
|
14
|
+
*
|
|
15
|
+
* The live run occupies one slot out of `keep`, so the maximum number
|
|
16
|
+
* of *historical* dirs to retain is `keep - 1`.
|
|
17
|
+
*/
|
|
18
|
+
export async function pruneLogDirs(
|
|
19
|
+
logsDir: string,
|
|
20
|
+
keep: number,
|
|
21
|
+
excludeRunId: string,
|
|
22
|
+
): Promise<void> {
|
|
23
|
+
let entries: string[];
|
|
24
|
+
try {
|
|
25
|
+
entries = await readdir(logsDir);
|
|
26
|
+
} catch {
|
|
27
|
+
return; // logsDir doesn't exist yet
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const runDirs = entries
|
|
31
|
+
.filter((e) => e.startsWith('run_') && e !== excludeRunId)
|
|
32
|
+
.sort();
|
|
33
|
+
const historyKeep = Math.max(0, keep - 1);
|
|
34
|
+
const toDelete = runDirs.slice(0, Math.max(0, runDirs.length - historyKeep));
|
|
35
|
+
|
|
36
|
+
await Promise.all(
|
|
37
|
+
toDelete.map((dir) =>
|
|
38
|
+
rm(resolve(logsDir, dir), { recursive: true, force: true }).catch(() => {
|
|
39
|
+
// Ignore deletion errors — stale dirs are better than a crash
|
|
40
|
+
}),
|
|
41
|
+
),
|
|
42
|
+
);
|
|
43
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import { preflight } from './preflight';
|
|
3
|
+
import { buildDag } from '../dag';
|
|
4
|
+
import { PluginRegistry } from '../registry';
|
|
5
|
+
import { bootstrapBuiltins } from '../bootstrap';
|
|
6
|
+
import type { PipelineConfig } from '../types';
|
|
7
|
+
|
|
8
|
+
function emptyConfig(overrides: Partial<PipelineConfig> = {}): PipelineConfig {
|
|
9
|
+
return {
|
|
10
|
+
name: 'p',
|
|
11
|
+
tracks: [{ id: 't', name: 'T', tasks: [] }],
|
|
12
|
+
...overrides,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
describe('preflight', () => {
|
|
17
|
+
test('throws when a referenced driver is not registered', () => {
|
|
18
|
+
const reg = new PluginRegistry();
|
|
19
|
+
const cfg = emptyConfig({
|
|
20
|
+
tracks: [
|
|
21
|
+
{ id: 't', name: 'T', tasks: [{ id: 'a', name: 'A', prompt: 'hi' }] },
|
|
22
|
+
],
|
|
23
|
+
});
|
|
24
|
+
expect(() => preflight(cfg, buildDag(cfg), reg)).toThrow(
|
|
25
|
+
/driver "opencode" not registered/,
|
|
26
|
+
);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('passes when all referenced plugins are registered', () => {
|
|
30
|
+
const reg = new PluginRegistry();
|
|
31
|
+
bootstrapBuiltins(reg);
|
|
32
|
+
const cfg = emptyConfig({
|
|
33
|
+
tracks: [
|
|
34
|
+
{ id: 't', name: 'T', tasks: [{ id: 'a', name: 'A', prompt: 'hi' }] },
|
|
35
|
+
],
|
|
36
|
+
});
|
|
37
|
+
expect(() => preflight(cfg, buildDag(cfg), reg)).not.toThrow();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('skips driver check for command-only tasks', () => {
|
|
41
|
+
const reg = new PluginRegistry(); // no drivers registered
|
|
42
|
+
const cfg = emptyConfig({
|
|
43
|
+
tracks: [
|
|
44
|
+
{ id: 't', name: 'T', tasks: [{ id: 'a', name: 'A', command: 'echo hi' }] },
|
|
45
|
+
],
|
|
46
|
+
});
|
|
47
|
+
expect(() => preflight(cfg, buildDag(cfg), reg)).not.toThrow();
|
|
48
|
+
});
|
|
49
|
+
});
|
|
@@ -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
|
+
}
|