Skip to content

Commit 10cb361

Browse files
committed
Fix stale cleanup lint issues
1 parent df978b5 commit 10cb361

8 files changed

Lines changed: 101 additions & 82 deletions

inc/Abilities/WorkspaceAbilities.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,33 +1562,33 @@ private function registerAbilities(): void {
15621562
'input_schema' => array(
15631563
'type' => 'object',
15641564
'properties' => array(
1565-
'mode' => array(
1565+
'mode' => array(
15661566
'type' => 'string',
15671567
'description' => 'Cleanup mode: inventory, artifacts, retention, stale-worktrees, or emergency.',
15681568
),
1569-
'force' => array(
1569+
'force' => array(
15701570
'type' => 'boolean',
15711571
'description' => 'Forward force=true to cleanup tasks that support it.',
15721572
),
1573-
'dry_run' => array(
1573+
'dry_run' => array(
15741574
'type' => 'boolean',
15751575
'description' => 'Rejected for background cleanup scheduling; use review abilities for dry-runs.',
15761576
),
1577-
'older_than' => array(
1577+
'older_than' => array(
15781578
'type' => 'string',
15791579
'description' => 'Optional worktree retention age gate such as 14d.',
15801580
),
15811581
'worktree_stale_only' => array(
15821582
'type' => 'boolean',
15831583
'description' => 'Only plan stale/inactive worktrees for destructive removal.',
15841584
),
1585-
'source' => array(
1585+
'source' => array(
15861586
'type' => 'string',
15871587
'description' => 'Caller source marker.',
15881588
),
1589-
'user_id' => array( 'type' => 'integer' ),
1590-
'agent_id' => array( 'type' => 'integer' ),
1591-
'agent_slug' => array( 'type' => 'string' ),
1589+
'user_id' => array( 'type' => 'integer' ),
1590+
'agent_id' => array( 'type' => 'integer' ),
1591+
'agent_slug' => array( 'type' => 'string' ),
15921592
),
15931593
),
15941594
'output_schema' => array(
@@ -2338,9 +2338,9 @@ private function registerAbilities(): void {
23382338
'output_schema' => array(
23392339
'type' => 'object',
23402340
'properties' => array(
2341-
'success' => array( 'type' => 'boolean' ),
2342-
'mode' => array( 'type' => 'string' ),
2343-
'plan_id' => array( 'type' => 'string' ),
2341+
'success' => array( 'type' => 'boolean' ),
2342+
'mode' => array( 'type' => 'string' ),
2343+
'plan_id' => array( 'type' => 'string' ),
23442344
'rows' => array( 'type' => 'object' ),
23452345
'action_rows' => array( 'type' => 'object' ),
23462346
'chunks' => array( 'type' => 'array' ),
@@ -3591,7 +3591,7 @@ public static function workspaceCleanupRun( array $input ): array|\WP_Error {
35913591

35923592
$mode = strtolower(preg_replace('/[^a-z0-9_\-]/', '', (string) ( $input['mode'] ?? 'retention' )));
35933593
$map = array(
3594-
'inventory' => array(
3594+
'inventory' => array(
35953595
'task_type' => 'workspace_hygiene_report',
35963596
'params' => array(
35973597
'include_cleanup' => true,
@@ -3600,7 +3600,7 @@ public static function workspaceCleanupRun( array $input ): array|\WP_Error {
36003600
'size_limit' => 200,
36013601
),
36023602
),
3603-
'artifacts' => array(
3603+
'artifacts' => array(
36043604
'task_type' => 'workspace_retention_cleanup',
36053605
'params' => array(
36063606
'dry_run' => false,
@@ -3612,15 +3612,15 @@ public static function workspaceCleanupRun( array $input ): array|\WP_Error {
36123612
'stale-worktrees' => array(
36133613
'task_type' => 'workspace_retention_cleanup',
36143614
'params' => array(
3615-
'dry_run' => false,
3616-
'artifact_cleanup' => false,
3617-
'worktree_cleanup' => true,
3618-
'skip_github' => true,
3619-
'worktree_older_than' => '14d',
3620-
'worktree_stale_only' => true,
3615+
'dry_run' => false,
3616+
'artifact_cleanup' => false,
3617+
'worktree_cleanup' => true,
3618+
'skip_github' => true,
3619+
'worktree_older_than' => '14d',
3620+
'worktree_stale_only' => true,
36213621
),
36223622
),
3623-
'retention' => array(
3623+
'retention' => array(
36243624
'task_type' => 'workspace_retention_cleanup',
36253625
'params' => array(
36263626
'dry_run' => false,
@@ -3630,7 +3630,7 @@ public static function workspaceCleanupRun( array $input ): array|\WP_Error {
36303630
'worktree_older_than' => '14d',
36313631
),
36323632
),
3633-
'emergency' => array(
3633+
'emergency' => array(
36343634
'task_type' => 'workspace_disk_emergency_cleanup',
36353635
'params' => array(
36363636
'artifact_chunk_size' => 10,

inc/Cli/Commands/WorkspaceCommand.php

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -783,11 +783,11 @@ private function attach_cleanup_run_commands( array $result, string $mode ): arr
783783
}
784784

785785
$result['commands'] = array(
786-
'drain_parent' => sprintf('studio wp datamachine drain --job-id=%d', $job_id),
787-
'status' => sprintf('studio wp datamachine-code workspace cleanup status %s --format=json', $run_id),
788-
'status_verbose' => sprintf('studio wp datamachine-code workspace cleanup status %s --verbose --format=json', $run_id),
789-
'one_command_drain' => sprintf('studio wp datamachine-code workspace cleanup run --mode=%s --drain --format=json', $mode),
790-
'bytes_verification' => sprintf('studio wp datamachine-code workspace cleanup status %s --format=json', $run_id),
786+
'drain_parent' => sprintf('studio wp datamachine drain --job-id=%d', $job_id),
787+
'status' => sprintf('studio wp datamachine-code workspace cleanup status %s --format=json', $run_id),
788+
'status_verbose' => sprintf('studio wp datamachine-code workspace cleanup status %s --verbose --format=json', $run_id),
789+
'one_command_drain' => sprintf('studio wp datamachine-code workspace cleanup run --mode=%s --drain --format=json', $mode),
790+
'bytes_verification' => sprintf('studio wp datamachine-code workspace cleanup status %s --format=json', $run_id),
791791
);
792792

793793
return $result;
@@ -811,8 +811,8 @@ private function drain_cleanup_run_to_status( array $result, array $assoc_args )
811811
return $result;
812812
}
813813

814-
$commands = array();
815-
$errors = array();
814+
$commands = array();
815+
$errors = array();
816816
$max_passes = 10;
817817

818818
$parent_command = sprintf('datamachine drain --job-id=%d', $job_id);
@@ -829,7 +829,7 @@ private function drain_cleanup_run_to_status( array $result, array $assoc_args )
829829
break;
830830
}
831831

832-
$children = (array) ( $status['evidence']['children'] ?? array() );
832+
$children = (array) ( $status['evidence']['children'] ?? array() );
833833
$active_child_ids = array_values(
834834
array_unique(
835835
array_filter(
@@ -856,17 +856,17 @@ private function drain_cleanup_run_to_status( array $result, array $assoc_args )
856856
}
857857
}
858858

859-
$final = $this->cleanup_run_evidence_store()->read($run_id, false, ! empty($assoc_args['verbose']));
860-
$output = $final instanceof \WP_Error ? $result : $final;
859+
$final = $this->cleanup_run_evidence_store()->read($run_id, false, ! empty($assoc_args['verbose']));
860+
$output = $final instanceof \WP_Error ? $result : $final;
861861
$output['initial_run'] = $result;
862862
$output['drain'] = array(
863-
'success' => array() === $errors,
864-
'commands' => $commands,
865-
'errors' => $errors,
866-
'verify_command' => sprintf('studio wp datamachine-code workspace cleanup status %s --format=json', $run_id),
867-
'bytes_reclaimed' => (int) ( $output['cleanup_items']['bytes_reclaimed'] ?? 0 ),
868-
'freed_human' => (string) ( $output['cleanup_items']['freed_human'] ?? $this->format_bytes(0) ),
869-
'completion_state' => (string) ( $output['state'] ?? 'unknown' ),
863+
'success' => array() === $errors,
864+
'commands' => $commands,
865+
'errors' => $errors,
866+
'verify_command' => sprintf('studio wp datamachine-code workspace cleanup status %s --format=json', $run_id),
867+
'bytes_reclaimed' => (int) ( $output['cleanup_items']['bytes_reclaimed'] ?? 0 ),
868+
'freed_human' => (string) ( $output['cleanup_items']['freed_human'] ?? $this->format_bytes(0) ),
869+
'completion_state' => (string) ( $output['state'] ?? 'unknown' ),
870870
);
871871

872872
return $output;
@@ -879,10 +879,6 @@ private function drain_cleanup_run_to_status( array $result, array $assoc_args )
879879
* @return string Empty string on success.
880880
*/
881881
private function run_wp_cli_command( string $command ): string {
882-
if ( ! method_exists('WP_CLI', 'runcommand') ) {
883-
return 'WP_CLI::runcommand is unavailable; run the reported drain commands manually.';
884-
}
885-
886882
try {
887883
WP_CLI::runcommand(
888884
$command,
@@ -1070,7 +1066,7 @@ private function run_cleanup_review( array $assoc_args ): void {
10701066
'stale_liveness_only' => true,
10711067
'older_than' => isset($assoc_args['older-than']) && '' !== trim( (string) $assoc_args['older-than']) ? trim( (string) $assoc_args['older-than']) : '14d',
10721068
);
1073-
$result = $ability ? $ability->execute($input) : new \WP_Error('worktree_cleanup_ability_missing', 'Worktree cleanup ability not registered.');
1069+
$result = $ability ? $ability->execute($input) : new \WP_Error('worktree_cleanup_ability_missing', 'Worktree cleanup ability not registered.');
10741070
$this->render_worktree_cleanup_result_from_ability($result, $assoc_args);
10751071
return;
10761072

@@ -1341,10 +1337,22 @@ private function render_cleanup_drain_summary( array $drain ): void {
13411337
WP_CLI::log('Drain summary:');
13421338
$this->format_items(
13431339
array(
1344-
array( 'metric' => 'success', 'value' => ! empty($drain['success']) ? 'yes' : 'no' ),
1345-
array( 'metric' => 'completion_state', 'value' => (string) ( $drain['completion_state'] ?? 'unknown' ) ),
1346-
array( 'metric' => 'bytes_reclaimed', 'value' => $this->format_bytes($drain['bytes_reclaimed'] ?? 0) ),
1347-
array( 'metric' => 'verify_command', 'value' => (string) ( $drain['verify_command'] ?? '' ) ),
1340+
array(
1341+
'metric' => 'success',
1342+
'value' => ! empty($drain['success']) ? 'yes' : 'no',
1343+
),
1344+
array(
1345+
'metric' => 'completion_state',
1346+
'value' => (string) ( $drain['completion_state'] ?? 'unknown' ),
1347+
),
1348+
array(
1349+
'metric' => 'bytes_reclaimed',
1350+
'value' => $this->format_bytes($drain['bytes_reclaimed'] ?? 0),
1351+
),
1352+
array(
1353+
'metric' => 'verify_command',
1354+
'value' => (string) ( $drain['verify_command'] ?? '' ),
1355+
),
13481356
),
13491357
array( 'metric', 'value' ),
13501358
array( 'format' => 'table' ),

inc/Tasks/WorkspaceRetentionCleanupTask.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ public function executeTask( int $jobId, array $params ): void {
8787
}
8888

8989
$opts = array(
90-
'dry_run' => ! empty($params['dry_run']),
91-
'force' => ! empty($params['force']),
92-
'skip_github' => array_key_exists('skip_github', $params) ? (bool) $params['skip_github'] : true,
93-
'worktree_cleanup' => array_key_exists('worktree_cleanup', $params) ? (bool) $params['worktree_cleanup'] : true,
94-
'artifact_cleanup' => array_key_exists('artifact_cleanup', $params) ? (bool) $params['artifact_cleanup'] : true,
95-
'worktree_stale_only' => ! empty($params['worktree_stale_only']),
90+
'dry_run' => ! empty($params['dry_run']),
91+
'force' => ! empty($params['force']),
92+
'skip_github' => array_key_exists('skip_github', $params) ? (bool) $params['skip_github'] : true,
93+
'worktree_cleanup' => array_key_exists('worktree_cleanup', $params) ? (bool) $params['worktree_cleanup'] : true,
94+
'artifact_cleanup' => array_key_exists('artifact_cleanup', $params) ? (bool) $params['artifact_cleanup'] : true,
95+
'worktree_stale_only' => ! empty($params['worktree_stale_only']),
9696
);
9797
if ( isset($params['worktree_older_than']) && '' !== trim( (string) $params['worktree_older_than']) ) {
9898
$opts['worktree_older_than'] = trim( (string) $params['worktree_older_than']);

inc/Tasks/WorktreeCleanupChunkTask.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function executeTask( int $jobId, array $params ): void {
117117
};
118118

119119
if ( $result instanceof \WP_Error ) {
120-
$failed = $this->rows_to_failed($rows, $result->get_error_code(), $result->get_error_message());
120+
$failed = $this->rows_to_failed($rows, (string) $result->get_error_code(), $result->get_error_message());
121121
$this->completeJob(
122122
$jobId,
123123
$this->build_chunk_result(
@@ -247,7 +247,7 @@ private function execute_artifact_discovery_chunk( int $jobId, array $params, fl
247247
$planned,
248248
array(),
249249
$skipped,
250-
$this->rows_to_failed($planned, $result->get_error_code(), $result->get_error_message()),
250+
$this->rows_to_failed($planned, (string) $result->get_error_code(), $result->get_error_message()),
251251
0,
252252
$started_at,
253253
array(

inc/Workspace/CleanupRunService.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
class CleanupRunService {
1616

17-
private const DEFAULT_APPLY_LIMIT = 25;
18-
private const MAX_APPLY_LIMIT = 100;
17+
private const DEFAULT_APPLY_LIMIT = 25;
18+
private const MAX_APPLY_LIMIT = 100;
1919

2020

2121

@@ -92,16 +92,16 @@ public function apply( string $run_id, array $opts = array() ): array|\WP_Error
9292
)
9393
);
9494

95-
$items = $this->repository->get_items($run_id);
96-
$artifact_rows = $this->pending_rows_of_type($items, 'artifact_cleanup');
97-
$worktree_rows = $this->pending_rows_of_type($items, 'worktree_removal');
95+
$items = $this->repository->get_items($run_id);
96+
$artifact_rows = $this->pending_rows_of_type($items, 'artifact_cleanup');
97+
$worktree_rows = $this->pending_rows_of_type($items, 'worktree_removal');
9898
$stale_worktrees_only = 'stale-worktrees' === (string) ( $run['mode'] ?? '' );
99-
$batch_type = '';
100-
$processed_rows = 0;
101-
$applied_rows = 0;
102-
$skipped_rows = 0;
103-
$remaining_rows = max(0, count($artifact_rows) + count($worktree_rows));
104-
$results = array();
99+
$batch_type = '';
100+
$processed_rows = 0;
101+
$applied_rows = 0;
102+
$skipped_rows = 0;
103+
$remaining_rows = max(0, count($artifact_rows) + count($worktree_rows));
104+
$results = array();
105105

106106
if ( array() !== $artifact_rows ) {
107107
$artifact_batch = array_slice($artifact_rows, 0, $limit);
@@ -116,8 +116,12 @@ public function apply( string $run_id, array $opts = array() ): array|\WP_Error
116116
)
117117
);
118118
$this->record_apply_result($artifact_batch, $results['artifact_cleanup'], 'removed');
119-
$applied_rows += count((array) ( $results['artifact_cleanup']['removed'] ?? array() ));
120-
$skipped_rows += count((array) ( $results['artifact_cleanup']['skipped'] ?? array() ));
119+
if ( ! is_wp_error($results['artifact_cleanup']) ) {
120+
$applied_rows += count( (array) ( $results['artifact_cleanup']['removed'] ?? array() ) );
121+
$skipped_rows += count( (array) ( $results['artifact_cleanup']['skipped'] ?? array() ) );
122+
} else {
123+
$skipped_rows += count($artifact_batch);
124+
}
121125
}
122126

123127
$remaining_capacity = max(0, $limit - $processed_rows);
@@ -135,8 +139,12 @@ public function apply( string $run_id, array $opts = array() ): array|\WP_Error
135139
)
136140
);
137141
$this->record_apply_result($worktree_batch, $results['worktree_removal'], 'removed');
138-
$applied_rows += count((array) ( $results['worktree_removal']['removed'] ?? array() ));
139-
$skipped_rows += count((array) ( $results['worktree_removal']['skipped'] ?? array() ));
142+
if ( ! is_wp_error($results['worktree_removal']) ) {
143+
$applied_rows += count( (array) ( $results['worktree_removal']['removed'] ?? array() ) );
144+
$skipped_rows += count( (array) ( $results['worktree_removal']['skipped'] ?? array() ) );
145+
} else {
146+
$skipped_rows += count($worktree_batch);
147+
}
140148
}
141149

142150
$status = $this->status($run_id);

inc/Workspace/WorkspaceCleanupPlan.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,20 @@ public function workspace_cleanup_plan( array $opts = array() ): array|\WP_Error
8585
'resolver' => $inputs['include_resolvers'] ? $this->build_cleanup_plan_resolver_rows( (array) ( $worktree_plan['skipped'] ?? array() )) : array(),
8686
);
8787

88-
$action_rows = array(
88+
$action_rows = array(
8989
'remove_artifacts' => $rows['artifact_cleanup'],
9090
'remove_worktree' => $rows['worktree_removal'],
9191
'resolve_signal' => $rows['resolver'],
9292
);
93-
$summary = $this->build_cleanup_plan_summary($rows);
93+
94+
$summary = $this->build_cleanup_plan_summary($rows);
9495
$summary['rows_by_action'] = array(
9596
'remove_artifacts' => count($action_rows['remove_artifacts']),
9697
'remove_worktree' => count($action_rows['remove_worktree']),
9798
'resolve_signal' => count($action_rows['resolve_signal']),
9899
);
99-
$plan = array(
100+
101+
$plan = array(
100102
'success' => true,
101103
'mode' => 'cleanup_plan',
102104
'generated_at' => gmdate('c'),
@@ -117,6 +119,7 @@ public function workspace_cleanup_plan( array $opts = array() ): array|\WP_Error
117119
'action_rows' => $action_rows,
118120
'summary' => $summary,
119121
);
122+
120123
$plan['plan_id'] = $this->stable_cleanup_hash(
121124
array(
122125
'inputs' => $inputs,

inc/Workspace/WorkspaceHygieneReport.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ private function build_workspace_inventory_rows(): array {
368368

369369
$rows = array();
370370
foreach ( $entries as $entry ) {
371-
if ( '.' === $entry || '..' === $entry || str_starts_with((string) $entry, '.') ) {
371+
if ( '.' === $entry || '..' === $entry || str_starts_with( (string) $entry, '.' ) ) {
372372
continue;
373373
}
374374

@@ -447,7 +447,7 @@ private function build_workspace_size_report( int $limit ): array {
447447
$dirs = array_values(
448448
array_filter(
449449
$entries,
450-
fn( $entry ) => '.' !== $entry && '..' !== $entry && ! str_starts_with((string) $entry, '.') && is_dir($this->workspace_path . '/' . $entry)
450+
fn( $entry ) => '.' !== $entry && '..' !== $entry && ! str_starts_with( (string) $entry, '.' ) && is_dir($this->workspace_path . '/' . $entry)
451451
)
452452
);
453453
sort($dirs, SORT_NATURAL);

0 commit comments

Comments
 (0)