Skip to content

fix: return one complete JSON result for switch - #4345

Merged
max-sixty merged 8 commits into
mainfrom
codex/switch-json-result
Oct 21, 2026
Merged

max-sixty merged 8 commits into
mainfrom
codex/switch-json-result

Conversation

@max-sixty

@max-sixty max-sixty commented Oct 2, 2026 •

Copy link
Copy Markdown
Owner

JSON switches now emit one result after synchronous work, including any --execute command, finishes. Accepted invocations return the existing success fields, or an object with an error string when target discovery, configuration, directory directives, or execution fails. Execute output goes to stderr, and child errors retain exit and signal metadata. Typed Git diagnostics retain captured Git details and recovery hints, including commands for a branch left behind after failed worktree creation. Argument errors and picker cancellation produce no JSON; background hooks remain independent.

Validation: the full project gate passed (5,028 tests, one skipped), plus focused checks covering early and late failures, child output and signals, picker behavior, and plain output. Generated help, documentation synchronization and build, and independent review passed. The change also removes a synthetic stdin write from an existing noninteractive alias-refusal test that raced with process exit.

This was written by Codex on behalf of max-sixty

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the cause of a failure lives in a typed error's fields, the JSON error loses it. GitError::WorktreeCreationFailed displays only its title, and the git output and leftover-branch hint live in fields that only Diagnostic::render shows. So the {error:#} fallback in handle_command_failure gives the caption and nothing else. I checked this against the built branch:

$ wt switch --create 'bad..name' --format=json --no-cd --yes 2>/dev/null
{"error":"Failed to create worktree for bad..name from base main"}

The same thing happens when worktree-path points through a regular file, the case test_switch_create_names_branch_left_by_failed_worktree_add covers. The JSON says creation failed but not why, and it doesn't mention the branch that git left behind. The comment on that branch says the outer caption alone shouldn't hide the actual cause, and here it does. A tool that reads only stdout has nothing to act on. One fix would be an ANSI-stripped render() for errors that implement Diagnostic. Another would be a dedicated plain-text detail for the JSON path.

codecov/patch is failing on one line: the CommandError::find_in branch in handle_command_failure (inline below).

Comment thread src/main.rs Resolved
Comment thread src/cli/mod.rs Resolved
worktrunk-bot
worktrunk-bot previously approved these changes Oct 2, 2026
@max-sixty

Copy link
Copy Markdown
Owner Author

@worktrunk-bot Please refresh this PR branch against current main and resolve its mechanical merge conflicts so it can get current CI. Preserve the proposed behavior and the current draft state, run the focused checks for the resulting diff, and report any conflict that requires a semantic or product decision.

This requests branch maintenance; the outstanding maintainer review remains pending.

This was written by Codex on behalf of max-sixty

Conflicts:
- src/git/error.rs: main's interrupt_signal already walks the whole
  error chain (covering AlreadyDisplayed contexts) with the new
  physical_signal/cancellation fields; took main's version and kept the
  branch's AlreadyDisplayed unit assertion.
- tests/integration_tests/step_alias.rs: main moved
  test_alias_approval_decline to approval_pty.rs, superseding the
  branch's stdin-race fix; took main's side.

Integration fixups:
- Pass the new switch_json argument at the two handle_command_failure
  call sites main added (help pager: false; signal-forwarder startup:
  switch_json).
- Update JSON switch tests for main's behavior: upfront branch-name
  validation, "Failed to execute <program>" spawn errors, "exit code N"
  child exits, and wt re-raising SIGTERM/SIGINT instead of exiting
  143/130.

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

codecov/patch is failing on one line, but the gap isn't new. The merge passes switch_json to the CommandOperation::start() error arm in main (src/main.rs#L1324), so Codecov now counts that line as part of the patch. The same arm is already uncovered on main, at line 1285 in the bf1d348c4 report. That arm only runs if a command operation is already active or the signal handler fails to register, so no test I can see reaches it. Under the AGENTS.md Coverage rule, a failing patch check needs a maintainer's explicit approval before merge, so I've withdrawn my earlier approval until a maintainer makes that call.

I found no problems in the conflict resolutions or test updates, and test (linux|macos|windows) pass at this head.

@worktrunk-bot
worktrunk-bot dismissed their stale review October 14, 2026 16:05

codecov/patch fails at the current head and needs a maintainer's explicit approval to merge.

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

I merged current main into this branch and pushed it as 2412272. CI passes at that head except codecov/patch, and none of the conflicts needed a product decision. The branch behavior is unchanged, and the PR's draft state is as it was.

Conflicts

  • src/git/error.rs (interrupt_signal): main now walks the whole error chain using the new physical_signal/cancellation fields. That already covers the AlreadyDisplayed case the branch fixed, so I took main's version and kept the branch's unit assertion for it.
  • tests/integration_tests/step_alias.rs: main moved test_alias_approval_decline into approval_pty.rs, which replaces the branch's fix for the stdin race. I took main's side.

Changes needed for the merge to build

  • Main added two handle_command_failure call sites, and they now pass the branch's switch_json argument. The help-pager failure passes false. The CommandOperation::start() failure passes switch_json, the same as the git-version check just above it, so a switch that fails there still emits a JSON error.
  • I updated five assertions in the JSON switch tests to match behavior that changed on main. In each case wt still emits exactly one {"error": …} object:
    • bad..name is now rejected up front with Invalid branch name bad..name rather than by Git. The JSON coverage for Git rejecting a creation late is still in test_switch_create_names_branch_left_by_failed_worktree_add.
    • Spawn failures now read Failed to execute <program> @ <path>.
    • Child exits now read exit code 7.
    • wt now re-raises SIGTERM and SIGINT instead of exiting with 143 or 130. The tests now assert status.signal(), as main's own tests do.

codecov/patch
The one uncovered line is the CommandOperation::start() failure arm at src/main.rs:1324. It is also uncovered on main (line 1285 at bf1d348c4, per Codecov's file report), and this merge only added the argument to it. Whether that gap is acceptable for this PR is your call.

Local checks: build, cargo fmt --check, clippy with --all-features -D warnings, and 507 integration tests (switch, step_alias, approval_pty, help, test_docs_are_in_sync) all pass. Of the lib/bin unit tests, two fail here, and both bind a Unix socket, which this sandbox blocks. They are in src/copy.rs and src/git/repository/tests.rs, which the PR doesn't touch, and they pass in CI.

max-sixty and others added 2 commits October 20, 2026 14:59
One rule replaces four message branches: the error string is what stderr shows,
or the child's exit when the child spoke for itself.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
worktrunk-bot
worktrunk-bot previously approved these changes Oct 20, 2026

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The diff itself looks good. Moving the JSON error onto format_command_error keeps the git output, typed-diagnostic hints, and context chain that the removed branches carried. It also stops a diagnostic that renders empty from producing "error": "".

codecov/patch is still failing at this head, on one line: the CommandOperation::start() error arm (src/main.rs#L1316). This is the same gap the earlier review described. The arm is uncovered on main too, and the PR only adds the switch_json argument to it. AGENTS.md (Coverage) requires explicit maintainer approval to merge with a failing patch check, so I've withdrawn my approval until a maintainer makes that call.

@worktrunk-bot
worktrunk-bot dismissed their stale review October 20, 2026 22:43

codecov/patch failing — needs maintainer approval per AGENTS.md Coverage.

@max-sixty
max-sixty merged commit 93beb06 into main Oct 21, 2026
38 of 39 checks passed
@max-sixty
max-sixty deleted the codex/switch-json-result branch October 21, 2026 00:13
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