-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWorktreeCleanupChunkTask.php
More file actions
482 lines (441 loc) · 12.9 KB
/
Copy pathWorktreeCleanupChunkTask.php
File metadata and controls
482 lines (441 loc) · 12.9 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
<?php
/**
* Worktree cleanup chunk system task.
*
* @package DataMachineCode\Tasks
*/
namespace DataMachineCode\Tasks;
use DataMachine\Engine\AI\System\Tasks\SystemTask;
use DataMachineCode\Workspace\Workspace;
defined('ABSPATH') || exit;
class WorktreeCleanupChunkTask extends SystemTask {
/**
* Task type identifier.
*
* @return string
*/
public function getTaskType(): string {
return 'worktree_cleanup_chunk';
}
/**
* Pure workspace/disk maintenance — runs without agent ownership context.
*
* This task applies one reviewed cleanup chunk (artifact deletion or worktree
* removal) via the Workspace service. It is fanned out by the recurring
* workspace-maintenance parent tasks, which forward agent_id from their own
* params (0 under an agent-less recurring schedule). Without opting out, every
* child chunk scheduled by an agent-less parent would be rejected by the
* SystemTask agent-context gate in TaskScheduler::schedule(), so the actual
* destructive cleanup would never run even after the parents are fixed.
*
* @return bool
*/
public function requiresAgentContext(): bool {
return false;
}
/**
* Task metadata for Data Machine job surfaces.
*
* @return array<string,mixed>
*/
public static function getTaskMeta(): array {
return array(
'label' => 'Worktree Cleanup Chunk',
'description' => 'Applies one reviewed cleanup chunk for artifacts or worktree removal.',
'setting_key' => null,
'default_enabled' => true,
'supports_run' => false,
);
}
/**
* Execute one cleanup chunk.
*
* @param int $jobId Job ID.
* @param array $params Task params.
* @return void
*/
public function executeTask( int $jobId, array $params ): void {
$started_at = microtime(true);
$chunk_type = (string) ( $params['chunk_type'] ?? '' );
$rows = is_array($params['rows'] ?? null) ? array_values( (array) $params['rows']) : array();
if ( 'artifact_discovery' === $chunk_type ) {
$this->execute_artifact_discovery_chunk($jobId, $params, $started_at);
return;
}
if ( 'metadata_reconciliation_page' === $chunk_type ) {
$this->execute_metadata_reconciliation_page($jobId, $params, $started_at);
return;
}
if ( '' === $chunk_type || array() === $rows ) {
$this->completeJob(
$jobId,
$this->build_chunk_result(
$chunk_type,
$rows,
array(),
array(),
$this->rows_to_failed($rows, 'invalid_chunk', 'chunk_type and rows are required'),
0,
$started_at,
array( 'error' => 'chunk_type and rows are required' )
)
);
return;
}
$workspace = new Workspace();
$result = match ( $chunk_type ) {
'artifacts' => $workspace->worktree_cleanup_artifacts(
array(
'apply_plan' => array( 'candidates' => $rows ),
'force' => ! empty($params['force']),
'limit' => count($rows),
)
),
'worktrees' => $workspace->worktree_cleanup_merged(
array(
'apply_plan' => array( 'candidates' => $rows ),
'skip_github' => array_key_exists('skip_github', $params) ? (bool) $params['skip_github'] : true,
'include_repaired_metadata' => ! empty($params['include_repaired_metadata']),
'stale_liveness_only' => ! empty($params['stale_liveness_only']),
'remove_timeout' => isset($params['remove_timeout']) ? (int) $params['remove_timeout'] : null,
)
),
default => new \WP_Error('invalid_cleanup_chunk_type', sprintf('Unknown cleanup chunk type: %s', $chunk_type), array( 'status' => 400 )),
};
if ( $result instanceof \WP_Error ) {
$failed = $this->rows_to_failed($rows, (string) $result->get_error_code(), $result->get_error_message());
$this->completeJob(
$jobId,
$this->build_chunk_result(
$chunk_type,
$rows,
array(),
array(),
$failed,
0,
$started_at,
array(
'error_code' => $result->get_error_code(),
'error' => $result->get_error_message(),
)
)
);
return;
}
$applied = $this->extract_applied_rows($chunk_type, $result);
$skipped = array_values( (array) ( $result['skipped'] ?? array() ));
$bytes_reclaimed = $this->bytes_reclaimed($chunk_type, $applied, $result);
$this->completeJob(
$jobId,
$this->build_chunk_result(
$chunk_type,
$rows,
$applied,
$skipped,
array(),
$bytes_reclaimed,
$started_at,
array(
'summary' => $result['summary'] ?? array(),
'result' => $this->compact_evidence_result($result),
)
)
);
}
/**
* Discover and apply one bounded artifact cleanup page.
*
* @param int $jobId Job ID.
* @param array $params Task params.
* @param float $started_at Start timestamp.
* @return void
*/
private function execute_artifact_discovery_chunk( int $jobId, array $params, float $started_at ): void {
$limit = max(1, (int) ( $params['limit'] ?? 10 ));
$offset = max(0, (int) ( $params['offset'] ?? 0 ));
$force = ! empty($params['force']);
$workspace = new Workspace();
$plan = $workspace->worktree_cleanup_artifacts(
array(
'dry_run' => true,
'force' => $force,
'limit' => $limit,
'offset' => $offset,
'safety_probes' => true,
)
);
if ( $plan instanceof \WP_Error ) {
$this->completeJob(
$jobId,
$this->build_chunk_result(
'artifact_discovery',
array(),
array(),
array(),
array(
array(
'handle' => '',
'reason_code' => $plan->get_error_code(),
'reason' => $plan->get_error_message(),
),
),
0,
$started_at,
array(
'offset' => $offset,
'limit' => $limit,
)
)
);
return;
}
$planned = array_values( (array) ( $plan['candidates'] ?? array() ));
$skipped = array_values( (array) ( $plan['skipped'] ?? array() ));
if ( array() === $planned ) {
$this->completeJob(
$jobId,
$this->build_chunk_result(
'artifact_discovery',
array(),
array(),
$skipped,
array(),
0,
$started_at,
array(
'pagination' => $plan['pagination'] ?? null,
'summary' => $plan['summary'] ?? array(),
)
)
);
return;
}
$result = $workspace->worktree_cleanup_artifacts(
array(
'apply_plan' => array( 'candidates' => $planned ),
'force' => $force,
'limit' => count($planned),
)
);
if ( $result instanceof \WP_Error ) {
$this->completeJob(
$jobId,
$this->build_chunk_result(
'artifact_discovery',
$planned,
array(),
$skipped,
$this->rows_to_failed($planned, (string) $result->get_error_code(), $result->get_error_message()),
0,
$started_at,
array(
'pagination' => $plan['pagination'] ?? null,
'summary' => $plan['summary'] ?? array(),
)
)
);
return;
}
$applied = $this->extract_applied_rows('artifacts', $result);
$skipped = array_merge($skipped, array_values( (array) ( $result['skipped'] ?? array() )));
$this->completeJob(
$jobId,
$this->build_chunk_result(
'artifact_discovery',
$planned,
$applied,
$skipped,
array(),
$this->bytes_reclaimed('artifacts', $applied, $result),
$started_at,
array(
'pagination' => $plan['pagination'] ?? null,
'summary' => $result['summary'] ?? array(),
'result' => $this->compact_evidence_result($result),
)
)
);
}
/**
* Apply one bounded metadata reconciliation page.
*
* @param int $jobId Job ID.
* @param array $params Task params.
* @param float $started_at Start timestamp.
* @return void
*/
private function execute_metadata_reconciliation_page( int $jobId, array $params, float $started_at ): void {
$limit = max(1, (int) ( $params['limit'] ?? 50 ));
$offset = max(0, (int) ( $params['offset'] ?? 0 ));
$workspace = new Workspace();
$result = $workspace->worktree_reconcile_metadata(
array(
'apply' => true,
'limit' => $limit,
'offset' => $offset,
)
);
if ( $result instanceof \WP_Error ) {
$this->completeJob(
$jobId,
$this->build_chunk_result(
'metadata_reconciliation_page',
array(),
array(),
array(),
array(
array(
'handle' => '',
'reason_code' => $result->get_error_code(),
'reason' => $result->get_error_message(),
),
),
0,
$started_at,
array(
'limit' => $limit,
'offset' => $offset,
)
)
);
return;
}
$this->completeJob(
$jobId,
$this->build_chunk_result(
'metadata_reconciliation_page',
array_values( (array) ( $result['proposals'] ?? array() )),
array_values( (array) ( $result['written'] ?? array() )),
array_values( (array) ( $result['skipped'] ?? array() )),
array(),
0,
$started_at,
array(
'pagination' => $result['pagination'] ?? null,
'summary' => $result['summary'] ?? array(),
'result' => $this->compact_evidence_result($result),
)
)
);
}
/**
* Build the persisted chunk result.
*
* @param string $chunk_type Cleanup chunk type.
* @param array $planned Planned rows.
* @param array $applied Applied rows.
* @param array $skipped Skipped rows.
* @param array $failed Failed rows.
* @param int $bytes_reclaimed Bytes reclaimed.
* @param float $started_at Start timestamp.
* @param array $evidence Evidence payload.
* @return array<string,mixed>
*/
private function build_chunk_result( string $chunk_type, array $planned, array $applied, array $skipped, array $failed, int $bytes_reclaimed, float $started_at, array $evidence ): array {
$elapsed_ms = (int) round(( microtime(true) - $started_at ) * 1000);
return array(
'success' => array() === $failed,
'chunk_type' => $chunk_type,
'planned_count' => count($planned),
'applied_count' => count($applied),
'skipped_count' => count($skipped),
'failed_count' => count($failed),
'bytes_reclaimed' => $bytes_reclaimed,
'elapsed_ms' => $elapsed_ms,
'applied' => $applied,
'skipped' => $skipped,
'failed' => $failed,
'evidence' => array_merge(
array(
'planned_handles' => $this->row_handles($planned),
'applied_handles' => $this->row_handles($applied),
'skipped_handles' => $this->row_handles($skipped),
'failed_handles' => $this->row_handles($failed),
),
$evidence
),
);
}
/**
* Extract applied rows from a workspace result.
*
* @param string $chunk_type Cleanup chunk type.
* @param array $result Workspace result.
* @return array<int,array<string,mixed>>
*/
private function extract_applied_rows( string $chunk_type, array $result ): array {
return match ( $chunk_type ) {
'worktrees' => array_values( (array) ( $result['removed'] ?? array() )),
default => array_values( (array) ( $result['removed'] ?? array() )),
};
}
/**
* Calculate reclaimed bytes for applied rows.
*
* @param string $chunk_type Cleanup chunk type.
* @param array $applied Applied rows.
* @param array $result Workspace result.
* @return int
*/
private function bytes_reclaimed( string $chunk_type, array $applied, array $result ): int {
if ( 'artifacts' === $chunk_type ) {
return max(0, (int) ( $result['summary']['removed_size_bytes'] ?? 0 ));
}
if ( 'worktrees' !== $chunk_type ) {
return 0;
}
$total = 0;
foreach ( $applied as $row ) {
$total += max(0, (int) ( is_array($row) ? ( $row['size_bytes'] ?? 0 ) : 0 ));
}
return $total;
}
/**
* Convert rows to failed row payloads.
*
* @param array $rows Rows.
* @param string $code Error code.
* @param string $reason Error reason.
* @return array<int,array<string,mixed>>
*/
private function rows_to_failed( array $rows, string $code, string $reason ): array {
$failed = array();
foreach ( $rows as $row ) {
$failed[] = array(
'handle' => is_array($row) ? (string) ( $row['handle'] ?? '' ) : '',
'reason_code' => $code,
'reason' => $reason,
'row' => $row,
);
}
return $failed;
}
/**
* Return stable row handles for summaries.
*
* @param array $rows Rows.
* @return array<int,string>
*/
private function row_handles( array $rows ): array {
$handles = array();
foreach ( $rows as $row ) {
if ( is_array($row) && '' !== (string) ( $row['handle'] ?? '' ) ) {
$handles[] = (string) $row['handle'];
}
}
return array_values(array_unique($handles));
}
/**
* Keep evidence bounded while retaining operator-relevant summaries.
*
* @param array $result Workspace result.
* @return array<string,mixed>
*/
private function compact_evidence_result( array $result ): array {
return array(
'success' => (bool) ( $result['success'] ?? true ),
'dry_run' => (bool) ( $result['dry_run'] ?? false ),
'generated_at' => $result['generated_at'] ?? null,
);
}
}