Skip to content

feat(switch): expose destination branch in pre-switch hooks - #1497

Merged
max-sixty merged 4 commits into
mainfrom
fix/pre-switch-hook-destination-branch
Mar 14, 2026
Merged

max-sixty merged 4 commits into
mainfrom
fix/pre-switch-hook-destination-branch

Conversation

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Summary

  • Moves branch resolution (plan_switch) before pre-switch hook execution, so {{ branch }} in pre-switch hooks is the destination branch rather than the source
  • Applies to both wt switch and the interactive picker (wt switch without args)
  • Adds a test verifying {{ branch }} expands to the destination

Discussed in #1494 — {{ branch }} was chosen over {{ target }} per this comment.

Test plan

  • All 5 pre-switch hook tests pass
  • All 62 user hook tests pass
  • CI passes on all platforms

🤖 Generated with Claude Code

… hooks

Move branch resolution (plan_switch) before pre-switch hook execution so
hooks can inspect or validate the target branch. Previously, {{ branch }}
in pre-switch hooks was the source branch; now it's the destination.

Closes #1494 (discussion)

Co-Authored-By: Claude Opus 4.6 <[email protected]>

@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 reordering logic is correct — plan_switch is side-effect-free (no state mutation), so moving it before hooks is safe. The behavioral change that hooks now skip when branch resolution fails (e.g., invalid branch) is actually an improvement over the previous behavior.

One issue: the hook documentation now contradicts the code.

Comment thread src/commands/handle_switch.rs Resolved
Comment thread tests/integration_tests/user_hooks.rs Resolved
Co-Authored-By: Claude Opus 4.6 <[email protected]>
@max-sixty

Copy link
Copy Markdown
Owner

@worktrunk-bot please fix

max-sixty pushed a commit that referenced this pull request Mar 14, 2026
## Summary

- Explicitly bans `gh pr checks --watch` in the `running-in-ci` skill,
alongside the existing `gh run watch` ban

## Evidence

[Run
23077253025](https://github.com/max-sixty/worktrunk/actions/runs/23077253025)
(`claude-review` for PR #1497) used `gh pr checks 1497 --watch` with a
600000ms timeout, which hung for ~55 minutes until the workflow was
cancelled. Session log excerpt:

```
Bash: {"command":"gh pr checks 1497 --watch 2>&1 | tail -5","description":"Wait for CI checks to complete","timeout":600000}
```

After the `--watch` call timed out/was cancelled, the bot tried `sleep
60 && gh pr checks 1497` — then the workflow was cancelled entirely.

## Root cause

The `running-in-ci` skill says:

> Avoid `gh run watch` — it can hang indefinitely.

But `gh pr checks --watch` has the same blocking behavior and wasn't
explicitly banned. The bot followed the letter of the rule (didn't use
`gh run watch`) while violating its spirit (used an equivalent blocking
command).

This is a recurrence of the pattern from #1402 and #1316 — CI polling
commands that block indefinitely.

## Test plan

- [x] Verify the updated text is clear and covers both commands

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: worktrunk-bot <[email protected]>
Co-authored-by: Claude Opus 4.6 <[email protected]>
Update the pre-switch hook documentation to reflect the new behavior:
{{ branch }} is now the destination branch, resolved before hook execution.
Also change assert! to assert_eq! for better failure output in tests.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Fixed both review comments:

  1. Updated pre-switch hook docs in src/cli/mod.rs (line 1121) — now says "after branch resolution but before worktree creation" and documents that {{ branch }} reflects the destination. The doc sync test propagated this to docs/content/hook.md and skills/worktrunk/reference/hook.md.

  2. Changed assert! to assert_eq! in the test for better failure output.

CI is green on all platforms (linux, macos, windows), plus lint, code-coverage, and check-docs.

worktrunk-bot added a commit that referenced this pull request Mar 14, 2026
… ordering in review-pr skill

Two recurring behavioral issues in review sessions:

1. Bot attempts `gh pr review --approve` on self-authored PRs despite the skill
   saying not to. Root cause: the approval instructions follow the self-authored
   caveat without an explicit gate, so the model reads past the caveat. Fix:
   add bold "Never attempt" instruction and gate approval bullets under
   "For non-self-authored PRs" heading.

2. Bot resolves review threads before CI passes (step 6 before step 5). The
   ordering was implicit ("After submitting the review"). Fix: add explicit
   "Only after CI passes in step 5" prerequisite to step 6.

Evidence:
- Self-approval: run 23095864243 (PR #1520 review) — bot tried --approve,
  got "Can not approve your own pull request", fell back to comment.
  Recurrence of #1254.
- Thread ordering: run 23095864428 (PR #1497 re-review) — bot resolved
  threads at step 5, then started CI monitoring at step 6.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Comment thread docs/content/hook.md Outdated
## pre-switch

Runs before every `wt switch` — before branch validation or worktree creation. Useful for ensuring the repository is up to date before switching. Template variables reflect the current worktree (the source), not the destination. Failure aborts the switch.
Runs before every `wt switch` — after branch resolution but before worktree creation. `{{ branch }}` reflects the destination branch, so hooks can inspect or validate the target. Failure aborts the switch.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

OK, I think this is slightly off — actually we should include branch but before resolution; is that viable?

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.

Yes, that's viable. We'd pass the raw branch argument (the user's input) to the hook instead of the resolved one, and move the hooks back to before plan_switch.

The tradeoff is that {{ branch }} would be the raw input rather than the resolved name, so it'd be - instead of the actual previous branch, pr:123 instead of the PR's branch name, @ instead of the current branch, etc. For the common case (wt switch feature-branch) it's identical.

This would also restore the original benefit that pre-switch hooks like git pull run before we validate the branch exists — which matters for --create off a remote ref that hasn't been fetched yet.

Want me to make that change? I'd:

  1. Move hooks back to before plan_switch
  2. Pass the raw branch string as {{ branch }} in the hook context
  3. Update the docs to reflect "user-supplied branch name (before resolution)"

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

yes

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.

Done — moved hooks back to before plan_switch and {{ branch }} now receives the raw user input (before resolution). Pushed in 9c24d2f.

Move pre-switch hooks back to before `plan_switch` so they run before
branch resolution. `{{ branch }}` now receives the user's raw input
rather than the resolved branch name.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

@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.

Code looks correct — the reordering is clean and both call sites (handle_switch and select/mod.rs) consistently pass the raw branch argument before resolution.

One thing to fix before merging: the PR description is stale. It says "Moves branch resolution (plan_switch) before pre-switch hook execution" but the final implementation (after the review iteration) does the opposite — hooks now run before plan_switch, passing the raw user input. Worth updating so the merge commit summary isn't misleading.

@max-sixty
max-sixty merged commit e47778d into main Mar 14, 2026
25 checks passed
@max-sixty
max-sixty deleted the fix/pre-switch-hook-destination-branch branch March 14, 2026 23:29
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