-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathphp-runtime-package-public-contract-smoke.php
More file actions
192 lines (173 loc) · 10.2 KB
/
Copy pathphp-runtime-package-public-contract-smoke.php
File metadata and controls
192 lines (173 loc) · 10.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
<?php
declare(strict_types=1);
define( 'ABSPATH', __DIR__ );
function is_wp_error( mixed $value ): bool {
return $value instanceof WP_Error;
}
function wp_json_encode( mixed $value, int $flags = 0, int $depth = 512 ): string|false { return json_encode( $value, $flags, $depth ); }
final class WP_Error {
public function __construct( private string $code, private string $message, private array $data = array() ) {}
public function get_error_code(): string { return $this->code; }
public function get_error_message(): string { return $this->message; }
public function get_error_data(): array { return $this->data; }
}
require_once __DIR__ . '/../packages/wordpress-plugin/src/class-wp-codebox-runtime-provider-registry.php';
require_once __DIR__ . '/../packages/wordpress-plugin/src/class-wp-codebox-runtime-package-executor.php';
require_once __DIR__ . '/../packages/wordpress-plugin/src/class-wp-codebox-abilities.php';
$GLOBALS['wp_codebox_runtime_package_smoke_filters'] = array();
function add_filter( string $hook, callable $callback, int $priority = 10, int $accepted_args = 1 ): void {
unset( $priority, $accepted_args );
$GLOBALS['wp_codebox_runtime_package_smoke_filters'][ $hook ][] = $callback;
}
function apply_filters( string $hook, mixed $value, mixed ...$args ): mixed {
foreach ( $GLOBALS['wp_codebox_runtime_package_smoke_filters'][ $hook ] ?? array() as $callback ) {
$value = $callback( $value, ...$args );
}
return $value;
}
function get_current_user_id(): int { return 1; }
function wp_agent_import_runtime_bundles( array $bundles, array $options ): array {
$GLOBALS['wp_codebox_runtime_package_imports'] = array( 'bundles' => $bundles, 'options' => $options );
return array_map(
static function( array $bundle ): array {
$document = json_decode( (string) file_get_contents( (string) $bundle['source'] ), true );
$slug = (string) ( $document['agent']['agent_slug'] ?? '' );
$GLOBALS['wp_codebox_runtime_package_registered_agents'][ $slug ] = true;
return array( 'success' => true, 'agent_slug' => $slug );
},
$bundles
);
}
function wp_get_agent( string $slug ): ?object { return ! empty( $GLOBALS['wp_codebox_runtime_package_registered_agents'][ $slug ] ) ? (object) array( 'slug' => $slug ) : null; }
function wp_codebox_smoke_package_digest( string $root ): string {
$files = array();
$iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $root, FilesystemIterator::SKIP_DOTS ), RecursiveIteratorIterator::LEAVES_ONLY );
foreach ( $iterator as $file ) {
$files[ str_replace( '\\', '/', substr( $file->getPathname(), strlen( $root ) + 1 ) ) ] = hash_file( 'sha256', $file->getPathname() );
}
ksort( $files, SORT_STRING );
$context = hash_init( 'sha256' );
foreach ( $files as $path => $digest ) {
hash_update( $context, $path . "\0" . $digest . "\n" );
}
return hash_final( $context );
}
final class WP_Codebox_Runtime_Package_Smoke_Ability {
public function execute( array $input ): array {
$GLOBALS['wp_codebox_runtime_package_smoke_input'] = $input;
return array(
'success' => true,
'outputs' => array( 'summary' => 'native semantic output', 'agent' => $input['agent'] ?? '' ),
'typed_artifacts' => array(
array(
'output_key' => 'concept_packet',
'schema' => 'wp-site-generator/ConceptPacket/v1',
'payload' => array( 'title' => 'Runtime package concept' ),
),
),
);
}
}
function wp_get_ability( string $name ): ?WP_Codebox_Runtime_Package_Smoke_Ability {
$GLOBALS['wp_codebox_runtime_package_smoke_abilities'][] = $name;
return 'agents/chat' === $name ? new WP_Codebox_Runtime_Package_Smoke_Ability() : null;
}
WP_Codebox_Runtime_Provider_Registry::register(
'contract-runtime',
static fn( array $input ): array => array(
'schema' => 'upstream/runtime-package-result/v1',
'success' => true,
'outputs' => array( 'summary' => 'semantic output' ),
'artifacts' => array( array( 'name' => 'report', 'type' => 'markdown', 'path' => 'files/report.md' ) ),
'received' => $input,
'diagnostics' => array(),
),
array( 'default' => true, 'label' => 'Contract runtime' )
);
$task = array(
'schema' => 'wp-codebox/runtime-package-task/v1',
'package' => array( 'slug' => 'example-agent', 'source' => '/workspace/bundles/example-agent' ),
'workflow' => array( 'id' => 'example-agent' ),
'input' => array( 'prompt' => 'ship' ),
'artifact_declarations' => array( array( 'name' => 'report', 'type' => 'markdown', 'required' => true ) ),
'required_artifacts' => array( 'report' ),
'metadata' => array( 'caller' => 'contract-smoke' ),
);
$result = WP_Codebox_Abilities::run_runtime_package( $task );
assert( ! is_wp_error( $result ) );
assert( 'wp-codebox/runtime-package-result/v1' === $result['schema'] );
assert( 'success' === $result['status'] );
assert( true === $result['success'] );
assert( $task['package'] === $result['package'] );
assert( array( 'summary' => 'semantic output' ) === $result['outputs'] );
assert( array( 'name' => 'report', 'type' => 'markdown', 'path' => 'files/report.md' ) === $result['artifacts'][0] );
assert( array() === $result['diagnostics'] );
assert( 'contract-runtime' === $result['metadata']['runtime_provider']['id'] );
assert( isset( $result['metadata']['received'] ) );
$optional_artifact_task = $task;
$optional_artifact_task['artifact_declarations'] = array(
array( 'name' => 'optional-report', 'type' => 'markdown', 'required' => false ),
array( 'name' => 'required-report', 'type' => 'markdown', 'required' => true ),
);
$optional_artifact_task['required_artifacts'] = array( 'optional-report' );
$undeclared_required = WP_Codebox_Abilities::run_runtime_package( $optional_artifact_task );
assert( is_wp_error( $undeclared_required ) );
assert( 'wp_codebox_runtime_package_task_invalid' === $undeclared_required->get_error_code() );
$bundle_root = realpath( (string) ( getenv( 'WP_CODEBOX_RUNTIME_PACKAGE_FIXTURE' ) ?: __DIR__ . '/../tests/fixtures/wpsg-runtime-package' ) );
assert( false !== $bundle_root );
$staged_bundle_root = sys_get_temp_dir() . '/wp-codebox-runtime-package-' . bin2hex( random_bytes( 8 ) );
mkdir( $staged_bundle_root, 0700, true );
$staged_bundle_file = $staged_bundle_root . '/package.agent.json';
copy( $bundle_root . '/.agent.json', $staged_bundle_file );
$wpsg_like_task = array(
'schema' => 'wp-codebox/runtime-package-task/v1',
'package' => array( 'slug' => 'store-idea-agent', 'source' => $staged_bundle_file, 'external_source' => array( 'digest' => 'sha256-bytes-v1:' . hash_file( 'sha256', $staged_bundle_file ) ) ),
'workflow' => array( 'id' => 'agents/chat' ),
'input' => array( 'prompt' => 'Industry: open', 'provider' => 'openai', 'model' => 'gpt-5.5' ),
'artifact_declarations' => array( array( 'name' => 'concept_packet', 'type' => 'typed_artifact', 'required' => true ) ),
'required_artifacts' => array( 'concept_packet' ),
'metadata' => array( 'caller' => 'wpsg-like-contract-smoke', 'imported_agent' => array( 'slug' => 'store-idea-agent' ) ),
);
WP_Codebox_Runtime_Package_Executor::register_runtime_provider();
$native = WP_Codebox_Abilities::run_runtime_package( $wpsg_like_task + array( 'runtime_provider' => 'codebox-runtime-package' ) );
assert( ! is_wp_error( $native ) );
assert( true === $native['success'] );
assert( 'native semantic output' === $native['outputs']['summary'] );
assert( 'store-idea-agent' === $native['outputs']['agent'] );
assert( 'agents/chat' === $native['metadata']['workflow_id'] );
assert( 'concept_packet' === $native['artifacts'][0]['name'] );
assert( 'Industry: open' === $GLOBALS['wp_codebox_runtime_package_smoke_input']['message'] );
assert( 'openai' === $GLOBALS['wp_codebox_runtime_package_smoke_input']['provider'] );
assert( 'gpt-5.5' === $GLOBALS['wp_codebox_runtime_package_smoke_input']['model'] );
assert( 'codebox-runtime-package' === $native['metadata']['runtime_provider']['id'] );
assert( 'store-idea-agent' === $GLOBALS['wp_codebox_runtime_package_imports']['bundles'][0]['slug'] );
assert( 1 === $GLOBALS['wp_codebox_runtime_package_imports']['options']['owner_id'] );
assert( ! in_array( 'agents/run-runtime-package', $GLOBALS['wp_codebox_runtime_package_smoke_abilities'], true ) );
$GLOBALS['wp_codebox_private_runtime_package_import'] = array(
'digest' => $wpsg_like_task['package']['external_source']['digest'],
'imports' => array( array( 'success' => true, 'agent_slug' => 'store-idea-agent' ) ),
'identity' => array( 'slug' => 'store-idea-agent' ),
);
$GLOBALS['wp_codebox_runtime_package_registered_agents']['store-idea-agent'] = true;
$bootstrap_task = $wpsg_like_task;
$bootstrap_task['package']['bootstrap_imported'] = true;
$bootstrap_task['input']['agent'] = 'store-idea-agent';
$bootstrap = WP_Codebox_Abilities::run_runtime_package( $bootstrap_task + array( 'runtime_provider' => 'codebox-runtime-package' ) );
assert( ! is_wp_error( $bootstrap ) );
assert( 'store-idea-agent' === $GLOBALS['wp_codebox_runtime_package_smoke_input']['agent'] );
$spoofed_bootstrap_task = $bootstrap_task;
$spoofed_bootstrap_task['input']['agent'] = 'caller-controlled-agent';
$spoofed_bootstrap = WP_Codebox_Abilities::run_runtime_package( $spoofed_bootstrap_task + array( 'runtime_provider' => 'codebox-runtime-package' ) );
assert( is_wp_error( $spoofed_bootstrap ) );
assert( 'wp_codebox_runtime_package_agent_identity_mismatch' === $spoofed_bootstrap->get_error_code() );
file_put_contents( $staged_bundle_file, "tampered\n" );
$tampered = WP_Codebox_Abilities::run_runtime_package( $wpsg_like_task + array( 'runtime_provider' => 'codebox-runtime-package' ) );
assert( is_wp_error( $tampered ) );
assert( 'wp_codebox_runtime_package_digest_mismatch' === $tampered->get_error_code() );
$cleanup = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $staged_bundle_root, FilesystemIterator::SKIP_DOTS ), RecursiveIteratorIterator::CHILD_FIRST );
unlink( $staged_bundle_file );
rmdir( $staged_bundle_root );
$invalid = WP_Codebox_Abilities::run_runtime_package( array( 'schema' => 'wp-codebox/runtime-package-task/v1', 'package' => array( 'slug' => 'example-agent' ) ) );
assert( is_wp_error( $invalid ) );
assert( 'wp_codebox_runtime_package_task_invalid' === $invalid->get_error_code() );
fwrite( STDOUT, "PHP runtime package public contract smoke passed\n" );