-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclass-wp-agent-task-run-control.php
More file actions
376 lines (323 loc) · 12.2 KB
/
Copy pathclass-wp-agent-task-run-control.php
File metadata and controls
376 lines (323 loc) · 12.2 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
<?php
/**
* Generic task run-control primitives.
*
* @package AgentsAPI
*/
namespace AgentsAPI\AI\Tasks;
use AgentsAPI\AI\WP_Agent_Run_Result_Envelope;
defined( 'ABSPATH' ) || exit;
/**
* Shared helpers for canonical task execution contracts.
*/
class WP_Agent_Task_Run_Control {
public const STATUS_QUEUED = 'queued';
public const STATUS_RUNNING = 'running';
public const STATUS_CANCELLING = 'cancelling';
public const STATUS_CANCELLED = 'cancelled';
public const STATUS_SUCCEEDED = 'succeeded';
public const STATUS_FAILED = 'failed';
private const OPTION_KEY = 'agents_api_task_run_control';
/** @return string[] */
public static function statuses(): array {
return array(
self::STATUS_QUEUED,
self::STATUS_RUNNING,
self::STATUS_CANCELLING,
self::STATUS_CANCELLED,
self::STATUS_SUCCEEDED,
self::STATUS_FAILED,
);
}
/**
* Generate an opaque client-addressable run ID.
*/
public static function generate_run_id(): string {
if ( function_exists( 'wp_generate_uuid4' ) ) {
return 'task_run_' . str_replace( '-', '', wp_generate_uuid4() );
}
try {
return 'task_run_' . bin2hex( random_bytes( 16 ) );
} catch ( \Throwable $error ) {
unset( $error );
return 'task_run_' . str_replace( '.', '', uniqid( '', true ) );
}
}
/**
* Normalize a task run status/result payload returned by an executor.
*
* @param array<string,mixed> $run Raw run payload.
* @return array<string,mixed>
*/
public static function normalize_run( array $run ): array {
$run_id = trim( self::string_value( $run['run_id'] ?? null ) );
$session_id = trim( self::string_value( $run['session_id'] ?? null ) );
$executor_id = trim( self::string_value( $run['executor_id'] ?? null ) );
$status = self::normalize_status( $run['status'] ?? self::STATUS_RUNNING );
if ( '' === $run_id ) {
throw new \InvalidArgumentException( 'run_id must be a non-empty string' );
}
if ( '' === $session_id ) {
throw new \InvalidArgumentException( 'session_id must be a non-empty string' );
}
if ( '' === $executor_id ) {
throw new \InvalidArgumentException( 'executor_id must be a non-empty string' );
}
return array(
'schema' => self::string_value( $run['schema'] ?? 'agents-api/task-result/v1' ),
'run_id' => $run_id,
'session_id' => $session_id,
'status' => $status,
'executor_id' => $executor_id,
'execution_metrics' => self::execution_metrics_value( $run['execution_metrics'] ?? array(), $executor_id ),
'artifact_refs' => self::list_value( $run['artifact_refs'] ?? array() ),
'diagnostics' => self::map_value( $run['diagnostics'] ?? array() ),
'events' => self::list_value( $run['events'] ?? array() ),
'provenance' => self::map_value( $run['provenance'] ?? array() ),
'output' => $run['output'] ?? null,
'started_at' => self::string_value( $run['started_at'] ?? null ),
'updated_at' => self::string_value( $run['updated_at'] ?? null ),
'metadata' => self::map_value( $run['metadata'] ?? array() ),
);
}
/**
* Convert a task run/result payload to the canonical run result envelope.
*
* @param array<string,mixed> $run Raw or normalized run payload.
*/
public static function to_run_result_envelope( array $run ): WP_Agent_Run_Result_Envelope {
$normalized = self::normalize_run( $run );
$status = self::envelope_status( self::string_value( $normalized['status'] ) );
return WP_Agent_Run_Result_Envelope::from_array(
array(
'run_id' => self::string_value( $normalized['run_id'] ),
'status' => $status,
'outputs' => self::map_value( $normalized['output'] ?? array() ),
'artifact_refs' => WP_Agent_Run_Result_Envelope::normalize_refs( $normalized['artifact_refs'] ?? array() ),
'evidence_refs' => WP_Agent_Run_Result_Envelope::normalize_refs( $normalized['evidence_refs'] ?? array() ),
'provenance' => self::map_value( $normalized['provenance'] ?? array() ),
'timestamps' => array(
'started_at' => $normalized['started_at'] ?? '',
'updated_at' => $normalized['updated_at'] ?? '',
),
'error' => self::map_value( $normalized['error'] ?? array() ),
'cancellation' => self::map_value( $normalized['cancellation'] ?? array() ),
'metadata' => self::map_value( $normalized['metadata'] ?? array() ) + array(
'session_id' => $normalized['session_id'],
'executor_id' => $normalized['executor_id'],
'execution_metrics' => $normalized['execution_metrics'],
'diagnostics' => $normalized['diagnostics'],
'events' => $normalized['events'],
),
)
);
}
/** @param array<string,mixed> $run Raw or normalized run payload. */
public static function to_canonical_envelope( array $run ): WP_Agent_Run_Result_Envelope {
return self::to_run_result_envelope( $run );
}
/**
* Normalize status values while keeping the public vocabulary bounded.
*/
public static function normalize_status( mixed $status ): string {
$status = is_string( $status ) ? strtolower( trim( $status ) ) : '';
return in_array( $status, self::statuses(), true ) ? $status : self::STATUS_RUNNING;
}
private static function envelope_status( string $status ): string {
if ( self::STATUS_QUEUED === $status ) {
return WP_Agent_Run_Result_Envelope::STATUS_QUEUED;
}
if ( self::STATUS_CANCELLING === $status ) {
return WP_Agent_Run_Result_Envelope::STATUS_CANCELLING;
}
return WP_Agent_Run_Result_Envelope::normalize_status( $status );
}
/**
* Start or update an addressable task run in the default store.
*
* @param string $run_id Run ID.
* @param string $session_id Session ID.
* @param string $executor_id Executor ID.
* @param array<string,mixed> $metadata Run metadata.
* @return array<string,mixed> Normalized run.
*/
public static function start_run( string $run_id, string $session_id, string $executor_id, array $metadata = array() ): array {
$now = self::now();
$run = array(
'run_id' => $run_id,
'session_id' => $session_id,
'executor_id' => $executor_id,
'status' => self::STATUS_RUNNING,
'started_at' => $metadata['started_at'] ?? $now,
'updated_at' => $now,
'metadata' => $metadata,
);
$state = self::state();
$state['runs'][ $run_id ] = $run;
self::save_state( $state );
return self::normalize_run( $run );
}
/**
* Store a normalized executor result.
*
* @param array<string,mixed> $run Run/result payload.
* @return array<string,mixed> Normalized run.
*/
public static function save_run( array $run ): array {
$normalized = self::normalize_run( $run );
$normalized['updated_at'] = '' !== $normalized['updated_at'] ? $normalized['updated_at'] : self::now();
$run_id = self::string_value( $normalized['run_id'] );
$state = self::state();
$state['runs'][ $run_id ] = $normalized;
self::save_state( $state );
return $normalized;
}
/**
* Read a stored task run.
*
* @param string $run_id Run ID.
* @return array<string,mixed>|null Normalized run, or null when absent.
*/
public static function get_run( string $run_id ): ?array {
$state = self::state();
$run = $state['runs'][ $run_id ] ?? null;
return is_array( $run ) ? self::normalize_run( $run ) : null;
}
/**
* Request cancellation of a stored task run.
*
* @param string $run_id Run ID.
* @return array<string,mixed>|null Normalized run, or null when absent.
*/
public static function request_cancel( string $run_id ): ?array {
$state = self::state();
if ( ! isset( $state['runs'][ $run_id ] ) ) {
return null;
}
$run = $state['runs'][ $run_id ];
$terminal = in_array( self::normalize_status( $run['status'] ?? '' ), array( self::STATUS_SUCCEEDED, self::STATUS_FAILED, self::STATUS_CANCELLED ), true );
$run['status'] = $terminal ? self::normalize_status( $run['status'] ?? '' ) : self::STATUS_CANCELLING;
$run['cancelled'] = ! $terminal;
$run['updated_at'] = self::now();
$state['runs'][ $run_id ] = $run;
self::save_state( $state );
return self::normalize_run( $run ) + array( 'cancelled' => (bool) $run['cancelled'] );
}
/** @return array{runs:array<string,array<string,mixed>>} */
private static function state(): array {
$state = function_exists( 'get_option' ) ? get_option( self::OPTION_KEY, array() ) : array();
if ( ! is_array( $state ) ) {
$state = array();
}
return array( 'runs' => self::stored_runs( $state['runs'] ?? array() ) );
}
private static function string_value( mixed $value ): string {
return is_int( $value ) || is_float( $value ) || is_string( $value ) || is_bool( $value ) ? (string) $value : '';
}
/** @return array<string,mixed> */
private static function map_value( mixed $value ): array {
if ( ! is_array( $value ) ) {
return array();
}
$map = array();
foreach ( $value as $key => $item ) {
if ( is_string( $key ) ) {
$map[ $key ] = $item;
}
}
return $map;
}
/** @return array<string,mixed> */
private static function execution_metrics_value( mixed $value, string $executor_id ): array {
$metrics = self::map_value( $value );
if ( array() === $metrics ) {
return array();
}
$metrics['schema'] = self::non_empty_string_value( $metrics['schema'] ?? null ) ?? 'agents-api/execution-metrics/v1';
if ( ! isset( $metrics['executor_id'] ) && '' !== $executor_id ) {
$metrics['executor_id'] = $executor_id;
}
foreach ( array( 'environment', 'executor_id', 'failure_class' ) as $field ) {
if ( isset( $metrics[ $field ] ) ) {
$value = self::non_empty_string_value( $metrics[ $field ] );
if ( null === $value ) {
unset( $metrics[ $field ] );
} else {
$metrics[ $field ] = $value;
}
}
}
foreach ( array( 'wall_time_ms', 'startup_time_ms', 'tool_call_count', 'payload_bytes_in', 'payload_bytes_out', 'artifact_bytes' ) as $field ) {
if ( isset( $metrics[ $field ] ) ) {
$value = self::non_negative_int_value( $metrics[ $field ] );
if ( null === $value ) {
unset( $metrics[ $field ] );
} else {
$metrics[ $field ] = $value;
}
}
}
if ( isset( $metrics['per_tool_timings_ms'] ) ) {
$metrics['per_tool_timings_ms'] = self::non_negative_int_map_value( $metrics['per_tool_timings_ms'] );
}
if ( isset( $metrics['quality_signals'] ) ) {
$metrics['quality_signals'] = self::map_value( $metrics['quality_signals'] );
}
if ( isset( $metrics['raw_refs'] ) ) {
$metrics['raw_refs'] = self::list_value( $metrics['raw_refs'] );
}
return $metrics;
}
private static function non_empty_string_value( mixed $value ): ?string {
$value = trim( self::string_value( $value ) );
return '' === $value ? null : $value;
}
private static function non_negative_int_value( mixed $value ): ?int {
if ( ! is_int( $value ) && ! is_float( $value ) && ! ( is_string( $value ) && is_numeric( $value ) ) ) {
return null;
}
$value = (int) $value;
return 0 <= $value ? $value : null;
}
/** @return array<string,int> */
private static function non_negative_int_map_value( mixed $value ): array {
$map = self::map_value( $value );
$normalized = array();
foreach ( $map as $key => $item ) {
$item = self::non_negative_int_value( $item );
if ( null !== $item ) {
$normalized[ $key ] = $item;
}
}
return $normalized;
}
/** @return array<int,mixed> */
private static function list_value( mixed $value ): array {
return is_array( $value ) ? array_values( $value ) : array();
}
/**
* @param mixed $runs Raw stored runs.
* @return array<string,array<string,mixed>>
*/
private static function stored_runs( mixed $runs ): array {
if ( ! is_array( $runs ) ) {
return array();
}
$stored = array();
foreach ( $runs as $run_id => $run ) {
if ( is_string( $run_id ) && is_array( $run ) ) {
$stored[ $run_id ] = self::map_value( $run );
}
}
return $stored;
}
/** @param array<string,mixed> $state State to persist. */
private static function save_state( array $state ): void {
if ( function_exists( 'update_option' ) ) {
update_option( self::OPTION_KEY, $state, false );
}
}
private static function now(): string {
return gmdate( 'c' );
}
}