@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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tagma/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Local AI task orchestration engine — core SDK for tagma pipelines",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -12,14 +12,59 @@
|
|
|
12
12
|
"homepage": "https://github.com/GoTagma/tagma-mono/tree/main/packages/sdk#readme",
|
|
13
13
|
"type": "module",
|
|
14
14
|
"sideEffects": false,
|
|
15
|
-
"main": "./dist/
|
|
16
|
-
"module": "./dist/
|
|
17
|
-
"types": "./dist/
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"module": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
18
|
"exports": {
|
|
19
19
|
".": {
|
|
20
|
-
"types": "./dist/
|
|
21
|
-
"import": "./dist/
|
|
22
|
-
"default": "./dist/
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./yaml": {
|
|
25
|
+
"types": "./dist/yaml.d.ts",
|
|
26
|
+
"import": "./dist/yaml.js",
|
|
27
|
+
"default": "./dist/yaml.js"
|
|
28
|
+
},
|
|
29
|
+
"./config": {
|
|
30
|
+
"types": "./dist/config.d.ts",
|
|
31
|
+
"import": "./dist/config.js",
|
|
32
|
+
"default": "./dist/config.js"
|
|
33
|
+
},
|
|
34
|
+
"./plugins": {
|
|
35
|
+
"types": "./dist/plugins.d.ts",
|
|
36
|
+
"import": "./dist/plugins.js",
|
|
37
|
+
"default": "./dist/plugins.js"
|
|
38
|
+
},
|
|
39
|
+
"./runner": {
|
|
40
|
+
"types": "./dist/pipeline-runner.d.ts",
|
|
41
|
+
"import": "./dist/pipeline-runner.js",
|
|
42
|
+
"default": "./dist/pipeline-runner.js"
|
|
43
|
+
},
|
|
44
|
+
"./logger": {
|
|
45
|
+
"types": "./dist/logger.d.ts",
|
|
46
|
+
"import": "./dist/logger.js",
|
|
47
|
+
"default": "./dist/logger.js"
|
|
48
|
+
},
|
|
49
|
+
"./approval": {
|
|
50
|
+
"types": "./dist/approval.d.ts",
|
|
51
|
+
"import": "./dist/approval.js",
|
|
52
|
+
"default": "./dist/approval.js"
|
|
53
|
+
},
|
|
54
|
+
"./utils": {
|
|
55
|
+
"types": "./dist/utils-api.d.ts",
|
|
56
|
+
"import": "./dist/utils-api.js",
|
|
57
|
+
"default": "./dist/utils-api.js"
|
|
58
|
+
},
|
|
59
|
+
"./adapters/stdin-approval": {
|
|
60
|
+
"types": "./dist/adapters/stdin-approval.d.ts",
|
|
61
|
+
"import": "./dist/adapters/stdin-approval.js",
|
|
62
|
+
"default": "./dist/adapters/stdin-approval.js"
|
|
63
|
+
},
|
|
64
|
+
"./adapters/websocket-approval": {
|
|
65
|
+
"types": "./dist/adapters/websocket-approval.d.ts",
|
|
66
|
+
"import": "./dist/adapters/websocket-approval.js",
|
|
67
|
+
"default": "./dist/adapters/websocket-approval.js"
|
|
23
68
|
},
|
|
24
69
|
"./ports": {
|
|
25
70
|
"types": "./dist/ports.d.ts",
|
package/src/bootstrap.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { PluginRegistry } from './registry';
|
|
2
2
|
|
|
3
3
|
// Built-in Drivers
|
|
4
4
|
// Only opencode is built in. Other drivers (codex, claude-code) ship as
|
|
@@ -20,15 +20,15 @@ import { OutputCheckCompletion } from './completions/output-check';
|
|
|
20
20
|
import { StaticContextMiddleware } from './middlewares/static-context';
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
|
-
* Register every built-in plugin into `target
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
23
|
+
* Register every built-in plugin into `target`. Hosts instantiate one
|
|
24
|
+
* PluginRegistry per workspace or SDK instance and call this once per
|
|
25
|
+
* instance so each workspace sees the same built-ins without sharing
|
|
26
|
+
* registration state.
|
|
27
27
|
*
|
|
28
28
|
* Built-in handlers are stateless module singletons — registering the same
|
|
29
29
|
* handler object into N registries is cheap and safe; no cloning is needed.
|
|
30
30
|
*/
|
|
31
|
-
export function bootstrapBuiltins(target: PluginRegistry
|
|
31
|
+
export function bootstrapBuiltins(target: PluginRegistry): void {
|
|
32
32
|
// Drivers
|
|
33
33
|
target.registerPlugin('drivers', 'opencode', OpenCodeDriver);
|
|
34
34
|
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export {
|
|
2
|
+
createEmptyPipeline,
|
|
3
|
+
setPipelineField,
|
|
4
|
+
upsertTrack,
|
|
5
|
+
removeTrack,
|
|
6
|
+
moveTrack,
|
|
7
|
+
updateTrack,
|
|
8
|
+
upsertTask,
|
|
9
|
+
removeTask,
|
|
10
|
+
moveTask,
|
|
11
|
+
transferTask,
|
|
12
|
+
} from './config-ops';
|
|
13
|
+
export { validateRaw } from './validate-raw';
|
|
14
|
+
export type { ValidationError, KnownPluginTypes } from './validate-raw';
|
|
15
|
+
export { buildRawDag } from './dag';
|
|
16
|
+
export type { RawDag, RawDagNode } from './dag';
|
|
17
|
+
export {
|
|
18
|
+
TASK_ID_RE,
|
|
19
|
+
isValidTaskId,
|
|
20
|
+
qualifyTaskId,
|
|
21
|
+
isQualifiedRef,
|
|
22
|
+
buildTaskIndex,
|
|
23
|
+
resolveTaskRef,
|
|
24
|
+
AMBIGUOUS,
|
|
25
|
+
} from './task-ref';
|
|
26
|
+
export type { TaskIndex, RefResolution } from './task-ref';
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import { buildDag } from '../dag';
|
|
3
|
+
import type { PipelineConfig, TaskResult } from '../types';
|
|
4
|
+
import { RunContext } from './run-context';
|
|
5
|
+
import {
|
|
6
|
+
inferEffectivePorts,
|
|
7
|
+
extractSuccessfulOutputs,
|
|
8
|
+
} from './dataflow';
|
|
9
|
+
|
|
10
|
+
function makeContext(config: PipelineConfig): RunContext {
|
|
11
|
+
return new RunContext({
|
|
12
|
+
runId: 'run_dataflow',
|
|
13
|
+
dag: buildDag(config),
|
|
14
|
+
config,
|
|
15
|
+
workDir: '/tmp/wd',
|
|
16
|
+
pipelineInfo: {
|
|
17
|
+
name: config.name,
|
|
18
|
+
run_id: 'run_dataflow',
|
|
19
|
+
started_at: '2026-04-26T00:00:00Z',
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function result(stdout: string, normalizedOutput: string | null = null): TaskResult {
|
|
25
|
+
return {
|
|
26
|
+
exitCode: 0,
|
|
27
|
+
stdout,
|
|
28
|
+
stderr: '',
|
|
29
|
+
stdoutPath: null,
|
|
30
|
+
stderrPath: null,
|
|
31
|
+
durationMs: 1,
|
|
32
|
+
sessionId: null,
|
|
33
|
+
normalizedOutput,
|
|
34
|
+
failureKind: null,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
describe('inferEffectivePorts', () => {
|
|
39
|
+
test('returns declared ports for command tasks', () => {
|
|
40
|
+
const config: PipelineConfig = {
|
|
41
|
+
name: 'p',
|
|
42
|
+
tracks: [
|
|
43
|
+
{
|
|
44
|
+
id: 't',
|
|
45
|
+
name: 'T',
|
|
46
|
+
tasks: [
|
|
47
|
+
{
|
|
48
|
+
id: 'cmd',
|
|
49
|
+
name: 'Cmd',
|
|
50
|
+
command: 'echo',
|
|
51
|
+
ports: { outputs: [{ name: 'city', type: 'string' }] },
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
};
|
|
57
|
+
const ctx = makeContext(config);
|
|
58
|
+
const inferred = inferEffectivePorts(ctx, 't.cmd');
|
|
59
|
+
expect(inferred.kind).toBe('ready');
|
|
60
|
+
if (inferred.kind === 'ready') {
|
|
61
|
+
expect(inferred.isPromptTask).toBe(false);
|
|
62
|
+
expect(inferred.effectivePorts?.outputs?.[0]?.name).toBe('city');
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('infers prompt inputs from upstream command outputs', () => {
|
|
67
|
+
const config: PipelineConfig = {
|
|
68
|
+
name: 'p',
|
|
69
|
+
tracks: [
|
|
70
|
+
{
|
|
71
|
+
id: 't',
|
|
72
|
+
name: 'T',
|
|
73
|
+
tasks: [
|
|
74
|
+
{
|
|
75
|
+
id: 'up',
|
|
76
|
+
name: 'Up',
|
|
77
|
+
command: 'echo',
|
|
78
|
+
ports: { outputs: [{ name: 'city', type: 'string' }] },
|
|
79
|
+
},
|
|
80
|
+
{ id: 'prompt', name: 'Prompt', prompt: 'hi', depends_on: ['up'] },
|
|
81
|
+
],
|
|
82
|
+
},
|
|
83
|
+
],
|
|
84
|
+
};
|
|
85
|
+
const ctx = makeContext(config);
|
|
86
|
+
const inferred = inferEffectivePorts(ctx, 't.prompt');
|
|
87
|
+
expect(inferred.kind).toBe('ready');
|
|
88
|
+
if (inferred.kind === 'ready') {
|
|
89
|
+
expect(inferred.isPromptTask).toBe(true);
|
|
90
|
+
expect(inferred.effectivePorts?.inputs?.[0]?.name).toBe('city');
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test('blocks prompt tasks when upstream command outputs are ambiguous', () => {
|
|
95
|
+
const config: PipelineConfig = {
|
|
96
|
+
name: 'p',
|
|
97
|
+
tracks: [
|
|
98
|
+
{
|
|
99
|
+
id: 't',
|
|
100
|
+
name: 'T',
|
|
101
|
+
tasks: [
|
|
102
|
+
{
|
|
103
|
+
id: 'a',
|
|
104
|
+
name: 'A',
|
|
105
|
+
command: 'echo',
|
|
106
|
+
ports: { outputs: [{ name: 'city', type: 'string' }] },
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
id: 'b',
|
|
110
|
+
name: 'B',
|
|
111
|
+
command: 'echo',
|
|
112
|
+
ports: { outputs: [{ name: 'city', type: 'string' }] },
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
id: 'prompt',
|
|
116
|
+
name: 'Prompt',
|
|
117
|
+
prompt: 'hi',
|
|
118
|
+
depends_on: ['a', 'b'],
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
},
|
|
122
|
+
],
|
|
123
|
+
};
|
|
124
|
+
const ctx = makeContext(config);
|
|
125
|
+
const inferred = inferEffectivePorts(ctx, 't.prompt');
|
|
126
|
+
expect(inferred.kind).toBe('blocked');
|
|
127
|
+
if (inferred.kind === 'blocked') {
|
|
128
|
+
expect(inferred.reason).toContain('city');
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
describe('extractSuccessfulOutputs', () => {
|
|
134
|
+
test('combines lightweight binding outputs with typed port outputs', () => {
|
|
135
|
+
const config: PipelineConfig = {
|
|
136
|
+
name: 'p',
|
|
137
|
+
tracks: [
|
|
138
|
+
{
|
|
139
|
+
id: 't',
|
|
140
|
+
name: 'T',
|
|
141
|
+
tasks: [
|
|
142
|
+
{
|
|
143
|
+
id: 'cmd',
|
|
144
|
+
name: 'Cmd',
|
|
145
|
+
command: 'echo',
|
|
146
|
+
outputs: { raw: { from: 'stdout' } },
|
|
147
|
+
ports: { outputs: [{ name: 'city', type: 'string' }] },
|
|
148
|
+
},
|
|
149
|
+
],
|
|
150
|
+
},
|
|
151
|
+
],
|
|
152
|
+
};
|
|
153
|
+
const ctx = makeContext(config);
|
|
154
|
+
const node = ctx.dag.nodes.get('t.cmd')!;
|
|
155
|
+
const extracted = extractSuccessfulOutputs({
|
|
156
|
+
task: node.task,
|
|
157
|
+
effectivePorts: node.task.ports,
|
|
158
|
+
result: result('{"city":"Paris"}'),
|
|
159
|
+
});
|
|
160
|
+
expect(extracted.outputs).toEqual({
|
|
161
|
+
raw: '{"city":"Paris"}',
|
|
162
|
+
city: 'Paris',
|
|
163
|
+
});
|
|
164
|
+
expect(extracted.bindingDiagnostic).toBeNull();
|
|
165
|
+
expect(extracted.portDiagnostic).toBeNull();
|
|
166
|
+
});
|
|
167
|
+
});
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { TaskConfig, TaskPorts, TaskResult } from '../types';
|
|
2
|
+
import {
|
|
3
|
+
extractTaskBindingOutputs,
|
|
4
|
+
extractTaskOutputs,
|
|
5
|
+
inferPromptPorts,
|
|
6
|
+
} from '../ports';
|
|
7
|
+
import type { RunContext } from './run-context';
|
|
8
|
+
|
|
9
|
+
function isPromptTaskConfig(
|
|
10
|
+
task: TaskConfig,
|
|
11
|
+
): task is TaskConfig & { readonly prompt: string; readonly command?: undefined } {
|
|
12
|
+
return task.prompt !== undefined && task.command === undefined;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function isCommandTaskConfig(
|
|
16
|
+
task: TaskConfig,
|
|
17
|
+
): task is TaskConfig & { readonly command: string; readonly prompt?: undefined } {
|
|
18
|
+
return task.command !== undefined && task.prompt === undefined;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type EffectivePortsResult =
|
|
22
|
+
| {
|
|
23
|
+
readonly kind: 'ready';
|
|
24
|
+
readonly isPromptTask: boolean;
|
|
25
|
+
readonly effectivePorts: TaskPorts | undefined;
|
|
26
|
+
}
|
|
27
|
+
| {
|
|
28
|
+
readonly kind: 'blocked';
|
|
29
|
+
readonly reason: string;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export function inferEffectivePorts(
|
|
33
|
+
ctx: RunContext,
|
|
34
|
+
taskId: string,
|
|
35
|
+
): EffectivePortsResult {
|
|
36
|
+
const node = ctx.dag.nodes.get(taskId)!;
|
|
37
|
+
const task = node.task;
|
|
38
|
+
const isPromptTask = isPromptTaskConfig(task);
|
|
39
|
+
|
|
40
|
+
if (!isPromptTask) {
|
|
41
|
+
return { kind: 'ready', isPromptTask: false, effectivePorts: task.ports };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const inference = inferPromptPorts({
|
|
45
|
+
upstreams: node.dependsOn.map((upstreamId) => {
|
|
46
|
+
const upstream = ctx.dag.nodes.get(upstreamId);
|
|
47
|
+
const isUpstreamCommand = upstream ? isCommandTaskConfig(upstream.task) : false;
|
|
48
|
+
return {
|
|
49
|
+
taskId: upstreamId,
|
|
50
|
+
outputs: isUpstreamCommand ? upstream?.task.ports?.outputs : undefined,
|
|
51
|
+
};
|
|
52
|
+
}),
|
|
53
|
+
downstreams: (ctx.directDownstreams.get(taskId) ?? []).map((downstreamId) => {
|
|
54
|
+
const downstream = ctx.dag.nodes.get(downstreamId);
|
|
55
|
+
const isDownstreamCommand = downstream ? isCommandTaskConfig(downstream.task) : false;
|
|
56
|
+
return {
|
|
57
|
+
taskId: downstreamId,
|
|
58
|
+
inputs: isDownstreamCommand ? downstream?.task.ports?.inputs : undefined,
|
|
59
|
+
};
|
|
60
|
+
}),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
if (inference.inputConflicts.length > 0 || inference.outputConflicts.length > 0) {
|
|
64
|
+
const lines: string[] = [];
|
|
65
|
+
for (const conflict of inference.inputConflicts) lines.push(conflict.reason);
|
|
66
|
+
for (const conflict of inference.outputConflicts) lines.push(conflict.reason);
|
|
67
|
+
return { kind: 'blocked', reason: lines.join('\n') };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return { kind: 'ready', isPromptTask: true, effectivePorts: inference.ports };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface ExtractSuccessfulOutputsOptions {
|
|
74
|
+
readonly task: TaskConfig;
|
|
75
|
+
readonly effectivePorts: TaskPorts | undefined;
|
|
76
|
+
readonly result: TaskResult;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface ExtractSuccessfulOutputsResult {
|
|
80
|
+
readonly outputs: Readonly<Record<string, unknown>> | null;
|
|
81
|
+
readonly bindingDiagnostic: string | null;
|
|
82
|
+
readonly portDiagnostic: string | null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function extractSuccessfulOutputs(
|
|
86
|
+
options: ExtractSuccessfulOutputsOptions,
|
|
87
|
+
): ExtractSuccessfulOutputsResult {
|
|
88
|
+
const { task, effectivePorts, result } = options;
|
|
89
|
+
let extractedOutputs: Readonly<Record<string, unknown>> | null = null;
|
|
90
|
+
|
|
91
|
+
const bindingExtraction = extractTaskBindingOutputs(
|
|
92
|
+
task.outputs,
|
|
93
|
+
result.stdout,
|
|
94
|
+
result.stderr,
|
|
95
|
+
result.normalizedOutput,
|
|
96
|
+
);
|
|
97
|
+
if (task.outputs && Object.keys(task.outputs).length > 0) {
|
|
98
|
+
extractedOutputs = bindingExtraction.outputs;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const portExtraction = extractTaskOutputs(
|
|
102
|
+
effectivePorts,
|
|
103
|
+
result.stdout,
|
|
104
|
+
result.normalizedOutput,
|
|
105
|
+
);
|
|
106
|
+
if (effectivePorts?.outputs && effectivePorts.outputs.length > 0) {
|
|
107
|
+
extractedOutputs = {
|
|
108
|
+
...(extractedOutputs ?? {}),
|
|
109
|
+
...portExtraction.outputs,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
outputs: extractedOutputs,
|
|
115
|
+
bindingDiagnostic: bindingExtraction.diagnostic,
|
|
116
|
+
portDiagnostic: portExtraction.diagnostic,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
@@ -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
|
+
}
|