Skip to content

Add a retry-setup hint when pre-start fails during wt switch --create - #4705

Merged
max-sixty merged 3 commits into
mainfrom
fix/pre-start-retry-hint
Nov 20, 2026
Merged

max-sixty merged 3 commits into
mainfrom
fix/pre-start-retry-hint

Conversation

@worktrunk-bot

@worktrunk-bot worktrunk-bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

When pre-start fails during wt switch --create, the error now names the command that retries setup, with the same variables the switch used:

✗ pre-start project:env failed (exit code 1)
↳ To retry setup, run wt -C ~/code/myproject.feature-auth hook pre-start --base-worktree-path=/home/me/code/myproject

The failed switch leaves the new branch and worktree in place, and a later wt switch <branch> only runs post-switch because the worktree already exists, so nothing re-runs setup unless the user knows to. This failure is the one point where wt knows setup didn't finish, so the hint goes here instead of on the later Branch already exists collision (#4172 put it there, so it showed on every ordinary branch conflict).

At this site the hint replaces the --no-hooks suggestion. Re-running wt switch --create --no-hooks would stop at Branch … already exists, which is the trap #4143 reported. Every other hook failure still shows the --no-hooks hint as before.

A manual wt hook pre-start binds base and base_worktree_path to the worktree it runs in and knows no PR. So the retry passes the switch's own --base, --base-worktree-path, --pr-number, and --pr-url, but only those the frozen pre-start templates reference, because wt hook forwards an unreferenced --KEY=VALUE into {{ args }}. With that, a hook like cp {{ base_worktree_path }}/.env . reruns the same setup. A hook that references none of them gets a bare wt -C <path> hook pre-start.

Verified by:

  • test_user_pre_start_failure_retry_reruns_same_setup: a hook copies a file from {{ base_worktree_path }} and fails; running the hinted command afterwards copies from the original worktree. Without the flag, the manual run would read from the new worktree.
  • A unit test of the flag selection, including shell quoting.
  • The inline error-render snapshot and the two pre-start failure integration snapshots.

Closes #4143

When pre-start fails, wt switch --create leaves the new worktree behind,
and switching to it again only runs post-switch, so nothing re-runs setup.
The failure now adds a hint naming the command that does:

  ↳ To retry setup, run wt -C <path> hook pre-start

Closes #4143
@worktrunk-bot worktrunk-bot added the automated-fix Automated CI fix label Nov 5, 2026

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The suggested wt hook pre-start doesn't re-run setup with the variables the failed run had. build_manual_hook_template_vars (in src/commands/hook_commands.rs) binds base to the current worktree's branch and base_worktree_path to the current worktree. Run via -C <new worktree>, both point at the new worktree. The switch passed the real base branch and the source worktree through with_base_strs, plus pr_number/pr_url from with_pr, and none of those reach the manual run. So a pre-start hook like cp {{ base_worktree_path }}/.env . copies the new worktree onto itself, and one that branches on {{ pr_number }} takes a different path. For those configs, "To retry setup" points at a command that runs different setup.

Hooks that don't reference those variables, including the documented wt step copy-ignored pattern, are unaffected. So this is a judgment call on how exact the hint should be. One option is to append --base=…/--base_worktree_path=… shorthand (and the PR vars) when the plan's templates reference them. That makes the hint longer, and it depends on shorthand bindings taking precedence over the manual defaults, which I haven't checked. The other is to word the hint so it doesn't promise an identical retry. I'm leaving that choice to a maintainer rather than pushing either one.

@max-sixty

Copy link
Copy Markdown
Owner

don't we generally dissuade having multiple lines of hints?

Hints with several suggestions share one line joined by a semicolon,
with the more commonly needed command last for easy copying:

  ↳ To skip pre-start hooks, re-run with --no-hooks; to retry setup, run wt -C <path> hook pre-start
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Yes — the output guidelines combine multiple suggestions into one hint with a semicolon, with the more commonly needed command last. Pushed 20b6157, which folds it into a single line:

✗ pre-start project:deps failed (exit code 1)
↳ To skip pre-start hooks, re-run with --no-hooks; to retry setup, run wt -C ~/code/myproject.feature-auth hook pre-start

The line is fairly long. If that's too much, the other option is to drop the --no-hooks half in this case, since re-running wt switch --create with --no-hooks fails anyway once the branch has been kept.

