-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWorktreeCleanupCandidateClassifier.php
More file actions
186 lines (170 loc) · 6.31 KB
/
Copy pathWorktreeCleanupCandidateClassifier.php
File metadata and controls
186 lines (170 loc) · 6.31 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
<?php
/**
* Worktree cleanup candidate classification.
*
* @package DataMachineCode\Workspace
*/
namespace DataMachineCode\Workspace;
defined('ABSPATH') || exit;
final class WorktreeCleanupCandidateClassifier {
/**
* Classify one clean, non-primary worktree after safety probes and signal detection.
*
* @param array<string,mixed> $context Normalized worktree context.
* @param array<string,mixed>|null $signal Cleanup signal, if any.
* @param array<string,mixed>|null $age_filter Mutable age filter summary.
* @param callable $no_merge_signal_evidence Lazy evidence callback for no-signal rows.
* @param array<string,string> $active_review_commands Review/apply commands for no-signal rows.
* @return array{type:string,row:array<string,mixed>}
*/
public static function classify_merge_signal_path( array $context, ?array $signal, ?array &$age_filter, callable $no_merge_signal_evidence, array $active_review_commands ): array {
$base = self::base_row($context);
$disk_fields = is_array($context['disk_fields'] ?? null) ? $context['disk_fields'] : array();
if ( null === $signal ) {
return self::skip(
array_merge(
$base,
array(
'reason_code' => 'no_merge_signal',
'reason' => 'no merge signal — leaving in place',
'merge_signal_evidence' => $no_merge_signal_evidence(),
'active_review_command' => 'studio wp datamachine-code workspace worktree active-no-signal-report --limit=25 --offset=0 --format=json',
'active_review_commands' => $active_review_commands,
),
$disk_fields
)
);
}
if ( 'probe-timeout' === (string) ( $signal['signal'] ?? '' ) ) {
return self::skip(
array_merge(
$base,
array(
'reason_code' => 'probe_timeout',
'reason' => (string) ( $signal['reason'] ?? '' ),
),
$disk_fields
)
);
}
if ( 'github-unknown' === (string) ( $signal['signal'] ?? '' ) ) {
return self::skip(
array_merge(
$base,
array(
'reason_code' => 'github_unknown',
'reason' => (string) ( $signal['reason'] ?? '' ),
'merge_signal_evidence' => array(
'classification' => 'github_signal_unavailable',
'github_signal' => 'unavailable',
'reason' => (string) ( $signal['reason'] ?? '' ),
),
'active_review_command' => 'studio wp datamachine-code workspace worktree active-no-signal-report --limit=25 --offset=0 --format=json',
'active_review_commands' => $active_review_commands,
),
$disk_fields
)
);
}
if ( 'remote-tracking-clean' === (string) ( $signal['signal'] ?? '' ) && ! self::has_removable_lifecycle($context['metadata'] ?? null) ) {
return self::skip(
array_merge(
$base,
array(
'row_type' => 'protected',
'reason_code' => 'active_lifecycle',
'protecting_reason' => 'active_lifecycle',
'reason' => 'remote-tracking-clean is not a standalone destructive cleanup signal; lifecycle must be finalized, abandoned, or cleanup_eligible first',
'merge_signal_evidence' => $signal,
'active_review_command' => 'studio wp datamachine-code workspace worktree active-no-signal-report --limit=25 --offset=0 --format=json',
'active_review_commands' => $active_review_commands,
'remote_tracking_clean_only' => true,
),
$disk_fields
)
);
}
$age_decision = null;
if ( null !== $age_filter ) {
$age_decision = WorktreeAgeFilter::decide($context['created_at'] ?? null, $age_filter);
if ( in_array( (string) ( $age_decision['decision'] ?? '' ), array( 'unknown_age', 'excluded' ), true) ) {
return self::skip(
array_merge(
$base,
WorktreeAgeFilter::skip_fields($age_decision),
$disk_fields
)
);
}
}
$candidate = array_merge(
$base,
array(
'dirty' => (int) ( $context['dirty_count'] ?? 0 ),
'unpushed' => 0,
'cleanup_reasons' => array_values(array_filter(array( $signal['signal'] ?? '', $signal['reason'] ?? '' ))),
'liveness' => (string) ( $context['liveness'] ?? '' ),
),
WorktreeCleanupSignal::candidate_fields($signal, true),
$disk_fields
);
if ( null !== $age_decision ) {
$candidate['age_filter'] = $age_decision['age_filter'];
}
if ( ! empty($signal['preserve_local_branch']) ) {
$candidate['preserve_local_branch'] = true;
}
return array(
'type' => 'candidate',
'row' => $candidate,
);
}
/**
* Build common row fields used by this cleanup path.
*
* @param array<string,mixed> $context Normalized worktree context.
* @return array<string,mixed>
*/
private static function base_row( array $context ): array {
return array(
'handle' => (string) ( $context['handle'] ?? '' ),
'repo' => (string) ( $context['repo'] ?? '' ),
'branch' => (string) ( $context['branch'] ?? '' ),
'path' => (string) ( $context['path'] ?? '' ),
'created_at' => $context['created_at'] ?? null,
'metadata' => $context['metadata'] ?? null,
);
}
/**
* Whether lifecycle metadata explicitly allows retention cleanup to remove a worktree.
*
* @param mixed $metadata Worktree metadata.
* @return bool
*/
private static function has_removable_lifecycle( mixed $metadata ): bool {
if ( ! is_array($metadata) ) {
return false;
}
$state = isset($metadata['lifecycle_state']) ? WorktreeContextInjector::normalize_state( (string) $metadata['lifecycle_state']) : null;
$finalized_state = isset($metadata['finalized_state']) ? WorktreeContextInjector::normalize_state( (string) $metadata['finalized_state']) : null;
$removable = array(
WorktreeContextInjector::STATE_CLEANUP_ELIGIBLE,
WorktreeContextInjector::STATE_MERGED,
WorktreeContextInjector::STATE_CLOSED,
WorktreeContextInjector::STATE_ABANDONED,
);
return in_array($state, $removable, true) || in_array($finalized_state, $removable, true);
}
/**
* Wrap a skip row in the classifier result shape.
*
* @param array<string,mixed> $row Skip row.
* @return array{type:string,row:array<string,mixed>}
*/
private static function skip( array $row ): array {
return array(
'type' => 'skip',
'row' => $row,
);
}
}