Skip to content

refactor: simplify JSON output patterns - #1998

Merged
max-sixty merged 1 commit into
mainfrom
json-output
Apr 8, 2026
Merged

refactor: simplify JSON output patterns#1998
max-sixty merged 1 commit into
mainfrom
json-output

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Deduplicates and reorganizes JSON output construction across several commands. Four changes:

  1. Extract partition_log_files_json() in state.rs — the log directory read/sort/partition logic was duplicated between handle_logs_get and handle_state_show_json. Now a single function serves both. Also adds the diagnostic key to logs get --format=json output, which was previously missing (diagnostic files were silently merged into hook_output).

  2. Move remove_result_to_jsonRemoveResult::to_json() — was a free function in main.rs, now lives on the type in worktree/types.rs.

  3. Add CandidateKind::as_str() in step_commands.rs — replaces two identical 5-line match blocks converting the enum to JSON string values.

  4. Restructure multi-remove JSON in main.rs — replaces interleaved if json_mode { json_items.push(...) } in each execution loop with a single post-execution iterator chain.

Net -12 lines.

This was written by Claude Code on behalf of Maximilian Roos

- 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>
@max-sixty
max-sixty merged commit 7a46401 into main Apr 8, 2026
25 checks passed
@max-sixty
max-sixty deleted the json-output branch April 8, 2026 03:09
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants