Repository navigation
fix(plugin): reopen an existing worktree from the WorktreeCreate hook - #4487
Conversation
Claude Code 2.1.300 runs plugin WorktreeCreate hooks for `claude --worktree NAME`, including when NAME names a worktree a previous session kept. The hook always passed `--create`, so reopening failed with "Branch NAME already exists". Both the shipped hooks.json command and the `wt config plugins claude hook` handler now drop `--create` when the local branch exists, so `wt switch` returns the existing worktree's path. Closes #4486
worktrunk-bot
left a comment
There was a problem hiding this comment.
The reopen check is "local branch NAME exists", which is wider than "a worktree an earlier claude --worktree session kept". That widens what the shipped WorktreeRemove hook can delete. Take a clean linked worktree the user made by hand, such as wt switch --create feature, or the default-branch worktree in a bare-repo layout. Before this PR, claude --worktree feature failed with Branch feature already exists. Now wt switch feature returns that worktree's path, and Claude Code opens the session there. When the session exits clean, Claude Code fires WorktreeRemove for the worktree (the handle_claude_hook doc comment says it does this "for any clean worktree"). The hook then runs wt remove --foreground <path>, which removes the user's own worktree, including its ignored files (.env, build outputs), and deletes the branch if it is integrated. Only the main worktree is still safe, because wt remove refuses it.
One narrower option: reopen only when the existing worktree is at NAME's worktree-path template location, which is where the hook's own --create put it. That keeps the #4486 fix and excludes most hand-made worktrees. It still changes which sessions reopen, so it's a call for a maintainer to make.
This change is shipped hook automation that can now delete a worktree it used to leave alone, so it's held for maintainer review.
…ove (#4489) Under OpenCode 2, the plugin that `wt config plugins opencode install` writes now registers a `worktrunk` worktree strategy. Worktrees that OpenCode creates and removes then go through `wt switch --create` and `wt remove`, the same way the Claude Code plugin's `WorktreeCreate`/`WorktreeRemove` hooks already route them. This was requested in #4488. I ran the strategy locally against a scratch repo with this branch's `wt` and a fake OpenCode context. I haven't run it inside a real OpenCode 2 session, which CI can't drive. How each callback maps ([`dev/opencode-plugin.ts`](https://github.com/max-sixty/worktrunk/blob/1c906f5599d7a16df832c56c51aa82d13c82bbd3/dev/opencode-plugin.ts)): - **create**: runs `wt switch [--create] <name> [--base <ref>] --no-cd --format=json` in the source checkout and returns `.path`. OpenCode's `branch` input is a [starting ref, not a branch name](https://github.com/anomalyco/opencode/blob/dev/packages/plugin/src/worktree.ts), so `<name>` is the basename of the directory OpenCode suggests (the name the user typed, or a generated slug). The path comes from `worktree-path`, since OpenCode accepts a different directory than the one it suggested. An existing branch is reopened, as in #4487. Passing a starting ref for an existing branch is refused rather than ignored. - **remove**: runs `wt remove --foreground [--force] <dir>`. It does nothing if the checkout is already gone. - **list**: reads `git worktree list --porcelain -z` and reports the first record as `root`. Bare and prunable entries are skipped. Things a maintainer may want to weigh: - **On by default.** OpenCode selects a strategy as soon as a plugin adds one. Every OpenCode 2 user with the plugin installed switches to worktrunk-managed worktrees, and OpenCode's `worktree.directory` setting stops applying. This matches the Claude Code plugin. The docs page says so. - **No force prompt on a dirty worktree.** OpenCode only offers to force when the error is an instance of its own `Worktree.OperationError` ([`operationError`](https://github.com/anomalyco/opencode/blob/dev/packages/core/src/worktree.ts) uses `instanceof`). A standalone plugin file can't import that class: the CLI's runtime shim for `@opencode/plugin` doesn't export `Worktree`. A dirty removal fails with worktrunk's own message instead (`Cannot remove worktree: … has uncommitted changes`), and nothing is deleted. - **Default base.** With no starting ref, the new branch starts from worktrunk's default (the default branch). OpenCode's `git` strategy uses the source checkout's HEAD instead. <details><summary>Local check</summary> A Node harness imported the plugin, called `setup` with a stub `ctx.worktree.transform`, and ran each callback against a fresh repo with `target/debug/wt` on PATH: - create → `ocrepo.brave-tiger` (the `worktree-path` default), and `list` returned root + that worktree - create with `branch: "brave-tiger"` → a second worktree based on it - create for an existing branch with a starting ref → refused. Without a ref → reopened the existing worktree - remove of a dirty worktree → wt's error. With `force` → removed. A repeat remove → no-op - final `list` → root only `test_docs_are_in_sync` and the 14 `opencode` integration tests pass. </details> Closes #4488 Co-authored-by: worktrunk-bot <[email protected]>
Since Claude Code 2.1.300, plugin
WorktreeCreatehooks run forclaude --worktree NAME, and Claude Code fires the hook again when NAME is a worktree an earlier session kept. The plugin always ranwt switch --create NAME, so reopening failed withBranch NAME already exists. The hook now omits--createwhen local branch NAME exists, sowt switchprints the existing worktree's path, matching Claude Code's built-in behavior ("Passing--worktreea name whose directory already exists opens that existing worktree", worktrees docs).The fix covers both hook paths:
plugins/worktrunk/hooks/hooks.json: the command users run today. It checksgit rev-parse --verify --quiet refs/heads/$namebefore choosing the flag. It uses only commands every installedwtalready has, so it fixes the problem as soon as the marketplace serves the mergedmain.wt config plugins claude hook: the same check throughRepository::branch(..).exists_locally(), so the fix survives feat(plugin): run every Claude hook throughwt config plugins claude hook#4305's switch to the subcommand. feat(plugin): run every Claude hook throughwt config plugins claude hook#4305 rewrites the samehooks.jsonline, so whichever lands second has to resolve the conflict.test_claude_hook_worktree_createpreviously asserted that a second create with the same name fails. It now asserts that the second create reopens the same path. Before the fix it failed with the error from the issue. The failure path (no stdout and a nonzero exit, #3545) is still asserted, using a project dir that is not inside a repository.What wasn't verified: the run inside Claude Code itself. I ran the
hooks.jsoncommand by hand against a scratch repo withCLAUDE_PLUGIN_ROOT/CLAUDE_PROJECT_DIRset: create, reopen, an existing branch without a worktree, and a non-repo project dir. I did not test it underclaude --worktree.One behavior to weigh:
claude --worktree NAMEwhere NAME is a pre-existing branch the user made by hand now opens (or creates a worktree for) that branch rather than failing. A clean exit then firesWorktreeRemoveon it. That hook runswt removewithout force, so it refuses dirty worktrees and keeps unmerged branches (#2939).Closes #4486 — automated triage