-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrait-wp-codebox-runner-workspace-executor-behavior.php
More file actions
523 lines (484 loc) · 19.7 KB
/
Copy pathtrait-wp-codebox-runner-workspace-executor-behavior.php
File metadata and controls
523 lines (484 loc) · 19.7 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
<?php
/**
* Runner workspace executor behavior.
*
* Shared tool-name -> engine mapping and workspace-root resolution for the
* runner workspace executor. Kept in a trait so the executor can implement the
* Agents API tool-executor interface only when that interface is loaded.
*
* @package WPCodebox
*/
defined( 'ABSPATH' ) || exit;
trait WP_Codebox_Runner_Workspace_Executor_Behavior {
public const TARGET_ID = 'wp-codebox/runner-workspace';
/**
* Agents API tool source slug for the runner-native tool surface.
*
* Distinct from the git-less sandbox executor's `client` source so the two
* surfaces never collide when both plugins are mounted in the same runtime.
*/
public const SOURCE_SLUG = 'wp-codebox-runner';
/** Constant a runner may define to pin its workspace root. */
public const WORKSPACE_ROOT_CONSTANT = 'WP_CODEBOX_RUNNER_WORKSPACE_ROOT';
/** Guards one-time registration onto the Agents API filters. */
private static bool $registered = false;
/**
* Agent-facing tool names mapped to engine operations. Tool names are the
* codebox-native surface; engine methods do the real work.
*
* @return array<string,string>
*/
public static function tool_map(): array {
return array(
'workspace-read' => 'read',
'workspace-ls' => 'ls',
'workspace-grep' => 'grep',
'workspace-write' => 'write',
'workspace-edit' => 'edit',
'workspace-apply-patch' => 'apply_patch',
'workspace-git-status' => 'git_status',
'workspace-git-diff' => 'git_diff',
'workspace-git-add' => 'git_add',
'workspace-git-commit' => 'git_commit',
'workspace-git-push' => 'git_push',
'create-github-pull-request' => 'create_pull_request',
'create-github-issue' => 'create_issue',
'comment-github-pull-request' => 'comment_pull_request',
);
}
/**
* Capability and side-effect metadata per agent-facing tool.
*
* Keyed by the base tool name from {@see tool_map()}. Side effects classify
* each tool so a host can reason about its blast radius the same way the
* git-less sandbox executor does.
*
* @return array<string,array{capability:string,description:string,side_effects:array<int,string>,parameters:array<string,mixed>}>
*/
public static function tool_metadata(): array {
$path_param = array(
'type' => 'string',
'description' => 'Workspace-root-relative file or directory path.',
);
return array(
'workspace-read' => array(
'capability' => 'workspace.files.read',
'description' => 'Read a text file from the runner workspace.',
'side_effects' => array(),
'parameters' => array(
'type' => 'object',
'required' => array( 'path' ),
'properties' => array( 'path' => $path_param ),
),
),
'workspace-ls' => array(
'capability' => 'workspace.files.read',
'description' => 'List directory contents within the runner workspace.',
'side_effects' => array(),
'parameters' => array(
'type' => 'object',
'properties' => array( 'path' => $path_param ),
),
),
'workspace-grep' => array(
'capability' => 'workspace.files.read',
'description' => 'Search workspace files with git grep.',
'side_effects' => array(),
'parameters' => array(
'type' => 'object',
'required' => array( 'query' ),
'properties' => array(
'query' => array( 'type' => 'string', 'description' => 'Pattern to search for.' ),
'path' => $path_param,
'ignore_case' => array( 'type' => 'boolean', 'description' => 'Case-insensitive match.' ),
'fixed_strings' => array( 'type' => 'boolean', 'description' => 'Treat the query as a literal string.' ),
),
),
),
'workspace-write' => array(
'capability' => 'workspace.files.write',
'description' => 'Create or overwrite a file in the runner workspace.',
'side_effects' => array( 'filesystem.write' ),
'parameters' => array(
'type' => 'object',
'required' => array( 'path', 'content' ),
'properties' => array(
'path' => $path_param,
'content' => array( 'type' => 'string', 'description' => 'File content to write.' ),
),
),
),
'workspace-edit' => array(
'capability' => 'workspace.files.write',
'description' => 'Find-and-replace text in a runner workspace file.',
'side_effects' => array( 'filesystem.write' ),
'parameters' => array(
'type' => 'object',
'required' => array( 'path', 'old' ),
'properties' => array(
'path' => $path_param,
'old' => array( 'type' => 'string', 'description' => 'Text to find.' ),
'new' => array( 'type' => 'string', 'description' => 'Replacement text.' ),
'replace_all' => array( 'type' => 'boolean', 'description' => 'Replace every occurrence (default false).' ),
),
),
),
'workspace-apply-patch' => array(
'capability' => 'workspace.files.write',
'description' => 'Apply a unified diff to the runner workspace via git apply.',
'side_effects' => array( 'filesystem.write' ),
'parameters' => array(
'type' => 'object',
'required' => array( 'patch' ),
'properties' => array(
'patch' => array( 'type' => 'string', 'description' => 'Unified diff content to apply.' ),
),
),
),
'workspace-git-status' => array(
'capability' => 'workspace.git.read',
'description' => 'Report git status for the runner workspace.',
'side_effects' => array(),
'parameters' => array( 'type' => 'object', 'properties' => array() ),
),
'workspace-git-diff' => array(
'capability' => 'workspace.git.read',
'description' => 'Show the git diff for the runner workspace.',
'side_effects' => array(),
'parameters' => array(
'type' => 'object',
'properties' => array(
'path' => $path_param,
'staged' => array( 'type' => 'boolean', 'description' => 'Diff staged (cached) changes.' ),
),
),
),
'workspace-git-add' => array(
'capability' => 'workspace.git.write',
'description' => 'Stage paths in the runner workspace.',
'side_effects' => array( 'git.index' ),
'parameters' => array(
'type' => 'object',
'properties' => array(
'paths' => array( 'type' => 'array', 'items' => array( 'type' => 'string' ), 'description' => 'Paths to stage (defaults to all).' ),
),
),
),
'workspace-git-commit' => array(
'capability' => 'workspace.git.write',
'description' => 'Commit staged changes in the runner workspace.',
'side_effects' => array( 'git.commit' ),
'parameters' => array(
'type' => 'object',
'required' => array( 'message' ),
'properties' => array(
'message' => array( 'type' => 'string', 'description' => 'Commit message.' ),
'author_name' => array( 'type' => 'string', 'description' => 'Optional commit author name.' ),
'author_email' => array( 'type' => 'string', 'description' => 'Optional commit author email.' ),
),
),
),
'workspace-git-push' => array(
'capability' => 'workspace.git.push',
'description' => 'Push the runner workspace branch to its remote.',
'side_effects' => array( 'git.push', 'network.write' ),
'parameters' => array(
'type' => 'object',
'properties' => array(
'remote' => array( 'type' => 'string', 'description' => 'Remote name (default origin).' ),
'branch' => array( 'type' => 'string', 'description' => 'Branch to push.' ),
'set_upstream' => array( 'type' => 'boolean', 'description' => 'Set upstream tracking.' ),
'force_with_lease' => array( 'type' => 'boolean', 'description' => 'Force push with lease.' ),
),
),
),
'create-github-pull-request' => array(
'capability' => 'github.pull_request.write',
'description' => 'Open a GitHub pull request from the runner workspace.',
'side_effects' => array( 'github.write', 'network.write' ),
'parameters' => array(
'type' => 'object',
'required' => array( 'repo', 'title', 'head', 'base' ),
'properties' => array(
'repo' => array( 'type' => 'string', 'description' => 'owner/repo slug.' ),
'title' => array( 'type' => 'string', 'description' => 'Pull request title.' ),
'head' => array( 'type' => 'string', 'description' => 'Head branch.' ),
'base' => array( 'type' => 'string', 'description' => 'Base branch.' ),
'body' => array( 'type' => 'string', 'description' => 'Pull request body.' ),
'draft' => array( 'type' => 'boolean', 'description' => 'Open as a draft.' ),
),
),
),
'create-github-issue' => array(
'capability' => 'github.issue.write',
'description' => 'Open a GitHub issue from the runner workspace.',
'side_effects' => array( 'github.write', 'network.write' ),
'parameters' => array(
'type' => 'object',
'required' => array( 'repo', 'title' ),
'properties' => array(
'repo' => array( 'type' => 'string', 'description' => 'owner/repo slug.' ),
'title' => array( 'type' => 'string', 'description' => 'Issue title.' ),
'body' => array( 'type' => 'string', 'description' => 'Issue body.' ),
'labels' => array( 'type' => 'array', 'items' => array( 'type' => 'string' ), 'description' => 'Issue labels.' ),
),
),
),
'comment-github-pull-request' => array(
'capability' => 'github.pull_request.comment',
'description' => 'Comment on a GitHub pull request from the runner workspace.',
'side_effects' => array( 'github.write', 'network.write' ),
'parameters' => array(
'type' => 'object',
'required' => array( 'repo', 'number', 'body' ),
'properties' => array(
'repo' => array( 'type' => 'string', 'description' => 'owner/repo slug.' ),
'number' => array( 'type' => 'integer', 'description' => 'Pull request number.' ),
'body' => array( 'type' => 'string', 'description' => 'Comment body.' ),
),
),
),
);
}
/**
* The side-effect boundary tag this executor stamps on every result/metric.
*/
public const SIDE_EFFECT_BOUNDARY = 'wp-codebox-runner';
/**
* Register the runner-workspace executor onto the live Agents API contract.
*
* Mirrors the git-less sandbox executor: declares its tools as an Agents API
* tool source (each carrying `runtime.executor_target`), registers an
* executor target descriptor, and registers a tool-call executor under the
* target id so registry-based dispatch routes matching calls here. Idempotent;
* no-ops until the Agents API tool-execution substrate is loaded.
*/
public static function register(): bool {
if ( self::$registered ) {
return true;
}
if ( ! function_exists( 'add_filter' ) || ! self::substrate_exists() ) {
return false;
}
add_filter( 'agents_api_tool_sources', array( self::class, 'register_tool_source' ), 20, 3 );
add_filter( 'agents_api_executor_targets', array( self::class, 'register_executor_target' ), 20, 2 );
add_filter( 'agents_api_execution_targets', array( self::class, 'register_executor_target' ), 20, 2 );
add_filter( 'agents_api_tool_executors', array( self::class, 'register_tool_executor' ), 20, 2 );
self::$registered = true;
return true;
}
/**
* Whether the Agents API generic tool-execution substrate is loaded.
*
* Both the executor interface and the source registry must be present, the
* same gate the git-less sandbox executor uses, so the runner load path is
* verified before any registration runs.
*/
public static function substrate_exists(): bool {
return interface_exists( 'AgentsAPI\\AI\\Tools\\WP_Agent_Tool_Executor' )
&& class_exists( 'AgentsAPI\\AI\\Tools\\WP_Agent_Tool_Source_Registry' );
}
/**
* Register the runner-native tool surface as an Agents API tool source.
*
* @param array<string,callable> $sources Existing sources.
* @param array<string,mixed> $context Runtime context.
* @param mixed $registry Source registry.
* @return array<string,callable>
*/
public static function register_tool_source( array $sources, array $context = array(), $registry = null ): array {
unset( $context, $registry );
$existing = $sources[ self::SOURCE_SLUG ] ?? null;
$sources[ self::SOURCE_SLUG ] = static function ( array $source_context = array(), $source_registry = null ) use ( $existing ): array {
$tools = is_callable( $existing ) ? call_user_func( $existing, $source_context, $source_registry ) : array();
if ( ! is_array( $tools ) ) {
$tools = array();
}
foreach ( self::tool_declarations() as $tool_name => $tool_declaration ) {
if ( ! isset( $tools[ $tool_name ] ) ) {
$tools[ $tool_name ] = $tool_declaration;
}
}
return $tools;
};
return $sources;
}
/**
* Register this executor as an Agents API executor target descriptor.
*
* @param array<string,mixed> $targets Existing targets.
* @param array<string,mixed> $context Runtime context.
* @return array<string,mixed>
*/
public static function register_executor_target( array $targets, array $context = array() ): array {
unset( $context );
$targets[ self::TARGET_ID ] = self::target_metadata();
return $targets;
}
/**
* Register the tool-call executor for registry-based dispatch.
*
* A new executor instance is keyed under the target id so the Agents API
* tool-executor registry resolves `runtime.executor_target` calls here.
*
* @param array<string,mixed> $executors Existing executors.
* @param array<string,mixed> $context Runtime context.
* @return array<string,mixed>
*/
public static function register_tool_executor( array $executors, array $context = array() ): array {
unset( $context );
$executors[ self::TARGET_ID ] = new self();
return $executors;
}
/**
* Agents API tool declarations for the runner-native surface.
*
* @return array<string,array<string,mixed>>
*/
public static function tool_declarations(): array {
$tools = array();
$metadata = self::tool_metadata();
foreach ( self::tool_map() as $base => $operation ) {
unset( $operation );
$config = $metadata[ $base ] ?? array();
$qualified_name = self::SOURCE_SLUG . '/' . $base;
$tools[ $qualified_name ] = array(
'name' => $qualified_name,
'source' => self::SOURCE_SLUG,
'description' => (string) ( $config['description'] ?? '' ),
'parameters' => $config['parameters'] ?? array( 'type' => 'object', 'properties' => array() ),
'executor' => 'host',
'scope' => 'run',
'runtime' => array(
'executor_target' => self::TARGET_ID,
'capability' => (string) ( $config['capability'] ?? '' ),
'side_effects' => $config['side_effects'] ?? array(),
'side_effect_boundary' => self::SIDE_EFFECT_BOUNDARY,
),
);
}
return $tools;
}
/**
* Target metadata for generic Agents API executor discovery.
*
* @return array<string,mixed>
*/
public static function target_metadata(): array {
$metadata = self::tool_metadata();
return array(
'id' => self::TARGET_ID,
'label' => 'WP Codebox runner workspace',
'description' => 'Runner-native git + GitHub + file agent-tool surface bound to a single runner workspace root. Replaces the external coding-agent plugin dependency for the runner agent-facing surface.',
'resource_class' => 'workspace',
'kind' => 'runner-workspace',
'required_capabilities' => array_values(
array_unique(
array_filter(
array_map(
static fn( array $config ): string => (string) ( $config['capability'] ?? '' ),
$metadata
)
)
)
),
'side_effect_boundary' => self::SIDE_EFFECT_BOUNDARY,
'side_effects' => array( 'filesystem.write', 'git.commit', 'git.push', 'github.write', 'network.write' ),
);
}
/**
* Execute a tool call against the engine bound to the resolved workspace root.
*
* @param array<string,mixed> $parameters
* @param array<string,mixed> $context
* @return array<string,mixed>
*/
public function execute_tool( string $tool_name, array $parameters, array $context = array() ): array {
$operation = self::tool_map()[ self::normalize_tool_name( $tool_name ) ] ?? '';
if ( '' === $operation ) {
return array(
'success' => false,
'error' => array(
'code' => 'wp_codebox_runner_workspace_unknown_tool',
'message' => sprintf( 'Runner workspace executor does not handle tool "%s".', $tool_name ),
),
);
}
$root = self::resolve_workspace_root( $parameters, $context );
if ( '' === $root || ! is_dir( $root ) ) {
return array(
'success' => false,
'error' => array(
'code' => 'wp_codebox_runner_workspace_root_unavailable',
'message' => 'Runner workspace root is not configured or does not exist.',
),
);
}
$allowed_repos = is_array( $context['runner_workspace_policy']['allowed_repos'] ?? null )
? $context['runner_workspace_policy']['allowed_repos']
: array();
$engine = new WP_Codebox_Runner_Workspace_Tools( $root, null, $allowed_repos );
/** @var callable $callable */
$callable = array( $engine, $operation );
return (array) $callable( $parameters );
}
private static function normalize_tool_name( string $tool_name ): string {
$tool_name = trim( $tool_name );
// Accept namespaced declarations like "wp-codebox/workspace-read".
$slash = strrpos( $tool_name, '/' );
return false !== $slash ? substr( $tool_name, $slash + 1 ) : $tool_name;
}
/**
* Resolve the workspace root the tools operate on. Explicit per-call input
* wins, then the runtime client context, then a runner-defined constant,
* then an integration filter.
*
* @param array<string,mixed> $parameters
* @param array<string,mixed> $context
*/
public static function resolve_workspace_root( array $parameters, array $context = array() ): string {
$explicit = trim( (string) ( $parameters['workspace_root'] ?? $context['workspace_root'] ?? '' ) );
if ( '' !== $explicit ) {
return self::canonical_root( $explicit );
}
$from_context = self::workspace_root_from_context( $context );
if ( '' !== $from_context ) {
return self::canonical_root( $from_context );
}
if ( defined( self::WORKSPACE_ROOT_CONSTANT ) ) {
$constant = (string) constant( self::WORKSPACE_ROOT_CONSTANT );
if ( '' !== trim( $constant ) ) {
return self::canonical_root( $constant );
}
}
if ( function_exists( 'apply_filters' ) ) {
$filtered = apply_filters( 'wp_codebox_runner_workspace_root', '', $parameters, $context );
if ( is_string( $filtered ) && '' !== trim( $filtered ) ) {
return self::canonical_root( $filtered );
}
}
return '';
}
/** @param array<string,mixed> $context */
private static function workspace_root_from_context( array $context ): string {
$candidates = array(
$context['default_workspace']['target'] ?? null,
$context['sandbox_workspace']['root'] ?? null,
);
foreach ( is_array( $context['sandbox_workspace']['mounts'] ?? null ) ? $context['sandbox_workspace']['mounts'] : array() as $mount ) {
if ( is_array( $mount ) && 'readwrite' === ( $mount['mode'] ?? '' ) && is_string( $mount['target'] ?? null ) ) {
$candidates[] = $mount['target'];
}
}
foreach ( $candidates as $candidate ) {
if ( is_string( $candidate ) && '' !== trim( $candidate ) ) {
return trim( $candidate );
}
}
return '';
}
private static function canonical_root( string $root ): string {
$resolved = realpath( $root );
return false !== $resolved ? $resolved : rtrim( $root, '/' );
}
}