@lukstei/dsh-yaml-workflow 0.0.0-stage → 0.2.2-rc.5.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/src/rules.ts ADDED
@@ -0,0 +1,647 @@
1
+ /**
2
+ * PURPOSE: Pure workflow validation rules and composite validator.
3
+ * ALLOWED: Pure computation, AST traversal, string analysis, schema enforcement.
4
+ * FORBIDDEN: No I/O, no filesystem, no runtime state, no agent orchestration.
5
+ */
6
+
7
+ import { interpolateTemplate, isFlatChoiceMapping } from "./parser.ts";
8
+ import type { WorkflowDocument, WorkflowStep } from "./types.ts";
9
+
10
+ export interface ValidationError {
11
+ readonly rule: string;
12
+ readonly category: "template" | "output";
13
+ readonly message: string;
14
+ readonly stepIndex?: number;
15
+ }
16
+
17
+ export interface WorkflowValidationContext {
18
+ readonly doc: WorkflowDocument;
19
+ readonly args?: Record<string, string>;
20
+ }
21
+
22
+ export type WorkflowRule = (
23
+ context: WorkflowValidationContext,
24
+ ) => readonly ValidationError[];
25
+
26
+ const OUTPUT_REF_REGEX = /\{\{outputs\.([a-zA-Z0-9_-]+)\}\}/g;
27
+
28
+ function extractDeclaredOutputs(steps: readonly WorkflowStep[]): Map<
29
+ string,
30
+ {
31
+ stepIndex: number;
32
+ kind: WorkflowStep["kind"];
33
+ type: "file" | "string" | "list";
34
+ }
35
+ > {
36
+ const map = new Map<
37
+ string,
38
+ {
39
+ stepIndex: number;
40
+ kind: WorkflowStep["kind"];
41
+ type: "file" | "string" | "list";
42
+ }
43
+ >();
44
+ for (let i = 0; i < steps.length; i++) {
45
+ const step = steps[i];
46
+ if (step.kind === "subagent" && step.output && !map.has(step.output)) {
47
+ const outputType = step.foreach !== undefined ? "list" : "string";
48
+ map.set(step.output, { stepIndex: i, kind: step.kind, type: outputType });
49
+ }
50
+ if ((step.kind === "prompt" || step.kind === "subagent") && step.outputs) {
51
+ for (const [name, def] of Object.entries(step.outputs)) {
52
+ if (!map.has(name)) {
53
+ const outputType =
54
+ step.kind === "subagent" && step.foreach !== undefined
55
+ ? "list"
56
+ : def.type;
57
+ map.set(name, { stepIndex: i, kind: step.kind, type: outputType });
58
+ }
59
+ }
60
+ }
61
+ }
62
+ return map;
63
+ }
64
+
65
+ function extractStepStrings(
66
+ step: WorkflowStep,
67
+ options: { includeForeach?: boolean } = { includeForeach: true },
68
+ ): string[] {
69
+ const strings: string[] = [step.name];
70
+ if (step.if) strings.push(step.if);
71
+
72
+ if (step.kind === "prompt" || step.kind === "subagent") {
73
+ strings.push(step.prompt);
74
+ if (typeof step.gate === "string") strings.push(step.gate);
75
+ if (
76
+ step.kind === "subagent" &&
77
+ step.foreach &&
78
+ options.includeForeach !== false
79
+ ) {
80
+ strings.push(step.foreach);
81
+ }
82
+ if (step.kind === "subagent") {
83
+ if (step.rawSubagent) strings.push(step.rawSubagent);
84
+ if (step.config.provider) strings.push(step.config.provider);
85
+ if (step.config.llmProvider) strings.push(step.config.llmProvider);
86
+ if (step.config.preset) strings.push(step.config.preset);
87
+ if (step.config.model) strings.push(step.config.model);
88
+ if (step.config.thinking) strings.push(step.config.thinking);
89
+ }
90
+ if (step.outputs) {
91
+ for (const def of Object.values(step.outputs)) {
92
+ if (def.type === "file") {
93
+ strings.push(def.path);
94
+ }
95
+ }
96
+ }
97
+ } else {
98
+ strings.push(step.text);
99
+ }
100
+ return strings;
101
+ }
102
+
103
+ /**
104
+ * Purpose: Enforces that subagent steps do not declare direct 'list' outputs and foreach subagents only declare 'string' outputs.
105
+ * Contract:
106
+ * - Inputs: WorkflowValidationContext with doc.
107
+ * - Outputs: readonly array of invalid subagent output type errors.
108
+ */
109
+ export const ruleSubagentOutputTypes: WorkflowRule = (context) => {
110
+ const errors: ValidationError[] = [];
111
+
112
+ for (let i = 0; i < context.doc.steps.length; i++) {
113
+ const step = context.doc.steps[i];
114
+ if (step.kind !== "subagent" || !step.outputs) continue;
115
+
116
+ for (const [name, def] of Object.entries(step.outputs)) {
117
+ if (def.type === "list") {
118
+ errors.push({
119
+ rule: "subagent-output-list-disallowed",
120
+ category: "output",
121
+ message: `Subagent step ${i + 1} ('${step.name}') output '${name}' cannot be of type 'list'. Subagents cannot produce list outputs directly.`,
122
+ stepIndex: i,
123
+ });
124
+ } else if (step.foreach !== undefined && def.type === "file") {
125
+ errors.push({
126
+ rule: "foreach-subagent-output-file-disallowed",
127
+ category: "output",
128
+ message: `Subagent step ${i + 1} ('${step.name}') has foreach and cannot declare file output '${name}'. Foreach subagents only support string outputs.`,
129
+ stepIndex: i,
130
+ });
131
+ }
132
+ }
133
+ }
134
+
135
+ return errors;
136
+ };
137
+
138
+ /**
139
+ * Purpose: Enforces that all subagent steps declare at least one output via 'output' or 'outputs'.
140
+ * Contract:
141
+ * - Inputs: WorkflowValidationContext with doc.
142
+ * - Outputs: readonly array of subagent output missing errors.
143
+ */
144
+ export const ruleSubagentOutputRequired: WorkflowRule = (context) => {
145
+ const errors: ValidationError[] = [];
146
+
147
+ for (let i = 0; i < context.doc.steps.length; i++) {
148
+ const step = context.doc.steps[i];
149
+ if (step.kind === "subagent") {
150
+ const hasOutput =
151
+ Boolean(step.output?.trim()) ||
152
+ (step.outputs !== undefined && Object.keys(step.outputs).length > 0);
153
+ if (!hasOutput) {
154
+ errors.push({
155
+ rule: "subagent-output-required",
156
+ category: "output",
157
+ message: `Subagent step ${i + 1} ('${step.name}') must declare at least one output via 'output' or 'outputs'.`,
158
+ stepIndex: i,
159
+ });
160
+ }
161
+ }
162
+ }
163
+
164
+ return errors;
165
+ };
166
+
167
+ /**
168
+ * Purpose: Enforces that all output names across all steps in the document are globally unique.
169
+ * Contract:
170
+ * - Inputs: WorkflowValidationContext with doc.
171
+ * - Outputs: readonly array of duplicate output name errors.
172
+ */
173
+ export const ruleDuplicateOutputNames: WorkflowRule = (context) => {
174
+ const errors: ValidationError[] = [];
175
+ const seen = new Set<string>();
176
+
177
+ for (let i = 0; i < context.doc.steps.length; i++) {
178
+ const step = context.doc.steps[i];
179
+ if (step.kind === "subagent" && step.output) {
180
+ if (seen.has(step.output)) {
181
+ errors.push({
182
+ rule: "duplicate-output-name",
183
+ category: "output",
184
+ message: `Duplicate output name '${step.output}' declared at step ${i + 1}. Output names must be globally unique.`,
185
+ stepIndex: i,
186
+ });
187
+ } else {
188
+ seen.add(step.output);
189
+ }
190
+ }
191
+ if (step.kind === "prompt" || step.kind === "subagent") {
192
+ if (step.outputs) {
193
+ for (const name of Object.keys(step.outputs)) {
194
+ if (seen.has(name)) {
195
+ errors.push({
196
+ rule: "duplicate-output-name",
197
+ category: "output",
198
+ message: `Duplicate output name '${name}' declared at step ${i + 1}. Output names must be globally unique.`,
199
+ stepIndex: i,
200
+ });
201
+ } else {
202
+ seen.add(name);
203
+ }
204
+ }
205
+ }
206
+ }
207
+ }
208
+
209
+ return errors;
210
+ };
211
+
212
+ /**
213
+ * Purpose: Ensures workflow document name and title do not reference outputs before steps execute.
214
+ * Contract:
215
+ * - Inputs: WorkflowValidationContext with doc.
216
+ * - Outputs: readonly array of workflow name and title output reference errors.
217
+ */
218
+ export const ruleWorkflowNameOutputReferences: WorkflowRule = (context) => {
219
+ const errors: ValidationError[] = [];
220
+ const seenRefs = new Set<string>();
221
+
222
+ const checkOutputRefs = (text: string, field: "name" | "title") => {
223
+ const matches = text.matchAll(OUTPUT_REF_REGEX);
224
+ for (const match of matches) {
225
+ const refName = match[1];
226
+ const key = `${field}:${refName}`;
227
+ if (seenRefs.has(key)) continue;
228
+ seenRefs.add(key);
229
+ errors.push({
230
+ rule: "workflow-name-output",
231
+ category: "output",
232
+ message: `Output '{{outputs.${refName}}}' referenced in workflow ${field} before any step execution.`,
233
+ });
234
+ }
235
+ };
236
+
237
+ if (context.doc.name) {
238
+ checkOutputRefs(context.doc.name, "name");
239
+ }
240
+ if (context.doc.title) {
241
+ checkOutputRefs(context.doc.title, "title");
242
+ }
243
+ return errors;
244
+ };
245
+
246
+ /**
247
+ * Purpose: Validates that step output references point to declared outputs and are not forward references.
248
+ * Contract:
249
+ * - Inputs: WorkflowValidationContext with doc.
250
+ * - Outputs: readonly array of unknown and forward output reference errors.
251
+ */
252
+ export const ruleStepOutputReferences: WorkflowRule = (context) => {
253
+ const errors: ValidationError[] = [];
254
+ const declaredMap = extractDeclaredOutputs(context.doc.steps);
255
+
256
+ for (let i = 0; i < context.doc.steps.length; i++) {
257
+ const step = context.doc.steps[i];
258
+ const stringsToCheck = extractStepStrings(step, { includeForeach: false });
259
+ const seenRefs = new Set<string>();
260
+
261
+ for (const str of stringsToCheck) {
262
+ const matches = str.matchAll(OUTPUT_REF_REGEX);
263
+ for (const match of matches) {
264
+ const refName = match[1];
265
+ if (seenRefs.has(refName)) continue;
266
+ seenRefs.add(refName);
267
+
268
+ const declared = declaredMap.get(refName);
269
+ if (declared === undefined) {
270
+ errors.push({
271
+ rule: "unknown-output",
272
+ category: "output",
273
+ message: `Unknown output '{{outputs.${refName}}}' referenced at step ${i + 1}.`,
274
+ stepIndex: i,
275
+ });
276
+ } else if (declared.stepIndex >= i) {
277
+ errors.push({
278
+ rule: "forward-output-reference",
279
+ category: "output",
280
+ message: `Output '{{outputs.${refName}}}' referenced at step ${i + 1} before it is declared at step ${declared.stepIndex + 1}.`,
281
+ stepIndex: i,
282
+ });
283
+ }
284
+ }
285
+ }
286
+ }
287
+
288
+ return errors;
289
+ };
290
+
291
+ /**
292
+ * Purpose: Enforces that subagent steps do not depend on outputs produced by earlier subagent steps.
293
+ * Contract:
294
+ * - Inputs: WorkflowValidationContext with doc.
295
+ * - Outputs: readonly array of subagent dependency errors.
296
+ */
297
+ export const ruleSubagentDependencies: WorkflowRule = (context) => {
298
+ const errors: ValidationError[] = [];
299
+ const declaredMap = extractDeclaredOutputs(context.doc.steps);
300
+
301
+ for (let i = 0; i < context.doc.steps.length; i++) {
302
+ const step = context.doc.steps[i];
303
+ if (step.kind !== "subagent") continue;
304
+
305
+ const stringsToCheck = extractStepStrings(step);
306
+ const seenRefs = new Set<string>();
307
+
308
+ for (const str of stringsToCheck) {
309
+ const matches = str.matchAll(OUTPUT_REF_REGEX);
310
+ for (const match of matches) {
311
+ const refName = match[1];
312
+ if (seenRefs.has(refName)) continue;
313
+ seenRefs.add(refName);
314
+
315
+ const declared = declaredMap.get(refName);
316
+ if (
317
+ declared &&
318
+ declared.stepIndex < i &&
319
+ declared.kind === "subagent"
320
+ ) {
321
+ errors.push({
322
+ rule: "subagent-dependency",
323
+ category: "output",
324
+ message: `Subagent step ${i + 1} ('${step.name}') cannot depend on output '{{outputs.${refName}}}' produced by subagent step ${declared.stepIndex + 1}. Subagents execute concurrently and cannot depend on each other.`,
325
+ stepIndex: i,
326
+ });
327
+ }
328
+ }
329
+ }
330
+ }
331
+
332
+ return errors;
333
+ };
334
+
335
+ /**
336
+ * Purpose: Ensures that every declared output is referenced at least once in subsequent workflow steps.
337
+ * Contract:
338
+ * - Inputs: WorkflowValidationContext with doc.
339
+ * - Outputs: readonly array of unused output errors.
340
+ */
341
+ export const ruleUnusedOutputs: WorkflowRule = (context) => {
342
+ const errors: ValidationError[] = [];
343
+ const declaredMap = extractDeclaredOutputs(context.doc.steps);
344
+ const usedOutputs = new Set<string>();
345
+
346
+ for (let i = 0; i < context.doc.steps.length; i++) {
347
+ const step = context.doc.steps[i];
348
+ const stringsToCheck = extractStepStrings(step);
349
+
350
+ for (const str of stringsToCheck) {
351
+ const matches = str.matchAll(OUTPUT_REF_REGEX);
352
+ for (const match of matches) {
353
+ usedOutputs.add(match[1]);
354
+ }
355
+ }
356
+ }
357
+
358
+ for (const [name, { stepIndex }] of declaredMap) {
359
+ if (!usedOutputs.has(name)) {
360
+ errors.push({
361
+ rule: "unused-output",
362
+ category: "output",
363
+ message: `Output '${name}' declared at step ${stepIndex + 1} is never used in the workflow. All outputs must be used at least once.`,
364
+ stepIndex,
365
+ });
366
+ }
367
+ }
368
+
369
+ return errors;
370
+ };
371
+
372
+ /**
373
+ * Purpose: Validates that all {{args.<name>}} placeholders across doc name and steps can be resolved.
374
+ * Contract:
375
+ * - Inputs: WorkflowValidationContext with doc and optional resolved args.
376
+ * - Outputs: readonly array of template argument interpolation or declaration errors.
377
+ */
378
+ export const ruleTemplateArgs: WorkflowRule = (context) => {
379
+ const errors: ValidationError[] = [];
380
+ const declaredAndDerivedArgs = new Set(Object.keys(context.doc.args));
381
+ for (const def of Object.values(context.doc.args)) {
382
+ if (def.type === "choice") {
383
+ const mapping = def.mapping ?? def.mappings;
384
+ if (mapping && !isFlatChoiceMapping(mapping)) {
385
+ for (const derivedKey of Object.keys(mapping)) {
386
+ declaredAndDerivedArgs.add(derivedKey);
387
+ }
388
+ }
389
+ }
390
+ }
391
+ const seenMissing = new Set<string>();
392
+
393
+ const check = (str: string, stepIndex?: number) => {
394
+ if (context.args !== undefined) {
395
+ try {
396
+ interpolateTemplate(str, context.args);
397
+ } catch (err: unknown) {
398
+ const message = err instanceof Error ? err.message : String(err);
399
+ errors.push({
400
+ rule: "template-args",
401
+ category: "template",
402
+ message,
403
+ ...(stepIndex !== undefined ? { stepIndex } : {}),
404
+ });
405
+ }
406
+ } else {
407
+ const matches = str.matchAll(/\{\{args\.([a-zA-Z0-9_-]+)\}\}/g);
408
+ for (const match of matches) {
409
+ const argName = match[1];
410
+ const key = `${stepIndex ?? "doc"}:${argName}`;
411
+ if (!declaredAndDerivedArgs.has(argName) && !seenMissing.has(key)) {
412
+ seenMissing.add(key);
413
+ errors.push({
414
+ rule: "template-args",
415
+ category: "template",
416
+ message: `Unresolved workflow argument: {{args.${argName}}} (not declared in workflow 'args')`,
417
+ ...(stepIndex !== undefined ? { stepIndex } : {}),
418
+ });
419
+ }
420
+ }
421
+ }
422
+ };
423
+
424
+ if (context.doc.name) {
425
+ check(context.doc.name);
426
+ }
427
+ if (context.doc.title) {
428
+ check(context.doc.title);
429
+ }
430
+
431
+ for (let i = 0; i < context.doc.steps.length; i++) {
432
+ const step = context.doc.steps[i];
433
+ const uniqueStrings = new Set(extractStepStrings(step));
434
+ for (const str of uniqueStrings) {
435
+ check(str, i);
436
+ }
437
+ }
438
+
439
+ return errors;
440
+ };
441
+
442
+ /**
443
+ * Purpose: Enforces that 'foreach' on a subagent references a valid declared output of type 'list'.
444
+ * Contract:
445
+ * - Inputs: WorkflowValidationContext with doc.
446
+ * - Outputs: readonly array of foreach validation errors.
447
+ */
448
+ export const ruleForeachOutputReferences: WorkflowRule = (context) => {
449
+ const errors: ValidationError[] = [];
450
+ const declaredMap = extractDeclaredOutputs(context.doc.steps);
451
+
452
+ for (let i = 0; i < context.doc.steps.length; i++) {
453
+ const step = context.doc.steps[i];
454
+ if (step.kind !== "subagent" || !step.foreach) continue;
455
+
456
+ const match = step.foreach.match(/^\{\{outputs\.([a-zA-Z0-9_-]+)\}\}$/);
457
+ if (!match) {
458
+ errors.push({
459
+ rule: "foreach-invalid-expression",
460
+ category: "output",
461
+ message: `Subagent step ${i + 1} ('${step.name}') has invalid foreach expression '${step.foreach}'. Must be '{{outputs.<name>}}'.`,
462
+ stepIndex: i,
463
+ });
464
+ continue;
465
+ }
466
+
467
+ const refName = match[1];
468
+ const declared = declaredMap.get(refName);
469
+ if (declared === undefined) {
470
+ errors.push({
471
+ rule: "unknown-output",
472
+ category: "output",
473
+ message: `Subagent step ${i + 1} ('${step.name}') foreach references unknown output '{{outputs.${refName}}}'.`,
474
+ stepIndex: i,
475
+ });
476
+ } else if (declared.stepIndex >= i) {
477
+ errors.push({
478
+ rule: "forward-output-reference",
479
+ category: "output",
480
+ message: `Subagent step ${i + 1} ('${step.name}') foreach references output '{{outputs.${refName}}}' before it is declared at step ${declared.stepIndex + 1}.`,
481
+ stepIndex: i,
482
+ });
483
+ } else if (declared.type !== "list") {
484
+ errors.push({
485
+ rule: "foreach-non-list-output",
486
+ category: "output",
487
+ message: `Subagent step ${i + 1} ('${step.name}') foreach references output '{{outputs.${refName}}}' which is of type '${declared.type}', expected 'list'.`,
488
+ stepIndex: i,
489
+ });
490
+ }
491
+ }
492
+
493
+ return errors;
494
+ };
495
+
496
+ /**
497
+ * Purpose: Validates exhaustiveness and unknown choice keys for a choice mapping entry.
498
+ */
499
+ function validateChoiceMappingEntries(
500
+ choices: readonly string[],
501
+ choiceMap: Record<string, string>,
502
+ argName: string,
503
+ derivedKey?: string,
504
+ ): ValidationError[] {
505
+ const errors: ValidationError[] = [];
506
+ const contextLabel = derivedKey
507
+ ? `Argument '${argName}' mapping for '${derivedKey}'`
508
+ : `Argument '${argName}' mapping`;
509
+
510
+ for (const choice of choices) {
511
+ if (!Object.hasOwn(choiceMap, choice)) {
512
+ errors.push({
513
+ rule: "choice-mapping-non-exhaustive",
514
+ category: "template",
515
+ message: `${contextLabel} is missing choice '${choice}'. All choices must be mapped.`,
516
+ });
517
+ }
518
+ }
519
+
520
+ for (const mappedKey of Object.keys(choiceMap)) {
521
+ if (!choices.includes(mappedKey)) {
522
+ errors.push({
523
+ rule: "choice-mapping-unknown-choice",
524
+ category: "template",
525
+ message: `${contextLabel} references unknown choice '${mappedKey}'. Allowed choices: ${choices.join(", ")}.`,
526
+ });
527
+ }
528
+ }
529
+
530
+ return errors;
531
+ }
532
+
533
+ /**
534
+ * Purpose: Enforces that choice argument mapping definitions are exhaustive, reference valid choices, and do not collide.
535
+ * Contract:
536
+ * - Inputs: WorkflowValidationContext with doc.
537
+ * - Outputs: readonly array of choice mapping validation errors.
538
+ */
539
+ export const ruleChoiceMapping: WorkflowRule = (context) => {
540
+ const errors: ValidationError[] = [];
541
+ const seenDerived = new Set<string>();
542
+ const topLevelArgNames = new Set(Object.keys(context.doc.args));
543
+
544
+ for (const [argName, def] of Object.entries(context.doc.args)) {
545
+ if (def.type !== "choice") continue;
546
+ const mapping = def.mapping ?? def.mappings;
547
+ if (!mapping) continue;
548
+
549
+ if (Object.keys(mapping).length === 0) {
550
+ errors.push({
551
+ rule: "choice-mapping-empty",
552
+ category: "template",
553
+ message: `Argument '${argName}' mapping cannot be empty.`,
554
+ });
555
+ continue;
556
+ }
557
+
558
+ if (isFlatChoiceMapping(mapping)) {
559
+ errors.push(
560
+ ...validateChoiceMappingEntries(def.choices, mapping, argName),
561
+ );
562
+ } else {
563
+ for (const [derivedKey, choiceMap] of Object.entries(mapping)) {
564
+ if (topLevelArgNames.has(derivedKey)) {
565
+ errors.push({
566
+ rule: "choice-mapping-collision",
567
+ category: "template",
568
+ message: `Derived argument '${derivedKey}' in argument '${argName}' mapping collides with top-level argument '${derivedKey}'.`,
569
+ });
570
+ } else if (seenDerived.has(derivedKey)) {
571
+ errors.push({
572
+ rule: "choice-mapping-duplicate",
573
+ category: "template",
574
+ message: `Derived argument '${derivedKey}' in argument '${argName}' mapping is already defined in another choice mapping.`,
575
+ });
576
+ } else {
577
+ seenDerived.add(derivedKey);
578
+ }
579
+
580
+ if (Object.keys(choiceMap).length === 0) {
581
+ errors.push({
582
+ rule: "choice-mapping-empty",
583
+ category: "template",
584
+ message: `Argument '${argName}' mapping for '${derivedKey}' cannot be empty.`,
585
+ });
586
+ continue;
587
+ }
588
+
589
+ errors.push(
590
+ ...validateChoiceMappingEntries(
591
+ def.choices,
592
+ choiceMap,
593
+ argName,
594
+ derivedKey,
595
+ ),
596
+ );
597
+ }
598
+ }
599
+ }
600
+
601
+ return errors;
602
+ };
603
+
604
+ export const WORKFLOW_RULES: readonly WorkflowRule[] = [
605
+ ruleSubagentOutputRequired,
606
+ ruleSubagentOutputTypes,
607
+ ruleDuplicateOutputNames,
608
+ ruleWorkflowNameOutputReferences,
609
+ ruleStepOutputReferences,
610
+ ruleSubagentDependencies,
611
+ ruleForeachOutputReferences,
612
+ ruleUnusedOutputs,
613
+ ruleTemplateArgs,
614
+ ruleChoiceMapping,
615
+ ];
616
+
617
+ /**
618
+ * Purpose: Executes a set of workflow validation rules against a document and arguments context.
619
+ * Contract:
620
+ * - Inputs: WorkflowValidationContext.
621
+ * - Outputs: readonly array of all collected validation errors.
622
+ */
623
+ export function validateWorkflow(
624
+ context: WorkflowValidationContext,
625
+ ): readonly ValidationError[] {
626
+ return WORKFLOW_RULES.flatMap((rule) => rule(context));
627
+ }
628
+
629
+ /**
630
+ * Purpose: Formats an array of validation errors into a human-readable diagnostic message.
631
+ * Contract:
632
+ * - Inputs: workflow target path and array of validation errors.
633
+ * - Outputs: formatted error string.
634
+ */
635
+ export function formatValidationErrors(
636
+ targetPath: string,
637
+ errors: readonly ValidationError[],
638
+ ): string {
639
+ if (errors.length === 0) {
640
+ return "";
641
+ }
642
+ if (errors.length === 1) {
643
+ const prefix = errors[0].category === "template" ? "Template" : "Output";
644
+ return `${prefix} validation error in workflow '${targetPath}': ${errors[0].message}`;
645
+ }
646
+ return `Validation errors in workflow '${targetPath}':\n${errors.map((e) => `- ${e.message}`).join("\n")}`;
647
+ }