@tagma/sdk 0.5.2 → 0.6.1
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 +573 -573
- package/dist/drivers/opencode.d.ts.map +1 -1
- package/dist/drivers/opencode.js +47 -17
- package/dist/drivers/opencode.js.map +1 -1
- package/package.json +2 -2
- package/scripts/preinstall.js +31 -31
- package/src/adapters/stdin-approval.ts +106 -106
- package/src/adapters/websocket-approval.ts +224 -224
- package/src/approval.ts +131 -131
- package/src/bootstrap.ts +37 -37
- package/src/completions/exit-code.ts +34 -34
- package/src/completions/file-exists.ts +66 -66
- package/src/completions/output-check.ts +92 -92
- package/src/config-ops.ts +307 -307
- package/src/drivers/opencode.ts +68 -29
- package/src/engine.ts +1220 -1220
- package/src/hooks.ts +193 -193
- package/src/logger.ts +182 -182
- package/src/middlewares/static-context.ts +49 -49
- package/src/pipeline-runner.ts +173 -173
- package/src/registry.ts +267 -267
- package/src/runner.ts +460 -460
- package/src/schema.test.ts +101 -101
- package/src/schema.ts +338 -338
- package/src/sdk.ts +118 -118
- package/src/triggers/file.ts +164 -164
- package/src/triggers/manual.ts +86 -86
- package/src/types.ts +18 -18
- package/src/utils.ts +203 -203
- package/src/validate-raw.ts +412 -412
- package/dist/drivers/claude-code.d.ts +0 -3
- package/dist/drivers/claude-code.d.ts.map +0 -1
- package/dist/drivers/claude-code.js +0 -225
- package/dist/drivers/claude-code.js.map +0 -1
- package/dist/templates.d.ts +0 -20
- package/dist/templates.d.ts.map +0 -1
- package/dist/templates.js +0 -93
- package/dist/templates.js.map +0 -1
package/src/validate-raw.ts
CHANGED
|
@@ -1,412 +1,412 @@
|
|
|
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
|
-
import {
|
|
11
|
-
isValidTaskId,
|
|
12
|
-
qualifyTaskId,
|
|
13
|
-
buildTaskIndex,
|
|
14
|
-
resolveTaskRef,
|
|
15
|
-
type TaskIndex,
|
|
16
|
-
} from './task-ref';
|
|
17
|
-
|
|
18
|
-
const DURATION_RE = /^(\d*\.?\d+)\s*(s|m|h|d)$/;
|
|
19
|
-
function isValidDuration(input: string): boolean {
|
|
20
|
-
return DURATION_RE.test(input.trim());
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// D8: IDs may only contain letters, digits, underscores, and hyphens, and must
|
|
24
|
-
// start with a letter or underscore. Dots are explicitly forbidden because the
|
|
25
|
-
// engine uses "trackId.taskId" as the qualified separator — a dot in either
|
|
26
|
-
// part creates an ambiguous qualified ID and breaks resolveRef.
|
|
27
|
-
// Canonical regex and helper live in ./task-ref so every resolver (dag.ts,
|
|
28
|
-
// engine.ts, editor) stays in lockstep with what we accept here.
|
|
29
|
-
const isValidId = isValidTaskId;
|
|
30
|
-
|
|
31
|
-
const VALID_ON_FAILURE = new Set(['skip_downstream', 'stop_all', 'ignore']);
|
|
32
|
-
const VALID_REASONING_EFFORT = new Set(['low', 'medium', 'high']);
|
|
33
|
-
|
|
34
|
-
// Built-in plugin types always known to the SDK core, regardless of which
|
|
35
|
-
// external plugin packages are installed. These MUST stay in sync with the
|
|
36
|
-
// types that `bootstrapBuiltins()` registers, otherwise the editor will
|
|
37
|
-
// emit false-positive "unknown type" warnings for stock pipelines.
|
|
38
|
-
const BUILTIN_TRIGGER_TYPES: ReadonlySet<string> = new Set(['manual', 'file']);
|
|
39
|
-
const BUILTIN_COMPLETION_TYPES: ReadonlySet<string> = new Set([
|
|
40
|
-
'exit_code',
|
|
41
|
-
'file_exists',
|
|
42
|
-
'output_check',
|
|
43
|
-
]);
|
|
44
|
-
const BUILTIN_MIDDLEWARE_TYPES: ReadonlySet<string> = new Set(['static_context']);
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Optional second argument to `validateRaw`: the set of plugin types currently
|
|
48
|
-
* registered in the SDK runtime, keyed by category. Hosts (e.g. the editor
|
|
49
|
-
* server) pass this so `validateRaw` can emit a soft warning when a task
|
|
50
|
-
* references a type that isn't loaded — otherwise the Task panel would show
|
|
51
|
-
* no hint and the pipeline would only blow up at run time. Callers that
|
|
52
|
-
* legitimately validate a config offline (before plugins are loaded) can omit
|
|
53
|
-
* this argument and no plugin warnings will be produced.
|
|
54
|
-
*/
|
|
55
|
-
export interface KnownPluginTypes {
|
|
56
|
-
readonly triggers?: readonly string[];
|
|
57
|
-
readonly completions?: readonly string[];
|
|
58
|
-
readonly middlewares?: readonly string[];
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export type ValidationSeverity = 'error' | 'warning';
|
|
62
|
-
|
|
63
|
-
export interface ValidationError {
|
|
64
|
-
/** JSONPath-style location, e.g. "tracks[0].tasks[1].prompt" */
|
|
65
|
-
path: string;
|
|
66
|
-
message: string;
|
|
67
|
-
/**
|
|
68
|
-
* H8: not all "errors" are equally fatal. The DAG runtime is happy to
|
|
69
|
-
* insert implicit `continue_from → depends_on` ordering, so the matching
|
|
70
|
-
* validate-raw check is a *style* nit, not a hard failure. Severity lets
|
|
71
|
-
* the editor render it as a soft warning instead of blocking save / run.
|
|
72
|
-
* Existing call sites that don't read this field still treat every entry
|
|
73
|
-
* as fatal — defaulting `severity` to undefined preserves that behaviour.
|
|
74
|
-
*/
|
|
75
|
-
severity?: ValidationSeverity;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Validate a raw pipeline config.
|
|
80
|
-
* Checks structure, required fields, prompt/command exclusivity,
|
|
81
|
-
* depends_on reference integrity, and circular dependencies.
|
|
82
|
-
*
|
|
83
|
-
* Plugin type checks: when `knownTypes` is provided, task/track references to
|
|
84
|
-
* trigger/completion/middleware types that are neither built-in nor in the
|
|
85
|
-
* supplied set produce a soft warning (severity: 'warning') — these don't
|
|
86
|
-
* block save/run but light up the Task panel so users discover the broken
|
|
87
|
-
* reference in the editor instead of at run time. Omit `knownTypes` to skip
|
|
88
|
-
* plugin checks entirely (offline/pre-load validation).
|
|
89
|
-
*/
|
|
90
|
-
export function validateRaw(
|
|
91
|
-
config: RawPipelineConfig,
|
|
92
|
-
knownTypes?: KnownPluginTypes,
|
|
93
|
-
): ValidationError[] {
|
|
94
|
-
const errors: ValidationError[] = [];
|
|
95
|
-
|
|
96
|
-
const knownTriggers = knownTypes
|
|
97
|
-
? new Set<string>([...BUILTIN_TRIGGER_TYPES, ...(knownTypes.triggers ?? [])])
|
|
98
|
-
: null;
|
|
99
|
-
const knownCompletions = knownTypes
|
|
100
|
-
? new Set<string>([...BUILTIN_COMPLETION_TYPES, ...(knownTypes.completions ?? [])])
|
|
101
|
-
: null;
|
|
102
|
-
const knownMiddlewares = knownTypes
|
|
103
|
-
? new Set<string>([...BUILTIN_MIDDLEWARE_TYPES, ...(knownTypes.middlewares ?? [])])
|
|
104
|
-
: null;
|
|
105
|
-
|
|
106
|
-
// ── Top level ──
|
|
107
|
-
if (!config.name?.trim()) {
|
|
108
|
-
errors.push({ path: 'name', message: 'Pipeline name is required' });
|
|
109
|
-
}
|
|
110
|
-
if (config.reasoning_effort && !VALID_REASONING_EFFORT.has(config.reasoning_effort)) {
|
|
111
|
-
errors.push({
|
|
112
|
-
path: 'reasoning_effort',
|
|
113
|
-
message: `Invalid reasoning_effort "${config.reasoning_effort}". Expected "low", "medium", or "high".`,
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
if (!config.tracks || config.tracks.length === 0) {
|
|
118
|
-
errors.push({ path: 'tracks', message: 'At least one track is required' });
|
|
119
|
-
return errors; // No point going further without tracks
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// ── Build qualified ID sets for cross-reference checks ──
|
|
123
|
-
// Qualified ID format: "trackId.taskId" (mirrors the engine's convention).
|
|
124
|
-
// Shared with dag.ts so "ambiguous" / "not found" stay consistent — refs
|
|
125
|
-
// that buildDag later throws on will be reported here as errors first.
|
|
126
|
-
const index = buildTaskIndex(config);
|
|
127
|
-
|
|
128
|
-
// ── Per-track validation ──
|
|
129
|
-
const seenTrackIds = new Set<string>();
|
|
130
|
-
for (let ti = 0; ti < config.tracks.length; ti++) {
|
|
131
|
-
const track = config.tracks[ti];
|
|
132
|
-
const trackPath = `tracks[${ti}]`;
|
|
133
|
-
|
|
134
|
-
if (!track.id?.trim()) {
|
|
135
|
-
errors.push({ path: `${trackPath}.id`, message: 'Track id is required' });
|
|
136
|
-
} else if (!isValidId(track.id)) {
|
|
137
|
-
errors.push({
|
|
138
|
-
path: `${trackPath}.id`,
|
|
139
|
-
message: `Track id "${track.id}" contains invalid characters. IDs must match /^[A-Za-z_][A-Za-z0-9_-]*$/ (no dots, spaces, or special chars).`,
|
|
140
|
-
});
|
|
141
|
-
} else if (seenTrackIds.has(track.id)) {
|
|
142
|
-
errors.push({ path: `${trackPath}.id`, message: `Duplicate track id "${track.id}"` });
|
|
143
|
-
} else {
|
|
144
|
-
seenTrackIds.add(track.id);
|
|
145
|
-
}
|
|
146
|
-
if (!track.name?.trim()) {
|
|
147
|
-
errors.push({ path: `${trackPath}.name`, message: 'Track name is required' });
|
|
148
|
-
}
|
|
149
|
-
if (track.on_failure && !VALID_ON_FAILURE.has(track.on_failure)) {
|
|
150
|
-
errors.push({
|
|
151
|
-
path: `${trackPath}.on_failure`,
|
|
152
|
-
message: `Invalid on_failure value "${track.on_failure}". Expected "skip_downstream", "stop_all", or "ignore".`,
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
if (track.reasoning_effort && !VALID_REASONING_EFFORT.has(track.reasoning_effort)) {
|
|
156
|
-
errors.push({
|
|
157
|
-
path: `${trackPath}.reasoning_effort`,
|
|
158
|
-
message: `Invalid reasoning_effort "${track.reasoning_effort}". Expected "low", "medium", or "high".`,
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// Track-level middlewares can reference a plugin that was uninstalled
|
|
163
|
-
// after the YAML was written — surface a warning so the user notices
|
|
164
|
-
// before hitting Run.
|
|
165
|
-
if (knownMiddlewares && track.middlewares) {
|
|
166
|
-
for (let mi = 0; mi < track.middlewares.length; mi++) {
|
|
167
|
-
const mw = track.middlewares[mi];
|
|
168
|
-
if (mw?.type && !knownMiddlewares.has(mw.type)) {
|
|
169
|
-
errors.push({
|
|
170
|
-
path: `${trackPath}.middlewares[${mi}].type`,
|
|
171
|
-
message: `Middleware type "${mw.type}" is not registered. Install the plugin (e.g. @tagma/middleware-${mw.type}) or remove the reference — the pipeline will fail at run time.`,
|
|
172
|
-
severity: 'warning',
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
if (!track.tasks || track.tasks.length === 0) {
|
|
179
|
-
errors.push({
|
|
180
|
-
path: `${trackPath}.tasks`,
|
|
181
|
-
message: `Track "${track.id || ti}": must have at least one task`,
|
|
182
|
-
});
|
|
183
|
-
continue;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// ── Per-task validation ──
|
|
187
|
-
const seenTaskIds = new Set<string>();
|
|
188
|
-
for (let ki = 0; ki < track.tasks.length; ki++) {
|
|
189
|
-
const task = track.tasks[ki];
|
|
190
|
-
const taskPath = `${trackPath}.tasks[${ki}]`;
|
|
191
|
-
|
|
192
|
-
if (!task.id?.trim()) {
|
|
193
|
-
errors.push({ path: `${taskPath}.id`, message: 'Task id is required' });
|
|
194
|
-
continue; // Can't check further without an id
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
if (!isValidId(task.id)) {
|
|
198
|
-
errors.push({
|
|
199
|
-
path: `${taskPath}.id`,
|
|
200
|
-
message: `Task id "${task.id}" contains invalid characters. IDs must match /^[A-Za-z_][A-Za-z0-9_-]*$/ (no dots, spaces, or special chars).`,
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
if (seenTaskIds.has(task.id)) {
|
|
204
|
-
errors.push({
|
|
205
|
-
path: taskPath,
|
|
206
|
-
message: `Duplicate task id "${task.id}" in track "${track.id}"`,
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
seenTaskIds.add(task.id);
|
|
210
|
-
|
|
211
|
-
const hasPromptKey = typeof task.prompt === 'string';
|
|
212
|
-
const hasCommandKey = typeof task.command === 'string';
|
|
213
|
-
const promptEmpty = hasPromptKey && task.prompt!.trim().length === 0;
|
|
214
|
-
const commandEmpty = hasCommandKey && task.command!.trim().length === 0;
|
|
215
|
-
|
|
216
|
-
if (hasPromptKey && hasCommandKey) {
|
|
217
|
-
errors.push({
|
|
218
|
-
path: taskPath,
|
|
219
|
-
message: `Task "${task.id}": cannot have both "prompt" and "command"`,
|
|
220
|
-
});
|
|
221
|
-
} else if (!hasPromptKey && !hasCommandKey) {
|
|
222
|
-
errors.push({
|
|
223
|
-
path: taskPath,
|
|
224
|
-
message: `Task "${task.id}": must have "prompt" or "command"`,
|
|
225
|
-
});
|
|
226
|
-
} else if (promptEmpty) {
|
|
227
|
-
errors.push({
|
|
228
|
-
path: taskPath,
|
|
229
|
-
message: `Task "${task.id}": prompt content cannot be empty`,
|
|
230
|
-
});
|
|
231
|
-
} else if (commandEmpty) {
|
|
232
|
-
errors.push({
|
|
233
|
-
path: taskPath,
|
|
234
|
-
message: `Task "${task.id}": command content cannot be empty`,
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
// ── Field-level validations ──
|
|
239
|
-
if (task.timeout && !isValidDuration(task.timeout)) {
|
|
240
|
-
errors.push({
|
|
241
|
-
path: `${taskPath}.timeout`,
|
|
242
|
-
message: `Invalid duration format "${task.timeout}". Expected e.g. "30s", "5m", "1h".`,
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
if (task.reasoning_effort && !VALID_REASONING_EFFORT.has(task.reasoning_effort)) {
|
|
246
|
-
errors.push({
|
|
247
|
-
path: `${taskPath}.reasoning_effort`,
|
|
248
|
-
message: `Invalid reasoning_effort "${task.reasoning_effort}". Expected "low", "medium", or "high".`,
|
|
249
|
-
});
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// ── Plugin type warnings (trigger / completion / middlewares) ──
|
|
253
|
-
// Only fire when the host supplied a `knownTypes` snapshot, so offline
|
|
254
|
-
// validation stays quiet. The messages deliberately name the npm
|
|
255
|
-
// scope so users can copy-paste the install command.
|
|
256
|
-
if (knownTriggers && task.trigger?.type && !knownTriggers.has(task.trigger.type)) {
|
|
257
|
-
errors.push({
|
|
258
|
-
path: `${taskPath}.trigger.type`,
|
|
259
|
-
message: `Trigger type "${task.trigger.type}" is not registered. Install the plugin (e.g. @tagma/trigger-${task.trigger.type}) or the task will fail at run time.`,
|
|
260
|
-
severity: 'warning',
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
if (
|
|
264
|
-
knownCompletions &&
|
|
265
|
-
task.completion?.type &&
|
|
266
|
-
!knownCompletions.has(task.completion.type)
|
|
267
|
-
) {
|
|
268
|
-
errors.push({
|
|
269
|
-
path: `${taskPath}.completion.type`,
|
|
270
|
-
message: `Completion type "${task.completion.type}" is not registered. Install the plugin (e.g. @tagma/completion-${task.completion.type}) or the task will fail at run time.`,
|
|
271
|
-
severity: 'warning',
|
|
272
|
-
});
|
|
273
|
-
}
|
|
274
|
-
if (knownMiddlewares && task.middlewares) {
|
|
275
|
-
for (let mi = 0; mi < task.middlewares.length; mi++) {
|
|
276
|
-
const mw = task.middlewares[mi];
|
|
277
|
-
if (mw?.type && !knownMiddlewares.has(mw.type)) {
|
|
278
|
-
errors.push({
|
|
279
|
-
path: `${taskPath}.middlewares[${mi}].type`,
|
|
280
|
-
message: `Middleware type "${mw.type}" is not registered. Install the plugin (e.g. @tagma/middleware-${mw.type}) or remove the reference — the pipeline will fail at run time.`,
|
|
281
|
-
severity: 'warning',
|
|
282
|
-
});
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
// ── depends_on reference checks ──
|
|
288
|
-
if (task.depends_on && task.depends_on.length > 0) {
|
|
289
|
-
for (const dep of task.depends_on) {
|
|
290
|
-
const resolved = resolveTaskRef(dep, track.id, index);
|
|
291
|
-
if (resolved.kind === 'not_found') {
|
|
292
|
-
errors.push({
|
|
293
|
-
path: `${taskPath}.depends_on`,
|
|
294
|
-
message: `Task "${task.id}": depends_on "${dep}" — no such task found`,
|
|
295
|
-
});
|
|
296
|
-
} else if (resolved.kind === 'ambiguous') {
|
|
297
|
-
errors.push({
|
|
298
|
-
path: `${taskPath}.depends_on`,
|
|
299
|
-
message: `Task "${task.id}": depends_on "${dep}" is ambiguous — multiple tracks have a task with this id. Use the fully-qualified form "trackId.${dep}".`,
|
|
300
|
-
});
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
// ── continue_from reference check ──
|
|
306
|
-
if (task.continue_from) {
|
|
307
|
-
const resolved = resolveTaskRef(task.continue_from, track.id, index);
|
|
308
|
-
if (resolved.kind === 'not_found') {
|
|
309
|
-
errors.push({
|
|
310
|
-
path: `${taskPath}.continue_from`,
|
|
311
|
-
message: `Task "${task.id}": continue_from "${task.continue_from}" — no such task found`,
|
|
312
|
-
});
|
|
313
|
-
} else if (resolved.kind === 'ambiguous') {
|
|
314
|
-
errors.push({
|
|
315
|
-
path: `${taskPath}.continue_from`,
|
|
316
|
-
message: `Task "${task.id}": continue_from "${task.continue_from}" is ambiguous — multiple tracks have a task with this id. Use the fully-qualified form "trackId.${task.continue_from}".`,
|
|
317
|
-
});
|
|
318
|
-
} else if (
|
|
319
|
-
!task.depends_on ||
|
|
320
|
-
!task.depends_on.some((dep) => {
|
|
321
|
-
const depResolved = resolveTaskRef(dep, track.id, index);
|
|
322
|
-
return depResolved.kind === 'resolved' && depResolved.qid === resolved.qid;
|
|
323
|
-
})
|
|
324
|
-
) {
|
|
325
|
-
// H8: demote to a warning. dag.ts/buildDag inserts continue_from
|
|
326
|
-
// as an implicit dependency at runtime, so the pipeline runs fine
|
|
327
|
-
// without the explicit listing. Treat as a style hint rather than
|
|
328
|
-
// blocking save / run, otherwise we frighten users with a red
|
|
329
|
-
// "Configuration error" for code that would have run successfully.
|
|
330
|
-
errors.push({
|
|
331
|
-
path: `${taskPath}.continue_from`,
|
|
332
|
-
message: `Task "${task.id}": continue_from "${task.continue_from}" should also be listed in depends_on for clarity (the runtime will add it implicitly).`,
|
|
333
|
-
severity: 'warning',
|
|
334
|
-
});
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
// ── Cycle detection ──
|
|
341
|
-
errors.push(...detectCycles(config, index));
|
|
342
|
-
|
|
343
|
-
return errors;
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
function detectCycles(config: RawPipelineConfig, index: TaskIndex): ValidationError[] {
|
|
347
|
-
// Build adjacency: qualifiedId → [resolved dep qualifiedIds]
|
|
348
|
-
const adj = new Map<string, string[]>();
|
|
349
|
-
|
|
350
|
-
for (const track of config.tracks) {
|
|
351
|
-
if (!track.id) continue;
|
|
352
|
-
for (const task of track.tasks ?? []) {
|
|
353
|
-
if (!task.id) continue;
|
|
354
|
-
const qid = qualifyTaskId(track.id, task.id);
|
|
355
|
-
const deps: string[] = [];
|
|
356
|
-
for (const dep of task.depends_on ?? []) {
|
|
357
|
-
const resolved = resolveTaskRef(dep, track.id, index);
|
|
358
|
-
if (resolved.kind === 'resolved') deps.push(resolved.qid);
|
|
359
|
-
}
|
|
360
|
-
if (task.continue_from) {
|
|
361
|
-
const resolved = resolveTaskRef(task.continue_from, track.id, index);
|
|
362
|
-
if (resolved.kind === 'resolved' && !deps.includes(resolved.qid)) deps.push(resolved.qid);
|
|
363
|
-
}
|
|
364
|
-
adj.set(qid, deps);
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
const errors: ValidationError[] = [];
|
|
369
|
-
const visited = new Set<string>();
|
|
370
|
-
const inStack = new Set<string>();
|
|
371
|
-
// Deduplicate cycles: the same cycle can be discovered from multiple entry points.
|
|
372
|
-
// Canonical key = sorted node list joined — order-independent fingerprint.
|
|
373
|
-
const seenCycles = new Set<string>();
|
|
374
|
-
|
|
375
|
-
// Use a mutable path array instead of copying at each level (O(n) vs O(n^2)).
|
|
376
|
-
const pathStack: string[] = [];
|
|
377
|
-
|
|
378
|
-
function dfs(id: string): void {
|
|
379
|
-
if (inStack.has(id)) {
|
|
380
|
-
const cycleStart = pathStack.indexOf(id);
|
|
381
|
-
// Unique nodes in the cycle (without repeating the start node) for dedup.
|
|
382
|
-
// Previously the duplicate start node caused different sorted keys when
|
|
383
|
-
// the same cycle was discovered from different entry points.
|
|
384
|
-
const uniqueNodes = pathStack.slice(cycleStart);
|
|
385
|
-
const key = [...uniqueNodes].sort().join(',');
|
|
386
|
-
if (!seenCycles.has(key)) {
|
|
387
|
-
seenCycles.add(key);
|
|
388
|
-
const display = [...uniqueNodes, id]; // include start for readable display
|
|
389
|
-
errors.push({
|
|
390
|
-
path: 'tracks',
|
|
391
|
-
message: `Circular dependency detected: ${display.join(' → ')}`,
|
|
392
|
-
});
|
|
393
|
-
}
|
|
394
|
-
return;
|
|
395
|
-
}
|
|
396
|
-
if (visited.has(id)) return;
|
|
397
|
-
visited.add(id);
|
|
398
|
-
inStack.add(id);
|
|
399
|
-
pathStack.push(id);
|
|
400
|
-
for (const dep of adj.get(id) ?? []) {
|
|
401
|
-
dfs(dep);
|
|
402
|
-
}
|
|
403
|
-
pathStack.pop();
|
|
404
|
-
inStack.delete(id);
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
for (const id of adj.keys()) {
|
|
408
|
-
if (!visited.has(id)) dfs(id);
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
return errors;
|
|
412
|
-
}
|
|
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
|
+
import {
|
|
11
|
+
isValidTaskId,
|
|
12
|
+
qualifyTaskId,
|
|
13
|
+
buildTaskIndex,
|
|
14
|
+
resolveTaskRef,
|
|
15
|
+
type TaskIndex,
|
|
16
|
+
} from './task-ref';
|
|
17
|
+
|
|
18
|
+
const DURATION_RE = /^(\d*\.?\d+)\s*(s|m|h|d)$/;
|
|
19
|
+
function isValidDuration(input: string): boolean {
|
|
20
|
+
return DURATION_RE.test(input.trim());
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// D8: IDs may only contain letters, digits, underscores, and hyphens, and must
|
|
24
|
+
// start with a letter or underscore. Dots are explicitly forbidden because the
|
|
25
|
+
// engine uses "trackId.taskId" as the qualified separator — a dot in either
|
|
26
|
+
// part creates an ambiguous qualified ID and breaks resolveRef.
|
|
27
|
+
// Canonical regex and helper live in ./task-ref so every resolver (dag.ts,
|
|
28
|
+
// engine.ts, editor) stays in lockstep with what we accept here.
|
|
29
|
+
const isValidId = isValidTaskId;
|
|
30
|
+
|
|
31
|
+
const VALID_ON_FAILURE = new Set(['skip_downstream', 'stop_all', 'ignore']);
|
|
32
|
+
const VALID_REASONING_EFFORT = new Set(['low', 'medium', 'high']);
|
|
33
|
+
|
|
34
|
+
// Built-in plugin types always known to the SDK core, regardless of which
|
|
35
|
+
// external plugin packages are installed. These MUST stay in sync with the
|
|
36
|
+
// types that `bootstrapBuiltins()` registers, otherwise the editor will
|
|
37
|
+
// emit false-positive "unknown type" warnings for stock pipelines.
|
|
38
|
+
const BUILTIN_TRIGGER_TYPES: ReadonlySet<string> = new Set(['manual', 'file']);
|
|
39
|
+
const BUILTIN_COMPLETION_TYPES: ReadonlySet<string> = new Set([
|
|
40
|
+
'exit_code',
|
|
41
|
+
'file_exists',
|
|
42
|
+
'output_check',
|
|
43
|
+
]);
|
|
44
|
+
const BUILTIN_MIDDLEWARE_TYPES: ReadonlySet<string> = new Set(['static_context']);
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Optional second argument to `validateRaw`: the set of plugin types currently
|
|
48
|
+
* registered in the SDK runtime, keyed by category. Hosts (e.g. the editor
|
|
49
|
+
* server) pass this so `validateRaw` can emit a soft warning when a task
|
|
50
|
+
* references a type that isn't loaded — otherwise the Task panel would show
|
|
51
|
+
* no hint and the pipeline would only blow up at run time. Callers that
|
|
52
|
+
* legitimately validate a config offline (before plugins are loaded) can omit
|
|
53
|
+
* this argument and no plugin warnings will be produced.
|
|
54
|
+
*/
|
|
55
|
+
export interface KnownPluginTypes {
|
|
56
|
+
readonly triggers?: readonly string[];
|
|
57
|
+
readonly completions?: readonly string[];
|
|
58
|
+
readonly middlewares?: readonly string[];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export type ValidationSeverity = 'error' | 'warning';
|
|
62
|
+
|
|
63
|
+
export interface ValidationError {
|
|
64
|
+
/** JSONPath-style location, e.g. "tracks[0].tasks[1].prompt" */
|
|
65
|
+
path: string;
|
|
66
|
+
message: string;
|
|
67
|
+
/**
|
|
68
|
+
* H8: not all "errors" are equally fatal. The DAG runtime is happy to
|
|
69
|
+
* insert implicit `continue_from → depends_on` ordering, so the matching
|
|
70
|
+
* validate-raw check is a *style* nit, not a hard failure. Severity lets
|
|
71
|
+
* the editor render it as a soft warning instead of blocking save / run.
|
|
72
|
+
* Existing call sites that don't read this field still treat every entry
|
|
73
|
+
* as fatal — defaulting `severity` to undefined preserves that behaviour.
|
|
74
|
+
*/
|
|
75
|
+
severity?: ValidationSeverity;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Validate a raw pipeline config.
|
|
80
|
+
* Checks structure, required fields, prompt/command exclusivity,
|
|
81
|
+
* depends_on reference integrity, and circular dependencies.
|
|
82
|
+
*
|
|
83
|
+
* Plugin type checks: when `knownTypes` is provided, task/track references to
|
|
84
|
+
* trigger/completion/middleware types that are neither built-in nor in the
|
|
85
|
+
* supplied set produce a soft warning (severity: 'warning') — these don't
|
|
86
|
+
* block save/run but light up the Task panel so users discover the broken
|
|
87
|
+
* reference in the editor instead of at run time. Omit `knownTypes` to skip
|
|
88
|
+
* plugin checks entirely (offline/pre-load validation).
|
|
89
|
+
*/
|
|
90
|
+
export function validateRaw(
|
|
91
|
+
config: RawPipelineConfig,
|
|
92
|
+
knownTypes?: KnownPluginTypes,
|
|
93
|
+
): ValidationError[] {
|
|
94
|
+
const errors: ValidationError[] = [];
|
|
95
|
+
|
|
96
|
+
const knownTriggers = knownTypes
|
|
97
|
+
? new Set<string>([...BUILTIN_TRIGGER_TYPES, ...(knownTypes.triggers ?? [])])
|
|
98
|
+
: null;
|
|
99
|
+
const knownCompletions = knownTypes
|
|
100
|
+
? new Set<string>([...BUILTIN_COMPLETION_TYPES, ...(knownTypes.completions ?? [])])
|
|
101
|
+
: null;
|
|
102
|
+
const knownMiddlewares = knownTypes
|
|
103
|
+
? new Set<string>([...BUILTIN_MIDDLEWARE_TYPES, ...(knownTypes.middlewares ?? [])])
|
|
104
|
+
: null;
|
|
105
|
+
|
|
106
|
+
// ── Top level ──
|
|
107
|
+
if (!config.name?.trim()) {
|
|
108
|
+
errors.push({ path: 'name', message: 'Pipeline name is required' });
|
|
109
|
+
}
|
|
110
|
+
if (config.reasoning_effort && !VALID_REASONING_EFFORT.has(config.reasoning_effort)) {
|
|
111
|
+
errors.push({
|
|
112
|
+
path: 'reasoning_effort',
|
|
113
|
+
message: `Invalid reasoning_effort "${config.reasoning_effort}". Expected "low", "medium", or "high".`,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (!config.tracks || config.tracks.length === 0) {
|
|
118
|
+
errors.push({ path: 'tracks', message: 'At least one track is required' });
|
|
119
|
+
return errors; // No point going further without tracks
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// ── Build qualified ID sets for cross-reference checks ──
|
|
123
|
+
// Qualified ID format: "trackId.taskId" (mirrors the engine's convention).
|
|
124
|
+
// Shared with dag.ts so "ambiguous" / "not found" stay consistent — refs
|
|
125
|
+
// that buildDag later throws on will be reported here as errors first.
|
|
126
|
+
const index = buildTaskIndex(config);
|
|
127
|
+
|
|
128
|
+
// ── Per-track validation ──
|
|
129
|
+
const seenTrackIds = new Set<string>();
|
|
130
|
+
for (let ti = 0; ti < config.tracks.length; ti++) {
|
|
131
|
+
const track = config.tracks[ti];
|
|
132
|
+
const trackPath = `tracks[${ti}]`;
|
|
133
|
+
|
|
134
|
+
if (!track.id?.trim()) {
|
|
135
|
+
errors.push({ path: `${trackPath}.id`, message: 'Track id is required' });
|
|
136
|
+
} else if (!isValidId(track.id)) {
|
|
137
|
+
errors.push({
|
|
138
|
+
path: `${trackPath}.id`,
|
|
139
|
+
message: `Track id "${track.id}" contains invalid characters. IDs must match /^[A-Za-z_][A-Za-z0-9_-]*$/ (no dots, spaces, or special chars).`,
|
|
140
|
+
});
|
|
141
|
+
} else if (seenTrackIds.has(track.id)) {
|
|
142
|
+
errors.push({ path: `${trackPath}.id`, message: `Duplicate track id "${track.id}"` });
|
|
143
|
+
} else {
|
|
144
|
+
seenTrackIds.add(track.id);
|
|
145
|
+
}
|
|
146
|
+
if (!track.name?.trim()) {
|
|
147
|
+
errors.push({ path: `${trackPath}.name`, message: 'Track name is required' });
|
|
148
|
+
}
|
|
149
|
+
if (track.on_failure && !VALID_ON_FAILURE.has(track.on_failure)) {
|
|
150
|
+
errors.push({
|
|
151
|
+
path: `${trackPath}.on_failure`,
|
|
152
|
+
message: `Invalid on_failure value "${track.on_failure}". Expected "skip_downstream", "stop_all", or "ignore".`,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
if (track.reasoning_effort && !VALID_REASONING_EFFORT.has(track.reasoning_effort)) {
|
|
156
|
+
errors.push({
|
|
157
|
+
path: `${trackPath}.reasoning_effort`,
|
|
158
|
+
message: `Invalid reasoning_effort "${track.reasoning_effort}". Expected "low", "medium", or "high".`,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Track-level middlewares can reference a plugin that was uninstalled
|
|
163
|
+
// after the YAML was written — surface a warning so the user notices
|
|
164
|
+
// before hitting Run.
|
|
165
|
+
if (knownMiddlewares && track.middlewares) {
|
|
166
|
+
for (let mi = 0; mi < track.middlewares.length; mi++) {
|
|
167
|
+
const mw = track.middlewares[mi];
|
|
168
|
+
if (mw?.type && !knownMiddlewares.has(mw.type)) {
|
|
169
|
+
errors.push({
|
|
170
|
+
path: `${trackPath}.middlewares[${mi}].type`,
|
|
171
|
+
message: `Middleware type "${mw.type}" is not registered. Install the plugin (e.g. @tagma/middleware-${mw.type}) or remove the reference — the pipeline will fail at run time.`,
|
|
172
|
+
severity: 'warning',
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (!track.tasks || track.tasks.length === 0) {
|
|
179
|
+
errors.push({
|
|
180
|
+
path: `${trackPath}.tasks`,
|
|
181
|
+
message: `Track "${track.id || ti}": must have at least one task`,
|
|
182
|
+
});
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// ── Per-task validation ──
|
|
187
|
+
const seenTaskIds = new Set<string>();
|
|
188
|
+
for (let ki = 0; ki < track.tasks.length; ki++) {
|
|
189
|
+
const task = track.tasks[ki];
|
|
190
|
+
const taskPath = `${trackPath}.tasks[${ki}]`;
|
|
191
|
+
|
|
192
|
+
if (!task.id?.trim()) {
|
|
193
|
+
errors.push({ path: `${taskPath}.id`, message: 'Task id is required' });
|
|
194
|
+
continue; // Can't check further without an id
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (!isValidId(task.id)) {
|
|
198
|
+
errors.push({
|
|
199
|
+
path: `${taskPath}.id`,
|
|
200
|
+
message: `Task id "${task.id}" contains invalid characters. IDs must match /^[A-Za-z_][A-Za-z0-9_-]*$/ (no dots, spaces, or special chars).`,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
if (seenTaskIds.has(task.id)) {
|
|
204
|
+
errors.push({
|
|
205
|
+
path: taskPath,
|
|
206
|
+
message: `Duplicate task id "${task.id}" in track "${track.id}"`,
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
seenTaskIds.add(task.id);
|
|
210
|
+
|
|
211
|
+
const hasPromptKey = typeof task.prompt === 'string';
|
|
212
|
+
const hasCommandKey = typeof task.command === 'string';
|
|
213
|
+
const promptEmpty = hasPromptKey && task.prompt!.trim().length === 0;
|
|
214
|
+
const commandEmpty = hasCommandKey && task.command!.trim().length === 0;
|
|
215
|
+
|
|
216
|
+
if (hasPromptKey && hasCommandKey) {
|
|
217
|
+
errors.push({
|
|
218
|
+
path: taskPath,
|
|
219
|
+
message: `Task "${task.id}": cannot have both "prompt" and "command"`,
|
|
220
|
+
});
|
|
221
|
+
} else if (!hasPromptKey && !hasCommandKey) {
|
|
222
|
+
errors.push({
|
|
223
|
+
path: taskPath,
|
|
224
|
+
message: `Task "${task.id}": must have "prompt" or "command"`,
|
|
225
|
+
});
|
|
226
|
+
} else if (promptEmpty) {
|
|
227
|
+
errors.push({
|
|
228
|
+
path: taskPath,
|
|
229
|
+
message: `Task "${task.id}": prompt content cannot be empty`,
|
|
230
|
+
});
|
|
231
|
+
} else if (commandEmpty) {
|
|
232
|
+
errors.push({
|
|
233
|
+
path: taskPath,
|
|
234
|
+
message: `Task "${task.id}": command content cannot be empty`,
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// ── Field-level validations ──
|
|
239
|
+
if (task.timeout && !isValidDuration(task.timeout)) {
|
|
240
|
+
errors.push({
|
|
241
|
+
path: `${taskPath}.timeout`,
|
|
242
|
+
message: `Invalid duration format "${task.timeout}". Expected e.g. "30s", "5m", "1h".`,
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
if (task.reasoning_effort && !VALID_REASONING_EFFORT.has(task.reasoning_effort)) {
|
|
246
|
+
errors.push({
|
|
247
|
+
path: `${taskPath}.reasoning_effort`,
|
|
248
|
+
message: `Invalid reasoning_effort "${task.reasoning_effort}". Expected "low", "medium", or "high".`,
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// ── Plugin type warnings (trigger / completion / middlewares) ──
|
|
253
|
+
// Only fire when the host supplied a `knownTypes` snapshot, so offline
|
|
254
|
+
// validation stays quiet. The messages deliberately name the npm
|
|
255
|
+
// scope so users can copy-paste the install command.
|
|
256
|
+
if (knownTriggers && task.trigger?.type && !knownTriggers.has(task.trigger.type)) {
|
|
257
|
+
errors.push({
|
|
258
|
+
path: `${taskPath}.trigger.type`,
|
|
259
|
+
message: `Trigger type "${task.trigger.type}" is not registered. Install the plugin (e.g. @tagma/trigger-${task.trigger.type}) or the task will fail at run time.`,
|
|
260
|
+
severity: 'warning',
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
if (
|
|
264
|
+
knownCompletions &&
|
|
265
|
+
task.completion?.type &&
|
|
266
|
+
!knownCompletions.has(task.completion.type)
|
|
267
|
+
) {
|
|
268
|
+
errors.push({
|
|
269
|
+
path: `${taskPath}.completion.type`,
|
|
270
|
+
message: `Completion type "${task.completion.type}" is not registered. Install the plugin (e.g. @tagma/completion-${task.completion.type}) or the task will fail at run time.`,
|
|
271
|
+
severity: 'warning',
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
if (knownMiddlewares && task.middlewares) {
|
|
275
|
+
for (let mi = 0; mi < task.middlewares.length; mi++) {
|
|
276
|
+
const mw = task.middlewares[mi];
|
|
277
|
+
if (mw?.type && !knownMiddlewares.has(mw.type)) {
|
|
278
|
+
errors.push({
|
|
279
|
+
path: `${taskPath}.middlewares[${mi}].type`,
|
|
280
|
+
message: `Middleware type "${mw.type}" is not registered. Install the plugin (e.g. @tagma/middleware-${mw.type}) or remove the reference — the pipeline will fail at run time.`,
|
|
281
|
+
severity: 'warning',
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// ── depends_on reference checks ──
|
|
288
|
+
if (task.depends_on && task.depends_on.length > 0) {
|
|
289
|
+
for (const dep of task.depends_on) {
|
|
290
|
+
const resolved = resolveTaskRef(dep, track.id, index);
|
|
291
|
+
if (resolved.kind === 'not_found') {
|
|
292
|
+
errors.push({
|
|
293
|
+
path: `${taskPath}.depends_on`,
|
|
294
|
+
message: `Task "${task.id}": depends_on "${dep}" — no such task found`,
|
|
295
|
+
});
|
|
296
|
+
} else if (resolved.kind === 'ambiguous') {
|
|
297
|
+
errors.push({
|
|
298
|
+
path: `${taskPath}.depends_on`,
|
|
299
|
+
message: `Task "${task.id}": depends_on "${dep}" is ambiguous — multiple tracks have a task with this id. Use the fully-qualified form "trackId.${dep}".`,
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// ── continue_from reference check ──
|
|
306
|
+
if (task.continue_from) {
|
|
307
|
+
const resolved = resolveTaskRef(task.continue_from, track.id, index);
|
|
308
|
+
if (resolved.kind === 'not_found') {
|
|
309
|
+
errors.push({
|
|
310
|
+
path: `${taskPath}.continue_from`,
|
|
311
|
+
message: `Task "${task.id}": continue_from "${task.continue_from}" — no such task found`,
|
|
312
|
+
});
|
|
313
|
+
} else if (resolved.kind === 'ambiguous') {
|
|
314
|
+
errors.push({
|
|
315
|
+
path: `${taskPath}.continue_from`,
|
|
316
|
+
message: `Task "${task.id}": continue_from "${task.continue_from}" is ambiguous — multiple tracks have a task with this id. Use the fully-qualified form "trackId.${task.continue_from}".`,
|
|
317
|
+
});
|
|
318
|
+
} else if (
|
|
319
|
+
!task.depends_on ||
|
|
320
|
+
!task.depends_on.some((dep) => {
|
|
321
|
+
const depResolved = resolveTaskRef(dep, track.id, index);
|
|
322
|
+
return depResolved.kind === 'resolved' && depResolved.qid === resolved.qid;
|
|
323
|
+
})
|
|
324
|
+
) {
|
|
325
|
+
// H8: demote to a warning. dag.ts/buildDag inserts continue_from
|
|
326
|
+
// as an implicit dependency at runtime, so the pipeline runs fine
|
|
327
|
+
// without the explicit listing. Treat as a style hint rather than
|
|
328
|
+
// blocking save / run, otherwise we frighten users with a red
|
|
329
|
+
// "Configuration error" for code that would have run successfully.
|
|
330
|
+
errors.push({
|
|
331
|
+
path: `${taskPath}.continue_from`,
|
|
332
|
+
message: `Task "${task.id}": continue_from "${task.continue_from}" should also be listed in depends_on for clarity (the runtime will add it implicitly).`,
|
|
333
|
+
severity: 'warning',
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// ── Cycle detection ──
|
|
341
|
+
errors.push(...detectCycles(config, index));
|
|
342
|
+
|
|
343
|
+
return errors;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function detectCycles(config: RawPipelineConfig, index: TaskIndex): ValidationError[] {
|
|
347
|
+
// Build adjacency: qualifiedId → [resolved dep qualifiedIds]
|
|
348
|
+
const adj = new Map<string, string[]>();
|
|
349
|
+
|
|
350
|
+
for (const track of config.tracks) {
|
|
351
|
+
if (!track.id) continue;
|
|
352
|
+
for (const task of track.tasks ?? []) {
|
|
353
|
+
if (!task.id) continue;
|
|
354
|
+
const qid = qualifyTaskId(track.id, task.id);
|
|
355
|
+
const deps: string[] = [];
|
|
356
|
+
for (const dep of task.depends_on ?? []) {
|
|
357
|
+
const resolved = resolveTaskRef(dep, track.id, index);
|
|
358
|
+
if (resolved.kind === 'resolved') deps.push(resolved.qid);
|
|
359
|
+
}
|
|
360
|
+
if (task.continue_from) {
|
|
361
|
+
const resolved = resolveTaskRef(task.continue_from, track.id, index);
|
|
362
|
+
if (resolved.kind === 'resolved' && !deps.includes(resolved.qid)) deps.push(resolved.qid);
|
|
363
|
+
}
|
|
364
|
+
adj.set(qid, deps);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const errors: ValidationError[] = [];
|
|
369
|
+
const visited = new Set<string>();
|
|
370
|
+
const inStack = new Set<string>();
|
|
371
|
+
// Deduplicate cycles: the same cycle can be discovered from multiple entry points.
|
|
372
|
+
// Canonical key = sorted node list joined — order-independent fingerprint.
|
|
373
|
+
const seenCycles = new Set<string>();
|
|
374
|
+
|
|
375
|
+
// Use a mutable path array instead of copying at each level (O(n) vs O(n^2)).
|
|
376
|
+
const pathStack: string[] = [];
|
|
377
|
+
|
|
378
|
+
function dfs(id: string): void {
|
|
379
|
+
if (inStack.has(id)) {
|
|
380
|
+
const cycleStart = pathStack.indexOf(id);
|
|
381
|
+
// Unique nodes in the cycle (without repeating the start node) for dedup.
|
|
382
|
+
// Previously the duplicate start node caused different sorted keys when
|
|
383
|
+
// the same cycle was discovered from different entry points.
|
|
384
|
+
const uniqueNodes = pathStack.slice(cycleStart);
|
|
385
|
+
const key = [...uniqueNodes].sort().join(',');
|
|
386
|
+
if (!seenCycles.has(key)) {
|
|
387
|
+
seenCycles.add(key);
|
|
388
|
+
const display = [...uniqueNodes, id]; // include start for readable display
|
|
389
|
+
errors.push({
|
|
390
|
+
path: 'tracks',
|
|
391
|
+
message: `Circular dependency detected: ${display.join(' → ')}`,
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
if (visited.has(id)) return;
|
|
397
|
+
visited.add(id);
|
|
398
|
+
inStack.add(id);
|
|
399
|
+
pathStack.push(id);
|
|
400
|
+
for (const dep of adj.get(id) ?? []) {
|
|
401
|
+
dfs(dep);
|
|
402
|
+
}
|
|
403
|
+
pathStack.pop();
|
|
404
|
+
inStack.delete(id);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
for (const id of adj.keys()) {
|
|
408
|
+
if (!visited.has(id)) dfs(id);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
return errors;
|
|
412
|
+
}
|