@tagma/sdk 0.1.3 → 0.1.5

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.
@@ -0,0 +1,199 @@
1
+ // ═══ Raw Pipeline Config Validation ═══
2
+ //
3
+ // Validates a RawPipelineConfig without resolving inheritance or executing
4
+ // anything — intended for real-time feedback in a visual editor (e.g. drag
5
+ // to add a task, live error highlighting).
6
+ //
7
+ // Returns a flat list of ValidationError objects. An empty array means valid.
8
+
9
+ import type { RawPipelineConfig } from './types';
10
+
11
+ export interface ValidationError {
12
+ /** JSONPath-style location, e.g. "tracks[0].tasks[1].prompt" */
13
+ path: string;
14
+ message: string;
15
+ }
16
+
17
+ /**
18
+ * Validate a raw pipeline config.
19
+ * Checks structure, required fields, prompt/command exclusivity,
20
+ * depends_on reference integrity, and circular dependencies.
21
+ *
22
+ * Does NOT check plugin registration — plugins may not be loaded yet
23
+ * when the frontend is editing a config offline.
24
+ */
25
+ export function validateRaw(config: RawPipelineConfig): ValidationError[] {
26
+ const errors: ValidationError[] = [];
27
+
28
+ // ── Top level ──
29
+ if (!config.name?.trim()) {
30
+ errors.push({ path: 'name', message: 'Pipeline name is required' });
31
+ }
32
+
33
+ if (!config.tracks || config.tracks.length === 0) {
34
+ errors.push({ path: 'tracks', message: 'At least one track is required' });
35
+ return errors; // No point going further without tracks
36
+ }
37
+
38
+ // ── Build qualified ID sets for cross-reference checks ──
39
+ // Qualified ID format: "trackId.taskId" (mirrors the engine's convention)
40
+ const allQualified = new Set<string>();
41
+ // For bare depends_on references: bare taskId → first qualified ID found
42
+ const bareToQualified = new Map<string, string>();
43
+
44
+ for (const track of config.tracks) {
45
+ if (!track.id) continue;
46
+ for (const task of track.tasks ?? []) {
47
+ if (!task.id) continue;
48
+ const qid = `${track.id}.${task.id}`;
49
+ allQualified.add(qid);
50
+ if (!bareToQualified.has(task.id)) {
51
+ bareToQualified.set(task.id, qid);
52
+ }
53
+ }
54
+ }
55
+
56
+ // ── Per-track validation ──
57
+ for (let ti = 0; ti < config.tracks.length; ti++) {
58
+ const track = config.tracks[ti];
59
+ const trackPath = `tracks[${ti}]`;
60
+
61
+ if (!track.id?.trim()) {
62
+ errors.push({ path: `${trackPath}.id`, message: 'Track id is required' });
63
+ }
64
+ if (!track.name?.trim()) {
65
+ errors.push({ path: `${trackPath}.name`, message: 'Track name is required' });
66
+ }
67
+
68
+ if (!track.tasks || track.tasks.length === 0) {
69
+ errors.push({ path: `${trackPath}.tasks`, message: `Track "${track.id || ti}": must have at least one task` });
70
+ continue;
71
+ }
72
+
73
+ // ── Per-task validation ──
74
+ for (let ki = 0; ki < track.tasks.length; ki++) {
75
+ const task = track.tasks[ki];
76
+ const taskPath = `${trackPath}.tasks[${ki}]`;
77
+
78
+ if (!task.id?.trim()) {
79
+ errors.push({ path: `${taskPath}.id`, message: 'Task id is required' });
80
+ continue; // Can't check further without an id
81
+ }
82
+
83
+ // Template-based tasks: skip prompt/command checks (params validated at runtime)
84
+ if (task.use) continue;
85
+
86
+ const hasPrompt = typeof task.prompt === 'string' && task.prompt.trim().length > 0;
87
+ const hasCommand = typeof task.command === 'string' && task.command.trim().length > 0;
88
+
89
+ if (!hasPrompt && !hasCommand) {
90
+ errors.push({
91
+ path: taskPath,
92
+ message: `Task "${task.id}": must have "prompt" or "command"`,
93
+ });
94
+ }
95
+ if (hasPrompt && hasCommand) {
96
+ errors.push({
97
+ path: taskPath,
98
+ message: `Task "${task.id}": cannot have both "prompt" and "command"`,
99
+ });
100
+ }
101
+
102
+ // ── depends_on reference checks ──
103
+ if (task.depends_on && task.depends_on.length > 0) {
104
+ for (const dep of task.depends_on) {
105
+ const resolved = resolveDepRef(dep, track.id, allQualified, bareToQualified);
106
+ if (!resolved) {
107
+ errors.push({
108
+ path: `${taskPath}.depends_on`,
109
+ message: `Task "${task.id}": depends_on "${dep}" — no such task found`,
110
+ });
111
+ }
112
+ }
113
+ }
114
+
115
+ // ── continue_from reference check ──
116
+ if (task.continue_from) {
117
+ const resolved = resolveDepRef(task.continue_from, track.id, allQualified, bareToQualified);
118
+ if (!resolved) {
119
+ errors.push({
120
+ path: `${taskPath}.continue_from`,
121
+ message: `Task "${task.id}": continue_from "${task.continue_from}" — no such task found`,
122
+ });
123
+ }
124
+ }
125
+ }
126
+ }
127
+
128
+ // ── Cycle detection ──
129
+ errors.push(...detectCycles(config, allQualified, bareToQualified));
130
+
131
+ return errors;
132
+ }
133
+
134
+ // ── Helpers ──
135
+
136
+ function resolveDepRef(
137
+ ref: string,
138
+ fromTrackId: string,
139
+ allQualified: Set<string>,
140
+ bareToQualified: Map<string, string>,
141
+ ): string | null {
142
+ // Fully qualified reference (trackId.taskId)
143
+ if (allQualified.has(ref)) return ref;
144
+ // Same-track shorthand (just taskId)
145
+ const sameTrack = `${fromTrackId}.${ref}`;
146
+ if (allQualified.has(sameTrack)) return sameTrack;
147
+ // Global bare lookup (first match across all tracks)
148
+ return bareToQualified.get(ref) ?? null;
149
+ }
150
+
151
+ function detectCycles(
152
+ config: RawPipelineConfig,
153
+ allQualified: Set<string>,
154
+ bareToQualified: Map<string, string>,
155
+ ): ValidationError[] {
156
+ // Build adjacency: qualifiedId → [resolved dep qualifiedIds]
157
+ const adj = new Map<string, string[]>();
158
+
159
+ for (const track of config.tracks) {
160
+ if (!track.id) continue;
161
+ for (const task of track.tasks ?? []) {
162
+ if (!task.id || task.use) continue;
163
+ const qid = `${track.id}.${task.id}`;
164
+ const deps: string[] = [];
165
+ for (const dep of task.depends_on ?? []) {
166
+ const resolved = resolveDepRef(dep, track.id, allQualified, bareToQualified);
167
+ if (resolved) deps.push(resolved);
168
+ }
169
+ adj.set(qid, deps);
170
+ }
171
+ }
172
+
173
+ const errors: ValidationError[] = [];
174
+ const visited = new Set<string>();
175
+ const inStack = new Set<string>();
176
+
177
+ function dfs(id: string, path: string[]): void {
178
+ if (inStack.has(id)) {
179
+ // Trim path to just the cycle portion
180
+ const cycleStart = path.indexOf(id);
181
+ const cycle = [...path.slice(cycleStart), id].join(' → ');
182
+ errors.push({ path: 'tracks', message: `Circular dependency detected: ${cycle}` });
183
+ return;
184
+ }
185
+ if (visited.has(id)) return;
186
+ visited.add(id);
187
+ inStack.add(id);
188
+ for (const dep of adj.get(id) ?? []) {
189
+ dfs(dep, [...path, id]);
190
+ }
191
+ inStack.delete(id);
192
+ }
193
+
194
+ for (const id of adj.keys()) {
195
+ if (!visited.has(id)) dfs(id, []);
196
+ }
197
+
198
+ return errors;
199
+ }