Skip to content

Commit 89ff7ee

Browse files
Fix disk-budget refusal cleanup guidance (#782)
* Fix disk-budget refusal cleanup guidance * Fix disk-budget lint alignment --------- Co-authored-by: homeboy-ci[bot] <266378653+homeboy-ci[bot]@users.noreply.github.com>
1 parent 478b13a commit 89ff7ee

3 files changed

Lines changed: 85 additions & 9 deletions

File tree

inc/Workspace/WorkspaceWorktreeLifecycle.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,30 @@ public function worktree_add( string $repo, string $branch, ?string $from = null
114114
$disk_budget = WorktreeDiskBudget::inspect($this->workspace_path, WorktreeDiskBudget::thresholds($repo, $branch), $force);
115115
if ( 'refused' === ( $disk_budget['status'] ?? '' ) ) {
116116
$recommendations = array_map(
117-
fn( $row ) => sprintf(
118-
'%d. %s: %s (target reclaim: %s)',
119-
(int) ( $row['priority'] ?? 0 ),
120-
(string) ( $row['action'] ?? 'cleanup' ),
121-
(string) ( $row['command'] ?? '' ),
122-
(string) ( $row['expected_reclaim'] ?? 'unknown' )
123-
),
117+
static function ( $row ): string {
118+
$commands = array_filter(
119+
array(
120+
'preview' => (string) ( $row['preview_command'] ?? $row['command'] ?? '' ),
121+
'apply' => (string) ( $row['apply_command'] ?? '' ),
122+
)
123+
);
124+
$command_text = implode(
125+
'; ',
126+
array_map(
127+
static fn( string $label, string $command ): string => sprintf('%s: %s', $label, $command),
128+
array_keys($commands),
129+
array_values($commands)
130+
)
131+
);
132+
133+
return sprintf(
134+
'%d. %s: %s (target reclaim: %s)',
135+
(int) ( $row['priority'] ?? 0 ),
136+
(string) ( $row['action'] ?? 'cleanup' ),
137+
$command_text,
138+
(string) ( $row['expected_reclaim'] ?? 'unknown' )
139+
);
140+
},
124141
(array) ( $disk_budget['cleanup_recommendations'] ?? array() )
125142
);
126143
return new \WP_Error(

inc/Workspace/WorktreeDiskBudget.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,20 +199,24 @@ private static function cleanup_recommendations( ?int $free_bytes, int $effectiv
199199
'expected_reclaim_bytes' => $target_reclaim,
200200
'expected_reclaim' => $target_human,
201201
'command' => 'studio wp datamachine-code workspace worktree cleanup-artifacts --dry-run --sort=size',
202+
'preview_command' => 'studio wp datamachine-code workspace worktree cleanup-artifacts --dry-run --sort=size',
202203
),
203204
array(
204205
'priority' => 2,
205-
'action' => 'apply reviewed cleanup-eligible worktrees',
206+
'action' => 'review and apply bounded cleanup-eligible worktrees',
206207
'expected_reclaim_bytes' => $target_reclaim,
207208
'expected_reclaim' => $target_human,
208209
'command' => 'studio wp datamachine-code workspace worktree bounded-cleanup-eligible-apply --dry-run --limit=25',
210+
'preview_command' => 'studio wp datamachine-code workspace worktree bounded-cleanup-eligible-apply --dry-run --limit=25',
211+
'apply_command' => 'studio wp datamachine-code workspace worktree bounded-cleanup-eligible-apply --limit=25',
209212
),
210213
array(
211214
'priority' => 3,
212-
'action' => 'generate combined emergency plan',
215+
'action' => 'generate combined emergency cleanup report',
213216
'expected_reclaim_bytes' => $target_reclaim,
214217
'expected_reclaim' => $target_human,
215218
'command' => 'studio wp datamachine-code workspace worktree emergency-cleanup --format=json',
219+
'preview_command' => 'studio wp datamachine-code workspace worktree emergency-cleanup --format=json',
216220
),
217221
);
218222
}

tests/worktree-disk-budget.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
if ( ! defined('ABSPATH') ) {
6+
define('ABSPATH', __DIR__ . '/fixtures/');
7+
}
8+
9+
require_once dirname(__DIR__) . '/vendor/autoload.php';
10+
11+
use DataMachineCode\Workspace\WorktreeDiskBudget;
12+
13+
function assert_true( bool $condition, string $message ): void {
14+
if ( ! $condition ) {
15+
throw new RuntimeException($message);
16+
}
17+
}
18+
19+
$gib = 1073741824;
20+
21+
try {
22+
$budget = WorktreeDiskBudget::evaluate(
23+
array(
24+
'workspace_path' => '/tmp/dmc-test-workspace',
25+
'free_bytes' => 2 * $gib,
26+
'total_bytes' => 100 * $gib,
27+
'worktree_count' => 12,
28+
),
29+
array(
30+
'warn_free_bytes' => 20 * $gib,
31+
'refuse_free_bytes' => 10 * $gib,
32+
'warn_free_percent' => 15.0,
33+
'refuse_free_percent' => 10.0,
34+
'warn_worktree_count' => 100,
35+
)
36+
);
37+
38+
assert_true('refused' === $budget['status'], 'low free space should refuse worktree creation');
39+
assert_true(8 * $gib === $budget['cleanup_recommendations'][0]['expected_reclaim_bytes'], 'recommendations should include the bytes needed to clear the effective floor');
40+
assert_true(str_contains(WorktreeDiskBudget::format_summary($budget), '2.0 GiB (2.0%) free'), 'summary should include current free GiB and percent');
41+
42+
$commands = array_column($budget['cleanup_recommendations'], 'command');
43+
assert_true(in_array('studio wp datamachine-code workspace worktree cleanup-artifacts --dry-run --sort=size', $commands, true), 'artifact cleanup preview command is missing');
44+
assert_true(in_array('studio wp datamachine-code workspace worktree bounded-cleanup-eligible-apply --dry-run --limit=25', $commands, true), 'bounded cleanup-eligible dry-run command is missing');
45+
assert_true(in_array('studio wp datamachine-code workspace worktree emergency-cleanup --format=json', $commands, true), 'emergency cleanup report command is missing');
46+
47+
$bounded = $budget['cleanup_recommendations'][1];
48+
assert_true('studio wp datamachine-code workspace worktree bounded-cleanup-eligible-apply --dry-run --limit=25' === $bounded['preview_command'], 'bounded cleanup preview command is missing');
49+
assert_true('studio wp datamachine-code workspace worktree bounded-cleanup-eligible-apply --limit=25' === $bounded['apply_command'], 'bounded cleanup apply command is missing');
50+
51+
fwrite(STDOUT, "worktree-disk-budget ok\n");
52+
} catch (Throwable $e) {
53+
fwrite(STDERR, $e->getMessage() . "\n");
54+
exit(1);
55+
}

0 commit comments

Comments
 (0)