|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Concise active/no-signal triage preview for bounded cleanup output. |
| 4 | + * |
| 5 | + * @package DataMachineCode\Workspace |
| 6 | + */ |
| 7 | + |
| 8 | +namespace DataMachineCode\Workspace; |
| 9 | + |
| 10 | +defined('ABSPATH') || exit; |
| 11 | + |
| 12 | +final class WorktreeActiveNoSignalTriagePreview { |
| 13 | + |
| 14 | + private const ACTIVE_REASON_CODES = array( |
| 15 | + 'active_no_signal', |
| 16 | + 'no_inventory_cleanup_signal', |
| 17 | + 'lifecycle_reconciliation_candidate', |
| 18 | + ); |
| 19 | + |
| 20 | + /** |
| 21 | + * Build a bounded operator preview for unresolved active/no-signal rows. |
| 22 | + * |
| 23 | + * @param array<int,array<string,mixed>> $rows Skipped cleanup rows. |
| 24 | + * @param int $limit Suggested command page size. |
| 25 | + * @param int $now Current timestamp for age buckets. |
| 26 | + * @return array<string,mixed> |
| 27 | + */ |
| 28 | + public static function build( array $rows, int $limit = 25, ?int $now = null ): array { |
| 29 | + $now = $now ?? time(); |
| 30 | + $limit = max(1, min(200, $limit)); |
| 31 | + $preview = array( |
| 32 | + 'total' => 0, |
| 33 | + 'by_age' => array( |
| 34 | + 'lt_1d' => 0, |
| 35 | + '1_7d' => 0, |
| 36 | + '7_30d' => 0, |
| 37 | + 'gte_30d' => 0, |
| 38 | + 'unknown' => 0, |
| 39 | + ), |
| 40 | + 'by_liveness' => array(), |
| 41 | + 'by_repo' => array(), |
| 42 | + 'commands' => self::commands($limit), |
| 43 | + 'safety' => 'Commands classify active/no-signal rows into cleanup_eligible metadata only; they do not remove worktrees or branches.', |
| 44 | + ); |
| 45 | + |
| 46 | + foreach ( $rows as $row ) { |
| 47 | + if ( ! is_array($row) || ! in_array((string) ( $row['reason_code'] ?? '' ), self::ACTIVE_REASON_CODES, true) ) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + ++$preview['total']; |
| 52 | + ++$preview['by_age'][ self::age_bucket($row['created_at'] ?? null, $now) ]; |
| 53 | + |
| 54 | + $liveness = (string) ( $row['liveness'] ?? 'unknown' ); |
| 55 | + if ( '' === $liveness ) { |
| 56 | + $liveness = 'unknown'; |
| 57 | + } |
| 58 | + $preview['by_liveness'][ $liveness ] = (int) ( $preview['by_liveness'][ $liveness ] ?? 0 ) + 1; |
| 59 | + |
| 60 | + $repo = (string) ( $row['repo'] ?? 'unknown' ); |
| 61 | + if ( '' === $repo ) { |
| 62 | + $repo = 'unknown'; |
| 63 | + } |
| 64 | + $preview['by_repo'][ $repo ] = (int) ( $preview['by_repo'][ $repo ] ?? 0 ) + 1; |
| 65 | + } |
| 66 | + |
| 67 | + arsort($preview['by_liveness']); |
| 68 | + arsort($preview['by_repo']); |
| 69 | + $preview['by_repo'] = array_slice($preview['by_repo'], 0, 10, true); |
| 70 | + |
| 71 | + return $preview; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param mixed $created_at Created-at value from inventory metadata. |
| 76 | + */ |
| 77 | + private static function age_bucket( mixed $created_at, int $now ): string { |
| 78 | + if ( ! is_string($created_at) || '' === trim($created_at) ) { |
| 79 | + return 'unknown'; |
| 80 | + } |
| 81 | + |
| 82 | + $created = strtotime($created_at); |
| 83 | + if ( false === $created ) { |
| 84 | + return 'unknown'; |
| 85 | + } |
| 86 | + |
| 87 | + $age = max(0, $now - $created); |
| 88 | + $day = 86400; |
| 89 | + if ( $age < $day ) { |
| 90 | + return 'lt_1d'; |
| 91 | + } |
| 92 | + if ( $age < 7 * $day ) { |
| 93 | + return '1_7d'; |
| 94 | + } |
| 95 | + if ( $age < 30 * $day ) { |
| 96 | + return '7_30d'; |
| 97 | + } |
| 98 | + |
| 99 | + return 'gte_30d'; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @return array<string,string> |
| 104 | + */ |
| 105 | + private static function commands( int $limit ): array { |
| 106 | + $base = sprintf('--limit=%d --offset=0 --until-budget=60s --format=json', $limit); |
| 107 | + |
| 108 | + return array( |
| 109 | + 'report' => 'studio wp datamachine-code workspace worktree active-no-signal-report ' . $base, |
| 110 | + 'equivalent_clean_dry_run' => 'studio wp datamachine-code workspace worktree active-no-signal-equivalent-clean-apply --dry-run ' . $base, |
| 111 | + 'equivalent_clean_apply' => 'studio wp datamachine-code workspace worktree active-no-signal-equivalent-clean-apply ' . $base, |
| 112 | + 'merged_to_default_dry_run' => 'studio wp datamachine-code workspace worktree active-no-signal-merged-apply --dry-run ' . $base, |
| 113 | + 'merged_to_default_apply' => 'studio wp datamachine-code workspace worktree active-no-signal-merged-apply ' . $base, |
| 114 | + 'remote_clean_dry_run' => 'studio wp datamachine-code workspace worktree active-no-signal-remote-clean-apply --dry-run ' . $base, |
| 115 | + 'remote_clean_apply' => 'studio wp datamachine-code workspace worktree active-no-signal-remote-clean-apply ' . $base, |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
0 commit comments