@tagma/sdk 0.1.7 → 0.1.9

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/src/dag.ts CHANGED
@@ -1,137 +1,222 @@
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, RawPipelineConfig, RawTaskConfig, 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
+ }
138
+
139
+ // ═══ Raw DAG (for visual editor — no workDir required) ═══
140
+
141
+ export interface RawDagNode {
142
+ readonly taskId: string; // fully qualified: track_id.task_id
143
+ readonly trackId: string;
144
+ readonly rawTask: RawTaskConfig;
145
+ readonly dependsOn: readonly string[]; // fully qualified IDs, best-effort resolved
146
+ }
147
+
148
+ export interface RawDag {
149
+ readonly nodes: ReadonlyMap<string, RawDagNode>;
150
+ /** Directed edges: from → to means "from must complete before to starts" */
151
+ readonly edges: readonly { readonly from: string; readonly to: string }[];
152
+ }
153
+
154
+ /**
155
+ * Build a lightweight DAG from a raw (unresolved) pipeline config.
156
+ * Unlike buildDag, this function:
157
+ * - Does not require a workDir or resolved PipelineConfig
158
+ * - Is lenient: missing or ambiguous refs are silently skipped
159
+ * - Skips template-expansion tasks (those with a `use` field)
160
+ *
161
+ * Intended for the visual editor to render the flow graph before a pipeline is run.
162
+ */
163
+ export function buildRawDag(config: RawPipelineConfig): RawDag {
164
+ const nodes = new Map<string, RawDagNode>();
165
+ const bareToQualified = new Map<string, string>();
166
+
167
+ // 1. Register all concrete tasks
168
+ for (const track of config.tracks) {
169
+ for (const task of track.tasks) {
170
+ if (task.use) continue; // template-expansion tasks are not yet materialized
171
+ const qid = `${track.id}.${task.id}`;
172
+ if (nodes.has(qid)) continue; // skip duplicates silently
173
+
174
+ if (bareToQualified.has(task.id)) {
175
+ bareToQualified.set(task.id, '__ambiguous__');
176
+ } else {
177
+ bareToQualified.set(task.id, qid);
178
+ }
179
+ nodes.set(qid, { taskId: qid, trackId: track.id, rawTask: task, dependsOn: [] });
180
+ }
181
+ }
182
+
183
+ // 2. Resolve dependency refs leniently (missing / ambiguous refs are skipped)
184
+ function tryResolve(ref: string, fromTrackId: string): string | null {
185
+ if (ref.includes('.')) return nodes.has(ref) ? ref : null;
186
+ const sameTrack = `${fromTrackId}.${ref}`;
187
+ if (nodes.has(sameTrack)) return sameTrack;
188
+ const global = bareToQualified.get(ref);
189
+ if (global && global !== '__ambiguous__') return global;
190
+ return null;
191
+ }
192
+
193
+ const edges: { from: string; to: string }[] = [];
194
+
195
+ for (const track of config.tracks) {
196
+ for (const task of track.tasks) {
197
+ if (task.use) continue;
198
+ const qid = `${track.id}.${task.id}`;
199
+ const deps: string[] = [];
200
+
201
+ for (const ref of task.depends_on ?? []) {
202
+ const resolved = tryResolve(ref, track.id);
203
+ if (resolved && !deps.includes(resolved)) {
204
+ deps.push(resolved);
205
+ edges.push({ from: resolved, to: qid });
206
+ }
207
+ }
208
+ if (task.continue_from) {
209
+ const resolved = tryResolve(task.continue_from, track.id);
210
+ if (resolved && !deps.includes(resolved)) {
211
+ deps.push(resolved);
212
+ edges.push({ from: resolved, to: qid });
213
+ }
214
+ }
215
+
216
+ const node = nodes.get(qid)!;
217
+ nodes.set(qid, { ...node, dependsOn: deps });
218
+ }
219
+ }
220
+
221
+ return { nodes, edges };
222
+ }