-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclass-wp-agent-workflow-spec-validator.php
More file actions
532 lines (477 loc) · 16.3 KB
/
Copy pathclass-wp-agent-workflow-spec-validator.php
File metadata and controls
532 lines (477 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
<?php
/**
* Structural validator for workflow specs.
*
* Walks a raw spec array and returns a list of structured errors:
*
* [
* [
* 'path' => 'steps.1.ability',
* 'code' => 'missing_required',
* 'message' => 'ability step is missing required `ability` field',
* ],
* ...
* ]
*
* An empty list means the spec is well-formed enough to construct a
* {@see WP_Agent_Workflow_Spec}. Validators are deliberately separated
* from the value object so consumers can use them on partial specs in
* editor surfaces (linting in-progress JSON, REST validate endpoints,
* `agents/validate-workflow` ability) without having to construct or
* discard a Spec each call.
*
* The validator does NOT verify that referenced abilities or agents
* actually exist — that's a runtime concern handled by the runner.
* This pass is structural only.
*
* @package AgentsAPI
* @since 0.103.0
*/
namespace AgentsAPI\AI\Workflows;
defined( 'ABSPATH' ) || exit;
final class WP_Agent_Workflow_Spec_Validator {
/** @since 0.103.0 */
public const KNOWN_STEP_TYPES = array( 'ability', 'agent', 'foreach', 'parallel' );
/** @since 0.103.0 */
public const KNOWN_TRIGGER_TYPES = array( 'on_demand', 'wp_action', 'cron' );
/**
* Validate a raw workflow spec.
*
* @since 0.103.0
*
* @param array<mixed> $spec Raw spec.
* @return array<int,array{path:string,code:string,message:string}> Empty when valid.
*/
public static function validate( array $spec ): array {
$errors = array();
// id
if ( empty( $spec['id'] ) || ! is_string( $spec['id'] ) ) {
$errors[] = array(
'path' => 'id',
'code' => 'missing_required',
'message' => 'workflow spec is missing a non-empty `id`',
);
}
// inputs (optional but if present must be a map)
if ( isset( $spec['inputs'] ) && ! is_array( $spec['inputs'] ) ) {
$errors[] = array(
'path' => 'inputs',
'code' => 'invalid_type',
'message' => '`inputs` must be a map of input_name => schema',
);
}
// steps
if ( ! isset( $spec['steps'] ) || ! is_array( $spec['steps'] ) || empty( $spec['steps'] ) ) {
$errors[] = array(
'path' => 'steps',
'code' => 'missing_required',
'message' => 'workflow spec must declare at least one step',
);
} else {
$step_errors = self::validate_steps( array_values( $spec['steps'] ) );
$errors = array_merge( $errors, $step_errors );
}
// triggers (optional but if present must be a list)
if ( isset( $spec['triggers'] ) ) {
if ( ! is_array( $spec['triggers'] ) || array_values( $spec['triggers'] ) !== $spec['triggers'] ) {
$errors[] = array(
'path' => 'triggers',
'code' => 'invalid_type',
'message' => '`triggers` must be a list of trigger definitions',
);
} else {
$trigger_errors = self::validate_triggers( $spec['triggers'] );
$errors = array_merge( $errors, $trigger_errors );
}
}
// Forward / unknown step-id binding references. Cheap structural
// pass: scan every `${steps.<id>.output.*}` token and verify <id>
// exists earlier in the list. Catches typos and re-orderings that
// would otherwise resolve to null silently at runtime.
if ( isset( $spec['steps'] ) && is_array( $spec['steps'] ) ) {
$binding_errors = self::validate_step_binding_references( array_values( $spec['steps'] ) );
$errors = array_merge( $errors, $binding_errors );
}
return $errors;
}
/**
* @param array<int,mixed> $steps
* @return array<int,array{path:string,code:string,message:string}>
*/
private static function validate_steps( array $steps ): array {
$errors = array();
$seen = array();
foreach ( $steps as $idx => $step ) {
$path = "steps.{$idx}";
if ( ! is_array( $step ) ) {
$errors[] = array(
'path' => $path,
'code' => 'invalid_type',
'message' => 'step entry must be an array',
);
continue;
}
if ( empty( $step['id'] ) || ! is_string( $step['id'] ) ) {
$errors[] = array(
'path' => "{$path}.id",
'code' => 'missing_required',
'message' => 'step is missing a non-empty `id`',
);
} elseif ( isset( $seen[ $step['id'] ] ) ) {
$errors[] = array(
'path' => "{$path}.id",
'code' => 'duplicate_id',
'message' => sprintf( 'step id `%s` is reused at index %d (first seen at index %d)', $step['id'], $idx, $seen[ $step['id'] ] ),
);
} else {
$seen[ $step['id'] ] = $idx;
}
if ( empty( $step['type'] ) || ! is_string( $step['type'] ) ) {
$errors[] = array(
'path' => "{$path}.type",
'code' => 'missing_required',
'message' => 'step is missing a non-empty `type`',
);
continue; // type is needed for the per-type checks below
}
if ( ! in_array( $step['type'], self::KNOWN_STEP_TYPES, true ) ) {
/**
* Allow consumer-extended step types. Agents-api ships only
* `ability` and `agent`; extra types (`branch`, `parallel`,
* `workflow`) get registered by consumers via a filter on
* `wp_agent_workflow_known_step_types`. Filtered list wins.
*
* @since 0.103.0
*
* @param array<string> $known_types Default v0 set.
*/
$known = (array) apply_filters( 'wp_agent_workflow_known_step_types', self::KNOWN_STEP_TYPES );
if ( ! in_array( $step['type'], $known, true ) ) {
$errors[] = array(
'path' => "{$path}.type",
'code' => 'unknown_step_type',
'message' => sprintf(
'unknown step type `%s` (known: %s)',
$step['type'],
implode( ', ', $known )
),
);
continue;
}
}
if ( 'ability' === $step['type'] ) {
if ( empty( $step['ability'] ) || ! is_string( $step['ability'] ) ) {
$errors[] = array(
'path' => "{$path}.ability",
'code' => 'missing_required',
'message' => 'ability step is missing a non-empty `ability`',
);
}
}
if ( 'agent' === $step['type'] ) {
if ( empty( $step['agent'] ) || ! is_string( $step['agent'] ) ) {
$errors[] = array(
'path' => "{$path}.agent",
'code' => 'missing_required',
'message' => 'agent step is missing a non-empty `agent`',
);
}
if ( empty( $step['message'] ) || ! is_string( $step['message'] ) ) {
$errors[] = array(
'path' => "{$path}.message",
'code' => 'missing_required',
'message' => 'agent step is missing a non-empty `message`',
);
}
}
if ( 'foreach' === $step['type'] ) {
if ( ! array_key_exists( 'items', $step ) ) {
$errors[] = array(
'path' => "{$path}.items",
'code' => 'missing_required',
'message' => 'foreach step is missing required `items` field',
);
}
if ( empty( $step['steps'] ) || ! is_array( $step['steps'] ) || array_values( $step['steps'] ) !== $step['steps'] ) {
$errors[] = array(
'path' => "{$path}.steps",
'code' => 'missing_required',
'message' => 'foreach step must declare a non-empty `steps` list',
);
} else {
foreach ( self::validate_steps( $step['steps'] ) as $inner_error ) {
$inner_path = (string) preg_replace( '/^steps\./', '', $inner_error['path'] );
$inner_error['path'] = "{$path}.steps." . $inner_path;
$errors[] = $inner_error;
}
}
}
if ( 'parallel' === $step['type'] ) {
$errors = array_merge( $errors, self::validate_parallel_step( $step, $path ) );
}
}
return $errors;
}
/**
* Validate a `parallel` (agent fanout) step. The one step type expresses
* two shapes; exactly one must be present:
*
* - parallel-map: `items` + a non-empty nested `steps` list.
* - parallel-roles: a non-empty `branches` list, each branch a role
* contract with a `role` + nested `steps`. At most one branch may be
* flagged `is_aggregator` (the optional aggregator); zero is valid.
*
* @since 0.4.0
*
* @param array<mixed> $step Raw parallel step.
* @param string $path Error path prefix for this step.
* @return array<int,array{path:string,code:string,message:string}>
*/
private static function validate_parallel_step( array $step, string $path ): array {
$errors = array();
$has_branches = isset( $step['branches'] );
$has_items = array_key_exists( 'items', $step );
if ( $has_branches === $has_items ) {
$errors[] = array(
'path' => $path,
'code' => 'invalid_parallel_shape',
'message' => 'parallel step must declare exactly one of `branches` (roles) or `items` (map)',
);
// Without a clear shape there's nothing further to validate.
if ( ! $has_branches && ! $has_items ) {
return $errors;
}
}
// parallel-map shape.
if ( $has_items ) {
if ( empty( $step['steps'] ) || ! is_array( $step['steps'] ) || array_values( $step['steps'] ) !== $step['steps'] ) {
$errors[] = array(
'path' => "{$path}.steps",
'code' => 'missing_required',
'message' => 'parallel-map step must declare a non-empty `steps` list',
);
} else {
foreach ( self::validate_steps( $step['steps'] ) as $inner_error ) {
$inner_path = (string) preg_replace( '/^steps\./', '', $inner_error['path'] );
$inner_error['path'] = "{$path}.steps." . $inner_path;
$errors[] = $inner_error;
}
}
}
// parallel-roles shape.
if ( $has_branches ) {
if ( ! is_array( $step['branches'] ) || array_values( $step['branches'] ) !== $step['branches'] || empty( $step['branches'] ) ) {
$errors[] = array(
'path' => "{$path}.branches",
'code' => 'missing_required',
'message' => 'parallel-roles step must declare a non-empty list of `branches`',
);
return $errors;
}
$aggregator_count = 0;
foreach ( $step['branches'] as $branch_idx => $branch ) {
$branch_path = "{$path}.branches.{$branch_idx}";
if ( ! is_array( $branch ) ) {
$errors[] = array(
'path' => $branch_path,
'code' => 'invalid_type',
'message' => 'parallel branch entry must be an array',
);
continue;
}
if ( empty( $branch['role'] ) || ! is_string( $branch['role'] ) ) {
$errors[] = array(
'path' => "{$branch_path}.role",
'code' => 'missing_required',
'message' => 'parallel branch is missing a non-empty `role`',
);
}
if ( empty( $branch['steps'] ) || ! is_array( $branch['steps'] ) || array_values( $branch['steps'] ) !== $branch['steps'] ) {
$errors[] = array(
'path' => "{$branch_path}.steps",
'code' => 'missing_required',
'message' => 'parallel branch must declare a non-empty `steps` list',
);
} else {
foreach ( self::validate_steps( $branch['steps'] ) as $inner_error ) {
$inner_path = (string) preg_replace( '/^steps\./', '', $inner_error['path'] );
$inner_error['path'] = "{$branch_path}.steps." . $inner_path;
$errors[] = $inner_error;
}
}
if ( ! empty( $branch['is_aggregator'] ) ) {
++$aggregator_count;
}
}
// The aggregator branch is OPTIONAL: zero or one is valid, more than
// one is ambiguous (which output is the step's final?).
if ( $aggregator_count > 1 ) {
$errors[] = array(
'path' => "{$path}.branches",
'code' => 'invalid_parallel_aggregator',
'message' => sprintf(
'parallel-roles step may flag at most one branch with `is_aggregator` (the aggregator); found %d',
$aggregator_count
),
);
}
}
return $errors;
}
/**
* Scan every `${steps.<id>.output.*}` reference inside step args and
* fields, return errors for any id that's unknown or only appears
* later in the list. Bindings to `inputs.*` aren't checked here — the
* runner validates inputs against the spec at run time.
*
* @param array<int,mixed> $steps
* @return array<int,array{path:string,code:string,message:string}>
*/
private static function validate_step_binding_references( array $steps ): array {
$errors = array();
$seen = array();
foreach ( $steps as $idx => $step ) {
if ( ! is_array( $step ) ) {
continue;
}
$step_id = is_string( $step['id'] ?? null ) ? $step['id'] : '';
$tokens = self::extract_top_level_step_binding_ids( $step );
foreach ( $tokens as $referenced_id ) {
if ( ! isset( $seen[ $referenced_id ] ) ) {
$errors[] = array(
'path' => "steps.{$idx}",
'code' => 'unknown_step_reference',
'message' => sprintf(
'step `%s` references `%s` which is not defined %s in the workflow',
'' !== $step_id ? $step_id : (string) $idx,
$referenced_id,
isset( $seen[ $step_id ] ) ? 'before this step' : 'before this step'
),
);
}
}
if ( '' !== $step_id ) {
$seen[ $step_id ] = true;
}
}
return $errors;
}
/**
* Pull step references from a top-level step without validating nested
* foreach step bodies against the outer step order.
*
* @param array<mixed> $step
* @return array<int,string>
*/
private static function extract_top_level_step_binding_ids( array $step ): array {
$type = $step['type'] ?? '';
$ids = array();
foreach ( $step as $key => $value ) {
// foreach + parallel-map defer their nested `steps`, and
// parallel-roles defers each branch's nested `steps`, to
// branch-scoped resolution; those bodies don't reference the
// outer step order so they're excluded from this cheap pass.
if ( 'steps' === $key && in_array( $type, array( 'foreach', 'parallel' ), true ) ) {
continue;
}
if ( 'branches' === $key && 'parallel' === $type ) {
continue;
}
$ids = array_merge( $ids, self::extract_step_binding_ids( $value ) );
}
return $ids;
}
/**
* Walk a step array and pull out every `${steps.<id>.output.*}` token's
* id segment. Used by {@see validate_step_binding_references()}.
*
* @param mixed $value
* @return array<int,string>
*/
private static function extract_step_binding_ids( $value ): array {
$ids = array();
if ( is_array( $value ) ) {
foreach ( $value as $inner ) {
$ids = array_merge( $ids, self::extract_step_binding_ids( $inner ) );
}
return $ids;
}
if ( ! is_string( $value ) ) {
return $ids;
}
if ( preg_match_all( '/\$\{\s*steps\.([a-zA-Z0-9_\-]+)\./', $value, $matches ) ) {
foreach ( $matches[1] as $found ) {
$ids[] = (string) $found;
}
}
return $ids;
}
/**
* @param array<int,mixed> $triggers
* @return array<int,array{path:string,code:string,message:string}>
*/
private static function validate_triggers( array $triggers ): array {
$errors = array();
foreach ( $triggers as $idx => $trigger ) {
$path = "triggers.{$idx}";
if ( ! is_array( $trigger ) ) {
$errors[] = array(
'path' => $path,
'code' => 'invalid_type',
'message' => 'trigger entry must be an array',
);
continue;
}
if ( empty( $trigger['type'] ) || ! is_string( $trigger['type'] ) ) {
$errors[] = array(
'path' => "{$path}.type",
'code' => 'missing_required',
'message' => 'trigger is missing a non-empty `type`',
);
continue;
}
$known = self::string_list( apply_filters( 'wp_agent_workflow_known_trigger_types', self::KNOWN_TRIGGER_TYPES ) );
if ( ! in_array( $trigger['type'], $known, true ) ) {
$errors[] = array(
'path' => "{$path}.type",
'code' => 'unknown_trigger_type',
'message' => sprintf(
'unknown trigger type `%s` (known: %s)',
$trigger['type'],
implode( ', ', $known )
),
);
continue;
}
if ( 'wp_action' === $trigger['type'] && empty( $trigger['hook'] ) ) {
$errors[] = array(
'path' => "{$path}.hook",
'code' => 'missing_required',
'message' => 'wp_action trigger is missing a non-empty `hook`',
);
}
if ( 'cron' === $trigger['type'] && empty( $trigger['expression'] ) && empty( $trigger['interval'] ) ) {
$errors[] = array(
'path' => $path,
'code' => 'missing_required',
'message' => 'cron trigger needs either `expression` (cron string) or `interval` (seconds)',
);
}
}
return $errors;
}
/**
* @param mixed $values Raw values.
* @return array<int,string>
*/
private static function string_list( $values ): array {
$values = is_array( $values ) ? $values : array();
$prepared = array();
foreach ( $values as $value ) {
if ( is_string( $value ) && '' !== $value ) {
$prepared[] = $value;
}
}
return array_values( array_unique( $prepared ) );
}
}