-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworktree-add-lifecycle.php
More file actions
475 lines (411 loc) · 23.1 KB
/
Copy pathworktree-add-lifecycle.php
File metadata and controls
475 lines (411 loc) · 23.1 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
<?php
declare(strict_types=1);
const ARRAY_A = 'ARRAY_A';
if ( ! defined('ABSPATH') ) {
define('ABSPATH', __DIR__ . '/fixtures/');
}
$temp_root = realpath(sys_get_temp_dir()) ?: sys_get_temp_dir();
$workspace_root = rtrim($temp_root, '/') . '/datamachine-code-worktree-add-' . getmypid();
if ( ! defined('DATAMACHINE_WORKSPACE_PATH') ) {
define('DATAMACHINE_WORKSPACE_PATH', $workspace_root);
}
final class WP_Error {
private string $code;
private string $message;
private mixed $data;
public function __construct( string $code = '', string $message = '', mixed $data = null ) {
$this->code = $code;
$this->message = $message;
$this->data = $data;
}
public function get_error_code(): string {
return $this->code;
}
public function get_error_message(): string {
return $this->message;
}
public function get_error_data(): mixed {
return $this->data;
}
}
function is_wp_error( mixed $value ): bool {
return $value instanceof WP_Error;
}
$GLOBALS['datamachine_code_test_filters'] = array();
function apply_filters( string $hook_name, mixed $value, mixed ...$args ): mixed {
$callback = $GLOBALS['datamachine_code_test_filters'][ $hook_name ] ?? null;
if ( is_callable($callback) ) {
return $callback($value, ...$args);
}
return $value;
}
function current_time( string $type, bool $gmt = false ): string {
return gmdate('Y-m-d H:i:s');
}
function wp_json_encode( mixed $value, int $flags = 0, int $depth = 512 ): string|false {
return json_encode($value, $flags, $depth);
}
$GLOBALS['datamachine_code_test_options'] = array();
function get_option( string $name, mixed $default = false ): mixed {
return $GLOBALS['datamachine_code_test_options'][ $name ] ?? $default;
}
function update_option( string $name, mixed $value, mixed $autoload = null ): bool {
$GLOBALS['datamachine_code_test_options'][ $name ] = $value;
return true;
}
function home_url(): string {
return 'https://example.test';
}
function get_bloginfo( string $show = '' ): string {
return 'DMC Test';
}
function dbDelta( string $sql ): array {
return array();
}
final class Datamachine_Code_Test_Wpdb {
public string $prefix = 'wp_';
public bool $fail_replace = false;
public bool $sqlite = false;
public bool $busy_replace = false;
public string $last_error = '';
public int $insert_id = 0;
public int $rows_affected = 0;
public int $get_row_calls = 0;
/** @var array<string,array<string,mixed>> */
public array $rows = array();
/** @var array<int,array<string,mixed>> */
public array $lock_rows = array();
public function get_charset_collate(): string {
return '';
}
public function db_server_info(): string {
return $this->sqlite ? 'SQLite 3' : 'MySQL 8.4';
}
public function replace( string $table, array $data ): int|false {
if ( $this->busy_replace ) {
$this->last_error = 'database is locked';
return false;
}
if ( $this->fail_replace ) {
$this->last_error = 'constraint failed for token=ghp_abcdefghijklmnop and ' . str_repeat('x', 600);
return false;
}
$this->rows[ (string) $data['handle'] ] = $data;
$this->rows_affected = 1;
return 1;
}
public function insert( string $table, array $data, array $format = array() ): int|false {
++$this->insert_id;
$data['id'] = $this->insert_id;
$this->lock_rows[ $this->insert_id ] = $data;
$this->rows_affected = 1;
return 1;
}
public function delete( string $table, array $where ): int|false {
unset($this->rows[ (string) ( $where['handle'] ?? '' ) ]);
return 1;
}
public function update( string $table, array $data, array $where ): int|false {
$handle = (string) ( $where['handle'] ?? '' );
if ( isset($this->rows[ $handle ]) ) {
$this->rows[ $handle ] = array_merge($this->rows[ $handle ], $data);
}
if ( isset($where['id'], $this->lock_rows[ (int) $where['id'] ]) ) {
$this->lock_rows[ (int) $where['id'] ] = array_merge($this->lock_rows[ (int) $where['id'] ], $data);
}
$this->rows_affected = 1;
return 1;
}
public function get_results( string $sql, string $output = ARRAY_A ): array {
return array_values($this->rows);
}
public function get_row( string $sql, string $output = ARRAY_A ): ?array {
++$this->get_row_calls;
foreach ( $this->rows as $handle => $row ) {
if ( str_contains($sql, (string) $handle) ) {
return $row;
}
}
return null;
}
public function prepare( string $query, mixed ...$args ): string {
foreach ( $args as $arg ) {
$query = preg_replace('/%s/', addslashes((string) $arg), $query, 1) ?? $query;
}
return $query;
}
public function query( string $sql ): int|false {
$this->rows_affected = 0;
return 1;
}
public function get_var( string $sql ): string|int|null {
if ( str_contains($sql, 'SHOW TABLES LIKE') ) {
return str_contains($sql, 'datamachine_code_locks') ? $this->prefix . 'datamachine_code_locks' : $this->prefix . 'datamachine_code_worktrees';
}
return 0;
}
public function get_col( string $sql ): array {
return array();
}
}
require_once dirname(__DIR__) . '/vendor/autoload.php';
require_once dirname(__DIR__) . '/inc/Workspace/Workspace.php';
require_once dirname(__DIR__) . '/inc/Abilities/WorkspaceAbilities.php';
use DataMachineCode\Abilities\WorkspaceAbilities;
use DataMachineCode\Workspace\Workspace;
function run_command( string $command, ?string $cwd = null ): string {
$prefix = null === $cwd ? '' : 'cd ' . escapeshellarg($cwd) . ' && ';
$output = array();
$code = 0;
exec($prefix . $command . ' 2>&1', $output, $code);
if ( 0 !== $code ) {
throw new RuntimeException(sprintf("Command failed (%d): %s\n%s", $code, $command, implode("\n", $output)));
}
return implode("\n", $output);
}
function remove_tree( string $path ): void {
if ( ! file_exists($path) ) {
return;
}
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ( $iterator as $item ) {
$item->isDir() && ! $item->isLink() ? rmdir($item->getPathname()) : unlink($item->getPathname());
}
rmdir($path);
}
function assert_true( bool $condition, string $message ): void {
if ( ! $condition ) {
throw new RuntimeException($message);
}
}
function create_primary_checkout( string $workspace_root ): void {
$source = $workspace_root . '/source';
$origin = $workspace_root . '/origin.git';
mkdir($workspace_root, 0777, true);
mkdir($source, 0777, true);
run_command('git init -b main', $source);
run_command('git config user.email test@example.test', $source);
run_command('git config user.name "DMC Test"', $source);
file_put_contents($source . '/README.md', "fixture\n");
run_command('git add README.md', $source);
run_command('git commit -m initial', $source);
run_command('git init --bare ' . escapeshellarg($origin));
run_command('git remote add origin ' . escapeshellarg($origin), $source);
run_command('git push -u origin main', $source);
run_command('git clone ' . escapeshellarg($origin) . ' ' . escapeshellarg($workspace_root . '/homeboy'));
run_command('git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main', $workspace_root . '/homeboy');
}
remove_tree($workspace_root);
try {
create_primary_checkout($workspace_root);
$wpdb = new Datamachine_Code_Test_Wpdb();
$GLOBALS['wpdb'] = $wpdb;
$workspace = new Workspace();
run_command(
'git clone ' . escapeshellarg($workspace_root . '/origin.git') . ' ' . escapeshellarg($workspace_root . '/homeboy@custom-provider-auth-live')
);
run_command(
'git worktree add -b issue/242-embedding-generation ' . escapeshellarg($workspace_root . '/homeboy@address-darren-embedding-review') . ' origin/main',
$workspace_root . '/homeboy@custom-provider-auth-live'
);
$canonical_targeted = $workspace->worktree_list(
null,
null,
array(
'handle' => 'homeboy@address-darren-embedding-review',
'include_status' => true,
'include_disk' => false,
)
);
assert_true(1 === count($canonical_targeted['worktrees'] ?? array()), 'canonical worktree handle did not resolve when its directory slug differs from the Git branch');
assert_true('homeboy@address-darren-embedding-review' === ( $canonical_targeted['worktrees'][0]['handle'] ?? '' ), 'canonical worktree lookup returned the wrong handle');
assert_true('issue/242-embedding-generation' === ( $canonical_targeted['worktrees'][0]['branch'] ?? '' ), 'canonical worktree lookup did not preserve the Git branch');
assert_true(null !== ( $canonical_targeted['worktrees'][0]['dirty'] ?? null ), 'canonical worktree lookup did not run the requested status probe');
// A GitHub API workspace registers only this identity. Materialization must
// use the normal local lifecycle so the resulting handle is resolver-ready.
$materialized = $workspace->materialize_remote_workspace(
array(
'handle' => 'homeboy@feat-remote-materialization',
'repo_name' => 'homeboy',
'repo' => 'owner/homeboy',
'url' => $workspace_root . '/origin.git',
'branch' => 'feat/remote-materialization',
'base_ref' => 'origin/main',
'task' => array( 'task_url' => 'https://example.test/issues/255' ),
),
array(
'inject_context' => false,
'bootstrap' => false,
'force' => true,
'require_task_tracker' => true,
)
);
assert_true(! is_wp_error($materialized), is_wp_error($materialized) ? $materialized->get_error_message() : 'remote workspace materialization failed');
assert_true($workspace_root . '/homeboy@feat-remote-materialization' === ( $materialized['path'] ?? '' ), 'materialized workspace returned an unexpected path');
assert_true(is_file($workspace_root . '/homeboy@feat-remote-materialization/.git'), 'materialized workspace did not create a local worktree');
assert_true('https://example.test/issues/255' === ( $wpdb->rows['homeboy@feat-remote-materialization']['task_url'] ?? '' ), 'materialization did not preserve remote task metadata');
$materialized_targeted = $workspace->worktree_list(null, null, array( 'handle' => 'homeboy@feat-remote-materialization', 'include_status' => false, 'include_disk' => false ));
assert_true(1 === count($materialized_targeted['worktrees'] ?? array()), 'materialized workspace is not discoverable by targeted worktree lookup');
assert_true($workspace_root . '/homeboy@feat-remote-materialization' === ( $materialized_targeted['worktrees'][0]['path'] ?? '' ), 'targeted lookup did not return the materialized local path');
$GLOBALS['datamachine_code_test_filters']['datamachine_worktree_disk_budget_thresholds'] = static function ( array $thresholds ) use ( $workspace_root ): array {
$free = disk_free_space($workspace_root);
assert_true(false !== $free, 'fixture workspace free space is not measurable');
$thresholds['refuse_free_bytes'] = (int) $free + 1;
$thresholds['warn_free_bytes'] = (int) $free + 1;
$thresholds['refuse_free_percent'] = 0.0;
$thresholds['warn_free_percent'] = 0.0;
return $thresholds;
};
$refused = $workspace->worktree_add('homeboy', 'audit-primitives-disk-refused', 'origin/main', false, false, false, false, false);
unset($GLOBALS['datamachine_code_test_filters']['datamachine_worktree_disk_budget_thresholds']);
assert_true(is_wp_error($refused), 'disk pressure below the hard floor reported success');
assert_true('worktree_disk_budget_exceeded' === $refused->get_error_code(), 'unexpected disk pressure refusal error code');
$refusal_data = (array) $refused->get_error_data();
$disk_budget = (array) ( $refusal_data['disk_budget'] ?? array() );
assert_true('refused' === ( $disk_budget['status'] ?? '' ), 'disk pressure refusal did not include refused budget status');
assert_true(isset($disk_budget['free_bytes'], $disk_budget['effective_refuse_bytes']), 'disk pressure refusal must include exact free and required bytes');
assert_true(str_contains($refused->get_error_message(), 'studio wp datamachine-code workspace worktree bounded-cleanup-eligible-apply --dry-run --limit=25'), 'disk pressure refusal must include the next cleanup command');
assert_true(! is_dir($workspace_root . '/homeboy@audit-primitives-disk-refused'), 'disk pressure refusal left a worktree directory behind');
$ability_default = WorkspaceAbilities::worktreeAdd(
array(
'repo' => 'homeboy',
'branch' => 'ability-default-tracker-required',
'from' => 'origin/main',
'inject_context' => false,
'bootstrap' => false,
'force' => true,
)
);
assert_true(is_wp_error($ability_default), 'agent-facing worktree ability accepted missing tracker metadata by default');
assert_true('worktree_task_tracker_required' === $ability_default->get_error_code(), 'agent-facing worktree ability returned an unexpected error code');
assert_true(! is_dir($workspace_root . '/homeboy@ability-default-tracker-required'), 'agent-facing tracker refusal left a worktree directory behind');
$ability_operator_local = WorkspaceAbilities::worktreeAdd(
array(
'repo' => 'homeboy',
'branch' => 'ability-operator-local',
'from' => 'origin/main',
'inject_context' => false,
'bootstrap' => false,
'force' => true,
'require_task_tracker' => false,
)
);
assert_true(! is_wp_error($ability_operator_local), is_wp_error($ability_operator_local) ? $ability_operator_local->get_error_message() : 'explicit operator-local ability creation failed');
assert_true(is_dir($workspace_root . '/homeboy@ability-operator-local'), 'explicit operator-local ability creation did not materialize a worktree');
$strict_missing = $workspace->worktree_add('homeboy', 'audit-primitives-tracker-required', 'origin/main', false, false, false, false, true, array(), false, true);
assert_true(is_wp_error($strict_missing), 'strict worktree creation accepted missing tracker metadata');
assert_true('worktree_task_tracker_required' === $strict_missing->get_error_code(), 'strict worktree creation returned an unexpected error code');
assert_true(! is_dir($workspace_root . '/homeboy@audit-primitives-tracker-required'), 'strict tracker refusal left a worktree directory behind');
putenv('DATAMACHINE_TASK_URL=https://example.test/issues/environment');
$result = $workspace->worktree_add('homeboy', 'audit-primitives-20260616', 'origin/main', false, false, false, false, true, array( 'task_url' => 'https://example.test/issues/explicit' ), false, true);
assert_true(! is_wp_error($result), is_wp_error($result) ? $result->get_error_message() : 'worktree_add failed');
assert_true(is_dir($result['path']), 'successful worktree_add path is not accessible');
assert_true(isset($wpdb->rows['homeboy@audit-primitives-20260616']), 'successful worktree_add was not persisted');
assert_true('refused' !== ( $result['disk_budget']['status'] ?? '' ), 'normal worktree_add should pass the disk budget gate without hard refusal');
assert_true('https://example.test/issues/explicit' === ( $wpdb->rows['homeboy@audit-primitives-20260616']['task_url'] ?? '' ), 'explicit tracker metadata did not override the environment fallback');
$environment_tracker = $workspace->worktree_add('homeboy', 'audit-primitives-environment-tracker', 'origin/main', false, false, false, false, true, array(), false, true);
assert_true(! is_wp_error($environment_tracker), is_wp_error($environment_tracker) ? $environment_tracker->get_error_message() : 'environment tracker fallback failed');
assert_true('https://example.test/issues/environment' === ( $wpdb->rows['homeboy@audit-primitives-environment-tracker']['task_url'] ?? '' ), 'environment tracker metadata was not persisted');
putenv('DATAMACHINE_TASK_URL');
$handle = 'homeboy@audit-primitives-20260616';
file_put_contents($result['path'] . '/untracked.txt', "untracked\n");
$untracked_finalization = $workspace->worktree_finalize($handle, 'merged');
assert_true(is_wp_error($untracked_finalization), 'untracked worktree finalization reported success');
assert_true('worktree_dirty' === $untracked_finalization->get_error_code(), 'untracked finalization did not return worktree_dirty');
assert_true(1 === ( $untracked_finalization->get_error_data()['dirty_count'] ?? 0 ), 'untracked finalization did not report the dirty count');
assert_true(in_array('?? untracked.txt', $untracked_finalization->get_error_data()['dirty_paths'] ?? array(), true), 'untracked finalization did not report the dirty path');
assert_true('active' === ( $wpdb->rows[$handle]['lifecycle_state'] ?? '' ), 'dirty terminal finalization mutated lifecycle metadata');
$active_update = $workspace->worktree_finalize($handle, 'active');
assert_true(! is_wp_error($active_update), 'dirty active lifecycle update must remain permissive');
unlink($result['path'] . '/untracked.txt');
file_put_contents($result['path'] . '/README.md', "unstaged\n");
$unstaged_finalization = $workspace->worktree_finalize($handle, 'closed');
assert_true(is_wp_error($unstaged_finalization) && 'worktree_dirty' === $unstaged_finalization->get_error_code(), 'unstaged worktree finalization did not fail closed');
run_command('git checkout -- README.md', $result['path']);
file_put_contents($result['path'] . '/README.md', "staged\n");
run_command('git add README.md', $result['path']);
$staged_finalization = $workspace->worktree_finalize($handle, 'cleanup_eligible');
assert_true(is_wp_error($staged_finalization) && 'worktree_dirty' === $staged_finalization->get_error_code(), 'staged worktree finalization did not fail closed');
assert_true('active' === ( $wpdb->rows[$handle]['lifecycle_state'] ?? '' ), 'staged terminal finalization mutated lifecycle metadata');
run_command('git reset --hard HEAD', $result['path']);
$clean_finalization = $workspace->worktree_finalize($handle, 'merged');
assert_true(! is_wp_error($clean_finalization), 'clean terminal worktree finalization failed');
assert_true('cleanup_eligible' === ( $clean_finalization['lifecycle_state'] ?? '' ), 'clean terminal finalization did not expose cleanup eligibility');
$show = $workspace->show_repo('homeboy@audit-primitives-20260616');
assert_true(! is_wp_error($show), 'persisted worktree is not visible to show_repo');
assert_true(0 < $wpdb->get_row_calls, 'persisted worktree metadata did not use direct inventory lookup');
$list = $workspace->worktree_list('homeboy', null, array( 'include_status' => false, 'include_disk' => false ));
$handles = array_map(static fn( array $row ): string => (string) $row['handle'], $list['worktrees'] ?? array());
assert_true(in_array('homeboy@audit-primitives-20260616', $handles, true), 'persisted worktree is not visible to worktree_list');
$targeted = $workspace->worktree_list(
null,
null,
array(
'handle' => 'homeboy@audit-primitives-20260616',
'include_status' => true,
'include_disk' => false,
)
);
assert_true(1 === count($targeted['worktrees'] ?? array()), 'targeted worktree lookup returned unrelated rows');
assert_true('homeboy@audit-primitives-20260616' === ( $targeted['worktrees'][0]['handle'] ?? '' ), 'targeted worktree lookup returned the wrong handle');
assert_true(null !== ( $targeted['worktrees'][0]['dirty'] ?? null ), 'targeted worktree lookup did not run the requested status probe');
update_option(
'datamachine_code_remote_workspace_state',
array(
'repos' => array(
'other-repo' => array( 'repo' => 'owner/other-repo' ),
),
)
);
$removed = WorkspaceAbilities::worktreeRemove(
array(
'repo' => 'homeboy',
'branch' => 'audit-primitives-20260616',
'force' => true,
)
);
assert_true(! is_wp_error($removed), is_wp_error($removed) ? $removed->get_error_message() : 'worktreeRemove failed');
assert_true(! is_dir($workspace_root . '/homeboy@audit-primitives-20260616'), 'local worktree remove did not remove the fixture path');
assert_true(! isset($wpdb->rows['homeboy@audit-primitives-20260616']), 'local worktree remove did not delete inventory row');
$failure_wpdb = new Datamachine_Code_Test_Wpdb();
$failure_wpdb->fail_replace = true;
$GLOBALS['wpdb'] = $failure_wpdb;
$failed = $workspace->worktree_add('homeboy', 'audit-primitives-persist-fails', 'origin/main', false, false, false, false, true);
assert_true(is_wp_error($failed), 'inventory persistence failure reported success');
assert_true('worktree_inventory_persist_failed' === $failed->get_error_code(), 'unexpected persistence failure error code');
$failure_data = (array) $failed->get_error_data();
assert_true('worktree_inventory_upsert' === ( $failure_data['operation'] ?? '' ), 'inventory persistence failure did not identify the failed operation');
assert_true('database' === ( $failure_data['backend'] ?? '' ), 'inventory persistence failure did not identify the database backend');
assert_true(! str_contains((string) ( $failure_data['database_error'] ?? '' ), 'ghp_abcdefghijklmnop'), 'inventory persistence failure exposed a secret-like database detail');
assert_true(strlen((string) ( $failure_data['database_error'] ?? '' )) <= 512, 'inventory persistence failure did not bound database details');
assert_true(! is_dir($workspace_root . '/homeboy@audit-primitives-persist-fails'), 'failed persistence left a worktree directory behind');
$contention_wpdb = new Datamachine_Code_Test_Wpdb();
$contention_wpdb->sqlite = true;
$contention_wpdb->busy_replace = true;
$GLOBALS['wpdb'] = $contention_wpdb;
$GLOBALS['datamachine_code_test_filters']['datamachine_code_sqlite_busy_retry_max_wait_ms'] = static fn(): int => 1;
$contention = $workspace->worktree_add('homeboy', 'audit-primitives-sqlite-locked', 'origin/main', false, false, false, false, true);
unset($GLOBALS['datamachine_code_test_filters']['datamachine_code_sqlite_busy_retry_max_wait_ms']);
assert_true(is_wp_error($contention), 'SQLite contention reported success');
assert_true('workspace_sqlite_lock_contention' === $contention->get_error_code(), 'SQLite contention did not return the structured error');
assert_true(! is_dir($workspace_root . '/homeboy@audit-primitives-sqlite-locked'), 'SQLite contention left a partial Git worktree behind');
$GLOBALS['wpdb'] = new Datamachine_Code_Test_Wpdb();
run_command('git remote set-url origin ' . escapeshellarg($workspace_root . '/missing-origin.git'), $workspace_root . '/homeboy');
$fetch_failed_default = $workspace->worktree_add('homeboy', 'audit-primitives-fetch-fails', 'origin/main', false, false, false, false, true);
assert_true(is_wp_error($fetch_failed_default), 'fetch failure reported success without explicit opt-in');
assert_true('worktree_freshness_unverified' === $fetch_failed_default->get_error_code(), 'unexpected fetch failure error code');
assert_true(! is_dir($workspace_root . '/homeboy@audit-primitives-fetch-fails'), 'fetch failure left a worktree directory behind');
$fetch_failed_allowed = $workspace->worktree_add('homeboy', 'audit-primitives-fetch-fails-allowed', 'origin/main', false, false, false, false, true, array(), true);
assert_true(! is_wp_error($fetch_failed_allowed), is_wp_error($fetch_failed_allowed) ? $fetch_failed_allowed->get_error_message() : 'fetch failure opt-in failed');
assert_true(! empty($fetch_failed_allowed['fetch_failed']), 'fetch failure opt-in did not surface fetch_failed');
assert_true(is_dir($fetch_failed_allowed['path']), 'fetch failure opt-in worktree path is not accessible');
remove_tree($workspace_root);
fwrite(STDOUT, "worktree-add-lifecycle ok\n");
} catch (Throwable $e) {
remove_tree($workspace_root);
fwrite(STDERR, $e->getMessage() . "\n");
exit(1);
}