max-sixty pushed a commit that referenced this pull request Nov 7, 2026
…ing hint (#4725)

`writing-user-outputs` explained how to join two suggestions into one
hint, but never said to join them instead of stacking a second hint
line. This edit states that rule. Without it, a session that loaded the
skill still wrote a second `hint_message` line on #4705, and only
maintainer review caught it, since no test or lint checks how many hint
lines a message has. With the rule, a second suggestion goes into the
existing hint with a semicolon, most commonly needed command last.

The rule covers suggestions only. Context hints already sit beside a
suggestion hint in `GitError::WorktreeRemovalFailed` ("Remaining in
directory") and `GitError::WorktreeCreationFailed` ("Failed command"),
and those stay as they are.

<details><summary>Evidence</summary>

- The session that wrote #4705's two-line hint was mention run
41976059785. It loaded `writing-user-outputs` before writing the change.
- A maintainer then asked on #4705 whether multiple hint lines are
generally dissuaded. The bot folded the hint into one line in 20b6157.
- Observation logged in the review-runs tracker #4657.

</details>

---------

Co-authored-by: worktrunk-bot <[email protected]>
max-sixty pushed a commit that referenced this pull request Nov 18, 2026
…4841)

When `wt switch --create` failed after `git worktree add -b` had already
written the branch, the leftover-branch hint suggested a bare `wt switch
<branch>`. Following it created the worktree at the `worktree-path`
template location instead of the `--path` given, and skipped the
`--execute` command. `WorktreeCreationFailed` is raised in
`execute_switch`, whose errors weren't wrapped in `WithSwitchSuggestion`
the way `plan_switch`'s are, so the hint had no flags to draw on.

`SwitchPipeline::run` now wraps that variant with the switch context,
and the hint applies it. The hint becomes e.g. `wt switch stranded
--path=blocked/stranded --execute='echo hi' -- extra`. `--create` is not
in the context, so it stays out of the suggestion, which is correct now
that the branch exists. Other `execute_switch` errors pass through
unchanged, so their context chains and downcasts are untouched.

`test_switch_create_leftover_branch_hint_keeps_flags` fails on `main`
(the hint ends at `wt switch stranded`) and passes with this change. The
existing leftover-branch test still covers the no-flags form.

#4705 adds a hint for pre-start failures on the same `execute_switch`
path. If it suggests a `wt switch` command, it can reuse this wrapping.

Closes #4839 — automated triage

---------

Co-authored-by: worktrunk-bot <[email protected]>
@max-sixty

Copy link
Copy Markdown
Owner

@worktrunk-bot two changes to the hint:

  • When pre-start fails during wt switch --create, drop the --no-hooks half. The branch exists by then, so re-running --create --no-hooks stops at "Branch … already exists", which is the trap wt switch <branch> skips pre-start, so the suggested recovery from a failed setup reports success on an unusable worktree #4143 reported.
  • Make the retry exact. wt hook binds --KEY=VALUE over built-in variables (wt hook --help, "Passing values"), so pass the switch's --base and --base-worktree-path, plus --pr-number and --pr-url when it had them. Pass only the ones the pre-start templates reference, since an unreferenced --KEY=VALUE is forwarded into {{ args }}. Then a hook like cp {{ base_worktree_path }}/.env . reruns the same setup.

This was written by Claude Code on behalf of max-sixty

When pre-start fails during `wt switch --create`, the branch and worktree
are kept, so re-running the switch with --no-hooks stops at "Branch already
exists". The hint now names only the retry command.

A manual `wt hook pre-start` binds base and base_worktree_path to the
worktree it runs in and has no PR, so the retry passes the switch's own
base, base_worktree_path, pr_number and pr_url as --KEY=VALUE — only those
the frozen pre-start templates reference, since an unreferenced key would
forward into {{ args }}.
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

I made both changes in ed7b7b7:

  • When pre-start fails during wt switch --create, the hint now names only the retry command. The --no-hooks half is gone at that site. Other hook failures keep it.
  • The retry passes the switch's --base, --base-worktree-path, --pr-number, and --pr-url, but only those the frozen pre-start templates reference. Every other --KEY=VALUE is left off, so nothing ends up in {{ args }}. Values are shell-quoted when needed, for example '--base-worktree-path=/path/with space'.

A new integration test sets up a hook that copies from {{ base_worktree_path }} and fails. After the precondition is fixed, the hinted command copies from the original worktree, not from the new one.

@max-sixty
max-sixty merged commit 5828070 into main Nov 20, 2026
36 checks passed
@max-sixty
max-sixty deleted the fix/pre-start-retry-hint branch November 20, 2026 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automated-fix Automated CI fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wt switch <branch> skips pre-start, so the suggested recovery from a failed setup reports success on an unusable worktree

2 participants