Skip to content

Commit 5361afe

Browse files
Compact worktree cleanup operator JSON
1 parent 8c4fa27 commit 5361afe

3 files changed

Lines changed: 80 additions & 4 deletions

File tree

inc/Cli/Commands/WorkspaceCommand.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5346,6 +5346,9 @@ private function render_worktree_metadata_reconciliation_result( array $result,
53465346
private function render_worktree_active_no_signal_report_result( array $result, array $assoc_args ): void {
53475347
$format = isset($assoc_args['format']) ? (string) $assoc_args['format'] : 'table';
53485348
if ( 'json' === $format ) {
5349+
if ( empty($assoc_args['verbose']) ) {
5350+
$result = WorkspaceCompactOutput::cleanup_result($result);
5351+
}
53495352
$this->renderer()->json($result);
53505353
return;
53515354
}
@@ -5436,6 +5439,9 @@ private function render_worktree_active_no_signal_report_result( array $result,
54365439
private function render_worktree_active_no_signal_finalized_apply_result( array $result, array $assoc_args ): void {
54375440
$format = isset($assoc_args['format']) ? (string) $assoc_args['format'] : 'table';
54385441
if ( 'json' === $format ) {
5442+
if ( empty($assoc_args['verbose']) ) {
5443+
$result = WorkspaceCompactOutput::cleanup_result($result);
5444+
}
54395445
$this->renderer()->json($result);
54405446
return;
54415447
}
@@ -5526,6 +5532,9 @@ private function render_worktree_active_no_signal_finalized_apply_result( array
55265532
private function render_worktree_active_no_signal_equivalent_clean_apply_result( array $result, array $assoc_args ): void {
55275533
$format = isset($assoc_args['format']) ? (string) $assoc_args['format'] : 'table';
55285534
if ( 'json' === $format ) {
5535+
if ( empty($assoc_args['verbose']) ) {
5536+
$result = WorkspaceCompactOutput::cleanup_result($result);
5537+
}
55295538
$this->renderer()->json($result);
55305539
return;
55315540
}
@@ -5616,6 +5625,9 @@ private function render_worktree_active_no_signal_equivalent_clean_apply_result(
56165625
private function render_worktree_active_no_signal_merged_apply_result( array $result, array $assoc_args ): void {
56175626
$format = isset($assoc_args['format']) ? (string) $assoc_args['format'] : 'table';
56185627
if ( 'json' === $format ) {
5628+
if ( empty($assoc_args['verbose']) ) {
5629+
$result = WorkspaceCompactOutput::cleanup_result($result);
5630+
}
56195631
$this->renderer()->json($result);
56205632
return;
56215633
}
@@ -5706,6 +5718,9 @@ private function render_worktree_active_no_signal_merged_apply_result( array $re
57065718
private function render_worktree_active_no_signal_remote_clean_apply_result( array $result, array $assoc_args ): void {
57075719
$format = isset($assoc_args['format']) ? (string) $assoc_args['format'] : 'table';
57085720
if ( 'json' === $format ) {
5721+
if ( empty($assoc_args['verbose']) ) {
5722+
$result = WorkspaceCompactOutput::cleanup_result($result);
5723+
}
57095724
$this->renderer()->json($result);
57105725
return;
57115726
}
@@ -5945,7 +5960,7 @@ private function flatten_artifact_cleanup_rows( array $rows ): array {
59455960
private function render_worktree_bounded_cleanup_eligible_apply_result( array $result, array $assoc_args ): void {
59465961
$format = isset($assoc_args['format']) ? (string) $assoc_args['format'] : 'table';
59475962
if ( 'json' === $format ) {
5948-
$report = ! empty($assoc_args['verbose']) ? $result : $this->compact_worktree_bounded_cleanup_eligible_apply_json($result);
5963+
$report = ! empty($assoc_args['verbose']) ? $result : WorkspaceCompactOutput::cleanup_result($result);
59495964
$this->renderer()->json($report);
59505965
return;
59515966
}

inc/Cli/WorkspaceCompactOutput.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class WorkspaceCompactOutput {
1515

1616
public static function cleanup_result( array $result ): array {
1717
$summary = (array) ( $result['summary'] ?? array() );
18-
$candidates = (array) ( $result['candidates'] ?? $result['artifact_candidates'] ?? array() );
19-
$removed = (array) ( $result['removed'] ?? $result['removed_worktrees'] ?? $result['removed_artifacts'] ?? array() );
18+
$candidates = (array) ( $result['candidates'] ?? $result['artifact_candidates'] ?? $result['worktree_candidates'] ?? $result['rows'] ?? $result['planned'] ?? array() );
19+
$removed = (array) ( $result['removed'] ?? $result['removed_worktrees'] ?? $result['removed_artifacts'] ?? $result['written'] ?? array() );
2020
$skipped = (array) ( $result['skipped'] ?? array() );
2121

2222
return self::filter_empty(
@@ -144,7 +144,7 @@ public static function lock_result( array $result ): array {
144144

145145
private static function row_counts( array $result ): array {
146146
$counts = array();
147-
foreach ( array( 'candidates', 'artifact_candidates', 'worktree_candidates', 'removed', 'removed_artifacts', 'removed_worktrees', 'skipped', 'written', 'proposals', 'pass_results' ) as $key ) {
147+
foreach ( array( 'candidates', 'artifact_candidates', 'worktree_candidates', 'rows', 'planned', 'removed', 'removed_artifacts', 'removed_worktrees', 'written', 'skipped', 'proposals', 'pass_results' ) as $key ) {
148148
if ( isset( $result[ $key ] ) && is_array( $result[ $key ] ) ) {
149149
$counts[ $key ] = count( $result[ $key ] );
150150
}
@@ -204,6 +204,14 @@ private static function next_commands( array $result, array $summary ): array {
204204
$commands[] = (string) $summary[ $field ];
205205
}
206206
}
207+
foreach ( array( 'pagination', 'continuation' ) as $bucket ) {
208+
if ( ! empty($result[ $bucket ]['next_command']) ) {
209+
$commands[] = (string) $result[ $bucket ]['next_command'];
210+
}
211+
if ( ! empty($summary[ $bucket ]['next_command']) ) {
212+
$commands[] = (string) $summary[ $bucket ]['next_command'];
213+
}
214+
}
207215
$deduped = array();
208216
$seen = array();
209217
foreach ( $commands as $command ) {

tests/workspace-compact-output.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,59 @@ function compact_output_large_rows( int $count ): array {
7171
compact_output_assert(count((array) ( $cleanup['samples']['skipped'] ?? array() )) <= 5, 'Compact cleanup output must sample skipped rows.');
7272
compact_output_assert(! empty($cleanup['next_commands']), 'Compact cleanup output must preserve next commands.');
7373

74+
$active_report = WorkspaceCompactOutput::cleanup_result(
75+
array(
76+
'success' => true,
77+
'mode' => 'active_no_signal_report',
78+
'rows' => $large_rows,
79+
'summary' => array(
80+
'total_active_no_signal' => 40,
81+
'inspected' => 40,
82+
'by_suggested_action' => array( 'remote_tracking_clean' => 40 ),
83+
),
84+
'pagination' => array(
85+
'total' => 80,
86+
'offset' => 0,
87+
'limit' => 40,
88+
'next_offset' => 40,
89+
'next_command' => 'studio wp datamachine-code workspace worktree active-no-signal-report --limit=40 --offset=40 --format=json',
90+
),
91+
)
92+
);
93+
94+
compact_output_assert(! isset($active_report['rows']), 'Compact active/no-signal report must omit full rows array.');
95+
compact_output_assert(40 === ( $active_report['row_counts']['rows'] ?? null ), 'Compact active/no-signal report must preserve row count.');
96+
compact_output_assert(40 === ( $active_report['pagination']['next_offset'] ?? null ), 'Compact active/no-signal report must preserve pagination.');
97+
compact_output_assert(in_array('studio wp datamachine-code workspace worktree active-no-signal-report --limit=40 --offset=40 --format=json', (array) ( $active_report['next_commands'] ?? array() ), true), 'Compact active/no-signal report must expose next page command.');
98+
99+
$active_apply = WorkspaceCompactOutput::cleanup_result(
100+
array(
101+
'success' => true,
102+
'mode' => 'active_no_signal_remote_clean_apply',
103+
'dry_run' => true,
104+
'planned' => $large_rows,
105+
'written' => $large_rows,
106+
'skipped' => $large_rows,
107+
'summary' => array(
108+
'inspected' => 40,
109+
'planned' => 40,
110+
'written' => 40,
111+
'skipped' => 40,
112+
'skipped_by_reason' => array( 'not_remote_tracking_clean' => 40 ),
113+
),
114+
'pagination' => array(
115+
'next_command' => 'studio wp datamachine-code workspace worktree active-no-signal-remote-clean-apply --dry-run --limit=40 --offset=40 --format=json',
116+
),
117+
)
118+
);
119+
120+
compact_output_assert(! isset($active_apply['planned']), 'Compact active/no-signal apply must omit full planned array.');
121+
compact_output_assert(! isset($active_apply['written']), 'Compact active/no-signal apply must omit full written array.');
122+
compact_output_assert(40 === ( $active_apply['row_counts']['planned'] ?? null ), 'Compact active/no-signal apply must preserve planned count.');
123+
compact_output_assert(40 === ( $active_apply['row_counts']['written'] ?? null ), 'Compact active/no-signal apply must preserve written count.');
124+
compact_output_assert(40 === ( $active_apply['blockers']['not_remote_tracking_clean']['count'] ?? null ), 'Compact active/no-signal apply must preserve blocker counts from summary.');
125+
compact_output_assert(in_array('studio wp datamachine-code workspace worktree active-no-signal-remote-clean-apply --dry-run --limit=40 --offset=40 --format=json', (array) ( $active_apply['next_commands'] ?? array() ), true), 'Compact active/no-signal apply must expose next page command.');
126+
74127
$locks = WorkspaceCompactOutput::lock_result(
75128
array(
76129
'active' => 2,

0 commit comments

Comments
 (0)