refactor: simplify JSON output patterns - #1998
Merged
Merged
Conversation
- Extract `partition_log_files_json()` to deduplicate log partitioning between `handle_logs_get` and `handle_state_show_json` - Move `remove_result_to_json` to `RemoveResult::to_json()` method - Add `CandidateKind::as_str()` to deduplicate prune JSON match blocks - Restructure multi-remove JSON to post-execution iterator chain - Add `diagnostic` key to `logs get --format=json` (was missing) Co-Authored-By: Claude <noreply@anthropic.com>
worktrunk-bot
approved these changes
Apr 8, 2026
max-sixty
added a commit
that referenced
this pull request
Apr 8, 2026
Four targeted fixes identified as follow-ups from the `--format=json` rollout (#1969, #1998): 1. **Sort stability in `partition_log_files_json`** — added filename tiebreaker to match `render_log_table`'s sort. Without this, entries with identical timestamps could appear in non-deterministic order in JSON output. 2. **for-each `error` field always present on failure** — previously, the `"error"` field only appeared for `SpawnFailed` errors, not `ExitCode`. Consumers parsing the JSON had to handle two shapes. Now both variants include `"error"`: `"exit code N"` for ExitCode, `"spawn failed: ..."` for SpawnFailed, `"killed by signal"` for signal termination. Error formatting lives in `CommandError::Display` impl with a unit test covering all three variants. 3. **Test: `config show --format=json` outside a git repo** — verifies that project path/config are `null` when not in a git repository. 4. **Test: multi-remove `--format=json` with current worktree** — covers the `plans.current` code path where the current worktree appears in the multi-remove target list (deferred to last in output). > _This was written by Claude Code on behalf of Maximilian Roos_ --------- Co-authored-by: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Deduplicates and reorganizes JSON output construction across several commands. Four changes:
Extract
partition_log_files_json()instate.rs— the log directory read/sort/partition logic was duplicated betweenhandle_logs_getandhandle_state_show_json. Now a single function serves both. Also adds thediagnostickey tologs get --format=jsonoutput, which was previously missing (diagnostic files were silently merged intohook_output).Move
remove_result_to_json→RemoveResult::to_json()— was a free function inmain.rs, now lives on the type inworktree/types.rs.Add
CandidateKind::as_str()instep_commands.rs— replaces two identical 5-line match blocks converting the enum to JSON string values.Restructure multi-remove JSON in
main.rs— replaces interleavedif json_mode { json_items.push(...) }in each execution loop with a single post-execution iterator chain.Net -12 lines.