-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-wp-codebox-task-input-contract.php
More file actions
188 lines (174 loc) · 6.78 KB
/
class-wp-codebox-task-input-contract.php
File metadata and controls
188 lines (174 loc) · 6.78 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
<?php
/**
* Canonical WP Codebox task input contract.
*
* @package WPCodebox
*/
defined( 'ABSPATH' ) || exit;
final class WP_Codebox_Task_Input_Contract {
public const SCHEMA = 'wp-codebox/task-input/v1';
public const VERSION = 1;
/** @return array<string,mixed> */
public static function schema(): array {
return array(
'$id' => self::SCHEMA,
'type' => 'object',
'required' => array( 'schema', 'version', 'goal', 'target', 'allowed_tools', 'expected_artifacts', 'agent_bundles', 'sandbox_tool_policy', 'policy', 'context' ),
'properties' => array(
'schema' => array(
'type' => 'string',
'const' => self::SCHEMA,
'description' => 'Task input contract schema id.',
),
'version' => array(
'type' => 'integer',
'const' => self::VERSION,
'description' => 'Task input contract version.',
),
'goal' => array(
'type' => 'string',
'description' => 'User-facing outcome the sandboxed coding agent should accomplish.',
),
'target' => array(
'type' => 'object',
'description' => 'Bounded target for the task, such as a repo, site, plugin, or theme.',
'properties' => array(
'kind' => array( 'type' => 'string' ),
'ref' => array( 'type' => 'string' ),
'path' => array( 'type' => 'string' ),
'url' => array( 'type' => 'string' ),
),
),
'allowed_tools' => array(
'type' => 'array',
'description' => 'Tool names the product caller expects the sandboxed agent to stay within.',
'items' => array( 'type' => 'string' ),
),
'expected_artifacts' => array(
'type' => 'array',
'description' => 'Artifact kinds the caller wants back, such as patch, review, tests, preview, or package.',
'items' => array( 'type' => 'string' ),
),
'agent_bundles' => array(
'type' => 'array',
'description' => 'Runtime agent bundles to import into the disposable sandbox before invoking the selected runtime agent.',
'items' => array(
'type' => 'object',
'anyOf' => array(
array( 'required' => array( 'source' ) ),
array( 'required' => array( 'bundle' ) ),
),
'properties' => array(
'source' => array( 'type' => 'string' ),
'bundle' => array( 'type' => 'object' ),
'slug' => array( 'type' => 'string' ),
'on_conflict' => array( 'type' => 'string', 'enum' => array( 'error', 'skip', 'upgrade' ) ),
'owner_id' => array( 'type' => 'integer' ),
'token_env' => array( 'type' => 'string' ),
'import_principal' => array( 'type' => 'object' ),
),
),
),
'sandbox_tool_policy' => array(
'type' => 'object',
'description' => 'Resolved caller-owned sandbox tool policy snapshot. Codebox validates and enforces it without owning product-specific tool taxonomy.',
),
'policy' => array(
'type' => 'object',
'description' => 'Caller policy hints for approvals, apply-back, sandboxing, and risk controls.',
),
'context' => array(
'type' => 'object',
'description' => 'Additional non-secret caller context for the sandboxed task.',
),
),
);
}
/** @param array<string,mixed> $input Ability input. @return array<string,mixed>|WP_Error */
public static function normalize( array $input ): array|WP_Error {
$goal = trim( (string) ( $input['goal'] ?? '' ) );
if ( '' === $goal ) {
return new WP_Error( 'wp_codebox_task_missing', 'goal is required.', array( 'status' => 400 ) );
}
return array(
'schema' => self::SCHEMA,
'version' => self::VERSION,
'goal' => $goal,
'target' => is_array( $input['target'] ?? null ) ? $input['target'] : array(),
'allowed_tools' => self::string_list( $input['allowed_tools'] ?? array() ),
'expected_artifacts' => self::string_list( $input['expected_artifacts'] ?? array() ),
'agent_bundles' => self::agent_bundles( $input['agent_bundles'] ?? array() ),
'sandbox_tool_policy' => is_array( $input['sandbox_tool_policy'] ?? null ) ? $input['sandbox_tool_policy'] : array(),
'policy' => is_array( $input['policy'] ?? null ) ? $input['policy'] : array(),
'context' => is_array( $input['context'] ?? null ) ? $input['context'] : array(),
);
}
/** @return array<int,array<string,mixed>> */
private static function agent_bundles( mixed $value ): array {
$bundles = array();
foreach ( is_array( $value ) ? $value : array() as $entry ) {
if ( ! is_array( $entry ) ) {
continue;
}
$source = isset( $entry['source'] ) ? trim( (string) $entry['source'] ) : '';
$inline = is_array( $entry['bundle'] ?? null ) ? $entry['bundle'] : null;
if ( '' === $source && null === $inline ) {
continue;
}
$bundle = array();
if ( '' !== $source ) {
$bundle['source'] = $source;
}
if ( null !== $inline ) {
$bundle['bundle'] = $inline;
}
foreach ( array( 'slug', 'token_env' ) as $field ) {
$value = isset( $entry[ $field ] ) ? trim( (string) $entry[ $field ] ) : '';
if ( '' !== $value ) {
$bundle[ $field ] = $value;
}
}
$on_conflict = (string) ( $entry['on_conflict'] ?? 'upgrade' );
$bundle['on_conflict'] = in_array( $on_conflict, array( 'error', 'skip', 'upgrade' ), true ) ? $on_conflict : 'upgrade';
if ( isset( $entry['owner_id'] ) && (int) $entry['owner_id'] > 0 ) {
$bundle['owner_id'] = (int) $entry['owner_id'];
}
if ( is_array( $entry['import_principal'] ?? null ) ) {
$bundle['import_principal'] = self::import_principal( $entry['import_principal'] );
}
$bundles[] = $bundle;
}
return $bundles;
}
/** @param array<string,mixed> $principal Raw import principal. @return array<string,mixed> */
private static function import_principal( array $principal ): array {
$normalized = array();
foreach ( array( 'agent_id', 'owner_id', 'token_id' ) as $field ) {
if ( isset( $principal[ $field ] ) && (int) $principal[ $field ] > 0 ) {
$normalized[ $field ] = (int) $principal[ $field ];
}
}
$capabilities = self::string_list( $principal['capabilities'] ?? array() );
if ( ! empty( $capabilities ) ) {
$normalized['capabilities'] = $capabilities;
}
if ( is_array( $principal['scope'] ?? null ) ) {
$normalized['scope'] = $principal['scope'];
}
return $normalized;
}
/** @return string[] */
private static function string_list( mixed $value ): array {
if ( ! is_array( $value ) ) {
return array();
}
$items = array();
foreach ( $value as $item ) {
$item = trim( (string) $item );
if ( '' !== $item && ! in_array( $item, $items, true ) ) {
$items[] = $item;
}
}
return $items;
}
}