@tagma/sdk 0.1.3 → 0.1.4
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 +139 -139
- package/package.json +4 -4
- package/src/adapters/stdin-approval.ts +117 -117
- package/src/adapters/websocket-approval.ts +144 -144
- package/src/completions/exit-code.ts +19 -19
- package/src/completions/file-exists.ts +39 -39
- package/src/completions/output-check.ts +57 -57
- package/src/dag.ts +137 -137
- package/src/drivers/claude-code.ts +207 -207
- package/src/engine.ts +637 -598
- package/src/hooks.ts +138 -138
- package/src/logger.ts +107 -100
- package/src/middlewares/static-context.ts +29 -29
- package/src/runner.ts +193 -193
- package/src/schema.ts +260 -260
- package/src/triggers/file.ts +94 -94
- package/src/triggers/manual.ts +61 -61
- package/src/utils.ts +147 -147
package/src/dag.ts
CHANGED
|
@@ -1,137 +1,137 @@
|
|
|
1
|
-
import type { PipelineConfig, TaskConfig, TrackConfig } from './types';
|
|
2
|
-
|
|
3
|
-
export interface DagNode {
|
|
4
|
-
readonly taskId: string; // fully qualified: track_id.task_id or just task_id
|
|
5
|
-
readonly task: TaskConfig;
|
|
6
|
-
readonly track: TrackConfig;
|
|
7
|
-
readonly dependsOn: readonly string[];
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface Dag {
|
|
11
|
-
readonly nodes: ReadonlyMap<string, DagNode>;
|
|
12
|
-
readonly sorted: readonly string[]; // topological order
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// Build a global task ID: for cross-track refs we use "track_id.task_id"
|
|
16
|
-
// Within a track, bare "task_id" is also valid
|
|
17
|
-
function qualifyId(trackId: string, taskId: string): string {
|
|
18
|
-
return `${trackId}.${taskId}`;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function buildDag(config: PipelineConfig): Dag {
|
|
22
|
-
const nodes = new Map<string, DagNode>();
|
|
23
|
-
// Map bare task IDs to qualified IDs (for resolving unqualified refs)
|
|
24
|
-
const bareToQualified = new Map<string, string>();
|
|
25
|
-
|
|
26
|
-
// 1. Register all nodes
|
|
27
|
-
for (const track of config.tracks) {
|
|
28
|
-
for (const task of track.tasks) {
|
|
29
|
-
const qid = qualifyId(track.id, task.id);
|
|
30
|
-
|
|
31
|
-
if (nodes.has(qid)) {
|
|
32
|
-
throw new Error(`Duplicate task ID: "${qid}"`);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// Track bare ID → qualified. If same bare ID in multiple tracks, mark ambiguous
|
|
36
|
-
if (bareToQualified.has(task.id)) {
|
|
37
|
-
bareToQualified.set(task.id, '__ambiguous__');
|
|
38
|
-
} else {
|
|
39
|
-
bareToQualified.set(task.id, qid);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
nodes.set(qid, {
|
|
43
|
-
taskId: qid,
|
|
44
|
-
task,
|
|
45
|
-
track,
|
|
46
|
-
dependsOn: [], // filled below
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// Helper to resolve a dependency ref to a qualified ID
|
|
52
|
-
function resolveRef(ref: string, fromTrackId: string): string {
|
|
53
|
-
// Already qualified (contains dot)
|
|
54
|
-
if (ref.includes('.')) {
|
|
55
|
-
if (!nodes.has(ref)) {
|
|
56
|
-
throw new Error(`Task reference "${ref}" not found`);
|
|
57
|
-
}
|
|
58
|
-
return ref;
|
|
59
|
-
}
|
|
60
|
-
// Try within same track first
|
|
61
|
-
const sameTrack = qualifyId(fromTrackId, ref);
|
|
62
|
-
if (nodes.has(sameTrack)) return sameTrack;
|
|
63
|
-
// Try global bare lookup
|
|
64
|
-
const global = bareToQualified.get(ref);
|
|
65
|
-
if (global && global !== '__ambiguous__') return global;
|
|
66
|
-
if (global === '__ambiguous__') {
|
|
67
|
-
throw new Error(
|
|
68
|
-
`Ambiguous task reference "${ref}" exists in multiple tracks. ` +
|
|
69
|
-
`Use "track_id.task_id" format.`
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
throw new Error(`Task reference "${ref}" not found`);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// 2. Resolve depends_on and continue_from to qualified IDs
|
|
76
|
-
for (const track of config.tracks) {
|
|
77
|
-
for (const task of track.tasks) {
|
|
78
|
-
const qid = qualifyId(track.id, task.id);
|
|
79
|
-
const deps: string[] = [];
|
|
80
|
-
|
|
81
|
-
if (task.depends_on) {
|
|
82
|
-
for (const dep of task.depends_on) {
|
|
83
|
-
deps.push(resolveRef(dep, track.id));
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
if (task.continue_from) {
|
|
87
|
-
const resolved = resolveRef(task.continue_from, track.id);
|
|
88
|
-
if (!deps.includes(resolved)) {
|
|
89
|
-
deps.push(resolved); // continue_from implies dependency
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// Replace node with resolved deps
|
|
94
|
-
const node = nodes.get(qid)!;
|
|
95
|
-
nodes.set(qid, { ...node, dependsOn: deps });
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// 3. Topological sort + cycle detection (Kahn's algorithm)
|
|
100
|
-
const inDegree = new Map<string, number>();
|
|
101
|
-
const adjacency = new Map<string, string[]>(); // parent → children
|
|
102
|
-
|
|
103
|
-
for (const [id] of nodes) {
|
|
104
|
-
inDegree.set(id, 0);
|
|
105
|
-
adjacency.set(id, []);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
for (const [id, node] of nodes) {
|
|
109
|
-
for (const dep of node.dependsOn) {
|
|
110
|
-
adjacency.get(dep)!.push(id);
|
|
111
|
-
inDegree.set(id, (inDegree.get(id) ?? 0) + 1);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const queue: string[] = [];
|
|
116
|
-
for (const [id, degree] of inDegree) {
|
|
117
|
-
if (degree === 0) queue.push(id);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const sorted: string[] = [];
|
|
121
|
-
while (queue.length > 0) {
|
|
122
|
-
const current = queue.shift()!;
|
|
123
|
-
sorted.push(current);
|
|
124
|
-
for (const child of adjacency.get(current)!) {
|
|
125
|
-
const newDegree = inDegree.get(child)! - 1;
|
|
126
|
-
inDegree.set(child, newDegree);
|
|
127
|
-
if (newDegree === 0) queue.push(child);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if (sorted.length !== nodes.size) {
|
|
132
|
-
const remaining = [...nodes.keys()].filter(id => !sorted.includes(id));
|
|
133
|
-
throw new Error(`Circular dependency detected involving tasks: ${remaining.join(', ')}`);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
return { nodes, sorted };
|
|
137
|
-
}
|
|
1
|
+
import type { PipelineConfig, TaskConfig, TrackConfig } from './types';
|
|
2
|
+
|
|
3
|
+
export interface DagNode {
|
|
4
|
+
readonly taskId: string; // fully qualified: track_id.task_id or just task_id
|
|
5
|
+
readonly task: TaskConfig;
|
|
6
|
+
readonly track: TrackConfig;
|
|
7
|
+
readonly dependsOn: readonly string[];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface Dag {
|
|
11
|
+
readonly nodes: ReadonlyMap<string, DagNode>;
|
|
12
|
+
readonly sorted: readonly string[]; // topological order
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Build a global task ID: for cross-track refs we use "track_id.task_id"
|
|
16
|
+
// Within a track, bare "task_id" is also valid
|
|
17
|
+
function qualifyId(trackId: string, taskId: string): string {
|
|
18
|
+
return `${trackId}.${taskId}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function buildDag(config: PipelineConfig): Dag {
|
|
22
|
+
const nodes = new Map<string, DagNode>();
|
|
23
|
+
// Map bare task IDs to qualified IDs (for resolving unqualified refs)
|
|
24
|
+
const bareToQualified = new Map<string, string>();
|
|
25
|
+
|
|
26
|
+
// 1. Register all nodes
|
|
27
|
+
for (const track of config.tracks) {
|
|
28
|
+
for (const task of track.tasks) {
|
|
29
|
+
const qid = qualifyId(track.id, task.id);
|
|
30
|
+
|
|
31
|
+
if (nodes.has(qid)) {
|
|
32
|
+
throw new Error(`Duplicate task ID: "${qid}"`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Track bare ID → qualified. If same bare ID in multiple tracks, mark ambiguous
|
|
36
|
+
if (bareToQualified.has(task.id)) {
|
|
37
|
+
bareToQualified.set(task.id, '__ambiguous__');
|
|
38
|
+
} else {
|
|
39
|
+
bareToQualified.set(task.id, qid);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
nodes.set(qid, {
|
|
43
|
+
taskId: qid,
|
|
44
|
+
task,
|
|
45
|
+
track,
|
|
46
|
+
dependsOn: [], // filled below
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Helper to resolve a dependency ref to a qualified ID
|
|
52
|
+
function resolveRef(ref: string, fromTrackId: string): string {
|
|
53
|
+
// Already qualified (contains dot)
|
|
54
|
+
if (ref.includes('.')) {
|
|
55
|
+
if (!nodes.has(ref)) {
|
|
56
|
+
throw new Error(`Task reference "${ref}" not found`);
|
|
57
|
+
}
|
|
58
|
+
return ref;
|
|
59
|
+
}
|
|
60
|
+
// Try within same track first
|
|
61
|
+
const sameTrack = qualifyId(fromTrackId, ref);
|
|
62
|
+
if (nodes.has(sameTrack)) return sameTrack;
|
|
63
|
+
// Try global bare lookup
|
|
64
|
+
const global = bareToQualified.get(ref);
|
|
65
|
+
if (global && global !== '__ambiguous__') return global;
|
|
66
|
+
if (global === '__ambiguous__') {
|
|
67
|
+
throw new Error(
|
|
68
|
+
`Ambiguous task reference "${ref}" exists in multiple tracks. ` +
|
|
69
|
+
`Use "track_id.task_id" format.`
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
throw new Error(`Task reference "${ref}" not found`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// 2. Resolve depends_on and continue_from to qualified IDs
|
|
76
|
+
for (const track of config.tracks) {
|
|
77
|
+
for (const task of track.tasks) {
|
|
78
|
+
const qid = qualifyId(track.id, task.id);
|
|
79
|
+
const deps: string[] = [];
|
|
80
|
+
|
|
81
|
+
if (task.depends_on) {
|
|
82
|
+
for (const dep of task.depends_on) {
|
|
83
|
+
deps.push(resolveRef(dep, track.id));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (task.continue_from) {
|
|
87
|
+
const resolved = resolveRef(task.continue_from, track.id);
|
|
88
|
+
if (!deps.includes(resolved)) {
|
|
89
|
+
deps.push(resolved); // continue_from implies dependency
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Replace node with resolved deps
|
|
94
|
+
const node = nodes.get(qid)!;
|
|
95
|
+
nodes.set(qid, { ...node, dependsOn: deps });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// 3. Topological sort + cycle detection (Kahn's algorithm)
|
|
100
|
+
const inDegree = new Map<string, number>();
|
|
101
|
+
const adjacency = new Map<string, string[]>(); // parent → children
|
|
102
|
+
|
|
103
|
+
for (const [id] of nodes) {
|
|
104
|
+
inDegree.set(id, 0);
|
|
105
|
+
adjacency.set(id, []);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
for (const [id, node] of nodes) {
|
|
109
|
+
for (const dep of node.dependsOn) {
|
|
110
|
+
adjacency.get(dep)!.push(id);
|
|
111
|
+
inDegree.set(id, (inDegree.get(id) ?? 0) + 1);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const queue: string[] = [];
|
|
116
|
+
for (const [id, degree] of inDegree) {
|
|
117
|
+
if (degree === 0) queue.push(id);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const sorted: string[] = [];
|
|
121
|
+
while (queue.length > 0) {
|
|
122
|
+
const current = queue.shift()!;
|
|
123
|
+
sorted.push(current);
|
|
124
|
+
for (const child of adjacency.get(current)!) {
|
|
125
|
+
const newDegree = inDegree.get(child)! - 1;
|
|
126
|
+
inDegree.set(child, newDegree);
|
|
127
|
+
if (newDegree === 0) queue.push(child);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (sorted.length !== nodes.size) {
|
|
132
|
+
const remaining = [...nodes.keys()].filter(id => !sorted.includes(id));
|
|
133
|
+
throw new Error(`Circular dependency detected involving tasks: ${remaining.join(', ')}`);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return { nodes, sorted };
|
|
137
|
+
}
|